browser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. const ascii = require('./lib/ascii')
  2. const base64 = require('./lib/base64')
  3. const hex = require('./lib/hex')
  4. const utf8 = require('./lib/utf8')
  5. const utf16le = require('./lib/utf16le')
  6. const LE = new Uint8Array(Uint16Array.of(0xff).buffer)[0] === 0xff
  7. function codecFor(encoding) {
  8. switch (encoding) {
  9. case 'ascii':
  10. return ascii
  11. case 'base64':
  12. return base64
  13. case 'hex':
  14. return hex
  15. case 'utf8':
  16. case 'utf-8':
  17. case undefined:
  18. case null:
  19. return utf8
  20. case 'ucs2':
  21. case 'ucs-2':
  22. case 'utf16le':
  23. case 'utf-16le':
  24. return utf16le
  25. default:
  26. throw new Error(`Unknown encoding '${encoding}'`)
  27. }
  28. }
  29. function isBuffer(value) {
  30. return value instanceof Uint8Array
  31. }
  32. function isEncoding(encoding) {
  33. try {
  34. codecFor(encoding)
  35. return true
  36. } catch {
  37. return false
  38. }
  39. }
  40. function alloc(size, fill, encoding) {
  41. const buffer = new Uint8Array(size)
  42. if (fill !== undefined) {
  43. exports.fill(buffer, fill, 0, buffer.byteLength, encoding)
  44. }
  45. return buffer
  46. }
  47. function allocUnsafe(size) {
  48. return new Uint8Array(size)
  49. }
  50. function allocUnsafeSlow(size) {
  51. return new Uint8Array(size)
  52. }
  53. function byteLength(string, encoding) {
  54. return codecFor(encoding).byteLength(string)
  55. }
  56. function compare(a, b) {
  57. if (a === b) return 0
  58. const len = Math.min(a.byteLength, b.byteLength)
  59. a = new DataView(a.buffer, a.byteOffset, a.byteLength)
  60. b = new DataView(b.buffer, b.byteOffset, b.byteLength)
  61. let i = 0
  62. for (let n = len - (len % 4); i < n; i += 4) {
  63. const x = a.getUint32(i, LE)
  64. const y = b.getUint32(i, LE)
  65. if (x !== y) break
  66. }
  67. for (; i < len; i++) {
  68. const x = a.getUint8(i)
  69. const y = b.getUint8(i)
  70. if (x < y) return -1
  71. if (x > y) return 1
  72. }
  73. return a.byteLength > b.byteLength ? 1 : a.byteLength < b.byteLength ? -1 : 0
  74. }
  75. function concat(buffers, length) {
  76. if (length === undefined) {
  77. length = buffers.reduce((len, buffer) => len + buffer.byteLength, 0)
  78. }
  79. const result = new Uint8Array(length)
  80. let offset = 0
  81. for (const buffer of buffers) {
  82. if (offset + buffer.byteLength > result.byteLength) {
  83. result.set(buffer.subarray(0, result.byteLength - offset), offset)
  84. return result
  85. }
  86. result.set(buffer, offset)
  87. offset += buffer.byteLength
  88. }
  89. return result
  90. }
  91. function copy(
  92. source,
  93. target,
  94. targetStart = 0,
  95. sourceStart = 0,
  96. sourceEnd = source.byteLength
  97. ) {
  98. if (targetStart < 0) targetStart = 0
  99. if (targetStart >= target.byteLength) return 0
  100. const targetLength = target.byteLength - targetStart
  101. if (sourceStart < 0) sourceStart = 0
  102. if (sourceStart >= source.byteLength) return 0
  103. if (sourceEnd <= sourceStart) return 0
  104. if (sourceEnd > source.byteLength) sourceEnd = source.byteLength
  105. if (sourceEnd - sourceStart > targetLength) {
  106. sourceEnd = sourceStart + targetLength
  107. }
  108. const sourceLength = sourceEnd - sourceStart
  109. if (source === target) {
  110. target.copyWithin(targetStart, sourceStart, sourceEnd)
  111. } else {
  112. if (sourceStart !== 0 || sourceEnd !== source.byteLength) {
  113. source = source.subarray(sourceStart, sourceEnd)
  114. }
  115. target.set(source, targetStart)
  116. }
  117. return sourceLength
  118. }
  119. function equals(a, b) {
  120. if (a === b) return true
  121. if (a.byteLength !== b.byteLength) return false
  122. return compare(a, b) === 0
  123. }
  124. function fill(
  125. buffer,
  126. value,
  127. offset = 0,
  128. end = buffer.byteLength,
  129. encoding = 'utf8'
  130. ) {
  131. if (typeof value === 'string') {
  132. if (typeof offset === 'string') {
  133. // fill(string, encoding)
  134. encoding = offset
  135. offset = 0
  136. end = buffer.byteLength
  137. } else if (typeof end === 'string') {
  138. // fill(string, offset, encoding)
  139. encoding = end
  140. end = buffer.byteLength
  141. }
  142. } else if (typeof value === 'number') {
  143. value = value & 0xff
  144. } else if (typeof value === 'boolean') {
  145. value = +value
  146. }
  147. if (offset < 0) offset = 0
  148. if (offset >= buffer.byteLength) return buffer
  149. if (end <= offset) return buffer
  150. if (end > buffer.byteLength) end = buffer.byteLength
  151. if (typeof value === 'number') return buffer.fill(value, offset, end)
  152. if (typeof value === 'string') value = exports.from(value, encoding)
  153. const len = value.byteLength
  154. for (let i = 0, n = end - offset; i < n; ++i) {
  155. buffer[i + offset] = value[i % len]
  156. }
  157. return buffer
  158. }
  159. function from(value, encodingOrOffset, length) {
  160. // from(string, encoding)
  161. if (typeof value === 'string') return fromString(value, encodingOrOffset)
  162. // from(array)
  163. if (Array.isArray(value)) return fromArray(value)
  164. // from(buffer)
  165. if (ArrayBuffer.isView(value)) return fromBuffer(value)
  166. // from(arrayBuffer[, byteOffset[, length]])
  167. return fromArrayBuffer(value, encodingOrOffset, length)
  168. }
  169. function fromString(string, encoding) {
  170. const codec = codecFor(encoding)
  171. const buffer = new Uint8Array(codec.byteLength(string))
  172. codec.write(buffer, string)
  173. return buffer
  174. }
  175. function fromArray(array) {
  176. const buffer = new Uint8Array(array.length)
  177. buffer.set(array)
  178. return buffer
  179. }
  180. function fromBuffer(buffer) {
  181. const copy = new Uint8Array(buffer.byteLength)
  182. copy.set(buffer)
  183. return copy
  184. }
  185. function fromArrayBuffer(arrayBuffer, byteOffset, length) {
  186. return new Uint8Array(arrayBuffer, byteOffset, length)
  187. }
  188. function includes(buffer, value, byteOffset, encoding) {
  189. return indexOf(buffer, value, byteOffset, encoding) !== -1
  190. }
  191. function indexOf(buffer, value, byteOffset, encoding) {
  192. return bidirectionalIndexOf(
  193. buffer,
  194. value,
  195. byteOffset,
  196. encoding,
  197. true /* first */
  198. )
  199. }
  200. function lastIndexOf(buffer, value, byteOffset, encoding) {
  201. return bidirectionalIndexOf(
  202. buffer,
  203. value,
  204. byteOffset,
  205. encoding,
  206. false /* last */
  207. )
  208. }
  209. function bidirectionalIndexOf(buffer, value, byteOffset, encoding, first) {
  210. if (buffer.byteLength === 0) return -1
  211. if (typeof byteOffset === 'string') {
  212. encoding = byteOffset
  213. byteOffset = 0
  214. } else if (byteOffset === undefined) {
  215. byteOffset = first ? 0 : buffer.length - 1
  216. } else if (byteOffset < 0) {
  217. byteOffset += buffer.byteLength
  218. }
  219. if (byteOffset >= buffer.byteLength) {
  220. if (first) return -1
  221. else byteOffset = buffer.byteLength - 1
  222. } else if (byteOffset < 0) {
  223. if (first) byteOffset = 0
  224. else return -1
  225. }
  226. if (typeof value === 'string') {
  227. value = from(value, encoding)
  228. } else if (typeof value === 'number') {
  229. value = value & 0xff
  230. if (first) {
  231. return buffer.indexOf(value, byteOffset)
  232. } else {
  233. return buffer.lastIndexOf(value, byteOffset)
  234. }
  235. }
  236. if (value.byteLength === 0) return -1
  237. if (first) {
  238. let foundIndex = -1
  239. for (let i = byteOffset; i < buffer.byteLength; i++) {
  240. if (buffer[i] === value[foundIndex === -1 ? 0 : i - foundIndex]) {
  241. if (foundIndex === -1) foundIndex = i
  242. if (i - foundIndex + 1 === value.byteLength) return foundIndex
  243. } else {
  244. if (foundIndex !== -1) i -= i - foundIndex
  245. foundIndex = -1
  246. }
  247. }
  248. } else {
  249. if (byteOffset + value.byteLength > buffer.byteLength) {
  250. byteOffset = buffer.byteLength - value.byteLength
  251. }
  252. for (let i = byteOffset; i >= 0; i--) {
  253. let found = true
  254. for (let j = 0; j < value.byteLength; j++) {
  255. if (buffer[i + j] !== value[j]) {
  256. found = false
  257. break
  258. }
  259. }
  260. if (found) return i
  261. }
  262. }
  263. return -1
  264. }
  265. function swap(buffer, n, m) {
  266. const i = buffer[n]
  267. buffer[n] = buffer[m]
  268. buffer[m] = i
  269. }
  270. function swap16(buffer) {
  271. const len = buffer.byteLength
  272. if (len % 2 !== 0)
  273. throw new RangeError('Buffer size must be a multiple of 16-bits')
  274. for (let i = 0; i < len; i += 2) swap(buffer, i, i + 1)
  275. return buffer
  276. }
  277. function swap32(buffer) {
  278. const len = buffer.byteLength
  279. if (len % 4 !== 0)
  280. throw new RangeError('Buffer size must be a multiple of 32-bits')
  281. for (let i = 0; i < len; i += 4) {
  282. swap(buffer, i, i + 3)
  283. swap(buffer, i + 1, i + 2)
  284. }
  285. return buffer
  286. }
  287. function swap64(buffer) {
  288. const len = buffer.byteLength
  289. if (len % 8 !== 0)
  290. throw new RangeError('Buffer size must be a multiple of 64-bits')
  291. for (let i = 0; i < len; i += 8) {
  292. swap(buffer, i, i + 7)
  293. swap(buffer, i + 1, i + 6)
  294. swap(buffer, i + 2, i + 5)
  295. swap(buffer, i + 3, i + 4)
  296. }
  297. return buffer
  298. }
  299. function toBuffer(buffer) {
  300. return buffer
  301. }
  302. function toString(
  303. buffer,
  304. encoding = 'utf8',
  305. start = 0,
  306. end = buffer.byteLength
  307. ) {
  308. // toString(buffer)
  309. if (arguments.length === 1) return utf8.toString(buffer)
  310. // toString(buffer, encoding)
  311. if (arguments.length === 2) return codecFor(encoding).toString(buffer)
  312. if (start < 0) start = 0
  313. if (start >= buffer.byteLength) return ''
  314. if (end <= start) return ''
  315. if (end > buffer.byteLength) end = buffer.byteLength
  316. if (start !== 0 || end !== buffer.byteLength) {
  317. buffer = buffer.subarray(start, end)
  318. }
  319. return codecFor(encoding).toString(buffer)
  320. }
  321. function write(buffer, string, offset, length, encoding) {
  322. // write(buffer, string)
  323. if (arguments.length === 2) return utf8.write(buffer, string)
  324. if (typeof offset === 'string') {
  325. // write(buffer, string, encoding)
  326. encoding = offset
  327. offset = 0
  328. length = buffer.byteLength
  329. } else if (typeof length === 'string') {
  330. // write(buffer, string, offset, encoding)
  331. encoding = length
  332. length = buffer.byteLength - offset
  333. }
  334. length = Math.min(length, exports.byteLength(string, encoding))
  335. let start = offset
  336. if (start < 0) start = 0
  337. if (start >= buffer.byteLength) return 0
  338. let end = offset + length
  339. if (end <= start) return 0
  340. if (end > buffer.byteLength) end = buffer.byteLength
  341. if (start !== 0 || end !== buffer.byteLength) {
  342. buffer = buffer.subarray(start, end)
  343. }
  344. return codecFor(encoding).write(buffer, string)
  345. }
  346. function readDoubleBE(buffer, offset = 0) {
  347. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  348. return view.getFloat64(offset, false)
  349. }
  350. function readDoubleLE(buffer, offset = 0) {
  351. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  352. return view.getFloat64(offset, true)
  353. }
  354. function readFloatBE(buffer, offset = 0) {
  355. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  356. return view.getFloat32(offset, false)
  357. }
  358. function readFloatLE(buffer, offset = 0) {
  359. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  360. return view.getFloat32(offset, true)
  361. }
  362. function readInt32BE(buffer, offset = 0) {
  363. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  364. return view.getInt32(offset, false)
  365. }
  366. function readInt32LE(buffer, offset = 0) {
  367. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  368. return view.getInt32(offset, true)
  369. }
  370. function readUInt32BE(buffer, offset = 0) {
  371. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  372. return view.getUint32(offset, false)
  373. }
  374. function readUInt32LE(buffer, offset = 0) {
  375. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  376. return view.getUint32(offset, true)
  377. }
  378. function writeDoubleBE(buffer, value, offset = 0) {
  379. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  380. view.setFloat64(offset, value, false)
  381. return offset + 8
  382. }
  383. function writeDoubleLE(buffer, value, offset = 0) {
  384. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  385. view.setFloat64(offset, value, true)
  386. return offset + 8
  387. }
  388. function writeFloatBE(buffer, value, offset = 0) {
  389. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  390. view.setFloat32(offset, value, false)
  391. return offset + 4
  392. }
  393. function writeFloatLE(buffer, value, offset = 0) {
  394. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  395. view.setFloat32(offset, value, true)
  396. return offset + 4
  397. }
  398. function writeInt32BE(buffer, value, offset = 0) {
  399. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  400. view.setInt32(offset, value, false)
  401. return offset + 4
  402. }
  403. function writeInt32LE(buffer, value, offset = 0) {
  404. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  405. view.setInt32(offset, value, true)
  406. return offset + 4
  407. }
  408. function writeUInt32BE(buffer, value, offset = 0) {
  409. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  410. view.setUint32(offset, value, false)
  411. return offset + 4
  412. }
  413. function writeUInt32LE(buffer, value, offset = 0) {
  414. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
  415. view.setUint32(offset, value, true)
  416. return offset + 4
  417. }
  418. module.exports = exports = {
  419. isBuffer,
  420. isEncoding,
  421. alloc,
  422. allocUnsafe,
  423. allocUnsafeSlow,
  424. byteLength,
  425. compare,
  426. concat,
  427. copy,
  428. equals,
  429. fill,
  430. from,
  431. includes,
  432. indexOf,
  433. lastIndexOf,
  434. swap16,
  435. swap32,
  436. swap64,
  437. toBuffer,
  438. toString,
  439. write,
  440. readDoubleBE,
  441. readDoubleLE,
  442. readFloatBE,
  443. readFloatLE,
  444. readInt32BE,
  445. readInt32LE,
  446. readUInt32BE,
  447. readUInt32LE,
  448. writeDoubleBE,
  449. writeDoubleLE,
  450. writeFloatBE,
  451. writeFloatLE,
  452. writeInt32BE,
  453. writeInt32LE,
  454. writeUInt32BE,
  455. writeUInt32LE
  456. }