@orpc/client 2.0.0-beta.4 → 2.0.0-beta.41
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/README.md +97 -94
- package/dist/adapters/fetch/index.d.mts +16 -33
- package/dist/adapters/fetch/index.d.ts +16 -33
- package/dist/adapters/fetch/index.mjs +33 -30
- package/dist/adapters/message-port/index.d.mts +13 -8
- package/dist/adapters/message-port/index.d.ts +13 -8
- package/dist/adapters/message-port/index.mjs +27 -17
- package/dist/adapters/standard/index.d.mts +9 -10
- package/dist/adapters/standard/index.d.ts +9 -10
- package/dist/adapters/standard/index.mjs +5 -5
- package/dist/adapters/websocket/index.d.mts +33 -18
- package/dist/adapters/websocket/index.d.ts +33 -18
- package/dist/adapters/websocket/index.mjs +45 -24
- package/dist/index.d.mts +62 -61
- package/dist/index.d.ts +62 -61
- package/dist/index.mjs +47 -54
- package/dist/plugins/index.d.mts +174 -13
- package/dist/plugins/index.d.ts +174 -13
- package/dist/plugins/index.mjs +580 -198
- package/dist/shared/{client.BdItY5DT.d.mts → client.BPa_Mmuh.d.mts} +25 -4
- package/dist/shared/{client.BdItY5DT.d.ts → client.BPa_Mmuh.d.ts} +25 -4
- package/dist/shared/{client.Cby_-GGh.d.mts → client.BjbdW5Rj.d.mts} +2 -2
- package/dist/shared/{client.DXhchJ84.mjs → client.Bstqyaro.mjs} +25 -30
- package/dist/shared/{client.Dnfj8jnT.mjs → client.DD2eNne_.mjs} +20 -15
- package/dist/shared/{client.CPF3hX6O.d.ts → client.DiHfJmHK.d.ts} +2 -2
- package/dist/shared/client.DpkGsZt-.mjs +486 -0
- package/dist/shared/{client.8ug8I-zu.d.mts → client.Dw9_40WO.d.mts} +57 -16
- package/dist/shared/{client.8ug8I-zu.d.ts → client.Dw9_40WO.d.ts} +57 -16
- package/package.json +22 -8
- package/dist/shared/client.DMXKFDyV.mjs +0 -343
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PromiseWithError, Registry, MaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
+
import { StandardResponse } from '@standard-server/core';
|
|
2
3
|
|
|
3
4
|
interface ClientContext {
|
|
4
5
|
[key: PropertyKey]: any;
|
|
@@ -21,6 +22,11 @@ type NestedClient<TClientContext extends ClientContext> = Client<TClientContext,
|
|
|
21
22
|
[k: string]: NestedClient<TClientContext>;
|
|
22
23
|
};
|
|
23
24
|
type AnyNestedClient = NestedClient<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Infers the **client context type** required by a client.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-context | Client-Side Clients - Infer Client Context}
|
|
29
|
+
*/
|
|
24
30
|
type InferClientContext<T extends AnyNestedClient> = T extends NestedClient<infer U> ? U : never;
|
|
25
31
|
interface ClientLink<TClientContext extends ClientContext> {
|
|
26
32
|
call: (path: string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
@@ -29,6 +35,8 @@ interface ClientLink<TClientContext extends ClientContext> {
|
|
|
29
35
|
* Recursively infers the **input types** from a client.
|
|
30
36
|
*
|
|
31
37
|
* Produces a nested map where each endpoint's input type is preserved.
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-inputs | Client-Side Clients - Infer Client Inputs}
|
|
32
40
|
*/
|
|
33
41
|
type InferClientInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U : {
|
|
34
42
|
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientInputs<T[K]> : never;
|
|
@@ -38,6 +46,8 @@ type InferClientInputs<T extends AnyNestedClient> = T extends Client<any, infer
|
|
|
38
46
|
*
|
|
39
47
|
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
40
48
|
* Produces a nested map of body input types.
|
|
49
|
+
*
|
|
50
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-inputs | Client-Side Clients - Infer Client Body Inputs}
|
|
41
51
|
*/
|
|
42
52
|
type InferClientBodyInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U extends {
|
|
43
53
|
body: infer UBody;
|
|
@@ -48,6 +58,8 @@ type InferClientBodyInputs<T extends AnyNestedClient> = T extends Client<any, in
|
|
|
48
58
|
* Recursively infers the **output types** from a client.
|
|
49
59
|
*
|
|
50
60
|
* Produces a nested map where each endpoint's output type is preserved.
|
|
61
|
+
*
|
|
62
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-outputs | Client-Side Clients - Infer Client Outputs}
|
|
51
63
|
*/
|
|
52
64
|
type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U : {
|
|
53
65
|
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientOutputs<T[K]> : never;
|
|
@@ -57,6 +69,8 @@ type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any, any,
|
|
|
57
69
|
*
|
|
58
70
|
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
59
71
|
* Produces a nested map of body output types.
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-outputs | Client-Side Clients - Infer Client Body Outputs}
|
|
60
74
|
*/
|
|
61
75
|
type InferClientBodyOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U extends {
|
|
62
76
|
body: infer UBody;
|
|
@@ -64,22 +78,33 @@ type InferClientBodyOutputs<T extends AnyNestedClient> = T extends Client<any, a
|
|
|
64
78
|
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyOutputs<T[K]> : never;
|
|
65
79
|
};
|
|
66
80
|
/**
|
|
67
|
-
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#
|
|
81
|
+
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
|
|
68
82
|
*
|
|
69
83
|
* Produces a nested map where each endpoint's error type is preserved.
|
|
84
|
+
*
|
|
85
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-errors | Client-Side Clients - Infer Client Errors}
|
|
70
86
|
*/
|
|
71
87
|
type InferClientErrors<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
|
|
72
88
|
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientErrors<T[K]> : never;
|
|
73
89
|
};
|
|
74
90
|
/**
|
|
75
|
-
* Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#
|
|
91
|
+
* Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
|
|
76
92
|
*
|
|
77
93
|
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
94
|
+
*
|
|
95
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-error | Client-Side Clients - Infer Client Error}
|
|
78
96
|
*/
|
|
79
97
|
type InferClientError<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
|
|
80
98
|
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientError<T[K]> : never;
|
|
81
99
|
}[keyof T];
|
|
82
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Default mapping between common oRPC error codes and HTTP status codes.
|
|
103
|
+
* Handlers use it to determine response status codes; spread it to build a custom `errorStatusMap`.
|
|
104
|
+
*
|
|
105
|
+
* @see {@link https://orpc.dev/docs/rpc/handler#custom-error-response | RPC Handler - Custom Error Response}
|
|
106
|
+
* @see {@link https://orpc.dev/docs/openapi/handler#custom-error-response | OpenAPI Handler - Custom Error Response}
|
|
107
|
+
*/
|
|
83
108
|
declare const COMMON_ERROR_STATUS_MAP: {
|
|
84
109
|
BAD_REQUEST: number;
|
|
85
110
|
UNAUTHORIZED: number;
|
|
@@ -91,6 +116,7 @@ declare const COMMON_ERROR_STATUS_MAP: {
|
|
|
91
116
|
TIMEOUT: number;
|
|
92
117
|
CONFLICT: number;
|
|
93
118
|
GONE: number;
|
|
119
|
+
LENGTH_REQUIRED: number;
|
|
94
120
|
PRECONDITION_FAILED: number;
|
|
95
121
|
PAYLOAD_TOO_LARGE: number;
|
|
96
122
|
UNSUPPORTED_MEDIA_TYPE: number;
|
|
@@ -114,24 +140,26 @@ type ORPCErrorOptions<TData> = ErrorOptions & {
|
|
|
114
140
|
} : {
|
|
115
141
|
data: TData;
|
|
116
142
|
});
|
|
143
|
+
/**
|
|
144
|
+
* Typed error carrying a `code`, a `message`, and optional `data`.
|
|
145
|
+
* Throw it from handlers or middleware to produce typed error responses on the client.
|
|
146
|
+
*
|
|
147
|
+
* @see {@link https://orpc.dev/docs/error-handling#orpcerror-class | Error Handling - ORPCError Class}
|
|
148
|
+
*/
|
|
117
149
|
declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
118
150
|
/**
|
|
119
|
-
* @
|
|
120
|
-
* The `__branch` property is used for type branding, helping TypeScript distinguish
|
|
151
|
+
* @remarks
|
|
152
|
+
* **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
|
|
121
153
|
* an `ORPCError` instance from plain objects with a similar structure.
|
|
122
154
|
*/
|
|
123
155
|
readonly name: "ORPCError" & {
|
|
124
156
|
__branch: "ORPCError";
|
|
125
157
|
};
|
|
126
158
|
/**
|
|
127
|
-
* Indicates whether the error matches a definition in the procedure's `.errors` map
|
|
159
|
+
* Indicates whether the error matches a definition in the procedure's `.errors` map,
|
|
160
|
+
* which makes its type inferable on the client.
|
|
128
161
|
*/
|
|
129
162
|
readonly defined: boolean;
|
|
130
|
-
/**
|
|
131
|
-
* Indicates whether the error's type is inferable at the TypeScript level.
|
|
132
|
-
* This is typically true when the error is explicitly defined or returned within a handler.
|
|
133
|
-
*/
|
|
134
|
-
readonly inferable: boolean;
|
|
135
163
|
code: TCode;
|
|
136
164
|
data: TData;
|
|
137
165
|
constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
|
|
@@ -155,13 +183,26 @@ interface ORPCErrorJSON<TCode extends string, TData> extends Pick<ORPCError<TCod
|
|
|
155
183
|
* remove readonly
|
|
156
184
|
*/
|
|
157
185
|
defined: boolean;
|
|
158
|
-
/**
|
|
159
|
-
* remove readonly
|
|
160
|
-
*/
|
|
161
|
-
inferable: boolean;
|
|
162
186
|
}
|
|
163
187
|
type AnyORPCError = ORPCError<any, any>;
|
|
164
188
|
type AnyORPCErrorJSON = ORPCErrorJSON<any, any>;
|
|
189
|
+
interface MalformedResponseErrorOptions extends ErrorOptions {
|
|
190
|
+
message?: string;
|
|
191
|
+
response: StandardResponse;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Error indicating a response does not follow the expected oRPC format, carrying
|
|
195
|
+
* the resolved response. Found as the `cause` of a `MALFORMED_ORPC_RESPONSE`
|
|
196
|
+
* `ORPCError`.
|
|
197
|
+
*
|
|
198
|
+
* @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
|
|
199
|
+
* @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
|
|
200
|
+
*/
|
|
201
|
+
declare class MalformedResponseError extends Error {
|
|
202
|
+
readonly name = "MalformedResponseError";
|
|
203
|
+
response: StandardResponse;
|
|
204
|
+
constructor(options: MalformedResponseErrorOptions);
|
|
205
|
+
}
|
|
165
206
|
|
|
166
|
-
export { ORPCError as
|
|
167
|
-
export type { AnyORPCError as A, ClientContext as C, FriendlyClientOptions as F, InferClientContext as I, NestedClient as N, ORPCErrorCode as O, ClientOptions as a, ClientLink as b,
|
|
207
|
+
export { ORPCError as h, COMMON_ERROR_STATUS_MAP as j, MalformedResponseError as p };
|
|
208
|
+
export type { AnyORPCError as A, ClientContext as C, FriendlyClientOptions as F, InferClientContext as I, MalformedResponseErrorOptions as M, NestedClient as N, ORPCErrorCode as O, ClientOptions as a, ClientLink as b, AnyNestedClient as c, InferClientError as d, Client as e, ClientRest as f, ORPCErrorJSON as g, AnyORPCErrorJSON as i, InferClientBodyInputs as k, InferClientBodyOutputs as l, InferClientErrors as m, InferClientInputs as n, InferClientOutputs as o, ORPCErrorOptions as q };
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.41",
|
|
5
|
+
"description": "Consume oRPC and OpenAPI APIs with end-to-end type safety over HTTP, WebSocket, or message ports",
|
|
5
6
|
"license": "MIT",
|
|
7
|
+
"funding": [
|
|
8
|
+
"https://github.com/sponsors/dinwwwh",
|
|
9
|
+
"https://opencollective.com/middleapi"
|
|
10
|
+
],
|
|
6
11
|
"homepage": "https://orpc.dev",
|
|
7
12
|
"repository": {
|
|
8
13
|
"type": "git",
|
|
@@ -10,7 +15,15 @@
|
|
|
10
15
|
"directory": "packages/client"
|
|
11
16
|
},
|
|
12
17
|
"keywords": [
|
|
13
|
-
"orpc"
|
|
18
|
+
"orpc",
|
|
19
|
+
"rpc",
|
|
20
|
+
"api",
|
|
21
|
+
"typescript",
|
|
22
|
+
"typesafe",
|
|
23
|
+
"client",
|
|
24
|
+
"fetch",
|
|
25
|
+
"websocket",
|
|
26
|
+
"openapi"
|
|
14
27
|
],
|
|
15
28
|
"sideEffects": false,
|
|
16
29
|
"exports": {
|
|
@@ -50,16 +63,17 @@
|
|
|
50
63
|
"dist"
|
|
51
64
|
],
|
|
52
65
|
"dependencies": {
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
66
|
+
"@orpc/shared": "2.0.0-beta.41",
|
|
67
|
+
"@standard-server/core": "~0.10.0",
|
|
68
|
+
"@standard-server/fetch": "~0.10.0",
|
|
69
|
+
"@standard-server/peer": "~0.10.0"
|
|
57
70
|
},
|
|
58
71
|
"devDependencies": {
|
|
59
|
-
"
|
|
72
|
+
"@types/ws": "^8.18.1",
|
|
73
|
+
"ws": "^8.21.3",
|
|
74
|
+
"zod": "^4.5.4"
|
|
60
75
|
},
|
|
61
76
|
"scripts": {
|
|
62
|
-
"build": "unbuild",
|
|
63
77
|
"type:check": "tsc -b"
|
|
64
78
|
}
|
|
65
79
|
}
|
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { isPlainObject, wrapAsyncIterator, isTypescriptObject, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
|
2
|
-
import { getEventMeta, withEventMeta, ErrorEvent } from '@standardserver/core';
|
|
3
|
-
import { O as ORPCError } from './client.Dnfj8jnT.mjs';
|
|
4
|
-
|
|
5
|
-
function isInferableError(error) {
|
|
6
|
-
return error instanceof ORPCError && error.inferable;
|
|
7
|
-
}
|
|
8
|
-
function toORPCError(error) {
|
|
9
|
-
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
10
|
-
}
|
|
11
|
-
function isORPCErrorJson(json) {
|
|
12
|
-
if (!isPlainObject(json)) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
const validKeys = ["defined", "inferable", "code", "message", "data"];
|
|
16
|
-
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return "defined" in json && typeof json.defined === "boolean" && "inferable" in json && typeof json.inferable === "boolean" && "code" in json && typeof json.code === "string" && "message" in json && typeof json.message === "string";
|
|
20
|
-
}
|
|
21
|
-
function createORPCErrorFromJson(json, options = {}) {
|
|
22
|
-
const error = new ORPCError(json.code, {
|
|
23
|
-
...json,
|
|
24
|
-
...options
|
|
25
|
-
});
|
|
26
|
-
error.defined = json.defined;
|
|
27
|
-
error.inferable = json.inferable;
|
|
28
|
-
return error;
|
|
29
|
-
}
|
|
30
|
-
function cloneORPCError(error) {
|
|
31
|
-
const cloned = new ORPCError(error.code, {
|
|
32
|
-
...error,
|
|
33
|
-
message: error.message,
|
|
34
|
-
data: error.data,
|
|
35
|
-
cause: error.cause
|
|
36
|
-
});
|
|
37
|
-
cloned.stack = error.stack;
|
|
38
|
-
cloned.defined = error.defined;
|
|
39
|
-
cloned.inferable = error.inferable;
|
|
40
|
-
return cloned;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function wrapEventIteratorPreservingMeta(iterator, { mapResult, mapError, ...rest }) {
|
|
44
|
-
return wrapAsyncIterator(iterator, {
|
|
45
|
-
...rest,
|
|
46
|
-
mapResult: mapResult && (async (result) => {
|
|
47
|
-
const mapped = await mapResult(result);
|
|
48
|
-
if (mapped.value !== result.value) {
|
|
49
|
-
const meta = getEventMeta(result.value);
|
|
50
|
-
if (meta && isTypescriptObject(mapped.value)) {
|
|
51
|
-
return { done: mapped.done, value: withEventMeta(mapped.value, meta) };
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return mapped;
|
|
55
|
-
}),
|
|
56
|
-
mapError: mapError && (async (error) => {
|
|
57
|
-
const mapped = await mapError(error);
|
|
58
|
-
if (mapped !== error) {
|
|
59
|
-
const meta = getEventMeta(error);
|
|
60
|
-
if (meta && isTypescriptObject(mapped)) {
|
|
61
|
-
return withEventMeta(mapped, meta);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return mapped;
|
|
65
|
-
})
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const REGEX_STRING_PATTERN = /^\/(.*)\/([a-z]*)$/;
|
|
70
|
-
const DEFAULT_RPC_JSON_SERIALIZER_HANDLERS = {
|
|
71
|
-
undefined: {
|
|
72
|
-
condition(data) {
|
|
73
|
-
return data === void 0;
|
|
74
|
-
},
|
|
75
|
-
serialize() {
|
|
76
|
-
return null;
|
|
77
|
-
},
|
|
78
|
-
deserialize() {
|
|
79
|
-
return void 0;
|
|
80
|
-
},
|
|
81
|
-
isTerminal: true
|
|
82
|
-
},
|
|
83
|
-
bigint: {
|
|
84
|
-
condition(data) {
|
|
85
|
-
return typeof data === "bigint";
|
|
86
|
-
},
|
|
87
|
-
serialize(data) {
|
|
88
|
-
return data.toString();
|
|
89
|
-
},
|
|
90
|
-
deserialize(serialized) {
|
|
91
|
-
return BigInt(serialized);
|
|
92
|
-
},
|
|
93
|
-
isTerminal: true
|
|
94
|
-
},
|
|
95
|
-
date: {
|
|
96
|
-
condition(data) {
|
|
97
|
-
return data instanceof Date;
|
|
98
|
-
},
|
|
99
|
-
serialize(data) {
|
|
100
|
-
if (Number.isNaN(data.getTime())) {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
return data.toISOString();
|
|
104
|
-
},
|
|
105
|
-
deserialize(serialized) {
|
|
106
|
-
return new Date(serialized ?? "Invalid Date");
|
|
107
|
-
},
|
|
108
|
-
isTerminal: true
|
|
109
|
-
},
|
|
110
|
-
nan: {
|
|
111
|
-
condition(data) {
|
|
112
|
-
return typeof data === "number" && Number.isNaN(data);
|
|
113
|
-
},
|
|
114
|
-
serialize() {
|
|
115
|
-
return null;
|
|
116
|
-
},
|
|
117
|
-
deserialize() {
|
|
118
|
-
return Number.NaN;
|
|
119
|
-
},
|
|
120
|
-
isTerminal: true
|
|
121
|
-
},
|
|
122
|
-
url: {
|
|
123
|
-
condition(data) {
|
|
124
|
-
return data instanceof URL;
|
|
125
|
-
},
|
|
126
|
-
serialize(data) {
|
|
127
|
-
return data.toString();
|
|
128
|
-
},
|
|
129
|
-
deserialize(serialized) {
|
|
130
|
-
return new URL(serialized);
|
|
131
|
-
},
|
|
132
|
-
isTerminal: true
|
|
133
|
-
},
|
|
134
|
-
regexp: {
|
|
135
|
-
condition(data) {
|
|
136
|
-
return data instanceof RegExp;
|
|
137
|
-
},
|
|
138
|
-
serialize(data) {
|
|
139
|
-
return data.toString();
|
|
140
|
-
},
|
|
141
|
-
deserialize(serialized) {
|
|
142
|
-
const [, pattern, flags] = serialized.match(REGEX_STRING_PATTERN);
|
|
143
|
-
return new RegExp(pattern, flags);
|
|
144
|
-
},
|
|
145
|
-
isTerminal: true
|
|
146
|
-
},
|
|
147
|
-
set: {
|
|
148
|
-
condition(data) {
|
|
149
|
-
return data instanceof Set;
|
|
150
|
-
},
|
|
151
|
-
serialize(data) {
|
|
152
|
-
return Array.from(data);
|
|
153
|
-
},
|
|
154
|
-
deserialize(serialized) {
|
|
155
|
-
return new Set(serialized);
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
map: {
|
|
159
|
-
condition(data) {
|
|
160
|
-
return data instanceof Map;
|
|
161
|
-
},
|
|
162
|
-
serialize(data) {
|
|
163
|
-
return Array.from(data.entries());
|
|
164
|
-
},
|
|
165
|
-
deserialize(serialized) {
|
|
166
|
-
return new Map(serialized);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
class RPCJsonSerializer {
|
|
171
|
-
handlers;
|
|
172
|
-
omitUndefinedProperties;
|
|
173
|
-
constructor(options = {}) {
|
|
174
|
-
this.handlers = {
|
|
175
|
-
...DEFAULT_RPC_JSON_SERIALIZER_HANDLERS,
|
|
176
|
-
...options.handlers
|
|
177
|
-
};
|
|
178
|
-
this.omitUndefinedProperties = options.omitUndefinedProperties !== false;
|
|
179
|
-
}
|
|
180
|
-
serialize(data) {
|
|
181
|
-
const [json, meta_, maps, blobs] = this.serializeValue(data, [], [], [], []);
|
|
182
|
-
const meta = meta_.length === 0 ? void 0 : meta_;
|
|
183
|
-
if (maps.length === 0) {
|
|
184
|
-
return { json, meta };
|
|
185
|
-
}
|
|
186
|
-
return { json, meta, maps, blobs };
|
|
187
|
-
}
|
|
188
|
-
serializeValue(data, segments, meta, maps, blobs) {
|
|
189
|
-
for (const key in this.handlers) {
|
|
190
|
-
const handler = this.handlers[key];
|
|
191
|
-
if (handler && handler.condition(data)) {
|
|
192
|
-
const serialized = handler.serialize(data);
|
|
193
|
-
if (handler.isTerminal) {
|
|
194
|
-
meta.push([key, ...segments]);
|
|
195
|
-
return [serialized, meta, maps, blobs];
|
|
196
|
-
}
|
|
197
|
-
const result = this.serializeValue(serialized, segments, meta, maps, blobs);
|
|
198
|
-
meta.push([key, ...segments]);
|
|
199
|
-
return result;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
if (data instanceof Blob) {
|
|
203
|
-
maps.push(segments);
|
|
204
|
-
blobs.push(data);
|
|
205
|
-
return [data, meta, maps, blobs];
|
|
206
|
-
}
|
|
207
|
-
if (Array.isArray(data)) {
|
|
208
|
-
const json = data.map((v, i) => {
|
|
209
|
-
return this.serializeValue(v, [...segments, i], meta, maps, blobs)[0];
|
|
210
|
-
});
|
|
211
|
-
return [json, meta, maps, blobs];
|
|
212
|
-
}
|
|
213
|
-
if (isPlainObject(data)) {
|
|
214
|
-
const json = {};
|
|
215
|
-
for (const k in data) {
|
|
216
|
-
const v = data[k];
|
|
217
|
-
if (k === "toJSON" && typeof v === "function") {
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
if (v === void 0 && this.omitUndefinedProperties) {
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
json[k] = this.serializeValue(v, [...segments, k], meta, maps, blobs)[0];
|
|
224
|
-
}
|
|
225
|
-
return [json, meta, maps, blobs];
|
|
226
|
-
}
|
|
227
|
-
return [data, meta, maps, blobs];
|
|
228
|
-
}
|
|
229
|
-
deserialize(serialized) {
|
|
230
|
-
const ref = { data: serialized.json };
|
|
231
|
-
if (serialized.blobs?.length) {
|
|
232
|
-
serialized.maps.forEach((segments, i) => {
|
|
233
|
-
let currentRef = ref;
|
|
234
|
-
let preSegment = "data";
|
|
235
|
-
segments.forEach((segment) => {
|
|
236
|
-
currentRef = currentRef[preSegment];
|
|
237
|
-
preSegment = segment;
|
|
238
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
239
|
-
throw new Error(`Security error: Invalid serialized data. Segment "${preSegment}" does not exist.`);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
currentRef[preSegment] = serialized.blobs[i];
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
serialized.meta?.forEach((item) => {
|
|
246
|
-
const type = item[0];
|
|
247
|
-
let currentRef = ref;
|
|
248
|
-
let preSegment = "data";
|
|
249
|
-
for (let i = 1; i < item.length; i++) {
|
|
250
|
-
currentRef = currentRef[preSegment];
|
|
251
|
-
preSegment = item[i];
|
|
252
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
253
|
-
throw new Error(`Security error: Invalid serialized data. Segment "${preSegment}" does not exist.`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
currentRef[preSegment] = this.handlers[type].deserialize(currentRef[preSegment]);
|
|
257
|
-
});
|
|
258
|
-
return ref.data;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
class RPCSerializer {
|
|
263
|
-
jsonSerializer;
|
|
264
|
-
defaultSerializeOptions;
|
|
265
|
-
constructor(options = {}) {
|
|
266
|
-
this.jsonSerializer = new RPCJsonSerializer(options);
|
|
267
|
-
this.defaultSerializeOptions = options.serialize;
|
|
268
|
-
}
|
|
269
|
-
serialize(data, options = {}) {
|
|
270
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
271
|
-
return data;
|
|
272
|
-
}
|
|
273
|
-
if (isAsyncIteratorObject(data)) {
|
|
274
|
-
return wrapEventIteratorPreservingMeta(data, {
|
|
275
|
-
mapResult: (result) => {
|
|
276
|
-
if (result.value === void 0) {
|
|
277
|
-
return result;
|
|
278
|
-
}
|
|
279
|
-
return { done: result.done, value: this.serializeValue(result.value, options) };
|
|
280
|
-
},
|
|
281
|
-
mapError: (e) => new ErrorEvent(
|
|
282
|
-
this.serializeValue(toORPCError(e).toJSON(), { ...options, useFormDataForBlobFields: false }),
|
|
283
|
-
{ cause: e }
|
|
284
|
-
)
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
return this.serializeValue(data, options);
|
|
288
|
-
}
|
|
289
|
-
serializeValue(data, options) {
|
|
290
|
-
const useFormDataForBlobs = options.useFormDataForBlobFields ?? this.defaultSerializeOptions?.useFormDataForBlobFields ?? true;
|
|
291
|
-
const { json, meta, maps, blobs } = this.jsonSerializer.serialize(data);
|
|
292
|
-
if (!useFormDataForBlobs || !blobs?.length) {
|
|
293
|
-
return { json, meta };
|
|
294
|
-
}
|
|
295
|
-
const form = new FormData();
|
|
296
|
-
form.set("data", stringifyJSON({ json, meta, maps }));
|
|
297
|
-
blobs.forEach((blob, i) => {
|
|
298
|
-
form.set(i.toString(), blob);
|
|
299
|
-
});
|
|
300
|
-
return form;
|
|
301
|
-
}
|
|
302
|
-
deserialize(data) {
|
|
303
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
304
|
-
return data;
|
|
305
|
-
}
|
|
306
|
-
if (isAsyncIteratorObject(data)) {
|
|
307
|
-
return wrapEventIteratorPreservingMeta(data, {
|
|
308
|
-
mapResult: (result) => {
|
|
309
|
-
if (result.value === void 0) {
|
|
310
|
-
return result;
|
|
311
|
-
}
|
|
312
|
-
return { done: result.done, value: this.deserializeValue(result.value) };
|
|
313
|
-
},
|
|
314
|
-
mapError: (e) => {
|
|
315
|
-
if (!(e instanceof ErrorEvent)) {
|
|
316
|
-
return e;
|
|
317
|
-
}
|
|
318
|
-
const deserialized = this.deserializeValue(e.data);
|
|
319
|
-
if (isORPCErrorJson(deserialized)) {
|
|
320
|
-
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
321
|
-
}
|
|
322
|
-
return new ErrorEvent(deserialized, { cause: e });
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
return this.deserializeValue(data);
|
|
327
|
-
}
|
|
328
|
-
deserializeValue(data) {
|
|
329
|
-
if (!(data instanceof FormData)) {
|
|
330
|
-
return this.jsonSerializer.deserialize(data);
|
|
331
|
-
}
|
|
332
|
-
const serialized = JSON.parse(data.get("data"));
|
|
333
|
-
const blobs = [];
|
|
334
|
-
for (const [key, value] of data.entries()) {
|
|
335
|
-
if (value instanceof Blob) {
|
|
336
|
-
blobs[Number(key)] = value;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return this.jsonSerializer.deserialize({ ...serialized, blobs });
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
export { RPCSerializer as R, isInferableError as a, RPCJsonSerializer as b, createORPCErrorFromJson as c, cloneORPCError as d, isORPCErrorJson as i, toORPCError as t, wrapEventIteratorPreservingMeta as w };
|