"path" module from Node.js implementation

This commit is contained in:
Jarred Sumner
2022-02-02 18:02:06 -08:00
parent 64b74ede87
commit 68cb6130d3
15 changed files with 875 additions and 12 deletions

View File

@@ -356,6 +356,18 @@ pub const Bun = struct {
return JSValue.createStringArray(ctx.ptr(), styles.ptr, styles.len, true).asRef();
}
pub fn newPath(
_: void,
ctx: js.JSContextRef,
_: js.JSObjectRef,
_: js.JSObjectRef,
args: []const js.JSValueRef,
_: js.ExceptionRef,
) js.JSValueRef {
const is_windows = args.len == 1 and JSValue.fromRef(args[0]).toBoolean();
return Node.Path.create(ctx.ptr(), is_windows).asObjectRef();
}
pub fn readFileAsStringCallback(
ctx: js.JSContextRef,
buf_z: [:0]const u8,
@@ -705,6 +717,10 @@ pub const Bun = struct {
.@"return" = "string[]",
},
},
._Path = .{
.rfn = Bun.newPath,
.ts = d.ts{},
},
.getRouteNames = .{
.rfn = Bun.getRouteNames,
.ts = d.ts{
@@ -1596,6 +1612,15 @@ pub const VirtualMachine = struct {
.hash = 0,
.bytecodecache_fd = 0,
};
} else if (strings.eqlComptime(_specifier, "node:path")) {
return ResolvedSource{
.allocator = null,
.source_code = ZigString.init(Node.Path.code),
.specifier = ZigString.init("node:path"),
.source_url = ZigString.init("node:path"),
.hash = 0,
.bytecodecache_fd = 0,
};
}
const specifier = normalizeSpecifier(_specifier);
@@ -1746,11 +1771,16 @@ pub const VirtualMachine = struct {
ret.result = null;
ret.path = specifier;
return;
} else if (strings.eqlComptime(specifier, "fs") or strings.eqlComptime(specifier, "node:fs")) {
} else if (strings.eqlComptime(specifier, "node:fs")) {
ret.result = null;
ret.path = "node:fs";
return;
}
if (strings.eqlComptime(specifier, "node:path")) {
ret.result = null;
ret.path = "node:path";
return;
}
const is_special_source = strings.eqlComptime(source, main_file_name) or js_ast.Macro.isMacroPath(source);