@gravito/core 2.0.3 → 2.0.6

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.
@@ -0,0 +1,344 @@
1
+ // @bun
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
12
+ var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
20
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
21
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
+ for (let key of __getOwnPropNames(mod))
23
+ if (!__hasOwnProp.call(to, key))
24
+ __defProp(to, key, {
25
+ get: __accessProp.bind(mod, key),
26
+ enumerable: true
27
+ });
28
+ if (canCache)
29
+ cache.set(mod, to);
30
+ return to;
31
+ };
32
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
33
+ var __returnValue = (v) => v;
34
+ function __exportSetter(name, newValue) {
35
+ this[name] = __returnValue.bind(null, newValue);
36
+ }
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, {
40
+ get: all[name],
41
+ enumerable: true,
42
+ configurable: true,
43
+ set: __exportSetter.bind(all, name)
44
+ });
45
+ };
46
+ var __require = import.meta.require;
47
+
48
+ // src/ffi/types.ts
49
+ var CBOR_MAJOR_TYPES = {
50
+ UINT: 0,
51
+ NEGINT: 1,
52
+ BYTES: 2,
53
+ TEXT: 3,
54
+ ARRAY: 4,
55
+ MAP: 5,
56
+ TAG: 6,
57
+ SIMPLE: 7
58
+ };
59
+ var CBOR_SIMPLE_VALUES = {
60
+ FALSE: 20,
61
+ TRUE: 21,
62
+ NULL: 22,
63
+ UNDEFINED: 23
64
+ };
65
+ var CBOR_LENGTH_ENCODING = {
66
+ SMALL_RANGE_END: 23,
67
+ UINT8: 24,
68
+ UINT16: 25,
69
+ UINT32: 26,
70
+ UINT64: 27,
71
+ FLOAT16: 25,
72
+ FLOAT32: 26,
73
+ FLOAT64: 27,
74
+ INDEFINITE: 31
75
+ };
76
+
77
+ // src/ffi/cbor-fallback.ts
78
+ class CborFallbackEncoder {
79
+ static DEFAULT_BUFFER_SIZE = 4096;
80
+ static MAX_DEPTH = 16;
81
+ static MAX_BUFFER_SIZE = 1024 * 1024;
82
+ buffer;
83
+ offset;
84
+ constructor() {
85
+ this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE);
86
+ this.offset = 0;
87
+ }
88
+ encode(data) {
89
+ this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE);
90
+ this.offset = 0;
91
+ this.encodeValue(data, 0);
92
+ return this.buffer.slice(0, this.offset);
93
+ }
94
+ decode(bytes) {
95
+ const decoder = new CborFallbackDecoder(bytes);
96
+ const value = decoder.decode();
97
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
98
+ throw new Error("CBOR \u6839\u503C\u5FC5\u9808\u662F\u7269\u4EF6");
99
+ }
100
+ return value;
101
+ }
102
+ ensureCapacity(required) {
103
+ let newCapacity = this.buffer.length;
104
+ while (this.offset + required > newCapacity) {
105
+ newCapacity *= 2;
106
+ if (newCapacity > CborFallbackEncoder.MAX_BUFFER_SIZE) {
107
+ throw new Error(`CBOR \u7DE8\u78BC\u8D85\u904E\u6700\u5927 buffer \u5927\u5C0F ${CborFallbackEncoder.MAX_BUFFER_SIZE} \u4F4D\u5143\u7D44`);
108
+ }
109
+ }
110
+ if (newCapacity !== this.buffer.length) {
111
+ const newBuffer = new Uint8Array(newCapacity);
112
+ newBuffer.set(this.buffer);
113
+ this.buffer = newBuffer;
114
+ }
115
+ }
116
+ writeByte(byte) {
117
+ this.ensureCapacity(1);
118
+ this.buffer[this.offset++] = byte & 255;
119
+ }
120
+ writeBytes(bytes) {
121
+ this.ensureCapacity(bytes.length);
122
+ this.buffer.set(bytes, this.offset);
123
+ this.offset += bytes.length;
124
+ }
125
+ writeLength(majorType, length) {
126
+ const type = majorType << 5;
127
+ if (length < 24) {
128
+ this.writeByte(type | length);
129
+ } else if (length <= 255) {
130
+ this.writeByte(type | CBOR_LENGTH_ENCODING.UINT8);
131
+ this.writeByte(length);
132
+ } else if (length <= 65535) {
133
+ this.writeByte(type | CBOR_LENGTH_ENCODING.UINT16);
134
+ this.writeByte(length >> 8 & 255);
135
+ this.writeByte(length & 255);
136
+ } else if (length <= 4294967295) {
137
+ this.writeByte(type | CBOR_LENGTH_ENCODING.UINT32);
138
+ this.writeByte(length >> 24 & 255);
139
+ this.writeByte(length >> 16 & 255);
140
+ this.writeByte(length >> 8 & 255);
141
+ this.writeByte(length & 255);
142
+ } else {
143
+ throw new Error(`CBOR \u4E0D\u652F\u63F4 > 2^32 \u7684\u9577\u5EA6: ${length}`);
144
+ }
145
+ }
146
+ encodeValue(value, depth) {
147
+ if (depth > CborFallbackEncoder.MAX_DEPTH) {
148
+ throw new Error(`CBOR \u7DE8\u78BC\u6DF1\u5EA6\u8D85\u904E\u9650\u5236 ${CborFallbackEncoder.MAX_DEPTH}`);
149
+ }
150
+ if (value === null) {
151
+ this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.NULL);
152
+ } else if (value === true) {
153
+ this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.TRUE);
154
+ } else if (value === false) {
155
+ this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.FALSE);
156
+ } else if (typeof value === "number") {
157
+ this.encodeNumber(value);
158
+ } else if (typeof value === "string") {
159
+ this.encodeString(value);
160
+ } else if (value instanceof Uint8Array) {
161
+ this.encodeBytes(value);
162
+ } else if (Array.isArray(value)) {
163
+ this.encodeArray(value, depth);
164
+ } else if (typeof value === "object") {
165
+ this.encodeMap(value, depth);
166
+ } else {
167
+ throw new Error(`CBOR \u4E0D\u652F\u63F4\u985E\u578B: ${typeof value}`);
168
+ }
169
+ }
170
+ encodeNumber(num) {
171
+ if (Number.isInteger(num) && !Object.is(num, -0)) {
172
+ if (num >= 0 && num <= 4294967295) {
173
+ this.writeLength(CBOR_MAJOR_TYPES.UINT, num);
174
+ return;
175
+ }
176
+ if (num < 0 && -1 - num <= 4294967295) {
177
+ this.writeLength(CBOR_MAJOR_TYPES.NEGINT, -1 - num);
178
+ return;
179
+ }
180
+ }
181
+ {
182
+ const bytes = new Uint8Array(9);
183
+ bytes[0] = CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_LENGTH_ENCODING.FLOAT64;
184
+ const view = new DataView(new ArrayBuffer(8));
185
+ view.setFloat64(0, num, false);
186
+ for (let i = 0;i < 8; i++) {
187
+ bytes[i + 1] = view.getUint8(i);
188
+ }
189
+ this.writeBytes(bytes);
190
+ }
191
+ }
192
+ encodeString(str) {
193
+ const encoder = new TextEncoder;
194
+ const bytes = encoder.encode(str);
195
+ this.writeLength(CBOR_MAJOR_TYPES.TEXT, bytes.length);
196
+ this.writeBytes(bytes);
197
+ }
198
+ encodeBytes(bytes) {
199
+ this.writeLength(CBOR_MAJOR_TYPES.BYTES, bytes.length);
200
+ this.writeBytes(bytes);
201
+ }
202
+ encodeArray(arr, depth) {
203
+ this.writeLength(CBOR_MAJOR_TYPES.ARRAY, arr.length);
204
+ for (const item of arr) {
205
+ this.encodeValue(item, depth + 1);
206
+ }
207
+ }
208
+ encodeMap(obj, depth) {
209
+ const keys = Object.keys(obj);
210
+ this.writeLength(CBOR_MAJOR_TYPES.MAP, keys.length);
211
+ for (const key of keys) {
212
+ this.encodeString(key);
213
+ this.encodeValue(obj[key], depth + 1);
214
+ }
215
+ }
216
+ }
217
+
218
+ class CborFallbackDecoder {
219
+ static MAX_DEPTH = 16;
220
+ data;
221
+ offset;
222
+ constructor(data) {
223
+ this.data = data;
224
+ this.offset = 0;
225
+ }
226
+ decode() {
227
+ return this.decodeValue(0);
228
+ }
229
+ readByte() {
230
+ if (this.offset >= this.data.length) {
231
+ throw new Error("CBOR \u8CC7\u6599\u4E0D\u8DB3");
232
+ }
233
+ return this.data[this.offset++];
234
+ }
235
+ readBytes(length) {
236
+ if (this.offset + length > this.data.length) {
237
+ throw new Error("CBOR \u8CC7\u6599\u4E0D\u8DB3");
238
+ }
239
+ const result = this.data.slice(this.offset, this.offset + length);
240
+ this.offset += length;
241
+ return result;
242
+ }
243
+ readLength(additionalInfo) {
244
+ if (additionalInfo < 24) {
245
+ return additionalInfo;
246
+ }
247
+ if (additionalInfo === 24) {
248
+ return this.readByte();
249
+ }
250
+ if (additionalInfo === 25) {
251
+ const b1 = this.readByte();
252
+ const b2 = this.readByte();
253
+ return b1 << 8 | b2;
254
+ }
255
+ if (additionalInfo === 26) {
256
+ const b1 = this.readByte();
257
+ const b2 = this.readByte();
258
+ const b3 = this.readByte();
259
+ const b4 = this.readByte();
260
+ return b1 << 24 | b2 << 16 | b3 << 8 | b4;
261
+ }
262
+ if (additionalInfo === 27) {
263
+ const high = this.readByte() << 24 | this.readByte() << 16 | this.readByte() << 8 | this.readByte();
264
+ const low = this.readByte() << 24 | this.readByte() << 16 | this.readByte() << 8 | this.readByte();
265
+ return high * 4294967296 + low;
266
+ }
267
+ throw new Error(`CBOR \u7121\u6548\u7684\u9577\u5EA6\u7DE8\u78BC: ${additionalInfo}`);
268
+ }
269
+ decodeValue(depth) {
270
+ if (depth > CborFallbackDecoder.MAX_DEPTH) {
271
+ throw new Error(`CBOR \u89E3\u78BC\u6DF1\u5EA6\u8D85\u904E\u9650\u5236 ${CborFallbackDecoder.MAX_DEPTH}`);
272
+ }
273
+ const byte = this.readByte();
274
+ const majorType = byte >> 5 & 7;
275
+ const additionalInfo = byte & 31;
276
+ switch (majorType) {
277
+ case CBOR_MAJOR_TYPES.UINT:
278
+ return this.readLength(additionalInfo);
279
+ case CBOR_MAJOR_TYPES.NEGINT:
280
+ return -1 - this.readLength(additionalInfo);
281
+ case CBOR_MAJOR_TYPES.BYTES: {
282
+ const length = this.readLength(additionalInfo);
283
+ return this.readBytes(length);
284
+ }
285
+ case CBOR_MAJOR_TYPES.TEXT: {
286
+ const length = this.readLength(additionalInfo);
287
+ const bytes = this.readBytes(length);
288
+ const decoder = new TextDecoder;
289
+ return decoder.decode(bytes);
290
+ }
291
+ case CBOR_MAJOR_TYPES.ARRAY: {
292
+ const length = this.readLength(additionalInfo);
293
+ const result = [];
294
+ for (let i = 0;i < length; i++) {
295
+ result.push(this.decodeValue(depth + 1));
296
+ }
297
+ return result;
298
+ }
299
+ case CBOR_MAJOR_TYPES.MAP: {
300
+ const length = this.readLength(additionalInfo);
301
+ const result = {};
302
+ for (let i = 0;i < length; i++) {
303
+ const key = this.decodeValue(depth + 1);
304
+ if (typeof key !== "string") {
305
+ throw new Error(`CBOR Map \u7684 key \u5FC5\u9808\u662F\u5B57\u4E32\uFF0C\u5F97\u5230: ${typeof key}`);
306
+ }
307
+ result[key] = this.decodeValue(depth + 1);
308
+ }
309
+ return result;
310
+ }
311
+ case CBOR_MAJOR_TYPES.SIMPLE:
312
+ if (additionalInfo === CBOR_SIMPLE_VALUES.FALSE) {
313
+ return false;
314
+ }
315
+ if (additionalInfo === CBOR_SIMPLE_VALUES.TRUE) {
316
+ return true;
317
+ }
318
+ if (additionalInfo === CBOR_SIMPLE_VALUES.NULL) {
319
+ return null;
320
+ }
321
+ if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT32) {
322
+ const bytes = this.readBytes(4);
323
+ const view = new DataView(bytes.buffer, bytes.byteOffset);
324
+ return view.getFloat32(0, false);
325
+ }
326
+ if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT64) {
327
+ const bytes = this.readBytes(8);
328
+ const view = new DataView(bytes.buffer, bytes.byteOffset);
329
+ return view.getFloat64(0, false);
330
+ }
331
+ throw new Error(`CBOR \u4E0D\u652F\u63F4\u7684 simple value: ${additionalInfo}`);
332
+ case CBOR_MAJOR_TYPES.TAG:
333
+ return this.decodeValue(depth);
334
+ default:
335
+ throw new Error(`CBOR \u7121\u6548\u7684 major type: ${majorType}`);
336
+ }
337
+ }
338
+ }
339
+ export {
340
+ CborFallbackEncoder,
341
+ CborFallbackDecoder
342
+ };
343
+
344
+ //# debugId=8CA25F55A43EF27E64756E2164756E21
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/ffi/types.ts", "../src/ffi/cbor-fallback.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * FFI (Foreign Function Interface) 類型定義\n * 支持 bun:ffi 的原生加速層\n */\n\n/**\n * CBOR 編碼/解碼加速器介面\n * 可由原生 C 實現或 JavaScript 回退實現\n */\nexport interface CborAccelerator {\n /**\n * 將任意 JavaScript 物件編碼為 CBOR 二進制格式\n * @param data - 要編碼的物件(支援:map、string、uint、float64、bytes、null、boolean)\n * @returns CBOR 編碼的二進制資料\n */\n encode(data: Record<string, unknown>): Uint8Array\n\n /**\n * 將 CBOR 二進制格式解碼為 JavaScript 物件\n * @param bytes - CBOR 編碼的二進制資料\n * @returns 解碼後的物件\n */\n decode(bytes: Uint8Array): Record<string, unknown>\n}\n\n/**\n * FFI 加速層狀態報告\n */\nexport interface NativeAcceleratorStatus {\n /**\n * 原生 FFI 加速是否可用\n */\n readonly available: boolean\n\n /**\n * 當前使用的運行時實現\n * - 'bun-ffi': Bun C 編譯器(bun:ffi 的 cc())\n * - 'js-fallback': 手寫 JavaScript CBOR 實現\n * - 'cborg': npm 的 cborg 套件(已棄用,僅用於向後相容)\n */\n readonly runtime: 'bun-ffi' | 'js-fallback' | 'cborg'\n\n /**\n * 運行時版本或詳細資訊\n */\n readonly version: string\n}\n\n/**\n * FFI 層配置選項\n */\nexport interface FfiConfig {\n /**\n * 啟用調試日誌\n * @default false\n */\n readonly debug?: boolean\n\n /**\n * 最大 buffer 大小(位元組)\n * @default 1048576 (1MB)\n */\n readonly maxBufferSize?: number\n\n /**\n * 強制使用特定的加速器實現\n * - undefined: 自動選擇(優先 bun-ffi,降級到 js-fallback)\n * - 'bun-ffi': 只使用原生 C 實現\n * - 'js-fallback': 只使用 JavaScript 實現\n */\n readonly forceImplementation?: 'bun-ffi' | 'js-fallback'\n}\n\n/**\n * CBOR Major Type 常數\n * 符合 RFC 7049 規範\n */\nexport const CBOR_MAJOR_TYPES = {\n UINT: 0, // 正整數 (0 to 2^64-1)\n NEGINT: 1, // 負整數 (-1 to -2^64)\n BYTES: 2, // 位元組字串\n TEXT: 3, // 文字字串\n ARRAY: 4, // 陣列\n MAP: 5, // Map 物件\n TAG: 6, // 語意 tag\n SIMPLE: 7, // 簡單值與浮點數\n} as const\n\n/**\n * CBOR 簡單值常數\n */\nexport const CBOR_SIMPLE_VALUES = {\n FALSE: 20, // Boolean false\n TRUE: 21, // Boolean true\n NULL: 22, // null\n UNDEFINED: 23, // undefined (不常用)\n} as const\n\n/**\n * CBOR 長度編碼的附加資訊\n */\nexport const CBOR_LENGTH_ENCODING = {\n SMALL_RANGE_END: 23, // 0-23: 直接作為值\n UINT8: 24, // 24: 接下來 1 byte 為 uint8_t\n UINT16: 25, // 25: 接下來 2 bytes 為 uint16_t (大端序)\n UINT32: 26, // 26: 接下來 4 bytes 為 uint32_t (大端序)\n UINT64: 27, // 27: 接下來 8 bytes 為 uint64_t (大端序)\n FLOAT16: 25, // 25: IEEE 754 半精度浮點數 (special case for SIMPLE)\n FLOAT32: 26, // 26: IEEE 754 單精度浮點數\n FLOAT64: 27, // 27: IEEE 754 雙精度浮點數\n INDEFINITE: 31, // 31: 無限長度編碼\n} as const\n\n/**\n * 雜湊加速器介面\n * 可由 Bun 原生實現或 Node.js 回退實現\n */\nexport interface HashAccelerator {\n /**\n * SHA-256 雜湊計算\n * @param input - 輸入(字串或二進制)\n * @returns 十六進制編碼的 SHA-256 雜湊值(64 字元)\n */\n sha256(input: string | Uint8Array): string\n\n /**\n * HMAC-SHA256 計算\n * @param key - 密鑰\n * @param data - 要雜湊的數據\n * @returns 十六進制編碼的 HMAC-SHA256 值(64 字元)\n */\n hmacSha256(key: string, data: string): string\n}\n\n/**\n * 雜湊加速器狀態報告\n */\nexport interface NativeHasherStatus {\n /**\n * 雜湊加速層是否可用\n */\n readonly available: boolean\n\n /**\n * 當前使用的運行時實現\n * - 'bun-crypto-hasher': Bun 原生 CryptoHasher(C 實現,推薦)\n * - 'node-crypto': node:crypto 回退實現\n */\n readonly runtime: 'bun-crypto-hasher' | 'node-crypto'\n}\n",
6
+ "/**\n * JavaScript CBOR 回退實現\n * 用於非 Bun 環境或 FFI 不可用的情況\n * 符合 RFC 7049 規範\n */\n\nimport type { CborAccelerator } from './types'\nimport { CBOR_LENGTH_ENCODING, CBOR_MAJOR_TYPES, CBOR_SIMPLE_VALUES } from './types'\n\n/**\n * CBOR 編碼器\n * 將 JavaScript 物件編碼為 CBOR 二進制格式\n */\nexport class CborFallbackEncoder implements CborAccelerator {\n private static readonly DEFAULT_BUFFER_SIZE = 4096\n private static readonly MAX_DEPTH = 16\n private static readonly MAX_BUFFER_SIZE = 1024 * 1024 // 1MB\n\n private buffer: Uint8Array\n private offset: number\n\n constructor() {\n this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE)\n this.offset = 0\n }\n\n /**\n * 編碼 JavaScript 物件為 CBOR 格式\n */\n encode(data: Record<string, unknown>): Uint8Array {\n this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE)\n this.offset = 0\n this.encodeValue(data, 0)\n return this.buffer.slice(0, this.offset)\n }\n\n /**\n * 解碼 CBOR 二進制為 JavaScript 物件\n */\n decode(bytes: Uint8Array): Record<string, unknown> {\n const decoder = new CborFallbackDecoder(bytes)\n const value = decoder.decode()\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n throw new Error('CBOR 根值必須是物件')\n }\n return value as Record<string, unknown>\n }\n\n /**\n * 確保 buffer 有足夠空間\n */\n private ensureCapacity(required: number): void {\n let newCapacity = this.buffer.length\n while (this.offset + required > newCapacity) {\n newCapacity *= 2\n if (newCapacity > CborFallbackEncoder.MAX_BUFFER_SIZE) {\n throw new Error(\n `CBOR 編碼超過最大 buffer 大小 ${CborFallbackEncoder.MAX_BUFFER_SIZE} 位元組`\n )\n }\n }\n if (newCapacity !== this.buffer.length) {\n const newBuffer = new Uint8Array(newCapacity)\n newBuffer.set(this.buffer)\n this.buffer = newBuffer\n }\n }\n\n /**\n * 編寫一個位元組\n */\n private writeByte(byte: number): void {\n this.ensureCapacity(1)\n this.buffer[this.offset++] = byte & 0xff\n }\n\n /**\n * 編寫多個位元組\n */\n private writeBytes(bytes: ArrayLike<number>): void {\n this.ensureCapacity(bytes.length)\n this.buffer.set(bytes, this.offset)\n this.offset += bytes.length\n }\n\n /**\n * 編寫 CBOR 長度(Major Type + Additional Info)\n */\n private writeLength(majorType: number, length: number): void {\n const type = majorType << 5\n\n if (length < 24) {\n this.writeByte(type | length)\n } else if (length <= 0xff) {\n this.writeByte(type | CBOR_LENGTH_ENCODING.UINT8)\n this.writeByte(length)\n } else if (length <= 0xffff) {\n this.writeByte(type | CBOR_LENGTH_ENCODING.UINT16)\n this.writeByte((length >> 8) & 0xff)\n this.writeByte(length & 0xff)\n } else if (length <= 0xffffffff) {\n this.writeByte(type | CBOR_LENGTH_ENCODING.UINT32)\n this.writeByte((length >> 24) & 0xff)\n this.writeByte((length >> 16) & 0xff)\n this.writeByte((length >> 8) & 0xff)\n this.writeByte(length & 0xff)\n } else {\n throw new Error(`CBOR 不支援 > 2^32 的長度: ${length}`)\n }\n }\n\n /**\n * 遞迴編碼值\n */\n private encodeValue(value: unknown, depth: number): void {\n if (depth > CborFallbackEncoder.MAX_DEPTH) {\n throw new Error(`CBOR 編碼深度超過限制 ${CborFallbackEncoder.MAX_DEPTH}`)\n }\n\n if (value === null) {\n this.writeByte((CBOR_MAJOR_TYPES.SIMPLE << 5) | CBOR_SIMPLE_VALUES.NULL)\n } else if (value === true) {\n this.writeByte((CBOR_MAJOR_TYPES.SIMPLE << 5) | CBOR_SIMPLE_VALUES.TRUE)\n } else if (value === false) {\n this.writeByte((CBOR_MAJOR_TYPES.SIMPLE << 5) | CBOR_SIMPLE_VALUES.FALSE)\n } else if (typeof value === 'number') {\n this.encodeNumber(value)\n } else if (typeof value === 'string') {\n this.encodeString(value)\n } else if (value instanceof Uint8Array) {\n this.encodeBytes(value)\n } else if (Array.isArray(value)) {\n this.encodeArray(value, depth)\n } else if (typeof value === 'object') {\n this.encodeMap(value as Record<string, unknown>, depth)\n } else {\n throw new Error(`CBOR 不支援類型: ${typeof value}`)\n }\n }\n\n /**\n * 編碼整數或浮點數\n * 對於超過 uint32 範圍的整數,使用 float64 編碼(JavaScript 精度限制)\n */\n private encodeNumber(num: number): void {\n // 檢查是否為整數且在 uint32/negint32 範圍內\n if (Number.isInteger(num) && !Object.is(num, -0)) {\n if (num >= 0 && num <= 0xffffffff) {\n this.writeLength(CBOR_MAJOR_TYPES.UINT, num)\n return\n }\n if (num < 0 && -1 - num <= 0xffffffff) {\n this.writeLength(CBOR_MAJOR_TYPES.NEGINT, -1 - num)\n return\n }\n // 超過 32-bit 範圍的整數,降級為 float64\n }\n {\n // 浮點數(float64)\n const bytes = new Uint8Array(9)\n bytes[0] = (CBOR_MAJOR_TYPES.SIMPLE << 5) | CBOR_LENGTH_ENCODING.FLOAT64\n\n // IEEE 754 雙精度大端序\n const view = new DataView(new ArrayBuffer(8))\n view.setFloat64(0, num, false)\n for (let i = 0; i < 8; i++) {\n bytes[i + 1] = view.getUint8(i)\n }\n this.writeBytes(bytes)\n }\n }\n\n /**\n * 編碼字串\n */\n private encodeString(str: string): void {\n const encoder = new TextEncoder()\n const bytes = encoder.encode(str)\n this.writeLength(CBOR_MAJOR_TYPES.TEXT, bytes.length)\n this.writeBytes(bytes)\n }\n\n /**\n * 編碼位元組陣列\n */\n private encodeBytes(bytes: Uint8Array): void {\n this.writeLength(CBOR_MAJOR_TYPES.BYTES, bytes.length)\n this.writeBytes(bytes)\n }\n\n /**\n * 編碼陣列\n */\n private encodeArray(arr: unknown[], depth: number): void {\n this.writeLength(CBOR_MAJOR_TYPES.ARRAY, arr.length)\n for (const item of arr) {\n this.encodeValue(item, depth + 1)\n }\n }\n\n /**\n * 編碼物件(Map)\n */\n private encodeMap(obj: Record<string, unknown>, depth: number): void {\n const keys = Object.keys(obj)\n this.writeLength(CBOR_MAJOR_TYPES.MAP, keys.length)\n for (const key of keys) {\n this.encodeString(key)\n this.encodeValue(obj[key], depth + 1)\n }\n }\n}\n\n/**\n * CBOR 解碼器\n */\nexport class CborFallbackDecoder {\n private static readonly MAX_DEPTH = 16\n private data: Uint8Array\n private offset: number\n\n constructor(data: Uint8Array) {\n this.data = data\n this.offset = 0\n }\n\n /**\n * 解碼 CBOR 資料\n */\n decode(): unknown {\n return this.decodeValue(0)\n }\n\n /**\n * 讀取一個位元組\n */\n private readByte(): number {\n if (this.offset >= this.data.length) {\n throw new Error('CBOR 資料不足')\n }\n return this.data[this.offset++]\n }\n\n /**\n * 讀取固定長度的位元組\n */\n private readBytes(length: number): Uint8Array {\n if (this.offset + length > this.data.length) {\n throw new Error('CBOR 資料不足')\n }\n const result = this.data.slice(this.offset, this.offset + length)\n this.offset += length\n return result\n }\n\n /**\n * 讀取 CBOR 長度\n */\n private readLength(additionalInfo: number): number {\n if (additionalInfo < 24) {\n return additionalInfo\n }\n if (additionalInfo === 24) {\n return this.readByte()\n }\n if (additionalInfo === 25) {\n const b1 = this.readByte()\n const b2 = this.readByte()\n return (b1 << 8) | b2\n }\n if (additionalInfo === 26) {\n const b1 = this.readByte()\n const b2 = this.readByte()\n const b3 = this.readByte()\n const b4 = this.readByte()\n return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4\n }\n if (additionalInfo === 27) {\n // 64-bit,但 JavaScript 無法精確表示,只支援到 2^53\n const high =\n (this.readByte() << 24) | (this.readByte() << 16) | (this.readByte() << 8) | this.readByte()\n const low =\n (this.readByte() << 24) | (this.readByte() << 16) | (this.readByte() << 8) | this.readByte()\n return high * 0x100000000 + low\n }\n throw new Error(`CBOR 無效的長度編碼: ${additionalInfo}`)\n }\n\n /**\n * 遞迴解碼值\n */\n private decodeValue(depth: number): unknown {\n if (depth > CborFallbackDecoder.MAX_DEPTH) {\n throw new Error(`CBOR 解碼深度超過限制 ${CborFallbackDecoder.MAX_DEPTH}`)\n }\n\n const byte = this.readByte()\n const majorType = (byte >> 5) & 0x07\n const additionalInfo = byte & 0x1f\n\n switch (majorType) {\n case CBOR_MAJOR_TYPES.UINT:\n return this.readLength(additionalInfo)\n\n case CBOR_MAJOR_TYPES.NEGINT:\n return -1 - this.readLength(additionalInfo)\n\n case CBOR_MAJOR_TYPES.BYTES: {\n const length = this.readLength(additionalInfo)\n return this.readBytes(length)\n }\n\n case CBOR_MAJOR_TYPES.TEXT: {\n const length = this.readLength(additionalInfo)\n const bytes = this.readBytes(length)\n const decoder = new TextDecoder()\n return decoder.decode(bytes)\n }\n\n case CBOR_MAJOR_TYPES.ARRAY: {\n const length = this.readLength(additionalInfo)\n const result: unknown[] = []\n for (let i = 0; i < length; i++) {\n result.push(this.decodeValue(depth + 1))\n }\n return result\n }\n\n case CBOR_MAJOR_TYPES.MAP: {\n const length = this.readLength(additionalInfo)\n const result: Record<string, unknown> = {}\n for (let i = 0; i < length; i++) {\n const key = this.decodeValue(depth + 1)\n if (typeof key !== 'string') {\n throw new Error(`CBOR Map 的 key 必須是字串,得到: ${typeof key}`)\n }\n result[key] = this.decodeValue(depth + 1)\n }\n return result\n }\n\n case CBOR_MAJOR_TYPES.SIMPLE:\n if (additionalInfo === CBOR_SIMPLE_VALUES.FALSE) {\n return false\n }\n if (additionalInfo === CBOR_SIMPLE_VALUES.TRUE) {\n return true\n }\n if (additionalInfo === CBOR_SIMPLE_VALUES.NULL) {\n return null\n }\n\n // 浮點數\n if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT32) {\n const bytes = this.readBytes(4)\n const view = new DataView(bytes.buffer, bytes.byteOffset)\n return view.getFloat32(0, false)\n }\n if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT64) {\n const bytes = this.readBytes(8)\n const view = new DataView(bytes.buffer, bytes.byteOffset)\n return view.getFloat64(0, false)\n }\n\n throw new Error(`CBOR 不支援的 simple value: ${additionalInfo}`)\n\n case CBOR_MAJOR_TYPES.TAG:\n // TAG 類型在此實現中跳過\n return this.decodeValue(depth)\n\n default:\n throw new Error(`CBOR 無效的 major type: ${majorType}`)\n }\n }\n}\n"
7
+ ],
8
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EO,IAAM,mBAAmB;AAAA,EAC9B,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,MAAM;AAAA,EACN,OAAO;AAAA,EACP,KAAK;AAAA,EACL,KAAK;AAAA,EACL,QAAQ;AACV;AAKO,IAAM,qBAAqB;AAAA,EAChC,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,WAAW;AACb;AAKO,IAAM,uBAAuB;AAAA,EAClC,iBAAiB;AAAA,EACjB,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS;AAAA,EACT,YAAY;AACd;;;AClGO,MAAM,oBAA+C;AAAA,SAClC,sBAAsB;AAAA,SACtB,YAAY;AAAA,SACZ,kBAAkB,OAAO;AAAA,EAEzC;AAAA,EACA;AAAA,EAER,WAAW,GAAG;AAAA,IACZ,KAAK,SAAS,IAAI,WAAW,oBAAoB,mBAAmB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A,QACT;AAAA,QAGA,IAAI,mBAAmB,qBAAqB,SAAS;AAAA,UACnD,MAAM,QAAQ,KAAK,UAAU,CAAC;AAAA,UAC9B,MAAM,OAAO,IAAI,SAAS,MAAM,QAAQ,MAAM,UAAU;AAAA,UACxD,OAAO,KAAK,WAAW,GAAG,KAAK;AAAA,QACjC;AAAA,QACA,IAAI,mBAAmB,qBAAqB,SAAS;AAAA,UACnD,MAAM,QAAQ,KAAK,UAAU,CAAC;AAAA,UAC9B,MAAM,OAAO,IAAI,SAAS,MAAM,QAAQ,MAAM,UAAU;AAAA,UACxD,OAAO,KAAK,WAAW,GAAG,KAAK;AAAA,QACjC;AAAA,QAEA,MAAM,IAAI,MAAM,+CAA0B,gBAAgB;AAAA,WAEvD,iBAAiB;AAAA,QAEpB,OAAO,KAAK,YAAY,KAAK;AAAA;AAAA,QAG7B,MAAM,IAAI,MAAM,uCAAuB,WAAW;AAAA;AAAA;AAG1D;",
9
+ "debugId": "8CA25F55A43EF27E64756E2164756E21",
10
+ "names": []
11
+ }
@@ -0,0 +1,63 @@
1
+ // @bun
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
12
+ var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
20
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
21
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
+ for (let key of __getOwnPropNames(mod))
23
+ if (!__hasOwnProp.call(to, key))
24
+ __defProp(to, key, {
25
+ get: __accessProp.bind(mod, key),
26
+ enumerable: true
27
+ });
28
+ if (canCache)
29
+ cache.set(mod, to);
30
+ return to;
31
+ };
32
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
33
+ var __returnValue = (v) => v;
34
+ function __exportSetter(name, newValue) {
35
+ this[name] = __returnValue.bind(null, newValue);
36
+ }
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, {
40
+ get: all[name],
41
+ enumerable: true,
42
+ configurable: true,
43
+ set: __exportSetter.bind(all, name)
44
+ });
45
+ };
46
+ var __require = import.meta.require;
47
+
48
+ // src/ffi/hash-fallback.ts
49
+ import { createHash, createHmac } from "crypto";
50
+
51
+ class HashFallback {
52
+ sha256(input) {
53
+ return createHash("sha256").update(input).digest("hex");
54
+ }
55
+ hmacSha256(key, data) {
56
+ return createHmac("sha256", key).update(data).digest("hex");
57
+ }
58
+ }
59
+ export {
60
+ HashFallback
61
+ };
62
+
63
+ //# debugId=3459244799B7D36C64756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/ffi/hash-fallback.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * 雜湊加速器 JavaScript 回退實現\n * 基於 node:crypto 標準庫\n *\n * 使用場景:\n * - 非 Bun 環境(Node.js、Deno 等)\n * - Bun CryptoHasher 不可用的情況\n *\n * 性能特性:\n * - 短 key (<100 bytes):~1-2x 慢於 Bun.CryptoHasher(N-API 橋接開銷)\n * - 長 payload:差異較小(主要計算時間)\n * - 一致性:與 node:crypto 標準行為完全相同\n */\n\nimport { createHash, createHmac } from 'node:crypto'\nimport type { HashAccelerator } from './types'\n\n/**\n * Node.js crypto 回退實現\n * 適用於非 Bun 環境\n */\nexport class HashFallback implements HashAccelerator {\n /**\n * SHA-256 計算(回退實現)\n * @param input - 輸入(字串或 Uint8Array)\n * @returns 十六進制編碼的 SHA-256 雜湊值\n */\n sha256(input: string | Uint8Array): string {\n return createHash('sha256').update(input).digest('hex')\n }\n\n /**\n * HMAC-SHA256 計算(回退實現)\n * @param key - 密鑰\n * @param data - 要雜湊的數據\n * @returns 十六進制編碼的 HMAC-SHA256 值\n */\n hmacSha256(key: string, data: string): string {\n return createHmac('sha256', key).update(data).digest('hex')\n }\n}\n"
6
+ ],
7
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA;AAAA;AAOO,MAAM,aAAwC;AAAA,EAMnD,MAAM,CAAC,OAAoC;AAAA,IACzC,OAAO,WAAW,QAAQ,EAAE,OAAO,KAAK,EAAE,OAAO,KAAK;AAAA;AAAA,EASxD,UAAU,CAAC,KAAa,MAAsB;AAAA,IAC5C,OAAO,WAAW,UAAU,GAAG,EAAE,OAAO,IAAI,EAAE,OAAO,KAAK;AAAA;AAE9D;",
8
+ "debugId": "3459244799B7D36C64756E2164756E21",
9
+ "names": []
10
+ }
package/dist/ffi/index.js CHANGED
@@ -1,131 +1,5 @@
1
- // @bun
2
- var __create = Object.create;
3
- var __getProtoOf = Object.getPrototypeOf;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- function __accessProp(key) {
8
- return this[key];
9
- }
10
- var __toESMCache_node;
11
- var __toESMCache_esm;
12
- var __toESM = (mod, isNodeMode, target) => {
13
- var canCache = mod != null && typeof mod === "object";
14
- if (canCache) {
15
- var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
- var cached = cache.get(mod);
17
- if (cached)
18
- return cached;
19
- }
20
- target = mod != null ? __create(__getProtoOf(mod)) : {};
21
- const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
- for (let key of __getOwnPropNames(mod))
23
- if (!__hasOwnProp.call(to, key))
24
- __defProp(to, key, {
25
- get: __accessProp.bind(mod, key),
26
- enumerable: true
27
- });
28
- if (canCache)
29
- cache.set(mod, to);
30
- return to;
31
- };
32
- var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
33
- var __returnValue = (v) => v;
34
- function __exportSetter(name, newValue) {
35
- this[name] = __returnValue.bind(null, newValue);
36
- }
37
- var __export = (target, all) => {
38
- for (var name in all)
39
- __defProp(target, name, {
40
- get: all[name],
41
- enumerable: true,
42
- configurable: true,
43
- set: __exportSetter.bind(all, name)
44
- });
45
- };
46
- var __require = import.meta.require;
47
- // src/ffi/hash-fallback.ts
48
- import { createHash, createHmac } from "crypto";
49
-
50
- class HashFallback {
51
- sha256(input) {
52
- return createHash("sha256").update(input).digest("hex");
53
- }
54
- hmacSha256(key, data) {
55
- return createHmac("sha256", key).update(data).digest("hex");
56
- }
57
- }
58
-
59
- // src/ffi/NativeHasher.ts
60
- class BunCryptoHasher {
61
- sha256(input) {
62
- return new Bun.CryptoHasher("sha256").update(input).digest("hex");
63
- }
64
- hmacSha256(key, data) {
65
- return new Bun.CryptoHasher("sha256", key).update(data).digest("hex");
66
- }
67
- }
68
-
69
- class NativeHasher {
70
- static accelerator = null;
71
- static status = null;
72
- static getAccelerator() {
73
- if (this.accelerator !== null) {
74
- return this.accelerator;
75
- }
76
- if (this.isBunAvailable()) {
77
- try {
78
- this.accelerator = new BunCryptoHasher;
79
- this.updateStatus("bun-crypto-hasher");
80
- return this.accelerator;
81
- } catch {}
82
- }
83
- this.accelerator = new HashFallback;
84
- this.updateStatus("node-crypto");
85
- return this.accelerator;
86
- }
87
- static isBunAvailable() {
88
- try {
89
- return typeof Bun !== "undefined" && typeof Bun.CryptoHasher === "function";
90
- } catch {
91
- return false;
92
- }
93
- }
94
- static updateStatus(runtime) {
95
- this.status = {
96
- available: runtime === "bun-crypto-hasher",
97
- runtime
98
- };
99
- }
100
- static sha256(input) {
101
- return this.getAccelerator().sha256(input);
102
- }
103
- static hmacSha256(key, data) {
104
- return this.getAccelerator().hmacSha256(key, data);
105
- }
106
- static getStatus() {
107
- if (this.status === null) {
108
- this.getAccelerator();
109
- }
110
- return this.status || {
111
- available: false,
112
- runtime: "node-crypto"
113
- };
114
- }
115
- static reset() {
116
- this.accelerator = null;
117
- this.status = null;
118
- }
119
- }
120
- export {
121
- NativeHasher,
122
- NativeAccelerator,
123
- HashFallback2 as HashFallback,
124
- CborFallbackEncoder,
125
- CborFallbackDecoder,
126
- CBOR_SIMPLE_VALUES,
127
- CBOR_MAJOR_TYPES,
128
- CBOR_LENGTH_ENCODING
129
- };
130
-
131
- //# debugId=3132C96BF3309F7664756E2164756E21
1
+ export { CborFallbackDecoder, CborFallbackEncoder } from './cbor-fallback.js'
2
+ export { HashFallback } from './hash-fallback.js'
3
+ export { NativeAccelerator } from './NativeAccelerator.js'
4
+ export { NativeHasher } from './NativeHasher.js'
5
+ export { CBOR_LENGTH_ENCODING, CBOR_MAJOR_TYPES, CBOR_SIMPLE_VALUES } from './types.js'