bl.js 9.5 KB

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