diff options
author | 2022-07-24 01:20:44 -0700 | |
---|---|---|
committer | 2022-07-24 01:22:33 -0700 | |
commit | c921e4a3f1e8eb4f5f584407cfdcebd4ea66cbfa (patch) | |
tree | cfc43b3d8cfbeb35c36916c6dca7ab89a9f0ae03 | |
parent | f524c720958c0e8c81fe846ec2a6a90a3a9b7092 (diff) | |
download | bun-c921e4a3f1e8eb4f5f584407cfdcebd4ea66cbfa.tar.gz bun-c921e4a3f1e8eb4f5f584407cfdcebd4ea66cbfa.tar.zst bun-c921e4a3f1e8eb4f5f584407cfdcebd4ea66cbfa.zip |
Improve `console.log(new Blob([123]))` output
-rw-r--r-- | src/bun.js/webcore/response.zig | 102 |
1 files changed, 95 insertions, 7 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 867be4445..16d5c8951 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -194,11 +194,6 @@ pub const Response = struct { formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable; try writer.writeAll("\n"); - try this.body.writeFormat(formatter, writer, enable_ansi_colors); - - formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable; - try writer.writeAll("\n"); - try formatter.writeIndent(Writer, writer); try writer.writeAll("url: \""); try writer.print(comptime Output.prettyFmt("<r><b>{s}<r>", enable_ansi_colors), .{this.url}); @@ -215,6 +210,9 @@ pub const Response = struct { try formatter.writeIndent(Writer, writer); try writer.writeAll("redirected: "); formatter.printAs(.Boolean, Writer, writer, JSC.JSValue.jsBoolean(this.redirected), .BooleanObject, enable_ansi_colors); + formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable; + try writer.writeAll("\n"); + try this.body.writeFormat(formatter, writer, enable_ansi_colors); } try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); @@ -1068,6 +1066,89 @@ pub const Blob = struct { return this.store == null; } + pub fn writeFormat(this: *const Blob, formatter: *JSC.Formatter, writer: anytype, comptime enable_ansi_colors: bool) !void { + const Writer = @TypeOf(writer); + + try formatter.writeIndent(Writer, writer); + + if (this.isDetached()) { + try writer.writeAll(comptime Output.prettyFmt("<d>[<r>Blob<r> detached<d>]<r>", enable_ansi_colors)); + return; + } + + var store = this.store.?; + switch (store.data) { + .file => |file| { + try writer.writeAll(comptime Output.prettyFmt("<r>FileRef<r>", enable_ansi_colors)); + switch (file.pathlike) { + .path => |path| { + try writer.print( + comptime Output.prettyFmt(" (<green>\"{s}\"<r>)<r>", enable_ansi_colors), + .{ + path.slice(), + }, + ); + }, + .fd => |fd| { + try writer.print( + comptime Output.prettyFmt(" (<r>fd: <yellow>{d}<r>)<r>", enable_ansi_colors), + .{ + fd, + }, + ); + }, + } + }, + .bytes => { + try writer.writeAll(comptime Output.prettyFmt("<r>Blob<r>", enable_ansi_colors)); + try writer.print( + comptime Output.prettyFmt(" (<yellow>{any}<r>)", enable_ansi_colors), + .{ + bun.fmt.size(this.size), + }, + ); + }, + } + + if (this.content_type.len > 0 or this.offset > 0) { + try writer.writeAll(" {\n"); + { + formatter.indent += 1; + defer formatter.indent -= 1; + + if (this.content_type.len > 0) { + try formatter.writeIndent(Writer, writer); + try writer.print( + comptime Output.prettyFmt("type: <green>\"{s}\"<r>", enable_ansi_colors), + .{ + this.content_type, + }, + ); + + if (this.offset > 0) { + formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable; + } + + try writer.writeAll("\n"); + } + + if (this.offset > 0) { + try formatter.writeIndent(Writer, writer); + + try writer.print( + comptime Output.prettyFmt("offset: <yellow>{d}<r>\n", enable_ansi_colors), + .{ + this.offset, + }, + ); + } + } + + try formatter.writeIndent(Writer, writer); + try writer.writeAll("}"); + } + } + const CopyFilePromiseHandler = struct { promise: *JSPromise, globalThis: *JSGlobalObject, @@ -1962,7 +2043,6 @@ pub const Blob = struct { system_error: ?JSC.SystemError = null, errno: ?anyerror = null, open_completion: HTTPClient.NetworkThread.Completion = undefined, - write_completion: HTTPClient.NetworkThread.Completion = undefined, close_completion: HTTPClient.NetworkThread.Completion = undefined, task: HTTPClient.NetworkThread.Task = undefined, @@ -3701,6 +3781,11 @@ pub const Body = struct { try formatter.writeIndent(Writer, writer); try writer.writeAll("status: "); formatter.printAs(.Double, Writer, writer, JSC.JSValue.jsNumber(this.init.status_code), .NumberObject, enable_ansi_colors); + if (this.value == .Blob) { + try formatter.printComma(Writer, writer, enable_ansi_colors); + try writer.writeAll("\n"); + try this.value.Blob.writeFormat(formatter, writer, enable_ansi_colors); + } } pub fn deinit(this: *Body, _: std.mem.Allocator) void { @@ -4144,7 +4229,10 @@ pub const Request = struct { try writer.writeAll("url: \""); try writer.print(comptime Output.prettyFmt("<r><b>{}<r>", enable_ansi_colors), .{this.url}); try writer.writeAll("\""); - formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable; + if (this.body == .Blob) { + try writer.writeAll("\n"); + try this.body.Blob.writeFormat(formatter, writer, enable_ansi_colors); + } } try writer.writeAll("\n"); try formatter.writeIndent(Writer, writer); |