@ccheever/exact-ibex-runtime 0.1.0
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/package.json +63 -0
- package/src/abort/AbortController.ts +23 -0
- package/src/abort/AbortSignal.ts +152 -0
- package/src/abort/index.ts +2 -0
- package/src/accessibility.ts +12 -0
- package/src/arraybuffer-detach.ts +109 -0
- package/src/base64/base64.ts +168 -0
- package/src/base64/index.ts +1 -0
- package/src/blob/Blob.ts +259 -0
- package/src/blob/File.ts +59 -0
- package/src/blob/FormData.ts +323 -0
- package/src/blob/index.ts +3 -0
- package/src/bootstrap.ts +1946 -0
- package/src/broadcast/BroadcastChannel.ts +280 -0
- package/src/broadcast/index.ts +5 -0
- package/src/cache/Cache.ts +349 -0
- package/src/cache/CacheStorage.ts +89 -0
- package/src/cache/index.ts +27 -0
- package/src/camera/index.ts +6202 -0
- package/src/camera/processor.worker.ts +194 -0
- package/src/camera/scene.ts +195 -0
- package/src/clipboard/Clipboard.ts +129 -0
- package/src/clipboard/ClipboardItem.ts +97 -0
- package/src/clipboard/index.ts +6 -0
- package/src/clone/index.ts +1 -0
- package/src/clone/structuredClone.ts +389 -0
- package/src/clone/transferableSymbols.ts +2 -0
- package/src/compression/CompressionStream.ts +146 -0
- package/src/compression/DecompressionStream.ts +342 -0
- package/src/compression/index.ts +4 -0
- package/src/console/Console.ts +341 -0
- package/src/console/index.ts +2 -0
- package/src/core/accessibility-state.ts +263 -0
- package/src/core/accessibility.ts +184 -0
- package/src/core/agent-state.ts +37 -0
- package/src/core/diagnostics-logs.ts +144 -0
- package/src/core/host-call-bridge.ts +16 -0
- package/src/core/i18n-helpers.ts +189 -0
- package/src/core/locale-state.ts +253 -0
- package/src/core/locale.ts +95 -0
- package/src/crypto/Crypto.ts +2743 -0
- package/src/crypto/index.ts +1 -0
- package/src/diagnostics/logs.ts +7 -0
- package/src/encoding/TextDecoder.ts +1181 -0
- package/src/encoding/TextDecoderStream.ts +58 -0
- package/src/encoding/TextEncoder.ts +180 -0
- package/src/encoding/TextEncoderStream.ts +39 -0
- package/src/encoding/index.ts +8 -0
- package/src/events/CloseEvent.ts +91 -0
- package/src/events/DOMException.ts +409 -0
- package/src/events/ErrorEvent.ts +39 -0
- package/src/events/Event.ts +151 -0
- package/src/events/EventTarget.ts +280 -0
- package/src/events/FocusEvent.ts +27 -0
- package/src/events/KeyboardEvent.ts +46 -0
- package/src/events/MessageEvent.ts +61 -0
- package/src/events/ProgressEvent.ts +33 -0
- package/src/events/PromiseRejectionEvent.ts +31 -0
- package/src/events/index.ts +52 -0
- package/src/eventsource/EventSource.ts +371 -0
- package/src/eventsource/index.ts +2 -0
- package/src/fetch/Headers.ts +642 -0
- package/src/fetch/Request.ts +760 -0
- package/src/fetch/Response.ts +543 -0
- package/src/fetch/body.ts +1256 -0
- package/src/fetch/cookie-jar.ts +566 -0
- package/src/fetch/demo.ts +207 -0
- package/src/fetch/errors.ts +101 -0
- package/src/fetch/fetch.ts +2610 -0
- package/src/fetch/index.ts +101 -0
- package/src/fetch/native-bridge.ts +65 -0
- package/src/fetch/types.ts +258 -0
- package/src/filereader/FileReader.ts +236 -0
- package/src/filereader/index.ts +1 -0
- package/src/fs/Dirent.ts +39 -0
- package/src/fs/ExactFile.ts +450 -0
- package/src/fs/Stats.ts +80 -0
- package/src/fs/index.ts +944 -0
- package/src/fs/promises.ts +386 -0
- package/src/fs/shared.ts +328 -0
- package/src/http-server/index.js +697 -0
- package/src/http-server/index.ts +27 -0
- package/src/identity.generated.ts +14 -0
- package/src/index.ts +283 -0
- package/src/indexeddb/IDBCursor.ts +188 -0
- package/src/indexeddb/IDBDatabase.ts +343 -0
- package/src/indexeddb/IDBFactory.ts +269 -0
- package/src/indexeddb/IDBIndex.ts +194 -0
- package/src/indexeddb/IDBKeyRange.ts +109 -0
- package/src/indexeddb/IDBObjectStore.ts +468 -0
- package/src/indexeddb/IDBRequest.ts +163 -0
- package/src/indexeddb/IDBTransaction.ts +207 -0
- package/src/indexeddb/index.ts +34 -0
- package/src/indexeddb/utils.ts +52 -0
- package/src/inspect/index.ts +1 -0
- package/src/inspect/inspect.ts +465 -0
- package/src/internal/detect.ts +104 -0
- package/src/locale.ts +10 -0
- package/src/location/index.ts +1059 -0
- package/src/locks/LockManager.ts +460 -0
- package/src/locks/index.ts +12 -0
- package/src/media/VideoFrame.ts +58 -0
- package/src/messaging/MessageChannel.ts +31 -0
- package/src/messaging/MessagePort.ts +180 -0
- package/src/messaging/index.ts +2 -0
- package/src/messaging.ts +247 -0
- package/src/native/NativeModules.ts +354 -0
- package/src/native/index.ts +1 -0
- package/src/navigator/Navigator.ts +351 -0
- package/src/navigator/index.ts +1 -0
- package/src/node/Buffer.ts +1786 -0
- package/src/node/index.ts +4 -0
- package/src/node/path.ts +495 -0
- package/src/node/process.ts +2528 -0
- package/src/performance/Performance.ts +532 -0
- package/src/performance/index.ts +21 -0
- package/src/polyfills/array.ts +236 -0
- package/src/polyfills/arraybuffer.ts +172 -0
- package/src/polyfills/groupby.ts +85 -0
- package/src/polyfills/index.ts +85 -0
- package/src/polyfills/intl.ts +1956 -0
- package/src/polyfills/iterator.ts +479 -0
- package/src/polyfills/promise.ts +37 -0
- package/src/polyfills/set.ts +245 -0
- package/src/polyfills/string.ts +85 -0
- package/src/polyfills/typedarray.ts +110 -0
- package/src/promise-rejection-tracking.ts +464 -0
- package/src/react-native/index.ts +388 -0
- package/src/runtime-entry.ts +55 -0
- package/src/scheduling/AnimationFrame.ts +105 -0
- package/src/scheduling/IdleCallback.ts +167 -0
- package/src/scheduling/index.ts +13 -0
- package/src/security/Capabilities.ts +1146 -0
- package/src/security/Permissions.ts +392 -0
- package/src/security/capability-bits.generated.ts +63 -0
- package/src/security/index.ts +16 -0
- package/src/sqlite/Database.ts +456 -0
- package/src/sqlite/Statement.ts +206 -0
- package/src/sqlite/constants.ts +79 -0
- package/src/sqlite/errors.ts +25 -0
- package/src/sqlite/index.ts +34 -0
- package/src/sqlite/module.js +438 -0
- package/src/storage/Storage.ts +291 -0
- package/src/storage/StorageManager.ts +91 -0
- package/src/storage/index.ts +3 -0
- package/src/stream-compat.ts +47 -0
- package/src/streams/ReadableStream.ts +4131 -0
- package/src/streams/TransformStream.ts +375 -0
- package/src/streams/WritableStream.ts +866 -0
- package/src/streams/index.ts +41 -0
- package/src/timers/Timers.ts +296 -0
- package/src/timers/index.ts +11 -0
- package/src/url/URL.ts +656 -0
- package/src/url/URLPattern.ts +850 -0
- package/src/url/URLSearchParams.ts +244 -0
- package/src/url/index.ts +9 -0
- package/src/websocket/WebSocket.ts +770 -0
- package/src/websocket/WebSocketError.ts +52 -0
- package/src/websocket/WebSocketStream.ts +628 -0
- package/src/websocket/index.ts +7 -0
- package/src/window/index.ts +872 -0
|
@@ -0,0 +1,1786 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Buffer implementation for Ibex runtime (Node.js compatibility)
|
|
4
|
+
*
|
|
5
|
+
* Buffer is a Uint8Array subclass that provides additional methods
|
|
6
|
+
* for working with binary data.
|
|
7
|
+
* @see https://nodejs.org/api/buffer.html
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
type BufferEncoding = 'utf8' | 'utf-8' | 'ascii' | 'latin1' | 'binary' | 'hex' | 'base64' | 'base64url' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le';
|
|
11
|
+
type NormalizedEncoding = 'utf8' | 'ascii' | 'latin1' | 'hex' | 'base64' | 'base64url' | 'utf16le';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Buffer class for Node.js compatibility.
|
|
15
|
+
* Extends Uint8Array with additional methods.
|
|
16
|
+
*/
|
|
17
|
+
export class Buffer extends Uint8Array {
|
|
18
|
+
/**
|
|
19
|
+
* Allocates a new Buffer of size bytes.
|
|
20
|
+
* The buffer is initialized with zeros.
|
|
21
|
+
*/
|
|
22
|
+
static alloc(size: number, fill?: string | number | Uint8Array, encoding?: BufferEncoding): Buffer {
|
|
23
|
+
size = validateBufferSize(size);
|
|
24
|
+
|
|
25
|
+
const buffer = new Buffer(size);
|
|
26
|
+
|
|
27
|
+
if (fill !== undefined) {
|
|
28
|
+
buffer.fill(fill, 0, size, encoding);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return buffer;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Allocates a new Buffer of size bytes.
|
|
36
|
+
* The buffer is NOT initialized (contains garbage data).
|
|
37
|
+
*
|
|
38
|
+
* WARNING: This returns uninitialized memory for performance.
|
|
39
|
+
* Use Buffer.alloc() for security-sensitive code.
|
|
40
|
+
*/
|
|
41
|
+
static allocUnsafe(size: number): Buffer {
|
|
42
|
+
size = validateBufferSize(size);
|
|
43
|
+
// Note: In JS we can't actually get uninitialized memory,
|
|
44
|
+
// but we skip the fill step for "unsafe" behavior
|
|
45
|
+
return new Buffer(size);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Same as allocUnsafe but guaranteed to never use pooled memory.
|
|
50
|
+
*/
|
|
51
|
+
static allocUnsafeSlow(size: number): Buffer {
|
|
52
|
+
return Buffer.allocUnsafe(size);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Creates a Buffer from various inputs.
|
|
57
|
+
*/
|
|
58
|
+
static from(value: string, encoding?: BufferEncoding): Buffer;
|
|
59
|
+
static from(value: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): Buffer;
|
|
60
|
+
static from(value: ArrayLike<number> | Iterable<number>): Buffer;
|
|
61
|
+
static from(value: Buffer): Buffer;
|
|
62
|
+
static from(value: { type: 'Buffer'; data: ArrayLike<number> | Iterable<number> }): Buffer;
|
|
63
|
+
static from(
|
|
64
|
+
value: string | ArrayBuffer | SharedArrayBuffer | ArrayLike<number> | Iterable<number> | Buffer | { type: 'Buffer'; data: ArrayLike<number> | Iterable<number> },
|
|
65
|
+
encodingOrOffset?: BufferEncoding | number | unknown,
|
|
66
|
+
length?: number
|
|
67
|
+
): Buffer {
|
|
68
|
+
if (value === null || value === undefined) {
|
|
69
|
+
throw makeFirstArgumentError(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof value === 'string') {
|
|
73
|
+
return Buffer.fromString(value, encodingOrOffset as BufferEncoding);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const arrayBufferByteLength = getArrayBufferByteLength(value);
|
|
77
|
+
if (arrayBufferByteLength !== null) {
|
|
78
|
+
const byteOffset = normalizeArrayBufferOffset(encodingOrOffset, arrayBufferByteLength);
|
|
79
|
+
const viewLength = normalizeArrayBufferLength(length, arrayBufferByteLength, byteOffset);
|
|
80
|
+
// Copy the data to avoid aliasing bugs with native memory that may be
|
|
81
|
+
// freed or reused. This matches Node.js Buffer.from(ArrayBuffer) semantics.
|
|
82
|
+
const copy = new Uint8Array(viewLength);
|
|
83
|
+
copy.set(new Uint8Array(value as ArrayBuffer, byteOffset, viewLength));
|
|
84
|
+
return Object.setPrototypeOf(copy, Buffer.prototype) as Buffer;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (value instanceof Buffer) {
|
|
88
|
+
const buffer = new Buffer(value.length);
|
|
89
|
+
buffer.set(value);
|
|
90
|
+
return buffer;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (
|
|
94
|
+
typeof value === 'object' &&
|
|
95
|
+
value !== null &&
|
|
96
|
+
(value as { type?: unknown }).type === 'Buffer' &&
|
|
97
|
+
'data' in value
|
|
98
|
+
) {
|
|
99
|
+
return Buffer.from((value as { data: ArrayLike<number> | Iterable<number> }).data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (Array.isArray(value) || ArrayBuffer.isView(value)) {
|
|
103
|
+
const buffer = new Buffer(toArrayLikeLength(value));
|
|
104
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
105
|
+
buffer[i] = (value as ArrayLike<number>)[i] & 0xff;
|
|
106
|
+
}
|
|
107
|
+
return buffer;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (typeof value === 'object' && 'length' in value) {
|
|
111
|
+
const arrayLike = value as ArrayLike<number>;
|
|
112
|
+
const buffer = new Buffer(toArrayLikeLength(value));
|
|
113
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
114
|
+
buffer[i] = (arrayLike[i] ?? 0) & 0xff;
|
|
115
|
+
}
|
|
116
|
+
return buffer;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Iterable
|
|
120
|
+
if (typeof value === 'object' && Symbol.iterator in value) {
|
|
121
|
+
const arr = [...(value as Iterable<number>)];
|
|
122
|
+
return Buffer.from(arr);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
throw makeFirstArgumentError(value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Creates a Buffer from a string with specified encoding.
|
|
130
|
+
*/
|
|
131
|
+
private static fromString(str: string, encoding: BufferEncoding = 'utf8'): Buffer {
|
|
132
|
+
const normalizedEncoding = normalizeEncoding(encoding);
|
|
133
|
+
|
|
134
|
+
switch (normalizedEncoding) {
|
|
135
|
+
case 'utf8': {
|
|
136
|
+
const bytes = new TextEncoder().encode(str);
|
|
137
|
+
const buffer = new Buffer(bytes.length);
|
|
138
|
+
buffer.set(bytes);
|
|
139
|
+
return buffer;
|
|
140
|
+
}
|
|
141
|
+
case 'ascii':
|
|
142
|
+
case 'latin1': {
|
|
143
|
+
const buffer = new Buffer(str.length);
|
|
144
|
+
for (let i = 0; i < str.length; i++) {
|
|
145
|
+
buffer[i] = str.charCodeAt(i) & 0xff;
|
|
146
|
+
}
|
|
147
|
+
return buffer;
|
|
148
|
+
}
|
|
149
|
+
case 'hex': {
|
|
150
|
+
const bytes: number[] = [];
|
|
151
|
+
for (let i = 0; i < str.length - 1; i += 2) {
|
|
152
|
+
const byte = parseInt(str.slice(i, i + 2), 16);
|
|
153
|
+
if (Number.isNaN(byte)) {
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
bytes.push(byte);
|
|
157
|
+
}
|
|
158
|
+
const buffer = new Buffer(bytes.length);
|
|
159
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
160
|
+
buffer[i] = bytes[i];
|
|
161
|
+
}
|
|
162
|
+
return buffer;
|
|
163
|
+
}
|
|
164
|
+
case 'base64':
|
|
165
|
+
case 'base64url': {
|
|
166
|
+
const bytes = decodeBase64Bytes(normalizedEncoding === 'base64url'
|
|
167
|
+
? str.replace(/-/g, '+').replace(/_/g, '/')
|
|
168
|
+
: str);
|
|
169
|
+
const buffer = new Buffer(bytes.length);
|
|
170
|
+
buffer.set(bytes);
|
|
171
|
+
return buffer;
|
|
172
|
+
}
|
|
173
|
+
case 'utf16le': {
|
|
174
|
+
const buffer = new Buffer(str.length * 2);
|
|
175
|
+
for (let i = 0; i < str.length; i++) {
|
|
176
|
+
const code = str.charCodeAt(i);
|
|
177
|
+
buffer[i * 2] = code & 0xff;
|
|
178
|
+
buffer[i * 2 + 1] = (code >> 8) & 0xff;
|
|
179
|
+
}
|
|
180
|
+
return buffer;
|
|
181
|
+
}
|
|
182
|
+
default:
|
|
183
|
+
throw new TypeError(`Unknown encoding: ${encoding}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns the byte length of a string when encoded.
|
|
189
|
+
*/
|
|
190
|
+
static byteLength(
|
|
191
|
+
string: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView,
|
|
192
|
+
encoding?: BufferEncoding | string,
|
|
193
|
+
): number {
|
|
194
|
+
if (typeof string !== 'string') {
|
|
195
|
+
if (ArrayBuffer.isView(string)) {
|
|
196
|
+
return string.byteLength;
|
|
197
|
+
}
|
|
198
|
+
if (isArrayBufferLike(string)) {
|
|
199
|
+
return string.byteLength;
|
|
200
|
+
}
|
|
201
|
+
throw makeInvalidArgTypeError(
|
|
202
|
+
'string',
|
|
203
|
+
'of type string or an instance of Buffer or ArrayBuffer',
|
|
204
|
+
string,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const normalizedEncoding = normalizeEncodingOrUtf8(encoding);
|
|
209
|
+
|
|
210
|
+
switch (normalizedEncoding) {
|
|
211
|
+
case 'ascii':
|
|
212
|
+
case 'latin1':
|
|
213
|
+
return string.length;
|
|
214
|
+
case 'hex':
|
|
215
|
+
return Math.floor(string.length / 2);
|
|
216
|
+
case 'base64':
|
|
217
|
+
case 'base64url':
|
|
218
|
+
return Buffer.from(string, normalizedEncoding).length;
|
|
219
|
+
case 'utf16le':
|
|
220
|
+
return string.length * 2;
|
|
221
|
+
case 'utf8':
|
|
222
|
+
default:
|
|
223
|
+
return utf8ByteLength(string);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Concatenate multiple Buffers into one.
|
|
229
|
+
*/
|
|
230
|
+
static concat(list: (Buffer | Uint8Array)[], totalLength?: number): Buffer {
|
|
231
|
+
if (!Array.isArray(list)) {
|
|
232
|
+
throw makeInvalidArgTypeError('list', 'an instance of Array', list);
|
|
233
|
+
}
|
|
234
|
+
if (list.length === 0) {
|
|
235
|
+
return new Buffer(0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let len: number;
|
|
239
|
+
if (totalLength === undefined) {
|
|
240
|
+
len = 0;
|
|
241
|
+
for (let i = 0; i < list.length; i++) {
|
|
242
|
+
const item = list[i];
|
|
243
|
+
if (!isUint8ArrayLike(item)) {
|
|
244
|
+
throw makeInvalidArgTypeError(`list[${i}]`, 'an instance of Buffer or Uint8Array', item);
|
|
245
|
+
}
|
|
246
|
+
len += item.length;
|
|
247
|
+
}
|
|
248
|
+
} else {
|
|
249
|
+
if (typeof totalLength !== 'number') {
|
|
250
|
+
throw makeInvalidArgTypeError('length', 'of type number', totalLength);
|
|
251
|
+
}
|
|
252
|
+
if (!Number.isInteger(totalLength)) {
|
|
253
|
+
throw createOutOfRangeError('length', 'an integer', totalLength);
|
|
254
|
+
}
|
|
255
|
+
if (totalLength < 0 || totalLength > Number.MAX_SAFE_INTEGER) {
|
|
256
|
+
throw createOutOfRangeError('length', '>= 0 && <= 9007199254740991', totalLength);
|
|
257
|
+
}
|
|
258
|
+
len = totalLength;
|
|
259
|
+
for (let i = 0; i < list.length; i++) {
|
|
260
|
+
const item = list[i];
|
|
261
|
+
if (!isUint8ArrayLike(item)) {
|
|
262
|
+
throw makeInvalidArgTypeError(`list[${i}]`, 'an instance of Buffer or Uint8Array', item);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const result = new Buffer(len);
|
|
267
|
+
|
|
268
|
+
let offset = 0;
|
|
269
|
+
for (const buf of list) {
|
|
270
|
+
if (offset >= len) break;
|
|
271
|
+
const copyLen = Math.min(buf.length, len - offset);
|
|
272
|
+
result.set(buf.subarray(0, copyLen), offset);
|
|
273
|
+
offset += copyLen;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Compares two Buffers.
|
|
281
|
+
*/
|
|
282
|
+
static compare(buf1: Buffer | Uint8Array, buf2: Buffer | Uint8Array): -1 | 0 | 1 {
|
|
283
|
+
if (!isUint8ArrayLike(buf1)) {
|
|
284
|
+
throw makeInvalidArgTypeError('buf1', 'an instance of Buffer or Uint8Array', buf1, true);
|
|
285
|
+
}
|
|
286
|
+
if (!isUint8ArrayLike(buf2)) {
|
|
287
|
+
throw makeInvalidArgTypeError('buf2', 'an instance of Buffer or Uint8Array', buf2, true);
|
|
288
|
+
}
|
|
289
|
+
return compareByteRanges(buf1, buf2, 0, buf1.length, 0, buf2.length);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Check if value is a Buffer.
|
|
294
|
+
*/
|
|
295
|
+
static isBuffer(obj: unknown): obj is Buffer {
|
|
296
|
+
return !!(
|
|
297
|
+
obj &&
|
|
298
|
+
typeof obj === 'object' &&
|
|
299
|
+
ArrayBuffer.isView(obj) &&
|
|
300
|
+
(((obj as any)[Symbol.toStringTag] === 'Buffer') || obj instanceof Buffer)
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Check if encoding is valid.
|
|
306
|
+
*/
|
|
307
|
+
static isEncoding(encoding: unknown): encoding is BufferEncoding {
|
|
308
|
+
if (typeof encoding !== 'string' || encoding === '') {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
normalizeEncoding(encoding as BufferEncoding);
|
|
313
|
+
return true;
|
|
314
|
+
} catch {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Pool size for allocUnsafe (not used in this implementation).
|
|
321
|
+
*/
|
|
322
|
+
static poolSize = 8192;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Buffer constants.
|
|
326
|
+
*/
|
|
327
|
+
static constants = Object.freeze({
|
|
328
|
+
MAX_LENGTH: 2 ** 31 - 1,
|
|
329
|
+
MAX_STRING_LENGTH: 2 ** 28 - 16,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Instance methods
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Compare this buffer with another.
|
|
336
|
+
*/
|
|
337
|
+
compare(target: Buffer | Uint8Array, targetStart = 0, targetEnd?: number, sourceStart = 0, sourceEnd?: number): -1 | 0 | 1 {
|
|
338
|
+
if (!isUint8ArrayLike(target)) {
|
|
339
|
+
throw makeInvalidArgTypeError('target', 'an instance of Buffer or Uint8Array', target, true);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const actualTargetStart = normalizeCompareIndex(targetStart, 'targetStart', 0);
|
|
343
|
+
const actualTargetEnd = normalizeCompareIndex(targetEnd, 'targetEnd', target.length);
|
|
344
|
+
const actualSourceStart = normalizeCompareIndex(sourceStart, 'sourceStart', 0);
|
|
345
|
+
const actualSourceEnd = normalizeCompareIndex(sourceEnd, 'sourceEnd', this.length);
|
|
346
|
+
|
|
347
|
+
if (actualTargetStart < 0) {
|
|
348
|
+
throw createOutOfRangeError('targetStart', '>= 0', actualTargetStart);
|
|
349
|
+
}
|
|
350
|
+
if (actualTargetEnd < 0) {
|
|
351
|
+
throw createOutOfRangeError('targetEnd', '>= 0', actualTargetEnd);
|
|
352
|
+
}
|
|
353
|
+
if (actualSourceStart < 0) {
|
|
354
|
+
throw createOutOfRangeError('sourceStart', '>= 0', actualSourceStart);
|
|
355
|
+
}
|
|
356
|
+
if (actualSourceEnd < 0) {
|
|
357
|
+
throw createOutOfRangeError('sourceEnd', '>= 0', actualSourceEnd);
|
|
358
|
+
}
|
|
359
|
+
if (actualTargetEnd > target.length) {
|
|
360
|
+
throw createOutOfRangeError('targetEnd', `>= 0 && <= ${target.length}`, actualTargetEnd);
|
|
361
|
+
}
|
|
362
|
+
if (actualSourceEnd > this.length) {
|
|
363
|
+
throw createOutOfRangeError('sourceEnd', `>= 0 && <= ${this.length}`, actualSourceEnd);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (actualSourceStart >= actualSourceEnd) {
|
|
367
|
+
return actualTargetStart >= actualTargetEnd ? 0 : -1;
|
|
368
|
+
}
|
|
369
|
+
if (actualTargetStart >= actualTargetEnd) {
|
|
370
|
+
return 1;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return compareByteRanges(
|
|
374
|
+
this,
|
|
375
|
+
target,
|
|
376
|
+
actualSourceStart,
|
|
377
|
+
actualSourceEnd,
|
|
378
|
+
actualTargetStart,
|
|
379
|
+
actualTargetEnd,
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Copy data from this buffer to target.
|
|
385
|
+
*/
|
|
386
|
+
copy(target: Buffer | Uint8Array, targetStart = 0, sourceStart = 0, sourceEnd?: number): number {
|
|
387
|
+
const sourceBytes = getWritableByteView(this);
|
|
388
|
+
if (!sourceBytes) {
|
|
389
|
+
throw makeInvalidArgTypeError('this', 'an instance of Buffer or Uint8Array', this);
|
|
390
|
+
}
|
|
391
|
+
const targetBytes = getWritableByteView(target);
|
|
392
|
+
if (!targetBytes) {
|
|
393
|
+
throw makeInvalidArgTypeError('target', 'an instance of Buffer or Uint8Array', target);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const actualTargetStart = normalizeCopyIndex(targetStart, 'targetStart', 0);
|
|
397
|
+
const actualSourceStart = normalizeCopyIndex(sourceStart, 'sourceStart', 0);
|
|
398
|
+
let actualSourceEnd = sourceEnd == null
|
|
399
|
+
? sourceBytes.length
|
|
400
|
+
: normalizeCopyIndex(sourceEnd, 'sourceEnd', sourceBytes.length);
|
|
401
|
+
|
|
402
|
+
if (actualTargetStart < 0) {
|
|
403
|
+
throw createOutOfRangeError('targetStart', '>= 0', actualTargetStart);
|
|
404
|
+
}
|
|
405
|
+
if (actualSourceStart < 0) {
|
|
406
|
+
throw createOutOfRangeError('sourceStart', '>= 0', actualSourceStart);
|
|
407
|
+
}
|
|
408
|
+
if (actualSourceEnd < 0) {
|
|
409
|
+
throw createOutOfRangeError('sourceEnd', '>= 0', actualSourceEnd);
|
|
410
|
+
}
|
|
411
|
+
if (actualSourceStart > sourceBytes.length) {
|
|
412
|
+
throw createOutOfRangeError('sourceStart', `>= 0 && <= ${sourceBytes.length}`, actualSourceStart);
|
|
413
|
+
}
|
|
414
|
+
if (actualSourceEnd > sourceBytes.length) {
|
|
415
|
+
actualSourceEnd = sourceBytes.length;
|
|
416
|
+
}
|
|
417
|
+
if (actualSourceEnd <= actualSourceStart || actualTargetStart >= targetBytes.length) {
|
|
418
|
+
return 0;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const length = Math.min(actualSourceEnd - actualSourceStart, targetBytes.length - actualTargetStart);
|
|
422
|
+
if (
|
|
423
|
+
sourceBytes.buffer === targetBytes.buffer &&
|
|
424
|
+
actualSourceStart < actualTargetStart &&
|
|
425
|
+
actualTargetStart < actualSourceStart + length
|
|
426
|
+
) {
|
|
427
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
428
|
+
targetBytes[actualTargetStart + i] = sourceBytes[actualSourceStart + i];
|
|
429
|
+
}
|
|
430
|
+
return length;
|
|
431
|
+
}
|
|
432
|
+
for (let i = 0; i < length; i++) {
|
|
433
|
+
targetBytes[actualTargetStart + i] = sourceBytes[actualSourceStart + i];
|
|
434
|
+
}
|
|
435
|
+
return length;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Check if this buffer equals another.
|
|
440
|
+
*/
|
|
441
|
+
equals(other: Buffer | Uint8Array): boolean {
|
|
442
|
+
if (!isUint8ArrayLike(other)) {
|
|
443
|
+
throw makeInvalidArgTypeError('otherBuffer', 'an instance of Buffer or Uint8Array', other, true);
|
|
444
|
+
}
|
|
445
|
+
return compareByteRanges(this, other, 0, this.length, 0, other.length) === 0;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Fill buffer with a value.
|
|
450
|
+
*/
|
|
451
|
+
fill(value: string | number | Uint8Array, offset = 0, end?: number, encoding?: BufferEncoding): this {
|
|
452
|
+
// Handle overload: fill(value, encoding)
|
|
453
|
+
if (typeof offset === 'string') {
|
|
454
|
+
encoding = offset as unknown as BufferEncoding;
|
|
455
|
+
offset = 0;
|
|
456
|
+
}
|
|
457
|
+
// Handle overload: fill(value, offset, encoding)
|
|
458
|
+
if (typeof end === 'string') {
|
|
459
|
+
encoding = end as unknown as BufferEncoding;
|
|
460
|
+
end = undefined;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const fillEnd = end ?? this.length;
|
|
464
|
+
|
|
465
|
+
// Clamp offset and end to valid range
|
|
466
|
+
const clampedOffset = Math.max(0, Math.min(offset, this.length));
|
|
467
|
+
const clampedEnd = Math.max(0, Math.min(fillEnd, this.length));
|
|
468
|
+
|
|
469
|
+
if (clampedOffset >= clampedEnd) {
|
|
470
|
+
return this;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (typeof value === 'number') {
|
|
474
|
+
for (let i = clampedOffset; i < clampedEnd; i++) {
|
|
475
|
+
this[i] = value & 0xff;
|
|
476
|
+
}
|
|
477
|
+
} else if (typeof value === 'string') {
|
|
478
|
+
if (value.length === 0) {
|
|
479
|
+
// Node.js fills with zeros for empty string
|
|
480
|
+
for (let i = clampedOffset; i < clampedEnd; i++) {
|
|
481
|
+
this[i] = 0;
|
|
482
|
+
}
|
|
483
|
+
} else {
|
|
484
|
+
const fillBuffer = Buffer.from(value, encoding);
|
|
485
|
+
if (fillBuffer.length === 0) {
|
|
486
|
+
// Encoded to zero bytes - fill with zeros
|
|
487
|
+
for (let i = clampedOffset; i < clampedEnd; i++) {
|
|
488
|
+
this[i] = 0;
|
|
489
|
+
}
|
|
490
|
+
} else {
|
|
491
|
+
let j = 0;
|
|
492
|
+
for (let i = clampedOffset; i < clampedEnd; i++) {
|
|
493
|
+
this[i] = fillBuffer[j % fillBuffer.length];
|
|
494
|
+
j++;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
if (value.length === 0) {
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
let j = 0;
|
|
503
|
+
for (let i = clampedOffset; i < clampedEnd; i++) {
|
|
504
|
+
this[i] = value[j % value.length];
|
|
505
|
+
j++;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return this;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Check if buffer includes a value.
|
|
514
|
+
*/
|
|
515
|
+
includes(value: string | number | Buffer | Uint8Array, byteOffset?: number | string, encoding?: BufferEncoding): boolean {
|
|
516
|
+
if (typeof byteOffset === 'string') {
|
|
517
|
+
return this.indexOf(value, 0, byteOffset as BufferEncoding) !== -1;
|
|
518
|
+
}
|
|
519
|
+
return this.indexOf(value, byteOffset, encoding) !== -1;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Find index of value in buffer.
|
|
524
|
+
*/
|
|
525
|
+
indexOf(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number {
|
|
526
|
+
// Handle overload: indexOf(value, encoding)
|
|
527
|
+
if (typeof byteOffset === 'string') {
|
|
528
|
+
encoding = byteOffset as unknown as BufferEncoding;
|
|
529
|
+
byteOffset = 0;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Normalize byteOffset: negative values count from end
|
|
533
|
+
let offset = byteOffset ?? 0;
|
|
534
|
+
if (offset < 0) {
|
|
535
|
+
offset = Math.max(0, this.length + offset);
|
|
536
|
+
}
|
|
537
|
+
if (offset >= this.length) {
|
|
538
|
+
if (typeof value === 'number') return -1;
|
|
539
|
+
const searchBuffer = typeof value === 'string' ? Buffer.from(value, encoding) : value;
|
|
540
|
+
return searchBuffer.length === 0 ? this.length : -1;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (typeof value === 'number') {
|
|
544
|
+
for (let i = offset; i < this.length; i++) {
|
|
545
|
+
if (this[i] === (value & 0xff)) return i;
|
|
546
|
+
}
|
|
547
|
+
return -1;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const searchBuffer = typeof value === 'string'
|
|
551
|
+
? Buffer.from(value, encoding)
|
|
552
|
+
: value;
|
|
553
|
+
|
|
554
|
+
if (searchBuffer.length === 0) return Math.min(offset, this.length);
|
|
555
|
+
if (searchBuffer.length > this.length - offset) return -1;
|
|
556
|
+
|
|
557
|
+
outer: for (let i = offset; i <= this.length - searchBuffer.length; i++) {
|
|
558
|
+
for (let j = 0; j < searchBuffer.length; j++) {
|
|
559
|
+
if (this[i + j] !== searchBuffer[j]) continue outer;
|
|
560
|
+
}
|
|
561
|
+
return i;
|
|
562
|
+
}
|
|
563
|
+
return -1;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Find last index of value in buffer.
|
|
568
|
+
*/
|
|
569
|
+
lastIndexOf(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number {
|
|
570
|
+
// Handle overload: lastIndexOf(value, encoding)
|
|
571
|
+
if (typeof byteOffset === 'string') {
|
|
572
|
+
encoding = byteOffset as unknown as BufferEncoding;
|
|
573
|
+
byteOffset = undefined;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
let startOffset = byteOffset ?? this.length;
|
|
577
|
+
|
|
578
|
+
// Negative byteOffset counts from end
|
|
579
|
+
if (startOffset < 0) {
|
|
580
|
+
startOffset = this.length + startOffset;
|
|
581
|
+
if (startOffset < 0) {
|
|
582
|
+
// If still negative after adjustment, nothing can be found
|
|
583
|
+
if (typeof value === 'number') return -1;
|
|
584
|
+
const searchBuffer = typeof value === 'string' ? Buffer.from(value, encoding) : value;
|
|
585
|
+
return searchBuffer.length === 0 ? 0 : -1;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (typeof value === 'number') {
|
|
590
|
+
for (let i = Math.min(startOffset, this.length - 1); i >= 0; i--) {
|
|
591
|
+
if (this[i] === (value & 0xff)) return i;
|
|
592
|
+
}
|
|
593
|
+
return -1;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const searchBuffer = typeof value === 'string'
|
|
597
|
+
? Buffer.from(value, encoding)
|
|
598
|
+
: value;
|
|
599
|
+
|
|
600
|
+
if (searchBuffer.length === 0) return Math.min(startOffset, this.length);
|
|
601
|
+
|
|
602
|
+
outer: for (let i = Math.min(startOffset, this.length - searchBuffer.length); i >= 0; i--) {
|
|
603
|
+
for (let j = 0; j < searchBuffer.length; j++) {
|
|
604
|
+
if (this[i + j] !== searchBuffer[j]) continue outer;
|
|
605
|
+
}
|
|
606
|
+
return i;
|
|
607
|
+
}
|
|
608
|
+
return -1;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Create a new Buffer that shares memory with this one.
|
|
613
|
+
*/
|
|
614
|
+
slice(start?: number, end?: number): Buffer {
|
|
615
|
+
return this.subarray(start, end);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Create a subarray (shares memory).
|
|
620
|
+
*/
|
|
621
|
+
subarray(start?: number, end?: number): Buffer {
|
|
622
|
+
const sub = super.subarray(start, end);
|
|
623
|
+
return Object.setPrototypeOf(sub, Buffer.prototype);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Swap byte order (16-bit).
|
|
628
|
+
*/
|
|
629
|
+
swap16(): this {
|
|
630
|
+
if (this.length % 2 !== 0) {
|
|
631
|
+
throw new RangeError('Buffer size must be a multiple of 16-bits');
|
|
632
|
+
}
|
|
633
|
+
for (let i = 0; i < this.length; i += 2) {
|
|
634
|
+
const tmp = this[i];
|
|
635
|
+
this[i] = this[i + 1];
|
|
636
|
+
this[i + 1] = tmp;
|
|
637
|
+
}
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Swap byte order (32-bit).
|
|
643
|
+
*/
|
|
644
|
+
swap32(): this {
|
|
645
|
+
if (this.length % 4 !== 0) {
|
|
646
|
+
throw new RangeError('Buffer size must be a multiple of 32-bits');
|
|
647
|
+
}
|
|
648
|
+
for (let i = 0; i < this.length; i += 4) {
|
|
649
|
+
const tmp0 = this[i];
|
|
650
|
+
const tmp1 = this[i + 1];
|
|
651
|
+
this[i] = this[i + 3];
|
|
652
|
+
this[i + 1] = this[i + 2];
|
|
653
|
+
this[i + 2] = tmp1;
|
|
654
|
+
this[i + 3] = tmp0;
|
|
655
|
+
}
|
|
656
|
+
return this;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Swap byte order (64-bit).
|
|
661
|
+
*/
|
|
662
|
+
swap64(): this {
|
|
663
|
+
if (this.length % 8 !== 0) {
|
|
664
|
+
throw new RangeError('Buffer size must be a multiple of 64-bits');
|
|
665
|
+
}
|
|
666
|
+
for (let i = 0; i < this.length; i += 8) {
|
|
667
|
+
const tmp0 = this[i];
|
|
668
|
+
const tmp1 = this[i + 1];
|
|
669
|
+
const tmp2 = this[i + 2];
|
|
670
|
+
const tmp3 = this[i + 3];
|
|
671
|
+
this[i] = this[i + 7];
|
|
672
|
+
this[i + 1] = this[i + 6];
|
|
673
|
+
this[i + 2] = this[i + 5];
|
|
674
|
+
this[i + 3] = this[i + 4];
|
|
675
|
+
this[i + 4] = tmp3;
|
|
676
|
+
this[i + 5] = tmp2;
|
|
677
|
+
this[i + 6] = tmp1;
|
|
678
|
+
this[i + 7] = tmp0;
|
|
679
|
+
}
|
|
680
|
+
return this;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Convert buffer to JSON.
|
|
685
|
+
*/
|
|
686
|
+
toJSON(): { type: 'Buffer'; data: number[] } {
|
|
687
|
+
return {
|
|
688
|
+
type: 'Buffer',
|
|
689
|
+
data: [...this],
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Convert buffer to string.
|
|
695
|
+
*/
|
|
696
|
+
toString(encoding: BufferEncoding = 'utf8', start = 0, end?: number): string {
|
|
697
|
+
const normalizedEncoding = normalizeEncoding(encoding);
|
|
698
|
+
const actualStart = normalizeToStringRangeIndex(start, this.length, 0);
|
|
699
|
+
const actualEnd = end === undefined
|
|
700
|
+
? this.length
|
|
701
|
+
: normalizeToStringRangeIndex(end, this.length, 0);
|
|
702
|
+
|
|
703
|
+
if (actualEnd <= actualStart) {
|
|
704
|
+
return '';
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const slice = this.subarray(actualStart, actualEnd);
|
|
708
|
+
|
|
709
|
+
switch (normalizedEncoding) {
|
|
710
|
+
case 'utf8': {
|
|
711
|
+
const decoded = new TextDecoder('utf-8').decode(slice);
|
|
712
|
+
if (
|
|
713
|
+
slice.length >= 3 &&
|
|
714
|
+
slice[0] === 0xef &&
|
|
715
|
+
slice[1] === 0xbb &&
|
|
716
|
+
slice[2] === 0xbf &&
|
|
717
|
+
decoded.charCodeAt(0) !== 0xfeff
|
|
718
|
+
) {
|
|
719
|
+
return `\uFEFF${decoded}`;
|
|
720
|
+
}
|
|
721
|
+
return decoded;
|
|
722
|
+
}
|
|
723
|
+
case 'ascii': {
|
|
724
|
+
let result = '';
|
|
725
|
+
for (let i = 0; i < slice.length; i++) {
|
|
726
|
+
result += String.fromCharCode(slice[i] & 0x7f);
|
|
727
|
+
}
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
case 'latin1': {
|
|
731
|
+
let result = '';
|
|
732
|
+
for (let i = 0; i < slice.length; i++) {
|
|
733
|
+
result += String.fromCharCode(slice[i]);
|
|
734
|
+
}
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
case 'hex': {
|
|
738
|
+
let result = '';
|
|
739
|
+
for (let i = 0; i < slice.length; i++) {
|
|
740
|
+
result += slice[i].toString(16).padStart(2, '0');
|
|
741
|
+
}
|
|
742
|
+
return result;
|
|
743
|
+
}
|
|
744
|
+
case 'base64': {
|
|
745
|
+
let binary = '';
|
|
746
|
+
for (let i = 0; i < slice.length; i++) {
|
|
747
|
+
binary += String.fromCharCode(slice[i]);
|
|
748
|
+
}
|
|
749
|
+
return btoa(binary);
|
|
750
|
+
}
|
|
751
|
+
case 'base64url': {
|
|
752
|
+
let binary = '';
|
|
753
|
+
for (let i = 0; i < slice.length; i++) {
|
|
754
|
+
binary += String.fromCharCode(slice[i]);
|
|
755
|
+
}
|
|
756
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
757
|
+
}
|
|
758
|
+
case 'utf16le': {
|
|
759
|
+
let result = '';
|
|
760
|
+
for (let i = 0; i < slice.length - 1; i += 2) {
|
|
761
|
+
result += String.fromCharCode(slice[i] | (slice[i + 1] << 8));
|
|
762
|
+
}
|
|
763
|
+
return result;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Write string to buffer.
|
|
770
|
+
*/
|
|
771
|
+
write(string: string, offset = 0, length?: number, encoding?: BufferEncoding): number {
|
|
772
|
+
let writeEncoding = encoding;
|
|
773
|
+
let writeLength = length;
|
|
774
|
+
|
|
775
|
+
if (typeof length === 'string') {
|
|
776
|
+
writeEncoding = length;
|
|
777
|
+
writeLength = undefined;
|
|
778
|
+
} else if (typeof offset === 'string') {
|
|
779
|
+
writeEncoding = offset;
|
|
780
|
+
offset = 0;
|
|
781
|
+
writeLength = undefined;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const data = Buffer.from(string, writeEncoding);
|
|
785
|
+
const bytesToWrite = Math.min(
|
|
786
|
+
writeLength ?? data.length,
|
|
787
|
+
this.length - offset,
|
|
788
|
+
data.length
|
|
789
|
+
);
|
|
790
|
+
|
|
791
|
+
if (bytesToWrite <= 0) {
|
|
792
|
+
return 0;
|
|
793
|
+
}
|
|
794
|
+
this.set(data.subarray(0, bytesToWrite), offset);
|
|
795
|
+
return bytesToWrite;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// Read/write integer methods (big-endian and little-endian)
|
|
799
|
+
|
|
800
|
+
readBigInt64BE(offset = 0): bigint {
|
|
801
|
+
offset = validateOffset(offset, 8, this.length);
|
|
802
|
+
return new DataView(this.buffer, this.byteOffset + offset).getBigInt64(0, false);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
readBigInt64LE(offset = 0): bigint {
|
|
806
|
+
offset = validateOffset(offset, 8, this.length);
|
|
807
|
+
return new DataView(this.buffer, this.byteOffset + offset).getBigInt64(0, true);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
readBigUInt64BE(offset = 0): bigint {
|
|
811
|
+
offset = validateOffset(offset, 8, this.length);
|
|
812
|
+
return new DataView(this.buffer, this.byteOffset + offset).getBigUint64(0, false);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
readBigUInt64LE(offset = 0): bigint {
|
|
816
|
+
offset = validateOffset(offset, 8, this.length);
|
|
817
|
+
return new DataView(this.buffer, this.byteOffset + offset).getBigUint64(0, true);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
readDoubleBE(offset = 0): number {
|
|
821
|
+
offset = validateOffset(offset, 8, this.length);
|
|
822
|
+
return new DataView(this.buffer, this.byteOffset + offset).getFloat64(0, false);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
readDoubleLE(offset = 0): number {
|
|
826
|
+
offset = validateOffset(offset, 8, this.length);
|
|
827
|
+
return new DataView(this.buffer, this.byteOffset + offset).getFloat64(0, true);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
readFloatBE(offset = 0): number {
|
|
831
|
+
offset = validateOffset(offset, 4, this.length);
|
|
832
|
+
return new DataView(this.buffer, this.byteOffset + offset).getFloat32(0, false);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
readFloatLE(offset = 0): number {
|
|
836
|
+
offset = validateOffset(offset, 4, this.length);
|
|
837
|
+
return new DataView(this.buffer, this.byteOffset + offset).getFloat32(0, true);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
readInt8(offset = 0): number {
|
|
841
|
+
offset = validateOffset(offset, 1, this.length);
|
|
842
|
+
return new DataView(this.buffer, this.byteOffset + offset).getInt8(0);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
readInt16BE(offset = 0): number {
|
|
846
|
+
offset = validateOffset(offset, 2, this.length);
|
|
847
|
+
return new DataView(this.buffer, this.byteOffset + offset).getInt16(0, false);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
readInt16LE(offset = 0): number {
|
|
851
|
+
offset = validateOffset(offset, 2, this.length);
|
|
852
|
+
return new DataView(this.buffer, this.byteOffset + offset).getInt16(0, true);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
readInt32BE(offset = 0): number {
|
|
856
|
+
offset = validateOffset(offset, 4, this.length);
|
|
857
|
+
return new DataView(this.buffer, this.byteOffset + offset).getInt32(0, false);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
readInt32LE(offset = 0): number {
|
|
861
|
+
offset = validateOffset(offset, 4, this.length);
|
|
862
|
+
return new DataView(this.buffer, this.byteOffset + offset).getInt32(0, true);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
readUInt8(offset = 0): number {
|
|
866
|
+
offset = validateOffset(offset, 1, this.length);
|
|
867
|
+
return this[offset];
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
readUInt16BE(offset = 0): number {
|
|
871
|
+
offset = validateOffset(offset, 2, this.length);
|
|
872
|
+
return new DataView(this.buffer, this.byteOffset + offset).getUint16(0, false);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
readUInt16LE(offset = 0): number {
|
|
876
|
+
offset = validateOffset(offset, 2, this.length);
|
|
877
|
+
return new DataView(this.buffer, this.byteOffset + offset).getUint16(0, true);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
readUInt32BE(offset = 0): number {
|
|
881
|
+
offset = validateOffset(offset, 4, this.length);
|
|
882
|
+
return new DataView(this.buffer, this.byteOffset + offset).getUint32(0, false);
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
readUInt32LE(offset = 0): number {
|
|
886
|
+
offset = validateOffset(offset, 4, this.length);
|
|
887
|
+
return new DataView(this.buffer, this.byteOffset + offset).getUint32(0, true);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
readUIntBE(offset?: number, byteLength?: number): number {
|
|
891
|
+
return readVariableUint(this, offset, byteLength, false);
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
readUIntLE(offset?: number, byteLength?: number): number {
|
|
895
|
+
return readVariableUint(this, offset, byteLength, true);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
readIntBE(offset?: number, byteLength?: number): number {
|
|
899
|
+
return readVariableInt(this, offset, byteLength, false);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
readIntLE(offset?: number, byteLength?: number): number {
|
|
903
|
+
return readVariableInt(this, offset, byteLength, true);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
writeBigInt64BE(value: bigint, offset = 0): number {
|
|
907
|
+
validateBigIntWrite(value, true);
|
|
908
|
+
validateFixedWidthOffset(offset, 8, this.length);
|
|
909
|
+
new DataView(this.buffer, this.byteOffset + offset).setBigInt64(0, value, false);
|
|
910
|
+
return offset + 8;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
writeBigInt64LE(value: bigint, offset = 0): number {
|
|
914
|
+
validateBigIntWrite(value, true);
|
|
915
|
+
validateFixedWidthOffset(offset, 8, this.length);
|
|
916
|
+
new DataView(this.buffer, this.byteOffset + offset).setBigInt64(0, value, true);
|
|
917
|
+
return offset + 8;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
writeBigUInt64BE(value: bigint, offset = 0): number {
|
|
921
|
+
validateBigIntWrite(value, false);
|
|
922
|
+
validateFixedWidthOffset(offset, 8, this.length);
|
|
923
|
+
new DataView(this.buffer, this.byteOffset + offset).setBigUint64(0, value, false);
|
|
924
|
+
return offset + 8;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
writeBigUInt64LE(value: bigint, offset = 0): number {
|
|
928
|
+
validateBigIntWrite(value, false);
|
|
929
|
+
validateFixedWidthOffset(offset, 8, this.length);
|
|
930
|
+
new DataView(this.buffer, this.byteOffset + offset).setBigUint64(0, value, true);
|
|
931
|
+
return offset + 8;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
writeDoubleBE(value: number, offset = 0): number {
|
|
935
|
+
new DataView(this.buffer, this.byteOffset + offset).setFloat64(0, value, false);
|
|
936
|
+
return offset + 8;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
writeDoubleLE(value: number, offset = 0): number {
|
|
940
|
+
new DataView(this.buffer, this.byteOffset + offset).setFloat64(0, value, true);
|
|
941
|
+
return offset + 8;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
writeFloatBE(value: number, offset = 0): number {
|
|
945
|
+
new DataView(this.buffer, this.byteOffset + offset).setFloat32(0, value, false);
|
|
946
|
+
return offset + 4;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
writeFloatLE(value: number, offset = 0): number {
|
|
950
|
+
new DataView(this.buffer, this.byteOffset + offset).setFloat32(0, value, true);
|
|
951
|
+
return offset + 4;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
writeInt8(value: number, offset = 0): number {
|
|
955
|
+
validateIntegerWriteValue(value, -0x80, 0x7f, '>= -128 and <= 127');
|
|
956
|
+
offset = validateOffset(offset, 1, this.length);
|
|
957
|
+
new DataView(this.buffer, this.byteOffset + offset).setInt8(0, value);
|
|
958
|
+
return offset + 1;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
writeInt16BE(value: number, offset = 0): number {
|
|
962
|
+
validateIntegerWriteValue(value, -0x8000, 0x7fff, '>= -32768 and <= 32767');
|
|
963
|
+
offset = validateOffset(offset, 2, this.length);
|
|
964
|
+
new DataView(this.buffer, this.byteOffset + offset).setInt16(0, value, false);
|
|
965
|
+
return offset + 2;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
writeInt16LE(value: number, offset = 0): number {
|
|
969
|
+
validateIntegerWriteValue(value, -0x8000, 0x7fff, '>= -32768 and <= 32767');
|
|
970
|
+
offset = validateOffset(offset, 2, this.length);
|
|
971
|
+
new DataView(this.buffer, this.byteOffset + offset).setInt16(0, value, true);
|
|
972
|
+
return offset + 2;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
writeInt32BE(value: number, offset = 0): number {
|
|
976
|
+
validateIntegerWriteValue(value, -0x80000000, 0x7fffffff, '>= -2147483648 and <= 2147483647');
|
|
977
|
+
offset = validateOffset(offset, 4, this.length);
|
|
978
|
+
new DataView(this.buffer, this.byteOffset + offset).setInt32(0, value, false);
|
|
979
|
+
return offset + 4;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
writeInt32LE(value: number, offset = 0): number {
|
|
983
|
+
validateIntegerWriteValue(value, -0x80000000, 0x7fffffff, '>= -2147483648 and <= 2147483647');
|
|
984
|
+
offset = validateOffset(offset, 4, this.length);
|
|
985
|
+
new DataView(this.buffer, this.byteOffset + offset).setInt32(0, value, true);
|
|
986
|
+
return offset + 4;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
writeUInt8(value: number, offset = 0): number {
|
|
990
|
+
validateIntegerWriteValue(value, 0, 0xff, '>= 0 and <= 255');
|
|
991
|
+
offset = validateOffset(offset, 1, this.length);
|
|
992
|
+
this[offset] = value & 0xff;
|
|
993
|
+
return offset + 1;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
writeUInt16BE(value: number, offset = 0): number {
|
|
997
|
+
validateIntegerWriteValue(value, 0, 0xffff, '>= 0 and <= 65535');
|
|
998
|
+
offset = validateOffset(offset, 2, this.length);
|
|
999
|
+
new DataView(this.buffer, this.byteOffset + offset).setUint16(0, value, false);
|
|
1000
|
+
return offset + 2;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
writeUInt16LE(value: number, offset = 0): number {
|
|
1004
|
+
validateIntegerWriteValue(value, 0, 0xffff, '>= 0 and <= 65535');
|
|
1005
|
+
offset = validateOffset(offset, 2, this.length);
|
|
1006
|
+
new DataView(this.buffer, this.byteOffset + offset).setUint16(0, value, true);
|
|
1007
|
+
return offset + 2;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
writeUInt32BE(value: number, offset = 0): number {
|
|
1011
|
+
validateIntegerWriteValue(value, 0, 0xffffffff, '>= 0 and <= 4294967295');
|
|
1012
|
+
offset = validateOffset(offset, 4, this.length);
|
|
1013
|
+
new DataView(this.buffer, this.byteOffset + offset).setUint32(0, value, false);
|
|
1014
|
+
return offset + 4;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
writeUInt32LE(value: number, offset = 0): number {
|
|
1018
|
+
validateIntegerWriteValue(value, 0, 0xffffffff, '>= 0 and <= 4294967295');
|
|
1019
|
+
offset = validateOffset(offset, 4, this.length);
|
|
1020
|
+
new DataView(this.buffer, this.byteOffset + offset).setUint32(0, value, true);
|
|
1021
|
+
return offset + 4;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
writeUIntBE(value: number, offset: number, byteLength: number): number {
|
|
1025
|
+
return writeVariableUint(this, value, offset, byteLength, false);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
writeUIntLE(value: number, offset: number, byteLength: number): number {
|
|
1029
|
+
return writeVariableUint(this, value, offset, byteLength, true);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
writeIntBE(value: number, offset: number, byteLength: number): number {
|
|
1033
|
+
return writeVariableInt(this, value, offset, byteLength, false);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
writeIntLE(value: number, offset: number, byteLength: number): number {
|
|
1037
|
+
return writeVariableInt(this, value, offset, byteLength, true);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
readUint8(offset = 0): number {
|
|
1041
|
+
return this.readUInt8(offset);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
readUint16BE(offset = 0): number {
|
|
1045
|
+
return this.readUInt16BE(offset);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
readUint16LE(offset = 0): number {
|
|
1049
|
+
return this.readUInt16LE(offset);
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
readUint32BE(offset = 0): number {
|
|
1053
|
+
return this.readUInt32BE(offset);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
readUint32LE(offset = 0): number {
|
|
1057
|
+
return this.readUInt32LE(offset);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
readUintBE(offset?: number, byteLength?: number): number {
|
|
1061
|
+
return this.readUIntBE(offset, byteLength);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
readUintLE(offset?: number, byteLength?: number): number {
|
|
1065
|
+
return this.readUIntLE(offset, byteLength);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
readBigUint64BE(offset = 0): bigint {
|
|
1069
|
+
return this.readBigUInt64BE(offset);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
readBigUint64LE(offset = 0): bigint {
|
|
1073
|
+
return this.readBigUInt64LE(offset);
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
writeUint8(value: number, offset = 0): number {
|
|
1077
|
+
return this.writeUInt8(value, offset);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
writeUint16BE(value: number, offset = 0): number {
|
|
1081
|
+
return this.writeUInt16BE(value, offset);
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
writeUint16LE(value: number, offset = 0): number {
|
|
1085
|
+
return this.writeUInt16LE(value, offset);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
writeUint32BE(value: number, offset = 0): number {
|
|
1089
|
+
return this.writeUInt32BE(value, offset);
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
writeUint32LE(value: number, offset = 0): number {
|
|
1093
|
+
return this.writeUInt32LE(value, offset);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
writeUintBE(value: number, offset: number, byteLength: number): number {
|
|
1097
|
+
return this.writeUIntBE(value, offset, byteLength);
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
writeUintLE(value: number, offset: number, byteLength: number): number {
|
|
1101
|
+
return this.writeUIntLE(value, offset, byteLength);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
writeBigUint64BE(value: bigint, offset = 0): number {
|
|
1105
|
+
return this.writeBigUInt64BE(value, offset);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
writeBigUint64LE(value: bigint, offset = 0): number {
|
|
1109
|
+
return this.writeBigUInt64LE(value, offset);
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
[Symbol.iterator](): IterableIterator<number> {
|
|
1113
|
+
return super.values();
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
entries(): IterableIterator<[number, number]> {
|
|
1117
|
+
return super.entries();
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
keys(): IterableIterator<number> {
|
|
1121
|
+
return super.keys();
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
values(): IterableIterator<number> {
|
|
1125
|
+
return super.values();
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
[Symbol.for('nodejs.util.inspect.custom')](_depth?: number, _opts?: unknown): string {
|
|
1129
|
+
if (this.length === 0) {
|
|
1130
|
+
return '<Buffer >';
|
|
1131
|
+
}
|
|
1132
|
+
const hexBytes: string[] = [];
|
|
1133
|
+
const limit = Math.min(this.length, 50);
|
|
1134
|
+
for (let i = 0; i < limit; i++) {
|
|
1135
|
+
hexBytes.push(this[i].toString(16).padStart(2, '0'));
|
|
1136
|
+
}
|
|
1137
|
+
const suffix = this.length > 50 ? ` ... ${this.length - 50} more bytes` : '';
|
|
1138
|
+
return `<Buffer ${hexBytes.join(' ')}${suffix}>`;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
get parent(): ArrayBufferLike {
|
|
1142
|
+
return this.buffer;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
get offset(): number {
|
|
1146
|
+
return this.byteOffset;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
get [Symbol.toStringTag](): string {
|
|
1150
|
+
return 'Buffer';
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/**
|
|
1155
|
+
* Normalize encoding name to standard form.
|
|
1156
|
+
*/
|
|
1157
|
+
function normalizeEncoding(encoding: BufferEncoding | string | unknown): NormalizedEncoding {
|
|
1158
|
+
const value = typeof encoding === 'string' ? encoding : String(encoding);
|
|
1159
|
+
switch (value.toLowerCase()) {
|
|
1160
|
+
case 'utf8':
|
|
1161
|
+
case 'utf-8':
|
|
1162
|
+
return 'utf8';
|
|
1163
|
+
case 'ascii':
|
|
1164
|
+
return 'ascii';
|
|
1165
|
+
case 'latin1':
|
|
1166
|
+
case 'binary':
|
|
1167
|
+
return 'latin1';
|
|
1168
|
+
case 'hex':
|
|
1169
|
+
return 'hex';
|
|
1170
|
+
case 'base64':
|
|
1171
|
+
return 'base64';
|
|
1172
|
+
case 'base64url':
|
|
1173
|
+
return 'base64url';
|
|
1174
|
+
case 'ucs2':
|
|
1175
|
+
case 'ucs-2':
|
|
1176
|
+
case 'utf16le':
|
|
1177
|
+
case 'utf-16le':
|
|
1178
|
+
return 'utf16le';
|
|
1179
|
+
default:
|
|
1180
|
+
throw makeUnknownEncodingError(value);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
function normalizeEncodingOrUtf8(encoding: unknown): NormalizedEncoding {
|
|
1185
|
+
if (typeof encoding !== 'string' || encoding === '') {
|
|
1186
|
+
return 'utf8';
|
|
1187
|
+
}
|
|
1188
|
+
try {
|
|
1189
|
+
return normalizeEncoding(encoding);
|
|
1190
|
+
} catch {
|
|
1191
|
+
return 'utf8';
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function utf8ByteLength(value: string): number {
|
|
1196
|
+
let total = 0;
|
|
1197
|
+
|
|
1198
|
+
for (let index = 0; index < value.length; index++) {
|
|
1199
|
+
const code = value.charCodeAt(index);
|
|
1200
|
+
if (code <= 0x7f) {
|
|
1201
|
+
total += 1;
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
if (code <= 0x7ff) {
|
|
1205
|
+
total += 2;
|
|
1206
|
+
continue;
|
|
1207
|
+
}
|
|
1208
|
+
if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length) {
|
|
1209
|
+
const next = value.charCodeAt(index + 1);
|
|
1210
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
1211
|
+
total += 4;
|
|
1212
|
+
index += 1;
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
total += 3;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
return total;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
function decodeBase64Char(code: number): number {
|
|
1223
|
+
if (code >= 0x41 && code <= 0x5a) return code - 0x41;
|
|
1224
|
+
if (code >= 0x61 && code <= 0x7a) return code - 0x47;
|
|
1225
|
+
if (code >= 0x30 && code <= 0x39) return code + 0x04;
|
|
1226
|
+
if (code === 0x2b || code === 0x2d) return 62;
|
|
1227
|
+
if (code === 0x2f || code === 0x5f) return 63;
|
|
1228
|
+
return -1;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
function decodeBase64Bytes(value: string): Uint8Array {
|
|
1232
|
+
const sextets: number[] = [];
|
|
1233
|
+
for (let i = 0; i < value.length; i++) {
|
|
1234
|
+
const code = value.charCodeAt(i);
|
|
1235
|
+
if (code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d || code === 0x20) {
|
|
1236
|
+
continue;
|
|
1237
|
+
}
|
|
1238
|
+
if (code === 0x3d) {
|
|
1239
|
+
break;
|
|
1240
|
+
}
|
|
1241
|
+
const sextet = decodeBase64Char(code);
|
|
1242
|
+
if (sextet >= 0) {
|
|
1243
|
+
sextets.push(sextet);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
const fullGroups = Math.floor(sextets.length / 4);
|
|
1248
|
+
const remainder = sextets.length % 4;
|
|
1249
|
+
let byteLength = fullGroups * 3;
|
|
1250
|
+
if (remainder === 2) {
|
|
1251
|
+
byteLength += 1;
|
|
1252
|
+
} else if (remainder === 3) {
|
|
1253
|
+
byteLength += 2;
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
const bytes = new Uint8Array(byteLength);
|
|
1257
|
+
let outIndex = 0;
|
|
1258
|
+
let sextetIndex = 0;
|
|
1259
|
+
|
|
1260
|
+
for (let group = 0; group < fullGroups; group++) {
|
|
1261
|
+
const a = sextets[sextetIndex++];
|
|
1262
|
+
const b = sextets[sextetIndex++];
|
|
1263
|
+
const c = sextets[sextetIndex++];
|
|
1264
|
+
const d = sextets[sextetIndex++];
|
|
1265
|
+
bytes[outIndex++] = (a << 2) | (b >> 4);
|
|
1266
|
+
bytes[outIndex++] = ((b & 0x0f) << 4) | (c >> 2);
|
|
1267
|
+
bytes[outIndex++] = ((c & 0x03) << 6) | d;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
if (remainder === 2) {
|
|
1271
|
+
const a = sextets[sextetIndex++];
|
|
1272
|
+
const b = sextets[sextetIndex++];
|
|
1273
|
+
bytes[outIndex++] = (a << 2) | (b >> 4);
|
|
1274
|
+
} else if (remainder === 3) {
|
|
1275
|
+
const a = sextets[sextetIndex++];
|
|
1276
|
+
const b = sextets[sextetIndex++];
|
|
1277
|
+
const c = sextets[sextetIndex++];
|
|
1278
|
+
bytes[outIndex++] = (a << 2) | (b >> 4);
|
|
1279
|
+
bytes[outIndex++] = ((b & 0x0f) << 4) | (c >> 2);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
return bytes;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function validateBufferSize(size: number): number {
|
|
1286
|
+
if (typeof size !== 'number') {
|
|
1287
|
+
throw makeInvalidArgTypeError('size', 'of type number', size);
|
|
1288
|
+
}
|
|
1289
|
+
if (Number.isNaN(size)) {
|
|
1290
|
+
throw createOutOfRangeError('size', 'a non-negative integer', 'NaN');
|
|
1291
|
+
}
|
|
1292
|
+
if (!Number.isFinite(size) || size < 0 || size > Buffer.constants.MAX_LENGTH) {
|
|
1293
|
+
throw createOutOfRangeError('size', `>= 0 && <= ${Buffer.constants.MAX_LENGTH}`, size);
|
|
1294
|
+
}
|
|
1295
|
+
return normalizeInteger(size);
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
function isArrayBufferLike(value: unknown): value is ArrayBuffer | SharedArrayBuffer {
|
|
1299
|
+
return getArrayBufferByteLength(value) !== null;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
function getArrayBufferByteLength(value: unknown): number | null {
|
|
1303
|
+
if (!value || typeof value !== 'object') {
|
|
1304
|
+
return null;
|
|
1305
|
+
}
|
|
1306
|
+
const tag = Object.prototype.toString.call(value);
|
|
1307
|
+
if (tag !== '[object ArrayBuffer]' && tag !== '[object SharedArrayBuffer]') {
|
|
1308
|
+
return null;
|
|
1309
|
+
}
|
|
1310
|
+
try {
|
|
1311
|
+
return typeof (value as ArrayBuffer | SharedArrayBuffer).byteLength === 'number'
|
|
1312
|
+
? (value as ArrayBuffer | SharedArrayBuffer).byteLength
|
|
1313
|
+
: null;
|
|
1314
|
+
} catch {
|
|
1315
|
+
return null;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
function isUint8ArrayLike(value: unknown): value is Uint8Array {
|
|
1320
|
+
return !!(
|
|
1321
|
+
value &&
|
|
1322
|
+
typeof value === 'object' &&
|
|
1323
|
+
(value instanceof Buffer || Object.prototype.toString.call(value) === '[object Uint8Array]')
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
function toArrayLikeLength(value: unknown): number {
|
|
1328
|
+
const raw = Number((value as { length?: unknown })?.length);
|
|
1329
|
+
if (!Number.isFinite(raw) || raw <= 0) {
|
|
1330
|
+
return 0;
|
|
1331
|
+
}
|
|
1332
|
+
return Math.min(Math.floor(raw), 0x7fffffff);
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
function formatReceivedValue(value: unknown, quoteStrings = false): string {
|
|
1336
|
+
if (value === null) return 'null';
|
|
1337
|
+
if (value === undefined) return 'undefined';
|
|
1338
|
+
if (typeof value === 'string') {
|
|
1339
|
+
return quoteStrings ? `type string ('${value}')` : `type string (${value})`;
|
|
1340
|
+
}
|
|
1341
|
+
if (typeof value === 'function') {
|
|
1342
|
+
return `function ${(value as Function).name || '<anonymous>'}`;
|
|
1343
|
+
}
|
|
1344
|
+
if (typeof value !== 'object') {
|
|
1345
|
+
return `type ${typeof value} (${String(value)})`;
|
|
1346
|
+
}
|
|
1347
|
+
const ctorName = (value as { constructor?: { name?: string } }).constructor?.name;
|
|
1348
|
+
if (ctorName) {
|
|
1349
|
+
return `an instance of ${ctorName}`;
|
|
1350
|
+
}
|
|
1351
|
+
return String(value);
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
function makeInvalidArgTypeError(
|
|
1355
|
+
name: string,
|
|
1356
|
+
expected: string,
|
|
1357
|
+
value: unknown,
|
|
1358
|
+
quoteStrings = false,
|
|
1359
|
+
): TypeError & { code: string } {
|
|
1360
|
+
const err = new TypeError(
|
|
1361
|
+
`The "${name}" argument must be ${expected}. Received ${formatReceivedValue(value, quoteStrings)}`,
|
|
1362
|
+
) as TypeError & { code: string };
|
|
1363
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
1364
|
+
return err;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
function makeFirstArgumentError(value: unknown): TypeError & { code: string } {
|
|
1368
|
+
const err = new TypeError(
|
|
1369
|
+
'The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. ' +
|
|
1370
|
+
`Received ${formatReceivedValue(value)}`,
|
|
1371
|
+
) as TypeError & { code: string };
|
|
1372
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
1373
|
+
return err;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
function makeUnknownEncodingError(encoding: string): TypeError & { code: string } {
|
|
1377
|
+
const err = new TypeError(`Unknown encoding: ${encoding}`) as TypeError & { code: string };
|
|
1378
|
+
err.code = 'ERR_UNKNOWN_ENCODING';
|
|
1379
|
+
return err;
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
function createOutOfRangeError(
|
|
1383
|
+
name: string,
|
|
1384
|
+
expectation: string,
|
|
1385
|
+
received: unknown,
|
|
1386
|
+
): RangeError & { code: string } {
|
|
1387
|
+
const err = new RangeError(
|
|
1388
|
+
`The value of "${name}" is out of range. It must be ${expectation}. Received ${String(received)}`,
|
|
1389
|
+
) as RangeError & { code: string };
|
|
1390
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
1391
|
+
return err;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
function makeBufferBoundsError(which: 'offset' | 'length'): RangeError & { code: string } {
|
|
1395
|
+
const err = new RangeError(`"${which}" is outside of buffer bounds`) as RangeError & { code: string };
|
|
1396
|
+
err.code = 'ERR_BUFFER_OUT_OF_BOUNDS';
|
|
1397
|
+
return err;
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
function normalizeInteger(value: number): number {
|
|
1401
|
+
return value < 0 ? Math.ceil(value) : Math.floor(value);
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function normalizeToStringRangeIndex(value: unknown, bufferLength: number, fallback: number): number {
|
|
1405
|
+
if (value === undefined) {
|
|
1406
|
+
return fallback;
|
|
1407
|
+
}
|
|
1408
|
+
let numeric = Number(value);
|
|
1409
|
+
if (Number.isNaN(numeric)) {
|
|
1410
|
+
numeric = 0;
|
|
1411
|
+
}
|
|
1412
|
+
if (!Number.isFinite(numeric)) {
|
|
1413
|
+
return numeric < 0 ? 0 : bufferLength;
|
|
1414
|
+
}
|
|
1415
|
+
numeric = normalizeInteger(numeric);
|
|
1416
|
+
if (numeric < 0) {
|
|
1417
|
+
return 0;
|
|
1418
|
+
}
|
|
1419
|
+
if (numeric > bufferLength) {
|
|
1420
|
+
return bufferLength;
|
|
1421
|
+
}
|
|
1422
|
+
return numeric;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
function normalizeArrayBufferOffset(value: unknown, totalLength: number): number {
|
|
1426
|
+
if (value === undefined) {
|
|
1427
|
+
return 0;
|
|
1428
|
+
}
|
|
1429
|
+
const offset = Number(value);
|
|
1430
|
+
if (Number.isNaN(offset)) {
|
|
1431
|
+
return 0;
|
|
1432
|
+
}
|
|
1433
|
+
if (!Number.isFinite(offset)) {
|
|
1434
|
+
throw makeBufferBoundsError('offset');
|
|
1435
|
+
}
|
|
1436
|
+
const normalized = normalizeInteger(offset);
|
|
1437
|
+
if (normalized < 0 || normalized > totalLength) {
|
|
1438
|
+
throw makeBufferBoundsError('offset');
|
|
1439
|
+
}
|
|
1440
|
+
return normalized;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
function normalizeArrayBufferLength(value: unknown, totalLength: number, offset: number): number {
|
|
1444
|
+
if (value === undefined) {
|
|
1445
|
+
return totalLength - offset;
|
|
1446
|
+
}
|
|
1447
|
+
const length = Number(value);
|
|
1448
|
+
if (Number.isNaN(length)) {
|
|
1449
|
+
return 0;
|
|
1450
|
+
}
|
|
1451
|
+
if (!Number.isFinite(length)) {
|
|
1452
|
+
throw makeBufferBoundsError('length');
|
|
1453
|
+
}
|
|
1454
|
+
const normalized = normalizeInteger(length);
|
|
1455
|
+
if (normalized < 0 || offset + normalized > totalLength) {
|
|
1456
|
+
throw makeBufferBoundsError('length');
|
|
1457
|
+
}
|
|
1458
|
+
return normalized;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
function compareByteRanges(
|
|
1462
|
+
left: Uint8Array,
|
|
1463
|
+
right: Uint8Array,
|
|
1464
|
+
leftStart: number,
|
|
1465
|
+
leftEnd: number,
|
|
1466
|
+
rightStart: number,
|
|
1467
|
+
rightEnd: number,
|
|
1468
|
+
): -1 | 0 | 1 {
|
|
1469
|
+
const leftLength = leftEnd - leftStart;
|
|
1470
|
+
const rightLength = rightEnd - rightStart;
|
|
1471
|
+
const length = Math.min(leftLength, rightLength);
|
|
1472
|
+
for (let i = 0; i < length; i++) {
|
|
1473
|
+
const leftValue = left[leftStart + i];
|
|
1474
|
+
const rightValue = right[rightStart + i];
|
|
1475
|
+
if (leftValue !== rightValue) {
|
|
1476
|
+
return leftValue < rightValue ? -1 : 1;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
if (leftLength === rightLength) {
|
|
1480
|
+
return 0;
|
|
1481
|
+
}
|
|
1482
|
+
return leftLength < rightLength ? -1 : 1;
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
function getWritableByteView(value: unknown): Uint8Array | null {
|
|
1486
|
+
if (isUint8ArrayLike(value)) {
|
|
1487
|
+
return value;
|
|
1488
|
+
}
|
|
1489
|
+
if (ArrayBuffer.isView(value)) {
|
|
1490
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
1491
|
+
}
|
|
1492
|
+
return null;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
function normalizeCompareIndex(value: unknown, name: string, fallback: number): number {
|
|
1496
|
+
if (value === undefined) {
|
|
1497
|
+
return fallback;
|
|
1498
|
+
}
|
|
1499
|
+
if (typeof value !== 'number') {
|
|
1500
|
+
throw makeInvalidArgTypeError(name, 'of type number', value);
|
|
1501
|
+
}
|
|
1502
|
+
if (Number.isNaN(value) || !Number.isInteger(value)) {
|
|
1503
|
+
throw createOutOfRangeError(name, 'an integer', value);
|
|
1504
|
+
}
|
|
1505
|
+
if (!Number.isFinite(value)) {
|
|
1506
|
+
throw createOutOfRangeError(name, '>= 0 && <= 2147483647', value);
|
|
1507
|
+
}
|
|
1508
|
+
return normalizeInteger(value);
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
function normalizeCopyIndex(value: unknown, name: string, fallback: number): number {
|
|
1512
|
+
if (value === undefined) {
|
|
1513
|
+
return fallback;
|
|
1514
|
+
}
|
|
1515
|
+
const numeric = Number(value);
|
|
1516
|
+
if (Number.isNaN(numeric)) {
|
|
1517
|
+
return fallback;
|
|
1518
|
+
}
|
|
1519
|
+
if (!Number.isFinite(numeric)) {
|
|
1520
|
+
throw createOutOfRangeError(name, '>= 0', value);
|
|
1521
|
+
}
|
|
1522
|
+
return normalizeInteger(numeric);
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
function validateBigIntWrite(value: unknown, signed: boolean): void {
|
|
1526
|
+
if (typeof value !== 'bigint') {
|
|
1527
|
+
throw makeInvalidArgTypeError('value', 'of type bigint', value);
|
|
1528
|
+
}
|
|
1529
|
+
const min = signed ? -(1n << 63n) : 0n;
|
|
1530
|
+
const max = signed ? (1n << 63n) - 1n : (1n << 64n) - 1n;
|
|
1531
|
+
if (value < min || value > max) {
|
|
1532
|
+
const expectation = signed
|
|
1533
|
+
? '>= -(2n ** 63n) and < 2n ** 63n'
|
|
1534
|
+
: '>= 0n and < 2n ** 64n';
|
|
1535
|
+
const err = new RangeError(
|
|
1536
|
+
`The value of "value" is out of range. It must be ${expectation}. Received ${formatBigInt(value)}`,
|
|
1537
|
+
) as RangeError & { code: string };
|
|
1538
|
+
err.code = 'ERR_OUT_OF_RANGE';
|
|
1539
|
+
throw err;
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
function formatBigInt(value: bigint): string {
|
|
1544
|
+
const negative = value < 0n;
|
|
1545
|
+
const digits = (negative ? -value : value).toString();
|
|
1546
|
+
let formatted = '';
|
|
1547
|
+
const prefixLength = digits.length % 3;
|
|
1548
|
+
if (prefixLength > 0) {
|
|
1549
|
+
formatted = digits.slice(0, prefixLength);
|
|
1550
|
+
}
|
|
1551
|
+
for (let index = prefixLength; index < digits.length; index += 3) {
|
|
1552
|
+
if (formatted) {
|
|
1553
|
+
formatted += '_';
|
|
1554
|
+
}
|
|
1555
|
+
formatted += digits.slice(index, index + 3);
|
|
1556
|
+
}
|
|
1557
|
+
return `${negative ? '-' : ''}${formatted || '0'}n`;
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
function formatIntegerForRangeError(value: number): string {
|
|
1561
|
+
if (!Number.isFinite(value) || Number.isNaN(value)) {
|
|
1562
|
+
return String(value);
|
|
1563
|
+
}
|
|
1564
|
+
const negative = value < 0;
|
|
1565
|
+
const digits = String(Math.trunc(Math.abs(value)));
|
|
1566
|
+
let formatted = '';
|
|
1567
|
+
const prefixLength = digits.length % 3;
|
|
1568
|
+
if (prefixLength > 0) {
|
|
1569
|
+
formatted = digits.slice(0, prefixLength);
|
|
1570
|
+
}
|
|
1571
|
+
for (let index = prefixLength; index < digits.length; index += 3) {
|
|
1572
|
+
if (formatted) {
|
|
1573
|
+
formatted += '_';
|
|
1574
|
+
}
|
|
1575
|
+
formatted += digits.slice(index, index + 3);
|
|
1576
|
+
}
|
|
1577
|
+
return `${negative ? '-' : ''}${formatted || '0'}`;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
function validateIntegerWriteValue(
|
|
1581
|
+
value: unknown,
|
|
1582
|
+
min: number,
|
|
1583
|
+
max: number,
|
|
1584
|
+
expectation: string,
|
|
1585
|
+
formatReceived = false,
|
|
1586
|
+
): number {
|
|
1587
|
+
if (typeof value !== 'number') {
|
|
1588
|
+
throw makeInvalidArgTypeError('value', 'of type number', value);
|
|
1589
|
+
}
|
|
1590
|
+
if (!Number.isFinite(value) || !Number.isInteger(value) || value < min || value > max) {
|
|
1591
|
+
throw createOutOfRangeError(
|
|
1592
|
+
'value',
|
|
1593
|
+
expectation,
|
|
1594
|
+
formatReceived ? formatIntegerForRangeError(value) : value,
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
return value;
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
function validateFixedWidthOffset(offset: number, byteLength: number, bufferLength: number): void {
|
|
1601
|
+
if (!Number.isInteger(offset) || offset < 0 || offset + byteLength > bufferLength) {
|
|
1602
|
+
throw createOutOfRangeError('offset', `>= 0 and <= ${Math.max(bufferLength - byteLength, 0)}`, offset);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
function makeOutOfBoundsReadError(): RangeError & { code: string } {
|
|
1607
|
+
const err = new RangeError('Attempt to access memory outside buffer bounds') as RangeError & { code: string };
|
|
1608
|
+
err.code = 'ERR_BUFFER_OUT_OF_BOUNDS';
|
|
1609
|
+
return err;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
function validateOffset(offset: unknown, byteLength: number, bufferLength: number): number {
|
|
1613
|
+
if (offset === undefined) {
|
|
1614
|
+
return 0;
|
|
1615
|
+
}
|
|
1616
|
+
if (typeof offset !== 'number') {
|
|
1617
|
+
throw makeInvalidArgTypeError('offset', 'of type number', offset);
|
|
1618
|
+
}
|
|
1619
|
+
if (Number.isNaN(offset) || (Number.isFinite(offset) && !Number.isInteger(offset))) {
|
|
1620
|
+
throw createOutOfRangeError('offset', 'an integer', offset);
|
|
1621
|
+
}
|
|
1622
|
+
const maxOffset = Math.max(bufferLength - byteLength, 0);
|
|
1623
|
+
if (!Number.isFinite(offset) || offset < 0 || offset > maxOffset) {
|
|
1624
|
+
if (bufferLength < byteLength && offset >= 0 && Number.isFinite(offset)) {
|
|
1625
|
+
throw makeOutOfBoundsReadError();
|
|
1626
|
+
}
|
|
1627
|
+
throw createOutOfRangeError('offset', `>= 0 and <= ${maxOffset}`, offset);
|
|
1628
|
+
}
|
|
1629
|
+
if (offset + byteLength > bufferLength) {
|
|
1630
|
+
throw makeOutOfBoundsReadError();
|
|
1631
|
+
}
|
|
1632
|
+
return offset;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function validateRequiredOffset(offset: unknown, byteLength: number, bufferLength: number): number {
|
|
1636
|
+
if (offset === undefined) {
|
|
1637
|
+
throw makeInvalidArgTypeError('offset', 'of type number', offset);
|
|
1638
|
+
}
|
|
1639
|
+
return validateOffset(offset, byteLength, bufferLength);
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
function validateByteLength(byteLength: unknown): number {
|
|
1643
|
+
if (typeof byteLength !== 'number') {
|
|
1644
|
+
throw makeInvalidArgTypeError('byteLength', 'of type number', byteLength);
|
|
1645
|
+
}
|
|
1646
|
+
if (Number.isNaN(byteLength) || (Number.isFinite(byteLength) && !Number.isInteger(byteLength))) {
|
|
1647
|
+
throw createOutOfRangeError('byteLength', 'an integer', byteLength);
|
|
1648
|
+
}
|
|
1649
|
+
if (!Number.isFinite(byteLength) || byteLength < 1 || byteLength > 6) {
|
|
1650
|
+
throw createOutOfRangeError('byteLength', '>= 1 and <= 6', byteLength);
|
|
1651
|
+
}
|
|
1652
|
+
return byteLength;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
function readVariableUint(
|
|
1656
|
+
buffer: Uint8Array,
|
|
1657
|
+
offset: unknown,
|
|
1658
|
+
byteLength: unknown,
|
|
1659
|
+
littleEndian: boolean,
|
|
1660
|
+
): number {
|
|
1661
|
+
const actualByteLength = validateByteLength(byteLength);
|
|
1662
|
+
const actualOffset = validateRequiredOffset(offset, actualByteLength, buffer.length);
|
|
1663
|
+
|
|
1664
|
+
let value = 0;
|
|
1665
|
+
if (littleEndian) {
|
|
1666
|
+
for (let index = actualByteLength - 1; index >= 0; index--) {
|
|
1667
|
+
value = (value * 256) + buffer[actualOffset + index];
|
|
1668
|
+
}
|
|
1669
|
+
return value;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
for (let index = 0; index < actualByteLength; index++) {
|
|
1673
|
+
value = (value * 256) + buffer[actualOffset + index];
|
|
1674
|
+
}
|
|
1675
|
+
return value;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
function readVariableInt(
|
|
1679
|
+
buffer: Uint8Array,
|
|
1680
|
+
offset: unknown,
|
|
1681
|
+
byteLength: unknown,
|
|
1682
|
+
littleEndian: boolean,
|
|
1683
|
+
): number {
|
|
1684
|
+
const actualByteLength = validateByteLength(byteLength);
|
|
1685
|
+
const unsigned = readVariableUint(buffer, offset, actualByteLength, littleEndian);
|
|
1686
|
+
const bits = actualByteLength * 8;
|
|
1687
|
+
const signBit = 2 ** (bits - 1);
|
|
1688
|
+
const fullRange = 2 ** bits;
|
|
1689
|
+
return unsigned >= signBit ? unsigned - fullRange : unsigned;
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
function writeVariableUint(
|
|
1693
|
+
buffer: Uint8Array,
|
|
1694
|
+
value: number,
|
|
1695
|
+
offset: number,
|
|
1696
|
+
byteLength: number,
|
|
1697
|
+
littleEndian: boolean,
|
|
1698
|
+
): number {
|
|
1699
|
+
const actualByteLength = validateByteLength(byteLength);
|
|
1700
|
+
const actualOffset = validateRequiredOffset(offset, actualByteLength, buffer.length);
|
|
1701
|
+
const max = (2 ** (actualByteLength * 8)) - 1;
|
|
1702
|
+
const expectation = actualByteLength > 4
|
|
1703
|
+
? `>= 0 and < 2 ** ${actualByteLength * 8}`
|
|
1704
|
+
: `>= 0 and <= ${max}`;
|
|
1705
|
+
let remaining = validateIntegerWriteValue(value, 0, max, expectation, actualByteLength > 4);
|
|
1706
|
+
|
|
1707
|
+
if (littleEndian) {
|
|
1708
|
+
for (let i = 0; i < actualByteLength; i++) {
|
|
1709
|
+
buffer[actualOffset + i] = remaining & 0xff;
|
|
1710
|
+
remaining = Math.floor(remaining / 256);
|
|
1711
|
+
}
|
|
1712
|
+
} else {
|
|
1713
|
+
for (let i = actualByteLength - 1; i >= 0; i--) {
|
|
1714
|
+
buffer[actualOffset + i] = remaining & 0xff;
|
|
1715
|
+
remaining = Math.floor(remaining / 256);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
return actualOffset + actualByteLength;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
function writeVariableInt(
|
|
1723
|
+
buffer: Uint8Array,
|
|
1724
|
+
value: number,
|
|
1725
|
+
offset: number,
|
|
1726
|
+
byteLength: number,
|
|
1727
|
+
littleEndian: boolean,
|
|
1728
|
+
): number {
|
|
1729
|
+
const actualByteLength = validateByteLength(byteLength);
|
|
1730
|
+
const min = -(2 ** (actualByteLength * 8 - 1));
|
|
1731
|
+
const max = (2 ** (actualByteLength * 8 - 1)) - 1;
|
|
1732
|
+
const expectation = actualByteLength > 4
|
|
1733
|
+
? `>= -(2 ** ${actualByteLength * 8 - 1}) and < 2 ** ${actualByteLength * 8 - 1}`
|
|
1734
|
+
: `>= ${min} and <= ${max}`;
|
|
1735
|
+
const actualValue = validateIntegerWriteValue(
|
|
1736
|
+
value,
|
|
1737
|
+
min,
|
|
1738
|
+
max,
|
|
1739
|
+
expectation,
|
|
1740
|
+
actualByteLength > 4,
|
|
1741
|
+
);
|
|
1742
|
+
const fullRange = 2 ** (actualByteLength * 8);
|
|
1743
|
+
const unsigned = actualValue < 0 ? actualValue + fullRange : actualValue;
|
|
1744
|
+
return writeVariableUint(buffer, unsigned, offset, actualByteLength, littleEndian);
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
/**
|
|
1748
|
+
* Maximum buffer size (same as Buffer.constants.MAX_LENGTH).
|
|
1749
|
+
*/
|
|
1750
|
+
export const kMaxLength = 2 ** 31 - 1;
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* SlowBuffer is deprecated but some packages still use it.
|
|
1754
|
+
* It's equivalent to Buffer.allocUnsafeSlow.
|
|
1755
|
+
*/
|
|
1756
|
+
export const SlowBuffer = Buffer.allocUnsafeSlow.bind(Buffer);
|
|
1757
|
+
|
|
1758
|
+
const bufferPrototype = Buffer.prototype as Record<string, unknown>;
|
|
1759
|
+
for (const [alias, original] of [
|
|
1760
|
+
['readUint8', 'readUInt8'],
|
|
1761
|
+
['readUint16LE', 'readUInt16LE'],
|
|
1762
|
+
['readUint16BE', 'readUInt16BE'],
|
|
1763
|
+
['readUint32LE', 'readUInt32LE'],
|
|
1764
|
+
['readUint32BE', 'readUInt32BE'],
|
|
1765
|
+
['readUintLE', 'readUIntLE'],
|
|
1766
|
+
['readUintBE', 'readUIntBE'],
|
|
1767
|
+
['readBigUint64LE', 'readBigUInt64LE'],
|
|
1768
|
+
['readBigUint64BE', 'readBigUInt64BE'],
|
|
1769
|
+
['writeUint8', 'writeUInt8'],
|
|
1770
|
+
['writeUint16LE', 'writeUInt16LE'],
|
|
1771
|
+
['writeUint16BE', 'writeUInt16BE'],
|
|
1772
|
+
['writeUint32LE', 'writeUInt32LE'],
|
|
1773
|
+
['writeUint32BE', 'writeUInt32BE'],
|
|
1774
|
+
['writeUintLE', 'writeUIntLE'],
|
|
1775
|
+
['writeUintBE', 'writeUIntBE'],
|
|
1776
|
+
['writeBigUint64LE', 'writeBigUInt64LE'],
|
|
1777
|
+
['writeBigUint64BE', 'writeBigUInt64BE'],
|
|
1778
|
+
] as const) {
|
|
1779
|
+
Object.defineProperty(bufferPrototype, alias, {
|
|
1780
|
+
value: bufferPrototype[original],
|
|
1781
|
+
writable: true,
|
|
1782
|
+
configurable: true,
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
export default Buffer;
|