@orpc/client 0.0.0-next.6c1c681 → 0.0.0-next.6c5f175

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.
@@ -1,59 +1,78 @@
1
- import { intercept, isAsyncIteratorObject, value, isObject, trim, stringifyJSON } from '@orpc/shared';
2
- import { c as createAutoRetryEventIterator, O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.XAn8cDTM.mjs';
3
- import { ErrorEvent } from '@orpc/standard-server';
1
+ import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
2
+ import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
3
+ import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, m as mapEventIterator, t as toORPCError } from './client.CgrbfYAp.mjs';
4
+ import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
4
5
 
5
- class InvalidEventIteratorRetryResponse extends Error {
6
+ class CompositeStandardLinkPlugin {
7
+ plugins;
8
+ constructor(plugins = []) {
9
+ this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
10
+ }
11
+ init(options) {
12
+ for (const plugin of this.plugins) {
13
+ plugin.init?.(options);
14
+ }
15
+ }
6
16
  }
17
+
7
18
  class StandardLink {
8
- constructor(codec, sender, options) {
19
+ constructor(codec, sender, options = {}) {
9
20
  this.codec = codec;
10
21
  this.sender = sender;
11
- this.eventIteratorMaxRetries = options.eventIteratorMaxRetries ?? 5;
12
- this.eventIteratorRetryDelay = options.eventIteratorRetryDelay ?? ((o) => o.lastRetry ?? 1e3 * 2 ** o.retryTimes);
13
- this.eventIteratorShouldRetry = options.eventIteratorShouldRetry ?? true;
14
- this.interceptors = options.interceptors ?? [];
15
- this.clientInterceptors = options.clientInterceptors ?? [];
22
+ const plugin = new CompositeStandardLinkPlugin(options.plugins);
23
+ plugin.init(options);
24
+ this.interceptors = toArray(options.interceptors);
25
+ this.clientInterceptors = toArray(options.clientInterceptors);
16
26
  }
17
- eventIteratorMaxRetries;
18
- eventIteratorRetryDelay;
19
- eventIteratorShouldRetry;
20
27
  interceptors;
21
28
  clientInterceptors;
22
29
  call(path, input, options) {
23
- return intercept(this.interceptors, { path, input, options }, async ({ path: path2, input: input2, options: options2 }) => {
24
- const output = await this.#call(path2, input2, options2);
25
- if (!isAsyncIteratorObject(output)) {
26
- return output;
27
- }
28
- return createAutoRetryEventIterator(output, async (reconnectOptions) => {
29
- const maxRetries = await value(this.eventIteratorMaxRetries, reconnectOptions, options2, path2, input2);
30
- if (options2.signal?.aborted || reconnectOptions.retryTimes > maxRetries) {
31
- return null;
32
- }
33
- const shouldRetry = await value(this.eventIteratorShouldRetry, reconnectOptions, options2, path2, input2);
34
- if (!shouldRetry) {
35
- return null;
30
+ return runWithSpan(
31
+ { name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
32
+ (span) => {
33
+ span?.setAttribute("rpc.system", ORPC_NAME);
34
+ span?.setAttribute("rpc.method", path.join("."));
35
+ if (isAsyncIteratorObject(input)) {
36
+ input = asyncIteratorWithSpan(
37
+ { name: "consume_event_iterator_input", signal: options.signal },
38
+ input
39
+ );
36
40
  }
37
- const retryDelay = await value(this.eventIteratorRetryDelay, reconnectOptions, options2, path2, input2);
38
- await new Promise((resolve) => setTimeout(resolve, retryDelay));
39
- const updatedOptions = { ...options2, lastEventId: reconnectOptions.lastEventId };
40
- const maybeIterator = await this.#call(path2, input2, updatedOptions);
41
- if (!isAsyncIteratorObject(maybeIterator)) {
42
- throw new InvalidEventIteratorRetryResponse("Invalid Event Iterator retry response");
43
- }
44
- return maybeIterator;
45
- }, options2.lastEventId);
46
- });
47
- }
48
- async #call(path, input, options) {
49
- const request = await this.codec.encode(path, input, options);
50
- const response = await intercept(
51
- this.clientInterceptors,
52
- { request },
53
- ({ request: request2 }) => this.sender.call(request2, options, path, input)
41
+ return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
42
+ const otelConfig = getGlobalOtelConfig();
43
+ let otelContext;
44
+ const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
45
+ if (currentSpan && otelConfig) {
46
+ otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
47
+ }
48
+ const request = await runWithSpan(
49
+ { name: "encode_request", context: otelContext },
50
+ () => this.codec.encode(path2, input2, options2)
51
+ );
52
+ const response = await intercept(
53
+ this.clientInterceptors,
54
+ { ...options2, input: input2, path: path2, request },
55
+ ({ input: input3, path: path3, request: request2, ...options3 }) => {
56
+ return runWithSpan(
57
+ { name: "send_request", signal: options3.signal, context: otelContext },
58
+ () => this.sender.call(request2, options3, path3, input3)
59
+ );
60
+ }
61
+ );
62
+ const output = await runWithSpan(
63
+ { name: "decode_response", context: otelContext },
64
+ () => this.codec.decode(response, options2, path2, input2)
65
+ );
66
+ if (isAsyncIteratorObject(output)) {
67
+ return asyncIteratorWithSpan(
68
+ { name: "consume_event_iterator_output", signal: options2.signal },
69
+ output
70
+ );
71
+ }
72
+ return output;
73
+ });
74
+ }
54
75
  );
55
- const output = await this.codec.decode(response, options, path, input);
56
- return output;
57
76
  }
58
77
  }
59
78
 
@@ -134,6 +153,9 @@ class StandardRPCJsonSerializer {
134
153
  if (isObject(data)) {
135
154
  const json = {};
136
155
  for (const k in data) {
156
+ if (k === "toJSON" && typeof data[k] === "function") {
157
+ continue;
158
+ }
137
159
  json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
138
160
  }
139
161
  return [json, meta, maps, blobs];
@@ -200,6 +222,19 @@ class StandardRPCJsonSerializer {
200
222
  }
201
223
  }
202
224
 
225
+ function toHttpPath(path) {
226
+ return `/${path.map(encodeURIComponent).join("/")}`;
227
+ }
228
+ function toStandardHeaders(headers) {
229
+ if (typeof headers.forEach === "function") {
230
+ return toStandardHeaders$1(headers);
231
+ }
232
+ return headers;
233
+ }
234
+ function getMalformedResponseErrorCode(status) {
235
+ return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
236
+ }
237
+
203
238
  class StandardRPCLinkCodec {
204
239
  constructor(serializer, options) {
205
240
  this.serializer = serializer;
@@ -215,15 +250,19 @@ class StandardRPCLinkCodec {
215
250
  expectedMethod;
216
251
  headers;
217
252
  async encode(path, input, options) {
253
+ let headers = toStandardHeaders(await value(this.headers, options, path, input));
254
+ if (options.lastEventId !== void 0) {
255
+ headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
256
+ }
218
257
  const expectedMethod = await value(this.expectedMethod, options, path, input);
219
- const headers = await value(this.headers, options, path, input);
220
258
  const baseUrl = await value(this.baseUrl, options, path, input);
221
- const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
259
+ const url = new URL(baseUrl);
260
+ url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
222
261
  const serialized = this.serializer.serialize(input);
223
- if (expectedMethod === "GET" && !(serialized instanceof FormData) && !(serialized instanceof Blob) && !isAsyncIteratorObject(serialized)) {
262
+ if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
224
263
  const maxUrlLength = await value(this.maxUrlLength, options, path, input);
225
264
  const getUrl = new URL(url);
226
- getUrl.searchParams.append("data", stringifyJSON(serialized) ?? "");
265
+ getUrl.searchParams.append("data", stringifyJSON(serialized));
227
266
  if (getUrl.toString().length <= maxUrlLength) {
228
267
  return {
229
268
  body: void 0,
@@ -243,7 +282,7 @@ class StandardRPCLinkCodec {
243
282
  };
244
283
  }
245
284
  async decode(response) {
246
- const isOk = response.status >= 200 && response.status < 300;
285
+ const isOk = !isORPCErrorStatus(response.status);
247
286
  const deserialized = await (async () => {
248
287
  let isBodyOk = false;
249
288
  try {
@@ -262,11 +301,12 @@ class StandardRPCLinkCodec {
262
301
  }
263
302
  })();
264
303
  if (!isOk) {
265
- if (ORPCError.isValidJSON(deserialized)) {
266
- throw ORPCError.fromJSON(deserialized);
304
+ if (isORPCErrorJson(deserialized)) {
305
+ throw createORPCErrorFromJson(deserialized);
267
306
  }
268
- throw new Error("Invalid RPC error response format.", {
269
- cause: deserialized
307
+ throw new ORPCError(getMalformedResponseErrorCode(response.status), {
308
+ status: response.status,
309
+ data: { ...response, body: deserialized }
270
310
  });
271
311
  }
272
312
  return deserialized;
@@ -292,9 +332,6 @@ class StandardRPCSerializer {
292
332
  return this.#serialize(data, true);
293
333
  }
294
334
  #serialize(data, enableFormData) {
295
- if (data === void 0 || data instanceof Blob) {
296
- return data;
297
- }
298
335
  const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
299
336
  const meta = meta_.length === 0 ? void 0 : meta_;
300
337
  if (!enableFormData || blobs.length === 0) {
@@ -319,8 +356,8 @@ class StandardRPCSerializer {
319
356
  return e;
320
357
  }
321
358
  const deserialized = this.#deserialize(e.data);
322
- if (ORPCError.isValidJSON(deserialized)) {
323
- return ORPCError.fromJSON(deserialized, { cause: e });
359
+ if (isORPCErrorJson(deserialized)) {
360
+ return createORPCErrorFromJson(deserialized, { cause: e });
324
361
  }
325
362
  return new ErrorEvent({
326
363
  data: deserialized,
@@ -332,8 +369,8 @@ class StandardRPCSerializer {
332
369
  return this.#deserialize(data);
333
370
  }
334
371
  #deserialize(data) {
335
- if (data === void 0 || data instanceof Blob) {
336
- return data;
372
+ if (data === void 0) {
373
+ return void 0;
337
374
  }
338
375
  if (!(data instanceof FormData)) {
339
376
  return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
@@ -348,4 +385,13 @@ class StandardRPCSerializer {
348
385
  }
349
386
  }
350
387
 
351
- export { InvalidEventIteratorRetryResponse as I, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLinkCodec as c, StandardRPCSerializer as d };
388
+ class StandardRPCLink extends StandardLink {
389
+ constructor(linkClient, options) {
390
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
391
+ const serializer = new StandardRPCSerializer(jsonSerializer);
392
+ const linkCodec = new StandardRPCLinkCodec(serializer, options);
393
+ super(linkCodec, linkClient, options);
394
+ }
395
+ }
396
+
397
+ export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.6c1c681",
4
+ "version": "0.0.0-next.6c5f175",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -19,6 +19,11 @@
19
19
  "import": "./dist/index.mjs",
20
20
  "default": "./dist/index.mjs"
21
21
  },
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
+ },
22
27
  "./standard": {
23
28
  "types": "./dist/adapters/standard/index.d.mts",
24
29
  "import": "./dist/adapters/standard/index.mjs",
@@ -28,18 +33,29 @@
28
33
  "types": "./dist/adapters/fetch/index.d.mts",
29
34
  "import": "./dist/adapters/fetch/index.mjs",
30
35
  "default": "./dist/adapters/fetch/index.mjs"
36
+ },
37
+ "./websocket": {
38
+ "types": "./dist/adapters/websocket/index.d.mts",
39
+ "import": "./dist/adapters/websocket/index.mjs",
40
+ "default": "./dist/adapters/websocket/index.mjs"
41
+ },
42
+ "./message-port": {
43
+ "types": "./dist/adapters/message-port/index.d.mts",
44
+ "import": "./dist/adapters/message-port/index.mjs",
45
+ "default": "./dist/adapters/message-port/index.mjs"
31
46
  }
32
47
  },
33
48
  "files": [
34
49
  "dist"
35
50
  ],
36
51
  "dependencies": {
37
- "@orpc/shared": "0.0.0-next.6c1c681",
38
- "@orpc/standard-server": "0.0.0-next.6c1c681",
39
- "@orpc/standard-server-fetch": "0.0.0-next.6c1c681"
52
+ "@orpc/shared": "0.0.0-next.6c5f175",
53
+ "@orpc/standard-server": "0.0.0-next.6c5f175",
54
+ "@orpc/standard-server-fetch": "0.0.0-next.6c5f175",
55
+ "@orpc/standard-server-peer": "0.0.0-next.6c5f175"
40
56
  },
41
57
  "devDependencies": {
42
- "zod": "^3.24.2"
58
+ "zod": "^4.1.12"
43
59
  },
44
60
  "scripts": {
45
61
  "build": "unbuild",
@@ -1,42 +0,0 @@
1
- type ClientContext = Record<string, any>;
2
- type ClientOptions<TClientContext extends ClientContext> = {
3
- signal?: AbortSignal;
4
- lastEventId?: string | undefined;
5
- } & (Record<never, never> extends TClientContext ? {
6
- context?: TClientContext;
7
- } : {
8
- context: TClientContext;
9
- });
10
- type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
- type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
- __error?: {
13
- type: TError;
14
- };
15
- };
16
- interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
- (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
- }
19
- type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
- [k: string]: NestedClient<TClientContext>;
21
- };
22
- type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
- type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
- context: TClientContext;
25
- };
26
- interface ClientLink<TClientContext extends ClientContext> {
27
- call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
- }
29
-
30
- declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
31
- value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
32
- error: (error: unknown) => Promise<unknown>;
33
- }): AsyncGenerator<TMap, TMap, TNext>;
34
- interface EventIteratorReconnectOptions {
35
- lastRetry: number | undefined;
36
- lastEventId: string | undefined;
37
- retryTimes: number;
38
- error: unknown;
39
- }
40
- declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
41
-
42
- export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
@@ -1,42 +0,0 @@
1
- type ClientContext = Record<string, any>;
2
- type ClientOptions<TClientContext extends ClientContext> = {
3
- signal?: AbortSignal;
4
- lastEventId?: string | undefined;
5
- } & (Record<never, never> extends TClientContext ? {
6
- context?: TClientContext;
7
- } : {
8
- context: TClientContext;
9
- });
10
- type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
- type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
- __error?: {
13
- type: TError;
14
- };
15
- };
16
- interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
- (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
- }
19
- type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
- [k: string]: NestedClient<TClientContext>;
21
- };
22
- type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
- type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
- context: TClientContext;
25
- };
26
- interface ClientLink<TClientContext extends ClientContext> {
27
- call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
- }
29
-
30
- declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
31
- value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
32
- error: (error: unknown) => Promise<unknown>;
33
- }): AsyncGenerator<TMap, TMap, TNext>;
34
- interface EventIteratorReconnectOptions {
35
- lastRetry: number | undefined;
36
- lastEventId: string | undefined;
37
- retryTimes: number;
38
- error: unknown;
39
- }
40
- declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
41
-
42
- export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
@@ -1,266 +0,0 @@
1
- import { isObject, isTypescriptObject, retry } from '@orpc/shared';
2
- import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
-
4
- const COMMON_ORPC_ERROR_DEFS = {
5
- BAD_REQUEST: {
6
- status: 400,
7
- message: "Bad Request"
8
- },
9
- UNAUTHORIZED: {
10
- status: 401,
11
- message: "Unauthorized"
12
- },
13
- FORBIDDEN: {
14
- status: 403,
15
- message: "Forbidden"
16
- },
17
- NOT_FOUND: {
18
- status: 404,
19
- message: "Not Found"
20
- },
21
- METHOD_NOT_SUPPORTED: {
22
- status: 405,
23
- message: "Method Not Supported"
24
- },
25
- NOT_ACCEPTABLE: {
26
- status: 406,
27
- message: "Not Acceptable"
28
- },
29
- TIMEOUT: {
30
- status: 408,
31
- message: "Request Timeout"
32
- },
33
- CONFLICT: {
34
- status: 409,
35
- message: "Conflict"
36
- },
37
- PRECONDITION_FAILED: {
38
- status: 412,
39
- message: "Precondition Failed"
40
- },
41
- PAYLOAD_TOO_LARGE: {
42
- status: 413,
43
- message: "Payload Too Large"
44
- },
45
- UNSUPPORTED_MEDIA_TYPE: {
46
- status: 415,
47
- message: "Unsupported Media Type"
48
- },
49
- UNPROCESSABLE_CONTENT: {
50
- status: 422,
51
- message: "Unprocessable Content"
52
- },
53
- TOO_MANY_REQUESTS: {
54
- status: 429,
55
- message: "Too Many Requests"
56
- },
57
- CLIENT_CLOSED_REQUEST: {
58
- status: 499,
59
- message: "Client Closed Request"
60
- },
61
- INTERNAL_SERVER_ERROR: {
62
- status: 500,
63
- message: "Internal Server Error"
64
- },
65
- NOT_IMPLEMENTED: {
66
- status: 501,
67
- message: "Not Implemented"
68
- },
69
- BAD_GATEWAY: {
70
- status: 502,
71
- message: "Bad Gateway"
72
- },
73
- SERVICE_UNAVAILABLE: {
74
- status: 503,
75
- message: "Service Unavailable"
76
- },
77
- GATEWAY_TIMEOUT: {
78
- status: 504,
79
- message: "Gateway Timeout"
80
- }
81
- };
82
- function fallbackORPCErrorStatus(code, status) {
83
- return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
84
- }
85
- function fallbackORPCErrorMessage(code, message) {
86
- return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
87
- }
88
- class ORPCError extends Error {
89
- defined;
90
- code;
91
- status;
92
- data;
93
- constructor(code, ...[options]) {
94
- if (options?.status && (options.status < 400 || options.status >= 600)) {
95
- throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
96
- }
97
- const message = fallbackORPCErrorMessage(code, options?.message);
98
- super(message, options);
99
- this.code = code;
100
- this.status = fallbackORPCErrorStatus(code, options?.status);
101
- this.defined = options?.defined ?? false;
102
- this.data = options?.data;
103
- }
104
- toJSON() {
105
- return {
106
- defined: this.defined,
107
- code: this.code,
108
- status: this.status,
109
- message: this.message,
110
- data: this.data
111
- };
112
- }
113
- static fromJSON(json, options) {
114
- return new ORPCError(json.code, {
115
- ...options,
116
- ...json
117
- });
118
- }
119
- static isValidJSON(json) {
120
- if (!isObject(json)) {
121
- return false;
122
- }
123
- const validKeys = ["defined", "code", "status", "message", "data"];
124
- if (Object.keys(json).some((k) => !validKeys.includes(k))) {
125
- return false;
126
- }
127
- return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
128
- }
129
- }
130
- function isDefinedError(error) {
131
- return error instanceof ORPCError && error.defined;
132
- }
133
- function toORPCError(error) {
134
- return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
135
- message: "Internal server error",
136
- cause: error
137
- });
138
- }
139
-
140
- const iteratorStates = /* @__PURE__ */ new WeakMap();
141
- function registerEventIteratorState(iterator, state) {
142
- iteratorStates.set(iterator, state);
143
- }
144
- function updateEventIteratorStatus(state, status) {
145
- if (state.status !== status) {
146
- state.status = status;
147
- state.listeners.forEach((cb) => cb(status));
148
- }
149
- }
150
- function onEventIteratorStatusChange(iterator, callback, options = {}) {
151
- const notifyImmediately = options.notifyImmediately ?? true;
152
- const state = iteratorStates.get(iterator);
153
- if (!state) {
154
- throw new Error("Iterator is not registered.");
155
- }
156
- if (notifyImmediately) {
157
- callback(state.status);
158
- }
159
- state.listeners.push(callback);
160
- return () => {
161
- const index = state.listeners.indexOf(callback);
162
- if (index !== -1) {
163
- state.listeners.splice(index, 1);
164
- }
165
- };
166
- }
167
-
168
- function mapEventIterator(iterator, maps) {
169
- return async function* () {
170
- try {
171
- while (true) {
172
- const { done, value } = await iterator.next();
173
- let mappedValue = await maps.value(value, done);
174
- if (mappedValue !== value) {
175
- const meta = getEventMeta(value);
176
- if (meta && isTypescriptObject(mappedValue)) {
177
- mappedValue = withEventMeta(mappedValue, meta);
178
- }
179
- }
180
- if (done) {
181
- return mappedValue;
182
- }
183
- yield mappedValue;
184
- }
185
- } catch (error) {
186
- let mappedError = await maps.error(error);
187
- if (mappedError !== error) {
188
- const meta = getEventMeta(error);
189
- if (meta && isTypescriptObject(mappedError)) {
190
- mappedError = withEventMeta(mappedError, meta);
191
- }
192
- }
193
- throw mappedError;
194
- } finally {
195
- await iterator.return?.();
196
- }
197
- }();
198
- }
199
- const MAX_ALLOWED_RETRY_TIMES = 99;
200
- function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
201
- const state = {
202
- status: "connected",
203
- listeners: []
204
- };
205
- const iterator = async function* () {
206
- let current = initial;
207
- let lastEventId = initialLastEventId;
208
- let lastRetry;
209
- let retryTimes = 0;
210
- try {
211
- while (true) {
212
- try {
213
- updateEventIteratorStatus(state, "connected");
214
- const { done, value } = await current.next();
215
- const meta = getEventMeta(value);
216
- lastEventId = meta?.id ?? lastEventId;
217
- lastRetry = meta?.retry ?? lastRetry;
218
- retryTimes = 0;
219
- if (done) {
220
- return value;
221
- }
222
- yield value;
223
- } catch (e) {
224
- updateEventIteratorStatus(state, "reconnecting");
225
- const meta = getEventMeta(e);
226
- lastEventId = meta?.id ?? lastEventId;
227
- lastRetry = meta?.retry ?? lastRetry;
228
- let currentError = e;
229
- current = await retry({ times: MAX_ALLOWED_RETRY_TIMES }, async (exit) => {
230
- retryTimes += 1;
231
- if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
232
- throw exit(new Error(
233
- `Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event iterator. Possible infinite retry loop detected. Please review the retry logic.`,
234
- { cause: currentError }
235
- ));
236
- }
237
- const reconnected = await (async () => {
238
- try {
239
- return await reconnect({
240
- lastRetry,
241
- lastEventId,
242
- retryTimes,
243
- error: currentError
244
- });
245
- } catch (e2) {
246
- currentError = e2;
247
- throw e2;
248
- }
249
- })();
250
- if (!reconnected) {
251
- throw exit(currentError);
252
- }
253
- return reconnected;
254
- });
255
- }
256
- }
257
- } finally {
258
- updateEventIteratorStatus(state, "closed");
259
- await current.return?.();
260
- }
261
- }();
262
- registerEventIteratorState(iterator, state);
263
- return iterator;
264
- }
265
-
266
- export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, createAutoRetryEventIterator as c, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, onEventIteratorStatusChange as o, registerEventIteratorState as r, toORPCError as t, updateEventIteratorStatus as u };