@irpclib/irpc 0.0.2 → 1.0.0-beta.16

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.
@@ -0,0 +1,20 @@
1
+ //#region src/cache.d.ts
2
+ type IRPCCacheEntry = {
3
+ key: string;
4
+ value: unknown;
5
+ expires: number;
6
+ };
7
+ declare class IRPCCacher {
8
+ caches: Map<string, IRPCCacheEntry>;
9
+ queues: Map<string, number | NodeJS.Timeout>;
10
+ get size(): number;
11
+ has(key: string): boolean;
12
+ get(key: string): IRPCCacheEntry | undefined;
13
+ values(): MapIterator<IRPCCacheEntry>;
14
+ entries(): MapIterator<[string, IRPCCacheEntry]>;
15
+ set(key: string, value: unknown, maxAge: number): void;
16
+ delete(key: string): void;
17
+ clear(): void;
18
+ }
19
+ //#endregion
20
+ export { IRPCCacheEntry, IRPCCacher };
package/dist/cache.js ADDED
@@ -0,0 +1,55 @@
1
+ //#region src/cache.ts
2
+ var IRPCCacher = class {
3
+ caches = /* @__PURE__ */ new Map();
4
+ queues = /* @__PURE__ */ new Map();
5
+ get size() {
6
+ return this.caches.size;
7
+ }
8
+ has(key) {
9
+ return this.caches.has(key);
10
+ }
11
+ get(key) {
12
+ const entry = this.caches.get(key);
13
+ if (!entry) return void 0;
14
+ if (entry.expires < Date.now()) {
15
+ this.caches.delete(key);
16
+ return;
17
+ }
18
+ return entry;
19
+ }
20
+ values() {
21
+ return this.caches.values();
22
+ }
23
+ entries() {
24
+ return this.caches.entries();
25
+ }
26
+ set(key, value, maxAge) {
27
+ if (!maxAge || maxAge < 0) throw new Error("Max age must be a positive number.");
28
+ const queue = this.queues.get(key);
29
+ if (queue) clearTimeout(queue);
30
+ const expires = Date.now() + maxAge;
31
+ const nextQueue = setTimeout(() => {
32
+ this.caches.delete(key);
33
+ this.queues.delete(key);
34
+ }, maxAge);
35
+ this.caches.set(key, {
36
+ key,
37
+ value,
38
+ expires
39
+ });
40
+ this.queues.set(key, nextQueue);
41
+ }
42
+ delete(key) {
43
+ clearTimeout(this.queues.get(key));
44
+ this.caches.delete(key);
45
+ this.queues.delete(key);
46
+ }
47
+ clear() {
48
+ for (const queue of this.queues.values()) clearTimeout(queue);
49
+ this.caches.clear();
50
+ this.queues.clear();
51
+ }
52
+ };
53
+
54
+ //#endregion
55
+ export { IRPCCacher };
package/dist/call.d.ts CHANGED
@@ -10,6 +10,7 @@ declare class IRPCCall {
10
10
  payload: IRPCPayload;
11
11
  resolver: (value: unknown) => void;
12
12
  rejector: (reason?: Error) => void;
13
+ timeout?: number | undefined;
13
14
  /**
14
15
  * Unique identifier for this RPC call, generated using shortId().
15
16
  */
@@ -25,7 +26,7 @@ declare class IRPCCall {
25
26
  * @param resolver - Function to resolve the associated promise with a value
26
27
  * @param rejector - Function to reject the associated promise with an error
27
28
  */
28
- constructor(payload: IRPCPayload, resolver: (value: unknown) => void, rejector: (reason?: Error) => void);
29
+ constructor(payload: IRPCPayload, resolver: (value: unknown) => void, rejector: (reason?: Error) => void, timeout?: number | undefined);
29
30
  /**
30
31
  * Resolves the RPC call with the provided value.
31
32
  * If the call is already resolved, this method does nothing.
package/dist/call.js CHANGED
@@ -1,4 +1,4 @@
1
- import { shortId } from "./utils.js";
1
+ import { uuid } from "./uuid.js";
2
2
 
3
3
  //#region src/call.ts
4
4
  /**
@@ -9,7 +9,7 @@ var IRPCCall = class {
9
9
  /**
10
10
  * Unique identifier for this RPC call, generated using shortId().
11
11
  */
12
- id = shortId();
12
+ id = uuid();
13
13
  /**
14
14
  * Flag indicating whether this call has been resolved or rejected.
15
15
  * Prevents multiple resolutions of the same call.
@@ -21,10 +21,11 @@ var IRPCCall = class {
21
21
  * @param resolver - Function to resolve the associated promise with a value
22
22
  * @param rejector - Function to reject the associated promise with an error
23
23
  */
24
- constructor(payload, resolver, rejector) {
24
+ constructor(payload, resolver, rejector, timeout) {
25
25
  this.payload = payload;
26
26
  this.resolver = resolver;
27
27
  this.rejector = rejector;
28
+ this.timeout = timeout;
28
29
  }
29
30
  /**
30
31
  * Resolves the RPC call with the provided value.
@@ -0,0 +1,50 @@
1
+ //#region src/error.d.ts
2
+ declare const ERROR_CODE: {
3
+ UNKNOWN: string;
4
+ TIMEOUT: string;
5
+ NOT_FOUND: string;
6
+ NOT_SUPPORTED: string;
7
+ INVALID_TYPE: string;
8
+ INVALID_STATE: string;
9
+ INVALID_VALUE: string;
10
+ INVALID_INPUT: string;
11
+ INVALID_OUTPUT: string;
12
+ NOT_IMPLEMENTED: string;
13
+ INVALID_HANDLER: string;
14
+ INVALID_OPERATION: string;
15
+ TRANSPORT_MISSING: string;
16
+ TRANSPORT_INVALID: string;
17
+ TRANSPORT_NOT_IMPLEMENTED: string;
18
+ STUB_MISSING: string;
19
+ STUB_INVALID: string;
20
+ STUB_NOT_IMPLEMENTED: string;
21
+ RESOLVER_MISSING: string;
22
+ RESOLVER_NOT_IMPLEMENTED: string;
23
+ RESOLVER_NOT_FOUND: string;
24
+ RESOLVER_NOT_SUPPORTED: string;
25
+ };
26
+ type ErrorCode = (typeof ERROR_CODE)[keyof typeof ERROR_CODE];
27
+ declare const ERROR_MESSAGE: {
28
+ [ERROR_CODE.UNKNOWN]: string;
29
+ [ERROR_CODE.TIMEOUT]: string;
30
+ [ERROR_CODE.NOT_FOUND]: string;
31
+ [ERROR_CODE.NOT_SUPPORTED]: string;
32
+ [ERROR_CODE.INVALID_TYPE]: string;
33
+ [ERROR_CODE.INVALID_STATE]: string;
34
+ [ERROR_CODE.INVALID_VALUE]: string;
35
+ [ERROR_CODE.INVALID_INPUT]: string;
36
+ [ERROR_CODE.INVALID_OUTPUT]: string;
37
+ [ERROR_CODE.NOT_IMPLEMENTED]: string;
38
+ [ERROR_CODE.INVALID_HANDLER]: string;
39
+ [ERROR_CODE.INVALID_OPERATION]: string;
40
+ [ERROR_CODE.TRANSPORT_MISSING]: string;
41
+ [ERROR_CODE.TRANSPORT_INVALID]: string;
42
+ [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: string;
43
+ [ERROR_CODE.STUB_INVALID]: string;
44
+ [ERROR_CODE.STUB_NOT_IMPLEMENTED]: string;
45
+ [ERROR_CODE.RESOLVER_MISSING]: string;
46
+ [ERROR_CODE.RESOLVER_NOT_IMPLEMENTED]: string;
47
+ [ERROR_CODE.RESOLVER_NOT_FOUND]: string;
48
+ };
49
+ //#endregion
50
+ export { ERROR_CODE, ERROR_MESSAGE, ErrorCode };
package/dist/error.js ADDED
@@ -0,0 +1,50 @@
1
+ //#region src/error.ts
2
+ const ERROR_CODE = {
3
+ UNKNOWN: "unknown",
4
+ TIMEOUT: "timeout",
5
+ NOT_FOUND: "not_found",
6
+ NOT_SUPPORTED: "not_supported",
7
+ INVALID_TYPE: "invalid_type",
8
+ INVALID_STATE: "invalid_state",
9
+ INVALID_VALUE: "invalid_value",
10
+ INVALID_INPUT: "invalid_argument",
11
+ INVALID_OUTPUT: "invalid_argument",
12
+ NOT_IMPLEMENTED: "not_implemented",
13
+ INVALID_HANDLER: "invalid_handler",
14
+ INVALID_OPERATION: "invalid_operation",
15
+ TRANSPORT_MISSING: "transport_missing",
16
+ TRANSPORT_INVALID: "transport_invalid",
17
+ TRANSPORT_NOT_IMPLEMENTED: "transport_not_implemented",
18
+ STUB_MISSING: "stub_missing",
19
+ STUB_INVALID: "stub_invalid",
20
+ STUB_NOT_IMPLEMENTED: "stub_not_implemented",
21
+ RESOLVER_MISSING: "resolver_missing",
22
+ RESOLVER_NOT_IMPLEMENTED: "resolver_not_implemented",
23
+ RESOLVER_NOT_FOUND: "resolver_not_found",
24
+ RESOLVER_NOT_SUPPORTED: "resolver_not_supported"
25
+ };
26
+ const ERROR_MESSAGE = {
27
+ [ERROR_CODE.UNKNOWN]: "IRPC: Unknown error",
28
+ [ERROR_CODE.TIMEOUT]: "IRPC: Timeout error",
29
+ [ERROR_CODE.NOT_FOUND]: "IRPC: Not found error",
30
+ [ERROR_CODE.NOT_SUPPORTED]: "IRPC: Not supported error",
31
+ [ERROR_CODE.INVALID_TYPE]: "IRPC: Invalid type error",
32
+ [ERROR_CODE.INVALID_STATE]: "IRPC: Invalid state error",
33
+ [ERROR_CODE.INVALID_VALUE]: "IRPC: Invalid value error",
34
+ [ERROR_CODE.INVALID_INPUT]: "IRPC: Invalid input error",
35
+ [ERROR_CODE.INVALID_OUTPUT]: "IRPC: Invalid output error",
36
+ [ERROR_CODE.NOT_IMPLEMENTED]: "IRPC: Not implemented error",
37
+ [ERROR_CODE.INVALID_HANDLER]: "IRPC: Invalid handler error",
38
+ [ERROR_CODE.INVALID_OPERATION]: "IRPC: Invalid operation error",
39
+ [ERROR_CODE.TRANSPORT_MISSING]: "IRPC: Transport missing error",
40
+ [ERROR_CODE.TRANSPORT_INVALID]: "IRPC: Transport invalid error",
41
+ [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: "IRPC: Transport not implemented error",
42
+ [ERROR_CODE.STUB_INVALID]: "IRPC: Stub invalid error",
43
+ [ERROR_CODE.STUB_NOT_IMPLEMENTED]: "IRPC: Stub not implemented error",
44
+ [ERROR_CODE.RESOLVER_MISSING]: "IRPC: Resolver missing error",
45
+ [ERROR_CODE.RESOLVER_NOT_IMPLEMENTED]: "IRPC: Resolver not implemented error",
46
+ [ERROR_CODE.RESOLVER_NOT_FOUND]: "IRPC: Resolver not found error"
47
+ };
48
+
49
+ //#endregion
50
+ export { ERROR_CODE, ERROR_MESSAGE };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { IRPCArraySchema, IRPCAuthorizer, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCFactory, IRPCHandler, IRPCHost, IRPCInputs, IRPCModule, IRPCNamespace, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCRegistry, IRPCRequest, IRPCResponse, IRPCSchema, IRPCSpec, IRPCStore, IRPCTransport } from "./types.js";
1
+ import { IRPCCacheEntry, IRPCCacher } from "./cache.js";
2
+ import { ERROR_CODE, ERROR_MESSAGE, ErrorCode } from "./error.js";
3
+ import { IRPCTransport } from "./transport.js";
4
+ import { IRPCArraySchema, IRPCCache, IRPCCaches, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCError, IRPCHandler, IRPCInit, IRPCInputs, IRPCModule, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCRequest, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStubStore, TransportConfig } from "./types.js";
2
5
  import { IRPCCall } from "./call.js";
3
- import { batch } from "./batch.js";
4
6
  import { createContext, getContext, setContext, setContextProvider, withContext } from "./context.js";
5
- import { createModule } from "./module.js";
6
- import { shortId } from "./utils.js";
7
- export { IRPCArraySchema, IRPCAuthorizer, IRPCCall, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCFactory, IRPCHandler, IRPCHost, IRPCInputs, IRPCModule, IRPCNamespace, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCRegistry, IRPCRequest, IRPCResponse, IRPCSchema, IRPCSpec, IRPCStore, IRPCTransport, batch, createContext, createModule, getContext, setContext, setContextProvider, shortId, withContext };
7
+ import { IRPCPackage, createPackage } from "./module.js";
8
+ import { IRPCResolver } from "./resolver.js";
9
+ export { ERROR_CODE, ERROR_MESSAGE, ErrorCode, IRPCArraySchema, IRPCCache, IRPCCacheEntry, IRPCCacher, IRPCCaches, IRPCCall, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCError, IRPCHandler, IRPCInit, IRPCInputs, IRPCModule, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackage, IRPCPackageConfig, IRPCPackageInfo, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCRequest, IRPCResolver, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStubStore, IRPCTransport, TransportConfig, createContext, createPackage, getContext, setContext, setContextProvider, withContext };
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
- import { batch } from "./batch.js";
2
- import { shortId } from "./utils.js";
1
+ import { IRPCCacher } from "./cache.js";
3
2
  import { IRPCCall } from "./call.js";
4
3
  import { createContext, getContext, setContext, setContextProvider, withContext } from "./context.js";
5
- import { createModule } from "./module.js";
6
- import { IRPCTransport } from "./types.js";
4
+ import { ERROR_CODE, ERROR_MESSAGE } from "./error.js";
5
+ import { IRPCTransport } from "./transport.js";
6
+ import { IRPCPackage, createPackage } from "./module.js";
7
+ import { IRPCResolver } from "./resolver.js";
7
8
 
8
- export { IRPCCall, IRPCTransport, batch, createContext, createModule, getContext, setContext, setContextProvider, shortId, withContext };
9
+ export { ERROR_CODE, ERROR_MESSAGE, IRPCCacher, IRPCCall, IRPCPackage, IRPCResolver, IRPCTransport, createContext, createPackage, getContext, setContext, setContextProvider, withContext };
package/dist/module.d.ts CHANGED
@@ -1,17 +1,103 @@
1
- import { IRPCFactory, IRPCModule } from "./types.js";
1
+ import { IRPCCacher } from "./cache.js";
2
+ import { IRPCTransport } from "./transport.js";
3
+ import { IRPCData, IRPCHandler, IRPCInit, IRPCInputs, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCRequest, IRPCSpec, IRPCSpecStore, IRPCStubStore } from "./types.js";
2
4
 
3
5
  //#region src/module.d.ts
4
6
 
5
7
  /**
6
- * Creates an IRPC module with the given configuration.
7
- *
8
- * This function initializes a new IRPC module with a store for tracking RPC calls,
9
- * a registry for mapping functions to their specifications, and various methods
10
- * for managing and executing remote procedure calls.
11
- *
12
- * @param config - Optional partial configuration for the IRPC module, excluding 'submit' and 'transport'
13
- * @returns An IRPC factory function with attached utility methods
8
+ * IRPCPackage represents a package containing multiple IRPC (Isomorphic-RPC) specifications
9
+ * and their corresponding stubs. It manages the configuration, transport, and execution
10
+ * of remote procedure calls.
14
11
  */
15
- declare function createModule(config?: Partial<Omit<IRPCModule, 'submit' | 'transport'>>): IRPCFactory;
12
+ declare class IRPCPackage {
13
+ /**
14
+ * A map storing all IRPC specifications by their names
15
+ */
16
+ specs: IRPCSpecStore;
17
+ /**
18
+ * A weak map linking stub functions to their corresponding specifications
19
+ */
20
+ stubs: IRPCStubStore;
21
+ /**
22
+ * A map storing caches for each IRPC Entry
23
+ */
24
+ cache: WeakMap<Function, IRPCCacher>;
25
+ /**
26
+ * Configuration object for the IRPC package
27
+ */
28
+ config: IRPCPackageConfig;
29
+ /**
30
+ * Gets the href URL for this package in the format "name/version"
31
+ */
32
+ get href(): string;
33
+ /**
34
+ * Gets the package information (name, version, and optional description)
35
+ */
36
+ get info(): IRPCPackageInfo;
37
+ /**
38
+ * Gets the transport mechanism used for remote calls
39
+ */
40
+ get transport(): IRPCTransport | undefined;
41
+ /**
42
+ * Creates a new IRPCPackage instance
43
+ * @param config - Optional partial configuration for the package
44
+ * @throws Error if the package name or version doesn't match the required format
45
+ */
46
+ constructor(config?: Partial<IRPCPackageConfig>);
47
+ /**
48
+ * Declares a new IRPC specification and creates a corresponding stub function
49
+ * @param init - The initialization object containing the IRPC specification
50
+ * @returns A stub function that can be used to call the IRPC
51
+ * @throws Error if an IRPC with the same name already exists
52
+ */
53
+ declare<F, I extends IRPCInputs = IRPCInputs, O extends IRPCOutput = IRPCOutput>(init: IRPCInit<I, O>): F;
54
+ /**
55
+ * Resolves and executes an IRPC call based on a request object
56
+ * @param req - The request containing the IRPC name and arguments
57
+ * @returns The result of the IRPC execution
58
+ * @throws Error if the IRPC doesn't exist or doesn't have an implementation
59
+ */
60
+ resolve(req: IRPCRequest): Promise<IRPCData>;
61
+ /**
62
+ * Associates a handler function with a stub function
63
+ * @param stub - The stub function created by declare()
64
+ * @param handler - The actual implementation function
65
+ * @returns This IRPCPackage instance for chaining
66
+ * @throws Error if the stub or handler is invalid, or if no IRPC exists for the stub
67
+ */
68
+ construct<F extends IRPCHandler>(stub: F, handler: F): this;
69
+ /**
70
+ * Sets the transport mechanism for this package
71
+ * @param transport - The transport instance to use for remote calls
72
+ * @returns This IRPCPackage instance for chaining
73
+ * @throws Error if the transport is not a valid Transport instance
74
+ */
75
+ use(transport: IRPCTransport): this;
76
+ /**
77
+ * Retrieves an IRPC specification by name or request object
78
+ * @param query - Either a string name or an IRPCRequest object
79
+ * @returns The IRPC specification or undefined if not found
80
+ */
81
+ get(query: string | IRPCRequest): IRPCSpec<IRPCInputs, IRPCOutput> | undefined;
82
+ /**
83
+ * Updates the package configuration
84
+ * @param config - Partial configuration object with properties to update
85
+ * @returns This IRPCPackage instance for chaining
86
+ * @throws Error if the provided name or version is invalid
87
+ */
88
+ configure(config: Partial<IRPCPackageConfig>): this;
89
+ /**
90
+ * Invalidates the cache for a specific stub and arguments combination
91
+ * @param stub - The IRPC stub function whose cache needs to be invalidated
92
+ * @param args - The arguments array used as cache key
93
+ */
94
+ invalidate(stub: IRPCHandler, ...args: IRPCData[]): void;
95
+ }
96
+ /**
97
+ * Creates a new IRPCPackage instance with the given configuration
98
+ * @param config - Optional partial configuration for the package
99
+ * @returns A new IRPCPackage instance
100
+ */
101
+ declare function createPackage(config?: Partial<IRPCPackageConfig>): IRPCPackage;
16
102
  //#endregion
17
- export { createModule };
103
+ export { IRPCPackage, createPackage };