@irpclib/irpc 1.2.6 → 1.2.8
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/adapter.d.ts +1 -1
- package/dist/error.d.ts +2 -0
- package/dist/error.js +5 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/{module.d.ts → package.d.ts} +1 -1
- package/dist/{module.js → package.js} +6 -3
- package/dist/resolver.d.ts +4 -3
- package/dist/resolver.js +6 -3
- package/dist/router.d.ts +16 -5
- package/dist/router.js +18 -2
- package/dist/store.d.ts +1 -1
- package/dist/store.js +1 -1
- package/dist/transport.d.ts +19 -4
- package/dist/transport.js +29 -2
- package/dist/types.d.ts +14 -1
- package/package.json +2 -2
package/dist/adapter.d.ts
CHANGED
package/dist/error.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ declare const TRANSPORT_ERROR: {
|
|
|
32
32
|
readonly INVALID_BODY: "invalid_body";
|
|
33
33
|
readonly STREAM_TERMINATED: "stream_terminated";
|
|
34
34
|
readonly ERROR: "error";
|
|
35
|
+
readonly NOT_FOUND: "not_found";
|
|
35
36
|
};
|
|
36
37
|
declare const RESOLVE_ERROR: {
|
|
37
38
|
readonly NOT_FOUND: "not_found";
|
|
@@ -95,6 +96,7 @@ declare class TransportError extends IRPCError {
|
|
|
95
96
|
static invalidBody(): TransportError;
|
|
96
97
|
static streamTerminated(): TransportError;
|
|
97
98
|
static failed(input: Error | string): TransportError;
|
|
99
|
+
static notFound(name: string): TransportError;
|
|
98
100
|
}
|
|
99
101
|
/** Errors related to server-side request resolution. */
|
|
100
102
|
declare class ResolveError extends IRPCError {
|
package/dist/error.js
CHANGED
|
@@ -28,7 +28,8 @@ const TRANSPORT_ERROR = {
|
|
|
28
28
|
CLOSED: "closed",
|
|
29
29
|
INVALID_BODY: "invalid_body",
|
|
30
30
|
STREAM_TERMINATED: "stream_terminated",
|
|
31
|
-
ERROR: "error"
|
|
31
|
+
ERROR: "error",
|
|
32
|
+
NOT_FOUND: "not_found"
|
|
32
33
|
};
|
|
33
34
|
const RESOLVE_ERROR = {
|
|
34
35
|
NOT_FOUND: "not_found",
|
|
@@ -151,6 +152,9 @@ var TransportError = class TransportError extends IRPCError {
|
|
|
151
152
|
const { message, cause } = unwrap(input);
|
|
152
153
|
return new TransportError(TRANSPORT_ERROR.ERROR, message, cause);
|
|
153
154
|
}
|
|
155
|
+
static notFound(name) {
|
|
156
|
+
return new TransportError(TRANSPORT_ERROR.NOT_FOUND, `Can not resolve package for "${name}" call.`);
|
|
157
|
+
}
|
|
154
158
|
};
|
|
155
159
|
/** Errors related to server-side request resolution. */
|
|
156
160
|
var ResolveError = class ResolveError extends IRPCError {
|
package/dist/index.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ import { IRPCBlobPointer, IRPCFilePointer, IRPCFileQueue, IRPCPacketJson, IRPCPa
|
|
|
4
4
|
import { RemoteState, stream } from "./state.js";
|
|
5
5
|
import { IRPCReader } from "./reader.js";
|
|
6
6
|
import { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall } from "./call.js";
|
|
7
|
-
import { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept } from "./
|
|
8
|
-
import { IRPCTransport } from "./transport.js";
|
|
9
|
-
import { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCEntityId, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCRequests, IRPCResponse, IRPCReturnOf, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig } from "./types.js";
|
|
7
|
+
import { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept } from "./package.js";
|
|
8
|
+
import { IRPCPackageMap, IRPCPackageVersionMap, IRPCTransport } from "./transport.js";
|
|
9
|
+
import { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCEntityId, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPackagePayload, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCRequests, IRPCResponse, IRPCReturnOf, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig } from "./types.js";
|
|
10
10
|
import { IRPCCrudDriver, IRPCDriver } from "./driver.js";
|
|
11
11
|
import { IRPCAdapter, IRPCCrudAdapter } from "./adapter.js";
|
|
12
12
|
import { IRPCCacheEntry, IRPCCacher } from "./cache.js";
|
|
@@ -18,4 +18,4 @@ import { IRPCHook, IRPCRouter } from "./router.js";
|
|
|
18
18
|
import { IRPCStream } from "./stream.js";
|
|
19
19
|
import { IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPC_STORE } from "./store.js";
|
|
20
20
|
import { plan } from "@anchorlib/core";
|
|
21
|
-
export { CALL_ERROR, CRUD_ERROR, CallError, CrudError, DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCAdapter, IRPCArraySchema, IRPCBlob, IRPCBlobPointer, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudAdapter, IRPCCrudDriver, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCDriver, IRPCEntityId, IRPCError, IRPCErrorType, IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFilePointer, IRPCFileQueue, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe, IRPCFunction, IRPCHandler, IRPCHook, IRPCHookArgs, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackage, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketJson, IRPCPacketQueues, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCReader, IRPCRequest, IRPCRequests, IRPCResolver, IRPCResponse, IRPCReturnOf, IRPCRouter, IRPCSchema, IRPCSpec, IRPCSpecHook, IRPCSpecStore, IRPCStatus, IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPCStream, IRPCStreamInit, IRPCStub, IRPCStubStore, IRPCTransport, IRPC_BASE_CONTEXT, IRPC_BLOB_IDENTIFIER, IRPC_ERROR_TYPE, IRPC_FILE_IDENTIFIER, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE, IRPC_STORE_EVENT, PacketStream, RESOLVE_ERROR, RemoteState, ResolveError, STUB_ERROR, StreamCleanup, StreamConstructor, StubError, TRANSPORT_ERROR, TransportConfig, TransportError, createContext, createContextStore, createCredentials, createPackage, credential, decode, decodeBlobs, encode, encodeBlobs, getAbortController, getAbortSignal, getContext, getCredentials, intercept, isBlobPointer, isFilePointer, plan, setContext, setContextProvider, stream, withContext };
|
|
21
|
+
export { CALL_ERROR, CRUD_ERROR, CallError, CrudError, DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCAdapter, IRPCArraySchema, IRPCBlob, IRPCBlobPointer, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudAdapter, IRPCCrudDriver, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCDriver, IRPCEntityId, IRPCError, IRPCErrorType, IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFilePointer, IRPCFileQueue, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe, IRPCFunction, IRPCHandler, IRPCHook, IRPCHookArgs, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackage, IRPCPackageConfig, IRPCPackageInfo, IRPCPackageMap, IRPCPackagePayload, IRPCPackageVersionMap, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketJson, IRPCPacketQueues, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCReader, IRPCRequest, IRPCRequests, IRPCResolver, IRPCResponse, IRPCReturnOf, IRPCRouter, IRPCSchema, IRPCSpec, IRPCSpecHook, IRPCSpecStore, IRPCStatus, IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPCStream, IRPCStreamInit, IRPCStub, IRPCStubStore, IRPCTransport, IRPC_BASE_CONTEXT, IRPC_BLOB_IDENTIFIER, IRPC_ERROR_TYPE, IRPC_FILE_IDENTIFIER, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE, IRPC_STORE_EVENT, PacketStream, RESOLVE_ERROR, RemoteState, ResolveError, STUB_ERROR, StreamCleanup, StreamConstructor, StubError, TRANSPORT_ERROR, TransportConfig, TransportError, createContext, createContextStore, createCredentials, createPackage, credential, decode, decodeBlobs, encode, encodeBlobs, getAbortController, getAbortSignal, getContext, getCredentials, intercept, isBlobPointer, isFilePointer, plan, setContext, setContextProvider, stream, withContext };
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { createContext, createContextStore, getAbortController, getAbortSignal,
|
|
|
6
6
|
import { RemoteState, stream } from "./state.js";
|
|
7
7
|
import { IRPCReader } from "./reader.js";
|
|
8
8
|
import { IRPCTransport } from "./transport.js";
|
|
9
|
-
import { IRPCPackage, createPackage, intercept } from "./
|
|
9
|
+
import { IRPCPackage, createPackage, intercept } from "./package.js";
|
|
10
10
|
import { IRPCRouter } from "./router.js";
|
|
11
11
|
import { IRPCBlob, IRPCFile, IRPCFileStream } from "./file.js";
|
|
12
12
|
import { IRPC_BLOB_IDENTIFIER, IRPC_FILE_IDENTIFIER, decode, decodeBlobs, encode, encodeBlobs, isBlobPointer, isFilePointer } from "./packet.js";
|
|
@@ -3,7 +3,7 @@ import { IRPCReader } from "./reader.js";
|
|
|
3
3
|
import { IRPCTransport } from "./transport.js";
|
|
4
4
|
import { IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDeclareConfig, IRPCDeclareInit, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInputs, IRPCObject, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCRequest, IRPCReturnOf, IRPCSpec, IRPCStub } from "./types.js";
|
|
5
5
|
|
|
6
|
-
//#region src/
|
|
6
|
+
//#region src/package.d.ts
|
|
7
7
|
type IRPCHookArgs<F$1> = F$1 extends ((...args: infer A) => unknown) ? {
|
|
8
8
|
name: string;
|
|
9
9
|
args: A;
|
|
@@ -8,7 +8,7 @@ import { IRPCTransport } from "./transport.js";
|
|
|
8
8
|
import { IRPC_STORE } from "./store.js";
|
|
9
9
|
import { anchor, createObserver, isBrowser, microtask, onCleanup, replay, uuid } from "@anchorlib/core";
|
|
10
10
|
|
|
11
|
-
//#region src/
|
|
11
|
+
//#region src/package.ts
|
|
12
12
|
const DEFAULT_TIMEOUT = 2e4;
|
|
13
13
|
const NAME_CONSTRAINT = /^[a-zA-Z0-9\-_]+$/;
|
|
14
14
|
const VERSION_CONSTRAINT = /^[0-9]+\.[0-9]+\.[0-9]+$/;
|
|
@@ -88,6 +88,10 @@ var IRPCPackage = class {
|
|
|
88
88
|
if (this.specs.has($options.name)) throw StubError.duplicate($options.name);
|
|
89
89
|
if ($options.init && !$options.seed) $options.seed = $options.init;
|
|
90
90
|
const spec = {
|
|
91
|
+
package: {
|
|
92
|
+
name: this.config.name,
|
|
93
|
+
version: this.config.version
|
|
94
|
+
},
|
|
91
95
|
seed: () => void 0,
|
|
92
96
|
...$options
|
|
93
97
|
};
|
|
@@ -301,8 +305,7 @@ var IRPCPackage = class {
|
|
|
301
305
|
*/
|
|
302
306
|
use(transport) {
|
|
303
307
|
if (!(transport instanceof IRPCTransport)) throw TransportError.invalid();
|
|
304
|
-
|
|
305
|
-
transport.modules.add(this);
|
|
308
|
+
transport.register(this);
|
|
306
309
|
this.config.transport = transport;
|
|
307
310
|
return this;
|
|
308
311
|
}
|
package/dist/resolver.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IRPCPackage } from "./
|
|
1
|
+
import { IRPCPackage } from "./package.js";
|
|
2
2
|
import { IRPCDataSchema, IRPCInputs, IRPCOutput, IRPCRequest, IRPCResponse, IRPCSpec } from "./types.js";
|
|
3
3
|
|
|
4
4
|
//#region src/resolver.d.ts
|
|
@@ -11,7 +11,7 @@ import { IRPCDataSchema, IRPCInputs, IRPCOutput, IRPCRequest, IRPCResponse, IRPC
|
|
|
11
11
|
*/
|
|
12
12
|
declare class IRPCResolver {
|
|
13
13
|
req: IRPCRequest;
|
|
14
|
-
module
|
|
14
|
+
module?: IRPCPackage | undefined;
|
|
15
15
|
/**
|
|
16
16
|
* Getter for the specification of the RPC method
|
|
17
17
|
*
|
|
@@ -24,7 +24,7 @@ declare class IRPCResolver {
|
|
|
24
24
|
* @param req - The IRPC request object containing id, name and arguments
|
|
25
25
|
* @param module - The IRPC package module that contains the method to be executed
|
|
26
26
|
*/
|
|
27
|
-
constructor(req: IRPCRequest, module
|
|
27
|
+
constructor(req: IRPCRequest, module?: IRPCPackage | undefined);
|
|
28
28
|
/**
|
|
29
29
|
* Resolves an IRPC request
|
|
30
30
|
*
|
|
@@ -44,6 +44,7 @@ declare class IRPCResolver {
|
|
|
44
44
|
forward({
|
|
45
45
|
id,
|
|
46
46
|
name,
|
|
47
|
+
package: pkg,
|
|
47
48
|
args
|
|
48
49
|
}: IRPCRequest, schema?: IRPCOutput): Promise<IRPCResponse>;
|
|
49
50
|
}
|
package/dist/resolver.js
CHANGED
|
@@ -15,7 +15,7 @@ var IRPCResolver = class {
|
|
|
15
15
|
* Retrieves the specification of the RPC method from the module based on the request
|
|
16
16
|
*/
|
|
17
17
|
get spec() {
|
|
18
|
-
return this.module
|
|
18
|
+
return this.module?.get(this.req);
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Creates a new IRPCResolver instance
|
|
@@ -36,7 +36,7 @@ var IRPCResolver = class {
|
|
|
36
36
|
* @returns A promise that resolves to an IRPC response with either the result or an error
|
|
37
37
|
*/
|
|
38
38
|
async resolve() {
|
|
39
|
-
const { id, name, args } = this.req;
|
|
39
|
+
const { id, name, package: pkg, args } = this.req;
|
|
40
40
|
if (!this.spec) return {
|
|
41
41
|
id,
|
|
42
42
|
name,
|
|
@@ -52,6 +52,7 @@ var IRPCResolver = class {
|
|
|
52
52
|
return this.forward({
|
|
53
53
|
id,
|
|
54
54
|
name,
|
|
55
|
+
package: pkg,
|
|
55
56
|
args: inputs.data
|
|
56
57
|
}, schema?.output);
|
|
57
58
|
}
|
|
@@ -62,16 +63,18 @@ var IRPCResolver = class {
|
|
|
62
63
|
* @param schema - Optional output schema for result validation
|
|
63
64
|
* @returns A promise that resolves to an IRPC response with the result or an error
|
|
64
65
|
*/
|
|
65
|
-
async forward({ id, name, args }, schema) {
|
|
66
|
+
async forward({ id, name, package: pkg, args }, schema) {
|
|
66
67
|
try {
|
|
67
68
|
await this.module.resolveHooks({
|
|
68
69
|
id,
|
|
69
70
|
name,
|
|
71
|
+
package: pkg,
|
|
70
72
|
args
|
|
71
73
|
});
|
|
72
74
|
const result = this.module.resolve({
|
|
73
75
|
id,
|
|
74
76
|
name,
|
|
77
|
+
package: pkg,
|
|
75
78
|
args
|
|
76
79
|
});
|
|
77
80
|
if (result instanceof RemoteState) {
|
package/dist/router.d.ts
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
import { IRPCPackage } from "./
|
|
1
|
+
import { IRPCPackage } from "./package.js";
|
|
2
2
|
import { IRPCTransport } from "./transport.js";
|
|
3
3
|
import { IRPCPacketError, IRPCRequest } from "./types.js";
|
|
4
4
|
|
|
5
5
|
//#region src/router.d.ts
|
|
6
6
|
type IRPCHook = () => void | Promise<void>;
|
|
7
7
|
declare class IRPCRouter {
|
|
8
|
-
module: IRPCPackage;
|
|
9
|
-
transport: IRPCTransport;
|
|
10
8
|
/** Array of middleware functions to be executed */
|
|
11
9
|
hooks: IRPCHook[];
|
|
10
|
+
transport: IRPCTransport;
|
|
11
|
+
get packages(): Set<IRPCPackage<"id">>;
|
|
12
12
|
/**
|
|
13
13
|
* Creates a new Router instance
|
|
14
|
-
* @param
|
|
15
|
-
|
|
14
|
+
* @param transport - The transport mechanism to use for resolving requests.
|
|
15
|
+
*/
|
|
16
|
+
constructor(transport: IRPCTransport);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new Router instance
|
|
19
|
+
* @param {IRPCPackage} module - The IRPC package module to resolve requests against.
|
|
20
|
+
* @param {IRPCTransport} transport - The transport mechanism to use for resolving requests.
|
|
16
21
|
*/
|
|
17
22
|
constructor(module: IRPCPackage, transport: IRPCTransport);
|
|
23
|
+
/**
|
|
24
|
+
* Returns the IRPC package module associated with the router for a given request.
|
|
25
|
+
* @param req - The IRPC request to resolve.
|
|
26
|
+
* @returns The IRPC package module associated with the router for the given request or undefined.
|
|
27
|
+
*/
|
|
28
|
+
packageOf(req: IRPCRequest): IRPCPackage<"id"> | undefined;
|
|
18
29
|
/**
|
|
19
30
|
* Adds a hook function to the router
|
|
20
31
|
* @param hook - The hook function to add
|
package/dist/router.js
CHANGED
|
@@ -1,23 +1,39 @@
|
|
|
1
1
|
import { HookError } from "./error.js";
|
|
2
2
|
import { IRPC_BASE_CONTEXT, IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
|
|
3
3
|
import { createContextStore, withContext } from "./context.js";
|
|
4
|
+
import { IRPCTransport } from "./transport.js";
|
|
4
5
|
import { IRPC_STORE } from "./store.js";
|
|
5
6
|
|
|
6
7
|
//#region src/router.ts
|
|
7
8
|
var IRPCRouter = class {
|
|
8
9
|
/** Array of middleware functions to be executed */
|
|
9
10
|
hooks = [];
|
|
11
|
+
transport;
|
|
12
|
+
get packages() {
|
|
13
|
+
return this.transport.packages;
|
|
14
|
+
}
|
|
10
15
|
/**
|
|
11
16
|
* Creates a new Router instance
|
|
12
17
|
* @param {IRPCPackage} module - The IRPC package module to resolve requests against
|
|
13
18
|
* @param {IRPCTransport} transport - The transport mechanism to use for resolving requests
|
|
14
19
|
*/
|
|
15
20
|
constructor(module, transport) {
|
|
16
|
-
this.
|
|
17
|
-
this.transport = transport;
|
|
21
|
+
if (module instanceof IRPCTransport) this.transport = module;
|
|
22
|
+
else this.transport = transport;
|
|
18
23
|
IRPC_STORE.route(this);
|
|
19
24
|
}
|
|
20
25
|
/**
|
|
26
|
+
* Returns the IRPC package module associated with the router for a given request.
|
|
27
|
+
* @param req - The IRPC request to resolve.
|
|
28
|
+
* @returns The IRPC package module associated with the router for the given request or undefined.
|
|
29
|
+
*/
|
|
30
|
+
packageOf(req) {
|
|
31
|
+
if (req.package) {
|
|
32
|
+
for (const pkg of this.packages) if (pkg.config.name === req.package.name && pkg.config.version === req.package.version) return pkg;
|
|
33
|
+
}
|
|
34
|
+
for (const pkg of this.packages) if (pkg.get(req.name)) return pkg;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
21
37
|
* Adds a hook function to the router
|
|
22
38
|
* @param hook - The hook function to add
|
|
23
39
|
* @returns The current Router instance for chaining
|
package/dist/store.d.ts
CHANGED
package/dist/store.js
CHANGED
package/dist/transport.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { IRPCReader } from "./reader.js";
|
|
2
2
|
import { IRPCCall } from "./call.js";
|
|
3
|
-
import { IRPCPackage } from "./
|
|
3
|
+
import { IRPCPackage } from "./package.js";
|
|
4
4
|
import { IRPCCallConfig, IRPCCredentials, IRPCCredentialsFactory, IRPCData, IRPCInputs, IRPCOutput, IRPCSpec, TransportConfig } from "./types.js";
|
|
5
5
|
|
|
6
6
|
//#region src/transport.d.ts
|
|
7
|
-
|
|
7
|
+
type IRPCPackageMap = Map<string, IRPCPackageVersionMap>;
|
|
8
|
+
type IRPCPackageVersionMap = Map<string, IRPCPackage>;
|
|
8
9
|
/**
|
|
9
10
|
* IRPCTransport is responsible for managing and dispatching RPC calls.
|
|
10
11
|
* It handles queuing, debouncing, and timeout management for RPC requests.
|
|
@@ -12,7 +13,9 @@ import { IRPCCallConfig, IRPCCredentials, IRPCCredentialsFactory, IRPCData, IRPC
|
|
|
12
13
|
declare class IRPCTransport {
|
|
13
14
|
#private;
|
|
14
15
|
config?: TransportConfig | undefined;
|
|
15
|
-
|
|
16
|
+
/** A set of packages registered with the transport. */
|
|
17
|
+
registry: IRPCPackageMap;
|
|
18
|
+
packages: Set<IRPCPackage<"id">>;
|
|
16
19
|
/**
|
|
17
20
|
* A set of pending RPC calls that are queued for execution.
|
|
18
21
|
*/
|
|
@@ -49,6 +52,18 @@ declare class IRPCTransport {
|
|
|
49
52
|
* @param call - The RPC call to cancel.
|
|
50
53
|
*/
|
|
51
54
|
close(call: IRPCCall): void;
|
|
55
|
+
/**
|
|
56
|
+
* Registers an RPC package with the transport.
|
|
57
|
+
* @param pkg - The package to register.
|
|
58
|
+
* @returns The transport instance.
|
|
59
|
+
*/
|
|
60
|
+
register(pkg: IRPCPackage): this;
|
|
61
|
+
/**
|
|
62
|
+
* Unregisters an RPC package from the transport.
|
|
63
|
+
* @param pkg - The package to unregister.
|
|
64
|
+
* @returns The transport instance.
|
|
65
|
+
*/
|
|
66
|
+
unregister(pkg: IRPCPackage): this;
|
|
52
67
|
/**
|
|
53
68
|
* Dispatches RPC calls over the transport. Subclasses must override this
|
|
54
69
|
* to provide the actual transport mechanism (HTTP, WebSocket, etc.).
|
|
@@ -69,4 +84,4 @@ declare class IRPCTransport {
|
|
|
69
84
|
protected dispatch(calls: IRPCCall[], standalone?: boolean): Promise<void>;
|
|
70
85
|
}
|
|
71
86
|
//#endregion
|
|
72
|
-
export { IRPCTransport };
|
|
87
|
+
export { IRPCPackageMap, IRPCPackageVersionMap, IRPCTransport };
|
package/dist/transport.js
CHANGED
|
@@ -12,7 +12,9 @@ import { onCleanup, uuid } from "@anchorlib/core";
|
|
|
12
12
|
*/
|
|
13
13
|
var IRPCTransport = class {
|
|
14
14
|
#credentialFactory;
|
|
15
|
-
|
|
15
|
+
/** A set of packages registered with the transport. */
|
|
16
|
+
registry = /* @__PURE__ */ new Map();
|
|
17
|
+
packages = /* @__PURE__ */ new Set();
|
|
16
18
|
/**
|
|
17
19
|
* A set of pending RPC calls that are queued for execution.
|
|
18
20
|
*/
|
|
@@ -43,7 +45,8 @@ var IRPCTransport = class {
|
|
|
43
45
|
call(spec, args, config, reader = new IRPCReader(uuid())) {
|
|
44
46
|
const payload = {
|
|
45
47
|
name: spec.name,
|
|
46
|
-
args
|
|
48
|
+
args,
|
|
49
|
+
package: spec.package
|
|
47
50
|
};
|
|
48
51
|
const { timeout, maxRetries, retryMode, retryDelay } = {
|
|
49
52
|
...this.config,
|
|
@@ -106,6 +109,30 @@ var IRPCTransport = class {
|
|
|
106
109
|
console.log("[irpc] Closing call", call);
|
|
107
110
|
}
|
|
108
111
|
/**
|
|
112
|
+
* Registers an RPC package with the transport.
|
|
113
|
+
* @param pkg - The package to register.
|
|
114
|
+
* @returns The transport instance.
|
|
115
|
+
*/
|
|
116
|
+
register(pkg) {
|
|
117
|
+
if (!this.registry.has(pkg.config.name)) this.registry.set(pkg.config.name, /* @__PURE__ */ new Map());
|
|
118
|
+
this.registry.get(pkg.config.name).set(pkg.config.version, pkg);
|
|
119
|
+
this.packages.add(pkg);
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Unregisters an RPC package from the transport.
|
|
124
|
+
* @param pkg - The package to unregister.
|
|
125
|
+
* @returns The transport instance.
|
|
126
|
+
*/
|
|
127
|
+
unregister(pkg) {
|
|
128
|
+
this.packages.delete(pkg);
|
|
129
|
+
const pkgMap = this.registry.get(pkg.config.name);
|
|
130
|
+
if (!pkgMap) return this;
|
|
131
|
+
pkgMap.delete(pkg.config.version);
|
|
132
|
+
if (!pkgMap.size) this.registry.delete(pkg.config.name);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
109
136
|
* Dispatches RPC calls over the transport. Subclasses must override this
|
|
110
137
|
* to provide the actual transport mechanism (HTTP, WebSocket, etc.).
|
|
111
138
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -172,6 +172,8 @@ type IRPCPayload = {
|
|
|
172
172
|
name: string;
|
|
173
173
|
/** The arguments to pass to the RPC function */
|
|
174
174
|
args: IRPCData[];
|
|
175
|
+
/** The package information */
|
|
176
|
+
package: IRPCPackagePayload;
|
|
175
177
|
};
|
|
176
178
|
/**
|
|
177
179
|
* Defines the schema for input and output validation of an RPC function.
|
|
@@ -320,9 +322,17 @@ type IRPCSpec<I extends IRPCInputs, O$1 extends IRPCOutput> = IRPCInit<IRPCData,
|
|
|
320
322
|
ttl?: number;
|
|
321
323
|
/** Whether to stream the result of the RPC call */
|
|
322
324
|
stream?: boolean;
|
|
325
|
+
/** The package that the RPC function belongs to */
|
|
326
|
+
package: IRPCPackagePayload;
|
|
323
327
|
/** The actual handler function that implements the RPC */
|
|
324
328
|
handler: IRPCHandler;
|
|
325
329
|
};
|
|
330
|
+
type IRPCPackagePayload = {
|
|
331
|
+
/** Name of the package */
|
|
332
|
+
name: string;
|
|
333
|
+
/** Version of the package */
|
|
334
|
+
version: string;
|
|
335
|
+
};
|
|
326
336
|
/**
|
|
327
337
|
* Represents an incoming RPC request.
|
|
328
338
|
*/
|
|
@@ -333,6 +343,9 @@ type IRPCRequest = {
|
|
|
333
343
|
name: string;
|
|
334
344
|
/** Arguments for the RPC function */
|
|
335
345
|
args: unknown[];
|
|
346
|
+
/** The package that the RPC function belongs to */
|
|
347
|
+
package: IRPCPackagePayload;
|
|
348
|
+
/** Optional file payload for the call */
|
|
336
349
|
files?: IRPCFilePointer[];
|
|
337
350
|
};
|
|
338
351
|
type IRPCCredentials = Iterable<[string, AsyncValue]>;
|
|
@@ -411,4 +424,4 @@ type StreamCleanup = () => void;
|
|
|
411
424
|
*/
|
|
412
425
|
type StreamConstructor<T> = (state: IRPCReadable<T>, resolve: (value?: T) => void, reject: (error: Error) => void) => StreamCleanup | void | Promise<StreamCleanup | void>;
|
|
413
426
|
//#endregion
|
|
414
|
-
export { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCEntityId, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCRequests, IRPCResponse, IRPCReturnOf, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig };
|
|
427
|
+
export { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCCrudField, IRPCCrudMeta, IRPCCrudMethod, IRPCCrudOptions, IRPCCrudStubs, IRPCData, IRPCDataSchema, IRPCDeclareConfig, IRPCDeclareInit, IRPCDefined, IRPCEntityId, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInit, IRPCInputs, IRPCMeta, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPackagePayload, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCRequests, IRPCResponse, IRPCReturnOf, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@irpclib/irpc",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.8",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"module": "./dist/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"zod": "^4.1.5"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@anchorlib/core": "1.2.
|
|
39
|
+
"@anchorlib/core": "1.2.8",
|
|
40
40
|
"typescript": "^5.9.3"
|
|
41
41
|
},
|
|
42
42
|
"optionalDependencies": {
|