Merge pull request #30 from Sparticuz/fix/cleanup

This commit is contained in:
Kyle McNally 2022-12-30 11:39:57 -05:00 committed by GitHub
commit b286dc3fc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 44 deletions

View File

@ -13,7 +13,7 @@
"build" "build"
], ],
"engines": { "engines": {
"node": ">= 14" "node": ">= 14.18.0"
}, },
"scripts": { "scripts": {
"test": "make clean && make && make pretest && make test", "test": "make clean && make && make pretest && make test",
@ -23,10 +23,10 @@
"preversion": "npm run build" "preversion": "npm run build"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.62", "@types/node": "^18.11.18",
"@types/tar-fs": "^2.0.1", "@types/tar-fs": "^2.0.1",
"clean-modules": "^2.0.6", "clean-modules": "^2.0.6",
"typescript": "^4.8.3" "typescript": "^4.9.4"
}, },
"bugs": { "bugs": {
"url": "https://github.com/Sparticuz/chromium/issues" "url": "https://github.com/Sparticuz/chromium/issues"

View File

@ -1,8 +1,8 @@
import { access, createWriteStream, existsSync, mkdirSync, symlink } from 'fs'; import { access, createWriteStream, existsSync, mkdirSync, symlink } from 'node:fs';
import { IncomingMessage } from 'http'; import { IncomingMessage } from 'node:http';
import LambdaFS from './lambdafs'; import LambdaFS from './lambdafs';
import { join } from 'path'; import { join } from 'node:path';
import { URL } from 'url'; import { URL } from 'node:url';
/** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */ /** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */
interface Viewport { interface Viewport {
@ -37,7 +37,7 @@ interface Viewport {
hasTouch?: boolean; hasTouch?: boolean;
} }
if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { if ( process.env.AWS_EXECUTION_ENV !== undefined && /^AWS_Lambda_nodejs(?:14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) {
if (process.env.FONTCONFIG_PATH === undefined) { if (process.env.FONTCONFIG_PATH === undefined) {
process.env.FONTCONFIG_PATH = '/tmp/aws'; process.env.FONTCONFIG_PATH = '/tmp/aws';
} }
@ -52,11 +52,13 @@ if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_E
class Chromium { class Chromium {
/** /**
* Downloads or symlinks a custom font and returns its basename, patching the environment so that Chromium can find it. * Downloads or symlinks a custom font and returns its basename, patching the environment so that Chromium can find it.
* If not running on AWS Lambda nor Google Cloud Functions, `null` is returned instead. * If headless is not true, `null` is returned instead.
*/ */
static font(input: string): Promise<string> { static font(input: string): Promise<string | null> {
if (Chromium.headless !== true) { if (Chromium.headless !== true) {
return null; return new Promise((resolve) => {
return resolve(null);
});
} }
if (process.env.HOME === undefined) { if (process.env.HOME === undefined) {
@ -76,7 +78,7 @@ class Chromium {
const output = `${process.env.HOME}/.fonts/${url.pathname.split('/').pop()}`; const output = `${process.env.HOME}/.fonts/${url.pathname.split('/').pop()}`;
if (existsSync(output) === true) { if (existsSync(output) === true) {
return resolve(output.split('/').pop()); return resolve(output.split('/').pop() as string);
} }
if (url.protocol === 'file:') { if (url.protocol === 'file:') {
@ -86,7 +88,7 @@ class Chromium {
} }
symlink(url.pathname, output, (error) => { symlink(url.pathname, output, (error) => {
return error != null ? reject(error) : resolve(url.pathname.split('/').pop()); return error != null ? reject(error) : resolve(url.pathname.split('/').pop() as string);
}); });
}); });
} else { } else {
@ -109,7 +111,7 @@ class Chromium {
response.once('end', () => { response.once('end', () => {
stream.end(() => { stream.end(() => {
return resolve(url.pathname.split('/').pop()); return resolve(url.pathname.split('/').pop() as string);
}); });
}); });
}); });
@ -208,12 +210,12 @@ class Chromium {
LambdaFS.inflate(`${input}/swiftshader.tar.br`), LambdaFS.inflate(`${input}/swiftshader.tar.br`),
]; ];
if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { if (process.env.AWS_EXECUTION_ENV !== undefined && /^AWS_Lambda_nodejs(?:14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) {
promises.push(LambdaFS.inflate(`${input}/aws.tar.br`)); promises.push(LambdaFS.inflate(`${input}/aws.tar.br`));
} }
const result = await Promise.all(promises); const result = await Promise.all(promises);
return result.shift(); return result.shift() as string;
} }
/** /**

View File

@ -1,8 +1,8 @@
import { createReadStream, createWriteStream, existsSync } from 'fs'; import { createReadStream, createWriteStream, existsSync } from 'node:fs';
import { tmpdir } from 'os'; import { tmpdir } from 'node:os';
import { basename, join } from 'path'; import { basename, join } from 'node:path';
import { extract } from 'tar-fs'; import { extract } from 'tar-fs';
import { createBrotliDecompress, createUnzip } from 'zlib'; import { createBrotliDecompress, createUnzip } from 'node:zlib';
class LambdaFS { class LambdaFS {
/** /**

View File

@ -1,41 +1,18 @@
{ {
"compileOnSave": false,
"compilerOptions": { "compilerOptions": {
"alwaysStrict": true,
"declaration": true, "declaration": true,
"declarationDir": "build", "declarationDir": "build",
"emitDecoratorMetadata": true,
"esModuleInterop": true, "esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"importHelpers": false,
"inlineSourceMap": false,
"lib": ["dom", "es2020"], "lib": ["dom", "es2020"],
"listEmittedFiles": false,
"listFiles": false,
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"outDir": "build", "outDir": "build",
"pretty": true, "pretty": true,
"resolveJsonModule": true,
"skipLibCheck": true, "skipLibCheck": true,
"sourceMap": true,
"strict": true, "strict": true,
"strictFunctionTypes": true,
"strictNullChecks": false,
"target": "es2020", "target": "es2020",
"traceResolution": false,
"types": ["node"]
}, },
"exclude": ["build", "node_modules", "tmp"], "exclude": ["build", "node_modules", "tmp"],
"include": ["source"], "include": ["source"]
"typeAcquisition": {
"enable": true
}
} }