package/package.json000644 0000006224 3560116604 011552 0ustar00000000 000000 { "name": "bufferstreams", "version": "3.0.0", "description": "Abstract streams to deal with the whole buffered contents.", "homepage": "https://github.com/nfroidure/BufferStreams", "main": "src/index.js", "files": [ "src/**/*.js", "LICENSE", "README.md", "CHANGELOG.md" ], "metapak": { "configs": [ "main", "readme", "eslint", "mocha", "codeclimate", "travis" ], "data": { "files": "src/*.js tests/*.mocha.js", "testsFiles": "tests/*.mocha.js" } }, "scripts": { "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md", "cli": "env NODE_ENV=${NODE_ENV:-cli}", "cover": "nyc npm test && nyc report --reporter=html --reporter=text", "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls && rm -rf ./coverage", "cz": "env NODE_ENV=${NODE_ENV:-cli} git cz", "lint": "eslint src/*.js tests/*.mocha.js", "metapak": "metapak", "mocha": "mocha tests/*.mocha.js", "precz": "npm t && npm run lint && npm run metapak -- -s", "prettier": "prettier --write src/*.js tests/*.mocha.js", "preversion": "npm t && npm run lint && npm run metapak -s && npm run metapak -- -s", "test": "npm run mocha", "version": "npm run changelog" }, "repository": { "type": "git", "url": "git://github.com/nfroidure/BufferStreams" }, "keywords": [ "buffer", "streaming", "stream", "async", "abstract" ], "dependencies": { "readable-stream": "^3.4.0" }, "devDependencies": { "commitizen": "^3.1.1", "conventional-changelog-cli": "^2.0.23", "coveralls": "^3.0.5", "cz-conventional-changelog": "^3.0.2", "eslint": "^6.1.0", "eslint-plugin-prettier": "^3.1.0", "metapak": "^3.1.6", "metapak-nfroidure": "^9.6.4", "mocha": "^6.2.0", "nyc": "^14.1.1", "prettier": "^1.18.2", "streamtest": "^1.2.4" }, "author": { "name": "Nicolas Froidure", "email": "nicolas.froidure@insertafter.com", "url": "http://insertafter.com/en/index.html" }, "engines": { "node": ">=8.12.0" }, "license": "MIT", "bugs": { "url": "https://github.com/nfroidure/BufferStreams/issues" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }, "greenkeeper": { "ignore": [ "commitizen", "cz-conventional-changelog", "conventional-changelog-cli", "eslint", "eslint-config-prettier", "prettier", "mocha", "coveralls", "nyc" ] }, "eslintConfig": { "extends": [ "eslint:recommended" ], "parserOptions": { "ecmaVersion": 2018, "sourceType": "script", "modules": true }, "env": { "es6": true, "node": true, "jest": true, "mocha": true }, "plugins": [ "prettier" ], "rules": { "prettier/prettier": "error" } }, "prettier": { "semi": true, "printWidth": 80, "singleQuote": true, "trailingComma": "es5", "proseWrap": "always" }, "nyc": { "exclude": [ "tests/*.mocha.js" ] }, "contributors": [] } package/CHANGELOG.md000644 0000015511 3560116604 011074 0ustar00000000 000000 # [3.0.0](https://github.com/nfroidure/BufferStreams/compare/v2.0.1...v3.0.0) (2019-11-15) ## [2.0.1](https://github.com/nfroidure/BufferStreams/compare/v2.0.0...v2.0.1) (2018-04-23) ### Bug Fixes * Remove debug dependency ([2719573](https://github.com/nfroidure/BufferStreams/commit/2719573)) # [2.0.0](https://github.com/nfroidure/BufferStreams/compare/v1.1.1...v2.0.0) (2017-12-06) ### build * **metapak-nfroidure:** Add metapak-nfroidure ([ed104f6](https://github.com/nfroidure/BufferStreams/commit/ed104f6)) ### BREAKING CHANGES * **metapak-nfroidure:** Removing support for versions minors to the last LTS ## Change Log ### upcoming (2017/03/05 07:36 +00:00) - [f1bea6b](https://github.com/nfroidure/bufferstreams/commit/f1bea6bc9ad1c013457b9b9137f6dae70e9cab6b) Add Node 4-6-7 (@nfroidure) ### v1.1.1 (2016/06/28 11:20 +00:00) - [acd1875](https://github.com/nfroidure/bufferstreams/commit/acd18756a3066c72b4cff8aa79aa44a3dcb4f85f) 1.1.1 (@shinnn) - [4fc64b9](https://github.com/nfroidure/bufferstreams/commit/4fc64b9be16693e59920b976d4822adf52ec6f3d) Use wider badges to fit modern Github design (@shinnn) - [2b28391](https://github.com/nfroidure/bufferstreams/commit/2b2839191eec654af9361fc654d430f69bc6d622) Fix invalid license statement in package.json (@shinnn) - [e330151](https://github.com/nfroidure/bufferstreams/commit/e330151fd994b90d4383e2e189f18f628c3c1157) Fix file mode (@shinnn) - [#4](https://github.com/nfroidure/bufferstreams/pull/4) Use files field instead of .npmignore (@shinnn) - [d7435e5](https://github.com/nfroidure/bufferstreams/commit/d7435e5729b4684492e746d015bbfee39ac564dc) Use files field instead of .npmignore (@shinnn) ### v1.1.0 (2015/08/04 09:48 +00:00) - [8b3f905](https://github.com/nfroidure/bufferstreams/commit/8b3f9058764b4ff8095df90eea55ea7683d3ee33) 1.1.0 (@nfroidure) - [e7a4854](https://github.com/nfroidure/bufferstreams/commit/e7a4854d032682593851e20c20ac0b63587594d7) Better doc (@nfroidure) - [0567b10](https://github.com/nfroidure/bufferstreams/commit/0567b10f2a8cc6ddb1eada72a9178533b1fa1dd2) Also build on Node 0.12 (@nfroidure) - [92cd9de](https://github.com/nfroidure/bufferstreams/commit/92cd9de1752cbb70d403c4fbbee0532e762bf3e1) Dependencies update (@nfroidure) - [5bc3649](https://github.com/nfroidure/bufferstreams/commit/5bc3649e1c8f58505cad3393cbf67c47581dac13) Better handling of errors (@nfroidure) - [1ca6222](https://github.com/nfroidure/bufferstreams/commit/1ca6222b488072004305156c94a388c100df6fc7) Code cleanup and backward compatibility (@nfroidure) - [999b805](https://github.com/nfroidure/bufferstreams/commit/999b805be2007b5a1100f9cdbf0f911aa667afab) Using Duplex stream instead of Transform one (@nfroidure) - [77051e3](https://github.com/nfroidure/bufferstreams/commit/77051e330e82931acf3fb4c4b0b4f24aa4ae13de) Add tests for callback error emitting (@nfroidure) - [6475b82](https://github.com/nfroidure/bufferstreams/commit/6475b820cbc5a80f4e98bea86cdda3acc6ec7743) Adding linter and suppress warnings (@nfroidure) - [8d24a90](https://github.com/nfroidure/bufferstreams/commit/8d24a90bf5b91c1185f5c5f00cc32a4b65fc9fc5) Improving the README file (@nfroidure) - [d5ab44e](https://github.com/nfroidure/bufferstreams/commit/d5ab44e5ddeaddd73744158d046eb71b2fb29fb6) Add stats (@nfroidure) ### v1.0.2 (2015/06/21 07:11 +00:00) - [8aaf7a5](https://github.com/nfroidure/bufferstreams/commit/8aaf7a576177d3c2c1e626d8bfba091d47d7cea3) 1.0.2 (@nfroidure) - [fe52a06](https://github.com/nfroidure/bufferstreams/commit/fe52a06f252b161676f722624e74e9290b845320) Fix trinity script (@nfroidure) - [d512161](https://github.com/nfroidure/bufferstreams/commit/d5121614d055a63fca0072ebfc360fc17f8e04aa) Dependencies update (@nfroidure) - [48687ed](https://github.com/nfroidure/bufferstreams/commit/48687ed86eea5262693bb9b11097003bc0985b19) Merge branch 'master' of github.com:nfroidure/bufferstreams (@nfroidure) - [#3](https://github.com/nfroidure/bufferstreams/pull/3) Use SVG-based badges (@shinnn) - [88eccc5](https://github.com/nfroidure/bufferstreams/commit/88eccc50275e9317cba820a72f69f611c69cf3ab) Use SVG-based badges (@shinnn) - [a2875b3](https://github.com/nfroidure/bufferstreams/commit/a2875b323ec90b0d4d7afb60186a07c9f60f9350) Simplify test scripts (@shinnn) ### v1.0.1 (2015/02/09 17:57 +00:00) - [7d4c975](https://github.com/nfroidure/bufferstreams/commit/7d4c975accd17ea382845d93e11761ad8c364534) 1.0.1 (@nfroidure) - [6027553](https://github.com/nfroidure/bufferstreams/commit/602755373fd77d9ca34248b2ba106bcd57e49dce) Update deps (@nfroidure) ### v1.0.0 (2015/02/09 17:49 +00:00) - [438812d](https://github.com/nfroidure/bufferstreams/commit/438812dc9e6ecfbc7bd184503a341b0627aa9bf6) 1.0.0 (@nfroidure) - [01859b0](https://github.com/nfroidure/bufferstreams/commit/01859b0b32a8bb630d97aec3e01dac9148fd7b17) Documenting objectMode (@nfroidure) - [d3cac8d](https://github.com/nfroidure/bufferstreams/commit/d3cac8dab9a8d6f8275e82eedfafe929b156c258) Fixing tests (@nfroidure) - [d7ae39a](https://github.com/nfroidure/bufferstreams/commit/d7ae39a4bab7f77a93cde5993f8da703e21db0f0) Adding code climate metrics badge (@nfroidure) - [677f394](https://github.com/nfroidure/bufferstreams/commit/677f394d68a49afd6a24865a111c061985f8739f) Allowing objectMode, adding a better test lib and 100% coveraging (@nfroidure) - [1bf9b40](https://github.com/nfroidure/bufferstreams/commit/1bf9b404b47c801387867b9a3614041c5285cfe2) New version 0.0.2 (@nfroidure) - [17723e2](https://github.com/nfroidure/bufferstreams/commit/17723e212599d3a6dd582980b98331c0651a93ea) Added readable-stream closes#1 (@nfroidure) - [51545c3](https://github.com/nfroidure/bufferstreams/commit/51545c3e26b26315c17b17bb9a09986b39a538bf) Accept forgiving new (@nfroidure) - [8d87f57](https://github.com/nfroidure/bufferstreams/commit/8d87f576047d65b2a86680df4d4a6469202d96e1) Dependencies update (@nfroidure) - [3a1408f](https://github.com/nfroidure/bufferstreams/commit/3a1408fd7886a0dd5f1edf357d4c2373521ae032) Added deps (@nfroidure) - [38f0690](https://github.com/nfroidure/bufferstreams/commit/38f06902efe745a9cbf648fe6634a2c78c18a544) Added coverage tests (@nfroidure) - [2a631d6](https://github.com/nfroidure/bufferstreams/commit/2a631d66a41547e32f79dcba2ffe164d3f890bf9) Typo fix (@nfroidure) - [7ce57c9](https://github.com/nfroidure/bufferstreams/commit/7ce57c96aa95a24038442026b318f98ee5e9318e) Fixing npm badge (@nfroidure) - [c6d7b48](https://github.com/nfroidure/bufferstreams/commit/c6d7b486bc537b57997dd329966d53a216f30bff) Adding npm badge (@nfroidure) - [bfbca40](https://github.com/nfroidure/bufferstreams/commit/bfbca40112d3341989961a7ddbed838d2620b8e2) Adding instructions, fixing naming (@nfroidure) - [1a531c7](https://github.com/nfroidure/bufferstreams/commit/1a531c78b79642c76bcdd09aa0262342833e6203) Readme updated (@nfroidure) - [b0e1be8](https://github.com/nfroidure/bufferstreams/commit/b0e1be875aa9583ed44b2f1e9c74d050ef777ebb) First commit (@nfroidure)package/LICENSE000644 0000002101 3560116604 010257 0ustar00000000 000000 The MIT License (MIT) Copyright © 2017 Nicolas Froidure 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. package/README.md000644 0000007513 3560116604 010545 0ustar00000000 000000 [//]: # ( ) [//]: # (This file is automatically generated by a `metapak`) [//]: # (module. Do not change it except between the) [//]: # (`content:start/end` flags, your changes would) [//]: # (be overridden.) [//]: # ( ) # bufferstreams > Abstract streams to deal with the whole buffered contents. [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/bufferstreams/blob/master/LICENSE) [![Build status](https://secure.travis-ci.org/nfroidure/bufferstreams.svg)](https://travis-ci.org/nfroidure/bufferstreams) [![Coverage Status](https://coveralls.io/repos/nfroidure/bufferstreams/badge.svg?branch=master)](https://coveralls.io/r/nfroidure/bufferstreams?branch=master) [![NPM version](https://badge.fury.io/js/bufferstreams.svg)](https://npmjs.org/package/bufferstreams) [![Dependency Status](https://david-dm.org/nfroidure/bufferstreams.svg)](https://david-dm.org/nfroidure/bufferstreams) [![devDependency Status](https://david-dm.org/nfroidure/bufferstreams/dev-status.svg)](https://david-dm.org/nfroidure/bufferstreams#info=devDependencies) [![Package Quality](http://npm.packagequality.com/shield/bufferstreams.svg)](http://packagequality.com/#?package=bufferstreams) [![Code Climate](https://codeclimate.com/github/nfroidure/bufferstreams.svg)](https://codeclimate.com/github/nfroidure/bufferstreams) [//]: # (::contents:start) `bufferstreams` abstracts streams to allow you to deal with their whole content in a single buffer when it becomes necessary (by example: a legacy library that do not support streams). It is not a good practice, just some glue. Using `bufferstreams` means: * there is no library dealing with streams for your needs * you filled an issue to the wrapped library to support streams `bufferstreams` can also be used to control the whole stream content in a single point of a streaming pipeline for testing purposes. ## Usage Install the [npm module](https://npmjs.org/package/bufferstreams): ```sh npm install bufferstreams --save ``` Then, in your scripts: ```js var fs = require('fs'); var bufferstreams = require('bufferstreams'); fs.createReadStream('input.txt') .pipe(new bufferstreams(function(err, buf, cb) { // err will be filled with an error if the piped in stream emits one. if(err) { throw err; } // buf will contain the whole piped in stream contents buf = Buffer(buf.toString('utf-8').replace('foo', 'bar')); // cb is a callback to pass the result back to the piped out stream // first argument is an error that will be emitted if any // the second argument is the modified buffer cb(null, buf); })) .pipe(fs.createWriteStream('output.txt')); ``` Note that you can use `bufferstreams` with the objectMode option. In this case, the given buffer will be an array containing the streamed objects: ```js new BufferStreams({objectMode: true}, myCallback); ``` ## API ### Stream : BufferStreams([options], callback) #### options ##### options.objectMode Type: `Boolean` Default value: `false` Use if piped in streams are in object mode. In this case, an array of the buffered will be transmitted to the `callback` function. ##### options.* `bufferstreams` inherits of Stream.Duplex, the options are passed to the parent constructor so you can use it's options too. ##### callback(err, buf, cb) Type: `Function`, required. A function to handle the buffered content. ## Stats [![NPM](https://nodei.co/npm/bufferstreams.png?downloads=true&stars=true)](https://nodei.co/npm/bufferstreams/) [![NPM](https://nodei.co/npm-dl/bufferstreams.png)](https://nodei.co/npm/bufferstreams/) ## Contributing Feel free to pull your code if you agree with publishing it under the MIT license. [//]: # (::contents:end) # Authors - [Nicolas Froidure](http://insertafter.com/en/index.html) # License [MIT](https://github.com/nfroidure/bufferstreams/blob/master/LICENSE) package/src/index.js000644 0000004216 3560116604 011517 0ustar00000000 000000 'use strict'; const Duplex = require('readable-stream').Duplex; const util = require('util'); // Inherit of Duplex stream util.inherits(BufferStream, Duplex); // Constructor function BufferStream(options, cb) { const _this = this; // Ensure new were used if (!(_this instanceof BufferStream)) { return new BufferStream(options, cb); } // Cast args if (options instanceof Function) { cb = options; options = {}; } options = options || {}; if (!(cb instanceof Function)) { throw new Error('The given callback must be a function.'); } _this.__objectMode = options.objectMode; // Parent constructor Duplex.call(_this, options); // Keep a reference to the callback _this._cb = cb; // Add a finished flag _this._bufferStreamFinished = false; // Internal buffer _this._bufferStreamBuffer = []; // Internal logic function _bufferStreamCallbackWrapper(err) { const buffer = options.objectMode ? _this._bufferStreamBuffer : Buffer.concat(_this._bufferStreamBuffer); err = err || null; _this._cb(err, buffer, (err2, buf) => { setImmediate(() => { _this.removeListener('error', _bufferStreamError); if (err2) { _this.emit('error', err2); } _this._bufferStreamBuffer = options.objectMode ? buf || [] : [buf]; _this._bufferStreamFinished = true; _this._read(); }); }); } function _bufferStreamError(err) { if (_this._bufferStreamFinished) { return; } _bufferStreamCallbackWrapper(err); } _this.once('finish', _bufferStreamCallbackWrapper); _this.on('error', _bufferStreamError); } BufferStream.prototype._write = function _bufferStreamWrite( chunk, encoding, done ) { this._bufferStreamBuffer.push(chunk); done(); }; BufferStream.prototype._read = function _bufferStreamRead() { const _this = this; if (_this._bufferStreamFinished) { while (_this._bufferStreamBuffer.length) { if (!_this.push(_this._bufferStreamBuffer.shift())) { break; } } if (0 === _this._bufferStreamBuffer.length) { _this.push(null); } } }; module.exports = BufferStream;