@ms-cloudpack/esm-stub-utilities 0.14.33 → 0.14.35
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/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/verifyStub/verifyStub.d.ts +9 -11
- package/lib/verifyStub/verifyStub.d.ts.map +1 -1
- package/lib/verifyStub/verifyStub.js +19 -55
- package/lib/verifyStub/verifyStub.js.map +1 -1
- package/lib/verifyStub/verifyStubWorker.d.ts +6 -0
- package/lib/verifyStub/verifyStubWorker.d.ts.map +1 -0
- package/lib/verifyStub/{loadStubWorker.js → verifyStubWorker.js} +18 -42
- package/lib/verifyStub/verifyStubWorker.js.map +1 -0
- package/lib/writeESMStubsInWorker.d.ts.map +1 -1
- package/lib/writeESMStubsInWorker.js +4 -2
- package/lib/writeESMStubsInWorker.js.map +1 -1
- package/package.json +4 -4
- package/lib/verifyStub/loadStubWorker.d.ts +0 -15
- package/lib/verifyStub/loadStubWorker.d.ts.map +0 -1
- package/lib/verifyStub/loadStubWorker.js.map +0 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { writeESMStubsInWorker, disposeStubWorkerPool } from './writeESMStubsInWorker.js';
|
|
2
2
|
export type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';
|
|
3
3
|
export type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';
|
|
4
|
-
export { _verifyStub } from './verifyStub/verifyStub.js';
|
|
4
|
+
export { _verifyStub, _disposeVerifyStubWorker } from './verifyStub/verifyStub.js';
|
|
5
5
|
export type { _VerifyStubOptions } from './types/VerifyStubOptions.js';
|
|
6
6
|
export { generateESMStubFromExports } from './generateESMStubFromExports.js';
|
|
7
7
|
export type { StubExportInfo } from './types/StubExportInfo.js';
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAC1F,YAAY,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAC1F,YAAY,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACnF,YAAY,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAC7E,YAAY,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { writeESMStubsInWorker, disposeStubWorkerPool } from './writeESMStubsInWorker.js';
|
|
2
|
-
export { _verifyStub } from './verifyStub/verifyStub.js';
|
|
2
|
+
export { _verifyStub, _disposeVerifyStubWorker } from './verifyStub/verifyStub.js';
|
|
3
3
|
export { generateESMStubFromExports } from './generateESMStubFromExports.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAG1F,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAG1F,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAEnF,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC","sourcesContent":["export { writeESMStubsInWorker, disposeStubWorkerPool } from './writeESMStubsInWorker.js';\nexport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nexport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\nexport { _verifyStub, _disposeVerifyStubWorker } from './verifyStub/verifyStub.js';\nexport type { _VerifyStubOptions } from './types/VerifyStubOptions.js';\nexport { generateESMStubFromExports } from './generateESMStubFromExports.js';\nexport type { StubExportInfo } from './types/StubExportInfo.js';\n"]}
|
|
@@ -4,16 +4,14 @@ import type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';
|
|
|
4
4
|
* Write a stub file (fixing paths for Windows) and try importing it in a worker,
|
|
5
5
|
* to verify that it's valid and returns the expected exports.
|
|
6
6
|
* @internal
|
|
7
|
+
* @returns The result of importing the stub in the worker, in IPC-friendly format.
|
|
8
|
+
* Any non-IPC-friendly values (such as functions) will be pretty-printed as strings.
|
|
9
|
+
* If there's an error, the promise will reject.
|
|
7
10
|
*/
|
|
8
|
-
export declare function _verifyStub(options: _VerifyStubOptions): Promise<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/** Error if unsuccessful. */
|
|
15
|
-
error?: unknown;
|
|
16
|
-
/** Dispose the worker. Unless/until this is called, the same worker will be reused. */
|
|
17
|
-
dispose: () => Promise<void>;
|
|
18
|
-
}>;
|
|
11
|
+
export declare function _verifyStub(options: _VerifyStubOptions): Promise<unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Stop the worker used by `_verifyStub`.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare function _disposeVerifyStubWorker(): Promise<void>;
|
|
19
17
|
//# sourceMappingURL=verifyStub.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyStub.d.ts","sourceRoot":"","sources":["../../src/verifyStub/verifyStub.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAQxE
|
|
1
|
+
{"version":3,"file":"verifyStub.d.ts","sourceRoot":"","sources":["../../src/verifyStub/verifyStub.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAQxE;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC,CAoB/E;AA8BD;;;GAGG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,IAAI,CAAC,CAG9D"}
|
|
@@ -9,41 +9,38 @@
|
|
|
9
9
|
// - Allow the module to run in a simulated browser environment if needed.
|
|
10
10
|
//
|
|
11
11
|
//
|
|
12
|
+
import { WorkerPool } from '@ms-cloudpack/worker-pool';
|
|
12
13
|
import fs from 'fs/promises';
|
|
13
14
|
import path from 'path';
|
|
14
15
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
15
|
-
import { Worker } from 'worker_threads';
|
|
16
16
|
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
17
|
-
const
|
|
18
|
-
let
|
|
17
|
+
const workerPath = path.join(dirname, 'verifyStubWorker.js');
|
|
18
|
+
let workerPool;
|
|
19
19
|
/**
|
|
20
20
|
* FOR TESTING ONLY:
|
|
21
21
|
* Write a stub file (fixing paths for Windows) and try importing it in a worker,
|
|
22
22
|
* to verify that it's valid and returns the expected exports.
|
|
23
23
|
* @internal
|
|
24
|
+
* @returns The result of importing the stub in the worker, in IPC-friendly format.
|
|
25
|
+
* Any non-IPC-friendly values (such as functions) will be pretty-printed as strings.
|
|
26
|
+
* If there's an error, the promise will reject.
|
|
24
27
|
*/
|
|
25
28
|
export async function _verifyStub(options) {
|
|
26
29
|
// Write the stub content to disk (fixing paths if needed)
|
|
27
30
|
await writeStubFile(options);
|
|
28
31
|
// Try to import the stub in a worker
|
|
29
|
-
|
|
32
|
+
workerPool ??= new WorkerPool({
|
|
33
|
+
name: 'ESM stub verification',
|
|
34
|
+
entryPath: workerPath,
|
|
35
|
+
maxWorkers: 1,
|
|
36
|
+
});
|
|
30
37
|
const stubImportRequest = {
|
|
31
38
|
stubPath: getStubImportPath(options.stubPath),
|
|
32
39
|
testCode: options.testCode,
|
|
33
40
|
};
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
resolve({ ...response, dispose: disposeWorker });
|
|
38
|
-
};
|
|
39
|
-
// unhandled exception (not expected)
|
|
40
|
-
const onError = (err) => {
|
|
41
|
-
worker?.off('message', onMessage);
|
|
42
|
-
resolve({ error: err, dispose: disposeWorker });
|
|
43
|
-
};
|
|
44
|
-
worker?.once('message', onMessage);
|
|
45
|
-
worker?.once('error', onError);
|
|
46
|
-
worker?.postMessage(stubImportRequest);
|
|
41
|
+
return workerPool.execute({
|
|
42
|
+
method: 'tryImportStub',
|
|
43
|
+
args: [stubImportRequest],
|
|
47
44
|
});
|
|
48
45
|
}
|
|
49
46
|
async function writeStubFile(options) {
|
|
@@ -72,44 +69,11 @@ function getStubImportPath(stubPath) {
|
|
|
72
69
|
return stubPath;
|
|
73
70
|
}
|
|
74
71
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* (The worker is reused between tests for now because it's expensive to set up and tear down;
|
|
78
|
-
* this can be changed later for certain tests if needed.)
|
|
79
|
-
*/
|
|
80
|
-
function ensureWorker() {
|
|
81
|
-
if (worker)
|
|
82
|
-
return worker;
|
|
83
|
-
worker = new Worker(loadStubWorkerPath);
|
|
84
|
-
// An 'error' event from a worker means an unhandled exception and it's about to exit.
|
|
85
|
-
// Don't throw an error in this fallback handler in case the error occurred during a call
|
|
86
|
-
// to `verifyStub` (which has its own error handler that can fail the calling test),
|
|
87
|
-
// but save the error to be included in the 'exit' handler's error message.
|
|
88
|
-
let lastError;
|
|
89
|
-
worker.on('error', (err) => {
|
|
90
|
-
lastError = err;
|
|
91
|
-
});
|
|
92
|
-
worker.on('exit', (code) => {
|
|
93
|
-
throw new Error(lastError
|
|
94
|
-
? `Worker exited due to an uncaught exception: ${lastError.stack}`
|
|
95
|
-
: `Worker exited unexpectedly with code ${code || 0}`);
|
|
96
|
-
});
|
|
97
|
-
return worker;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Stop the worker used by `verifyStub`.
|
|
72
|
+
* Stop the worker used by `_verifyStub`.
|
|
73
|
+
* @internal
|
|
101
74
|
*/
|
|
102
|
-
async function
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
worker.removeAllListeners();
|
|
106
|
-
try {
|
|
107
|
-
await worker.terminate();
|
|
108
|
-
}
|
|
109
|
-
catch {
|
|
110
|
-
// ignore
|
|
111
|
-
}
|
|
112
|
-
worker = undefined;
|
|
113
|
-
}
|
|
75
|
+
export async function _disposeVerifyStubWorker() {
|
|
76
|
+
await workerPool?.dispose();
|
|
77
|
+
workerPool = undefined;
|
|
114
78
|
}
|
|
115
79
|
//# sourceMappingURL=verifyStub.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyStub.js","sourceRoot":"","sources":["../../src/verifyStub/verifyStub.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,8FAA8F;AAC9F,qDAAqD;AACrD,EAAE;AACF,gDAAgD;AAChD,qFAAqF;AACrF,oEAAoE;AACpE,0EAA0E;AAC1E,EAAE;AACF,EAAE;AAEF,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,O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AC;KAC1B,CAAC,CAAC;AACL,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,OAA2B;IACtD,qCAAqC;IACrC,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACxB,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;QACjC,6DAA6D;QAC7D,iGAAiG;QACjG,0FAA0F;QAC1F,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,8BAA8B,EAAE,CAAC,KAAK,EAAE,UAAkB,EAAE,EAAE;YAChF,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;gBAChC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC;YACnE,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;IAED,sBAAsB;IACtB,MAAM,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;AAC7C,CAAC;AAED,kEAAkE;AAClE,SAAS,iBAAiB,CAAC,QAAgB;IACzC,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC9D,mEAAmE;QACnE,OAAO,aAAa,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC;IACtC,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB;IAC5C,MAAM,UAAU,EAAE,OAAO,EAAE,CAAC;IAC5B,UAAU,GAAG,SAAS,CAAC;AACzB,CAAC","sourcesContent":["//\n//\n// The utilities in this folder are used to verify that a generated stub file can be imported,\n// and that the results of importing are as expected.\n//\n// Why must the stub file be loaded in a worker?\n// - Ensure that the module is imported in a \"vanilla\" way, without interference from\n// Jest's transpilation, custom module resolution, or environment.\n// - Allow the module to run in a simulated browser environment if needed.\n//\n//\n\nimport { WorkerPool } from '@ms-cloudpack/worker-pool';\nimport fs from 'fs/promises';\nimport path from 'path';\nimport { fileURLToPath, pathToFileURL } from 'url';\nimport type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';\nimport type { TryImportStubRequest } from './verifyStubWorker.js';\n\nconst dirname = path.dirname(fileURLToPath(import.meta.url));\nconst workerPath = path.join(dirname, 'verifyStubWorker.js');\n\nlet workerPool: WorkerPool | undefined;\n\n/**\n * FOR TESTING ONLY:\n * Write a stub file (fixing paths for Windows) and try importing it in a worker,\n * to verify that it's valid and returns the expected exports.\n * @internal\n * @returns The result of importing the stub in the worker, in IPC-friendly format.\n * Any non-IPC-friendly values (such as functions) will be pretty-printed as strings.\n * If there's an error, the promise will reject.\n */\nexport async function _verifyStub(options: _VerifyStubOptions): Promise<unknown> {\n // Write the stub content to disk (fixing paths if needed)\n await writeStubFile(options);\n\n // Try to import the stub in a worker\n workerPool ??= new WorkerPool({\n name: 'ESM stub verification',\n entryPath: workerPath,\n maxWorkers: 1,\n });\n\n const stubImportRequest: TryImportStubRequest = {\n stubPath: getStubImportPath(options.stubPath),\n testCode: options.testCode,\n };\n\n return workerPool.execute<unknown>({\n method: 'tryImportStub',\n args: [stubImportRequest],\n });\n}\n\nasync function writeStubFile(options: _VerifyStubOptions): Promise<void> {\n // Fix the stub content on Windows...\n let stub = options.stub;\n if (process.platform === 'win32') {\n // Node only supports Windows absolute paths as file:// URLs.\n // Unfortunately, the bundlers don't like this syntax, so we don't put it in the stub by default.\n // Convert any absolute paths here so that the worker is able to natively import the stub.\n stub = stub.replace(/(?:from|import) \"([^\"]*?)\";/g, (match, importPath: string) => {\n if (path.isAbsolute(importPath)) {\n return match.replace(importPath, pathToFileURL(importPath).href);\n }\n return match;\n });\n }\n\n // Write the stub file\n await fs.writeFile(options.stubPath, stub);\n}\n\n/** Get a stub import path that works for the current platform. */\nfunction getStubImportPath(stubPath: string): string {\n if (process.platform === 'win32' && path.isAbsolute(stubPath)) {\n // Convert the path to a file:// URL so the import works on Windows\n return pathToFileURL(stubPath).href;\n }\n return stubPath;\n}\n\n/**\n * Stop the worker used by `_verifyStub`.\n * @internal\n */\nexport async function _disposeVerifyStubWorker(): Promise<void> {\n await workerPool?.dispose();\n workerPool = undefined;\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request to `verifyStubWorker.js` `tryImportStub`
|
|
3
|
+
*/
|
|
4
|
+
export type TryImportStubRequest = Pick<_VerifyStubOptions, "stubPath" | "testCode">;
|
|
5
|
+
import type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';
|
|
6
|
+
//# sourceMappingURL=verifyStubWorker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verifyStubWorker.d.ts","sourceRoot":"","sources":["../../src/verifyStub/verifyStubWorker.js"],"names":[],"mappings":";;;mCAea,IAAI,CAAC,kBAAkB,EAAE,UAAU,GAAG,UAAU,CAAC;wCAFtB,+BAA+B"}
|
|
@@ -11,41 +11,27 @@
|
|
|
11
11
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
12
12
|
/** @import { _VerifyStubOptions } from '../types/VerifyStubOptions.js' */
|
|
13
13
|
/**
|
|
14
|
-
* @typedef {Pick<_VerifyStubOptions, 'stubPath' | 'testCode'>}
|
|
15
|
-
*
|
|
16
|
-
* @typedef {{ value: unknown } | { error: string }} LoadStubWorkerResponse Response from `loadStubWorker.js`.
|
|
17
|
-
* `value` is the IPC-friendly result of importing the module.
|
|
14
|
+
* @typedef {Pick<_VerifyStubOptions, 'stubPath' | 'testCode'>} TryImportStubRequest Request to `verifyStubWorker.js` `tryImportStub`
|
|
18
15
|
*/
|
|
19
16
|
import v8 from 'v8';
|
|
20
|
-
import {
|
|
17
|
+
import { initializeWorker } from '@ms-cloudpack/worker-pool';
|
|
21
18
|
import { initBrowserEnvironment } from '../worker/initBrowserEnvironment.js';
|
|
22
19
|
import { cleanUpGlobals, getGlobalProperties } from '../worker/globals.js';
|
|
23
|
-
if (isMainThread || !parentPort) {
|
|
24
|
-
throw new Error('This file should only be loaded in a worker thread');
|
|
25
|
-
}
|
|
26
20
|
// These modules may expect a browser environment
|
|
27
21
|
await initBrowserEnvironment();
|
|
28
22
|
const initialGlobalProperties = getGlobalProperties();
|
|
23
|
+
initializeWorker({
|
|
24
|
+
methods: {
|
|
25
|
+
tryImportStub,
|
|
26
|
+
},
|
|
27
|
+
afterEach: () => {
|
|
28
|
+
cleanUpGlobals(initialGlobalProperties);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
29
31
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @
|
|
32
|
+
* @param {TryImportStubRequest} request
|
|
33
|
+
* @returns {Promise<unknown>}
|
|
32
34
|
*/
|
|
33
|
-
function emitMessage(message) {
|
|
34
|
-
parentPort?.postMessage(message);
|
|
35
|
-
}
|
|
36
|
-
parentPort.on('message', (/** @type {LoadStubWorkerRequest} */ request) => {
|
|
37
|
-
if (typeof request.stubPath !== 'string') {
|
|
38
|
-
emitMessage({
|
|
39
|
-
error: `Unexpected request format: ${JSON.stringify(request)}`,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
// Import the stub and send a reponse
|
|
44
|
-
// (`void` ignores the promise result since callbacks can't be async)
|
|
45
|
-
void tryImportStub(request);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
/** @param {LoadStubWorkerRequest} request */
|
|
49
35
|
async function tryImportStub(request) {
|
|
50
36
|
const { stubPath, testCode } = request;
|
|
51
37
|
let moduleExports;
|
|
@@ -60,22 +46,12 @@ async function tryImportStub(request) {
|
|
|
60
46
|
}
|
|
61
47
|
}
|
|
62
48
|
catch (err) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
finally {
|
|
69
|
-
cleanUpGlobals(initialGlobalProperties);
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
emitMessage({ value: serialize(moduleExports) });
|
|
73
|
-
}
|
|
74
|
-
catch (err) {
|
|
75
|
-
emitMessage({
|
|
76
|
-
error: `Error formatting or serializing exports: ${ /** @type {Error} */(err).stack || String(err)}`,
|
|
77
|
-
});
|
|
49
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
50
|
+
error.message = `${step} failed: ${error.message}`;
|
|
51
|
+
throw error;
|
|
78
52
|
}
|
|
53
|
+
// This might throw on a serialization error. Let it propagate to be handled as a rejection.
|
|
54
|
+
return serialize(moduleExports);
|
|
79
55
|
}
|
|
80
56
|
/**
|
|
81
57
|
* Convert a value into an IPC-serializable form, so it can be passed back to the test and verified.
|
|
@@ -136,4 +112,4 @@ function serialize(value, depth = 1, encountered = new WeakSet()) {
|
|
|
136
112
|
return JSON.stringify(value);
|
|
137
113
|
}
|
|
138
114
|
}
|
|
139
|
-
//# sourceMappingURL=
|
|
115
|
+
//# sourceMappingURL=verifyStubWorker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verifyStubWorker.js","sourceRoot":"","sources":["../../src/verifyStub/verifyStubWorker.js"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,oFAAoF;AACpF,qDAAqD;AACrD,EAAE;AACF,uDAAuD;AACvD,EAAE;AACF,EAAE;AAEF,+DAA+D;AAC/D,wDAAwD;AACxD,0DAA0D;AAE1D,0EAA0E;AAC1E;;GAEG;AACH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE3E,iDAAiD;AACjD,MAAM,sBAAsB,EAAE,CAAC;AAC/B,MAAM,uBAAuB,GAAG,mBAAmB,EAAE,CAAC;AA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,KAAK,CAAC,WAAW,EAAE,CAAC;YACtB,OAAO,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,GAAG,CAAC;QACvC,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,QAAQ,EAAE,CAAC;YAC1C,OAAO,gBAAgB,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAChC,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;QAChD,OAAO,aAAa,KAAK,CAAC,IAAI,IAAI,aAAa,GAAG,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE,GAAG,CAAC;IACnG,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;IAED,IAAI,CAAC;QACH,sDAAsD;QACtD,+EAA+E;QAC/E,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,gDAAgD;QAChD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC","sourcesContent":["//\n//\n// This worker is used in tests to verify that generated stub files can be imported,\n// and that the results of importing are as expected.\n//\n// See verifyStub.ts for more about why this is needed.\n//\n//\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\n/** @import { _VerifyStubOptions } from '../types/VerifyStubOptions.js' */\n/**\n * @typedef {Pick<_VerifyStubOptions, 'stubPath' | 'testCode'>} TryImportStubRequest Request to `verifyStubWorker.js` `tryImportStub`\n */\nimport v8 from 'v8';\nimport { initializeWorker } from '@ms-cloudpack/worker-pool';\nimport { initBrowserEnvironment } from '../worker/initBrowserEnvironment.js';\nimport { cleanUpGlobals, getGlobalProperties } from '../worker/globals.js';\n\n// These modules may expect a browser environment\nawait initBrowserEnvironment();\nconst initialGlobalProperties = getGlobalProperties();\n\ninitializeWorker({\n methods: {\n tryImportStub,\n },\n afterEach: () => {\n cleanUpGlobals(initialGlobalProperties);\n },\n});\n\n/**\n * @param {TryImportStubRequest} request\n * @returns {Promise<unknown>}\n */\nasync function tryImportStub(request) {\n const { stubPath, testCode } = request;\n let moduleExports;\n let step = 'Import';\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n moduleExports = await import(stubPath);\n step = 'Running test code';\n if (testCode) {\n // eslint-disable-next-line @typescript-eslint/no-implied-eval\n new Function('moduleExports', testCode)(moduleExports);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n error.message = `${step} failed: ${error.message}`;\n throw error;\n }\n\n // This might throw on a serialization error. Let it propagate to be handled as a rejection.\n return serialize(moduleExports);\n}\n\n/**\n * Convert a value into an IPC-serializable form, so it can be passed back to the test and verified.\n * @param {*} value\n * @returns {*}\n */\nfunction serialize(value, depth = 1, encountered = new WeakSet()) {\n if (depth > 3) {\n return '...';\n }\n\n if (value && typeof value === 'object') {\n // track encountered objects to avoid entering circular references\n if (encountered.has(value)) {\n return '[Circular]';\n }\n encountered.add(value);\n\n // Convert non-serializable types\n if (value instanceof Set) {\n return `Set ${JSON.stringify([...value])}`;\n }\n if (value instanceof Map) {\n return `Map ${JSON.stringify(Object.fromEntries(value))}`;\n }\n if (value instanceof Error) {\n return `[${value.name}: ${value.message}]`;\n }\n if (value instanceof RegExp) {\n return value.toString();\n }\n if (Array.isArray(value)) {\n return value.map((v) => serialize(v, depth + 1, encountered));\n }\n if (Object.keys(value).length || value.constructor?.name === 'Object') {\n return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, serialize(v, depth + 1, encountered)]));\n }\n if (value.constructor) {\n return `[${value.constructor.name}]`;\n }\n if (value[Symbol.toStringTag] == 'Module') {\n return '[empty module]';\n }\n }\n\n if (typeof value === 'function') {\n const properties = JSON.stringify({ ...value });\n return `[Function ${value.name || '<anonymous>'}${properties === '{}' ? '' : ` ${properties}`}]`;\n }\n\n if (typeof value === 'symbol') {\n return value.toString();\n }\n\n try {\n // try to serialize the same way postMessage does, per\n // https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist\n v8.serialize(value);\n return value;\n } catch {\n // not serializable, so format the value instead\n return JSON.stringify(value);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInWorker.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"writeESMStubsInWorker.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAU1E;;GAEG;AAGH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,oBAAoB,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAAC,mBAAmB,CAAC,CAuE9B;AAED,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC,CAG3D"}
|
|
@@ -2,6 +2,9 @@ import { getAdjustedTimeout } from '@ms-cloudpack/environment';
|
|
|
2
2
|
import { WorkerPool } from '@ms-cloudpack/worker-pool';
|
|
3
3
|
import { supportedExtensions } from './supportedExtensions.js';
|
|
4
4
|
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const workerPath = path.join(dirname, 'worker/workerEntry.js');
|
|
5
8
|
let workerPool;
|
|
6
9
|
/**
|
|
7
10
|
* Write ESM stubs using a worker pool.
|
|
@@ -31,8 +34,7 @@ export async function writeESMStubsInWorker(options) {
|
|
|
31
34
|
}
|
|
32
35
|
workerPool ??= new WorkerPool({
|
|
33
36
|
name: 'ESM stub generation',
|
|
34
|
-
|
|
35
|
-
entryPath: './worker/workerEntry',
|
|
37
|
+
entryPath: workerPath,
|
|
36
38
|
maxWorkers: options.maxWorkers || 5,
|
|
37
39
|
// Stop inactive workers after 5 minutes. This is intended to prevent one possible cause of
|
|
38
40
|
// OOM crashes if cloudpack start is left running for a long time (though it's much more likely
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInWorker.js","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAGvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"writeESMStubsInWorker.js","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAGvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;A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AC7F,2FAA2F;QAC3F,4BAA4B;QAC5B,eAAe,EAAE,CAAC,GAAG,EAAE,GAAG,IAAI;QAC9B,4FAA4F;QAC5F,oFAAoF;QACpF,iGAAiG;QACjG,WAAW,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI;QAC9B,gDAAgD;QAChD,aAAa,EAAE,kBAAkB,CAAC,IAAI,CAAC;KACxC,CAAC,CAAC;IAEH,MAAM,WAAW,GAAyB;QACxC,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,OAAO,EAAE,kBAAkB;QAC3B,oBAAoB;KACrB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,MAAM,UAAU,CAAC,OAAO,CAAsB;YAC/D,MAAM,EAAE,eAAe;YACvB,IAAI,EAAE,CAAC,WAAW,CAAC;SACpB,CAAC,CAAC;QAEH,iCAAiC;QACjC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;QAExD,OAAO,UAAU,CAAC;IACpB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO;YACL,UAAU,EAAE,EAAE;YACd,MAAM,EAAE;gBACN;oBACE,MAAM,EAAE,qBAAqB;oBAC7B,mEAAmE;oBACnE,IAAI,EAAG,GAAa,EAAE,KAAK,IAAK,GAAa,EAAE,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC;iBACtE;aACF;SACF,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB;IACzC,MAAM,UAAU,EAAE,OAAO,EAAE,CAAC;IAC5B,UAAU,GAAG,SAAS,CAAC;AACzB,CAAC","sourcesContent":["import { getAdjustedTimeout } from '@ms-cloudpack/environment';\nimport { WorkerPool } from '@ms-cloudpack/worker-pool';\nimport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nimport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\nimport { supportedExtensions } from './supportedExtensions.js';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\n\nconst dirname = path.dirname(fileURLToPath(import.meta.url));\nconst workerPath = path.join(dirname, 'worker/workerEntry.js');\n\nlet workerPool: WorkerPool | undefined;\n\n/**\n * Write ESM stubs using a worker pool.\n */\n// This is tested in bundler-rollup/src/writeESMStubsInWorker.test.ts so we can\n// use the compiled version of the workerEntry file.\nexport async function writeESMStubsInWorker(\n options: WriteESMStubsOptions & { maxWorkers?: number },\n): Promise<WriteESMStubsResult> {\n const { inputPath, entries, unsafeCjsExportNames } = options;\n\n // Mapping from entry key to entry absolute path\n const nonStubbedEntries: WriteESMStubsResult['newEntries'] = {};\n\n // We don't want to call into the worker for packages where stubbing is clearly not needed,\n // such as packages with only TS entries. So filter the entries by whether they potentially\n // need to be stubbed.\n const entriesToMaybeStub: Record<string, string> = {};\n\n for (const [entryKey, entryPath] of Object.entries(entries)) {\n const ext = path.extname(entryPath).toLowerCase();\n if (supportedExtensions.includes(ext)) {\n entriesToMaybeStub[entryKey] = entryPath;\n } else {\n nonStubbedEntries[entryKey] = path.join(inputPath, entryPath);\n }\n }\n\n if (!Object.keys(entriesToMaybeStub).length) {\n // Return early if there's nothing to stub\n return { newEntries: nonStubbedEntries, errors: [] };\n }\n\n workerPool ??= new WorkerPool({\n name: 'ESM stub generation',\n entryPath: workerPath,\n maxWorkers: options.maxWorkers || 5,\n // Stop inactive workers after 5 minutes. This is intended to prevent one possible cause of\n // OOM crashes if cloudpack start is left running for a long time (though it's much more likely\n // that the issue is caused by servers or file watchers). The timeout is conservative because\n // the fake browser environment has nontrivial startup time, so killing workers too quickly\n // could slow down rebuilds.\n inactiveTimeout: 5 * 60 * 1000,\n // Most stubs seem to stay well under this size, and Node appears to be decent about freeing\n // memory after writeESMStubs finishes, but this will catch anything giant or leaky.\n // (This is only checked after task completion, so a package exceeding it will still be stubbed.)\n maxHeapSize: 500 * 1024 * 1024,\n // console.warn if a stub takes longer than this\n warnAboveTime: getAdjustedTimeout(2000),\n });\n\n const stubOptions: WriteESMStubsOptions = {\n inputPath: options.inputPath,\n entries: entriesToMaybeStub,\n unsafeCjsExportNames,\n };\n\n try {\n const stubResult = await workerPool.execute<WriteESMStubsResult>({\n method: 'writeESMStubs',\n args: [stubOptions],\n });\n\n // Add in any non-stubbed entries\n Object.assign(stubResult.newEntries, nonStubbedEntries);\n\n return stubResult;\n } catch (err) {\n return {\n newEntries: {},\n errors: [\n {\n source: 'ESM stub generation',\n // note: err won't be an instance of Error because of serialization\n text: (err as Error)?.stack || (err as Error)?.message || String(err),\n },\n ],\n };\n }\n}\n\nexport async function disposeStubWorkerPool(): Promise<void> {\n await workerPool?.dispose();\n workerPool = undefined;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/esm-stub-utilities",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.35",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@happy-dom/global-registrator": "^17.1.1",
|
|
18
|
-
"@ms-cloudpack/common-types": "^0.24.
|
|
18
|
+
"@ms-cloudpack/common-types": "^0.24.20",
|
|
19
19
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
20
20
|
"@ms-cloudpack/json-utilities": "^0.1.10",
|
|
21
|
-
"@ms-cloudpack/package-utilities": "^12.3.
|
|
21
|
+
"@ms-cloudpack/package-utilities": "^12.3.10",
|
|
22
22
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
23
|
-
"@ms-cloudpack/worker-pool": "^0.
|
|
23
|
+
"@ms-cloudpack/worker-pool": "^0.4.0",
|
|
24
24
|
"regenerator-runtime": "^0.14.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request to `loadStubWorker.js`
|
|
3
|
-
*/
|
|
4
|
-
export type LoadStubWorkerRequest = Pick<_VerifyStubOptions, "stubPath" | "testCode">;
|
|
5
|
-
/**
|
|
6
|
-
* Response from `loadStubWorker.js`.
|
|
7
|
-
* `value` is the IPC-friendly result of importing the module.
|
|
8
|
-
*/
|
|
9
|
-
export type LoadStubWorkerResponse = {
|
|
10
|
-
value: unknown;
|
|
11
|
-
} | {
|
|
12
|
-
error: string;
|
|
13
|
-
};
|
|
14
|
-
import type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';
|
|
15
|
-
//# sourceMappingURL=loadStubWorker.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loadStubWorker.d.ts","sourceRoot":"","sources":["../../src/verifyStub/loadStubWorker.js"],"names":[],"mappings":";;;oCAea,IAAI,CAAC,kBAAkB,EAAE,UAAU,GAAG,UAAU,CAAC;;;;;qCAEjD;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE;wCAJX,+BAA+B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loadStubWorker.js","sourceRoot":"","sources":["../../src/verifyStub/loadStubWorker.js"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,oFAAoF;AACpF,qDAAqD;AACrD,EAAE;AACF,uDAAuD;AACvD,EAAE;AACF,EAAE;AAEF,+DAA+D;AAC/D,wDAAwD;AACxD,0DAA0D;AAE1D,0EAA0E;AAC1E;;;;;GAKG;AACH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE3E,IA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EAAE,GAAG,CAAC;IACnG,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;IAED,IAAI,CAAC;QACH,sDAAsD;QACtD,+EAA+E;QAC/E,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,gDAAgD;QAChD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC","sourcesContent":["//\n//\n// This worker is used in tests to verify that generated stub files can be imported,\n// and that the results of importing are as expected.\n//\n// See verifyStub.ts for more about why this is needed.\n//\n//\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\n/** @import { _VerifyStubOptions } from '../types/VerifyStubOptions.js' */\n/**\n * @typedef {Pick<_VerifyStubOptions, 'stubPath' | 'testCode'>} LoadStubWorkerRequest Request to `loadStubWorker.js`\n *\n * @typedef {{ value: unknown } | { error: string }} LoadStubWorkerResponse Response from `loadStubWorker.js`.\n * `value` is the IPC-friendly result of importing the module.\n */\nimport v8 from 'v8';\nimport { isMainThread, parentPort } from 'worker_threads';\nimport { initBrowserEnvironment } from '../worker/initBrowserEnvironment.js';\nimport { cleanUpGlobals, getGlobalProperties } from '../worker/globals.js';\n\nif (isMainThread || !parentPort) {\n throw new Error('This file should only be loaded in a worker thread');\n}\n\n// These modules may expect a browser environment\nawait initBrowserEnvironment();\nconst initialGlobalProperties = getGlobalProperties();\n\n/**\n * Type helper for sending messages to the parent.\n * @param {LoadStubWorkerResponse} message\n */\nfunction emitMessage(message) {\n parentPort?.postMessage(message);\n}\n\nparentPort.on('message', (/** @type {LoadStubWorkerRequest} */ request) => {\n if (typeof request.stubPath !== 'string') {\n emitMessage({\n error: `Unexpected request format: ${JSON.stringify(request)}`,\n });\n } else {\n // Import the stub and send a reponse\n // (`void` ignores the promise result since callbacks can't be async)\n void tryImportStub(request);\n }\n});\n\n/** @param {LoadStubWorkerRequest} request */\nasync function tryImportStub(request) {\n const { stubPath, testCode } = request;\n let moduleExports;\n let step = 'Import';\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n moduleExports = await import(stubPath);\n step = 'Running test code';\n if (testCode) {\n // eslint-disable-next-line @typescript-eslint/no-implied-eval\n new Function('moduleExports', testCode)(moduleExports);\n }\n } catch (err) {\n emitMessage({\n error: `${step} failed: ${/** @type {Error} */ (err).stack || String(err)}`,\n });\n return;\n } finally {\n cleanUpGlobals(initialGlobalProperties);\n }\n\n try {\n emitMessage({ value: serialize(moduleExports) });\n } catch (err) {\n emitMessage({\n error: `Error formatting or serializing exports: ${/** @type {Error} */ (err).stack || String(err)}`,\n });\n }\n}\n\n/**\n * Convert a value into an IPC-serializable form, so it can be passed back to the test and verified.\n * @param {*} value\n * @returns {*}\n */\nfunction serialize(value, depth = 1, encountered = new WeakSet()) {\n if (depth > 3) {\n return '...';\n }\n\n if (value && typeof value === 'object') {\n // track encountered objects to avoid entering circular references\n if (encountered.has(value)) {\n return '[Circular]';\n }\n encountered.add(value);\n\n // Convert non-serializable types\n if (value instanceof Set) {\n return `Set ${JSON.stringify([...value])}`;\n }\n if (value instanceof Map) {\n return `Map ${JSON.stringify(Object.fromEntries(value))}`;\n }\n if (value instanceof Error) {\n return `[${value.name}: ${value.message}]`;\n }\n if (value instanceof RegExp) {\n return value.toString();\n }\n if (Array.isArray(value)) {\n return value.map((v) => serialize(v, depth + 1, encountered));\n }\n if (Object.keys(value).length || value.constructor?.name === 'Object') {\n return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, serialize(v, depth + 1, encountered)]));\n }\n if (value.constructor) {\n return `[${value.constructor.name}]`;\n }\n if (value[Symbol.toStringTag] == 'Module') {\n return '[empty module]';\n }\n }\n\n if (typeof value === 'function') {\n const properties = JSON.stringify({ ...value });\n return `[Function ${value.name || '<anonymous>'}${properties === '{}' ? '' : ` ${properties}`}]`;\n }\n\n if (typeof value === 'symbol') {\n return value.toString();\n }\n\n try {\n // try to serialize the same way postMessage does, per\n // https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist\n v8.serialize(value);\n return value;\n } catch {\n // not serializable, so format the value instead\n return JSON.stringify(value);\n }\n}\n"]}
|