Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BufferList.js 9.3KB

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