diff options
author | 2022-10-15 21:03:40 -0700 | |
---|---|---|
committer | 2022-10-15 21:03:40 -0700 | |
commit | 63c9315b24910095b76c7a01c3129c3500a2a5ad (patch) | |
tree | 4a0eeddc9dc0631db99c54bf25be45ed3e30efa2 | |
parent | 40126bbe60df2aaaad1d6be28f27ba255a1ba6d0 (diff) | |
download | bun-63c9315b24910095b76c7a01c3129c3500a2a5ad.tar.gz bun-63c9315b24910095b76c7a01c3129c3500a2a5ad.tar.zst bun-63c9315b24910095b76c7a01c3129c3500a2a5ad.zip |
Add a helper for InlineBlob from two arrays
-rw-r--r-- | src/bun.js/api/server.zig | 5 | ||||
-rw-r--r-- | src/bun.js/webcore/response.zig | 26 |
2 files changed, 24 insertions, 7 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 809b35d32..f3b9425ef 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -2134,10 +2134,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp break :getter; } - req.body = .{ .InlineBlob = JSC.WebCore.InlineBlob.init(bytes.items) }; - var to_copy: []u8 = req.body.InlineBlob.bytes[bytes.items.len..]; - @memcpy(to_copy.ptr, chunk.ptr, chunk.len); - req.body.InlineBlob.len += @truncate(JSC.WebCore.InlineBlob.IntSize, chunk.len); + req.body = .{ .InlineBlob = JSC.WebCore.InlineBlob.concat(bytes.items, chunk) }; this.request_body_buf.clearAndFree(this.allocator); } else { bytes.ensureTotalCapacityPrecise(this.allocator, total) catch |err| { diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 8dc6a1971..b1142fc98 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -4194,6 +4194,23 @@ pub const InlineBlob = extern struct { len: IntSize align(1) = 0, was_string: bool align(1) = false, + pub fn concat(first: []const u8, second: []const u8) InlineBlob { + const total = first.len + second.len; + std.debug.assert(total <= available_bytes); + + var inline_blob: JSC.WebCore.InlineBlob = .{}; + var bytes_slice = inline_blob.bytes[0..total]; + + if (first.len > 0) + @memcpy(bytes_slice.ptr, first.ptr, first.len); + + if (second.len > 0) + @memcpy(bytes_slice.ptr + first.len, second.ptr, second.len); + + inline_blob.len = @truncate(@TypeOf(inline_blob.len), total); + return inline_blob; + } + pub fn init(data: []const u8) InlineBlob { std.debug.assert(data.len <= available_bytes); @@ -4553,7 +4570,7 @@ pub const Body = struct { pub const empty = Value{ .Empty = .{} }; pub fn toReadableStream(this: *Value, globalThis: *JSGlobalObject) JSValue { - JSC.markBinding(); + JSC.markBinding(@src()); switch (this.*) { .Used, .Empty => { @@ -5548,8 +5565,7 @@ fn BodyMixin(comptime Type: type) type { var value: *Body.Value = this.getBodyValue(); if (value.* == .Used) { - globalObject.throw("Body already used", .{}); - return JSValue.jsUndefined(); + return handleBodyAlreadyUsed(globalObject); } if (value.* == .Locked) { @@ -5567,6 +5583,10 @@ fn BodyMixin(comptime Type: type) type { ) callconv(.C) JSC.JSValue { var value: *Body.Value = this.getBodyValue(); + if (value.* == .Used) { + return handleBodyAlreadyUsed(globalObject); + } + if (value.* == .Locked) { return value.Locked.setPromise(globalObject, .getBlob); } |