rwsdk 1.0.0-beta.27-test.20251112194755 → 1.0.0-beta.27-test.20251116215153

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.
Files changed (35) hide show
  1. package/dist/runtime/client/client.d.ts +1 -0
  2. package/dist/runtime/imports/__mocks__/use-client-lookup.d.ts +6 -0
  3. package/dist/runtime/imports/__mocks__/use-client-lookup.js +6 -0
  4. package/dist/runtime/lib/db/typeInference/builders/alterTable.d.ts +3 -13
  5. package/dist/runtime/lib/db/typeInference/builders/columnDefinition.d.ts +20 -34
  6. package/dist/runtime/lib/db/typeInference/builders/createTable.d.ts +2 -9
  7. package/dist/runtime/lib/db/typeInference/database.d.ts +2 -16
  8. package/dist/runtime/lib/db/typeInference/typetests/alterTable.typetest.js +5 -80
  9. package/dist/runtime/lib/db/typeInference/typetests/createTable.typetest.js +1 -102
  10. package/dist/runtime/lib/db/typeInference/typetests/testUtils.d.ts +0 -1
  11. package/dist/runtime/lib/db/typeInference/utils.d.ts +9 -59
  12. package/dist/runtime/lib/stitchDocumentAndAppStreams.js +149 -21
  13. package/dist/runtime/lib/stitchDocumentAndAppStreams.test.d.ts +1 -0
  14. package/dist/runtime/lib/stitchDocumentAndAppStreams.test.js +384 -0
  15. package/dist/use-synced-state/SyncStateServer.d.mts +20 -0
  16. package/dist/use-synced-state/SyncStateServer.mjs +124 -0
  17. package/dist/use-synced-state/__tests__/SyncStateServer.test.d.mts +1 -0
  18. package/dist/use-synced-state/__tests__/SyncStateServer.test.mjs +109 -0
  19. package/dist/use-synced-state/__tests__/useSyncState.test.d.ts +1 -0
  20. package/dist/use-synced-state/__tests__/useSyncState.test.js +115 -0
  21. package/dist/use-synced-state/__tests__/useSyncedState.test.d.ts +1 -0
  22. package/dist/use-synced-state/__tests__/useSyncedState.test.js +115 -0
  23. package/dist/use-synced-state/__tests__/worker.test.d.mts +1 -0
  24. package/dist/use-synced-state/__tests__/worker.test.mjs +69 -0
  25. package/dist/use-synced-state/client.d.ts +28 -0
  26. package/dist/use-synced-state/client.js +39 -0
  27. package/dist/use-synced-state/constants.d.mts +1 -0
  28. package/dist/use-synced-state/constants.mjs +1 -0
  29. package/dist/use-synced-state/useSyncState.d.ts +20 -0
  30. package/dist/use-synced-state/useSyncState.js +58 -0
  31. package/dist/use-synced-state/useSyncedState.d.ts +20 -0
  32. package/dist/use-synced-state/useSyncedState.js +58 -0
  33. package/dist/use-synced-state/worker.d.mts +14 -0
  34. package/dist/use-synced-state/worker.mjs +73 -0
  35. package/package.json +12 -3
