@gravito/core 2.0.0 → 2.0.1
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/engine/index.js +62 -14
- package/dist/engine/index.js.map +7 -6
- package/dist/ffi/index.js +22 -493
- package/dist/ffi/index.js.map +3 -6
- package/dist/http/types.d.ts +7 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +6225 -6554
- package/dist/index.js.map +15 -17
- package/dist/runtime.d.ts +1 -1
- package/package.json +10 -10
- package/dist/compat.cjs +0 -49
- package/dist/compat.cjs.map +0 -9
- package/dist/engine/index.cjs +0 -1693
- package/dist/engine/index.cjs.map +0 -22
- package/dist/ffi/index.cjs +0 -621
- package/dist/ffi/index.cjs.map +0 -14
- package/dist/index.cjs +0 -14260
- package/dist/index.cjs.map +0 -168
package/dist/ffi/index.cjs
DELETED
|
@@ -1,621 +0,0 @@
|
|
|
1
|
-
// @bun @bun-cjs
|
|
2
|
-
(function(exports, require, module, __filename, __dirname) {var __create = Object.create;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
-
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
-
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
-
for (let key of __getOwnPropNames(mod))
|
|
12
|
-
if (!__hasOwnProp.call(to, key))
|
|
13
|
-
__defProp(to, key, {
|
|
14
|
-
get: () => mod[key],
|
|
15
|
-
enumerable: true
|
|
16
|
-
});
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
20
|
-
var __toCommonJS = (from) => {
|
|
21
|
-
var entry = __moduleCache.get(from), desc;
|
|
22
|
-
if (entry)
|
|
23
|
-
return entry;
|
|
24
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
25
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
26
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
27
|
-
get: () => from[key],
|
|
28
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
29
|
-
}));
|
|
30
|
-
__moduleCache.set(from, entry);
|
|
31
|
-
return entry;
|
|
32
|
-
};
|
|
33
|
-
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
34
|
-
var __export = (target, all) => {
|
|
35
|
-
for (var name in all)
|
|
36
|
-
__defProp(target, name, {
|
|
37
|
-
get: all[name],
|
|
38
|
-
enumerable: true,
|
|
39
|
-
configurable: true,
|
|
40
|
-
set: (newValue) => all[name] = () => newValue
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// src/ffi/index.ts
|
|
45
|
-
var exports_ffi = {};
|
|
46
|
-
__export(exports_ffi, {
|
|
47
|
-
NativeHasher: () => NativeHasher,
|
|
48
|
-
NativeAccelerator: () => NativeAccelerator,
|
|
49
|
-
HashFallback: () => HashFallback,
|
|
50
|
-
CborFallbackEncoder: () => CborFallbackEncoder,
|
|
51
|
-
CborFallbackDecoder: () => CborFallbackDecoder,
|
|
52
|
-
CBOR_SIMPLE_VALUES: () => CBOR_SIMPLE_VALUES,
|
|
53
|
-
CBOR_MAJOR_TYPES: () => CBOR_MAJOR_TYPES,
|
|
54
|
-
CBOR_LENGTH_ENCODING: () => CBOR_LENGTH_ENCODING
|
|
55
|
-
});
|
|
56
|
-
module.exports = __toCommonJS(exports_ffi);
|
|
57
|
-
|
|
58
|
-
// src/ffi/types.ts
|
|
59
|
-
var CBOR_MAJOR_TYPES = {
|
|
60
|
-
UINT: 0,
|
|
61
|
-
NEGINT: 1,
|
|
62
|
-
BYTES: 2,
|
|
63
|
-
TEXT: 3,
|
|
64
|
-
ARRAY: 4,
|
|
65
|
-
MAP: 5,
|
|
66
|
-
TAG: 6,
|
|
67
|
-
SIMPLE: 7
|
|
68
|
-
};
|
|
69
|
-
var CBOR_SIMPLE_VALUES = {
|
|
70
|
-
FALSE: 20,
|
|
71
|
-
TRUE: 21,
|
|
72
|
-
NULL: 22,
|
|
73
|
-
UNDEFINED: 23
|
|
74
|
-
};
|
|
75
|
-
var CBOR_LENGTH_ENCODING = {
|
|
76
|
-
SMALL_RANGE_END: 23,
|
|
77
|
-
UINT8: 24,
|
|
78
|
-
UINT16: 25,
|
|
79
|
-
UINT32: 26,
|
|
80
|
-
UINT64: 27,
|
|
81
|
-
FLOAT16: 25,
|
|
82
|
-
FLOAT32: 26,
|
|
83
|
-
FLOAT64: 27,
|
|
84
|
-
INDEFINITE: 31
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// src/ffi/cbor-fallback.ts
|
|
88
|
-
class CborFallbackEncoder {
|
|
89
|
-
static DEFAULT_BUFFER_SIZE = 4096;
|
|
90
|
-
static MAX_DEPTH = 16;
|
|
91
|
-
static MAX_BUFFER_SIZE = 1024 * 1024;
|
|
92
|
-
buffer;
|
|
93
|
-
offset;
|
|
94
|
-
constructor() {
|
|
95
|
-
this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE);
|
|
96
|
-
this.offset = 0;
|
|
97
|
-
}
|
|
98
|
-
encode(data) {
|
|
99
|
-
this.buffer = new Uint8Array(CborFallbackEncoder.DEFAULT_BUFFER_SIZE);
|
|
100
|
-
this.offset = 0;
|
|
101
|
-
this.encodeValue(data, 0);
|
|
102
|
-
return this.buffer.slice(0, this.offset);
|
|
103
|
-
}
|
|
104
|
-
decode(bytes) {
|
|
105
|
-
const decoder = new CborFallbackDecoder(bytes);
|
|
106
|
-
const value = decoder.decode();
|
|
107
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
108
|
-
throw new Error("CBOR \u6839\u503C\u5FC5\u9808\u662F\u7269\u4EF6");
|
|
109
|
-
}
|
|
110
|
-
return value;
|
|
111
|
-
}
|
|
112
|
-
ensureCapacity(required) {
|
|
113
|
-
let newCapacity = this.buffer.length;
|
|
114
|
-
while (this.offset + required > newCapacity) {
|
|
115
|
-
newCapacity *= 2;
|
|
116
|
-
if (newCapacity > CborFallbackEncoder.MAX_BUFFER_SIZE) {
|
|
117
|
-
throw new Error(`CBOR \u7DE8\u78BC\u8D85\u904E\u6700\u5927 buffer \u5927\u5C0F ${CborFallbackEncoder.MAX_BUFFER_SIZE} \u4F4D\u5143\u7D44`);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (newCapacity !== this.buffer.length) {
|
|
121
|
-
const newBuffer = new Uint8Array(newCapacity);
|
|
122
|
-
newBuffer.set(this.buffer);
|
|
123
|
-
this.buffer = newBuffer;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
writeByte(byte) {
|
|
127
|
-
this.ensureCapacity(1);
|
|
128
|
-
this.buffer[this.offset++] = byte & 255;
|
|
129
|
-
}
|
|
130
|
-
writeBytes(bytes) {
|
|
131
|
-
this.ensureCapacity(bytes.length);
|
|
132
|
-
this.buffer.set(bytes, this.offset);
|
|
133
|
-
this.offset += bytes.length;
|
|
134
|
-
}
|
|
135
|
-
writeLength(majorType, length) {
|
|
136
|
-
const type = majorType << 5;
|
|
137
|
-
if (length < 24) {
|
|
138
|
-
this.writeByte(type | length);
|
|
139
|
-
} else if (length <= 255) {
|
|
140
|
-
this.writeByte(type | CBOR_LENGTH_ENCODING.UINT8);
|
|
141
|
-
this.writeByte(length);
|
|
142
|
-
} else if (length <= 65535) {
|
|
143
|
-
this.writeByte(type | CBOR_LENGTH_ENCODING.UINT16);
|
|
144
|
-
this.writeByte(length >> 8 & 255);
|
|
145
|
-
this.writeByte(length & 255);
|
|
146
|
-
} else if (length <= 4294967295) {
|
|
147
|
-
this.writeByte(type | CBOR_LENGTH_ENCODING.UINT32);
|
|
148
|
-
this.writeByte(length >> 24 & 255);
|
|
149
|
-
this.writeByte(length >> 16 & 255);
|
|
150
|
-
this.writeByte(length >> 8 & 255);
|
|
151
|
-
this.writeByte(length & 255);
|
|
152
|
-
} else {
|
|
153
|
-
throw new Error(`CBOR \u4E0D\u652F\u63F4 > 2^32 \u7684\u9577\u5EA6: ${length}`);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
encodeValue(value, depth) {
|
|
157
|
-
if (depth > CborFallbackEncoder.MAX_DEPTH) {
|
|
158
|
-
throw new Error(`CBOR \u7DE8\u78BC\u6DF1\u5EA6\u8D85\u904E\u9650\u5236 ${CborFallbackEncoder.MAX_DEPTH}`);
|
|
159
|
-
}
|
|
160
|
-
if (value === null) {
|
|
161
|
-
this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.NULL);
|
|
162
|
-
} else if (value === true) {
|
|
163
|
-
this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.TRUE);
|
|
164
|
-
} else if (value === false) {
|
|
165
|
-
this.writeByte(CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_SIMPLE_VALUES.FALSE);
|
|
166
|
-
} else if (typeof value === "number") {
|
|
167
|
-
this.encodeNumber(value);
|
|
168
|
-
} else if (typeof value === "string") {
|
|
169
|
-
this.encodeString(value);
|
|
170
|
-
} else if (value instanceof Uint8Array) {
|
|
171
|
-
this.encodeBytes(value);
|
|
172
|
-
} else if (Array.isArray(value)) {
|
|
173
|
-
this.encodeArray(value, depth);
|
|
174
|
-
} else if (typeof value === "object") {
|
|
175
|
-
this.encodeMap(value, depth);
|
|
176
|
-
} else {
|
|
177
|
-
throw new Error(`CBOR \u4E0D\u652F\u63F4\u985E\u578B: ${typeof value}`);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
encodeNumber(num) {
|
|
181
|
-
if (Number.isInteger(num) && !Object.is(num, -0)) {
|
|
182
|
-
if (num >= 0 && num <= 4294967295) {
|
|
183
|
-
this.writeLength(CBOR_MAJOR_TYPES.UINT, num);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
if (num < 0 && -1 - num <= 4294967295) {
|
|
187
|
-
this.writeLength(CBOR_MAJOR_TYPES.NEGINT, -1 - num);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
{
|
|
192
|
-
const bytes = new Uint8Array(9);
|
|
193
|
-
bytes[0] = CBOR_MAJOR_TYPES.SIMPLE << 5 | CBOR_LENGTH_ENCODING.FLOAT64;
|
|
194
|
-
const view = new DataView(new ArrayBuffer(8));
|
|
195
|
-
view.setFloat64(0, num, false);
|
|
196
|
-
for (let i = 0;i < 8; i++) {
|
|
197
|
-
bytes[i + 1] = view.getUint8(i);
|
|
198
|
-
}
|
|
199
|
-
this.writeBytes(bytes);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
encodeString(str) {
|
|
203
|
-
const encoder = new TextEncoder;
|
|
204
|
-
const bytes = encoder.encode(str);
|
|
205
|
-
this.writeLength(CBOR_MAJOR_TYPES.TEXT, bytes.length);
|
|
206
|
-
this.writeBytes(bytes);
|
|
207
|
-
}
|
|
208
|
-
encodeBytes(bytes) {
|
|
209
|
-
this.writeLength(CBOR_MAJOR_TYPES.BYTES, bytes.length);
|
|
210
|
-
this.writeBytes(bytes);
|
|
211
|
-
}
|
|
212
|
-
encodeArray(arr, depth) {
|
|
213
|
-
this.writeLength(CBOR_MAJOR_TYPES.ARRAY, arr.length);
|
|
214
|
-
for (const item of arr) {
|
|
215
|
-
this.encodeValue(item, depth + 1);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
encodeMap(obj, depth) {
|
|
219
|
-
const keys = Object.keys(obj);
|
|
220
|
-
this.writeLength(CBOR_MAJOR_TYPES.MAP, keys.length);
|
|
221
|
-
for (const key of keys) {
|
|
222
|
-
this.encodeString(key);
|
|
223
|
-
this.encodeValue(obj[key], depth + 1);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
class CborFallbackDecoder {
|
|
229
|
-
static MAX_DEPTH = 16;
|
|
230
|
-
data;
|
|
231
|
-
offset;
|
|
232
|
-
constructor(data) {
|
|
233
|
-
this.data = data;
|
|
234
|
-
this.offset = 0;
|
|
235
|
-
}
|
|
236
|
-
decode() {
|
|
237
|
-
return this.decodeValue(0);
|
|
238
|
-
}
|
|
239
|
-
readByte() {
|
|
240
|
-
if (this.offset >= this.data.length) {
|
|
241
|
-
throw new Error("CBOR \u8CC7\u6599\u4E0D\u8DB3");
|
|
242
|
-
}
|
|
243
|
-
return this.data[this.offset++];
|
|
244
|
-
}
|
|
245
|
-
readBytes(length) {
|
|
246
|
-
if (this.offset + length > this.data.length) {
|
|
247
|
-
throw new Error("CBOR \u8CC7\u6599\u4E0D\u8DB3");
|
|
248
|
-
}
|
|
249
|
-
const result = this.data.slice(this.offset, this.offset + length);
|
|
250
|
-
this.offset += length;
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
readLength(additionalInfo) {
|
|
254
|
-
if (additionalInfo < 24) {
|
|
255
|
-
return additionalInfo;
|
|
256
|
-
}
|
|
257
|
-
if (additionalInfo === 24) {
|
|
258
|
-
return this.readByte();
|
|
259
|
-
}
|
|
260
|
-
if (additionalInfo === 25) {
|
|
261
|
-
const b1 = this.readByte();
|
|
262
|
-
const b2 = this.readByte();
|
|
263
|
-
return b1 << 8 | b2;
|
|
264
|
-
}
|
|
265
|
-
if (additionalInfo === 26) {
|
|
266
|
-
const b1 = this.readByte();
|
|
267
|
-
const b2 = this.readByte();
|
|
268
|
-
const b3 = this.readByte();
|
|
269
|
-
const b4 = this.readByte();
|
|
270
|
-
return b1 << 24 | b2 << 16 | b3 << 8 | b4;
|
|
271
|
-
}
|
|
272
|
-
if (additionalInfo === 27) {
|
|
273
|
-
const high = this.readByte() << 24 | this.readByte() << 16 | this.readByte() << 8 | this.readByte();
|
|
274
|
-
const low = this.readByte() << 24 | this.readByte() << 16 | this.readByte() << 8 | this.readByte();
|
|
275
|
-
return high * 4294967296 + low;
|
|
276
|
-
}
|
|
277
|
-
throw new Error(`CBOR \u7121\u6548\u7684\u9577\u5EA6\u7DE8\u78BC: ${additionalInfo}`);
|
|
278
|
-
}
|
|
279
|
-
decodeValue(depth) {
|
|
280
|
-
if (depth > CborFallbackDecoder.MAX_DEPTH) {
|
|
281
|
-
throw new Error(`CBOR \u89E3\u78BC\u6DF1\u5EA6\u8D85\u904E\u9650\u5236 ${CborFallbackDecoder.MAX_DEPTH}`);
|
|
282
|
-
}
|
|
283
|
-
const byte = this.readByte();
|
|
284
|
-
const majorType = byte >> 5 & 7;
|
|
285
|
-
const additionalInfo = byte & 31;
|
|
286
|
-
switch (majorType) {
|
|
287
|
-
case CBOR_MAJOR_TYPES.UINT:
|
|
288
|
-
return this.readLength(additionalInfo);
|
|
289
|
-
case CBOR_MAJOR_TYPES.NEGINT:
|
|
290
|
-
return -1 - this.readLength(additionalInfo);
|
|
291
|
-
case CBOR_MAJOR_TYPES.BYTES: {
|
|
292
|
-
const length = this.readLength(additionalInfo);
|
|
293
|
-
return this.readBytes(length);
|
|
294
|
-
}
|
|
295
|
-
case CBOR_MAJOR_TYPES.TEXT: {
|
|
296
|
-
const length = this.readLength(additionalInfo);
|
|
297
|
-
const bytes = this.readBytes(length);
|
|
298
|
-
const decoder = new TextDecoder;
|
|
299
|
-
return decoder.decode(bytes);
|
|
300
|
-
}
|
|
301
|
-
case CBOR_MAJOR_TYPES.ARRAY: {
|
|
302
|
-
const length = this.readLength(additionalInfo);
|
|
303
|
-
const result = [];
|
|
304
|
-
for (let i = 0;i < length; i++) {
|
|
305
|
-
result.push(this.decodeValue(depth + 1));
|
|
306
|
-
}
|
|
307
|
-
return result;
|
|
308
|
-
}
|
|
309
|
-
case CBOR_MAJOR_TYPES.MAP: {
|
|
310
|
-
const length = this.readLength(additionalInfo);
|
|
311
|
-
const result = {};
|
|
312
|
-
for (let i = 0;i < length; i++) {
|
|
313
|
-
const key = this.decodeValue(depth + 1);
|
|
314
|
-
if (typeof key !== "string") {
|
|
315
|
-
throw new Error(`CBOR Map \u7684 key \u5FC5\u9808\u662F\u5B57\u4E32\uFF0C\u5F97\u5230: ${typeof key}`);
|
|
316
|
-
}
|
|
317
|
-
result[key] = this.decodeValue(depth + 1);
|
|
318
|
-
}
|
|
319
|
-
return result;
|
|
320
|
-
}
|
|
321
|
-
case CBOR_MAJOR_TYPES.SIMPLE:
|
|
322
|
-
if (additionalInfo === CBOR_SIMPLE_VALUES.FALSE) {
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
if (additionalInfo === CBOR_SIMPLE_VALUES.TRUE) {
|
|
326
|
-
return true;
|
|
327
|
-
}
|
|
328
|
-
if (additionalInfo === CBOR_SIMPLE_VALUES.NULL) {
|
|
329
|
-
return null;
|
|
330
|
-
}
|
|
331
|
-
if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT32) {
|
|
332
|
-
const bytes = this.readBytes(4);
|
|
333
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset);
|
|
334
|
-
return view.getFloat32(0, false);
|
|
335
|
-
}
|
|
336
|
-
if (additionalInfo === CBOR_LENGTH_ENCODING.FLOAT64) {
|
|
337
|
-
const bytes = this.readBytes(8);
|
|
338
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset);
|
|
339
|
-
return view.getFloat64(0, false);
|
|
340
|
-
}
|
|
341
|
-
throw new Error(`CBOR \u4E0D\u652F\u63F4\u7684 simple value: ${additionalInfo}`);
|
|
342
|
-
case CBOR_MAJOR_TYPES.TAG:
|
|
343
|
-
return this.decodeValue(depth);
|
|
344
|
-
default:
|
|
345
|
-
throw new Error(`CBOR \u7121\u6548\u7684 major type: ${majorType}`);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
// src/ffi/hash-fallback.ts
|
|
350
|
-
var import_node_crypto = require("crypto");
|
|
351
|
-
|
|
352
|
-
class HashFallback {
|
|
353
|
-
sha256(input) {
|
|
354
|
-
return import_node_crypto.createHash("sha256").update(input).digest("hex");
|
|
355
|
-
}
|
|
356
|
-
hmacSha256(key, data) {
|
|
357
|
-
return import_node_crypto.createHmac("sha256", key).update(data).digest("hex");
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
// src/ffi/NativeAccelerator.ts
|
|
361
|
-
var __dirname = "/Users/carl/Dev/Carl/gravito-core/packages/core/src/ffi";
|
|
362
|
-
|
|
363
|
-
class NativeAccelerator {
|
|
364
|
-
static DEBUG_ENV = "GRAVITO_FFI_DEBUG";
|
|
365
|
-
static DISABLE_ENV = "GRAVITO_FFI_DISABLE";
|
|
366
|
-
static available = null;
|
|
367
|
-
static cborAccelerator = null;
|
|
368
|
-
static status = null;
|
|
369
|
-
static isAvailable() {
|
|
370
|
-
if (this.available !== null) {
|
|
371
|
-
return this.available;
|
|
372
|
-
}
|
|
373
|
-
if (typeof process !== "undefined" && process.env) {
|
|
374
|
-
if (process.env[this.DISABLE_ENV] === "1") {
|
|
375
|
-
this.available = false;
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
try {
|
|
380
|
-
if (typeof Bun === "undefined") {
|
|
381
|
-
this.available = false;
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
384
|
-
const bunFfi = require("bun:ffi");
|
|
385
|
-
if (typeof bunFfi.cc !== "function") {
|
|
386
|
-
this.available = false;
|
|
387
|
-
return false;
|
|
388
|
-
}
|
|
389
|
-
this.available = true;
|
|
390
|
-
return true;
|
|
391
|
-
} catch {
|
|
392
|
-
this.available = false;
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
static getCborAccelerator() {
|
|
397
|
-
if (this.cborAccelerator !== null) {
|
|
398
|
-
return this.cborAccelerator;
|
|
399
|
-
}
|
|
400
|
-
if (this.isAvailable()) {
|
|
401
|
-
try {
|
|
402
|
-
const native = this.loadNativeImplementation();
|
|
403
|
-
if (native !== null) {
|
|
404
|
-
this.cborAccelerator = native;
|
|
405
|
-
this.updateStatus("bun-ffi");
|
|
406
|
-
return native;
|
|
407
|
-
}
|
|
408
|
-
} catch (error) {
|
|
409
|
-
const debugMode = this.isDebugEnabled();
|
|
410
|
-
if (debugMode) {
|
|
411
|
-
console.warn("[GRAVITO_FFI] Native CBOR \u8F09\u5165\u5931\u6557\uFF0C\u964D\u7D1A\u5230 JavaScript \u5BE6\u73FE:", error);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
const fallback = new CborFallbackEncoder;
|
|
416
|
-
this.cborAccelerator = fallback;
|
|
417
|
-
this.updateStatus("js-fallback");
|
|
418
|
-
if (this.isDebugEnabled()) {
|
|
419
|
-
console.log("[GRAVITO_FFI] \u4F7F\u7528 JavaScript \u56DE\u9000\u5BE6\u73FE");
|
|
420
|
-
}
|
|
421
|
-
return fallback;
|
|
422
|
-
}
|
|
423
|
-
static getStatus() {
|
|
424
|
-
if (this.status === null) {
|
|
425
|
-
this.getCborAccelerator();
|
|
426
|
-
}
|
|
427
|
-
return this.status || {
|
|
428
|
-
available: false,
|
|
429
|
-
runtime: "js-fallback",
|
|
430
|
-
version: "1.0.0"
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
static reset() {
|
|
434
|
-
this.available = null;
|
|
435
|
-
this.cborAccelerator = null;
|
|
436
|
-
this.status = null;
|
|
437
|
-
}
|
|
438
|
-
static resolveCSourcePath() {
|
|
439
|
-
const path = require("path");
|
|
440
|
-
const fs = require("fs");
|
|
441
|
-
const currentDir = typeof import.meta?.dir === "string" ? "/Users/carl/Dev/Carl/gravito-core/packages/core/src/ffi" : typeof __dirname === "string" ? __dirname : process.cwd();
|
|
442
|
-
const candidates = [
|
|
443
|
-
path.resolve(currentDir, "native", "cbor.c"),
|
|
444
|
-
path.resolve(currentDir, "..", "ffi", "native", "cbor.c"),
|
|
445
|
-
path.resolve(currentDir, "..", "..", "src", "ffi", "native", "cbor.c")
|
|
446
|
-
];
|
|
447
|
-
for (const candidate of candidates) {
|
|
448
|
-
try {
|
|
449
|
-
if (fs.existsSync(candidate)) {
|
|
450
|
-
return candidate;
|
|
451
|
-
}
|
|
452
|
-
} catch {}
|
|
453
|
-
}
|
|
454
|
-
throw new Error(`C \u539F\u59CB\u78BC\u6A94\u6848\u672A\u627E\u5230\uFF0C\u5617\u8A66\u8DEF\u5F91: ${candidates.join(", ")}`);
|
|
455
|
-
}
|
|
456
|
-
static loadNativeImplementation() {
|
|
457
|
-
try {
|
|
458
|
-
if (typeof Bun === "undefined") {
|
|
459
|
-
return null;
|
|
460
|
-
}
|
|
461
|
-
const bunFfi = require("bun:ffi");
|
|
462
|
-
if (typeof bunFfi.cc !== "function") {
|
|
463
|
-
return null;
|
|
464
|
-
}
|
|
465
|
-
const cSourcePath = this.resolveCSourcePath();
|
|
466
|
-
if (this.isDebugEnabled()) {
|
|
467
|
-
console.log(`[GRAVITO_FFI] \u8F09\u5165 C \u539F\u59CB\u78BC: ${cSourcePath}`);
|
|
468
|
-
}
|
|
469
|
-
const lib = bunFfi.cc({
|
|
470
|
-
source: cSourcePath,
|
|
471
|
-
symbols: {
|
|
472
|
-
gravito_cbor_encode: {
|
|
473
|
-
args: ["ptr", "usize", "ptr", "usize"],
|
|
474
|
-
returns: "i32"
|
|
475
|
-
},
|
|
476
|
-
gravito_cbor_decode: {
|
|
477
|
-
args: ["ptr", "usize", "ptr", "usize"],
|
|
478
|
-
returns: "i32"
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
return new NativeCborAccelerator(lib.symbols);
|
|
483
|
-
} catch (error) {
|
|
484
|
-
if (this.isDebugEnabled()) {
|
|
485
|
-
console.error("[GRAVITO_FFI] \u7DE8\u8B6F\u5931\u6557:", error);
|
|
486
|
-
}
|
|
487
|
-
return null;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
static isDebugEnabled() {
|
|
491
|
-
if (typeof process !== "undefined" && process.env) {
|
|
492
|
-
return process.env[this.DEBUG_ENV] === "1";
|
|
493
|
-
}
|
|
494
|
-
return false;
|
|
495
|
-
}
|
|
496
|
-
static updateStatus(runtime) {
|
|
497
|
-
this.status = {
|
|
498
|
-
available: runtime === "bun-ffi",
|
|
499
|
-
runtime,
|
|
500
|
-
version: runtime === "bun-ffi" ? "native-1.0.0" : "js-fallback-1.0.0"
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
class NativeCborAccelerator {
|
|
506
|
-
symbols;
|
|
507
|
-
constructor(symbols) {
|
|
508
|
-
this.symbols = symbols;
|
|
509
|
-
}
|
|
510
|
-
encode(data) {
|
|
511
|
-
const json = JSON.stringify(data);
|
|
512
|
-
const jsonBytes = new TextEncoder().encode(json);
|
|
513
|
-
let outputCapacity = Math.ceil(jsonBytes.length * 1.5);
|
|
514
|
-
const maxAttempts = 3;
|
|
515
|
-
for (let attempt = 0;attempt < maxAttempts; attempt++) {
|
|
516
|
-
const outputBuffer = new Uint8Array(outputCapacity);
|
|
517
|
-
const result = this.symbols.gravito_cbor_encode(jsonBytes, jsonBytes.length, outputBuffer, outputCapacity);
|
|
518
|
-
if (typeof result !== "number") {
|
|
519
|
-
throw new Error("FFI \u56DE\u50B3\u503C\u7121\u6548");
|
|
520
|
-
}
|
|
521
|
-
if (result > 0) {
|
|
522
|
-
return outputBuffer.slice(0, result);
|
|
523
|
-
}
|
|
524
|
-
if (result === -1) {
|
|
525
|
-
outputCapacity *= 2;
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
throw new Error(`CBOR \u7DE8\u78BC\u5931\u6557 (error: ${result})`);
|
|
529
|
-
}
|
|
530
|
-
throw new Error(`CBOR \u7DE8\u78BC\u5931\u6557\uFF1A\u8D85\u904E\u6700\u5927\u91CD\u8A66\u6B21\u6578\uFF0C\u9700\u8981 > ${outputCapacity} \u4F4D\u5143\u7D44`);
|
|
531
|
-
}
|
|
532
|
-
decode(bytes) {
|
|
533
|
-
let jsonCapacity = Math.ceil(bytes.length * 3);
|
|
534
|
-
const maxAttempts = 3;
|
|
535
|
-
for (let attempt = 0;attempt < maxAttempts; attempt++) {
|
|
536
|
-
const jsonBuffer = new Uint8Array(jsonCapacity);
|
|
537
|
-
const result = this.symbols.gravito_cbor_decode(bytes, bytes.length, jsonBuffer, jsonCapacity);
|
|
538
|
-
if (typeof result !== "number") {
|
|
539
|
-
throw new Error("FFI \u56DE\u50B3\u503C\u7121\u6548");
|
|
540
|
-
}
|
|
541
|
-
if (result > 0) {
|
|
542
|
-
const jsonString = new TextDecoder().decode(jsonBuffer.slice(0, result));
|
|
543
|
-
const obj = JSON.parse(jsonString);
|
|
544
|
-
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
545
|
-
throw new Error("CBOR \u6839\u503C\u5FC5\u9808\u662F\u7269\u4EF6");
|
|
546
|
-
}
|
|
547
|
-
return obj;
|
|
548
|
-
}
|
|
549
|
-
if (result === -1) {
|
|
550
|
-
jsonCapacity *= 2;
|
|
551
|
-
continue;
|
|
552
|
-
}
|
|
553
|
-
throw new Error(`CBOR \u89E3\u78BC\u5931\u6557 (error: ${result})`);
|
|
554
|
-
}
|
|
555
|
-
throw new Error(`CBOR \u89E3\u78BC\u5931\u6557\uFF1A\u8D85\u904E\u6700\u5927\u91CD\u8A66\u6B21\u6578\uFF0C\u9700\u8981 > ${jsonCapacity} \u4F4D\u5143\u7D44`);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
// src/ffi/NativeHasher.ts
|
|
559
|
-
class BunCryptoHasher {
|
|
560
|
-
sha256(input) {
|
|
561
|
-
return new Bun.CryptoHasher("sha256").update(input).digest("hex");
|
|
562
|
-
}
|
|
563
|
-
hmacSha256(key, data) {
|
|
564
|
-
return new Bun.CryptoHasher("sha256", key).update(data).digest("hex");
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
class NativeHasher {
|
|
569
|
-
static accelerator = null;
|
|
570
|
-
static status = null;
|
|
571
|
-
static getAccelerator() {
|
|
572
|
-
if (this.accelerator !== null) {
|
|
573
|
-
return this.accelerator;
|
|
574
|
-
}
|
|
575
|
-
if (this.isBunAvailable()) {
|
|
576
|
-
try {
|
|
577
|
-
this.accelerator = new BunCryptoHasher;
|
|
578
|
-
this.updateStatus("bun-crypto-hasher");
|
|
579
|
-
return this.accelerator;
|
|
580
|
-
} catch {}
|
|
581
|
-
}
|
|
582
|
-
this.accelerator = new HashFallback;
|
|
583
|
-
this.updateStatus("node-crypto");
|
|
584
|
-
return this.accelerator;
|
|
585
|
-
}
|
|
586
|
-
static isBunAvailable() {
|
|
587
|
-
try {
|
|
588
|
-
return typeof Bun !== "undefined" && typeof Bun.CryptoHasher === "function";
|
|
589
|
-
} catch {
|
|
590
|
-
return false;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
static updateStatus(runtime) {
|
|
594
|
-
this.status = {
|
|
595
|
-
available: runtime === "bun-crypto-hasher",
|
|
596
|
-
runtime
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
static sha256(input) {
|
|
600
|
-
return this.getAccelerator().sha256(input);
|
|
601
|
-
}
|
|
602
|
-
static hmacSha256(key, data) {
|
|
603
|
-
return this.getAccelerator().hmacSha256(key, data);
|
|
604
|
-
}
|
|
605
|
-
static getStatus() {
|
|
606
|
-
if (this.status === null) {
|
|
607
|
-
this.getAccelerator();
|
|
608
|
-
}
|
|
609
|
-
return this.status || {
|
|
610
|
-
available: false,
|
|
611
|
-
runtime: "node-crypto"
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
static reset() {
|
|
615
|
-
this.accelerator = null;
|
|
616
|
-
this.status = null;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
})
|
|
620
|
-
|
|
621
|
-
//# debugId=1109E368D8BFEFF564756E2164756E21
|