@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/call.d.ts +7 -4
- package/dist/call.js +25 -7
- package/dist/context.d.ts +6 -16
- package/dist/context.js +11 -22
- package/dist/enum.d.ts +19 -12
- package/dist/enum.js +20 -13
- package/dist/error.d.ts +2 -0
- package/dist/error.js +2 -0
- package/dist/file.d.ts +37 -0
- package/dist/file.js +86 -0
- package/dist/index.d.ts +11 -6
- package/dist/index.js +11 -6
- package/dist/module.d.ts +41 -9
- package/dist/module.js +142 -12
- package/dist/packet.d.ts +32 -0
- package/dist/packet.js +100 -0
- package/dist/reader.d.ts +16 -3
- package/dist/reader.js +22 -4
- package/dist/resolver.js +5 -0
- package/dist/router.d.ts +42 -0
- package/dist/router.js +59 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +15 -0
- package/dist/state.d.ts +13 -17
- package/dist/state.js +77 -23
- package/dist/store.d.ts +44 -0
- package/dist/store.js +76 -0
- package/dist/stream.d.ts +13 -2
- package/dist/stream.js +78 -15
- package/dist/transport.d.ts +8 -1
- package/dist/transport.js +28 -5
- package/dist/types.d.ts +91 -16
- package/package.json +7 -3
- package/readme.md +8 -3
- package/dist/uuid.d.ts +0 -21
- package/dist/uuid.js +0 -45
package/dist/call.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare class IRPCCall {
|
|
|
16
16
|
/**
|
|
17
17
|
* Unique identifier for this RPC call, generated using shortId().
|
|
18
18
|
*/
|
|
19
|
-
id: string;
|
|
19
|
+
readonly id: string;
|
|
20
20
|
/**
|
|
21
21
|
* The status of the RPC call, indicating whether it is pending, resolved, or rejected.
|
|
22
22
|
*/
|
|
@@ -39,17 +39,19 @@ declare class IRPCCall {
|
|
|
39
39
|
*/
|
|
40
40
|
value?: unknown;
|
|
41
41
|
error?: Error;
|
|
42
|
-
private
|
|
42
|
+
private timerId?;
|
|
43
|
+
private retryId?;
|
|
43
44
|
private retries;
|
|
44
45
|
private retryReasons;
|
|
45
46
|
reader: IRPCReader<IRPCData>;
|
|
46
47
|
/**
|
|
47
48
|
* Creates a new IRPCCall instance.
|
|
48
|
-
* @param
|
|
49
|
+
* @param reader - The reader associated with this call.
|
|
50
|
+
* @param transport - The transport used for dispatching calls.
|
|
49
51
|
* @param payload - The RPC payload containing method and parameters
|
|
50
52
|
* @param options - Options for the call, such as timeout, maxRetries, etc.
|
|
51
53
|
*/
|
|
52
|
-
constructor(transport: IRPCTransport, payload: IRPCPayload, options: IRPCCallConfig);
|
|
54
|
+
constructor(transport: IRPCTransport, payload: IRPCPayload, options: IRPCCallConfig, reader?: IRPCReader<IRPCData>);
|
|
53
55
|
enqueue(packet: IRPCPacketStream<IRPCData>): void;
|
|
54
56
|
/**
|
|
55
57
|
* Resolves the RPC call with the provided value.
|
|
@@ -64,6 +66,7 @@ declare class IRPCCall {
|
|
|
64
66
|
* @param retriable - Flag indicating whether to retry the call
|
|
65
67
|
*/
|
|
66
68
|
reject(reason?: Error, retriable?: boolean): void;
|
|
69
|
+
close(): void;
|
|
67
70
|
}
|
|
68
71
|
//#endregion
|
|
69
72
|
export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall };
|
package/dist/call.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
|
|
2
2
|
import { ERROR_CODE, ERROR_MESSAGE } from "./error.js";
|
|
3
3
|
import { IRPCReader } from "./reader.js";
|
|
4
|
-
import {
|
|
4
|
+
import { IRPC_STORE } from "./store.js";
|
|
5
|
+
import { uuid } from "@anchorlib/core";
|
|
5
6
|
|
|
6
7
|
//#region src/call.ts
|
|
7
8
|
const DEFAULT_RETRY_MODE = "exponential";
|
|
@@ -14,7 +15,7 @@ var IRPCCall = class {
|
|
|
14
15
|
/**
|
|
15
16
|
* Unique identifier for this RPC call, generated using shortId().
|
|
16
17
|
*/
|
|
17
|
-
id
|
|
18
|
+
id;
|
|
18
19
|
/**
|
|
19
20
|
* The status of the RPC call, indicating whether it is pending, resolved, or rejected.
|
|
20
21
|
*/
|
|
@@ -38,16 +39,18 @@ var IRPCCall = class {
|
|
|
38
39
|
value;
|
|
39
40
|
error;
|
|
40
41
|
timerId;
|
|
42
|
+
retryId;
|
|
41
43
|
retries = 0;
|
|
42
44
|
retryReasons = /* @__PURE__ */ new Set();
|
|
43
45
|
reader;
|
|
44
46
|
/**
|
|
45
47
|
* Creates a new IRPCCall instance.
|
|
46
|
-
* @param
|
|
48
|
+
* @param reader - The reader associated with this call.
|
|
49
|
+
* @param transport - The transport used for dispatching calls.
|
|
47
50
|
* @param payload - The RPC payload containing method and parameters
|
|
48
51
|
* @param options - Options for the call, such as timeout, maxRetries, etc.
|
|
49
52
|
*/
|
|
50
|
-
constructor(transport, payload, options) {
|
|
53
|
+
constructor(transport, payload, options, reader) {
|
|
51
54
|
this.transport = transport;
|
|
52
55
|
this.payload = payload;
|
|
53
56
|
this.options = options;
|
|
@@ -63,11 +66,15 @@ var IRPCCall = class {
|
|
|
63
66
|
},
|
|
64
67
|
createdAt: Date.now()
|
|
65
68
|
});
|
|
69
|
+
clearTimeout(this.retryId);
|
|
66
70
|
this.reject(new Error(ERROR_MESSAGE[ERROR_CODE.TIMEOUT]), false);
|
|
67
71
|
}, options.timeout);
|
|
68
|
-
this.reader = new IRPCReader(
|
|
72
|
+
this.reader = reader ?? new IRPCReader(uuid(), options?.init?.());
|
|
73
|
+
this.reader.onClose = () => this.close();
|
|
74
|
+
this.id = this.reader.id;
|
|
69
75
|
}
|
|
70
76
|
enqueue(packet) {
|
|
77
|
+
if (this.resolved) return;
|
|
71
78
|
this.reader.push(packet);
|
|
72
79
|
if (this.reader.status === IRPC_STATUS.SUCCESS) this.resolve(this.reader.data);
|
|
73
80
|
else if (this.reader.status === IRPC_STATUS.ERROR) this.reject(this.reader.error);
|
|
@@ -83,6 +90,7 @@ var IRPCCall = class {
|
|
|
83
90
|
this.status = IRPC_STATUS.SUCCESS;
|
|
84
91
|
this.resolved = true;
|
|
85
92
|
this.finishedAt = Date.now();
|
|
93
|
+
this.retryReasons.clear();
|
|
86
94
|
clearTimeout(this.timerId);
|
|
87
95
|
}
|
|
88
96
|
/**
|
|
@@ -97,12 +105,16 @@ var IRPCCall = class {
|
|
|
97
105
|
if (maxRetries && retriable) {
|
|
98
106
|
if (reason) this.retryReasons.add(reason);
|
|
99
107
|
if (this.retries >= maxRetries) {
|
|
100
|
-
|
|
108
|
+
IRPC_STORE.error(/* @__PURE__ */ new Error(`${ERROR_MESSAGE[ERROR_CODE.CALL_MAX_RETRIES_REACHED]}: ${Array.from(this.retryReasons).map((r) => r.message).join(", ")}`), [{
|
|
109
|
+
id: this.id,
|
|
110
|
+
name: this.payload.name
|
|
111
|
+
}]);
|
|
101
112
|
this.reject(reason, false);
|
|
113
|
+
this.retryReasons.clear();
|
|
102
114
|
return;
|
|
103
115
|
}
|
|
104
116
|
const delay = retryMode === "linear" ? retryDelay : retryDelay * 2 ** this.retries;
|
|
105
|
-
setTimeout(() => {
|
|
117
|
+
this.retryId = setTimeout(() => {
|
|
106
118
|
this.retries++;
|
|
107
119
|
this.transport.schedule(this);
|
|
108
120
|
}, delay);
|
|
@@ -111,9 +123,15 @@ var IRPCCall = class {
|
|
|
111
123
|
this.status = IRPC_STATUS.ERROR;
|
|
112
124
|
this.resolved = true;
|
|
113
125
|
this.finishedAt = Date.now();
|
|
126
|
+
this.retryReasons.clear();
|
|
114
127
|
clearTimeout(this.timerId);
|
|
115
128
|
}
|
|
116
129
|
}
|
|
130
|
+
close() {
|
|
131
|
+
if (this.resolved) return;
|
|
132
|
+
this.transport.close(this);
|
|
133
|
+
this.resolve(this.reader.data);
|
|
134
|
+
}
|
|
117
135
|
};
|
|
118
136
|
|
|
119
137
|
//#endregion
|
package/dist/context.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IRPCContext, IRPCContextProvider } from "./types.js";
|
|
2
|
+
import { AsyncStore, getContext, setContext } from "@anchorlib/core";
|
|
2
3
|
|
|
3
4
|
//#region src/context.d.ts
|
|
4
5
|
|
|
@@ -16,25 +17,14 @@ declare function setContextProvider(store: IRPCContextProvider): void;
|
|
|
16
17
|
* @param fn - The function to execute
|
|
17
18
|
* @returns The result of the executed function
|
|
18
19
|
*/
|
|
19
|
-
declare function withContext<R>(ctx: IRPCContext<string, unknown>, fn: () => R): R
|
|
20
|
+
declare function withContext<R>(ctx: IRPCContext<string | symbol, unknown>, fn: () => R): Promise<R>;
|
|
20
21
|
/**
|
|
21
22
|
* Creates a new context map with optional initial values.
|
|
22
23
|
* @param init - Optional initial key-value pairs for the context
|
|
23
24
|
* @returns A new Map instance representing the context
|
|
24
25
|
*/
|
|
25
|
-
declare function createContext<K extends string, V>(init?: [K, V][]):
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* @param key - The key to set in the context
|
|
29
|
-
* @param value - The value to associate with the key
|
|
30
|
-
*/
|
|
31
|
-
declare function setContext<V, K extends string = string>(key: K, value: V): void;
|
|
32
|
-
/**
|
|
33
|
-
* Gets a value from the current context.
|
|
34
|
-
* @param key - The key to retrieve from the context
|
|
35
|
-
* @param fallback - Optional fallback value if the key is not found
|
|
36
|
-
* @returns The value associated with the key, or the fallback value if not found
|
|
37
|
-
*/
|
|
38
|
-
declare function getContext<V, K extends string = string>(key: K, fallback?: V): V | undefined;
|
|
26
|
+
declare function createContext<K extends string | symbol, V>(init?: [K, V][]): AsyncStore;
|
|
27
|
+
declare function getAbortSignal(): AbortSignal | undefined;
|
|
28
|
+
declare function getAbortController(): AbortController | undefined;
|
|
39
29
|
//#endregion
|
|
40
|
-
export { createContext, getContext, setContext, setContextProvider, withContext };
|
|
30
|
+
export { createContext, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext };
|
package/dist/context.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import { IRPC_BASE_CONTEXT } from "./enum.js";
|
|
2
|
+
import { AsyncStore, getContext, setAsyncScope, setContext, withIsolation } from "@anchorlib/core";
|
|
3
|
+
|
|
1
4
|
//#region src/context.ts
|
|
2
|
-
let currentStore;
|
|
3
5
|
/**
|
|
4
6
|
* Sets the global context store for the IRPC system.
|
|
5
7
|
* This store is used to manage context data across requests.
|
|
6
8
|
* @param store - The context store implementation to use
|
|
7
9
|
*/
|
|
8
10
|
function setContextProvider(store) {
|
|
9
|
-
|
|
11
|
+
setAsyncScope(store);
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* Executes a function with the provided context.
|
|
@@ -17,7 +19,7 @@ function setContextProvider(store) {
|
|
|
17
19
|
* @returns The result of the executed function
|
|
18
20
|
*/
|
|
19
21
|
function withContext(ctx, fn) {
|
|
20
|
-
return
|
|
22
|
+
return withIsolation(fn, true, ctx);
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* Creates a new context map with optional initial values.
|
|
@@ -25,27 +27,14 @@ function withContext(ctx, fn) {
|
|
|
25
27
|
* @returns A new Map instance representing the context
|
|
26
28
|
*/
|
|
27
29
|
function createContext(init) {
|
|
28
|
-
return new
|
|
30
|
+
return new AsyncStore(init);
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* @param key - The key to set in the context
|
|
33
|
-
* @param value - The value to associate with the key
|
|
34
|
-
*/
|
|
35
|
-
function setContext(key, value) {
|
|
36
|
-
(currentStore?.getStore())?.set(key, value);
|
|
32
|
+
function getAbortSignal() {
|
|
33
|
+
return getContext(IRPC_BASE_CONTEXT.ABORT_SIGNAL);
|
|
37
34
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
* @param key - The key to retrieve from the context
|
|
41
|
-
* @param fallback - Optional fallback value if the key is not found
|
|
42
|
-
* @returns The value associated with the key, or the fallback value if not found
|
|
43
|
-
*/
|
|
44
|
-
function getContext(key, fallback) {
|
|
45
|
-
const result = (currentStore?.getStore())?.get(key);
|
|
46
|
-
if (typeof result === "undefined" && typeof fallback !== "undefined") return fallback;
|
|
47
|
-
return result;
|
|
35
|
+
function getAbortController() {
|
|
36
|
+
return getContext(IRPC_BASE_CONTEXT.ABORT_CONTROLLER);
|
|
48
37
|
}
|
|
49
38
|
|
|
50
39
|
//#endregion
|
|
51
|
-
export { createContext, getContext, setContext, setContextProvider, withContext };
|
|
40
|
+
export { createContext, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext };
|
package/dist/enum.d.ts
CHANGED
|
@@ -14,22 +14,29 @@ declare const IRPC_DATA_TYPE: {
|
|
|
14
14
|
readonly WRITABLE: "writable";
|
|
15
15
|
readonly PRIMITIVE: "primitive";
|
|
16
16
|
};
|
|
17
|
-
declare const IRPC_EVENT_TYPE: {
|
|
18
|
-
readonly OBJECT_SET: "set";
|
|
19
|
-
readonly OBJECT_DELETE: "delete";
|
|
20
|
-
readonly ARRAY_PUSH: "push";
|
|
21
|
-
readonly ARRAY_POP: "pop";
|
|
22
|
-
readonly ARRAY_SHIFT: "shift";
|
|
23
|
-
readonly ARRAY_UNSHIFT: "unshift";
|
|
24
|
-
readonly ARRAY_SPLICE: "splice";
|
|
25
|
-
readonly ARRAY_REVERSE: "reverse";
|
|
26
|
-
readonly ARRAY_COPY_WITHIN: "copyWithin";
|
|
27
|
-
};
|
|
28
17
|
declare const IRPC_STATUS: {
|
|
29
18
|
readonly IDLE: "idle";
|
|
30
19
|
readonly ERROR: "error";
|
|
31
20
|
readonly PENDING: "pending";
|
|
32
21
|
readonly SUCCESS: "success";
|
|
22
|
+
readonly ABORTED: "aborted";
|
|
23
|
+
};
|
|
24
|
+
declare const IRPC_STORE_EVENT: {
|
|
25
|
+
readonly ROUTE: "route";
|
|
26
|
+
readonly QUEUE: "queue";
|
|
27
|
+
readonly DEQUEUE: "dequeue";
|
|
28
|
+
readonly REGISTER: "register";
|
|
29
|
+
readonly ERROR: "error";
|
|
30
|
+
};
|
|
31
|
+
declare const IRPC_BASE_CONTEXT: {
|
|
32
|
+
readonly ABORT_SIGNAL: symbol;
|
|
33
|
+
readonly ABORT_CONTROLLER: symbol;
|
|
34
|
+
};
|
|
35
|
+
declare const IRPC_FILE_STATUS: {
|
|
36
|
+
readonly IDLE: "idle";
|
|
37
|
+
readonly PENDING: "pending";
|
|
38
|
+
readonly SUCCESS: "success";
|
|
39
|
+
readonly ERROR: "error";
|
|
33
40
|
};
|
|
34
41
|
//#endregion
|
|
35
|
-
export { IRPC_DATA_TYPE,
|
|
42
|
+
export { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
|
package/dist/enum.js
CHANGED
|
@@ -14,23 +14,30 @@ const IRPC_DATA_TYPE = {
|
|
|
14
14
|
WRITABLE: "writable",
|
|
15
15
|
PRIMITIVE: "primitive"
|
|
16
16
|
};
|
|
17
|
-
const IRPC_EVENT_TYPE = {
|
|
18
|
-
OBJECT_SET: "set",
|
|
19
|
-
OBJECT_DELETE: "delete",
|
|
20
|
-
ARRAY_PUSH: "push",
|
|
21
|
-
ARRAY_POP: "pop",
|
|
22
|
-
ARRAY_SHIFT: "shift",
|
|
23
|
-
ARRAY_UNSHIFT: "unshift",
|
|
24
|
-
ARRAY_SPLICE: "splice",
|
|
25
|
-
ARRAY_REVERSE: "reverse",
|
|
26
|
-
ARRAY_COPY_WITHIN: "copyWithin"
|
|
27
|
-
};
|
|
28
17
|
const IRPC_STATUS = {
|
|
29
18
|
IDLE: "idle",
|
|
30
19
|
ERROR: "error",
|
|
31
20
|
PENDING: "pending",
|
|
32
|
-
SUCCESS: "success"
|
|
21
|
+
SUCCESS: "success",
|
|
22
|
+
ABORTED: "aborted"
|
|
23
|
+
};
|
|
24
|
+
const IRPC_STORE_EVENT = {
|
|
25
|
+
ROUTE: "route",
|
|
26
|
+
QUEUE: "queue",
|
|
27
|
+
DEQUEUE: "dequeue",
|
|
28
|
+
REGISTER: "register",
|
|
29
|
+
ERROR: "error"
|
|
30
|
+
};
|
|
31
|
+
const IRPC_BASE_CONTEXT = {
|
|
32
|
+
ABORT_SIGNAL: Symbol("abort-signal"),
|
|
33
|
+
ABORT_CONTROLLER: Symbol("abort-controller")
|
|
34
|
+
};
|
|
35
|
+
const IRPC_FILE_STATUS = {
|
|
36
|
+
IDLE: "idle",
|
|
37
|
+
PENDING: "pending",
|
|
38
|
+
SUCCESS: "success",
|
|
39
|
+
ERROR: "error"
|
|
33
40
|
};
|
|
34
41
|
|
|
35
42
|
//#endregion
|
|
36
|
-
export { IRPC_DATA_TYPE,
|
|
43
|
+
export { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
|
package/dist/error.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ declare const ERROR_CODE: {
|
|
|
12
12
|
NOT_IMPLEMENTED: string;
|
|
13
13
|
INVALID_HANDLER: string;
|
|
14
14
|
INVALID_OPERATION: string;
|
|
15
|
+
INVALID_HOOK: string;
|
|
15
16
|
TRANSPORT_MISSING: string;
|
|
16
17
|
TRANSPORT_INVALID: string;
|
|
17
18
|
TRANSPORT_NOT_IMPLEMENTED: string;
|
|
@@ -39,6 +40,7 @@ declare const ERROR_MESSAGE: {
|
|
|
39
40
|
[ERROR_CODE.NOT_IMPLEMENTED]: string;
|
|
40
41
|
[ERROR_CODE.INVALID_HANDLER]: string;
|
|
41
42
|
[ERROR_CODE.INVALID_OPERATION]: string;
|
|
43
|
+
[ERROR_CODE.INVALID_HOOK]: string;
|
|
42
44
|
[ERROR_CODE.TRANSPORT_MISSING]: string;
|
|
43
45
|
[ERROR_CODE.TRANSPORT_INVALID]: string;
|
|
44
46
|
[ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: string;
|
package/dist/error.js
CHANGED
|
@@ -12,6 +12,7 @@ const ERROR_CODE = {
|
|
|
12
12
|
NOT_IMPLEMENTED: "not_implemented",
|
|
13
13
|
INVALID_HANDLER: "invalid_handler",
|
|
14
14
|
INVALID_OPERATION: "invalid_operation",
|
|
15
|
+
INVALID_HOOK: "invalid_middleware",
|
|
15
16
|
TRANSPORT_MISSING: "transport_missing",
|
|
16
17
|
TRANSPORT_INVALID: "transport_invalid",
|
|
17
18
|
TRANSPORT_NOT_IMPLEMENTED: "transport_not_implemented",
|
|
@@ -38,6 +39,7 @@ const ERROR_MESSAGE = {
|
|
|
38
39
|
[ERROR_CODE.NOT_IMPLEMENTED]: "IRPC: Not implemented error",
|
|
39
40
|
[ERROR_CODE.INVALID_HANDLER]: "IRPC: Invalid handler error",
|
|
40
41
|
[ERROR_CODE.INVALID_OPERATION]: "IRPC: Invalid operation error",
|
|
42
|
+
[ERROR_CODE.INVALID_HOOK]: "IRPC: Invalid hook error",
|
|
41
43
|
[ERROR_CODE.TRANSPORT_MISSING]: "IRPC: Transport missing error",
|
|
42
44
|
[ERROR_CODE.TRANSPORT_INVALID]: "IRPC: Transport invalid error",
|
|
43
45
|
[ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: "IRPC: Transport not implemented error",
|
package/dist/file.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IRPC_FILE_STATUS } from "./enum.js";
|
|
2
|
+
|
|
3
|
+
//#region src/file.d.ts
|
|
4
|
+
type IRPCFileStatus = (typeof IRPC_FILE_STATUS)[keyof typeof IRPC_FILE_STATUS];
|
|
5
|
+
type IRPCFileState = {
|
|
6
|
+
error?: Error;
|
|
7
|
+
status: IRPCFileStatus;
|
|
8
|
+
downloaded: number;
|
|
9
|
+
};
|
|
10
|
+
type IRPCFileMeta = {
|
|
11
|
+
size: number;
|
|
12
|
+
type: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
};
|
|
15
|
+
type IRPCFilePipe = (chunk: Uint8Array) => void;
|
|
16
|
+
type IRPCFileUnpipe = () => void;
|
|
17
|
+
declare class IRPCFile {
|
|
18
|
+
meta: IRPCFileMeta;
|
|
19
|
+
protected state: IRPCFileState;
|
|
20
|
+
get status(): IRPCFileStatus;
|
|
21
|
+
set status(status: IRPCFileStatus);
|
|
22
|
+
get error(): Error | undefined;
|
|
23
|
+
get downloaded(): number;
|
|
24
|
+
get success(): boolean;
|
|
25
|
+
get completed(): boolean;
|
|
26
|
+
data: Blob;
|
|
27
|
+
constructor(meta: IRPCFileMeta, data?: Blob);
|
|
28
|
+
}
|
|
29
|
+
declare class IRPCFileStream extends IRPCFile {
|
|
30
|
+
private pipes;
|
|
31
|
+
private buffer;
|
|
32
|
+
constructor(meta: IRPCFileMeta);
|
|
33
|
+
write(chunk: Uint8Array): Uint8Array<ArrayBuffer> | null;
|
|
34
|
+
pipe(fn: IRPCFilePipe): IRPCFileUnpipe;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe };
|
package/dist/file.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { IRPC_FILE_STATUS } from "./enum.js";
|
|
2
|
+
import { IRPC_STORE } from "./store.js";
|
|
3
|
+
import { mutable } from "@anchorlib/core";
|
|
4
|
+
|
|
5
|
+
//#region src/file.ts
|
|
6
|
+
var IRPCFile = class {
|
|
7
|
+
state = mutable({
|
|
8
|
+
status: IRPC_FILE_STATUS.PENDING,
|
|
9
|
+
downloaded: 0
|
|
10
|
+
});
|
|
11
|
+
get status() {
|
|
12
|
+
return this.state.status;
|
|
13
|
+
}
|
|
14
|
+
set status(status) {
|
|
15
|
+
this.state.status = status;
|
|
16
|
+
}
|
|
17
|
+
get error() {
|
|
18
|
+
return this.state.error;
|
|
19
|
+
}
|
|
20
|
+
get downloaded() {
|
|
21
|
+
return this.state.downloaded;
|
|
22
|
+
}
|
|
23
|
+
get success() {
|
|
24
|
+
return this.status === IRPC_FILE_STATUS.SUCCESS;
|
|
25
|
+
}
|
|
26
|
+
get completed() {
|
|
27
|
+
return [IRPC_FILE_STATUS.SUCCESS, IRPC_FILE_STATUS.ERROR].includes(this.status);
|
|
28
|
+
}
|
|
29
|
+
data;
|
|
30
|
+
constructor(meta, data) {
|
|
31
|
+
this.meta = meta;
|
|
32
|
+
this.data = data ?? new Blob([], { type: meta.type });
|
|
33
|
+
this.state.status = data ? IRPC_FILE_STATUS.SUCCESS : IRPC_FILE_STATUS.PENDING;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var IRPCFileStream = class extends IRPCFile {
|
|
37
|
+
pipes = /* @__PURE__ */ new Set();
|
|
38
|
+
buffer;
|
|
39
|
+
constructor(meta) {
|
|
40
|
+
super(meta);
|
|
41
|
+
this.data = new Blob([], { type: meta.type });
|
|
42
|
+
this.buffer = new Uint8Array(meta.size);
|
|
43
|
+
}
|
|
44
|
+
write(chunk) {
|
|
45
|
+
if (this.completed) return null;
|
|
46
|
+
try {
|
|
47
|
+
const remaining = this.meta.size - this.state.downloaded;
|
|
48
|
+
const chunkSize = Math.min(chunk.byteLength, remaining);
|
|
49
|
+
const needSlice = chunkSize < chunk.byteLength;
|
|
50
|
+
const nextChunk = needSlice ? chunk.slice(0, chunkSize) : chunk;
|
|
51
|
+
const leftovers = needSlice ? chunk.slice(chunkSize) : null;
|
|
52
|
+
this.buffer?.set(nextChunk, this.state.downloaded);
|
|
53
|
+
this.state.downloaded += chunkSize;
|
|
54
|
+
this.pipes.forEach((fn) => {
|
|
55
|
+
try {
|
|
56
|
+
fn(nextChunk);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
IRPC_STORE.error(error, [this.meta]);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (this.downloaded >= this.meta.size) {
|
|
62
|
+
this.data = new Blob([this.buffer], { type: this.meta.type });
|
|
63
|
+
this.buffer = void 0;
|
|
64
|
+
this.state.status = IRPC_FILE_STATUS.SUCCESS;
|
|
65
|
+
}
|
|
66
|
+
return leftovers;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
IRPC_STORE.error(error, [this.meta]);
|
|
69
|
+
this.state.error = error;
|
|
70
|
+
this.state.status = IRPC_FILE_STATUS.ERROR;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
pipe(fn) {
|
|
75
|
+
this.pipes.add(fn);
|
|
76
|
+
if (this.state.downloaded > 0) try {
|
|
77
|
+
fn(this.buffer?.subarray(0, this.state.downloaded));
|
|
78
|
+
} catch (error) {
|
|
79
|
+
IRPC_STORE.error(error, [this.meta]);
|
|
80
|
+
}
|
|
81
|
+
return () => this.pipes.delete(fn);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { IRPCFile, IRPCFileStream };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { IRPCCacheEntry, IRPCCacher } from "./cache.js";
|
|
2
|
-
import { IRPC_DATA_TYPE,
|
|
2
|
+
import { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT } from "./enum.js";
|
|
3
3
|
import { ERROR_CODE, ERROR_MESSAGE, ErrorCode } from "./error.js";
|
|
4
|
+
import { IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe } from "./file.js";
|
|
4
5
|
import { IRPCTransport } from "./transport.js";
|
|
5
|
-
import { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCDataType, IRPCDeclareInit, IRPCError,
|
|
6
|
-
import { RemoteState,
|
|
6
|
+
import { 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 } from "./types.js";
|
|
7
|
+
import { RemoteState, stream } from "./state.js";
|
|
7
8
|
import { IRPCReader } from "./reader.js";
|
|
8
9
|
import { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall } from "./call.js";
|
|
9
|
-
import { createContext, getContext, setContext, setContextProvider, withContext } from "./context.js";
|
|
10
|
-
import { IRPCPackage, createPackage } from "./module.js";
|
|
10
|
+
import { createContext, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext } from "./context.js";
|
|
11
|
+
import { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept } from "./module.js";
|
|
12
|
+
import { IRPCFilePointer, IRPCFileQueue, IRPCPacketJson, IRPCPacketQueues, IRPC_FILE_IDENTIFIER, PacketStream, decode, encode, isFilePointer } from "./packet.js";
|
|
11
13
|
import { IRPCResolver } from "./resolver.js";
|
|
14
|
+
import { IRPCHook, IRPCRouter } from "./router.js";
|
|
12
15
|
import { IRPCStream } from "./stream.js";
|
|
13
|
-
|
|
16
|
+
import { IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPC_STORE } from "./store.js";
|
|
17
|
+
import { plan } from "@anchorlib/core";
|
|
18
|
+
export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, ErrorCode, IRPCArraySchema, IRPCBaseContext, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCDataType, IRPCDeclareInit, IRPCError, IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFilePointer, IRPCFileQueue, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe, IRPCFunction, IRPCHandler, IRPCHook, IRPCHookArgs, IRPCInit, IRPCInputs, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackage, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketEvent, IRPCPacketJson, IRPCPacketQueues, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCReader, IRPCRequest, IRPCResolver, IRPCResponse, IRPCRouter, IRPCSchema, IRPCSpec, IRPCSpecHook, IRPCSpecStore, IRPCStatus, IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPCStream, IRPCStreamInit, IRPCStub, IRPCStubStore, IRPCTransport, IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_IDENTIFIER, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE, IRPC_STORE_EVENT, PacketStream, RemoteState, StreamCleanup, StreamConstructor, TransportConfig, createContext, createPackage, decode, encode, getAbortController, getAbortSignal, getContext, intercept, isFilePointer, plan, setContext, setContextProvider, stream, withContext };
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { IRPCCacher } from "./cache.js";
|
|
2
|
-
import { IRPC_DATA_TYPE,
|
|
2
|
+
import { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT } from "./enum.js";
|
|
3
3
|
import { ERROR_CODE, ERROR_MESSAGE } from "./error.js";
|
|
4
|
+
import { createContext, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext } from "./context.js";
|
|
4
5
|
import { RemoteState, stream } from "./state.js";
|
|
5
6
|
import { IRPCReader } from "./reader.js";
|
|
6
|
-
import { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall } from "./call.js";
|
|
7
|
-
import { createContext, getContext, setContext, setContextProvider, withContext } from "./context.js";
|
|
8
7
|
import { IRPCTransport } from "./transport.js";
|
|
9
|
-
import { IRPCPackage, createPackage } from "./module.js";
|
|
10
|
-
import {
|
|
8
|
+
import { IRPCPackage, createPackage, intercept } from "./module.js";
|
|
9
|
+
import { IRPCRouter } from "./router.js";
|
|
11
10
|
import { IRPCStream } from "./stream.js";
|
|
11
|
+
import { IRPCStore, IRPC_STORE } from "./store.js";
|
|
12
|
+
import { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall } from "./call.js";
|
|
13
|
+
import { IRPCFile, IRPCFileStream } from "./file.js";
|
|
14
|
+
import { IRPC_FILE_IDENTIFIER, decode, encode, isFilePointer } from "./packet.js";
|
|
15
|
+
import { IRPCResolver } from "./resolver.js";
|
|
16
|
+
import { plan } from "@anchorlib/core";
|
|
12
17
|
|
|
13
|
-
export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, IRPCCacher, IRPCCall, IRPCPackage, IRPCReader, IRPCResolver, IRPCStream, IRPCTransport, IRPC_DATA_TYPE,
|
|
18
|
+
export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, IRPCCacher, IRPCCall, IRPCFile, IRPCFileStream, IRPCPackage, IRPCReader, IRPCResolver, IRPCRouter, IRPCStore, IRPCStream, IRPCTransport, IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_IDENTIFIER, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE, IRPC_STORE_EVENT, RemoteState, createContext, createPackage, decode, encode, getAbortController, getAbortSignal, getContext, intercept, isFilePointer, plan, setContext, setContextProvider, stream, withContext };
|
package/dist/module.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { IRPCCacher } from "./cache.js";
|
|
2
1
|
import { IRPCTransport } from "./transport.js";
|
|
3
|
-
import { IRPCData, IRPCDeclareInit, IRPCHandler, IRPCInputs, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCRequest, IRPCSpec,
|
|
2
|
+
import { IRPCData, IRPCDeclareInit, IRPCFunction, IRPCHandler, IRPCInputs, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCRequest, IRPCSpec, IRPCStub } from "./types.js";
|
|
4
3
|
import { RemoteState } from "./state.js";
|
|
4
|
+
import { IRPCReader } from "./reader.js";
|
|
5
5
|
|
|
6
6
|
//#region src/module.d.ts
|
|
7
|
-
|
|
7
|
+
type IRPCHookArgs<F$1> = F$1 extends ((...args: infer A) => unknown) ? {
|
|
8
|
+
name: string;
|
|
9
|
+
args: A;
|
|
10
|
+
} : {
|
|
11
|
+
name: string;
|
|
12
|
+
args: unknown[];
|
|
13
|
+
};
|
|
14
|
+
type IRPCSpecHook<F$1> = (req: IRPCHookArgs<F$1>) => void | Promise<void>;
|
|
8
15
|
/**
|
|
9
16
|
* IRPCPackage represents a package containing multiple IRPC (Isomorphic-RPC) specifications
|
|
10
17
|
* and their corresponding stubs. It manages the configuration, transport, and execution
|
|
@@ -14,15 +21,16 @@ declare class IRPCPackage {
|
|
|
14
21
|
/**
|
|
15
22
|
* A map storing all IRPC specifications by their names
|
|
16
23
|
*/
|
|
17
|
-
specs
|
|
24
|
+
private specs;
|
|
25
|
+
private hooks;
|
|
18
26
|
/**
|
|
19
27
|
* A weak map linking stub functions to their corresponding specifications
|
|
20
28
|
*/
|
|
21
|
-
stubs
|
|
29
|
+
private stubs;
|
|
22
30
|
/**
|
|
23
31
|
* A map storing caches for each IRPC Entry
|
|
24
32
|
*/
|
|
25
|
-
cache
|
|
33
|
+
private cache;
|
|
26
34
|
/**
|
|
27
35
|
* Configuration object for the IRPC package
|
|
28
36
|
*/
|
|
@@ -51,7 +59,7 @@ declare class IRPCPackage {
|
|
|
51
59
|
* @returns A stub function that can be used to call the IRPC
|
|
52
60
|
* @throws Error if an IRPC with the same name already exists
|
|
53
61
|
*/
|
|
54
|
-
declare<F, I extends IRPCInputs = IRPCInputs, O extends IRPCOutput = IRPCOutput>(options: IRPCDeclareInit<F, I, O>): F
|
|
62
|
+
declare<F, I extends IRPCInputs = IRPCInputs, O extends IRPCOutput = IRPCOutput>(options: IRPCDeclareInit<F, I, O>): IRPCFunction<F>;
|
|
55
63
|
/**
|
|
56
64
|
* Resolves and executes an IRPC call based on a request object
|
|
57
65
|
* @param req - The request containing the IRPC name and arguments
|
|
@@ -66,7 +74,22 @@ declare class IRPCPackage {
|
|
|
66
74
|
* @returns This IRPCPackage instance for chaining
|
|
67
75
|
* @throws Error if the stub or handler is invalid, or if no IRPC exists for the stub
|
|
68
76
|
*/
|
|
69
|
-
construct<F extends
|
|
77
|
+
construct<F, A extends unknown[], R extends IRPCData>(stub: IRPCStub<F, A, R>, handler: F): this;
|
|
78
|
+
/**
|
|
79
|
+
* Registers a hook function for a specific stub function
|
|
80
|
+
* @param stub - The stub function created by declare()
|
|
81
|
+
* @param handler - The hook function to register
|
|
82
|
+
* @returns This IRPCPackage instance for chaining
|
|
83
|
+
* @throws Error if the stub is invalid or if no IRPC exists for the stub
|
|
84
|
+
*/
|
|
85
|
+
hook<F extends IRPCHandler>(stub: F, handler: IRPCSpecHook<F>): this;
|
|
86
|
+
/**
|
|
87
|
+
* Resolves and executes all registered hooks for a given request
|
|
88
|
+
* @param req - The request containing the IRPC name and arguments
|
|
89
|
+
* @returns A promise that resolves when all hooks have been executed
|
|
90
|
+
* @throws Error if no IRPC exists for the request or if the hooks are not registered
|
|
91
|
+
*/
|
|
92
|
+
resolveHooks(req: IRPCRequest): Promise<void>;
|
|
70
93
|
/**
|
|
71
94
|
* Sets the transport mechanism for this package
|
|
72
95
|
* @param transport - The transport instance to use for remote calls
|
|
@@ -100,5 +123,14 @@ declare class IRPCPackage {
|
|
|
100
123
|
* @returns A new IRPCPackage instance
|
|
101
124
|
*/
|
|
102
125
|
declare function createPackage(config?: Partial<IRPCPackageConfig>): IRPCPackage;
|
|
126
|
+
/**
|
|
127
|
+
* Intercepts local function call to get an instant response without remote execution.
|
|
128
|
+
*
|
|
129
|
+
* @param reader - The reader object to intercept.
|
|
130
|
+
* @param spec - The IRPC specification for the function call.
|
|
131
|
+
* @param args - The arguments to be passed to the function.
|
|
132
|
+
* @returns {IRPCReader<IRPCData>} - The IRPCReader object for consumer.
|
|
133
|
+
*/
|
|
134
|
+
declare function intercept(spec: IRPCSpec<IRPCInputs, IRPCOutput>, args: unknown[], reader: IRPCReader<IRPCData>): IRPCReader<IRPCData>;
|
|
103
135
|
//#endregion
|
|
104
|
-
export { IRPCPackage, createPackage };
|
|
136
|
+
export { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept };
|