@i.un/api-client 1.1.8 → 1.2.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/dist/chunk-TPEZODIF.mjs +172 -0
- package/dist/chunk-TPEZODIF.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +31 -179
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/protobuf.d.mts +7 -11
- package/dist/protobuf.d.ts +7 -11
- package/dist/protobuf.js +29 -179
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.mjs +1 -1
- package/package.json +6 -5
- package/dist/chunk-QJ34L7YD.mjs +0 -305
- package/dist/chunk-QJ34L7YD.mjs.map +0 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// src/protobuf.ts
|
|
2
|
+
import {
|
|
3
|
+
create,
|
|
4
|
+
toBinary,
|
|
5
|
+
fromBinary,
|
|
6
|
+
toJson
|
|
7
|
+
} from "@bufbuild/protobuf";
|
|
8
|
+
|
|
9
|
+
// src/gen/secure_pb.ts
|
|
10
|
+
import { fileDesc, messageDesc } from "@bufbuild/protobuf/codegenv2";
|
|
11
|
+
var file_src_secure = /* @__PURE__ */ fileDesc("ChBzcmMvc2VjdXJlLnByb3RvEgZzZWN1cmUiKQoNU2VjdXJlUGF5bG9hZBIKCgJ0cxgBIAEoAxIMCgRkYXRhGAIgASgMYgZwcm90bzM");
|
|
12
|
+
var SecurePayloadSchema = /* @__PURE__ */ messageDesc(file_src_secure, 0);
|
|
13
|
+
|
|
14
|
+
// src/protobuf.ts
|
|
15
|
+
var PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
16
|
+
function isProtobufContentType(contentType) {
|
|
17
|
+
return !!contentType?.toLowerCase().includes(PROTOBUF_CONTENT_TYPE);
|
|
18
|
+
}
|
|
19
|
+
function getProtobufHeaders({
|
|
20
|
+
send = false,
|
|
21
|
+
receive = false
|
|
22
|
+
} = {}) {
|
|
23
|
+
const headers = {};
|
|
24
|
+
if (send) {
|
|
25
|
+
headers["Content-Type"] = PROTOBUF_CONTENT_TYPE;
|
|
26
|
+
}
|
|
27
|
+
if (receive) {
|
|
28
|
+
headers["Accept"] = PROTOBUF_CONTENT_TYPE;
|
|
29
|
+
}
|
|
30
|
+
return headers;
|
|
31
|
+
}
|
|
32
|
+
var encoder = new TextEncoder();
|
|
33
|
+
var decoder = new TextDecoder();
|
|
34
|
+
function xorTransform(data, key) {
|
|
35
|
+
const keyBytes = typeof key === "string" ? encoder.encode(key) : key;
|
|
36
|
+
if (keyBytes.length === 0) return data;
|
|
37
|
+
for (let i = 0; i < data.length; i++) {
|
|
38
|
+
data[i] ^= keyBytes[i % keyBytes.length];
|
|
39
|
+
}
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function createXorObfuscator(key) {
|
|
43
|
+
return {
|
|
44
|
+
encrypt: (data) => xorTransform(data, key),
|
|
45
|
+
decrypt: (data) => xorTransform(data, key)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function encodeSecure(data, options = {}) {
|
|
49
|
+
const { obfuscator, protoType, transform, encode: customEncode } = options;
|
|
50
|
+
let buffer;
|
|
51
|
+
const processedData = transform?.beforeEncode ? transform.beforeEncode(data) : data;
|
|
52
|
+
if (customEncode) {
|
|
53
|
+
buffer = await customEncode(processedData);
|
|
54
|
+
} else {
|
|
55
|
+
if (protoType) {
|
|
56
|
+
const message = create(protoType, processedData);
|
|
57
|
+
buffer = toBinary(protoType, message);
|
|
58
|
+
} else {
|
|
59
|
+
const payload = {
|
|
60
|
+
ts: BigInt(Date.now()),
|
|
61
|
+
data: processedData instanceof Uint8Array ? processedData : encoder.encode(JSON.stringify(processedData))
|
|
62
|
+
};
|
|
63
|
+
const message = create(SecurePayloadSchema, payload);
|
|
64
|
+
buffer = toBinary(SecurePayloadSchema, message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (obfuscator) {
|
|
68
|
+
return await obfuscator.encrypt(buffer);
|
|
69
|
+
}
|
|
70
|
+
return buffer;
|
|
71
|
+
}
|
|
72
|
+
async function decodeSecure(buffer, options = {}) {
|
|
73
|
+
const { obfuscator, protoType, transform, decode: customDecode } = options;
|
|
74
|
+
let uint8 = ArrayBuffer.isView(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength) : new Uint8Array(buffer);
|
|
75
|
+
if (uint8.length === 0) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (uint8.length > 0 && obfuscator) {
|
|
79
|
+
uint8 = await obfuscator.decrypt(uint8);
|
|
80
|
+
}
|
|
81
|
+
if (customDecode) {
|
|
82
|
+
return await customDecode(uint8);
|
|
83
|
+
}
|
|
84
|
+
const schema = protoType || SecurePayloadSchema;
|
|
85
|
+
const message = fromBinary(schema, uint8);
|
|
86
|
+
const plainObj = toJson(schema, message);
|
|
87
|
+
if (transform?.afterDecode) {
|
|
88
|
+
return transform.afterDecode(plainObj);
|
|
89
|
+
}
|
|
90
|
+
if (!protoType && plainObj.data) {
|
|
91
|
+
const rawData = message.data;
|
|
92
|
+
if (rawData) {
|
|
93
|
+
const jsonString = decoder.decode(rawData);
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(jsonString);
|
|
96
|
+
} catch {
|
|
97
|
+
return jsonString;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return plainObj;
|
|
102
|
+
}
|
|
103
|
+
async function secureResponse(data, options) {
|
|
104
|
+
const buffer = await encodeSecure(data, options);
|
|
105
|
+
const cleanBuffer = buffer.slice();
|
|
106
|
+
return new Response(cleanBuffer, {
|
|
107
|
+
headers: {
|
|
108
|
+
...getProtobufHeaders({ send: true }),
|
|
109
|
+
...options?.corsHeaders
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async function parseSecureRequest(request, options = {}) {
|
|
114
|
+
const buffer = await request.arrayBuffer();
|
|
115
|
+
return decodeSecure(buffer, options);
|
|
116
|
+
}
|
|
117
|
+
function createProtobufHooks(globalOptions = {}) {
|
|
118
|
+
return {
|
|
119
|
+
async onRequest(context) {
|
|
120
|
+
const { options: reqOptions } = context;
|
|
121
|
+
const mergedOptions = {
|
|
122
|
+
...globalOptions,
|
|
123
|
+
...reqOptions
|
|
124
|
+
};
|
|
125
|
+
const headers = reqOptions.headers instanceof Headers ? reqOptions.headers : new Headers(reqOptions.headers);
|
|
126
|
+
if (isProtobufContentType(headers.get("Content-Type")) && reqOptions.body && !(reqOptions.body instanceof Uint8Array)) {
|
|
127
|
+
reqOptions.body = await encodeSecure(reqOptions.body, mergedOptions);
|
|
128
|
+
}
|
|
129
|
+
if (isProtobufContentType(headers.get("Accept")) && !reqOptions.responseType) {
|
|
130
|
+
reqOptions.responseType = "arrayBuffer";
|
|
131
|
+
}
|
|
132
|
+
reqOptions.headers = headers;
|
|
133
|
+
},
|
|
134
|
+
async onResponse(context) {
|
|
135
|
+
const { response, options: reqOptions } = context;
|
|
136
|
+
if (!response?._data || response.status === 204) return;
|
|
137
|
+
const mergedOptions = {
|
|
138
|
+
...globalOptions,
|
|
139
|
+
...reqOptions
|
|
140
|
+
};
|
|
141
|
+
if (isProtobufContentType(response.headers.get("Content-Type"))) {
|
|
142
|
+
try {
|
|
143
|
+
response._data = await decodeSecure(response._data, mergedOptions);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
console.log("Error [Protobuf Decode Error]", e);
|
|
146
|
+
response._data = null;
|
|
147
|
+
}
|
|
148
|
+
} else if (response._data instanceof ArrayBuffer) {
|
|
149
|
+
const text = decoder.decode(response._data);
|
|
150
|
+
try {
|
|
151
|
+
response._data = JSON.parse(text);
|
|
152
|
+
} catch {
|
|
153
|
+
response._data = text;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export {
|
|
161
|
+
PROTOBUF_CONTENT_TYPE,
|
|
162
|
+
isProtobufContentType,
|
|
163
|
+
getProtobufHeaders,
|
|
164
|
+
xorTransform,
|
|
165
|
+
createXorObfuscator,
|
|
166
|
+
encodeSecure,
|
|
167
|
+
decodeSecure,
|
|
168
|
+
secureResponse,
|
|
169
|
+
parseSecureRequest,
|
|
170
|
+
createProtobufHooks
|
|
171
|
+
};
|
|
172
|
+
//# sourceMappingURL=chunk-TPEZODIF.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/protobuf.ts","../src/gen/secure_pb.ts"],"sourcesContent":["/**\n * Protobuf 安全通信模块\n *\n * 提供基于 Protobuf 的二进制序列化、XOR 混淆加密以及与 API Client 的无缝集成。\n */\n\nimport {\n create,\n toBinary,\n fromBinary,\n toJson,\n type MessageShape,\n type DescMessage,\n} from \"@bufbuild/protobuf\";\nimport { SecurePayloadSchema } from \"./gen/secure_pb.js\";\nimport type { FetchContext } from \"ofetch\";\n\n// ============================================================================\n// 1. 类型定义 (Types)\n// ============================================================================\n\n/**\n * 适配 @bufbuild/protobuf v2 的类型占位\n * 在 v2 中,Schema (MessageDesc) 是核心对象\n */\nexport type ProtobufTypeLike = DescMessage;\n\n/**\n * 二进制混淆接口\n * 允许外部定义混淆逻辑,以便与不同语言实现的后端兼容\n */\nexport interface ProtobufObfuscator {\n encrypt(data: Uint8Array): Uint8Array | Promise<Uint8Array>;\n decrypt(data: Uint8Array): Uint8Array | Promise<Uint8Array>;\n}\n\n/**\n * Protobuf 编解码集成配置项\n * 用于控制序列化、混淆、钩子集成等全流程\n */\nexport interface ProtobufOptions {\n /**\n * 混淆器实现\n * 提供该选项时才会启用混淆\n */\n obfuscator?: ProtobufObfuscator;\n /** 预编译的 Protobuf 类型 Schema (推荐,性能最高) */\n protoType?: ProtobufTypeLike;\n /** 数据转换钩子:处理业务模型与 Proto 结构不一致的情况 */\n transform?: {\n beforeEncode?: (data: any) => any;\n afterDecode?: (payload: any) => any;\n };\n /** 外部定义的编码逻辑 (返回原始二进制) */\n encode?: (data: any) => Uint8Array | Promise<Uint8Array>;\n /** 外部定义的解码逻辑 (返回原始对象) */\n decode?: <T>(buffer: Uint8Array) => T | Promise<T>;\n}\n\nexport const PROTOBUF_CONTENT_TYPE = \"application/x-protobuf\";\n\n/** 检查是否为 Protobuf 响应头 */\nexport function isProtobufContentType(contentType: string | null): boolean {\n return !!contentType?.toLowerCase().includes(PROTOBUF_CONTENT_TYPE);\n}\n\n/**\n * 获取 Protobuf 相关的 Header 配置对象\n */\nexport function getProtobufHeaders({\n send = false,\n receive = false,\n}: {\n send?: boolean;\n receive?: boolean;\n} = {}): Record<string, string> {\n const headers: Record<string, string> = {};\n\n if (send) {\n headers[\"Content-Type\"] = PROTOBUF_CONTENT_TYPE;\n }\n\n if (receive) {\n headers[\"Accept\"] = PROTOBUF_CONTENT_TYPE;\n }\n\n return headers;\n}\n\n// ============================================================================\n// 2. 内部工具函数 (Internal Utilities)\n// ============================================================================\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\n/** 简单的二进制异或混淆转换 */\nexport function xorTransform(\n data: Uint8Array,\n key: string | Uint8Array,\n): Uint8Array {\n const keyBytes = typeof key === \"string\" ? encoder.encode(key) : key;\n if (keyBytes.length === 0) return data;\n\n for (let i = 0; i < data.length; i++) {\n data[i] ^= keyBytes[i % keyBytes.length];\n }\n return data;\n}\n\n/** 创建一个简单的 XOR 混淆器 */\nexport function createXorObfuscator(\n key: string | Uint8Array,\n): ProtobufObfuscator {\n return {\n encrypt: (data) => xorTransform(data, key),\n decrypt: (data) => xorTransform(data, key),\n };\n}\n\n// ============================================================================\n// 3. 核心编解码逻辑 (Core Codecs)\n// ============================================================================\n\n/**\n * 编码安全载荷\n *\n * 流程:业务数据 -> (自定义编码 / Proto 序列化) -> 二进制混淆\n */\nexport async function encodeSecure<T>(\n data: T,\n options: ProtobufOptions = {},\n): Promise<Uint8Array> {\n const { obfuscator, protoType, transform, encode: customEncode } = options;\n\n let buffer: Uint8Array;\n\n // 1. 预处理阶段:无论后续走哪条路径,只要定义了 beforeEncode 就先执行\n const processedData = transform?.beforeEncode\n ? transform.beforeEncode(data)\n : data;\n\n // 1. 序列化阶段\n if (customEncode) {\n buffer = await customEncode(processedData);\n } else {\n // 构造最终要交给 Protobuf 序列化的对象\n if (protoType) {\n // 自定义模式:直接使用预处理后的数据\n const message = create(protoType, processedData as any);\n buffer = toBinary(protoType, message);\n } else {\n // 默认容器模式:将预处理后的数据封装进信封\n const payload = {\n ts: BigInt(Date.now()),\n data:\n processedData instanceof Uint8Array\n ? processedData\n : encoder.encode(JSON.stringify(processedData)),\n };\n const message = create(SecurePayloadSchema, payload);\n buffer = toBinary(SecurePayloadSchema, message);\n }\n }\n\n // 2. 混淆阶段\n if (obfuscator) {\n return await obfuscator.encrypt(buffer);\n }\n return buffer;\n}\n\n/**\n * 解码安全载荷\n *\n * 流程:二进制流 -> 二进制反混淆 -> (自定义解码 / Proto 反序列化) -> 业务数据\n */\nexport async function decodeSecure<T>(\n buffer: Uint8Array | ArrayBuffer,\n options: ProtobufOptions = {},\n): Promise<T> {\n const { obfuscator, protoType, transform, decode: customDecode } = options;\n\n // 1. 标准化为 Uint8Array 视图 (跨环境最稳写法)\n let uint8 = ArrayBuffer.isView(buffer)\n ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)\n : new Uint8Array(buffer);\n\n if (uint8.length === 0) {\n return null as T;\n }\n\n // 1. 混淆阶段\n if (uint8.length > 0 && obfuscator) {\n uint8 = await obfuscator.decrypt(uint8);\n }\n\n // 2. 反序列化阶段\n if (customDecode) {\n return await customDecode<T>(uint8);\n }\n\n const schema = protoType || SecurePayloadSchema;\n const message = fromBinary(schema, uint8);\n const plainObj = toJson(schema, message) as any;\n\n // 3. 转换阶段\n if (transform?.afterDecode) {\n return transform.afterDecode(plainObj);\n }\n\n // 内置容器模式的额外还原逻辑\n if (!protoType && plainObj.data) {\n // Note: toJson converts bytes to base64 string in pb-es v2\n // If we want raw Uint8Array, we might need a different conversion or manual handling\n // However, @bufbuild/protobuf's toJson by default base64 encodes bytes.\n // Let's check if we can get the raw object.\n\n // In pb-es v2, if we want the \"plain\" object with raw types,\n // we can just use the message itself as it is a standard JS object,\n // but with some non-enumerable properties.\n\n const rawData = (message as any).data as Uint8Array;\n if (rawData) {\n const jsonString = decoder.decode(rawData);\n try {\n return JSON.parse(jsonString) as T;\n } catch {\n return jsonString as unknown as T;\n }\n }\n }\n\n return plainObj as T;\n}\n\n// ============================================================================\n// 4. HTTP/环境适配工具 (HTTP Tools)\n// ============================================================================\n\n/** 创建安全响应 (Worker/Server 端使用) */\nexport async function secureResponse<T>(\n data: T,\n options?: ProtobufOptions & { corsHeaders?: Record<string, string> },\n): Promise<Response> {\n const buffer = await encodeSecure(data, options);\n const cleanBuffer = buffer.slice();\n\n return new Response(cleanBuffer, {\n headers: {\n ...getProtobufHeaders({ send: true }),\n ...options?.corsHeaders,\n },\n });\n}\n\n/** * 解析安全请求体 (Worker/Server 端使用)\n * @param request 原生 Request 对象\n * @param options 编解码配置(可包含当前接口对应的 protoType 和自定义 obfuscator)\n */\nexport async function parseSecureRequest<T>(\n request: Request,\n options: ProtobufOptions = {}, // 统一使用这个配置对象\n): Promise<T> {\n const buffer = await request.arrayBuffer();\n return decodeSecure<T>(buffer, options);\n}\n\n// ============================================================================\n// 5. API Client 钩子逻辑 (Integration Hooks)\n// ============================================================================\n\n/** 创建 Protobuf 编解码钩子 (用于与 createApiClient 配合使用) */\nexport function createProtobufHooks(globalOptions: ProtobufOptions = {}) {\n return {\n async onRequest(context: FetchContext) {\n const { options: reqOptions } = context;\n\n // 1. 动态合并配置\n const mergedOptions: ProtobufOptions = {\n ...globalOptions,\n ...reqOptions,\n };\n\n const headers =\n reqOptions.headers instanceof Headers\n ? reqOptions.headers\n : new Headers(reqOptions.headers as HeadersInit | undefined);\n\n // 自动编码请求体\n if (\n isProtobufContentType(headers.get(\"Content-Type\")) &&\n reqOptions.body &&\n !(reqOptions.body instanceof Uint8Array)\n ) {\n reqOptions.body = await encodeSecure(reqOptions.body, mergedOptions);\n }\n\n // 自动设置期望响应类型\n if (\n isProtobufContentType(headers.get(\"Accept\")) &&\n !reqOptions.responseType\n ) {\n reqOptions.responseType = \"arrayBuffer\";\n }\n\n reqOptions.headers = headers;\n },\n\n async onResponse(context: FetchContext) {\n const { response, options: reqOptions } = context;\n if (!response?._data || response.status === 204) return;\n\n // 同样在响应阶段合并 options\n const mergedOptions: ProtobufOptions = {\n ...globalOptions,\n ...reqOptions,\n };\n\n if (isProtobufContentType(response.headers.get(\"Content-Type\"))) {\n try {\n response._data = await decodeSecure(response._data, mergedOptions);\n } catch (e) {\n console.log(\"Error [Protobuf Decode Error]\", e);\n response._data = null;\n }\n } else if (response._data instanceof ArrayBuffer) {\n const text = decoder.decode(response._data);\n try {\n response._data = JSON.parse(text);\n } catch {\n response._data = text;\n }\n }\n },\n };\n}\n","// @generated by protoc-gen-es v2.10.2 with parameter \"target=ts\"\n// @generated from file src/secure.proto (package secure, syntax proto3)\n/* eslint-disable */\n\nimport type { GenFile, GenMessage } from \"@bufbuild/protobuf/codegenv2\";\nimport { fileDesc, messageDesc } from \"@bufbuild/protobuf/codegenv2\";\nimport type { Message } from \"@bufbuild/protobuf\";\n\n/**\n * Describes the file src/secure.proto.\n */\nexport const file_src_secure: GenFile = /*@__PURE__*/\n fileDesc(\"ChBzcmMvc2VjdXJlLnByb3RvEgZzZWN1cmUiKQoNU2VjdXJlUGF5bG9hZBIKCgJ0cxgBIAEoAxIMCgRkYXRhGAIgASgMYgZwcm90bzM\");\n\n/**\n * @generated from message secure.SecurePayload\n */\nexport type SecurePayload = Message<\"secure.SecurePayload\"> & {\n /**\n * @generated from field: int64 ts = 1;\n */\n ts: bigint;\n\n /**\n * @generated from field: bytes data = 2;\n */\n data: Uint8Array;\n};\n\n/**\n * Describes the message secure.SecurePayload.\n * Use `create(SecurePayloadSchema)` to create a new message.\n */\nexport const SecurePaylo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I,cAAc,CAAC,GAAG;AAC/D,YAAI;AACF,mBAAS,QAAQ,MAAM,aAAa,SAAS,OAAO,aAAa;AAAA,QACnE,SAAS,GAAG;AACV,kBAAQ,IAAI,iCAAiC,CAAC;AAC9C,mBAAS,QAAQ;AAAA,QACnB;AAAA,MACF,WAAW,SAAS,iBAAiB,aAAa;AAChD,cAAM,OAAO,QAAQ,OAAO,SAAS,KAAK;AAC1C,YAAI;AACF,mBAAS,QAAQ,KAAK,MAAM,IAAI;AAAA,QAClC,QAAQ;AACN,mBAAS,QAAQ;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -2,4 +2,4 @@ export { ApiError, ApiResult, CreateApiClientOptions, TokenStorage, createApiCli
|
|
|
2
2
|
export { ChainRequestRule, HttpRequestSpec, NetworkAdapter, NetworkHandler, executeRequestChain } from './chain.mjs';
|
|
3
3
|
export { PROTOBUF_CONTENT_TYPE, ProtobufObfuscator, ProtobufOptions, ProtobufTypeLike, createProtobufHooks, createXorObfuscator, decodeSecure, encodeSecure, getProtobufHeaders, isProtobufContentType, parseSecureRequest, secureResponse, xorTransform } from './protobuf.mjs';
|
|
4
4
|
import 'ofetch';
|
|
5
|
-
import '
|
|
5
|
+
import '@bufbuild/protobuf';
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export { ApiError, ApiResult, CreateApiClientOptions, TokenStorage, createApiCli
|
|
|
2
2
|
export { ChainRequestRule, HttpRequestSpec, NetworkAdapter, NetworkHandler, executeRequestChain } from './chain.js';
|
|
3
3
|
export { PROTOBUF_CONTENT_TYPE, ProtobufObfuscator, ProtobufOptions, ProtobufTypeLike, createProtobufHooks, createXorObfuscator, decodeSecure, encodeSecure, getProtobufHeaders, isProtobufContentType, parseSecureRequest, secureResponse, xorTransform } from './protobuf.js';
|
|
4
4
|
import 'ofetch';
|
|
5
|
-
import '
|
|
5
|
+
import '@bufbuild/protobuf';
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/index.ts
|
|
@@ -350,149 +340,13 @@ async function executeRequestChain(requests, handlers = [], getChainRequests, in
|
|
|
350
340
|
return lastResult;
|
|
351
341
|
}
|
|
352
342
|
|
|
353
|
-
// src/
|
|
354
|
-
var
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
var
|
|
358
|
-
var
|
|
359
|
-
var
|
|
360
|
-
const secure2 = {};
|
|
361
|
-
secure2.SecurePayload = (function() {
|
|
362
|
-
function SecurePayload(properties) {
|
|
363
|
-
if (properties) {
|
|
364
|
-
for (let keys = Object.keys(properties), i = 0; i < keys.length; ++i)
|
|
365
|
-
if (properties[keys[i]] != null)
|
|
366
|
-
this[keys[i]] = properties[keys[i]];
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
SecurePayload.prototype.ts = $util.Long ? $util.Long.fromBits(0, 0, false) : 0;
|
|
370
|
-
SecurePayload.prototype.data = $util.newBuffer([]);
|
|
371
|
-
SecurePayload.create = function create(properties) {
|
|
372
|
-
return new SecurePayload(properties);
|
|
373
|
-
};
|
|
374
|
-
SecurePayload.encode = function encode(message, writer) {
|
|
375
|
-
if (!writer)
|
|
376
|
-
writer = $Writer.create();
|
|
377
|
-
if (message.ts != null && Object.hasOwnProperty.call(message, "ts"))
|
|
378
|
-
writer.uint32(
|
|
379
|
-
/* id 1, wireType 0 =*/
|
|
380
|
-
8
|
|
381
|
-
).int64(message.ts);
|
|
382
|
-
if (message.data != null && Object.hasOwnProperty.call(message, "data"))
|
|
383
|
-
writer.uint32(
|
|
384
|
-
/* id 2, wireType 2 =*/
|
|
385
|
-
18
|
|
386
|
-
).bytes(message.data);
|
|
387
|
-
return writer;
|
|
388
|
-
};
|
|
389
|
-
SecurePayload.encodeDelimited = function encodeDelimited(message, writer) {
|
|
390
|
-
return this.encode(message, writer).ldelim();
|
|
391
|
-
};
|
|
392
|
-
SecurePayload.decode = function decode(reader, length, error) {
|
|
393
|
-
if (!(reader instanceof $Reader))
|
|
394
|
-
reader = $Reader.create(reader);
|
|
395
|
-
let end = length === void 0 ? reader.len : reader.pos + length, message = new $root.secure.SecurePayload();
|
|
396
|
-
while (reader.pos < end) {
|
|
397
|
-
let tag = reader.uint32();
|
|
398
|
-
if (tag === error)
|
|
399
|
-
break;
|
|
400
|
-
switch (tag >>> 3) {
|
|
401
|
-
case 1: {
|
|
402
|
-
message.ts = reader.int64();
|
|
403
|
-
break;
|
|
404
|
-
}
|
|
405
|
-
case 2: {
|
|
406
|
-
message.data = reader.bytes();
|
|
407
|
-
break;
|
|
408
|
-
}
|
|
409
|
-
default:
|
|
410
|
-
reader.skipType(tag & 7);
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return message;
|
|
415
|
-
};
|
|
416
|
-
SecurePayload.decodeDelimited = function decodeDelimited(reader) {
|
|
417
|
-
if (!(reader instanceof $Reader))
|
|
418
|
-
reader = new $Reader(reader);
|
|
419
|
-
return this.decode(reader, reader.uint32());
|
|
420
|
-
};
|
|
421
|
-
SecurePayload.verify = function verify(message) {
|
|
422
|
-
if (typeof message !== "object" || message === null)
|
|
423
|
-
return "object expected";
|
|
424
|
-
if (message.ts != null && message.hasOwnProperty("ts")) {
|
|
425
|
-
if (!$util.isInteger(message.ts) && !(message.ts && $util.isInteger(message.ts.low) && $util.isInteger(message.ts.high)))
|
|
426
|
-
return "ts: integer|Long expected";
|
|
427
|
-
}
|
|
428
|
-
if (message.data != null && message.hasOwnProperty("data")) {
|
|
429
|
-
if (!(message.data && typeof message.data.length === "number" || $util.isString(message.data)))
|
|
430
|
-
return "data: buffer expected";
|
|
431
|
-
}
|
|
432
|
-
return null;
|
|
433
|
-
};
|
|
434
|
-
SecurePayload.fromObject = function fromObject(object) {
|
|
435
|
-
if (object instanceof $root.secure.SecurePayload)
|
|
436
|
-
return object;
|
|
437
|
-
let message = new $root.secure.SecurePayload();
|
|
438
|
-
if (object.ts != null) {
|
|
439
|
-
if ($util.Long)
|
|
440
|
-
(message.ts = $util.Long.fromValue(object.ts)).unsigned = false;
|
|
441
|
-
else if (typeof object.ts === "string")
|
|
442
|
-
message.ts = parseInt(object.ts, 10);
|
|
443
|
-
else if (typeof object.ts === "number")
|
|
444
|
-
message.ts = object.ts;
|
|
445
|
-
else if (typeof object.ts === "object")
|
|
446
|
-
message.ts = new $util.LongBits(object.ts.low >>> 0, object.ts.high >>> 0).toNumber();
|
|
447
|
-
}
|
|
448
|
-
if (object.data != null) {
|
|
449
|
-
if (typeof object.data === "string")
|
|
450
|
-
$util.base64.decode(object.data, message.data = $util.newBuffer($util.base64.length(object.data)), 0);
|
|
451
|
-
else if (object.data.length >= 0)
|
|
452
|
-
message.data = object.data;
|
|
453
|
-
}
|
|
454
|
-
return message;
|
|
455
|
-
};
|
|
456
|
-
SecurePayload.toObject = function toObject(message, options) {
|
|
457
|
-
if (!options)
|
|
458
|
-
options = {};
|
|
459
|
-
let object = {};
|
|
460
|
-
if (options.defaults) {
|
|
461
|
-
if ($util.Long) {
|
|
462
|
-
let long = new $util.Long(0, 0, false);
|
|
463
|
-
object.ts = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
|
|
464
|
-
} else
|
|
465
|
-
object.ts = options.longs === String ? "0" : 0;
|
|
466
|
-
if (options.bytes === String)
|
|
467
|
-
object.data = "";
|
|
468
|
-
else {
|
|
469
|
-
object.data = [];
|
|
470
|
-
if (options.bytes !== Array)
|
|
471
|
-
object.data = $util.newBuffer(object.data);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
if (message.ts != null && message.hasOwnProperty("ts"))
|
|
475
|
-
if (typeof message.ts === "number")
|
|
476
|
-
object.ts = options.longs === String ? String(message.ts) : message.ts;
|
|
477
|
-
else
|
|
478
|
-
object.ts = options.longs === String ? $util.Long.prototype.toString.call(message.ts) : options.longs === Number ? new $util.LongBits(message.ts.low >>> 0, message.ts.high >>> 0).toNumber() : message.ts;
|
|
479
|
-
if (message.data != null && message.hasOwnProperty("data"))
|
|
480
|
-
object.data = options.bytes === String ? $util.base64.encode(message.data, 0, message.data.length) : options.bytes === Array ? Array.prototype.slice.call(message.data) : message.data;
|
|
481
|
-
return object;
|
|
482
|
-
};
|
|
483
|
-
SecurePayload.prototype.toJSON = function toJSON() {
|
|
484
|
-
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
|
|
485
|
-
};
|
|
486
|
-
SecurePayload.getTypeUrl = function getTypeUrl(typeUrlPrefix) {
|
|
487
|
-
if (typeUrlPrefix === void 0) {
|
|
488
|
-
typeUrlPrefix = "type.googleapis.com";
|
|
489
|
-
}
|
|
490
|
-
return typeUrlPrefix + "/secure.SecurePayload";
|
|
491
|
-
};
|
|
492
|
-
return SecurePayload;
|
|
493
|
-
})();
|
|
494
|
-
return secure2;
|
|
495
|
-
})();
|
|
343
|
+
// src/protobuf.ts
|
|
344
|
+
var import_protobuf = require("@bufbuild/protobuf");
|
|
345
|
+
|
|
346
|
+
// src/gen/secure_pb.ts
|
|
347
|
+
var import_codegenv2 = require("@bufbuild/protobuf/codegenv2");
|
|
348
|
+
var file_src_secure = /* @__PURE__ */ (0, import_codegenv2.fileDesc)("ChBzcmMvc2VjdXJlLnByb3RvEgZzZWN1cmUiKQoNU2VjdXJlUGF5bG9hZBIKCgJ0cxgBIAEoAxIMCgRkYXRhGAIgASgMYgZwcm90bzM");
|
|
349
|
+
var SecurePayloadSchema = /* @__PURE__ */ (0, import_codegenv2.messageDesc)(file_src_secure, 0);
|
|
496
350
|
|
|
497
351
|
// src/protobuf.ts
|
|
498
352
|
var PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
@@ -500,8 +354,8 @@ function isProtobufContentType(contentType) {
|
|
|
500
354
|
return !!contentType?.toLowerCase().includes(PROTOBUF_CONTENT_TYPE);
|
|
501
355
|
}
|
|
502
356
|
function getProtobufHeaders({
|
|
503
|
-
send =
|
|
504
|
-
receive =
|
|
357
|
+
send = false,
|
|
358
|
+
receive = false
|
|
505
359
|
} = {}) {
|
|
506
360
|
const headers = {};
|
|
507
361
|
if (send) {
|
|
@@ -528,9 +382,6 @@ function createXorObfuscator(key) {
|
|
|
528
382
|
decrypt: (data) => xorTransform(data, key)
|
|
529
383
|
};
|
|
530
384
|
}
|
|
531
|
-
function getInternalSecureType() {
|
|
532
|
-
return secure.SecurePayload;
|
|
533
|
-
}
|
|
534
385
|
async function encodeSecure(data, options = {}) {
|
|
535
386
|
const { obfuscator, protoType, transform, encode: customEncode } = options;
|
|
536
387
|
let buffer;
|
|
@@ -538,14 +389,17 @@ async function encodeSecure(data, options = {}) {
|
|
|
538
389
|
if (customEncode) {
|
|
539
390
|
buffer = await customEncode(processedData);
|
|
540
391
|
} else {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
392
|
+
if (protoType) {
|
|
393
|
+
const message = (0, import_protobuf.create)(protoType, processedData);
|
|
394
|
+
buffer = (0, import_protobuf.toBinary)(protoType, message);
|
|
395
|
+
} else {
|
|
396
|
+
const payload = {
|
|
397
|
+
ts: BigInt(Date.now()),
|
|
398
|
+
data: processedData instanceof Uint8Array ? processedData : encoder.encode(JSON.stringify(processedData))
|
|
399
|
+
};
|
|
400
|
+
const message = (0, import_protobuf.create)(SecurePayloadSchema, payload);
|
|
401
|
+
buffer = (0, import_protobuf.toBinary)(SecurePayloadSchema, message);
|
|
402
|
+
}
|
|
549
403
|
}
|
|
550
404
|
if (obfuscator) {
|
|
551
405
|
return await obfuscator.encrypt(buffer);
|
|
@@ -564,23 +418,21 @@ async function decodeSecure(buffer, options = {}) {
|
|
|
564
418
|
if (customDecode) {
|
|
565
419
|
return await customDecode(uint8);
|
|
566
420
|
}
|
|
567
|
-
const
|
|
568
|
-
const
|
|
569
|
-
const plainObj =
|
|
570
|
-
longs: String,
|
|
571
|
-
enums: String,
|
|
572
|
-
bytes: Uint8Array,
|
|
573
|
-
defaults: true
|
|
574
|
-
});
|
|
421
|
+
const schema = protoType || SecurePayloadSchema;
|
|
422
|
+
const message = (0, import_protobuf.fromBinary)(schema, uint8);
|
|
423
|
+
const plainObj = (0, import_protobuf.toJson)(schema, message);
|
|
575
424
|
if (transform?.afterDecode) {
|
|
576
425
|
return transform.afterDecode(plainObj);
|
|
577
426
|
}
|
|
578
427
|
if (!protoType && plainObj.data) {
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
428
|
+
const rawData = message.data;
|
|
429
|
+
if (rawData) {
|
|
430
|
+
const jsonString = decoder.decode(rawData);
|
|
431
|
+
try {
|
|
432
|
+
return JSON.parse(jsonString);
|
|
433
|
+
} catch {
|
|
434
|
+
return jsonString;
|
|
435
|
+
}
|
|
584
436
|
}
|
|
585
437
|
}
|
|
586
438
|
return plainObj;
|