You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # bl *(BufferList)*
  2. [![Build Status](https://travis-ci.org/rvagg/bl.svg?branch=master)](https://travis-ci.org/rvagg/bl)
  3. **A Node.js Buffer list collector, reader and streamer thingy.**
  4. [![NPM](https://nodei.co/npm/bl.png?downloads=true&downloadRank=true)](https://nodei.co/npm/bl/)
  5. [![NPM](https://nodei.co/npm-dl/bl.png?months=6&height=3)](https://nodei.co/npm/bl/)
  6. **bl** is a storage object for collections of Node Buffers, exposing them with the main Buffer readable API. Also works as a duplex stream so you can collect buffers from a stream that emits them and emit buffers to a stream that consumes them!
  7. The original buffers are kept intact and copies are only done as necessary. Any reads that require the use of a single original buffer will return a slice of that buffer only (which references the same memory as the original buffer). Reads that span buffers perform concatenation as required and return the results transparently.
  8. ```js
  9. const BufferList = require('bl')
  10. var bl = new BufferList()
  11. bl.append(new Buffer('abcd'))
  12. bl.append(new Buffer('efg'))
  13. bl.append('hi') // bl will also accept & convert Strings
  14. bl.append(new Buffer('j'))
  15. bl.append(new Buffer([ 0x3, 0x4 ]))
  16. console.log(bl.length) // 12
  17. console.log(bl.slice(0, 10).toString('ascii')) // 'abcdefghij'
  18. console.log(bl.slice(3, 10).toString('ascii')) // 'defghij'
  19. console.log(bl.slice(3, 6).toString('ascii')) // 'def'
  20. console.log(bl.slice(3, 8).toString('ascii')) // 'defgh'
  21. console.log(bl.slice(5, 10).toString('ascii')) // 'fghij'
  22. // or just use toString!
  23. console.log(bl.toString()) // 'abcdefghij\u0003\u0004'
  24. console.log(bl.toString('ascii', 3, 8)) // 'defgh'
  25. console.log(bl.toString('ascii', 5, 10)) // 'fghij'
  26. // other standard Buffer readables
  27. console.log(bl.readUInt16BE(10)) // 0x0304
  28. console.log(bl.readUInt16LE(10)) // 0x0403
  29. ```
  30. Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**:
  31. ```js
  32. const bl = require('bl')
  33. , fs = require('fs')
  34. fs.createReadStream('README.md')
  35. .pipe(bl(function (err, data) { // note 'new' isn't strictly required
  36. // `data` is a complete Buffer object containing the full data
  37. console.log(data.toString())
  38. }))
  39. ```
  40. Note that when you use the *callback* method like this, the resulting `data` parameter is a concatenation of all `Buffer` objects in the list. If you want to avoid the overhead of this concatenation (in cases of extreme performance consciousness), then avoid the *callback* method and just listen to `'end'` instead, like a standard Stream.
  41. Or to fetch a URL using [hyperquest](https://github.com/substack/hyperquest) (should work with [request](http://github.com/mikeal/request) and even plain Node http too!):
  42. ```js
  43. const hyperquest = require('hyperquest')
  44. , bl = require('bl')
  45. , url = 'https://raw.github.com/rvagg/bl/master/README.md'
  46. hyperquest(url).pipe(bl(function (err, data) {
  47. console.log(data.toString())
  48. }))
  49. ```
  50. Or, use it as a readable stream to recompose a list of Buffers to an output source:
  51. ```js
  52. const BufferList = require('bl')
  53. , fs = require('fs')
  54. var bl = new BufferList()
  55. bl.append(new Buffer('abcd'))
  56. bl.append(new Buffer('efg'))
  57. bl.append(new Buffer('hi'))
  58. bl.append(new Buffer('j'))
  59. bl.pipe(fs.createWriteStream('gibberish.txt'))
  60. ```
  61. ## API
  62. * <a href="#ctor"><code><b>new BufferList([ callback ])</b></code></a>
  63. * <a href="#length"><code>bl.<b>length</b></code></a>
  64. * <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
  65. * <a href="#get"><code>bl.<b>get(index)</b></code></a>
  66. * <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
  67. * <a href="#shallowSlice"><code>bl.<b>shallowSlice([ start[, end ] ])</b></code></a>
  68. * <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
  69. * <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a>
  70. * <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a>
  71. * <a href="#toString"><code>bl.<b>toString([encoding, [ start, [ end ]]])</b></code></a>
  72. * <a href="#readXX"><code>bl.<b>readDoubleBE()</b></code>, <code>bl.<b>readDoubleLE()</b></code>, <code>bl.<b>readFloatBE()</b></code>, <code>bl.<b>readFloatLE()</b></code>, <code>bl.<b>readInt32BE()</b></code>, <code>bl.<b>readInt32LE()</b></code>, <code>bl.<b>readUInt32BE()</b></code>, <code>bl.<b>readUInt32LE()</b></code>, <code>bl.<b>readInt16BE()</b></code>, <code>bl.<b>readInt16LE()</b></code>, <code>bl.<b>readUInt16BE()</b></code>, <code>bl.<b>readUInt16LE()</b></code>, <code>bl.<b>readInt8()</b></code>, <code>bl.<b>readUInt8()</b></code></a>
  73. * <a href="#streams">Streams</a>
  74. --------------------------------------------------------
  75. <a name="ctor"></a>
  76. ### new BufferList([ callback | Buffer | Buffer array | BufferList | BufferList array | String ])
  77. The constructor takes an optional callback, if supplied, the callback will be called with an error argument followed by a reference to the **bl** instance, when `bl.end()` is called (i.e. from a piped stream). This is a convenient method of collecting the entire contents of a stream, particularly when the stream is *chunky*, such as a network stream.
  78. Normally, no arguments are required for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` object.
  79. `new` is not strictly required, if you don't instantiate a new object, it will be done automatically for you so you can create a new instance simply with:
  80. ```js
  81. var bl = require('bl')
  82. var myinstance = bl()
  83. // equivalent to:
  84. var BufferList = require('bl')
  85. var myinstance = new BufferList()
  86. ```
  87. --------------------------------------------------------
  88. <a name="length"></a>
  89. ### bl.length
  90. Get the length of the list in bytes. This is the sum of the lengths of all of the buffers contained in the list, minus any initial offset for a semi-consumed buffer at the beginning. Should accurately represent the total number of bytes that can be read from the list.
  91. --------------------------------------------------------
  92. <a name="append"></a>
  93. ### bl.append(Buffer | Buffer array | BufferList | BufferList array | String)
  94. `append(buffer)` adds an additional buffer or BufferList to the internal list. `this` is returned so it can be chained.
  95. --------------------------------------------------------
  96. <a name="get"></a>
  97. ### bl.get(index)
  98. `get()` will return the byte at the specified index.
  99. --------------------------------------------------------
  100. <a name="slice"></a>
  101. ### bl.slice([ start, [ end ] ])
  102. `slice()` returns a new `Buffer` object containing the bytes within the range specified. Both `start` and `end` are optional and will default to the beginning and end of the list respectively.
  103. If the requested range spans a single internal buffer then a slice of that buffer will be returned which shares the original memory range of that Buffer. If the range spans multiple buffers then copy operations will likely occur to give you a uniform Buffer.
  104. --------------------------------------------------------
  105. <a name="shallowSlice"></a>
  106. ### bl.shallowSlice([ start, [ end ] ])
  107. `shallowSlice()` returns a new `BufferList` object containing the bytes within the range specified. Both `start` and `end` are optional and will default to the beginning and end of the list respectively.
  108. No copies will be performed. All buffers in the result share memory with the original list.
  109. --------------------------------------------------------
  110. <a name="copy"></a>
  111. ### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])
  112. `copy()` copies the content of the list in the `dest` buffer, starting from `destStart` and containing the bytes within the range specified with `srcStart` to `srcEnd`. `destStart`, `start` and `end` are optional and will default to the beginning of the `dest` buffer, and the beginning and end of the list respectively.
  113. --------------------------------------------------------
  114. <a name="duplicate"></a>
  115. ### bl.duplicate()
  116. `duplicate()` performs a **shallow-copy** of the list. The internal Buffers remains the same, so if you change the underlying Buffers, the change will be reflected in both the original and the duplicate. This method is needed if you want to call `consume()` or `pipe()` and still keep the original list.Example:
  117. ```js
  118. var bl = new BufferList()
  119. bl.append('hello')
  120. bl.append(' world')
  121. bl.append('\n')
  122. bl.duplicate().pipe(process.stdout, { end: false })
  123. console.log(bl.toString())
  124. ```
  125. --------------------------------------------------------
  126. <a name="consume"></a>
  127. ### bl.consume(bytes)
  128. `consume()` will shift bytes *off the start of the list*. The number of bytes consumed don't need to line up with the sizes of the internal Buffers&mdash;initial offsets will be calculated accordingly in order to give you a consistent view of the data.
  129. --------------------------------------------------------
  130. <a name="toString"></a>
  131. ### bl.toString([encoding, [ start, [ end ]]])
  132. `toString()` will return a string representation of the buffer. The optional `start` and `end` arguments are passed on to `slice()`, while the `encoding` is passed on to `toString()` of the resulting Buffer. See the [Buffer#toString()](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end) documentation for more information.
  133. --------------------------------------------------------
  134. <a name="readXX"></a>
  135. ### bl.readDoubleBE(), bl.readDoubleLE(), bl.readFloatBE(), bl.readFloatLE(), bl.readInt32BE(), bl.readInt32LE(), bl.readUInt32BE(), bl.readUInt32LE(), bl.readInt16BE(), bl.readInt16LE(), bl.readUInt16BE(), bl.readUInt16LE(), bl.readInt8(), bl.readUInt8()
  136. All of the standard byte-reading methods of the `Buffer` interface are implemented and will operate across internal Buffer boundaries transparently.
  137. See the <b><code>[Buffer](http://nodejs.org/docs/latest/api/buffer.html)</code></b> documentation for how these work.
  138. --------------------------------------------------------
  139. <a name="streams"></a>
  140. ### Streams
  141. **bl** is a Node **[Duplex Stream](http://nodejs.org/docs/latest/api/stream.html#stream_class_stream_duplex)**, so it can be read from and written to like a standard Node stream. You can also `pipe()` to and from a **bl** instance.
  142. --------------------------------------------------------
  143. ## Contributors
  144. **bl** is brought to you by the following hackers:
  145. * [Rod Vagg](https://github.com/rvagg)
  146. * [Matteo Collina](https://github.com/mcollina)
  147. * [Jarett Cruger](https://github.com/jcrugzz)
  148. =======
  149. <a name="license"></a>
  150. ## License &amp; copyright
  151. Copyright (c) 2013-2016 bl contributors (listed above).
  152. bl is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.