Skip to content

Refactor/typescript #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate to typescript
mooyoul committed May 7, 2019

Verified

This commit was signed with the committer’s verified signature. The key has expired.
mooyoul MooYeol Prescott Lee
commit a29d93ebe2adbe00289cf3b8818241c9c55aea78
2 changes: 2 additions & 0 deletions packages/lambda/package.json
Original file line number Diff line number Diff line change
@@ -44,9 +44,11 @@
"upgrade-dependencies": "yarn upgrade-interactive --latest --exact"
},
"dependencies": {
"debug": "^4.1.1",
"extract-zip": "1.6.6"
},
"devDependencies": {
"@types/debug": "^4.1.4",
"@types/node": "^8.10.48",
"ava": "0.25.0",
"chrome-launcher": "0.10.2",
2 changes: 1 addition & 1 deletion packages/lambda/rollup.config.js
Original file line number Diff line number Diff line change
@@ -22,5 +22,5 @@ export default {
}),
typescript(),
],
external: ['fs', 'child_process', 'net', 'http', 'path', 'chrome-launcher'],
external: ['fs', 'child_process', 'net', 'path', 'chrome-launcher', 'debug'],
}
17 changes: 0 additions & 17 deletions packages/lambda/src/flags.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/lambda/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ async function getLocalChromePath() {

test.serial("Chrome should launch using LocalChromeLauncher", async (t) => {
const chromePath = await getLocalChromePath();
const chrome = launch({
const chrome = await launch({
flags: DEFAULT_TEST_FLAGS,
chromePath,
port: 9220,
@@ -30,7 +30,7 @@ test.serial("Chrome should launch using LocalChromeLauncher", async (t) => {
t.truthy(instance.port, "port should be set");
t.is(instance.port, 9220, "port should be 9220");

instance.kill();
await instance.kill();
});

// Covered by the integration-test.
64 changes: 25 additions & 39 deletions packages/lambda/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as fs from "fs";
import DEFAULT_CHROME_FLAGS from "./flags";
// import path from 'path'
import debug from "debug";
import LambdaChromeLauncher from "./launcher";
import { debug, processExists } from "./utils";

const DEVTOOLS_PORT = 9222;
const log = debug("@serverless-chrome/lambda");

const DEVTOOLS_HOST = "http://127.0.0.1";

// Prepend NSS related libraries and binaries to the library path and path respectively on lambda.
@@ -18,7 +16,7 @@ const DEVTOOLS_HOST = "http://127.0.0.1";

// persist the instance across invocations
// when the *lambda* container is reused.
let chromeInstance: any;
let chromeInstance: LambdaChromeLauncher | undefined;

export interface LaunchOption {
flags?: string[];
@@ -28,13 +26,11 @@ export interface LaunchOption {
}

export default async function launch({
flags = [],
flags: chromeFlags = [],
chromePath,
port = DEVTOOLS_PORT,
port,
forceLambdaLauncher = false,
}: LaunchOption = {}) {
const chromeFlags = [...DEFAULT_CHROME_FLAGS, ...flags];

if (!chromeInstance || !processExists(chromeInstance.pid!)) {
if (process.env.AWS_EXECUTION_ENV || forceLambdaLauncher) {
chromeInstance = new LambdaChromeLauncher({
@@ -44,49 +40,37 @@ export default async function launch({
});
} else {
// This let's us use chrome-launcher in local development,
// but omit it from the lambda function's zip artefact
// but omit it from the lambda function's zip artifact
try {
// eslint-disable-next-line
const { Launcher: LocalChromeLauncher } = require("chrome-launcher");
chromeInstance = new LocalChromeLauncher({
chromeInstance = (new LocalChromeLauncher({
chromePath,
chromeFlags: flags,
chromeFlags,
port,
});
})) as LambdaChromeLauncher;
} catch (error) {
throw new Error('@serverless-chrome/lambda: Unable to find "chrome-launcher". ' +
"Make sure it's installed if you wish to develop locally.");
}
}
}

debug("Spawning headless shell");
log("Spawning headless shell");

const launchStartTime = Date.now();

try {
await chromeInstance.launch();
} catch (error) {
debug("Error trying to spawn chrome:", error);

if (process.env.DEBUG) {
debug(
"stdout log:",
fs.readFileSync(`${chromeInstance.userDataDir}/chrome-out.log`, "utf8"),
);
debug(
"stderr log:",
fs.readFileSync(`${chromeInstance.userDataDir}/chrome-err.log`, "utf8"),
);
}
log("Error trying to spawn chrome:", error);

throw new Error("Unable to start Chrome. If you have the DEBUG env variable set," +
"there will be more in the logs.");
}

const launchTime = Date.now() - launchStartTime;

debug(`It took ${launchTime}ms to spawn chrome.`);
log("It took %dms to spawn chrome.", launchTime);

// unref the chrome instance, otherwise the lambda process won't end correctly
/* @TODO: make this an option?
@@ -96,29 +80,31 @@ export default async function launch({
without unreffing chrome.
*/
if (chromeInstance.chrome) {
chromeInstance.chrome.removeAllListeners();
chromeInstance.chrome.unref();
}

return {
pid: chromeInstance.pid,
port: chromeInstance.port,
url: `${DEVTOOLS_HOST}:${chromeInstance.port}`,
log: `${chromeInstance.userDataDir}/chrome-out.log`,
errorLog: `${chromeInstance.userDataDir}/chrome-err.log`,
pidFile: `${chromeInstance.userDataDir}/chrome.pid`,
metaData: {
launchTime,
didLaunch: !!chromeInstance.pid,
},
async kill() {
// Defer killing chrome process to the end of the execution stack
// so that the node process doesn't end before chrome exists,
// avoiding chrome becoming orphaned.
setTimeout(async () => {
chromeInstance.kill();
if (chromeInstance) {
await chromeInstance.kill();
chromeInstance = undefined;
}, 0);
}
},
};
}

function processExists(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch (error) {
return false;
}
}
366 changes: 161 additions & 205 deletions packages/lambda/src/launcher.ts

Large diffs are not rendered by default.

Empty file removed packages/lambda/src/launcher2.ts
Empty file.
43 changes: 0 additions & 43 deletions packages/lambda/src/utils.ts

This file was deleted.

5 changes: 1 addition & 4 deletions packages/lambda/tsconfig.json
Original file line number Diff line number Diff line change
@@ -6,8 +6,5 @@
"declaration": true,
"sourceMap": true,
"strictNullChecks": true
},
"include": [
"src/*.ts"
]
}
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "es2015",
"target": "es2017",
"noImplicitAny": true,
"declaration": true,
"sourceMap": true,
"strictNullChecks": true
},
"include": [
"src/*.ts"
]
}