bl.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. var DuplexStream = require('readable-stream/duplex')
  2. , util = require('util')
  3. , Buffer = require('safe-buffer').Buffer
  4. function BufferList (callback) {
  5. if (!(this instanceof BufferList))
  6. return new BufferList(callback)
  7. this._bufs = []
  8. this.length = 0
  9. if (typeof callback == 'function') {
  10. this._callback = callback
  11. var piper = function piper (err) {
  12. if (this._callback) {
  13. this._callback(err)
  14. this._callback = null
  15. }
  16. }.bind(this)
  17. this.on('pipe', function onPipe (src) {
  18. src.on('error', piper)
  19. })
  20. this.on('unpipe', function onUnpipe (src) {
  21. src.removeListener('error', piper)
  22. })
  23. } else {
  24. this.append(callback)
  25. }
  26. DuplexStream.call(this)
  27. }
  28. util.inherits(BufferList, DuplexStream)
  29. BufferList.prototype._offset = function _offset (offset) {
  30. var tot = 0, i = 0, _t
  31. if (offset === 0) return [ 0, 0 ]
  32. for (; i < this._bufs.length; i++) {
  33. _t = tot + this._bufs[i].length
  34. if (offset < _t || i == this._bufs.length - 1)
  35. return [ i, offset - tot ]
  36. tot = _t
  37. }
  38. }
  39. BufferList.prototype.append = function append (buf) {
  40. var i = 0
  41. if (Buffer.isBuffer(buf)) {
  42. this._appendBuffer(buf);
  43. } else if (Array.isArray(buf)) {
  44. for (; i < buf.length; i++)
  45. this.append(buf[i])
  46. } else if (buf instanceof BufferList) {
  47. // unwrap argument into individual BufferLists
  48. for (; i < buf._bufs.length; i++)
  49. this.append(buf._bufs[i])
  50. } else if (buf != null) {
  51. // coerce number arguments to strings, since Buffer(number) does
  52. // uninitialized memory allocation
  53. if (typeof buf == 'number')
  54. buf = buf.toString()
  55. this._appendBuffer(Buffer.from(buf));
  56. }
  57. return this
  58. }
  59. BufferList.prototype._appendBuffer = function appendBuffer (buf) {
  60. this._bufs.push(buf)
  61. this.length += buf.length
  62. }
  63. BufferList.prototype._write = function _write (buf, encoding, callback) {
  64. this._appendBuffer(buf)
  65. if (typeof callback == 'function')
  66. callback()
  67. }
  68. BufferList.prototype._read = function _read (size) {
  69. if (!this.length)
  70. return this.push(null)
  71. size = Math.min(size, this.length)
  72. this.push(this.slice(0, size))
  73. this.consume(size)
  74. }
  75. BufferList.prototype.end = function end (chunk) {
  76. DuplexStream.prototype.end.call(this, chunk)
  77. if (this._callback) {
  78. this._callback(null, this.slice())
  79. this._callback = null
  80. }
  81. }
  82. BufferList.prototype.get = function get (index) {
  83. return this.slice(index, index + 1)[0]
  84. }
  85. BufferList.prototype.slice = function slice (start, end) {
  86. if (typeof start == 'number' && start < 0)
  87. start += this.length
  88. if (typeof end == 'number' && end < 0)
  89. end += this.length
  90. return this.copy(null, 0, start, end)
  91. }
  92. BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
  93. if (typeof srcStart != 'number' || srcStart < 0)
  94. srcStart = 0
  95. if (typeof srcEnd != 'number' || srcEnd > this.length)
  96. srcEnd = this.length
  97. if (srcStart >= this.length)
  98. return dst || Buffer.alloc(0)
  99. if (srcEnd <= 0)
  100. return dst || Buffer.alloc(0)
  101. var copy = !!dst
  102. , off = this._offset(srcStart)
  103. , len = srcEnd - srcStart
  104. , bytes = len
  105. , bufoff = (copy && dstStart) || 0
  106. , start = off[1]
  107. , l
  108. , i
  109. // copy/slice everything
  110. if (srcStart === 0 && srcEnd == this.length) {
  111. if (!copy) { // slice, but full concat if multiple buffers
  112. return this._bufs.length === 1
  113. ? this._bufs[0]
  114. : Buffer.concat(this._bufs, this.length)
  115. }
  116. // copy, need to copy individual buffers
  117. for (i = 0; i < this._bufs.length; i++) {
  118. this._bufs[i].copy(dst, bufoff)
  119. bufoff += this._bufs[i].length
  120. }
  121. return dst
  122. }
  123. // easy, cheap case where it's a subset of one of the buffers
  124. if (bytes <= this._bufs[off[0]].length - start) {
  125. return copy
  126. ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
  127. : this._bufs[off[0]].slice(start, start + bytes)
  128. }
  129. if (!copy) // a slice, we need something to copy in to
  130. dst = Buffer.allocUnsafe(len)
  131. for (i = off[0]; i < this._bufs.length; i++) {
  132. l = this._bufs[i].length - start
  133. if (bytes > l) {
  134. this._bufs[i].copy(dst, bufoff, start)
  135. bufoff += l
  136. } else {
  137. this._bufs[i].copy(dst, bufoff, start, start + bytes)
  138. bufoff += l
  139. break
  140. }
  141. bytes -= l
  142. if (start)
  143. start = 0
  144. }
  145. // safeguard so that we don't return uninitialized memory
  146. if (dst.length > bufoff) return dst.slice(0, bufoff)
  147. return dst
  148. }
  149. BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
  150. start = start || 0
  151. end = end || this.length
  152. if (start < 0)
  153. start += this.length
  154. if (end < 0)
  155. end += this.length
  156. var startOffset = this._offset(start)
  157. , endOffset = this._offset(end)
  158. , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
  159. if (endOffset[1] == 0)
  160. buffers.pop()
  161. else
  162. buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
  163. if (startOffset[1] != 0)
  164. buffers[0] = buffers[0].slice(startOffset[1])
  165. return new BufferList(buffers)
  166. }
  167. BufferList.prototype.toString = function toString (encoding, start, end) {
  168. return this.slice(start, end).toString(encoding)
  169. }
  170. BufferList.prototype.consume = function consume (bytes) {
  171. // first, normalize the argument, in accordance with how Buffer does it
  172. bytes = Math.trunc(bytes)
  173. // do nothing if not a positive number
  174. if (Number.isNaN(bytes) || bytes <= 0) return this
  175. while (this._bufs.length) {
  176. if (bytes >= this._bufs[0].length) {
  177. bytes -= this._bufs[0].length
  178. this.length -= this._bufs[0].length
  179. this._bufs.shift()
  180. } else {
  181. this._bufs[0] = this._bufs[0].slice(bytes)
  182. this.length -= bytes
  183. break
  184. }
  185. }
  186. return this
  187. }
  188. BufferList.prototype.duplicate = function duplicate () {
  189. var i = 0
  190. , copy = new BufferList()
  191. for (; i < this._bufs.length; i++)
  192. copy.append(this._bufs[i])
  193. return copy
  194. }
  195. BufferList.prototype.destroy = function destroy () {
  196. this._bufs.length = 0
  197. this.length = 0
  198. this.push(null)
  199. }
  200. ;(function () {
  201. var methods = {
  202. 'readDoubleBE' : 8
  203. , 'readDoubleLE' : 8
  204. , 'readFloatBE' : 4
  205. , 'readFloatLE' : 4
  206. , 'readInt32BE' : 4
  207. , 'readInt32LE' : 4
  208. , 'readUInt32BE' : 4
  209. , 'readUInt32LE' : 4
  210. , 'readInt16BE' : 2
  211. , 'readInt16LE' : 2
  212. , 'readUInt16BE' : 2
  213. , 'readUInt16LE' : 2
  214. , 'readInt8' : 1
  215. , 'readUInt8' : 1
  216. }
  217. for (var m in methods) {
  218. (function (m) {
  219. BufferList.prototype[m] = function (offset) {
  220. return this.slice(offset, offset + methods[m])[m](0)
  221. }
  222. }(m))
  223. }
  224. }())
  225. module.exports = BufferList