Skip to content

Commit

Permalink
Add prettier and eslint configs
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzyla committed Sep 1, 2023
1 parent b458071 commit af5d0b0
Show file tree
Hide file tree
Showing 8 changed files with 2,281 additions and 2,887 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/update-pdfium.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
- name: Check for updates and create PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npm run check-updates
run: node ./scripts/check-updates.mjs
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"build": "rm -rf dist && tsc && cp -r src/vendor dist",
"clean": "rm -rf dist",
"check-updates": "node ./scripts/check-updates.mjs"
"lint": "eslint ./src",
"format": "prettier ./src --write"
},
"repository": {
"type": "git",
Expand All @@ -29,11 +30,43 @@
"@typescript-eslint/parser": "^6.5.0",
"axios": "^1.4.0",
"eslint": "^8.48.0",
"prettier": "^3.0.3",
"tar": "^6.1.15",
"ts-node": "^10.9.1",
"unzipper": "^0.10.14"
},
"publishConfig": {
"access": "public"
},
"========== Tools ==========": "",
"eslintConfig": {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {},
"ignorePatterns": [
"src/vendor/**/*"
]
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 120
}
}
111 changes: 54 additions & 57 deletions scripts/check-updates.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Octokit } from "@octokit/rest";
import { execSync } from "child_process";
import { promises as fs } from "fs";
import axios from "axios";
import { createGunzip } from "zlib";
import { extract, t } from "tar";
import { pipeline as _pipeline } from "stream";
import { promisify } from "util";
import { Octokit } from '@octokit/rest';
import { execSync } from 'child_process';
import { promises as fs } from 'fs';
import axios from 'axios';
import { createGunzip } from 'zlib';
import { extract, t } from 'tar';
import { pipeline as _pipeline } from 'stream';
import { promisify } from 'util';

const pipeline = promisify(_pipeline);

Expand All @@ -16,25 +16,46 @@ const octokit = new Octokit({
auth: GITHUB_TOKEN,
});

const VERSION_FILE = 'src/vendor/LAST_RELEASE.txt';

/**
* Check whether branch exists
*/
async function isBranchExists(branchName) {
try {
const { data: branch } = await octokit.repos.getBranch({
owner: 'hyzyla',
repo: 'pdfium',
branch: branchName,
request: {
validate: false,
},
});
if (branch) {
return true;
}
} catch (e) {
if (e.status !== 404) throw e;
}
return false;
}

