Skip to content

Commit

Permalink
Allow to configure process.env and function params for importOneExpor…
Browse files Browse the repository at this point in the history
…tFromFile
  • Loading branch information
dmail committed Nov 20, 2023
1 parent 243dff9 commit 0dbe40a
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/dynamic-import-worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/dynamic-import-worker",
"version": "1.1.0",
"version": "1.2.0",
"description": "Bypass node cache on dynamic import thanks to worker",
"license": "MIT",
"author": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const WORKER_COLLECTING_ONE_EXPORT_FILE_URL = new URL(
import.meta.url,
);

export const importOneExportFromFile = async (fileUrl) => {
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}`);
Expand All @@ -23,9 +26,11 @@ export const importOneExportFromFile = async (fileUrl) => {
}

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

Expand Down
11 changes: 11 additions & 0 deletions packages/dynamic-import-worker/tests/basic/basic.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 expected = 42;
assert({ actual, expected });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 expected = `No export named "answer" in ${new URL(
"./exporting_toto.mjs",
import.meta.url,
)}`;
assert({ actual, expected });
}
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";

// file missing
try {
await importOneExportFromFile(
`${new URL("./toto.mjs", import.meta.url)}#answer`,
);
throw new Error("should throw");
} catch (e) {
const actual = e;
const expected = new Error(
`File not found at ${new URL("./toto.mjs", import.meta.url)}`,
);
assert({ actual, expected });
}

This file was deleted.

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,24 @@
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 expected = {
withoutParams: undefined,
withParam42: 42,
};
assert({ actual, expected });
}
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,26 @@
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 expected = {
withoutEnv: 43,
withTotoEnv: 42,
};
assert({ actual, expected });
}
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";

// 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 expected = "here";
assert({ actual, expected });
}

{
const actual = await importOneExportFromFile(
`${new URL("./exporting_answer.mjs", import.meta.url)}#answer`,
);
const expected = 42;
assert({ actual, expected });
}

0 comments on commit 0dbe40a

Please sign in to comment.