@ms-cloudpack/esm-stub-utilities 0.9.0 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generateESMStubFromJSON.d.ts +5 -3
- package/lib/generateESMStubFromJSON.d.ts.map +1 -1
- package/lib/generateESMStubFromJSON.js +7 -4
- package/lib/generateESMStubFromJSON.js.map +1 -1
- package/lib/processStubsInWorker.d.ts +5 -2
- package/lib/processStubsInWorker.d.ts.map +1 -1
- package/lib/processStubsInWorker.js +15 -11
- package/lib/processStubsInWorker.js.map +1 -1
- package/lib/types/WriteESMStubsResult.d.ts +7 -1
- package/lib/types/WriteESMStubsResult.d.ts.map +1 -1
- package/lib/types/WriteESMStubsResult.js.map +1 -1
- package/lib/worker/initBrowserEnvironment.d.ts.map +1 -1
- package/lib/worker/initBrowserEnvironment.js +1 -0
- package/lib/worker/initBrowserEnvironment.js.map +1 -1
- package/lib/worker/workerEntry.d.ts.map +1 -1
- package/lib/worker/workerEntry.js +8 -13
- package/lib/worker/workerEntry.js.map +1 -1
- package/lib/writeESMStubsInternal.d.ts.map +1 -1
- package/lib/writeESMStubsInternal.js +11 -13
- package/lib/writeESMStubsInternal.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Generates an ESM stub of a JSON file. This just inlines the file contents
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Generates an ESM stub of a JSON file. This just inlines the file contents, with a named export
|
|
3
|
+
* for each top-level object property that's a valid identifier.
|
|
4
|
+
*
|
|
5
|
+
* Throws an error if the file is not valid JSON.
|
|
5
6
|
*/
|
|
6
7
|
export declare function generateESMStubFromJSON(options: {
|
|
8
|
+
/** The full path to the JSON entry file. */
|
|
7
9
|
filePath: string;
|
|
8
10
|
}): Promise<string>;
|
|
9
11
|
//# sourceMappingURL=generateESMStubFromJSON.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromJSON.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromJSON.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"generateESMStubFromJSON.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromJSON.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAsB,uBAAuB,CAAC,OAAO,EAAE;IACrD,4CAA4C;IAC5C,QAAQ,EAAE,MAAM,CAAC;CAClB,GAAG,OAAO,CAAC,MAAM,CAAC,CAsBlB"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { readJson } from '@ms-cloudpack/json-utilities';
|
|
2
2
|
import { isValidIdentifierName } from './isValidIdentifierName.js';
|
|
3
3
|
/**
|
|
4
|
-
* Generates an ESM stub of a JSON file. This just inlines the file contents
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Generates an ESM stub of a JSON file. This just inlines the file contents, with a named export
|
|
5
|
+
* for each top-level object property that's a valid identifier.
|
|
6
|
+
*
|
|
7
|
+
* Throws an error if the file is not valid JSON.
|
|
7
8
|
*/
|
|
8
9
|
export async function generateESMStubFromJSON(options) {
|
|
9
10
|
const { filePath } = options;
|
|
10
11
|
const jsonData = await readJson(filePath);
|
|
11
12
|
if (!jsonData) {
|
|
12
|
-
|
|
13
|
+
// This will be caught by the parent, and the file path is available from the context
|
|
14
|
+
// and will be included in the processed error.
|
|
15
|
+
throw new Error('File is not valid JSON');
|
|
13
16
|
}
|
|
14
17
|
const result = [`const data = ${JSON.stringify(jsonData, null, 2)};`, 'export default data;'];
|
|
15
18
|
if (jsonData && typeof jsonData === 'object' && !Array.isArray(jsonData)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromJSON.js","sourceRoot":"","sources":["../src/generateESMStubFromJSON.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAEnE
|
|
1
|
+
{"version":3,"file":"generateESMStubFromJSON.js","sourceRoot":"","sources":["../src/generateESMStubFromJSON.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAEnE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAC,OAG7C;IACC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAE1C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,qFAAqF;QACrF,+CAA+C;QAC/C,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,MAAM,GAAG,CAAC,gBAAgB,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,sBAAsB,CAAC,CAAC;IAE9F,IAAI,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzE,iEAAiE;QACjE,MAAM,CAAC,IAAI,CACT,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;aACrB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC;aAC7C,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,gBAAgB,IAAI,WAAW,IAAI,GAAG,CAAC,CACzD,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import { readJson } from '@ms-cloudpack/json-utilities';\nimport { isValidIdentifierName } from './isValidIdentifierName.js';\n\n/**\n * Generates an ESM stub of a JSON file. This just inlines the file contents, with a named export\n * for each top-level object property that's a valid identifier.\n *\n * Throws an error if the file is not valid JSON.\n */\nexport async function generateESMStubFromJSON(options: {\n /** The full path to the JSON entry file. */\n filePath: string;\n}): Promise<string> {\n const { filePath } = options;\n const jsonData = await readJson(filePath);\n\n if (!jsonData) {\n // This will be caught by the parent, and the file path is available from the context\n // and will be included in the processed error.\n throw new Error('File is not valid JSON');\n }\n\n const result = [`const data = ${JSON.stringify(jsonData, null, 2)};`, 'export default data;'];\n\n if (jsonData && typeof jsonData === 'object' && !Array.isArray(jsonData)) {\n // Export individual properties if they're valid identifier names\n result.push(\n ...Object.keys(jsonData)\n .filter((name) => isValidIdentifierName(name))\n .map((prop) => `export const ${prop} = data.${prop};`),\n );\n }\n\n return result.join('\\n');\n}\n"]}
|
|
@@ -4,6 +4,9 @@ import type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';
|
|
|
4
4
|
* Generates ESM stubs for CommonJS and JSON modules.
|
|
5
5
|
*/
|
|
6
6
|
export declare function processStubsInWorker(options: WriteESMStubsOptions): Promise<WriteESMStubsResult>;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Stop the worker for testing.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function _stopStubWorker(): Promise<void>;
|
|
9
12
|
//# sourceMappingURL=processStubsInWorker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processStubsInWorker.d.ts","sourceRoot":"","sources":["../src/processStubsInWorker.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAU1E;;GAEG;AACH,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,
|
|
1
|
+
{"version":3,"file":"processStubsInWorker.d.ts","sourceRoot":"","sources":["../src/processStubsInWorker.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAU1E;;GAEG;AACH,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAoFtG;AAED;;;GAGG;AACH,wBAAsB,eAAe,kBAGpC"}
|
|
@@ -16,10 +16,10 @@ export async function processStubsInWorker(options) {
|
|
|
16
16
|
try {
|
|
17
17
|
worker = new Worker(workerPath, { stdout: true, stderr: true, execArgv: [] });
|
|
18
18
|
worker.stdout?.on('data', (data) => {
|
|
19
|
-
console.debug(`[
|
|
19
|
+
console.debug(`[worker stdout] ${data}`);
|
|
20
20
|
});
|
|
21
21
|
worker.stderr?.on('data', (data) => {
|
|
22
|
-
console.debug(`[
|
|
22
|
+
console.debug(`[worker stderr] ${data}`);
|
|
23
23
|
});
|
|
24
24
|
worker.on('exit', () => {
|
|
25
25
|
worker = undefined;
|
|
@@ -39,6 +39,7 @@ export async function processStubsInWorker(options) {
|
|
|
39
39
|
worker.on('messageerror', onMessageError);
|
|
40
40
|
worker.on('error', onError);
|
|
41
41
|
worker.on('exit', onExit);
|
|
42
|
+
const whileStubbingMessage = `while creating stubs for ${options.inputPath}`;
|
|
42
43
|
// Send a message requesting the stubs to be created.
|
|
43
44
|
const request = { id, ...options };
|
|
44
45
|
worker.postMessage(request);
|
|
@@ -52,26 +53,26 @@ export async function processStubsInWorker(options) {
|
|
|
52
53
|
resolve(message);
|
|
53
54
|
}
|
|
54
55
|
else {
|
|
55
|
-
reject(new Error(`Unexpected
|
|
56
|
+
reject(new Error(`Unexpected stub worker message ${whileStubbingMessage}: ${JSON.stringify(message, null, 2)}`));
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
|
-
/** 'messageerror' event: deserializing a message failed. */
|
|
59
|
+
/** 'messageerror' event: deserializing a message failed (very unexpected). */
|
|
59
60
|
function onMessageError(err) {
|
|
60
61
|
handlersOff();
|
|
61
|
-
reject(new Error(`Error with
|
|
62
|
+
reject(new Error(`Error with stub worker message serialization ${whileStubbingMessage}:\n${err.stack || err}\n`));
|
|
62
63
|
}
|
|
63
64
|
/** 'error' event: unhandled exception in the worker, which shouldn't happen. */
|
|
64
65
|
function onError(err) {
|
|
65
66
|
handlersOff();
|
|
66
|
-
reject(new Error(`Uncaught error in
|
|
67
|
+
reject(new Error(`Uncaught error in stub worker ${whileStubbingMessage}:\n${err.stack || err}\n`));
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
69
|
-
* Make sure the promise rejects if the worker exits unexpectedly. (
|
|
70
|
-
*
|
|
70
|
+
* Make sure the promise rejects if the worker exits unexpectedly. (We override process.exit
|
|
71
|
+
* in the worker to throw an error, so this shouldn't happen.)
|
|
71
72
|
*/
|
|
72
73
|
function onExit() {
|
|
73
74
|
worker = undefined;
|
|
74
|
-
reject(new Error(`
|
|
75
|
+
reject(new Error(`Stub worker exited unexpectedly ${whileStubbingMessage}`));
|
|
75
76
|
}
|
|
76
77
|
/** Remove all event handlers to avoid interference with future sandbox runs. */
|
|
77
78
|
function handlersOff() {
|
|
@@ -82,8 +83,11 @@ export async function processStubsInWorker(options) {
|
|
|
82
83
|
}
|
|
83
84
|
});
|
|
84
85
|
}
|
|
85
|
-
/**
|
|
86
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Stop the worker for testing.
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
export async function _stopStubWorker() {
|
|
87
91
|
await worker?.terminate();
|
|
88
92
|
worker = undefined;
|
|
89
93
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processStubsInWorker.js","sourceRoot":"","sources":["../src/processStubsInWorker.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,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AAmC,oBAAoB,EAAE,CAAC,CAAC,CAAC;QAC/E,CAAC;QAED,gFAAgF;QAChF,SAAS,WAAW;YAClB,MAAM,EAAE,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YAClC,MAAM,EAAE,GAAG,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;YAC5C,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9B,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe;IACnC,MAAM,MAAM,EAAE,SAAS,EAAE,CAAC;IAC1B,MAAM,GAAG,SAAS,CAAC;AACrB,CAAC","sourcesContent":["import path from 'path';\nimport { fileURLToPath } from 'url';\nimport { Worker } from 'worker_threads';\nimport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nimport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\nimport type { ESMStubWorkerRequest, ESMStubWorkerResponse } from './types/ESMStubWorkerMessages.js';\n\nconst currentPath = path.dirname(fileURLToPath(import.meta.url));\nconst workerPath = path.join(currentPath, './worker/workerEntry.js');\nlet _counter = 0;\n\n// TODO do we need a pool?\nlet worker: Worker | undefined;\n\n/**\n * Generates ESM stubs for CommonJS and JSON modules.\n */\nexport async function processStubsInWorker(options: WriteESMStubsOptions): Promise<WriteESMStubsResult> {\n const id = _counter++;\n\n // Initialize worker.\n if (!worker) {\n try {\n worker = new Worker(workerPath, { stdout: true, stderr: true, execArgv: [] });\n worker.stdout?.on('data', (data) => {\n console.debug(`[worker stdout] ${data}`);\n });\n worker.stderr?.on('data', (data) => {\n console.debug(`[worker stderr] ${data}`);\n });\n worker.on('exit', () => {\n worker = undefined;\n });\n // don't hold the process open if only the worker is left\n worker.unref();\n } catch (err) {\n throw new Error(`Error initializing worker: ${(err as Error)?.stack || err}`);\n }\n }\n\n return new Promise((resolve, reject) => {\n // just for type checking\n if (!worker) return;\n\n worker.on('message', onMessage);\n worker.on('messageerror', onMessageError);\n worker.on('error', onError);\n worker.on('exit', onExit);\n\n const whileStubbingMessage = `while creating stubs for ${options.inputPath}`;\n\n // Send a message requesting the stubs to be created.\n const request: ESMStubWorkerRequest = { id, ...options };\n worker.postMessage(request);\n\n /** Normally, both success and error results should be sent back as messages. */\n function onMessage(message: ESMStubWorkerResponse) {\n if (message.id !== id) {\n return; // result about a different request\n }\n\n handlersOff();\n\n if (message.newEntries) {\n resolve(message);\n } else {\n reject(\n new Error(`Unexpected stub worker message ${whileStubbingMessage}: ${JSON.stringify(message, null, 2)}`),\n );\n }\n }\n\n /** 'messageerror' event: deserializing a message failed (very unexpected). */\n function onMessageError(err: Error) {\n handlersOff();\n reject(new Error(`Error with stub worker message serialization ${whileStubbingMessage}:\\n${err.stack || err}\\n`));\n }\n\n /** 'error' event: unhandled exception in the worker, which shouldn't happen. */\n function onError(err: Error) {\n handlersOff();\n reject(new Error(`Uncaught error in stub worker ${whileStubbingMessage}:\\n${err.stack || err}\\n`));\n }\n\n /**\n * Make sure the promise rejects if the worker exits unexpectedly. (We override process.exit\n * in the worker to throw an error, so this shouldn't happen.)\n */\n function onExit() {\n worker = undefined;\n reject(new Error(`Stub worker exited unexpectedly ${whileStubbingMessage}`));\n }\n\n /** Remove all event handlers to avoid interference with future sandbox runs. */\n function handlersOff() {\n worker?.off('message', onMessage);\n worker?.off('messageerror', onMessageError);\n worker?.off('error', onError);\n worker?.off('exit', onExit);\n }\n });\n}\n\n/**\n * Stop the worker for testing.\n * @internal\n */\nexport async function _stopStubWorker() {\n await worker?.terminate();\n worker = undefined;\n}\n"]}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { BundleMessage } from '@ms-cloudpack/bundler-types';
|
|
2
2
|
export type WriteESMStubsResult = {
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Mapping from entry key to full path:
|
|
5
|
+
* - Stub file path, if the file needed to be stubbed and was successful
|
|
6
|
+
* - Original path, if the file didn't need to be stubbed or stubbing was unsuccessful
|
|
7
|
+
*/
|
|
4
8
|
newEntries: Record<string, string>;
|
|
9
|
+
/** Any errors that occurred */
|
|
10
|
+
errors: BundleMessage[];
|
|
5
11
|
};
|
|
6
12
|
//# sourceMappingURL=WriteESMStubsResult.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WriteESMStubsResult.d.ts","sourceRoot":"","sources":["../../src/types/WriteESMStubsResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,MAAM,MAAM,mBAAmB,GAAG;IAChC,
|
|
1
|
+
{"version":3,"file":"WriteESMStubsResult.d.ts","sourceRoot":"","sources":["../../src/types/WriteESMStubsResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEnC,+BAA+B;IAC/B,MAAM,EAAE,aAAa,EAAE,CAAC;CACzB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WriteESMStubsResult.js","sourceRoot":"","sources":["../../src/types/WriteESMStubsResult.ts"],"names":[],"mappings":"","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/bundler-types';\n\nexport type WriteESMStubsResult = {\n
|
|
1
|
+
{"version":3,"file":"WriteESMStubsResult.js","sourceRoot":"","sources":["../../src/types/WriteESMStubsResult.ts"],"names":[],"mappings":"","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/bundler-types';\n\nexport type WriteESMStubsResult = {\n /**\n * Mapping from entry key to full path:\n * - Stub file path, if the file needed to be stubbed and was successful\n * - Original path, if the file didn't need to be stubbed or stubbing was unsuccessful\n */\n newEntries: Record<string, string>;\n\n /** Any errors that occurred */\n errors: BundleMessage[];\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initBrowserEnvironment.d.ts","sourceRoot":"","sources":["../../src/worker/initBrowserEnvironment.js"],"names":[],"mappings":"AAKA;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"initBrowserEnvironment.d.ts","sourceRoot":"","sources":["../../src/worker/initBrowserEnvironment.js"],"names":[],"mappings":"AAKA;;GAEG;AACH,wDAuEC"}
|
|
@@ -43,6 +43,7 @@ export async function initBrowserEnvironment() {
|
|
|
43
43
|
// Prevent adding event listeners.
|
|
44
44
|
global.addEventListener = window.addEventListener = document.addEventListener = () => { };
|
|
45
45
|
global.removeEventListener = window.removeEventListener = document.removeEventListener = () => { };
|
|
46
|
+
global.dispatchEvent = window.dispatchEvent = document.dispatchEvent = () => false;
|
|
46
47
|
// Mock out canvas.
|
|
47
48
|
// eslint-disable-next-line etc/no-deprecated
|
|
48
49
|
const createElement = window.document.createElement.bind(window.document);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initBrowserEnvironment.js","sourceRoot":"","sources":["../../src/worker/initBrowserEnvironment.js"],"names":[],"mappings":"AAAA,sEAAsE;AAEtE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB;IAC1C,oCAAoC;IACpC,MAAM,CAAC,WAAW,KAAK,gBAAgB,CAAC,CAAC,WAAW,CAAC,CAAC;IACtD,MAAM,CAAC,WAAW,KAAK,IAAI,CAAC,WAAW,CAAC;IACxC,MAAM,CAAC,WAAW,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAE3D,CAAC,MAAM,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IAEzC,MAAM,MA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EAAE,GAAE,CAAC,CAAC,CAAC;IAEjD,wDAAwD;IACxD,OAAO,CAAC,GAAG,CAAC,YAAY,GAAG,GAAG,CAAC;IAE/B,mFAAmF;IACnF,gBAAgB,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC;AAC5C,CAAC","sourcesContent":["// This file is JS because it's used in a worker (see workerEntry.js).\n\nimport { performance } from 'perf_hooks';\nimport util from 'util';\n\n/**\n * Initialize a fake browser environment.\n */\nexport async function initBrowserEnvironment() {\n // These must be set up before JSDOM\n global.performance ??= /** @type {*} */ (performance);\n global.TextEncoder ??= util.TextEncoder;\n global.TextDecoder ??= /** @type {*} */ (util.TextDecoder);\n\n (await import('jsdom-global')).default();\n\n await import('regenerator-runtime');\n\n // Ensure assigning to and reading from `window.foo` globals works as expected by copying all the\n // properties from the jsdom `window` to node's `global`, then assigning `global` to `window`.\n Object.entries(Object.getOwnPropertyDescriptors(window)).forEach(([k, desc]) => {\n // Skip storage to avoid errors \"localStorage is not available for opaque origins\"\n // (these errors could in theory be resolved by passing a URL to jsdom-global setup, but that\n // triggers some weird issues with whatwg-url using TextDecoder in a way that works in other\n // environments but throws in the sandbox)\n if (!(k in global) && !['localStorage', 'sessionStorage'].includes(k)) {\n Object.defineProperty(global, k, desc);\n }\n });\n /** @type {*} */ (global).window = global;\n /** @type {*} */ (global).self = global;\n /** @type {*} */ (document).window = global;\n\n window.requestAnimationFrame = window.setTimeout;\n window.cancelAnimationFrame = window.clearTimeout;\n global.btoa = (str) => Buffer.from(str, 'binary').toString('base64');\n global.atob = (str) => Buffer.from(str, 'base64').toString('binary');\n\n // Simulate WebSocket existence. (Needed for isomorphic-ws)\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n global.WebSocket = window.WebSocket = /** @type {*} */ (class WebSocket {});\n\n // Fake MessageChannel so scheduler won't hang the process by opening a channel.\n global.MessageChannel = window.MessageChannel = class MessageChannel {\n port1 = /** @type {*} */ ({});\n port2 = /** @type {*} */ ({});\n };\n\n // Prevent adding event listeners.\n global.addEventListener = window.addEventListener = document.addEventListener = () => {};\n global.removeEventListener = window.removeEventListener = document.removeEventListener = () => {};\n global.dispatchEvent = window.dispatchEvent = document.dispatchEvent = () => false;\n\n // Mock out canvas.\n // eslint-disable-next-line etc/no-deprecated\n const createElement = window.document.createElement.bind(window.document);\n const createElementMock = (/** @type {string} */ tagName, /** @type {ElementCreationOptions} */ options) => {\n if (tagName === 'canvas') {\n return /** @type {*} */ ({\n getContext: () => ({ fillRect: () => {} }),\n measureText: () => ({}),\n });\n }\n\n return createElement.apply(window.document, [tagName, options]);\n };\n // eslint-disable-next-line etc/no-deprecated\n global.document.createElement =\n /** @type {*} */ (window).createElement =\n /** @type {*} */ (global).createElement =\n createElementMock;\n\n window.fetch = /** @type {*} */ (async () => {});\n\n // prevent 'debug' package from accessing process.stderr\n process.env.DEBUG_COLORS = '0';\n\n // Some packages like readable-stream@2.3.7 read process.browser to avoid node code\n /** @type {*} */ (process).browser = true;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workerEntry.d.ts","sourceRoot":"","sources":["../../src/worker/workerEntry.
|
|
1
|
+
{"version":3,"file":"workerEntry.d.ts","sourceRoot":"","sources":["../../src/worker/workerEntry.ts"],"names":[],"mappings":""}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// These files use JS because they're loaded directly into a worker (see processStubsInWorker.ts),
|
|
2
|
-
// which must work both during tests (where the files are loaded from "src") and at runtime.
|
|
3
|
-
// They can still be type checked like TS, and the transpilation step doesn't do anything that
|
|
4
|
-
// should change the desired runtime behavior.
|
|
5
1
|
import { isMainThread, parentPort } from 'worker_threads';
|
|
6
2
|
import { initBrowserEnvironment } from './initBrowserEnvironment.js';
|
|
7
3
|
import { cleanUpGlobals, getGlobalProperties } from './globals.js';
|
|
@@ -12,20 +8,19 @@ if (isMainThread || !parentPort) {
|
|
|
12
8
|
await initBrowserEnvironment();
|
|
13
9
|
// TODO should the globals be marked as readonly if possible?
|
|
14
10
|
const initialGlobalProperties = getGlobalProperties();
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
12
|
+
const originalExit = process.exit;
|
|
18
13
|
function emitMessage(message) {
|
|
19
14
|
parentPort?.postMessage(message);
|
|
20
15
|
}
|
|
21
|
-
parentPort.on('message', (
|
|
16
|
+
parentPort.on('message', (options) => {
|
|
22
17
|
const { id } = options;
|
|
23
18
|
function onExit() {
|
|
24
|
-
// In this case, throw an error rather than sending a message to avoid handling it twice.
|
|
25
|
-
// Don't include the filename since that's potentially ambiguous (and redundant with info
|
|
26
|
-
// included by the processStubsInWorker error handler).
|
|
27
19
|
throw new Error('process.exit() was unexpectedly called');
|
|
28
20
|
}
|
|
21
|
+
// Overwrite process.exit to throw an error, and also add an exit handler just in case.
|
|
22
|
+
// This makes it easier to handle if one of the files being processed calls process.exit().
|
|
23
|
+
process.exit = onExit;
|
|
29
24
|
process.on('exit', onExit);
|
|
30
25
|
writeESMStubsInternal(options)
|
|
31
26
|
.then((result) => {
|
|
@@ -35,13 +30,13 @@ parentPort.on('message', (/** @type {import('../types/ESMStubWorkerMessages.js')
|
|
|
35
30
|
emitMessage({
|
|
36
31
|
id,
|
|
37
32
|
newEntries: {},
|
|
38
|
-
|
|
39
|
-
errors: [{ text: /** @type {Error} */ (error).stack || String(error) }],
|
|
33
|
+
errors: [{ text: error.stack || String(error) }],
|
|
40
34
|
});
|
|
41
35
|
})
|
|
42
36
|
.finally(() => {
|
|
43
37
|
cleanUpGlobals(initialGlobalProperties);
|
|
44
38
|
process.off('exit', onExit);
|
|
39
|
+
process.exit = originalExit;
|
|
45
40
|
});
|
|
46
41
|
});
|
|
47
42
|
//# sourceMappingURL=workerEntry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workerEntry.js","sourceRoot":"","sources":["../../src/worker/workerEntry.
|
|
1
|
+
{"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AAE,EAAE;YACd,MAAM,EAAE,CAAC,EAAE,IAAI,EAAG,KAAe,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;SAC5D,CAAC,CAAC;IACL,CAAC,CAAC;SACD,OAAO,CAAC,GAAG,EAAE;QACZ,cAAc,CAAC,uBAAuB,CAAC,CAAC;QACxC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5B,OAAO,CAAC,IAAI,GAAG,YAAY,CAAC;IAC9B,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["import { isMainThread, parentPort } from 'worker_threads';\nimport { initBrowserEnvironment } from './initBrowserEnvironment.js';\nimport { cleanUpGlobals, getGlobalProperties } from './globals.js';\nimport { writeESMStubsInternal } from '../writeESMStubsInternal.js';\nimport type { ESMStubWorkerRequest, ESMStubWorkerResponse } from '../types/ESMStubWorkerMessages.js';\n\nif (isMainThread || !parentPort) {\n throw new Error('This file should only be loaded in a worker thread');\n}\n\nawait initBrowserEnvironment();\n\n// TODO should the globals be marked as readonly if possible?\nconst initialGlobalProperties = getGlobalProperties();\n\n// eslint-disable-next-line @typescript-eslint/unbound-method\nconst originalExit = process.exit;\n\nfunction emitMessage(message: ESMStubWorkerResponse) {\n parentPort?.postMessage(message);\n}\n\nparentPort.on('message', (options: ESMStubWorkerRequest) => {\n const { id } = options;\n\n function onExit(): never {\n throw new Error('process.exit() was unexpectedly called');\n }\n\n // Overwrite process.exit to throw an error, and also add an exit handler just in case.\n // This makes it easier to handle if one of the files being processed calls process.exit().\n process.exit = onExit;\n process.on('exit', onExit);\n\n writeESMStubsInternal(options)\n .then((result) => {\n emitMessage({ id, ...result });\n })\n .catch((error) => {\n emitMessage({\n id,\n newEntries: {},\n errors: [{ text: (error as Error).stack || String(error) }],\n });\n })\n .finally(() => {\n cleanUpGlobals(initialGlobalProperties);\n process.off('exit', onExit);\n process.exit = originalExit;\n });\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInternal.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInternal.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,
|
|
1
|
+
{"version":3,"file":"writeESMStubsInternal.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInternal.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAsCvG"}
|
|
@@ -18,29 +18,27 @@ export async function writeESMStubsInternal(options) {
|
|
|
18
18
|
for (const [entryKey, entryPath] of Object.entries(entries)) {
|
|
19
19
|
const filePath = slash(path.join(inputPath, entryPath));
|
|
20
20
|
const safeEntryKey = stripLeadingRelativePath(entryKey);
|
|
21
|
-
let stubContent = '';
|
|
22
|
-
let stubPath;
|
|
23
21
|
try {
|
|
24
|
-
stubPath = await getStubPath({ inputPath, entryPath });
|
|
22
|
+
const stubPath = await getStubPath({ inputPath, entryPath });
|
|
25
23
|
if (!stubPath) {
|
|
24
|
+
// Stub not needed
|
|
26
25
|
result.newEntries[safeEntryKey] = path.join(inputPath, entryPath);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
let stubContent = '';
|
|
29
|
+
if (path.extname(entryPath).toLowerCase() === '.json') {
|
|
30
|
+
stubContent = await generateESMStubFromJSON({ filePath });
|
|
27
31
|
}
|
|
28
32
|
else {
|
|
29
|
-
|
|
30
|
-
stubContent = await generateESMStubFromJSON({ filePath });
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
stubContent = generateESMStubFromCJS({ filePath, stubPath });
|
|
34
|
-
}
|
|
35
|
-
// Attempt to write it to disk.
|
|
36
|
-
await writeFile(stubPath, stubContent, 'utf-8');
|
|
37
|
-
result.newEntries[safeEntryKey] = stubPath;
|
|
33
|
+
stubContent = generateESMStubFromCJS({ filePath, stubPath });
|
|
38
34
|
}
|
|
35
|
+
// Attempt to write it to disk.
|
|
36
|
+
await writeFile(stubPath, stubContent, 'utf-8');
|
|
37
|
+
result.newEntries[safeEntryKey] = stubPath;
|
|
39
38
|
}
|
|
40
39
|
catch (e) {
|
|
41
40
|
const entryFullPath = path.join(inputPath, entryPath);
|
|
42
41
|
result.newEntries[safeEntryKey] = entryFullPath;
|
|
43
|
-
result.errors ||= [];
|
|
44
42
|
result.errors.push(processError(safeEntryKey, entryFullPath, e));
|
|
45
43
|
}
|
|
46
44
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInternal.js","sourceRoot":"","sources":["../src/writeESMStubsInternal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAIjD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAA6B;IACvE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACvC,MAAM,MAAM,GAAwB;QACl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from 'fs/promises';\nimport path from 'path';\nimport { generateESMStubFromCJS } from './generateESMStubFromCJS.js';\nimport { generateESMStubFromJSON } from './generateESMStubFromJSON.js';\nimport { getStubPath } from './getStubPath.js';\nimport { processError } from './processError.js';\nimport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nimport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\n\n/**\n * Generates a set of ESM stubs for given entries and writes it to disk. If\n * any files doesn't need a stub, then entry will have an undefined stubPath.\n */\nexport async function writeESMStubsInternal(options: WriteESMStubsOptions): Promise<WriteESMStubsResult> {\n const { inputPath, entries } = options;\n const result: WriteESMStubsResult = {\n newEntries: {},\n errors: [],\n };\n\n for (const [entryKey, entryPath] of Object.entries(entries)) {\n const filePath = slash(path.join(inputPath, entryPath));\n const safeEntryKey = stripLeadingRelativePath(entryKey);\n\n try {\n const stubPath = await getStubPath({ inputPath, entryPath });\n if (!stubPath) {\n // Stub not needed\n result.newEntries[safeEntryKey] = path.join(inputPath, entryPath);\n continue;\n }\n\n let stubContent: string = '';\n if (path.extname(entryPath).toLowerCase() === '.json') {\n stubContent = await generateESMStubFromJSON({ filePath });\n } else {\n stubContent = generateESMStubFromCJS({ filePath, stubPath });\n }\n\n // Attempt to write it to disk.\n await writeFile(stubPath, stubContent, 'utf-8');\n\n result.newEntries[safeEntryKey] = stubPath;\n } catch (e) {\n const entryFullPath = path.join(inputPath, entryPath);\n result.newEntries[safeEntryKey] = entryFullPath;\n result.errors.push(processError(safeEntryKey, entryFullPath, e));\n }\n }\n\n return result;\n}\n\nfunction stripLeadingRelativePath(relativePath: string) {\n return relativePath.replace(/^\\.\\//, '');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/esm-stub-utilities",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/bundler-types": "^0.
|
|
17
|
+
"@ms-cloudpack/bundler-types": "^0.24.0",
|
|
18
18
|
"@ms-cloudpack/json-utilities": "^0.1.3",
|
|
19
19
|
"@ms-cloudpack/path-string-parsing": "^1.1.3",
|
|
20
|
-
"@ms-cloudpack/package-utilities": "^5.8.
|
|
20
|
+
"@ms-cloudpack/package-utilities": "^5.8.2",
|
|
21
21
|
"jsdom-global": "^3.0.2",
|
|
22
22
|
"jsdom": "^22.0.0",
|
|
23
23
|
"regenerator-runtime": "^0.14.1"
|