diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig index 5b20f045a9..101c12e1a3 100644 --- a/src/bun.js/node/types.zig +++ b/src/bun.js/node/types.zig @@ -450,12 +450,7 @@ pub const StringOrBuffer = union(enum) { } } - pub fn fromJSMaybeAsync( - global: *JSC.JSGlobalObject, - allocator: std.mem.Allocator, - value: JSC.JSValue, - is_async: bool, - ) ?StringOrBuffer { + pub fn fromJSMaybeAsync(global: *JSC.JSGlobalObject, allocator: std.mem.Allocator, value: JSC.JSValue, is_async: bool) ?StringOrBuffer { return switch (value.jsType()) { JSC.JSValue.JSType.String, JSC.JSValue.JSType.StringObject, JSC.JSValue.JSType.DerivedStringObject, JSC.JSValue.JSType.Object => { const str = bun.String.tryFromJS(value, global) orelse return null; @@ -495,9 +490,11 @@ pub const StringOrBuffer = union(enum) { else => null, }; } + pub fn fromJS(global: *JSC.JSGlobalObject, allocator: std.mem.Allocator, value: JSC.JSValue) ?StringOrBuffer { return fromJSMaybeAsync(global, allocator, value, false); } + pub fn fromJSWithEncoding(global: *JSC.JSGlobalObject, allocator: std.mem.Allocator, value: JSC.JSValue, encoding: Encoding) ?StringOrBuffer { return fromJSWithEncodingMaybeAsync(global, allocator, value, encoding, false); } diff --git a/src/js/node/fs.promises.ts b/src/js/node/fs.promises.ts index 7ba2171442..803887f40f 100644 --- a/src/js/node/fs.promises.ts +++ b/src/js/node/fs.promises.ts @@ -159,7 +159,13 @@ const exports = { close: fs.close.bind(fs), copyFile: fs.copyFile.bind(fs), cp, - exists: fs.exists.bind(fs), + exists: async function exists() { + try { + return await fs.exists.$apply(fs, arguments); + } catch (e) { + return false; + } + }, chown: fs.chown.bind(fs), chmod: fs.chmod.bind(fs), fchmod: fs.fchmod.bind(fs), diff --git a/src/js/node/fs.ts b/src/js/node/fs.ts index 4885b9a8cb..c6fe06884b 100644 --- a/src/js/node/fs.ts +++ b/src/js/node/fs.ts @@ -315,7 +315,13 @@ var access = function access(...args) { appendFileSync = fs.appendFileSync.bind(fs), closeSync = fs.closeSync.bind(fs), copyFileSync = fs.copyFileSync.bind(fs), - existsSync = fs.existsSync.bind(fs), + existsSync = function existsSync() { + try { + return fs.existsSync.$apply(fs, arguments); + } catch (e) { + return false; + } + }, chownSync = fs.chownSync.bind(fs), chmodSync = fs.chmodSync.bind(fs), fchmodSync = fs.fchmodSync.bind(fs), diff --git a/src/string.zig b/src/string.zig index a7b9fbc0f4..5172be01f1 100644 --- a/src/string.zig +++ b/src/string.zig @@ -1305,6 +1305,10 @@ pub const SliceWithUnderlyingString = struct { return this.utf8.slice(); } + pub fn sliceZ(this: SliceWithUnderlyingString) [:0]const u8 { + return this.utf8.sliceZ(); + } + pub fn format(self: SliceWithUnderlyingString, comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { if (self.utf8.len == 0) { try self.underlying.format(fmt, opts, writer); diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 25ba2fa425..b28ff5f252 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -2998,3 +2998,11 @@ it("fs.close with one arg works", () => { const fd = fs.openSync(filepath, "w+"); fs.close(fd); }); + +it("existsSync should never throw ENAMETOOLONG", () => { + expect(existsSync(new Array(16).fill(new Array(64).fill("a")).join("/"))).toBeFalse(); +}); + +it("promises exists should never throw ENAMETOOLONG", async () => { + expect(await _promises.exists(new Array(16).fill(new Array(64).fill("a")).join("/"))).toBeFalse(); +});