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

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
@@ -12,7 +12,9 @@ declare const DEFAULT_RETRY_DELAY = 1000;
12
12
  declare class IRPCCall {
13
13
  transport: IRPCTransport;
14
14
  payload: IRPCPayload;
15
- options: IRPCCallConfig;
15
+ options: IRPCCallConfig & {
16
+ seed?: () => IRPCData;
17
+ };
16
18
  /**
17
19
  * Unique identifier for this RPC call, generated using shortId().
18
20
  */
@@ -51,7 +53,9 @@ declare class IRPCCall {
51
53
  * @param payload - The RPC payload containing method and parameters
52
54
  * @param options - Options for the call, such as timeout, maxRetries, etc.
53
55
  */
54
- constructor(transport: IRPCTransport, payload: IRPCPayload, options: IRPCCallConfig, reader?: IRPCReader<IRPCData>);
56
+ constructor(transport: IRPCTransport, payload: IRPCPayload, options: IRPCCallConfig & {
57
+ seed?: () => IRPCData;
58
+ }, reader?: IRPCReader<IRPCData>);
55
59
  enqueue(packet: IRPCPacketStream<IRPCData>): void;
56
60
  /**
57
61
  * Resolves the RPC call with the provided value.
package/dist/call.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IRPC_PACKET_TYPE, IRPC_STATUS } from "./enum.js";
2
- import { ERROR_CODE, ERROR_MESSAGE } from "./error.js";
2
+ import { CallError } from "./error.js";
3
3
  import { IRPCReader } from "./reader.js";
4
4
  import { IRPC_STORE } from "./store.js";
5
5
  import { uuid } from "@anchorlib/core";
@@ -60,16 +60,13 @@ var IRPCCall = class {
60
60
  name: this.payload.name,
61
61
  type: IRPC_PACKET_TYPE.CLOSE,
62
62
  status: IRPC_STATUS.ERROR,
63
- error: {
64
- code: ERROR_CODE.TIMEOUT,
65
- message: ERROR_MESSAGE[ERROR_CODE.TIMEOUT]
66
- },
63
+ error: CallError.timeout().json(),
67
64
  createdAt: Date.now()
68
65
  });
69
66
  clearTimeout(this.retryId);
70
- this.reject(new Error(ERROR_MESSAGE[ERROR_CODE.TIMEOUT]), false);
67
+ this.reject(CallError.timeout(), false);
71
68
  }, options.timeout);
72
- this.reader = reader ?? new IRPCReader(uuid(), options?.init?.());
69
+ this.reader = reader ?? new IRPCReader(uuid(), options?.seed?.());
73
70
  this.reader.onClose = () => this.close();
74
71
  this.id = this.reader.id;
75
72
  }
@@ -105,7 +102,7 @@ var IRPCCall = class {
105
102
  if (maxRetries && retriable) {
106
103
  if (reason) this.retryReasons.add(reason);
107
104
  if (this.retries >= maxRetries) {
108
- IRPC_STORE.error(/* @__PURE__ */ new Error(`${ERROR_MESSAGE[ERROR_CODE.CALL_MAX_RETRIES_REACHED]}: ${Array.from(this.retryReasons).map((r) => r.message).join(", ")}`), [{
105
+ IRPC_STORE.error(CallError.maxRetries(this.retryReasons), [{
109
106
  id: this.id,
110
107
  name: this.payload.name
111
108
  }]);
package/dist/context.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IRPCContext, IRPCContextProvider } from "./types.js";
2
- import { AsyncStore, getContext, setContext } from "@anchorlib/core";
2
+ import { AsyncStore, createContext, getContext, setContext } from "@anchorlib/core";
3
3
 
4
4
  //#region src/context.d.ts
5
5
 
@@ -23,8 +23,8 @@ declare function withContext<R>(ctx: IRPCContext<string | symbol, unknown>, fn:
23
23
  * @param init - Optional initial key-value pairs for the context
24
24
  * @returns A new Map instance representing the context
25
25
  */
26
- declare function createContext<K extends string | symbol, V>(init?: [K, V][]): AsyncStore;
26
+ declare function createContextStore<K extends string | symbol, V>(init?: [K, V][]): AsyncStore;
27
27
  declare function getAbortSignal(): AbortSignal | undefined;
28
28
  declare function getAbortController(): AbortController | undefined;
29
29
  //#endregion
30
- export { createContext, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext };
30
+ export { createContext, createContextStore, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext };
package/dist/context.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IRPC_BASE_CONTEXT } from "./enum.js";
2
- import { AsyncStore, getContext, setAsyncScope, setContext, withIsolation } from "@anchorlib/core";
2
+ import { AsyncStore, createContext, getContext, setAsyncScope, setContext, withIsolation } from "@anchorlib/core";
3
3
 
4
4
  //#region src/context.ts
5
5
  /**
@@ -26,7 +26,7 @@ function withContext(ctx, fn) {
26
26
  * @param init - Optional initial key-value pairs for the context
27
27
  * @returns A new Map instance representing the context
28
28
  */
29
- function createContext(init) {
29
+ function createContextStore(init) {
30
30
  return new AsyncStore(init);
31
31
  }
32
32
  function getAbortSignal() {
@@ -37,4 +37,4 @@ function getAbortController() {
37
37
  }
38
38
 
39
39
  //#endregion
40
- export { createContext, getAbortController, getAbortSignal, 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
@@ -4,15 +4,6 @@ declare const IRPC_PACKET_TYPE: {
4
4
  readonly EVENT: "event";
5
5
  readonly CLOSE: "close";
6
6
  readonly ANSWER: "answer";
7
- readonly REQUEST: "request";
8
- readonly RESPONSE: "response";
9
- };
10
- declare const IRPC_DATA_TYPE: {
11
- readonly ARRAY: "array";
12
- readonly OBJECT: "object";
13
- readonly READABLE: "readable";
14
- readonly WRITABLE: "writable";
15
- readonly PRIMITIVE: "primitive";
16
7
  };
17
8
  declare const IRPC_STATUS: {
18
9
  readonly IDLE: "idle";
@@ -31,6 +22,7 @@ declare const IRPC_STORE_EVENT: {
31
22
  declare const IRPC_BASE_CONTEXT: {
32
23
  readonly ABORT_SIGNAL: symbol;
33
24
  readonly ABORT_CONTROLLER: symbol;
25
+ readonly CREDENTIALS: symbol;
34
26
  };
35
27
  declare const IRPC_FILE_STATUS: {
36
28
  readonly IDLE: "idle";
@@ -39,4 +31,4 @@ declare const IRPC_FILE_STATUS: {
39
31
  readonly ERROR: "error";
40
32
  };
41
33
  //#endregion
42
- export { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
34
+ export { IRPC_BASE_CONTEXT, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
package/dist/enum.js CHANGED
@@ -3,16 +3,7 @@ const IRPC_PACKET_TYPE = {
3
3
  CALL: "call",
4
4
  EVENT: "event",
5
5
  CLOSE: "close",
6
- ANSWER: "answer",
7
- REQUEST: "request",
8
- RESPONSE: "response"
9
- };
10
- const IRPC_DATA_TYPE = {
11
- ARRAY: "array",
12
- OBJECT: "object",
13
- READABLE: "readable",
14
- WRITABLE: "writable",
15
- PRIMITIVE: "primitive"
6
+ ANSWER: "answer"
16
7
  };
17
8
  const IRPC_STATUS = {
18
9
  IDLE: "idle",
@@ -30,7 +21,8 @@ const IRPC_STORE_EVENT = {
30
21
  };
31
22
  const IRPC_BASE_CONTEXT = {
32
23
  ABORT_SIGNAL: Symbol("abort-signal"),
33
- ABORT_CONTROLLER: Symbol("abort-controller")
24
+ ABORT_CONTROLLER: Symbol("abort-controller"),
25
+ CREDENTIALS: Symbol("credentials")
34
26
  };
35
27
  const IRPC_FILE_STATUS = {
36
28
  IDLE: "idle",
@@ -40,4 +32,4 @@ const IRPC_FILE_STATUS = {
40
32
  };
41
33
 
42
34
  //#endregion
43
- export { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
35
+ export { IRPC_BASE_CONTEXT, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT };
package/dist/error.d.ts CHANGED
@@ -1,57 +1,116 @@
1
+ import { IRPCPacketError } from "./types.js";
2
+
1
3
  //#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
- INVALID_HOOK: string;
16
- TRANSPORT_MISSING: string;
17
- TRANSPORT_INVALID: string;
18
- TRANSPORT_NOT_IMPLEMENTED: string;
19
- STUB_MISSING: string;
20
- STUB_INVALID: string;
21
- STUB_NOT_IMPLEMENTED: string;
22
- RESOLVER_MISSING: string;
23
- RESOLVER_NOT_IMPLEMENTED: string;
24
- RESOLVER_NOT_FOUND: string;
25
- RESOLVER_NOT_SUPPORTED: string;
26
- STREAM_ERROR: string;
27
- CALL_MAX_RETRIES_REACHED: string;
4
+ declare const IRPC_ERROR_TYPE: {
5
+ readonly STUB: "stub";
6
+ readonly HOOK: "hook";
7
+ readonly CALL: "call";
8
+ readonly HANDLER: "handler";
9
+ readonly RESOLVE: "resolve";
10
+ readonly TRANSPORT: "transport";
28
11
  };
29
- type ErrorCode = (typeof ERROR_CODE)[keyof typeof ERROR_CODE];
30
- declare const ERROR_MESSAGE: {
31
- [ERROR_CODE.UNKNOWN]: string;
32
- [ERROR_CODE.TIMEOUT]: string;
33
- [ERROR_CODE.NOT_FOUND]: string;
34
- [ERROR_CODE.NOT_SUPPORTED]: string;
35
- [ERROR_CODE.INVALID_TYPE]: string;
36
- [ERROR_CODE.INVALID_STATE]: string;
37
- [ERROR_CODE.INVALID_VALUE]: string;
38
- [ERROR_CODE.INVALID_INPUT]: string;
39
- [ERROR_CODE.INVALID_OUTPUT]: string;
40
- [ERROR_CODE.NOT_IMPLEMENTED]: string;
41
- [ERROR_CODE.INVALID_HANDLER]: string;
42
- [ERROR_CODE.INVALID_OPERATION]: string;
43
- [ERROR_CODE.INVALID_HOOK]: string;
44
- [ERROR_CODE.TRANSPORT_MISSING]: string;
45
- [ERROR_CODE.TRANSPORT_INVALID]: string;
46
- [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: string;
47
- [ERROR_CODE.STUB_INVALID]: string;
48
- [ERROR_CODE.STREAM_ERROR]: string;
49
- [ERROR_CODE.STUB_NOT_IMPLEMENTED]: string;
50
- [ERROR_CODE.RESOLVER_MISSING]: string;
51
- [ERROR_CODE.RESOLVER_NOT_IMPLEMENTED]: string;
52
- [ERROR_CODE.RESOLVER_NOT_FOUND]: string;
53
- [ERROR_CODE.RESOLVER_NOT_SUPPORTED]: string;
54
- [ERROR_CODE.CALL_MAX_RETRIES_REACHED]: string;
12
+ type IRPCErrorType = (typeof IRPC_ERROR_TYPE)[keyof typeof IRPC_ERROR_TYPE];
13
+ declare const STUB_ERROR: {
14
+ readonly DUPLICATE: "duplicate";
15
+ readonly INVALID: "invalid";
16
+ readonly NOT_FOUND: "not_found";
17
+ readonly INVALID_NAME: "invalid_name";
18
+ readonly INVALID_VERSION: "invalid_version";
55
19
  };
20
+ declare const HANDLER_ERROR: {
21
+ readonly INVALID: "invalid";
22
+ readonly MISSING: "missing";
23
+ readonly ERROR: "error";
24
+ };
25
+ declare const TRANSPORT_ERROR: {
26
+ readonly INVALID: "invalid";
27
+ readonly MISSING: "missing";
28
+ readonly NOT_IMPLEMENTED: "not_implemented";
29
+ readonly NOT_CONNECTED: "not_connected";
30
+ readonly CLOSED: "closed";
31
+ readonly INVALID_BODY: "invalid_body";
32
+ readonly STREAM_TERMINATED: "stream_terminated";
33
+ readonly ERROR: "error";
34
+ };
35
+ declare const RESOLVE_ERROR: {
36
+ readonly NOT_FOUND: "not_found";
37
+ readonly INVALID_INPUT: "invalid_input";
38
+ readonly INVALID_OUTPUT: "invalid_output";
39
+ readonly ERROR: "error";
40
+ };
41
+ declare const HOOK_ERROR: {
42
+ readonly INVALID: "invalid";
43
+ readonly ERROR: "error";
44
+ };
45
+ declare const CALL_ERROR: {
46
+ readonly TIMEOUT: "timeout";
47
+ readonly MAX_RETRIES: "max_retries";
48
+ readonly STREAM_ERROR: "stream_error";
49
+ };
50
+ /**
51
+ * Base error class for all IRPC errors.
52
+ *
53
+ * @property type - The domain category of the error (stub, handler, transport, etc.)
54
+ * @property code - A stable, machine-readable code for translation or programmatic matching.
55
+ */
56
+ declare class IRPCError extends Error {
57
+ type: IRPCErrorType;
58
+ code: string;
59
+ cause?: Error | undefined;
60
+ constructor(type: IRPCErrorType, code: string, message: string, cause?: Error | undefined);
61
+ /** Serialize to the wire format used in IRPC packets. */
62
+ json(): IRPCPacketError;
63
+ /** Reconstruct an IRPCError from a wire packet error. */
64
+ static from(obj: IRPCPacketError): IRPCError;
65
+ }
66
+ /** Errors related to stub declaration, registration, and lookup. */
67
+ declare class StubError extends IRPCError {
68
+ constructor(code: string, message: string, cause?: Error);
69
+ static duplicate(name: string): StubError;
70
+ static invalid(): StubError;
71
+ static notFound(): StubError;
72
+ static invalidName(name: string): StubError;
73
+ static invalidVersion(version: string): StubError;
74
+ }
75
+ /** Errors related to handler registration and execution. */
76
+ declare class HandlerError extends IRPCError {
77
+ constructor(code: string, message: string, cause?: Error);
78
+ static invalid(): HandlerError;
79
+ static missing(name: string): HandlerError;
80
+ static failed(input: Error | string): HandlerError;
81
+ }
82
+ /** Errors related to the transport layer. */
83
+ declare class TransportError extends IRPCError {
84
+ constructor(code: string, message: string, cause?: Error);
85
+ static missing(): TransportError;
86
+ static invalid(): TransportError;
87
+ static notImplemented(): TransportError;
88
+ static notConnected(name: string): TransportError;
89
+ static closed(name: string): TransportError;
90
+ static invalidBody(): TransportError;
91
+ static streamTerminated(): TransportError;
92
+ static failed(input: Error | string): TransportError;
93
+ }
94
+ /** Errors related to server-side request resolution. */
95
+ declare class ResolveError extends IRPCError {
96
+ constructor(code: string, message: string, cause?: Error);
97
+ static notFound(name: string): ResolveError;
98
+ static invalidInput(input: Error | string): ResolveError;
99
+ static invalidOutput(input?: Error | string): ResolveError;
100
+ static failed(input: Error | string): ResolveError;
101
+ }
102
+ /** Errors related to hook registration and execution. */
103
+ declare class HookError extends IRPCError {
104
+ constructor(code: string, message: string, cause?: Error);
105
+ static invalid(): HookError;
106
+ static failed(input: Error | string): HookError;
107
+ }
108
+ /** Errors related to call execution lifecycle. */
109
+ declare class CallError extends IRPCError {
110
+ constructor(code: string, message: string, cause?: Error);
111
+ static timeout(): CallError;
112
+ static maxRetries(reasons: Set<Error>): CallError;
113
+ static streamError(input: Error | string): CallError;
114
+ }
56
115
  //#endregion
57
- export { ERROR_CODE, ERROR_MESSAGE, ErrorCode };
116
+ export { CALL_ERROR, CallError, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCError, IRPCErrorType, IRPC_ERROR_TYPE, RESOLVE_ERROR, ResolveError, STUB_ERROR, StubError, TRANSPORT_ERROR, TransportError };
package/dist/error.js CHANGED
@@ -1,57 +1,212 @@
1
1
  //#region src/error.ts
2
- const ERROR_CODE = {
3
- UNKNOWN: "unknown",
4
- TIMEOUT: "timeout",
2
+ const IRPC_ERROR_TYPE = {
3
+ STUB: "stub",
4
+ HOOK: "hook",
5
+ CALL: "call",
6
+ HANDLER: "handler",
7
+ RESOLVE: "resolve",
8
+ TRANSPORT: "transport"
9
+ };
10
+ const STUB_ERROR = {
11
+ DUPLICATE: "duplicate",
12
+ INVALID: "invalid",
5
13
  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",
14
+ INVALID_NAME: "invalid_name",
15
+ INVALID_VERSION: "invalid_version"
16
+ };
17
+ const HANDLER_ERROR = {
18
+ INVALID: "invalid",
19
+ MISSING: "missing",
20
+ ERROR: "error"
21
+ };
22
+ const TRANSPORT_ERROR = {
23
+ INVALID: "invalid",
24
+ MISSING: "missing",
12
25
  NOT_IMPLEMENTED: "not_implemented",
13
- INVALID_HANDLER: "invalid_handler",
14
- INVALID_OPERATION: "invalid_operation",
15
- INVALID_HOOK: "invalid_middleware",
16
- TRANSPORT_MISSING: "transport_missing",
17
- TRANSPORT_INVALID: "transport_invalid",
18
- TRANSPORT_NOT_IMPLEMENTED: "transport_not_implemented",
19
- STUB_MISSING: "stub_missing",
20
- STUB_INVALID: "stub_invalid",
21
- STUB_NOT_IMPLEMENTED: "stub_not_implemented",
22
- RESOLVER_MISSING: "resolver_missing",
23
- RESOLVER_NOT_IMPLEMENTED: "resolver_not_implemented",
24
- RESOLVER_NOT_FOUND: "resolver_not_found",
25
- RESOLVER_NOT_SUPPORTED: "resolver_not_supported",
26
- STREAM_ERROR: "stream_error",
27
- CALL_MAX_RETRIES_REACHED: "call_max_retries_reached"
28
- };
29
- const ERROR_MESSAGE = {
30
- [ERROR_CODE.UNKNOWN]: "IRPC: Unknown error",
31
- [ERROR_CODE.TIMEOUT]: "IRPC: Timeout error",
32
- [ERROR_CODE.NOT_FOUND]: "IRPC: Not found error",
33
- [ERROR_CODE.NOT_SUPPORTED]: "IRPC: Not supported error",
34
- [ERROR_CODE.INVALID_TYPE]: "IRPC: Invalid type error",
35
- [ERROR_CODE.INVALID_STATE]: "IRPC: Invalid state error",
36
- [ERROR_CODE.INVALID_VALUE]: "IRPC: Invalid value error",
37
- [ERROR_CODE.INVALID_INPUT]: "IRPC: Invalid input error",
38
- [ERROR_CODE.INVALID_OUTPUT]: "IRPC: Invalid output error",
39
- [ERROR_CODE.NOT_IMPLEMENTED]: "IRPC: Not implemented error",
40
- [ERROR_CODE.INVALID_HANDLER]: "IRPC: Invalid handler error",
41
- [ERROR_CODE.INVALID_OPERATION]: "IRPC: Invalid operation error",
42
- [ERROR_CODE.INVALID_HOOK]: "IRPC: Invalid hook error",
43
- [ERROR_CODE.TRANSPORT_MISSING]: "IRPC: Transport missing error",
44
- [ERROR_CODE.TRANSPORT_INVALID]: "IRPC: Transport invalid error",
45
- [ERROR_CODE.TRANSPORT_NOT_IMPLEMENTED]: "IRPC: Transport not implemented error",
46
- [ERROR_CODE.STUB_INVALID]: "IRPC: Stub invalid error",
47
- [ERROR_CODE.STREAM_ERROR]: "IRPC: Stream error",
48
- [ERROR_CODE.STUB_NOT_IMPLEMENTED]: "IRPC: Stub not implemented error",
49
- [ERROR_CODE.RESOLVER_MISSING]: "IRPC: Resolver missing error",
50
- [ERROR_CODE.RESOLVER_NOT_IMPLEMENTED]: "IRPC: Resolver not implemented error",
51
- [ERROR_CODE.RESOLVER_NOT_FOUND]: "IRPC: Resolver not found error",
52
- [ERROR_CODE.RESOLVER_NOT_SUPPORTED]: "IRPC: Resolver not supported error",
53
- [ERROR_CODE.CALL_MAX_RETRIES_REACHED]: "IRPC: Call max retries reached error"
26
+ NOT_CONNECTED: "not_connected",
27
+ CLOSED: "closed",
28
+ INVALID_BODY: "invalid_body",
29
+ STREAM_TERMINATED: "stream_terminated",
30
+ ERROR: "error"
31
+ };
32
+ const RESOLVE_ERROR = {
33
+ NOT_FOUND: "not_found",
34
+ INVALID_INPUT: "invalid_input",
35
+ INVALID_OUTPUT: "invalid_output",
36
+ ERROR: "error"
37
+ };
38
+ const HOOK_ERROR = {
39
+ INVALID: "invalid",
40
+ ERROR: "error"
41
+ };
42
+ const CALL_ERROR = {
43
+ TIMEOUT: "timeout",
44
+ MAX_RETRIES: "max_retries",
45
+ STREAM_ERROR: "stream_error"
46
+ };
47
+ function unwrap(input) {
48
+ if (input instanceof Error) return {
49
+ message: input.message,
50
+ cause: input
51
+ };
52
+ return { message: input };
53
+ }
54
+ /**
55
+ * Base error class for all IRPC errors.
56
+ *
57
+ * @property type - The domain category of the error (stub, handler, transport, etc.)
58
+ * @property code - A stable, machine-readable code for translation or programmatic matching.
59
+ */
60
+ var IRPCError = class IRPCError extends Error {
61
+ constructor(type, code, message, cause) {
62
+ super(message);
63
+ this.type = type;
64
+ this.code = code;
65
+ this.cause = cause;
66
+ }
67
+ /** Serialize to the wire format used in IRPC packets. */
68
+ json() {
69
+ return {
70
+ type: this.type,
71
+ code: this.code,
72
+ message: this.message
73
+ };
74
+ }
75
+ /** Reconstruct an IRPCError from a wire packet error. */
76
+ static from(obj) {
77
+ const ErrorClass = IRPC_ERROR_CLASS[obj.type];
78
+ if (ErrorClass) return new ErrorClass(obj.code, obj.message);
79
+ return new IRPCError(obj.type, obj.code, obj.message);
80
+ }
81
+ };
82
+ /** Errors related to stub declaration, registration, and lookup. */
83
+ var StubError = class StubError extends IRPCError {
84
+ constructor(code, message, cause) {
85
+ super(IRPC_ERROR_TYPE.STUB, code, message, cause);
86
+ }
87
+ static duplicate(name) {
88
+ return new StubError(STUB_ERROR.DUPLICATE, `IRPC "${name}" already exists.`);
89
+ }
90
+ static invalid() {
91
+ return new StubError(STUB_ERROR.INVALID, "Invalid stub.");
92
+ }
93
+ static notFound() {
94
+ return new StubError(STUB_ERROR.NOT_FOUND, "No spec found for stub.");
95
+ }
96
+ static invalidName(name) {
97
+ return new StubError(STUB_ERROR.INVALID_NAME, `Invalid name: ${name}`);
98
+ }
99
+ static invalidVersion(version) {
100
+ return new StubError(STUB_ERROR.INVALID_VERSION, `Invalid version: ${version}`);
101
+ }
102
+ };
103
+ /** Errors related to handler registration and execution. */
104
+ var HandlerError = class HandlerError extends IRPCError {
105
+ constructor(code, message, cause) {
106
+ super(IRPC_ERROR_TYPE.HANDLER, code, message, cause);
107
+ }
108
+ static invalid() {
109
+ return new HandlerError(HANDLER_ERROR.INVALID, "Handler must be a function.");
110
+ }
111
+ static missing(name) {
112
+ return new HandlerError(HANDLER_ERROR.MISSING, `IRPC "${name}" has no implementation.`);
113
+ }
114
+ static failed(input) {
115
+ const { message, cause } = unwrap(input);
116
+ return new HandlerError(HANDLER_ERROR.ERROR, message, cause);
117
+ }
118
+ };
119
+ /** Errors related to the transport layer. */
120
+ var TransportError = class TransportError extends IRPCError {
121
+ constructor(code, message, cause) {
122
+ super(IRPC_ERROR_TYPE.TRANSPORT, code, message, cause);
123
+ }
124
+ static missing() {
125
+ return new TransportError(TRANSPORT_ERROR.MISSING, "No transport configured.");
126
+ }
127
+ static invalid() {
128
+ return new TransportError(TRANSPORT_ERROR.INVALID, "Invalid transport.");
129
+ }
130
+ static notImplemented() {
131
+ return new TransportError(TRANSPORT_ERROR.NOT_IMPLEMENTED, "Transport dispatch not implemented.");
132
+ }
133
+ static notConnected(name) {
134
+ return new TransportError(TRANSPORT_ERROR.NOT_CONNECTED, `${name} is not connected.`);
135
+ }
136
+ static closed(name) {
137
+ return new TransportError(TRANSPORT_ERROR.CLOSED, `${name} connection closed.`);
138
+ }
139
+ static invalidBody() {
140
+ return new TransportError(TRANSPORT_ERROR.INVALID_BODY, "Invalid response body.");
141
+ }
142
+ static streamTerminated() {
143
+ return new TransportError(TRANSPORT_ERROR.STREAM_TERMINATED, "Response stream terminated.");
144
+ }
145
+ static failed(input) {
146
+ const { message, cause } = unwrap(input);
147
+ return new TransportError(TRANSPORT_ERROR.ERROR, message, cause);
148
+ }
149
+ };
150
+ /** Errors related to server-side request resolution. */
151
+ var ResolveError = class ResolveError extends IRPCError {
152
+ constructor(code, message, cause) {
153
+ super(IRPC_ERROR_TYPE.RESOLVE, code, message, cause);
154
+ }
155
+ static notFound(name) {
156
+ return new ResolveError(RESOLVE_ERROR.NOT_FOUND, `IRPC "${name}" does not exist.`);
157
+ }
158
+ static invalidInput(input) {
159
+ const { message, cause } = unwrap(input);
160
+ return new ResolveError(RESOLVE_ERROR.INVALID_INPUT, message, cause);
161
+ }
162
+ static invalidOutput(input) {
163
+ if (!input) return new ResolveError(RESOLVE_ERROR.INVALID_OUTPUT, "Invalid output.");
164
+ const { message, cause } = unwrap(input);
165
+ return new ResolveError(RESOLVE_ERROR.INVALID_OUTPUT, message, cause);
166
+ }
167
+ static failed(input) {
168
+ const { message, cause } = unwrap(input);
169
+ return new ResolveError(RESOLVE_ERROR.ERROR, message, cause);
170
+ }
171
+ };
172
+ /** Errors related to hook registration and execution. */
173
+ var HookError = class HookError extends IRPCError {
174
+ constructor(code, message, cause) {
175
+ super(IRPC_ERROR_TYPE.HOOK, code, message, cause);
176
+ }
177
+ static invalid() {
178
+ return new HookError(HOOK_ERROR.INVALID, "Hook must be a function.");
179
+ }
180
+ static failed(input) {
181
+ const { message, cause } = unwrap(input);
182
+ return new HookError(HOOK_ERROR.ERROR, message, cause);
183
+ }
184
+ };
185
+ /** Errors related to call execution lifecycle. */
186
+ var CallError = class CallError extends IRPCError {
187
+ constructor(code, message, cause) {
188
+ super(IRPC_ERROR_TYPE.CALL, code, message, cause);
189
+ }
190
+ static timeout() {
191
+ return new CallError(CALL_ERROR.TIMEOUT, "Call timed out.");
192
+ }
193
+ static maxRetries(reasons) {
194
+ const detail = Array.from(reasons).map((r) => r.message).join(", ");
195
+ return new CallError(CALL_ERROR.MAX_RETRIES, `Max retries reached: ${detail}`);
196
+ }
197
+ static streamError(input) {
198
+ const { message, cause } = unwrap(input);
199
+ return new CallError(CALL_ERROR.STREAM_ERROR, message, cause);
200
+ }
201
+ };
202
+ const IRPC_ERROR_CLASS = {
203
+ [IRPC_ERROR_TYPE.STUB]: StubError,
204
+ [IRPC_ERROR_TYPE.HANDLER]: HandlerError,
205
+ [IRPC_ERROR_TYPE.TRANSPORT]: TransportError,
206
+ [IRPC_ERROR_TYPE.RESOLVE]: ResolveError,
207
+ [IRPC_ERROR_TYPE.HOOK]: HookError,
208
+ [IRPC_ERROR_TYPE.CALL]: CallError
54
209
  };
55
210
 
56
211
  //#endregion
57
- export { ERROR_CODE, ERROR_MESSAGE };
212
+ export { CALL_ERROR, CallError, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCError, IRPC_ERROR_TYPE, RESOLVE_ERROR, ResolveError, STUB_ERROR, StubError, TRANSPORT_ERROR, TransportError };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,19 @@
1
1
  import { IRPCCacheEntry, IRPCCacher } from "./cache.js";
2
- import { IRPC_BASE_CONTEXT, IRPC_DATA_TYPE, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT } from "./enum.js";
3
- import { ERROR_CODE, ERROR_MESSAGE, ErrorCode } from "./error.js";
2
+ import { IRPC_BASE_CONTEXT, IRPC_FILE_STATUS, IRPC_PACKET_TYPE, IRPC_STATUS, IRPC_STORE_EVENT } from "./enum.js";
4
3
  import { IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe } from "./file.js";
4
+ import { IRPCFilePointer, IRPCFileQueue, IRPCPacketJson, IRPCPacketQueues, IRPC_FILE_IDENTIFIER, PacketStream, decode, encode, isFilePointer } from "./packet.js";
5
+ import { IRPCHookArgs, IRPCPackage, IRPCSpecHook, createPackage, intercept } from "./module.js";
5
6
  import { IRPCTransport } from "./transport.js";
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 { IRPCArraySchema, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCData, IRPCDataSchema, IRPCDeclareInit, IRPCDefined, IRPCFunction, IRPCHandler, IRPCInferInit, IRPCInit, IRPCInputs, IRPCObject, IRPCObjectSchema, IRPCOutput, IRPCPackageConfig, IRPCPackageInfo, IRPCPacketAnswer, IRPCPacketBase, IRPCPacketCall, IRPCPacketClose, IRPCPacketError, IRPCPacketEvent, IRPCPacketStream, IRPCPacketType, IRPCParseResult, IRPCPayload, IRPCPrimitive, IRPCPrimitiveSchema, IRPCReadable, IRPCRequest, IRPCRequests, IRPCResponse, IRPCSchema, IRPCSpec, IRPCSpecStore, IRPCStatus, IRPCStreamInit, IRPCStub, IRPCStubStore, StreamCleanup, StreamConstructor, TransportConfig } from "./types.js";
7
8
  import { RemoteState, stream } from "./state.js";
8
9
  import { IRPCReader } from "./reader.js";
9
10
  import { DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, IRPCCall } from "./call.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
+ import { createContext, createContextStore, getAbortController, getAbortSignal, getContext, setContext, setContextProvider, withContext } from "./context.js";
12
+ import { createCredentials, credential, getCredentials } from "./credential.js";
13
+ import { CALL_ERROR, CallError, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCError, IRPCErrorType, IRPC_ERROR_TYPE, RESOLVE_ERROR, ResolveError, STUB_ERROR, StubError, TRANSPORT_ERROR, TransportError } from "./error.js";
13
14
  import { IRPCResolver } from "./resolver.js";
14
15
  import { IRPCHook, IRPCRouter } from "./router.js";
15
16
  import { IRPCStream } from "./stream.js";
16
17
  import { IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPC_STORE } from "./store.js";
17
18
  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 };
19
+ export { CALL_ERROR, CallError, DEFAULT_RETRY_DELAY, DEFAULT_RETRY_MODE, HANDLER_ERROR, HOOK_ERROR, HandlerError, HookError, IRPCArraySchema, IRPCCacheEntry, IRPCCacher, IRPCCall, IRPCCallConfig, IRPCContext, IRPCContextProvider, IRPCCredentials, IRPCCredentialsFactory, IRPCData, IRPCDataSchema, IRPCDeclareInit, IRPCDefined, IRPCError, IRPCErrorType, IRPCFile, IRPCFileMeta, IRPCFilePipe, IRPCFilePointer, IRPCFileQueue, IRPCFileState, IRPCFileStatus, IRPCFileStream, IRPCFileUnpipe, IRPCFunction, IRPCHandler, IRPCHook, IRPCHookArgs, IRPCInferInit, IRPCInit, IRPCInputs, 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, IRPCRouter, IRPCSchema, IRPCSpec, IRPCSpecHook, IRPCSpecStore, IRPCStatus, IRPCStore, IRPCStoreEvent, IRPCStoreSubscriber, IRPCStream, IRPCStreamInit, IRPCStub, IRPCStubStore, IRPCTransport, IRPC_BASE_CONTEXT, 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, encode, getAbortController, getAbortSignal, getContext, getCredentials, intercept, isFilePointer, plan, setContext, setContextProvider, stream, withContext };