Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # bl *(BufferList)*
  2. [![Build Status](https://api.travis-ci.com/rvagg/bl.svg?branch=master)](https://travis-ci.com/rvagg/bl/)
  3. **A Node.js Buffer list collector, reader and streamer thingy.**
  4. [![NPM](https://nodei.co/npm/bl.svg)](https://nodei.co/npm/bl/)
  5. **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!
  6. 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.
  7. ```js
  8. const { BufferList } = require('bl')
  9. const bl = new BufferList()
  10. bl.append(Buffer.from('abcd'))
  11. bl.append(Buffer.from('efg'))
  12. bl.append('hi') // bl will also accept & convert Strings
  13. bl.append(Buffer.from('j'))
  14. bl.append(Buffer.from([ 0x3, 0x4 ]))
  15. console.log(bl.length) // 12
  16. console.log(bl.slice(0, 10).toString('ascii')) // 'abcdefghij'
  17. console.log(bl.slice(3, 10).toString('ascii')) // 'defghij'
  18. console.log(bl.slice(3, 6).toString('ascii')) // 'def'
  19. console.log(bl.slice(3, 8).toString('ascii')) // 'defgh'
  20. console.log(bl.slice(5, 10).toString('ascii')) // 'fghij'
  21. console.log(bl.indexOf('def')) // 3
  22. console.log(bl.indexOf('asdf')) // -1
  23. // or just use toString!
  24. console.log(bl.toString()) // 'abcdefghij\u0003\u0004'
  25. console.log(bl.toString('ascii', 3, 8)) // 'defgh'
  26. console.log(bl.toString('ascii', 5, 10)) // 'fghij'
  27. // other standard Buffer readables
  28. console.log(bl.readUInt16BE(10)) // 0x0304
  29. console.log(bl.readUInt16LE(10)) // 0x0403
  30. ```
  31. Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**:
  32. ```js
  33. const { BufferListStream } = require('bl')
  34. const fs = require('fs')
  35. fs.createReadStream('README.md')
  36. .pipe(BufferListStream((err, data) => { // note 'new' isn't strictly required
  37. // `data` is a complete Buffer object containing the full data
  38. console.log(data.toString())
  39. }))
  40. ```
  41. 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.
  42. 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!):
  43. ```js
  44. const hyperquest = require('hyperquest')
  45. const { BufferListStream } = require('bl')
  46. const url = 'https://raw.github.com/rvagg/bl/master/README.md'
  47. hyperquest(url).pipe(BufferListStream((err, data) => {
  48. console.log(data.toString())
  49. }))
  50. ```
  51. Or, use it as a readable stream to recompose a list of Buffers to an output source:
  52. ```js
  53. const { BufferListStream } = require('bl')
  54. const fs = require('fs')
  55. var bl = new BufferListStream()
  56. bl.append(Buffer.from('abcd'))
  57. bl.append(Buffer.from('efg'))
  58. bl.append(Buffer.from('hi'))
  59. bl.append(Buffer.from('j'))
  60. bl.pipe(fs.createWriteStream('gibberish.txt'))
  61. ```
  62. ## API
  63. * <a href="#ctor"><code><b>new BufferList([ buf ])</b></code></a>
  64. * <a href="#isBufferList"><code><b>BufferList.isBufferList(obj)</b></code></a>
  65. * <a href="#length"><code>bl.<b>length</b></code></a>
  66. * <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
  67. * <a href="#get"><code>bl.<b>get(index)</b></code></a>
  68. * <a href="#indexOf"><code>bl.<b>indexOf(value[, byteOffset][, encoding])</b></code></a>
  69. * <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
  70. * <a href="#shallowSlice"><code>bl.<b>shallowSlice([ start[, end ] ])</b></code></a>
  71. * <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
  72. * <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a>
  73. * <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a>
  74. * <a href="#toString"><code>bl.<b>toString([encoding, [ start, [ end ]]])</b></code></a>
  75. * <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>
  76. * <a href="#ctorStream"><code><b>new BufferListStream([ callback ])</b></code></a>
  77. --------------------------------------------------------
  78. <a name="ctor"></a>
  79. ### new BufferList([ Buffer | Buffer array | BufferList | BufferList array | String ])
  80. 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` objects.
  81. `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:
  82. ```js
  83. const { BufferList } = require('bl')
  84. const bl = BufferList()
  85. // equivalent to:
  86. const { BufferList } = require('bl')
  87. const bl = new BufferList()
  88. ```
  89. --------------------------------------------------------
  90. <a name="isBufferList"></a>
  91. ### BufferList.isBufferList(obj)
  92. Determines if the passed object is a `BufferList`. It will return `true` if the passed object is an instance of `BufferList` **or** `BufferListStream` and `false` otherwise.
  93. N.B. this won't return `true` for `BufferList` or `BufferListStream` instances created by versions of this library before this static method was added.
  94. --------------------------------------------------------
  95. <a name="length"></a>
  96. ### bl.length
  97. 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.
  98. --------------------------------------------------------
  99. <a name="append"></a>
  100. ### bl.append(Buffer | Buffer array | BufferList | BufferList array | String)
  101. `append(buffer)` adds an additional buffer or BufferList to the internal list. `this` is returned so it can be chained.
  102. --------------------------------------------------------
  103. <a name="get"></a>
  104. ### bl.get(index)
  105. `get()` will return the byte at the specified index.
  106. --------------------------------------------------------
  107. <a name="indexOf"></a>
  108. ### bl.indexOf(value[, byteOffset][, encoding])
  109. `get()` will return the byte at the specified index.
  110. `indexOf()` method returns the first index at which a given element can be found in the BufferList, or -1 if it is not present.
  111. --------------------------------------------------------
  112. <a name="slice"></a>
  113. ### bl.slice([ start, [ end ] ])
  114. `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.
  115. 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.
  116. --------------------------------------------------------
  117. <a name="shallowSlice"></a>
  118. ### bl.shallowSlice([ start, [ end ] ])
  119. `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.
  120. No copies will be performed. All buffers in the result share memory with the original list.
  121. --------------------------------------------------------
  122. <a name="copy"></a>
  123. ### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])
  124. `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.
  125. --------------------------------------------------------
  126. <a name="duplicate"></a>
  127. ### bl.duplicate()
  128. `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:
  129. ```js
  130. var bl = new BufferListStream()
  131. bl.append('hello')
  132. bl.append(' world')
  133. bl.append('\n')
  134. bl.duplicate().pipe(process.stdout, { end: false })
  135. console.log(bl.toString())
  136. ```
  137. --------------------------------------------------------
  138. <a name="consume"></a>
  139. ### bl.consume(bytes)
  140. `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.
  141. --------------------------------------------------------
  142. <a name="toString"></a>
  143. ### bl.toString([encoding, [ start, [ end ]]])
  144. `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.
  145. --------------------------------------------------------
  146. <a name="readXX"></a>
  147. ### 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()
  148. All of the standard byte-reading methods of the `Buffer` interface are implemented and will operate across internal Buffer boundaries transparently.
  149. See the <b><code>[Buffer](http://nodejs.org/docs/latest/api/buffer.html)</code></b> documentation for how these work.
  150. --------------------------------------------------------
  151. <a name="ctorStream"></a>
  152. ### new BufferListStream([ callback | Buffer | Buffer array | BufferList | BufferList array | String ])
  153. **BufferListStream** 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 **BufferListStream** instance.
  154. 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.
  155. 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.
  156. `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:
  157. ```js
  158. const { BufferListStream } = require('bl')
  159. const bl = BufferListStream()
  160. // equivalent to:
  161. const { BufferListStream } = require('bl')
  162. const bl = new BufferListStream()
  163. ```
  164. N.B. For backwards compatibility reasons, `BufferListStream` is the **default** export when you `require('bl')`:
  165. ```js
  166. const { BufferListStream } = require('bl')
  167. // equivalent to:
  168. const BufferListStream = require('bl')
  169. ```
  170. --------------------------------------------------------
  171. ## Contributors
  172. **bl** is brought to you by the following hackers:
  173. * [Rod Vagg](https://github.com/rvagg)
  174. * [Matteo Collina](https://github.com/mcollina)
  175. * [Jarett Cruger](https://github.com/jcrugzz)
  176. <a name="license"></a>
  177. ## License &amp; copyright
  178. Copyright (c) 2013-2019 bl contributors (listed above).
  179. 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.