Skip to main content
Module

std/node/internal/buffer.mjs

Deno standard library
Go to Latest
File
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Copyright Feross Aboukhadijeh, and other contributors. All rights reserved. MIT license.
import { codes } from "./error_codes.ts";import { encodings } from "../internal_binding/string_decoder.ts";import { indexOfBuffer, indexOfNumber } from "../internal_binding/buffer.ts";import { asciiToBytes, base64ToBytes, base64UrlToBytes, bytesToAscii, bytesToUtf16le, hexToBytes, utf16leToBytes,} from "../internal_binding/_utils.ts";import { isAnyArrayBuffer, isArrayBufferView } from "./util/types.ts";import { normalizeEncoding } from "./util.mjs";import { validateBuffer } from "./validators.mjs";import { isUint8Array } from "./util/types.ts";import * as base64 from "../../encoding/base64.ts";import * as base64url from "../../encoding/base64url.ts";
const utf8Encoder = new TextEncoder();
// Temporary buffers to convert numbers.const float32Array = new Float32Array(1);const uInt8Float32Array = new Uint8Array(float32Array.buffer);const float64Array = new Float64Array(1);const uInt8Float64Array = new Uint8Array(float64Array.buffer);
// Check endianness.float32Array[0] = -1; // 0xBF800000// Either it is [0, 0, 128, 191] or [191, 128, 0, 0]. It is not possible to// check this with `os.endianness()` because that is determined at compile time.export const bigEndian = uInt8Float32Array[3] === 0;
export const kMaxLength = 2147483647;export const kStringMaxLength = 536870888;const MAX_UINT32 = 2 ** 32;
const customInspectSymbol = typeof Symbol === "function" && typeof Symbol["for"] === "function" ? Symbol["for"]("nodejs.util.inspect.custom") : null;
const INSPECT_MAX_BYTES = 50;
export const constants = { MAX_LENGTH: kMaxLength, MAX_STRING_LENGTH: kStringMaxLength,};
Object.defineProperty(Buffer.prototype, "parent", { enumerable: true, get: function () { if (!Buffer.isBuffer(this)) { return void 0; } return this.buffer; },});
Object.defineProperty(Buffer.prototype, "offset", { enumerable: true, get: function () { if (!Buffer.isBuffer(this)) { return void 0; } return this.byteOffset; },});
function createBuffer(length) { if (length > kMaxLength) { throw new RangeError( 'The value "' + length + '" is invalid for option "size"', ); } const buf = new Uint8Array(length); Object.setPrototypeOf(buf, Buffer.prototype); return buf;}
export function Buffer(arg, encodingOrOffset, length) { if (typeof arg === "number") { if (typeof encodingOrOffset === "string") { throw new codes.ERR_INVALID_ARG_TYPE( "string", "string", arg, ); } return _allocUnsafe(arg); } return _from(arg, encodingOrOffset, length);}
Buffer.poolSize = 8192;
function _from(value, encodingOrOffset, length) { if (typeof value === "string") { return fromString(value, encodingOrOffset); }
if (typeof value === "object" && value !== null) { if (isAnyArrayBuffer(value)) { return fromArrayBuffer(value, encodingOrOffset, length); }
const valueOf = value.valueOf && value.valueOf(); if ( valueOf != null && valueOf !== value && (typeof valueOf === "string" || typeof valueOf === "object") ) { return _from(valueOf, encodingOrOffset, length); }
const b = fromObject(value); if (b) { return b; }
if (typeof value[Symbol.toPrimitive] === "function") { const primitive = value[Symbol.toPrimitive]("string"); if (typeof primitive === "string") { return fromString(primitive, encodingOrOffset); } } }
throw new codes.ERR_INVALID_ARG_TYPE( "first argument", ["string", "Buffer", "ArrayBuffer", "Array", "Array-like Object"], value, );}
Buffer.from = function from(value, encodingOrOffset, length) { return _from(value, encodingOrOffset, length);};
Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
Object.setPrototypeOf(Buffer, Uint8Array);
function assertSize(size) { validateNumber(size, "size"); if (!(size >= 0 && size <= kMaxLength)) { throw new codes.ERR_INVALID_ARG_VALUE.RangeError("size", size); }}
function _alloc(size, fill, encoding) { assertSize(size);
const buffer = createBuffer(size); if (fill !== undefined) { if (encoding !== undefined && typeof encoding !== "string") { throw new codes.ERR_INVALID_ARG_TYPE( "encoding", "string", encoding, ); } return buffer.fill(fill, encoding); } return buffer;}
Buffer.alloc = function alloc(size, fill, encoding) { return _alloc(size, fill, encoding);};
function _allocUnsafe(size) { assertSize(size); return createBuffer(size < 0 ? 0 : checked(size) | 0);}
Buffer.allocUnsafe = function allocUnsafe(size) { return _allocUnsafe(size);};
Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) { return _allocUnsafe(size);};
function fromString(string, encoding) { if (typeof encoding !== "string" || encoding === "") { encoding = "utf8"; } if (!Buffer.isEncoding(encoding)) { throw new codes.ERR_UNKNOWN_ENCODING(encoding); } const length = byteLength(string, encoding) | 0; let buf = createBuffer(length); const actual = buf.write(string, encoding); if (actual !== length) { buf = buf.slice(0, actual); } return buf;}
function fromArrayLike(array) { const length = array.length < 0 ? 0 : checked(array.length) | 0; const buf = createBuffer(length); for (let i = 0; i < length; i += 1) { buf[i] = array[i] & 255; } return buf;}
function fromObject(obj) { if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) { if (typeof obj.length !== "number") { return createBuffer(0); } return fromArrayLike(obj); }
if (obj.type === "Buffer" && Array.isArray(obj.data)) { return fromArrayLike(obj.data); }}
function checked(length) { if (length >= kMaxLength) { throw new RangeError( "Attempt to allocate Buffer larger than maximum size: 0x" + kMaxLength.toString(16) + " bytes", ); } return length | 0;}
export function SlowBuffer(length) { assertSize(length); return Buffer.alloc(+length);}
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
Object.setPrototypeOf(SlowBuffer, Uint8Array);
Buffer.isBuffer = function isBuffer(b) { return b != null && b._isBuffer === true && b !== Buffer.prototype;};
Buffer.compare = function compare(a, b) { if (isInstance(a, Uint8Array)) { a = Buffer.from(a, a.offset, a.byteLength); } if (isInstance(b, Uint8Array)) { b = Buffer.from(b, b.offset, b.byteLength); } if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { throw new TypeError( 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array', ); } if (a === b) { return 0; } let x = a.length; let y = b.length; for (let i = 0, len = Math.min(x, y); i < len; ++i) { if (a[i] !== b[i]) { x = a[i]; y = b[i]; break; } } if (x < y) { return -1; } if (y < x) { return 1; } return 0;};
Buffer.isEncoding = function isEncoding(encoding) { return typeof encoding === "string" && encoding.length !== 0 && normalizeEncoding(encoding) !== undefined;};
Buffer.concat = function concat(list, length) { if (!Array.isArray(list)) { throw new codes.ERR_INVALID_ARG_TYPE("list", "Array", list); }
if (list.length === 0) { return Buffer.alloc(0); }
if (length === undefined) { length = 0; for (let i = 0; i < list.length; i++) { if (list[i].length) { length += list[i].length; } } } else { validateOffset(length, "length"); }
const buffer = Buffer.allocUnsafe(length); let pos = 0; for (let i = 0; i < list.length; i++) { const buf = list[i]; if (!isUint8Array(buf)) { // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. // Instead, find the proper error code for this. throw new codes.ERR_INVALID_ARG_TYPE( `list[${i}]`, ["Buffer", "Uint8Array"], list[i], ); } pos += _copyActual(buf, buffer, pos, 0, buf.length); }
// Note: `length` is always equal to `buffer.length` at this point if (pos < length) { // Zero-fill the remaining bytes if the specified `length` was more than // the actual total length, i.e. if we have some remaining allocated bytes // there were not initialized. buffer.fill(0, pos, length); }
return buffer;};
function byteLength(string, encoding) { if (typeof string !== "string") { if (isArrayBufferView(string) || isAnyArrayBuffer(string)) { return string.byteLength; }
throw new codes.ERR_INVALID_ARG_TYPE( "string", ["string", "Buffer", "ArrayBuffer"], string, ); }
const len = string.length; const mustMatch = arguments.length > 2 && arguments[2] === true; if (!mustMatch && len === 0) { return 0; }
if (!encoding) { return (mustMatch ? -1 : byteLengthUtf8(string)); }
const ops = getEncodingOps(encoding); if (ops === undefined) { return (mustMatch ? -1 : byteLengthUtf8(string)); } return ops.byteLength(string);}
Buffer.byteLength = byteLength;
Buffer.prototype._isBuffer = true;
function swap(b, n, m) { const i = b[n]; b[n] = b[m]; b[m] = i;}
Buffer.prototype.swap16 = function swap16() { const len = this.length; if (len % 2 !== 0) { throw new RangeError("Buffer size must be a multiple of 16-bits"); } for (let i = 0; i < len; i += 2) { swap(this, i, i + 1); } return this;};
Buffer.prototype.swap32 = function swap32() { const len = this.length; if (len % 4 !== 0) { throw new RangeError("Buffer size must be a multiple of 32-bits"); } for (let i = 0; i < len; i += 4) { swap(this, i, i + 3); swap(this, i + 1, i + 2); } return this;};
Buffer.prototype.swap64 = function swap64() { const len = this.length; if (len % 8 !== 0) { throw new RangeError("Buffer size must be a multiple of 64-bits"); } for (let i = 0; i < len; i += 8) { swap(this, i, i + 7); swap(this, i + 1, i + 6); swap(this, i + 2, i + 5); swap(this, i + 3, i + 4); } return this;};
Buffer.prototype.toString = function toString(encoding, start, end) { if (arguments.length === 0) { return this.utf8Slice(0, this.length); }
const len = this.length;
if (start <= 0) { start = 0; } else if (start >= len) { return ""; } else { start |= 0; }
if (end === undefined || end > len) { end = len; } else { end |= 0; }
if (end <= start) { return ""; }
if (encoding === undefined) { return this.utf8Slice(start, end); }
const ops = getEncodingOps(encoding); if (ops === undefined) { throw new codes.ERR_UNKNOWN_ENCODING(encoding); }
return ops.slice(this, start, end);};
Buffer.prototype.toLocaleString = Buffer.prototype.toString;
Buffer.prototype.equals = function equals(b) { if (!isUint8Array(b)) { throw new codes.ERR_INVALID_ARG_TYPE( "otherBuffer", ["Buffer", "Uint8Array"], b, ); } if (this === b) { return true; } return Buffer.compare(this, b) === 0;};
Buffer.prototype.inspect = function inspect() { let str = ""; const max = INSPECT_MAX_BYTES; str = this.toString("hex", 0, max).replace(/(.{2})/g, "$1 ").trim(); if (this.length > max) { str += " ... "; } return "<Buffer " + str + ">";};
if (customInspectSymbol) { Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;}
Buffer.prototype.compare = function compare( target, start, end, thisStart, thisEnd,) { if (isInstance(target, Uint8Array)) { target = Buffer.from(target, target.offset, target.byteLength); } if (!Buffer.isBuffer(target)) { throw new codes.ERR_INVALID_ARG_TYPE( "target", ["Buffer", "Uint8Array"], target, ); }
if (start === undefined) { start = 0; } else { validateOffset(start, "targetStart", 0, kMaxLength); }
if (end === undefined) { end = target.length; } else { validateOffset(end, "targetEnd", 0, target.length); }
if (thisStart === undefined) { thisStart = 0; } else { validateOffset(start, "sourceStart", 0, kMaxLength); }
if (thisEnd === undefined) { thisEnd = this.length; } else { validateOffset(end, "sourceEnd", 0, this.length); }
if ( start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length ) { throw new codes.ERR_OUT_OF_RANGE("out of range index", "range"); }
if (thisStart >= thisEnd && start >= end) { return 0; } if (thisStart >= thisEnd) { return -1; } if (start >= end) { return 1; } start >>>= 0; end >>>= 0; thisStart >>>= 0; thisEnd >>>= 0; if (this === target) { return 0; } let x = thisEnd - thisStart; let y = end - start; const len = Math.min(x, y); const thisCopy = this.slice(thisStart, thisEnd); const targetCopy = target.slice(start, end); for (let i = 0; i < len; ++i) { if (thisCopy[i] !== targetCopy[i]) { x = thisCopy[i]; y = targetCopy[i]; break; } } if (x < y) { return -1; } if (y < x) { return 1; } return 0;};
function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { validateBuffer(buffer);
if (typeof byteOffset === "string") { encoding = byteOffset; byteOffset = undefined; } else if (byteOffset > 0x7fffffff) { byteOffset = 0x7fffffff; } else if (byteOffset < -0x80000000) { byteOffset = -0x80000000; } byteOffset = +byteOffset; if (Number.isNaN(byteOffset)) { byteOffset = dir ? 0 : (buffer.length || buffer.byteLength); } dir = !!dir;
if (typeof val === "number") { return indexOfNumber(buffer, val >>> 0, byteOffset, dir); }
let ops; if (encoding === undefined) { ops = encodingOps.utf8; } else { ops = getEncodingOps(encoding); }
if (typeof val === "string") { if (ops === undefined) { throw new codes.ERR_UNKNOWN_ENCODING(encoding); } return ops.indexOf(buffer, val, byteOffset, dir); }
if (isUint8Array(val)) { const encodingVal = ops === undefined ? encodingsMap.utf8 : ops.encodingVal; return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir); }
throw new codes.ERR_INVALID_ARG_TYPE( "value", ["number", "string", "Buffer", "Uint8Array"], val, );}
Buffer.prototype.includes = function includes(val, byteOffset, encoding) { return this.indexOf(val, byteOffset, encoding) !== -1;};
Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) { return bidirectionalIndexOf(this, val, byteOffset, encoding, true);};
Buffer.prototype.lastIndexOf = function lastIndexOf( val, byteOffset, encoding,) { return bidirectionalIndexOf(this, val, byteOffset, encoding, false);};
Buffer.prototype.asciiSlice = function asciiSlice(offset, length) { if (offset === 0 && length === this.length) { return bytesToAscii(this); } else { return bytesToAscii(this.slice(offset, length)); }};
Buffer.prototype.asciiWrite = function asciiWrite(string, offset, length) { return blitBuffer(asciiToBytes(string), this, offset, length);};
Buffer.prototype.base64Slice = function base64Slice( offset, length,) { if (offset === 0 && length === this.length) { return base64.encode(this); } else { return base64.encode(this.slice(offset, length)); }};
Buffer.prototype.base64Write = function base64Write( string, offset, length,) { return blitBuffer(base64ToBytes(string), this, offset, length);};
Buffer.prototype.base64urlSlice = function base64urlSlice( offset, length,) { if (offset === 0 && length === this.length) { return base64url.encode(this); } else { return base64url.encode(this.slice(offset, length)); }};
Buffer.prototype.base64urlWrite = function base64urlWrite( string, offset, length,) { return blitBuffer(base64UrlToBytes(string), this, offset, length);};
Buffer.prototype.hexWrite = function hexWrite(string, offset, length) { return blitBuffer( hexToBytes(string, this.length - offset), this, offset, length, );};
Buffer.prototype.hexSlice = function hexSlice(string, offset, length) { return _hexSlice(this, string, offset, length);};
Buffer.prototype.latin1Slice = function latin1Slice( string, offset, length,) { return _latin1Slice(this, string, offset, length);};
Buffer.prototype.latin1Write = function latin1Write( string, offset, length,) { return blitBuffer(asciiToBytes(string), this, offset, length);};
Buffer.prototype.ucs2Slice = function ucs2Slice(offset, length) { if (offset === 0 && length === this.length) { return bytesToUtf16le(this); } else { return bytesToUtf16le(this.slice(offset, length)); }};
Buffer.prototype.ucs2Write = function ucs2Write(string, offset, length) { return blitBuffer( utf16leToBytes(string, this.length - offset), this, offset, length, );};
Buffer.prototype.utf8Slice = function utf8Slice(string, offset, length) { return _utf8Slice(this, string, offset, length);};
Buffer.prototype.utf8Write = function utf8Write(string, offset, length) { return blitBuffer( utf8ToBytes(string, this.length - offset), this, offset, length, );};
Buffer.prototype.write = function write(string, offset, length, encoding) { // Buffer#write(string); if (offset === undefined) { return this.utf8Write(string, 0, this.length); } // Buffer#write(string, encoding) if (length === undefined && typeof offset === "string") { encoding = offset; length = this.length; offset = 0;
// Buffer#write(string, offset[, length][, encoding]) } else { validateOffset(offset, "offset", 0, this.length);
const remaining = this.length - offset;
if (length === undefined) { length = remaining; } else if (typeof length === "string") { encoding = length; length = remaining; } else { validateOffset(length, "length", 0, this.length); if (length > remaining) { length = remaining; } } }
if (!encoding) { return this.utf8Write(string, offset, length); }
const ops = getEncodingOps(encoding); if (ops === undefined) { throw new codes.ERR_UNKNOWN_ENCODING(encoding); } return ops.write(this, string, offset, length);};
Buffer.prototype.toJSON = function toJSON() { return { type: "Buffer", data: Array.prototype.slice.call(this._arr || this, 0), };};function fromArrayBuffer(obj, byteOffset, length) { // Convert byteOffset to integer if (byteOffset === undefined) { byteOffset = 0; } else { byteOffset = +byteOffset; if (Number.isNaN(byteOffset)) { byteOffset = 0; } }
const maxLength = obj.byteLength - byteOffset;
if (maxLength < 0) { throw new codes.ERR_BUFFER_OUT_OF_BOUNDS("offset"); }
if (length === undefined) { length = maxLength; } else { // Convert length to non-negative integer. length = +length; if (length > 0) { if (length > maxLength) { throw new codes.ERR_BUFFER_OUT_OF_BOUNDS("length"); } } else { length = 0; } }
const buffer = new Uint8Array(obj, byteOffset, length); Object.setPrototypeOf(buffer, Buffer.prototype); return buffer;}
function _base64Slice(buf, start, end) { if (start === 0 && end === buf.length) { return base64.encode(buf); } else { return base64.encode(buf.slice(start, end)); }}
const decoder = new TextDecoder();
function _utf8Slice(buf, start, end) { return decoder.decode(buf.slice(start, end));}
function _latin1Slice(buf, start, end) { let ret = ""; end = Math.min(buf.length, end); for (let i = start; i < end; ++i) { ret += String.fromCharCode(buf[i]); } return ret;}
function _hexSlice(buf, start, end) { const len = buf.length; if (!start || start < 0) { start = 0; } if (!end || end < 0 || end > len) { end = len; } let out = ""; for (let i = start; i < end; ++i) { out += hexSliceLookupTable[buf[i]]; } return out;}
Buffer.prototype.slice = function slice(start, end) { const len = this.length; start = ~~start; end = end === void 0 ? len : ~~end; if (start < 0) { start += len; if (start < 0) { start = 0; } } else if (start > len) { start = len; } if (end < 0) { end += len; if (end < 0) { end = 0; } } else if (end > len) { end = len; } if (end < start) { end = start; } const newBuf = this.subarray(start, end); Object.setPrototypeOf(newBuf, Buffer.prototype); return newBuf;};
Buffer.prototype.readUintLE = Buffer.prototype.readUIntLE = function readUIntLE( offset, byteLength,) { if (offset === undefined) { throw new codes.ERR_INVALID_ARG_TYPE("offset", "number", offset); } if (byteLength === 6) { return readUInt48LE(this, offset); } if (byteLength === 5) { return readUInt40LE(this, offset); } if (byteLength === 3) { return readUInt24LE(this, offset); } if (byteLength === 4) { return this.readUInt32LE(offset); } if (byteLength === 2) { return this.readUInt16LE(offset); } if (byteLength === 1) { return this.readUInt8(offset); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.readUintBE = Buffer.prototype.readUIntBE = function readUIntBE( offset, byteLength,) { if (offset === undefined) { throw new codes.ERR_INVALID_ARG_TYPE("offset", "number", offset); } if (byteLength === 6) { return readUInt48BE(this, offset); } if (byteLength === 5) { return readUInt40BE(this, offset); } if (byteLength === 3) { return readUInt24BE(this, offset); } if (byteLength === 4) { return this.readUInt32BE(offset); } if (byteLength === 2) { return this.readUInt16BE(offset); } if (byteLength === 1) { return this.readUInt8(offset); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.readUint8 = Buffer.prototype.readUInt8 = function readUInt8( offset = 0,) { validateNumber(offset, "offset"); const val = this[offset]; if (val === undefined) { boundsError(offset, this.length - 1); }
return val;};
Buffer.prototype.readUint16BE = Buffer.prototype.readUInt16BE = readUInt16BE;
Buffer.prototype.readUint16LE = Buffer.prototype.readUInt16LE = function readUInt16LE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 2); }
return first + last * 2 ** 8; };
Buffer.prototype.readUint32LE = Buffer.prototype.readUInt32LE = function readUInt32LE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 4); }
return first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + last * 2 ** 24; };
Buffer.prototype.readUint32BE = Buffer.prototype.readUInt32BE = readUInt32BE;
Buffer.prototype.readBigUint64LE = Buffer.prototype.readBigUInt64LE = defineBigIntMethod( function readBigUInt64LE(offset) { offset = offset >>> 0; validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 7]; if (first === void 0 || last === void 0) { boundsError(offset, this.length - 8); } const lo = first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24; const hi = this[++offset] + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + last * 2 ** 24; return BigInt(lo) + (BigInt(hi) << BigInt(32)); }, );
Buffer.prototype.readBigUint64BE = Buffer.prototype.readBigUInt64BE = defineBigIntMethod( function readBigUInt64BE(offset) { offset = offset >>> 0; validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 7]; if (first === void 0 || last === void 0) { boundsError(offset, this.length - 8); } const hi = first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset]; const lo = this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last; return (BigInt(hi) << BigInt(32)) + BigInt(lo); }, );
Buffer.prototype.readIntLE = function readIntLE( offset, byteLength,) { if (offset === undefined) { throw new codes.ERR_INVALID_ARG_TYPE("offset", "number", offset); } if (byteLength === 6) { return readInt48LE(this, offset); } if (byteLength === 5) { return readInt40LE(this, offset); } if (byteLength === 3) { return readInt24LE(this, offset); } if (byteLength === 4) { return this.readInt32LE(offset); } if (byteLength === 2) { return this.readInt16LE(offset); } if (byteLength === 1) { return this.readInt8(offset); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.readIntBE = function readIntBE(offset, byteLength) { if (offset === undefined) { throw new codes.ERR_INVALID_ARG_TYPE("offset", "number", offset); } if (byteLength === 6) { return readInt48BE(this, offset); } if (byteLength === 5) { return readInt40BE(this, offset); } if (byteLength === 3) { return readInt24BE(this, offset); } if (byteLength === 4) { return this.readInt32BE(offset); } if (byteLength === 2) { return this.readInt16BE(offset); } if (byteLength === 1) { return this.readInt8(offset); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.readInt8 = function readInt8(offset = 0) { validateNumber(offset, "offset"); const val = this[offset]; if (val === undefined) { boundsError(offset, this.length - 1); }
return val | (val & 2 ** 7) * 0x1fffffe;};
Buffer.prototype.readInt16LE = function readInt16LE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 2); }
const val = first + last * 2 ** 8; return val | (val & 2 ** 15) * 0x1fffe;};
Buffer.prototype.readInt16BE = function readInt16BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 2); }
const val = first * 2 ** 8 + last; return val | (val & 2 ** 15) * 0x1fffe;};
Buffer.prototype.readInt32LE = function readInt32LE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 4); }
return first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + (last << 24); // Overflow};
Buffer.prototype.readInt32BE = function readInt32BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 4); }
return (first << 24) + // Overflow this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;};
Buffer.prototype.readBigInt64LE = defineBigIntMethod( function readBigInt64LE(offset) { offset = offset >>> 0; validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 7]; if (first === void 0 || last === void 0) { boundsError(offset, this.length - 8); } const val = this[offset + 4] + this[offset + 5] * 2 ** 8 + this[offset + 6] * 2 ** 16 + (last << 24); return (BigInt(val) << BigInt(32)) + BigInt( first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24, ); },);
Buffer.prototype.readBigInt64BE = defineBigIntMethod( function readBigInt64BE(offset) { offset = offset >>> 0; validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 7]; if (first === void 0 || last === void 0) { boundsError(offset, this.length - 8); } const val = (first << 24) + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset]; return (BigInt(val) << BigInt(32)) + BigInt( this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last, ); },);
Buffer.prototype.readFloatLE = function readFloatLE(offset) { return bigEndian ? readFloatBackwards(this, offset) : readFloatForwards(this, offset);};
Buffer.prototype.readFloatBE = function readFloatBE(offset) { return bigEndian ? readFloatForwards(this, offset) : readFloatBackwards(this, offset);};
Buffer.prototype.readDoubleLE = function readDoubleLE(offset) { return bigEndian ? readDoubleBackwards(this, offset) : readDoubleForwards(this, offset);};
Buffer.prototype.readDoubleBE = function readDoubleBE(offset) { return bigEndian ? readDoubleForwards(this, offset) : readDoubleBackwards(this, offset);};
Buffer.prototype.writeUintLE = Buffer.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength) { if (byteLength === 6) { return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff); } if (byteLength === 5) { return writeU_Int40LE(this, value, offset, 0, 0xffffffffff); } if (byteLength === 3) { return writeU_Int24LE(this, value, offset, 0, 0xffffff); } if (byteLength === 4) { return writeU_Int32LE(this, value, offset, 0, 0xffffffff); } if (byteLength === 2) { return writeU_Int16LE(this, value, offset, 0, 0xffff); } if (byteLength === 1) { return writeU_Int8(this, value, offset, 0, 0xff); }
boundsError(byteLength, 6, "byteLength"); };
Buffer.prototype.writeUintBE = Buffer.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength) { if (byteLength === 6) { return writeU_Int48BE(this, value, offset, 0, 0xffffffffffff); } if (byteLength === 5) { return writeU_Int40BE(this, value, offset, 0, 0xffffffffff); } if (byteLength === 3) { return writeU_Int24BE(this, value, offset, 0, 0xffffff); } if (byteLength === 4) { return writeU_Int32BE(this, value, offset, 0, 0xffffffff); } if (byteLength === 2) { return writeU_Int16BE(this, value, offset, 0, 0xffff); } if (byteLength === 1) { return writeU_Int8(this, value, offset, 0, 0xff); }
boundsError(byteLength, 6, "byteLength"); };
Buffer.prototype.writeUint8 = Buffer.prototype.writeUInt8 = function writeUInt8( value, offset = 0,) { return writeU_Int8(this, value, offset, 0, 0xff);};
Buffer.prototype.writeUint16LE = Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset = 0) { return writeU_Int16LE(this, value, offset, 0, 0xffff); };
Buffer.prototype.writeUint16BE = Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset = 0) { return writeU_Int16BE(this, value, offset, 0, 0xffff); };
Buffer.prototype.writeUint32LE = Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset = 0) { return _writeUInt32LE(this, value, offset, 0, 0xffffffff); };
Buffer.prototype.writeUint32BE = Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset = 0) { return _writeUInt32BE(this, value, offset, 0, 0xffffffff); };
function wrtBigUInt64LE(buf, value, offset, min, max) { checkIntBI(value, min, max, buf, offset, 7); let lo = Number(value & BigInt(4294967295)); buf[offset++] = lo; lo = lo >> 8; buf[offset++] = lo; lo = lo >> 8; buf[offset++] = lo; lo = lo >> 8; buf[offset++] = lo; let hi = Number(value >> BigInt(32) & BigInt(4294967295)); buf[offset++] = hi; hi = hi >> 8; buf[offset++] = hi; hi = hi >> 8; buf[offset++] = hi; hi = hi >> 8; buf[offset++] = hi; return offset;}
function wrtBigUInt64BE(buf, value, offset, min, max) { checkIntBI(value, min, max, buf, offset, 7); let lo = Number(value & BigInt(4294967295)); buf[offset + 7] = lo; lo = lo >> 8; buf[offset + 6] = lo; lo = lo >> 8; buf[offset + 5] = lo; lo = lo >> 8; buf[offset + 4] = lo; let hi = Number(value >> BigInt(32) & BigInt(4294967295)); buf[offset + 3] = hi; hi = hi >> 8; buf[offset + 2] = hi; hi = hi >> 8; buf[offset + 1] = hi; hi = hi >> 8; buf[offset] = hi; return offset + 8;}
Buffer.prototype.writeBigUint64LE = Buffer.prototype.writeBigUInt64LE = defineBigIntMethod( function writeBigUInt64LE(value, offset = 0) { return wrtBigUInt64LE( this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"), ); }, );
Buffer.prototype.writeBigUint64BE = Buffer.prototype.writeBigUInt64BE = defineBigIntMethod( function writeBigUInt64BE(value, offset = 0) { return wrtBigUInt64BE( this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"), ); }, );
Buffer.prototype.writeIntLE = function writeIntLE( value, offset, byteLength,) { if (byteLength === 6) { return writeU_Int48LE( this, value, offset, -0x800000000000, 0x7fffffffffff, ); } if (byteLength === 5) { return writeU_Int40LE(this, value, offset, -0x8000000000, 0x7fffffffff); } if (byteLength === 3) { return writeU_Int24LE(this, value, offset, -0x800000, 0x7fffff); } if (byteLength === 4) { return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff); } if (byteLength === 2) { return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff); } if (byteLength === 1) { return writeU_Int8(this, value, offset, -0x80, 0x7f); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.writeIntBE = function writeIntBE( value, offset, byteLength,) { if (byteLength === 6) { return writeU_Int48BE( this, value, offset, -0x800000000000, 0x7fffffffffff, ); } if (byteLength === 5) { return writeU_Int40BE(this, value, offset, -0x8000000000, 0x7fffffffff); } if (byteLength === 3) { return writeU_Int24BE(this, value, offset, -0x800000, 0x7fffff); } if (byteLength === 4) { return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff); } if (byteLength === 2) { return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff); } if (byteLength === 1) { return writeU_Int8(this, value, offset, -0x80, 0x7f); }
boundsError(byteLength, 6, "byteLength");};
Buffer.prototype.writeInt8 = function writeInt8(value, offset = 0) { return writeU_Int8(this, value, offset, -0x80, 0x7f);};
Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset = 0) { return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);};
Buffer.prototype.writeInt16BE = function writeInt16BE( value, offset = 0,) { return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);};
Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset = 0) { return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);};
Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset = 0) { return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);};
Buffer.prototype.writeBigInt64LE = defineBigIntMethod( function writeBigInt64LE(value, offset = 0) { return wrtBigUInt64LE( this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"), ); },);
Buffer.prototype.writeBigInt64BE = defineBigIntMethod( function writeBigInt64BE(value, offset = 0) { return wrtBigUInt64BE( this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"), ); },);
Buffer.prototype.writeFloatLE = function writeFloatLE( value, offset,) { return bigEndian ? writeFloatBackwards(this, value, offset) : writeFloatForwards(this, value, offset);};
Buffer.prototype.writeFloatBE = function writeFloatBE( value, offset,) { return bigEndian ? writeFloatForwards(this, value, offset) : writeFloatBackwards(this, value, offset);};
Buffer.prototype.writeDoubleLE = function writeDoubleLE( value, offset,) { return bigEndian ? writeDoubleBackwards(this, value, offset) : writeDoubleForwards(this, value, offset);};
Buffer.prototype.writeDoubleBE = function writeDoubleBE( value, offset,) { return bigEndian ? writeDoubleForwards(this, value, offset) : writeDoubleBackwards(this, value, offset);};
Buffer.prototype.copy = function copy( target, targetStart, sourceStart, sourceEnd,) { if (!isUint8Array(this)) { throw new codes.ERR_INVALID_ARG_TYPE( "source", ["Buffer", "Uint8Array"], this, ); }
if (!isUint8Array(target)) { throw new codes.ERR_INVALID_ARG_TYPE( "target", ["Buffer", "Uint8Array"], target, ); }
if (targetStart === undefined) { targetStart = 0; } else { targetStart = toInteger(targetStart, 0); if (targetStart < 0) { throw new codes.ERR_OUT_OF_RANGE("targetStart", ">= 0", targetStart); } }
if (sourceStart === undefined) { sourceStart = 0; } else { sourceStart = toInteger(sourceStart, 0); if (sourceStart < 0) { throw new codes.ERR_OUT_OF_RANGE("sourceStart", ">= 0", sourceStart); } if (sourceStart >= MAX_UINT32) { throw new codes.ERR_OUT_OF_RANGE( "sourceStart", `< ${MAX_UINT32}`, sourceStart, ); } }
if (sourceEnd === undefined) { sourceEnd = this.length; } else { sourceEnd = toInteger(sourceEnd, 0); if (sourceEnd < 0) { throw new codes.ERR_OUT_OF_RANGE("sourceEnd", ">= 0", sourceEnd); } if (sourceEnd >= MAX_UINT32) { throw new codes.ERR_OUT_OF_RANGE( "sourceEnd", `< ${MAX_UINT32}`, sourceEnd, ); } }
if (targetStart >= target.length) { return 0; }
if (sourceEnd > 0 && sourceEnd < sourceStart) { sourceEnd = sourceStart; } if (sourceEnd === sourceStart) { return 0; } if (target.length === 0 || this.length === 0) { return 0; }
if (sourceEnd > this.length) { sourceEnd = this.length; }
if (target.length - targetStart < sourceEnd - sourceStart) { sourceEnd = target.length - targetStart + sourceStart; }
const len = sourceEnd - sourceStart; if ( this === target && typeof Uint8Array.prototype.copyWithin === "function" ) { this.copyWithin(targetStart, sourceStart, sourceEnd); } else { Uint8Array.prototype.set.call( target, this.subarray(sourceStart, sourceEnd), targetStart, ); } return len;};
Buffer.prototype.fill = function fill(val, start, end, encoding) { if (typeof val === "string") { if (typeof start === "string") { encoding = start; start = 0; end = this.length; } else if (typeof end === "string") { encoding = end; end = this.length; } if (encoding !== void 0 && typeof encoding !== "string") { throw new TypeError("encoding must be a string"); } if (typeof encoding === "string" && !Buffer.isEncoding(encoding)) { throw new TypeError("Unknown encoding: " + encoding); } if (val.length === 1) { const code = val.charCodeAt(0); if (encoding === "utf8" && code < 128 || encoding === "latin1") { val = code; } } } else if (typeof val === "number") { val = val & 255; } else if (typeof val === "boolean") { val = Number(val); } if (start < 0 || this.length < start || this.length < end) { throw new RangeError("Out of range index"); } if (end <= start) { return this; } start = start >>> 0; end = end === void 0 ? this.length : end >>> 0; if (!val) { val = 0; } let i; if (typeof val === "number") { for (i = start; i < end; ++i) { this[i] = val; } } else { const bytes = Buffer.isBuffer(val) ? val : Buffer.from(val, encoding); const len = bytes.length; if (len === 0) { throw new codes.ERR_INVALID_ARG_VALUE( "value", val, ); } for (i = 0; i < end - start; ++i) { this[i + start] = bytes[i % len]; } } return this;};
function checkBounds(buf, offset, byteLength2) { validateNumber(offset, "offset"); if (buf[offset] === void 0 || buf[offset + byteLength2] === void 0) { boundsError(offset, buf.length - (byteLength2 + 1)); }}
function checkIntBI(value, min, max, buf, offset, byteLength2) { if (value > max || value < min) { const n = typeof min === "bigint" ? "n" : ""; let range; if (byteLength2 > 3) { if (min === 0 || min === BigInt(0)) { range = `>= 0${n} and < 2${n} ** ${(byteLength2 + 1) * 8}${n}`; } else { range = `>= -(2${n} ** ${(byteLength2 + 1) * 8 - 1}${n}) and < 2 ** ${ (byteLength2 + 1) * 8 - 1 }${n}`; } } else { range = `>= ${min}${n} and <= ${max}${n}`; } throw new codes.ERR_OUT_OF_RANGE("value", range, value); } checkBounds(buf, offset, byteLength2);}
function utf8ToBytes(string, units) { units = units || Infinity; let codePoint; const length = string.length; let leadSurrogate = null; const bytes = []; for (let i = 0; i < length; ++i) { codePoint = string.charCodeAt(i); if (codePoint > 55295 && codePoint < 57344) { if (!leadSurrogate) { if (codePoint > 56319) { if ((units -= 3) > -1) { bytes.push(239, 191, 189); } continue; } else if (i + 1 === length) { if ((units -= 3) > -1) { bytes.push(239, 191, 189); } continue; } leadSurrogate = codePoint; continue; } if (codePoint < 56320) { if ((units -= 3) > -1) { bytes.push(239, 191, 189); } leadSurrogate = codePoint; continue; } codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536; } else if (leadSurrogate) { if ((units -= 3) > -1) { bytes.push(239, 191, 189); } } leadSurrogate = null; if (codePoint < 128) { if ((units -= 1) < 0) { break; } bytes.push(codePoint); } else if (codePoint < 2048) { if ((units -= 2) < 0) { break; } bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128); } else if (codePoint < 65536) { if ((units -= 3) < 0) { break; } bytes.push( codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128, ); } else if (codePoint < 1114112) { if ((units -= 4) < 0) { break; } bytes.push( codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128, ); } else { throw new Error("Invalid code point"); } } return bytes;}
function blitBuffer(src, dst, offset, byteLength) { let i; const length = byteLength === undefined ? src.length : byteLength; for (i = 0; i < length; ++i) { if (i + offset >= dst.length || i >= src.length) { break; } dst[i + offset] = src[i]; } return i;}
function isInstance(obj, type) { return obj instanceof type || obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name;}
const hexSliceLookupTable = function () { const alphabet = "0123456789abcdef"; const table = new Array(256); for (let i = 0; i < 16; ++i) { const i16 = i * 16; for (let j = 0; j < 16; ++j) { table[i16 + j] = alphabet[i] + alphabet[j]; } } return table;}();
function defineBigIntMethod(fn) { return typeof BigInt === "undefined" ? BufferBigIntNotDefined : fn;}
function BufferBigIntNotDefined() { throw new Error("BigInt not supported");}
export const atob = globalThis.atob;export const Blob = globalThis.Blob;export const btoa = globalThis.btoa;
export function readUInt48LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
return first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24 + (buf[++offset] + last * 2 ** 8) * 2 ** 32;}
export function readUInt40LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24 + last * 2 ** 32;}
export function readUInt24LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
return first + buf[++offset] * 2 ** 8 + last * 2 ** 16;}
export function readUInt48BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
return (first * 2 ** 8 + buf[++offset]) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt40BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return first * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt24BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt16BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 2); }
return first * 2 ** 8 + last;}
export function readUInt32BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 4); }
return first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;}
export function readDoubleBackwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 7]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 8); }
uInt8Float64Array[7] = first; uInt8Float64Array[6] = buffer[++offset]; uInt8Float64Array[5] = buffer[++offset]; uInt8Float64Array[4] = buffer[++offset]; uInt8Float64Array[3] = buffer[++offset]; uInt8Float64Array[2] = buffer[++offset]; uInt8Float64Array[1] = buffer[++offset]; uInt8Float64Array[0] = last; return float64Array[0];}
export function readDoubleForwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 7]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 8); }
uInt8Float64Array[0] = first; uInt8Float64Array[1] = buffer[++offset]; uInt8Float64Array[2] = buffer[++offset]; uInt8Float64Array[3] = buffer[++offset]; uInt8Float64Array[4] = buffer[++offset]; uInt8Float64Array[5] = buffer[++offset]; uInt8Float64Array[6] = buffer[++offset]; uInt8Float64Array[7] = last; return float64Array[0];}
export function writeDoubleForwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 7);
float64Array[0] = val; buffer[offset++] = uInt8Float64Array[0]; buffer[offset++] = uInt8Float64Array[1]; buffer[offset++] = uInt8Float64Array[2]; buffer[offset++] = uInt8Float64Array[3]; buffer[offset++] = uInt8Float64Array[4]; buffer[offset++] = uInt8Float64Array[5]; buffer[offset++] = uInt8Float64Array[6]; buffer[offset++] = uInt8Float64Array[7]; return offset;}
export function writeDoubleBackwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 7);
float64Array[0] = val; buffer[offset++] = uInt8Float64Array[7]; buffer[offset++] = uInt8Float64Array[6]; buffer[offset++] = uInt8Float64Array[5]; buffer[offset++] = uInt8Float64Array[4]; buffer[offset++] = uInt8Float64Array[3]; buffer[offset++] = uInt8Float64Array[2]; buffer[offset++] = uInt8Float64Array[1]; buffer[offset++] = uInt8Float64Array[0]; return offset;}
export function readFloatBackwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 4); }
uInt8Float32Array[3] = first; uInt8Float32Array[2] = buffer[++offset]; uInt8Float32Array[1] = buffer[++offset]; uInt8Float32Array[0] = last; return float32Array[0];}
export function readFloatForwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 4); }
uInt8Float32Array[0] = first; uInt8Float32Array[1] = buffer[++offset]; uInt8Float32Array[2] = buffer[++offset]; uInt8Float32Array[3] = last; return float32Array[0];}
export function writeFloatForwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 3);
float32Array[0] = val; buffer[offset++] = uInt8Float32Array[0]; buffer[offset++] = uInt8Float32Array[1]; buffer[offset++] = uInt8Float32Array[2]; buffer[offset++] = uInt8Float32Array[3]; return offset;}
export function writeFloatBackwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 3);
float32Array[0] = val; buffer[offset++] = uInt8Float32Array[3]; buffer[offset++] = uInt8Float32Array[2]; buffer[offset++] = uInt8Float32Array[1]; buffer[offset++] = uInt8Float32Array[0]; return offset;}
export function readInt24LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
const val = first + buf[++offset] * 2 ** 8 + last * 2 ** 16; return val | (val & 2 ** 23) * 0x1fe;}
export function readInt40LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return (last | (last & 2 ** 7) * 0x1fffffe) * 2 ** 32 + first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24;}
export function readInt48LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
const val = buf[offset + 4] + last * 2 ** 8; return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 + first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24;}
export function readInt24BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
const val = first * 2 ** 16 + buf[++offset] * 2 ** 8 + last; return val | (val & 2 ** 23) * 0x1fe;}
export function readInt48BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
const val = buf[++offset] + first * 2 ** 8; return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readInt40BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return (first | (first & 2 ** 7) * 0x1fffffe) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function byteLengthUtf8(str) { return utf8Encoder.encode(str).length;}
function base64ByteLength(str, bytes) { // Handle padding if (str.charCodeAt(bytes - 1) === 0x3D) { bytes--; } if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D) { bytes--; }
// Base64 ratio: 3/4 return (bytes * 3) >>> 2;}
export const encodingsMap = Object.create(null);for (let i = 0; i < encodings.length; ++i) { encodingsMap[encodings[i]] = i;}
export const encodingOps = { ascii: { byteLength: (string) => string.length, encoding: "ascii", encodingVal: encodingsMap.ascii, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, asciiToBytes(val), byteOffset, encodingsMap.ascii, dir, ), slice: (buf, start, end) => buf.asciiSlice(start, end), write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len), }, base64: { byteLength: (string) => base64ByteLength(string, string.length), encoding: "base64", encodingVal: encodingsMap.base64, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, base64ToBytes(val), byteOffset, encodingsMap.base64, dir, ), slice: (buf, start, end) => buf.base64Slice(start, end), write: (buf, string, offset, len) => buf.base64Write(string, offset, len), }, base64url: { byteLength: (string) => base64ByteLength(string, string.length), encoding: "base64url", encodingVal: encodingsMap.base64url, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, base64UrlToBytes(val), byteOffset, encodingsMap.base64url, dir, ), slice: (buf, start, end) => buf.base64urlSlice(start, end), write: (buf, string, offset, len) => buf.base64urlWrite(string, offset, len), }, hex: { byteLength: (string) => string.length >>> 1, encoding: "hex", encodingVal: encodingsMap.hex, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, hexToBytes(val), byteOffset, encodingsMap.hex, dir, ), slice: (buf, start, end) => buf.hexSlice(start, end), write: (buf, string, offset, len) => buf.hexWrite(string, offset, len), }, latin1: { byteLength: (string) => string.length, encoding: "latin1", encodingVal: encodingsMap.latin1, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, asciiToBytes(val), byteOffset, encodingsMap.latin1, dir, ), slice: (buf, start, end) => buf.latin1Slice(start, end), write: (buf, string, offset, len) => buf.latin1Write(string, offset, len), }, ucs2: { byteLength: (string) => string.length * 2, encoding: "ucs2", encodingVal: encodingsMap.utf16le, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf16leToBytes(val), byteOffset, encodingsMap.utf16le, dir, ), slice: (buf, start, end) => buf.ucs2Slice(start, end), write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), }, utf8: { byteLength: byteLengthUtf8, encoding: "utf8", encodingVal: encodingsMap.utf8, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf8Encoder.encode(val), byteOffset, encodingsMap.utf8, dir, ), slice: (buf, start, end) => buf.utf8Slice(start, end), write: (buf, string, offset, len) => buf.utf8Write(string, offset, len), }, utf16le: { byteLength: (string) => string.length * 2, encoding: "utf16le", encodingVal: encodingsMap.utf16le, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf16leToBytes(val), byteOffset, encodingsMap.utf16le, dir, ), slice: (buf, start, end) => buf.ucs2Slice(start, end), write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), },};
export function getEncodingOps(encoding) { encoding = String(encoding).toLowerCase(); switch (encoding.length) { case 4: if (encoding === "utf8") return encodingOps.utf8; if (encoding === "ucs2") return encodingOps.ucs2; break; case 5: if (encoding === "utf-8") return encodingOps.utf8; if (encoding === "ascii") return encodingOps.ascii; if (encoding === "ucs-2") return encodingOps.ucs2; break; case 7: if (encoding === "utf16le") { return encodingOps.utf16le; } break; case 8: if (encoding === "utf-16le") { return encodingOps.utf16le; } break; // deno-lint-ignore no-fallthrough case 6: if (encoding === "latin1" || encoding === "binary") { return encodingOps.latin1; } if (encoding === "base64") return encodingOps.base64; case 3: if (encoding === "hex") { return encodingOps.hex; } break; case 9: if (encoding === "base64url") { return encodingOps.base64url; } break; }}
export function _copyActual( source, target, targetStart, sourceStart, sourceEnd,) { if (sourceEnd - sourceStart > target.length - targetStart) { sourceEnd = sourceStart + target.length - targetStart; }
let nb = sourceEnd - sourceStart; const sourceLen = source.length - sourceStart; if (nb > sourceLen) { nb = sourceLen; }
if (sourceStart !== 0 || sourceEnd < source.length) { source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb); }
target.set(source, targetStart);
return nb;}
export function boundsError(value, length, type) { if (Math.floor(value) !== value) { validateNumber(value, type); throw new codes.ERR_OUT_OF_RANGE(type || "offset", "an integer", value); }
if (length < 0) { throw new codes.ERR_BUFFER_OUT_OF_BOUNDS(); }
throw new codes.ERR_OUT_OF_RANGE( type || "offset", `>= ${type ? 1 : 0} and <= ${length}`, value, );}
export function validateNumber(value, name) { if (typeof value !== "number") { throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value); }}
function checkInt(value, min, max, buf, offset, byteLength) { if (value > max || value < min) { const n = typeof min === "bigint" ? "n" : ""; let range; if (byteLength > 3) { if (min === 0 || min === 0n) { range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`; } else { range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and ` + `< 2${n} ** ${(byteLength + 1) * 8 - 1}${n}`; } } else { range = `>= ${min}${n} and <= ${max}${n}`; } throw new codes.ERR_OUT_OF_RANGE("value", range, value); } checkBounds(buf, offset, byteLength);}
export function toInteger(n, defaultVal) { n = +n; if ( !Number.isNaN(n) && n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER ) { return ((n % 1) === 0 ? n : Math.floor(n)); } return defaultVal;}
// deno-lint-ignore camelcaseexport function writeU_Int8(buf, value, offset, min, max) { value = +value; validateNumber(offset, "offset"); if (value > max || value < min) { throw new codes.ERR_OUT_OF_RANGE("value", `>= ${min} and <= ${max}`, value); } if (buf[offset] === undefined) { boundsError(offset, buf.length - 1); }
buf[offset] = value; return offset + 1;}
// deno-lint-ignore camelcaseexport function writeU_Int16BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 1);
buf[offset++] = value >>> 8; buf[offset++] = value; return offset;}
export function _writeUInt32LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}
// deno-lint-ignore camelcaseexport function writeU_Int16LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 1);
buf[offset++] = value; buf[offset++] = value >>> 8; return offset;}
export function _writeUInt32BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
// deno-lint-ignore camelcaseexport function writeU_Int48BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32); buf[offset++] = newVal >>> 8; buf[offset++] = newVal; buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
// deno-lint-ignore camelcaseexport function writeU_Int40BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 4);
buf[offset++] = Math.floor(value * 2 ** -32); buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
// deno-lint-ignore camelcaseexport function writeU_Int32BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
// deno-lint-ignore camelcaseexport function writeU_Int24BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 2);
buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 3;}
export function validateOffset( value, name, min = 0, max = Number.MAX_SAFE_INTEGER,) { if (typeof value !== "number") { throw new codes.ERR_INVALID_ARG_TYPE(name, "number", value); } if (!Number.isInteger(value)) { throw new codes.ERR_OUT_OF_RANGE(name, "an integer", value); } if (value < min || value > max) { throw new codes.ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value); }}
// deno-lint-ignore camelcaseexport function writeU_Int48LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32); buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; buf[offset++] = newVal; buf[offset++] = newVal >>> 8; return offset;}
// deno-lint-ignore camelcaseexport function writeU_Int40LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 4);
const newVal = value; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; buf[offset++] = Math.floor(newVal * 2 ** -32); return offset;}
// deno-lint-ignore camelcaseexport function writeU_Int32LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}
// deno-lint-ignore camelcaseexport function writeU_Int24LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 2);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}
export default { atob, btoa, Blob, Buffer, constants, kMaxLength, kStringMaxLength, SlowBuffer,};