@firtoz/websocket-do 12.0.0 → 13.0.0

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 (55) hide show
  1. package/README.md +5 -1
  2. package/dist/BaseSession.d.ts +41 -0
  3. package/dist/BaseSession.js +5 -0
  4. package/dist/BaseSession.js.map +1 -0
  5. package/dist/BaseWebSocketDO.d.ts +42 -0
  6. package/dist/BaseWebSocketDO.js +4 -0
  7. package/dist/BaseWebSocketDO.js.map +1 -0
  8. package/dist/StandardSchemaSession.d.ts +41 -0
  9. package/dist/StandardSchemaSession.js +8 -0
  10. package/dist/StandardSchemaSession.js.map +1 -0
  11. package/dist/StandardSchemaWebSocketClient.d.ts +45 -0
  12. package/dist/StandardSchemaWebSocketClient.js +5 -0
  13. package/dist/StandardSchemaWebSocketClient.js.map +1 -0
  14. package/dist/StandardSchemaWebSocketDO.d.ts +28 -0
  15. package/dist/StandardSchemaWebSocketDO.js +5 -0
  16. package/dist/StandardSchemaWebSocketDO.js.map +1 -0
  17. package/dist/WebsocketWrapper.d.ts +9 -0
  18. package/dist/WebsocketWrapper.js +4 -0
  19. package/dist/WebsocketWrapper.js.map +1 -0
  20. package/dist/chunk-3C77OSOD.js +54 -0
  21. package/dist/chunk-3C77OSOD.js.map +1 -0
  22. package/dist/chunk-3LWVEY3R.js +130 -0
  23. package/dist/chunk-3LWVEY3R.js.map +1 -0
  24. package/dist/chunk-53MFRNQS.js +153 -0
  25. package/dist/chunk-53MFRNQS.js.map +1 -0
  26. package/dist/chunk-CAX4POIL.js +13 -0
  27. package/dist/chunk-CAX4POIL.js.map +1 -0
  28. package/dist/chunk-KCPOB32E.js +20 -0
  29. package/dist/chunk-KCPOB32E.js.map +1 -0
  30. package/dist/chunk-NOUFNU2O.js +10 -0
  31. package/dist/chunk-NOUFNU2O.js.map +1 -0
  32. package/dist/chunk-QMGIRIHJ.js +18 -0
  33. package/dist/chunk-QMGIRIHJ.js.map +1 -0
  34. package/dist/chunk-ULGH6X42.js +23 -0
  35. package/dist/chunk-ULGH6X42.js.map +1 -0
  36. package/dist/chunk-WJIQBI6I.js +35 -0
  37. package/dist/chunk-WJIQBI6I.js.map +1 -0
  38. package/dist/chunk-XFB6C3NZ.js +134 -0
  39. package/dist/chunk-XFB6C3NZ.js.map +1 -0
  40. package/dist/index.d.ts +14 -0
  41. package/dist/index.js +11 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/parseStandardSchema.d.ts +9 -0
  44. package/dist/parseStandardSchema.js +4 -0
  45. package/dist/parseStandardSchema.js.map +1 -0
  46. package/dist/standardSchemaMsgpack.d.ts +8 -0
  47. package/dist/standardSchemaMsgpack.js +5 -0
  48. package/dist/standardSchemaMsgpack.js.map +1 -0
  49. package/dist/standardSchemaRpc.d.ts +30 -0
  50. package/dist/standardSchemaRpc.js +6 -0
  51. package/dist/standardSchemaRpc.js.map +1 -0
  52. package/dist/standardSchemaRpcReact.d.ts +27 -0
  53. package/dist/standardSchemaRpcReact.js +54 -0
  54. package/dist/standardSchemaRpcReact.js.map +1 -0
  55. package/package.json +19 -17
