aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-30 20:50:12 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-30 20:50:12 -0700
commitab7183217a33323c991ab5c3b04caec32954d9ee (patch)
tree69a0dbeaf1109f73b8a8b556c321dd2af25ce752
parent47a9a452d8ff4d6876d389df2205507a383d5dc8 (diff)
downloadbun-ab7183217a33323c991ab5c3b04caec32954d9ee.tar.gz
bun-ab7183217a33323c991ab5c3b04caec32954d9ee.tar.zst
bun-ab7183217a33323c991ab5c3b04caec32954d9ee.zip
[bun.js] Support `file://` URLs in node fs
-rw-r--r--integration/bunjs-only-snippets/fs.test.js8
-rw-r--r--src/javascript/jsc/bindings/bindings.zig41
-rw-r--r--src/javascript/jsc/bindings/headers-cpp.h8
-rw-r--r--src/javascript/jsc/bindings/headers-handwritten.h5
-rw-r--r--src/javascript/jsc/bindings/headers-replacements.zig1
-rw-r--r--src/javascript/jsc/bindings/headers.h10
-rw-r--r--src/javascript/jsc/bindings/headers.zig7
-rw-r--r--src/javascript/jsc/bindings/helpers.h8
-rw-r--r--src/javascript/jsc/node/types.zig20
9 files changed, 102 insertions, 6 deletions
diff --git a/integration/bunjs-only-snippets/fs.test.js b/integration/bunjs-only-snippets/fs.test.js
index 4c048d199..4ee4c652a 100644
--- a/integration/bunjs-only-snippets/fs.test.js
+++ b/integration/bunjs-only-snippets/fs.test.js
@@ -30,6 +30,14 @@ describe("readFileSync", () => {
expect(text).toBe("File read successfully");
});
+ it("works with a file url", () => {
+ const text = readFileSync(
+ new URL("file://" + import.meta.dir + "/readFileSync.txt"),
+ "utf8"
+ );
+ expect(text).toBe("File read successfully");
+ });
+
it("returning Buffer works", () => {
const text = readFileSync(import.meta.dir + "/readFileSync.txt");
const encoded = [
diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig
index ffb7baae0..15f71a470 100644
--- a/src/javascript/jsc/bindings/bindings.zig
+++ b/src/javascript/jsc/bindings/bindings.zig
@@ -349,6 +349,43 @@ pub const ZigString = extern struct {
pub const Extern = [_][]const u8{ "toValue", "toExternalValue", "to16BitValue", "toValueGC", "toErrorInstance", "toExternalU16", "toExternalValueWithCallback", "external" };
};
+pub const DOMURL = opaque {
+ pub const shim = Shimmer("WebCore", "DOMURL", @This());
+
+ const cppFn = shim.cppFn;
+ pub const name = "WebCore::DOMURL";
+
+ pub fn cast(value: JSValue) ?*DOMURL {
+ return shim.cppFn("cast", .{value});
+ }
+
+ pub fn href_(this: *DOMURL, out: *ZigString) void {
+ return shim.cppFn("href_", .{ this, out });
+ }
+
+ pub fn href(this: *DOMURL) ZigString {
+ var out = ZigString.Empty;
+ this.href_(&out);
+ return out;
+ }
+
+ pub fn pathname_(this: *DOMURL, out: *ZigString) void {
+ return shim.cppFn("pathname_", .{ this, out });
+ }
+
+ pub fn pathname(this: *DOMURL) ZigString {
+ var out = ZigString.Empty;
+ this.pathname_(&out);
+ return out;
+ }
+
+ pub const Extern = [_][]const u8{
+ "cast",
+ "href_",
+ "pathname_",
+ };
+};
+
pub const SystemError = extern struct {
errno: c_int = 0,
/// label for errno
@@ -1901,6 +1938,10 @@ pub const JSValue = enum(u64) {
if (value.isUndefinedOrNull())
return null;
+ if (comptime ZigType == DOMURL) {
+ return DOMURL.cast(value);
+ }
+
return JSC.GetJSPrivateData(ZigType, value.asObjectRef());
}
diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h
index aa51de7e7..86d8ca7a0 100644
--- a/src/javascript/jsc/bindings/headers-cpp.h
+++ b/src/javascript/jsc/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1648440945
+//-- AUTOGENERATED FILE -- 1648698232
// clang-format off
#pragma once
@@ -256,8 +256,8 @@ extern "C" const size_t Zig__ConsoleClient_object_align_ = alignof(Zig::ConsoleC
extern "C" const size_t Bun__Timer_object_size_ = sizeof(Bun__Timer);
extern "C" const size_t Bun__Timer_object_align_ = alignof(Bun__Timer);
-const size_t sizes[30] = {sizeof(JSC::JSObject), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::CallFrame), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(Zig::GlobalObject), sizeof(Bun__Readable), sizeof(Bun__Writable), sizeof(Bun__Path)};
+const size_t sizes[31] = {sizeof(JSC::JSObject), sizeof(WebCore::DOMURL), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::CallFrame), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(Zig::GlobalObject), sizeof(Bun__Readable), sizeof(Bun__Writable), sizeof(Bun__Path)};
-const char* names[30] = {"JSC__JSObject", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__CallFrame", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "Zig__GlobalObject", "Bun__Readable", "Bun__Writable", "Bun__Path"};
+const char* names[31] = {"JSC__JSObject", "WebCore__DOMURL", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__CallFrame", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "Zig__GlobalObject", "Bun__Readable", "Bun__Writable", "Bun__Path"};
-const size_t aligns[30] = {alignof(JSC::JSObject), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::CallFrame), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView), alignof(Zig::GlobalObject), alignof(Bun__Readable), alignof(Bun__Writable), alignof(Bun__Path)};
+const size_t aligns[31] = {alignof(JSC::JSObject), alignof(WebCore::DOMURL), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::CallFrame), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView), alignof(Zig::GlobalObject), alignof(Bun__Readable), alignof(Bun__Writable), alignof(Bun__Path)};
diff --git a/src/javascript/jsc/bindings/headers-handwritten.h b/src/javascript/jsc/bindings/headers-handwritten.h
index 8e2bafc76..0c4cc2754 100644
--- a/src/javascript/jsc/bindings/headers-handwritten.h
+++ b/src/javascript/jsc/bindings/headers-handwritten.h
@@ -177,6 +177,11 @@ typedef struct {
#ifdef __cplusplus
+typedef struct StringPointer {
+ uint32_t off;
+ uint32_t len;
+} StringPointer;
+
extern "C" ZigErrorCode Zig_ErrorCodeParserError;
extern "C" void ZigString__free(const unsigned char* ptr, size_t len, void* allocator);
diff --git a/src/javascript/jsc/bindings/headers-replacements.zig b/src/javascript/jsc/bindings/headers-replacements.zig
index 5f453b64f..f17cef709 100644
--- a/src/javascript/jsc/bindings/headers-replacements.zig
+++ b/src/javascript/jsc/bindings/headers-replacements.zig
@@ -59,3 +59,4 @@ pub const JSC__CatchScope = bindings.CatchScope;
pub const Bun__Readable = bindings.NodeReadableStream;
pub const Bun__Writable = bindings.NodeWritableStream;
pub const Bun__ArrayBuffer = bindings.ArrayBuffer;
+pub const WebCore__DOMURL = bindings.DOMURL;
diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h
index 95f5ec3a9..501f21d23 100644
--- a/src/javascript/jsc/bindings/headers.h
+++ b/src/javascript/jsc/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format: off
-//-- AUTOGENERATED FILE -- 1648440945
+//-- AUTOGENERATED FILE -- 1648698232
#pragma once
#include <stddef.h>
@@ -170,6 +170,7 @@ typedef uint64_t JSC__JSValue;
typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype
typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype
typedef ZigString ZigString;
+typedef struct WebCore__DOMURL WebCore__DOMURL; // WebCore::DOMURL
typedef struct JSC__FunctionPrototype JSC__FunctionPrototype; // JSC::FunctionPrototype
typedef bInspector__ScriptArguments Inspector__ScriptArguments; // Inspector::ScriptArguments
typedef bJSC__Exception JSC__Exception; // JSC::Exception
@@ -227,6 +228,9 @@ class ExternalStringImpl;
namespace Zig {
class JSMicrotaskCallback;
}
+namespace WebCore {
+class DOMURL;
+}
namespace Inspector {
class ScriptArguments;
}
@@ -282,6 +286,7 @@ using WTF__String = WTF::String;
using WTF__StringView = WTF::StringView;
using WTF__ExternalStringImpl = WTF::ExternalStringImpl;
using Zig__JSMicrotaskCallback = Zig::JSMicrotaskCallback;
+using WebCore__DOMURL = WebCore::DOMURL;
using Inspector__ScriptArguments = Inspector::ScriptArguments;
#endif
@@ -301,6 +306,9 @@ CPP_DECL JSC__JSValue ZigString__toExternalValue(const ZigString* arg0, JSC__JSG
CPP_DECL JSC__JSValue ZigString__toExternalValueWithCallback(const ZigString* arg0, JSC__JSGlobalObject* arg1, void (*ArgFn2)(void* arg0, void* arg1, size_t arg2));
CPP_DECL JSC__JSValue ZigString__toValue(const ZigString* arg0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSValue ZigString__toValueGC(const ZigString* arg0, JSC__JSGlobalObject* arg1);
+CPP_DECL WebCore__DOMURL* WebCore__DOMURL__cast(JSC__JSValue JSValue0);
+CPP_DECL void WebCore__DOMURL__href_(WebCore__DOMURL* arg0, ZigString* arg1);
+CPP_DECL void WebCore__DOMURL__pathname_(WebCore__DOMURL* arg0, ZigString* arg1);
CPP_DECL JSC__JSValue SystemError__toErrorInstance(const SystemError* arg0, JSC__JSGlobalObject* arg1);
#pragma mark - JSC::JSCell
diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig
index d42eaf7e5..fd2abbf13 100644
--- a/src/javascript/jsc/bindings/headers.zig
+++ b/src/javascript/jsc/bindings/headers.zig
@@ -45,6 +45,8 @@ pub const bJSC__CallFrame = bindings.CallFrame;
pub const bInspector__ScriptArguments = bindings.ScriptArguments;
pub const JSC__JSValue = bindings.JSValue;
+pub const struct_WebCore__DOMURL = bindings.DOMURL;
+
// Inlined types
pub const ZigString = bindings.ZigString;
pub const ZigException = bindings.ZigException;
@@ -123,6 +125,8 @@ pub const JSC__BigIntPrototype = struct_JSC__BigIntPrototype;
pub const JSC__GeneratorFunctionPrototype = struct_JSC__GeneratorFunctionPrototype;
+pub const WebCore__DOMURL = struct_WebCore__DOMURL;
+
pub const JSC__FunctionPrototype = struct_JSC__FunctionPrototype;
pub const Inspector__ScriptArguments = bInspector__ScriptArguments;
pub const JSC__Exception = bJSC__Exception;
@@ -144,6 +148,9 @@ pub extern fn ZigString__toExternalValue(arg0: [*c]const ZigString, arg1: [*c]JS
pub extern fn ZigString__toExternalValueWithCallback(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject, ArgFn2: ?fn (?*anyopaque, ?*anyopaque, usize) callconv(.C) void) JSC__JSValue;
pub extern fn ZigString__toValue(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn ZigString__toValueGC(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
+pub extern fn WebCore__DOMURL__cast(JSValue0: JSC__JSValue) ?*WebCore__DOMURL;
+pub extern fn WebCore__DOMURL__href_(arg0: ?*WebCore__DOMURL, arg1: [*c]ZigString) void;
+pub extern fn WebCore__DOMURL__pathname_(arg0: ?*WebCore__DOMURL, arg1: [*c]ZigString) void;
pub extern fn SystemError__toErrorInstance(arg0: [*c]const SystemError, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSCell__getObject(arg0: [*c]JSC__JSCell) [*c]JSC__JSObject;
pub extern fn JSC__JSCell__getString(arg0: [*c]JSC__JSCell, arg1: [*c]JSC__JSGlobalObject) bWTF__String;
diff --git a/src/javascript/jsc/bindings/helpers.h b/src/javascript/jsc/bindings/helpers.h
index 392bf6070..79c9a7ace 100644
--- a/src/javascript/jsc/bindings/helpers.h
+++ b/src/javascript/jsc/bindings/helpers.h
@@ -202,6 +202,14 @@ static ZigString toZigString(WTF::StringView& str)
str.length() };
}
+static ZigString toZigString(const WTF::StringView& str)
+{
+ return str.isEmpty()
+ ? ZigStringEmpty
+ : ZigString { str.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
+ str.length() };
+}
+
static ZigString toZigString(JSC::JSString& str, JSC::JSGlobalObject* global)
{
return toZigString(str.value(global));
diff --git a/src/javascript/jsc/node/types.zig b/src/javascript/jsc/node/types.zig
index ea57dc583..9ad49e756 100644
--- a/src/javascript/jsc/node/types.zig
+++ b/src/javascript/jsc/node/types.zig
@@ -14,6 +14,7 @@ const Buffer = JSC.MarkedArrayBuffer;
const IdentityContext = @import("../../../identity_context.zig").IdentityContext;
const logger = @import("../../../logger.zig");
const Fs = @import("../../../fs.zig");
+const URL = @import("../../../url.zig").URL;
const Shimmer = @import("../bindings/shimmer.zig").Shimmer;
const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false;
const meta = bun.meta;
@@ -335,7 +336,24 @@ pub const PathLike = union(Tag) {
return PathLike{ .string = PathString.init(zig_str.slice()) };
},
- else => return null,
+ else => {
+ if (arg.as(JSC.DOMURL)) |domurl| {
+ var zig_str = domurl.pathname();
+ if (!Valid.pathString(zig_str, ctx, exception)) return null;
+
+ JSC.C.JSValueProtect(ctx, arg.asObjectRef());
+ arguments.eat();
+
+ if (zig_str.is16Bit()) {
+ var printed = std.mem.span(std.fmt.allocPrintZ(arguments.arena.allocator(), "{}", .{zig_str}) catch unreachable);
+ return PathLike{ .string = PathString.init(printed.ptr[0 .. printed.len + 1]) };
+ }
+
+ return PathLike{ .string = PathString.init(zig_str.slice()) };
+ }
+
+ return null;
+ },
}
}
};