diff options
author | 2023-02-14 23:37:25 -0800 | |
---|---|---|
committer | 2023-02-14 23:37:25 -0800 | |
commit | c6ee401bf42f305470150d5a7769d857482c9067 (patch) | |
tree | 03fb52ff97332d5c08f83ed7e5c58dd551e5ac79 | |
parent | f19e3d66cb82f3e4cbf740a5a1e5fe50c3fd48f3 (diff) | |
download | bun-c6ee401bf42f305470150d5a7769d857482c9067.tar.gz bun-c6ee401bf42f305470150d5a7769d857482c9067.tar.zst bun-c6ee401bf42f305470150d5a7769d857482c9067.zip |
Add temporary polyfill for async_hooks
Diffstat (limited to '')
-rw-r--r-- | src/bun.js/async_hooks.exports.js | 184 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 16 | ||||
-rw-r--r-- | src/bun.js/module_loader.zig | 196 | ||||
-rw-r--r-- | src/bundler/generate_node_modules_bundle.zig | 26 | ||||
-rw-r--r-- | src/linker.zig | 10 |
5 files changed, 324 insertions, 108 deletions
diff --git a/src/bun.js/async_hooks.exports.js b/src/bun.js/async_hooks.exports.js new file mode 100644 index 000000000..21792e295 --- /dev/null +++ b/src/bun.js/async_hooks.exports.js @@ -0,0 +1,184 @@ +const warnOnce = fn => { + let warned = false; + return (...args) => { + if (!warned) { + warned = true; + fn(...args); + } + }; +}; + +const notImplemented = warnOnce(() => console.warn("[bun]: async_hooks has not been implemented yet :(")); + +class AsyncLocalStorage { + #store; + _enabled; + + constructor() { + this._enabled = false; + } + + enterWith(store) { + this.#store = store; + notImplemented(); + + return this; + } + + exit(cb, ...args) { + this.#store = null; + notImplemented(); + cb(...args); + } + + run(store, callback, ...args) { + if (typeof callback !== "function") throw new TypeError("ERR_INVALID_CALLBACK"); + const prev = this.#store; + this.enterWith(store); + + try { + return callback(...args); + } finally { + this.#store = prev; + } + } + + getStore() { + return this.#store; + } +} + +function createHook() { + return { + enable() { + notImplemented(); + }, + disable() { + notImplemented(); + }, + }; +} + +function executionAsyncId() { + return 0; +} + +function triggerAsyncId() { + return 0; +} + +function executionAsyncResource() { + return null; +} + +const asyncWrapProviders = { + NONE: 0, + DIRHANDLE: 1, + DNSCHANNEL: 2, + ELDHISTOGRAM: 3, + FILEHANDLE: 4, + FILEHANDLECLOSEREQ: 5, + FIXEDSIZEBLOBCOPY: 6, + FSEVENTWRAP: 7, + FSREQCALLBACK: 8, + FSREQPROMISE: 9, + GETADDRINFOREQWRAP: 10, + GETNAMEINFOREQWRAP: 11, + HEAPSNAPSHOT: 12, + HTTP2SESSION: 13, + HTTP2STREAM: 14, + HTTP2PING: 15, + HTTP2SETTINGS: 16, + HTTPINCOMINGMESSAGE: 17, + HTTPCLIENTREQUEST: 18, + JSSTREAM: 19, + JSUDPWRAP: 20, + MESSAGEPORT: 21, + PIPECONNECTWRAP: 22, + PIPESERVERWRAP: 23, + PIPEWRAP: 24, + PROCESSWRAP: 25, + PROMISE: 26, + QUERYWRAP: 27, + SHUTDOWNWRAP: 28, + SIGNALWRAP: 29, + STATWATCHER: 30, + STREAMPIPE: 31, + TCPCONNECTWRAP: 32, + TCPSERVERWRAP: 33, + TCPWRAP: 34, + TTYWRAP: 35, + UDPSENDWRAP: 36, + UDPWRAP: 37, + SIGINTWATCHDOG: 38, + WORKER: 39, + WORKERHEAPSNAPSHOT: 40, + WRITEWRAP: 41, + ZLIB: 42, + CHECKPRIMEREQUEST: 43, + PBKDF2REQUEST: 44, + KEYPAIRGENREQUEST: 45, + KEYGENREQUEST: 46, + KEYEXPORTREQUEST: 47, + CIPHERREQUEST: 48, + DERIVEBITSREQUEST: 49, + HASHREQUEST: 50, + RANDOMBYTESREQUEST: 51, + RANDOMPRIMEREQUEST: 52, + SCRYPTREQUEST: 53, + SIGNREQUEST: 54, + TLSWRAP: 55, + VERIFYREQUEST: 56, + INSPECTORJSBINDING: 57, +}; + +class AsyncResource { + constructor(type, triggerAsyncId) { + this.type = type; + this.triggerAsyncId = triggerAsyncId; + } + + type; + triggerAsyncId; + + emitBefore() { + return true; + } + + emitAfter() { + return true; + } + + emitDestroy() {} + + runInAsyncScope(fn, ...args) { + notImplemented(); + process.nextTick(fn, ...args); + } + + asyncId() { + return 0; + } +} + +export { + AsyncLocalStorage, + createHook, + executionAsyncId, + triggerAsyncId, + executionAsyncResource, + asyncWrapProviders, + AsyncResource, +}; + +export default { + AsyncLocalStorage, + createHook, + executionAsyncId, + triggerAsyncId, + executionAsyncResource, + asyncWrapProviders, + AsyncResource, + [Symbol.toStringTag]: "Module (not implemented yet)", + [Symbol.for("CommonJS")]: 0, +}; diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 7aa74f51f..b926c4ee9 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1160,7 +1160,21 @@ pub const VirtualMachine = struct { } if (JSC.HardcodedModule.Aliases.getWithEql(specifier, ZigString.eqlComptime)) |hardcoded| { - res.* = ErrorableZigString.ok(ZigString.init(hardcoded)); + if (hardcoded.tag == .none) { + resolveMaybeNeedsTrailingSlash( + res, + global, + ZigString.init(hardcoded.path), + source, + query_string, + is_esm, + is_a_file_path, + realpath, + ); + return; + } + + res.* = ErrorableZigString.ok(ZigString.init(hardcoded.path)); return; } var old_log = jsc_vm.log; diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 09df14600..373f546e0 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -1713,6 +1713,15 @@ pub const ModuleLoader = struct { .hash = 0, }; }, + .@"node:async_hooks" => { + return ResolvedSource{ + .allocator = null, + .source_code = ZigString.init(jsModuleFromFile(jsc_vm.load_builtins_from_path, "async_hooks.exports.js")), + .specifier = ZigString.init("node:async_hooks"), + .source_url = ZigString.init("node:async_hooks"), + .hash = 0, + }; + }, .@"node:fs/promises" => { return ResolvedSource{ @@ -2131,6 +2140,7 @@ pub const HardcodedModule = enum { @"bun:sqlite", @"detect-libc", @"node:assert", + @"node:async_hooks", @"node:buffer", @"node:child_process", @"node:crypto", @@ -2143,7 +2153,6 @@ pub const HardcodedModule = enum { @"node:https", @"node:module", @"node:net", - @"node:tls", @"node:os", @"node:path", @"node:path/posix", @@ -2158,6 +2167,7 @@ pub const HardcodedModule = enum { @"node:string_decoder", @"node:timers", @"node:timers/promises", + @"node:tls", @"node:tty", @"node:url", @"node:util", @@ -2183,6 +2193,7 @@ pub const HardcodedModule = enum { .{ "detect-libc", HardcodedModule.@"detect-libc" }, .{ "node:assert", HardcodedModule.@"node:assert" }, .{ "node:buffer", HardcodedModule.@"node:buffer" }, + .{ "node:async_hooks", HardcodedModule.@"node:async_hooks" }, .{ "node:child_process", HardcodedModule.@"node:child_process" }, .{ "node:crypto", HardcodedModule.@"node:crypto" }, .{ "node:dns", HardcodedModule.@"node:dns" }, @@ -2219,96 +2230,103 @@ pub const HardcodedModule = enum { .{ "ws", HardcodedModule.ws }, }, ); + pub const Alias = struct { + path: string, + tag: ImportRecord.Tag = ImportRecord.Tag.hardcoded, + }; pub const Aliases = bun.ComptimeStringMap( - string, + Alias, .{ - .{ "assert", "node:assert" }, - .{ "async_hooks", "node:async_hooks" }, - .{ "buffer", "node:buffer" }, - .{ "bun:ffi", "bun:ffi" }, - .{ "bun:jsc", "bun:jsc" }, - .{ "bun:sqlite", "bun:sqlite" }, - .{ "bun:wrap", "bun:wrap" }, - .{ "bun", "bun" }, - .{ "child_process", "node:child_process" }, - .{ "crypto", "node:crypto" }, - .{ "depd", "depd" }, - .{ "detect-libc", "detect-libc" }, - .{ "detect-libc/lib/detect-libc.js", "detect-libc" }, - .{ "dns", "node:dns" }, - .{ "dns/promises", "node:dns/promises" }, - .{ "events", "node:events" }, - .{ "ffi", "bun:ffi" }, - .{ "fs", "node:fs" }, - .{ "fs/promises", "node:fs/promises" }, - .{ "http", "node:http" }, - .{ "https", "node:https" }, - .{ "module", "node:module" }, - .{ "net", "node:net" }, - .{ "node:assert", "node:assert" }, - .{ "node:async_hooks", "node:async_hooks" }, - .{ "node:buffer", "node:buffer" }, - .{ "node:child_process", "node:child_process" }, - .{ "node:crypto", "node:crypto" }, - .{ "node:dns", "node:dns" }, - .{ "node:dns/promises", "node:dns/promises" }, - .{ "node:events", "node:events" }, - .{ "node:fs", "node:fs" }, - .{ "node:fs/promises", "node:fs/promises" }, - .{ "node:http", "node:http" }, - .{ "node:https", "node:https" }, - .{ "node:module", "node:module" }, - .{ "node:net", "node:net" }, - .{ "node:os", "node:os" }, - .{ "node:path", "node:path" }, - .{ "node:path/posix", "node:path/posix" }, - .{ "node:path/win32", "node:path/win32" }, - .{ "node:perf_hooks", "node:perf_hooks" }, - .{ "node:process", "node:process" }, - .{ "node:readline", "node:readline" }, - .{ "node:readline/promises", "node:readline/promises" }, - .{ "node:stream", "node:stream" }, - .{ "node:stream/consumers", "node:stream/consumers" }, - .{ "node:stream/web", "node:stream/web" }, - .{ "node:string_decoder", "node:string_decoder" }, - .{ "node:timers", "node:timers" }, - .{ "node:timers/promises", "node:timers/promises" }, - .{ "node:tls", "node:tls" }, - .{ "node:tty", "node:tty" }, - .{ "node:url", "node:url" }, - .{ "node:util", "node:util" }, - .{ "node:util/types", "node:util/types" }, - .{ "node:wasi", "node:wasi" }, - .{ "node:worker_threads", "node:worker_threads" }, - .{ "node:zlib", "node:zlib" }, - .{ "os", "node:os" }, - .{ "path", "node:path" }, - .{ "path/posix", "node:path/posix" }, - .{ "path/win32", "node:path/win32" }, - .{ "perf_hooks", "node:perf_hooks" }, - .{ "process", "node:process" }, - .{ "readable-stream", "node:stream" }, - .{ "readable-stream/consumer", "node:stream/consumers" }, - .{ "readable-stream/web", "node:stream/web" }, - .{ "readline", "node:readline" }, - .{ "readline/promises", "node:readline/promises" }, - .{ "stream", "node:stream" }, - .{ "stream/consumers", "node:stream/consumers" }, - .{ "stream/web", "node:stream/web" }, - .{ "string_decoder", "node:string_decoder" }, - .{ "timers", "node:timers" }, - .{ "timers/promises", "node:timers/promises" }, - .{ "tls", "node:tls" }, - .{ "tty", "node:tty" }, - .{ "undici", "undici" }, - .{ "url", "node:url" }, - .{ "util", "node:util" }, - .{ "util/types", "node:util/types" }, - .{ "wasi", "node:wasi" }, - .{ "worker_threads", "node:worker_threads" }, - .{ "ws", "ws" }, - .{ "ws/lib/websocket", "ws" }, - .{ "zlib", "node:zlib" }, + .{ "assert", .{ .path = "node:assert" } }, + .{ "async_hooks", .{ .path = "node:async_hooks" } }, + .{ "buffer", .{ .path = "node:buffer" } }, + .{ "bun:ffi", .{ .path = "bun:ffi" } }, + .{ "bun:jsc", .{ .path = "bun:jsc" } }, + .{ "bun:sqlite", .{ .path = "bun:sqlite" } }, + .{ "bun:wrap", .{ .path = "bun:wrap" } }, + .{ "bun", .{ .path = "bun", .tag = .bun } }, + .{ "child_process", .{ .path = "node:child_process" } }, + .{ "crypto", .{ .path = "node:crypto" } }, + .{ "depd", .{ .path = "depd" } }, + .{ "detect-libc", .{ .path = "detect-libc" } }, + .{ "detect-libc/lib/detect-libc.js", .{ .path = "detect-libc" } }, + .{ "dns", .{ .path = "node:dns" } }, + .{ "dns/promises", .{ .path = "node:dns/promises" } }, + .{ "events", .{ .path = "node:events" } }, + .{ "ffi", .{ .path = "bun:ffi" } }, + .{ "fs", .{ .path = "node:fs" } }, + .{ "fs/promises", .{ .path = "node:fs/promises" } }, + .{ "http", .{ .path = "node:http" } }, + .{ "https", .{ .path = "node:https" } }, + .{ "module", .{ .path = "node:module" } }, + .{ "net", .{ .path = "node:net" } }, + .{ "node:assert", .{ .path = "node:assert" } }, + .{ "node:async_hooks", .{ .path = "node:async_hooks" } }, + .{ "node:buffer", .{ .path = "node:buffer" } }, + .{ "node:child_process", .{ .path = "node:child_process" } }, + .{ "node:crypto", .{ .path = "node:crypto" } }, + .{ "node:dns", .{ .path = "node:dns" } }, + .{ "node:dns/promises", .{ .path = "node:dns/promises" } }, + .{ "node:events", .{ .path = "node:events" } }, + .{ "node:fs", .{ .path = "node:fs" } }, + .{ "node:fs/promises", .{ .path = "node:fs/promises" } }, + .{ "node:http", .{ .path = "node:http" } }, + .{ "node:https", .{ .path = "node:https" } }, + .{ "node:module", .{ .path = "node:module" } }, + .{ "node:net", .{ .path = "node:net" } }, + .{ "node:os", .{ .path = "node:os" } }, + .{ "node:path", .{ .path = "node:path" } }, + .{ "node:path/posix", .{ .path = "node:path/posix" } }, + .{ "node:path/win32", .{ .path = "node:path/win32" } }, + .{ "node:perf_hooks", .{ .path = "node:perf_hooks" } }, + .{ "node:process", .{ .path = "node:process" } }, + .{ "node:readline", .{ .path = "node:readline" } }, + .{ "node:readline/promises", .{ .path = "node:readline/promises" } }, + .{ "node:stream", .{ .path = "node:stream" } }, + .{ "node:stream/consumers", .{ .path = "node:stream/consumers" } }, + .{ "node:stream/web", .{ .path = "node:stream/web" } }, + .{ "node:string_decoder", .{ .path = "node:string_decoder" } }, + .{ "node:timers", .{ .path = "node:timers" } }, + .{ "node:timers/promises", .{ .path = "node:timers/promises" } }, + .{ "node:tls", .{ .path = "node:tls" } }, + .{ "node:tty", .{ .path = "node:tty" } }, + .{ "node:url", .{ .path = "node:url" } }, + .{ "node:util", .{ .path = "node:util" } }, + .{ "node:util/types", .{ .path = "node:util/types" } }, + .{ "node:wasi", .{ .path = "node:wasi" } }, + .{ "node:worker_threads", .{ .path = "node:worker_threads" } }, + .{ "node:zlib", .{ .path = "node:zlib" } }, + .{ "os", .{ .path = "node:os" } }, + .{ "path", .{ .path = "node:path" } }, + .{ "path/posix", .{ .path = "node:path/posix" } }, + .{ "path/win32", .{ .path = "node:path/win32" } }, + .{ "perf_hooks", .{ .path = "node:perf_hooks" } }, + .{ "process", .{ .path = "node:process" } }, + .{ "readable-stream", .{ .path = "node:stream" } }, + .{ "readable-stream/consumer", .{ .path = "node:stream/consumers" } }, + .{ "readable-stream/web", .{ .path = "node:stream/web" } }, + .{ "readline", .{ .path = "node:readline" } }, + .{ "readline/promises", .{ .path = "node:readline/promises" } }, + .{ "stream", .{ .path = "node:stream" } }, + .{ "stream/consumers", .{ .path = "node:stream/consumers" } }, + .{ "stream/web", .{ .path = "node:stream/web" } }, + .{ "string_decoder", .{ .path = "node:string_decoder" } }, + .{ "timers", .{ .path = "node:timers" } }, + .{ "timers/promises", .{ .path = "node:timers/promises" } }, + .{ "tls", .{ .path = "node:tls" } }, + .{ "tty", .{ .path = "node:tty" } }, + .{ "undici", .{ .path = "undici" } }, + .{ "url", .{ .path = "node:url" } }, + .{ "util", .{ .path = "node:util" } }, + .{ "util/types", .{ .path = "node:util/types" } }, + .{ "wasi", .{ .path = "node:wasi" } }, + .{ "worker_threads", .{ .path = "node:worker_threads" } }, + .{ "ws", .{ .path = "ws" } }, + .{ "ws/lib/websocket", .{ .path = "ws" } }, + .{ "zlib", .{ .path = "node:zlib" } }, + + // // Prisma has an edge build, but has not set it in package.json exports + // .{ "@prisma/client", .{ .path = "@prisma/client/edge", .tag = .none } }, }, ); }; diff --git a/src/bundler/generate_node_modules_bundle.zig b/src/bundler/generate_node_modules_bundle.zig index c57e50399..8a965df08 100644 --- a/src/bundler/generate_node_modules_bundle.zig +++ b/src/bundler/generate_node_modules_bundle.zig @@ -1342,13 +1342,12 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, } if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |remapped| { - import_record.path.text = remapped; - import_record.tag = if (strings.eqlComptime(remapped, "bun")) - ImportRecord.Tag.bun - else - ImportRecord.Tag.hardcoded; - import_record.is_bundled = false; - continue; + import_record.path.text = remapped.path; + import_record.tag = remapped.tag; + if (remapped.tag != .none) { + import_record.is_bundled = false; + continue; + } } } @@ -1792,13 +1791,12 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, } if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |remapped| { - import_record.path.text = remapped; - import_record.tag = if (strings.eqlComptime(remapped, "bun")) - ImportRecord.Tag.bun - else - ImportRecord.Tag.hardcoded; - import_record.is_bundled = false; - continue; + import_record.path.text = remapped.path; + import_record.tag = remapped.tag; + if (remapped.tag != .none) { + import_record.is_bundled = false; + continue; + } } } diff --git a/src/linker.zig b/src/linker.zig index 072031863..8e4daf5b9 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -270,10 +270,12 @@ pub const Linker = struct { if (comptime is_bun) { if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |replacement| { - import_record.path.text = replacement; - import_record.tag = if (strings.eqlComptime(replacement, "bun")) ImportRecord.Tag.bun else .hardcoded; - externals.append(record_index) catch unreachable; - continue; + import_record.path.text = replacement.path; + import_record.tag = replacement.tag; + if (replacement.tag != .none) { + externals.append(record_index) catch unreachable; + continue; + } } if (JSC.DisabledModule.has(import_record.path.text)) { |