Skip to content

Commit fb14c3a

Browse files
committed
[scramjet/demo] better request viewer
1 parent 0dcfc3f commit fb14c3a

File tree

3 files changed

+247
-75
lines changed

3 files changed

+247
-75
lines changed

packages/scramjet/packages/demo/src/App.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const normalizeHeaders = (
2020
input?: Headers | Array<[string, string]> | Record<string, string> | null
2121
): Array<[string, string]> => {
2222
if (!input) return [];
23+
if (typeof (input as any).toRawHeaders === "function") {
24+
return (input as any).toRawHeaders();
25+
}
2326
if (input instanceof Headers) return [...input.entries()];
2427
if (Array.isArray(input)) return input;
2528
return Object.entries(input);
@@ -85,6 +88,7 @@ export const App: Component<
8588
performance.now()
8689
);
8790
const reqHeaders = normalizeHeaders(props.init?.headers);
91+
const reqHeadersPre = normalizeHeaders(context.request.initialHeaders);
8892
const reqBodyInfo = getBodyPreview(props.init?.body);
8993

9094
const entry: RequestEntry = {
@@ -94,6 +98,7 @@ export const App: Component<
9498
time: new Date().toLocaleTimeString(),
9599
destination: context.request.destination,
96100
mode: context.request.mode,
101+
requestHeadersPre: reqHeadersPre,
97102
requestHeaders: reqHeaders,
98103
requestBodyPreview: reqBodyInfo.preview,
99104
requestBodySize: reqBodyInfo.size,
@@ -105,6 +110,27 @@ export const App: Component<
105110
}
106111
});
107112

113+
plugin.tap(
114+
this.frame.hooks.fetch.preresponse,
115+
(context: any, props: any) => {
116+
const id = this.requestIdByRequest.get(context.request as object);
117+
if (!id) return;
118+
const preHeaders = normalizeHeaders(props.response?.rawHeaders);
119+
const preBodyInfo = getBodyPreview(props.response?.body);
120+
121+
this.requests = this.requests.map((entry) =>
122+
entry.id === id
123+
? {
124+
...entry,
125+
responseHeadersPre: preHeaders,
126+
responseBodyPreviewPre: preBodyInfo.preview,
127+
responseBodySizePre: preBodyInfo.size,
128+
}
129+
: entry
130+
);
131+
}
132+
);
133+
108134
plugin.tap(
109135
this.frame.hooks.fetch.response,
110136
(context: any, props: any) => {

packages/scramjet/packages/demo/src/RequestCard.tsx

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,10 @@ export const RequestCard: Component<
88
onSelect?: (id: string) => void;
99
},
1010
{},
11-
data-status-class={
12-
this.request.status !== undefined
13-
? String(Math.floor(this.request.status / 100))
14-
: ""
15-
}
1611
{}
1712
> = function () {
1813
return (
19-
<span
20-
class={
21-
this.request.status !== undefined
22-
? `request-status status-${Math.floor(
23-
this.request.status / 100
24-
)}`
25-
: "request-status"
26-
}
27-
>
28-
{this.request.status ?? "…"}
29-
</span>
14+
<div
3015
class={use(this.selected).map(
3116
(selected) => `request-row ${selected ? "selected" : ""}`
3217
)}
@@ -35,7 +20,15 @@ export const RequestCard: Component<
3520
>
3621
<div class="request-main">
3722
<span class="request-method">{this.request.method}</span>
38-
<span class="request-status">{this.request.status ?? "…"}</span>
23+
<span
24+
class={
25+
this.request.status !== undefined
26+
? `request-status status-${Math.floor(this.request.status / 100)}`
27+
: "request-status"
28+
}
29+
>
30+
{this.request.status ?? "…"}
31+
</span>
3932
<span class="request-duration">
4033
{this.request.durationMs !== undefined
4134
? `${this.request.durationMs}ms`
@@ -92,13 +85,22 @@ RequestCard.style = css`
9285
.request-method {
9386
font-weight: 600;
9487
color: #fff;
95-
background: rgba(148, 163, 184, 0.15);
96-
border: 1px solid rgba(148, 163, 184, 0.35);
97-
color: #e2e8f0;
88+
padding: 0.15em 0.5em;
89+
border-radius: 6px;
90+
background: rgba(59, 130, 246, 0.2);
9891
border: 1px solid rgba(59, 130, 246, 0.35);
9992
letter-spacing: 0.02em;
10093
}
10194
.request-status {
95+
padding: 0.15em 0.45em;
96+
border-radius: 6px;
97+
background: rgba(148, 163, 184, 0.15);
98+
border: 1px solid rgba(148, 163, 184, 0.35);
99+
color: #e2e8f0;
100+
min-width: 2.5em;
101+
text-align: center;
102+
font-variant-numeric: tabular-nums;
103+
}
102104
.request-status.status-2 {
103105
background: rgba(34, 197, 94, 0.15);
104106
border: 1px solid rgba(34, 197, 94, 0.35);
@@ -119,17 +121,6 @@ RequestCard.style = css`
119121
border: 1px solid rgba(248, 113, 113, 0.4);
120122
color: #fecaca;
121123
}
122-
padding: 0.15em 0.45em;
123-
border-radius: 6px;
124-
background: rgba(34, 197, 94, 0.15);
125-
border: 1px solid rgba(34, 197, 94, 0.35);
126-
color: #bbf7d0;
127-
min-width: 2.5em;
128-
text-align: center;
129-
font-variant-numeric: tabular-nums;
130-
white-space: nowrap;
131-
overflow: hidden;
132-
text-overflow: ellipsis;
133124
.request-duration,
134125
.request-destination,
135126
.request-time {
@@ -138,7 +129,9 @@ RequestCard.style = css`
138129
.request-url {
139130
font-size: 0.85em;
140131
color: #e5e7eb;
141-
word-break: break-all;
132+
white-space: nowrap;
133+
overflow: hidden;
134+
text-overflow: ellipsis;
142135
}
143136
.request-type {
144137
font-size: 0.75em;

0 commit comments

Comments
 (0)