-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyfills.js
60 lines (56 loc) · 1.56 KB
/
polyfills.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (C) 2024, Nuklai. All rights reserved.
// See the file LICENSE for licensing terms.
(function (global) {
const isBrowser =
typeof window !== "undefined" && typeof window.document !== "undefined";
// Ensure BigInt serialization for JSON
if (typeof BigInt !== "undefined" && !BigInt.prototype.toJSON) {
BigInt.prototype.toJSON = function () {
return this.toString();
};
}
// Buffer polyfill for browsers
if (isBrowser && typeof global.Buffer === "undefined") {
global.Buffer = {
from: function (data, encoding) {
if (typeof data === "string") {
if (encoding === "hex") {
return Uint8Array.from(
data.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
);
}
return new TextEncoder().encode(data);
}
if (data instanceof Uint8Array) {
return data;
}
throw new Error("Unsupported data type for Buffer.from");
},
alloc: function (size) {
return new Uint8Array(size);
},
isBuffer: function (obj) {
return obj instanceof Uint8Array;
},
};
}
// Process polyfill for browsers
if (isBrowser && typeof global.process === "undefined") {
global.process = {
env: {},
nextTick: function (callback) {
setTimeout(callback, 0);
},
};
}
})(
typeof globalThis !== "undefined"
? globalThis
: typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: typeof self !== "undefined"
? self
: {}
);