BufferList.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. 'use strict'
  2. const { Buffer } = require('buffer')
  3. const symbol = Symbol.for('BufferList')
  4. function BufferList (buf) {
  5. if (!(this instanceof BufferList)) {
  6. return new BufferList(buf)
  7. }
  8. BufferList._init.call(this, buf)
  9. }
  10. BufferList._init = function _init (buf) {
  11. Object.defineProperty(this, symbol, { value: true })
  12. this._bufs = []
  13. this.length = 0
  14. if (buf) {
  15. this.append(buf)
  16. }
  17. }
  18. BufferList.prototype._new = function _new (buf) {
  19. return new BufferList(buf)
  20. }
  21. BufferList.prototype._offset = function _offset (offset) {
  22. if (offset === 0) {
  23. return [0, 0]
  24. }
  25. let tot = 0
  26. for (let i = 0; i < this._bufs.length; i++) {
  27. const _t = tot + this._bufs[i].length
  28. if (offset < _t || i === this._bufs.length - 1) {
  29. return [i, offset - tot]
  30. }
  31. tot = _t
  32. }
  33. }
  34. BufferList.prototype._reverseOffset = function (blOffset) {
  35. const bufferId = blOffset[0]
  36. let offset = blOffset[1]
  37. for (let i = 0; i < bufferId; i++) {
  38. offset += this._bufs[i].length
  39. }
  40. return offset
  41. }
  42. BufferList.prototype.getBuffers = function getBuffers () {
  43. return this._bufs
  44. }
  45. BufferList.prototype.get = function get (index) {
  46. if (index > this.length || index < 0) {
  47. return undefined
  48. }
  49. const offset = this._offset(index)
  50. return this._bufs[offset[0]][offset[1]]
  51. }
  52. BufferList.prototype.slice = function slice (start, end) {
  53. if (typeof start === 'number' && start < 0) {
  54. start += this.length
  55. }
  56. if (typeof end === 'number' && end < 0) {
  57. end += this.length
  58. }
  59. return this.copy(null, 0, start, end)
  60. }
  61. BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
  62. if (typeof srcStart !== 'number' || srcStart < 0) {
  63. srcStart = 0
  64. }
  65. if (typeof srcEnd !== 'number' || srcEnd > this.length) {
  66. srcEnd = this.length
  67. }
  68. if (srcStart >= this.length) {
  69. return dst || Buffer.alloc(0)
  70. }
  71. if (srcEnd <= 0) {
  72. return dst || Buffer.alloc(0)
  73. }
  74. const copy = !!dst
  75. const off = this._offset(srcStart)
  76. const len = srcEnd - srcStart
  77. let bytes = len
  78. let bufoff = (copy && dstStart) || 0
  79. let start = off[1]
  80. // copy/slice everything
  81. if (srcStart === 0 && srcEnd === this.length) {
  82. if (!copy) {
  83. // slice, but full concat if multiple buffers
  84. return this._bufs.length === 1
  85. ? this._bufs[0]
  86. : Buffer.concat(this._bufs, this.length)
  87. }
  88. // copy, need to copy individual buffers
  89. for (let i = 0; i < this._bufs.length; i++) {
  90. this._bufs[i].copy(dst, bufoff)
  91. bufoff += this._bufs[i].length
  92. }
  93. return dst
  94. }
  95. // easy, cheap case where it's a subset of one of the buffers
  96. if (bytes <= this._bufs[off[0]].length - start) {
  97. return copy
  98. ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
  99. : this._bufs[off[0]].slice(start, start + bytes)
  100. }
  101. if (!copy) {
  102. // a slice, we need something to copy in to
  103. dst = Buffer.allocUnsafe(len)
  104. }
  105. for (let i = off[0]; i < this._bufs.length; i++) {
  106. const l = this._bufs[i].length - start
  107. if (bytes > l) {
  108. this._bufs[i].copy(dst, bufoff, start)
  109. bufoff += l
  110. } else {
  111. this._bufs[i].copy(dst, bufoff, start, start + bytes)
  112. bufoff += l
  113. break
  114. }
  115. bytes -= l
  116. if (start) {
  117. start = 0
  118. }
  119. }
  120. // safeguard so that we don't return uninitialized memory
  121. if (dst.length > bufoff) return dst.slice(0, bufoff)
  122. return dst
  123. }
  124. BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
  125. start = start || 0
  126. end = typeof end !== 'number' ? this.length : end
  127. if (start < 0) {
  128. start += this.length
  129. }
  130. if (end < 0) {
  131. end += this.length
  132. }
  133. if (start === end) {
  134. return this._new()
  135. }
  136. const startOffset = this._offset(start)
  137. const endOffset = this._offset(end)
  138. const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
  139. if (endOffset[1] === 0) {
  140. buffers.pop()
  141. } else {
  142. buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
  143. }
  144. if (startOffset[1] !== 0) {
  145. buffers[0] = buffers[0].slice(startOffset[1])
  146. }
  147. return this._new(buffers)
  148. }
  149. BufferList.prototype.toString = function toString (encoding, start, end) {
  150. return this.slice(start, end).toString(encoding)
  151. }
  152. BufferList.prototype.consume = function consume (bytes) {
  153. // first, normalize the argument, in accordance with how Buffer does it
  154. bytes = Math.trunc(bytes)
  155. // do nothing if not a positive number
  156. if (Number.isNaN(bytes) || bytes <= 0) return this
  157. while (this._bufs.length) {
  158. if (bytes >= this._bufs[0].length) {
  159. bytes -= this._bufs[0].length
  160. this.length -= this._bufs[0].length
  161. this._bufs.shift()
  162. } else {
  163. this._bufs[0] = this._bufs[0].slice(bytes)
  164. this.length -= bytes
  165. break
  166. }
  167. }
  168. return this
  169. }
  170. BufferList.prototype.duplicate = function duplicate () {
  171. const copy = this._new()
  172. for (let i = 0; i < this._bufs.length; i++) {
  173. copy.append(this._bufs[i])
  174. }
  175. return copy
  176. }
  177. BufferList.prototype.append = function append (buf) {
  178. return this._attach(buf, BufferList.prototype._appendBuffer)
  179. }
  180. BufferList.prototype.prepend = function prepend (buf) {
  181. return this._attach(buf, BufferList.prototype._prependBuffer, true)
  182. }
  183. BufferList.prototype._attach = function _attach (buf, attacher, prepend) {
  184. if (buf == null) {
  185. return this
  186. }
  187. if (buf.buffer) {
  188. // append/prepend a view of the underlying ArrayBuffer
  189. attacher.call(this, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength))
  190. } else if (Array.isArray(buf)) {
  191. const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1]
  192. for (let i = starting; i >= 0 && i < buf.length; i += modifier) {
  193. this._attach(buf[i], attacher, prepend)
  194. }
  195. } else if (this._isBufferList(buf)) {
  196. // unwrap argument into individual BufferLists
  197. const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1]
  198. for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) {
  199. this._attach(buf._bufs[i], attacher, prepend)
  200. }
  201. } else {
  202. // coerce number arguments to strings, since Buffer(number) does
  203. // uninitialized memory allocation
  204. if (typeof buf === 'number') {
  205. buf = buf.toString()
  206. }
  207. attacher.call(this, Buffer.from(buf))
  208. }
  209. return this
  210. }
  211. BufferList.prototype._appendBuffer = function appendBuffer (buf) {
  212. this._bufs.push(buf)
  213. this.length += buf.length
  214. }
  215. BufferList.prototype._prependBuffer = function prependBuffer (buf) {
  216. this._bufs.unshift(buf)
  217. this.length += buf.length
  218. }
  219. BufferList.prototype.indexOf = function (search, offset, encoding) {
  220. if (encoding === undefined && typeof offset === 'string') {
  221. encoding = offset
  222. offset = undefined
  223. }
  224. if (typeof search === 'function' || Array.isArray(search)) {
  225. throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
  226. } else if (typeof search === 'number') {
  227. search = Buffer.from([search])
  228. } else if (typeof search === 'string') {
  229. search = Buffer.from(search, encoding)
  230. } else if (this._isBufferList(search)) {
  231. search = search.slice()
  232. } else if (Array.isArray(search.buffer)) {
  233. search = Buffer.from(search.buffer, search.byteOffset, search.byteLength)
  234. } else if (!Buffer.isBuffer(search)) {
  235. search = Buffer.from(search)
  236. }
  237. offset = Number(offset || 0)
  238. if (isNaN(offset)) {
  239. offset = 0
  240. }
  241. if (offset < 0) {
  242. offset = this.length + offset
  243. }
  244. if (offset < 0) {
  245. offset = 0
  246. }
  247. if (search.length === 0) {
  248. return offset > this.length ? this.length : offset
  249. }
  250. const blOffset = this._offset(offset)
  251. let blIndex = blOffset[0] // index of which internal buffer we're working on
  252. let buffOffset = blOffset[1] // offset of the internal buffer we're working on
  253. // scan over each buffer
  254. for (; blIndex < this._bufs.length; blIndex++) {
  255. const buff = this._bufs[blIndex]
  256. while (buffOffset < buff.length) {
  257. const availableWindow = buff.length - buffOffset
  258. if (availableWindow >= search.length) {
  259. const nativeSearchResult = buff.indexOf(search, buffOffset)
  260. if (nativeSearchResult !== -1) {
  261. return this._reverseOffset([blIndex, nativeSearchResult])
  262. }
  263. buffOffset = buff.length - search.length + 1 // end of native search window
  264. } else {
  265. const revOffset = this._reverseOffset([blIndex, buffOffset])
  266. if (this._match(revOffset, search)) {
  267. return revOffset
  268. }
  269. buffOffset++
  270. }
  271. }
  272. buffOffset = 0
  273. }
  274. return -1
  275. }
  276. BufferList.prototype._match = function (offset, search) {
  277. if (this.length - offset < search.length) {
  278. return false
  279. }
  280. for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
  281. if (this.get(offset + searchOffset) !== search[searchOffset]) {
  282. return false
  283. }
  284. }
  285. return true
  286. }
  287. ;(function () {
  288. const methods = {
  289. readDoubleBE: 8,
  290. readDoubleLE: 8,
  291. readFloatBE: 4,
  292. readFloatLE: 4,
  293. readBigInt64BE: 8,
  294. readBigInt64LE: 8,
  295. readBigUInt64BE: 8,
  296. readBigUInt64LE: 8,
  297. readInt32BE: 4,
  298. readInt32LE: 4,
  299. readUInt32BE: 4,
  300. readUInt32LE: 4,
  301. readInt16BE: 2,
  302. readInt16LE: 2,
  303. readUInt16BE: 2,
  304. readUInt16LE: 2,
  305. readInt8: 1,
  306. readUInt8: 1,
  307. readIntBE: null,
  308. readIntLE: null,
  309. readUIntBE: null,
  310. readUIntLE: null
  311. }
  312. for (const m in methods) {
  313. (function (m) {
  314. if (methods[m] === null) {
  315. BufferList.prototype[m] = function (offset, byteLength) {
  316. return this.slice(offset, offset + byteLength)[m](0, byteLength)
  317. }
  318. } else {
  319. BufferList.prototype[m] = function (offset = 0) {
  320. return this.slice(offset, offset + methods[m])[m](0)
  321. }
  322. }
  323. }(m))
  324. }
  325. }())
  326. // Used internally by the class and also as an indicator of this object being
  327. // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
  328. // environment because there could be multiple different copies of the
  329. // BufferList class and some `BufferList`s might be `BufferList`s.
  330. BufferList.prototype._isBufferList = function _isBufferList (b) {
  331. return b instanceof BufferList || BufferList.isBufferList(b)
  332. }
  333. BufferList.isBufferList = function isBufferList (b) {
  334. return b != null && b[symbol]
  335. }
  336. module.exports = BufferList