/** * Node.js module shims for the QuickJS sandbox. * Each is a JS source string evaluated inside the sandbox IIFE. * They set up globals (e.g. globalThis[Symbol.for('jb:os')]) referenced by both * require() and ESM VIRTUAL_MODULES. */ /** EventEmitter — pure JS, no dependencies. Must run before stream. */ export declare const EVENTS_MODULE_SOURCE = "\nvar EventEmitter = (function() {\n function EE() {\n this._events = {};\n this._maxListeners = 10;\n }\n EE.prototype.on = function(event, listener) {\n if (!this._events[event]) this._events[event] = [];\n this._events[event].push(listener);\n return this;\n };\n EE.prototype.addListener = EE.prototype.on;\n EE.prototype.once = function(event, listener) {\n var self = this;\n function wrapper() {\n self.removeListener(event, wrapper);\n listener.apply(this, arguments);\n }\n wrapper._original = listener;\n return this.on(event, wrapper);\n };\n EE.prototype.off = function(event, listener) {\n return this.removeListener(event, listener);\n };\n EE.prototype.removeListener = function(event, listener) {\n var list = this._events[event];\n if (list) {\n this._events[event] = list.filter(function(fn) {\n return fn !== listener && fn._original !== listener;\n });\n }\n return this;\n };\n EE.prototype.removeAllListeners = function(event) {\n if (event) delete this._events[event];\n else this._events = {};\n return this;\n };\n EE.prototype.emit = function(event) {\n var list = this._events[event];\n if (!list || list.length === 0) return false;\n var args = Array.prototype.slice.call(arguments, 1);\n var fns = list.slice();\n for (var i = 0; i < fns.length; i++) fns[i].apply(this, args);\n return true;\n };\n EE.prototype.listeners = function(event) {\n return (this._events[event] || []).slice();\n };\n EE.prototype.listenerCount = function(event) {\n return (this._events[event] || []).length;\n };\n EE.prototype.setMaxListeners = function(n) {\n this._maxListeners = n;\n return this;\n };\n EE.prototype.eventNames = function() {\n return Object.keys(this._events);\n };\n EE.prototype.prependListener = function(event, listener) {\n if (!this._events[event]) this._events[event] = [];\n this._events[event].unshift(listener);\n return this;\n };\n return EE;\n})();\nglobalThis[Symbol.for('jb:events')] = { EventEmitter: EventEmitter };\n"; /** OS module — hardcoded sandbox values */ export declare const OS_MODULE_SOURCE = "\nvar _os = {\n platform: function() { return globalThis.process.platform; },\n arch: function() { return globalThis.process.arch; },\n homedir: function() { return '/home/user'; },\n tmpdir: function() { return '/tmp'; },\n type: function() { return 'Linux'; },\n hostname: function() { return 'sandbox'; },\n EOL: '\\n',\n cpus: function() { return []; },\n totalmem: function() { return 0; },\n freemem: function() { return 0; },\n endianness: function() { return 'LE'; }\n};\nglobalThis[Symbol.for('jb:os')] = _os;\n"; /** URL module — wraps globalThis.URL/URLSearchParams from fetch polyfill */ export declare const URL_MODULE_SOURCE = "\nvar _urlMod = {\n URL: globalThis.URL,\n URLSearchParams: globalThis.URLSearchParams,\n parse: function(urlStr) {\n try {\n var u = new URL(urlStr);\n return {\n protocol: u.protocol, host: u.host, hostname: u.hostname,\n port: u.port, pathname: u.pathname, search: u.search,\n hash: u.hash, href: u.href, path: u.pathname + u.search\n };\n } catch(e) {\n return {\n protocol: null, host: null, hostname: null, port: null,\n pathname: urlStr, search: '', hash: '', href: urlStr, path: urlStr\n };\n }\n },\n format: function(obj) {\n if (typeof obj === 'string') return obj;\n if (obj instanceof URL) return obj.href;\n var auth = obj.auth ? obj.auth + '@' : '';\n var host = obj.host || ((obj.hostname || '') + (obj.port ? ':' + obj.port : ''));\n return (obj.protocol ? obj.protocol + '//' : '') + auth + host +\n (obj.pathname || '/') + (obj.search || '') + (obj.hash || '');\n }\n};\nglobalThis[Symbol.for('jb:url')] = _urlMod;\n"; /** Assert module — pure JS */ export declare const ASSERT_MODULE_SOURCE = "\nvar _deepEqual = function(a, b) {\n if (a === b) return true;\n if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n var ka = Object.keys(a), kb = Object.keys(b);\n if (ka.length !== kb.length) return false;\n for (var i = 0; i < ka.length; i++) {\n if (!_deepEqual(a[ka[i]], b[ka[i]])) return false;\n }\n return true;\n};\nvar _assert = function(val, msg) {\n if (!val) throw new Error(msg || 'AssertionError: expected truthy value');\n};\n_assert.ok = _assert;\n_assert.equal = function(a, b, msg) {\n if (a != b) throw new Error(msg || 'AssertionError: ' + a + ' != ' + b);\n};\n_assert.notEqual = function(a, b, msg) {\n if (a == b) throw new Error(msg || 'AssertionError: ' + a + ' == ' + b);\n};\n_assert.strictEqual = function(a, b, msg) {\n if (a !== b) throw new Error(msg || 'AssertionError: ' + a + ' !== ' + b);\n};\n_assert.notStrictEqual = function(a, b, msg) {\n if (a === b) throw new Error(msg || 'AssertionError: ' + a + ' === ' + b);\n};\n_assert.deepEqual = function(a, b, msg) {\n if (!_deepEqual(a, b)) throw new Error(msg || 'AssertionError: objects not deep equal');\n};\n_assert.deepStrictEqual = _assert.deepEqual;\n_assert.notDeepEqual = function(a, b, msg) {\n if (_deepEqual(a, b)) throw new Error(msg || 'AssertionError: objects are deep equal');\n};\n_assert.throws = function(fn, expected, msg) {\n var threw = false;\n try { fn(); } catch(e) {\n threw = true;\n if (expected instanceof RegExp && !expected.test(e.message))\n throw new Error(msg || 'AssertionError: error message did not match');\n }\n if (!threw) throw new Error(msg || 'AssertionError: function did not throw');\n};\n_assert.doesNotThrow = function(fn, msg) {\n // @banned-pattern-ignore: sandbox-internal assertion helper; e.message is from user code inside QuickJS, not host details\n try { fn(); } catch(e) {\n throw new Error(msg || 'AssertionError: function threw: ' + e.message);\n }\n};\n_assert.fail = function(msg) {\n throw new Error(msg || 'AssertionError: assert.fail()');\n};\nglobalThis[Symbol.for('jb:assert')] = _assert;\n"; /** Util module — format, inspect, promisify, types */ export declare const UTIL_MODULE_SOURCE = "\nvar _util = {\n format: function() {\n var args = Array.prototype.slice.call(arguments);\n if (args.length === 0) return '';\n var fmt = args[0];\n if (typeof fmt !== 'string') {\n return args.map(function(a) {\n return typeof a === 'string' ? a : JSON.stringify(a);\n }).join(' ');\n }\n var i = 1;\n var str = fmt.replace(/%[sdjifoO%]/g, function(m) {\n if (m === '%%') return '%';\n if (i >= args.length) return m;\n var v = args[i++];\n if (m === '%s') return String(v);\n if (m === '%d') return Number(v).toString();\n if (m === '%i') { var n = Number(v); return (isNaN(n) ? 'NaN' : Math.trunc(n)).toString(); }\n if (m === '%j') return JSON.stringify(v);\n if (m === '%f') return parseFloat(v).toString();\n if (m === '%o' || m === '%O') return JSON.stringify(v);\n return m;\n });\n while (i < args.length) {\n str += ' ' + (typeof args[i] === 'string' ? args[i] : JSON.stringify(args[i]));\n i++;\n }\n return str;\n },\n inspect: function(obj, opts) {\n if (obj === null) return 'null';\n if (obj === undefined) return 'undefined';\n if (typeof obj === 'string') return \"'\" + obj + \"'\";\n if (typeof obj === 'function') return '[Function: ' + (obj.name || 'anonymous') + ']';\n var seen = [];\n try {\n return JSON.stringify(obj, function(key, val) {\n if (typeof val === 'object' && val !== null) {\n if (seen.indexOf(val) !== -1) return '[Circular]';\n seen.push(val);\n }\n return val;\n });\n } catch(e) { return String(obj); }\n },\n promisify: function(fn) {\n return function() {\n var args = Array.prototype.slice.call(arguments);\n return new Promise(function(resolve, reject) {\n args.push(function(err, val) { if (err) reject(err); else resolve(val); });\n fn.apply(null, args);\n });\n };\n },\n types: {\n isDate: function(v) { return v instanceof Date; },\n isRegExp: function(v) { return v instanceof RegExp; },\n isArray: function(v) { return Array.isArray(v); },\n isMap: function(v) { return typeof Map !== 'undefined' && v instanceof Map; },\n isSet: function(v) { return typeof Set !== 'undefined' && v instanceof Set; }\n },\n inherits: function(ctor, superCtor) {\n ctor.prototype = Object.create(superCtor.prototype);\n ctor.prototype.constructor = ctor;\n }\n};\nglobalThis[Symbol.for('jb:util')] = _util;\n"; /** Buffer module — wraps Uint8Array, pure-JS UTF-8 (no TextEncoder/TextDecoder) */ export declare const BUFFER_MODULE_SOURCE = "\nfunction _utf8Encode(str) {\n var bytes = [];\n for (var i = 0; i < str.length; i++) {\n var c = str.charCodeAt(i);\n if (c < 0x80) {\n bytes.push(c);\n } else if (c < 0x800) {\n bytes.push(0xC0 | (c >> 6), 0x80 | (c & 0x3F));\n } else if (c >= 0xD800 && c <= 0xDBFF && i + 1 < str.length) {\n var lo = str.charCodeAt(++i);\n var cp = ((c - 0xD800) * 0x400) + (lo - 0xDC00) + 0x10000;\n bytes.push(0xF0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3F), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));\n } else {\n bytes.push(0xE0 | (c >> 12), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F));\n }\n }\n return bytes;\n}\nfunction _utf8Decode(bytes) {\n var str = '';\n var i = 0;\n while (i < bytes.length) {\n var b = bytes[i];\n if (b < 0x80) { str += String.fromCharCode(b); i++; }\n else if ((b & 0xE0) === 0xC0) { str += String.fromCharCode(((b & 0x1F) << 6) | (bytes[i+1] & 0x3F)); i += 2; }\n else if ((b & 0xF0) === 0xE0) { str += String.fromCharCode(((b & 0x0F) << 12) | ((bytes[i+1] & 0x3F) << 6) | (bytes[i+2] & 0x3F)); i += 3; }\n else if ((b & 0xF8) === 0xF0) { var cp = ((b & 0x07) << 18) | ((bytes[i+1] & 0x3F) << 12) | ((bytes[i+2] & 0x3F) << 6) | (bytes[i+3] & 0x3F); cp -= 0x10000; str += String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00); i += 4; }\n else { i++; }\n }\n return str;\n}\n// _utf8Encode/_utf8Decode are IIFE-local vars, available to all module shims\n\nfunction Buffer(arg) {\n if (typeof arg === 'number') {\n this._data = new Uint8Array(arg);\n } else if (arg instanceof ArrayBuffer) {\n this._data = new Uint8Array(arg);\n } else if (arg instanceof Uint8Array) {\n this._data = new Uint8Array(arg);\n } else if (Array.isArray(arg)) {\n this._data = new Uint8Array(arg);\n } else {\n this._data = new Uint8Array(0);\n }\n this.length = this._data.length;\n}\nBuffer.from = function(data, encoding) {\n if (typeof data === 'string') {\n return new Buffer(_utf8Encode(data));\n }\n if (data instanceof ArrayBuffer) return new Buffer(data);\n if (data instanceof Uint8Array) return new Buffer(data);\n if (Array.isArray(data)) return new Buffer(data);\n if (data && data._data) return new Buffer(data._data.slice());\n return new Buffer(0);\n};\nBuffer.alloc = function(size, fill) {\n var buf = new Buffer(size);\n if (fill !== undefined) {\n var fillByte = typeof fill === 'number' ? fill : 0;\n buf._data.fill(fillByte);\n }\n return buf;\n};\nBuffer.allocUnsafe = Buffer.alloc;\nBuffer.isBuffer = function(obj) { return obj instanceof Buffer; };\nBuffer.concat = function(list, totalLength) {\n if (!totalLength) {\n totalLength = 0;\n for (var i = 0; i < list.length; i++) totalLength += list[i].length;\n }\n var result = new Uint8Array(totalLength);\n var offset = 0;\n for (var i = 0; i < list.length; i++) {\n result.set(list[i]._data, offset);\n offset += list[i].length;\n }\n return new Buffer(result);\n};\nBuffer.byteLength = function(str) {\n return _utf8Encode(str).length;\n};\nBuffer.prototype.toString = function(encoding) {\n return _utf8Decode(this._data);\n};\nBuffer.prototype.toJSON = function() {\n return { type: 'Buffer', data: Array.from(this._data) };\n};\nBuffer.prototype.slice = function(start, end) {\n return new Buffer(this._data.slice(start, end));\n};\nBuffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) {\n targetStart = targetStart || 0;\n sourceStart = sourceStart || 0;\n sourceEnd = sourceEnd || this.length;\n var sub = this._data.subarray(sourceStart, sourceEnd);\n target._data.set(sub, targetStart);\n return sub.length;\n};\nBuffer.prototype.write = function(str, offset) {\n var bytes = _utf8Encode(str);\n offset = offset || 0;\n this._data.set(bytes, offset);\n return bytes.length;\n};\nBuffer.prototype.fill = function(val, offset, end) {\n this._data.fill(typeof val === 'number' ? val : 0, offset, end);\n return this;\n};\nBuffer.prototype.equals = function(other) {\n if (this.length !== other.length) return false;\n for (var i = 0; i < this.length; i++) {\n if (this._data[i] !== other._data[i]) return false;\n }\n return true;\n};\nBuffer.prototype.readUInt8 = function(offset) { return this._data[offset]; };\nBuffer.prototype.writeUInt8 = function(value, offset) { this._data[offset] = value; return offset + 1; };\nglobalThis[Symbol.for('jb:buffer')] = { Buffer: Buffer };\nglobalThis.Buffer = Buffer;\n"; /** Stream module — minimal stubs based on EventEmitter */ export declare const STREAM_MODULE_SOURCE = "\nvar _EE = globalThis[Symbol.for('jb:events')].EventEmitter;\n\nfunction Stream() { _EE.call(this); }\nStream.prototype = Object.create(_EE.prototype);\nStream.prototype.constructor = Stream;\nStream.prototype.pipe = function(dest) {\n this.on('data', function(chunk) { dest.write(chunk); });\n this.on('end', function() { if (dest.end) dest.end(); });\n return dest;\n};\n\nfunction Readable(opts) {\n Stream.call(this);\n this.readable = true;\n this._readableState = { ended: false, buffer: [] };\n}\nReadable.prototype = Object.create(Stream.prototype);\nReadable.prototype.constructor = Readable;\nReadable.prototype.read = function() { return null; };\nReadable.prototype.push = function(chunk) {\n if (chunk === null) { this._readableState.ended = true; this.emit('end'); return false; }\n this.emit('data', chunk);\n return true;\n};\nReadable.prototype.destroy = function() { this.emit('close'); return this; };\n\nfunction Writable(opts) {\n Stream.call(this);\n this.writable = true;\n this._writableState = { ended: false };\n}\nWritable.prototype = Object.create(Stream.prototype);\nWritable.prototype.constructor = Writable;\nWritable.prototype.write = function(chunk) { return true; };\nWritable.prototype.end = function(chunk) {\n if (chunk) this.write(chunk);\n this._writableState.ended = true;\n this.emit('finish');\n return this;\n};\nWritable.prototype.destroy = function() { this.emit('close'); return this; };\n\nfunction Duplex(opts) {\n Readable.call(this, opts);\n Writable.call(this, opts);\n}\nDuplex.prototype = Object.create(Readable.prototype);\nvar _wKeys = Object.keys(Writable.prototype);\nfor (var _wi = 0; _wi < _wKeys.length; _wi++) {\n if (!Duplex.prototype[_wKeys[_wi]]) Duplex.prototype[_wKeys[_wi]] = Writable.prototype[_wKeys[_wi]];\n}\nDuplex.prototype.constructor = Duplex;\n\nfunction Transform(opts) { Duplex.call(this, opts); }\nTransform.prototype = Object.create(Duplex.prototype);\nTransform.prototype.constructor = Transform;\nTransform.prototype._transform = function(chunk, encoding, cb) { if (cb) cb(null, chunk); };\n\nfunction PassThrough(opts) { Transform.call(this, opts); }\nPassThrough.prototype = Object.create(Transform.prototype);\nPassThrough.prototype.constructor = PassThrough;\n\nfunction pipeline() {\n var streams = Array.prototype.slice.call(arguments);\n var cb = typeof streams[streams.length - 1] === 'function' ? streams.pop() : null;\n for (var i = 0; i < streams.length - 1; i++) streams[i].pipe(streams[i + 1]);\n if (cb) {\n var last = streams[streams.length - 1];\n last.on('finish', function() { cb(null); });\n last.on('error', function(e) { cb(e); });\n }\n return streams[streams.length - 1];\n}\n\nglobalThis[Symbol.for('jb:stream')] = {\n Stream: Stream, Readable: Readable, Writable: Writable,\n Duplex: Duplex, Transform: Transform, PassThrough: PassThrough,\n pipeline: pipeline\n};\n"; /** StringDecoder module — uses _utf8Decode from Buffer shim */ export declare const STRING_DECODER_MODULE_SOURCE = "\nfunction StringDecoder(encoding) {\n this.encoding = (encoding || 'utf-8').toLowerCase();\n if (this.encoding === 'utf8') this.encoding = 'utf-8';\n}\nStringDecoder.prototype.write = function(buf) {\n if (typeof buf === 'string') return buf;\n var data = buf instanceof Uint8Array ? buf : (buf && buf._data ? buf._data : new Uint8Array(0));\n return _utf8Decode(data);\n};\nStringDecoder.prototype.end = function(buf) {\n if (buf) return this.write(buf);\n return '';\n};\nglobalThis[Symbol.for('jb:string_decoder')] = { StringDecoder: StringDecoder };\n"; /** Querystring module — pure JS */ export declare const QUERYSTRING_MODULE_SOURCE = "\nvar _qs = {\n parse: function(str, sep, eq) {\n sep = sep || '&'; eq = eq || '=';\n var result = Object.create(null);\n if (!str || typeof str !== 'string') return result;\n var pairs = str.split(sep);\n for (var i = 0; i < pairs.length; i++) {\n var idx = pairs[i].indexOf(eq);\n var key, val;\n if (idx >= 0) {\n key = decodeURIComponent(pairs[i].slice(0, idx).replace(/\\+/g, ' '));\n val = decodeURIComponent(pairs[i].slice(idx + 1).replace(/\\+/g, ' '));\n } else {\n key = decodeURIComponent(pairs[i].replace(/\\+/g, ' '));\n val = '';\n }\n if (result[key] !== undefined) {\n if (Array.isArray(result[key])) result[key].push(val);\n else result[key] = [result[key], val];\n } else {\n result[key] = val;\n }\n }\n return result;\n },\n stringify: function(obj, sep, eq) {\n sep = sep || '&'; eq = eq || '=';\n var pairs = [];\n var keys = Object.keys(obj);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n var val = obj[key];\n if (Array.isArray(val)) {\n for (var j = 0; j < val.length; j++) {\n pairs.push(encodeURIComponent(key) + eq + encodeURIComponent(val[j]));\n }\n } else {\n pairs.push(encodeURIComponent(key) + eq + encodeURIComponent(val));\n }\n }\n return pairs.join(sep);\n },\n escape: function(str) { return encodeURIComponent(str); },\n unescape: function(str) { return decodeURIComponent(str); }\n};\n_qs.decode = _qs.parse;\n_qs.encode = _qs.stringify;\nglobalThis[Symbol.for('jb:querystring')] = _qs;\n"; /** * Unsupported Node.js modules — throw clear error at require/import time. * Map of module name to hint message. */ export declare const UNSUPPORTED_MODULES: Record;