@ms-cloudpack/esm-stub-utilities 0.15.49 → 0.16.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processError.d.ts","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"processError.d.ts","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAWnH;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACnC,gGAAgG;IAChG,QAAQ,EAAE,MAAM,CAAC;IACjB,sCAAsC;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,OAAO,CAAC;IACf,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;CACnB,GAAG,aAAa,CA2GhB"}
|
package/lib/processError.js
CHANGED
|
@@ -2,7 +2,6 @@ import { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing'
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
/** Some known error codes that can occur when running a file in a sandbox. */
|
|
4
4
|
const errorCodes = {
|
|
5
|
-
requireESM: 'ERR_REQUIRE_ESM',
|
|
6
5
|
moduleNotFound: 'MODULE_NOT_FOUND',
|
|
7
6
|
};
|
|
8
7
|
const esmPackageName = 'esm-stub-utilities';
|
|
@@ -18,13 +17,7 @@ export function processError(params) {
|
|
|
18
17
|
let lineNumber;
|
|
19
18
|
let columnNumber;
|
|
20
19
|
let message = originalMessage;
|
|
21
|
-
if (code === errorCodes.
|
|
22
|
-
// Remove the instruction to change to a dynamic import, since it's less likely to be applicable here.
|
|
23
|
-
// Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/esm.mjs not supported.
|
|
24
|
-
// Instead change the require of [...]/esm.mjs to a dynamic import() which is available in all CommonJS modules.
|
|
25
|
-
message = message.split('\n')[0];
|
|
26
|
-
}
|
|
27
|
-
else if (code === errorCodes.moduleNotFound && requireStack) {
|
|
20
|
+
if (code === errorCodes.moduleNotFound && requireStack) {
|
|
28
21
|
// Module not found errors have a require stack included in the error message, as well as a
|
|
29
22
|
// requireStack property. Remove the stack from the message and rebuild it with filtering.
|
|
30
23
|
// Error: Cannot find module 'foo'
|
package/lib/processError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processError.js","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,8EAA8E;AAC9E,MAAM,UAAU,GAAG;IACjB,UAAU,EAAE,iBAAiB;IAC7B,cAAc,EAAE,kBAAkB;CACnC,CAAC;AAEF,MAAM,cAAc,GAAG,oBAAoB,CAAC;AAC5C,MAAM,iBAAiB,GAAG,aAAa,CAAC;AACxC,MAAM,eAAe,GAAG,gBAAgB,CAAC;AAEzC;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,MAQ5B;IACC,MAAM,EAAE,QA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C,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,KAAK,UAAU,CAAC,cAAc,EAAE,CAAC;QAChG,+BAA+B;QAC/B,OAAO,IAAI,gDAAgD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IAChF,CAAC;IAED,OAAO;QACL,IAAI,EAAE,kCAAkC,QAAQ,OAAO,OAAO,EAAE;QAChE,QAAQ,EAAE;YACR,IAAI,EAAE,qBAAqB,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;YACpE,IAAI,EAAE,UAAU;YAChB,MAAM,EAAE,YAAY;SACrB;QACD,MAAM,EAAE,qBAAqB;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\n\nexport type ErrorLike = { name?: string; message: string; stack?: string; code?: string; requireStack?: string[] };\n\n/** Some known error codes that can occur when running a file in a sandbox. */\nconst errorCodes = {\n requireESM: 'ERR_REQUIRE_ESM',\n moduleNotFound: 'MODULE_NOT_FOUND',\n};\n\nconst esmPackageName = 'esm-stub-utilities';\nconst workerPackageName = 'worker-pool';\nconst workerEntryName = 'workerEntry.js';\n\n/**\n * Process a raw error into a form which may be more useful for debugging.\n */\nexport function processError(params: {\n /** Entry key in the format of `BundleOptions.entries`: forward slashes, leading `./`, no ext */\n entryKey: string;\n /** Absolute path to the entry file */\n entryFullPath: string;\n error: unknown;\n /** Input path from `BundleOptions` */\n inputPath: string;\n}): BundleMessage {\n const { entryKey, entryFullPath, error: errorParam, inputPath } = params;\n const error = (errorParam as Error).message ? (errorParam as ErrorLike) : { message: String(errorParam) };\n\n const { message: originalMessage, name, code, stack: originalStack, requireStack } = error;\n\n let lineNumber: number | undefined;\n let columnNumber: number | undefined;\n let message = originalMessage;\n\n if (code === errorCodes.requireESM) {\n // Remove the instruction to change to a dynamic import, since it's less likely to be applicable here.\n // Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/esm.mjs not supported.\n // Instead change the require of [...]/esm.mjs to a dynamic import() which is available in all CommonJS modules.\n message = message.split('\\n')[0];\n } else if (code === errorCodes.moduleNotFound && requireStack) {\n // Module not found errors have a require stack included in the error message, as well as a\n // requireStack property. Remove the stack from the message and rebuild it with filtering.\n // Error: Cannot find module 'foo'\n // Require stack:\n // - [...]/index.js\n // - [...]/generateESMStubFromCJS.js\n message = message.split('\\n')[0];\n // remove the generateESMStubFromCJS require line (not interesting)\n const filteredStack = requireStack.filter(\n (line) => !line.includes(esmPackageName) && !line.includes(workerPackageName),\n );\n if (filteredStack.length > 1) {\n // Only include the stack if it goes beyond the original file\n message += `.\\nRequire stack:\\n${filteredStack.map((line) => ` - ${line}`).join('\\n')}`;\n }\n } else if (name === 'SyntaxError' && originalStack) {\n // SyntaxErrors will have a `stack` like this:\n // /path/to/file.js:1\n // oh no\n // ^^\n // SyntaxError: Unexpected identifier\n // at ....\n // Use the first part as the message, but remove the stack trace (will be added back later if needed).\n message = originalStack.split(/\\n\\s+at /)[0];\n\n // If the error is in the entry point file, get the line and possibly column number.\n // (There won't be a stack line for the file with the syntax error, so the normal logic to get\n // the line and column later won't work.)\n const lineNumberMatch = message.match(/^(.*?):(\\d+)\\n/);\n if (lineNumberMatch && slash(lineNumberMatch[1]).includes(slash(entryFullPath))) {\n lineNumber = Number(lineNumberMatch[2]);\n\n // Get the column if the stack includes ^^ indicating the error location\n // (not all of them will have this, e.g. \"unexpected end of input\" errors won't)\n const columnNumberMatch = message.match(/^\\s*\\^/m);\n if (columnNumberMatch) {\n columnNumber = columnNumberMatch[0].length - 1;\n }\n }\n\n // This happens if a file is inferred from context or extension to be CJS but is actually ESM.\n if (/Unexpected token '(import|export)'/.test(message)) {\n message = `File appears to be an ES module that was loaded with require:\\n${message}`;\n }\n } else if (name !== 'Error') {\n message = `${name}: ${message}`;\n }\n\n let stack: string[] | undefined;\n if (originalStack) {\n // Get stack frame lines from the original error stack, and remove parts that are less likely\n // to be useful (node internals, and jest internals for errors while running tests).\n stack = originalStack\n .split('\\n')\n .filter((line) => /^\\s+at /.test(line) && !/\\(node:|jest-runtime|jest-resolve/.test(line));\n\n // The user probably only cares about the error location in the original code,\n // so make it easier to see that by removing all the esm-stub-utilities frames.\n const workerEntryIndex = stack.findIndex((line) => line.includes(workerEntryName));\n if (workerEntryIndex !== -1) {\n // Special case: if the error came from the file calling process.exit(), the most recent stack\n // frame will be from workerEntry.js, and the real frames will be after that.\n stack = stack.slice(workerEntryIndex + 1);\n }\n // Find the first esm-stub-utilities line and remove that and everything after.\n const utilsIndex = stack.findIndex((line) => line.includes(esmPackageName));\n if (utilsIndex !== -1) {\n stack = stack.slice(0, utilsIndex);\n }\n }\n\n // See if we can find the line and column in the entry file from the stack trace\n const locationMatch = stack?.find((line) => slash(line).includes(slash(entryFullPath)))?.match(/:(\\d+):(\\d+)\\)$/);\n if (locationMatch) {\n lineNumber = Number(locationMatch[1]);\n columnNumber = Number(locationMatch[2]);\n }\n\n // Include the stack if present, provided it's not just the entry file,\n // and this isn't a module not found error which already includes a require stack.\n // (If the only interesting stack line is the entry file, this is redundant with the returned\n // `location` object.)\n if (stack?.length && (stack.length > 1 || !locationMatch) && code !== errorCodes.moduleNotFound) {\n // Add the stack to the message\n message += `\\n\\nPartial stack (node internals omitted):\\n${stack.join('\\n')}`;\n }\n\n return {\n text: `Error creating stub for entry \"${entryKey}\":\\n${message}`,\n location: {\n file: normalizeRelativePath(path.relative(inputPath, entryFullPath)),\n line: lineNumber,\n column: columnNumber,\n },\n source: 'ESM stub generation',\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"processError.js","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,8EAA8E;AAC9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F;IAC7F,sBAAsB;IACtB,IAAI,KAAK,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,KAAK,UAAU,CAAC,cAAc,EAAE,CAAC;QAChG,+BAA+B;QAC/B,OAAO,IAAI,gDAAgD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IAChF,CAAC;IAED,OAAO;QACL,IAAI,EAAE,kCAAkC,QAAQ,OAAO,OAAO,EAAE;QAChE,QAAQ,EAAE;YACR,IAAI,EAAE,qBAAqB,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;YACpE,IAAI,EAAE,UAAU;YAChB,MAAM,EAAE,YAAY;SACrB;QACD,MAAM,EAAE,qBAAqB;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\n\nexport type ErrorLike = { name?: string; message: string; stack?: string; code?: string; requireStack?: string[] };\n\n/** Some known error codes that can occur when running a file in a sandbox. */\nconst errorCodes = {\n moduleNotFound: 'MODULE_NOT_FOUND',\n};\n\nconst esmPackageName = 'esm-stub-utilities';\nconst workerPackageName = 'worker-pool';\nconst workerEntryName = 'workerEntry.js';\n\n/**\n * Process a raw error into a form which may be more useful for debugging.\n */\nexport function processError(params: {\n /** Entry key in the format of `BundleOptions.entries`: forward slashes, leading `./`, no ext */\n entryKey: string;\n /** Absolute path to the entry file */\n entryFullPath: string;\n error: unknown;\n /** Input path from `BundleOptions` */\n inputPath: string;\n}): BundleMessage {\n const { entryKey, entryFullPath, error: errorParam, inputPath } = params;\n const error = (errorParam as Error).message ? (errorParam as ErrorLike) : { message: String(errorParam) };\n\n const { message: originalMessage, name, code, stack: originalStack, requireStack } = error;\n\n let lineNumber: number | undefined;\n let columnNumber: number | undefined;\n let message = originalMessage;\n\n if (code === errorCodes.moduleNotFound && requireStack) {\n // Module not found errors have a require stack included in the error message, as well as a\n // requireStack property. Remove the stack from the message and rebuild it with filtering.\n // Error: Cannot find module 'foo'\n // Require stack:\n // - [...]/index.js\n // - [...]/generateESMStubFromCJS.js\n message = message.split('\\n')[0];\n // remove the generateESMStubFromCJS require line (not interesting)\n const filteredStack = requireStack.filter(\n (line) => !line.includes(esmPackageName) && !line.includes(workerPackageName),\n );\n if (filteredStack.length > 1) {\n // Only include the stack if it goes beyond the original file\n message += `.\\nRequire stack:\\n${filteredStack.map((line) => ` - ${line}`).join('\\n')}`;\n }\n } else if (name === 'SyntaxError' && originalStack) {\n // SyntaxErrors will have a `stack` like this:\n // /path/to/file.js:1\n // oh no\n // ^^\n // SyntaxError: Unexpected identifier\n // at ....\n // Use the first part as the message, but remove the stack trace (will be added back later if needed).\n message = originalStack.split(/\\n\\s+at /)[0];\n\n // If the error is in the entry point file, get the line and possibly column number.\n // (There won't be a stack line for the file with the syntax error, so the normal logic to get\n // the line and column later won't work.)\n const lineNumberMatch = message.match(/^(.*?):(\\d+)\\n/);\n if (lineNumberMatch && slash(lineNumberMatch[1]).includes(slash(entryFullPath))) {\n lineNumber = Number(lineNumberMatch[2]);\n\n // Get the column if the stack includes ^^ indicating the error location\n // (not all of them will have this, e.g. \"unexpected end of input\" errors won't)\n const columnNumberMatch = message.match(/^\\s*\\^/m);\n if (columnNumberMatch) {\n columnNumber = columnNumberMatch[0].length - 1;\n }\n }\n\n // This happens if a file is inferred from context or extension to be CJS but is actually ESM.\n if (/Unexpected token '(import|export)'/.test(message)) {\n message = `File appears to be an ES module that was loaded with require:\\n${message}`;\n }\n } else if (name !== 'Error') {\n message = `${name}: ${message}`;\n }\n\n let stack: string[] | undefined;\n if (originalStack) {\n // Get stack frame lines from the original error stack, and remove parts that are less likely\n // to be useful (node internals, and jest internals for errors while running tests).\n stack = originalStack\n .split('\\n')\n .filter((line) => /^\\s+at /.test(line) && !/\\(node:|jest-runtime|jest-resolve/.test(line));\n\n // The user probably only cares about the error location in the original code,\n // so make it easier to see that by removing all the esm-stub-utilities frames.\n const workerEntryIndex = stack.findIndex((line) => line.includes(workerEntryName));\n if (workerEntryIndex !== -1) {\n // Special case: if the error came from the file calling process.exit(), the most recent stack\n // frame will be from workerEntry.js, and the real frames will be after that.\n stack = stack.slice(workerEntryIndex + 1);\n }\n // Find the first esm-stub-utilities line and remove that and everything after.\n const utilsIndex = stack.findIndex((line) => line.includes(esmPackageName));\n if (utilsIndex !== -1) {\n stack = stack.slice(0, utilsIndex);\n }\n }\n\n // See if we can find the line and column in the entry file from the stack trace\n const locationMatch = stack?.find((line) => slash(line).includes(slash(entryFullPath)))?.match(/:(\\d+):(\\d+)\\)$/);\n if (locationMatch) {\n lineNumber = Number(locationMatch[1]);\n columnNumber = Number(locationMatch[2]);\n }\n\n // Include the stack if present, provided it's not just the entry file,\n // and this isn't a module not found error which already includes a require stack.\n // (If the only interesting stack line is the entry file, this is redundant with the returned\n // `location` object.)\n if (stack?.length && (stack.length > 1 || !locationMatch) && code !== errorCodes.moduleNotFound) {\n // Add the stack to the message\n message += `\\n\\nPartial stack (node internals omitted):\\n${stack.join('\\n')}`;\n }\n\n return {\n text: `Error creating stub for entry \"${entryKey}\":\\n${message}`,\n location: {\n file: normalizeRelativePath(path.relative(inputPath, entryFullPath)),\n line: lineNumber,\n column: columnNumber,\n },\n source: 'ESM stub generation',\n };\n}\n"]}
|
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
// See verifyStub.ts for more about why this is needed.
|
|
7
7
|
//
|
|
8
8
|
//
|
|
9
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
10
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
11
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
12
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return path;
|
|
16
|
+
};
|
|
9
17
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
10
18
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
11
19
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
@@ -38,7 +46,7 @@ async function tryImportStub(request) {
|
|
|
38
46
|
let step = 'Import';
|
|
39
47
|
try {
|
|
40
48
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
41
|
-
moduleExports = await import(stubPath);
|
|
49
|
+
moduleExports = await import(__rewriteRelativeImportExtension(stubPath));
|
|
42
50
|
step = 'Running test code';
|
|
43
51
|
if (testCode) {
|
|
44
52
|
// eslint-disable-next-line
|
|
@@ -1 +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
|
|
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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AAC,EAAE,KAAK,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9G,CAAC;QACD,IAAI,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\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/esm-stub-utilities",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"@ms-cloudpack/common-types": "^0.33.3",
|
|
29
29
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
30
30
|
"@ms-cloudpack/json-utilities": "^0.1.11",
|
|
31
|
-
"@ms-cloudpack/package-utilities": "^13.7.
|
|
31
|
+
"@ms-cloudpack/package-utilities": "^13.7.3",
|
|
32
32
|
"@ms-cloudpack/path-string-parsing": "^1.3.0",
|
|
33
|
-
"@ms-cloudpack/worker-pool": "^0.
|
|
34
|
-
"oxc-parser": "^0.
|
|
33
|
+
"@ms-cloudpack/worker-pool": "^0.5.0",
|
|
34
|
+
"oxc-parser": "^0.119.0",
|
|
35
35
|
"regenerator-runtime": "^0.14.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|