@igoforth/ws-rpc 1.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 (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +446 -0
  3. package/dist/adapters/client.d.ts +117 -0
  4. package/dist/adapters/client.js +241 -0
  5. package/dist/adapters/cloudflare-do.d.ts +72 -0
  6. package/dist/adapters/cloudflare-do.js +192 -0
  7. package/dist/adapters/index.d.ts +13 -0
  8. package/dist/adapters/index.js +16 -0
  9. package/dist/adapters/server.d.ts +10 -0
  10. package/dist/adapters/server.js +122 -0
  11. package/dist/adapters/types.d.ts +125 -0
  12. package/dist/adapters/types.js +3 -0
  13. package/dist/codecs/cbor.d.ts +16 -0
  14. package/dist/codecs/cbor.js +36 -0
  15. package/dist/codecs/factory.d.ts +3 -0
  16. package/dist/codecs/factory.js +3 -0
  17. package/dist/codecs/index.d.ts +5 -0
  18. package/dist/codecs/index.js +5 -0
  19. package/dist/codecs/json.d.ts +4 -0
  20. package/dist/codecs/json.js +4 -0
  21. package/dist/codecs/msgpack.d.ts +16 -0
  22. package/dist/codecs/msgpack.js +34 -0
  23. package/dist/codecs-BmYG2d_U.js +0 -0
  24. package/dist/default-BkrMd28n.js +253 -0
  25. package/dist/default-xDNNMrg0.d.ts +129 -0
  26. package/dist/durable-MZjkvyS6.js +165 -0
  27. package/dist/errors-5BfreE63.js +96 -0
  28. package/dist/errors.d.ts +69 -0
  29. package/dist/errors.js +7 -0
  30. package/dist/factory-3ziwTuZe.js +132 -0
  31. package/dist/factory-C1v0AEHY.d.ts +101 -0
  32. package/dist/index-Be7jjS77.d.ts +1 -0
  33. package/dist/index.d.ts +14 -0
  34. package/dist/index.js +14 -0
  35. package/dist/interface-C4S-WCqW.d.ts +120 -0
  36. package/dist/json-54Z2bIIs.d.ts +22 -0
  37. package/dist/json-Bshec-bZ.js +41 -0
  38. package/dist/memory-Bqb3KEVr.js +48 -0
  39. package/dist/memory-D1nGjzzH.d.ts +41 -0
  40. package/dist/multi-peer-BAi9yVzp.js +242 -0
  41. package/dist/peers/default.d.ts +8 -0
  42. package/dist/peers/default.js +8 -0
  43. package/dist/peers/durable.d.ts +136 -0
  44. package/dist/peers/durable.js +9 -0
  45. package/dist/peers/index.d.ts +10 -0
  46. package/dist/peers/index.js +9 -0
  47. package/dist/protocol-DA84zrc2.d.ts +211 -0
  48. package/dist/protocol-_mpoOPp6.js +192 -0
  49. package/dist/protocol.d.ts +6 -0
  50. package/dist/protocol.js +6 -0
  51. package/dist/reconnect-CGAA_1Gf.js +26 -0
  52. package/dist/reconnect-DbcN0R_1.d.ts +35 -0
  53. package/dist/schema-CN5HHHku.d.ts +108 -0
  54. package/dist/schema.d.ts +2 -0
  55. package/dist/schema.js +43 -0
  56. package/dist/server-zTjpJpoX.d.ts +209 -0
  57. package/dist/sql-CCjc6Bid.js +142 -0
  58. package/dist/sql-DPmHOeZy.d.ts +131 -0
  59. package/dist/storage/index.d.ts +8 -0
  60. package/dist/storage/index.js +7 -0
  61. package/dist/storage/interface.d.ts +3 -0
  62. package/dist/storage/interface.js +0 -0
  63. package/dist/storage/memory.d.ts +7 -0
  64. package/dist/storage/memory.js +6 -0
  65. package/dist/storage/sql.d.ts +7 -0
  66. package/dist/storage/sql.js +6 -0
  67. package/dist/types-Be-qmQu0.d.ts +111 -0
  68. package/dist/types-D_psiH09.js +13 -0
  69. package/dist/types.d.ts +7 -0
  70. package/dist/types.js +3 -0
  71. package/dist/utils/index.d.ts +2 -0
  72. package/dist/utils/index.js +3 -0
  73. package/dist/utils/reconnect.d.ts +2 -0
  74. package/dist/utils/reconnect.js +3 -0
  75. package/package.json +156 -0
  76. package/src/adapters/client.ts +396 -0
  77. package/src/adapters/cloudflare-do.ts +346 -0
  78. package/src/adapters/index.ts +16 -0
  79. package/src/adapters/multi-peer.ts +404 -0
  80. package/src/adapters/server.ts +192 -0
  81. package/src/adapters/types.ts +202 -0
  82. package/src/codecs/cbor.ts +42 -0
  83. package/src/codecs/factory.ts +210 -0
  84. package/src/codecs/index.ts +30 -0
  85. package/src/codecs/json.ts +42 -0
  86. package/src/codecs/msgpack.ts +36 -0
  87. package/src/errors.ts +105 -0
  88. package/src/index.ts +102 -0
  89. package/src/peers/default.ts +433 -0
  90. package/src/peers/durable.ts +280 -0
  91. package/src/peers/index.ts +13 -0
  92. package/src/protocol.ts +306 -0
  93. package/src/schema.ts +167 -0
  94. package/src/storage/index.ts +20 -0
  95. package/src/storage/interface.ts +146 -0
  96. package/src/storage/memory.ts +84 -0
  97. package/src/storage/sql.ts +266 -0
  98. package/src/types.ts +158 -0
  99. package/src/utils/index.ts +9 -0
  100. package/src/utils/reconnect.ts +51 -0
@@ -0,0 +1,9 @@
1
+ import "../factory-3ziwTuZe.js";
2
+ import "../json-Bshec-bZ.js";
3
+ import "../codecs-BmYG2d_U.js";
4
+ import "../protocol-_mpoOPp6.js";
5
+ import "../errors-5BfreE63.js";
6
+ import { t as RpcPeer } from "../default-BkrMd28n.js";
7
+ import { t as DurableRpcPeer } from "../durable-MZjkvyS6.js";
8
+
9
+ export { DurableRpcPeer, RpcPeer };
@@ -0,0 +1,211 @@
1
+ import { a as WireCodec } from "./factory-C1v0AEHY.js";
2
+ import * as z from "zod";
3
+
4
+ //#region src/protocol.d.ts
5
+
6
+ /**
7
+ * RPC Request - sent when calling a remote method
8
+ */
9
+ declare const RpcRequestSchema: z.ZodObject<{
10
+ type: z.ZodLiteral<"rpc:request">;
11
+ id: z.ZodString;
12
+ method: z.ZodString;
13
+ params: z.ZodUnknown;
14
+ }, z.core.$strip>;
15
+ type RpcRequest = z.infer<typeof RpcRequestSchema>;
16
+ /**
17
+ * RPC Response - sent as success response to a request
18
+ */
19
+ declare const RpcResponseSchema: z.ZodObject<{
20
+ type: z.ZodLiteral<"rpc:response">;
21
+ id: z.ZodString;
22
+ result: z.ZodUnknown;
23
+ }, z.core.$strip>;
24
+ type RpcResponse = z.infer<typeof RpcResponseSchema>;
25
+ /**
26
+ * RPC Error - sent when a request fails
27
+ */
28
+ declare const RpcErrorSchema: z.ZodObject<{
29
+ type: z.ZodLiteral<"rpc:error">;
30
+ id: z.ZodString;
31
+ code: z.ZodNumber;
32
+ message: z.ZodString;
33
+ data: z.ZodOptional<z.ZodUnknown>;
34
+ }, z.core.$strip>;
35
+ type RpcError = z.infer<typeof RpcErrorSchema>;
36
+ /**
37
+ * RPC Event - fire-and-forget event (no response expected)
38
+ */
39
+ declare const RpcEventSchema: z.ZodObject<{
40
+ type: z.ZodLiteral<"rpc:event">;
41
+ event: z.ZodString;
42
+ data: z.ZodUnknown;
43
+ }, z.core.$strip>;
44
+ type RpcEvent = z.infer<typeof RpcEventSchema>;
45
+ /**
46
+ * Union of all RPC message types
47
+ */
48
+ declare const RpcMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
49
+ type: z.ZodLiteral<"rpc:request">;
50
+ id: z.ZodString;
51
+ method: z.ZodString;
52
+ params: z.ZodUnknown;
53
+ }, z.core.$strip>, z.ZodObject<{
54
+ type: z.ZodLiteral<"rpc:response">;
55
+ id: z.ZodString;
56
+ result: z.ZodUnknown;
57
+ }, z.core.$strip>, z.ZodObject<{
58
+ type: z.ZodLiteral<"rpc:error">;
59
+ id: z.ZodString;
60
+ code: z.ZodNumber;
61
+ message: z.ZodString;
62
+ data: z.ZodOptional<z.ZodUnknown>;
63
+ }, z.core.$strip>, z.ZodObject<{
64
+ type: z.ZodLiteral<"rpc:event">;
65
+ event: z.ZodString;
66
+ data: z.ZodUnknown;
67
+ }, z.core.$strip>]>;
68
+ type RpcMessage = z.infer<typeof RpcMessageSchema>;
69
+ /**
70
+ * Standard RPC error codes (JSON-RPC 2.0 compatible)
71
+ */
72
+ declare const RpcErrorCodes: {
73
+ readonly PARSE_ERROR: -32700;
74
+ readonly INVALID_REQUEST: -32600;
75
+ readonly METHOD_NOT_FOUND: -32601;
76
+ readonly INVALID_PARAMS: -32602;
77
+ readonly INTERNAL_ERROR: -32603;
78
+ readonly TIMEOUT: -32000;
79
+ readonly CONNECTION_CLOSED: -32001;
80
+ readonly VALIDATION_ERROR: -32002;
81
+ };
82
+ /**
83
+ * Default JSON codec for RPC messages
84
+ *
85
+ * Encodes RPC messages to JSON strings with validation on decode.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * // Encode a message
90
+ * const json = RpcMessageCodec.encode(createRequest("1", "ping", {}));
91
+ *
92
+ * // Decode and validate
93
+ * const message = RpcMessageCodec.decode(json);
94
+ * ```
95
+ */
96
+ declare const RpcMessageCodec: z.ZodCodec<z.ZodString, z.ZodUnion<readonly [z.ZodObject<{
97
+ type: z.ZodLiteral<"rpc:request">;
98
+ id: z.ZodString;
99
+ method: z.ZodString;
100
+ params: z.ZodUnknown;
101
+ }, z.core.$strip>, z.ZodObject<{
102
+ type: z.ZodLiteral<"rpc:response">;
103
+ id: z.ZodString;
104
+ result: z.ZodUnknown;
105
+ }, z.core.$strip>, z.ZodObject<{
106
+ type: z.ZodLiteral<"rpc:error">;
107
+ id: z.ZodString;
108
+ code: z.ZodNumber;
109
+ message: z.ZodString;
110
+ data: z.ZodOptional<z.ZodUnknown>;
111
+ }, z.core.$strip>, z.ZodObject<{
112
+ type: z.ZodLiteral<"rpc:event">;
113
+ event: z.ZodString;
114
+ data: z.ZodUnknown;
115
+ }, z.core.$strip>]>>;
116
+ /**
117
+ * Type alias for RPC wire codecs
118
+ *
119
+ * Wire codecs can encode to string (text frames) or Uint8Array (binary frames).
120
+ */
121
+ type RpcWireCodec = WireCodec<typeof RpcMessageSchema>;
122
+ /**
123
+ * Wire data type - inferred from codec
124
+ */
125
+ type WireDataOf<T extends RpcWireCodec> = T extends z.ZodCodec<infer A> ? A extends z.ZodType<infer V> ? V : never : never;
126
+ /**
127
+ * Wire input types accepted by decode methods
128
+ *
129
+ * Includes Node.js ws library's RawData type (Buffer | ArrayBuffer | Buffer[])
130
+ * for seamless integration with the ws package.
131
+ */
132
+ type WireInput = string | ArrayBuffer | Uint8Array | Uint8Array[];
133
+ /**
134
+ * Protocol interface returned by createProtocol
135
+ */
136
+ interface RpcProtocol<TWire extends RpcWireCodec = RpcWireCodec> {
137
+ /** The underlying codec */
138
+ readonly codec: TWire;
139
+ /** Create and encode an RPC request */
140
+ createRequest(id: string, method: string, params: unknown): WireDataOf<TWire>;
141
+ /** Create and encode an RPC response */
142
+ createResponse(id: string, result: unknown): WireDataOf<TWire>;
143
+ /** Create and encode an RPC error */
144
+ createError(id: string, code: number, message: string, data?: unknown): WireDataOf<TWire>;
145
+ /** Create and encode an RPC event */
146
+ createEvent(event: string, data: unknown): WireDataOf<TWire>;
147
+ /**
148
+ * Decode wire data to an RPC message (throws on invalid)
149
+ *
150
+ * Accepts string, ArrayBuffer, Uint8Array (including Node.js Buffer),
151
+ * or Uint8Array[] (for ws library's fragmented messages).
152
+ */
153
+ decodeMessage(data: WireInput): RpcMessage;
154
+ /**
155
+ * Safely decode wire data (returns null on invalid)
156
+ *
157
+ * Accepts string, ArrayBuffer, Uint8Array (including Node.js Buffer),
158
+ * or Uint8Array[] (for ws library's fragmented messages).
159
+ */
160
+ safeDecodeMessage(data: WireInput): RpcMessage | null;
161
+ }
162
+ /**
163
+ * Create a protocol instance with bound encode/decode functions
164
+ *
165
+ * @param codec - Wire codec for serialization (defaults to JSON)
166
+ * @returns Protocol object with pre-bound encode/decode methods
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * // JSON protocol (default)
171
+ * const protocol = createProtocol();
172
+ *
173
+ * // MessagePack protocol
174
+ * import { createMsgpackCodec } from "@igoforth/ws-rpc/codecs/msgpack";
175
+ * const protocol = createProtocol(createMsgpackCodec(RpcMessageSchema));
176
+ *
177
+ * // Use in peer
178
+ * const wire = protocol.createRequest("1", "ping", {});
179
+ * ws.send(wire); // string or Uint8Array depending on codec
180
+ *
181
+ * const message = protocol.decodeMessage(event.data);
182
+ * ```
183
+ */
184
+ declare function createProtocol<TWire extends RpcWireCodec = typeof RpcMessageCodec>(codec?: TWire): RpcProtocol<TWire>;
185
+ /**
186
+ * Default JSON protocol instance
187
+ *
188
+ * Pre-configured with JSON codec for convenience.
189
+ */
190
+ declare const JsonProtocol: RpcProtocol<z.ZodCodec<z.ZodString, z.ZodUnion<readonly [z.ZodObject<{
191
+ type: z.ZodLiteral<"rpc:request">;
192
+ id: z.ZodString;
193
+ method: z.ZodString;
194
+ params: z.ZodUnknown;
195
+ }, z.core.$strip>, z.ZodObject<{
196
+ type: z.ZodLiteral<"rpc:response">;
197
+ id: z.ZodString;
198
+ result: z.ZodUnknown;
199
+ }, z.core.$strip>, z.ZodObject<{
200
+ type: z.ZodLiteral<"rpc:error">;
201
+ id: z.ZodString;
202
+ code: z.ZodNumber;
203
+ message: z.ZodString;
204
+ data: z.ZodOptional<z.ZodUnknown>;
205
+ }, z.core.$strip>, z.ZodObject<{
206
+ type: z.ZodLiteral<"rpc:event">;
207
+ event: z.ZodString;
208
+ data: z.ZodUnknown;
209
+ }, z.core.$strip>]>>>;
210
+ //#endregion
211
+ export { createProtocol as _, RpcEvent as a, RpcMessageCodec as c, RpcRequest as d, RpcRequestSchema as f, WireInput as g, RpcWireCodec as h, RpcErrorSchema as i, RpcMessageSchema as l, RpcResponseSchema as m, RpcError as n, RpcEventSchema as o, RpcResponse as p, RpcErrorCodes as r, RpcMessage as s, JsonProtocol as t, RpcProtocol as u };
@@ -0,0 +1,192 @@
1
+ import { i as isStringCodec } from "./factory-3ziwTuZe.js";
2
+ import { n as createJsonCodec } from "./json-Bshec-bZ.js";
3
+ import * as z from "zod";
4
+
5
+ //#region src/protocol.ts
6
+ /**
7
+ * Wire Protocol Definitions
8
+ *
9
+ * Defines the message format for bidirectional RPC over WebSocket.
10
+ * Messages can be JSON-encoded (string) or binary-encoded (Uint8Array).
11
+ */
12
+ /**
13
+ * RPC Request - sent when calling a remote method
14
+ */
15
+ const RpcRequestSchema = z.object({
16
+ type: z.literal("rpc:request"),
17
+ id: z.string(),
18
+ method: z.string(),
19
+ params: z.unknown()
20
+ });
21
+ /**
22
+ * RPC Response - sent as success response to a request
23
+ */
24
+ const RpcResponseSchema = z.object({
25
+ type: z.literal("rpc:response"),
26
+ id: z.string(),
27
+ result: z.unknown()
28
+ });
29
+ /**
30
+ * RPC Error - sent when a request fails
31
+ */
32
+ const RpcErrorSchema = z.object({
33
+ type: z.literal("rpc:error"),
34
+ id: z.string(),
35
+ code: z.number(),
36
+ message: z.string(),
37
+ data: z.unknown().optional()
38
+ });
39
+ /**
40
+ * RPC Event - fire-and-forget event (no response expected)
41
+ */
42
+ const RpcEventSchema = z.object({
43
+ type: z.literal("rpc:event"),
44
+ event: z.string(),
45
+ data: z.unknown()
46
+ });
47
+ /**
48
+ * Union of all RPC message types
49
+ */
50
+ const RpcMessageSchema = z.union([
51
+ RpcRequestSchema,
52
+ RpcResponseSchema,
53
+ RpcErrorSchema,
54
+ RpcEventSchema
55
+ ]);
56
+ /**
57
+ * Standard RPC error codes (JSON-RPC 2.0 compatible)
58
+ */
59
+ const RpcErrorCodes = {
60
+ PARSE_ERROR: -32700,
61
+ INVALID_REQUEST: -32600,
62
+ METHOD_NOT_FOUND: -32601,
63
+ INVALID_PARAMS: -32602,
64
+ INTERNAL_ERROR: -32603,
65
+ TIMEOUT: -32e3,
66
+ CONNECTION_CLOSED: -32001,
67
+ VALIDATION_ERROR: -32002
68
+ };
69
+ /**
70
+ * Default JSON codec for RPC messages
71
+ *
72
+ * Encodes RPC messages to JSON strings with validation on decode.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * // Encode a message
77
+ * const json = RpcMessageCodec.encode(createRequest("1", "ping", {}));
78
+ *
79
+ * // Decode and validate
80
+ * const message = RpcMessageCodec.decode(json);
81
+ * ```
82
+ */
83
+ const RpcMessageCodec = createJsonCodec(RpcMessageSchema);
84
+ /**
85
+ * Create a protocol instance with bound encode/decode functions
86
+ *
87
+ * @param codec - Wire codec for serialization (defaults to JSON)
88
+ * @returns Protocol object with pre-bound encode/decode methods
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * // JSON protocol (default)
93
+ * const protocol = createProtocol();
94
+ *
95
+ * // MessagePack protocol
96
+ * import { createMsgpackCodec } from "@igoforth/ws-rpc/codecs/msgpack";
97
+ * const protocol = createProtocol(createMsgpackCodec(RpcMessageSchema));
98
+ *
99
+ * // Use in peer
100
+ * const wire = protocol.createRequest("1", "ping", {});
101
+ * ws.send(wire); // string or Uint8Array depending on codec
102
+ *
103
+ * const message = protocol.decodeMessage(event.data);
104
+ * ```
105
+ */
106
+ function createProtocol(codec = RpcMessageCodec) {
107
+ const isString = isStringCodec(codec);
108
+ const textDecoder = new TextDecoder();
109
+ const textEncoder = new TextEncoder();
110
+ /**
111
+ * Normalize input for the codec type.
112
+ * String codecs need string input (decode ArrayBuffer via TextDecoder).
113
+ * Binary codecs need Uint8Array input.
114
+ *
115
+ * Handles ws library's RawData (Buffer | ArrayBuffer | Buffer[]):
116
+ * - Buffer extends Uint8Array, so it's handled as Uint8Array
117
+ * - Buffer[] (fragmented messages) are concatenated
118
+ */
119
+ const normalizeInput = (data) => {
120
+ if (Array.isArray(data)) {
121
+ const totalLength = data.reduce((sum, buf) => sum + buf.byteLength, 0);
122
+ const result = new Uint8Array(totalLength);
123
+ let offset = 0;
124
+ for (const buf of data) {
125
+ result.set(buf, offset);
126
+ offset += buf.byteLength;
127
+ }
128
+ return isString ? textDecoder.decode(result) : result;
129
+ }
130
+ if (isString) {
131
+ if (typeof data === "string") return data;
132
+ if (data instanceof ArrayBuffer) return textDecoder.decode(data);
133
+ return textDecoder.decode(data);
134
+ }
135
+ if (typeof data === "string") return textEncoder.encode(data);
136
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
137
+ return data;
138
+ };
139
+ return {
140
+ codec,
141
+ createRequest(id, method, params) {
142
+ return codec.encode({
143
+ type: "rpc:request",
144
+ id,
145
+ method,
146
+ params
147
+ });
148
+ },
149
+ createResponse(id, result) {
150
+ return codec.encode({
151
+ type: "rpc:response",
152
+ id,
153
+ result
154
+ });
155
+ },
156
+ createError(id, code, message, data) {
157
+ return codec.encode({
158
+ type: "rpc:error",
159
+ id,
160
+ code,
161
+ message,
162
+ data
163
+ });
164
+ },
165
+ createEvent(event, data) {
166
+ return codec.encode({
167
+ type: "rpc:event",
168
+ event,
169
+ data
170
+ });
171
+ },
172
+ decodeMessage(data) {
173
+ return codec.decode(normalizeInput(data));
174
+ },
175
+ safeDecodeMessage(data) {
176
+ try {
177
+ return codec.decode(normalizeInput(data));
178
+ } catch {
179
+ return null;
180
+ }
181
+ }
182
+ };
183
+ }
184
+ /**
185
+ * Default JSON protocol instance
186
+ *
187
+ * Pre-configured with JSON codec for convenience.
188
+ */
189
+ const JsonProtocol = createProtocol(RpcMessageCodec);
190
+
191
+ //#endregion
192
+ export { RpcMessageCodec as a, RpcResponseSchema as c, RpcEventSchema as i, createProtocol as l, RpcErrorCodes as n, RpcMessageSchema as o, RpcErrorSchema as r, RpcRequestSchema as s, JsonProtocol as t };
@@ -0,0 +1,6 @@
1
+ import "./schema-CN5HHHku.js";
2
+ import "./factory-C1v0AEHY.js";
3
+ import "./json-54Z2bIIs.js";
4
+ import "./index-Be7jjS77.js";
5
+ import { _ as createProtocol, a as RpcEvent, c as RpcMessageCodec, d as RpcRequest, f as RpcRequestSchema, g as WireInput, h as RpcWireCodec, i as RpcErrorSchema, l as RpcMessageSchema, m as RpcResponseSchema, n as RpcError, o as RpcEventSchema, p as RpcResponse, r as RpcErrorCodes, s as RpcMessage, t as JsonProtocol, u as RpcProtocol } from "./protocol-DA84zrc2.js";
6
+ export { JsonProtocol, RpcError, RpcErrorCodes, RpcErrorSchema, RpcEvent, RpcEventSchema, RpcMessage, RpcMessageCodec, RpcMessageSchema, RpcProtocol, RpcRequest, RpcRequestSchema, RpcResponse, RpcResponseSchema, RpcWireCodec, WireInput, createProtocol };
@@ -0,0 +1,6 @@
1
+ import "./factory-3ziwTuZe.js";
2
+ import "./json-Bshec-bZ.js";
3
+ import "./codecs-BmYG2d_U.js";
4
+ import { a as RpcMessageCodec, c as RpcResponseSchema, i as RpcEventSchema, l as createProtocol, n as RpcErrorCodes, o as RpcMessageSchema, r as RpcErrorSchema, s as RpcRequestSchema, t as JsonProtocol } from "./protocol-_mpoOPp6.js";
5
+
6
+ export { JsonProtocol, RpcErrorCodes, RpcErrorSchema, RpcEventSchema, RpcMessageCodec, RpcMessageSchema, RpcRequestSchema, RpcResponseSchema, createProtocol };
@@ -0,0 +1,26 @@
1
+ //#region src/utils/reconnect.ts
2
+ /**
3
+ * Default reconnection options
4
+ */
5
+ const defaultReconnectOptions = {
6
+ initialDelay: 1e3,
7
+ maxDelay: 3e4,
8
+ backoffMultiplier: 2,
9
+ maxAttempts: 0,
10
+ jitter: .1
11
+ };
12
+ /**
13
+ * Calculate delay for reconnection attempt with exponential backoff
14
+ *
15
+ * @param attempt - Reconnection attempt number (0-indexed)
16
+ * @param options - Reconnection options including backoff multiplier and max delay
17
+ * @returns Delay in milliseconds before the next reconnection attempt
18
+ */
19
+ function calculateReconnectDelay(attempt, options) {
20
+ const baseDelay = Math.min(options.initialDelay * Math.pow(options.backoffMultiplier, attempt), options.maxDelay);
21
+ const jitter = baseDelay * options.jitter * (Math.random() * 2 - 1);
22
+ return Math.max(0, baseDelay + jitter);
23
+ }
24
+
25
+ //#endregion
26
+ export { defaultReconnectOptions as n, calculateReconnectDelay as t };
@@ -0,0 +1,35 @@
1
+ //#region src/utils/reconnect.d.ts
2
+ /**
3
+ * Reconnection Utilities
4
+ *
5
+ * Exponential backoff with jitter for client-side reconnection.
6
+ */
7
+ /**
8
+ * Options for client-side reconnection
9
+ */
10
+ interface ReconnectOptions {
11
+ /** Initial delay before first reconnect attempt (ms) */
12
+ initialDelay?: number;
13
+ /** Maximum delay between reconnect attempts (ms) */
14
+ maxDelay?: number;
15
+ /** Multiplier for exponential backoff */
16
+ backoffMultiplier?: number;
17
+ /** Maximum number of reconnect attempts (0 = unlimited) */
18
+ maxAttempts?: number;
19
+ /** Jitter factor (0-1) to randomize delays */
20
+ jitter?: number;
21
+ }
22
+ /**
23
+ * Default reconnection options
24
+ */
25
+ declare const defaultReconnectOptions: Required<ReconnectOptions>;
26
+ /**
27
+ * Calculate delay for reconnection attempt with exponential backoff
28
+ *
29
+ * @param attempt - Reconnection attempt number (0-indexed)
30
+ * @param options - Reconnection options including backoff multiplier and max delay
31
+ * @returns Delay in milliseconds before the next reconnection attempt
32
+ */
33
+ declare function calculateReconnectDelay(attempt: number, options: Required<ReconnectOptions>): number;
34
+ //#endregion
35
+ export { calculateReconnectDelay as n, defaultReconnectOptions as r, ReconnectOptions as t };
@@ -0,0 +1,108 @@
1
+ import * as z from "zod";
2
+ import { LiteralUnion } from "type-fest";
3
+
4
+ //#region src/schema.d.ts
5
+
6
+ type StringKeys<T> = keyof T extends string ? keyof T : never;
7
+ type LiteralString = "" | (string & Record<never, never>);
8
+ type LiteralStringUnion<T> = LiteralUnion<T, string>;
9
+ /**
10
+ * Method definition with input and output schemas
11
+ */
12
+ interface MethodDef<TInput$1 extends z.ZodType = z.ZodType, TOutput$1 extends z.ZodType = z.ZodType> {
13
+ _type: "method";
14
+ input: TInput$1;
15
+ output: TOutput$1;
16
+ }
17
+ /**
18
+ * Event definition with data schema
19
+ */
20
+ interface EventDef<TData$1 extends z.ZodType = z.ZodType> {
21
+ _type: "event";
22
+ data: TData$1;
23
+ }
24
+ /**
25
+ * Define an RPC method with input/output schemas
26
+ *
27
+ * @param def - Object containing input and output Zod schemas
28
+ * @returns MethodDef with preserved type information
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const getUser = method({
33
+ * input: z.object({ id: z.string() }),
34
+ * output: z.object({ name: z.string(), email: z.string() }),
35
+ * });
36
+ * ```
37
+ */
38
+ declare function method<TInput$1 extends z.ZodType, TOutput$1 extends z.ZodType>(def: {
39
+ input: TInput$1;
40
+ output: TOutput$1;
41
+ }): MethodDef<TInput$1, TOutput$1>;
42
+ /**
43
+ * Define a fire-and-forget event with data schema
44
+ *
45
+ * @param def - Object containing data Zod schema
46
+ * @returns EventDef with preserved type information
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const userCreated = event({
51
+ * data: z.object({ id: z.string(), name: z.string() }),
52
+ * });
53
+ * ```
54
+ */
55
+ declare function event<TData$1 extends z.ZodType>(def: {
56
+ data: TData$1;
57
+ }): EventDef<TData$1>;
58
+ /**
59
+ * Schema definition containing methods and events
60
+ */
61
+ interface RpcSchema {
62
+ methods?: Record<string, MethodDef>;
63
+ events?: Record<string, EventDef>;
64
+ }
65
+ /**
66
+ * Infer the input type from a method definition
67
+ *
68
+ * @typeParam T - A MethodDef type to extract the input from
69
+ */
70
+ type InferInput<T> = T extends MethodDef<infer TInput, z.ZodType> ? z.infer<TInput> : never;
71
+ /**
72
+ * Infer the output type from a method definition
73
+ *
74
+ * @typeParam T - A MethodDef type to extract the output from
75
+ */
76
+ type InferOutput<T> = T extends MethodDef<z.ZodType, infer TOutput> ? z.infer<TOutput> : never;
77
+ /**
78
+ * Infer the data type from an event definition
79
+ *
80
+ * @typeParam T - An EventDef type to extract the data type from
81
+ */
82
+ type InferEventData<T> = T extends EventDef<infer TData> ? z.infer<TData> : never;
83
+ /**
84
+ * Infer method signatures from a schema's methods
85
+ */
86
+ type InferMethods<T extends RpcSchema> = T["methods"] extends Record<string, MethodDef> ? { [K in StringKeys<T["methods"]>]: (input: InferInput<T["methods"][K]>) => Promise<InferOutput<T["methods"][K]>> } : object;
87
+ /**
88
+ * Infer event emitter signatures from a schema's events
89
+ */
90
+ type InferEvents<T extends RpcSchema> = T["events"] extends Record<string, EventDef> ? { [K in StringKeys<T["events"]>]: InferEventData<T["events"][K]> } : object;
91
+ /**
92
+ * Provider type - implements the local methods defined in a schema
93
+ */
94
+ type Provider<T extends RpcSchema> = InferMethods<T>;
95
+ /**
96
+ * Driver type - proxy to call remote methods defined in a schema
97
+ */
98
+ type Driver<T extends RpcSchema> = InferMethods<T>;
99
+ /**
100
+ * Event handler type - handles incoming events
101
+ */
102
+ type EventHandler<T extends RpcSchema, ExtraArgs extends any[] = []> = <K$1 extends StringKeys<T["events"]>>(...args: [...ExtraArgs, event: K$1, data: T["events"] extends Record<string, EventDef> ? InferEventData<T["events"][K$1]> : never]) => void;
103
+ /**
104
+ * Event emitter type - emits outgoing events
105
+ */
106
+ type EventEmitter<T extends RpcSchema, ExtraArgs extends any[] = []> = <K$1 extends StringKeys<T["events"]>>(...args: [event: K$1, data: T["events"] extends Record<string, EventDef> ? InferEventData<T["events"][K$1]> : never, ...ExtraArgs]) => void;
107
+ //#endregion
108
+ export { method as _, InferEventData as a, InferMethods as c, LiteralStringUnion as d, MethodDef as f, event as g, StringKeys as h, EventHandler as i, InferOutput as l, RpcSchema as m, EventDef as n, InferEvents as o, Provider as p, EventEmitter as r, InferInput as s, Driver as t, LiteralString as u };
@@ -0,0 +1,2 @@
1
+ import { _ as method, a as InferEventData, c as InferMethods, d as LiteralStringUnion, f as MethodDef, g as event, h as StringKeys, i as EventHandler, l as InferOutput, m as RpcSchema, n as EventDef, o as InferEvents, p as Provider, r as EventEmitter, s as InferInput, t as Driver, u as LiteralString } from "./schema-CN5HHHku.js";
2
+ export { Driver, EventDef, EventEmitter, EventHandler, InferEventData, InferEvents, InferInput, InferMethods, InferOutput, LiteralString, LiteralStringUnion, MethodDef, Provider, RpcSchema, StringKeys, event, method };
package/dist/schema.js ADDED
@@ -0,0 +1,43 @@
1
+ //#region src/schema.ts
2
+ /**
3
+ * Define an RPC method with input/output schemas
4
+ *
5
+ * @param def - Object containing input and output Zod schemas
6
+ * @returns MethodDef with preserved type information
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const getUser = method({
11
+ * input: z.object({ id: z.string() }),
12
+ * output: z.object({ name: z.string(), email: z.string() }),
13
+ * });
14
+ * ```
15
+ */
16
+ function method(def) {
17
+ return {
18
+ _type: "method",
19
+ ...def
20
+ };
21
+ }
22
+ /**
23
+ * Define a fire-and-forget event with data schema
24
+ *
25
+ * @param def - Object containing data Zod schema
26
+ * @returns EventDef with preserved type information
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const userCreated = event({
31
+ * data: z.object({ id: z.string(), name: z.string() }),
32
+ * });
33
+ * ```
34
+ */
35
+ function event(def) {
36
+ return {
37
+ _type: "event",
38
+ ...def
39
+ };
40
+ }
41
+
42
+ //#endregion
43
+ export { event, method };