@@ -0,0 +1,27 @@
1
+ import { DependencyList, RefObject } from 'react';
2
+ import { StandardSchemaWebSocketRpcSessionConstructorOptions, StandardSchemaWebSocketRpcSession } from './standardSchemaRpc.js';
3
+ import './StandardSchemaWebSocketClient.js';
4
+ import '@standard-schema/spec';
5
+
6
+ /** Options for {@link useStandardSchemaWebSocketRpc} (same as constructor options for {@link StandardSchemaWebSocketRpcSession}). */
7
+ type UseStandardSchemaWebSocketRpcOptions<TClientMsg, TServerMsg, TPending extends {
8
+ reject: (error: Error) => void;
9
+ }> = StandardSchemaWebSocketRpcSessionConstructorOptions<TClientMsg, TServerMsg, TPending>;
10
+ /**
11
+ * Connects a {@link StandardSchemaWebSocketRpcSession} in an effect: rejects all pending
12
+ * RPCs and closes the socket on cleanup or when `deps` change.
13
+ *
14
+ * Callback refs keep the latest `onMessage` / `onOpen` / `onClose` without
15
+ * listing them in `deps`, so inline handlers do not reconnect every render.
16
+ *
17
+ * Pass `deps` as the second argument; keep it aligned with values used to
18
+ * build `url` / `webSocket`.
19
+ */
20
+ declare function useStandardSchemaWebSocketRpc<TClientMsg, TServerMsg, TPending extends {
21
+ reject: (error: Error) => void;
22
+ }>(options: UseStandardSchemaWebSocketRpcOptions<TClientMsg, TServerMsg, TPending>, deps: DependencyList): {
23
+ ready: boolean;
24
+ sessionRef: RefObject<StandardSchemaWebSocketRpcSession<TClientMsg, TServerMsg, TPending> | null>;
25
+ };
26
+
27
+ export { type UseStandardSchemaWebSocketRpcOptions, useStandardSchemaWebSocketRpc };
@@ -0,0 +1,54 @@
1
+ import { createStandardSchemaWebSocketRpcSession } from './chunk-WJIQBI6I.js';
2
+ import './chunk-3LWVEY3R.js';
3
+ import './chunk-CAX4POIL.js';
4
+ import './chunk-NOUFNU2O.js';
5
+ import { useRef, useState, useEffect } from 'react';
6
+
7
+ function useStandardSchemaWebSocketRpc(options, deps) {
8
+ const { onMessage, onOpen, onClose, ...clientOptions } = options;
9
+ const onMessageRef = useRef(onMessage);
10
+ onMessageRef.current = onMessage;
11
+ const onOpenRef = useRef(onOpen);
12
+ onOpenRef.current = onOpen;
13
+ const onCloseRef = useRef(onClose);
14
+ onCloseRef.current = onClose;
15
+ const [ready, setReady] = useState(false);
16
+ const sessionRef = useRef(null);
17
+ useEffect(() => {
18
+ let cancelled = false;
19
+ setReady(false);
20
+ const session = createStandardSchemaWebSocketRpcSession({
21
+ ...clientOptions,
22
+ onMessage: (msg, s) => {
23
+ onMessageRef.current(msg, s);
24
+ },
25
+ onOpen: (event) => {
26
+ if (!cancelled) {
27
+ setReady(true);
28
+ }
29
+ onOpenRef.current?.(event);
30
+ },
31
+ onClose: (event) => {
32
+ if (!cancelled) {
33
+ setReady(false);
34
+ }
35
+ onCloseRef.current?.(event);
36
+ }
37
+ });
38
+ sessionRef.current = session;
39
+ if (session.client.socket.readyState === WebSocket.OPEN) {
40
+ setReady(true);
41
+ }
42
+ return () => {
43
+ cancelled = true;
44
+ sessionRef.current = null;
45
+ session.rejectAllPending(new Error("WebSocket closed"));
46
+ session.close();
47
+ };
48
+ }, deps);
49
+ return { ready, sessionRef };
50
+ }
51
+
52
+ export { useStandardSchemaWebSocketRpc };
53
+ //# sourceMappingURL=standardSchemaRpcReact.js.map
54
+ //# sourceMappingURL=standardSchemaRpcReact.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/standardSchemaRpcReact.ts"],"names":[],"mappings":";;;;;;AA6BO,SAAS,6BAAA,CAKf,SAKA,IAAA,EAQC;AACD,EAAA,MAAM,EAAE,SAAA,EAAW,MAAA,EAAQ,OAAA,EAAS,GAAG,eAAc,GAAI,OAAA;AAEzD,EAAA,MAAM,YAAA,GAAe,OAAO,SAAS,CAAA;AACrC,EAAA,YAAA,CAAa,OAAA,GAAU,SAAA;AACvB,EAAA,MAAM,SAAA,GAAY,OAAO,MAAM,CAAA;AAC/B,EAAA,SAAA,CAAU,OAAA,GAAU,MAAA;AACpB,EAAA,MAAM,UAAA,GAAa,OAAO,OAAO,CAAA;AACjC,EAAA,UAAA,CAAW,OAAA,GAAU,OAAA;AAErB,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,SAAS,KAAK,CAAA;AACxC,EAAA,MAAM,UAAA,GAAa,OAIT,IAAI,CAAA;AAEd,EAAA,SAAA,CAAU,MAAM;AACf,IAAA,IAAI,SAAA,GAAY,KAAA;AAChB,IAAA,QAAA,CAAS,KAAK,CAAA;AAEd,IAAA,MAAM,UAAU,uCAAA,CAId;AAAA,MACD,GAAG,aAAA;AAAA,MACH,SAAA,EAAW,CAAC,GAAA,EAAK,CAAA,KAAM;AACtB,QAAA,YAAA,CAAa,OAAA,CAAQ,KAAK,CAAC,CAAA;AAAA,MAC5B,CAAA;AAAA,MACA,MAAA,EAAQ,CAAC,KAAA,KAAU;AAClB,QAAA,IAAI,CAAC,SAAA,EAAW;AACf,UAAA,QAAA,CAAS,IAAI,CAAA;AAAA,QACd;AACA,QAAA,SAAA,CAAU,UAAU,KAAK,CAAA;AAAA,MAC1B,CAAA;AAAA,MACA,OAAA,EAAS,CAAC,KAAA,KAAU;AACnB,QAAA,IAAI,CAAC,SAAA,EAAW;AACf,UAAA,QAAA,CAAS,KAAK,CAAA;AAAA,QACf;AACA,QAAA,UAAA,CAAW,UAAU,KAAK,CAAA;AAAA,MAC3B;AAAA,KACA,CAAA;AAED,IAAA,UAAA,CAAW,OAAA,GAAU,OAAA;AACrB,IAAA,IAAI,OAAA,CAAQ,MAAA,CAAO,MAAA,CAAO,UAAA,KAAe,UAAU,IAAA,EAAM;AACxD,MAAA,QAAA,CAAS,IAAI,CAAA;AAAA,IACd;AAEA,IAAA,OAAO,MAAM;AACZ,MAAA,SAAA,GAAY,IAAA;AACZ,MAAA,UAAA,CAAW,OAAA,GAAU,IAAA;AACrB,MAAA,OAAA,CAAQ,gBAAA,CAAiB,IAAI,KAAA,CAAM,kBAAkB,CAAC,CAAA;AACtD,MAAA,OAAA,CAAQ,KAAA,EAAM;AAAA,IACf,CAAA;AAAA,EAED,GAAG,IAAI,CAAA;AAEP,EAAA,OAAO,EAAE,OAAO,UAAA,EAAW;AAC5B","file":"standardSchemaRpcReact.js","sourcesContent":["import type { DependencyList, RefObject } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n\ttype StandardSchemaWebSocketRpcSession,\n\ttype StandardSchemaWebSocketRpcSessionConstructorOptions,\n\tcreateStandardSchemaWebSocketRpcSession,\n} from \"./standardSchemaRpc\";\n\n/** Options for {@link useStandardSchemaWebSocketRpc} (same as constructor options for {@link StandardSchemaWebSocketRpcSession}). */\nexport type UseStandardSchemaWebSocketRpcOptions<\n\tTClientMsg,\n\tTServerMsg,\n\tTPending extends { reject: (error: Error) => void },\n> = StandardSchemaWebSocketRpcSessionConstructorOptions<\n\tTClientMsg,\n\tTServerMsg,\n\tTPending\n>;\n\n/**\n * Connects a {@link StandardSchemaWebSocketRpcSession} in an effect: rejects all pending\n * RPCs and closes the socket on cleanup or when `deps` change.\n *\n * Callback refs keep the latest `onMessage` / `onOpen` / `onClose` without\n * listing them in `deps`, so inline handlers do not reconnect every render.\n *\n * Pass `deps` as the second argument; keep it aligned with values used to\n * build `url` / `webSocket`.\n */\nexport function useStandardSchemaWebSocketRpc<\n\tTClientMsg,\n\tTServerMsg,\n\tTPending extends { reject: (error: Error) => void },\n>(\n\toptions: UseStandardSchemaWebSocketRpcOptions<\n\t\tTClientMsg,\n\t\tTServerMsg,\n\t\tTPending\n\t>,\n\tdeps: DependencyList,\n): {\n\tready: boolean;\n\tsessionRef: RefObject<StandardSchemaWebSocketRpcSession<\n\t\tTClientMsg,\n\t\tTServerMsg,\n\t\tTPending\n\t> | null>;\n} {\n\tconst { onMessage, onOpen, onClose, ...clientOptions } = options;\n\n\tconst onMessageRef = useRef(onMessage);\n\tonMessageRef.current = onMessage;\n\tconst onOpenRef = useRef(onOpen);\n\tonOpenRef.current = onOpen;\n\tconst onCloseRef = useRef(onClose);\n\tonCloseRef.current = onClose;\n\n\tconst [ready, setReady] = useState(false);\n\tconst sessionRef = useRef<StandardSchemaWebSocketRpcSession<\n\t\tTClientMsg,\n\t\tTServerMsg,\n\t\tTPending\n\t> | null>(null);\n\n\tuseEffect(() => {\n\t\tlet cancelled = false;\n\t\tsetReady(false);\n\n\t\tconst session = createStandardSchemaWebSocketRpcSession<\n\t\t\tTClientMsg,\n\t\t\tTServerMsg,\n\t\t\tTPending\n\t\t>({\n\t\t\t...clientOptions,\n\t\t\tonMessage: (msg, s) => {\n\t\t\t\tonMessageRef.current(msg, s);\n\t\t\t},\n\t\t\tonOpen: (event) => {\n\t\t\t\tif (!cancelled) {\n\t\t\t\t\tsetReady(true);\n\t\t\t\t}\n\t\t\t\tonOpenRef.current?.(event);\n\t\t\t},\n\t\t\tonClose: (event) => {\n\t\t\t\tif (!cancelled) {\n\t\t\t\t\tsetReady(false);\n\t\t\t\t}\n\t\t\t\tonCloseRef.current?.(event);\n\t\t\t},\n\t\t});\n\n\t\tsessionRef.current = session;\n\t\tif (session.client.socket.readyState === WebSocket.OPEN) {\n\t\t\tsetReady(true);\n\t\t}\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t\tsessionRef.current = null;\n\t\t\tsession.rejectAllPending(new Error(\"WebSocket closed\"));\n\t\t\tsession.close();\n\t\t};\n\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: deps is the explicit contract\n\t}, deps);\n\n\treturn { ready, sessionRef };\n}\n"]}
package/package.json CHANGED
@@ -1,38 +1,40 @@
1
1
  {
2
2
  "name": "@firtoz/websocket-do",
3
- "version": "12.0.0",
3
+ "version": "13.0.0",
4
4
  "description": "Type-safe WebSocket session management for Cloudflare Durable Objects with Hono integration",
5
- "main": "./src/index.ts",
6
- "module": "./src/index.ts",
7
- "types": "./src/index.ts",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
8
9
  "exports": {
9
10
  ".": {
10
- "types": "./src/index.ts",
11
- "import": "./src/index.ts",
12
- "require": "./src/index.ts"
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
13
  },
14
14
  "./schema-client": {
15
- "types": "./src/StandardSchemaWebSocketClient.ts",
16
- "import": "./src/StandardSchemaWebSocketClient.ts",
17
- "require": "./src/StandardSchemaWebSocketClient.ts"
15
+ "types": "./dist/StandardSchemaWebSocketClient.d.ts",
16
+ "import": "./dist/StandardSchemaWebSocketClient.js"
18
17
  },
19
18
  "./rpc": {
20
- "types": "./src/standardSchemaRpc.ts",
21
- "import": "./src/standardSchemaRpc.ts",
22
- "require": "./src/standardSchemaRpc.ts"
19
+ "types": "./dist/standardSchemaRpc.d.ts",
20
+ "import": "./dist/standardSchemaRpc.js"
23
21
  },
24
22
  "./rpc-react": {
25
- "types": "./src/standardSchemaRpcReact.ts",
26
- "import": "./src/standardSchemaRpcReact.ts",
27
- "require": "./src/standardSchemaRpcReact.ts"
23
+ "types": "./dist/standardSchemaRpcReact.d.ts",
24
+ "import": "./dist/standardSchemaRpcReact.js"
28
25
  }
29
26
  },
30
27
  "files": [
28
+ "dist/**/*.js",
29
+ "dist/**/*.js.map",
30
+ "dist/**/*.d.ts",
31
31
  "src/**/*.ts",
32
32
  "!src/**/*.test.ts",
33
33
  "README.md"
34
34
  ],
35
35
  "scripts": {
36
+ "build": "tsup",
37
+ "prepack": "bun run build",
36
38
  "typecheck": "tsgo --noEmit -p ./tsconfig.json",
37
39
  "lint": "biome check --write src",
38
40
  "lint:ci": "biome ci src",
@@ -61,7 +63,7 @@
61
63
  },
62
64
  "peerDependencies": {
63
65
  "@cloudflare/workers-types": "^4.20260329.1",
64
- "@firtoz/hono-fetcher": "^2.6.0",
66
+ "@firtoz/hono-fetcher": "^2.7.0",
65
67
  "hono": "^4.12.9",
66
68
  "react": ">=18.0.0"
67
69
  },