├── .github ├── dependabot.yml └── workflows │ └── test-and-release.yml ├── .gitignore ├── BufferList.d.ts ├── BufferList.js ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── bl.js ├── index.d.ts ├── package.json └── test ├── convert.js ├── indexOf.js ├── isBufferList.js └── test.js /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: 'github-actions' 4 | directory: '/' 5 | schedule: 6 | interval: 'daily' 7 | commit-message: 8 | prefix: 'chore' 9 | include: 'scope' 10 | - package-ecosystem: 'npm' 11 | directory: '/' 12 | schedule: 13 | interval: 'daily' 14 | commit-message: 15 | prefix: 'chore' 16 | include: 'scope' 17 | -------------------------------------------------------------------------------- /.github/workflows/test-and-release.yml: -------------------------------------------------------------------------------- 1 | name: Test & Maybe Release 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | strategy: 6 | fail-fast: false 7 | matrix: 8 | node: [18.x, 20.x, lts/*, current] 9 | os: [macos-latest, ubuntu-latest, windows-latest] 10 | runs-on: ${{ matrix.os }} 11 | steps: 12 | - name: Checkout Repository 13 | uses: actions/checkout@v4 14 | - name: Use Node.js ${{ matrix.node }} 15 | uses: actions/setup-node@v4.2.0 16 | with: 17 | node-version: ${{ matrix.node }} 18 | - name: Install Dependencies 19 | run: | 20 | npm install --no-progress 21 | - name: Run tests 22 | run: | 23 | npm config set script-shell bash 24 | npm run test:ci 25 | release: 26 | name: Release 27 | needs: test 28 | runs-on: ubuntu-latest 29 | if: github.event_name == 'push' && github.ref == 'refs/heads/master' 30 | steps: 31 | - name: Checkout 32 | uses: actions/checkout@v4 33 | with: 34 | fetch-depth: 0 35 | - name: Setup Node.js 36 | uses: actions/setup-node@v4.2.0 37 | with: 38 | node-version: lts/* 39 | - name: Install dependencies 40 | run: | 41 | npm install --no-progress --no-package-lock --no-save 42 | - name: Build 43 | run: | 44 | npm run build 45 | - name: Install plugins 46 | run: | 47 | npm install \ 48 | @semantic-release/commit-analyzer \ 49 | conventional-changelog-conventionalcommits \ 50 | @semantic-release/release-notes-generator \ 51 | @semantic-release/npm \ 52 | @semantic-release/github \ 53 | @semantic-release/git \ 54 | @semantic-release/changelog \ 55 | --no-progress --no-package-lock --no-save 56 | - name: Release 57 | env: 58 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 59 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 60 | run: npx semantic-release 61 | 62 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /BufferList.d.ts: -------------------------------------------------------------------------------- 1 | export type BufferListAcceptedTypes = 2 | | Buffer 3 | | BufferList 4 | | Uint8Array 5 | | BufferListAcceptedTypes[] 6 | | string 7 | | number; 8 | 9 | export interface BufferListConstructor { 10 | new (initData?: BufferListAcceptedTypes): BufferList; 11 | (initData?: BufferListAcceptedTypes): BufferList; 12 | 13 | /** 14 | * Determines if the passed object is a BufferList. It will return true 15 | * if the passed object is an instance of BufferList or BufferListStream 16 | * and false otherwise. 17 | * 18 | * N.B. this won't return true for BufferList or BufferListStream instances 19 | * created by versions of this library before this static method was added. 20 | * 21 | * @param other 22 | */ 23 | 24 | isBufferList(other: unknown): boolean; 25 | } 26 | 27 | interface BufferList { 28 | prototype: Object 29 | 30 | /** 31 | * Get the length of the list in bytes. This is the sum of the lengths 32 | * of all of the buffers contained in the list, minus any initial offset 33 | * for a semi-consumed buffer at the beginning. Should accurately 34 | * represent the total number of bytes that can be read from the list. 35 | */ 36 | 37 | length: number; 38 | 39 | /** 40 | * Adds an additional buffer or BufferList to the internal list. 41 | * this is returned so it can be chained. 42 | * 43 | * @param buffer 44 | */ 45 | 46 | append(buffer: BufferListAcceptedTypes): this; 47 | 48 | /** 49 | * Adds an additional buffer or BufferList at the beginning of the internal list. 50 | * this is returned so it can be chained. 51 | * 52 | * @param buffer 53 | */ 54 | prepend(buffer: BufferListAcceptedTypes): this; 55 | 56 | /** 57 | * Will return the byte at the specified index. 58 | * @param index 59 | */ 60 | 61 | get(index: number): number; 62 | 63 | /** 64 | * Returns a new Buffer object containing the bytes within the 65 | * range specified. Both start and end are optional and will 66 | * default to the beginning and end of the list respectively. 67 | * 68 | * If the requested range spans a single internal buffer then a 69 | * slice of that buffer will be returned which shares the original 70 | * memory range of that Buffer. If the range spans multiple buffers 71 | * then copy operations will likely occur to give you a uniform Buffer. 72 | * 73 | * @param start 74 | * @param end 75 | */ 76 | 77 | slice(start?: number, end?: number): Buffer; 78 | 79 | /** 80 | * Returns a new BufferList object containing the bytes within the 81 | * range specified. Both start and end are optional and will default 82 | * to the beginning and end of the list respectively. 83 | * 84 | * No copies will be performed. All buffers in the result share 85 | * memory with the original list. 86 | * 87 | * @param start 88 | * @param end 89 | */ 90 | 91 | shallowSlice(start?: number, end?: number): this; 92 | 93 | /** 94 | * Copies the content of the list in the `dest` buffer, starting from 95 | * `destStart` and containing the bytes within the range specified 96 | * with `srcStart` to `srcEnd`. 97 | * 98 | * `destStart`, `start` and `end` are optional and will default to the 99 | * beginning of the dest buffer, and the beginning and end of the 100 | * list respectively. 101 | * 102 | * @param dest 103 | * @param destStart 104 | * @param srcStart 105 | * @param srcEnd 106 | */ 107 | 108 | copy( 109 | dest: Buffer, 110 | destStart?: number, 111 | srcStart?: number, 112 | srcEnd?: number 113 | ): Buffer; 114 | 115 | /** 116 | * Performs a shallow-copy of the list. The internal Buffers remains the 117 | * same, so if you change the underlying Buffers, the change will be 118 | * reflected in both the original and the duplicate. 119 | * 120 | * This method is needed if you want to call consume() or pipe() and 121 | * still keep the original list. 122 | * 123 | * @example 124 | * 125 | * ```js 126 | * var bl = new BufferListStream(); 127 | * bl.append('hello'); 128 | * bl.append(' world'); 129 | * bl.append('\n'); 130 | * bl.duplicate().pipe(process.stdout, { end: false }); 131 | * 132 | * console.log(bl.toString()) 133 | * ``` 134 | */ 135 | 136 | duplicate(): this; 137 | 138 | /** 139 | * Will shift bytes off the start of the list. The number of bytes 140 | * consumed don't need to line up with the sizes of the internal 141 | * Buffers—initial offsets will be calculated accordingly in order 142 | * to give you a consistent view of the data. 143 | * 144 | * @param bytes 145 | */ 146 | 147 | consume(bytes?: number): void; 148 | 149 | /** 150 | * Will return a string representation of the buffer. The optional 151 | * `start` and `end` arguments are passed on to `slice()`, while 152 | * the encoding is passed on to `toString()` of the resulting Buffer. 153 | * 154 | * See the [`Buffer#toString()`](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end) 155 | * documentation for more information. 156 | * 157 | * @param encoding 158 | * @param start 159 | * @param end 160 | */ 161 | 162 | toString(encoding?: string, start?: number, end?: number): string; 163 | 164 | /** 165 | * Will return the byte at the specified index. indexOf() method 166 | * returns the first index at which a given element can be found 167 | * in the BufferList, or -1 if it is not present. 168 | * 169 | * @param value 170 | * @param byteOffset 171 | * @param encoding 172 | */ 173 | 174 | indexOf( 175 | value: string | number | Uint8Array | BufferList | Buffer, 176 | byteOffset?: number, 177 | encoding?: string 178 | ): number; 179 | 180 | /** 181 | * Will return the internal list of buffers. 182 | */ 183 | getBuffers(): Buffer[]; 184 | 185 | /** 186 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 187 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 188 | * 189 | * @param offset 190 | */ 191 | 192 | readDoubleBE: Buffer['readDoubleBE']; 193 | 194 | /** 195 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 196 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 197 | * 198 | * @param offset 199 | */ 200 | 201 | readDoubleLE: Buffer['readDoubleLE']; 202 | 203 | /** 204 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 205 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 206 | * 207 | * @param offset 208 | */ 209 | 210 | readFloatBE: Buffer['readFloatBE']; 211 | 212 | /** 213 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 214 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 215 | * 216 | * @param offset 217 | */ 218 | 219 | readFloatLE: Buffer['readFloatLE']; 220 | 221 | /** 222 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 223 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 224 | * 225 | * @param offset 226 | */ 227 | 228 | readBigInt64BE: Buffer['readBigInt64BE']; 229 | 230 | /** 231 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 232 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 233 | * 234 | * @param offset 235 | */ 236 | 237 | readBigInt64LE: Buffer['readBigInt64LE']; 238 | 239 | /** 240 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 241 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 242 | * 243 | * @param offset 244 | */ 245 | 246 | readBigUInt64BE: Buffer['readBigUInt64BE']; 247 | 248 | /** 249 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 250 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 251 | * 252 | * @param offset 253 | */ 254 | 255 | readBigUInt64LE: Buffer['readBigUInt64LE']; 256 | 257 | /** 258 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 259 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 260 | * 261 | * @param offset 262 | */ 263 | 264 | readInt32BE: Buffer['readInt32BE']; 265 | 266 | /** 267 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 268 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 269 | * 270 | * @param offset 271 | */ 272 | 273 | readInt32LE: Buffer['readInt32LE']; 274 | 275 | /** 276 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 277 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 278 | * 279 | * @param offset 280 | */ 281 | 282 | readUInt32BE: Buffer['readUInt32BE']; 283 | 284 | /** 285 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 286 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 287 | * 288 | * @param offset 289 | */ 290 | 291 | readUInt32LE: Buffer['readUInt32LE']; 292 | 293 | /** 294 | * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. 295 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 296 | * 297 | * @param offset 298 | */ 299 | 300 | readInt16BE: Buffer['readInt16BE']; 301 | 302 | /** 303 | * All of the standard byte-reading methods of the Buffer interface are 304 | * implemented and will operate across internal Buffer boundaries transparently. 305 | * 306 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 307 | * documentation for how these work. 308 | * 309 | * @param offset 310 | */ 311 | 312 | readInt16LE: Buffer['readInt16LE']; 313 | 314 | /** 315 | * All of the standard byte-reading methods of the Buffer interface are 316 | * implemented and will operate across internal Buffer boundaries transparently. 317 | * 318 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 319 | * documentation for how these work. 320 | * 321 | * @param offset 322 | */ 323 | 324 | readUInt16BE: Buffer['readUInt16BE']; 325 | 326 | /** 327 | * All of the standard byte-reading methods of the Buffer interface are 328 | * implemented and will operate across internal Buffer boundaries transparently. 329 | * 330 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 331 | * documentation for how these work. 332 | * 333 | * @param offset 334 | */ 335 | 336 | readUInt16LE: Buffer['readUInt16LE']; 337 | 338 | /** 339 | * All of the standard byte-reading methods of the Buffer interface are 340 | * implemented and will operate across internal Buffer boundaries transparently. 341 | * 342 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 343 | * documentation for how these work. 344 | * 345 | * @param offset 346 | */ 347 | 348 | readInt8: Buffer['readInt8']; 349 | 350 | /** 351 | * All of the standard byte-reading methods of the Buffer interface are 352 | * implemented and will operate across internal Buffer boundaries transparently. 353 | * 354 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 355 | * documentation for how these work. 356 | * 357 | * @param offset 358 | */ 359 | 360 | readUInt8: Buffer['readUInt8']; 361 | 362 | /** 363 | * All of the standard byte-reading methods of the Buffer interface are 364 | * implemented and will operate across internal Buffer boundaries transparently. 365 | * 366 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 367 | * documentation for how these work. 368 | * 369 | * @param offset 370 | */ 371 | 372 | readIntBE: Buffer['readIntBE']; 373 | 374 | /** 375 | * All of the standard byte-reading methods of the Buffer interface are 376 | * implemented and will operate across internal Buffer boundaries transparently. 377 | * 378 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 379 | * documentation for how these work. 380 | * 381 | * @param offset 382 | */ 383 | 384 | readIntLE: Buffer['readIntLE']; 385 | 386 | /** 387 | * All of the standard byte-reading methods of the Buffer interface are 388 | * implemented and will operate across internal Buffer boundaries transparently. 389 | * 390 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 391 | * documentation for how these work. 392 | * 393 | * @param offset 394 | */ 395 | 396 | readUIntBE: Buffer['readUIntBE']; 397 | 398 | /** 399 | * All of the standard byte-reading methods of the Buffer interface are 400 | * implemented and will operate across internal Buffer boundaries transparently. 401 | * 402 | * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) 403 | * documentation for how these work. 404 | * 405 | * @param offset 406 | */ 407 | 408 | readUIntLE: Buffer['readUIntLE']; 409 | } 410 | 411 | /** 412 | * No arguments are required for the constructor, but you can initialise 413 | * the list by passing in a single Buffer object or an array of Buffer 414 | * objects. 415 | * 416 | * `new` is not strictly required, if you don't instantiate a new object, 417 | * it will be done automatically for you so you can create a new instance 418 | * simply with: 419 | * 420 | * ```js 421 | * const { BufferList } = require('bl') 422 | * const bl = BufferList() 423 | * 424 | * // equivalent to: 425 | * 426 | * const { BufferList } = require('bl') 427 | * const bl = new BufferList() 428 | * ``` 429 | */ 430 | 431 | declare const BufferList: BufferListConstructor; 432 | -------------------------------------------------------------------------------- /BufferList.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { Buffer } = require('buffer') 4 | const symbol = Symbol.for('BufferList') 5 | 6 | function BufferList (buf) { 7 | if (!(this instanceof BufferList)) { 8 | return new BufferList(buf) 9 | } 10 | 11 | BufferList._init.call(this, buf) 12 | } 13 | 14 | BufferList._init = function _init (buf) { 15 | Object.defineProperty(this, symbol, { value: true }) 16 | 17 | this._bufs = [] 18 | this.length = 0 19 | 20 | if (buf) { 21 | this.append(buf) 22 | } 23 | } 24 | 25 | BufferList.prototype._new = function _new (buf) { 26 | return new BufferList(buf) 27 | } 28 | 29 | BufferList.prototype._offset = function _offset (offset) { 30 | if (offset === 0) { 31 | return [0, 0] 32 | } 33 | 34 | let tot = 0 35 | 36 | for (let i = 0; i < this._bufs.length; i++) { 37 | const _t = tot + this._bufs[i].length 38 | if (offset < _t || i === this._bufs.length - 1) { 39 | return [i, offset - tot] 40 | } 41 | tot = _t 42 | } 43 | } 44 | 45 | BufferList.prototype._reverseOffset = function (blOffset) { 46 | const bufferId = blOffset[0] 47 | let offset = blOffset[1] 48 | 49 | for (let i = 0; i < bufferId; i++) { 50 | offset += this._bufs[i].length 51 | } 52 | 53 | return offset 54 | } 55 | 56 | BufferList.prototype.getBuffers = function getBuffers () { 57 | return this._bufs 58 | } 59 | 60 | BufferList.prototype.get = function get (index) { 61 | if (index > this.length || index < 0) { 62 | return undefined 63 | } 64 | 65 | const offset = this._offset(index) 66 | 67 | return this._bufs[offset[0]][offset[1]] 68 | } 69 | 70 | BufferList.prototype.slice = function slice (start, end) { 71 | if (typeof start === 'number' && start < 0) { 72 | start += this.length 73 | } 74 | 75 | if (typeof end === 'number' && end < 0) { 76 | end += this.length 77 | } 78 | 79 | return this.copy(null, 0, start, end) 80 | } 81 | 82 | BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) { 83 | if (typeof srcStart !== 'number' || srcStart < 0) { 84 | srcStart = 0 85 | } 86 | 87 | if (typeof srcEnd !== 'number' || srcEnd > this.length) { 88 | srcEnd = this.length 89 | } 90 | 91 | if (srcStart >= this.length) { 92 | return dst || Buffer.alloc(0) 93 | } 94 | 95 | if (srcEnd <= 0) { 96 | return dst || Buffer.alloc(0) 97 | } 98 | 99 | const copy = !!dst 100 | const off = this._offset(srcStart) 101 | const len = srcEnd - srcStart 102 | let bytes = len 103 | let bufoff = (copy && dstStart) || 0 104 | let start = off[1] 105 | 106 | // copy/slice everything 107 | if (srcStart === 0 && srcEnd === this.length) { 108 | if (!copy) { 109 | // slice, but full concat if multiple buffers 110 | return this._bufs.length === 1 111 | ? this._bufs[0] 112 | : Buffer.concat(this._bufs, this.length) 113 | } 114 | 115 | // copy, need to copy individual buffers 116 | for (let i = 0; i < this._bufs.length; i++) { 117 | this._bufs[i].copy(dst, bufoff) 118 | bufoff += this._bufs[i].length 119 | } 120 | 121 | return dst 122 | } 123 | 124 | // easy, cheap case where it's a subset of one of the buffers 125 | if (bytes <= this._bufs[off[0]].length - start) { 126 | return copy 127 | ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes) 128 | : this._bufs[off[0]].slice(start, start + bytes) 129 | } 130 | 131 | if (!copy) { 132 | // a slice, we need something to copy in to 133 | dst = Buffer.allocUnsafe(len) 134 | } 135 | 136 | for (let i = off[0]; i < this._bufs.length; i++) { 137 | const l = this._bufs[i].length - start 138 | 139 | if (bytes > l) { 140 | this._bufs[i].copy(dst, bufoff, start) 141 | bufoff += l 142 | } else { 143 | this._bufs[i].copy(dst, bufoff, start, start + bytes) 144 | bufoff += l 145 | break 146 | } 147 | 148 | bytes -= l 149 | 150 | if (start) { 151 | start = 0 152 | } 153 | } 154 | 155 | // safeguard so that we don't return uninitialized memory 156 | if (dst.length > bufoff) return dst.slice(0, bufoff) 157 | 158 | return dst 159 | } 160 | 161 | BufferList.prototype.shallowSlice = function shallowSlice (start, end) { 162 | start = start || 0 163 | end = typeof end !== 'number' ? this.length : end 164 | 165 | if (start < 0) { 166 | start += this.length 167 | } 168 | 169 | if (end < 0) { 170 | end += this.length 171 | } 172 | 173 | if (start === end) { 174 | return this._new() 175 | } 176 | 177 | const startOffset = this._offset(start) 178 | const endOffset = this._offset(end) 179 | const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1) 180 | 181 | if (endOffset[1] === 0) { 182 | buffers.pop() 183 | } else { 184 | buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1]) 185 | } 186 | 187 | if (startOffset[1] !== 0) { 188 | buffers[0] = buffers[0].slice(startOffset[1]) 189 | } 190 | 191 | return this._new(buffers) 192 | } 193 | 194 | BufferList.prototype.toString = function toString (encoding, start, end) { 195 | return this.slice(start, end).toString(encoding) 196 | } 197 | 198 | BufferList.prototype.consume = function consume (bytes) { 199 | // first, normalize the argument, in accordance with how Buffer does it 200 | bytes = Math.trunc(bytes) 201 | // do nothing if not a positive number 202 | if (Number.isNaN(bytes) || bytes <= 0) return this 203 | 204 | while (this._bufs.length) { 205 | if (bytes >= this._bufs[0].length) { 206 | bytes -= this._bufs[0].length 207 | this.length -= this._bufs[0].length 208 | this._bufs.shift() 209 | } else { 210 | this._bufs[0] = this._bufs[0].slice(bytes) 211 | this.length -= bytes 212 | break 213 | } 214 | } 215 | 216 | return this 217 | } 218 | 219 | BufferList.prototype.duplicate = function duplicate () { 220 | const copy = this._new() 221 | 222 | for (let i = 0; i < this._bufs.length; i++) { 223 | copy.append(this._bufs[i]) 224 | } 225 | 226 | return copy 227 | } 228 | 229 | BufferList.prototype.append = function append (buf) { 230 | return this._attach(buf, BufferList.prototype._appendBuffer) 231 | } 232 | 233 | BufferList.prototype.prepend = function prepend (buf) { 234 | return this._attach(buf, BufferList.prototype._prependBuffer, true) 235 | } 236 | 237 | BufferList.prototype._attach = function _attach (buf, attacher, prepend) { 238 | if (buf == null) { 239 | return this 240 | } 241 | 242 | if (buf.buffer) { 243 | // append/prepend a view of the underlying ArrayBuffer 244 | attacher.call(this, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)) 245 | } else if (Array.isArray(buf)) { 246 | const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1] 247 | 248 | for (let i = starting; i >= 0 && i < buf.length; i += modifier) { 249 | this._attach(buf[i], attacher, prepend) 250 | } 251 | } else if (this._isBufferList(buf)) { 252 | // unwrap argument into individual BufferLists 253 | const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1] 254 | 255 | for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) { 256 | this._attach(buf._bufs[i], attacher, prepend) 257 | } 258 | } else { 259 | // coerce number arguments to strings, since Buffer(number) does 260 | // uninitialized memory allocation 261 | if (typeof buf === 'number') { 262 | buf = buf.toString() 263 | } 264 | 265 | attacher.call(this, Buffer.from(buf)) 266 | } 267 | 268 | return this 269 | } 270 | 271 | BufferList.prototype._appendBuffer = function appendBuffer (buf) { 272 | this._bufs.push(buf) 273 | this.length += buf.length 274 | } 275 | 276 | BufferList.prototype._prependBuffer = function prependBuffer (buf) { 277 | this._bufs.unshift(buf) 278 | this.length += buf.length 279 | } 280 | 281 | BufferList.prototype.indexOf = function (search, offset, encoding) { 282 | if (encoding === undefined && typeof offset === 'string') { 283 | encoding = offset 284 | offset = undefined 285 | } 286 | 287 | if (typeof search === 'function' || Array.isArray(search)) { 288 | throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.') 289 | } else if (typeof search === 'number') { 290 | search = Buffer.from([search]) 291 | } else if (typeof search === 'string') { 292 | search = Buffer.from(search, encoding) 293 | } else if (this._isBufferList(search)) { 294 | search = search.slice() 295 | } else if (Array.isArray(search.buffer)) { 296 | search = Buffer.from(search.buffer, search.byteOffset, search.byteLength) 297 | } else if (!Buffer.isBuffer(search)) { 298 | search = Buffer.from(search) 299 | } 300 | 301 | offset = Number(offset || 0) 302 | 303 | if (isNaN(offset)) { 304 | offset = 0 305 | } 306 | 307 | if (offset < 0) { 308 | offset = this.length + offset 309 | } 310 | 311 | if (offset < 0) { 312 | offset = 0 313 | } 314 | 315 | if (search.length === 0) { 316 | return offset > this.length ? this.length : offset 317 | } 318 | 319 | const blOffset = this._offset(offset) 320 | let blIndex = blOffset[0] // index of which internal buffer we're working on 321 | let buffOffset = blOffset[1] // offset of the internal buffer we're working on 322 | 323 | // scan over each buffer 324 | for (; blIndex < this._bufs.length; blIndex++) { 325 | const buff = this._bufs[blIndex] 326 | 327 | while (buffOffset < buff.length) { 328 | const availableWindow = buff.length - buffOffset 329 | 330 | if (availableWindow >= search.length) { 331 | const nativeSearchResult = buff.indexOf(search, buffOffset) 332 | 333 | if (nativeSearchResult !== -1) { 334 | return this._reverseOffset([blIndex, nativeSearchResult]) 335 | } 336 | 337 | buffOffset = buff.length - search.length + 1 // end of native search window 338 | } else { 339 | const revOffset = this._reverseOffset([blIndex, buffOffset]) 340 | 341 | if (this._match(revOffset, search)) { 342 | return revOffset 343 | } 344 | 345 | buffOffset++ 346 | } 347 | } 348 | 349 | buffOffset = 0 350 | } 351 | 352 | return -1 353 | } 354 | 355 | BufferList.prototype._match = function (offset, search) { 356 | if (this.length - offset < search.length) { 357 | return false 358 | } 359 | 360 | for (let searchOffset = 0; searchOffset < search.length; searchOffset++) { 361 | if (this.get(offset + searchOffset) !== search[searchOffset]) { 362 | return false 363 | } 364 | } 365 | return true 366 | } 367 | 368 | ;(function () { 369 | const methods = { 370 | readDoubleBE: 8, 371 | readDoubleLE: 8, 372 | readFloatBE: 4, 373 | readFloatLE: 4, 374 | readBigInt64BE: 8, 375 | readBigInt64LE: 8, 376 | readBigUInt64BE: 8, 377 | readBigUInt64LE: 8, 378 | readInt32BE: 4, 379 | readInt32LE: 4, 380 | readUInt32BE: 4, 381 | readUInt32LE: 4, 382 | readInt16BE: 2, 383 | readInt16LE: 2, 384 | readUInt16BE: 2, 385 | readUInt16LE: 2, 386 | readInt8: 1, 387 | readUInt8: 1, 388 | readIntBE: null, 389 | readIntLE: null, 390 | readUIntBE: null, 391 | readUIntLE: null 392 | } 393 | 394 | for (const m in methods) { 395 | (function (m) { 396 | if (methods[m] === null) { 397 | BufferList.prototype[m] = function (offset, byteLength) { 398 | return this.slice(offset, offset + byteLength)[m](0, byteLength) 399 | } 400 | } else { 401 | BufferList.prototype[m] = function (offset = 0) { 402 | return this.slice(offset, offset + methods[m])[m](0) 403 | } 404 | } 405 | }(m)) 406 | } 407 | }()) 408 | 409 | // Used internally by the class and also as an indicator of this object being 410 | // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser 411 | // environment because there could be multiple different copies of the 412 | // BufferList class and some `BufferList`s might be `BufferList`s. 413 | BufferList.prototype._isBufferList = function _isBufferList (b) { 414 | return b instanceof BufferList || BufferList.isBufferList(b) 415 | } 416 | 417 | BufferList.isBufferList = function isBufferList (b) { 418 | return b != null && b[symbol] 419 | } 420 | 421 | module.exports = BufferList 422 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [6.1.0](https://github.com/rvagg/bl/compare/v6.0.20...v6.1.0) (2025-03-11) 2 | 3 | ### Features 4 | 5 | * Added prepend and getBuffers methods. ([#154](https://github.com/rvagg/bl/issues/154)) ([e9eda95](https://github.com/rvagg/bl/commit/e9eda9549b1235af16afbe0c721f92e705109663)) 6 | 7 | ## [6.0.20](https://github.com/rvagg/bl/compare/v6.0.19...v6.0.20) (2025-03-03) 8 | 9 | ### Trivial Changes 10 | 11 | * **deps-dev:** bump typescript from 5.7.3 to 5.8.2 ([#153](https://github.com/rvagg/bl/issues/153)) ([9291cf9](https://github.com/rvagg/bl/commit/9291cf9ec4b3cdef8c5779c73247844f48943c02)) 12 | 13 | ## [6.0.19](https://github.com/rvagg/bl/compare/v6.0.18...v6.0.19) (2025-01-28) 14 | 15 | ### Trivial Changes 16 | 17 | * **deps:** bump actions/setup-node from 4.1.0 to 4.2.0 ([#151](https://github.com/rvagg/bl/issues/151)) ([2e72553](https://github.com/rvagg/bl/commit/2e7255395260941d199ffe644feecbe6ed2647e4)) 18 | 19 | ## [6.0.18](https://github.com/rvagg/bl/compare/v6.0.17...v6.0.18) (2024-12-30) 20 | 21 | ### Trivial Changes 22 | 23 | * **deps-dev:** bump typescript from 5.6.3 to 5.7.2 ([d28178a](https://github.com/rvagg/bl/commit/d28178ae2e5c740de0e3d891beae77b26f801b0f)) 24 | * **deps:** bump actions/setup-node from 4.0.4 to 4.1.0 ([#146](https://github.com/rvagg/bl/issues/146)) ([45c312b](https://github.com/rvagg/bl/commit/45c312b48b150d53336fecce69cb6895c8daaaeb)) 25 | 26 | ## [6.0.17](https://github.com/rvagg/bl/compare/v6.0.16...v6.0.17) (2024-12-30) 27 | 28 | ### Tests 29 | 30 | * ignore TS errors from dependencies ([17e7a10](https://github.com/rvagg/bl/commit/17e7a10c82b07f3ef63a4235a842159d6c08a7d0)) 31 | 32 | ## [6.0.16](https://github.com/rvagg/bl/compare/v6.0.15...v6.0.16) (2024-09-25) 33 | 34 | ### Trivial Changes 35 | 36 | * **deps:** bump actions/setup-node from 4.0.3 to 4.0.4 ([19d67ab](https://github.com/rvagg/bl/commit/19d67ab90e1e49a9b5ebc250969c2c5bde508db9)) 37 | 38 | ## [6.0.15](https://github.com/rvagg/bl/compare/v6.0.14...v6.0.15) (2024-09-10) 39 | 40 | ### Trivial Changes 41 | 42 | * **deps-dev:** bump typescript from 5.5.4 to 5.6.2 ([edfc739](https://github.com/rvagg/bl/commit/edfc73964665530cfcc046319b446c6b6efeebff)) 43 | 44 | ## [6.0.14](https://github.com/rvagg/bl/compare/v6.0.13...v6.0.14) (2024-07-10) 45 | 46 | ### Trivial Changes 47 | 48 | * **deps:** bump actions/setup-node from 4.0.2 to 4.0.3 ([09aa80d](https://github.com/rvagg/bl/commit/09aa80de083f045f0fd92414e97f2c241f8f15bf)) 49 | 50 | ## [6.0.13](https://github.com/rvagg/bl/compare/v6.0.12...v6.0.13) (2024-06-21) 51 | 52 | ### Trivial Changes 53 | 54 | * **deps-dev:** bump typescript from 5.4.5 to 5.5.2 ([41eff82](https://github.com/rvagg/bl/commit/41eff826534912051ab60fe7c36baad7a3c09492)) 55 | 56 | ## [6.0.12](https://github.com/rvagg/bl/compare/v6.0.11...v6.0.12) (2024-03-07) 57 | 58 | 59 | ### Trivial Changes 60 | 61 | * **deps-dev:** bump typescript from 5.3.3 to 5.4.2 ([18e99a2](https://github.com/rvagg/bl/commit/18e99a233d82c0ff5f3b00b04aab5a4ce6d37452)) 62 | 63 | ## [6.0.11](https://github.com/rvagg/bl/compare/v6.0.10...v6.0.11) (2024-02-08) 64 | 65 | 66 | ### Trivial Changes 67 | 68 | * **deps:** bump actions/setup-node from 4.0.1 to 4.0.2 ([d8ac460](https://github.com/rvagg/bl/commit/d8ac460597a24b0e783da2acd6ab37eacbbb0af5)) 69 | * update Node.js versions in CI ([863a5e0](https://github.com/rvagg/bl/commit/863a5e02f2c144c54be88ff962b0a902684c6527)) 70 | 71 | ## [6.0.10](https://github.com/rvagg/bl/compare/v6.0.9...v6.0.10) (2024-01-01) 72 | 73 | 74 | ### Trivial Changes 75 | 76 | * **deps:** bump actions/setup-node from 4.0.0 to 4.0.1 ([a018907](https://github.com/rvagg/bl/commit/a0189073aee3e906b135a37595f8b4007e6dd3e7)) 77 | 78 | ## [6.0.9](https://github.com/rvagg/bl/compare/v6.0.8...v6.0.9) (2023-11-27) 79 | 80 | 81 | ### Trivial Changes 82 | 83 | * **deps-dev:** bump typescript from 5.2.2 to 5.3.2 ([bb294bd](https://github.com/rvagg/bl/commit/bb294bd7baa5c5e1e062bd23b5d714692e04d414)) 84 | 85 | ## [6.0.8](https://github.com/rvagg/bl/compare/v6.0.7...v6.0.8) (2023-10-25) 86 | 87 | 88 | ### Trivial Changes 89 | 90 | * **deps:** bump actions/checkout from 3 to 4 ([a9ad973](https://github.com/rvagg/bl/commit/a9ad973d1fe4e5f673fe3b9b72b4484136e1655d)) 91 | * **deps:** bump actions/setup-node from 3.8.1 to 4.0.0 ([5921489](https://github.com/rvagg/bl/commit/59214897520fd6ba6d20a7cf370373275d4cfe1d)) 92 | 93 | ## [6.0.7](https://github.com/rvagg/bl/compare/v6.0.6...v6.0.7) (2023-08-25) 94 | 95 | 96 | ### Trivial Changes 97 | 98 | * **deps-dev:** bump typescript from 5.1.6 to 5.2.2 ([7e539ad](https://github.com/rvagg/bl/commit/7e539ad2e9cf959f431e82eaafe137cf33cf22ef)) 99 | 100 | ## [6.0.6](https://github.com/rvagg/bl/compare/v6.0.5...v6.0.6) (2023-08-18) 101 | 102 | 103 | ### Trivial Changes 104 | 105 | * **deps:** bump actions/setup-node from 3.8.0 to 3.8.1 ([39d3e17](https://github.com/rvagg/bl/commit/39d3e1729f0a7ddeac21e02b7983b0255ea212a2)) 106 | 107 | ## [6.0.5](https://github.com/rvagg/bl/compare/v6.0.4...v6.0.5) (2023-08-15) 108 | 109 | 110 | ### Trivial Changes 111 | 112 | * **deps:** bump actions/setup-node from 3.7.0 to 3.8.0 ([183d80a](https://github.com/rvagg/bl/commit/183d80a616a32e5473ac47e46cecd19ca0dfcf9f)) 113 | 114 | ## [6.0.4](https://github.com/rvagg/bl/compare/v6.0.3...v6.0.4) (2023-08-07) 115 | 116 | 117 | ### Trivial Changes 118 | 119 | * **deps-dev:** bump @types/readable-stream from 2.3.15 to 4.0.0 ([dd8cdb0](https://github.com/rvagg/bl/commit/dd8cdb0c64e1272c21d3bb251971afaaabbb0a1b)) 120 | 121 | ## [6.0.3](https://github.com/rvagg/bl/compare/v6.0.2...v6.0.3) (2023-07-07) 122 | 123 | 124 | ### Trivial Changes 125 | 126 | * **deps:** bump actions/setup-node from 3.6.0 to 3.7.0 ([40ac0a5](https://github.com/rvagg/bl/commit/40ac0a52e3c1ef83ae95d9433aebe4135f79b761)) 127 | 128 | ## [6.0.2](https://github.com/rvagg/bl/compare/v6.0.1...v6.0.2) (2023-06-05) 129 | 130 | 131 | ### Trivial Changes 132 | 133 | * **deps-dev:** bump typescript from 5.0.4 to 5.1.3 ([bea30ad](https://github.com/rvagg/bl/commit/bea30addef635d30f6e97769afacf5049615cdfe)) 134 | 135 | ## [6.0.1](https://github.com/rvagg/bl/compare/v6.0.0...v6.0.1) (2023-03-17) 136 | 137 | 138 | ### Bug Fixes 139 | 140 | * release with Node.js 18 ([6965a1d](https://github.com/rvagg/bl/commit/6965a1dee6b2af5bca304c8c9b747b796a652ffd)) 141 | 142 | 143 | ### Trivial Changes 144 | 145 | * **deps-dev:** bump typescript from 4.9.5 to 5.0.2 ([0885658](https://github.com/rvagg/bl/commit/0885658f7c1696220ac846e5bbc19f8b6ae8d3c0)) 146 | * **no-release:** bump actions/setup-node from 3.5.1 to 3.6.0 ([#120](https://github.com/rvagg/bl/issues/120)) ([60bee1b](https://github.com/rvagg/bl/commit/60bee1bd37a9f1a2a128f506f7da008c094db5c4)) 147 | * **no-release:** bump typescript from 4.8.4 to 4.9.3 ([#118](https://github.com/rvagg/bl/issues/118)) ([8be6dd6](https://github.com/rvagg/bl/commit/8be6dd62f639fd6c2c2f7d5d6ac4db988adb1886)) 148 | 149 | ## [6.0.0](https://github.com/rvagg/bl/compare/v5.1.0...v6.0.0) (2022-10-19) 150 | 151 | 152 | ### ⚠ BREAKING CHANGES 153 | 154 | * **deps:** bump readable-stream from 3.6.0 to 4.2.0 155 | * added bigint (Int64) support 156 | 157 | ### Features 158 | 159 | * added bigint (Int64) support ([131ad32](https://github.com/rvagg/bl/commit/131ad3217b91090323513a8ea3ef179e8427cf47)) 160 | 161 | 162 | ### Trivial Changes 163 | 164 | * add TypeScript definitions for BigInt ([78c5ff4](https://github.com/rvagg/bl/commit/78c5ff489235a4e4233086c364133123c71acef4)) 165 | * **deps-dev:** bump typescript from 4.7.4 to 4.8.4 ([dba13e1](https://github.com/rvagg/bl/commit/dba13e1cadc5857dde6a9425e975faf2abbb270f)) 166 | * **deps:** bump readable-stream from 3.6.0 to 4.2.0 ([fa03eda](https://github.com/rvagg/bl/commit/fa03eda54b4412c0fdfc9053bd0b0bebaf80bfd9)) 167 | * **docs:** BigInt in API docs ([c68af50](https://github.com/rvagg/bl/commit/c68af500a04b2c3a14132ae6946412d2e39402d0)) 168 | 169 | ## [5.1.0](https://github.com/rvagg/bl/compare/v5.0.0...v5.1.0) (2022-10-18) 170 | 171 | 172 | ### Features 173 | 174 | * added integrated TypeScript typings ([#108](https://github.com/rvagg/bl/issues/108)) ([433ff89](https://github.com/rvagg/bl/commit/433ff8942f47fab8a5c9d13b2c00989ccf8d0710)) 175 | 176 | 177 | ### Bug Fixes 178 | 179 | * windows support in tests ([387dfaf](https://github.com/rvagg/bl/commit/387dfaf9b2bca7849f12785436ceb01e42adac2c)) 180 | 181 | 182 | ### Trivial Changes 183 | 184 | * GH Actions, Dependabot, auto-release, remove Travis ([997f058](https://github.com/rvagg/bl/commit/997f058357de8f2a7f66998e80a72b491835573f)) 185 | * **no-release:** bump standard from 16.0.4 to 17.0.0 ([#112](https://github.com/rvagg/bl/issues/112)) ([078bfe3](https://github.com/rvagg/bl/commit/078bfe33390d125297b1c946e5989c4aa9228961)) 186 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright (c) 2013-2019 bl contributors 5 | ---------------------------------- 6 | 7 | *bl contributors listed at * 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bl *(BufferList)* 2 | 3 | ![Build Status](https://img.shields.io/github/actions/workflow/status/rvagg/bl/test-and-release.yml?branch=master) 4 | 5 | **A Node.js Buffer list collector, reader and streamer thingy.** 6 | 7 | [![NPM](https://nodei.co/npm/bl.svg)](https://nodei.co/npm/bl/) 8 | 9 | **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! 10 | 11 | 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. 12 | 13 | ```js 14 | const { BufferList } = require('bl') 15 | 16 | const bl = new BufferList() 17 | bl.append(Buffer.from('abcd')) 18 | bl.append(Buffer.from('efg')) 19 | bl.append('hi') // bl will also accept & convert Strings 20 | bl.append(Buffer.from('j')) 21 | bl.append(Buffer.from([ 0x3, 0x4 ])) 22 | 23 | console.log(bl.length) // 12 24 | 25 | console.log(bl.slice(0, 10).toString('ascii')) // 'abcdefghij' 26 | console.log(bl.slice(3, 10).toString('ascii')) // 'defghij' 27 | console.log(bl.slice(3, 6).toString('ascii')) // 'def' 28 | console.log(bl.slice(3, 8).toString('ascii')) // 'defgh' 29 | console.log(bl.slice(5, 10).toString('ascii')) // 'fghij' 30 | 31 | console.log(bl.indexOf('def')) // 3 32 | console.log(bl.indexOf('asdf')) // -1 33 | 34 | // or just use toString! 35 | console.log(bl.toString()) // 'abcdefghij\u0003\u0004' 36 | console.log(bl.toString('ascii', 3, 8)) // 'defgh' 37 | console.log(bl.toString('ascii', 5, 10)) // 'fghij' 38 | 39 | // other standard Buffer readables 40 | console.log(bl.readUInt16BE(10)) // 0x0304 41 | console.log(bl.readUInt16LE(10)) // 0x0403 42 | ``` 43 | 44 | Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**: 45 | 46 | ```js 47 | const { BufferListStream } = require('bl') 48 | const fs = require('fs') 49 | 50 | fs.createReadStream('README.md') 51 | .pipe(BufferListStream((err, data) => { // note 'new' isn't strictly required 52 | // `data` is a complete Buffer object containing the full data 53 | console.log(data.toString()) 54 | })) 55 | ``` 56 | 57 | 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. 58 | 59 | 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!): 60 | 61 | ```js 62 | const hyperquest = require('hyperquest') 63 | const { BufferListStream } = require('bl') 64 | 65 | const url = 'https://raw.github.com/rvagg/bl/master/README.md' 66 | 67 | hyperquest(url).pipe(BufferListStream((err, data) => { 68 | console.log(data.toString()) 69 | })) 70 | ``` 71 | 72 | Or, use it as a readable stream to recompose a list of Buffers to an output source: 73 | 74 | ```js 75 | const { BufferListStream } = require('bl') 76 | const fs = require('fs') 77 | 78 | var bl = new BufferListStream() 79 | bl.append(Buffer.from('abcd')) 80 | bl.append(Buffer.from('efg')) 81 | bl.append(Buffer.from('hi')) 82 | bl.append(Buffer.from('j')) 83 | 84 | bl.pipe(fs.createWriteStream('gibberish.txt')) 85 | ``` 86 | 87 | ## API 88 | 89 | * new BufferList([ buf ]) 90 | * BufferList.isBufferList(obj) 91 | * bl.length 92 | * bl.append(buffer) 93 | * bl.append(buffer) 94 | * bl.get(index) 95 | * bl.indexOf(value[, byteOffset][, encoding]) 96 | * bl.slice([ start[, end ] ]) 97 | * bl.shallowSlice([ start[, end ] ]) 98 | * bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ]) 99 | * bl.duplicate() 100 | * bl.consume(bytes) 101 | * bl.toString([encoding, [ start, [ end ]]]) 102 | * bl.readDoubleBE(), bl.readDoubleLE(), bl.readFloatBE(), bl.readFloatLE(), bl.readBigInt64BE(), bl.readBigInt64LE(), bl.readBigUInt64BE(), bl.readBigUInt64LE(), bl.readInt32BE(), bl.readInt32LE(), bl.readUInt32BE(), bl.readUInt32LE(), bl.readInt16BE(), bl.readInt16LE(), bl.readUInt16BE(), bl.readUInt16LE(), bl.readInt8(), bl.readUInt8() 103 | * new BufferListStream([ callback ]) 104 | * bl.getBuffers() 105 | 106 | -------------------------------------------------------- 107 | 108 | ### new BufferList([ Buffer | Buffer array | BufferList | BufferList array | String ]) 109 | 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. 110 | 111 | `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: 112 | 113 | ```js 114 | const { BufferList } = require('bl') 115 | const bl = BufferList() 116 | 117 | // equivalent to: 118 | 119 | const { BufferList } = require('bl') 120 | const bl = new BufferList() 121 | ``` 122 | 123 | -------------------------------------------------------- 124 | 125 | ### BufferList.isBufferList(obj) 126 | 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. 127 | 128 | N.B. this won't return `true` for `BufferList` or `BufferListStream` instances created by versions of this library before this static method was added. 129 | 130 | -------------------------------------------------------- 131 | 132 | ### bl.length 133 | 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. 134 | 135 | -------------------------------------------------------- 136 | 137 | ### bl.append(Buffer | Buffer array | BufferList | BufferList array | String) 138 | `append(buffer)` adds an additional buffer or BufferList to the internal list. `this` is returned so it can be chained. 139 | 140 | -------------------------------------------------------- 141 | 142 | ### bl.prepend(Buffer | Buffer array | BufferList | BufferList array | String) 143 | `prepend(buffer)` adds an additional buffer or BufferList at the beginning of the internal list. `this` is returned so it can be chained. 144 | 145 | -------------------------------------------------------- 146 | 147 | ### bl.get(index) 148 | `get()` will return the byte at the specified index. 149 | 150 | -------------------------------------------------------- 151 | 152 | ### bl.indexOf(value[, byteOffset][, encoding]) 153 | `get()` will return the byte at the specified index. 154 | `indexOf()` method returns the first index at which a given element can be found in the BufferList, or -1 if it is not present. 155 | 156 | -------------------------------------------------------- 157 | 158 | ### bl.slice([ start, [ end ] ]) 159 | `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. 160 | 161 | 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. 162 | 163 | -------------------------------------------------------- 164 | 165 | ### bl.shallowSlice([ start, [ end ] ]) 166 | `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. 167 | 168 | No copies will be performed. All buffers in the result share memory with the original list. 169 | 170 | -------------------------------------------------------- 171 | 172 | ### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ]) 173 | `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. 174 | 175 | -------------------------------------------------------- 176 | 177 | ### bl.duplicate() 178 | `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: 179 | 180 | ```js 181 | var bl = new BufferListStream() 182 | 183 | bl.append('hello') 184 | bl.append(' world') 185 | bl.append('\n') 186 | 187 | bl.duplicate().pipe(process.stdout, { end: false }) 188 | 189 | console.log(bl.toString()) 190 | ``` 191 | 192 | -------------------------------------------------------- 193 | 194 | ### bl.consume(bytes) 195 | `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—initial offsets will be calculated accordingly in order to give you a consistent view of the data. 196 | 197 | -------------------------------------------------------- 198 | 199 | ### bl.toString([encoding, [ start, [ end ]]]) 200 | `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. 201 | 202 | -------------------------------------------------------- 203 | 204 | ### bl.readDoubleBE(), bl.readDoubleLE(), bl.readFloatBE(), bl.readFloatLE(), bl.readBigInt64BE(), bl.readBigInt64LE(), bl.readBigUInt64BE(), bl.readBigUInt64LE(), bl.readInt32BE(), bl.readInt32LE(), bl.readUInt32BE(), bl.readUInt32LE(), bl.readInt16BE(), bl.readInt16LE(), bl.readUInt16BE(), bl.readUInt16LE(), bl.readInt8(), bl.readUInt8() 205 | 206 | All of the standard byte-reading methods of the `Buffer` interface are implemented and will operate across internal Buffer boundaries transparently. 207 | 208 | See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. 209 | 210 | -------------------------------------------------------- 211 | 212 | ### new BufferListStream([ callback | Buffer | Buffer array | BufferList | BufferList array | String ]) 213 | **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. 214 | 215 | 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. 216 | 217 | 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. 218 | 219 | `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: 220 | 221 | ```js 222 | const { BufferListStream } = require('bl') 223 | const bl = BufferListStream() 224 | 225 | // equivalent to: 226 | 227 | const { BufferListStream } = require('bl') 228 | const bl = new BufferListStream() 229 | ``` 230 | 231 | N.B. For backwards compatibility reasons, `BufferListStream` is the **default** export when you `require('bl')`: 232 | 233 | ```js 234 | const { BufferListStream } = require('bl') 235 | // equivalent to: 236 | const BufferListStream = require('bl') 237 | ``` 238 | 239 | -------------------------------------------------------- 240 | 241 | ### bl.getBuffers() 242 | 243 | `getBuffers()` returns the internal list of buffers. 244 | 245 | 246 | ## Contributors 247 | 248 | **bl** is brought to you by the following hackers: 249 | 250 | * [Rod Vagg](https://github.com/rvagg) 251 | * [Matteo Collina](https://github.com/mcollina) 252 | * [Jarett Cruger](https://github.com/jcrugzz) 253 | 254 | 255 | ## License & copyright 256 | 257 | Copyright (c) 2013-2019 bl contributors (listed above). 258 | 259 | 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. 260 | -------------------------------------------------------------------------------- /bl.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const DuplexStream = require('readable-stream').Duplex 4 | const inherits = require('inherits') 5 | const BufferList = require('./BufferList') 6 | 7 | function BufferListStream (callback) { 8 | if (!(this instanceof BufferListStream)) { 9 | return new BufferListStream(callback) 10 | } 11 | 12 | if (typeof callback === 'function') { 13 | this._callback = callback 14 | 15 | const piper = function piper (err) { 16 | if (this._callback) { 17 | this._callback(err) 18 | this._callback = null 19 | } 20 | }.bind(this) 21 | 22 | this.on('pipe', function onPipe (src) { 23 | src.on('error', piper) 24 | }) 25 | this.on('unpipe', function onUnpipe (src) { 26 | src.removeListener('error', piper) 27 | }) 28 | 29 | callback = null 30 | } 31 | 32 | BufferList._init.call(this, callback) 33 | DuplexStream.call(this) 34 | } 35 | 36 | inherits(BufferListStream, DuplexStream) 37 | Object.assign(BufferListStream.prototype, BufferList.prototype) 38 | 39 | BufferListStream.prototype._new = function _new (callback) { 40 | return new BufferListStream(callback) 41 | } 42 | 43 | BufferListStream.prototype._write = function _write (buf, encoding, callback) { 44 | this._appendBuffer(buf) 45 | 46 | if (typeof callback === 'function') { 47 | callback() 48 | } 49 | } 50 | 51 | BufferListStream.prototype._read = function _read (size) { 52 | if (!this.length) { 53 | return this.push(null) 54 | } 55 | 56 | size = Math.min(size, this.length) 57 | this.push(this.slice(0, size)) 58 | this.consume(size) 59 | } 60 | 61 | BufferListStream.prototype.end = function end (chunk) { 62 | DuplexStream.prototype.end.call(this, chunk) 63 | 64 | if (this._callback) { 65 | this._callback(null, this.slice()) 66 | this._callback = null 67 | } 68 | } 69 | 70 | BufferListStream.prototype._destroy = function _destroy (err, cb) { 71 | this._bufs.length = 0 72 | this.length = 0 73 | cb(err) 74 | } 75 | 76 | BufferListStream.prototype._isBufferList = function _isBufferList (b) { 77 | return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b) 78 | } 79 | 80 | BufferListStream.isBufferList = BufferList.isBufferList 81 | 82 | module.exports = BufferListStream 83 | module.exports.BufferListStream = BufferListStream 84 | module.exports.BufferList = BufferList 85 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import { Duplex } from "readable-stream"; 2 | import { 3 | BufferList as BL, 4 | BufferListConstructor, 5 | BufferListAcceptedTypes, 6 | } from "./BufferList"; 7 | 8 | type BufferListStreamInit = 9 | | ((err: Error, buffer: Buffer) => void) 10 | | BufferListAcceptedTypes; 11 | 12 | interface BufferListStreamConstructor { 13 | new (initData?: BufferListStreamInit): BufferListStream; 14 | (callback?: BufferListStreamInit): BufferListStream; 15 | 16 | /** 17 | * Determines if the passed object is a BufferList. It will return true 18 | * if the passed object is an instance of BufferList or BufferListStream 19 | * and false otherwise. 20 | * 21 | * N.B. this won't return true for BufferList or BufferListStream instances 22 | * created by versions of this library before this static method was added. 23 | * 24 | * @param other 25 | */ 26 | 27 | isBufferList(other: unknown): boolean; 28 | 29 | /** 30 | * Rexporting BufferList and BufferListStream to fix 31 | * issue with require/commonjs import and "export = " below. 32 | */ 33 | 34 | BufferList: BufferListConstructor; 35 | BufferListStream: BufferListStreamConstructor; 36 | } 37 | 38 | interface BufferListStream extends Duplex, BL { 39 | prototype: BufferListStream & BL; 40 | } 41 | 42 | /** 43 | * BufferListStream is a Node Duplex Stream, so it can be read from 44 | * and written to like a standard Node stream. You can also pipe() 45 | * to and from a BufferListStream instance. 46 | * 47 | * The constructor takes an optional callback, if supplied, the 48 | * callback will be called with an error argument followed by a 49 | * reference to the bl instance, when bl.end() is called 50 | * (i.e. from a piped stream). 51 | * 52 | * This is a convenient method of collecting the entire contents of 53 | * a stream, particularly when the stream is chunky, such as a network 54 | * stream. 55 | * 56 | * Normally, no arguments are required for the constructor, but you can 57 | * initialise the list by passing in a single Buffer object or an array 58 | * of Buffer object. 59 | * 60 | * `new` is not strictly required, if you don't instantiate a new object, 61 | * it will be done automatically for you so you can create a new instance 62 | * simply with: 63 | * 64 | * ```js 65 | * const { BufferListStream } = require('bl'); 66 | * const bl = BufferListStream(); 67 | * 68 | * // equivalent to: 69 | * 70 | * const { BufferListStream } = require('bl'); 71 | * const bl = new BufferListStream(); 72 | * ``` 73 | * 74 | * N.B. For backwards compatibility reasons, BufferListStream is the default 75 | * export when you `require('bl')`: 76 | * 77 | * ```js 78 | * const { BufferListStream } = require('bl') 79 | * 80 | * // equivalent to: 81 | * 82 | * const BufferListStream = require('bl') 83 | * ``` 84 | */ 85 | 86 | declare const BufferListStream: BufferListStreamConstructor; 87 | 88 | export = BufferListStream; 89 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bl", 3 | "version": "6.1.0", 4 | "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!", 5 | "license": "MIT", 6 | "main": "bl.js", 7 | "scripts": { 8 | "lint": "standard *.js test/*.js", 9 | "test": "npm run lint && npm run test:types && node test/test.js | faucet", 10 | "test:ci": "npm run lint && node test/test.js && npm run test:types", 11 | "test:types": "tsc --target esnext --moduleResolution node --allowJs --noEmit --skipLibCheck test/test.js", 12 | "build": "true" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/rvagg/bl.git" 17 | }, 18 | "homepage": "https://github.com/rvagg/bl", 19 | "authors": [ 20 | "Rod Vagg (https://github.com/rvagg)", 21 | "Matteo Collina (https://github.com/mcollina)", 22 | "Jarett Cruger (https://github.com/jcrugzz)" 23 | ], 24 | "keywords": [ 25 | "buffer", 26 | "buffers", 27 | "stream", 28 | "awesomesauce" 29 | ], 30 | "dependencies": { 31 | "@types/readable-stream": "^4.0.0", 32 | "buffer": "^6.0.3", 33 | "inherits": "^2.0.4", 34 | "readable-stream": "^4.2.0" 35 | }, 36 | "devDependencies": { 37 | "faucet": "~0.0.1", 38 | "standard": "^17.0.0", 39 | "tape": "^5.2.2", 40 | "typescript": "~5.8.2" 41 | }, 42 | "release": { 43 | "branches": [ 44 | "master" 45 | ], 46 | "plugins": [ 47 | [ 48 | "@semantic-release/commit-analyzer", 49 | { 50 | "preset": "conventionalcommits", 51 | "releaseRules": [ 52 | { 53 | "breaking": true, 54 | "release": "major" 55 | }, 56 | { 57 | "revert": true, 58 | "release": "patch" 59 | }, 60 | { 61 | "type": "feat", 62 | "release": "minor" 63 | }, 64 | { 65 | "type": "fix", 66 | "release": "patch" 67 | }, 68 | { 69 | "type": "chore", 70 | "release": "patch" 71 | }, 72 | { 73 | "type": "docs", 74 | "release": "patch" 75 | }, 76 | { 77 | "type": "test", 78 | "release": "patch" 79 | }, 80 | { 81 | "scope": "no-release", 82 | "release": false 83 | } 84 | ] 85 | } 86 | ], 87 | [ 88 | "@semantic-release/release-notes-generator", 89 | { 90 | "preset": "conventionalcommits", 91 | "presetConfig": { 92 | "types": [ 93 | { 94 | "type": "feat", 95 | "section": "Features" 96 | }, 97 | { 98 | "type": "fix", 99 | "section": "Bug Fixes" 100 | }, 101 | { 102 | "type": "chore", 103 | "section": "Trivial Changes" 104 | }, 105 | { 106 | "type": "docs", 107 | "section": "Trivial Changes" 108 | }, 109 | { 110 | "type": "test", 111 | "section": "Tests" 112 | } 113 | ] 114 | } 115 | } 116 | ], 117 | "@semantic-release/changelog", 118 | "@semantic-release/npm", 119 | "@semantic-release/github", 120 | "@semantic-release/git" 121 | ] 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /test/convert.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const tape = require('tape') 4 | const { BufferList, BufferListStream } = require('../') 5 | const { Buffer } = require('buffer') 6 | 7 | tape('convert from BufferList to BufferListStream', (t) => { 8 | const data = Buffer.from(`TEST-${Date.now()}`) 9 | const bl = new BufferList(data) 10 | const bls = new BufferListStream(bl) 11 | t.ok(bl.slice().equals(bls.slice())) 12 | t.end() 13 | }) 14 | 15 | tape('convert from BufferListStream to BufferList', (t) => { 16 | const data = Buffer.from(`TEST-${Date.now()}`) 17 | const bls = new BufferListStream(data) 18 | const bl = new BufferList(bls) 19 | t.ok(bl.slice().equals(bls.slice())) 20 | t.end() 21 | }) 22 | -------------------------------------------------------------------------------- /test/indexOf.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const tape = require('tape') 4 | const BufferList = require('../') 5 | const { Buffer } = require('buffer') 6 | 7 | tape('indexOf single byte needle', (t) => { 8 | const bl = new BufferList(['abcdefg', 'abcdefg', '12345']) 9 | 10 | t.equal(bl.indexOf('e'), 4) 11 | t.equal(bl.indexOf('e', 5), 11) 12 | t.equal(bl.indexOf('e', 12), -1) 13 | t.equal(bl.indexOf('5'), 18) 14 | 15 | t.end() 16 | }) 17 | 18 | tape('indexOf multiple byte needle', (t) => { 19 | const bl = new BufferList(['abcdefg', 'abcdefg']) 20 | 21 | t.equal(bl.indexOf('ef'), 4) 22 | t.equal(bl.indexOf('ef', 5), 11) 23 | 24 | t.end() 25 | }) 26 | 27 | tape('indexOf multiple byte needles across buffer boundaries', (t) => { 28 | const bl = new BufferList(['abcdefg', 'abcdefg']) 29 | 30 | t.equal(bl.indexOf('fgabc'), 5) 31 | 32 | t.end() 33 | }) 34 | 35 | tape('indexOf takes a Uint8Array search', (t) => { 36 | const bl = new BufferList(['abcdefg', 'abcdefg']) 37 | const search = new Uint8Array([102, 103, 97, 98, 99]) // fgabc 38 | 39 | t.equal(bl.indexOf(search), 5) 40 | 41 | t.end() 42 | }) 43 | 44 | tape('indexOf takes a buffer list search', (t) => { 45 | const bl = new BufferList(['abcdefg', 'abcdefg']) 46 | const search = new BufferList('fgabc') 47 | 48 | t.equal(bl.indexOf(search), 5) 49 | 50 | t.end() 51 | }) 52 | 53 | tape('indexOf a zero byte needle', (t) => { 54 | const b = new BufferList('abcdef') 55 | const bufEmpty = Buffer.from('') 56 | 57 | t.equal(b.indexOf(''), 0) 58 | t.equal(b.indexOf('', 1), 1) 59 | t.equal(b.indexOf('', b.length + 1), b.length) 60 | t.equal(b.indexOf('', Infinity), b.length) 61 | t.equal(b.indexOf(bufEmpty), 0) 62 | t.equal(b.indexOf(bufEmpty, 1), 1) 63 | t.equal(b.indexOf(bufEmpty, b.length + 1), b.length) 64 | t.equal(b.indexOf(bufEmpty, Infinity), b.length) 65 | 66 | t.end() 67 | }) 68 | 69 | tape('indexOf buffers smaller and larger than the needle', (t) => { 70 | const bl = new BufferList(['abcdefg', 'a', 'bcdefg', 'a', 'bcfgab']) 71 | 72 | t.equal(bl.indexOf('fgabc'), 5) 73 | t.equal(bl.indexOf('fgabc', 6), 12) 74 | t.equal(bl.indexOf('fgabc', 13), -1) 75 | 76 | t.end() 77 | }) 78 | 79 | // only present in node 6+ 80 | ;(process.version.substr(1).split('.')[0] >= 6) && tape('indexOf latin1 and binary encoding', (t) => { 81 | const b = new BufferList('abcdef') 82 | 83 | // test latin1 encoding 84 | t.equal( 85 | new BufferList(Buffer.from(b.toString('latin1'), 'latin1')) 86 | .indexOf('d', 0, 'latin1'), 87 | 3 88 | ) 89 | t.equal( 90 | new BufferList(Buffer.from(b.toString('latin1'), 'latin1')) 91 | .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'), 92 | 3 93 | ) 94 | t.equal( 95 | new BufferList(Buffer.from('aa\u00e8aa', 'latin1')) 96 | .indexOf('\u00e8', 'latin1'), 97 | 2 98 | ) 99 | t.equal( 100 | new BufferList(Buffer.from('\u00e8', 'latin1')) 101 | .indexOf('\u00e8', 'latin1'), 102 | 0 103 | ) 104 | t.equal( 105 | new BufferList(Buffer.from('\u00e8', 'latin1')) 106 | .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'), 107 | 0 108 | ) 109 | 110 | // test binary encoding 111 | t.equal( 112 | new BufferList(Buffer.from(b.toString('binary'), 'binary')) 113 | .indexOf('d', 0, 'binary'), 114 | 3 115 | ) 116 | t.equal( 117 | new BufferList(Buffer.from(b.toString('binary'), 'binary')) 118 | .indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 119 | 3 120 | ) 121 | t.equal( 122 | new BufferList(Buffer.from('aa\u00e8aa', 'binary')) 123 | .indexOf('\u00e8', 'binary'), 124 | 2 125 | ) 126 | t.equal( 127 | new BufferList(Buffer.from('\u00e8', 'binary')) 128 | .indexOf('\u00e8', 'binary'), 129 | 0 130 | ) 131 | t.equal( 132 | new BufferList(Buffer.from('\u00e8', 'binary')) 133 | .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 134 | 0 135 | ) 136 | 137 | t.end() 138 | }) 139 | 140 | tape('indexOf the entire nodejs10 buffer test suite', (t) => { 141 | const b = new BufferList('abcdef') 142 | const bufA = Buffer.from('a') 143 | const bufBc = Buffer.from('bc') 144 | const bufF = Buffer.from('f') 145 | const bufZ = Buffer.from('z') 146 | 147 | const stringComparison = 'abcdef' 148 | 149 | t.equal(b.indexOf('a'), 0) 150 | t.equal(b.indexOf('a', 1), -1) 151 | t.equal(b.indexOf('a', -1), -1) 152 | t.equal(b.indexOf('a', -4), -1) 153 | t.equal(b.indexOf('a', -b.length), 0) 154 | t.equal(b.indexOf('a', NaN), 0) 155 | t.equal(b.indexOf('a', -Infinity), 0) 156 | t.equal(b.indexOf('a', Infinity), -1) 157 | t.equal(b.indexOf('bc'), 1) 158 | t.equal(b.indexOf('bc', 2), -1) 159 | t.equal(b.indexOf('bc', -1), -1) 160 | t.equal(b.indexOf('bc', -3), -1) 161 | t.equal(b.indexOf('bc', -5), 1) 162 | t.equal(b.indexOf('bc', NaN), 1) 163 | t.equal(b.indexOf('bc', -Infinity), 1) 164 | t.equal(b.indexOf('bc', Infinity), -1) 165 | t.equal(b.indexOf('f'), b.length - 1) 166 | t.equal(b.indexOf('z'), -1) 167 | 168 | // empty search tests 169 | t.equal(b.indexOf(bufA), 0) 170 | t.equal(b.indexOf(bufA, 1), -1) 171 | t.equal(b.indexOf(bufA, -1), -1) 172 | t.equal(b.indexOf(bufA, -4), -1) 173 | t.equal(b.indexOf(bufA, -b.length), 0) 174 | t.equal(b.indexOf(bufA, NaN), 0) 175 | t.equal(b.indexOf(bufA, -Infinity), 0) 176 | t.equal(b.indexOf(bufA, Infinity), -1) 177 | t.equal(b.indexOf(bufBc), 1) 178 | t.equal(b.indexOf(bufBc, 2), -1) 179 | t.equal(b.indexOf(bufBc, -1), -1) 180 | t.equal(b.indexOf(bufBc, -3), -1) 181 | t.equal(b.indexOf(bufBc, -5), 1) 182 | t.equal(b.indexOf(bufBc, NaN), 1) 183 | t.equal(b.indexOf(bufBc, -Infinity), 1) 184 | t.equal(b.indexOf(bufBc, Infinity), -1) 185 | t.equal(b.indexOf(bufF), b.length - 1) 186 | t.equal(b.indexOf(bufZ), -1) 187 | t.equal(b.indexOf(0x61), 0) 188 | t.equal(b.indexOf(0x61, 1), -1) 189 | t.equal(b.indexOf(0x61, -1), -1) 190 | t.equal(b.indexOf(0x61, -4), -1) 191 | t.equal(b.indexOf(0x61, -b.length), 0) 192 | t.equal(b.indexOf(0x61, NaN), 0) 193 | t.equal(b.indexOf(0x61, -Infinity), 0) 194 | t.equal(b.indexOf(0x61, Infinity), -1) 195 | t.equal(b.indexOf(0x0), -1) 196 | 197 | // test offsets 198 | t.equal(b.indexOf('d', 2), 3) 199 | t.equal(b.indexOf('f', 5), 5) 200 | t.equal(b.indexOf('f', -1), 5) 201 | t.equal(b.indexOf('f', 6), -1) 202 | 203 | t.equal(b.indexOf(Buffer.from('d'), 2), 3) 204 | t.equal(b.indexOf(Buffer.from('f'), 5), 5) 205 | t.equal(b.indexOf(Buffer.from('f'), -1), 5) 206 | t.equal(b.indexOf(Buffer.from('f'), 6), -1) 207 | 208 | t.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1) 209 | 210 | // test invalid and uppercase encoding 211 | t.equal(b.indexOf('b', 'utf8'), 1) 212 | t.equal(b.indexOf('b', 'UTF8'), 1) 213 | t.equal(b.indexOf('62', 'HEX'), 1) 214 | t.throws(() => b.indexOf('bad', 'enc'), TypeError) 215 | 216 | // test hex encoding 217 | t.equal( 218 | Buffer.from(b.toString('hex'), 'hex') 219 | .indexOf('64', 0, 'hex'), 220 | 3 221 | ) 222 | t.equal( 223 | Buffer.from(b.toString('hex'), 'hex') 224 | .indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 225 | 3 226 | ) 227 | 228 | // test base64 encoding 229 | t.equal( 230 | Buffer.from(b.toString('base64'), 'base64') 231 | .indexOf('ZA==', 0, 'base64'), 232 | 3 233 | ) 234 | t.equal( 235 | Buffer.from(b.toString('base64'), 'base64') 236 | .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 237 | 3 238 | ) 239 | 240 | // test ascii encoding 241 | t.equal( 242 | Buffer.from(b.toString('ascii'), 'ascii') 243 | .indexOf('d', 0, 'ascii'), 244 | 3 245 | ) 246 | t.equal( 247 | Buffer.from(b.toString('ascii'), 'ascii') 248 | .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 249 | 3 250 | ) 251 | 252 | // test optional offset with passed encoding 253 | t.equal(Buffer.from('aaaa0').indexOf('30', 'hex'), 4) 254 | t.equal(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4) 255 | 256 | { 257 | // test usc2 encoding 258 | const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2') 259 | 260 | t.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2')) 261 | t.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2')) 262 | t.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2')) 263 | t.equal(4, twoByteString.indexOf( 264 | Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2')) 265 | t.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2')) 266 | } 267 | 268 | const mixedByteStringUcs2 = 269 | Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2') 270 | 271 | t.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2')) 272 | t.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2')) 273 | t.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2')) 274 | 275 | t.equal( 276 | 6, mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2')) 277 | t.equal( 278 | 10, mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2')) 279 | t.equal( 280 | -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')) 281 | 282 | { 283 | const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2') 284 | 285 | // Test single char pattern 286 | t.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2')) 287 | let index = twoByteString.indexOf('\u0391', 0, 'ucs2') 288 | t.equal(2, index, `Alpha - at index ${index}`) 289 | index = twoByteString.indexOf('\u03a3', 0, 'ucs2') 290 | t.equal(4, index, `First Sigma - at index ${index}`) 291 | index = twoByteString.indexOf('\u03a3', 6, 'ucs2') 292 | t.equal(6, index, `Second Sigma - at index ${index}`) 293 | index = twoByteString.indexOf('\u0395', 0, 'ucs2') 294 | t.equal(8, index, `Epsilon - at index ${index}`) 295 | index = twoByteString.indexOf('\u0392', 0, 'ucs2') 296 | t.equal(-1, index, `Not beta - at index ${index}`) 297 | 298 | // Test multi-char pattern 299 | index = twoByteString.indexOf('\u039a\u0391', 0, 'ucs2') 300 | t.equal(0, index, `Lambda Alpha - at index ${index}`) 301 | index = twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2') 302 | t.equal(2, index, `Alpha Sigma - at index ${index}`) 303 | index = twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2') 304 | t.equal(4, index, `Sigma Sigma - at index ${index}`) 305 | index = twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2') 306 | t.equal(6, index, `Sigma Epsilon - at index ${index}`) 307 | } 308 | 309 | const mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395') 310 | 311 | t.equal(5, mixedByteStringUtf8.indexOf('bc')) 312 | t.equal(5, mixedByteStringUtf8.indexOf('bc', 5)) 313 | t.equal(5, mixedByteStringUtf8.indexOf('bc', -8)) 314 | t.equal(7, mixedByteStringUtf8.indexOf('\u03a3')) 315 | t.equal(-1, mixedByteStringUtf8.indexOf('\u0396')) 316 | 317 | // Test complex string indexOf algorithms. Only trigger for long strings. 318 | // Long string that isn't a simple repeat of a shorter string. 319 | let longString = 'A' 320 | for (let i = 66; i < 76; i++) { // from 'B' to 'K' 321 | longString = longString + String.fromCharCode(i) + longString 322 | } 323 | 324 | const longBufferString = Buffer.from(longString) 325 | 326 | // pattern of 15 chars, repeated every 16 chars in long 327 | let pattern = 'ABACABADABACABA' 328 | for (let i = 0; i < longBufferString.length - pattern.length; i += 7) { 329 | const index = longBufferString.indexOf(pattern, i) 330 | t.equal((i + 15) & ~0xf, index, 331 | `Long ABACABA...-string at index ${i}`) 332 | } 333 | 334 | let index = longBufferString.indexOf('AJABACA') 335 | t.equal(510, index, `Long AJABACA, First J - at index ${index}`) 336 | index = longBufferString.indexOf('AJABACA', 511) 337 | t.equal(1534, index, `Long AJABACA, Second J - at index ${index}`) 338 | 339 | pattern = 'JABACABADABACABA' 340 | index = longBufferString.indexOf(pattern) 341 | t.equal(511, index, `Long JABACABA..., First J - at index ${index}`) 342 | index = longBufferString.indexOf(pattern, 512) 343 | t.equal( 344 | 1535, index, `Long JABACABA..., Second J - at index ${index}`) 345 | 346 | // Search for a non-ASCII string in a pure ASCII string. 347 | const asciiString = Buffer.from( 348 | 'somethingnotatallsinisterwhichalsoworks') 349 | t.equal(-1, asciiString.indexOf('\x2061')) 350 | t.equal(3, asciiString.indexOf('eth', 0)) 351 | 352 | // Search in string containing many non-ASCII chars. 353 | const allCodePoints = [] 354 | for (let i = 0; i < 65536; i++) { 355 | allCodePoints[i] = i 356 | } 357 | 358 | const allCharsString = String.fromCharCode.apply(String, allCodePoints) 359 | const allCharsBufferUtf8 = Buffer.from(allCharsString) 360 | const allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2') 361 | 362 | // Search for string long enough to trigger complex search with ASCII pattern 363 | // and UC16 subject. 364 | t.equal(-1, allCharsBufferUtf8.indexOf('notfound')) 365 | t.equal(-1, allCharsBufferUcs2.indexOf('notfound')) 366 | 367 | // Needle is longer than haystack, but only because it's encoded as UTF-16 368 | t.equal(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1) 369 | 370 | t.equal(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0) 371 | t.equal(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1) 372 | 373 | // Haystack has odd length, but the needle is UCS2. 374 | t.equal(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1) 375 | 376 | { 377 | // Find substrings in Utf8. 378 | const lengths = [1, 3, 15] // Single char, simple and complex. 379 | const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b] 380 | for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 381 | for (let i = 0; i < indices.length; i++) { 382 | const index = indices[i] 383 | let length = lengths[lengthIndex] 384 | 385 | if (index + length > 0x7F) { 386 | length = 2 * length 387 | } 388 | 389 | if (index + length > 0x7FF) { 390 | length = 3 * length 391 | } 392 | 393 | if (index + length > 0xFFFF) { 394 | length = 4 * length 395 | } 396 | 397 | const patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length) 398 | t.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8)) 399 | 400 | const patternStringUtf8 = patternBufferUtf8.toString() 401 | t.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8)) 402 | } 403 | } 404 | } 405 | 406 | { 407 | // Find substrings in Usc2. 408 | const lengths = [2, 4, 16] // Single char, simple and complex. 409 | const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0] 410 | 411 | for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 412 | for (let i = 0; i < indices.length; i++) { 413 | const index = indices[i] * 2 414 | const length = lengths[lengthIndex] 415 | 416 | const patternBufferUcs2 = 417 | allCharsBufferUcs2.slice(index, index + length) 418 | t.equal( 419 | index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2')) 420 | 421 | const patternStringUcs2 = patternBufferUcs2.toString('ucs2') 422 | t.equal( 423 | index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2')) 424 | } 425 | } 426 | } 427 | 428 | [ 429 | () => {}, 430 | {}, 431 | [] 432 | ].forEach((val) => { 433 | t.throws(() => b.indexOf(val), TypeError, `"${JSON.stringify(val)}" should throw`) 434 | }) 435 | 436 | // Test weird offset arguments. 437 | // The following offsets coerce to NaN or 0, searching the whole Buffer 438 | t.equal(b.indexOf('b', undefined), 1) 439 | t.equal(b.indexOf('b', {}), 1) 440 | t.equal(b.indexOf('b', 0), 1) 441 | t.equal(b.indexOf('b', null), 1) 442 | t.equal(b.indexOf('b', []), 1) 443 | 444 | // The following offset coerces to 2, in other words +[2] === 2 445 | t.equal(b.indexOf('b', [2]), -1) 446 | 447 | // Behavior should match String.indexOf() 448 | t.equal( 449 | b.indexOf('b', undefined), 450 | stringComparison.indexOf('b', undefined)) 451 | t.equal( 452 | b.indexOf('b', {}), 453 | stringComparison.indexOf('b', {})) 454 | t.equal( 455 | b.indexOf('b', 0), 456 | stringComparison.indexOf('b', 0)) 457 | t.equal( 458 | b.indexOf('b', null), 459 | stringComparison.indexOf('b', null)) 460 | t.equal( 461 | b.indexOf('b', []), 462 | stringComparison.indexOf('b', [])) 463 | t.equal( 464 | b.indexOf('b', [2]), 465 | stringComparison.indexOf('b', [2])) 466 | 467 | // test truncation of Number arguments to uint8 468 | { 469 | const buf = Buffer.from('this is a test') 470 | 471 | t.equal(buf.indexOf(0x6973), 3) 472 | t.equal(buf.indexOf(0x697320), 4) 473 | t.equal(buf.indexOf(0x69732069), 2) 474 | t.equal(buf.indexOf(0x697374657374), 0) 475 | t.equal(buf.indexOf(0x69737374), 0) 476 | t.equal(buf.indexOf(0x69737465), 11) 477 | t.equal(buf.indexOf(0x69737465), 11) 478 | t.equal(buf.indexOf(-140), 0) 479 | t.equal(buf.indexOf(-152), 1) 480 | t.equal(buf.indexOf(0xff), -1) 481 | t.equal(buf.indexOf(0xffff), -1) 482 | } 483 | 484 | // Test that Uint8Array arguments are okay. 485 | { 486 | const needle = new Uint8Array([0x66, 0x6f, 0x6f]) 487 | const haystack = new BufferList(Buffer.from('a foo b foo')) 488 | t.equal(haystack.indexOf(needle), 2) 489 | } 490 | 491 | t.end() 492 | }) 493 | -------------------------------------------------------------------------------- /test/isBufferList.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const tape = require('tape') 4 | const { BufferList, BufferListStream } = require('../') 5 | const { Buffer } = require('buffer') 6 | 7 | tape('isBufferList positives', (t) => { 8 | t.ok(BufferList.isBufferList(new BufferList())) 9 | t.ok(BufferList.isBufferList(new BufferListStream())) 10 | 11 | t.end() 12 | }) 13 | 14 | tape('isBufferList negatives', (t) => { 15 | const types = [ 16 | null, 17 | undefined, 18 | NaN, 19 | true, 20 | false, 21 | {}, 22 | [], 23 | Buffer.alloc(0), 24 | [Buffer.alloc(0)] 25 | ] 26 | 27 | for (const obj of types) { 28 | t.notOk(BufferList.isBufferList(obj)) 29 | } 30 | 31 | t.end() 32 | }) 33 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 'use strict' 3 | 4 | const tape = require('tape') 5 | const crypto = require('crypto') 6 | const fs = require('fs') 7 | const path = require('path') 8 | const os = require('os') 9 | const BufferListStream = require('../') 10 | const { Buffer } = require('buffer') 11 | 12 | /** 13 | * This typedef allows us to add _bufs to the API without declaring it publicly on types. 14 | * @typedef { BufferListStream & { _bufs?: Buffer[] }} BufferListStreamWithPrivate 15 | */ 16 | 17 | /** 18 | * Just for typechecking in js 19 | * @type { NodeJS.Process & { browser?: boolean }} 20 | */ 21 | 22 | const process = globalThis.process 23 | 24 | /** @type {BufferEncoding[]} */ 25 | const encodings = ['ascii', 'utf8', 'utf-8', 'hex', 'binary', 'base64'] 26 | 27 | if (process.browser) { 28 | encodings.push( 29 | 'ucs2', 30 | 'ucs-2', 31 | 'utf16le', 32 | /** 33 | * This alias is not in typescript typings for BufferEncoding. Still have to fix 34 | * @see https://nodejs.org/api/buffer.html#buffers-and-character-encodings 35 | */ 36 | // @ts-ignore 37 | 'utf-16le' 38 | ) 39 | } 40 | 41 | require('./indexOf') 42 | require('./isBufferList') 43 | require('./convert') 44 | 45 | tape('single bytes from single buffer', function (t) { 46 | const bl = new BufferListStream() 47 | 48 | bl.append(Buffer.from('abcd')) 49 | 50 | t.equal(bl.length, 4) 51 | t.equal(bl.get(-1), undefined) 52 | t.equal(bl.get(0), 97) 53 | t.equal(bl.get(1), 98) 54 | t.equal(bl.get(2), 99) 55 | t.equal(bl.get(3), 100) 56 | t.equal(bl.get(4), undefined) 57 | 58 | t.end() 59 | }) 60 | 61 | tape('single bytes from multiple buffers', function (t) { 62 | const bl = new BufferListStream() 63 | 64 | bl.append(Buffer.from('abcd')) 65 | bl.append(Buffer.from('efg')) 66 | bl.append(Buffer.from('hi')) 67 | bl.append(Buffer.from('j')) 68 | 69 | t.equal(bl.length, 10) 70 | 71 | t.equal(bl.get(0), 97) 72 | t.equal(bl.get(1), 98) 73 | t.equal(bl.get(2), 99) 74 | t.equal(bl.get(3), 100) 75 | t.equal(bl.get(4), 101) 76 | t.equal(bl.get(5), 102) 77 | t.equal(bl.get(6), 103) 78 | t.equal(bl.get(7), 104) 79 | t.equal(bl.get(8), 105) 80 | t.equal(bl.get(9), 106) 81 | 82 | t.end() 83 | }) 84 | 85 | tape('multi bytes from single buffer', function (t) { 86 | const bl = new BufferListStream() 87 | 88 | bl.append(Buffer.from('abcd')) 89 | 90 | t.equal(bl.length, 4) 91 | 92 | t.equal(bl.slice(0, 4).toString('ascii'), 'abcd') 93 | t.equal(bl.slice(0, 3).toString('ascii'), 'abc') 94 | t.equal(bl.slice(1, 4).toString('ascii'), 'bcd') 95 | t.equal(bl.slice(-4, -1).toString('ascii'), 'abc') 96 | 97 | t.end() 98 | }) 99 | 100 | tape('multi bytes from single buffer (negative indexes)', function (t) { 101 | const bl = new BufferListStream() 102 | 103 | bl.append(Buffer.from('buffer')) 104 | 105 | t.equal(bl.length, 6) 106 | 107 | t.equal(bl.slice(-6, -1).toString('ascii'), 'buffe') 108 | t.equal(bl.slice(-6, -2).toString('ascii'), 'buff') 109 | t.equal(bl.slice(-5, -2).toString('ascii'), 'uff') 110 | 111 | t.end() 112 | }) 113 | 114 | tape('multiple bytes from multiple buffers', function (t) { 115 | const bl = new BufferListStream() 116 | 117 | bl.append(Buffer.from('abcd')) 118 | bl.append(Buffer.from('efg')) 119 | bl.append(Buffer.from('hi')) 120 | bl.append(Buffer.from('j')) 121 | 122 | t.equal(bl.length, 10) 123 | 124 | t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij') 125 | t.equal(bl.slice(3, 10).toString('ascii'), 'defghij') 126 | t.equal(bl.slice(3, 6).toString('ascii'), 'def') 127 | t.equal(bl.slice(3, 8).toString('ascii'), 'defgh') 128 | t.equal(bl.slice(5, 10).toString('ascii'), 'fghij') 129 | t.equal(bl.slice(-7, -4).toString('ascii'), 'def') 130 | 131 | t.end() 132 | }) 133 | 134 | tape('multiple bytes from multiple buffer lists', function (t) { 135 | const bl = new BufferListStream() 136 | 137 | bl.append(new BufferListStream([Buffer.from('abcd'), Buffer.from('efg')])) 138 | bl.append(new BufferListStream([Buffer.from('hi'), Buffer.from('j')])) 139 | 140 | t.equal(bl.length, 10) 141 | 142 | t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij') 143 | 144 | t.equal(bl.slice(3, 10).toString('ascii'), 'defghij') 145 | t.equal(bl.slice(3, 6).toString('ascii'), 'def') 146 | t.equal(bl.slice(3, 8).toString('ascii'), 'defgh') 147 | t.equal(bl.slice(5, 10).toString('ascii'), 'fghij') 148 | 149 | t.end() 150 | }) 151 | 152 | // same data as previous test, just using nested constructors 153 | tape('multiple bytes from crazy nested buffer lists', function (t) { 154 | const bl = new BufferListStream() 155 | 156 | bl.append( 157 | new BufferListStream([ 158 | new BufferListStream([ 159 | new BufferListStream(Buffer.from('abc')), 160 | Buffer.from('d'), 161 | new BufferListStream(Buffer.from('efg')) 162 | ]), 163 | new BufferListStream([Buffer.from('hi')]), 164 | new BufferListStream(Buffer.from('j')) 165 | ]) 166 | ) 167 | 168 | t.equal(bl.length, 10) 169 | 170 | t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij') 171 | 172 | t.equal(bl.slice(3, 10).toString('ascii'), 'defghij') 173 | t.equal(bl.slice(3, 6).toString('ascii'), 'def') 174 | t.equal(bl.slice(3, 8).toString('ascii'), 'defgh') 175 | t.equal(bl.slice(5, 10).toString('ascii'), 'fghij') 176 | 177 | t.end() 178 | }) 179 | 180 | tape('append accepts arrays of Buffers', function (t) { 181 | const bl = new BufferListStream() 182 | 183 | bl.append(Buffer.from('abc')) 184 | bl.append([Buffer.from('def')]) 185 | bl.append([Buffer.from('ghi'), Buffer.from('jkl')]) 186 | bl.append([Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz')]) 187 | t.equal(bl.length, 26) 188 | t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz') 189 | 190 | t.end() 191 | }) 192 | 193 | tape('append accepts arrays of Uint8Arrays', function (t) { 194 | const bl = new BufferListStream() 195 | 196 | bl.append(new Uint8Array([97, 98, 99])) 197 | bl.append([Uint8Array.from([100, 101, 102])]) 198 | bl.append([new Uint8Array([103, 104, 105]), new Uint8Array([106, 107, 108])]) 199 | bl.append([new Uint8Array([109, 110, 111, 112]), new Uint8Array([113, 114, 115, 116, 117]), new Uint8Array([118, 119, 120, 121, 122])]) 200 | t.equal(bl.length, 26) 201 | t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz') 202 | 203 | t.end() 204 | }) 205 | 206 | tape('append accepts arrays of BufferLists', function (t) { 207 | const bl = new BufferListStream() 208 | 209 | bl.append(Buffer.from('abc')) 210 | bl.append([new BufferListStream('def')]) 211 | bl.append( 212 | new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')]) 213 | ) 214 | bl.append([ 215 | Buffer.from('mnop'), 216 | new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')]) 217 | ]) 218 | t.equal(bl.length, 26) 219 | t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz') 220 | 221 | t.end() 222 | }) 223 | 224 | tape('append chainable', function (t) { 225 | const bl = new BufferListStream() 226 | 227 | t.ok(bl.append(Buffer.from('abcd')) === bl) 228 | t.ok(bl.append([Buffer.from('abcd')]) === bl) 229 | t.ok(bl.append(new BufferListStream(Buffer.from('abcd'))) === bl) 230 | t.ok(bl.append([new BufferListStream(Buffer.from('abcd'))]) === bl) 231 | 232 | t.end() 233 | }) 234 | 235 | tape('append chainable (test results)', function (t) { 236 | const bl = new BufferListStream('abc') 237 | .append([new BufferListStream('def')]) 238 | .append( 239 | new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')]) 240 | ) 241 | .append([ 242 | Buffer.from('mnop'), 243 | new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')]) 244 | ]) 245 | 246 | t.equal(bl.length, 26) 247 | t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz') 248 | 249 | t.end() 250 | }) 251 | 252 | tape('prepend accepts arrays of Buffers', function (t) { 253 | const bl = new BufferListStream() 254 | 255 | bl.prepend(Buffer.from('abc')) 256 | bl.prepend([Buffer.from('def')]) 257 | bl.prepend([Buffer.from('ghi'), Buffer.from('jkl')]) 258 | bl.prepend([Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz')]) 259 | t.equal(bl.length, 26) 260 | t.equal(bl.slice().toString('ascii'), 'mnopqrstuvwxyzghijkldefabc') 261 | 262 | t.end() 263 | }) 264 | 265 | tape('prepend accepts arrays of Uint8Arrays', function (t) { 266 | const bl = new BufferListStream() 267 | 268 | bl.prepend(new Uint8Array([97, 98, 99])) 269 | bl.prepend([Uint8Array.from([100, 101, 102])]) 270 | bl.prepend([new Uint8Array([103, 104, 105]), new Uint8Array([106, 107, 108])]) 271 | bl.prepend([new Uint8Array([109, 110, 111, 112]), new Uint8Array([113, 114, 115, 116, 117]), new Uint8Array([118, 119, 120, 121, 122])]) 272 | t.equal(bl.length, 26) 273 | t.equal(bl.slice().toString('ascii'), 'mnopqrstuvwxyzghijkldefabc') 274 | 275 | t.end() 276 | }) 277 | 278 | tape('prepend accepts arrays of BufferLists', function (t) { 279 | const bl = new BufferListStream() 280 | 281 | bl.prepend(Buffer.from('abc')) 282 | bl.prepend([new BufferListStream('def')]) 283 | bl.prepend( 284 | new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')]) 285 | ) 286 | bl.prepend([ 287 | Buffer.from('mnop'), 288 | new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')]) 289 | ]) 290 | t.equal(bl.length, 26) 291 | t.equal(bl.slice().toString('ascii'), 'mnopqrstuvwxyzghijkldefabc') 292 | 293 | t.end() 294 | }) 295 | 296 | tape('prepend chainable', function (t) { 297 | const bl = new BufferListStream() 298 | 299 | t.ok(bl.prepend(Buffer.from('abcd')) === bl) 300 | t.ok(bl.prepend([Buffer.from('abcd')]) === bl) 301 | t.ok(bl.prepend(new BufferListStream(Buffer.from('abcd'))) === bl) 302 | t.ok(bl.prepend([new BufferListStream(Buffer.from('abcd'))]) === bl) 303 | 304 | t.end() 305 | }) 306 | 307 | tape('prepend chainable (test results)', function (t) { 308 | const bl = new BufferListStream('abc') 309 | .prepend([new BufferListStream('def')]) 310 | .prepend( 311 | new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')]) 312 | ) 313 | .prepend([ 314 | Buffer.from('mnop'), 315 | new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')]) 316 | ]) 317 | 318 | t.equal(bl.length, 26) 319 | t.equal(bl.slice().toString('ascii'), 'mnopqrstuvwxyzghijkldefabc') 320 | 321 | t.end() 322 | }) 323 | 324 | tape('consuming from multiple buffers', function (t) { 325 | const bl = new BufferListStream() 326 | 327 | bl.append(Buffer.from('abcd')) 328 | bl.append(Buffer.from('efg')) 329 | bl.append(Buffer.from('hi')) 330 | bl.append(Buffer.from('j')) 331 | 332 | t.equal(bl.length, 10) 333 | 334 | t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij') 335 | 336 | bl.consume(3) 337 | t.equal(bl.length, 7) 338 | t.equal(bl.slice(0, 7).toString('ascii'), 'defghij') 339 | 340 | bl.consume(2) 341 | t.equal(bl.length, 5) 342 | t.equal(bl.slice(0, 5).toString('ascii'), 'fghij') 343 | 344 | bl.consume(1) 345 | t.equal(bl.length, 4) 346 | t.equal(bl.slice(0, 4).toString('ascii'), 'ghij') 347 | 348 | bl.consume(1) 349 | t.equal(bl.length, 3) 350 | t.equal(bl.slice(0, 3).toString('ascii'), 'hij') 351 | 352 | bl.consume(2) 353 | t.equal(bl.length, 1) 354 | t.equal(bl.slice(0, 1).toString('ascii'), 'j') 355 | 356 | t.end() 357 | }) 358 | 359 | tape('complete consumption', function (t) { 360 | /** @type {BufferListStreamWithPrivate} */ 361 | const bl = new BufferListStream() 362 | 363 | bl.append(Buffer.from('a')) 364 | bl.append(Buffer.from('b')) 365 | 366 | bl.consume(2) 367 | 368 | t.equal(bl.length, 0) 369 | t.equal(bl._bufs.length, 0) 370 | 371 | t.end() 372 | }) 373 | 374 | tape('test readUInt8 / readInt8', function (t) { 375 | const buf1 = Buffer.alloc(1) 376 | const buf2 = Buffer.alloc(3) 377 | const buf3 = Buffer.alloc(3) 378 | const bl = new BufferListStream() 379 | 380 | buf1[0] = 0x1 381 | buf2[1] = 0x3 382 | buf2[2] = 0x4 383 | buf3[0] = 0x23 384 | buf3[1] = 0x42 385 | 386 | bl.append(buf1) 387 | bl.append(buf2) 388 | bl.append(buf3) 389 | 390 | t.equal(bl.readUInt8(), 0x1) 391 | t.equal(bl.readUInt8(2), 0x3) 392 | t.equal(bl.readInt8(2), 0x3) 393 | t.equal(bl.readUInt8(3), 0x4) 394 | t.equal(bl.readInt8(3), 0x4) 395 | t.equal(bl.readUInt8(4), 0x23) 396 | t.equal(bl.readInt8(4), 0x23) 397 | t.equal(bl.readUInt8(5), 0x42) 398 | t.equal(bl.readInt8(5), 0x42) 399 | 400 | t.end() 401 | }) 402 | 403 | tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) { 404 | const buf1 = Buffer.alloc(1) 405 | const buf2 = Buffer.alloc(3) 406 | const buf3 = Buffer.alloc(3) 407 | const bl = new BufferListStream() 408 | 409 | buf1[0] = 0x1 410 | buf2[1] = 0x3 411 | buf2[2] = 0x4 412 | buf3[0] = 0x23 413 | buf3[1] = 0x42 414 | 415 | bl.append(buf1) 416 | bl.append(buf2) 417 | bl.append(buf3) 418 | 419 | t.equal(bl.readUInt16BE(), 0x0100) 420 | t.equal(bl.readUInt16LE(), 0x0001) 421 | t.equal(bl.readUInt16BE(2), 0x0304) 422 | t.equal(bl.readUInt16LE(2), 0x0403) 423 | t.equal(bl.readInt16BE(2), 0x0304) 424 | t.equal(bl.readInt16LE(2), 0x0403) 425 | t.equal(bl.readUInt16BE(3), 0x0423) 426 | t.equal(bl.readUInt16LE(3), 0x2304) 427 | t.equal(bl.readInt16BE(3), 0x0423) 428 | t.equal(bl.readInt16LE(3), 0x2304) 429 | t.equal(bl.readUInt16BE(4), 0x2342) 430 | t.equal(bl.readUInt16LE(4), 0x4223) 431 | t.equal(bl.readInt16BE(4), 0x2342) 432 | t.equal(bl.readInt16LE(4), 0x4223) 433 | 434 | t.end() 435 | }) 436 | 437 | tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) { 438 | const buf1 = Buffer.alloc(1) 439 | const buf2 = Buffer.alloc(3) 440 | const buf3 = Buffer.alloc(3) 441 | const bl = new BufferListStream() 442 | 443 | buf1[0] = 0x1 444 | buf2[1] = 0x3 445 | buf2[2] = 0x4 446 | buf3[0] = 0x23 447 | buf3[1] = 0x42 448 | 449 | bl.append(buf1) 450 | bl.append(buf2) 451 | bl.append(buf3) 452 | 453 | t.equal(bl.readUInt32BE(), 0x01000304) 454 | t.equal(bl.readUInt32LE(), 0x04030001) 455 | t.equal(bl.readUInt32BE(2), 0x03042342) 456 | t.equal(bl.readUInt32LE(2), 0x42230403) 457 | t.equal(bl.readInt32BE(2), 0x03042342) 458 | t.equal(bl.readInt32LE(2), 0x42230403) 459 | 460 | t.end() 461 | }) 462 | 463 | tape('test readBigUInt64LE / readBigUInt64BE / readBigInt64LE / readBigInt64BE', function (t) { 464 | const buf1 = Buffer.alloc(1) 465 | const buf2 = Buffer.alloc(3) 466 | const buf3 = Buffer.alloc(2) 467 | const buf4 = Buffer.alloc(5) 468 | const bl = new BufferListStream() 469 | 470 | buf1[0] = 0x05 471 | buf2[0] = 0x07 472 | 473 | buf2[1] = 0x03 474 | buf2[2] = 0x04 475 | buf3[0] = 0x23 476 | buf3[1] = 0x42 477 | buf4[0] = 0x00 478 | buf4[1] = 0x01 479 | buf4[2] = 0x02 480 | buf4[3] = 0x03 481 | 482 | buf4[4] = 0x04 483 | 484 | bl.append(buf1) 485 | bl.append(buf2) 486 | bl.append(buf3) 487 | bl.append(buf4) 488 | 489 | t.equal(bl.readBigUInt64BE(2), 0x0304234200010203n) 490 | t.equal(bl.readBigUInt64LE(2), 0x0302010042230403n) 491 | t.equal(bl.readBigInt64BE(2), 0x0304234200010203n) 492 | t.equal(bl.readBigInt64LE(2), 0x0302010042230403n) 493 | 494 | t.end() 495 | }) 496 | 497 | tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) { 498 | const buf1 = Buffer.alloc(1) 499 | const buf2 = Buffer.alloc(3) 500 | const buf3 = Buffer.alloc(3) 501 | const bl = new BufferListStream() 502 | 503 | buf2[0] = 0x2 504 | buf2[1] = 0x3 505 | buf2[2] = 0x4 506 | buf3[0] = 0x23 507 | buf3[1] = 0x42 508 | buf3[2] = 0x61 509 | 510 | bl.append(buf1) 511 | bl.append(buf2) 512 | bl.append(buf3) 513 | 514 | t.equal(bl.readUIntBE(1, 1), 0x02) 515 | t.equal(bl.readUIntBE(1, 2), 0x0203) 516 | t.equal(bl.readUIntBE(1, 3), 0x020304) 517 | t.equal(bl.readUIntBE(1, 4), 0x02030423) 518 | t.equal(bl.readUIntBE(1, 5), 0x0203042342) 519 | t.equal(bl.readUIntBE(1, 6), 0x020304234261) 520 | t.equal(bl.readUIntLE(1, 1), 0x02) 521 | t.equal(bl.readUIntLE(1, 2), 0x0302) 522 | t.equal(bl.readUIntLE(1, 3), 0x040302) 523 | t.equal(bl.readUIntLE(1, 4), 0x23040302) 524 | t.equal(bl.readUIntLE(1, 5), 0x4223040302) 525 | t.equal(bl.readUIntLE(1, 6), 0x614223040302) 526 | t.equal(bl.readIntBE(1, 1), 0x02) 527 | t.equal(bl.readIntBE(1, 2), 0x0203) 528 | t.equal(bl.readIntBE(1, 3), 0x020304) 529 | t.equal(bl.readIntBE(1, 4), 0x02030423) 530 | t.equal(bl.readIntBE(1, 5), 0x0203042342) 531 | t.equal(bl.readIntBE(1, 6), 0x020304234261) 532 | t.equal(bl.readIntLE(1, 1), 0x02) 533 | t.equal(bl.readIntLE(1, 2), 0x0302) 534 | t.equal(bl.readIntLE(1, 3), 0x040302) 535 | t.equal(bl.readIntLE(1, 4), 0x23040302) 536 | t.equal(bl.readIntLE(1, 5), 0x4223040302) 537 | t.equal(bl.readIntLE(1, 6), 0x614223040302) 538 | 539 | t.end() 540 | }) 541 | 542 | tape('test readFloatLE / readFloatBE', function (t) { 543 | const buf1 = Buffer.alloc(1) 544 | const buf2 = Buffer.alloc(3) 545 | const buf3 = Buffer.alloc(3) 546 | const bl = new BufferListStream() 547 | 548 | buf1[0] = 0x01 549 | buf2[1] = 0x00 550 | buf2[2] = 0x00 551 | buf3[0] = 0x80 552 | buf3[1] = 0x3f 553 | 554 | bl.append(buf1) 555 | bl.append(buf2) 556 | bl.append(buf3) 557 | 558 | const canonical = Buffer.concat([buf1, buf2, buf3]) 559 | t.equal(bl.readFloatLE(), canonical.readFloatLE()) 560 | t.equal(bl.readFloatBE(), canonical.readFloatBE()) 561 | t.equal(bl.readFloatLE(2), canonical.readFloatLE(2)) 562 | t.equal(bl.readFloatBE(2), canonical.readFloatBE(2)) 563 | 564 | t.end() 565 | }) 566 | 567 | tape('test readDoubleLE / readDoubleBE', function (t) { 568 | const buf1 = Buffer.alloc(1) 569 | const buf2 = Buffer.alloc(3) 570 | const buf3 = Buffer.alloc(10) 571 | const bl = new BufferListStream() 572 | 573 | buf1[0] = 0x01 574 | buf2[1] = 0x55 575 | buf2[2] = 0x55 576 | buf3[0] = 0x55 577 | buf3[1] = 0x55 578 | buf3[2] = 0x55 579 | buf3[3] = 0x55 580 | buf3[4] = 0xd5 581 | buf3[5] = 0x3f 582 | 583 | bl.append(buf1) 584 | bl.append(buf2) 585 | bl.append(buf3) 586 | 587 | const canonical = Buffer.concat([buf1, buf2, buf3]) 588 | t.equal(bl.readDoubleBE(), canonical.readDoubleBE()) 589 | t.equal(bl.readDoubleLE(), canonical.readDoubleLE()) 590 | t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2)) 591 | t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2)) 592 | 593 | t.end() 594 | }) 595 | 596 | tape('test toString', function (t) { 597 | const bl = new BufferListStream() 598 | 599 | bl.append(Buffer.from('abcd')) 600 | bl.append(Buffer.from('efg')) 601 | bl.append(Buffer.from('hi')) 602 | bl.append(Buffer.from('j')) 603 | 604 | t.equal(bl.toString('ascii', 0, 10), 'abcdefghij') 605 | t.equal(bl.toString('ascii', 3, 10), 'defghij') 606 | t.equal(bl.toString('ascii', 3, 6), 'def') 607 | t.equal(bl.toString('ascii', 3, 8), 'defgh') 608 | t.equal(bl.toString('ascii', 5, 10), 'fghij') 609 | 610 | t.end() 611 | }) 612 | 613 | tape('test toString encoding', function (t) { 614 | const bl = new BufferListStream() 615 | const b = Buffer.from('abcdefghij\xff\x00') 616 | 617 | bl.append(Buffer.from('abcd')) 618 | bl.append(Buffer.from('efg')) 619 | bl.append(Buffer.from('hi')) 620 | bl.append(Buffer.from('j')) 621 | bl.append(Buffer.from('\xff\x00')) 622 | 623 | encodings.forEach(function (enc) { 624 | t.equal(bl.toString(enc), b.toString(enc), enc) 625 | }) 626 | 627 | t.end() 628 | }) 629 | 630 | tape('getBuffers', function (t) { 631 | const bl = new BufferListStream([Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]) 632 | 633 | t.deepEquals( 634 | bl.getBuffers(), 635 | // @ts-ignore 636 | bl._bufs 637 | ) 638 | 639 | t.end() 640 | }) 641 | 642 | tape('uninitialized memory', function (t) { 643 | const secret = crypto.randomBytes(256) 644 | for (let i = 0; i < 1e6; i++) { 645 | const clone = Buffer.from(secret) 646 | const bl = new BufferListStream() 647 | bl.append(Buffer.from('a')) 648 | bl.consume(-1024) 649 | const buf = bl.slice(1) 650 | if (buf.indexOf(clone) !== -1) { 651 | t.fail(`Match (at ${i})`) 652 | break 653 | } 654 | } 655 | t.end() 656 | }) 657 | 658 | !process.browser && tape('test stream', function (t) { 659 | const random = crypto.randomBytes(65534) 660 | 661 | const bl = new BufferListStream((err, buf) => { 662 | t.ok(Buffer.isBuffer(buf)) 663 | t.ok(err === null) 664 | t.ok(random.equals(bl.slice())) 665 | t.ok(random.equals(buf.slice())) 666 | 667 | bl.pipe(fs.createWriteStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat'))) 668 | .on('close', function () { 669 | const rndhash = crypto.createHash('md5').update(random).digest('hex') 670 | const md5sum = crypto.createHash('md5') 671 | const s = fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat')) 672 | 673 | s.on('data', md5sum.update.bind(md5sum)) 674 | s.on('end', function () { 675 | t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!') 676 | t.end() 677 | }) 678 | }) 679 | }) 680 | 681 | fs.writeFileSync(path.join(os.tmpdir(), 'bl_test_rnd.dat'), random) 682 | fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd.dat')).pipe(bl) 683 | }) 684 | 685 | tape('instantiation with Buffer', function (t) { 686 | const buf = crypto.randomBytes(1024) 687 | const buf2 = crypto.randomBytes(1024) 688 | let b = BufferListStream(buf) 689 | 690 | t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer') 691 | b = BufferListStream([buf, buf2]) 692 | t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer') 693 | 694 | t.end() 695 | }) 696 | 697 | tape('test String appendage', function (t) { 698 | const bl = new BufferListStream() 699 | const b = Buffer.from('abcdefghij\xff\x00') 700 | 701 | bl.append('abcd') 702 | bl.append('efg') 703 | bl.append('hi') 704 | bl.append('j') 705 | bl.append('\xff\x00') 706 | 707 | encodings.forEach(function (enc) { 708 | t.equal(bl.toString(enc), b.toString(enc)) 709 | }) 710 | 711 | t.end() 712 | }) 713 | 714 | tape('test Number appendage', function (t) { 715 | const bl = new BufferListStream() 716 | const b = Buffer.from('1234567890') 717 | 718 | bl.append(1234) 719 | bl.append(567) 720 | bl.append(89) 721 | bl.append(0) 722 | 723 | encodings.forEach(function (enc) { 724 | t.equal(bl.toString(enc), b.toString(enc)) 725 | }) 726 | 727 | t.end() 728 | }) 729 | 730 | tape('write nothing, should get empty buffer', function (t) { 731 | t.plan(3) 732 | BufferListStream(function (err, data) { 733 | t.notOk(err, 'no error') 734 | t.ok(Buffer.isBuffer(data), 'got a buffer') 735 | t.equal(0, data.length, 'got a zero-length buffer') 736 | t.end() 737 | }).end() 738 | }) 739 | 740 | tape('unicode string', function (t) { 741 | t.plan(2) 742 | 743 | const inp1 = '\u2600' 744 | const inp2 = '\u2603' 745 | const exp = inp1 + ' and ' + inp2 746 | const bl = BufferListStream() 747 | 748 | bl.write(inp1) 749 | bl.write(' and ') 750 | bl.write(inp2) 751 | t.equal(exp, bl.toString()) 752 | t.equal(Buffer.from(exp).toString('hex'), bl.toString('hex')) 753 | }) 754 | 755 | tape('should emit finish', function (t) { 756 | const source = BufferListStream() 757 | const dest = BufferListStream() 758 | 759 | source.write('hello') 760 | source.pipe(dest) 761 | 762 | dest.on('finish', function () { 763 | t.equal(dest.toString('utf8'), 'hello') 764 | t.end() 765 | }) 766 | }) 767 | 768 | tape('basic copy', function (t) { 769 | const buf = crypto.randomBytes(1024) 770 | const buf2 = Buffer.alloc(1024) 771 | const b = BufferListStream(buf) 772 | 773 | b.copy(buf2) 774 | t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer') 775 | 776 | t.end() 777 | }) 778 | 779 | tape('copy after many appends', function (t) { 780 | const buf = crypto.randomBytes(512) 781 | const buf2 = Buffer.alloc(1024) 782 | const b = BufferListStream(buf) 783 | 784 | b.append(buf) 785 | b.copy(buf2) 786 | t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer') 787 | 788 | t.end() 789 | }) 790 | 791 | tape('copy at a precise position', function (t) { 792 | const buf = crypto.randomBytes(1004) 793 | const buf2 = Buffer.alloc(1024) 794 | const b = BufferListStream(buf) 795 | 796 | b.copy(buf2, 20) 797 | t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer') 798 | 799 | t.end() 800 | }) 801 | 802 | tape('copy starting from a precise location', function (t) { 803 | const buf = crypto.randomBytes(10) 804 | const buf2 = Buffer.alloc(5) 805 | const b = BufferListStream(buf) 806 | 807 | b.copy(buf2, 0, 5) 808 | t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer') 809 | 810 | t.end() 811 | }) 812 | 813 | tape('copy in an interval', function (t) { 814 | const rnd = crypto.randomBytes(10) 815 | const b = BufferListStream(rnd) // put the random bytes there 816 | const actual = Buffer.alloc(3) 817 | const expected = Buffer.alloc(3) 818 | 819 | rnd.copy(expected, 0, 5, 8) 820 | b.copy(actual, 0, 5, 8) 821 | 822 | t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer') 823 | 824 | t.end() 825 | }) 826 | 827 | tape('copy an interval between two buffers', function (t) { 828 | const buf = crypto.randomBytes(10) 829 | const buf2 = Buffer.alloc(10) 830 | const b = BufferListStream(buf) 831 | 832 | b.append(buf) 833 | b.copy(buf2, 0, 5, 15) 834 | 835 | t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer') 836 | 837 | t.end() 838 | }) 839 | 840 | tape('shallow slice across buffer boundaries', function (t) { 841 | const bl = new BufferListStream(['First', 'Second', 'Third']) 842 | 843 | t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh') 844 | 845 | t.end() 846 | }) 847 | 848 | tape('shallow slice within single buffer', function (t) { 849 | t.plan(2) 850 | 851 | const bl = new BufferListStream(['First', 'Second', 'Third']) 852 | 853 | t.equal(bl.shallowSlice(5, 10).toString(), 'Secon') 854 | t.equal(bl.shallowSlice(7, 10).toString(), 'con') 855 | 856 | t.end() 857 | }) 858 | 859 | tape('shallow slice single buffer', function (t) { 860 | t.plan(3) 861 | 862 | const bl = new BufferListStream(['First', 'Second', 'Third']) 863 | 864 | t.equal(bl.shallowSlice(0, 5).toString(), 'First') 865 | t.equal(bl.shallowSlice(5, 11).toString(), 'Second') 866 | t.equal(bl.shallowSlice(11, 16).toString(), 'Third') 867 | }) 868 | 869 | tape('shallow slice with negative or omitted indices', function (t) { 870 | t.plan(4) 871 | 872 | const bl = new BufferListStream(['First', 'Second', 'Third']) 873 | 874 | t.equal(bl.shallowSlice().toString(), 'FirstSecondThird') 875 | t.equal(bl.shallowSlice(5).toString(), 'SecondThird') 876 | t.equal(bl.shallowSlice(5, -3).toString(), 'SecondTh') 877 | t.equal(bl.shallowSlice(-8).toString(), 'ondThird') 878 | }) 879 | 880 | tape('shallow slice does not make a copy', function (t) { 881 | t.plan(1) 882 | 883 | const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')] 884 | const bl = new BufferListStream(buffers).shallowSlice(5, -3) 885 | 886 | buffers[1].fill('h') 887 | buffers[2].fill('h') 888 | 889 | t.equal(bl.toString(), 'hhhhhhhh') 890 | }) 891 | 892 | tape('shallow slice with 0 length', function (t) { 893 | t.plan(1) 894 | 895 | const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')] 896 | const bl = (new BufferListStream(buffers)).shallowSlice(0, 0) 897 | 898 | t.equal(bl.length, 0) 899 | }) 900 | 901 | tape('shallow slice with 0 length from middle', function (t) { 902 | t.plan(1) 903 | 904 | const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')] 905 | const bl = (new BufferListStream(buffers)).shallowSlice(10, 10) 906 | 907 | t.equal(bl.length, 0) 908 | }) 909 | 910 | tape('duplicate', function (t) { 911 | t.plan(2) 912 | 913 | const bl = new BufferListStream('abcdefghij\xff\x00') 914 | const dup = bl.duplicate() 915 | 916 | t.equal(bl.prototype, dup.prototype) 917 | t.equal(bl.toString('hex'), dup.toString('hex')) 918 | }) 919 | 920 | tape('destroy no pipe', function (t) { 921 | t.plan(2) 922 | 923 | /** @type {BufferListStreamWithPrivate} */ 924 | const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk') 925 | 926 | bl.destroy() 927 | 928 | t.equal(bl._bufs.length, 0) 929 | t.equal(bl.length, 0) 930 | }) 931 | 932 | tape('destroy with error', function (t) { 933 | t.plan(3) 934 | 935 | /** @type {BufferListStreamWithPrivate} */ 936 | const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk') 937 | const err = new Error('kaboom') 938 | 939 | bl.destroy(err) 940 | bl.on('error', function (_err) { 941 | t.equal(_err, err) 942 | }) 943 | 944 | t.equal(bl._bufs.length, 0) 945 | t.equal(bl.length, 0) 946 | }) 947 | 948 | !process.browser && tape('destroy with pipe before read end', function (t) { 949 | t.plan(2) 950 | 951 | /** @type {BufferListStreamWithPrivate} */ 952 | const bl = new BufferListStream() 953 | fs.createReadStream(path.join(__dirname, '/test.js')) 954 | .pipe(bl) 955 | 956 | bl.destroy() 957 | 958 | t.equal(bl._bufs.length, 0) 959 | t.equal(bl.length, 0) 960 | }) 961 | 962 | !process.browser && tape('destroy with pipe before read end with race', function (t) { 963 | t.plan(2) 964 | 965 | /** @type {BufferListStreamWithPrivate} */ 966 | const bl = new BufferListStream() 967 | 968 | fs.createReadStream(path.join(__dirname, '/test.js')) 969 | .pipe(bl) 970 | 971 | setTimeout(function () { 972 | bl.destroy() 973 | setTimeout(function () { 974 | t.equal(bl._bufs.length, 0) 975 | t.equal(bl.length, 0) 976 | }, 500) 977 | }, 500) 978 | }) 979 | 980 | !process.browser && tape('destroy with pipe after read end', function (t) { 981 | t.plan(2) 982 | 983 | /** @type {BufferListStreamWithPrivate} */ 984 | const bl = new BufferListStream() 985 | fs.createReadStream(path.join(__dirname, '/test.js')) 986 | .on('end', onEnd) 987 | .pipe(bl) 988 | 989 | function onEnd () { 990 | bl.destroy() 991 | 992 | t.equal(bl._bufs.length, 0) 993 | t.equal(bl.length, 0) 994 | } 995 | }) 996 | 997 | !process.browser && tape('destroy with pipe while writing to a destination', function (t) { 998 | t.plan(4) 999 | 1000 | /** @type {BufferListStreamWithPrivate} */ 1001 | const bl = new BufferListStream() 1002 | const ds = new BufferListStream() 1003 | 1004 | fs.createReadStream(path.join(__dirname, '/test.js')) 1005 | .on('end', onEnd) 1006 | .pipe(bl) 1007 | 1008 | function onEnd () { 1009 | bl.pipe(ds) 1010 | 1011 | setTimeout(function () { 1012 | bl.destroy() 1013 | 1014 | t.equals(bl._bufs.length, 0) 1015 | t.equals(bl.length, 0) 1016 | 1017 | ds.destroy() 1018 | 1019 | t.equals(bl._bufs.length, 0) 1020 | t.equals(bl.length, 0) 1021 | }, 100) 1022 | } 1023 | }) 1024 | 1025 | !process.browser && tape('handle error', function (t) { 1026 | t.plan(2) 1027 | 1028 | fs.createReadStream('/does/not/exist').pipe(BufferListStream(function (err, data) { 1029 | t.ok(err instanceof Error, 'has error') 1030 | t.notOk(data, 'no data') 1031 | })) 1032 | }) 1033 | --------------------------------------------------------------------------------