@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
package/README.md
CHANGED
|
@@ -138,11 +138,14 @@ generic HTTP entry, version pinning, and troubleshooting.
|
|
|
138
138
|
|
|
139
139
|
## Entry points
|
|
140
140
|
|
|
141
|
-
| Import
|
|
142
|
-
|
|
|
143
|
-
| `@yorun-ai/vrpc`
|
|
144
|
-
| `@yorun-ai/vrpc/client`
|
|
145
|
-
| `@yorun-ai/vrpc/http`
|
|
141
|
+
| Import | Contents |
|
|
142
|
+
| ------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| `@yorun-ai/vrpc` | vRPC client, protocol helpers, transport, errors, and types. |
|
|
144
|
+
| `@yorun-ai/vrpc/client` | Explicit vRPC entry; identical to the package root. |
|
|
145
|
+
| `@yorun-ai/vrpc/http` | Generic HTTP client, Fetch transport, HTTP errors, and types. |
|
|
146
|
+
| `@yorun-ai/vrpc/protocol` | Runtime-independent protocol helpers with plain header records and adapter-provided body readers. |
|
|
147
|
+
|
|
148
|
+
For miniapps and other runtimes without Fetch objects, use the [protocol entry](docs/guides/protocol.md). It adds no polyfills or network implementation. Existing root/client/http APIs remain unchanged.
|
|
146
149
|
|
|
147
150
|
## Configuration at a glance
|
|
148
151
|
|
|
@@ -186,6 +189,13 @@ examples.
|
|
|
186
189
|
examples.
|
|
187
190
|
- [Axios integration](docs/guides/axios.md) — custom Axios transport and JSON
|
|
188
191
|
protocol helpers.
|
|
192
|
+
- [Taro integration](docs/guides/taro.md) — native requests with the protocol
|
|
193
|
+
entry and compatibility with existing generated SDKs and response hooks.
|
|
194
|
+
- [Protocol helpers](docs/guides/protocol.md) — runtime-independent headers,
|
|
195
|
+
encoding, response validation, and adapter responsibilities.
|
|
196
|
+
|
|
197
|
+
- [Upgrading to 0.11.0](docs/guides/upgrading-0.11.md) — motivation, compatibility,
|
|
198
|
+
Taro hook/error migration, and the unchanged official H5 dependency.
|
|
189
199
|
|
|
190
200
|
### Reference and maintainers
|
|
191
201
|
|
package/README.zh-CN.md
CHANGED
|
@@ -133,11 +133,14 @@ try {
|
|
|
133
133
|
|
|
134
134
|
## 入口
|
|
135
135
|
|
|
136
|
-
| Import
|
|
137
|
-
|
|
|
138
|
-
| `@yorun-ai/vrpc`
|
|
139
|
-
| `@yorun-ai/vrpc/client`
|
|
140
|
-
| `@yorun-ai/vrpc/http`
|
|
136
|
+
| Import | 内容 |
|
|
137
|
+
| ------------------------- | ---------------------------------------------------------------------- |
|
|
138
|
+
| `@yorun-ai/vrpc` | vRPC client、协议 helper、transport、错误与类型。 |
|
|
139
|
+
| `@yorun-ai/vrpc/client` | 显式 vRPC 入口,与包根入口一致。 |
|
|
140
|
+
| `@yorun-ai/vrpc/http` | 通用 HTTP client、Fetch transport、HTTP 错误与类型。 |
|
|
141
|
+
| `@yorun-ai/vrpc/protocol` | 不依赖运行环境的协议工具,使用普通请求头对象和平台提供的正文读取函数。 |
|
|
142
|
+
|
|
143
|
+
小程序等缺少 Fetch 对象的环境可使用[独立协议入口](docs/guides/protocol.md)。它不安装 polyfill,也不负责网络请求;现有 root/client/http API 保持不变。
|
|
141
144
|
|
|
142
145
|
## 配置概览
|
|
143
146
|
|
|
@@ -176,6 +179,13 @@ vRPC client 还提供两个请求选项:`trace` 覆盖自动生成的 `vrpc-tr
|
|
|
176
179
|
- [Browser CDN(English)](docs/guides/cdn.md):CDN 加载方式与完整 HTML 示例。
|
|
177
180
|
- [Axios integration(English)](docs/guides/axios.md):自定义 Axios transport 与
|
|
178
181
|
JSON 协议 helper。
|
|
182
|
+
- [Taro integration(English)](docs/guides/taro.md):使用协议入口接入原生请求,
|
|
183
|
+
并兼容已有生成 SDK 和响应钩子。
|
|
184
|
+
- [Protocol helpers(English)](docs/guides/protocol.md):不依赖运行环境的请求头、
|
|
185
|
+
编码、响应校验,以及平台适配器的职责。
|
|
186
|
+
|
|
187
|
+
- [Upgrading to 0.11.0(English)](docs/guides/upgrading-0.11.md):升级原因、兼容边界、
|
|
188
|
+
Taro 钩子/错误迁移,以及保持不变的官方 H5 依赖。
|
|
179
189
|
|
|
180
190
|
### Reference 与维护者文档
|
|
181
191
|
|
package/dist/client.cjs
CHANGED
|
@@ -1,262 +1,10 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_request_options = require("./request-options-
|
|
3
|
-
|
|
4
|
-
var DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = "vrpc-client-instance-id";
|
|
5
|
-
function getRandomByteArray(length) {
|
|
6
|
-
const array = new Uint8Array(length);
|
|
7
|
-
if (globalThis.crypto?.getRandomValues) {
|
|
8
|
-
globalThis.crypto.getRandomValues(array);
|
|
9
|
-
return array;
|
|
10
|
-
}
|
|
11
|
-
for (let i = 0; i < length; i++) array[i] = Math.floor(Math.random() * 256);
|
|
12
|
-
return array;
|
|
13
|
-
}
|
|
14
|
-
function toHex(bytes) {
|
|
15
|
-
return Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("");
|
|
16
|
-
}
|
|
17
|
-
function newNonZeroHex(bytesLength, zeroValue) {
|
|
18
|
-
for (;;) {
|
|
19
|
-
const value = toHex(getRandomByteArray(bytesLength));
|
|
20
|
-
if (value !== zeroValue) return value;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function generateVrpcId() {
|
|
24
|
-
return newNonZeroHex(16, "00000000000000000000000000000000");
|
|
25
|
-
}
|
|
26
|
-
function generateVrpcSpan() {
|
|
27
|
-
return newNonZeroHex(8, "0000000000000000");
|
|
28
|
-
}
|
|
29
|
-
function generateClientInstanceId() {
|
|
30
|
-
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID().toLowerCase();
|
|
31
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
|
|
32
|
-
const value = Math.floor(Math.random() * 16);
|
|
33
|
-
return (char === "x" ? value : value & 3 | 8).toString(16);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
function getClientInstanceId({ key } = {}) {
|
|
37
|
-
const storageKey = key?.trim() || "vrpc-client-instance-id";
|
|
38
|
-
try {
|
|
39
|
-
const existingClientInstanceId = globalThis.localStorage.getItem(storageKey);
|
|
40
|
-
if (existingClientInstanceId) return existingClientInstanceId;
|
|
41
|
-
const clientInstanceId = generateClientInstanceId();
|
|
42
|
-
globalThis.localStorage.setItem(storageKey, clientInstanceId);
|
|
43
|
-
return clientInstanceId;
|
|
44
|
-
} catch {
|
|
45
|
-
return generateClientInstanceId();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
//#endregion
|
|
49
|
-
//#region src/protocol/headers.ts
|
|
50
|
-
var VRPC_JSON_CONTENT_TYPE = "application/vrpc+json";
|
|
51
|
-
var VRPC_CBOR_CONTENT_TYPE = "application/vrpc+cbor";
|
|
52
|
-
var CLIENT_NAME_PATTERN = /^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$/;
|
|
53
|
-
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-]+)*)?$/;
|
|
54
|
-
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}$/;
|
|
55
|
-
var TRACE_ID_PATTERN = /^[0-9a-f]{32}$/;
|
|
56
|
-
var TRACE_SPAN_PATTERN = /^[0-9a-f]{16}$/;
|
|
57
|
-
function assertClientInfo(clientInfo, scope = "client") {
|
|
58
|
-
if (!CLIENT_NAME_PATTERN.test(clientInfo.clientName)) throw new Error(`Invalid vRPC ${scope} name: ${clientInfo.clientName}`);
|
|
59
|
-
if (!SEMVER_PATTERN.test(clientInfo.clientVersion)) throw new Error(`Invalid vRPC ${scope} version: ${clientInfo.clientVersion}`);
|
|
60
|
-
if (!UUID_PATTERN.test(clientInfo.clientInstanceId)) throw new Error(`Invalid vRPC ${scope} instanceId: ${clientInfo.clientInstanceId}`);
|
|
61
|
-
}
|
|
62
|
-
function decodeDelimitedAppInfo(value) {
|
|
63
|
-
const fields = {};
|
|
64
|
-
for (const entry of value.split(",")) {
|
|
65
|
-
const separator = entry.indexOf("=");
|
|
66
|
-
if (separator <= 0) return null;
|
|
67
|
-
const key = entry.slice(0, separator).trim();
|
|
68
|
-
const fieldValue = entry.slice(separator + 1).trim();
|
|
69
|
-
if (!key || !fieldValue || key in fields) return null;
|
|
70
|
-
fields[key] = fieldValue;
|
|
71
|
-
}
|
|
72
|
-
if (Object.keys(fields).length !== 3) return null;
|
|
73
|
-
const name = fields.name;
|
|
74
|
-
const version = fields.version;
|
|
75
|
-
const instanceId = fields.instanceId;
|
|
76
|
-
if (!name || !version || !instanceId) return null;
|
|
77
|
-
return {
|
|
78
|
-
clientName: name,
|
|
79
|
-
clientVersion: version,
|
|
80
|
-
clientInstanceId: instanceId
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function assertValidVrpcServerHeader(value) {
|
|
84
|
-
if (!value) throw new Error("Invalid vRPC response: missing vrpc-server header.");
|
|
85
|
-
const serverInfo = decodeDelimitedAppInfo(value);
|
|
86
|
-
if (!serverInfo) throw new Error("Invalid vRPC response: invalid vrpc-server header.");
|
|
87
|
-
try {
|
|
88
|
-
assertClientInfo(serverInfo, "server");
|
|
89
|
-
} catch {
|
|
90
|
-
throw new Error("Invalid vRPC response: invalid vrpc-server header.");
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function assertValidVrpcTrace(trace) {
|
|
94
|
-
if (!TRACE_ID_PATTERN.test(trace.id)) throw new Error(`Invalid vRPC trace id: ${trace.id}`);
|
|
95
|
-
if (trace.span !== void 0 && !TRACE_SPAN_PATTERN.test(trace.span)) throw new Error(`Invalid vRPC trace span: ${trace.span}`);
|
|
96
|
-
}
|
|
97
|
-
function assertValidVrpcTimeout(timeoutMs) {
|
|
98
|
-
if (timeoutMs !== void 0 && (!Number.isSafeInteger(timeoutMs) || !Number.isFinite(timeoutMs) || timeoutMs <= 0)) throw new Error(`Invalid vRPC timeoutMs: ${timeoutMs}`);
|
|
99
|
-
}
|
|
100
|
-
function isReservedVrpcHeader(name) {
|
|
101
|
-
const normalized = name.toLowerCase();
|
|
102
|
-
return normalized === "accept" || normalized === "content-type" || normalized.startsWith("vrpc-");
|
|
103
|
-
}
|
|
104
|
-
function assertNoReservedVrpcHeaders(...values) {
|
|
105
|
-
for (const value of values) {
|
|
106
|
-
if (!value) continue;
|
|
107
|
-
const headers = new Headers(value);
|
|
108
|
-
for (const name of headers.keys()) if (isReservedVrpcHeader(name)) throw new Error(`vRPC protocol header cannot be overridden: ${name}`);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
function mediaTypeOf(value) {
|
|
112
|
-
return value?.split(";", 1)[0]?.trim().toLowerCase() || "";
|
|
113
|
-
}
|
|
114
|
-
function buildVrpcHeaders({ clientInfo, trace, timeoutMs, requestUsesCbor = false, responseUsesCbor = false }) {
|
|
115
|
-
assertClientInfo(clientInfo);
|
|
116
|
-
assertValidVrpcTrace(trace);
|
|
117
|
-
assertValidVrpcTimeout(timeoutMs);
|
|
118
|
-
const headers = new Headers();
|
|
119
|
-
headers.set("content-type", requestUsesCbor ? VRPC_CBOR_CONTENT_TYPE : VRPC_JSON_CONTENT_TYPE);
|
|
120
|
-
headers.set("accept", responseUsesCbor ? `${VRPC_CBOR_CONTENT_TYPE}, ${VRPC_JSON_CONTENT_TYPE}` : VRPC_JSON_CONTENT_TYPE);
|
|
121
|
-
headers.set("vrpc-client", `name=${clientInfo.clientName},version=${clientInfo.clientVersion},instanceId=${clientInfo.clientInstanceId}`);
|
|
122
|
-
headers.set("vrpc-trace", trace.span ? `id=${trace.id},span=${trace.span}` : `id=${trace.id}`);
|
|
123
|
-
if (timeoutMs !== void 0) headers.set("vrpc-options", `timeout=${timeoutMs}ms`);
|
|
124
|
-
return headers;
|
|
125
|
-
}
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/protocol/wire.ts
|
|
128
|
-
function resolveFields(schema) {
|
|
129
|
-
return typeof schema.fields === "function" ? schema.fields() : schema.fields;
|
|
130
|
-
}
|
|
131
|
-
function entriesOf(value) {
|
|
132
|
-
if (value instanceof Map) return Array.from(value.entries());
|
|
133
|
-
if (value && typeof value === "object" && !Array.isArray(value)) return Object.entries(value);
|
|
134
|
-
throw new Error("vRPC wire map value must be a Record or Map.");
|
|
135
|
-
}
|
|
136
|
-
function objectOf(value) {
|
|
137
|
-
if (value instanceof Map) return Object.fromEntries(Array.from(value.entries(), ([key, item]) => [String(key), item]));
|
|
138
|
-
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
139
|
-
throw new Error("vRPC wire object value must be an object.");
|
|
140
|
-
}
|
|
141
|
-
function toIntegerKey(value) {
|
|
142
|
-
const integer = typeof value === "number" ? value : Number(value);
|
|
143
|
-
if (!Number.isSafeInteger(integer)) throw new Error(`vRPC wire map key must be an integer: ${String(value)}`);
|
|
144
|
-
return integer;
|
|
145
|
-
}
|
|
146
|
-
function normalizeBinary(value) {
|
|
147
|
-
if (value instanceof Uint8Array) return value;
|
|
148
|
-
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
149
|
-
if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
150
|
-
throw new Error("vRPC Binary value must be a Uint8Array.");
|
|
151
|
-
}
|
|
152
|
-
function toCborWireValue(value, schema) {
|
|
153
|
-
if (value == null) return value;
|
|
154
|
-
switch (schema.kind) {
|
|
155
|
-
case "value": return value;
|
|
156
|
-
case "binary": return normalizeBinary(value);
|
|
157
|
-
case "list":
|
|
158
|
-
if (!Array.isArray(value)) throw new Error("vRPC wire list value must be an array.");
|
|
159
|
-
return value.map((item) => toCborWireValue(item, schema.value));
|
|
160
|
-
case "map": {
|
|
161
|
-
const entries = entriesOf(value);
|
|
162
|
-
if (schema.key === "int") return new Map(entries.map(([key, item]) => [toIntegerKey(key), toCborWireValue(item, schema.value)]));
|
|
163
|
-
return Object.fromEntries(entries.map(([key, item]) => [String(key), toCborWireValue(item, schema.value)]));
|
|
164
|
-
}
|
|
165
|
-
case "object": {
|
|
166
|
-
const input = objectOf(value);
|
|
167
|
-
const output = { ...input };
|
|
168
|
-
for (const [field, fieldSchema] of Object.entries(resolveFields(schema))) if (field in input) output[field] = toCborWireValue(input[field], fieldSchema);
|
|
169
|
-
return output;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
function fromCborWireValue(value, schema) {
|
|
174
|
-
if (value == null) return value;
|
|
175
|
-
switch (schema.kind) {
|
|
176
|
-
case "value": return value;
|
|
177
|
-
case "binary": return normalizeBinary(value);
|
|
178
|
-
case "list":
|
|
179
|
-
if (!Array.isArray(value)) throw new Error("vRPC wire list value must be an array.");
|
|
180
|
-
return value.map((item) => fromCborWireValue(item, schema.value));
|
|
181
|
-
case "map": return Object.fromEntries(entriesOf(value).map(([key, item]) => {
|
|
182
|
-
const normalizedKey = schema.key === "int" ? toIntegerKey(key) : String(key);
|
|
183
|
-
return [String(normalizedKey), fromCborWireValue(item, schema.value)];
|
|
184
|
-
}));
|
|
185
|
-
case "object": {
|
|
186
|
-
const input = objectOf(value);
|
|
187
|
-
const output = { ...input };
|
|
188
|
-
for (const [field, fieldSchema] of Object.entries(resolveFields(schema))) if (field in input) output[field] = fromCborWireValue(input[field], fieldSchema);
|
|
189
|
-
return output;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function normalizeCborValue(value) {
|
|
194
|
-
if (value instanceof Uint8Array) return value;
|
|
195
|
-
if (value instanceof Map) return Object.fromEntries(Array.from(value.entries(), ([key, item]) => [String(key), normalizeCborValue(item)]));
|
|
196
|
-
if (Array.isArray(value)) return value.map(normalizeCborValue);
|
|
197
|
-
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, normalizeCborValue(item)]));
|
|
198
|
-
return value;
|
|
199
|
-
}
|
|
200
|
-
//#endregion
|
|
201
|
-
//#region src/protocol/request.ts
|
|
202
|
-
function buildVrpcPath(serviceName, methodName) {
|
|
203
|
-
return `${encodeURIComponent(serviceName)}/${encodeURIComponent(methodName)}`;
|
|
204
|
-
}
|
|
205
|
-
function buildVrpcRequestBody(params) {
|
|
206
|
-
return JSON.stringify({ params: params ?? null });
|
|
207
|
-
}
|
|
208
|
-
async function encodeVrpcRequestBody(params, schema, cborCodec) {
|
|
209
|
-
if (!schema) return buildVrpcRequestBody(params);
|
|
210
|
-
if (!cborCodec) throw new Error("A cborCodec is required to encode a CBOR vRPC request.");
|
|
211
|
-
const bytes = await cborCodec.encode({ params: toCborWireValue(params, schema) });
|
|
212
|
-
if (!(bytes instanceof Uint8Array)) throw new Error("cborCodec.encode() must return a Uint8Array.");
|
|
213
|
-
const body = new Uint8Array(bytes.byteLength);
|
|
214
|
-
body.set(bytes);
|
|
215
|
-
return body.buffer;
|
|
216
|
-
}
|
|
217
|
-
//#endregion
|
|
2
|
+
const require_request_options = require("./request-options-B_uQsyrU.cjs");
|
|
3
|
+
const require_data = require("./data-pemgw5-O.cjs");
|
|
218
4
|
//#region src/protocol/response.ts
|
|
219
|
-
function readEnvelopeError(payload) {
|
|
220
|
-
if (!payload || typeof payload !== "object") return payload;
|
|
221
|
-
return payload.error ?? payload;
|
|
222
|
-
}
|
|
223
|
-
function readEnvelopeResult(payload) {
|
|
224
|
-
if (!payload || typeof payload !== "object") return;
|
|
225
|
-
return payload.result;
|
|
226
|
-
}
|
|
227
|
-
function requireResponseHeader(response, name) {
|
|
228
|
-
const value = response.headers.get(name);
|
|
229
|
-
if (!value) throw new Error(`Invalid vRPC response: missing ${name} header.`);
|
|
230
|
-
return value;
|
|
231
|
-
}
|
|
232
|
-
function readDecodedEnvelopeField(payload, field) {
|
|
233
|
-
if (payload instanceof Map) return payload.get(field);
|
|
234
|
-
if (payload && typeof payload === "object") return payload[field];
|
|
235
|
-
}
|
|
236
5
|
async function decodeVrpcResponsePayload(response, cborCodec, resultSchema) {
|
|
237
6
|
try {
|
|
238
|
-
|
|
239
|
-
assertValidVrpcServerHeader(response.headers.get("vrpc-server"));
|
|
240
|
-
const contentType = mediaTypeOf(requireResponseHeader(response, "content-type"));
|
|
241
|
-
if (contentType === "application/vrpc+json") {
|
|
242
|
-
const text = await response.text();
|
|
243
|
-
if (!text) throw new Error("Invalid vRPC response: empty response body.");
|
|
244
|
-
try {
|
|
245
|
-
return JSON.parse(text);
|
|
246
|
-
} catch {
|
|
247
|
-
throw new Error("Invalid vRPC response: JSON body cannot be parsed.");
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
if (contentType !== "application/vrpc+cbor") throw new Error(`Invalid vRPC response content-type: ${contentType || "empty"}.`);
|
|
251
|
-
if (!resultSchema) throw new Error("Invalid vRPC response: unexpected CBOR body for a JSON method.");
|
|
252
|
-
if (!cborCodec) throw new Error("A cborCodec is required to decode a CBOR vRPC response.");
|
|
253
|
-
const decoded = await cborCodec.decode(new Uint8Array(await response.arrayBuffer()));
|
|
254
|
-
const result = readDecodedEnvelopeField(decoded, "result");
|
|
255
|
-
const error = readDecodedEnvelopeField(decoded, "error");
|
|
256
|
-
return {
|
|
257
|
-
result: fromCborWireValue(result, resultSchema),
|
|
258
|
-
error: normalizeCborValue(error)
|
|
259
|
-
};
|
|
7
|
+
return await require_data.decodeVrpcBody(response, cborCodec, resultSchema);
|
|
260
8
|
} catch (cause) {
|
|
261
9
|
if (cause instanceof require_request_options.VrpcProtocolError) throw cause;
|
|
262
10
|
const message = cause instanceof Error ? cause.message : "Invalid vRPC response.";
|
|
@@ -295,34 +43,16 @@ function resolveVrpcResponse(responseLike, payload) {
|
|
|
295
43
|
const statusText = response.statusText || "";
|
|
296
44
|
const url = responseLike.url || response.url || "";
|
|
297
45
|
const method = responseLike.method || "POST";
|
|
298
|
-
if (response.ok && vrpcStatus === "OK") return {
|
|
299
|
-
response,
|
|
300
|
-
parsed: {
|
|
301
|
-
ok: true,
|
|
302
|
-
status: response.status,
|
|
303
|
-
statusText,
|
|
304
|
-
vrpcStatus: "OK",
|
|
305
|
-
headers: response.headers,
|
|
306
|
-
url,
|
|
307
|
-
method,
|
|
308
|
-
payload,
|
|
309
|
-
result: payload.result,
|
|
310
|
-
error: null
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
46
|
return {
|
|
314
47
|
response,
|
|
315
48
|
parsed: {
|
|
316
|
-
ok: false,
|
|
317
49
|
status: response.status,
|
|
318
50
|
statusText,
|
|
319
|
-
vrpcStatus,
|
|
320
51
|
headers: response.headers,
|
|
321
52
|
url,
|
|
322
53
|
method,
|
|
323
54
|
payload,
|
|
324
|
-
|
|
325
|
-
error: readEnvelopeError(payload)
|
|
55
|
+
...require_data.resolveVrpcEnvelope(response.status, vrpcStatus, payload)
|
|
326
56
|
}
|
|
327
57
|
};
|
|
328
58
|
}
|
|
@@ -350,8 +80,8 @@ function normalizeInvokeRequest(request) {
|
|
|
350
80
|
//#endregion
|
|
351
81
|
//#region src/core/create-vrpc-client.ts
|
|
352
82
|
function buildDefaultTrace(traceMode) {
|
|
353
|
-
const trace = { id: generateVrpcId() };
|
|
354
|
-
if (traceMode === "direct") trace.span = generateVrpcSpan();
|
|
83
|
+
const trace = { id: require_data.generateVrpcId() };
|
|
84
|
+
if (traceMode === "direct") trace.span = require_data.generateVrpcSpan();
|
|
355
85
|
return trace;
|
|
356
86
|
}
|
|
357
87
|
function assertWireSpec(serviceName, methodName, wire) {
|
|
@@ -376,12 +106,12 @@ function createVrpcClient(options) {
|
|
|
376
106
|
const wire = requestOptions.wire;
|
|
377
107
|
assertWireSpec(resolvedRequest.serviceName, resolvedRequest.methodName, wire);
|
|
378
108
|
requireCborCodec(resolvedRequest.serviceName, resolvedRequest.methodName, wire, cborCodec);
|
|
379
|
-
assertNoReservedVrpcHeaders(helpers.baseOptions.headers, requestOptions.headers);
|
|
109
|
+
require_data.assertNoReservedVrpcHeaders(helpers.baseOptions.headers, requestOptions.headers);
|
|
380
110
|
const timeoutMs = requestOptions.timeoutMs ?? helpers.baseOptions.timeoutMs;
|
|
381
111
|
const trace = requestOptions.trace ?? buildDefaultTrace(traceMode);
|
|
382
112
|
const requestUsesCbor = Boolean(wire?.arguments);
|
|
383
113
|
const responseUsesCbor = Boolean(wire?.result);
|
|
384
|
-
const protocolHeaders = buildVrpcHeaders({
|
|
114
|
+
const protocolHeaders = require_data.buildVrpcHeaders({
|
|
385
115
|
clientInfo,
|
|
386
116
|
trace,
|
|
387
117
|
timeoutMs,
|
|
@@ -389,8 +119,8 @@ function createVrpcClient(options) {
|
|
|
389
119
|
responseUsesCbor
|
|
390
120
|
});
|
|
391
121
|
const finalHeaders = require_request_options.mergeHeaders(helpers.baseOptions.headers, requestOptions.headers, protocolHeaders);
|
|
392
|
-
const body = await encodeVrpcRequestBody(resolvedRequest.params, wire?.arguments, cborCodec);
|
|
393
|
-
const path = buildVrpcPath(resolvedRequest.serviceName, resolvedRequest.methodName);
|
|
122
|
+
const body = await require_data.encodeVrpcRequestBody(resolvedRequest.params, wire?.arguments, cborCodec);
|
|
123
|
+
const path = require_data.buildVrpcPath(resolvedRequest.serviceName, resolvedRequest.methodName);
|
|
394
124
|
const url = require_request_options.buildRequestUrl(helpers.prefixUrl, path);
|
|
395
125
|
const resolvedRequestInit = {
|
|
396
126
|
credentials: "omit",
|
|
@@ -457,28 +187,28 @@ function createVrpcClient(options) {
|
|
|
457
187
|
};
|
|
458
188
|
}
|
|
459
189
|
//#endregion
|
|
460
|
-
exports.DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY;
|
|
190
|
+
exports.DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = require_data.DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY;
|
|
461
191
|
exports.HTTP_TIMEOUT_ERROR_CODE = require_request_options.HTTP_TIMEOUT_ERROR_CODE;
|
|
462
192
|
exports.HttpAbortError = require_request_options.HttpAbortError;
|
|
463
193
|
exports.HttpClientError = require_request_options.HttpClientError;
|
|
464
194
|
exports.HttpInvokeError = require_request_options.HttpInvokeError;
|
|
465
195
|
exports.HttpTimeoutError = require_request_options.HttpTimeoutError;
|
|
466
196
|
exports.HttpTransportError = require_request_options.HttpTransportError;
|
|
467
|
-
exports.VRPC_CBOR_CONTENT_TYPE = VRPC_CBOR_CONTENT_TYPE;
|
|
468
|
-
exports.VRPC_JSON_CONTENT_TYPE = VRPC_JSON_CONTENT_TYPE;
|
|
197
|
+
exports.VRPC_CBOR_CONTENT_TYPE = require_data.VRPC_CBOR_CONTENT_TYPE;
|
|
198
|
+
exports.VRPC_JSON_CONTENT_TYPE = require_data.VRPC_JSON_CONTENT_TYPE;
|
|
469
199
|
exports.VrpcInvokeError = require_request_options.VrpcInvokeError;
|
|
470
200
|
exports.VrpcProtocolError = require_request_options.VrpcProtocolError;
|
|
471
|
-
exports.buildVrpcHeaders = buildVrpcHeaders;
|
|
472
|
-
exports.buildVrpcPath = buildVrpcPath;
|
|
473
|
-
exports.buildVrpcRequestBody = buildVrpcRequestBody;
|
|
201
|
+
exports.buildVrpcHeaders = require_data.buildVrpcHeaders;
|
|
202
|
+
exports.buildVrpcPath = require_data.buildVrpcPath;
|
|
203
|
+
exports.buildVrpcRequestBody = require_data.buildVrpcRequestBody;
|
|
474
204
|
exports.createFetchTransport = require_request_options.createFetchTransport;
|
|
475
205
|
exports.createVrpcClient = createVrpcClient;
|
|
476
206
|
exports.decodeVrpcResponsePayload = decodeVrpcResponsePayload;
|
|
477
|
-
exports.encodeVrpcRequestBody = encodeVrpcRequestBody;
|
|
478
|
-
exports.generateClientInstanceId = generateClientInstanceId;
|
|
479
|
-
exports.generateVrpcId = generateVrpcId;
|
|
480
|
-
exports.generateVrpcSpan = generateVrpcSpan;
|
|
481
|
-
exports.getClientInstanceId = getClientInstanceId;
|
|
207
|
+
exports.encodeVrpcRequestBody = require_data.encodeVrpcRequestBody;
|
|
208
|
+
exports.generateClientInstanceId = require_data.generateClientInstanceId;
|
|
209
|
+
exports.generateVrpcId = require_data.generateVrpcId;
|
|
210
|
+
exports.generateVrpcSpan = require_data.generateVrpcSpan;
|
|
211
|
+
exports.getClientInstanceId = require_data.getClientInstanceId;
|
|
482
212
|
exports.isVrpcError = require_request_options.isVrpcError;
|
|
483
213
|
exports.parseVrpcResponse = parseVrpcResponse;
|
|
484
214
|
exports.unwrapVrpcResponse = unwrapVrpcResponse;
|
package/dist/client.d.cts
CHANGED
|
@@ -260,7 +260,7 @@ type BuildVrpcHeadersOptions = {
|
|
|
260
260
|
requestUsesCbor?: boolean;
|
|
261
261
|
responseUsesCbor?: boolean;
|
|
262
262
|
};
|
|
263
|
-
declare function buildVrpcHeaders(
|
|
263
|
+
declare function buildVrpcHeaders(options: BuildVrpcHeadersOptions): Headers;
|
|
264
264
|
//#endregion
|
|
265
265
|
//#region src/protocol/request.d.ts
|
|
266
266
|
declare function buildVrpcPath(serviceName: string, methodName: string): string;
|
package/dist/client.d.ts
CHANGED
|
@@ -260,7 +260,7 @@ type BuildVrpcHeadersOptions = {
|
|
|
260
260
|
requestUsesCbor?: boolean;
|
|
261
261
|
responseUsesCbor?: boolean;
|
|
262
262
|
};
|
|
263
|
-
declare function buildVrpcHeaders(
|
|
263
|
+
declare function buildVrpcHeaders(options: BuildVrpcHeadersOptions): Headers;
|
|
264
264
|
//#endregion
|
|
265
265
|
//#region src/protocol/request.d.ts
|
|
266
266
|
declare function buildVrpcPath(serviceName: string, methodName: string): string;
|