@irpclib/irpc 1.0.0-beta.21 → 1.0.0-beta.23

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.
package/dist/types.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
1
+ import { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
2
2
  import { ErrorCode } from "./error.js";
3
+ import { IRPCFile } from "./file.js";
3
4
  import { IRPCTransport } from "./transport.js";
4
5
  import { RemoteState } from "./state.js";
6
+ import { IRPCReader } from "./reader.js";
5
7
  import { StateChange } from "@anchorlib/core";
6
8
  import { ZodArray, ZodBoolean, ZodNull, ZodNumber, ZodObject, ZodSafeParseResult, ZodString, ZodUndefined } from "zod/v4";
7
9
 
@@ -20,7 +22,7 @@ type IRPCSpecStore = Map<string, IRPCSpec<IRPCInputs, IRPCOutput>>;
20
22
  type IRPCStatus = (typeof IRPC_STATUS)[keyof typeof IRPC_STATUS];
21
23
  type IRPCDataType = (typeof IRPC_DATA_TYPE)[keyof typeof IRPC_DATA_TYPE];
22
24
  type IRPCPacketType = (typeof IRPC_PACKET_TYPE)[keyof typeof IRPC_PACKET_TYPE];
23
- type IRPCEventType = (typeof IRPC_EVENT_TYPE)[keyof typeof IRPC_EVENT_TYPE];
25
+ type IRPCBaseContext = (typeof IRPC_BASE_CONTEXT)[keyof typeof IRPC_BASE_CONTEXT];
24
26
  type IRPCPacketBase = {
25
27
  id: string;
26
28
  name: string;
@@ -29,10 +31,6 @@ type IRPCPacketBase = {
29
31
  createdAt?: number;
30
32
  arrivedAt?: number;
31
33
  };
32
- type IRPCPacketData = {
33
- type: IRPCDataType;
34
- value: IRPCData;
35
- };
36
34
  type IRPCPacketCall = IRPCPacketBase & {
37
35
  args: IRPCData[];
38
36
  };
@@ -52,6 +50,53 @@ interface IRPCReadable<T> {
52
50
  error: Error | undefined;
53
51
  status: IRPCStatus;
54
52
  }
53
+ /**
54
+ * Represents a client-side stub for a remote function.
55
+ * When called, it returns an IRPCReader to handle the asynchronous result or stream.
56
+ *
57
+ * @template T - The original function type.
58
+ * @template A - The argument types of the function.
59
+ * @template R - The return data type.
60
+ */
61
+ interface IRPCStub<T, A$1 extends unknown[], R$1 extends IRPCData> {
62
+ (...args: A$1): IRPCReader<R$1>;
63
+ stub: T;
64
+ /**
65
+ * Creates a call that expect to run in browser environment.
66
+ * The function runs immediately on the browser and will not re-run.
67
+ *
68
+ * @param args - A factory function returning the argument array.
69
+ * @returns An IRPCReader instance for handling the asynchronous result or stream.
70
+ */
71
+ once(...args: A$1): IRPCReader<R$1>;
72
+ /**
73
+ * Creates a reactive call that expect to run in browser environment.
74
+ * The function runs immediately on the browser and will re-run when
75
+ * the reactive dependencies change.
76
+ *
77
+ * @param args - A factory function returning the argument array.
78
+ * @param debounce - The debounce time in milliseconds.
79
+ * @returns An IRPCReader instance for handling the asynchronous result or stream.
80
+ */
81
+ with(args: () => A$1, debounce?: number): IRPCReader<R$1>;
82
+ /**
83
+ * Creates a reactive call that expect to run in browser environment.
84
+ * The function only runs on the first dependency change and re-run
85
+ * when the reactive dependencies change.
86
+ *
87
+ * @param args - A factory function returning the argument array.
88
+ * @param debounce - The debounce time in milliseconds.
89
+ * @returns An IRPCReader instance for handling the asynchronous result or stream.
90
+ */
91
+ when(args: () => A$1, debounce?: number): IRPCReader<R$1>;
92
+ }
93
+ /**
94
+ * A utility type that transforms a standard function type into its corresponding IRPCStub.
95
+ * It automatically unwraps RemoteState types to determine the underlying data type.
96
+ *
97
+ * @template T - The function type to be transformed.
98
+ */
99
+ type IRPCFunction<T> = T extends ((...args: infer A) => infer R) ? R extends RemoteState<infer S> ? S extends IRPCData ? IRPCStub<T, A, S> : IRPCStub<T, A, IRPCData> : R extends Promise<infer O> ? O extends IRPCData ? IRPCStub<T, A, O> : IRPCStub<T, A, IRPCData> : R extends IRPCData ? IRPCStub<T, A, R> : IRPCStub<T, A, IRPCData> : IRPCStub<T, [], IRPCData>;
55
100
  /**
56
101
  * Represents primitive data types that can be used in IRPC communications.
57
102
  * Includes string, number, boolean, null, and undefined.
@@ -68,7 +113,7 @@ type IRPCObject = {
68
113
  * Represents all possible data types in IRPC, including primitives, objects, and arrays.
69
114
  * This is a recursive type that allows nested structures.
70
115
  */
71
- type IRPCData = IRPCPrimitive | IRPCObject | IRPCData[];
116
+ type IRPCData = IRPCPrimitive | IRPCObject | IRPCFile | IRPCData[];
72
117
  /**
73
118
  * Union type of all primitive Zod schema types used for validation.
74
119
  */
@@ -123,11 +168,11 @@ type IRPCPayload = {
123
168
  /**
124
169
  * Defines the schema for input and output validation of an RPC function.
125
170
  */
126
- type IRPCSchema<I extends IRPCInputs, O extends IRPCOutput> = {
171
+ type IRPCSchema<I extends IRPCInputs, O$1 extends IRPCOutput> = {
127
172
  /** Optional input validation schemas */
128
173
  input?: I;
129
174
  /** Optional output validation schema */
130
- output?: O;
175
+ output?: O$1;
131
176
  };
132
177
  /**
133
178
  * Type definition for an RPC handler function.
@@ -141,13 +186,13 @@ type IRPCHandler = Function;
141
186
  * @template I - Tuple of input validation schemas
142
187
  * @template O - Output validation schema
143
188
  */
144
- type IRPCInit<I extends IRPCInputs, O extends IRPCOutput> = {
189
+ type IRPCInit<R$1, I extends IRPCInputs, O$1 extends IRPCOutput> = {
145
190
  /** The name of the RPC function */
146
191
  name: string;
147
192
  /** Optional description of the RPC function */
148
193
  description?: string;
149
194
  /** Optional schema for input/output validation */
150
- schema?: IRPCSchema<I, O>;
195
+ schema?: IRPCSchema<I, O$1>;
151
196
  /** Optional maximum age of a call in milliseconds */
152
197
  maxAge?: number;
153
198
  /**
@@ -157,7 +202,20 @@ type IRPCInit<I extends IRPCInputs, O extends IRPCOutput> = {
157
202
  * This can help reduce the number of actual function executions.
158
203
  */
159
204
  coalesce?: boolean;
205
+ /** Optional initialization function to seed the data */
206
+ init?: () => R$1;
160
207
  } & IRPCCallConfig;
208
+ /**
209
+ * Configuration options for initializing an RPC stream function.
210
+ * Contains metadata and constraints for the RPC stream function.
211
+ *
212
+ * @template I - Tuple of input validation schemas
213
+ * @template O - Output validation schema
214
+ */
215
+ type IRPCStreamInit<I extends IRPCInputs, O$1 extends IRPCOutput, R$1> = IRPCInit<R$1, I, O$1> & {
216
+ stream: true;
217
+ ttl?: number;
218
+ };
161
219
  /**
162
220
  * Type definition for an RPC declaration.
163
221
  * Represents an RPC function with its name, description, and configuration.
@@ -166,9 +224,7 @@ type IRPCInit<I extends IRPCInputs, O extends IRPCOutput> = {
166
224
  * @template I - Tuple of input validation schemas
167
225
  * @template O - Output validation schema
168
226
  */
169
- type IRPCDeclareInit<F, I extends IRPCInputs, O extends IRPCOutput> = F extends ((...args: any[]) => RemoteState<infer R>) ? IRPCInit<I, IRPCOutput> & {
170
- init: () => R;
171
- } : IRPCInit<I, O>;
227
+ type IRPCDeclareInit<F, I extends IRPCInputs, O$1 extends IRPCOutput> = F extends ((...args: IRPCData[]) => infer R) ? R extends RemoteState<infer S> ? S extends IRPCData ? IRPCStreamInit<I, O$1, S> : IRPCInit<IRPCData, IRPCInputs, IRPCOutput> : R extends Promise<infer D> ? D extends IRPCData ? IRPCInit<D, I, O$1> : IRPCInit<IRPCData, IRPCInputs, IRPCOutput> : R extends IRPCData ? IRPCInit<R, I, O$1> : IRPCInit<IRPCData, IRPCInputs, IRPCOutput> : IRPCInit<IRPCData, IRPCInputs, IRPCOutput>;
172
228
  /**
173
229
  * Complete specification for an RPC function including its implementation.
174
230
  * Extends IRPCInit with the actual handler function.
@@ -176,9 +232,14 @@ type IRPCDeclareInit<F, I extends IRPCInputs, O extends IRPCOutput> = F extends
176
232
  * @template I - Tuple of input validation schemas
177
233
  * @template O - Output validation schema
178
234
  */
179
- type IRPCSpec<I extends IRPCInputs, O extends IRPCOutput> = IRPCInit<I, O> & {
235
+ type IRPCSpec<I extends IRPCInputs, O$1 extends IRPCOutput> = IRPCInit<IRPCData, I, O$1> & {
236
+ /** Optional time-to-live for a call in milliseconds */
237
+ ttl?: number;
238
+ /** Whether to stream the result of the RPC call */
239
+ stream?: boolean;
180
240
  /** The actual handler function that implements the RPC */
181
241
  handler: IRPCHandler;
242
+ /** Optional initialization function for a stream RPC */
182
243
  init?: () => unknown;
183
244
  };
184
245
  /**
@@ -238,6 +299,8 @@ type IRPCCallConfig = {
238
299
  retryMode?: 'linear' | 'exponential';
239
300
  /** Base delay between retries in milliseconds */
240
301
  retryDelay?: number;
302
+ /** Optional initialization function for a stream RPC */
303
+ init?: () => unknown;
241
304
  };
242
305
  /**
243
306
  * Configuration for transport layer, extending call configuration with debounce settings.
@@ -246,5 +309,17 @@ type TransportConfig = IRPCCallConfig & {
246
309
  /** Debounce setting for transport - can be a boolean to enable/disable or a number for specific delay */
247
310
  debounce?: number | boolean;
248
311
  };
312
+ type StreamCleanup = () => void;
313
+ /**
314
+ * A callback function type used to natively construct and drive a reactive stream.
315
+ * It provides the initial reactive data reference and terminal resolution hooks
316
+ * without forcing strict async/await boundaries, securely yielding stream operations.
317
+ *
318
+ * @template T - The type of data yielded globally by the stream.
319
+ * @param state - The reactive state reference for the stream.
320
+ * @param resolve - Callback to statically mark the stream as successfully completed, optionally with a resolved value.
321
+ * @param reject - Callback to forcefully throw a runtime error into the stream structure.
322
+ */
323
+ type StreamConstructor<T> = (state: IRPCReadable<T>, resolve: (value?: T) => void, reject: (error: Error) => void) => StreamCleanup | void | Promise<StreamCleanup | void>;
249
324
  //#endregion
250
- export { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCDataType, IRPCDeclareInit, IRPCError, IRPCEventType, IRPCHandler, IRPCInit, IRPCInputs, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketData, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStubStore, TransportConfig };
325
+ export { IRPCArraySchema, IRPCBaseContext, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCDataType, IRPCDeclareInit, IRPCError, IRPCFunction, IRPCHandler, IRPCInit, IRPCInputs, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig };
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@irpclib/irpc",
4
- "version": "1.0.0-beta.21",
4
+ "version": "1.0.0-beta.23",
5
5
  "types": "./dist/index.d.ts",
6
6
  "module": "./dist/index.js",
7
7
  "exports": {
8
8
  ".": {
9
9
  "types": "./dist/index.d.ts",
10
10
  "import": "./dist/index.js"
11
+ },
12
+ "./server": {
13
+ "types": "./dist/server/index.d.ts",
14
+ "import": "./dist/server/index.js"
11
15
  }
12
16
  },
13
17
  "files": ["dist"],
@@ -25,7 +29,7 @@
25
29
  "publint": "0.3.15",
26
30
  "rimraf": "6.0.1",
27
31
  "tsdown": "0.15.9",
28
- "vite": "7.1.12",
32
+ "vite": "8.0.10",
29
33
  "vitest": "^3.2.4",
30
34
  "zod": "^4.1.5"
31
35
  },
@@ -46,6 +50,6 @@
46
50
  },
47
51
  "license": "MIT",
48
52
  "dependencies": {
49
- "@anchorlib/core": "1.0.0-beta.20"
53
+ "@anchorlib/core": "^1.0.0-beta.23"
50
54
  }
51
55
  }
package/readme.md CHANGED
@@ -120,7 +120,7 @@ import { loadDashboard } from './index.js';
120
120
  import { stream } from '@irpclib/irpc';
121
121
 
122
122
  irpc.construct(loadDashboard, (userId) => {
123
- return stream((data, resolve) => {
123
+ return stream(({ data }, resolve) => {
124
124
  const q1 = db.users.get(userId).then(res => data.user = res);
125
125
  const q2 = db.sales.aggregate(userId).then(res => data.sales = res);
126
126
 
@@ -131,6 +131,8 @@ irpc.construct(loadDashboard, (userId) => {
131
131
 
132
132
  ### 4. Setup Server
133
133
 
134
+ The integration point extracts application-level values from transport-specific objects and injects them as standardized context via `initContext`. This keeps middleware and handlers transport-agnostic.
135
+
134
136
  ```typescript
135
137
  // server.ts
136
138
  import { setContextProvider } from '@irpclib/irpc';
@@ -147,7 +149,10 @@ Bun.serve({
147
149
  port: 3000,
148
150
  routes: {
149
151
  [transport.endpoint]: {
150
- POST: (req) => router.resolve(req),
152
+ POST: (req) => router.resolve(req, [
153
+ ['token', req.headers.get('authorization')],
154
+ ['locale', req.headers.get('accept-language')],
155
+ ]),
151
156
  }
152
157
  },
153
158
  });
@@ -256,7 +261,7 @@ export const createUser = irpc.declare({
256
261
 
257
262
  ## Documentation
258
263
 
259
- For detailed documentation, visit [https://anchorlib.dev/docs/irpc](https://anchorlib.dev/docs/irpc)
264
+ For detailed documentation, visit [https://airlib.dev/irpc](https://airlib.dev/irpc)
260
265
 
261
266
  ## License
262
267
 
package/dist/uuid.d.ts DELETED
@@ -1,21 +0,0 @@
1
- //#region src/uuid.d.ts
2
- /**
3
- * A function that generates a random UUID string.
4
- *
5
- * @returns A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
6
- */
7
- type UUIDProvider = () => string;
8
- /**
9
- * Generates a new UUID using the currently configured provider.
10
- *
11
- * @returns A UUID string generated by the current provider
12
- */
13
- declare function uuid(): string;
14
- /**
15
- * Sets a custom UUID provider function to be used by the uuid() function.
16
- *
17
- * @param provider - A function that returns a UUID string when called
18
- */
19
- declare function setUUIDProvider(provider: UUIDProvider): void;
20
- //#endregion
21
- export { UUIDProvider, setUUIDProvider, uuid };
package/dist/uuid.js DELETED
@@ -1,45 +0,0 @@
1
- //#region src/uuid.ts
2
- /**
3
- * Generates a random UUID v4 string using a simple algorithm.
4
- *
5
- * @returns A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
6
- */
7
- function simpleId() {
8
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
9
- const r = Math.random() * 16 | 0;
10
- return (c === "x" ? r : r & 3 | 8).toString(16);
11
- });
12
- }
13
- /**
14
- * Default UUID provider that attempts to use the crypto API if available,
15
- * otherwise falls back to a simpler implementation.
16
- *
17
- * @returns A UUID v4 string
18
- */
19
- function defaultUUIDProvider() {
20
- if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
21
- return simpleId();
22
- }
23
- /**
24
- * The currently active UUID provider function.
25
- */
26
- let uuidProvider = defaultUUIDProvider;
27
- /**
28
- * Generates a new UUID using the currently configured provider.
29
- *
30
- * @returns A UUID string generated by the current provider
31
- */
32
- function uuid() {
33
- return (uuidProvider ?? defaultUUIDProvider)();
34
- }
35
- /**
36
- * Sets a custom UUID provider function to be used by the uuid() function.
37
- *
38
- * @param provider - A function that returns a UUID string when called
39
- */
40
- function setUUIDProvider(provider) {
41
- uuidProvider = provider;
42
- }
43
-
44
- //#endregion
45
- export { setUUIDProvider, uuid };