rwsdk 1.0.0-beta.9-test.20251006190822 → 1.0.0-beta.9-test.20251007155132
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/runtime/entries/worker.d.ts +1 -0
- package/dist/runtime/entries/worker.js +1 -0
- package/dist/runtime/lib/requestState.d.ts +7 -0
- package/dist/runtime/lib/requestState.js +40 -0
- package/dist/runtime/requestInfo/types.d.ts +1 -0
- package/dist/runtime/requestInfo/worker.js +9 -1
- package/dist/runtime/worker.js +1 -0
- package/dist/vite/runDirectivesScan.mjs +0 -2
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a request-scoped state variable that automatically resolves to the correct
|
|
3
|
+
* instance based on the current request context using AsyncLocalStorage.
|
|
4
|
+
*
|
|
5
|
+
* @returns A tuple containing [proxy object, setter function]
|
|
6
|
+
*/
|
|
7
|
+
export declare function defineRequestState<T>(): [T, (value: T) => void];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { requestInfo } from "../requestInfo/worker.js";
|
|
2
|
+
let stateCounter = 0;
|
|
3
|
+
/**
|
|
4
|
+
* Creates a request-scoped state variable that automatically resolves to the correct
|
|
5
|
+
* instance based on the current request context using AsyncLocalStorage.
|
|
6
|
+
*
|
|
7
|
+
* @returns A tuple containing [proxy object, setter function]
|
|
8
|
+
*/
|
|
9
|
+
export function defineRequestState() {
|
|
10
|
+
// Generate a unique key for this state variable to prevent collisions
|
|
11
|
+
const key = `__requestState_${stateCounter++}`;
|
|
12
|
+
const setter = (value) => {
|
|
13
|
+
requestInfo.__userContext[key] = value;
|
|
14
|
+
};
|
|
15
|
+
// Create a proxy that delegates all property access to the stored instance
|
|
16
|
+
const proxy = new Proxy({}, {
|
|
17
|
+
get(target, prop, receiver) {
|
|
18
|
+
const userContext = requestInfo.__userContext;
|
|
19
|
+
const instance = userContext?.[key];
|
|
20
|
+
if (!instance) {
|
|
21
|
+
throw new Error(`Request-scoped state not initialized. Make sure to call setter before accessing properties.`);
|
|
22
|
+
}
|
|
23
|
+
const value = instance[prop];
|
|
24
|
+
if (typeof value === "function") {
|
|
25
|
+
return value.bind(instance);
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
},
|
|
29
|
+
set(target, prop, value) {
|
|
30
|
+
const userContext = requestInfo.__userContext;
|
|
31
|
+
const instance = userContext?.[key];
|
|
32
|
+
if (!instance) {
|
|
33
|
+
throw new Error(`Request-scoped state not initialized. Make sure to call setter before setting properties.`);
|
|
34
|
+
}
|
|
35
|
+
instance[prop] = value;
|
|
36
|
+
return true;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return [proxy, setter];
|
|
40
|
+
}
|
|
@@ -2,7 +2,15 @@ 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 = [
|
|
5
|
+
const REQUEST_INFO_KEYS = [
|
|
6
|
+
"request",
|
|
7
|
+
"params",
|
|
8
|
+
"ctx",
|
|
9
|
+
"rw",
|
|
10
|
+
"cf",
|
|
11
|
+
"response",
|
|
12
|
+
"__userContext",
|
|
13
|
+
];
|
|
6
14
|
REQUEST_INFO_KEYS.forEach((key) => {
|
|
7
15
|
Object.defineProperty(requestInfoBase, key, {
|
|
8
16
|
enumerable: true,
|
package/dist/runtime/worker.js
CHANGED
|
@@ -20,7 +20,6 @@ const externalRE = /^(https?:)?\/\//;
|
|
|
20
20
|
const isExternalUrl = (url) => externalRE.test(url);
|
|
21
21
|
async function findDirectiveRoots({ root, readFileWithCache, directiveCheckCache, }) {
|
|
22
22
|
const srcDir = path.resolve(root, "src");
|
|
23
|
-
console.log("########", srcDir);
|
|
24
23
|
const files = await glob("**/*.{ts,tsx,js,jsx,mjs,mts,cjs,cts,mdx}", {
|
|
25
24
|
cwd: srcDir,
|
|
26
25
|
absolute: true,
|
|
@@ -87,7 +86,6 @@ export function classifyModule({ contents, inheritedEnv, }) {
|
|
|
87
86
|
return { moduleEnv, isClient, isServer };
|
|
88
87
|
}
|
|
89
88
|
export const runDirectivesScan = async ({ rootConfig, environments, clientFiles, serverFiles, entries: initialEntries, }) => {
|
|
90
|
-
console.log("######## entries", initialEntries);
|
|
91
89
|
deferredLog("\n… (rwsdk) Scanning for 'use client' and 'use server' directives...");
|
|
92
90
|
// Set environment variable to indicate scanning is in progress
|
|
93
91
|
process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE = "true";
|
package/package.json
CHANGED