rwsdk 1.1.0 → 1.2.1

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.
@@ -9,91 +9,107 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _SyncedStateProxy_instances, _SyncedStateProxy_stub, _SyncedStateProxy_keyHandler, _SyncedStateProxy_requestInfo, _SyncedStateProxy_transformKey, _SyncedStateProxy_callHandler;
13
- import { RpcTarget, newWorkersRpcResponse } from "capnweb";
14
12
  import { env } from "cloudflare:workers";
15
13
  import { route } from "../runtime/entries/router";
16
14
  import { runWithRequestInfo } from "../runtime/requestInfo/worker";
15
+ import { loadCapnweb } from "./capnweb-loader.mjs";
17
16
  import { SyncedStateServer, } from "./SyncedStateServer.mjs";
18
17
  import { DEFAULT_SYNCED_STATE_PATH } from "./constants.mjs";
19
18
  export { SyncedStateServer };
20
19
  const DEFAULT_SYNC_STATE_NAME = "syncedState";
21
- class SyncedStateProxy extends RpcTarget {
22
- constructor(stub, keyHandler, requestInfo) {
23
- super();
24
- _SyncedStateProxy_instances.add(this);
25
- _SyncedStateProxy_stub.set(this, void 0);
26
- _SyncedStateProxy_keyHandler.set(this, void 0);
27
- _SyncedStateProxy_requestInfo.set(this, void 0);
28
- __classPrivateFieldSet(this, _SyncedStateProxy_stub, stub, "f");
29
- __classPrivateFieldSet(this, _SyncedStateProxy_keyHandler, keyHandler, "f");
30
- __classPrivateFieldSet(this, _SyncedStateProxy_requestInfo, requestInfo, "f");
31
- // Set stub in DO instance so handlers can access it
32
- if (stub && typeof stub._setStub === "function") {
33
- void stub._setStub(stub);
34
- }
35
- }
36
- async getState(key) {
37
- const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
38
- return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").getState(transformedKey);
39
- }
40
- async setState(value, key) {
41
- const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
42
- return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").setState(value, transformedKey);
43
- }
44
- async subscribe(key, client) {
45
- const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
46
- const subscribeHandler = SyncedStateServer.getSubscribeHandler();
47
- if (subscribeHandler) {
48
- __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_callHandler).call(this, subscribeHandler, transformedKey, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
49
- }
50
- // dup the client if it is a function; otherwise, pass it as is;
51
- // this is because the client is a WebSocketRpcSession, and we need to pass a new instance of the client to the DO;
52
- const clientToPass = typeof client.dup === "function" ? client.dup() : client;
53
- return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").subscribe(transformedKey, clientToPass);
54
- }
55
- async unsubscribe(key, client) {
56
- const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
57
- // Call unsubscribe handler before unsubscribe, similar to subscribe handler
58
- // This ensures the handler is called even if the unsubscribe doesn't find a match
59
- // or if the RPC call fails
60
- const unsubscribeHandler = SyncedStateServer.getUnsubscribeHandler();
61
- if (unsubscribeHandler) {
62
- __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_callHandler).call(this, unsubscribeHandler, transformedKey, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
63
- }
64
- try {
65
- await __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").unsubscribe(transformedKey, client);
66
- }
67
- catch (error) {
68
- // Ignore errors during unsubscribe - handler has already been called
69
- // This prevents RPC stub disposal errors from propagating
70
- }
20
+ let SyncedStateProxyClass = null;
21
+ async function getSyncedStateProxy() {
22
+ var _SyncedStateProxy_instances, _SyncedStateProxy_stub, _SyncedStateProxy_keyHandler, _SyncedStateProxy_requestInfo, _SyncedStateProxy_transformKey, _SyncedStateProxy_callHandler, _a;
23
+ const { RpcTarget, newWorkersRpcResponse } = await loadCapnweb();
24
+ if (!SyncedStateProxyClass) {
25
+ SyncedStateProxyClass = (_a = class SyncedStateProxy extends RpcTarget {
26
+ constructor(stub, keyHandler, requestInfo) {
27
+ super();
28
+ _SyncedStateProxy_instances.add(this);
29
+ _SyncedStateProxy_stub.set(this, void 0);
30
+ _SyncedStateProxy_keyHandler.set(this, void 0);
31
+ _SyncedStateProxy_requestInfo.set(this, void 0);
32
+ __classPrivateFieldSet(this, _SyncedStateProxy_stub, stub, "f");
33
+ __classPrivateFieldSet(this, _SyncedStateProxy_keyHandler, keyHandler, "f");
34
+ __classPrivateFieldSet(this, _SyncedStateProxy_requestInfo, requestInfo, "f");
35
+ // Set stub in DO instance so handlers can access it
36
+ if (stub && typeof stub._setStub === "function") {
37
+ void stub._setStub(stub);
38
+ }
39
+ }
40
+ async getState(key) {
41
+ const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
42
+ return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").getState(transformedKey);
43
+ }
44
+ async setState(value, key) {
45
+ const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
46
+ return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").setState(value, transformedKey);
47
+ }
48
+ async subscribe(key, client) {
49
+ const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
50
+ const subscribeHandler = SyncedStateServer.getSubscribeHandler();
51
+ if (subscribeHandler) {
52
+ __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_callHandler).call(this, subscribeHandler, transformedKey, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
53
+ }
54
+ // dup the client if it is a function; otherwise, pass it as is;
55
+ // this is because the client is a WebSocketRpcSession, and we need to pass a new instance of the client to the DO;
56
+ const clientToPass = typeof client.dup === "function" ? client.dup() : client;
57
+ return __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").subscribe(transformedKey, clientToPass);
58
+ }
59
+ async unsubscribe(key, client) {
60
+ const transformedKey = await __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_transformKey).call(this, key);
61
+ // Call unsubscribe handler before unsubscribe, similar to subscribe handler
62
+ // This ensures the handler is called even if the unsubscribe doesn't find a match
63
+ // or if the RPC call fails
64
+ const unsubscribeHandler = SyncedStateServer.getUnsubscribeHandler();
65
+ if (unsubscribeHandler) {
66
+ __classPrivateFieldGet(this, _SyncedStateProxy_instances, "m", _SyncedStateProxy_callHandler).call(this, unsubscribeHandler, transformedKey, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
67
+ }
68
+ try {
69
+ await __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f").unsubscribe(transformedKey, client);
70
+ }
71
+ catch (error) {
72
+ // Ignore errors during unsubscribe - handler has already been called
73
+ // This prevents RPC stub disposal errors from propagating
74
+ }
75
+ }
76
+ },
77
+ _SyncedStateProxy_stub = new WeakMap(),
78
+ _SyncedStateProxy_keyHandler = new WeakMap(),
79
+ _SyncedStateProxy_requestInfo = new WeakMap(),
80
+ _SyncedStateProxy_instances = new WeakSet(),
81
+ _SyncedStateProxy_transformKey =
82
+ /**
83
+ * Transforms a key using the keyHandler, preserving async context so requestInfo.ctx is available.
84
+ */
85
+ async function _SyncedStateProxy_transformKey(key) {
86
+ if (!__classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f")) {
87
+ return key;
88
+ }
89
+ if (__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f")) {
90
+ // Preserve async context when calling keyHandler so requestInfo.ctx is available
91
+ return await runWithRequestInfo(__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f"), async () => await __classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f")(key, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f")));
92
+ }
93
+ return await __classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f").call(this, key, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
94
+ },
95
+ _SyncedStateProxy_callHandler = function _SyncedStateProxy_callHandler(handler, key, stub) {
96
+ if (__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f")) {
97
+ // Preserve async context when calling handler so requestInfo.ctx is available
98
+ runWithRequestInfo(__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f"), () => {
99
+ handler(key, stub);
100
+ });
101
+ }
102
+ else {
103
+ handler(key, stub);
104
+ }
105
+ },
106
+ _a);
71
107
  }
108
+ return {
109
+ SyncedStateProxy: SyncedStateProxyClass,
110
+ newWorkersRpcResponse,
111
+ };
72
112
  }
73
- _SyncedStateProxy_stub = new WeakMap(), _SyncedStateProxy_keyHandler = new WeakMap(), _SyncedStateProxy_requestInfo = new WeakMap(), _SyncedStateProxy_instances = new WeakSet(), _SyncedStateProxy_transformKey =
74
- /**
75
- * Transforms a key using the keyHandler, preserving async context so requestInfo.ctx is available.
76
- */
77
- async function _SyncedStateProxy_transformKey(key) {
78
- if (!__classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f")) {
79
- return key;
80
- }
81
- if (__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f")) {
82
- // Preserve async context when calling keyHandler so requestInfo.ctx is available
83
- return await runWithRequestInfo(__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f"), async () => await __classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f")(key, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f")));
84
- }
85
- return await __classPrivateFieldGet(this, _SyncedStateProxy_keyHandler, "f").call(this, key, __classPrivateFieldGet(this, _SyncedStateProxy_stub, "f"));
86
- }, _SyncedStateProxy_callHandler = function _SyncedStateProxy_callHandler(handler, key, stub) {
87
- if (__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f")) {
88
- // Preserve async context when calling handler so requestInfo.ctx is available
89
- runWithRequestInfo(__classPrivateFieldGet(this, _SyncedStateProxy_requestInfo, "f"), () => {
90
- handler(key, stub);
91
- });
92
- }
93
- else {
94
- handler(key, stub);
95
- }
96
- };
97
113
  /**
98
114
  * Registers routes that forward sync state requests to the configured Durable Object namespace.
99
115
  * @param getNamespace Function that returns the Durable Object namespace from the Worker env.
@@ -125,6 +141,7 @@ export const syncedStateRoutes = (getNamespace, options = {}) => {
125
141
  }
126
142
  const id = namespace.idFromName(resolvedRoomName);
127
143
  const coordinator = namespace.get(id);
144
+ const { SyncedStateProxy, newWorkersRpcResponse } = await getSyncedStateProxy();
128
145
  const proxy = new SyncedStateProxy(coordinator, keyHandler, requestInfo);
129
146
  return newWorkersRpcResponse(request, proxy);
130
147
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -201,6 +201,11 @@
201
201
  "vite": "^6.2.6 || 7.x",
202
202
  "wrangler": "^4.77.0"
203
203
  },
204
+ "peerDependenciesMeta": {
205
+ "capnweb": {
206
+ "optional": true
207
+ }
208
+ },
204
209
  "packageManager": "pnpm@10.31.0",
205
210
  "devDependencies": {
206
211
  "@cloudflare/vite-plugin": "1.31.0",