@yorun-ai/vrpc 0.9.3 → 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 -3
- package/dist/client.d.ts +1 -3
- 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.d.cts +0 -2
- package/dist/http.d.ts +0 -2
- 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-m8_63jpk.cjs → request-options-B_uQsyrU.cjs} +5 -18
- package/dist/{request-options-Bjy2LvRE.js → request-options-CV6EPON5.js} +1 -14
- 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 +16 -0
- package/docs/maintainers/architecture.md +7 -1
- package/docs/maintainers/protocol.md +1 -1
- package/docs/maintainers/releasing.md +34 -34
- package/docs/reference/README.md +35 -13
- package/package.json +11 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/contracts/types.d.ts
|
|
2
|
+
type VrpcClientInfo = {
|
|
3
|
+
clientName: string;
|
|
4
|
+
clientVersion: string;
|
|
5
|
+
clientInstanceId: string;
|
|
6
|
+
};
|
|
7
|
+
type VrpcTrace = {
|
|
8
|
+
id: string;
|
|
9
|
+
span?: string;
|
|
10
|
+
};
|
|
11
|
+
type VrpcWireSchema = {
|
|
12
|
+
kind: "value";
|
|
13
|
+
nullable?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "binary";
|
|
16
|
+
nullable?: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "list";
|
|
19
|
+
value: VrpcWireSchema;
|
|
20
|
+
nullable?: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "map";
|
|
23
|
+
key: "int" | "string";
|
|
24
|
+
value: VrpcWireSchema;
|
|
25
|
+
nullable?: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "object";
|
|
28
|
+
fields: Readonly<Record<string, VrpcWireSchema>> | (() => Readonly<Record<string, VrpcWireSchema>>);
|
|
29
|
+
nullable?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type VrpcMethodWireSpec = {
|
|
32
|
+
arguments?: VrpcWireSchema;
|
|
33
|
+
result?: VrpcWireSchema;
|
|
34
|
+
};
|
|
35
|
+
type VrpcCborCodec = {
|
|
36
|
+
encode: (value: unknown) => Uint8Array | Promise<Uint8Array>;
|
|
37
|
+
decode: (bytes: Uint8Array) => unknown | Promise<unknown>;
|
|
38
|
+
};
|
|
39
|
+
interface VrpcRequestParams {
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/protocol/headers.d.ts
|
|
44
|
+
declare const VRPC_JSON_CONTENT_TYPE = "application/vrpc+json";
|
|
45
|
+
declare const VRPC_CBOR_CONTENT_TYPE = "application/vrpc+cbor";
|
|
46
|
+
type BuildVrpcHeadersOptions = {
|
|
47
|
+
clientInfo: VrpcClientInfo;
|
|
48
|
+
trace: VrpcTrace;
|
|
49
|
+
timeoutMs?: number;
|
|
50
|
+
requestUsesCbor?: boolean;
|
|
51
|
+
responseUsesCbor?: boolean;
|
|
52
|
+
};
|
|
53
|
+
declare function buildVrpcHeaderRecord({ clientInfo, trace, timeoutMs, requestUsesCbor, responseUsesCbor }: BuildVrpcHeadersOptions): Record<string, string>;
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/protocol/request.d.ts
|
|
56
|
+
declare function buildVrpcPath(serviceName: string, methodName: string): string;
|
|
57
|
+
declare function buildVrpcRequestBody(params: VrpcRequestParams | null | undefined): string;
|
|
58
|
+
declare function encodeVrpcRequestBody(params: VrpcRequestParams | null, schema: VrpcWireSchema | undefined, cborCodec: VrpcCborCodec | undefined): Promise<string | ArrayBuffer>;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/protocol/id.d.ts
|
|
61
|
+
declare function generateVrpcId(): string;
|
|
62
|
+
declare function generateVrpcSpan(): string;
|
|
63
|
+
declare function generateClientInstanceId(): string;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/contracts/protocol.d.ts
|
|
66
|
+
type VrpcHeaderRecord = Record<string, string>;
|
|
67
|
+
type VrpcResponseData = {
|
|
68
|
+
status: number;
|
|
69
|
+
statusText?: string;
|
|
70
|
+
headers: Readonly<Record<string, string | readonly string[] | undefined | null>>;
|
|
71
|
+
url?: string;
|
|
72
|
+
method?: string;
|
|
73
|
+
};
|
|
74
|
+
type VrpcResponseSource = VrpcResponseData & {
|
|
75
|
+
text(): string | Promise<string>;
|
|
76
|
+
arrayBuffer(): ArrayBuffer | Promise<ArrayBuffer>;
|
|
77
|
+
};
|
|
78
|
+
type VrpcDecodeOptions = {
|
|
79
|
+
cborCodec?: VrpcCborCodec;
|
|
80
|
+
resultSchema?: VrpcWireSchema;
|
|
81
|
+
};
|
|
82
|
+
type VrpcResponseResult<T = unknown> = {
|
|
83
|
+
status: number;
|
|
84
|
+
statusText: string;
|
|
85
|
+
headers: VrpcHeaderRecord;
|
|
86
|
+
url: string;
|
|
87
|
+
method: string;
|
|
88
|
+
payload: unknown;
|
|
89
|
+
} & ({
|
|
90
|
+
ok: true;
|
|
91
|
+
vrpcStatus: "OK";
|
|
92
|
+
result: T;
|
|
93
|
+
error: null;
|
|
94
|
+
} | {
|
|
95
|
+
ok: false;
|
|
96
|
+
vrpcStatus: string | null;
|
|
97
|
+
result: unknown;
|
|
98
|
+
error: unknown;
|
|
99
|
+
});
|
|
100
|
+
declare class VrpcResponseError extends Error {
|
|
101
|
+
readonly kind: "protocol" | "invoke";
|
|
102
|
+
readonly cause?: unknown;
|
|
103
|
+
readonly status: number;
|
|
104
|
+
readonly statusText: string;
|
|
105
|
+
readonly responseHeaders: VrpcHeaderRecord;
|
|
106
|
+
readonly vrpcStatus: string | null;
|
|
107
|
+
readonly url: string;
|
|
108
|
+
readonly method: string;
|
|
109
|
+
readonly payload: unknown;
|
|
110
|
+
readonly code?: string;
|
|
111
|
+
readonly reason?: string;
|
|
112
|
+
readonly detail?: string;
|
|
113
|
+
constructor(kind: "protocol" | "invoke", response: VrpcResponseResult, payload: unknown, options?: {
|
|
114
|
+
message?: string;
|
|
115
|
+
cause?: unknown;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/protocol/data.d.ts
|
|
120
|
+
declare function normalizeVrpcHeaderRecord(headers: VrpcResponseData["headers"]): VrpcHeaderRecord;
|
|
121
|
+
declare function mergeVrpcApplicationHeaders(...headers: Array<VrpcResponseData["headers"] | undefined>): VrpcHeaderRecord;
|
|
122
|
+
declare function parseVrpcResponseData<T = unknown>(response: VrpcResponseData, payload: unknown): VrpcResponseResult<T>;
|
|
123
|
+
declare function unwrapVrpcResponseData<T = unknown>(response: VrpcResponseData, payload: unknown): T;
|
|
124
|
+
declare function decodeVrpcResponseData<T = unknown>(response: VrpcResponseSource, options?: VrpcDecodeOptions): Promise<VrpcResponseResult<T>>;
|
|
125
|
+
declare function decodeVrpcResponse<T = unknown>(response: VrpcResponseSource, options?: VrpcDecodeOptions): Promise<T>;
|
|
126
|
+
//#endregion
|
|
127
|
+
export { type BuildVrpcHeadersOptions, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, type VrpcCborCodec, type VrpcClientInfo, type VrpcDecodeOptions, type VrpcHeaderRecord, type VrpcMethodWireSpec, type VrpcResponseData, VrpcResponseError, type VrpcResponseResult, type VrpcResponseSource, type VrpcTrace, type VrpcWireSchema, buildVrpcHeaderRecord, buildVrpcPath, buildVrpcRequestBody, decodeVrpcResponse, decodeVrpcResponseData, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, mergeVrpcApplicationHeaders, normalizeVrpcHeaderRecord, parseVrpcResponseData, unwrapVrpcResponseData };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/contracts/types.d.ts
|
|
2
|
+
type VrpcClientInfo = {
|
|
3
|
+
clientName: string;
|
|
4
|
+
clientVersion: string;
|
|
5
|
+
clientInstanceId: string;
|
|
6
|
+
};
|
|
7
|
+
type VrpcTrace = {
|
|
8
|
+
id: string;
|
|
9
|
+
span?: string;
|
|
10
|
+
};
|
|
11
|
+
type VrpcWireSchema = {
|
|
12
|
+
kind: "value";
|
|
13
|
+
nullable?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "binary";
|
|
16
|
+
nullable?: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "list";
|
|
19
|
+
value: VrpcWireSchema;
|
|
20
|
+
nullable?: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "map";
|
|
23
|
+
key: "int" | "string";
|
|
24
|
+
value: VrpcWireSchema;
|
|
25
|
+
nullable?: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "object";
|
|
28
|
+
fields: Readonly<Record<string, VrpcWireSchema>> | (() => Readonly<Record<string, VrpcWireSchema>>);
|
|
29
|
+
nullable?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type VrpcMethodWireSpec = {
|
|
32
|
+
arguments?: VrpcWireSchema;
|
|
33
|
+
result?: VrpcWireSchema;
|
|
34
|
+
};
|
|
35
|
+
type VrpcCborCodec = {
|
|
36
|
+
encode: (value: unknown) => Uint8Array | Promise<Uint8Array>;
|
|
37
|
+
decode: (bytes: Uint8Array) => unknown | Promise<unknown>;
|
|
38
|
+
};
|
|
39
|
+
interface VrpcRequestParams {
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/protocol/headers.d.ts
|
|
44
|
+
declare const VRPC_JSON_CONTENT_TYPE = "application/vrpc+json";
|
|
45
|
+
declare const VRPC_CBOR_CONTENT_TYPE = "application/vrpc+cbor";
|
|
46
|
+
type BuildVrpcHeadersOptions = {
|
|
47
|
+
clientInfo: VrpcClientInfo;
|
|
48
|
+
trace: VrpcTrace;
|
|
49
|
+
timeoutMs?: number;
|
|
50
|
+
requestUsesCbor?: boolean;
|
|
51
|
+
responseUsesCbor?: boolean;
|
|
52
|
+
};
|
|
53
|
+
declare function buildVrpcHeaderRecord({ clientInfo, trace, timeoutMs, requestUsesCbor, responseUsesCbor }: BuildVrpcHeadersOptions): Record<string, string>;
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/protocol/request.d.ts
|
|
56
|
+
declare function buildVrpcPath(serviceName: string, methodName: string): string;
|
|
57
|
+
declare function buildVrpcRequestBody(params: VrpcRequestParams | null | undefined): string;
|
|
58
|
+
declare function encodeVrpcRequestBody(params: VrpcRequestParams | null, schema: VrpcWireSchema | undefined, cborCodec: VrpcCborCodec | undefined): Promise<string | ArrayBuffer>;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/protocol/id.d.ts
|
|
61
|
+
declare function generateVrpcId(): string;
|
|
62
|
+
declare function generateVrpcSpan(): string;
|
|
63
|
+
declare function generateClientInstanceId(): string;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/contracts/protocol.d.ts
|
|
66
|
+
type VrpcHeaderRecord = Record<string, string>;
|
|
67
|
+
type VrpcResponseData = {
|
|
68
|
+
status: number;
|
|
69
|
+
statusText?: string;
|
|
70
|
+
headers: Readonly<Record<string, string | readonly string[] | undefined | null>>;
|
|
71
|
+
url?: string;
|
|
72
|
+
method?: string;
|
|
73
|
+
};
|
|
74
|
+
type VrpcResponseSource = VrpcResponseData & {
|
|
75
|
+
text(): string | Promise<string>;
|
|
76
|
+
arrayBuffer(): ArrayBuffer | Promise<ArrayBuffer>;
|
|
77
|
+
};
|
|
78
|
+
type VrpcDecodeOptions = {
|
|
79
|
+
cborCodec?: VrpcCborCodec;
|
|
80
|
+
resultSchema?: VrpcWireSchema;
|
|
81
|
+
};
|
|
82
|
+
type VrpcResponseResult<T = unknown> = {
|
|
83
|
+
status: number;
|
|
84
|
+
statusText: string;
|
|
85
|
+
headers: VrpcHeaderRecord;
|
|
86
|
+
url: string;
|
|
87
|
+
method: string;
|
|
88
|
+
payload: unknown;
|
|
89
|
+
} & ({
|
|
90
|
+
ok: true;
|
|
91
|
+
vrpcStatus: "OK";
|
|
92
|
+
result: T;
|
|
93
|
+
error: null;
|
|
94
|
+
} | {
|
|
95
|
+
ok: false;
|
|
96
|
+
vrpcStatus: string | null;
|
|
97
|
+
result: unknown;
|
|
98
|
+
error: unknown;
|
|
99
|
+
});
|
|
100
|
+
declare class VrpcResponseError extends Error {
|
|
101
|
+
readonly kind: "protocol" | "invoke";
|
|
102
|
+
readonly cause?: unknown;
|
|
103
|
+
readonly status: number;
|
|
104
|
+
readonly statusText: string;
|
|
105
|
+
readonly responseHeaders: VrpcHeaderRecord;
|
|
106
|
+
readonly vrpcStatus: string | null;
|
|
107
|
+
readonly url: string;
|
|
108
|
+
readonly method: string;
|
|
109
|
+
readonly payload: unknown;
|
|
110
|
+
readonly code?: string;
|
|
111
|
+
readonly reason?: string;
|
|
112
|
+
readonly detail?: string;
|
|
113
|
+
constructor(kind: "protocol" | "invoke", response: VrpcResponseResult, payload: unknown, options?: {
|
|
114
|
+
message?: string;
|
|
115
|
+
cause?: unknown;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/protocol/data.d.ts
|
|
120
|
+
declare function normalizeVrpcHeaderRecord(headers: VrpcResponseData["headers"]): VrpcHeaderRecord;
|
|
121
|
+
declare function mergeVrpcApplicationHeaders(...headers: Array<VrpcResponseData["headers"] | undefined>): VrpcHeaderRecord;
|
|
122
|
+
declare function parseVrpcResponseData<T = unknown>(response: VrpcResponseData, payload: unknown): VrpcResponseResult<T>;
|
|
123
|
+
declare function unwrapVrpcResponseData<T = unknown>(response: VrpcResponseData, payload: unknown): T;
|
|
124
|
+
declare function decodeVrpcResponseData<T = unknown>(response: VrpcResponseSource, options?: VrpcDecodeOptions): Promise<VrpcResponseResult<T>>;
|
|
125
|
+
declare function decodeVrpcResponse<T = unknown>(response: VrpcResponseSource, options?: VrpcDecodeOptions): Promise<T>;
|
|
126
|
+
//#endregion
|
|
127
|
+
export { type BuildVrpcHeadersOptions, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, type VrpcCborCodec, type VrpcClientInfo, type VrpcDecodeOptions, type VrpcHeaderRecord, type VrpcMethodWireSpec, type VrpcResponseData, VrpcResponseError, type VrpcResponseResult, type VrpcResponseSource, type VrpcTrace, type VrpcWireSchema, buildVrpcHeaderRecord, buildVrpcPath, buildVrpcRequestBody, decodeVrpcResponse, decodeVrpcResponseData, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, mergeVrpcApplicationHeaders, normalizeVrpcHeaderRecord, parseVrpcResponseData, unwrapVrpcResponseData };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as parseVrpcResponseData, b as generateVrpcId, d as buildVrpcRequestBody, f as encodeVrpcRequestBody, g as buildVrpcHeaderRecord, i as normalizeVrpcHeaderRecord, l as VrpcResponseError, m as VRPC_JSON_CONTENT_TYPE, n as decodeVrpcResponseData, p as VRPC_CBOR_CONTENT_TYPE, r as mergeVrpcApplicationHeaders, s as unwrapVrpcResponseData, t as decodeVrpcResponse, u as buildVrpcPath, x as generateVrpcSpan, y as generateClientInstanceId } from "./data-DOZk_tFi.js";
|
|
2
|
+
export { VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, VrpcResponseError, buildVrpcHeaderRecord, buildVrpcPath, buildVrpcRequestBody, decodeVrpcResponse, decodeVrpcResponseData, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, mergeVrpcApplicationHeaders, normalizeVrpcHeaderRecord, parseVrpcResponseData, unwrapVrpcResponseData };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_error_payload = require("./error-payload-dL9RvdTy.cjs");
|
|
1
2
|
//#region src/contracts/errors.ts
|
|
2
3
|
var HTTP_TIMEOUT_ERROR_CODE = "HTTP_TIMEOUT";
|
|
3
4
|
var HTTP_ABORT_ERROR_CODE = "HTTP_ABORTED";
|
|
@@ -63,20 +64,7 @@ var VrpcProtocolError = class extends HttpClientError {
|
|
|
63
64
|
this.vrpcStatus = response.headers.get("vrpc-status");
|
|
64
65
|
}
|
|
65
66
|
};
|
|
66
|
-
function readServerErrorMessage(payload) {
|
|
67
|
-
if (!payload || typeof payload !== "object") return;
|
|
68
|
-
const body = payload;
|
|
69
|
-
if (typeof body.message !== "string" || !body.message.trim()) return;
|
|
70
|
-
const detail = typeof body.detail === "string" ? body.detail.trim() : "";
|
|
71
|
-
return detail ? `${body.message}\n${detail}` : body.message;
|
|
72
|
-
}
|
|
73
|
-
function readServerErrorStringField(payload, field) {
|
|
74
|
-
if (!payload || typeof payload !== "object") return;
|
|
75
|
-
const value = payload[field];
|
|
76
|
-
return typeof value === "string" ? value : void 0;
|
|
77
|
-
}
|
|
78
67
|
var HttpInvokeError = class extends HttpClientError {
|
|
79
|
-
type;
|
|
80
68
|
code;
|
|
81
69
|
reason;
|
|
82
70
|
detail;
|
|
@@ -85,7 +73,7 @@ var HttpInvokeError = class extends HttpClientError {
|
|
|
85
73
|
responseHeaders;
|
|
86
74
|
payload;
|
|
87
75
|
constructor(response, payload, requestMeta = {}, options = {}) {
|
|
88
|
-
const message = readServerErrorMessage(payload) || `request failed (status=${response.status})`;
|
|
76
|
+
const message = require_error_payload.readServerErrorMessage(payload) || `request failed (status=${response.status})`;
|
|
89
77
|
super(message, "invoke", {
|
|
90
78
|
url: requestMeta.url || response.url || "",
|
|
91
79
|
method: requestMeta.method || "GET"
|
|
@@ -95,10 +83,9 @@ var HttpInvokeError = class extends HttpClientError {
|
|
|
95
83
|
this.statusText = response.statusText;
|
|
96
84
|
this.responseHeaders = response.headers;
|
|
97
85
|
this.payload = payload;
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
100
|
-
this.
|
|
101
|
-
this.detail = readServerErrorStringField(payload, "detail");
|
|
86
|
+
this.code = require_error_payload.readServerErrorStringField(payload, "code");
|
|
87
|
+
this.reason = require_error_payload.readServerErrorStringField(payload, "reason");
|
|
88
|
+
this.detail = require_error_payload.readServerErrorStringField(payload, "detail");
|
|
102
89
|
}
|
|
103
90
|
};
|
|
104
91
|
var VrpcInvokeError = class extends HttpInvokeError {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { n as readServerErrorStringField, t as readServerErrorMessage } from "./error-payload-MmlaZW_V.js";
|
|
1
2
|
//#region src/contracts/errors.ts
|
|
2
3
|
var HTTP_TIMEOUT_ERROR_CODE = "HTTP_TIMEOUT";
|
|
3
4
|
var HTTP_ABORT_ERROR_CODE = "HTTP_ABORTED";
|
|
@@ -63,20 +64,7 @@ var VrpcProtocolError = class extends HttpClientError {
|
|
|
63
64
|
this.vrpcStatus = response.headers.get("vrpc-status");
|
|
64
65
|
}
|
|
65
66
|
};
|
|
66
|
-
function readServerErrorMessage(payload) {
|
|
67
|
-
if (!payload || typeof payload !== "object") return;
|
|
68
|
-
const body = payload;
|
|
69
|
-
if (typeof body.message !== "string" || !body.message.trim()) return;
|
|
70
|
-
const detail = typeof body.detail === "string" ? body.detail.trim() : "";
|
|
71
|
-
return detail ? `${body.message}\n${detail}` : body.message;
|
|
72
|
-
}
|
|
73
|
-
function readServerErrorStringField(payload, field) {
|
|
74
|
-
if (!payload || typeof payload !== "object") return;
|
|
75
|
-
const value = payload[field];
|
|
76
|
-
return typeof value === "string" ? value : void 0;
|
|
77
|
-
}
|
|
78
67
|
var HttpInvokeError = class extends HttpClientError {
|
|
79
|
-
type;
|
|
80
68
|
code;
|
|
81
69
|
reason;
|
|
82
70
|
detail;
|
|
@@ -95,7 +83,6 @@ var HttpInvokeError = class extends HttpClientError {
|
|
|
95
83
|
this.statusText = response.statusText;
|
|
96
84
|
this.responseHeaders = response.headers;
|
|
97
85
|
this.payload = payload;
|
|
98
|
-
this.type = readServerErrorStringField(payload, "type");
|
|
99
86
|
this.code = readServerErrorStringField(payload, "code");
|
|
100
87
|
this.reason = readServerErrorStringField(payload, "reason");
|
|
101
88
|
this.detail = readServerErrorStringField(payload, "detail");
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Application-owned example. This file is not a package runtime export.
|
|
2
|
+
import type Taro from "@tarojs/taro";
|
|
3
|
+
import type {
|
|
4
|
+
VrpcClient,
|
|
5
|
+
VrpcClientOptions,
|
|
6
|
+
VrpcInterceptor,
|
|
7
|
+
VrpcInvokeContext,
|
|
8
|
+
VrpcInvokeRequest,
|
|
9
|
+
} from "@yorun-ai/vrpc/client";
|
|
10
|
+
import {
|
|
11
|
+
buildVrpcHeaderRecord,
|
|
12
|
+
buildVrpcPath,
|
|
13
|
+
decodeVrpcResponseData,
|
|
14
|
+
encodeVrpcRequestBody,
|
|
15
|
+
generateVrpcId,
|
|
16
|
+
generateVrpcSpan,
|
|
17
|
+
mergeVrpcApplicationHeaders,
|
|
18
|
+
normalizeVrpcHeaderRecord,
|
|
19
|
+
VrpcResponseError,
|
|
20
|
+
type VrpcResponseResult,
|
|
21
|
+
} from "@yorun-ai/vrpc/protocol";
|
|
22
|
+
|
|
23
|
+
import { createTaroTransport } from "./taro-transport";
|
|
24
|
+
|
|
25
|
+
export type TaroVrpcInterceptor = {
|
|
26
|
+
beforeRequest?: VrpcInterceptor["beforeRequest"];
|
|
27
|
+
afterResponse?: (
|
|
28
|
+
response: VrpcResponseResult,
|
|
29
|
+
context: VrpcInvokeContext,
|
|
30
|
+
) => void | Promise<void>;
|
|
31
|
+
onError?: VrpcInterceptor["onError"];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export interface TaroVrpcClient extends VrpcClient {
|
|
35
|
+
useData: (interceptor: TaroVrpcInterceptor) => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type TaroVrpcClientOptions = Omit<
|
|
39
|
+
VrpcClientOptions,
|
|
40
|
+
"fetchImpl" | "transport" | "prefixUrl" | "interceptors"
|
|
41
|
+
> & {
|
|
42
|
+
prefixUrl: string;
|
|
43
|
+
interceptors?: TaroVrpcInterceptor[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Generated factories still accept this client. Taro response hooks and errors
|
|
47
|
+
// use plain protocol data; no Fetch objects or Fetch globals are installed.
|
|
48
|
+
export function createTaroVrpcClient(
|
|
49
|
+
request: typeof Taro.request,
|
|
50
|
+
options: TaroVrpcClientOptions,
|
|
51
|
+
): TaroVrpcClient {
|
|
52
|
+
const transport = createTaroTransport(request);
|
|
53
|
+
const interceptors = [...(options.interceptors ?? [])];
|
|
54
|
+
const prefixUrl = options.prefixUrl.replace(/\/+$/, "");
|
|
55
|
+
if (!/^https?:\/\/[^/?#]+(?:\/[^?#]*)?$/.test(prefixUrl)) {
|
|
56
|
+
throw new Error("prefixUrl must be an absolute HTTP(S) invoke URL without query or fragment");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function invoke<T = unknown>(input: VrpcInvokeRequest): Promise<T> {
|
|
60
|
+
const call = input.options ?? {};
|
|
61
|
+
const timeoutMs = call.timeoutMs ?? options.timeoutMs ?? 15000;
|
|
62
|
+
const wire = call.wire;
|
|
63
|
+
if (wire && !wire.arguments && !wire.result) throw new Error("vRPC wire is empty");
|
|
64
|
+
if (wire && !options.cborCodec)
|
|
65
|
+
throw new Error("A cborCodec is required for vRPC wire methods");
|
|
66
|
+
const trace = call.trace ?? {
|
|
67
|
+
id: generateVrpcId(),
|
|
68
|
+
...(options.traceMode === "direct" ? { span: generateVrpcSpan() } : {}),
|
|
69
|
+
};
|
|
70
|
+
const headers = {
|
|
71
|
+
...mergeVrpcApplicationHeaders(headerRecord(options.headers), headerRecord(call.headers)),
|
|
72
|
+
...buildVrpcHeaderRecord({
|
|
73
|
+
clientInfo: options.clientInfo,
|
|
74
|
+
trace,
|
|
75
|
+
timeoutMs,
|
|
76
|
+
requestUsesCbor: Boolean(wire?.arguments),
|
|
77
|
+
responseUsesCbor: Boolean(wire?.result),
|
|
78
|
+
}),
|
|
79
|
+
};
|
|
80
|
+
const body = await encodeVrpcRequestBody(input.params, wire?.arguments, options.cborCodec);
|
|
81
|
+
const requestInit = { ...options.requestInit, ...call.requestInit };
|
|
82
|
+
const context: VrpcInvokeContext = {
|
|
83
|
+
url: `${prefixUrl}/${buildVrpcPath(input.serviceName, input.methodName)}`,
|
|
84
|
+
request: input,
|
|
85
|
+
options: {
|
|
86
|
+
...call,
|
|
87
|
+
headers,
|
|
88
|
+
timeoutMs,
|
|
89
|
+
trace,
|
|
90
|
+
requestInit,
|
|
91
|
+
suppressGlobalToast: call.suppressGlobalToast ?? options.suppressGlobalToast,
|
|
92
|
+
},
|
|
93
|
+
init: { ...requestInit, method: "POST", headers, body },
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
for (const interceptor of interceptors) await interceptor.beforeRequest?.(context);
|
|
97
|
+
if (context.init.method !== "POST") throw new Error("vRPC only supports POST");
|
|
98
|
+
const requestBody = context.init.body;
|
|
99
|
+
if (
|
|
100
|
+
requestBody != null &&
|
|
101
|
+
typeof requestBody !== "string" &&
|
|
102
|
+
!(requestBody instanceof ArrayBuffer)
|
|
103
|
+
) {
|
|
104
|
+
throw new Error("Taro vRPC requests require a string or ArrayBuffer body");
|
|
105
|
+
}
|
|
106
|
+
const result = await transport.request({
|
|
107
|
+
url: context.url,
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: headerRecord(context.init.headers ?? headers),
|
|
110
|
+
body: requestBody ?? undefined,
|
|
111
|
+
timeoutMs,
|
|
112
|
+
signal: context.init.signal,
|
|
113
|
+
});
|
|
114
|
+
const parsed = await decodeVrpcResponseData<T>(
|
|
115
|
+
{
|
|
116
|
+
...result,
|
|
117
|
+
method: "POST",
|
|
118
|
+
text: () => new TextDecoder().decode(result.body ?? new Uint8Array()),
|
|
119
|
+
arrayBuffer: () => new Uint8Array(result.body ?? []).buffer,
|
|
120
|
+
},
|
|
121
|
+
{ cborCodec: options.cborCodec, resultSchema: wire?.result },
|
|
122
|
+
);
|
|
123
|
+
for (const interceptor of interceptors) {
|
|
124
|
+
await interceptor.afterResponse?.(parsed, context);
|
|
125
|
+
}
|
|
126
|
+
if (!parsed.ok) throw new VrpcResponseError("invoke", parsed, parsed.error);
|
|
127
|
+
return parsed.result;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
for (const interceptor of interceptors) await interceptor.onError?.(error, context);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function use(interceptor: VrpcInterceptor) {
|
|
134
|
+
if (interceptor.afterResponse) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
"Taro does not provide Fetch Response hooks. Use useData({ afterResponse }) instead.",
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
interceptors.push({ beforeRequest: interceptor.beforeRequest, onError: interceptor.onError });
|
|
140
|
+
}
|
|
141
|
+
return { invoke, use, useData: (interceptor) => void interceptors.push(interceptor) };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function headerRecord(input?: HeadersInit): Record<string, string> {
|
|
145
|
+
if (!input) return {};
|
|
146
|
+
if (Array.isArray(input)) {
|
|
147
|
+
const values: Record<string, string[]> = Object.create(null);
|
|
148
|
+
for (const [name, value] of input) (values[name.toLowerCase()] ??= []).push(value);
|
|
149
|
+
return normalizeVrpcHeaderRecord(values);
|
|
150
|
+
}
|
|
151
|
+
if (typeof (input as Headers).forEach === "function") {
|
|
152
|
+
const values: Record<string, string> = Object.create(null);
|
|
153
|
+
(input as Headers).forEach((value, name) => {
|
|
154
|
+
values[name] = value;
|
|
155
|
+
});
|
|
156
|
+
return normalizeVrpcHeaderRecord(values);
|
|
157
|
+
}
|
|
158
|
+
return normalizeVrpcHeaderRecord(input as Record<string, string>);
|
|
159
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Application-owned example. This file is not a package runtime export.
|
|
2
|
+
import type Taro from "@tarojs/taro";
|
|
3
|
+
import "fast-text-encoding";
|
|
4
|
+
import { HttpAbortError, HttpTimeoutError, HttpTransportError } from "@yorun-ai/vrpc/client";
|
|
5
|
+
import { normalizeVrpcHeaderRecord } from "@yorun-ai/vrpc/protocol";
|
|
6
|
+
|
|
7
|
+
export type TaroTransportRequest = {
|
|
8
|
+
url: string;
|
|
9
|
+
method: "POST";
|
|
10
|
+
headers: Record<string, string>;
|
|
11
|
+
body?: string | ArrayBuffer;
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
signal?: AbortSignal | null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type TaroTransportResponse = {
|
|
17
|
+
status: number;
|
|
18
|
+
statusText?: string;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
body: Uint8Array | null;
|
|
21
|
+
url: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function createTaroTransport(request: typeof Taro.request) {
|
|
25
|
+
return {
|
|
26
|
+
request(input: TaroTransportRequest): Promise<TaroTransportResponse> {
|
|
27
|
+
const meta = { url: input.url, method: input.method };
|
|
28
|
+
const signal = input.signal;
|
|
29
|
+
if (signal?.aborted)
|
|
30
|
+
return Promise.reject(new HttpAbortError(meta, { reason: signal.reason }));
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
let task: ReturnType<typeof Taro.request> | undefined;
|
|
33
|
+
const abort = () => task?.abort();
|
|
34
|
+
const cleanup = () => signal?.removeEventListener("abort", abort);
|
|
35
|
+
try {
|
|
36
|
+
task = request<ArrayBuffer>({
|
|
37
|
+
url: input.url,
|
|
38
|
+
method: input.method as "POST",
|
|
39
|
+
header: input.headers,
|
|
40
|
+
data: input.body as string | ArrayBuffer | undefined,
|
|
41
|
+
dataType: "其他",
|
|
42
|
+
responseType: "arraybuffer",
|
|
43
|
+
timeout: input.timeoutMs ?? 60000,
|
|
44
|
+
success(response) {
|
|
45
|
+
cleanup();
|
|
46
|
+
resolve({
|
|
47
|
+
status: response.statusCode,
|
|
48
|
+
headers: normalizeVrpcHeaderRecord(response.header as Record<string, string>),
|
|
49
|
+
body: [204, 205, 304].includes(response.statusCode)
|
|
50
|
+
? null
|
|
51
|
+
: new Uint8Array(response.data),
|
|
52
|
+
url: input.url,
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
fail(cause) {
|
|
56
|
+
cleanup();
|
|
57
|
+
if (signal?.aborted)
|
|
58
|
+
reject(new HttpAbortError(meta, { cause, reason: signal.reason }));
|
|
59
|
+
else if (cause.errMsg?.includes("timeout"))
|
|
60
|
+
reject(new HttpTimeoutError(input.timeoutMs ?? 60000, meta, { cause }));
|
|
61
|
+
else reject(new HttpTransportError(meta, { cause }));
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
} catch (cause) {
|
|
65
|
+
cleanup();
|
|
66
|
+
reject(new HttpTransportError(meta, { cause }));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Taro also rejects its returned Promise when invoking fail. The
|
|
70
|
+
// callbacks above own our result; consume that parallel rejection.
|
|
71
|
+
void task.catch?.(() => {});
|
|
72
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
73
|
+
if (signal?.aborted) abort();
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Protocol helpers for non-browser runtimes
|
|
2
|
+
|
|
3
|
+
Use `@yorun-ai/vrpc/protocol` when the platform owns HTTP I/O and does not provide Fetch objects, for example a miniapp using a native request API. This entry needs no `fetch`, `Headers`, `Response`, `XMLHttpRequest`, or DOM type library and installs no globals. It is an additive entry in the same package, not a polyfill package.
|
|
4
|
+
|
|
5
|
+
The existing root, `/client`, and `/http` entries keep their published behavior. Their browser helpers still return `Headers` and use `Response`; existing applications need no migration.
|
|
6
|
+
|
|
7
|
+
For a native request example and generated-SDK compatibility, see [Taro integration](./taro.md).
|
|
8
|
+
|
|
9
|
+
## Build a request
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {
|
|
13
|
+
buildVrpcHeaderRecord,
|
|
14
|
+
buildVrpcPath,
|
|
15
|
+
encodeVrpcRequestBody,
|
|
16
|
+
generateVrpcId,
|
|
17
|
+
mergeVrpcApplicationHeaders,
|
|
18
|
+
} from "@yorun-ai/vrpc/protocol";
|
|
19
|
+
|
|
20
|
+
const path = buildVrpcPath("user.UserService", "getProfile");
|
|
21
|
+
const headers = {
|
|
22
|
+
...mergeVrpcApplicationHeaders({ Authorization: "Bearer token" }),
|
|
23
|
+
...buildVrpcHeaderRecord({
|
|
24
|
+
clientInfo: {
|
|
25
|
+
clientName: "demo.miniapp",
|
|
26
|
+
clientVersion: "1.0.0",
|
|
27
|
+
clientInstanceId: "11111111-1111-4111-8111-111111111111",
|
|
28
|
+
},
|
|
29
|
+
trace: { id: generateVrpcId() },
|
|
30
|
+
timeoutMs: 15_000,
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
const body = await encodeVrpcRequestBody({ userId: 1 }, undefined, undefined);
|
|
34
|
+
// Send POST <invoke-prefix>/<path> using the platform API, headers and body.
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The header builder validates client identity, traces and timeout values. Merge application headers before applying protocol headers; the merge helper rejects reserved names, including case variants. Use the generated method's wire schemas and a `cborCodec` when encoding or decoding Binary methods. Wire metadata is client-only and never enters the request envelope.
|
|
38
|
+
|
|
39
|
+
## Read a response
|
|
40
|
+
|
|
41
|
+
The adapter supplies response metadata and body readers. Readers can return values or Promises; cache bytes in the adapter if they must be read more than once. They must preserve the original bytes and decode text as UTF-8.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { decodeVrpcResponse, VrpcResponseError } from "@yorun-ai/vrpc/protocol";
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const profile = await decodeVrpcResponse<{ name: string }>({
|
|
48
|
+
status: 200,
|
|
49
|
+
headers: {
|
|
50
|
+
"content-type": "application/vrpc+json",
|
|
51
|
+
"vrpc-status": "OK",
|
|
52
|
+
"vrpc-server":
|
|
53
|
+
"name=demo.server,version=1.0.0,instanceId=22222222-2222-4222-8222-222222222222",
|
|
54
|
+
},
|
|
55
|
+
text: () => '{"result":{"name":"Ada"},"error":null}',
|
|
56
|
+
arrayBuffer: () => new ArrayBuffer(0), // Actual binary bytes for CBOR responses.
|
|
57
|
+
});
|
|
58
|
+
// Use profile.name.
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof VrpcResponseError) {
|
|
61
|
+
// error.kind is "invoke" or "protocol"; headers are a plain record.
|
|
62
|
+
// Branch on error.vrpcStatus/code/reason, not human-readable messages.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`decodeVrpcResponse` validates required server/status/content-type headers and the JSON/CBOR body. Successful HTTP status plus `vrpc-status: OK` returns the result. Failed status throws an invocation error; an unreadable failure body preserves that outcome and attaches its decoding cause. Malformed successful responses throw protocol errors. This matches the status precedence of the browser client.
|
|
68
|
+
|
|
69
|
+
`parseVrpcResponseData` and `unwrapVrpcResponseData` work on already decoded envelopes and do **not** replace full decoding/metadata validation. The new `VrpcResponseError` has plain header data and is intentionally separate from browser `VrpcInvokeError`/`VrpcProtocolError`. Use its own class and `kind`; the existing `isVrpcError` guard remains specific to the browser client's error contract.
|
|
70
|
+
|
|
71
|
+
## Adapter responsibilities
|
|
72
|
+
|
|
73
|
+
The platform adapter owns URL configuration, I/O, UTF-8 body reading, native cancellation, local timeout enforcement, storage, authentication and interception policy. Protocol helpers do not create timers, automatically send requests, store tokens, or access localStorage. A timeout header alone does not implement a local timeout.
|
|
74
|
+
|
|
75
|
+
Generated factories requiring `VrpcClient` still require `invoke()` and `use()`; the published interface and browser behavior are unchanged. A Taro application can define its own data hooks and errors using this entry. Its `use()` may register the supported legacy request/error hooks and explicitly reject browser Response hooks, directing callers to its local data-hook API. This is an opt-in Taro usage change, not full browser-hook compatibility. See [Taro integration](taro.md) for the application-local contract; no new client types or factories are added to the runtime. Do not cast an incomplete adapter to `VrpcClient` or silently discard hooks.
|
|
76
|
+
|
|
77
|
+
Use `decodeVrpcResponseData` when an adapter needs the fully validated, decoded outcome before deciding how to throw or run response hooks. Unlike `decodeVrpcResponse`, it returns a decoded invocation failure as `{ ok: false, ... }`. Undecodable responses still throw `VrpcResponseError` with the same status precedence. This allows an adapter to preserve its existing hook ordering and error classes without reimplementing decoding.
|