async function checkForUpdates() {
// Get latest release
const { data: lastRelease } = await octokit.repos.getLatestRelease({
owner: "paulocoutinhox",
repo: "pdfium-lib",
owner: 'paulocoutinhox',
repo: 'pdfium-lib',
});
const lastReleaseTag = lastRelease.tag_name;

console.info(`Got latest release: "${lastReleaseTag}"`);

const lastCheckedReleaseFile = await fs.readFile(
"src/vendor/LAST_RELEASE.txt",
"utf-8"
);
const lastCheckedReleaseFile = await fs.readFile(VERSION_FILE, 'utf-8');
const lastCheckedReleaseTag = lastCheckedReleaseFile.trim();
console.log(`Last checked release: "${lastCheckedReleaseTag}"`);

if (lastReleaseTag === lastCheckedReleaseTag) {
console.log("No new release found", lastReleaseTag, lastCheckedReleaseTag);
console.log('No new release found', lastReleaseTag, lastCheckedReleaseTag);
return;
}

Expand All @@ -44,77 +65,53 @@ async function checkForUpdates() {
execSync(`git switch -c ${branchName}`);

// check if branch exists
try {
const { data: branch } = await octokit.repos.getBranch({
owner: "hyzyla",
repo: "pdfium",
branch: branchName,
request: {
validate: false,
},
});
if (branch) {
console.log(`Branch ${branchName} already exists`);
return;
}
} catch (e) {
if (e.status !== 404) throw e;
if (await isBranchExists(branchName)) {
console.log(`Branch ${branchName} already exists`);
return;
}

try {
// Download wasm asset
const wasmAssetUrl = lastRelease.assets.find(
(asset) => asset.name === "wasm.tgz"
).browser_download_url;
const wasmAssetUrl = lastRelease.assets.find((asset) => asset.name === 'wasm.tgz').browser_download_url;
console.log(`Downloading wasm asset: ${wasmAssetUrl}`);

const response = await axios({
url: wasmAssetUrl,
method: "GET",
responseType: "stream",
method: 'GET',
responseType: 'stream',
});
console.log(`Downloaded wasm asset: ${response.status}`);

// Unzip archive
await pipeline(
response.data,
createGunzip(),
extract({ cwd: "src/vendor" })
);
console.log("Unzipped wasm asset to src/vendor folder");
await pipeline(response.data, createGunzip(), extract({ cwd: 'src/vendor' }));
console.log('Unzipped wasm asset to src/vendor folder');

// Copy files
await fs.copyFile(
"src/vendor/release/node/pdfium.js",
"src/vendor/pdfium.js"
);
await fs.copyFile(
"src/vendor/release/node/pdfium.wasm",
"src/vendor/pdfium.wasm"
);
console.log("Copied files to src/vendor folder");
await fs.copyFile('src/vendor/release/node/pdfium.js', 'src/vendor/pdfium.js');
await fs.copyFile('src/vendor/release/node/pdfium.wasm', 'src/vendor/pdfium.wasm');
console.log('Copied files to src/vendor folder');

execSync(`npx prettier --write src/vendor`);
console.log("Formatted files");
console.log('Formatted files');

await fs.writeFile("src/vendor/LAST_RELEASE.txt", lastReleaseTag);
await fs.writeFile('src/vendor/LAST_RELEASE.txt', lastReleaseTag);

execSync(`git commit -am "Update PDFium"`);
execSync(`git push origin ${branchName}`);

await octokit.pulls.create({
owner: "hyzyla",
repo: "pdfium",
owner: 'hyzyla',
repo: 'pdfium',
title: `Update to ${lastReleaseTag}`,
head: branchName,
base: "main",
base: 'main',
body: `Update PDFium from ${lastCheckedReleaseTag} to ${lastReleaseTag} version
https://github.com/paulocoutinhox/pdfium-lib/releases/tag/${lastReleaseTag}`,
});
console.log("Created pull request");
console.log('Created pull request');
} finally {
// Remove archive folder
await fs.rm("src/vendor/release", {
await fs.rm('src/vendor/release', {
recursive: true,
force: true,
});
Expand Down
14 changes: 4 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sharp from "sharp";
import sharp from 'sharp';

import pdfium from "./vendor/pdfium";
import pdfium from './vendor/pdfium';

const BYTES_PER_PIXEL = 4;

Expand Down Expand Up @@ -91,7 +91,7 @@ export class PDFiumLibrary {
this.module = module;
}

async loadDocument(buff: Buffer, password: string = "") {
async loadDocument(buff: Buffer, password: string = '') {
const size = buff.length;

const ptr = this.module.asm.malloc(size);
Expand Down Expand Up @@ -148,13 +148,7 @@ export class PDFiumDocument {
const ptr = this.module.asm.malloc(buffSize);
this.module.HEAPU8.fill(0, ptr, ptr + buffSize);

const bitmap = this.module._FPDFBitmap_CreateEx(
width,
height,
FPDFBitmap.BGRA,
ptr,
width * BYTES_PER_PIXEL
);
const bitmap = this.module._FPDFBitmap_CreateEx(width, height, FPDFBitmap.BGRA, ptr, width * BYTES_PER_PIXEL);
this.module._FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xffffffff);
this.module._FPDF_RenderPageBitmap(
bitmap,
Expand Down
19 changes: 3 additions & 16 deletions src/vendor/pdfium.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@ declare type PDFium = {
_FPDF_GetPageWidth: (page: any) => number;
_FPDF_GetPageHeight: (page: any) => number;
_FPDF_ClosePage: (page: any) => void;
_FPDFBitmap_CreateEx: (
width: number,
height: number,
format: number,
ptr: number,
stride: number,
) => any;
_FPDFBitmap_FillRect: (
bitmap: any,
left: number,
top: number,
width: number,
height: number,
color: number,
) => void;
_FPDFBitmap_CreateEx: (width: number, height: number, format: number, ptr: number, stride: number) => any;
_FPDFBitmap_FillRect: (bitmap: any, left: number, top: number, width: number, height: number, color: number) => void;
_FPDF_RenderPageBitmap: (
bitmap: any,
page: any,
Expand All @@ -31,7 +18,7 @@ declare type PDFium = {
size_x: number,
size_y: number,
rotate: number,
flags: number,
flags: number
) => void;
_FPDFBitmap_Destroy: (bitmap: any) => void;
asm: {
Expand Down
Loading

0 comments on commit af5d0b0

Please sign in to comment.