@anteros/core 0.0.1-alpha.3 → 0.0.1-alpha.4
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/index.ts +1 -0
- package/lib/buffer-polyfill.ts +57 -0
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polyfill for Bun's Buffer to accept numeric encodings.
|
|
3
|
+
*
|
|
4
|
+
* Node.js allows encoding to be passed as a number (e.g. 3 = 'latin1'),
|
|
5
|
+
* but Bun throws "Invalid encoding: 3". This patches Buffer.from,
|
|
6
|
+
* Buffer.alloc, and Buffer.allocUnsafe to convert numeric encodings
|
|
7
|
+
* to their string equivalents before calling the original methods.
|
|
8
|
+
*
|
|
9
|
+
* Required by: mongodb driver (bson), and any library that uses
|
|
10
|
+
* legacy numeric encoding values internally.
|
|
11
|
+
*/
|
|
12
|
+
const ENCODING_MAP: Record<number, string> = {
|
|
13
|
+
0: "utf8",
|
|
14
|
+
1: "utf-8",
|
|
15
|
+
2: "utf16le",
|
|
16
|
+
3: "latin1",
|
|
17
|
+
4: "latin1", // binary alias
|
|
18
|
+
5: "base64",
|
|
19
|
+
6: "base64url",
|
|
20
|
+
7: "hex",
|
|
21
|
+
8: "ascii",
|
|
22
|
+
9: "ucs2",
|
|
23
|
+
10: "ucs-2",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function normalizeEncoding(
|
|
27
|
+
encoding: string | number | undefined,
|
|
28
|
+
): BufferEncoding | undefined {
|
|
29
|
+
if (encoding === undefined || typeof encoding === "string") {
|
|
30
|
+
return encoding as BufferEncoding | undefined;
|
|
31
|
+
}
|
|
32
|
+
const mapped = ENCODING_MAP[encoding];
|
|
33
|
+
if (mapped) return mapped as BufferEncoding;
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Patch Buffer.from
|
|
38
|
+
const _Buffer_from = Buffer.from.bind(Buffer) as typeof Buffer.from;
|
|
39
|
+
(Buffer as any).from = function (
|
|
40
|
+
...args: any[]
|
|
41
|
+
): Buffer {
|
|
42
|
+
if (args.length >= 2 && typeof args[1] === "number") {
|
|
43
|
+
args[1] = normalizeEncoding(args[1]);
|
|
44
|
+
}
|
|
45
|
+
return _Buffer_from(...args as [any, any?, any?]);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Patch Buffer.alloc
|
|
49
|
+
const _Buffer_alloc = Buffer.alloc.bind(Buffer);
|
|
50
|
+
(Buffer as any).alloc = function (
|
|
51
|
+
...args: any[]
|
|
52
|
+
): Buffer {
|
|
53
|
+
if (args.length >= 2 && typeof args[1] === "number") {
|
|
54
|
+
args[1] = normalizeEncoding(args[1]);
|
|
55
|
+
}
|
|
56
|
+
return _Buffer_alloc(...args as [any, any?, any?]);
|
|
57
|
+
};
|