@@ -0,0 +1,58 @@
1
+ import { React } from "../runtime/client/client";
2
+ import { getSyncStateClient } from "./client";
3
+ import { DEFAULT_SYNC_STATE_PATH } from "./constants.mjs";
4
+ const defaultDeps = {
5
+ useState: React.useState,
6
+ useEffect: React.useEffect,
7
+ useRef: React.useRef,
8
+ useCallback: React.useCallback,
9
+ };
10
+ /**
11
+ * Builds a `useSyncedState` hook configured with optional endpoint and hook overrides.
12
+ * @param options Optional overrides for endpoint and React primitives.
13
+ * @returns Hook that syncs state through the sync state service.
14
+ */
15
+ export const createSyncStateHook = (options = {}) => {
16
+ const resolvedUrl = options.url ?? DEFAULT_SYNC_STATE_PATH;
17
+ const deps = options.hooks ?? defaultDeps;
18
+ const { useState, useEffect, useRef, useCallback } = deps;
19
+ return function useSyncedState(initialValue, key) {
20
+ if (typeof window === "undefined" && !options.hooks) {
21
+ return [initialValue, () => { }];
22
+ }
23
+ const client = getSyncStateClient(resolvedUrl);
24
+ const [value, setValue] = useState(initialValue);
25
+ const valueRef = useRef(value);
26
+ valueRef.current = value;
27
+ const setSyncValue = useCallback((nextValue) => {
28
+ const resolved = typeof nextValue === "function"
29
+ ? nextValue(valueRef.current)
30
+ : nextValue;
31
+ setValue(resolved);
32
+ valueRef.current = resolved;
33
+ void client.setState(resolved, key);
34
+ }, [client, key, setValue, valueRef]);
35
+ useEffect(() => {
36
+ let isActive = true;
37
+ const handleUpdate = (next) => {
38
+ if (isActive) {
39
+ setValue(next);
40
+ valueRef.current = next;
41
+ }
42
+ };
43
+ void client.getState(key).then((existing) => {
44
+ if (existing !== undefined && isActive) {
45
+ setValue(existing);
46
+ valueRef.current = existing;
47
+ }
48
+ });
49
+ void client.subscribe(key, handleUpdate);
50
+ return () => {
51
+ isActive = false;
52
+ void client.unsubscribe(key, handleUpdate);
53
+ };
54
+ }, [client, key, setValue, valueRef]);
55
+ return [value, setSyncValue];
56
+ };
57
+ };
58
+ export const useSyncedState = createSyncStateHook();
@@ -0,0 +1,20 @@
1
+ import { React } from "../runtime/client/client";
2
+ type HookDeps = {
3
+ useState: typeof React.useState;
4
+ useEffect: typeof React.useEffect;
5
+ useRef: typeof React.useRef;
6
+ useCallback: typeof React.useCallback;
7
+ };
8
+ type Setter<T> = (value: T | ((previous: T) => T)) => void;
9
+ export type CreateSyncStateHookOptions = {
10
+ url?: string;
11
+ hooks?: HookDeps;
12
+ };
13
+ /**
14
+ * Builds a `useSyncedState` hook configured with optional endpoint and hook overrides.
15
+ * @param options Optional overrides for endpoint and React primitives.
16
+ * @returns Hook that syncs state through the sync state service.
17
+ */
18
+ export declare const createSyncStateHook: (options?: CreateSyncStateHookOptions) => <T>(initialValue: T, key: string) => [T, Setter<T>];
19
+ export declare const useSyncedState: <T>(initialValue: T, key: string) => [T, Setter<T>];
20
+ export {};
@@ -0,0 +1,58 @@
1
+ import { React } from "../runtime/client/client";
2
+ import { getSyncStateClient } from "./client";
3
+ import { DEFAULT_SYNC_STATE_PATH } from "./constants.mjs";
4
+ const defaultDeps = {
5
+ useState: React.useState,
6
+ useEffect: React.useEffect,
7
+ useRef: React.useRef,
8
+ useCallback: React.useCallback,
9
+ };
10
+ /**
11
+ * Builds a `useSyncedState` hook configured with optional endpoint and hook overrides.
12
+ * @param options Optional overrides for endpoint and React primitives.
13
+ * @returns Hook that syncs state through the sync state service.
14
+ */
15
+ export const createSyncStateHook = (options = {}) => {
16
+ const resolvedUrl = options.url ?? DEFAULT_SYNC_STATE_PATH;
17
+ const deps = options.hooks ?? defaultDeps;
18
+ const { useState, useEffect, useRef, useCallback } = deps;
19
+ return function useSyncedState(initialValue, key) {
20
+ if (typeof window === "undefined" && !options.hooks) {
21
+ return [initialValue, () => { }];
22
+ }
23
+ const client = getSyncStateClient(resolvedUrl);
24
+ const [value, setValue] = useState(initialValue);
25
+ const valueRef = useRef(value);
26
+ valueRef.current = value;
27
+ const setSyncValue = useCallback((nextValue) => {
28
+ const resolved = typeof nextValue === "function"
29
+ ? nextValue(valueRef.current)
30
+ : nextValue;
31
+ setValue(resolved);
32
+ valueRef.current = resolved;
33
+ void client.setState(resolved, key);
34
+ }, [client, key, setValue, valueRef]);
35
+ useEffect(() => {
36
+ let isActive = true;
37
+ const handleUpdate = (next) => {
38
+ if (isActive) {
39
+ setValue(next);
40
+ valueRef.current = next;
41
+ }
42
+ };
43
+ void client.getState(key).then((existing) => {
44
+ if (existing !== undefined && isActive) {
45
+ setValue(existing);
46
+ valueRef.current = existing;
47
+ }
48
+ });
49
+ void client.subscribe(key, handleUpdate);
50
+ return () => {
51
+ isActive = false;
52
+ void client.unsubscribe(key, handleUpdate);
53
+ };
54
+ }, [client, key, setValue, valueRef]);
55
+ return [value, setSyncValue];
56
+ };
57
+ };
58
+ export const useSyncedState = createSyncStateHook();
@@ -0,0 +1,14 @@
1
+ import { SyncStateServer } from "./SyncStateServer.mjs";
2
+ export { SyncStateServer } from "./SyncStateServer.mjs";
3
+ export type SyncStateRouteOptions = {
4
+ basePath?: string;
5
+ resetPath?: string;
6
+ durableObjectName?: string;
7
+ };
8
+ /**
9
+ * Registers routes that forward sync state requests to the configured Durable Object namespace.
10
+ * @param getNamespace Function that returns the Durable Object namespace from the Worker env.
11
+ * @param options Optional overrides for base path, reset path, and object name.
12
+ * @returns Router entries for the sync state API and reset endpoint.
13
+ */
14
+ export declare const syncStateRoutes: (getNamespace: (env: Cloudflare.Env) => DurableObjectNamespace<SyncStateServer>, options?: SyncStateRouteOptions) => import("../runtime/lib/router.js").RouteDefinition<import("../runtime/worker.js").RequestInfo<any, import("../runtime/worker.js").DefaultAppContext>>[];
@@ -0,0 +1,73 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _SyncStateProxy_stub, _SyncStateProxy_keyHandler;
13
+ import { RpcTarget, newWorkersRpcResponse } from "capnweb";
14
+ import { env } from "cloudflare:workers";
15
+ import { route } from "../runtime/entries/router";
16
+ import { SyncStateServer } from "./SyncStateServer.mjs";
17
+ import { DEFAULT_SYNC_STATE_PATH } from "./constants.mjs";
18
+ export { SyncStateServer } from "./SyncStateServer.mjs";
19
+ const DEFAULT_SYNC_STATE_NAME = "syncedState";
20
+ class SyncStateProxy extends RpcTarget {
21
+ constructor(stub, keyHandler) {
22
+ super();
23
+ _SyncStateProxy_stub.set(this, void 0);
24
+ _SyncStateProxy_keyHandler.set(this, void 0);
25
+ __classPrivateFieldSet(this, _SyncStateProxy_stub, stub, "f");
26
+ __classPrivateFieldSet(this, _SyncStateProxy_keyHandler, keyHandler, "f");
27
+ }
28
+ async getState(key) {
29
+ const transformedKey = __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f") ? await __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f").call(this, key) : key;
30
+ return __classPrivateFieldGet(this, _SyncStateProxy_stub, "f").getState(transformedKey);
31
+ }
32
+ async setState(value, key) {
33
+ const transformedKey = __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f") ? await __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f").call(this, key) : key;
34
+ return __classPrivateFieldGet(this, _SyncStateProxy_stub, "f").setState(value, transformedKey);
35
+ }
36
+ async subscribe(key, client) {
37
+ const transformedKey = __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f") ? await __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f").call(this, key) : key;
38
+ return __classPrivateFieldGet(this, _SyncStateProxy_stub, "f").subscribe(transformedKey, client);
39
+ }
40
+ async unsubscribe(key, client) {
41
+ const transformedKey = __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f") ? await __classPrivateFieldGet(this, _SyncStateProxy_keyHandler, "f").call(this, key) : key;
42
+ return __classPrivateFieldGet(this, _SyncStateProxy_stub, "f").unsubscribe(transformedKey, client);
43
+ }
44
+ }
45
+ _SyncStateProxy_stub = new WeakMap(), _SyncStateProxy_keyHandler = new WeakMap();
46
+ /**
47
+ * Registers routes that forward sync state requests to the configured Durable Object namespace.
48
+ * @param getNamespace Function that returns the Durable Object namespace from the Worker env.
49
+ * @param options Optional overrides for base path, reset path, and object name.
50
+ * @returns Router entries for the sync state API and reset endpoint.
51
+ */
52
+ export const syncStateRoutes = (getNamespace, options = {}) => {
53
+ const basePath = options.basePath ?? DEFAULT_SYNC_STATE_PATH;
54
+ const resetPath = options.resetPath ?? `${basePath}/reset`;
55
+ const durableObjectName = options.durableObjectName ?? DEFAULT_SYNC_STATE_NAME;
56
+ const forwardRequest = async (request) => {
57
+ const keyHandler = SyncStateServer.getKeyHandler();
58
+ if (!keyHandler) {
59
+ const namespace = getNamespace(env);
60
+ const id = namespace.idFromName(durableObjectName);
61
+ return namespace.get(id).fetch(request);
62
+ }
63
+ const namespace = getNamespace(env);
64
+ const id = namespace.idFromName(durableObjectName);
65
+ const coordinator = namespace.get(id);
66
+ const proxy = new SyncStateProxy(coordinator, keyHandler);
67
+ return newWorkersRpcResponse(request, proxy);
68
+ };
69
+ return [
70
+ route(basePath, ({ request }) => forwardRequest(request)),
71
+ route(resetPath, ({ request }) => forwardRequest(request)),
72
+ ];
73
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-beta.27-test.20251112194755",
3
+ "version": "1.0.0-beta.27-test.20251116215153",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -104,6 +104,14 @@
104
104
  "./realtime/durableObject": {
105
105
  "types": "./dist/runtime/lib/realtime/durableObject.d.ts",
106
106
  "default": "./dist/runtime/lib/realtime/durableObject.js"
107
+ },
108
+ "./use-synced-state/client": {
109
+ "types": "./dist/use-synced-state/client.d.ts",
110
+ "default": "./dist/use-synced-state/client.js"
111
+ },
112
+ "./use-synced-state/worker": {
113
+ "types": "./dist/use-synced-state/worker.d.ts",
114
+ "default": "./dist/use-synced-state/worker.js"
107
115
  }
108
116
  },
109
117
  "keywords": [
@@ -144,10 +152,12 @@
144
152
  "@puppeteer/browsers": "~2.10.0",
145
153
  "@types/decompress": "~4.2.7",
146
154
  "@types/fs-extra": "~11.0.4",
155
+ "@types/glob": "^8.1.0",
147
156
  "@types/react": "~19.1.2",
148
157
  "@types/react-dom": "~19.1.2",
149
158
  "@types/react-is": "~19.0.0",
150
159
  "@vitejs/plugin-react": "~5.0.0",
160
+ "capnweb": "~0.2.0",
151
161
  "chokidar": "~4.0.0",
152
162
  "debug": "~4.4.0",
153
163
  "decompress": "~4.2.1",
@@ -173,8 +183,7 @@
173
183
  "ts-morph": "~27.0.0",
174
184
  "unique-names-generator": "~4.7.1",
175
185
  "vibe-rules": "~0.3.0",
176
- "vite-tsconfig-paths": "~5.1.4",
177
- "@types/glob": "^8.1.0"
186
+ "vite-tsconfig-paths": "~5.1.4"
178
187
  },
179
188
  "peerDependencies": {
180
189
  "@cloudflare/vite-plugin": "^1.13.10",