@ms-cloudpack/esm-stub-utilities 0.6.10 → 0.6.11

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.
@@ -37,15 +37,17 @@ function findSandboxRoot(filePath) {
37
37
  const root = path.parse(filePath).root;
38
38
  let dir = path.dirname(filePath);
39
39
  if (!sandboxRoots[dir]) {
40
- // try to find a git root first
40
+ // Try to find a git root first
41
41
  while (dir !== root && !fs.existsSync(path.join(dir, '.git'))) {
42
42
  dir = path.dirname(dir);
43
43
  }
44
44
  if (dir === root) {
45
- // fallback: package root
46
- dir = findPackageRoot(filePath);
45
+ // Fallback: package root. Note that in the case of running cloudpack bundle inside a
46
+ // dependency package folder under node_modules, we need to find the actual package root
47
+ // (rather than the root of the dep) because that's where other modules are installed.
48
+ dir = findPackageRoot(filePath, true /* excludeNodeModules */);
47
49
  if (!dir) {
48
- // final fallback: parent directory
50
+ // Final fallback: parent directory
49
51
  dir = path.dirname(filePath);
50
52
  }
51
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"runInSandbox.js","sourceRoot":"","sources":["../src/runInSandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACvC,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,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SAC3G;KACF;IAED,MAAM,OAAO,GAAG,CAAC,GAAY,EAAE,EAAE;QAC/B,WAAW,CAAC,WAAW,SAAS,8BAA8B,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC;IACpF,CAAC,CAAC;IACF,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;IAEzC,IAAI;QACF,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACnD,OAAO,GAAG,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAY,CAAC;KAC1E;IAAC,OAAO,GAAG,EAAE;QACZ,OAAO,CAAC,GAAG,CAAC,CAAC;KACd;YAAS;QACR,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport { builtinModules } from 'module';\nimport { fileURLToPath } from 'url';\nimport { NodeVM, VMError } from 'vm2';\nimport { findPackageRoot } from '@ms-cloudpack/path-utilities';\n\nconst filename = fileURLToPath(import.meta.url);\nconst dirname = path.dirname(filename);\nconst browserEnvSetup = path.join(path.dirname(dirname), 'browserEnvironment/index.cjs');\n\n/** Allow the sandbox to load these built-in modules */\n// ('events' and 'stream' export classes which other things extend and can't be proxied)\nconst allowedBuiltins = ['buffer', 'events', 'path', 'stream', 'url', 'util'];\n/** Empty mocks of all built-in modules that aren't allowed (to ensure that requires succeed) */\nconst mockBuiltins = Object.fromEntries(builtinModules.filter((m) => !allowedBuiltins.includes(m)).map((m) => [m, {}]));\n\n/** Mapping from sandbox root to VM */\nconst vms: Record<string, NodeVM> = {};\n\n/** Mapping from directory to sandbox root */\nconst sandboxRoots: Record<string, string> = {};\n/** Sandbox root for this file and other cloudpack-related files */\nlet cloudpackSandboxRoot = '';\n\n/** Reset `vms` and `sandboxRoots` for testing */\nexport function _clearSandboxCaches() {\n Object.keys(vms).forEach((k) => delete vms[k]);\n Object.keys(sandboxRoots).forEach((k) => delete sandboxRoots[k]);\n}\n\n/** Get `vms` state for testing */\nexport function _getSandboxVms() {\n return vms;\n}\n\n/**\n * Try to figure out a root directory where the sandbox should be allowed to require files from.\n * This prefers the git root if found, falling back to package root or parent directory.\n *\n * (This is very likely overly permissive and might be good to restrict in the future.)\n */\nfunction findSandboxRoot(filePath: string) {\n const root = path.parse(filePath).root;\n let dir: string | undefined = path.dirname(filePath);\n\n if (!sandboxRoots[dir]) {\n // try to find a git root first\n while (dir !== root && !fs.existsSync(path.join(dir, '.git'))) {\n dir = path.dirname(dir);\n }\n\n if (dir === root) {\n // fallback: package root\n dir = findPackageRoot(filePath);\n if (!dir) {\n // final fallback: parent directory\n dir = path.dirname(filePath);\n }\n }\n\n sandboxRoots[dir] = dir;\n }\n\n return sandboxRoots[dir];\n}\n\nfunction handleError(action: string, err: unknown, sandboxRoot: string) {\n const message = (err as Error).message || String(err);\n const stack = (err as Error).stack?.split(/\\r?\\n/g) || [];\n // instanceof checks don't work with errors from code run within the sandbox, but name is preserved\n const errorName = (err as Error).name;\n\n const newMessage = [`Error ${action}:`, message];\n\n if (stack.length && err instanceof VMError && err.message.includes('Cannot find module')) {\n newMessage.push(\n '',\n 'Ensure that this file exists on disk. If it exists, this may be a bug with the choice of `root` ' +\n '(allowed parent directories) settings in @ms-cloudpack/esm-stub-utilities runInSandbox.',\n `- Root for file being stubbed: ${sandboxRoot}`,\n `- Root for cloudpack files: ${cloudpackSandboxRoot}`,\n );\n\n // Find the actual line which caused the error\n const errorLines = stack.filter((line) => line.trim().startsWith('at') && !line.includes('vm2'));\n if (errorLines.length) {\n newMessage.push('', 'Code that may have required this module:', ...errorLines);\n }\n } else if (errorName === 'TypeError') {\n newMessage.push(\n '',\n 'If the code was referencing a built-in global or a property of a Node built-in module, ' +\n 'this error may be caused by a missing mock or other issue with the sandbox environment ' +\n 'configured in @ms-cloudpack/esm-stub-utilities runInSandbox.',\n );\n } else if (\n errorName === 'SyntaxError' &&\n (/Unexpected token '(import|export)'/.test(message) ||\n message.includes(`'import' and 'export' may appear only with`))\n ) {\n newMessage.push(\n '',\n \"It appears you're trying to load an ES module, which is not currently supported in the sandbox. \" +\n 'If this module is loaded at startup within a CommonJS file, please file an issue with cloudpack. ',\n );\n }\n\n if (stack.length) {\n newMessage.push('', 'Original stack:', ...stack.slice(1));\n }\n\n throw new Error(newMessage.join('\\n'));\n}\n\n/**\n * Run a CommonJS file within a sandboxed environment.\n * @param entryPath - Path to a CommonJS file to run in a sandbox (does not work with ESM files)\n * @returns The module's exports\n */\n// eslint-disable-next-line @typescript-eslint/require-await -- using async to catch async errors\nexport async function runInSandbox(entryPath: string) {\n if (!path.isAbsolute(entryPath)) {\n throw new Error('Entry path must be absolute. Received: ' + entryPath);\n }\n\n if (!fs.existsSync(entryPath)) {\n throw new Error('Entry path does not exist: ' + entryPath);\n }\n\n // Allow code in the sandbox to require files under the requested file's package root,\n // or under its directory if it's not in a package.\n const sandboxRoot = findSandboxRoot(entryPath);\n\n if (!vms[sandboxRoot]) {\n if (!cloudpackSandboxRoot) {\n cloudpackSandboxRoot = findSandboxRoot(filename);\n }\n\n console.debug('esm-stub-utilities: Creating sandbox for ' + sandboxRoot);\n\n try {\n vms[sandboxRoot] = new NodeVM({\n // disable console logging instead of passing through to the host console\n console: 'off',\n // look for these import extensions (mjs is not included because this doesn't support loading ESM)\n sourceExtensions: ['js', 'cjs'],\n // use a commonjs module wrapper\n wrapper: 'commonjs',\n require: {\n // allow local and dependency modules to be required under here\n root: [cloudpackSandboxRoot, sandboxRoot],\n // load, compile, and require modules in the sandbox (instead of requiring in the host)\n context: 'sandbox',\n // allowed built-in modules\n builtin: allowedBuiltins,\n // allow loading \"external\" modules under the given roots\n external: true,\n // simulate the browser at startup (can throw errors)\n import: [browserEnvSetup],\n // mock these modules so they can be successfully required\n mock: {\n ...mockBuiltins,\n // some modules reference these built-ins during their setup\n perf_hooks: {\n performance: { now: () => Date.now() },\n },\n tty: {\n isatty: () => false,\n },\n vm: {\n isContext: () => false,\n },\n },\n },\n env: {\n // prevent debug from accessing process.stderr\n DEBUG_COLORS: 0,\n },\n });\n } catch (err) {\n handleError(`initializing sandbox for ${sandboxRoot} (to create stub of ${entryPath})`, err, sandboxRoot);\n }\n }\n\n const onError = (err: unknown) => {\n handleError(`running ${entryPath} in sandbox (to create stub)`, err, sandboxRoot);\n };\n process.on('uncaughtException', onError);\n\n try {\n const cjsFile = fs.readFileSync(entryPath, 'utf8');\n return vms[sandboxRoot].run(cjsFile, { filename: entryPath }) as unknown;\n } catch (err) {\n onError(err);\n } finally {\n process.off('uncaughtException', onError);\n }\n}\n"]}
1
+ {"version":3,"file":"runInSandbox.js","sourceRoot":"","sources":["../src/runInSandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACvC,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,8BAA8B,CAAC,CAAC;AAEzF,uDAAuD;AACvD,wFAAwF;AACxF,MAAM,eAAe,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAC9E,gGAAgG;AAChG,MAAM,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,CAAC,CAAC;IACF,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;IAEzC,IAAI;QACF,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACnD,OAAO,GAAG,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAY,CAAC;KAC1E;IAAC,OAAO,GAAG,EAAE;QACZ,OAAO,CAAC,GAAG,CAAC,CAAC;KACd;YAAS;QACR,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;KAC3C;AACH,CAAC","sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport { builtinModules } from 'module';\nimport { fileURLToPath } from 'url';\nimport { NodeVM, VMError } from 'vm2';\nimport { findPackageRoot } from '@ms-cloudpack/path-utilities';\n\nconst filename = fileURLToPath(import.meta.url);\nconst dirname = path.dirname(filename);\nconst browserEnvSetup = path.join(path.dirname(dirname), 'browserEnvironment/index.cjs');\n\n/** Allow the sandbox to load these built-in modules */\n// ('events' and 'stream' export classes which other things extend and can't be proxied)\nconst allowedBuiltins = ['buffer', 'events', 'path', 'stream', 'url', 'util'];\n/** Empty mocks of all built-in modules that aren't allowed (to ensure that requires succeed) */\nconst mockBuiltins = Object.fromEntries(builtinModules.filter((m) => !allowedBuiltins.includes(m)).map((m) => [m, {}]));\n\n/** Mapping from sandbox root to VM */\nconst vms: Record<string, NodeVM> = {};\n\n/** Mapping from directory to sandbox root */\nconst sandboxRoots: Record<string, string> = {};\n/** Sandbox root for this file and other cloudpack-related files */\nlet cloudpackSandboxRoot = '';\n\n/** Reset `vms` and `sandboxRoots` for testing */\nexport function _clearSandboxCaches() {\n Object.keys(vms).forEach((k) => delete vms[k]);\n Object.keys(sandboxRoots).forEach((k) => delete sandboxRoots[k]);\n}\n\n/** Get `vms` state for testing */\nexport function _getSandboxVms() {\n return vms;\n}\n\n/**\n * Try to figure out a root directory where the sandbox should be allowed to require files from.\n * This prefers the git root if found, falling back to package root or parent directory.\n *\n * (This is very likely overly permissive and might be good to restrict in the future.)\n */\nfunction findSandboxRoot(filePath: string) {\n const root = path.parse(filePath).root;\n let dir: string | undefined = path.dirname(filePath);\n\n if (!sandboxRoots[dir]) {\n // Try to find a git root first\n while (dir !== root && !fs.existsSync(path.join(dir, '.git'))) {\n dir = path.dirname(dir);\n }\n\n if (dir === root) {\n // Fallback: package root. Note that in the case of running cloudpack bundle inside a\n // dependency package folder under node_modules, we need to find the actual package root\n // (rather than the root of the dep) because that's where other modules are installed.\n dir = findPackageRoot(filePath, true /* excludeNodeModules */);\n if (!dir) {\n // Final fallback: parent directory\n dir = path.dirname(filePath);\n }\n }\n\n sandboxRoots[dir] = dir;\n }\n\n return sandboxRoots[dir];\n}\n\nfunction handleError(action: string, err: unknown, sandboxRoot: string) {\n const message = (err as Error).message || String(err);\n const stack = (err as Error).stack?.split(/\\r?\\n/g) || [];\n // instanceof checks don't work with errors from code run within the sandbox, but name is preserved\n const errorName = (err as Error).name;\n\n const newMessage = [`Error ${action}:`, message];\n\n if (stack.length && err instanceof VMError && err.message.includes('Cannot find module')) {\n newMessage.push(\n '',\n 'Ensure that this file exists on disk. If it exists, this may be a bug with the choice of `root` ' +\n '(allowed parent directories) settings in @ms-cloudpack/esm-stub-utilities runInSandbox.',\n `- Root for file being stubbed: ${sandboxRoot}`,\n `- Root for cloudpack files: ${cloudpackSandboxRoot}`,\n );\n\n // Find the actual line which caused the error\n const errorLines = stack.filter((line) => line.trim().startsWith('at') && !line.includes('vm2'));\n if (errorLines.length) {\n newMessage.push('', 'Code that may have required this module:', ...errorLines);\n }\n } else if (errorName === 'TypeError') {\n newMessage.push(\n '',\n 'If the code was referencing a built-in global or a property of a Node built-in module, ' +\n 'this error may be caused by a missing mock or other issue with the sandbox environment ' +\n 'configured in @ms-cloudpack/esm-stub-utilities runInSandbox.',\n );\n } else if (\n errorName === 'SyntaxError' &&\n (/Unexpected token '(import|export)'/.test(message) ||\n message.includes(`'import' and 'export' may appear only with`))\n ) {\n newMessage.push(\n '',\n \"It appears you're trying to load an ES module, which is not currently supported in the sandbox. \" +\n 'If this module is loaded at startup within a CommonJS file, please file an issue with cloudpack. ',\n );\n }\n\n if (stack.length) {\n newMessage.push('', 'Original stack:', ...stack.slice(1));\n }\n\n throw new Error(newMessage.join('\\n'));\n}\n\n/**\n * Run a CommonJS file within a sandboxed environment.\n * @param entryPath - Path to a CommonJS file to run in a sandbox (does not work with ESM files)\n * @returns The module's exports\n */\n// eslint-disable-next-line @typescript-eslint/require-await -- using async to catch async errors\nexport async function runInSandbox(entryPath: string) {\n if (!path.isAbsolute(entryPath)) {\n throw new Error('Entry path must be absolute. Received: ' + entryPath);\n }\n\n if (!fs.existsSync(entryPath)) {\n throw new Error('Entry path does not exist: ' + entryPath);\n }\n\n // Allow code in the sandbox to require files under the requested file's package root,\n // or under its directory if it's not in a package.\n const sandboxRoot = findSandboxRoot(entryPath);\n\n if (!vms[sandboxRoot]) {\n if (!cloudpackSandboxRoot) {\n cloudpackSandboxRoot = findSandboxRoot(filename);\n }\n\n console.debug('esm-stub-utilities: Creating sandbox for ' + sandboxRoot);\n\n try {\n vms[sandboxRoot] = new NodeVM({\n // disable console logging instead of passing through to the host console\n console: 'off',\n // look for these import extensions (mjs is not included because this doesn't support loading ESM)\n sourceExtensions: ['js', 'cjs'],\n // use a commonjs module wrapper\n wrapper: 'commonjs',\n require: {\n // allow local and dependency modules to be required under here\n root: [cloudpackSandboxRoot, sandboxRoot],\n // load, compile, and require modules in the sandbox (instead of requiring in the host)\n context: 'sandbox',\n // allowed built-in modules\n builtin: allowedBuiltins,\n // allow loading \"external\" modules under the given roots\n external: true,\n // simulate the browser at startup (can throw errors)\n import: [browserEnvSetup],\n // mock these modules so they can be successfully required\n mock: {\n ...mockBuiltins,\n // some modules reference these built-ins during their setup\n perf_hooks: {\n performance: { now: () => Date.now() },\n },\n tty: {\n isatty: () => false,\n },\n vm: {\n isContext: () => false,\n },\n },\n },\n env: {\n // prevent debug from accessing process.stderr\n DEBUG_COLORS: 0,\n },\n });\n } catch (err) {\n handleError(`initializing sandbox for ${sandboxRoot} (to create stub of ${entryPath})`, err, sandboxRoot);\n }\n }\n\n const onError = (err: unknown) => {\n handleError(`running ${entryPath} in sandbox (to create stub)`, err, sandboxRoot);\n };\n process.on('uncaughtException', onError);\n\n try {\n const cjsFile = fs.readFileSync(entryPath, 'utf8');\n return vms[sandboxRoot].run(cjsFile, { filename: entryPath }) as unknown;\n } catch (err) {\n onError(err);\n } finally {\n process.off('uncaughtException', onError);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/esm-stub-utilities",
3
- "version": "0.6.10",
3
+ "version": "0.6.11",
4
4
  "description": "Generates ESM stubs for CommonJS entry files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@ms-cloudpack/json-utilities": "^0.0.6",
17
- "@ms-cloudpack/path-utilities": "^2.1.2",
17
+ "@ms-cloudpack/path-utilities": "^2.2.0",
18
18
  "@ms-cloudpack/path-string-parsing": "^1.0.1",
19
19
  "atob": "^2.1.2",
20
20
  "btoa": "^1.2.1",