Skip to content

Commit

Permalink
Introduce synckit for speed-up checkSync
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Mar 6, 2023
1 parent d2c07e8 commit f3a8bcb
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/recheck/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
"clean": "rimraf lib",
"format": "prettier -w '*.{js,json,md}' '{scripts,src}/**/*.{js,ts}' index.d.ts",
"lint": "prettier --check '*.{js,json,md}' '{scripts,src}/**/*.{js,ts}' index.d.ts",
"test": "jest",
"test": "SYNCKIT_TS_RUNNER=esbuild-register jest",
"typecheck": "tsc --noEmit -p ."
},
"sideEffects": false,
"types": "index.d.ts",
"dependencies": {
"synckit": "0.8.5"
},
"optionalDependencies": {
"recheck-jar": "0.0.0",
"recheck-linux-x64": "0.0.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/recheck/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ const main = async () => {
plugins: [makeAllPackagesExternalPlugin, inlineWorkerPlugin],
outfile: "lib/browser.js",
});
await esbuild({
entryPoints: ["src/synckit-worker.ts"],
bundle: false,
minify: isProduction,
format: "cjs",
target: "es2016",
logLevel: "error",
platform: "node",
plugins: [makeAllPackagesExternalPlugin],
outfile: "lib/synckit-worker.js",
});
};

