@secure-exec/nodejs 0.2.0-rc.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.
Files changed (68) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +7 -0
  3. package/dist/bindings.d.ts +31 -0
  4. package/dist/bindings.js +67 -0
  5. package/dist/bridge/active-handles.d.ts +22 -0
  6. package/dist/bridge/active-handles.js +112 -0
  7. package/dist/bridge/child-process.d.ts +99 -0
  8. package/dist/bridge/child-process.js +672 -0
  9. package/dist/bridge/dispatch.d.ts +2 -0
  10. package/dist/bridge/dispatch.js +40 -0
  11. package/dist/bridge/fs.d.ts +502 -0
  12. package/dist/bridge/fs.js +3307 -0
  13. package/dist/bridge/index.d.ts +10 -0
  14. package/dist/bridge/index.js +41 -0
  15. package/dist/bridge/module.d.ts +75 -0
  16. package/dist/bridge/module.js +325 -0
  17. package/dist/bridge/network.d.ts +1093 -0
  18. package/dist/bridge/network.js +8651 -0
  19. package/dist/bridge/os.d.ts +13 -0
  20. package/dist/bridge/os.js +256 -0
  21. package/dist/bridge/polyfills.d.ts +9 -0
  22. package/dist/bridge/polyfills.js +67 -0
  23. package/dist/bridge/process.d.ts +121 -0
  24. package/dist/bridge/process.js +1382 -0
  25. package/dist/bridge/whatwg-url.d.ts +67 -0
  26. package/dist/bridge/whatwg-url.js +712 -0
  27. package/dist/bridge-contract.d.ts +774 -0
  28. package/dist/bridge-contract.js +172 -0
  29. package/dist/bridge-handlers.d.ts +199 -0
  30. package/dist/bridge-handlers.js +4263 -0
  31. package/dist/bridge-loader.d.ts +9 -0
  32. package/dist/bridge-loader.js +87 -0
  33. package/dist/bridge-setup.d.ts +1 -0
  34. package/dist/bridge-setup.js +3 -0
  35. package/dist/bridge.js +21652 -0
  36. package/dist/builtin-modules.d.ts +25 -0
  37. package/dist/builtin-modules.js +312 -0
  38. package/dist/default-network-adapter.d.ts +13 -0
  39. package/dist/default-network-adapter.js +351 -0
  40. package/dist/driver.d.ts +87 -0
  41. package/dist/driver.js +191 -0
  42. package/dist/esm-compiler.d.ts +14 -0
  43. package/dist/esm-compiler.js +68 -0
  44. package/dist/execution-driver.d.ts +37 -0
  45. package/dist/execution-driver.js +977 -0
  46. package/dist/host-network-adapter.d.ts +7 -0
  47. package/dist/host-network-adapter.js +279 -0
  48. package/dist/index.d.ts +20 -0
  49. package/dist/index.js +23 -0
  50. package/dist/isolate-bootstrap.d.ts +86 -0
  51. package/dist/isolate-bootstrap.js +125 -0
  52. package/dist/ivm-compat.d.ts +7 -0
  53. package/dist/ivm-compat.js +31 -0
  54. package/dist/kernel-runtime.d.ts +58 -0
  55. package/dist/kernel-runtime.js +535 -0
  56. package/dist/module-access.d.ts +75 -0
  57. package/dist/module-access.js +606 -0
  58. package/dist/module-resolver.d.ts +8 -0
  59. package/dist/module-resolver.js +150 -0
  60. package/dist/os-filesystem.d.ts +42 -0
  61. package/dist/os-filesystem.js +161 -0
  62. package/dist/package-bundler.d.ts +36 -0
  63. package/dist/package-bundler.js +497 -0
  64. package/dist/polyfills.d.ts +17 -0
  65. package/dist/polyfills.js +97 -0
  66. package/dist/worker-adapter.d.ts +21 -0
  67. package/dist/worker-adapter.js +34 -0
  68. package/package.json +123 -0
