rwsdk 1.0.0-beta.9-test.20251006190822 → 1.0.0-beta.9-test.20251007055438

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,5 +1,6 @@
1
1
  import "./types/worker";
2
2
  export * from "../error";
3
+ export * from "../lib/requestState";
3
4
  export * from "../lib/utils";
4
5
  export * from "../register/worker";
5
6
  export * from "../render/renderToStream";
@@ -1,5 +1,6 @@
1
1
  import "./types/worker";
2
2
  export * from "../error";
3
+ export * from "../lib/requestState";
3
4
  export * from "../lib/utils";
4
5
  export * from "../register/worker";
5
6
  export * from "../render/renderToStream";
@@ -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,39 @@
1
+ import { requestInfo } from "../requestInfo/worker.js";
2
+ /**
3
+ * Creates a request-scoped state variable that automatically resolves to the correct
4
+ * instance based on the current request context using AsyncLocalStorage.
5
+ *
6
+ * @returns A tuple containing [proxy object, setter function]
7
+ */
8
+ export function defineRequestState() {
9
+ // Generate a unique key for this state variable to prevent collisions
10
+ const key = `__requestState_${crypto.randomUUID()}`;
11
+ const setter = (value) => {
12
+ requestInfo.__userContext[key] = value;
13
+ };
14
+ // Create a proxy that delegates all property access to the stored instance
15
+ const proxy = new Proxy({}, {
16
+ get(target, prop, receiver) {
17
+ const userContext = requestInfo.__userContext;
18
+ const instance = userContext?.[key];
19
+ if (!instance) {
20
+ throw new Error(`Request-scoped state not initialized. Make sure to call setter before accessing properties.`);
21
+ }
22
+ const value = instance[prop];
23
+ if (typeof value === "function") {
24
+ return value.bind(instance);
25
+ }
26
+ return value;
27
+ },
28
+ set(target, prop, value) {
29
+ const userContext = requestInfo.__userContext;
30
+ const instance = userContext?.[key];
31
+ if (!instance) {
32
+ throw new Error(`Request-scoped state not initialized. Make sure to call setter before setting properties.`);
33
+ }
34
+ instance[prop] = value;
35
+ return true;
36
+ },
37
+ });
38
+ return [proxy, setter];
39
+ }
@@ -11,4 +11,5 @@ export interface RequestInfo<Params = any, AppContext = DefaultAppContext> {
11
11
  headers: Headers;
12
12
  };
13
13
  isAction: boolean;
14
+ __userContext?: Record<string, any>;
14
15
  }
@@ -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 = ["request", "params", "ctx", "rw", "cf", "response"];
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,
@@ -80,6 +80,7 @@ export const defineApp = (routes) => {
80
80
  rw,
81
81
  response: userResponseInit,
82
82
  isAction,
83
+ __userContext: {},
83
84
  };
84
85
  const createPageElement = (requestInfo, Page) => {
85
86
  let pageElement;
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-beta.9-test.20251006190822",
3
+ "version": "1.0.0-beta.9-test.20251007055438",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {