Skip to content
File

Blob: src/node/internal/internal_buffer.ts

typescript2720 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4//
5 
6// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
7// Copyright Joyent and Node contributors. All rights reserved. MIT license.
8// Copyright Feross Aboukhadijeh, and other contributors. All rights reserved. MIT license.
9 
10/* TODO: the following is adopted code, enabling linting one day */
11/* eslint-disable */
12 
13import {
14 ERR_BUFFER_OUT_OF_BOUNDS,
15 ERR_OUT_OF_RANGE,
16 ERR_INVALID_ARG_TYPE,
17 ERR_INVALID_ARG_VALUE,
18 ERR_INVALID_BUFFER_SIZE,
19 ERR_UNKNOWN_ENCODING,
20} from 'node-internal:internal_errors';
21 
22import { default as bufferUtil } from 'node-internal:buffer';
23import type { Encoding } from 'node-internal:buffer';
24 
25import {
26 isAnyArrayBuffer,
27 isArrayBufferView,
28 isUint8Array,
29} from 'node-internal:internal_types';
30 
31import {
32 normalizeEncoding,
33 getEncodingOps,
34} from 'node-internal:internal_utils';
35 
36import { validateString } from 'node-internal:validators';
37 
38import internalUtil from 'node-internal:util';
39import {
40 type InspectOptionsStylized,
41 inspect as utilInspect,
42} from 'node-internal:internal_inspect';
43 
44const { ASCII, BASE64, BASE64URL, HEX, LATIN1, UTF16LE, UTF8 } = bufferUtil;
45 
46// Temporary buffers to convert numbers.
47const float32Array = new Float32Array(1);
48const uInt8Float32Array = new Uint8Array(float32Array.buffer);
49const float64Array = new Float64Array(1);
50const uInt8Float64Array = new Uint8Array(float64Array.buffer);
51 
52// Check endianness.
53float32Array[0] = -1; // 0xBF800000
54// Either it is [0, 0, 128, 191] or [191, 128, 0, 0]. It is not possible to
55// check this with `os.endianness()` because that is determined at compile time.
56export const bigEndian = uInt8Float32Array[3] === 0;
57 
58// Node.js caps its max length at uint32_t max, we are very intentionally more
59// conservative here, capping at int32_t max.
60export const kMaxLength = 2147483647;
61export const kStringMaxLength = 536870888;
62const MAX_UINT32 = 2 ** 32;
63const kIsBuffer = Symbol('kIsBuffer');
64 
65const customInspectSymbol =
66 typeof Symbol === 'function' && typeof Symbol['for'] === 'function'
67 ? Symbol['for']('nodejs.util.inspect.custom')
68 : null;
69 
70// One difference between Node.js and workerd is that, workerd
71// doesn't expose a setter for this, whereas Node.js does.
72export const INSPECT_MAX_BYTES = 50;
73 
74export const constants = {
75 MAX_LENGTH: kMaxLength,
76 MAX_STRING_LENGTH: kStringMaxLength,
77};
78 
79function createBuffer(length: number): Buffer {
80 if (length > kMaxLength) {
81 throw new ERR_OUT_OF_RANGE(
82 'The given length is invalid',
83 `0 to ${kMaxLength}`,
84 length
85 );
86 }
87 const buf = new Uint8Array(length);
88 Object.setPrototypeOf(buf, Buffer.prototype);
89 return buf as Buffer;
90}
91 
92type WithImplicitCoercion<T> = T | { valueOf(): T };
93export type StringLike =
94 | WithImplicitCoercion<string>
95 | { [Symbol.toPrimitive](hint: 'string'): string };
96type ArrayBufferLike = WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>;
97type BufferSource =
98 | StringLike
99 | ArrayBufferLike
100 | Uint8Array
101 | ReadonlyArray<number>;
102 
103export interface Buffer extends Uint8Array {
104 readonly buffer: ArrayBuffer;
105 readonly parent: ArrayBuffer;
106 readonly byteOffset: number;
107 readonly length: number;
108 compare(
109 target: Uint8Array,
110 targetStart?: number,
111 targetEnd?: number,
112 sourceStart?: number,
113 sourceEnd?: number
114 ): number;
115 copy(
116 target: Uint8Array,
117 targetStart?: number,
118 sourceStart?: number,
119 sourceEnd?: number
120 ): number;
121 equals(other: Uint8Array): boolean;
122 fill(value: number, offset?: number, end?: number): this;
123 fill(value: string, encoding?: string): this;
124 fill(value: string, offset?: number, end?: number, encoding?: string): this;
125 fill(value: Uint8Array, offset?: number, end?: number): this;
126 includes(value: number, byteOffset?: number): boolean;
127 includes(value: string, encoding?: string): boolean;
128 includes(value: string, byteOffset?: number, encoding?: string): boolean;
129 includes(value: Uint8Array, byteOffset?: number): boolean;
130 indexOf(value: number, byteOffset?: number): number;
131 indexOf(value: string, encoding?: string): number;
132 indexOf(value: string, byteOffset?: number, encoding?: string): number;
133 indexOf(value: Uint8Array, byteOffset?: number): number;
134 lastIndexOf(value: number, byteOffset?: number): number;
135 lastIndexOf(value: string, encoding?: string): number;
136 lastIndexOf(value: string, byteOffset?: number, encoding?: string): number;
137 lastIndexOf(value: Uint8Array, byteOffset?: number): number;
138 readBigInt64BE(offset?: number): bigint;
139 readBigInt64LE(offset?: number): bigint;
140 readBigUInt64BE(offset?: number): bigint;
141 readBigUInt64LE(offset?: number): bigint;
142 readDoubleBE(offset?: number): number;
143 readDoubleLE(offset?: number): number;
144 readFloatBE(offset?: number): number;
145 readFloatLE(offset?: number): number;
146 readInt8(offset?: number): number;
147 readInt16BE(offset?: number): number;
148 readInt16LE(offset?: number): number;
149 readInt32BE(offset?: number): number;
150 readInt32LE(offset?: number): number;
151 readIntBE(offset?: number, byteLength?: number): number;
152 readIntLE(offset?: number, byteLength?: number): number;
153 readUInt8(offset?: number): number;
154 readUInt16BE(offset?: number): number;
155 readUInt16LE(offset?: number): number;
156 readUInt32BE(offset?: number): number;
157 readUInt32LE(offset?: number): number;
158 readUIntBE(offset?: number, byteLength?: number): number;
159 readUIntLE(offset?: number, byteLength?: number): number;
160 swap16(): this;
161 swap32(): this;
162 swap64(): this;
163 toJSON(): { type: 'Buffer'; data: number[] };
164 toString(encoding?: string, start?: number, end?: number): string;
165 write(string: string, encoding?: string): number;
166 write(string: string, offset?: number, encoding?: string): number;
167 write(
168 string: string,
169 offset?: number,
170 length?: number,
171 encoding?: string
172 ): number;
173 writeBigInt64BE(value: bigint, offset?: number): number;
174 writeBigInt64LE(value: bigint, offset?: number): number;
175 writeBigUInt64BE(value: bigint, offset?: number): number;
176 writeBigUInt64LE(value: bigint, offset?: number): number;
177 writeDoubleBE(value: number, offset?: number): number;
178 writeDoubleLE(value: number, offset?: number): number;
179 writeFloatBE(value: number, offset?: number): number;
180 writeFloatLE(value: number, offset?: number): number;
181 writeInt8(value: number, offset?: number): number;
182 writeInt16BE(value: number, offset?: number): number;
183 writeInt16LE(value: number, offset?: number): number;
184 writeInt32BE(value: number, offset?: number): number;
185 writeInt32LE(value: number, offset?: number): number;
186 writeIntBE(value: number, offset?: number, byteLength?: number): number;
187 writeIntLE(value: number, offset?: number, byteLength?: number): number;
188 writeUInt8(value: number, offset?: number): number;
189 writeUInt16BE(value: number, offset?: number): number;
190 writeUInt16LE(value: number, offset?: number): number;
191 writeUInt32BE(value: number, offset?: number): number;
192 writeUInt32LE(value: number, offset?: number): number;
193 writeUIntBE(value: number, offset?: number, byteLength?: number): number;
194 writeUIntLE(value: number, offset?: number, byteLength?: number): number;
195 new (array: Iterable<number>): Buffer;
196 new (
197 arrayBuffer: ArrayBufferLike,
198 byteOffset?: number,
199 length?: number
200 ): Buffer;
201 new (buffer: ArrayBufferView): Buffer;
202 new (size: number): Buffer;
203 new (string: string, encoding?: string): Buffer;
204}
205 
206type FillValue = string | number | ArrayBufferView;
207 
208export function Buffer(value: number): Buffer;
209export function Buffer(value: StringLike, encoding?: string): Buffer;
210export function Buffer(
211 value: ArrayBufferLike,
212 byteOffset?: number,
213 length?: number
214): Buffer;
215export function Buffer(
216 value: Uint8Array | ReadonlyArray<number>,
217 byteOffset?: number,
218 length?: number
219): Buffer;
220export function Buffer(value: StringLike, encoding?: string): Buffer;
221export function Buffer(
222 value: number | BufferSource,
223 encodingOrOffset?: string | number,
224 length?: number
225): Buffer {
226 if (typeof value === 'number') {
227 if (typeof encodingOrOffset === 'string') {
228 throw new ERR_INVALID_ARG_TYPE('string', 'string', value);
229 }
230 return allocUnsafe(value);
231 }
232 
233 return _from(value, encodingOrOffset, length);
234}
235 
236Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
237Object.setPrototypeOf(Buffer, Uint8Array);
238 
239Object.defineProperties(Buffer, {
240 // By default, Node.js allocates Buffers from a shared slab allocation
241 // as a performance optimization. While this is fast, it's not ideal
242 // in our environment as it could potentially be used to leak buffer
243 // data across multiple requests. We always want our Buffer instances
244 // to be distinct allocations. To signal this, we keep our poolSize
245 // always set to 0.
246 poolSize: {
247 enumerable: true,
248 value: 0,
249 writable: false,
250 },
251});
252 
253Object.defineProperties(Buffer.prototype, {
254 parent: {
255 enumerable: true,
256 get() {
257 if (!Buffer.isBuffer(this)) {
258 return void 0;
259 }
260 return this.buffer;
261 },
262 },
263 offset: {
264 enumerable: true,
265 get() {
266 if (!Buffer.isBuffer(this)) {
267 return void 0;
268 }
269 return this.byteOffset;
270 },
271 },
272 [kIsBuffer]: {
273 enumerable: false,
274 configurable: false,
275 value: true,
276 },
277});
278 
279function _from(
280 value: BufferSource,
281 encodingOrOffset?: string | number,
282 length?: number
283): Buffer {
284 if (typeof value === 'string') {
285 return fromString(value, encodingOrOffset as string | undefined) as Buffer;
286 }
287 
288 if (typeof value === 'object' && value != null) {
289 if (isAnyArrayBuffer(value)) {
290 return fromArrayBuffer(
291 value as ArrayBufferLike,
292 encodingOrOffset as number,
293 length
294 ) as Buffer;
295 }
296 
297 const valueOf = value?.valueOf();
298 if (
299 valueOf != null &&
300 valueOf !== value &&
301 (typeof valueOf === 'string' || typeof valueOf === 'object')
302 ) {
303 return _from(valueOf as BufferSource, encodingOrOffset, length);
304 }
305 
306 if (
307 (value as any).length !== undefined ||
308 isAnyArrayBuffer((value as any).buffer)
309 ) {
310 if (typeof (value as any).length !== 'number') {
311 return createBuffer(0);
312 }
313 
314 return fromArrayLike(value as any) as Buffer;
315 }
316 
317 if (
318 (value as any).type === 'Buffer' &&
319 Array.isArray((value as any).data)
320 ) {
321 return fromArrayLike((value as any).data) as Buffer;
322 }
323 
324 const toPrimitive = (value as any)[Symbol.toPrimitive];
325 if (typeof toPrimitive === 'function') {
326 const primitive = toPrimitive('string');
327 if (typeof primitive === 'string') {
328 return fromString(
329 primitive,
330 encodingOrOffset as string | undefined
331 ) as Buffer;
332 }
333 }
334 }
335 
336 throw new ERR_INVALID_ARG_TYPE(
337 'first argument',
338 [
339 'string',
340 'Buffer',
341 'TypedArray',
342 'ArrayBuffer',
343 'SharedArrayBuffer',
344 'Array',
345 'Array-like Object',
346 ],
347 value
348 );
349}
350 
351function from(value: StringLike, encoding?: string): Buffer;
352function from(
353 value: ArrayBufferLike,
354 byteOffset?: number,
355 length?: number
356): Buffer;
357function from(
358 value: Uint8Array | ReadonlyArray<number>,
359 byteOffset?: number,
360 length?: number
361): Buffer;
362function from(
363 value: BufferSource,
364 encodingOrOffset?: string | number,
365 length?: number
366) {
367 return _from(value, encodingOrOffset, length);
368}
369 
370function fromString(string: StringLike, encoding?: string) {
371 if (typeof encoding !== 'string' || encoding === '') {
372 encoding = 'utf8';
373 }
374 const normalizedEncoding = normalizeEncoding(encoding);
375 if (normalizedEncoding === undefined) {
376 throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
377 }
378 
379 const ab = bufferUtil.decodeString(`${string}`, normalizedEncoding);
380 if (ab === undefined) {
381 throw new ERR_INVALID_ARG_VALUE(
382 'string',
383 string,
384 `Unable to decode string using encoding ${encoding}`
385 );
386 }
387 return fromArrayBuffer(ab.buffer, 0, ab.byteLength);
388}
389 
390function fromArrayLike(array: Uint8Array | ReadonlyArray<number>) {
391 const u8 = Uint8Array.from(array);
392 return fromArrayBuffer(u8.buffer, u8.byteOffset, u8.byteLength);
393}
394 
395function fromArrayBuffer(
396 obj: ArrayBufferLike,
397 byteOffset: number,
398 length?: number
399) {
400 // Convert byteOffset to integer
401 if (byteOffset === undefined) {
402 byteOffset = 0;
403 } else {
404 byteOffset = +byteOffset;
405 if (Number.isNaN(byteOffset)) {
406 byteOffset = 0;
407 }
408 }
409 
410 const maxLength = (obj as ArrayBuffer).byteLength - byteOffset;
411 
412 if (maxLength < 0) {
413 throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
414 }
415 
416 if (length === undefined) {
417 length = maxLength;
418 } else {
419 // Convert length to non-negative integer.
420 length = +length;
421 if (length > 0) {
422 if (length > maxLength) {
423 throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
424 }
425 } else {
426 length = 0;
427 }
428 }
429 
430 const buffer = new Uint8Array(obj as ArrayBuffer, byteOffset, length);
431 Object.setPrototypeOf(buffer, Buffer.prototype);
432 return buffer;
433}
434 
435Buffer.from = from;
436 
437function of(...args: number[]) {
438 const buf = Buffer.alloc(args.length);
439 for (let k = 0; k < args.length; k++) buf[k] = args[k]!;
440 return buf;
441}
442 
443Buffer.of = of;
444 
445function alloc(size: number, fill?: FillValue, encoding?: string): Buffer {
446 validateNumber(size, 'size');
447 if (Number.isNaN(size)) {
448 throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
449 }
450 if (size >= kMaxLength) {
451 throw new ERR_OUT_OF_RANGE('size', `0 to ${kMaxLength}`, size);
452 }
453 
454 const buffer = createBuffer(size);
455 if (fill !== undefined) {
456 if (encoding !== undefined) {
457 validateString(encoding, 'encoding');
458 }
459 return buffer.fill(fill as any, encoding);
460 }
461 return buffer;
462}
463 
464Buffer.alloc = alloc;
465 
466function allocUnsafe(size: number): Buffer {
467 return alloc(size);
468}
469 
470Buffer.allocUnsafe = allocUnsafe;
471Buffer.allocUnsafeSlow = allocUnsafe;
472 
473export function SlowBuffer(length: number) {
474 return alloc(+length);
475}
476 
477Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
478Object.setPrototypeOf(SlowBuffer, Uint8Array);
479 
480Buffer.isBuffer = function isBuffer(b: unknown): b is Buffer {
481 return b instanceof Buffer;
482};
483 
484export function compare(a: Buffer | Uint8Array, b: Buffer | Uint8Array) {
485 if (isInstance(a, Uint8Array)) {
486 const buf = a as Uint8Array;
487 a = fromArrayBuffer(buf.buffer, buf.byteOffset, buf.byteLength);
488 }
489 if (isInstance(b, Uint8Array)) {
490 const buf = b as Uint8Array;
491 b = fromArrayBuffer(buf.buffer, buf.byteOffset, buf.byteLength);
492 }
493 if (!Buffer.isBuffer(a)) {
494 throw new ERR_INVALID_ARG_TYPE('a', ['Buffer', 'Uint8Array'], typeof a);
495 }
496 if (!Buffer.isBuffer(b)) {
497 throw new ERR_INVALID_ARG_TYPE('b', ['Buffer', 'Uint8Array'], typeof b);
498 }
499 if (a === b) return 0;
500 
501 return bufferUtil.compare(a, b);
502}
503 
504Buffer.compare = compare;
505 
506export function isEncoding(encoding: unknown): encoding is string {
507 return (
508 typeof encoding === 'string' &&
509 encoding.length !== 0 &&
510 normalizeEncoding(encoding) !== undefined
511 );
512}
513 
514Buffer.isEncoding = isEncoding;
515 
516Buffer.concat = function concat(
517 list: (Buffer | Uint8Array)[],
518 length?: number
519) {
520 if (!Array.isArray(list)) {
521 throw new ERR_INVALID_ARG_TYPE('list', '(Buffer|Uint8Array)[]', list);
522 }
523 
524 if (list.length === 0) return alloc(0);
525 
526 if (length === undefined) {
527 length = 0;
528 for (let i = 0; i < list.length; i++) {
529 if (list[i]!.length !== undefined) {
530 length += list[i]!.length;
531 } else {
532 throw new ERR_INVALID_ARG_TYPE(
533 'list',
534 '(Buffer|Uint8Array)[]',
535 list[i]
536 );
537 }
538 }
539 }
540 validateOffset(length, 'length');
541 
542 const ab = bufferUtil.concat(list, length as number);
543 return fromArrayBuffer(ab.buffer, 0, length);
544};
545 
546function base64ByteLength(str: string) {
547 let len = str.length;
548 if (str.charCodeAt(len - 1) === 0x3d) {
549 len--;
550 }
551 if (len > 1 && str.charCodeAt(len - 1) === 0x3d) len--;
552 
553 // Base64 ratio: 3/4
554 return (len * 3) >>> 2;
555}
556 
557function byteLength(
558 string: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
559 encoding?: string
560) {
561 if (typeof string !== 'string') {
562 if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
563 return string.byteLength;
564 }
565 
566 throw new ERR_INVALID_ARG_TYPE(
567 'string',
568 ['string', 'Buffer', 'ArrayBuffer'],
569 string
570 );
571 }
572 
573 string = `${string}`;
574 const normalizedEncoding = normalizeEncoding(encoding) ?? UTF8;
575 
576 switch (normalizedEncoding) {
577 case ASCII:
578 // Fall through
579 case LATIN1:
580 return (string as string).length;
581 case UTF16LE:
582 return (string as string).length * 2;
583 case BASE64:
584 // Fall through
585 case BASE64URL:
586 return base64ByteLength(string as string);
587 case HEX:
588 return (string as string).length >>> 1;
589 case UTF8:
590 // Fall-through
591 default:
592 return bufferUtil.byteLength(string as string);
593 }
594}
595 
596Buffer.byteLength = byteLength;
597 
598Buffer.prototype.swap16 = function swap16() {
599 const len = this.length;
600 if (len % 2 !== 0) {
601 throw new ERR_INVALID_BUFFER_SIZE(16);
602 }
603 bufferUtil.swap(this, 16);
604 return this;
605};
606 
607Buffer.prototype.swap32 = function swap32() {
608 const len = this.length;
609 if (len % 4 !== 0) {
610 throw new ERR_INVALID_BUFFER_SIZE(32);
611 }
612 bufferUtil.swap(this, 32);
613 return this;
614};
615 
616Buffer.prototype.swap64 = function swap64() {
617 const len = this.length;
618 if (len % 8 !== 0) {
619 throw new ERR_INVALID_BUFFER_SIZE(64);
620 }
621 bufferUtil.swap(this, 64);
622 return this;
623};
624 
625Buffer.prototype.toString = function toString(
626 encoding?: string,
627 start?: number,
628 end?: number
629) {
630 if (arguments.length === 0) {
631 return bufferUtil.toString(this, 0, this.length, UTF8);
632 }
633 
634 const len = this.length;
635 
636 if (start === undefined || start <= 0) {
637 start = 0;
638 } else if (start >= len) {
639 return '';
640 } else {
641 start |= 0;
642 }
643 
644 if (end === undefined || end > len) {
645 end = len;
646 } else {
647 end |= 0;
648 }
649 
650 if ((end as number) <= start) {
651 return '';
652 }
653 
654 const normalizedEncoding = getEncodingOps(encoding);
655 if (normalizedEncoding === undefined) {
656 throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
657 }
658 
659 return bufferUtil.toString(
660 this,
661 start as number,
662 end as number,
663 normalizedEncoding
664 );
665};
666 
667Buffer.prototype.toLocaleString = Buffer.prototype.toString;
668 
669Buffer.prototype.equals = function equals(b: Buffer | Uint8Array) {
670 return compare(this, b) === 0;
671};
672 
673Buffer.prototype.inspect = function inspect(
674 _recurseTimes: number,
675 ctx: InspectOptionsStylized
676) {
677 let str = '';
678 const max = Math.min(this.byteLength, INSPECT_MAX_BYTES);
679 
680 str = this.toString('hex', 0, max)
681 .replace(/(.{2})/g, '$1 ')
682 .trim();
683 const remaining = this.byteLength - max;
684 if (remaining > 0) {
685 str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
686 }
687 // Inspect special properties as well, if possible.
688 if (ctx) {
689 let extras = false;
690 const filter = ctx.showHidden
691 ? internalUtil.ALL_PROPERTIES
692 : internalUtil.ONLY_ENUMERABLE;
693 const obj: Record<PropertyKey, unknown> = { __proto__: null };
694 internalUtil.getOwnNonIndexProperties(this, filter).forEach((key) => {
695 extras = true;
696 obj[key] = this[key];
697 });
698 if (extras) {
699 if (this.length !== 0) str += ', ';
700 // '[Object: null prototype] {'.length === 26
701 // This is guarded with a test.
702 str += utilInspect(obj, {
703 ...ctx,
704 breakLength: Infinity,
705 compact: true,
706 }).slice(27, -2);
707 }
708 }
709 return '<Buffer ' + str + '>';
710};
711 
712if (customInspectSymbol) {
713 Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
714}
715 
716Buffer.prototype.compare = function compare(
717 target: Buffer | Uint8Array,
718 start?: number,
719 end?: number,
720 thisStart?: number,
721 thisEnd?: number
722) {
723 if (isInstance(target, Uint8Array)) {
724 target = fromArrayBuffer(
725 target.buffer,
726 target.byteOffset,
727 target.byteLength
728 );
729 }
730 if (!Buffer.isBuffer(target)) {
731 throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
732 }
733 
734 if (start === undefined) {
735 start = 0;
736 } else {
737 validateOffset(start, 'targetStart', 0, kMaxLength);
738 }
739 
740 if (end === undefined) {
741 end = target.length;
742 } else {
743 validateOffset(end, 'targetEnd', 0, target.length);
744 }
745 
746 if (thisStart === undefined) {
747 thisStart = 0;
748 } else {
749 validateOffset(thisStart as number, 'sourceStart', 0, kMaxLength);
750 }
751 
752 if (thisEnd === undefined) {
753 thisEnd = this.length;
754 } else {
755 validateOffset(thisEnd as number, 'sourceEnd', 0, this.length);
756 }
757 
758 return bufferUtil.compare(this, target, {
759 aStart: thisStart as number,
760 aEnd: thisEnd as number,
761 bStart: start as number,
762 bEnd: end as number,
763 });
764};
765 
766function includes(
767 this: Buffer,
768 val: string | number | Buffer | Uint8Array,
769 byteOffset?: number,
770 encoding?: string
771) {
772 return this.indexOf(val as any, byteOffset, encoding) !== -1;
773}
774 
775Buffer.prototype.includes = includes;
776 
777// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
778// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
779//
780// Arguments:
781// - buffer - a Buffer to search
782// - val - a string, Buffer, or number
783// - byteOffset - an index into `buffer`; will be clamped to an int32
784// - encoding - an optional encoding, relevant if val is a string
785// - dir - true for indexOf, false for lastIndexOf
786function bidirectionalIndexOf(
787 buffer: Uint8Array,
788 val: string | number | Buffer | Uint8Array,
789 byteOffset: number | string | undefined,
790 encoding: string | undefined,
791 dir: boolean | undefined
792) {
793 if (Buffer.isBuffer(val) && !isUint8Array(val)) {
794 throw new ERR_INVALID_ARG_TYPE(
795 'val',
796 ['string', 'number', 'Buffer', 'Uint8Array'],
797 val
798 );
799 }
800 
801 if (typeof byteOffset === 'string') {
802 encoding = byteOffset;
803 byteOffset = undefined;
804 } else if ((byteOffset as number) > 0x7fffffff) {
805 byteOffset = 0x7fffffff;
806 } else if ((byteOffset as number) < -0x80000000) {
807 byteOffset = -0x80000000;
808 }
809 // Coerce to Number. Values like null and [] become 0.
810 byteOffset = +(byteOffset as number);
811 // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
812 if (Number.isNaN(byteOffset)) {
813 byteOffset = dir ? 0 : buffer.length || buffer.byteLength;
814 }
815 dir = !!dir; // Cast to bool.
816 
817 if (typeof val === 'number') {
818 val = (val >>> 0) & 0xff;
819 if (dir) {
820 return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
821 } else {
822 return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
823 }
824 }
825 
826 if (typeof val !== 'string' && !isUint8Array(val) && !Buffer.isBuffer(val)) {
827 throw new ERR_INVALID_ARG_TYPE(
828 'value',
829 ['number', 'string', 'Buffer', 'Uint8Array'],
830 val
831 );
832 }
833 
834 const normalizedEncoding = normalizeEncoding(encoding);
835 if (normalizedEncoding === undefined) {
836 throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
837 }
838 
839 const result = bufferUtil.indexOf(
840 buffer,
841 val,
842 byteOffset,
843 normalizedEncoding,
844 dir
845 );
846 return result == null ? -1 : result;
847}
848 
849Buffer.prototype.indexOf = function indexOf(
850 val: string | number | Buffer | Uint8Array,
851 byteOffset?: number | string,
852 encoding?: string
853) {
854 return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
855};
856 
857Buffer.prototype.lastIndexOf = function lastIndexOf(
858 val: string | number | Buffer | Uint8Array,
859 byteOffset?: number | string,
860 encoding?: string
861) {
862 return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
863};
864 
865Buffer.prototype.asciiSlice = function asciiSlice(start: number, end: number) {
866 validateOffset(start, 'start', 0, this.length);
867 validateOffset(end, 'end', 0, this.length);
868 return bufferUtil.toString(this, start, end, ASCII);
869};
870 
871Buffer.prototype.base64Slice = function base64Slice(
872 start: number,
873 end: number
874) {
875 validateOffset(start, 'start', 0, this.length);
876 validateOffset(end, 'end', 0, this.length);
877 return bufferUtil.toString(this, start, end, BASE64);
878};
879 
880Buffer.prototype.base64urlSlice = function base64urlSlice(
881 start: number,
882 end: number
883) {
884 validateOffset(start, 'start', 0, this.length);
885 validateOffset(end, 'end', 0, this.length);
886 return bufferUtil.toString(this, start, end, BASE64URL);
887};
888 
889Buffer.prototype.hexSlice = function hexSlice(start: number, end: number) {
890 validateOffset(start, 'start', 0, this.length);
891 validateOffset(end, 'end', 0, this.length);
892 return bufferUtil.toString(this, start, end, HEX);
893};
894 
895Buffer.prototype.latin1Slice = function latin1Slice(
896 start: number,
897 end: number
898) {
899 validateOffset(start, 'start', 0, this.length);
900 validateOffset(end, 'end', 0, this.length);
901 return bufferUtil.toString(this, start, end, LATIN1);
902};
903 
904Buffer.prototype.ucs2Slice = function ucs2Slice(start: number, end: number) {
905 validateOffset(start, 'start', 0, this.length);
906 validateOffset(end, 'end', 0, this.length);
907 return bufferUtil.toString(this, start, end, UTF16LE);
908};
909 
910Buffer.prototype.utf8Slice = function utf8Slice(start: number, end: number) {
911 validateOffset(start, 'start', 0, this.length);
912 validateOffset(end, 'end', 0, this.length);
913 return bufferUtil.toString(this, start, end, UTF8);
914};
915 
916Buffer.prototype.asciiWrite = function asciiWrite(
917 string: StringLike,
918 offset?: number,
919 length?: number
920) {
921 offset ??= 0;
922 length ??= this.length;
923 validateOffset(offset as number, 'offset', 0, this.length);
924 validateOffset(length as number, 'length', 0, this.length - offset);
925 return bufferUtil.write(
926 this,
927 `${string}`,
928 offset as number,
929 length as number,
930 ASCII
931 );
932};
933 
934Buffer.prototype.base64Write = function base64Write(
935 string: StringLike,
936 offset?: number,
937 length?: number
938) {
939 offset ??= 0;
940 length ??= this.length;
941 validateOffset(offset as number, 'offset', 0, this.length);
942 validateOffset(length as number, 'length', 0, this.length - offset);
943 return bufferUtil.write(
944 this,
945 `${string}`,
946 offset as number,
947 length as number,
948 BASE64
949 );
950};
951 
952Buffer.prototype.base64urlWrite = function base64urlWrite(
953 string: StringLike,
954 offset?: number,
955 length?: number
956) {
957 offset ??= 0;
958 length ??= this.length;
959 validateOffset(offset as number, 'offset', 0, this.length);
960 validateOffset(length as number, 'length', 0, this.length - offset);
961 return bufferUtil.write(
962 this,
963 `${string}`,
964 offset as number,
965 length as number,
966 BASE64URL
967 );
968};
969 
970Buffer.prototype.hexWrite = function hexWrite(
971 string: StringLike,
972 offset: number,
973 length: number
974) {
975 offset ??= 0;
976 length ??= this.length;
977 validateOffset(offset as number, 'offset', 0, this.length);
978 validateOffset(length as number, 'length', 0, this.length - offset);
979 return bufferUtil.write(
980 this,
981 `${string}`,
982 offset as number,
983 length as number,
984 HEX
985 );
986};
987 
988Buffer.prototype.latin1Write = function latin1Write(
989 string: StringLike,
990 offset: number,
991 length: number
992) {
993 offset ??= 0;
994 length ??= this.length;
995 validateOffset(offset as number, 'offset', 0, this.length);
996 validateOffset(length as number, 'length', 0, this.length - offset);
997 return bufferUtil.write(
998 this,
999 `${string}`,
1000 offset as number,
1001 length as number,
1002 LATIN1
1003 );
1004};
1005 
1006Buffer.prototype.ucs2Write = function ucs2Write(
1007 string: StringLike,
1008 offset: number,
1009 length: number
1010) {
1011 offset ??= 0;
1012 length ??= this.length;
1013 validateOffset(offset as number, 'offset', 0, this.length);
1014 validateOffset(length as number, 'length', 0, this.length - offset);
1015 return bufferUtil.write(
1016 this,
1017 `${string}`,
1018 offset as number,
1019 length as number,
1020 UTF16LE
1021 );
1022};
1023 
1024Buffer.prototype.utf8Write = function utf8Write(
1025 string: StringLike,
1026 offset: number,
1027 length: number
1028) {
1029 offset ??= 0;
1030 length ??= this.length;
1031 validateOffset(offset as number, 'offset', 0, this.length);
1032 validateOffset(length as number, 'length', 0, this.length - offset);
1033 return bufferUtil.write(
1034 this,
1035 `${string}`,
1036 offset as number,
1037 length as number,
1038 UTF8
1039 );
1040};
1041 
1042Buffer.prototype.write = function write(
1043 string: StringLike,
1044 offset?: number | string,
1045 length?: number | string,
1046 encoding?: string
1047) {
1048 string = `${string}`;
1049 if (offset === undefined) {
1050 // Buffer#write(string)
1051 return bufferUtil.write(this, string as string, 0, this.length, UTF8);
1052 }
1053 
1054 if (length === undefined && typeof offset === 'string') {
1055 // Buffer#write(string, encoding)
1056 encoding = offset;
1057 length = this.length;
1058 offset = 0;
1059 } else {
1060 // Buffer#write(string, offset[, length][, encoding])
1061 validateOffset(offset as number, 'offset', 0, this.length);
1062 
1063 const remaining = this.length - (offset as number);
1064 
1065 if (length === undefined) {
1066 length = remaining;
1067 } else if (typeof length === 'string') {
1068 encoding = length;
1069 length = remaining;
1070 } else {
1071 validateOffset(length, 'length', 0, this.length);
1072 if (length > remaining) {
1073 length = remaining;
1074 }
1075 }
1076 }
1077 
1078 if (!encoding) {
1079 return bufferUtil.write(
1080 this,
1081 string as string,
1082 offset as number,
1083 length as number,
1084 UTF8
1085 );
1086 }
1087 
1088 const normalizedEncoding = normalizeEncoding(encoding);
1089 if (normalizedEncoding === undefined) {
1090 throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
1091 }
1092 
1093 return bufferUtil.write(
1094 this,
1095 string as string,
1096 offset as number,
1097 length as number,
1098 normalizedEncoding
1099 );
1100};
1101 
1102Buffer.prototype.toJSON = function toJSON() {
1103 return {
1104 type: 'Buffer',
1105 data: Array.prototype.slice.call(this._arr || this, 0),
1106 };
1107};
1108 
1109Buffer.prototype.slice = function slice(start: number, end?: number) {
1110 const len = this.length;
1111 start = ~~start;
1112 end = end === void 0 ? len : ~~end;
1113 if (start < 0) {
1114 start += len;
1115 if (start < 0) {
1116 start = 0;
1117 }
1118 } else if (start > len) {
1119 start = len;
1120 }
1121 if (end === undefined) {
1122 end = this.byteLength;
1123 } else if (end < 0) {
1124 end += len;
1125 if (end < 0) {
1126 end = 0;
1127 }
1128 } else if (end > len) {
1129 end = len;
1130 }
1131 if ((end as number) < start) {
1132 end = start;
1133 }
1134 const newBuf = this.subarray(start, end);
1135 Object.setPrototypeOf(newBuf, Buffer.prototype);
1136 return newBuf;
1137};
1138 
1139Buffer.prototype.readUintLE = Buffer.prototype.readUIntLE = function readUIntLE(
1140 offset: number,
1141 byteLength: number
1142) {
1143 if (offset === undefined) {
1144 throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
1145 }
1146 switch (byteLength) {
1147 case 1:
1148 return this.readUInt8(offset);
1149 case 2:
1150 return this.readUInt16LE(offset);
1151 case 3:
1152 return readUInt24LE(this, offset);
1153 case 4:
1154 return this.readUInt32LE(offset);
1155 case 5:
1156 return readUInt40LE(this, offset);
1157 case 6:
1158 return readUInt48LE(this, offset);
1159 default:
1160 boundsError(byteLength, 6, 'byteLength');
1161 }
1162};
1163 
1164Buffer.prototype.readUintBE = Buffer.prototype.readUIntBE = function readUIntBE(
1165 offset: number,
1166 byteLength: number
1167) {
1168 if (offset === undefined) {
1169 throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
1170 }
1171 switch (byteLength) {
1172 case 1:
1173 return this.readUInt8(offset);
1174 case 2:
1175 return this.readUInt16BE(offset);
1176 case 3:
1177 return readUInt24BE(this, offset);
1178 case 4:
1179 return this.readUInt32BE(offset);
1180 case 5:
1181 return readUInt40BE(this, offset);
1182 case 6:
1183 return readUInt48BE(this, offset);
1184 default:
1185 boundsError(byteLength, 6, 'byteLength');
1186 }
1187};
1188 
1189Buffer.prototype.readUint8 = Buffer.prototype.readUInt8 = function readUInt8(
1190 offset: number = 0
1191) {
1192 validateOffset(offset, 'offset', 0, this.length);
1193 const val = this[offset];
1194 if (val === undefined) {
1195 boundsError(offset, this.length - 1);
1196 }
1197 
1198 return val;
1199};
1200 
1201Buffer.prototype.readUint16BE = Buffer.prototype.readUInt16BE = readUInt16BE;
1202 
1203Buffer.prototype.readUint16LE = Buffer.prototype.readUInt16LE =
1204 function readUInt16LE(offset: number = 0) {
1205 validateOffset(offset, 'offset', 0, this.length);
1206 const first = this[offset];
1207 const last = this[offset + 1];
1208 if (first === undefined || last === undefined) {
1209 boundsError(offset, this.length - 2);
1210 }
1211 
1212 return first + last * 2 ** 8;
1213 };
1214 
1215Buffer.prototype.readUint32LE = Buffer.prototype.readUInt32LE =
1216 function readUInt32LE(this: Buffer, offset: number = 0) {
1217 validateOffset(offset, 'offset', 0, this.length);
1218 const first = this[offset];
1219 const last = this[offset + 3];
1220 if (first === undefined || last === undefined) {
1221 boundsError(offset, this.length - 4);
1222 }
1223 
1224 return (
1225 first +
1226 this[++offset]! * 2 ** 8 +
1227 this[++offset]! * 2 ** 16 +
1228 last * 2 ** 24
1229 );
1230 };
1231 
1232Buffer.prototype.readUint32BE = Buffer.prototype.readUInt32BE = readUInt32BE;
1233 
1234Buffer.prototype.readBigUint64LE = Buffer.prototype.readBigUInt64LE =
1235 function readBigUInt64LE(this: Buffer, offset: number = 0) {
1236 offset = offset >>> 0;
1237 validateOffset(offset, 'offset', 0, this.length);
1238 const first = this[offset];
1239 const last = this[offset + 7];
1240 if (first === undefined || last === undefined) {
1241 boundsError(offset, this.length - 8);
1242 }
1243 const lo =
1244 first +
1245 this[++offset]! * 2 ** 8 +
1246 this[++offset]! * 2 ** 16 +
1247 this[++offset]! * 2 ** 24;
1248 const hi =
1249 this[++offset]! +
1250 this[++offset]! * 2 ** 8 +
1251 this[++offset]! * 2 ** 16 +
1252 last * 2 ** 24;
1253 return BigInt(lo) + (BigInt(hi) << BigInt(32));
1254 };
1255 
1256Buffer.prototype.readBigUint64BE = Buffer.prototype.readBigUInt64BE =
1257 function readBigUInt64BE(this: Buffer, offset: number = 0) {
1258 offset = offset >>> 0;
1259 validateOffset(offset, 'offset', 0, this.length);
1260 const first = this[offset];
1261 const last = this[offset + 7];
1262 if (first === undefined || last === undefined) {
1263 boundsError(offset, this.length - 8);
1264 }
1265 const hi =
1266 first * 2 ** 24 +
1267 this[++offset]! * 2 ** 16 +
1268 this[++offset]! * 2 ** 8 +
1269 this[++offset]!;
1270 const lo =
1271 this[++offset]! * 2 ** 24 +
1272 this[++offset]! * 2 ** 16 +
1273 this[++offset]! * 2 ** 8 +
1274 last;
1275 return (BigInt(hi) << BigInt(32)) + BigInt(lo);
1276 };
1277 
1278Buffer.prototype.readIntLE = function readIntLE(
1279 offset: number,
1280 byteLength: number
1281) {
1282 if (offset === undefined) {
1283 throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
1284 }
1285 switch (byteLength) {
1286 case 1:
1287 return this.readInt8(offset);
1288 case 2:
1289 return this.readInt16LE(offset);
1290 case 3:
1291 return readInt24LE(this, offset);
1292 case 4:
1293 return this.readInt32LE(offset);
1294 case 5:
1295 return readInt40LE(this, offset);
1296 case 6:
1297 return readInt48LE(this, offset);
1298 default:
1299 boundsError(byteLength, 6, 'byteLength');
1300 }
1301};
1302 
1303Buffer.prototype.readIntBE = function readIntBE(
1304 offset: number,
1305 byteLength: number
1306) {
1307 if (offset === undefined) {
1308 throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
1309 }
1310 switch (byteLength) {
1311 case 1:
1312 return this.readInt8(offset);
1313 case 2:
1314 return this.readInt16BE(offset);
1315 case 3:
1316 return readInt24BE(this, offset);
1317 case 4:
1318 return this.readInt32BE(offset);
1319 case 5:
1320 return readInt40BE(this, offset);
1321 case 6:
1322 return readInt48BE(this, offset);
1323 default:
1324 boundsError(byteLength, 6, 'byteLength');
1325 }
1326};
1327 
1328Buffer.prototype.readInt8 = function readInt8(offset: number = 0) {
1329 validateOffset(offset, 'offset', 0, this.length);
1330 const val = this[offset];
1331 if (val === undefined) {
1332 boundsError(offset, this.length - 1);
1333 }
1334 
1335 return val | ((val & (2 ** 7)) * 0x1fffffe);
1336};
1337 
1338Buffer.prototype.readInt16LE = function readInt16LE(offset: number = 0) {
1339 validateOffset(offset, 'offset', 0, this.length);
1340 const first = this[offset];
1341 const last = this[offset + 1];
1342 if (first === undefined || last === undefined) {
1343 boundsError(offset, this.length - 2);
1344 }
1345 
1346 const val = first + last * 2 ** 8;
1347 return val | ((val & (2 ** 15)) * 0x1fffe);
1348};
1349 
1350Buffer.prototype.readInt16BE = function readInt16BE(offset: number = 0) {
1351 validateOffset(offset, 'offset', 0, this.length);
1352 const first = this[offset];
1353 const last = this[offset + 1];
1354 if (first === undefined || last === undefined) {
1355 boundsError(offset, this.length - 2);
1356 }
1357 
1358 const val = first * 2 ** 8 + last;
1359 return val | ((val & (2 ** 15)) * 0x1fffe);
1360};
1361 
1362Buffer.prototype.readInt32LE = function readInt32LE(offset: number = 0) {
1363 validateOffset(offset, 'offset', 0, this.length);
1364 const first = this[offset];
1365 const last = this[offset + 3];
1366 if (first === undefined || last === undefined) {
1367 boundsError(offset, this.length - 4);
1368 }
1369 
1370 return (
1371 first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + (last << 24)
1372 ); // Overflow
1373};
1374 
1375Buffer.prototype.readInt32BE = function readInt32BE(offset: number = 0) {
1376 validateOffset(offset, 'offset', 0, this.length);
1377 const first = this[offset];
1378 const last = this[offset + 3];
1379 if (first === undefined || last === undefined) {
1380 boundsError(offset, this.length - 4);
1381 }
1382 
1383 return (
1384 (first << 24) + // Overflow
1385 this[++offset] * 2 ** 16 +
1386 this[++offset] * 2 ** 8 +
1387 last
1388 );
1389};
1390 
1391Buffer.prototype.readBigInt64LE = function readBigInt64LE(
1392 this: Buffer,
1393 offset: number = 0
1394) {
1395 offset = offset >>> 0;
1396 validateOffset(offset, 'offset', 0, this.length);
1397 const first = this[offset];
1398 const last = this[offset + 7];
1399 if (first === undefined || last === undefined) {
1400 boundsError(offset, this.length - 8);
1401 }
1402 const val =
1403 this[offset + 4]! +
1404 this[offset + 5]! * 2 ** 8 +
1405 this[offset + 6]! * 2 ** 16 +
1406 (last << 24);
1407 return (
1408 (BigInt(val) << BigInt(32)) +
1409 BigInt(
1410 first +
1411 this[++offset]! * 2 ** 8 +
1412 this[++offset]! * 2 ** 16 +
1413 this[++offset]! * 2 ** 24
1414 )
1415 );
1416};
1417 
1418Buffer.prototype.readBigInt64BE = function readBigInt64BE(
1419 this: Buffer,
1420 offset: number = 0
1421) {
1422 offset = offset >>> 0;
1423 validateOffset(offset, 'offset', 0, this.length);
1424 const first = this[offset];
1425 const last = this[offset + 7];
1426 if (first === undefined || last === undefined) {
1427 boundsError(offset, this.length - 8);
1428 }
1429 const val =
1430 (first << 24) +
1431 this[++offset]! * 2 ** 16 +
1432 this[++offset]! * 2 ** 8 +
1433 this[++offset]!;
1434 return (
1435 (BigInt(val) << BigInt(32)) +
1436 BigInt(
1437 this[++offset]! * 2 ** 24 +
1438 this[++offset]! * 2 ** 16 +
1439 this[++offset]! * 2 ** 8 +
1440 last
1441 )
1442 );
1443};
1444 
1445Buffer.prototype.readFloatLE = function readFloatLE(offset: number = 0) {
1446 return bigEndian
1447 ? readFloatBackwards(this, offset)
1448 : readFloatForwards(this, offset);
1449};
1450 
1451Buffer.prototype.readFloatBE = function readFloatBE(offset: number = 0) {
1452 return bigEndian
1453 ? readFloatForwards(this, offset)
1454 : readFloatBackwards(this, offset);
1455};
1456 
1457Buffer.prototype.readDoubleLE = function readDoubleLE(offset: number = 0) {
1458 return bigEndian
1459 ? readDoubleBackwards(this, offset)
1460 : readDoubleForwards(this, offset);
1461};
1462 
1463Buffer.prototype.readDoubleBE = function readDoubleBE(offset: number = 0) {
1464 return bigEndian
1465 ? readDoubleForwards(this, offset)
1466 : readDoubleBackwards(this, offset);
1467};
1468 
1469Buffer.prototype.writeUintLE = Buffer.prototype.writeUIntLE =
1470 function writeUIntLE(value: number, offset: number, byteLength: number) {
1471 switch (byteLength) {
1472 case 1:
1473 return writeU_Int8(this, value, offset, 0, 0xff);
1474 case 2:
1475 return writeU_Int16LE(this, value, offset, 0, 0xffff);
1476 case 3:
1477 return writeU_Int24LE(this, value, offset, 0, 0xffffff);
1478 case 4:
1479 return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
1480 case 5:
1481 return writeU_Int40LE(this, value, offset, 0, 0xffffffffff);
1482 case 6:
1483 return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff);
1484 default:
1485 boundsError(byteLength, 6, 'byteLength');
1486 }
1487 };
1488 
1489Buffer.prototype.writeUintBE = Buffer.prototype.writeUIntBE =
1490 function writeUIntBE(value: number, offset: number, byteLength: number) {
1491 switch (byteLength) {
1492 case 1:
1493 return writeU_Int8(this, value, offset, 0, 0xff);
1494 case 2:
1495 return writeU_Int16BE(this, value, offset, 0, 0xffff);
1496 case 3:
1497 return writeU_Int24BE(this, value, offset, 0, 0xffffff);
1498 case 4:
1499 return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
1500 case 5:
1501 return writeU_Int40BE(this, value, offset, 0, 0xffffffffff);
1502 case 6:
1503 return writeU_Int48BE(this, value, offset, 0, 0xffffffffffff);
1504 default:
1505 boundsError(byteLength, 6, 'byteLength');
1506 }
1507 };
1508 
1509Buffer.prototype.writeUint8 = Buffer.prototype.writeUInt8 = function writeUInt8(
1510 value: number,
1511 offset: number = 0
1512) {
1513 return writeU_Int8(this, value, offset, 0, 0xff);
1514};
1515 
1516Buffer.prototype.writeUint16LE = Buffer.prototype.writeUInt16LE =
1517 function writeUInt16LE(value: number, offset: number = 0) {
1518 return writeU_Int16LE(this, value, offset, 0, 0xffff);
1519 };
1520 
1521Buffer.prototype.writeUint16BE = Buffer.prototype.writeUInt16BE =
1522 function writeUInt16BE(value: number, offset: number = 0) {
1523 return writeU_Int16BE(this, value, offset, 0, 0xffff);
1524 };
1525 
1526Buffer.prototype.writeUint32LE = Buffer.prototype.writeUInt32LE =
1527 function writeUInt32LE(value: number, offset: number = 0) {
1528 return _writeUInt32LE(this, value, offset, 0, 0xffffffff);
1529 };
1530 
1531Buffer.prototype.writeUint32BE = Buffer.prototype.writeUInt32BE =
1532 function writeUInt32BE(value: number, offset: number = 0) {
1533 return _writeUInt32BE(this, value, offset, 0, 0xffffffff);
1534 };
1535 
1536function wrtBigUInt64LE(
1537 buf: Buffer,
1538 value: bigint,
1539 offset: number,
1540 min: bigint,
1541 max: bigint
1542) {
1543 checkIntBI(value, min, max, buf, offset, 7);
1544 let lo = Number(value & BigInt(4294967295));
1545 buf[offset++] = lo;
1546 lo = lo >> 8;
1547 buf[offset++] = lo;
1548 lo = lo >> 8;
1549 buf[offset++] = lo;
1550 lo = lo >> 8;
1551 buf[offset++] = lo;
1552 let hi = Number((value >> BigInt(32)) & BigInt(4294967295));
1553 buf[offset++] = hi;
1554 hi = hi >> 8;
1555 buf[offset++] = hi;
1556 hi = hi >> 8;
1557 buf[offset++] = hi;
1558 hi = hi >> 8;
1559 buf[offset++] = hi;
1560 return offset;
1561}
1562 
1563function wrtBigUInt64BE(
1564 buf: Buffer,
1565 value: bigint,
1566 offset: number,
1567 min: bigint,
1568 max: bigint
1569) {
1570 checkIntBI(value, min, max, buf, offset, 7);
1571 let lo = Number(value & BigInt(4294967295));
1572 buf[offset + 7] = lo;
1573 lo = lo >> 8;
1574 buf[offset + 6] = lo;
1575 lo = lo >> 8;
1576 buf[offset + 5] = lo;
1577 lo = lo >> 8;
1578 buf[offset + 4] = lo;
1579 let hi = Number((value >> BigInt(32)) & BigInt(4294967295));
1580 buf[offset + 3] = hi;
1581 hi = hi >> 8;
1582 buf[offset + 2] = hi;
1583 hi = hi >> 8;
1584 buf[offset + 1] = hi;
1585 hi = hi >> 8;
1586 buf[offset] = hi;
1587 return offset + 8;
1588}
1589 
1590Buffer.prototype.writeBigUint64LE = Buffer.prototype.writeBigUInt64LE =
1591 function writeBigUInt64LE(this: Buffer, value: bigint, offset: number = 0) {
1592 return wrtBigUInt64LE(this, value, offset, 0n, 0xffffffffffffffffn);
1593 };
1594 
1595Buffer.prototype.writeBigUint64BE = Buffer.prototype.writeBigUInt64BE =
1596 function writeBigUInt64BE(this: Buffer, value: bigint, offset: number = 0) {
1597 return wrtBigUInt64BE(this, value, offset, 0n, 0xffffffffffffffffn);
1598 };
1599 
1600Buffer.prototype.writeIntLE = function writeIntLE(
1601 value: number,
1602 offset: number,
1603 byteLength: number
1604) {
1605 switch (byteLength) {
1606 case 1:
1607 return writeU_Int8(this, value, offset, -0x80, 0x7f);
1608 case 2:
1609 return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
1610 case 3:
1611 return writeU_Int24LE(this, value, offset, -0x800000, 0x7fffff);
1612 case 4:
1613 return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
1614 case 5:
1615 return writeU_Int40LE(this, value, offset, -0x8000000000, 0x7fffffffff);
1616 case 6:
1617 return writeU_Int48LE(
1618 this,
1619 value,
1620 offset,
1621 -0x800000000000,
1622 0x7fffffffffff
1623 );
1624 default:
1625 boundsError(byteLength, 6, 'byteLength');
1626 }
1627};
1628 
1629Buffer.prototype.writeIntBE = function writeIntBE(
1630 value: number,
1631 offset: number,
1632 byteLength: number
1633) {
1634 switch (byteLength) {
1635 case 1:
1636 return writeU_Int8(this, value, offset, -0x80, 0x7f);
1637 case 2:
1638 return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
1639 case 3:
1640 return writeU_Int24BE(this, value, offset, -0x800000, 0x7fffff);
1641 case 4:
1642 return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
1643 case 5:
1644 return writeU_Int40BE(this, value, offset, -0x8000000000, 0x7fffffffff);
1645 case 6:
1646 return writeU_Int48BE(
1647 this,
1648 value,
1649 offset,
1650 -0x800000000000,
1651 0x7fffffffffff
1652 );
1653 default:
1654 boundsError(byteLength, 6, 'byteLength');
1655 }
1656};
1657 
1658Buffer.prototype.writeInt8 = function writeInt8(
1659 value: number,
1660 offset: number = 0
1661) {
1662 return writeU_Int8(this, value, offset, -0x80, 0x7f);
1663};
1664 
1665Buffer.prototype.writeInt16LE = function writeInt16LE(
1666 value: number,
1667 offset: number = 0
1668) {
1669 return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
1670};
1671 
1672Buffer.prototype.writeInt16BE = function writeInt16BE(
1673 value: number,
1674 offset: number = 0
1675) {
1676 return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
1677};
1678 
1679Buffer.prototype.writeInt32LE = function writeInt32LE(
1680 value: number,
1681 offset: number = 0
1682) {
1683 return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
1684};
1685 
1686Buffer.prototype.writeInt32BE = function writeInt32BE(
1687 value: number,
1688 offset: number = 0
1689) {
1690 return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
1691};
1692 
1693Buffer.prototype.writeBigInt64LE = function writeBigInt64LE(
1694 this: Buffer,
1695 value: bigint,
1696 offset: number = 0
1697) {
1698 return wrtBigUInt64LE(
1699 this,
1700 value,
1701 offset,
1702 -0x8000000000000000n,
1703 0x7fffffffffffffffn
1704 );
1705};
1706 
1707Buffer.prototype.writeBigInt64BE = function writeBigInt64BE(
1708 this: Buffer,
1709 value: bigint,
1710 offset: number = 0
1711) {
1712 return wrtBigUInt64BE(
1713 this,
1714 value,
1715 offset,
1716 -0x8000000000000000n,
1717 0x7fffffffffffffffn
1718 );
1719};
1720 
1721Buffer.prototype.writeFloatLE = function writeFloatLE(
1722 value: number,
1723 offset: number
1724) {
1725 return bigEndian
1726 ? writeFloatBackwards(this, value, offset)
1727 : writeFloatForwards(this, value, offset);
1728};
1729 
1730Buffer.prototype.writeFloatBE = function writeFloatBE(
1731 value: number,
1732 offset: number
1733) {
1734 return bigEndian
1735 ? writeFloatForwards(this, value, offset)
1736 : writeFloatBackwards(this, value, offset);
1737};
1738 
1739Buffer.prototype.writeDoubleLE = function writeDoubleLE(
1740 value: number,
1741 offset: number
1742) {
1743 return bigEndian
1744 ? writeDoubleBackwards(this, value, offset)
1745 : writeDoubleForwards(this, value, offset);
1746};
1747 
1748Buffer.prototype.writeDoubleBE = function writeDoubleBE(
1749 value: number,
1750 offset: number
1751) {
1752 return bigEndian
1753 ? writeDoubleForwards(this, value, offset)
1754 : writeDoubleBackwards(this, value, offset);
1755};
1756 
1757Buffer.prototype.copy = function copy(
1758 target: Buffer | Uint8Array,
1759 targetStart?: number,
1760 sourceStart?: number,
1761 sourceEnd?: number
1762) {
1763 if (!isUint8Array(target)) {
1764 throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
1765 }
1766 
1767 targetStart = toInteger(targetStart, 0);
1768 if ((targetStart as number) < 0) {
1769 throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
1770 }
1771 
1772 sourceStart = toInteger(sourceStart, 0);
1773 if ((sourceStart as number) < 0) {
1774 throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart);
1775 }
1776 if ((sourceStart as number) >= MAX_UINT32) {
1777 throw new ERR_OUT_OF_RANGE('sourceStart', `< ${MAX_UINT32}`, sourceStart);
1778 }
1779 
1780 sourceEnd ??= this.length;
1781 sourceEnd = toInteger(sourceEnd, 0);
1782 if ((sourceEnd as number) < 0) {
1783 throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
1784 }
1785 if ((sourceEnd as number) >= MAX_UINT32) {
1786 throw new ERR_OUT_OF_RANGE('sourceEnd', `< ${MAX_UINT32}`, sourceEnd);
1787 }
1788 
1789 if ((targetStart as number) >= target.length) {
1790 return 0;
1791 }
1792 
1793 if (
1794 (sourceEnd as number) > 0 &&
1795 (sourceEnd as number) < (sourceStart as number)
1796 ) {
1797 sourceEnd = sourceStart;
1798 }
1799 if (sourceEnd === sourceStart) {
1800 return 0;
1801 }
1802 if (target.length === 0 || this.length === 0) {
1803 return 0;
1804 }
1805 
1806 if ((sourceEnd as number) > this.length) {
1807 sourceEnd = this.length;
1808 }
1809 
1810 if (
1811 target.length - (targetStart as number) <
1812 (sourceEnd as number) - (sourceStart as number)
1813 ) {
1814 sourceEnd =
1815 target.length - (targetStart as number) + (sourceStart as number);
1816 }
1817 
1818 const len = (sourceEnd as number) - (sourceStart as number);
1819 if (this === target) {
1820 this.copyWithin(
1821 targetStart as number,
1822 sourceStart as number,
1823 sourceEnd as number
1824 );
1825 } else {
1826 const sub = this.subarray(sourceStart, sourceEnd);
1827 target.set(sub, targetStart);
1828 }
1829 
1830 return len;
1831};
1832 
1833Buffer.prototype.fill = function fill(
1834 val: string | number | Buffer | Uint8Array,
1835 start?: number | string,
1836 end?: number,
1837 encoding?: string
1838) {
1839 let normalizedEncoding: Encoding | undefined;
1840 if (typeof val === 'string') {
1841 if (typeof start === 'string') {
1842 encoding = start;
1843 start = 0;
1844 end = this.length;
1845 } else if (typeof end === 'string') {
1846 encoding = end;
1847 end = this.length;
1848 }
1849 normalizedEncoding = normalizeEncoding(encoding);
1850 if (normalizedEncoding === undefined) {
1851 throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
1852 }
1853 if (val.length === 1) {
1854 const code = val.charCodeAt(0);
1855 if ((encoding === 'utf8' && code < 128) || encoding === 'latin1') {
1856 val = code;
1857 }
1858 }
1859 }
1860 
1861 if (start !== undefined) {
1862 validateNumber(start, 'start');
1863 }
1864 if (end !== undefined) {
1865 validateNumber(end, 'end');
1866 }
1867 
1868 if ((end as number) < 0 || (end as number) > this.length) {
1869 throw new ERR_OUT_OF_RANGE('end', `0 to ${this.length}`, end);
1870 }
1871 if (
1872 (start as number) < 0 ||
1873 this.length < (start as number) ||
1874 this.length < (end as number)
1875 ) {
1876 throw new ERR_OUT_OF_RANGE('start', '0 to end', start);
1877 }
1878 if ((end as number) <= (start as number)) {
1879 return this;
1880 }
1881 start = (start as number) >>> 0;
1882 end = end === void 0 ? this.length : end >>> 0;
1883 
1884 if (typeof val === 'string') {
1885 bufferUtil.fillImpl(
1886 this,
1887 val as string,
1888 start as number,
1889 end as number,
1890 normalizedEncoding
1891 );
1892 return this;
1893 }
1894 
1895 if (isArrayBufferView(val)) {
1896 if ((val as ArrayBufferView).byteLength === 0) {
1897 throw new ERR_INVALID_ARG_VALUE('value', 'zero-length');
1898 }
1899 bufferUtil.fillImpl(
1900 this,
1901 val as ArrayBufferView,
1902 start as number,
1903 end as number
1904 );
1905 return this;
1906 }
1907 
1908 if (typeof val === 'number') {
1909 val = val & 255;
1910 } else if (typeof val === 'boolean') {
1911 val = Number(val);
1912 }
1913 val ??= 0;
1914 
1915 Uint8Array.prototype.fill.call(this, val as number, start, end);
1916 
1917 return this;
1918};
1919 
1920function checkBounds(buf: Buffer, offset: number, byteLength2: number) {
1921 validateOffset(offset, 'offset', 0, buf.length);
1922 if (buf[offset] === undefined || buf[offset + byteLength2] === undefined) {
1923 boundsError(offset, buf.length - (byteLength2 + 1));
1924 }
1925}
1926 
1927function checkIntBI(
1928 value: bigint | number,
1929 min: bigint | number,
1930 max: bigint | number,
1931 buf: Buffer,
1932 offset: number,
1933 byteLength2: number
1934) {
1935 if (value > max || value < min) {
1936 const n = typeof min === 'bigint' ? 'n' : '';
1937 let range;
1938 if (byteLength2 > 3) {
1939 if (min === 0 || min === BigInt(0)) {
1940 range = `>= 0${n} and < 2${n} ** ${(byteLength2 + 1) * 8}${n}`;
1941 } else {
1942 range = `>= -(2${n} ** ${(byteLength2 + 1) * 8 - 1}${n}) and < 2 ** ${
1943 (byteLength2 + 1) * 8 - 1
1944 }${n}`;
1945 }
1946 } else {
1947 range = `>= ${min}${n} and <= ${max}${n}`;
1948 }
1949 throw new ERR_OUT_OF_RANGE('value', range, value);
1950 }
1951 checkBounds(buf, offset, byteLength2);
1952}
1953 
1954function isInstance(obj: unknown, type: Function) {
1955 return (
1956 obj instanceof type ||
1957 (obj != null &&
1958 obj.constructor != null &&
1959 obj.constructor.name != null &&
1960 obj.constructor.name === type.name)
1961 );
1962}
1963 
1964function readUInt48LE(buf: Buffer | Uint8Array, offset: number = 0) {
1965 validateOffset(offset, 'offset', 0, buf.length);
1966 const first = buf[offset];
1967 const last = buf[offset + 5];
1968 if (first === undefined || last === undefined) {
1969 boundsError(offset, buf.length - 6);
1970 }
1971 
1972 return (
1973 first +
1974 buf[++offset]! * 2 ** 8 +
1975 buf[++offset]! * 2 ** 16 +
1976 buf[++offset]! * 2 ** 24 +
1977 (buf[++offset]! + last * 2 ** 8) * 2 ** 32
1978 );
1979}
1980 
1981function readUInt40LE(buf: Buffer | Uint8Array, offset: number = 0) {
1982 validateOffset(offset, 'offset', 0, buf.length);
1983 const first = buf[offset];
1984 const last = buf[offset + 4];
1985 if (first === undefined || last === undefined) {
1986 boundsError(offset, buf.length - 5);
1987 }
1988 
1989 return (
1990 first +
1991 buf[++offset]! * 2 ** 8 +
1992 buf[++offset]! * 2 ** 16 +
1993 buf[++offset]! * 2 ** 24 +
1994 last * 2 ** 32
1995 );
1996}
1997 
1998function readUInt24LE(buf: Buffer | Uint8Array, offset: number = 0) {
1999 validateOffset(offset, 'offset', 0, buf.length);
2000 const first = buf[offset];
2001 const last = buf[offset + 2];
2002 if (first === undefined || last === undefined) {
2003 boundsError(offset, buf.length - 3);
2004 }
2005 
2006 return first + buf[++offset]! * 2 ** 8 + last * 2 ** 16;
2007}
2008 
2009function readUInt48BE(buf: Buffer | Uint8Array, offset: number = 0) {
2010 validateOffset(offset, 'offset', 0, buf.length);
2011 const first = buf[offset];
2012 const last = buf[offset + 5];
2013 if (first === undefined || last === undefined) {
2014 boundsError(offset, buf.length - 6);
2015 }
2016 
2017 return (
2018 (first * 2 ** 8 + buf[++offset]!) * 2 ** 32 +
2019 buf[++offset]! * 2 ** 24 +
2020 buf[++offset]! * 2 ** 16 +
2021 buf[++offset]! * 2 ** 8 +
2022 last
2023 );
2024}
2025 
2026function readUInt40BE(buf: Buffer | Uint8Array, offset: number = 0) {
2027 validateOffset(offset, 'offset', 0, buf.length);
2028 const first = buf[offset];
2029 const last = buf[offset + 4];
2030 if (first === undefined || last === undefined) {
2031 boundsError(offset, buf.length - 5);
2032 }
2033 
2034 return (
2035 first * 2 ** 32 +
2036 buf[++offset]! * 2 ** 24 +
2037 buf[++offset]! * 2 ** 16 +
2038 buf[++offset]! * 2 ** 8 +
2039 last
2040 );
2041}
2042 
2043function readUInt24BE(buf: Buffer | Uint8Array, offset: number = 0) {
2044 validateOffset(offset, 'offset', 0, buf.length);
2045 const first = buf[offset];
2046 const last = buf[offset + 2];
2047 if (first === undefined || last === undefined) {
2048 boundsError(offset, buf.length - 3);
2049 }
2050 
2051 return first * 2 ** 16 + buf[++offset]! * 2 ** 8 + last;
2052}
2053 
2054function readUInt16BE(this: Buffer, offset: number = 0) {
2055 validateOffset(offset, 'offset', 0, this.length);
2056 const first = this[offset];
2057 const last = this[offset + 1];
2058 if (first === undefined || last === undefined) {
2059 boundsError(offset, this.length - 2);
2060 }
2061 
2062 return first * 2 ** 8 + last;
2063}
2064 
2065function readUInt32BE(this: Buffer, offset: number = 0) {
2066 validateOffset(offset, 'offset', 0, this.length);
2067 const first = this[offset];
2068 const last = this[offset + 3];
2069 if (first === undefined || last === undefined) {
2070 boundsError(offset, this.length - 4);
2071 }
2072 
2073 return (
2074 first * 2 ** 24 +
2075 this[++offset]! * 2 ** 16 +
2076 this[++offset]! * 2 ** 8 +
2077 last
2078 );
2079}
2080 
2081function readDoubleBackwards(buffer: Buffer | Uint8Array, offset: number = 0) {
2082 validateOffset(offset, 'offset', 0, buffer.length);
2083 const first = buffer[offset];
2084 const last = buffer[offset + 7];
2085 if (first === undefined || last === undefined) {
2086 boundsError(offset, buffer.length - 8);
2087 }
2088 
2089 uInt8Float64Array[7] = first;
2090 uInt8Float64Array[6] = buffer[++offset]!;
2091 uInt8Float64Array[5] = buffer[++offset]!;
2092 uInt8Float64Array[4] = buffer[++offset]!;
2093 uInt8Float64Array[3] = buffer[++offset]!;
2094 uInt8Float64Array[2] = buffer[++offset]!;
2095 uInt8Float64Array[1] = buffer[++offset]!;
2096 uInt8Float64Array[0] = last;
2097 return float64Array[0];
2098}
2099 
2100function readDoubleForwards(buffer: Buffer | Uint8Array, offset: number = 0) {
2101 validateOffset(offset, 'offset', 0, buffer.length);
2102 const first = buffer[offset];
2103 const last = buffer[offset + 7];
2104 if (first === undefined || last === undefined) {
2105 boundsError(offset, buffer.length - 8);
2106 }
2107 
2108 uInt8Float64Array[0] = first;
2109 uInt8Float64Array[1] = buffer[++offset]!;
2110 uInt8Float64Array[2] = buffer[++offset]!;
2111 uInt8Float64Array[3] = buffer[++offset]!;
2112 uInt8Float64Array[4] = buffer[++offset]!;
2113 uInt8Float64Array[5] = buffer[++offset]!;
2114 uInt8Float64Array[6] = buffer[++offset]!;
2115 uInt8Float64Array[7] = last;
2116 return float64Array[0];
2117}
2118 
2119function writeDoubleForwards(
2120 buffer: Buffer | Uint8Array,
2121 val: number,
2122 offset: number = 0
2123) {
2124 val = +val;
2125 checkBounds(buffer as any, offset, 7);
2126 
2127 float64Array[0] = val;
2128 buffer[offset++] = uInt8Float64Array[0]!;
2129 buffer[offset++] = uInt8Float64Array[1]!;
2130 buffer[offset++] = uInt8Float64Array[2]!;
2131 buffer[offset++] = uInt8Float64Array[3]!;
2132 buffer[offset++] = uInt8Float64Array[4]!;
2133 buffer[offset++] = uInt8Float64Array[5]!;
2134 buffer[offset++] = uInt8Float64Array[6]!;
2135 buffer[offset++] = uInt8Float64Array[7]!;
2136 return offset;
2137}
2138 
2139function writeDoubleBackwards(
2140 buffer: Buffer | Uint8Array,
2141 val: number,
2142 offset: number = 0
2143) {
2144 val = +val;
2145 checkBounds(buffer as any, offset, 7);
2146 
2147 float64Array[0] = val;
2148 buffer[offset++] = uInt8Float64Array[7]!;
2149 buffer[offset++] = uInt8Float64Array[6]!;
2150 buffer[offset++] = uInt8Float64Array[5]!;
2151 buffer[offset++] = uInt8Float64Array[4]!;
2152 buffer[offset++] = uInt8Float64Array[3]!;
2153 buffer[offset++] = uInt8Float64Array[2]!;
2154 buffer[offset++] = uInt8Float64Array[1]!;
2155 buffer[offset++] = uInt8Float64Array[0]!;
2156 return offset;
2157}
2158 
2159function readFloatBackwards(buffer: Buffer | Uint8Array, offset: number = 0) {
2160 validateOffset(offset, 'offset', 0, buffer.length);
2161 const first = buffer[offset];
2162 const last = buffer[offset + 3];
2163 if (first === undefined || last === undefined) {
2164 boundsError(offset, buffer.length - 4);
2165 }
2166 
2167 uInt8Float32Array[3] = first;
2168 uInt8Float32Array[2] = buffer[++offset]!;
2169 uInt8Float32Array[1] = buffer[++offset]!;
2170 uInt8Float32Array[0] = last;
2171 return float32Array[0];
2172}
2173 
2174function readFloatForwards(buffer: Buffer | Uint8Array, offset: number = 0) {
2175 validateOffset(offset, 'offset', 0, buffer.length);
2176 const first = buffer[offset];
2177 const last = buffer[offset + 3];
2178 if (first === undefined || last === undefined) {
2179 boundsError(offset, buffer.length - 4);
2180 }
2181 
2182 uInt8Float32Array[0] = first;
2183 uInt8Float32Array[1] = buffer[++offset]!;
2184 uInt8Float32Array[2] = buffer[++offset]!;
2185 uInt8Float32Array[3] = last;
2186 return float32Array[0];
2187}
2188 
2189function writeFloatForwards(
2190 buffer: Buffer | Uint8Array,
2191 val: number,
2192 offset: number = 0
2193) {
2194 val = +val;
2195 checkBounds(buffer as any, offset, 3);
2196 
2197 float32Array[0] = val;
2198 buffer[offset++] = uInt8Float32Array[0]!;
2199 buffer[offset++] = uInt8Float32Array[1]!;
2200 buffer[offset++] = uInt8Float32Array[2]!;
2201 buffer[offset++] = uInt8Float32Array[3]!;
2202 return offset;
2203}
2204 
2205function writeFloatBackwards(
2206 buffer: Buffer | Uint8Array,
2207 val: number,
2208 offset: number = 0
2209) {
2210 val = +val;
2211 checkBounds(buffer as any, offset, 3);
2212 
2213 float32Array[0] = val;
2214 buffer[offset++] = uInt8Float32Array[3]!;
2215 buffer[offset++] = uInt8Float32Array[2]!;
2216 buffer[offset++] = uInt8Float32Array[1]!;
2217 buffer[offset++] = uInt8Float32Array[0]!;
2218 return offset;
2219}
2220 
2221function readInt24LE(buf: Buffer | Uint8Array, offset: number = 0) {
2222 validateOffset(offset, 'offset', 0, buf.length);
2223 const first = buf[offset];
2224 const last = buf[offset + 2];
2225 if (first === undefined || last === undefined) {
2226 boundsError(offset, buf.length - 3);
2227 }
2228 
2229 const val = first + buf[++offset]! * 2 ** 8 + last * 2 ** 16;
2230 return val | ((val & (2 ** 23)) * 0x1fe);
2231}
2232 
2233function readInt40LE(buf: Buffer | Uint8Array, offset: number = 0) {
2234 validateOffset(offset, 'offset', 0, buf.length);
2235 const first = buf[offset];
2236 const last = buf[offset + 4];
2237 if (first === undefined || last === undefined) {
2238 boundsError(offset, buf.length - 5);
2239 }
2240 
2241 return (
2242 (last | ((last & (2 ** 7)) * 0x1fffffe)) * 2 ** 32 +
2243 first +
2244 buf[++offset]! * 2 ** 8 +
2245 buf[++offset]! * 2 ** 16 +
2246 buf[++offset]! * 2 ** 24
2247 );
2248}
2249 
2250function readInt48LE(buf: Buffer | Uint8Array, offset: number = 0) {
2251 validateOffset(offset, 'offset', 0, buf.length);
2252 const first = buf[offset];
2253 const last = buf[offset + 5];
2254 if (first === undefined || last === undefined) {
2255 boundsError(offset, buf.length - 6);
2256 }
2257 
2258 const val = buf[offset + 4]! + last * 2 ** 8;
2259 return (
2260 (val | ((val & (2 ** 15)) * 0x1fffe)) * 2 ** 32 +
2261 first +
2262 buf[++offset]! * 2 ** 8 +
2263 buf[++offset]! * 2 ** 16 +
2264 buf[++offset]! * 2 ** 24
2265 );
2266}
2267 
2268function readInt24BE(buf: Buffer | Uint8Array, offset: number = 0) {
2269 validateOffset(offset, 'offset', 0, buf.length);
2270 const first = buf[offset];
2271 const last = buf[offset + 2];
2272 if (first === undefined || last === undefined) {
2273 boundsError(offset, buf.length - 3);
2274 }
2275 
2276 const val = first * 2 ** 16 + buf[++offset]! * 2 ** 8 + last;
2277 return val | ((val & (2 ** 23)) * 0x1fe);
2278}
2279 
2280function readInt48BE(buf: Buffer | Uint8Array, offset: number = 0) {
2281 validateOffset(offset, 'offset', 0, buf.length);
2282 const first = buf[offset];
2283 const last = buf[offset + 5];
2284 if (first === undefined || last === undefined) {
2285 boundsError(offset, buf.length - 6);
2286 }
2287 
2288 const val = buf[++offset]! + first * 2 ** 8;
2289 return (
2290 (val | ((val & (2 ** 15)) * 0x1fffe)) * 2 ** 32 +
2291 buf[++offset]! * 2 ** 24 +
2292 buf[++offset]! * 2 ** 16 +
2293 buf[++offset]! * 2 ** 8 +
2294 last
2295 );
2296}
2297 
2298function readInt40BE(buf: Buffer | Uint8Array, offset: number = 0) {
2299 validateOffset(offset, 'offset', 0, buf.length);
2300 const first = buf[offset];
2301 const last = buf[offset + 4];
2302 if (first === undefined || last === undefined) {
2303 boundsError(offset, buf.length - 5);
2304 }
2305 
2306 return (
2307 (first | ((first & (2 ** 7)) * 0x1fffffe)) * 2 ** 32 +
2308 buf[++offset]! * 2 ** 24 +
2309 buf[++offset]! * 2 ** 16 +
2310 buf[++offset]! * 2 ** 8 +
2311 last
2312 );
2313}
2314 
2315function boundsError(value: number, length: number, type?: string): never {
2316 if (Math.floor(value) !== value) {
2317 throw new ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value);
2318 }
2319 
2320 if (length < 0) {
2321 throw new ERR_BUFFER_OUT_OF_BOUNDS();
2322 }
2323 
2324 throw new ERR_OUT_OF_RANGE(
2325 type || 'offset',
2326 `>= ${type ? 1 : 0} and <= ${length}`,
2327 value
2328 );
2329}
2330 
2331function validateNumber(value: unknown, name: string) {
2332 if (typeof value !== 'number') {
2333 throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
2334 }
2335}
2336 
2337function checkInt(
2338 value: number | bigint,
2339 min: number | bigint,
2340 max: number | bigint,
2341 buf: Buffer,
2342 offset: number,
2343 byteLength: number
2344) {
2345 if (value > max || value < min) {
2346 const n = typeof min === 'bigint' ? 'n' : '';
2347 let range;
2348 if (byteLength > 3) {
2349 if (min === 0 || min === 0n) {
2350 range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
2351 } else {
2352 range =
2353 `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and ` +
2354 `< 2${n} ** ${(byteLength + 1) * 8 - 1}${n}`;
2355 }
2356 } else {
2357 range = `>= ${min}${n} and <= ${max}${n}`;
2358 }
2359 throw new ERR_OUT_OF_RANGE('value', range, value);
2360 }
2361 checkBounds(buf, offset, byteLength);
2362}
2363 
2364function toInteger(n: number | undefined, defaultVal: number) {
2365 if (n === undefined) n = 0;
2366 n = +(n as number);
2367 if (
2368 !Number.isNaN(n) &&
2369 n >= Number.MIN_SAFE_INTEGER &&
2370 n <= Number.MAX_SAFE_INTEGER
2371 ) {
2372 return n % 1 === 0 ? n : Math.floor(n);
2373 }
2374 return defaultVal;
2375}
2376 
2377function writeU_Int8(
2378 buf: Buffer,
2379 value: number,
2380 offset: number,
2381 min: number,
2382 max: number
2383) {
2384 value = +value;
2385 validateOffset(offset, 'offset', 0, buf.length);
2386 if (value > max || value < min) {
2387 throw new ERR_OUT_OF_RANGE('value', `>= ${min} and <= ${max}`, value);
2388 }
2389 if (buf[offset] === undefined) {
2390 boundsError(offset, buf.length - 1);
2391 }
2392 
2393 buf[offset] = value;
2394 return offset + 1;
2395}
2396 
2397function writeU_Int16BE(
2398 buf: Buffer,
2399 value: number,
2400 offset: number,
2401 min: number,
2402 max: number
2403) {
2404 value = +value;
2405 validateOffset(offset, 'offset', 0, buf.length);
2406 checkInt(value, min, max, buf, offset, 1);
2407 
2408 buf[offset++] = value >>> 8;
2409 buf[offset++] = value;
2410 return offset;
2411}
2412 
2413function _writeUInt32LE(
2414 buf: Buffer,
2415 value: number,
2416 offset: number,
2417 min: number,
2418 max: number
2419) {
2420 value = +value;
2421 validateOffset(offset, 'offset', 0, buf.length);
2422 checkInt(value, min, max, buf, offset, 3);
2423 
2424 buf[offset++] = value;
2425 value = value >>> 8;
2426 buf[offset++] = value;
2427 value = value >>> 8;
2428 buf[offset++] = value;
2429 value = value >>> 8;
2430 buf[offset++] = value;
2431 return offset;
2432}
2433 
2434function writeU_Int16LE(
2435 buf: Buffer,
2436 value: number,
2437 offset: number,
2438 min: number,
2439 max: number
2440) {
2441 value = +value;
2442 validateOffset(offset, 'offset', 0, buf.length);
2443 checkInt(value, min, max, buf, offset, 1);
2444 
2445 buf[offset++] = value;
2446 buf[offset++] = value >>> 8;
2447 return offset;
2448}
2449 
2450function _writeUInt32BE(
2451 buf: Buffer,
2452 value: number,
2453 offset: number,
2454 min: number,
2455 max: number
2456) {
2457 value = +value;
2458 validateOffset(offset, 'offset', 0, buf.length);
2459 checkInt(value, min, max, buf, offset, 3);
2460 
2461 buf[offset + 3] = value;
2462 value = value >>> 8;
2463 buf[offset + 2] = value;
2464 value = value >>> 8;
2465 buf[offset + 1] = value;
2466 value = value >>> 8;
2467 buf[offset] = value;
2468 return offset + 4;
2469}
2470 
2471function writeU_Int48BE(
2472 buf: Buffer,
2473 value: number,
2474 offset: number,
2475 min: number,
2476 max: number
2477) {
2478 value = +value;
2479 validateOffset(offset, 'offset', 0, buf.length);
2480 checkInt(value, min, max, buf, offset, 5);
2481 
2482 const newVal = Math.floor(value * 2 ** -32);
2483 buf[offset++] = newVal >>> 8;
2484 buf[offset++] = newVal;
2485 buf[offset + 3] = value;
2486 value = value >>> 8;
2487 buf[offset + 2] = value;
2488 value = value >>> 8;
2489 buf[offset + 1] = value;
2490 value = value >>> 8;
2491 buf[offset] = value;
2492 return offset + 4;
2493}
2494 
2495function writeU_Int40BE(
2496 buf: Buffer,
2497 value: number,
2498 offset: number,
2499 min: number,
2500 max: number
2501) {
2502 value = +value;
2503 validateOffset(offset, 'offset', 0, buf.length);
2504 checkInt(value, min, max, buf, offset, 4);
2505 
2506 buf[offset++] = Math.floor(value * 2 ** -32);
2507 buf[offset + 3] = value;
2508 value = value >>> 8;
2509 buf[offset + 2] = value;
2510 value = value >>> 8;
2511 buf[offset + 1] = value;
2512 value = value >>> 8;
2513 buf[offset] = value;
2514 return offset + 4;
2515}
2516 
2517function writeU_Int32BE(
2518 buf: Buffer,
2519 value: number,
2520 offset: number,
2521 min: number,
2522 max: number
2523) {
2524 value = +value;
2525 validateOffset(offset, 'offset', 0, buf.length);
2526 checkInt(value, min, max, buf, offset, 3);
2527 
2528 buf[offset + 3] = value;
2529 value = value >>> 8;
2530 buf[offset + 2] = value;
2531 value = value >>> 8;
2532 buf[offset + 1] = value;
2533 value = value >>> 8;
2534 buf[offset] = value;
2535 return offset + 4;
2536}
2537 
2538function writeU_Int24BE(
2539 buf: Buffer,
2540 value: number,
2541 offset: number,
2542 min: number,
2543 max: number
2544) {
2545 value = +value;
2546 validateOffset(offset, 'offset', 0, buf.length);
2547 checkInt(value, min, max, buf, offset, 2);
2548 
2549 buf[offset + 2] = value;
2550 value = value >>> 8;
2551 buf[offset + 1] = value;
2552 value = value >>> 8;
2553 buf[offset] = value;
2554 return offset + 3;
2555}
2556 
2557function validateOffset(
2558 value: number,
2559 name: string,
2560 min: number = 0,
2561 max: number = Number.MAX_SAFE_INTEGER
2562) {
2563 if (typeof value !== 'number') {
2564 throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
2565 }
2566 if (!Number.isInteger(value)) {
2567 throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
2568 }
2569 if (value < min || value > max) {
2570 throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
2571 }
2572}
2573 
2574function writeU_Int48LE(
2575 buf: Buffer,
2576 value: number,
2577 offset: number,
2578 min: number,
2579 max: number
2580) {
2581 value = +value;
2582 validateOffset(offset, 'offset', 0, buf.length);
2583 checkInt(value, min, max, buf, offset, 5);
2584 
2585 const newVal = Math.floor(value * 2 ** -32);
2586 buf[offset++] = value;
2587 value = value >>> 8;
2588 buf[offset++] = value;
2589 value = value >>> 8;
2590 buf[offset++] = value;
2591 value = value >>> 8;
2592 buf[offset++] = value;
2593 buf[offset++] = newVal;
2594 buf[offset++] = newVal >>> 8;
2595 return offset;
2596}
2597 
2598function writeU_Int40LE(
2599 buf: Buffer,
2600 value: number,
2601 offset: number,
2602 min: number,
2603 max: number
2604) {
2605 value = +value;
2606 validateOffset(offset, 'offset', 0, buf.length);
2607 checkInt(value, min, max, buf, offset, 4);
2608 
2609 const newVal = value;
2610 buf[offset++] = value;
2611 value = value >>> 8;
2612 buf[offset++] = value;
2613 value = value >>> 8;
2614 buf[offset++] = value;
2615 value = value >>> 8;
2616 buf[offset++] = value;
2617 buf[offset++] = Math.floor(newVal * 2 ** -32);
2618 return offset;
2619}
2620 
2621function writeU_Int32LE(
2622 buf: Buffer,
2623 value: number,
2624 offset: number,
2625 min: number,
2626 max: number
2627) {
2628 value = +value;
2629 validateOffset(offset, 'offset', 0, buf.length);
2630 checkInt(value, min, max, buf, offset, 3);
2631 
2632 buf[offset++] = value;
2633 value = value >>> 8;
2634 buf[offset++] = value;
2635 value = value >>> 8;
2636 buf[offset++] = value;
2637 value = value >>> 8;
2638 buf[offset++] = value;
2639 return offset;
2640}
2641 
2642function writeU_Int24LE(
2643 buf: Buffer,
2644 value: number,
2645 offset: number,
2646 min: number,
2647 max: number
2648) {
2649 value = +value;
2650 validateOffset(offset, 'offset', 0, buf.length);
2651 checkInt(value, min, max, buf, offset, 2);
2652 
2653 buf[offset++] = value;
2654 value = value >>> 8;
2655 buf[offset++] = value;
2656 value = value >>> 8;
2657 buf[offset++] = value;
2658 return offset;
2659}
2660 
2661export function isAscii(value: ArrayBufferView) {
2662 if ((value as any)?.detached || (value as any)?.buffer?.detached) {
2663 throw new Error(
2664 'Unable to determine if buffer is ASCII when it is detached'
2665 );
2666 }
2667 return bufferUtil.isAscii(value);
2668}
2669 
2670export function isUtf8(value: ArrayBufferView) {
2671 if ((value as any)?.detached || (value as any)?.buffer?.detached) {
2672 throw new Error(
2673 'Unable to determine if buffer is UTF8 when it is detached'
2674 );
2675 }
2676 return bufferUtil.isUtf8(value);
2677}
2678 
2679export function transcode(
2680 source: ArrayBufferView,
2681 fromEncoding: string,
2682 toEncoding: string
2683) {
2684 if (!isArrayBufferView(source)) {
2685 throw new ERR_INVALID_ARG_TYPE('source', 'ArrayBufferView', typeof source);
2686 }
2687 const normalizedFromEncoding = normalizeEncoding(fromEncoding);
2688 if (normalizedFromEncoding === undefined) {
2689 throw new ERR_UNKNOWN_ENCODING(fromEncoding);
2690 }
2691 const normalizedToEncoding = normalizeEncoding(toEncoding);
2692 if (normalizedToEncoding === undefined) {
2693 throw new ERR_UNKNOWN_ENCODING(toEncoding);
2694 }
2695 
2696 const u8: Uint8Array = bufferUtil.transcode(
2697 source,
2698 normalizedFromEncoding,
2699 normalizedToEncoding
2700 );
2701 return Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength);
2702}
2703 
2704export function resolveObjectURL(_id: string): unknown {
2705 throw new Error('resolveObjectURL is not implemented');
2706}
2707 
2708export default {
2709 Buffer,
2710 constants,
2711 kMaxLength,
2712 kStringMaxLength,
2713 SlowBuffer,
2714 isAscii,
2715 isUtf8,
2716 INSPECT_MAX_BYTES,
2717 transcode,
2718 resolveObjectURL,
2719};