diff options
author | 2022-09-07 20:59:51 -0700 | |
---|---|---|
committer | 2022-09-07 20:59:51 -0700 | |
commit | c038f513d32726fb0abb43936065ca236c2b8840 (patch) | |
tree | ebfbd67419331385d7f3e4e01ae2a1d715c93623 /src | |
parent | 1e5978ad4fac919d44104e1ee502a9aa6e41956a (diff) | |
download | bun-c038f513d32726fb0abb43936065ca236c2b8840.tar.gz bun-c038f513d32726fb0abb43936065ca236c2b8840.tar.zst bun-c038f513d32726fb0abb43936065ca236c2b8840.zip |
[Bun.write] clean up some error handling
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/webcore/response.zig | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index e36de8257..4e96cff4c 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -1209,6 +1209,11 @@ pub const Blob = struct { return null; } + if (path_or_blob == .blob and path_or_blob.blob.store == null) { + exception.* = JSC.toInvalidArguments("Blob is detached", .{}, ctx).asObjectRef(); + return null; + } + if (data.isString()) { const len = data.getLengthOfArray(ctx); if (len == 0) @@ -1344,31 +1349,40 @@ pub const Blob = struct { return writeFileWithSourceDestination(ctx, &source_blob, &destination_blob); } + const write_permissions = 0o664; + fn writeStringToFileFast( globalThis: *JSC.JSGlobalObject, pathlike: JSC.Node.PathOrFileDescriptor, str: ZigString, comptime needs_open: bool, ) JSC.JSValue { - var fd: JSC.Node.FileDescriptor = if (comptime !needs_open) pathlike.fd else std.math.maxInt(JSC.Node.FileDescriptor); - - if (needs_open) { + const fd: JSC.Node.FileDescriptor = if (comptime !needs_open) pathlike.fd else brk: { var file_path: [bun.MAX_PATH_BYTES]u8 = undefined; - switch (JSC.Node.Syscall.open(pathlike.path.sliceZ(&file_path), std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK, 0o644)) { + switch (JSC.Node.Syscall.open( + pathlike.path.sliceZ(&file_path), + // we deliberately don't use O_TRUNC here + // it's a perf optimization + std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK, + write_permissions, + )) { .result => |result| { - fd = result; + break :brk result; }, .err => |err| { return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); }, } - } + unreachable; + }; var truncate = needs_open; var jsc_vm = globalThis.bunVM(); var written: usize = 0; defer { + // we only truncate if it's a path + // if it's a file descriptor, we assume they want manual control over that behavior if (truncate) { _ = JSC.Node.Syscall.system.ftruncate(fd, @intCast(i64, written)); } @@ -1419,8 +1433,7 @@ pub const Blob = struct { } } else { var decoded = str.toOwnedSlice(jsc_vm.allocator) catch { - globalThis.vm().throwError(globalThis, ZigString.static("Out of memory").toErrorInstance(globalThis)); - return JSC.JSValue.jsUndefined(); + return JSC.JSPromise.rejectedPromiseValue(globalThis, ZigString.static("Out of memory").toErrorInstance(globalThis)); }; defer jsc_vm.allocator.free(decoded); var remain = decoded; @@ -1450,19 +1463,24 @@ pub const Blob = struct { bytes: []const u8, comptime needs_open: bool, ) JSC.JSValue { - var fd: JSC.Node.FileDescriptor = if (comptime !needs_open) pathlike.fd else std.math.maxInt(JSC.Node.FileDescriptor); - - if (needs_open) { + const fd: JSC.Node.FileDescriptor = if (comptime !needs_open) pathlike.fd else brk: { var file_path: [bun.MAX_PATH_BYTES]u8 = undefined; - switch (JSC.Node.Syscall.open(pathlike.path.sliceZ(&file_path), std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK, 0644)) { + switch (JSC.Node.Syscall.open( + pathlike.path.sliceZ(&file_path), + // we deliberately don't use O_TRUNC here + // it's a perf optimization + std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK, + write_permissions, + )) { .result => |result| { - fd = result; + break :brk result; }, .err => |err| { return JSC.JSPromise.rejectedPromiseValue(globalThis, err.toJSC(globalThis)); }, } - } + unreachable; + }; var truncate = needs_open; var written: usize = 0; |