@rangojs/router 0.0.0-experimental.82 → 0.0.0-experimental.83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/response-route-handler.ts +12 -0
package/dist/vite/index.js
CHANGED
|
@@ -1864,7 +1864,7 @@ import { resolve } from "node:path";
|
|
|
1864
1864
|
// package.json
|
|
1865
1865
|
var package_default = {
|
|
1866
1866
|
name: "@rangojs/router",
|
|
1867
|
-
version: "0.0.0-experimental.
|
|
1867
|
+
version: "0.0.0-experimental.83",
|
|
1868
1868
|
description: "Django-inspired RSC router with composable URL patterns",
|
|
1869
1869
|
keywords: [
|
|
1870
1870
|
"react",
|
package/package.json
CHANGED
package/src/rsc/helpers.ts
CHANGED
|
@@ -8,9 +8,49 @@ import {
|
|
|
8
8
|
_getRequestContext,
|
|
9
9
|
getLocationState,
|
|
10
10
|
} from "../server/request-context.js";
|
|
11
|
+
import type { RequestContext } from "../server/request-context.js";
|
|
11
12
|
import { resolveLocationStateEntries } from "../browser/react/location-state-shared.js";
|
|
12
13
|
import type { MiddlewareEntry, MiddlewareFn } from "../router/middleware.js";
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Copy stub headers from the request context onto a target Headers instance:
|
|
17
|
+
* append Set-Cookie entries, set everything else only if absent. Header
|
|
18
|
+
* mutation failures are swallowed so the same logic works against Response
|
|
19
|
+
* headers that may be immutable (e.g. Cloudflare protocol-switch responses).
|
|
20
|
+
*/
|
|
21
|
+
function applyStubHeaders(target: Headers, stub: Headers): void {
|
|
22
|
+
stub.forEach((value, name) => {
|
|
23
|
+
try {
|
|
24
|
+
if (name.toLowerCase() === "set-cookie") {
|
|
25
|
+
target.append(name, value);
|
|
26
|
+
} else if (!target.has(name)) {
|
|
27
|
+
target.set(name, value);
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// Headers immutable — skip.
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Drain ctx._onResponseCallbacks onto a response. Swapping the array before
|
|
37
|
+
* iteration prevents re-entrant registrations from double-firing and matches
|
|
38
|
+
* the contract that each callback runs at most once per request.
|
|
39
|
+
*/
|
|
40
|
+
function drainOnResponseCallbacks(
|
|
41
|
+
ctx: RequestContext,
|
|
42
|
+
response: Response,
|
|
43
|
+
): Response {
|
|
44
|
+
const callbacks = ctx._onResponseCallbacks;
|
|
45
|
+
if (callbacks.length === 0) return response;
|
|
46
|
+
ctx._onResponseCallbacks = [];
|
|
47
|
+
let result = response;
|
|
48
|
+
for (const callback of callbacks) {
|
|
49
|
+
result = callback(result) ?? result;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
14
54
|
/**
|
|
15
55
|
* Check if a request body has content to decode
|
|
16
56
|
*/
|
|
@@ -39,40 +79,23 @@ export function createResponseWithMergedHeaders(
|
|
|
39
79
|
return new Response(body, init);
|
|
40
80
|
}
|
|
41
81
|
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
// merge points (e.g. executeMiddleware) do not duplicate them.
|
|
82
|
+
// Delete Set-Cookie from the stub after consuming so downstream merge
|
|
83
|
+
// points (e.g. executeMiddleware) don't duplicate them.
|
|
45
84
|
const mergedHeaders = new Headers(init.headers);
|
|
46
|
-
ctx.res.headers
|
|
47
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
48
|
-
mergedHeaders.append(name, value);
|
|
49
|
-
} else if (!mergedHeaders.has(name)) {
|
|
50
|
-
// Only set if not already present in init.headers
|
|
51
|
-
mergedHeaders.set(name, value);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
85
|
+
applyStubHeaders(mergedHeaders, ctx.res.headers);
|
|
54
86
|
ctx.res.headers.delete("set-cookie");
|
|
55
87
|
|
|
56
|
-
//
|
|
57
|
-
//
|
|
88
|
+
// ctx.res.status overrides init.status when explicitly set (e.g. 404 for
|
|
89
|
+
// notFound, 500 for error). Default ctx.res.status is 200.
|
|
58
90
|
const status = ctx.res.status !== 200 ? ctx.res.status : init.status;
|
|
59
91
|
|
|
60
|
-
|
|
92
|
+
const response = new Response(body, {
|
|
61
93
|
...init,
|
|
62
94
|
status,
|
|
63
95
|
headers: mergedHeaders,
|
|
64
96
|
});
|
|
65
97
|
|
|
66
|
-
|
|
67
|
-
// Drain the array so that downstream callers (e.g. finalizeResponse)
|
|
68
|
-
// do not re-execute the same callbacks on this response.
|
|
69
|
-
const callbacks = ctx._onResponseCallbacks;
|
|
70
|
-
ctx._onResponseCallbacks = [];
|
|
71
|
-
for (const callback of callbacks) {
|
|
72
|
-
response = callback(response) ?? response;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return response;
|
|
98
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
76
99
|
}
|
|
77
100
|
|
|
78
101
|
/**
|
|
@@ -175,24 +198,29 @@ export function buildRouteMiddlewareEntries<TEnv>(
|
|
|
175
198
|
}
|
|
176
199
|
|
|
177
200
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
201
|
+
* Merge stub headers from the request context onto an existing Response in
|
|
202
|
+
* place, then drain onResponse callbacks. Used when a Response cannot flow
|
|
203
|
+
* through `new Response()` — status 101 is outside the constructor's
|
|
204
|
+
* 200-599 range, and the Cloudflare-specific `webSocket` property would be
|
|
205
|
+
* lost on reconstruction.
|
|
183
206
|
*/
|
|
184
|
-
export function
|
|
207
|
+
export function mergeStubHeadersAndFinalize(response: Response): Response {
|
|
185
208
|
const ctx = _getRequestContext();
|
|
186
|
-
if (!ctx
|
|
187
|
-
return response;
|
|
188
|
-
}
|
|
209
|
+
if (!ctx) return response;
|
|
189
210
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
211
|
+
applyStubHeaders(response.headers, ctx.res.headers);
|
|
212
|
+
ctx.res.headers.delete("set-cookie");
|
|
213
|
+
|
|
214
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Run onResponse callbacks on an existing Response. Used by code paths that
|
|
219
|
+
* bypass createResponseWithMergedHeaders (e.g. middleware short-circuits)
|
|
220
|
+
* but still need ctx.onResponse() callbacks to fire.
|
|
221
|
+
*/
|
|
222
|
+
export function finalizeResponse(response: Response): Response {
|
|
223
|
+
const ctx = _getRequestContext();
|
|
224
|
+
if (!ctx) return response;
|
|
225
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
198
226
|
}
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
finalizeResponse,
|
|
27
27
|
isCacheableStatus,
|
|
28
28
|
buildRouteMiddlewareEntries,
|
|
29
|
+
mergeStubHeadersAndFinalize,
|
|
29
30
|
} from "./helpers.js";
|
|
30
31
|
|
|
31
32
|
export interface ResponseRouteMatch {
|
|
@@ -96,6 +97,17 @@ export async function handleResponseRoute<TEnv>(
|
|
|
96
97
|
// so that stub headers (cookies, custom headers set via ctx.header()) are included.
|
|
97
98
|
// Use Headers (not Record<string, string>) to preserve duplicate entries like Set-Cookie.
|
|
98
99
|
const rewrapResponse = (result: Response) => {
|
|
100
|
+
// WebSocket upgrades can't flow through `new Response()`: status 101 is
|
|
101
|
+
// outside the constructor's 200-599 range, and the Cloudflare-specific
|
|
102
|
+
// `webSocket` property would be lost on reconstruction. 204/205/304
|
|
103
|
+
// are intentionally NOT short-circuited — they're valid for the
|
|
104
|
+
// constructor and must honor ctx.setStatus() overrides.
|
|
105
|
+
const hasWebSocket =
|
|
106
|
+
(result as unknown as { webSocket?: unknown }).webSocket != null;
|
|
107
|
+
if (hasWebSocket || result.status === 101) {
|
|
108
|
+
return mergeStubHeadersAndFinalize(result);
|
|
109
|
+
}
|
|
110
|
+
|
|
99
111
|
const headers = new Headers();
|
|
100
112
|
result.headers.forEach((value, key) => {
|
|
101
113
|
if (key.toLowerCase() === "set-cookie") {
|