rwsdk 0.2.0-alpha.1 → 0.2.0-alpha.2
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.
|
@@ -5,7 +5,9 @@ export interface RequestInfo<Params = any, AppContext = DefaultAppContext> {
|
|
|
5
5
|
request: Request;
|
|
6
6
|
params: Params;
|
|
7
7
|
ctx: AppContext;
|
|
8
|
+
/** @deprecated: Use `response.headers` instead */
|
|
8
9
|
headers: Headers;
|
|
9
10
|
rw: RwContext;
|
|
10
11
|
cf: ExecutionContext;
|
|
12
|
+
response: ResponseInit;
|
|
11
13
|
}
|
|
@@ -2,7 +2,7 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
2
2
|
const requestInfoDeferred = Promise.withResolvers();
|
|
3
3
|
const requestInfoStore = new AsyncLocalStorage();
|
|
4
4
|
const requestInfoBase = {};
|
|
5
|
-
const REQUEST_INFO_KEYS = ["request", "params", "ctx", "headers", "rw", "cf"];
|
|
5
|
+
const REQUEST_INFO_KEYS = ["request", "params", "ctx", "headers", "rw", "cf", "response"];
|
|
6
6
|
REQUEST_INFO_KEYS.forEach((key) => {
|
|
7
7
|
Object.defineProperty(requestInfoBase, key, {
|
|
8
8
|
enumerable: true,
|
package/dist/runtime/worker.js
CHANGED
|
@@ -44,6 +44,10 @@ export const defineApp = (routes) => {
|
|
|
44
44
|
scriptsToBeLoaded: new Set(),
|
|
45
45
|
pageRouteResolved: undefined,
|
|
46
46
|
};
|
|
47
|
+
const userResponseInit = {
|
|
48
|
+
status: 200,
|
|
49
|
+
headers: new Headers(),
|
|
50
|
+
};
|
|
47
51
|
const outerRequestInfo = {
|
|
48
52
|
request,
|
|
49
53
|
headers: userHeaders,
|
|
@@ -51,6 +55,7 @@ export const defineApp = (routes) => {
|
|
|
51
55
|
params: {},
|
|
52
56
|
ctx: {},
|
|
53
57
|
rw,
|
|
58
|
+
response: userResponseInit,
|
|
54
59
|
};
|
|
55
60
|
const createPageElement = (requestInfo, Page) => {
|
|
56
61
|
let pageElement;
|
|
@@ -89,10 +94,12 @@ export const defineApp = (routes) => {
|
|
|
89
94
|
onError,
|
|
90
95
|
});
|
|
91
96
|
if (isRSCRequest) {
|
|
97
|
+
const responseHeaders = new Headers(userResponseInit.headers);
|
|
98
|
+
responseHeaders.set("content-type", "text/x-component; charset=utf-8");
|
|
92
99
|
return new Response(rscPayloadStream, {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
status: userResponseInit.status,
|
|
101
|
+
statusText: userResponseInit.statusText,
|
|
102
|
+
headers: responseHeaders,
|
|
96
103
|
});
|
|
97
104
|
}
|
|
98
105
|
let injectRSCPayloadStream;
|
|
@@ -112,10 +119,12 @@ export const defineApp = (routes) => {
|
|
|
112
119
|
if (injectRSCPayloadStream) {
|
|
113
120
|
html = html.pipeThrough(injectRSCPayloadStream);
|
|
114
121
|
}
|
|
122
|
+
const responseHeaders = new Headers(userResponseInit.headers);
|
|
123
|
+
responseHeaders.set("content-type", "text/html; charset=utf-8");
|
|
115
124
|
return new Response(html, {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
status: userResponseInit.status,
|
|
126
|
+
statusText: userResponseInit.statusText,
|
|
127
|
+
headers: responseHeaders,
|
|
119
128
|
});
|
|
120
129
|
};
|
|
121
130
|
const response = await runWithRequestInfo(outerRequestInfo, async () => new Promise(async (resolve, reject) => {
|
|
@@ -135,11 +144,21 @@ export const defineApp = (routes) => {
|
|
|
135
144
|
// context(justinvdm, 18 Mar 2025): In some cases, such as a .fetch() call to a durable object instance, or Response.redirect(),
|
|
136
145
|
// we need to return a mutable response object.
|
|
137
146
|
const mutableResponse = new Response(response.body, response);
|
|
147
|
+
// Merge user headers from the legacy headers object
|
|
138
148
|
for (const [key, value] of userHeaders.entries()) {
|
|
139
149
|
if (!response.headers.has(key)) {
|
|
140
150
|
mutableResponse.headers.set(key, value);
|
|
141
151
|
}
|
|
142
152
|
}
|
|
153
|
+
// Merge headers from user response init (these take precedence)
|
|
154
|
+
if (userResponseInit.headers) {
|
|
155
|
+
const userResponseHeaders = new Headers(userResponseInit.headers);
|
|
156
|
+
for (const [key, value] of userResponseHeaders.entries()) {
|
|
157
|
+
if (!response.headers.has(key)) {
|
|
158
|
+
mutableResponse.headers.set(key, value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
143
162
|
await rw.pageRouteResolved?.promise;
|
|
144
163
|
return mutableResponse;
|
|
145
164
|
}
|