@irpclib/irpc 1.0.0-beta.22 → 1.0.0-beta.24

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 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 readonly timerId?;
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 transport
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 { uuid } from "./uuid.js";
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 = uuid();
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 transport
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(this.id);
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
- console.error(ERROR_MESSAGE[ERROR_CODE.CALL_MAX_RETRIES_REACHED], this.retryReasons);
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, createContext, 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][]): Map<K, V>;
26
- /**
27
- * Sets a value in the current context.
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 createContextStore<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, createContextStore, 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, createContext, 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
- currentStore = store;
11
+ setAsyncScope(store);
10
12
  }
11
13
  /**
12
14
  * Executes a function with the provided context.
@@ -17,35 +19,22 @@ function setContextProvider(store) {
17
19
  * @returns The result of the executed function
18
20
  */
19
21
  function withContext(ctx, fn) {
20
- return currentStore?.run(ctx, fn) ?? fn();
22
+ return withIsolation(fn, true, ctx);
21
23
  }
22
24
  /**
23
25
  * Creates a new context map with optional initial values.
24
26
  * @param init - Optional initial key-value pairs for the context
25
27
  * @returns A new Map instance representing the context
26
28
  */
27
- function createContext(init) {
28
- return new Map(init);
29
+ function createContextStore(init) {
30
+ return new AsyncStore(init);
29
31
  }
30
- /**
31
- * Sets a value in the current context.
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
- * Gets a value from the current context.
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, createContextStore, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext };
@@ -0,0 +1,9 @@
1
+ import { IRPCCredentials } from "./types.js";
2
+ import { AsyncStore } from "@anchorlib/core";
3
+
4
+ //#region src/credential.d.ts
5
+ declare function createCredentials(seeds: IRPCCredentials): AsyncStore;
6
+ declare function getCredentials(): AsyncStore;
7
+ declare function credential<V>(key: string): V | undefined;
8
+ //#endregion
9
+ export { createCredentials, credential, getCredentials };
@@ -0,0 +1,16 @@
1
+ import { IRPC_BASE_CONTEXT } from "./enum.js";
2
+ import { AsyncStore, getContext } from "@anchorlib/core";
3
+
4
+ //#region src/credential.ts
5
+ function createCredentials(seeds) {
6
+ return new AsyncStore(seeds);
7
+ }
8
+ function getCredentials() {
9
+ return getContext(IRPC_BASE_CONTEXT.CREDENTIALS);
10
+ }
11
+ function credential(key) {
12
+ return getCredentials()?.get(key);
13
+ }
14
+
15
+ //#endregion
16
+ export { createCredentials, credential, getCredentials };
package/dist/enum.d.ts CHANGED
@@ -14,22 +14,30 @@ 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
+ readonly CREDENTIALS: symbol;
35
+ };
36
+ declare const IRPC_FILE_STATUS: {
37
+ readonly IDLE: "idle";
38
+ readonly PENDING: "pending";
39
+ readonly SUCCESS: "success";
40
+ readonly ERROR: "error";
33
41
  };
34
42
  //#endregion
35
- export { IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS };
43
+ 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,31 @@ 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
+ CREDENTIALS: Symbol("credentials")
35
+ };
36
+ const IRPC_FILE_STATUS = {
37
+ IDLE: "idle",
38
+ PENDING: "pending",
39
+ SUCCESS: "success",
40
+ ERROR: "error"
33
41
  };
34
42
 
35
43
  //#endregion
36
- export { IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS };
44
+ 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;
@@ -23,6 +24,8 @@ declare const ERROR_CODE: {
23
24
  RESOLVER_NOT_FOUND: string;
24
25
  RESOLVER_NOT_SUPPORTED: string;
25
26
  STREAM_ERROR: string;
27
+ HANDLER_ERROR: string;
28
+ RESOLVE_ERROR: string;
26
29
  CALL_MAX_RETRIES_REACHED: string;
27
30
  };
28
31
  type ErrorCode = (typeof ERROR_CODE)[keyof typeof ERROR_CODE];
@@ -39,11 +42,14 @@ declare const ERROR_MESSAGE: {
39
42
  [ERROR_CODE.NOT_IMPLEMENTED]: string;
40
43
  [ERROR_CODE.INVALID_HANDLER]: string;
41
44
  [ERROR_CODE.INVALID_OPERATION]: string;
45
+ [ERROR_CODE.INVALID_HOOK]: string;
42
46
  [ERROR_CODE.TRANSPORT_MISSING]: string;
43
47
  [ERROR_CODE.TRANSPORT_INVALID]: string;
44
48
  [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: string;
45
49
  [ERROR_CODE.STUB_INVALID]: string;
46
50
  [ERROR_CODE.STREAM_ERROR]: string;
51
+ [ERROR_CODE.HANDLER_ERROR]: string;
52
+ [ERROR_CODE.RESOLVE_ERROR]: string;
47
53
  [ERROR_CODE.STUB_NOT_IMPLEMENTED]: string;
48
54
  [ERROR_CODE.RESOLVER_MISSING]: string;
49
55
  [ERROR_CODE.RESOLVER_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",
@@ -23,6 +24,8 @@ const ERROR_CODE = {
23
24
  RESOLVER_NOT_FOUND: "resolver_not_found",
24
25
  RESOLVER_NOT_SUPPORTED: "resolver_not_supported",
25
26
  STREAM_ERROR: "stream_error",
27
+ HANDLER_ERROR: "handler_error",
28
+ RESOLVE_ERROR: "resolve_error",
26
29
  CALL_MAX_RETRIES_REACHED: "call_max_retries_reached"
27
30
  };
28
31
  const ERROR_MESSAGE = {
@@ -38,11 +41,14 @@ const ERROR_MESSAGE = {
38
41
  [ERROR_CODE.NOT_IMPLEMENTED]: "IRPC: Not implemented error",
39
42
  [ERROR_CODE.INVALID_HANDLER]: "IRPC: Invalid handler error",
40
43
  [ERROR_CODE.INVALID_OPERATION]: "IRPC: Invalid operation error",
44
+ [ERROR_CODE.INVALID_HOOK]: "IRPC: Invalid hook error",
41
45
  [ERROR_CODE.TRANSPORT_MISSING]: "IRPC: Transport missing error",
42
46
  [ERROR_CODE.TRANSPORT_INVALID]: "IRPC: Transport invalid error",
43
47
  [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: "IRPC: Transport not implemented error",
44
48
  [ERROR_CODE.STUB_INVALID]: "IRPC: Stub invalid error",
45
49
  [ERROR_CODE.STREAM_ERROR]: "IRPC: Stream error",
50
+ [ERROR_CODE.HANDLER_ERROR]: "IRPC: Handler error",
51
+ [ERROR_CODE.RESOLVE_ERROR]: "IRPC: Resolve error",
46
52
  [ERROR_CODE.STUB_NOT_IMPLEMENTED]: "IRPC: Stub not implemented error",
47
53
  [ERROR_CODE.RESOLVER_MISSING]: "IRPC: Resolver missing error",
48
54
  [ERROR_CODE.RESOLVER_NOT_IMPLEMENTED]: "IRPC: Resolver 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,19 @@
1
1
  import { IRPCCacheEntry, IRPCCacher } from "./cache.js";
2
- import { IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
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";
5
+ import { IRPCFilePointer, IRPCFileQueue, IRPCPacketJson, IRPCPacketQueues, IRPC_FILE_IDENTIFIER, PacketStream, decode, encode, isFilePointer } from "./packet.js";
6
+ import { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept } from "./module.js";
4
7
  import { IRPCTransport } from "./transport.js";
5
- import { 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 } from "./types.js";
6
- import { RemoteState, StreamConstructor, stream } from "./state.js";
8
+ import { IRPCArraySchema, IRPCBaseContext, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, 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, IRPCRequests, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig } from "./types.js";
9
+ import { RemoteState, stream } from "./state.js";
7
10
  import { IRPCReader } from "./reader.js";
8
11
  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";
12
+ import { createContext, createContextStore, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext } from "./context.js";
13
+ import { createCredentials, credential, getCredentials } from "./credential.js";
11
14
  import { IRPCResolver } from "./resolver.js";
15
+ import { IRPCHook, IRPCRouter } from "./router.js";
12
16
  import { IRPCStream } from "./stream.js";
13
- export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, ErrorCode, IRPCArraySchema, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCData, IRPCDataSchema, IRPCDataType, IRPCDeclareInit, IRPCError, IRPCEventType, IRPCHandler, IRPCInit, IRPCInputs, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackage, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketData, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCReader, IRPCRequest, IRPCResolver, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStream, IRPCStubStore, IRPCTransport, IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS, RemoteState, StreamConstructor, TransportConfig, createContext, createPackage, getContext, setContext, setContextProvider, stream, withContext };
17
+ import { IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPC_STORE } from "./store.js";
18
+ import { plan } from "@anchorlib/core";
19
+ export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, ErrorCode, IRPCArraySchema, IRPCBaseContext, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, 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, IRPCRequests, 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, createContextStore, createCredentials, createPackage, credential, decode, encode, getAbortController, getAbortSignal, getContext, getCredentials, intercept, isFilePointer, plan, setContext, setContextProvider, stream, withContext };
package/dist/index.js CHANGED
@@ -1,13 +1,19 @@
1
1
  import { IRPCCacher } from "./cache.js";
2
- import { IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
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, createContextStore, 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 { IRPCResolver } from "./resolver.js";
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 { createCredentials, credential, getCredentials } from "./credential.js";
14
+ import { IRPCFile, IRPCFileStream } from "./file.js";
15
+ import { IRPC_FILE_IDENTIFIER, decode, encode, isFilePointer } from "./packet.js";
16
+ import { IRPCResolver } from "./resolver.js";
17
+ import { plan } from "@anchorlib/core";
12
18
 
13
- export { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, ERROR_CODE, ERROR_MESSAGE, IRPCCacher, IRPCCall, IRPCPackage, IRPCReader, IRPCResolver, IRPCStream, IRPCTransport, IRPC_DATA_TYPE, IRPC_EVENT_TYPE, IRPC_PACKET_TYPE, IRPC_STATUS, RemoteState, createContext, createPackage, getContext, setContext, setContextProvider, stream, withContext };
19
+ 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, createContextStore, createCredentials, createPackage, credential, decode, encode, getAbortController, getAbortSignal, getContext, getCredentials, intercept, isFilePointer, plan, setContext, setContextProvider, stream, withContext };