index.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*!
  2. * The buffer module from node.js, for the browser.
  3. *
  4. * @author Feross Aboukhadijeh <https://feross.org>
  5. * @license MIT
  6. */
  7. /* eslint-disable no-proto */
  8. 'use strict'
  9. const base64 = require('base64-js')
  10. const ieee754 = require('ieee754')
  11. const customInspectSymbol =
  12. (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
  13. ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
  14. : null
  15. exports.Buffer = Buffer
  16. exports.SlowBuffer = SlowBuffer
  17. exports.INSPECT_MAX_BYTES = 50
  18. const K_MAX_LENGTH = 0x7fffffff
  19. exports.kMaxLength = K_MAX_LENGTH
  20. /**
  21. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  22. * === true Use Uint8Array implementation (fastest)
  23. * === false Print warning and recommend using `buffer` v4.x which has an Object
  24. * implementation (most compatible, even IE6)
  25. *
  26. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  27. * Opera 11.6+, iOS 4.2+.
  28. *
  29. * We report that the browser does not support typed arrays if the are not subclassable
  30. * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
  31. * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
  32. * for __proto__ and has a buggy typed array implementation.
  33. */
  34. Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
  35. if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
  36. typeof console.error === 'function') {
  37. console.error(
  38. 'This browser lacks typed array (Uint8Array) support which is required by ' +
  39. '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
  40. )
  41. }
  42. function typedArraySupport () {
  43. // Can typed array instances can be augmented?
  44. try {
  45. const arr = new Uint8Array(1)
  46. const proto = { foo: function () { return 42 } }
  47. Object.setPrototypeOf(proto, Uint8Array.prototype)
  48. Object.setPrototypeOf(arr, proto)
  49. return arr.foo() === 42
  50. } catch (e) {
  51. return false
  52. }
  53. }
  54. Object.defineProperty(Buffer.prototype, 'parent', {
  55. enumerable: true,
  56. get: function () {
  57. if (!Buffer.isBuffer(this)) return undefined
  58. return this.buffer
  59. }
  60. })
  61. Object.defineProperty(Buffer.prototype, 'offset', {
  62. enumerable: true,
  63. get: function () {
  64. if (!Buffer.isBuffer(this)) return undefined
  65. return this.byteOffset
  66. }
  67. })
  68. function createBuffer (length) {
  69. if (length > K_MAX_LENGTH) {
  70. throw new RangeError('The value "' + length + '" is invalid for option "size"')
  71. }
  72. // Return an augmented `Uint8Array` instance
  73. const buf = new Uint8Array(length)
  74. Object.setPrototypeOf(buf, Buffer.prototype)
  75. return buf
  76. }
  77. /**
  78. * The Buffer constructor returns instances of `Uint8Array` that have their
  79. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  80. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  81. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  82. * returns a single octet.
  83. *
  84. * The `Uint8Array` prototype remains unmodified.
  85. */
  86. function Buffer (arg, encodingOrOffset, length) {
  87. // Common case.
  88. if (typeof arg === 'number') {
  89. if (typeof encodingOrOffset === 'string') {
  90. throw new TypeError(
  91. 'The "string" argument must be of type string. Received type number'
  92. )
  93. }
  94. return allocUnsafe(arg)
  95. }
  96. return from(arg, encodingOrOffset, length)
  97. }
  98. Buffer.poolSize = 8192 // not used by this implementation
  99. function from (value, encodingOrOffset, length) {
  100. if (typeof value === 'string') {
  101. return fromString(value, encodingOrOffset)
  102. }
  103. if (ArrayBuffer.isView(value)) {
  104. return fromArrayView(value)
  105. }
  106. if (value == null) {
  107. throw new TypeError(
  108. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  109. 'or Array-like Object. Received type ' + (typeof value)
  110. )
  111. }
  112. if (isInstance(value, ArrayBuffer) ||
  113. (value && isInstance(value.buffer, ArrayBuffer))) {
  114. return fromArrayBuffer(value, encodingOrOffset, length)
  115. }
  116. if (typeof SharedArrayBuffer !== 'undefined' &&
  117. (isInstance(value, SharedArrayBuffer) ||
  118. (value && isInstance(value.buffer, SharedArrayBuffer)))) {
  119. return fromArrayBuffer(value, encodingOrOffset, length)
  120. }
  121. if (typeof value === 'number') {
  122. throw new TypeError(
  123. 'The "value" argument must not be of type number. Received type number'
  124. )
  125. }
  126. const valueOf = value.valueOf && value.valueOf()
  127. if (valueOf != null && valueOf !== value) {
  128. return Buffer.from(valueOf, encodingOrOffset, length)
  129. }
  130. const b = fromObject(value)
  131. if (b) return b
  132. if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
  133. typeof value[Symbol.toPrimitive] === 'function') {
  134. return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length)
  135. }
  136. throw new TypeError(
  137. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  138. 'or Array-like Object. Received type ' + (typeof value)
  139. )
  140. }
  141. /**
  142. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  143. * if value is a number.
  144. * Buffer.from(str[, encoding])
  145. * Buffer.from(array)
  146. * Buffer.from(buffer)
  147. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  148. **/
  149. Buffer.from = function (value, encodingOrOffset, length) {
  150. return from(value, encodingOrOffset, length)
  151. }
  152. // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
  153. // https://github.com/feross/buffer/pull/148
  154. Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype)
  155. Object.setPrototypeOf(Buffer, Uint8Array)
  156. function assertSize (size) {
  157. if (typeof size !== 'number') {
  158. throw new TypeError('"size" argument must be of type number')
  159. } else if (size < 0) {
  160. throw new RangeError('The value "' + size + '" is invalid for option "size"')
  161. }
  162. }
  163. function alloc (size, fill, encoding) {
  164. assertSize(size)
  165. if (size <= 0) {
  166. return createBuffer(size)
  167. }
  168. if (fill !== undefined) {
  169. // Only pay attention to encoding if it's a string. This
  170. // prevents accidentally sending in a number that would
  171. // be interpreted as a start offset.
  172. return typeof encoding === 'string'
  173. ? createBuffer(size).fill(fill, encoding)
  174. : createBuffer(size).fill(fill)
  175. }
  176. return createBuffer(size)
  177. }
  178. /**
  179. * Creates a new filled Buffer instance.
  180. * alloc(size[, fill[, encoding]])
  181. **/
  182. Buffer.alloc = function (size, fill, encoding) {
  183. return alloc(size, fill, encoding)
  184. }
  185. function allocUnsafe (size) {
  186. assertSize(size)
  187. return createBuffer(size < 0 ? 0 : checked(size) | 0)
  188. }
  189. /**
  190. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  191. * */
  192. Buffer.allocUnsafe = function (size) {
  193. return allocUnsafe(size)
  194. }
  195. /**
  196. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  197. */
  198. Buffer.allocUnsafeSlow = function (size) {
  199. return allocUnsafe(size)
  200. }
  201. function fromString (string, encoding) {
  202. if (typeof encoding !== 'string' || encoding === '') {
  203. encoding = 'utf8'
  204. }
  205. if (!Buffer.isEncoding(encoding)) {
  206. throw new TypeError('Unknown encoding: ' + encoding)
  207. }
  208. const length = byteLength(string, encoding) | 0
  209. let buf = createBuffer(length)
  210. const actual = buf.write(string, encoding)
  211. if (actual !== length) {
  212. // Writing a hex string, for example, that contains invalid characters will
  213. // cause everything after the first invalid character to be ignored. (e.g.
  214. // 'abxxcd' will be treated as 'ab')
  215. buf = buf.slice(0, actual)
  216. }
  217. return buf
  218. }
  219. function fromArrayLike (array) {
  220. const length = array.length < 0 ? 0 : checked(array.length) | 0
  221. const buf = createBuffer(length)
  222. for (let i = 0; i < length; i += 1) {
  223. buf[i] = array[i] & 255
  224. }
  225. return buf
  226. }
  227. function fromArrayView (arrayView) {
  228. if (isInstance(arrayView, Uint8Array)) {
  229. const copy = new Uint8Array(arrayView)
  230. return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
  231. }
  232. return fromArrayLike(arrayView)
  233. }
  234. function fromArrayBuffer (array, byteOffset, length) {
  235. if (byteOffset < 0 || array.byteLength < byteOffset) {
  236. throw new RangeError('"offset" is outside of buffer bounds')
  237. }
  238. if (array.byteLength < byteOffset + (length || 0)) {
  239. throw new RangeError('"length" is outside of buffer bounds')
  240. }
  241. let buf
  242. if (byteOffset === undefined && length === undefined) {
  243. buf = new Uint8Array(array)
  244. } else if (length === undefined) {
  245. buf = new Uint8Array(array, byteOffset)
  246. } else {
  247. buf = new Uint8Array(array, byteOffset, length)
  248. }
  249. // Return an augmented `Uint8Array` instance
  250. Object.setPrototypeOf(buf, Buffer.prototype)
  251. return buf
  252. }
  253. function fromObject (obj) {
  254. if (Buffer.isBuffer(obj)) {
  255. const len = checked(obj.length) | 0
  256. const buf = createBuffer(len)
  257. if (buf.length === 0) {
  258. return buf
  259. }
  260. obj.copy(buf, 0, 0, len)
  261. return buf
  262. }
  263. if (obj.length !== undefined) {
  264. if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
  265. return createBuffer(0)
  266. }
  267. return fromArrayLike(obj)
  268. }
  269. if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
  270. return fromArrayLike(obj.data)
  271. }
  272. }
  273. function checked (length) {
  274. // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
  275. // length is NaN (which is otherwise coerced to zero.)
  276. if (length >= K_MAX_LENGTH) {
  277. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  278. 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
  279. }
  280. return length | 0
  281. }
  282. function SlowBuffer (length) {
  283. if (+length != length) { // eslint-disable-line eqeqeq
  284. length = 0
  285. }
  286. return Buffer.alloc(+length)
  287. }
  288. Buffer.isBuffer = function isBuffer (b) {
  289. return b != null && b._isBuffer === true &&
  290. b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
  291. }
  292. Buffer.compare = function compare (a, b) {
  293. if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
  294. if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
  295. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  296. throw new TypeError(
  297. 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
  298. )
  299. }
  300. if (a === b) return 0
  301. let x = a.length
  302. let y = b.length
  303. for (let i = 0, len = Math.min(x, y); i < len; ++i) {
  304. if (a[i] !== b[i]) {
  305. x = a[i]
  306. y = b[i]
  307. break
  308. }
  309. }
  310. if (x < y) return -1
  311. if (y < x) return 1
  312. return 0
  313. }
  314. Buffer.isEncoding = function isEncoding (encoding) {
  315. switch (String(encoding).toLowerCase()) {
  316. case 'hex':
  317. case 'utf8':
  318. case 'utf-8':
  319. case 'ascii':
  320. case 'latin1':
  321. case 'binary':
  322. case 'base64':
  323. case 'ucs2':
  324. case 'ucs-2':
  325. case 'utf16le':
  326. case 'utf-16le':
  327. return true
  328. default:
  329. return false
  330. }
  331. }
  332. Buffer.concat = function concat (list, length) {
  333. if (!Array.isArray(list)) {
  334. throw new TypeError('"list" argument must be an Array of Buffers')
  335. }
  336. if (list.length === 0) {
  337. return Buffer.alloc(0)
  338. }
  339. let i
  340. if (length === undefined) {
  341. length = 0
  342. for (i = 0; i < list.length; ++i) {
  343. length += list[i].length
  344. }
  345. }
  346. const buffer = Buffer.allocUnsafe(length)
  347. let pos = 0
  348. for (i = 0; i < list.length; ++i) {
  349. let buf = list[i]
  350. if (isInstance(buf, Uint8Array)) {
  351. if (pos + buf.length > buffer.length) {
  352. if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
  353. buf.copy(buffer, pos)
  354. } else {
  355. Uint8Array.prototype.set.call(
  356. buffer,
  357. buf,
  358. pos
  359. )
  360. }
  361. } else if (!Buffer.isBuffer(buf)) {
  362. throw new TypeError('"list" argument must be an Array of Buffers')
  363. } else {
  364. buf.copy(buffer, pos)
  365. }
  366. pos += buf.length
  367. }
  368. return buffer
  369. }
  370. function byteLength (string, encoding) {
  371. if (Buffer.isBuffer(string)) {
  372. return string.length
  373. }
  374. if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
  375. return string.byteLength
  376. }
  377. if (typeof string !== 'string') {
  378. throw new TypeError(
  379. 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
  380. 'Received type ' + typeof string
  381. )
  382. }
  383. const len = string.length
  384. const mustMatch = (arguments.length > 2 && arguments[2] === true)
  385. if (!mustMatch && len === 0) return 0
  386. // Use a for loop to avoid recursion
  387. let loweredCase = false
  388. for (;;) {
  389. switch (encoding) {
  390. case 'ascii':
  391. case 'latin1':
  392. case 'binary':
  393. return len
  394. case 'utf8':
  395. case 'utf-8':
  396. return utf8ToBytes(string).length
  397. case 'ucs2':
  398. case 'ucs-2':
  399. case 'utf16le':
  400. case 'utf-16le':
  401. return len * 2
  402. case 'hex':
  403. return len >>> 1
  404. case 'base64':
  405. return base64ToBytes(string).length
  406. default:
  407. if (loweredCase) {
  408. return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
  409. }
  410. encoding = ('' + encoding).toLowerCase()
  411. loweredCase = true
  412. }
  413. }
  414. }
  415. Buffer.byteLength = byteLength
  416. function slowToString (encoding, start, end) {
  417. let loweredCase = false
  418. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  419. // property of a typed array.
  420. // This behaves neither like String nor Uint8Array in that we set start/end
  421. // to their upper/lower bounds if the value passed is out of range.
  422. // undefined is handled specially as per ECMA-262 6th Edition,
  423. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  424. if (start === undefined || start < 0) {
  425. start = 0
  426. }
  427. // Return early if start > this.length. Done here to prevent potential uint32
  428. // coercion fail below.
  429. if (start > this.length) {
  430. return ''
  431. }
  432. if (end === undefined || end > this.length) {
  433. end = this.length
  434. }
  435. if (end <= 0) {
  436. return ''
  437. }
  438. // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
  439. end >>>= 0
  440. start >>>= 0
  441. if (end <= start) {
  442. return ''
  443. }
  444. if (!encoding) encoding = 'utf8'
  445. while (true) {
  446. switch (encoding) {
  447. case 'hex':
  448. return hexSlice(this, start, end)
  449. case 'utf8':
  450. case 'utf-8':
  451. return utf8Slice(this, start, end)
  452. case 'ascii':
  453. return asciiSlice(this, start, end)
  454. case 'latin1':
  455. case 'binary':
  456. return latin1Slice(this, start, end)
  457. case 'base64':
  458. return base64Slice(this, start, end)
  459. case 'ucs2':
  460. case 'ucs-2':
  461. case 'utf16le':
  462. case 'utf-16le':
  463. return utf16leSlice(this, start, end)
  464. default:
  465. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  466. encoding = (encoding + '').toLowerCase()
  467. loweredCase = true
  468. }
  469. }
  470. }
  471. // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
  472. // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
  473. // reliably in a browserify context because there could be multiple different
  474. // copies of the 'buffer' package in use. This method works even for Buffer
  475. // instances that were created from another copy of the `buffer` package.
  476. // See: https://github.com/feross/buffer/issues/154
  477. Buffer.prototype._isBuffer = true
  478. function swap (b, n, m) {
  479. const i = b[n]
  480. b[n] = b[m]
  481. b[m] = i
  482. }
  483. Buffer.prototype.swap16 = function swap16 () {
  484. const len = this.length
  485. if (len % 2 !== 0) {
  486. throw new RangeError('Buffer size must be a multiple of 16-bits')
  487. }
  488. for (let i = 0; i < len; i += 2) {
  489. swap(this, i, i + 1)
  490. }
  491. return this
  492. }
  493. Buffer.prototype.swap32 = function swap32 () {
  494. const len = this.length
  495. if (len % 4 !== 0) {
  496. throw new RangeError('Buffer size must be a multiple of 32-bits')
  497. }
  498. for (let i = 0; i < len; i += 4) {
  499. swap(this, i, i + 3)
  500. swap(this, i + 1, i + 2)
  501. }
  502. return this
  503. }
  504. Buffer.prototype.swap64 = function swap64 () {
  505. const len = this.length
  506. if (len % 8 !== 0) {
  507. throw new RangeError('Buffer size must be a multiple of 64-bits')
  508. }
  509. for (let i = 0; i < len; i += 8) {
  510. swap(this, i, i + 7)
  511. swap(this, i + 1, i + 6)
  512. swap(this, i + 2, i + 5)
  513. swap(this, i + 3, i + 4)
  514. }
  515. return this
  516. }
  517. Buffer.prototype.toString = function toString () {
  518. const length = this.length
  519. if (length === 0) return ''
  520. if (arguments.length === 0) return utf8Slice(this, 0, length)
  521. return slowToString.apply(this, arguments)
  522. }
  523. Buffer.prototype.toLocaleString = Buffer.prototype.toString
  524. Buffer.prototype.equals = function equals (b) {
  525. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  526. if (this === b) return true
  527. return Buffer.compare(this, b) === 0
  528. }
  529. Buffer.prototype.inspect = function inspect () {
  530. let str = ''
  531. const max = exports.INSPECT_MAX_BYTES
  532. str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
  533. if (this.length > max) str += ' ... '
  534. return '<Buffer ' + str + '>'
  535. }
  536. if (customInspectSymbol) {
  537. Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect
  538. }
  539. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  540. if (isInstance(target, Uint8Array)) {
  541. target = Buffer.from(target, target.offset, target.byteLength)
  542. }
  543. if (!Buffer.isBuffer(target)) {
  544. throw new TypeError(
  545. 'The "target" argument must be one of type Buffer or Uint8Array. ' +
  546. 'Received type ' + (typeof target)
  547. )
  548. }
  549. if (start === undefined) {
  550. start = 0
  551. }
  552. if (end === undefined) {
  553. end = target ? target.length : 0
  554. }
  555. if (thisStart === undefined) {
  556. thisStart = 0
  557. }
  558. if (thisEnd === undefined) {
  559. thisEnd = this.length
  560. }
  561. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  562. throw new RangeError('out of range index')
  563. }
  564. if (thisStart >= thisEnd && start >= end) {
  565. return 0
  566. }
  567. if (thisStart >= thisEnd) {
  568. return -1
  569. }
  570. if (start >= end) {
  571. return 1
  572. }
  573. start >>>= 0
  574. end >>>= 0
  575. thisStart >>>= 0
  576. thisEnd >>>= 0
  577. if (this === target) return 0
  578. let x = thisEnd - thisStart
  579. let y = end - start
  580. const len = Math.min(x, y)
  581. const thisCopy = this.slice(thisStart, thisEnd)
  582. const targetCopy = target.slice(start, end)
  583. for (let i = 0; i < len; ++i) {
  584. if (thisCopy[i] !== targetCopy[i]) {
  585. x = thisCopy[i]
  586. y = targetCopy[i]
  587. break
  588. }
  589. }
  590. if (x < y) return -1
  591. if (y < x) return 1
  592. return 0
  593. }
  594. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  595. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  596. //
  597. // Arguments:
  598. // - buffer - a Buffer to search
  599. // - val - a string, Buffer, or number
  600. // - byteOffset - an index into `buffer`; will be clamped to an int32
  601. // - encoding - an optional encoding, relevant is val is a string
  602. // - dir - true for indexOf, false for lastIndexOf
  603. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  604. // Empty buffer means no match
  605. if (buffer.length === 0) return -1
  606. // Normalize byteOffset
  607. if (typeof byteOffset === 'string') {
  608. encoding = byteOffset
  609. byteOffset = 0
  610. } else if (byteOffset > 0x7fffffff) {
  611. byteOffset = 0x7fffffff
  612. } else if (byteOffset < -0x80000000) {
  613. byteOffset = -0x80000000
  614. }
  615. byteOffset = +byteOffset // Coerce to Number.
  616. if (numberIsNaN(byteOffset)) {
  617. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  618. byteOffset = dir ? 0 : (buffer.length - 1)
  619. }
  620. // Normalize byteOffset: negative offsets start from the end of the buffer
  621. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  622. if (byteOffset >= buffer.length) {
  623. if (dir) return -1
  624. else byteOffset = buffer.length - 1
  625. } else if (byteOffset < 0) {
  626. if (dir) byteOffset = 0
  627. else return -1
  628. }
  629. // Normalize val
  630. if (typeof val === 'string') {
  631. val = Buffer.from(val, encoding)
  632. }
  633. // Finally, search either indexOf (if dir is true) or lastIndexOf
  634. if (Buffer.isBuffer(val)) {
  635. // Special case: looking for empty string/buffer always fails
  636. if (val.length === 0) {
  637. return -1
  638. }
  639. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  640. } else if (typeof val === 'number') {
  641. val = val & 0xFF // Search for a byte value [0-255]
  642. if (typeof Uint8Array.prototype.indexOf === 'function') {
  643. if (dir) {
  644. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  645. } else {
  646. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  647. }
  648. }
  649. return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
  650. }
  651. throw new TypeError('val must be string, number or Buffer')
  652. }
  653. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  654. let indexSize = 1
  655. let arrLength = arr.length
  656. let valLength = val.length
  657. if (encoding !== undefined) {
  658. encoding = String(encoding).toLowerCase()
  659. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  660. encoding === 'utf16le' || encoding === 'utf-16le') {
  661. if (arr.length < 2 || val.length < 2) {
  662. return -1
  663. }
  664. indexSize = 2
  665. arrLength /= 2
  666. valLength /= 2
  667. byteOffset /= 2
  668. }
  669. }
  670. function read (buf, i) {
  671. if (indexSize === 1) {
  672. return buf[i]
  673. } else {
  674. return buf.readUInt16BE(i * indexSize)
  675. }
  676. }
  677. let i
  678. if (dir) {
  679. let foundIndex = -1
  680. for (i = byteOffset; i < arrLength; i++) {
  681. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  682. if (foundIndex === -1) foundIndex = i
  683. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  684. } else {
  685. if (foundIndex !== -1) i -= i - foundIndex
  686. foundIndex = -1
  687. }
  688. }
  689. } else {
  690. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  691. for (i = byteOffset; i >= 0; i--) {
  692. let found = true
  693. for (let j = 0; j < valLength; j++) {
  694. if (read(arr, i + j) !== read(val, j)) {
  695. found = false
  696. break
  697. }
  698. }
  699. if (found) return i
  700. }
  701. }
  702. return -1
  703. }
  704. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  705. return this.indexOf(val, byteOffset, encoding) !== -1
  706. }
  707. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  708. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  709. }
  710. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  711. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  712. }
  713. function hexWrite (buf, string, offset, length) {
  714. offset = Number(offset) || 0
  715. const remaining = buf.length - offset
  716. if (!length) {
  717. length = remaining
  718. } else {
  719. length = Number(length)
  720. if (length > remaining) {
  721. length = remaining
  722. }
  723. }
  724. const strLen = string.length
  725. if (length > strLen / 2) {
  726. length = strLen / 2
  727. }
  728. let i
  729. for (i = 0; i < length; ++i) {
  730. const parsed = parseInt(string.substr(i * 2, 2), 16)
  731. if (numberIsNaN(parsed)) return i
  732. buf[offset + i] = parsed
  733. }
  734. return i
  735. }
  736. function utf8Write (buf, string, offset, length) {
  737. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  738. }
  739. function asciiWrite (buf, string, offset, length) {
  740. return blitBuffer(asciiToBytes(string), buf, offset, length)
  741. }
  742. function base64Write (buf, string, offset, length) {
  743. return blitBuffer(base64ToBytes(string), buf, offset, length)
  744. }
  745. function ucs2Write (buf, string, offset, length) {
  746. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  747. }
  748. Buffer.prototype.write = function write (string, offset, length, encoding) {
  749. // Buffer#write(string)
  750. if (offset === undefined) {
  751. encoding = 'utf8'
  752. length = this.length
  753. offset = 0
  754. // Buffer#write(string, encoding)
  755. } else if (length === undefined && typeof offset === 'string') {
  756. encoding = offset
  757. length = this.length
  758. offset = 0
  759. // Buffer#write(string, offset[, length][, encoding])
  760. } else if (isFinite(offset)) {
  761. offset = offset >>> 0
  762. if (isFinite(length)) {
  763. length = length >>> 0
  764. if (encoding === undefined) encoding = 'utf8'
  765. } else {
  766. encoding = length
  767. length = undefined
  768. }
  769. } else {
  770. throw new Error(
  771. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  772. )
  773. }
  774. const remaining = this.length - offset
  775. if (length === undefined || length > remaining) length = remaining
  776. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  777. throw new RangeError('Attempt to write outside buffer bounds')
  778. }
  779. if (!encoding) encoding = 'utf8'
  780. let loweredCase = false
  781. for (;;) {
  782. switch (encoding) {
  783. case 'hex':
  784. return hexWrite(this, string, offset, length)
  785. case 'utf8':
  786. case 'utf-8':
  787. return utf8Write(this, string, offset, length)
  788. case 'ascii':
  789. case 'latin1':
  790. case 'binary':
  791. return asciiWrite(this, string, offset, length)
  792. case 'base64':
  793. // Warning: maxLength not taken into account in base64Write
  794. return base64Write(this, string, offset, length)
  795. case 'ucs2':
  796. case 'ucs-2':
  797. case 'utf16le':
  798. case 'utf-16le':
  799. return ucs2Write(this, string, offset, length)
  800. default:
  801. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  802. encoding = ('' + encoding).toLowerCase()
  803. loweredCase = true
  804. }
  805. }
  806. }
  807. Buffer.prototype.toJSON = function toJSON () {
  808. return {
  809. type: 'Buffer',
  810. data: Array.prototype.slice.call(this._arr || this, 0)
  811. }
  812. }
  813. function base64Slice (buf, start, end) {
  814. if (start === 0 && end === buf.length) {
  815. return base64.fromByteArray(buf)
  816. } else {
  817. return base64.fromByteArray(buf.slice(start, end))
  818. }
  819. }
  820. function utf8Slice (buf, start, end) {
  821. end = Math.min(buf.length, end)
  822. const res = []
  823. let i = start
  824. while (i < end) {
  825. const firstByte = buf[i]
  826. let codePoint = null
  827. let bytesPerSequence = (firstByte > 0xEF)
  828. ? 4
  829. : (firstByte > 0xDF)
  830. ? 3
  831. : (firstByte > 0xBF)
  832. ? 2
  833. : 1
  834. if (i + bytesPerSequence <= end) {
  835. let secondByte, thirdByte, fourthByte, tempCodePoint
  836. switch (bytesPerSequence) {
  837. case 1:
  838. if (firstByte < 0x80) {
  839. codePoint = firstByte
  840. }
  841. break
  842. case 2:
  843. secondByte = buf[i + 1]
  844. if ((secondByte & 0xC0) === 0x80) {
  845. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  846. if (tempCodePoint > 0x7F) {
  847. codePoint = tempCodePoint
  848. }
  849. }
  850. break
  851. case 3:
  852. secondByte = buf[i + 1]
  853. thirdByte = buf[i + 2]
  854. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  855. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  856. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  857. codePoint = tempCodePoint
  858. }
  859. }
  860. break
  861. case 4:
  862. secondByte = buf[i + 1]
  863. thirdByte = buf[i + 2]
  864. fourthByte = buf[i + 3]
  865. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  866. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  867. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  868. codePoint = tempCodePoint
  869. }
  870. }
  871. }
  872. }
  873. if (codePoint === null) {
  874. // we did not generate a valid codePoint so insert a
  875. // replacement char (U+FFFD) and advance only 1 byte
  876. codePoint = 0xFFFD
  877. bytesPerSequence = 1
  878. } else if (codePoint > 0xFFFF) {
  879. // encode to utf16 (surrogate pair dance)
  880. codePoint -= 0x10000
  881. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  882. codePoint = 0xDC00 | codePoint & 0x3FF
  883. }
  884. res.push(codePoint)
  885. i += bytesPerSequence
  886. }
  887. return decodeCodePointsArray(res)
  888. }
  889. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  890. // the lowest limit is Chrome, with 0x10000 args.
  891. // We go 1 magnitude less, for safety
  892. const MAX_ARGUMENTS_LENGTH = 0x1000
  893. function decodeCodePointsArray (codePoints) {
  894. const len = codePoints.length
  895. if (len <= MAX_ARGUMENTS_LENGTH) {
  896. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  897. }
  898. // Decode in chunks to avoid "call stack size exceeded".
  899. let res = ''
  900. let i = 0
  901. while (i < len) {
  902. res += String.fromCharCode.apply(
  903. String,
  904. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  905. )
  906. }
  907. return res
  908. }
  909. function asciiSlice (buf, start, end) {
  910. let ret = ''
  911. end = Math.min(buf.length, end)
  912. for (let i = start; i < end; ++i) {
  913. ret += String.fromCharCode(buf[i] & 0x7F)
  914. }
  915. return ret
  916. }
  917. function latin1Slice (buf, start, end) {
  918. let ret = ''
  919. end = Math.min(buf.length, end)
  920. for (let i = start; i < end; ++i) {
  921. ret += String.fromCharCode(buf[i])
  922. }
  923. return ret
  924. }
  925. function hexSlice (buf, start, end) {
  926. const len = buf.length
  927. if (!start || start < 0) start = 0
  928. if (!end || end < 0 || end > len) end = len
  929. let out = ''
  930. for (let i = start; i < end; ++i) {
  931. out += hexSliceLookupTable[buf[i]]
  932. }
  933. return out
  934. }
  935. function utf16leSlice (buf, start, end) {
  936. const bytes = buf.slice(start, end)
  937. let res = ''
  938. // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
  939. for (let i = 0; i < bytes.length - 1; i += 2) {
  940. res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
  941. }
  942. return res
  943. }
  944. Buffer.prototype.slice = function slice (start, end) {
  945. const len = this.length
  946. start = ~~start
  947. end = end === undefined ? len : ~~end
  948. if (start < 0) {
  949. start += len
  950. if (start < 0) start = 0
  951. } else if (start > len) {
  952. start = len
  953. }
  954. if (end < 0) {
  955. end += len
  956. if (end < 0) end = 0
  957. } else if (end > len) {
  958. end = len
  959. }
  960. if (end < start) end = start
  961. const newBuf = this.subarray(start, end)
  962. // Return an augmented `Uint8Array` instance
  963. Object.setPrototypeOf(newBuf, Buffer.prototype)
  964. return newBuf
  965. }
  966. /*
  967. * Need to make sure that buffer isn't trying to write out of bounds.
  968. */
  969. function checkOffset (offset, ext, length) {
  970. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  971. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  972. }
  973. Buffer.prototype.readUintLE =
  974. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  975. offset = offset >>> 0
  976. byteLength = byteLength >>> 0
  977. if (!noAssert) checkOffset(offset, byteLength, this.length)
  978. let val = this[offset]
  979. let mul = 1
  980. let i = 0
  981. while (++i < byteLength && (mul *= 0x100)) {
  982. val += this[offset + i] * mul
  983. }
  984. return val
  985. }
  986. Buffer.prototype.readUintBE =
  987. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  988. offset = offset >>> 0
  989. byteLength = byteLength >>> 0
  990. if (!noAssert) {
  991. checkOffset(offset, byteLength, this.length)
  992. }
  993. let val = this[offset + --byteLength]
  994. let mul = 1
  995. while (byteLength > 0 && (mul *= 0x100)) {
  996. val += this[offset + --byteLength] * mul
  997. }
  998. return val
  999. }
  1000. Buffer.prototype.readUint8 =
  1001. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  1002. offset = offset >>> 0
  1003. if (!noAssert) checkOffset(offset, 1, this.length)
  1004. return this[offset]
  1005. }
  1006. Buffer.prototype.readUint16LE =
  1007. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  1008. offset = offset >>> 0
  1009. if (!noAssert) checkOffset(offset, 2, this.length)
  1010. return this[offset] | (this[offset + 1] << 8)
  1011. }
  1012. Buffer.prototype.readUint16BE =
  1013. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  1014. offset = offset >>> 0
  1015. if (!noAssert) checkOffset(offset, 2, this.length)
  1016. return (this[offset] << 8) | this[offset + 1]
  1017. }
  1018. Buffer.prototype.readUint32LE =
  1019. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  1020. offset = offset >>> 0
  1021. if (!noAssert) checkOffset(offset, 4, this.length)
  1022. return ((this[offset]) |
  1023. (this[offset + 1] << 8) |
  1024. (this[offset + 2] << 16)) +
  1025. (this[offset + 3] * 0x1000000)
  1026. }
  1027. Buffer.prototype.readUint32BE =
  1028. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  1029. offset = offset >>> 0
  1030. if (!noAssert) checkOffset(offset, 4, this.length)
  1031. return (this[offset] * 0x1000000) +
  1032. ((this[offset + 1] << 16) |
  1033. (this[offset + 2] << 8) |
  1034. this[offset + 3])
  1035. }
  1036. Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (offset) {
  1037. offset = offset >>> 0
  1038. validateNumber(offset, 'offset')
  1039. const first = this[offset]
  1040. const last = this[offset + 7]
  1041. if (first === undefined || last === undefined) {
  1042. boundsError(offset, this.length - 8)
  1043. }
  1044. const lo = first +
  1045. this[++offset] * 2 ** 8 +
  1046. this[++offset] * 2 ** 16 +
  1047. this[++offset] * 2 ** 24
  1048. const hi = this[++offset] +
  1049. this[++offset] * 2 ** 8 +
  1050. this[++offset] * 2 ** 16 +
  1051. last * 2 ** 24
  1052. return BigInt(lo) + (BigInt(hi) << BigInt(32))
  1053. })
  1054. Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (offset) {
  1055. offset = offset >>> 0
  1056. validateNumber(offset, 'offset')
  1057. const first = this[offset]
  1058. const last = this[offset + 7]
  1059. if (first === undefined || last === undefined) {
  1060. boundsError(offset, this.length - 8)
  1061. }
  1062. const hi = first * 2 ** 24 +
  1063. this[++offset] * 2 ** 16 +
  1064. this[++offset] * 2 ** 8 +
  1065. this[++offset]
  1066. const lo = this[++offset] * 2 ** 24 +
  1067. this[++offset] * 2 ** 16 +
  1068. this[++offset] * 2 ** 8 +
  1069. last
  1070. return (BigInt(hi) << BigInt(32)) + BigInt(lo)
  1071. })
  1072. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  1073. offset = offset >>> 0
  1074. byteLength = byteLength >>> 0
  1075. if (!noAssert) checkOffset(offset, byteLength, this.length)
  1076. let val = this[offset]
  1077. let mul = 1
  1078. let i = 0
  1079. while (++i < byteLength && (mul *= 0x100)) {
  1080. val += this[offset + i] * mul
  1081. }
  1082. mul *= 0x80
  1083. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  1084. return val
  1085. }
  1086. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  1087. offset = offset >>> 0
  1088. byteLength = byteLength >>> 0
  1089. if (!noAssert) checkOffset(offset, byteLength, this.length)
  1090. let i = byteLength
  1091. let mul = 1
  1092. let val = this[offset + --i]
  1093. while (i > 0 && (mul *= 0x100)) {
  1094. val += this[offset + --i] * mul
  1095. }
  1096. mul *= 0x80
  1097. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  1098. return val
  1099. }
  1100. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  1101. offset = offset >>> 0
  1102. if (!noAssert) checkOffset(offset, 1, this.length)
  1103. if (!(this[offset] & 0x80)) return (this[offset])
  1104. return ((0xff - this[offset] + 1) * -1)
  1105. }
  1106. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  1107. offset = offset >>> 0
  1108. if (!noAssert) checkOffset(offset, 2, this.length)
  1109. const val = this[offset] | (this[offset + 1] << 8)
  1110. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1111. }
  1112. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  1113. offset = offset >>> 0
  1114. if (!noAssert) checkOffset(offset, 2, this.length)
  1115. const val = this[offset + 1] | (this[offset] << 8)
  1116. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1117. }
  1118. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  1119. offset = offset >>> 0
  1120. if (!noAssert) checkOffset(offset, 4, this.length)
  1121. return (this[offset]) |
  1122. (this[offset + 1] << 8) |
  1123. (this[offset + 2] << 16) |
  1124. (this[offset + 3] << 24)
  1125. }
  1126. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  1127. offset = offset >>> 0
  1128. if (!noAssert) checkOffset(offset, 4, this.length)
  1129. return (this[offset] << 24) |
  1130. (this[offset + 1] << 16) |
  1131. (this[offset + 2] << 8) |
  1132. (this[offset + 3])
  1133. }
  1134. Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE (offset) {
  1135. offset = offset >>> 0
  1136. validateNumber(offset, 'offset')
  1137. const first = this[offset]
  1138. const last = this[offset + 7]
  1139. if (first === undefined || last === undefined) {
  1140. boundsError(offset, this.length - 8)
  1141. }
  1142. const val = this[offset + 4] +
  1143. this[offset + 5] * 2 ** 8 +
  1144. this[offset + 6] * 2 ** 16 +
  1145. (last << 24) // Overflow
  1146. return (BigInt(val) << BigInt(32)) +
  1147. BigInt(first +
  1148. this[++offset] * 2 ** 8 +
  1149. this[++offset] * 2 ** 16 +
  1150. this[++offset] * 2 ** 24)
  1151. })
  1152. Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE (offset) {
  1153. offset = offset >>> 0
  1154. validateNumber(offset, 'offset')
  1155. const first = this[offset]
  1156. const last = this[offset + 7]
  1157. if (first === undefined || last === undefined) {
  1158. boundsError(offset, this.length - 8)
  1159. }
  1160. const val = (first << 24) + // Overflow
  1161. this[++offset] * 2 ** 16 +
  1162. this[++offset] * 2 ** 8 +
  1163. this[++offset]
  1164. return (BigInt(val) << BigInt(32)) +
  1165. BigInt(this[++offset] * 2 ** 24 +
  1166. this[++offset] * 2 ** 16 +
  1167. this[++offset] * 2 ** 8 +
  1168. last)
  1169. })
  1170. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  1171. offset = offset >>> 0
  1172. if (!noAssert) checkOffset(offset, 4, this.length)
  1173. return ieee754.read(this, offset, true, 23, 4)
  1174. }
  1175. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  1176. offset = offset >>> 0
  1177. if (!noAssert) checkOffset(offset, 4, this.length)
  1178. return ieee754.read(this, offset, false, 23, 4)
  1179. }
  1180. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  1181. offset = offset >>> 0
  1182. if (!noAssert) checkOffset(offset, 8, this.length)
  1183. return ieee754.read(this, offset, true, 52, 8)
  1184. }
  1185. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  1186. offset = offset >>> 0
  1187. if (!noAssert) checkOffset(offset, 8, this.length)
  1188. return ieee754.read(this, offset, false, 52, 8)
  1189. }
  1190. function checkInt (buf, value, offset, ext, max, min) {
  1191. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  1192. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  1193. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1194. }
  1195. Buffer.prototype.writeUintLE =
  1196. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  1197. value = +value
  1198. offset = offset >>> 0
  1199. byteLength = byteLength >>> 0
  1200. if (!noAssert) {
  1201. const maxBytes = Math.pow(2, 8 * byteLength) - 1
  1202. checkInt(this, value, offset, byteLength, maxBytes, 0)
  1203. }
  1204. let mul = 1
  1205. let i = 0
  1206. this[offset] = value & 0xFF
  1207. while (++i < byteLength && (mul *= 0x100)) {
  1208. this[offset + i] = (value / mul) & 0xFF
  1209. }
  1210. return offset + byteLength
  1211. }
  1212. Buffer.prototype.writeUintBE =
  1213. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  1214. value = +value
  1215. offset = offset >>> 0
  1216. byteLength = byteLength >>> 0
  1217. if (!noAssert) {
  1218. const maxBytes = Math.pow(2, 8 * byteLength) - 1
  1219. checkInt(this, value, offset, byteLength, maxBytes, 0)
  1220. }
  1221. let i = byteLength - 1
  1222. let mul = 1
  1223. this[offset + i] = value & 0xFF
  1224. while (--i >= 0 && (mul *= 0x100)) {
  1225. this[offset + i] = (value / mul) & 0xFF
  1226. }
  1227. return offset + byteLength
  1228. }
  1229. Buffer.prototype.writeUint8 =
  1230. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  1231. value = +value
  1232. offset = offset >>> 0
  1233. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  1234. this[offset] = (value & 0xff)
  1235. return offset + 1
  1236. }
  1237. Buffer.prototype.writeUint16LE =
  1238. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  1239. value = +value
  1240. offset = offset >>> 0
  1241. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  1242. this[offset] = (value & 0xff)
  1243. this[offset + 1] = (value >>> 8)
  1244. return offset + 2
  1245. }
  1246. Buffer.prototype.writeUint16BE =
  1247. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  1248. value = +value
  1249. offset = offset >>> 0
  1250. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  1251. this[offset] = (value >>> 8)
  1252. this[offset + 1] = (value & 0xff)
  1253. return offset + 2
  1254. }
  1255. Buffer.prototype.writeUint32LE =
  1256. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  1257. value = +value
  1258. offset = offset >>> 0
  1259. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  1260. this[offset + 3] = (value >>> 24)
  1261. this[offset + 2] = (value >>> 16)
  1262. this[offset + 1] = (value >>> 8)
  1263. this[offset] = (value & 0xff)
  1264. return offset + 4
  1265. }
  1266. Buffer.prototype.writeUint32BE =
  1267. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  1268. value = +value
  1269. offset = offset >>> 0
  1270. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  1271. this[offset] = (value >>> 24)
  1272. this[offset + 1] = (value >>> 16)
  1273. this[offset + 2] = (value >>> 8)
  1274. this[offset + 3] = (value & 0xff)
  1275. return offset + 4
  1276. }
  1277. function wrtBigUInt64LE (buf, value, offset, min, max) {
  1278. checkIntBI(value, min, max, buf, offset, 7)
  1279. let lo = Number(value & BigInt(0xffffffff))
  1280. buf[offset++] = lo
  1281. lo = lo >> 8
  1282. buf[offset++] = lo
  1283. lo = lo >> 8
  1284. buf[offset++] = lo
  1285. lo = lo >> 8
  1286. buf[offset++] = lo
  1287. let hi = Number(value >> BigInt(32) & BigInt(0xffffffff))
  1288. buf[offset++] = hi
  1289. hi = hi >> 8
  1290. buf[offset++] = hi
  1291. hi = hi >> 8
  1292. buf[offset++] = hi
  1293. hi = hi >> 8
  1294. buf[offset++] = hi
  1295. return offset
  1296. }
  1297. function wrtBigUInt64BE (buf, value, offset, min, max) {
  1298. checkIntBI(value, min, max, buf, offset, 7)
  1299. let lo = Number(value & BigInt(0xffffffff))
  1300. buf[offset + 7] = lo
  1301. lo = lo >> 8
  1302. buf[offset + 6] = lo
  1303. lo = lo >> 8
  1304. buf[offset + 5] = lo
  1305. lo = lo >> 8
  1306. buf[offset + 4] = lo
  1307. let hi = Number(value >> BigInt(32) & BigInt(0xffffffff))
  1308. buf[offset + 3] = hi
  1309. hi = hi >> 8
  1310. buf[offset + 2] = hi
  1311. hi = hi >> 8
  1312. buf[offset + 1] = hi
  1313. hi = hi >> 8
  1314. buf[offset] = hi
  1315. return offset + 8
  1316. }
  1317. Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE (value, offset = 0) {
  1318. return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
  1319. })
  1320. Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE (value, offset = 0) {
  1321. return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
  1322. })
  1323. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  1324. value = +value
  1325. offset = offset >>> 0
  1326. if (!noAssert) {
  1327. const limit = Math.pow(2, (8 * byteLength) - 1)
  1328. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  1329. }
  1330. let i = 0
  1331. let mul = 1
  1332. let sub = 0
  1333. this[offset] = value & 0xFF
  1334. while (++i < byteLength && (mul *= 0x100)) {
  1335. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  1336. sub = 1
  1337. }
  1338. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  1339. }
  1340. return offset + byteLength
  1341. }
  1342. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  1343. value = +value
  1344. offset = offset >>> 0
  1345. if (!noAssert) {
  1346. const limit = Math.pow(2, (8 * byteLength) - 1)
  1347. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  1348. }
  1349. let i = byteLength - 1
  1350. let mul = 1
  1351. let sub = 0
  1352. this[offset + i] = value & 0xFF
  1353. while (--i >= 0 && (mul *= 0x100)) {
  1354. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  1355. sub = 1
  1356. }
  1357. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  1358. }
  1359. return offset + byteLength
  1360. }
  1361. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  1362. value = +value
  1363. offset = offset >>> 0
  1364. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  1365. if (value < 0) value = 0xff + value + 1
  1366. this[offset] = (value & 0xff)
  1367. return offset + 1
  1368. }
  1369. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  1370. value = +value
  1371. offset = offset >>> 0
  1372. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  1373. this[offset] = (value & 0xff)
  1374. this[offset + 1] = (value >>> 8)
  1375. return offset + 2
  1376. }
  1377. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  1378. value = +value
  1379. offset = offset >>> 0
  1380. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  1381. this[offset] = (value >>> 8)
  1382. this[offset + 1] = (value & 0xff)
  1383. return offset + 2
  1384. }
  1385. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  1386. value = +value
  1387. offset = offset >>> 0
  1388. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  1389. this[offset] = (value & 0xff)
  1390. this[offset + 1] = (value >>> 8)
  1391. this[offset + 2] = (value >>> 16)
  1392. this[offset + 3] = (value >>> 24)
  1393. return offset + 4
  1394. }
  1395. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  1396. value = +value
  1397. offset = offset >>> 0
  1398. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  1399. if (value < 0) value = 0xffffffff + value + 1
  1400. this[offset] = (value >>> 24)
  1401. this[offset + 1] = (value >>> 16)
  1402. this[offset + 2] = (value >>> 8)
  1403. this[offset + 3] = (value & 0xff)
  1404. return offset + 4
  1405. }
  1406. Buffer.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE (value, offset = 0) {
  1407. return wrtBigUInt64LE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
  1408. })
  1409. Buffer.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE (value, offset = 0) {
  1410. return wrtBigUInt64BE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
  1411. })
  1412. function checkIEEE754 (buf, value, offset, ext, max, min) {
  1413. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1414. if (offset < 0) throw new RangeError('Index out of range')
  1415. }
  1416. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  1417. value = +value
  1418. offset = offset >>> 0
  1419. if (!noAssert) {
  1420. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  1421. }
  1422. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  1423. return offset + 4
  1424. }
  1425. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  1426. return writeFloat(this, value, offset, true, noAssert)
  1427. }
  1428. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  1429. return writeFloat(this, value, offset, false, noAssert)
  1430. }
  1431. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  1432. value = +value
  1433. offset = offset >>> 0
  1434. if (!noAssert) {
  1435. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  1436. }
  1437. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  1438. return offset + 8
  1439. }
  1440. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  1441. return writeDouble(this, value, offset, true, noAssert)
  1442. }
  1443. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  1444. return writeDouble(this, value, offset, false, noAssert)
  1445. }
  1446. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  1447. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  1448. if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
  1449. if (!start) start = 0
  1450. if (!end && end !== 0) end = this.length
  1451. if (targetStart >= target.length) targetStart = target.length
  1452. if (!targetStart) targetStart = 0
  1453. if (end > 0 && end < start) end = start
  1454. // Copy 0 bytes; we're done
  1455. if (end === start) return 0
  1456. if (target.length === 0 || this.length === 0) return 0
  1457. // Fatal error conditions
  1458. if (targetStart < 0) {
  1459. throw new RangeError('targetStart out of bounds')
  1460. }
  1461. if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
  1462. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  1463. // Are we oob?
  1464. if (end > this.length) end = this.length
  1465. if (target.length - targetStart < end - start) {
  1466. end = target.length - targetStart + start
  1467. }
  1468. const len = end - start
  1469. if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
  1470. // Use built-in when available, missing from IE11
  1471. this.copyWithin(targetStart, start, end)
  1472. } else {
  1473. Uint8Array.prototype.set.call(
  1474. target,
  1475. this.subarray(start, end),
  1476. targetStart
  1477. )
  1478. }
  1479. return len
  1480. }
  1481. // Usage:
  1482. // buffer.fill(number[, offset[, end]])
  1483. // buffer.fill(buffer[, offset[, end]])
  1484. // buffer.fill(string[, offset[, end]][, encoding])
  1485. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  1486. // Handle string cases:
  1487. if (typeof val === 'string') {
  1488. if (typeof start === 'string') {
  1489. encoding = start
  1490. start = 0
  1491. end = this.length
  1492. } else if (typeof end === 'string') {
  1493. encoding = end
  1494. end = this.length
  1495. }
  1496. if (encoding !== undefined && typeof encoding !== 'string') {
  1497. throw new TypeError('encoding must be a string')
  1498. }
  1499. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  1500. throw new TypeError('Unknown encoding: ' + encoding)
  1501. }
  1502. if (val.length === 1) {
  1503. const code = val.charCodeAt(0)
  1504. if ((encoding === 'utf8' && code < 128) ||
  1505. encoding === 'latin1') {
  1506. // Fast path: If `val` fits into a single byte, use that numeric value.
  1507. val = code
  1508. }
  1509. }
  1510. } else if (typeof val === 'number') {
  1511. val = val & 255
  1512. } else if (typeof val === 'boolean') {
  1513. val = Number(val)
  1514. }
  1515. // Invalid ranges are not set to a default, so can range check early.
  1516. if (start < 0 || this.length < start || this.length < end) {
  1517. throw new RangeError('Out of range index')
  1518. }
  1519. if (end <= start) {
  1520. return this
  1521. }
  1522. start = start >>> 0
  1523. end = end === undefined ? this.length : end >>> 0
  1524. if (!val) val = 0
  1525. let i
  1526. if (typeof val === 'number') {
  1527. for (i = start; i < end; ++i) {
  1528. this[i] = val
  1529. }
  1530. } else {
  1531. const bytes = Buffer.isBuffer(val)
  1532. ? val
  1533. : Buffer.from(val, encoding)
  1534. const len = bytes.length
  1535. if (len === 0) {
  1536. throw new TypeError('The value "' + val +
  1537. '" is invalid for argument "value"')
  1538. }
  1539. for (i = 0; i < end - start; ++i) {
  1540. this[i + start] = bytes[i % len]
  1541. }
  1542. }
  1543. return this
  1544. }
  1545. // CUSTOM ERRORS
  1546. // =============
  1547. // Simplified versions from Node, changed for Buffer-only usage
  1548. const errors = {}
  1549. function E (sym, getMessage, Base) {
  1550. errors[sym] = class NodeError extends Base {
  1551. constructor () {
  1552. super()
  1553. Object.defineProperty(this, 'message', {
  1554. value: getMessage.apply(this, arguments),
  1555. writable: true,
  1556. configurable: true
  1557. })
  1558. // Add the error code to the name to include it in the stack trace.
  1559. this.name = `${this.name} [${sym}]`
  1560. // Access the stack to generate the error message including the error code
  1561. // from the name.
  1562. this.stack // eslint-disable-line no-unused-expressions
  1563. // Reset the name to the actual name.
  1564. delete this.name
  1565. }
  1566. get code () {
  1567. return sym
  1568. }
  1569. set code (value) {
  1570. Object.defineProperty(this, 'code', {
  1571. configurable: true,
  1572. enumerable: true,
  1573. value,
  1574. writable: true
  1575. })
  1576. }
  1577. toString () {
  1578. return `${this.name} [${sym}]: ${this.message}`
  1579. }
  1580. }
  1581. }
  1582. E('ERR_BUFFER_OUT_OF_BOUNDS',
  1583. function (name) {
  1584. if (name) {
  1585. return `${name} is outside of buffer bounds`
  1586. }
  1587. return 'Attempt to access memory outside buffer bounds'
  1588. }, RangeError)
  1589. E('ERR_INVALID_ARG_TYPE',
  1590. function (name, actual) {
  1591. return `The "${name}" argument must be of type number. Received type ${typeof actual}`
  1592. }, TypeError)
  1593. E('ERR_OUT_OF_RANGE',
  1594. function (str, range, input) {
  1595. let msg = `The value of "${str}" is out of range.`
  1596. let received = input
  1597. if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
  1598. received = addNumericalSeparator(String(input))
  1599. } else if (typeof input === 'bigint') {
  1600. received = String(input)
  1601. if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
  1602. received = addNumericalSeparator(received)
  1603. }
  1604. received += 'n'
  1605. }
  1606. msg += ` It must be ${range}. Received ${received}`
  1607. return msg
  1608. }, RangeError)
  1609. function addNumericalSeparator (val) {
  1610. let res = ''
  1611. let i = val.length
  1612. const start = val[0] === '-' ? 1 : 0
  1613. for (; i >= start + 4; i -= 3) {
  1614. res = `_${val.slice(i - 3, i)}${res}`
  1615. }
  1616. return `${val.slice(0, i)}${res}`
  1617. }
  1618. // CHECK FUNCTIONS
  1619. // ===============
  1620. function checkBounds (buf, offset, byteLength) {
  1621. validateNumber(offset, 'offset')
  1622. if (buf[offset] === undefined || buf[offset + byteLength] === undefined) {
  1623. boundsError(offset, buf.length - (byteLength + 1))
  1624. }
  1625. }
  1626. function checkIntBI (value, min, max, buf, offset, byteLength) {
  1627. if (value > max || value < min) {
  1628. const n = typeof min === 'bigint' ? 'n' : ''
  1629. let range
  1630. if (byteLength > 3) {
  1631. if (min === 0 || min === BigInt(0)) {
  1632. range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`
  1633. } else {
  1634. range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
  1635. `${(byteLength + 1) * 8 - 1}${n}`
  1636. }
  1637. } else {
  1638. range = `>= ${min}${n} and <= ${max}${n}`
  1639. }
  1640. throw new errors.ERR_OUT_OF_RANGE('value', range, value)
  1641. }
  1642. checkBounds(buf, offset, byteLength)
  1643. }
  1644. function validateNumber (value, name) {
  1645. if (typeof value !== 'number') {
  1646. throw new errors.ERR_INVALID_ARG_TYPE(name, 'number', value)
  1647. }
  1648. }
  1649. function boundsError (value, length, type) {
  1650. if (Math.floor(value) !== value) {
  1651. validateNumber(value, type)
  1652. throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value)
  1653. }
  1654. if (length < 0) {
  1655. throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
  1656. }
  1657. throw new errors.ERR_OUT_OF_RANGE(type || 'offset',
  1658. `>= ${type ? 1 : 0} and <= ${length}`,
  1659. value)
  1660. }
  1661. // HELPER FUNCTIONS
  1662. // ================
  1663. const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
  1664. function base64clean (str) {
  1665. // Node takes equal signs as end of the Base64 encoding
  1666. str = str.split('=')[0]
  1667. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  1668. str = str.trim().replace(INVALID_BASE64_RE, '')
  1669. // Node converts strings with length < 2 to ''
  1670. if (str.length < 2) return ''
  1671. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  1672. while (str.length % 4 !== 0) {
  1673. str = str + '='
  1674. }
  1675. return str
  1676. }
  1677. function utf8ToBytes (string, units) {
  1678. units = units || Infinity
  1679. let codePoint
  1680. const length = string.length
  1681. let leadSurrogate = null
  1682. const bytes = []
  1683. for (let i = 0; i < length; ++i) {
  1684. codePoint = string.charCodeAt(i)
  1685. // is surrogate component
  1686. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  1687. // last char was a lead
  1688. if (!leadSurrogate) {
  1689. // no lead yet
  1690. if (codePoint > 0xDBFF) {
  1691. // unexpected trail
  1692. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1693. continue
  1694. } else if (i + 1 === length) {
  1695. // unpaired lead
  1696. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1697. continue
  1698. }
  1699. // valid lead
  1700. leadSurrogate = codePoint
  1701. continue
  1702. }
  1703. // 2 leads in a row
  1704. if (codePoint < 0xDC00) {
  1705. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1706. leadSurrogate = codePoint
  1707. continue
  1708. }
  1709. // valid surrogate pair
  1710. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  1711. } else if (leadSurrogate) {
  1712. // valid bmp char, but last char was a lead
  1713. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1714. }
  1715. leadSurrogate = null
  1716. // encode utf8
  1717. if (codePoint < 0x80) {
  1718. if ((units -= 1) < 0) break
  1719. bytes.push(codePoint)
  1720. } else if (codePoint < 0x800) {
  1721. if ((units -= 2) < 0) break
  1722. bytes.push(
  1723. codePoint >> 0x6 | 0xC0,
  1724. codePoint & 0x3F | 0x80
  1725. )
  1726. } else if (codePoint < 0x10000) {
  1727. if ((units -= 3) < 0) break
  1728. bytes.push(
  1729. codePoint >> 0xC | 0xE0,
  1730. codePoint >> 0x6 & 0x3F | 0x80,
  1731. codePoint & 0x3F | 0x80
  1732. )
  1733. } else if (codePoint < 0x110000) {
  1734. if ((units -= 4) < 0) break
  1735. bytes.push(
  1736. codePoint >> 0x12 | 0xF0,
  1737. codePoint >> 0xC & 0x3F | 0x80,
  1738. codePoint >> 0x6 & 0x3F | 0x80,
  1739. codePoint & 0x3F | 0x80
  1740. )
  1741. } else {
  1742. throw new Error('Invalid code point')
  1743. }
  1744. }
  1745. return bytes
  1746. }
  1747. function asciiToBytes (str) {
  1748. const byteArray = []
  1749. for (let i = 0; i < str.length; ++i) {
  1750. // Node's code seems to be doing this and not & 0x7F..
  1751. byteArray.push(str.charCodeAt(i) & 0xFF)
  1752. }
  1753. return byteArray
  1754. }
  1755. function utf16leToBytes (str, units) {
  1756. let c, hi, lo
  1757. const byteArray = []
  1758. for (let i = 0; i < str.length; ++i) {
  1759. if ((units -= 2) < 0) break
  1760. c = str.charCodeAt(i)
  1761. hi = c >> 8
  1762. lo = c % 256
  1763. byteArray.push(lo)
  1764. byteArray.push(hi)
  1765. }
  1766. return byteArray
  1767. }
  1768. function base64ToBytes (str) {
  1769. return base64.toByteArray(base64clean(str))
  1770. }
  1771. function blitBuffer (src, dst, offset, length) {
  1772. let i
  1773. for (i = 0; i < length; ++i) {
  1774. if ((i + offset >= dst.length) || (i >= src.length)) break
  1775. dst[i + offset] = src[i]
  1776. }
  1777. return i
  1778. }
  1779. // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
  1780. // the `instanceof` check but they should be treated as of that type.
  1781. // See: https://github.com/feross/buffer/issues/166
  1782. function isInstance (obj, type) {
  1783. return obj instanceof type ||
  1784. (obj != null && obj.constructor != null && obj.constructor.name != null &&
  1785. obj.constructor.name === type.name)
  1786. }
  1787. function numberIsNaN (obj) {
  1788. // For IE11 support
  1789. return obj !== obj // eslint-disable-line no-self-compare
  1790. }
  1791. // Create lookup table for `toString('hex')`
  1792. // See: https://github.com/feross/buffer/issues/219
  1793. const hexSliceLookupTable = (function () {
  1794. const alphabet = '0123456789abcdef'
  1795. const table = new Array(256)
  1796. for (let i = 0; i < 16; ++i) {
  1797. const i16 = i * 16
  1798. for (let j = 0; j < 16; ++j) {
  1799. table[i16 + j] = alphabet[i] + alphabet[j]
  1800. }
  1801. }
  1802. return table
  1803. })()
  1804. // Return not function with Error if BigInt not supported
  1805. function defineBigIntMethod (fn) {
  1806. return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
  1807. }
  1808. function BufferBigIntNotDefined () {
  1809. throw new Error('BigInt not supported')
  1810. }