forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_fallbacks.zig
81 lines (72 loc) · 3.04 KB
/
node_fallbacks.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const std = @import("std");
const string = @import("./string_types.zig").string;
const PackageJSON = @import("./resolver/package_json.zig").PackageJSON;
const logger = bun.logger;
const Fs = @import("./fs.zig");
const bun = @import("root").bun;
const Environment = bun.Environment;
pub const import_path = "/bun-vfs$$/node_modules/";
comptime {
// Ensure that checking for the prefix should be a cheap lookup (bun.strings.hasPrefixComptime)
// because 24 bytes == 8 * 3 --> read and compare three u64s
bun.assert(import_path.len % 8 == 0);
}
pub const FallbackModule = struct {
path: Fs.Path,
package_json: *const PackageJSON,
code: string,
pub fn init(comptime name: string) FallbackModule {
@setEvalBranchQuota(99999);
const version = "0.0.0-polyfill";
const code_path = "node-fallbacks/" ++ name ++ ".js";
return .{
.path = Fs.Path.initWithNamespaceVirtual(import_path ++ name ++ "/index.js", "node", name),
.package_json = &PackageJSON{
.name = name,
.version = version,
.module_type = .esm,
.hash = @as(u32, @truncate(bun.hash(name ++ "@" ++ version))),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString(import_path ++ name ++ "/package.json", ""),
.side_effects = .false,
},
.code = @embedFile(code_path),
};
}
};
pub const Map = bun.ComptimeStringMap(FallbackModule, .{
.{ "assert", FallbackModule.init("assert") },
.{ "buffer", FallbackModule.init("buffer") },
.{ "console", FallbackModule.init("console") },
.{ "constants", FallbackModule.init("constants") },
.{ "crypto", FallbackModule.init("crypto") },
.{ "domain", FallbackModule.init("domain") },
.{ "events", FallbackModule.init("events") },
.{ "http", FallbackModule.init("http") },
.{ "https", FallbackModule.init("https") },
.{ "net", FallbackModule.init("net") },
.{ "os", FallbackModule.init("os") },
.{ "path", FallbackModule.init("path") },
.{ "process", FallbackModule.init("process") },
.{ "punycode", FallbackModule.init("punycode") },
.{ "querystring", FallbackModule.init("querystring") },
.{ "stream", FallbackModule.init("stream") },
.{ "string_decoder", FallbackModule.init("string_decoder") },
.{ "sys", FallbackModule.init("sys") },
.{ "timers", FallbackModule.init("timers") },
.{ "tty", FallbackModule.init("tty") },
.{ "url", FallbackModule.init("url") },
.{ "util", FallbackModule.init("util") },
.{ "zlib", FallbackModule.init("zlib") },
});
pub fn contentsFromPath(path: string) ?string {
if (Environment.allow_assert)
bun.assert(bun.strings.hasPrefixComptime(path, import_path));
var module_name = path[import_path.len..];
module_name = module_name[0 .. std.mem.indexOfScalar(u8, module_name, '/') orelse module_name.len];
if (Map.get(module_name)) |mod| {
return mod.code;
}
return null;
}