@yorun-ai/vrpc 0.10.0 → 0.11.0
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 +15 -5
- package/README.zh-CN.md +15 -5
- package/dist/client.cjs +21 -291
- package/dist/client.d.cts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.es.js +4 -274
- package/dist/data-DOZk_tFi.js +395 -0
- package/dist/data-pemgw5-O.cjs +526 -0
- package/dist/error-payload-MmlaZW_V.js +15 -0
- package/dist/error-payload-dL9RvdTy.cjs +26 -0
- package/dist/http.cjs +1 -1
- package/dist/http.es.js +1 -1
- package/dist/protocol.cjs +18 -0
- package/dist/protocol.d.cts +127 -0
- package/dist/protocol.d.ts +127 -0
- package/dist/protocol.es.js +2 -0
- package/dist/{request-options-gljZwoeI.cjs → request-options-B_uQsyrU.cjs} +5 -16
- package/dist/{request-options-2g2EJjOi.js → request-options-CV6EPON5.js} +1 -12
- package/docs/examples/taro/taro-client.ts +159 -0
- package/docs/examples/taro/taro-transport.ts +77 -0
- package/docs/guides/protocol.md +77 -0
- package/docs/guides/taro.md +145 -0
- package/docs/guides/upgrading-0.11.md +45 -0
- package/docs/guides/usage.md +2 -0
- package/docs/maintainers/architecture.md +7 -1
- package/docs/reference/README.md +27 -5
- package/package.json +11 -1
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
const require_error_payload = require("./error-payload-dL9RvdTy.cjs");
|
|
2
|
+
//#region src/protocol/id.ts
|
|
3
|
+
var DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = "vrpc-client-instance-id";
|
|
4
|
+
function getRandomByteArray(length) {
|
|
5
|
+
const array = new Uint8Array(length);
|
|
6
|
+
if (globalThis.crypto?.getRandomValues) {
|
|
7
|
+
globalThis.crypto.getRandomValues(array);
|
|
8
|
+
return array;
|
|
9
|
+
}
|
|
10
|
+
for (let i = 0; i < length; i++) array[i] = Math.floor(Math.random() * 256);
|
|
11
|
+
return array;
|
|
12
|
+
}
|
|
13
|
+
function toHex(bytes) {
|
|
14
|
+
return Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("");
|
|
15
|
+
}
|
|
16
|
+
function newNonZeroHex(bytesLength, zeroValue) {
|
|
17
|
+
for (;;) {
|
|
18
|
+
const value = toHex(getRandomByteArray(bytesLength));
|
|
19
|
+
if (value !== zeroValue) return value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function generateVrpcId() {
|
|
23
|
+
return newNonZeroHex(16, "00000000000000000000000000000000");
|
|
24
|
+
}
|
|
25
|
+
function generateVrpcSpan() {
|
|
26
|
+
return newNonZeroHex(8, "0000000000000000");
|
|
27
|
+
}
|
|
28
|
+
function generateClientInstanceId() {
|
|
29
|
+
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID().toLowerCase();
|
|
30
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
|
|
31
|
+
const value = Math.floor(Math.random() * 16);
|
|
32
|
+
return (char === "x" ? value : value & 3 | 8).toString(16);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function getClientInstanceId({ key } = {}) {
|
|
36
|
+
const storageKey = key?.trim() || "vrpc-client-instance-id";
|
|
37
|
+
try {
|
|
38
|
+
const existingClientInstanceId = globalThis.localStorage.getItem(storageKey);
|
|
39
|
+
if (existingClientInstanceId) return existingClientInstanceId;
|
|
40
|
+
const clientInstanceId = generateClientInstanceId();
|
|
41
|
+
globalThis.localStorage.setItem(storageKey, clientInstanceId);
|
|
42
|
+
return clientInstanceId;
|
|
43
|
+
} catch {
|
|
44
|
+
return generateClientInstanceId();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/protocol/headers.ts
|
|
49
|
+
var VRPC_JSON_CONTENT_TYPE = "application/vrpc+json";
|
|
50
|
+
var VRPC_CBOR_CONTENT_TYPE = "application/vrpc+cbor";
|
|
51
|
+
var CLIENT_NAME_PATTERN = /^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$/;
|
|
52
|
+
var SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
53
|
+
var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
54
|
+
var TRACE_ID_PATTERN = /^[0-9a-f]{32}$/;
|
|
55
|
+
var TRACE_SPAN_PATTERN = /^[0-9a-f]{16}$/;
|
|
56
|
+
function assertClientInfo(clientInfo, scope = "client") {
|
|
57
|
+
if (!CLIENT_NAME_PATTERN.test(clientInfo.clientName)) throw new Error(`Invalid vRPC ${scope} name: ${clientInfo.clientName}`);
|
|
58
|
+
if (!SEMVER_PATTERN.test(clientInfo.clientVersion)) throw new Error(`Invalid vRPC ${scope} version: ${clientInfo.clientVersion}`);
|
|
59
|
+
if (!UUID_PATTERN.test(clientInfo.clientInstanceId)) throw new Error(`Invalid vRPC ${scope} instanceId: ${clientInfo.clientInstanceId}`);
|
|
60
|
+
}
|
|
61
|
+
function decodeDelimitedAppInfo(value) {
|
|
62
|
+
const fields = {};
|
|
63
|
+
for (const entry of value.split(",")) {
|
|
64
|
+
const separator = entry.indexOf("=");
|
|
65
|
+
if (separator <= 0) return null;
|
|
66
|
+
const key = entry.slice(0, separator).trim();
|
|
67
|
+
const fieldValue = entry.slice(separator + 1).trim();
|
|
68
|
+
if (!key || !fieldValue || key in fields) return null;
|
|
69
|
+
fields[key] = fieldValue;
|
|
70
|
+
}
|
|
71
|
+
if (Object.keys(fields).length !== 3) return null;
|
|
72
|
+
const name = fields.name;
|
|
73
|
+
const version = fields.version;
|
|
74
|
+
const instanceId = fields.instanceId;
|
|
75
|
+
if (!name || !version || !instanceId) return null;
|
|
76
|
+
return {
|
|
77
|
+
clientName: name,
|
|
78
|
+
clientVersion: version,
|
|
79
|
+
clientInstanceId: instanceId
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function assertValidVrpcServerHeader(value) {
|
|
83
|
+
if (!value) throw new Error("Invalid vRPC response: missing vrpc-server header.");
|
|
84
|
+
const serverInfo = decodeDelimitedAppInfo(value);
|
|
85
|
+
if (!serverInfo) throw new Error("Invalid vRPC response: invalid vrpc-server header.");
|
|
86
|
+
try {
|
|
87
|
+
assertClientInfo(serverInfo, "server");
|
|
88
|
+
} catch {
|
|
89
|
+
throw new Error("Invalid vRPC response: invalid vrpc-server header.");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function assertValidVrpcTrace(trace) {
|
|
93
|
+
if (!TRACE_ID_PATTERN.test(trace.id)) throw new Error(`Invalid vRPC trace id: ${trace.id}`);
|
|
94
|
+
if (trace.span !== void 0 && !TRACE_SPAN_PATTERN.test(trace.span)) throw new Error(`Invalid vRPC trace span: ${trace.span}`);
|
|
95
|
+
}
|
|
96
|
+
function assertValidVrpcTimeout(timeoutMs) {
|
|
97
|
+
if (timeoutMs !== void 0 && (!Number.isSafeInteger(timeoutMs) || !Number.isFinite(timeoutMs) || timeoutMs <= 0)) throw new Error(`Invalid vRPC timeoutMs: ${timeoutMs}`);
|
|
98
|
+
}
|
|
99
|
+
function isReservedVrpcHeader(name) {
|
|
100
|
+
const normalized = name.toLowerCase();
|
|
101
|
+
return normalized === "accept" || normalized === "content-type" || normalized.startsWith("vrpc-");
|
|
102
|
+
}
|
|
103
|
+
function assertNoReservedVrpcHeaders(...values) {
|
|
104
|
+
for (const value of values) {
|
|
105
|
+
if (!value) continue;
|
|
106
|
+
const headers = new Headers(value);
|
|
107
|
+
for (const name of headers.keys()) if (isReservedVrpcHeader(name)) throw new Error(`vRPC protocol header cannot be overridden: ${name}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function mediaTypeOf(value) {
|
|
111
|
+
return value?.split(";", 1)[0]?.trim().toLowerCase() || "";
|
|
112
|
+
}
|
|
113
|
+
function buildVrpcHeaderRecord({ clientInfo, trace, timeoutMs, requestUsesCbor = false, responseUsesCbor = false }) {
|
|
114
|
+
assertClientInfo(clientInfo);
|
|
115
|
+
assertValidVrpcTrace(trace);
|
|
116
|
+
assertValidVrpcTimeout(timeoutMs);
|
|
117
|
+
const headers = {
|
|
118
|
+
"content-type": requestUsesCbor ? VRPC_CBOR_CONTENT_TYPE : VRPC_JSON_CONTENT_TYPE,
|
|
119
|
+
accept: responseUsesCbor ? `${VRPC_CBOR_CONTENT_TYPE}, ${VRPC_JSON_CONTENT_TYPE}` : VRPC_JSON_CONTENT_TYPE,
|
|
120
|
+
"vrpc-client": `name=${clientInfo.clientName},version=${clientInfo.clientVersion},instanceId=${clientInfo.clientInstanceId}`,
|
|
121
|
+
"vrpc-trace": trace.span ? `id=${trace.id},span=${trace.span}` : `id=${trace.id}`
|
|
122
|
+
};
|
|
123
|
+
if (timeoutMs !== void 0) headers["vrpc-options"] = `timeout=${timeoutMs}ms`;
|
|
124
|
+
return headers;
|
|
125
|
+
}
|
|
126
|
+
function buildVrpcHeaders(options) {
|
|
127
|
+
return new Headers(buildVrpcHeaderRecord(options));
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/protocol/wire.ts
|
|
131
|
+
function resolveFields(schema) {
|
|
132
|
+
return typeof schema.fields === "function" ? schema.fields() : schema.fields;
|
|
133
|
+
}
|
|
134
|
+
function entriesOf(value) {
|
|
135
|
+
if (value instanceof Map) return Array.from(value.entries());
|
|
136
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return Object.entries(value);
|
|
137
|
+
throw new Error("vRPC wire map value must be a Record or Map.");
|
|
138
|
+
}
|
|
139
|
+
function objectOf(value) {
|
|
140
|
+
if (value instanceof Map) return Object.fromEntries(Array.from(value.entries(), ([key, item]) => [String(key), item]));
|
|
141
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
142
|
+
throw new Error("vRPC wire object value must be an object.");
|
|
143
|
+
}
|
|
144
|
+
function toIntegerKey(value) {
|
|
145
|
+
const integer = typeof value === "number" ? value : Number(value);
|
|
146
|
+
if (!Number.isSafeInteger(integer)) throw new Error(`vRPC wire map key must be an integer: ${String(value)}`);
|
|
147
|
+
return integer;
|
|
148
|
+
}
|
|
149
|
+
function normalizeBinary(value) {
|
|
150
|
+
if (value instanceof Uint8Array) return value;
|
|
151
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
152
|
+
if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
153
|
+
throw new Error("vRPC Binary value must be a Uint8Array.");
|
|
154
|
+
}
|
|
155
|
+
function toCborWireValue(value, schema) {
|
|
156
|
+
if (value == null) return value;
|
|
157
|
+
switch (schema.kind) {
|
|
158
|
+
case "value": return value;
|
|
159
|
+
case "binary": return normalizeBinary(value);
|
|
160
|
+
case "list":
|
|
161
|
+
if (!Array.isArray(value)) throw new Error("vRPC wire list value must be an array.");
|
|
162
|
+
return value.map((item) => toCborWireValue(item, schema.value));
|
|
163
|
+
case "map": {
|
|
164
|
+
const entries = entriesOf(value);
|
|
165
|
+
if (schema.key === "int") return new Map(entries.map(([key, item]) => [toIntegerKey(key), toCborWireValue(item, schema.value)]));
|
|
166
|
+
return Object.fromEntries(entries.map(([key, item]) => [String(key), toCborWireValue(item, schema.value)]));
|
|
167
|
+
}
|
|
168
|
+
case "object": {
|
|
169
|
+
const input = objectOf(value);
|
|
170
|
+
const output = { ...input };
|
|
171
|
+
for (const [field, fieldSchema] of Object.entries(resolveFields(schema))) if (field in input) output[field] = toCborWireValue(input[field], fieldSchema);
|
|
172
|
+
return output;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function fromCborWireValue(value, schema) {
|
|
177
|
+
if (value == null) return value;
|
|
178
|
+
switch (schema.kind) {
|
|
179
|
+
case "value": return value;
|
|
180
|
+
case "binary": return normalizeBinary(value);
|
|
181
|
+
case "list":
|
|
182
|
+
if (!Array.isArray(value)) throw new Error("vRPC wire list value must be an array.");
|
|
183
|
+
return value.map((item) => fromCborWireValue(item, schema.value));
|
|
184
|
+
case "map": return Object.fromEntries(entriesOf(value).map(([key, item]) => {
|
|
185
|
+
const normalizedKey = schema.key === "int" ? toIntegerKey(key) : String(key);
|
|
186
|
+
return [String(normalizedKey), fromCborWireValue(item, schema.value)];
|
|
187
|
+
}));
|
|
188
|
+
case "object": {
|
|
189
|
+
const input = objectOf(value);
|
|
190
|
+
const output = { ...input };
|
|
191
|
+
for (const [field, fieldSchema] of Object.entries(resolveFields(schema))) if (field in input) output[field] = fromCborWireValue(input[field], fieldSchema);
|
|
192
|
+
return output;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function normalizeCborValue(value) {
|
|
197
|
+
if (value instanceof Uint8Array) return value;
|
|
198
|
+
if (value instanceof Map) return Object.fromEntries(Array.from(value.entries(), ([key, item]) => [String(key), normalizeCborValue(item)]));
|
|
199
|
+
if (Array.isArray(value)) return value.map(normalizeCborValue);
|
|
200
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, normalizeCborValue(item)]));
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region src/protocol/request.ts
|
|
205
|
+
function buildVrpcPath(serviceName, methodName) {
|
|
206
|
+
return `${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`;
|
|
207
|
+
}
|
|
208
|
+
function buildVrpcRequestBody(params) {
|
|
209
|
+
return JSON.stringify({ params: params ?? null });
|
|
210
|
+
}
|
|
211
|
+
async function encodeVrpcRequestBody(params, schema, cborCodec) {
|
|
212
|
+
if (!schema) return buildVrpcRequestBody(params);
|
|
213
|
+
if (!cborCodec) throw new Error("A cborCodec is required to encode a CBOR vRPC request.");
|
|
214
|
+
const bytes = await cborCodec.encode({ params: toCborWireValue(params, schema) });
|
|
215
|
+
if (!(bytes instanceof Uint8Array)) throw new Error("cborCodec.encode() must return a Uint8Array.");
|
|
216
|
+
const body = new Uint8Array(bytes.byteLength);
|
|
217
|
+
body.set(bytes);
|
|
218
|
+
return body.buffer;
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/contracts/protocol.ts
|
|
222
|
+
var VrpcResponseError = class extends Error {
|
|
223
|
+
kind;
|
|
224
|
+
cause;
|
|
225
|
+
status;
|
|
226
|
+
statusText;
|
|
227
|
+
responseHeaders;
|
|
228
|
+
vrpcStatus;
|
|
229
|
+
url;
|
|
230
|
+
method;
|
|
231
|
+
payload;
|
|
232
|
+
code;
|
|
233
|
+
reason;
|
|
234
|
+
detail;
|
|
235
|
+
constructor(kind, response, payload, options = {}) {
|
|
236
|
+
super(options.message ?? require_error_payload.readServerErrorMessage(payload) ?? `request failed (status=${response.status})`, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
237
|
+
this.name = "VrpcResponseError";
|
|
238
|
+
this.kind = kind;
|
|
239
|
+
this.cause = options.cause;
|
|
240
|
+
this.status = response.status;
|
|
241
|
+
this.statusText = response.statusText;
|
|
242
|
+
this.responseHeaders = response.headers;
|
|
243
|
+
this.vrpcStatus = response.vrpcStatus;
|
|
244
|
+
this.url = response.url;
|
|
245
|
+
this.method = response.method;
|
|
246
|
+
this.payload = payload;
|
|
247
|
+
this.code = kind === "protocol" ? "VRPC_PROTOCOL_ERROR" : require_error_payload.readServerErrorStringField(payload, "code");
|
|
248
|
+
this.reason = require_error_payload.readServerErrorStringField(payload, "reason");
|
|
249
|
+
this.detail = require_error_payload.readServerErrorStringField(payload, "detail");
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/protocol/response-body.ts
|
|
254
|
+
function requireResponseHeader(response, name) {
|
|
255
|
+
const value = response.headers.get(name);
|
|
256
|
+
if (!value) throw new Error(`Invalid vRPC response: missing ${name} header.`);
|
|
257
|
+
return value;
|
|
258
|
+
}
|
|
259
|
+
function readDecodedEnvelopeField(payload, field) {
|
|
260
|
+
if (payload instanceof Map) return payload.get(field);
|
|
261
|
+
if (payload && typeof payload === "object") return payload[field];
|
|
262
|
+
}
|
|
263
|
+
async function decodeVrpcBody(response, cborCodec, resultSchema) {
|
|
264
|
+
requireResponseHeader(response, "vrpc-status");
|
|
265
|
+
assertValidVrpcServerHeader(response.headers.get("vrpc-server"));
|
|
266
|
+
const contentType = mediaTypeOf(requireResponseHeader(response, "content-type"));
|
|
267
|
+
if (contentType === "application/vrpc+json") {
|
|
268
|
+
const text = await response.text();
|
|
269
|
+
if (!text) throw new Error("Invalid vRPC response: empty response body.");
|
|
270
|
+
try {
|
|
271
|
+
return JSON.parse(text);
|
|
272
|
+
} catch {
|
|
273
|
+
throw new Error("Invalid vRPC response: JSON body cannot be parsed.");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (contentType !== "application/vrpc+cbor") throw new Error(`Invalid vRPC response content-type: ${contentType || "empty"}.`);
|
|
277
|
+
if (!resultSchema) throw new Error("Invalid vRPC response: unexpected CBOR body for a JSON method.");
|
|
278
|
+
if (!cborCodec) throw new Error("A cborCodec is required to decode a CBOR vRPC response.");
|
|
279
|
+
const decoded = await cborCodec.decode(new Uint8Array(await response.arrayBuffer()));
|
|
280
|
+
const result = readDecodedEnvelopeField(decoded, "result");
|
|
281
|
+
const error = readDecodedEnvelopeField(decoded, "error");
|
|
282
|
+
return {
|
|
283
|
+
result: fromCborWireValue(result, resultSchema),
|
|
284
|
+
error: normalizeCborValue(error)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/protocol/data.ts
|
|
289
|
+
function normalizeVrpcHeaderRecord(headers) {
|
|
290
|
+
const result = {};
|
|
291
|
+
for (const [rawName, rawValue] of Object.entries(headers)) {
|
|
292
|
+
if (rawValue == null) continue;
|
|
293
|
+
const name = rawName.toLowerCase();
|
|
294
|
+
if (!/^[!#$%&'*+.^_`|~0-9a-z-]+$/.test(name)) throw new TypeError(`Invalid header name: ${rawName}`);
|
|
295
|
+
const values = Array.isArray(rawValue) ? rawValue : [rawValue];
|
|
296
|
+
for (const item of values) {
|
|
297
|
+
const raw = String(item);
|
|
298
|
+
if (/[\r\n\0]/.test(raw)) throw new TypeError(`Invalid header value: ${rawName}`);
|
|
299
|
+
const value = raw.trim();
|
|
300
|
+
const previous = Object.prototype.hasOwnProperty.call(result, name) ? result[name] : void 0;
|
|
301
|
+
Object.defineProperty(result, name, {
|
|
302
|
+
value: previous === void 0 ? value : `${previous}, ${value}`,
|
|
303
|
+
writable: true,
|
|
304
|
+
enumerable: true,
|
|
305
|
+
configurable: true
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
function mergeVrpcApplicationHeaders(...headers) {
|
|
312
|
+
const result = {};
|
|
313
|
+
for (const source of headers) {
|
|
314
|
+
if (!source) continue;
|
|
315
|
+
for (const [name, value] of Object.entries(normalizeVrpcHeaderRecord(source))) {
|
|
316
|
+
if (isReservedVrpcHeader(name)) throw new Error(`vRPC protocol header cannot be overridden: ${name}`);
|
|
317
|
+
Object.defineProperty(result, name, {
|
|
318
|
+
value,
|
|
319
|
+
writable: true,
|
|
320
|
+
enumerable: true,
|
|
321
|
+
configurable: true
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
function resolveVrpcEnvelope(status, vrpcStatus, payload) {
|
|
328
|
+
if (status >= 200 && status < 300 && vrpcStatus === "OK") return {
|
|
329
|
+
ok: true,
|
|
330
|
+
vrpcStatus: "OK",
|
|
331
|
+
result: payload.result,
|
|
332
|
+
error: null
|
|
333
|
+
};
|
|
334
|
+
const envelope = payload && typeof payload === "object" ? payload : void 0;
|
|
335
|
+
return {
|
|
336
|
+
ok: false,
|
|
337
|
+
vrpcStatus,
|
|
338
|
+
result: envelope?.result,
|
|
339
|
+
error: envelope?.error ?? payload
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function parseVrpcResponseData(response, payload) {
|
|
343
|
+
const headers = normalizeVrpcHeaderRecord(response.headers);
|
|
344
|
+
const vrpcStatus = headers["vrpc-status"] ?? null;
|
|
345
|
+
return {
|
|
346
|
+
status: response.status,
|
|
347
|
+
statusText: response.statusText ?? "",
|
|
348
|
+
headers,
|
|
349
|
+
url: response.url ?? "",
|
|
350
|
+
method: response.method || "POST",
|
|
351
|
+
payload,
|
|
352
|
+
...resolveVrpcEnvelope(response.status, vrpcStatus, payload)
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function unwrapVrpcResponseData(response, payload) {
|
|
356
|
+
const parsed = parseVrpcResponseData(response, payload);
|
|
357
|
+
if (!parsed.ok) throw new VrpcResponseError("invoke", parsed, parsed.error);
|
|
358
|
+
return parsed.result;
|
|
359
|
+
}
|
|
360
|
+
async function decodeVrpcResponseData(response, options = {}) {
|
|
361
|
+
const headers = normalizeVrpcHeaderRecord(response.headers);
|
|
362
|
+
let payload;
|
|
363
|
+
try {
|
|
364
|
+
payload = await decodeVrpcBody({
|
|
365
|
+
headers: { get: (name) => headers[name.toLowerCase()] ?? null },
|
|
366
|
+
text: () => response.text(),
|
|
367
|
+
arrayBuffer: () => response.arrayBuffer()
|
|
368
|
+
}, options.cborCodec, options.resultSchema);
|
|
369
|
+
return parseVrpcResponseData({
|
|
370
|
+
...response,
|
|
371
|
+
headers
|
|
372
|
+
}, payload);
|
|
373
|
+
} catch (cause) {
|
|
374
|
+
if (cause instanceof VrpcResponseError) throw cause;
|
|
375
|
+
const parsed = parseVrpcResponseData({
|
|
376
|
+
...response,
|
|
377
|
+
headers
|
|
378
|
+
}, {
|
|
379
|
+
result: null,
|
|
380
|
+
error: null
|
|
381
|
+
});
|
|
382
|
+
const kind = parsed.vrpcStatus && !parsed.ok ? "invoke" : "protocol";
|
|
383
|
+
throw new VrpcResponseError(kind, parsed, null, {
|
|
384
|
+
cause,
|
|
385
|
+
...kind === "protocol" ? { message: cause instanceof Error ? cause.message : "Invalid vRPC response." } : {}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
async function decodeVrpcResponse(response, options = {}) {
|
|
390
|
+
const result = await decodeVrpcResponseData(response, options);
|
|
391
|
+
if (!result.ok) throw new VrpcResponseError("invoke", result, result.error);
|
|
392
|
+
return result.result;
|
|
393
|
+
}
|
|
394
|
+
//#endregion
|
|
395
|
+
Object.defineProperty(exports, "DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY", {
|
|
396
|
+
enumerable: true,
|
|
397
|
+
get: function() {
|
|
398
|
+
return DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY;
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
Object.defineProperty(exports, "VRPC_CBOR_CONTENT_TYPE", {
|
|
402
|
+
enumerable: true,
|
|
403
|
+
get: function() {
|
|
404
|
+
return VRPC_CBOR_CONTENT_TYPE;
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
Object.defineProperty(exports, "VRPC_JSON_CONTENT_TYPE", {
|
|
408
|
+
enumerable: true,
|
|
409
|
+
get: function() {
|
|
410
|
+
return VRPC_JSON_CONTENT_TYPE;
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
Object.defineProperty(exports, "VrpcResponseError", {
|
|
414
|
+
enumerable: true,
|
|
415
|
+
get: function() {
|
|
416
|
+
return VrpcResponseError;
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
Object.defineProperty(exports, "assertNoReservedVrpcHeaders", {
|
|
420
|
+
enumerable: true,
|
|
421
|
+
get: function() {
|
|
422
|
+
return assertNoReservedVrpcHeaders;
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
Object.defineProperty(exports, "buildVrpcHeaderRecord", {
|
|
426
|
+
enumerable: true,
|
|
427
|
+
get: function() {
|
|
428
|
+
return buildVrpcHeaderRecord;
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
Object.defineProperty(exports, "buildVrpcHeaders", {
|
|
432
|
+
enumerable: true,
|
|
433
|
+
get: function() {
|
|
434
|
+
return buildVrpcHeaders;
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
Object.defineProperty(exports, "buildVrpcPath", {
|
|
438
|
+
enumerable: true,
|
|
439
|
+
get: function() {
|
|
440
|
+
return buildVrpcPath;
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
Object.defineProperty(exports, "buildVrpcRequestBody", {
|
|
444
|
+
enumerable: true,
|
|
445
|
+
get: function() {
|
|
446
|
+
return buildVrpcRequestBody;
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
Object.defineProperty(exports, "decodeVrpcBody", {
|
|
450
|
+
enumerable: true,
|
|
451
|
+
get: function() {
|
|
452
|
+
return decodeVrpcBody;
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
Object.defineProperty(exports, "decodeVrpcResponse", {
|
|
456
|
+
enumerable: true,
|
|
457
|
+
get: function() {
|
|
458
|
+
return decodeVrpcResponse;
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
Object.defineProperty(exports, "decodeVrpcResponseData", {
|
|
462
|
+
enumerable: true,
|
|
463
|
+
get: function() {
|
|
464
|
+
return decodeVrpcResponseData;
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
Object.defineProperty(exports, "encodeVrpcRequestBody", {
|
|
468
|
+
enumerable: true,
|
|
469
|
+
get: function() {
|
|
470
|
+
return encodeVrpcRequestBody;
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
Object.defineProperty(exports, "generateClientInstanceId", {
|
|
474
|
+
enumerable: true,
|
|
475
|
+
get: function() {
|
|
476
|
+
return generateClientInstanceId;
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
Object.defineProperty(exports, "generateVrpcId", {
|
|
480
|
+
enumerable: true,
|
|
481
|
+
get: function() {
|
|
482
|
+
return generateVrpcId;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
Object.defineProperty(exports, "generateVrpcSpan", {
|
|
486
|
+
enumerable: true,
|
|
487
|
+
get: function() {
|
|
488
|
+
return generateVrpcSpan;
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
Object.defineProperty(exports, "getClientInstanceId", {
|
|
492
|
+
enumerable: true,
|
|
493
|
+
get: function() {
|
|
494
|
+
return getClientInstanceId;
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
Object.defineProperty(exports, "mergeVrpcApplicationHeaders", {
|
|
498
|
+
enumerable: true,
|
|
499
|
+
get: function() {
|
|
500
|
+
return mergeVrpcApplicationHeaders;
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
Object.defineProperty(exports, "normalizeVrpcHeaderRecord", {
|
|
504
|
+
enumerable: true,
|
|
505
|
+
get: function() {
|
|
506
|
+
return normalizeVrpcHeaderRecord;
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
Object.defineProperty(exports, "parseVrpcResponseData", {
|
|
510
|
+
enumerable: true,
|
|
511
|
+
get: function() {
|
|
512
|
+
return parseVrpcResponseData;
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
Object.defineProperty(exports, "resolveVrpcEnvelope", {
|
|
516
|
+
enumerable: true,
|
|
517
|
+
get: function() {
|
|
518
|
+
return resolveVrpcEnvelope;
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
Object.defineProperty(exports, "unwrapVrpcResponseData", {
|
|
522
|
+
enumerable: true,
|
|
523
|
+
get: function() {
|
|
524
|
+
return unwrapVrpcResponseData;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/contracts/error-payload.ts
|
|
2
|
+
function readServerErrorMessage(payload) {
|
|
3
|
+
if (!payload || typeof payload !== "object") return;
|
|
4
|
+
const body = payload;
|
|
5
|
+
if (typeof body.message !== "string" || !body.message.trim()) return;
|
|
6
|
+
const detail = typeof body.detail === "string" ? body.detail.trim() : "";
|
|
7
|
+
return detail ? `${body.message}\n${detail}` : body.message;
|
|
8
|
+
}
|
|
9
|
+
function readServerErrorStringField(payload, field) {
|
|
10
|
+
if (!payload || typeof payload !== "object") return;
|
|
11
|
+
const value = payload[field];
|
|
12
|
+
return typeof value === "string" ? value : void 0;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { readServerErrorStringField as n, readServerErrorMessage as t };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/contracts/error-payload.ts
|
|
2
|
+
function readServerErrorMessage(payload) {
|
|
3
|
+
if (!payload || typeof payload !== "object") return;
|
|
4
|
+
const body = payload;
|
|
5
|
+
if (typeof body.message !== "string" || !body.message.trim()) return;
|
|
6
|
+
const detail = typeof body.detail === "string" ? body.detail.trim() : "";
|
|
7
|
+
return detail ? `${body.message}\n${detail}` : body.message;
|
|
8
|
+
}
|
|
9
|
+
function readServerErrorStringField(payload, field) {
|
|
10
|
+
if (!payload || typeof payload !== "object") return;
|
|
11
|
+
const value = payload[field];
|
|
12
|
+
return typeof value === "string" ? value : void 0;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
Object.defineProperty(exports, "readServerErrorMessage", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function() {
|
|
18
|
+
return readServerErrorMessage;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(exports, "readServerErrorStringField", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function() {
|
|
24
|
+
return readServerErrorStringField;
|
|
25
|
+
}
|
|
26
|
+
});
|
package/dist/http.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_request_options = require("./request-options-
|
|
2
|
+
const require_request_options = require("./request-options-B_uQsyrU.cjs");
|
|
3
3
|
//#region src/core/create-http-client.ts
|
|
4
4
|
function resolveJsonBody(request) {
|
|
5
5
|
if (request.json !== void 0 && request.body !== void 0) throw new Error("Request json and body cannot be used together.");
|
package/dist/http.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as appendQueryToUrl, c as HTTP_TIMEOUT_ERROR_CODE, d as HttpInvokeError, f as HttpTimeoutError, g as isHttpError, i as createFetchTransport, l as HttpAbortError, o as buildRequestUrl, p as HttpTransportError, r as createClientCore, s as mergeHeaders, t as splitHttpRequestOptions, u as HttpClientError } from "./request-options-
|
|
1
|
+
import { a as appendQueryToUrl, c as HTTP_TIMEOUT_ERROR_CODE, d as HttpInvokeError, f as HttpTimeoutError, g as isHttpError, i as createFetchTransport, l as HttpAbortError, o as buildRequestUrl, p as HttpTransportError, r as createClientCore, s as mergeHeaders, t as splitHttpRequestOptions, u as HttpClientError } from "./request-options-CV6EPON5.js";
|
|
2
2
|
//#region src/core/create-http-client.ts
|
|
3
3
|
function resolveJsonBody(request) {
|
|
4
4
|
if (request.json !== void 0 && request.body !== void 0) throw new Error("Request json and body cannot be used together.");
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_data = require("./data-pemgw5-O.cjs");
|
|
3
|
+
exports.VRPC_CBOR_CONTENT_TYPE = require_data.VRPC_CBOR_CONTENT_TYPE;
|
|
4
|
+
exports.VRPC_JSON_CONTENT_TYPE = require_data.VRPC_JSON_CONTENT_TYPE;
|
|
5
|
+
exports.VrpcResponseError = require_data.VrpcResponseError;
|
|
6
|
+
exports.buildVrpcHeaderRecord = require_data.buildVrpcHeaderRecord;
|
|
7
|
+
exports.buildVrpcPath = require_data.buildVrpcPath;
|
|
8
|
+
exports.buildVrpcRequestBody = require_data.buildVrpcRequestBody;
|
|
9
|
+
exports.decodeVrpcResponse = require_data.decodeVrpcResponse;
|
|
10
|
+
exports.decodeVrpcResponseData = require_data.decodeVrpcResponseData;
|
|
11
|
+
exports.encodeVrpcRequestBody = require_data.encodeVrpcRequestBody;
|
|
12
|
+
exports.generateClientInstanceId = require_data.generateClientInstanceId;
|
|
13
|
+
exports.generateVrpcId = require_data.generateVrpcId;
|
|
14
|
+
exports.generateVrpcSpan = require_data.generateVrpcSpan;
|
|
15
|
+
exports.mergeVrpcApplicationHeaders = require_data.mergeVrpcApplicationHeaders;
|
|
16
|
+
exports.normalizeVrpcHeaderRecord = require_data.normalizeVrpcHeaderRecord;
|
|
17
|
+
exports.parseVrpcResponseData = require_data.parseVrpcResponseData;
|
|
18
|
+
exports.unwrapVrpcResponseData = require_data.unwrapVrpcResponseData;
|