-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-shim.js
More file actions
98 lines (88 loc) · 2.71 KB
/
browser-shim.js
File metadata and controls
98 lines (88 loc) · 2.71 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Complete Buffer polyfill for browser environments
// Provides minimal API surface needed for cryptographic operations
class BrowserBuffer extends Uint8Array {
constructor(input) {
if (typeof input === 'number') {
super(input);
} else if (input instanceof ArrayBuffer) {
super(input);
} else if (input instanceof Uint8Array) {
super(input);
} else if (Array.isArray(input)) {
super(input);
} else {
super(0);
}
}
static from(data, encoding) {
if (typeof data === 'string') {
if (encoding === 'hex') {
return BrowserBuffer.fromHex(data);
} else if (encoding === 'base64') {
return BrowserBuffer.fromBase64(data);
} else {
const bytes = new TextEncoder().encode(data);
return new BrowserBuffer(bytes);
}
} else if (data instanceof ArrayBuffer) {
return new BrowserBuffer(new Uint8Array(data));
} else if (data instanceof Uint8Array) {
return new BrowserBuffer(data);
} else if (Array.isArray(data)) {
return new BrowserBuffer(data);
}
throw new Error('Unsupported data type for Buffer.from');
}
static fromHex(hex) {
const cleaned = hex.replace(/[^0-9a-fA-F]/g, '');
if (cleaned.length % 2 !== 0) {
throw new Error('Invalid hex string');
}
const bytes = new Uint8Array(cleaned.length / 2);
for (let i = 0; i < cleaned.length; i += 2) {
bytes[i / 2] = parseInt(cleaned.substr(i, 2), 16);
}
return new BrowserBuffer(bytes);
}
static fromBase64(base64) {
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return new BrowserBuffer(bytes);
}
toString(encoding) {
if (encoding === 'hex') {
return Array.from(this)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
} else if (encoding === 'base64') {
const binaryString = String.fromCharCode(...Array.from(this));
return btoa(binaryString);
} else {
return new TextDecoder().decode(this);
}
}
slice(start, end) {
const sliced = super.slice(start, end);
return new BrowserBuffer(sliced);
}
}
// Install Buffer polyfill
if (typeof globalThis.Buffer === 'undefined') {
globalThis.Buffer = BrowserBuffer;
}
// Install process polyfill
if (typeof globalThis.process === 'undefined') {
globalThis.process = {
env: {},
version: 'browser',
platform: 'browser'
};
}
// Install crypto polyfill if needed
if (typeof globalThis.crypto === 'undefined' && typeof window !== 'undefined' && window.crypto) {
globalThis.crypto = window.crypto;
}
export { BrowserBuffer as Buffer };