main().catch((err) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/recheck/src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export type Backend = "auto" | "java" | "native" | "worker" | "pure";
export type SyncBackend = "synckit" | "pure";

/** Returns `RECHECK_BACKEND` environment variable value, or `'auto'` as the default. */
export const RECHECK_BACKEND: () => Backend = () =>
(process.env["RECHECK_BACKEND"] as Backend) || "auto";

/** Returns `RECHECK_SYNC_BACKEND` environment variable value, or `'synckit'` as the default. */
export const RECHECK_SYNC_BACKEND: () => SyncBackend = () =>
(process.env["RECHECK_SYNC_BACKEND"] as SyncBackend) || "synckit";

/** Returns `RECHECK_JAR` environment variable value, or `null` as the default. */
export const RECHECK_JAR: () => string | null = () =>
process.env["RECHECK_JAR"] || null;
Expand Down
16 changes: 15 additions & 1 deletion packages/recheck/src/lib/exe.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as exe from "./exe";

const isSupported =
typeof exe.osNames[process.platform] !== "undefined" &&
typeof exe.cpuNames[process.arch] !== "undefined";

beforeEach(() => {
delete process.env["RECHECK_BIN"];
delete process.env["RECHECK_JAR"];
Expand Down Expand Up @@ -39,13 +43,19 @@ test("jar: invalid resolve (2)", () => {
});

test("bin", () => {
expect(exe.bin()).toMatch(/recheck-\w+-\w+\/recheck(?:\.exe)?$/);
if (isSupported) {
expect(exe.bin()).toMatch(/recheck-\w+-\w+\/recheck(?:\.exe)?$/);
}

process.env["RECHECK_BIN"] = "x";
expect(exe.bin()).toBe("x");
});

test("bin: invalid resolve (1)", () => {
if (!isSupported) {
return;
}

const resolve = jest.spyOn(exe.__mock__require, "resolve");
resolve.mockImplementationOnce(() => {
const err: any = new Error("module not found");
Expand All @@ -57,6 +67,10 @@ test("bin: invalid resolve (1)", () => {
});

test("bin: invalid resolve (2)", () => {
if (!isSupported) {
return;
}

const resolve = jest.spyOn(exe.__mock__require, "resolve");
resolve.mockImplementationOnce(() => {
const err: any = new Error("unknown error");
Expand Down
4 changes: 2 additions & 2 deletions packages/recheck/src/lib/exe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export const jar: () => string | null = () => {
};

/** A mapping from a supported platform (OS) name to the corresponding package name component. */
const osNames: Record<string, string> = {
export const osNames: Record<string, string> = {
darwin: "macos",
linux: "linux",
win32: "windows",
};

/** A mapping from a supported architecture (CPU) name to the corresponding package name component. */
const cpuNames: Record<string, string> = {
export const cpuNames: Record<string, string> = {
x64: "x64",
};

Expand Down
22 changes: 21 additions & 1 deletion packages/recheck/src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,27 @@ test("check: invalid", async () => {
);
});

test("checkSync", () => {
test("checkSync: synckit", () => {
const backend = jest.spyOn(env, "RECHECK_SYNC_BACKEND");
backend.mockReturnValueOnce("synckit" as any);

const diagnostics = main.checkSync("^(a|a)+$", "");
expect(diagnostics.status).toBe("vulnerable");
});

test("checkSync: pure", () => {
const backend = jest.spyOn(env, "RECHECK_SYNC_BACKEND");
backend.mockReturnValueOnce("pure" as any);

const diagnostics = main.checkSync("^(a|a)+$", "");
expect(diagnostics.status).toBe("vulnerable");
});

test("checkSync: invalid", () => {
const backend = jest.spyOn(env, "RECHECK_SYNC_BACKEND");
backend.mockReturnValueOnce("invalid" as any);

expect(() => main.checkSync("^(a|a)+$", "")).toThrowError(
"invalid sync backend: invalid"
);
});
28 changes: 27 additions & 1 deletion packages/recheck/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createSyncFn } from "synckit";

import { check as checkAgent } from "./lib/agent";
import * as env from "./lib/env";
import * as java from "./lib/java";
Expand Down Expand Up @@ -85,4 +87,28 @@ export async function check(
return checkAgent(agent, source, flags, params);
}

export const checkSync = pure.check;
let syncFnCache: typeof pure.check | null = null;

export const checkSync = (
source: string,
flags: string,
params: Parameters = {}
): Diagnostics => {
let syncFn: typeof pure.check | null = null;
const backend = env.RECHECK_SYNC_BACKEND();
switch (backend) {
case "synckit":
if (syncFnCache === null) {
syncFnCache = createSyncFn(require.resolve("./synckit-worker"));
}
syncFn = syncFnCache;
case "pure":
syncFn = pure.check;
break;
}
if (syncFn === null) {
throw new Error(`invalid sync backend: ${backend}`);
}

return syncFn(source, flags, params);
};
14 changes: 14 additions & 0 deletions packages/recheck/src/synckit-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { runAsWorker } from "synckit";

import { check } from "./main";
import type { Diagnostics, Parameters } from "..";

runAsWorker(
async (
source: string,
flags: string,
params: Parameters
): Promise<Diagnostics> => {
return check(source, flags, params);
}
);
40 changes: 39 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,18 @@
dependencies:
esquery "^1.0.1"

"@pkgr/utils@^2.3.1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@pkgr/utils/-/utils-2.3.1.tgz#0a9b06ffddee364d6642b3cd562ca76f55b34a03"
integrity sha512-wfzX8kc1PMyUILA+1Z/EqoE4UCXGy0iRGMhPwdfae1+f0OXlLqCk+By+aMzgJBzR9AzS4CDizioG6Ss1gvAFJw==
dependencies:
cross-spawn "^7.0.3"
is-glob "^4.0.3"
open "^8.4.0"
picocolors "^1.0.0"
tiny-glob "^0.2.9"
tslib "^2.4.0"

"@polka/url@^1.0.0-next.20":
version "1.0.0-next.21"
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.21.tgz#5de5a2385a35309427f6011992b544514d559aa1"
Expand Down Expand Up @@ -6440,6 +6452,11 @@ globals@^13.19.0:
dependencies:
type-fest "^0.20.2"

[email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.0.tgz#cb76da79555669a1519d5a8edf093afaa0bf1465"
integrity sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==

[email protected], globby@^11.0.1, globby@^11.0.4, globby@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
Expand All @@ -6463,6 +6480,11 @@ globby@^13.1.1:
merge2 "^1.4.1"
slash "^4.0.0"

globrex@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/globrex/-/globrex-0.1.2.tgz#dd5d9ec826232730cd6793a5e33a9302985e6098"
integrity sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==

got@^9.6.0:
version "9.6.0"
resolved "https://registry.yarnpkg.com/got/-/got-9.6.0.tgz#edf45e7d67f99545705de1f7bbeeeb121765ed85"
Expand Down Expand Up @@ -11783,6 +11805,14 @@ svgo@^2.7.0, svgo@^2.8.0:
picocolors "^1.0.0"
stable "^0.1.8"

[email protected]:
version "0.8.5"
resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.5.tgz#b7f4358f9bb559437f9f167eb6bc46b3c9818fa3"
integrity sha512-L1dapNV6vu2s/4Sputv8xGsCdAVlb5nRDMFU/E27D44l5U6cw1g0dGd45uLc+OXjNMmF4ntiMdCimzcjFKQI8Q==
dependencies:
"@pkgr/utils" "^2.3.1"
tslib "^2.5.0"

tapable@^1.0.0:
version "1.1.3"
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
Expand Down Expand Up @@ -11908,6 +11938,14 @@ thunky@^1.0.2:
resolved "https://registry.yarnpkg.com/thunky/-/thunky-1.1.0.tgz#5abaf714a9405db0504732bbccd2cedd9ef9537d"
integrity sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==

tiny-glob@^0.2.9:
version "0.2.9"
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.9.tgz#2212d441ac17928033b110f8b3640683129d31e2"
integrity sha512-g/55ssRPUjShh+xkfx9UPDXqhckHEsHr4Vd9zX55oSdGZc/MD0m3sferOkwWtp98bv+kcVfEHtRJgBVJzelrzg==
dependencies:
globalyzer "0.1.0"
globrex "^0.1.2"

tiny-invariant@^1.0.2:
version "1.3.1"
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.3.1.tgz#8560808c916ef02ecfd55e66090df23a4b7aa642"
Expand Down Expand Up @@ -12028,7 +12066,7 @@ tsconfig-paths@^4.1.2:
minimist "^1.2.6"
strip-bom "^3.0.0"

tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.4.0:
tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.4.0, tslib@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf"
integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==
Expand Down

0 comments on commit f3a8bcb

Please sign in to comment.