rwsdk 1.0.0-beta.9-test.20251008035247 → 1.0.0-beta.9-test.20251008040832
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.
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { DefaultAppContext, RequestInfo } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* A custom error class to signal that a request is being processed
|
|
4
|
+
* on a stale context, likely due to an HMR update.
|
|
5
|
+
*/
|
|
6
|
+
export declare class StaleHmrRequestError extends Error {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
2
9
|
type DefaultRequestInfo = RequestInfo<DefaultAppContext>;
|
|
3
10
|
export declare const requestInfo: DefaultRequestInfo;
|
|
4
11
|
export declare function getRequestInfo(): RequestInfo;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
/**
|
|
3
|
+
* A custom error class to signal that a request is being processed
|
|
4
|
+
* on a stale context, likely due to an HMR update.
|
|
5
|
+
*/
|
|
6
|
+
export class StaleHmrRequestError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Stale HMR Request");
|
|
9
|
+
this.name = "StaleHmrRequestError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
const requestInfoStore = new AsyncLocalStorage();
|
|
3
13
|
const requestInfoBase = {};
|
|
4
14
|
const REQUEST_INFO_KEYS = [
|
|
@@ -16,27 +26,18 @@ REQUEST_INFO_KEYS.forEach((key) => {
|
|
|
16
26
|
configurable: false,
|
|
17
27
|
get: function () {
|
|
18
28
|
const store = requestInfoStore.getStore();
|
|
19
|
-
|
|
20
|
-
return store[key];
|
|
21
|
-
}
|
|
22
|
-
// context(justinvdm, 2025-10-08): During a chaotic HMR update, the async
|
|
23
|
-
// context (store) can be torn down while a request is in-flight. If this
|
|
24
|
-
// happens, we return an empty object for context properties instead of
|
|
25
|
-
// undefined. This prevents a hard crash when middleware (like setupDb)
|
|
26
|
-
// tries to set a property on the context of an already-orphaned request.
|
|
27
|
-
if (key === "ctx" || key === "__userContext") {
|
|
28
|
-
return {};
|
|
29
|
-
}
|
|
30
|
-
return undefined;
|
|
29
|
+
return store ? store[key] : undefined;
|
|
31
30
|
},
|
|
32
31
|
set: function (value) {
|
|
33
32
|
const store = requestInfoStore.getStore();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
if (!store) {
|
|
34
|
+
// If the store is missing, it means this setter is being called on a
|
|
35
|
+
// request that has been orphaned by an HMR update. We throw a
|
|
36
|
+
// specific error to signal this condition so the main request
|
|
37
|
+
// handler can catch it and short-circuit the request.
|
|
38
|
+
throw new StaleHmrRequestError();
|
|
39
39
|
}
|
|
40
|
+
store[key] = value;
|
|
40
41
|
},
|
|
41
42
|
});
|
|
42
43
|
});
|
package/dist/runtime/worker.js
CHANGED
|
@@ -5,7 +5,7 @@ import { renderToRscStream } from "./render/renderToRscStream";
|
|
|
5
5
|
import { injectRSCPayload } from "rsc-html-stream/server";
|
|
6
6
|
import { ErrorResponse } from "./error";
|
|
7
7
|
import { rscActionHandler } from "./register/worker";
|
|
8
|
-
import { getRequestInfo, runWithRequestInfo, runWithRequestInfoOverrides, } from "./requestInfo/worker";
|
|
8
|
+
import { getRequestInfo, runWithRequestInfo, runWithRequestInfoOverrides, StaleHmrRequestError, } from "./requestInfo/worker";
|
|
9
9
|
import { ssrWebpackRequire } from "./imports/worker";
|
|
10
10
|
import { defineRoutes } from "./lib/router";
|
|
11
11
|
import { generateNonce } from "./lib/utils";
|
|
@@ -160,6 +160,11 @@ export const defineApp = (routes) => {
|
|
|
160
160
|
}));
|
|
161
161
|
}
|
|
162
162
|
catch (e) {
|
|
163
|
+
if (e instanceof StaleHmrRequestError) {
|
|
164
|
+
console.warn("RWSDK: A request was short-circuited due to a stale HMR context.");
|
|
165
|
+
// Return a simple, empty response to gracefully terminate the request.
|
|
166
|
+
return new Response(null, { status: 204 });
|
|
167
|
+
}
|
|
163
168
|
reject(e);
|
|
164
169
|
}
|
|
165
170
|
}));
|
package/package.json
CHANGED