Skip to content

Commit

Permalink
Merge pull request #433 from jsenv/migrate_workflow_packages
Browse files Browse the repository at this point in the history
Migrate workflow packages
  • Loading branch information
dmail authored Aug 13, 2024
2 parents 09ff0fe + 8248a9f commit b87dd52
Show file tree
Hide file tree
Showing 203 changed files with 21,258 additions and 23 deletions.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"workspaces": [
"./packages/independent/*",
"./packages/independent/workflow/*",
"./packages/internal/*",
"./packages/related/*",
"./packages/related/cli/*"
Expand Down Expand Up @@ -91,17 +92,17 @@
"@babel/plugin-syntax-import-attributes": "7.24.7",
"@babel/plugin-syntax-optional-chaining-assign": "7.24.7",
"@eslint/compat": "1.1.1",
"@jsenv/assert": "./packages/independent/assert/",
"@jsenv/cli": "./packages/related/cli/",
"@jsenv/assert": "workspace:*",
"@jsenv/cli": "workspace:*",
"@jsenv/core": "./",
"@jsenv/eslint-config": "./packages/independent/eslint-config/",
"@jsenv/file-size-impact": "14.2.1",
"@jsenv/eslint-config-relax": "workspace:*",
"@jsenv/file-size-impact": "workspace:*",
"@jsenv/https-local": "3.0.7",
"@jsenv/monorepo": "0.0.8",
"@jsenv/performance-impact": "4.3.1",
"@jsenv/plugin-as-js-classic": "./packages/related/plugin-as-js-classic/",
"@jsenv/snapshot": "./packages/independent/snapshot/",
"@jsenv/test": "./packages/related/test/",
"@jsenv/monorepo": "workspace:*",
"@jsenv/performance-impact": "workspace:*",
"@jsenv/plugin-as-js-classic": "workspace:*",
"@jsenv/snapshot": "workspace:*",
"@jsenv/test": "workspace:*",
"@playwright/browser-chromium": "1.46.0",
"@playwright/browser-firefox": "1.46.0",
"@playwright/browser-webkit": "1.46.0",
Expand Down
21 changes: 21 additions & 0 deletions packages/independent/dynamic-import-worker/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 jsenv

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions packages/independent/dynamic-import-worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Dynamic import worker

[![npm package](https://img.shields.io/npm/v/@jsenv/dynamic-import-worker.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/dynamic-import-worker)

Bypass node cache on dynamic import thanks to worker

# Example

_docs/demo/random_number.mjs_

```js
export const randomNumber = Math.random();
```

_docs/demo/demo.mjs_

```js
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

const randomNumberFileUrl = new URL(
"./random_number.mjs#randomNumber",
import.meta.url,
);

const randomNumberA = await importOneExportFromFile(randomNumberExportUrl);
const randomNumberB = await importOneExportFromFile(randomNumberExportUrl);

console.log(randomNumberA);
console.log(randomNumberB);
```

```console
> node ./docs/demo/demo.mjs
0.5362418125287491
0.35129949391010595
```
10 changes: 10 additions & 0 deletions packages/independent/dynamic-import-worker/docs/demo/demo.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

const randomNumberFileUrl = new URL("./random_number.mjs", import.meta.url);
const randomNumberExportUrl = `${randomNumberFileUrl}#randomNumber`;

const randomNumberA = await importOneExportFromFile(randomNumberExportUrl);
const randomNumberB = await importOneExportFromFile(randomNumberExportUrl);

console.log(randomNumberA);
console.log(randomNumberB);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const randomNumber = Math.random();
31 changes: 31 additions & 0 deletions packages/independent/dynamic-import-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@jsenv/dynamic-import-worker",
"version": "1.2.1",
"description": "Bypass node cache on dynamic import thanks to worker",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/jsenv/core",
"directory": "packages/independent/dynamic-import-worker"
},
"engines": {
"node": ">=20.0.0"
},
"publishConfig": {
"access": "public"
},
"type": "module",
"exports": {
".": {
"import": "./src/main.js"
},
"./*": "./*"
},
"files": [
"/src/"
],
"scripts": {
"test": "node ./scripts/test.mjs"
},
"dependencies": {}
}
12 changes: 12 additions & 0 deletions packages/independent/dynamic-import-worker/scripts/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { executeTestPlan, nodeWorkerThread } from "@jsenv/test";

await executeTestPlan({
rootDirectoryUrl: new URL("../", import.meta.url),
testPlan: {
"./tests/**/*.test.mjs": {
node: {
runtime: nodeWorkerThread(),
},
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Worker } from "node:worker_threads";
import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";

// we use a worker to bypass node cache on dynamic import
const WORKER_COLLECTING_ONE_EXPORT_FILE_URL = new URL(
"./worker_collecting_one_export.js",
import.meta.url,
);

export const importOneExportFromFile = async (
fileUrl,
{ env = process.env, params } = {},
) => {
const { hash, urlWithoutHash } = extractHashFromUrl(fileUrl);
if (!hash) {
throw new Error(`no hash found in fileUrl ${fileUrl}`);
}

const url = urlWithoutHash;
const exportName = hash.slice(1);

const exists = existsSync(fileURLToPath(new URL(url)));
if (!exists) {
throw new Error(`File not found at ${url}`);
}

const worker = new Worker(WORKER_COLLECTING_ONE_EXPORT_FILE_URL, {
env,
workerData: {
url,
exportName,
params,
},
});

let errorData;
let messageData;
await new Promise((resolve) => {
worker.once("message", (message) => {
messageData = message;
});
worker.once("error", (error) => {
errorData = error;
});
worker.once("exit", () => {
resolve();
});
});

if (errorData) {
const error = new Error(errorData.message);
error.name = errorData.name;
error.stack = errorData.stack;
throw error;
}

return messageData;
};

const extractHashFromUrl = (url) => {
const urlObject = new URL(url);
const { hash } = urlObject;
urlObject.hash = "";
const urlWithoutHash = String(urlObject);
return { hash, urlWithoutHash };
};
7 changes: 7 additions & 0 deletions packages/independent/dynamic-import-worker/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* This file is the first file executed by code using the package
* Its responsability is to export what is documented
* Ideally this file should be kept simple to help discovering codebase progressively.
*/

export { importOneExportFromFile } from "./import_one_export_from_file.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { parentPort, workerData } from "node:worker_threads";

const { url, exportName, params } = workerData;
const namespace = await import(url);

if (!Object.prototype.hasOwnProperty.call(namespace, exportName)) {
throw new Error(`No export named "${exportName}" in ${url}`);
}

const exportValue = namespace[exportName];
if (typeof exportValue === "function") {
const metrics = await exportValue(params);
parentPort.postMessage(metrics);
} else {
const metrics = exportValue;
parentPort.postMessage(metrics);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

{
const actual = await importOneExportFromFile(
`${new URL("./exporting_answer.mjs", import.meta.url)}#answer`,
);
const expect = 42;
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const answer = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

// export missing
try {
await importOneExportFromFile(
`${new URL("./exporting_toto.mjs", import.meta.url)}#answer`,
);
throw new Error("should throw");
} catch (e) {
const actual = e.message;
const expect = `No export named "answer" in ${new URL(
"./exporting_toto.mjs",
import.meta.url,
)}`;
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const toto = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

// file missing
try {
await importOneExportFromFile(
`${new URL("./toto.mjs", import.meta.url)}#answer`,
);
throw new Error("should throw");
} catch (e) {
const actual = e;
const expect = new Error(
`File not found at ${new URL("./toto.mjs", import.meta.url)}`,
);
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const get = (value) => {
return value;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

{
const withoutParams = await importOneExportFromFile(
`${new URL("./exporting_getter.mjs", import.meta.url)}#get`,
);
const withParam42 = await importOneExportFromFile(
`${new URL("./exporting_getter.mjs", import.meta.url)}#get`,
{
params: 42,
},
);
const actual = {
withoutParams,
withParam42,
};
const expect = {
withoutParams: undefined,
withParam42: 42,
};
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const answer = process.env.TOTO ? 42 : 43;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

{
const withoutEnv = await importOneExportFromFile(
`${new URL("./exporting_answer.mjs", import.meta.url)}#answer`,
);
const withTotoEnv = await importOneExportFromFile(
`${new URL("./exporting_answer.mjs", import.meta.url)}#answer`,
{
env: {
TOTO: "true",
},
},
);
const actual = {
withoutEnv,
withTotoEnv,
};
const expect = {
withoutEnv: 43,
withTotoEnv: 42,
};
assert({ actual, expect });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error("here");
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assert } from "@jsenv/assert";
import { importOneExportFromFile } from "@jsenv/dynamic-import-worker";

// runtime error
try {
await importOneExportFromFile(
`${new URL("./runtime_error.mjs", import.meta.url)}#answer`,
);
throw new Error("should throw");
} catch (e) {
const actual = e.message;
const expect = "here";
assert({ actual, expect });
}
8 changes: 8 additions & 0 deletions packages/independent/workflow/file-size-impact/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 14.0.0

- Rename reportFileSizeImpact into reportFileSizeImpactInGitHubPullRequest

# 13.0.0

- Rename fileSizeReportModulePath into fileSizeReportUrl
- Reduce number of decimals when file size is big
21 changes: 21 additions & 0 deletions packages/independent/workflow/file-size-impact/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 jsenv

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit b87dd52

Please sign in to comment.