diff options
author | 2022-03-08 19:33:45 -0800 | |
---|---|---|
committer | 2022-03-08 19:33:45 -0800 | |
commit | 1bf898828b62922887b74752122ea0f9cd042078 (patch) | |
tree | 30db922ae15c9bf3de3f7379241aa55708526ace | |
parent | 0a742506ff6aa746367ae14450aa099945e265e0 (diff) | |
download | bun-1bf898828b62922887b74752122ea0f9cd042078.tar.gz bun-1bf898828b62922887b74752122ea0f9cd042078.tar.zst bun-1bf898828b62922887b74752122ea0f9cd042078.zip |
print size of headers
-rw-r--r-- | src/global.zig | 13 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/response.zig | 19 |
2 files changed, 20 insertions, 12 deletions
diff --git a/src/global.zig b/src/global.zig index 9b2eaff3a..e15034e92 100644 --- a/src/global.zig +++ b/src/global.zig @@ -19,7 +19,7 @@ pub const fmt = struct { pub usingnamespace std.fmt; pub const SizeFormatter = struct { - value: f64 = 0.0, + value: usize = 0, pub fn format(self: SizeFormatter, comptime _: []const u8, _: fmt.FormatOptions, writer: anytype) !void { const math = std.math; const value = self.value; @@ -39,10 +39,11 @@ pub const fmt = struct { else => unreachable, }; - try fmt.formatFloatDecimal(new_value, .{ .precision = if (new_value > 10.0 or new_value < -10.0) 2 else 0 }, writer); - if (suffix == ' ') { + try fmt.formatFloatDecimal(new_value / 1000.0, .{ .precision = 2 }, writer); return writer.writeAll(" KB"); + } else { + try fmt.formatFloatDecimal(new_value, .{ .precision = if (std.math.approxEqAbs(f64, new_value, @trunc(new_value), 0.100)) @as(usize, 0) else @as(usize, 2) }, writer); } const buf = switch (1000) { @@ -56,10 +57,10 @@ pub const fmt = struct { pub fn size(value: anytype) SizeFormatter { return switch (@TypeOf(value)) { - .f64, .f32, .f128 => SizeFormatter{ - .value = @floatCast(f64, value), + f64, f32, f128 => SizeFormatter{ + .value = @floatToInt(u64, value), }, - else => SizeFormatter{ .value = @intToFloat(f64, value) }, + else => SizeFormatter{ .value = @intCast(u64, value) }, }; } }; diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 10df2499d..2680f54a3 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Api = @import("../../../api/schema.zig").Api; +const bun = @import("../../../global.zig"); const RequestContext = @import("../../../http.zig").RequestContext; const MimeType = @import("../../../http.zig").MimeType; const ZigURL = @import("../../../query_string_map.zig").URL; @@ -84,12 +85,13 @@ pub const Response = struct { body: Body, url: string = "", status_text: string = "", + redirected: bool = false, pub const Props = struct {}; pub fn writeFormat(this: *const Response, formatter: *JSC.Formatter, writer: anytype, comptime enable_ansi_colors: bool) !void { try formatter.writeIndent(@TypeOf(writer), writer); - try writer.writeAll("Response {\n"); + try writer.print("Response ({}) {{\n", .{bun.fmt.size(this.body.len)}); { formatter.indent += 1; defer formatter.indent -|= 1; @@ -110,7 +112,11 @@ pub const Response = struct { try formatter.writeIndent(@TypeOf(writer), writer); try writer.writeAll("statusText: \""); try writer.writeAll(this.status_text); - try writer.writeAll("\""); + try writer.writeAll("\"\n"); + + try formatter.writeIndent(@TypeOf(writer), writer); + try writer.writeAll("redirected: "); + formatter.printAs(.Boolean, @TypeOf(writer), writer, JSC.JSValue.jsBoolean(this.redirected), .BooleanObject, enable_ansi_colors); } try writer.writeAll("\n"); try formatter.writeIndent(@TypeOf(writer), writer); @@ -646,6 +652,7 @@ pub const Fetch = struct { .allocator = allocator, .url = allocator.dupe(u8, this.http.url.href) catch unreachable, .status_text = allocator.dupe(u8, http_response.status) catch unreachable, + .redirected = this.http.redirect_count > 0, .body = .{ .init = .{ .headers = response_headers, @@ -1375,14 +1382,12 @@ pub const Headers = struct { } pub fn writeFormat(this: *const Headers, formatter: *JSC.Formatter, writer: anytype, comptime _: bool) !void { - try formatter.writeIndent(@TypeOf(writer), writer); - if (this.entries.len == 0) { - try writer.writeAll("Headers (0 kB) {}"); + try writer.writeAll("Headers (0 KB) {}"); return; } - try writer.print("Headers ({}) {{\n", .{std.fmt.fmtIntSizeDec(this.buf.items.len)}); + try writer.print("Headers ({}) {{\n", .{bun.fmt.size(this.buf.items.len)}); { var slice = this.entries.slice(); @@ -1463,6 +1468,8 @@ pub const Body = struct { try writer.writeAll("\n"); if (this.init.headers) |*headers| { + try formatter.writeIndent(@TypeOf(writer), writer); + try writer.writeAll("headers: "); try headers.writeFormat(formatter, writer, comptime enable_ansi_colors); try writer.writeAll("\n"); } |