@@ -0,0 +1,10 @@
1
+ import "./polyfills.js";
2
+ import { _registerHandle, _unregisterHandle, _waitForActiveHandles, _getActiveHandles } from "./active-handles.js";
3
+ import fs from "./fs.js";
4
+ import os from "./os.js";
5
+ import * as childProcess from "./child-process.js";
6
+ import * as network from "./network.js";
7
+ import process, { setupGlobals, setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, URL, URLSearchParams, TextEncoder, TextDecoder, Buffer, cryptoPolyfill, ProcessExitError } from "./process.js";
8
+ import moduleModule, { createRequire, Module, SourceMap } from "./module.js";
9
+ export { fs, os, childProcess, process, moduleModule as module, network, setupGlobals, setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, URL, URLSearchParams, TextEncoder, TextDecoder, Buffer, cryptoPolyfill, ProcessExitError, createRequire, Module, SourceMap, _registerHandle, _unregisterHandle, _waitForActiveHandles, _getActiveHandles, };
10
+ export default fs;
@@ -0,0 +1,41 @@
1
+ // Bridge module entry point
2
+ // This file is compiled to a single JS bundle that gets injected into the isolate
3
+ //
4
+ // Each module provides polyfills for Node.js built-in modules that need to
5
+ // communicate with the host environment via bridge functions.
6
+ // IMPORTANT: Import polyfills FIRST before any other modules!
7
+ // Some packages (like whatwg-url) use TextEncoder/TextDecoder at module load time.
8
+ // This import installs them on globalThis before other imports execute.
9
+ import "./polyfills.js";
10
+ // Active handles mechanism - must be imported early so other modules can use it.
11
+ // See: docs-internal/node/ACTIVE_HANDLES.md
12
+ import { _registerHandle, _unregisterHandle, _waitForActiveHandles, _getActiveHandles, } from "./active-handles.js";
13
+ // File system module
14
+ import fs from "./fs.js";
15
+ // Operating system module
16
+ import os from "./os.js";
17
+ // Child process module
18
+ import * as childProcess from "./child-process.js";
19
+ // Network modules (fetch, dns, http, https)
20
+ import * as network from "./network.js";
21
+ // Process and global polyfills
22
+ import process, { setupGlobals, setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, URL, URLSearchParams, TextEncoder, TextDecoder, Buffer, cryptoPolyfill, ProcessExitError, } from "./process.js";
23
+ // Module system (createRequire, Module class)
24
+ import moduleModule, { createRequire, Module, SourceMap } from "./module.js";
25
+ // Export all modules
26
+ export {
27
+ // Core modules
28
+ fs, os, childProcess, process, moduleModule as module,
29
+ // Network
30
+ network,
31
+ // Process globals
32
+ setupGlobals, setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, URL, URLSearchParams, TextEncoder, TextDecoder, Buffer, cryptoPolyfill, ProcessExitError,
33
+ // Module utilities
34
+ createRequire, Module, SourceMap,
35
+ // Active handles (see docs-internal/node/ACTIVE_HANDLES.md)
36
+ _registerHandle, _unregisterHandle, _waitForActiveHandles, _getActiveHandles, };
37
+ // Default export is fs for backward compatibility
38
+ export default fs;
39
+ // Auto-setup globals when bridge loads
40
+ // This installs process, timers, URL, Buffer, crypto, etc. on globalThis
41
+ setupGlobals();
@@ -0,0 +1,75 @@
1
+ interface RequireFunction {
2
+ (request: string): unknown;
3
+ resolve: RequireResolve;
4
+ cache: Record<string, {
5
+ exports: unknown;
6
+ }>;
7
+ main: Module | undefined;
8
+ extensions: Record<string, (module: Module, filename: string) => void>;
9
+ }
10
+ interface RequireResolve {
11
+ (request: string, options?: {
12
+ paths?: string[];
13
+ }): string;
14
+ paths: (request: string) => string[] | null;
15
+ }
16
+ /**
17
+ * Create a require function that resolves relative to the given filename.
18
+ * This mimics Node.js's module.createRequire(filename).
19
+ */
20
+ export declare function createRequire(filename: string | URL): RequireFunction;
21
+ /**
22
+ * Polyfill of Node.js `Module` class for sandbox compatibility. Provides
23
+ * `_compile`, `_resolveFilename`, `_load`, `_extensions`, and `_cache` statics
24
+ * that npm tooling (promzard, resolve, etc.) relies on.
25
+ */
26
+ export declare class Module {
27
+ id: string;
28
+ path: string;
29
+ exports: unknown;
30
+ filename: string;
31
+ loaded: boolean;
32
+ children: Module[];
33
+ paths: string[];
34
+ parent: Module | null | undefined;
35
+ isPreloading: boolean;
36
+ constructor(id: string, parent?: Module | null);
37
+ require(request: string): unknown;
38
+ _compile(content: string, filename: string): unknown;
39
+ static _extensions: Record<string, (module: Module, filename: string) => void>;
40
+ static _cache: Record<string, {
41
+ exports: unknown;
42
+ }>;
43
+ static _resolveFilename(request: string, parent: Module | null | undefined, _isMain?: boolean, _options?: unknown): string;
44
+ static wrap(content: string): string;
45
+ static builtinModules: string[];
46
+ static isBuiltin(moduleName: string): boolean;
47
+ static createRequire: typeof createRequire;
48
+ static syncBuiltinESMExports(): void;
49
+ static findSourceMap(_path: string): undefined;
50
+ static _nodeModulePaths(from: string): string[];
51
+ static _load(request: string, parent: Module | null | undefined, _isMain?: boolean): unknown;
52
+ static runMain(): void;
53
+ }
54
+ export declare class SourceMap {
55
+ constructor(_payload: unknown);
56
+ get payload(): never;
57
+ set payload(_value: unknown);
58
+ findEntry(_line: number, _column: number): never;
59
+ }
60
+ declare const moduleModule: {
61
+ Module: typeof Module;
62
+ createRequire: typeof createRequire;
63
+ _extensions: Record<string, (module: Module, filename: string) => void>;
64
+ _cache: Record<string, {
65
+ exports: unknown;
66
+ }>;
67
+ builtinModules: string[];
68
+ isBuiltin: typeof Module.isBuiltin;
69
+ _resolveFilename: typeof Module._resolveFilename;
70
+ wrap: typeof Module.wrap;
71
+ syncBuiltinESMExports: typeof Module.syncBuiltinESMExports;
72
+ findSourceMap: typeof Module.findSourceMap;
73
+ SourceMap: typeof SourceMap;
74
+ };
75
+ export default moduleModule;
@@ -0,0 +1,325 @@
1
+ import { exposeCustomGlobal, exposeMutableRuntimeStateGlobal, } from "@secure-exec/core/internal/shared/global-exposure";
2
+ // Module polyfill for the sandbox
3
+ // Provides module.createRequire and other module utilities for npm compatibility
4
+ // Seed the mutable CommonJS loader state before requireSetup runs.
5
+ const initialModuleCache = {};
6
+ const initialPendingModules = {};
7
+ const initialCurrentModule = {
8
+ id: "/<entry>.js",
9
+ filename: "/<entry>.js",
10
+ dirname: "/",
11
+ exports: {},
12
+ loaded: false,
13
+ };
14
+ exposeMutableRuntimeStateGlobal("_moduleCache", initialModuleCache);
15
+ exposeMutableRuntimeStateGlobal("_pendingModules", initialPendingModules);
16
+ exposeMutableRuntimeStateGlobal("_currentModule", initialCurrentModule);
17
+ // Path utilities for module resolution
18
+ function _pathDirname(p) {
19
+ const lastSlash = p.lastIndexOf("/");
20
+ if (lastSlash === -1)
21
+ return ".";
22
+ if (lastSlash === 0)
23
+ return "/";
24
+ return p.slice(0, lastSlash);
25
+ }
26
+ function _parseFileUrl(url) {
27
+ // Handle file:// URLs
28
+ if (url.startsWith("file://")) {
29
+ // Remove file:// prefix
30
+ let path = url.slice(7);
31
+ // Handle file:///path on Unix (3 slashes = absolute path)
32
+ if (path.startsWith("/")) {
33
+ return path;
34
+ }
35
+ // Handle file://host/path (rare, treat host as empty)
36
+ return "/" + path;
37
+ }
38
+ return url;
39
+ }
40
+ /**
41
+ * Create a require function that resolves relative to the given filename.
42
+ * This mimics Node.js's module.createRequire(filename).
43
+ */
44
+ export function createRequire(filename) {
45
+ if (typeof filename !== "string" && !(filename instanceof URL)) {
46
+ throw new TypeError("filename must be a string or URL");
47
+ }
48
+ // Parse file:// URLs
49
+ const filepath = _parseFileUrl(String(filename));
50
+ const dirname = _pathDirname(filepath);
51
+ const builtins = [
52
+ "fs",
53
+ "path",
54
+ "os",
55
+ "events",
56
+ "util",
57
+ "http",
58
+ "_http_common",
59
+ "https",
60
+ "dns",
61
+ "dgram",
62
+ "child_process",
63
+ "stream",
64
+ "buffer",
65
+ "url",
66
+ "querystring",
67
+ "crypto",
68
+ "zlib",
69
+ "assert",
70
+ "tty",
71
+ "net",
72
+ "tls",
73
+ ];
74
+ // Create resolve.paths function
75
+ const resolvePaths = function (request) {
76
+ // For built-in modules, return null
77
+ if (builtins.includes(request) || request.startsWith("node:")) {
78
+ return null;
79
+ }
80
+ // For relative paths, return array starting from dirname
81
+ if (request.startsWith("./") ||
82
+ request.startsWith("../") ||
83
+ request.startsWith("/")) {
84
+ return [dirname];
85
+ }
86
+ // For bare specifiers, return node_modules search paths
87
+ const paths = [];
88
+ let current = dirname;
89
+ while (current !== "/") {
90
+ paths.push(current + "/node_modules");
91
+ current = _pathDirname(current);
92
+ }
93
+ paths.push("/node_modules");
94
+ return paths;
95
+ };
96
+ // Create resolve function
97
+ const resolve = function (request, _options) {
98
+ const resolved = _resolveModule.applySyncPromise(undefined, [
99
+ request,
100
+ dirname,
101
+ ]);
102
+ if (resolved === null) {
103
+ const err = new Error("Cannot find module '" + request + "'");
104
+ err.code = "MODULE_NOT_FOUND";
105
+ throw err;
106
+ }
107
+ return resolved;
108
+ };
109
+ resolve.paths = resolvePaths;
110
+ // Create a require function bound to this directory
111
+ const requireFn = function (request) {
112
+ return _requireFrom(request, dirname);
113
+ };
114
+ // Add require.resolve
115
+ requireFn.resolve = resolve;
116
+ // Add require.cache reference to global module cache
117
+ requireFn.cache = _moduleCache;
118
+ // Add require.main (null for dynamically created require)
119
+ requireFn.main = undefined;
120
+ // Add require.extensions (deprecated but still used by some tools)
121
+ requireFn.extensions = {
122
+ ".js": function (_module, _filename) {
123
+ // This is a stub - actual loading is handled by our require implementation
124
+ },
125
+ ".json": function (_module, _filename) {
126
+ // JSON loading stub
127
+ },
128
+ ".node": function (_module, _filename) {
129
+ throw new Error(".node extensions are not supported in sandbox");
130
+ },
131
+ };
132
+ return requireFn;
133
+ }
134
+ /**
135
+ * Polyfill of Node.js `Module` class for sandbox compatibility. Provides
136
+ * `_compile`, `_resolveFilename`, `_load`, `_extensions`, and `_cache` statics
137
+ * that npm tooling (promzard, resolve, etc.) relies on.
138
+ */
139
+ export class Module {
140
+ id;
141
+ path;
142
+ exports;
143
+ filename;
144
+ loaded;
145
+ children;
146
+ paths;
147
+ parent;
148
+ isPreloading;
149
+ constructor(id, parent) {
150
+ this.id = id;
151
+ this.path = _pathDirname(id);
152
+ this.exports = {};
153
+ this.filename = id;
154
+ this.loaded = false;
155
+ this.children = [];
156
+ this.paths = [];
157
+ this.parent = parent;
158
+ this.isPreloading = false;
159
+ // Build module paths
160
+ let current = this.path;
161
+ while (current !== "/") {
162
+ this.paths.push(current + "/node_modules");
163
+ current = _pathDirname(current);
164
+ }
165
+ this.paths.push("/node_modules");
166
+ }
167
+ require(request) {
168
+ return _requireFrom(request, this.path);
169
+ }
170
+ _compile(content, filename) {
171
+ // Create wrapper function and execute
172
+ const wrapper = new Function("exports", "require", "module", "__filename", "__dirname", content);
173
+ const moduleRequire = (request) => _requireFrom(request, this.path);
174
+ moduleRequire.resolve = (request) => {
175
+ const resolved = _resolveModule.applySyncPromise(undefined, [
176
+ request,
177
+ this.path,
178
+ ]);
179
+ if (resolved === null) {
180
+ const err = new Error("Cannot find module '" + request + "'");
181
+ err.code = "MODULE_NOT_FOUND";
182
+ throw err;
183
+ }
184
+ return resolved;
185
+ };
186
+ wrapper(this.exports, moduleRequire, this, filename, this.path);
187
+ this.loaded = true;
188
+ return this.exports;
189
+ }
190
+ static _extensions = {
191
+ ".js": function (module, filename) {
192
+ const fs = _requireFrom("fs", "/");
193
+ const content = fs.readFileSync(filename, "utf8");
194
+ module._compile(content, filename);
195
+ },
196
+ ".json": function (module, filename) {
197
+ const fs = _requireFrom("fs", "/");
198
+ const content = fs.readFileSync(filename, "utf8");
199
+ module.exports = JSON.parse(content);
200
+ },
201
+ ".node": function () {
202
+ throw new Error(".node extensions are not supported in sandbox");
203
+ },
204
+ };
205
+ static _cache = typeof _moduleCache !== "undefined"
206
+ ? _moduleCache
207
+ : {};
208
+ static _resolveFilename(request, parent, _isMain, _options) {
209
+ const parentDir = parent && parent.path ? parent.path : "/";
210
+ const resolved = _resolveModule.applySyncPromise(undefined, [
211
+ request,
212
+ parentDir,
213
+ ]);
214
+ if (resolved === null) {
215
+ const err = new Error("Cannot find module '" + request + "'");
216
+ err.code = "MODULE_NOT_FOUND";
217
+ throw err;
218
+ }
219
+ return resolved;
220
+ }
221
+ static wrap(content) {
222
+ return ("(function (exports, require, module, __filename, __dirname) { " +
223
+ content +
224
+ "\n});");
225
+ }
226
+ static builtinModules = [
227
+ "assert",
228
+ "buffer",
229
+ "child_process",
230
+ "crypto",
231
+ "dns",
232
+ "events",
233
+ "fs",
234
+ "http",
235
+ "https",
236
+ "net",
237
+ "os",
238
+ "path",
239
+ "querystring",
240
+ "stream",
241
+ "string_decoder",
242
+ "timers",
243
+ "tls",
244
+ "tty",
245
+ "url",
246
+ "util",
247
+ "zlib",
248
+ "vm",
249
+ "module",
250
+ ];
251
+ static isBuiltin(moduleName) {
252
+ const name = moduleName.replace(/^node:/, "");
253
+ return Module.builtinModules.includes(name);
254
+ }
255
+ static createRequire = createRequire;
256
+ static syncBuiltinESMExports() {
257
+ // No-op in our environment
258
+ }
259
+ static findSourceMap(_path) {
260
+ return undefined;
261
+ }
262
+ static _nodeModulePaths(from) {
263
+ // Return array of node_modules paths from the given directory up to root
264
+ const paths = [];
265
+ let current = from;
266
+ while (current !== "/") {
267
+ paths.push(current + "/node_modules");
268
+ current = _pathDirname(current);
269
+ if (current === ".")
270
+ break;
271
+ }
272
+ paths.push("/node_modules");
273
+ return paths;
274
+ }
275
+ static _load(request, parent, _isMain) {
276
+ // Use our require system
277
+ const parentDir = parent && parent.path ? parent.path : "/";
278
+ return _requireFrom(request, parentDir);
279
+ }
280
+ static runMain() {
281
+ // No-op - we don't have a main module in this context
282
+ }
283
+ }
284
+ // SourceMap class - not implemented
285
+ export class SourceMap {
286
+ constructor(_payload) {
287
+ throw new Error("SourceMap is not implemented in sandbox");
288
+ }
289
+ get payload() {
290
+ throw new Error("SourceMap is not implemented in sandbox");
291
+ }
292
+ set payload(_value) {
293
+ throw new Error("SourceMap is not implemented in sandbox");
294
+ }
295
+ findEntry(_line, _column) {
296
+ throw new Error("SourceMap is not implemented in sandbox");
297
+ }
298
+ }
299
+ // Module namespace export matching Node.js 'module' module
300
+ // Note: We don't strictly satisfy typeof nodeModule due to complex intersection types
301
+ const moduleModule = {
302
+ Module: Module,
303
+ createRequire: createRequire,
304
+ // Module._extensions (deprecated alias)
305
+ _extensions: Module._extensions,
306
+ // Module._cache reference
307
+ _cache: Module._cache,
308
+ // Built-in module list
309
+ builtinModules: Module.builtinModules,
310
+ // isBuiltin check
311
+ isBuiltin: Module.isBuiltin,
312
+ // Module._resolveFilename (internal but sometimes used)
313
+ _resolveFilename: Module._resolveFilename,
314
+ // wrap function
315
+ wrap: Module.wrap,
316
+ // syncBuiltinESMExports (stub for ESM interop)
317
+ syncBuiltinESMExports: Module.syncBuiltinESMExports,
318
+ // findSourceMap (stub)
319
+ findSourceMap: Module.findSourceMap,
320
+ // SourceMap class (stub)
321
+ SourceMap: SourceMap,
322
+ };
323
+ // Expose to global for require() to use
324
+ exposeCustomGlobal("_moduleModule", moduleModule);
325
+ export default moduleModule;