diff options
author | 2023-07-11 17:48:52 -0700 | |
---|---|---|
committer | 2023-07-11 17:48:52 -0700 | |
commit | 03904f73cce64c91aa770b494ef40e257f212037 (patch) | |
tree | 389e83e7d1251b2669440a799fb9c36b01e82aa3 | |
parent | 609f81a74614b20a1d49ce1197e1ddfe5aa2736f (diff) | |
parent | 2106e1d7f657a2f5fcad2c329532773bc5ce31e2 (diff) | |
download | bun-03904f73cce64c91aa770b494ef40e257f212037.tar.gz bun-03904f73cce64c91aa770b494ef40e257f212037.tar.zst bun-03904f73cce64c91aa770b494ef40e257f212037.zip |
Merge branch 'jarred/throw-if'
-rw-r--r-- | src/bun.js/node/node_fs.zig | 51 | ||||
-rw-r--r-- | test/js/node/fs/fs.test.ts | 12 |
2 files changed, 55 insertions, 8 deletions
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 74a41c5bd..3f298c5c7 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -652,6 +652,7 @@ pub const Arguments = struct { pub const Stat = struct { path: PathLike, big_int: bool = false, + throw_if_no_entry: bool = true, pub fn deinit(this: Stat) void { this.path.deinit(); @@ -672,13 +673,25 @@ pub const Arguments = struct { if (exception.* != null) return null; + var throw_if_no_entry = true; + const big_int = brk: { if (arguments.next()) |next_val| { if (next_val.isObject()) { if (next_val.isCallable(ctx.ptr().vm())) break :brk false; arguments.eat(); - if (next_val.getOptional(ctx.ptr(), "bigint", bool) catch false) |big_int| { + if (next_val.getOptional(ctx.ptr(), "throwIfNoEntry", bool) catch { + path.deinit(); + return null; + }) |throw_if_no_entry_val| { + throw_if_no_entry = throw_if_no_entry_val; + } + + if (next_val.getOptional(ctx.ptr(), "bigint", bool) catch { + path.deinit(); + return null; + }) |big_int| { break :brk big_int; } } @@ -688,7 +701,7 @@ pub const Arguments = struct { if (exception.* != null) return null; - return Stat{ .path = path, .big_int = big_int }; + return Stat{ .path = path, .big_int = big_int, .throw_if_no_entry = throw_if_no_entry }; } }; @@ -2497,6 +2510,18 @@ pub const Arguments = struct { }; }; +pub const StatOrNotFound = union(enum) { + stats: Stats, + not_found: void, + + pub fn toJS(this: *StatOrNotFound, globalObject: *JSC.JSGlobalObject) JSC.JSValue { + return switch (this.*) { + .stats => this.stats.toJS(globalObject), + .not_found => JSC.JSValue.undefined, + }; + } +}; + const Return = struct { pub const Access = void; pub const AppendFile = void; @@ -2515,7 +2540,7 @@ const Return = struct { pub const Lchmod = void; pub const Lchown = void; pub const Link = void; - pub const Lstat = Stats; + pub const Lstat = StatOrNotFound; pub const Mkdir = bun.String; pub const Mkdtemp = JSC.ZigString; pub const Open = FileDescriptor; @@ -2617,7 +2642,7 @@ const Return = struct { pub const RealpathNative = Realpath; pub const Rename = void; pub const Rmdir = void; - pub const Stat = Stats; + pub const Stat = StatOrNotFound; pub const Symlink = void; pub const Truncate = void; @@ -3129,8 +3154,13 @@ pub const NodeFS = struct { &this.sync_error_buf, ), )) { - .result => |result| Maybe(Return.Lstat){ .result = Return.Lstat.init(result, false) }, - .err => |err| Maybe(Return.Lstat){ .err = err }, + .result => |result| Maybe(Return.Lstat){ .result = .{ .stats = Stats.init(result, args.big_int) } }, + .err => |err| brk: { + if (!args.throw_if_no_entry and err.getErrno() == .NOENT) { + return Maybe(Return.Lstat){ .result = .{ .not_found = {} } }; + } + break :brk Maybe(Return.Lstat){ .err = err }; + }, }; }, else => {}, @@ -4333,8 +4363,13 @@ pub const NodeFS = struct { &this.sync_error_buf, ), )) { - .result => |result| Maybe(Return.Stat){ .result = Return.Stat.init(result, false) }, - .err => |err| Maybe(Return.Stat){ .err = err }, + .result => |result| Maybe(Return.Stat){ .result = .{ .stats = Stats.init(result, args.big_int) } }, + .err => |err| brk: { + if (!args.throw_if_no_entry and err.getErrno() == .NOENT) { + return Maybe(Return.Stat){ .result = .{ .not_found = {} } }; + } + break :brk Maybe(Return.Stat){ .err = err }; + }, }); }, else => {}, diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 0236010be..48aa9d3b9 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -159,6 +159,18 @@ it("readdirSync on import.meta.dir", () => { expect(match).toBe(true); }); +it("statSync throwIfNoEntry", () => { + expect(statSync("/tmp/404/not-found/ok", { throwIfNoEntry: false })).toBeUndefined(); + expect(lstatSync("/tmp/404/not-found/ok", { throwIfNoEntry: false })).toBeUndefined(); +}); + +it("statSync throwIfNoEntry: true", () => { + expect(() => statSync("/tmp/404/not-found/ok", { throwIfNoEntry: true })).toThrow("No such file or directory"); + expect(() => statSync("/tmp/404/not-found/ok")).toThrow("No such file or directory"); + expect(() => lstatSync("/tmp/404/not-found/ok", { throwIfNoEntry: true })).toThrow("No such file or directory"); + expect(() => lstatSync("/tmp/404/not-found/ok")).toThrow("No such file or directory"); +}); + // https://github.com/oven-sh/bun/issues/1887 it("mkdtempSync, readdirSync, rmdirSync and unlinkSync with non-ascii", () => { const tempdir = mkdtempSync(`${tmpdir()}/emoji-fruit-🍇 🍈 🍉 🍊 🍋`); |