rwsdk 0.1.28 → 0.1.29

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,
@@ -43,6 +43,10 @@ export const defineApp = (routes) => {
43
43
  databases: new Map(),
44
44
  pageRouteResolved: undefined,
45
45
  };
46
+ const userResponseInit = {
47
+ status: 200,
48
+ headers: new Headers(),
49
+ };
46
50
  const outerRequestInfo = {
47
51
  request,
48
52
  headers: userHeaders,
@@ -50,6 +54,7 @@ export const defineApp = (routes) => {
50
54
  params: {},
51
55
  ctx: {},
52
56
  rw,
57
+ response: userResponseInit,
53
58
  };
54
59
  const createPageElement = (requestInfo, Page) => {
55
60
  let pageElement;
@@ -88,10 +93,12 @@ export const defineApp = (routes) => {
88
93
  onError,
89
94
  });
90
95
  if (isRSCRequest) {
96
+ const responseHeaders = new Headers(userResponseInit.headers);
97
+ responseHeaders.set("content-type", "text/x-component; charset=utf-8");
91
98
  return new Response(rscPayloadStream, {
92
- headers: {
93
- "content-type": "text/x-component; charset=utf-8",
94
- },
99
+ status: userResponseInit.status,
100
+ statusText: userResponseInit.statusText,
101
+ headers: responseHeaders,
95
102
  });
96
103
  }
97
104
  let injectRSCPayloadStream;
@@ -111,10 +118,12 @@ export const defineApp = (routes) => {
111
118
  if (injectRSCPayloadStream) {
112
119
  html = html.pipeThrough(injectRSCPayloadStream);
113
120
  }
121
+ const responseHeaders = new Headers(userResponseInit.headers);
122
+ responseHeaders.set("content-type", "text/html; charset=utf-8");
114
123
  return new Response(html, {
115
- headers: {
116
- "content-type": "text/html; charset=utf-8",
117
- },
124
+ status: userResponseInit.status,
125
+ statusText: userResponseInit.statusText,
126
+ headers: responseHeaders,
118
127
  });
119
128
  };
120
129
  const response = await runWithRequestInfo(outerRequestInfo, async () => new Promise(async (resolve, reject) => {
@@ -134,11 +143,21 @@ export const defineApp = (routes) => {
134
143
  // context(justinvdm, 18 Mar 2025): In some cases, such as a .fetch() call to a durable object instance, or Response.redirect(),
135
144
  // we need to return a mutable response object.
136
145
  const mutableResponse = new Response(response.body, response);
146
+ // Merge user headers from the legacy headers object
137
147
  for (const [key, value] of userHeaders.entries()) {
138
148
  if (!response.headers.has(key)) {
139
149
  mutableResponse.headers.set(key, value);
140
150
  }
141
151
  }
152
+ // Merge headers from user response init (these take precedence)
153
+ if (userResponseInit.headers) {
154
+ const userResponseHeaders = new Headers(userResponseInit.headers);
155
+ for (const [key, value] of userResponseHeaders.entries()) {
156
+ if (!response.headers.has(key)) {
157
+ mutableResponse.headers.set(key, value);
158
+ }
159
+ }
160
+ }
142
161
  await rw.pageRouteResolved?.promise;
143
162
  return mutableResponse;
144
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {