duplexer3
Advanced tools
Comparing version 0.1.5 to 1.0.0
109
index.js
@@ -1,52 +0,48 @@ | ||
"use strict"; | ||
import stream from 'node:stream'; | ||
var stream = require("stream"); | ||
export function DuplexWrapper(options, writable, readable) { | ||
if (typeof readable === 'undefined') { | ||
readable = writable; | ||
writable = options; | ||
options = undefined; | ||
} | ||
function DuplexWrapper(options, writable, readable) { | ||
if (typeof readable === "undefined") { | ||
readable = writable; | ||
writable = options; | ||
options = null; | ||
} | ||
stream.Duplex.call(this, options); | ||
stream.Duplex.call(this, options); | ||
if (typeof readable.read !== 'function') { | ||
readable = (new stream.Readable(options)).wrap(readable); | ||
} | ||
if (typeof readable.read !== "function") { | ||
readable = (new stream.Readable(options)).wrap(readable); | ||
} | ||
this._writable = writable; | ||
this._readable = readable; | ||
this._waiting = false; | ||
this._writable = writable; | ||
this._readable = readable; | ||
this._waiting = false; | ||
writable.once('finish', () => { | ||
this.end(); | ||
}); | ||
var self = this; | ||
this.once('finish', () => { | ||
writable.end(); | ||
}); | ||
writable.once("finish", function() { | ||
self.end(); | ||
}); | ||
readable.on('readable', () => { | ||
if (this._waiting) { | ||
this._waiting = false; | ||
this._read(); | ||
} | ||
}); | ||
this.once("finish", function() { | ||
writable.end(); | ||
}); | ||
readable.once('end', () => { | ||
this.push(null); | ||
}); | ||
readable.on("readable", function() { | ||
if (self._waiting) { | ||
self._waiting = false; | ||
self._read(); | ||
} | ||
}); | ||
if (!options || typeof options.bubbleErrors === 'undefined' || options.bubbleErrors) { | ||
writable.on('error', error => { | ||
this.emit('error', error); | ||
}); | ||
readable.once("end", function() { | ||
self.push(null); | ||
}); | ||
if (!options || typeof options.bubbleErrors === "undefined" || options.bubbleErrors) { | ||
writable.on("error", function(err) { | ||
self.emit("error", err); | ||
}); | ||
readable.on("error", function(err) { | ||
self.emit("error", err); | ||
}); | ||
} | ||
readable.on('error', error => { | ||
this.emit('error', error); | ||
}); | ||
} | ||
} | ||
@@ -56,22 +52,21 @@ | ||
DuplexWrapper.prototype._write = function _write(input, encoding, done) { | ||
this._writable.write(input, encoding, done); | ||
DuplexWrapper.prototype._write = function (input, encoding, done) { | ||
this._writable.write(input, encoding, done); | ||
}; | ||
DuplexWrapper.prototype._read = function _read() { | ||
var buf; | ||
var reads = 0; | ||
while ((buf = this._readable.read()) !== null) { | ||
this.push(buf); | ||
reads++; | ||
} | ||
if (reads === 0) { | ||
this._waiting = true; | ||
} | ||
}; | ||
DuplexWrapper.prototype._read = function () { | ||
let buffer; | ||
let readCount = 0; | ||
while ((buffer = this._readable.read()) !== null) { | ||
this.push(buffer); | ||
readCount++; | ||
} | ||
module.exports = function duplex2(options, writable, readable) { | ||
return new DuplexWrapper(options, writable, readable); | ||
if (readCount === 0) { | ||
this._waiting = true; | ||
} | ||
}; | ||
module.exports.DuplexWrapper = DuplexWrapper; | ||
export default function duplexer(options, writable, readable) { | ||
return new DuplexWrapper(options, writable, readable); | ||
} |
{ | ||
"name": "duplexer3", | ||
"version": "0.1.5", | ||
"description": "Like duplexer but using streams3", | ||
"engine": { | ||
"node": ">=4" | ||
"version": "1.0.0", | ||
"description": "Modern version of `duplexer2`", | ||
"license": "BSD-3-Clause", | ||
"repository": "sindresorhus/duplexer3", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"scripts": { | ||
"test": "xo && mocha" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "mocha -R tap" | ||
}, | ||
"repository": "sindresorhus/duplexer3", | ||
"keywords": [ | ||
@@ -19,10 +23,10 @@ "duplex", | ||
"stream", | ||
"stream3", | ||
"streams", | ||
"join", | ||
"combine" | ||
], | ||
"license": "BSD-3-Clause", | ||
"devDependencies": { | ||
"mocha": "^2.2.5" | ||
"mocha": "^10.0.0", | ||
"xo": "^0.50.0" | ||
} | ||
} |
# duplexer3 | ||
Like [`duplexer2`](https://github.com/deoxxa/duplexer2) but using Streams3 without readable-stream dependency | ||
> Modern version of `duplexer2` | ||
## Install | ||
```sh | ||
npm install duplexer3 | ||
``` | ||
## Usage | ||
```js | ||
var stream = require("stream"); | ||
var duplexer3 = require("duplexer3"); | ||
import stream from 'node:stream'; | ||
import duplexer from 'duplexer3'; | ||
var writable = new stream.Writable({objectMode: true}), | ||
readable = new stream.Readable({objectMode: true}); | ||
const writable = new stream.Writable({objectMode: true}); | ||
const readable = new stream.Readable({objectMode: true}); | ||
writable._write = function _write(input, encoding, done) { | ||
writable._write = function (input, encoding, done) { | ||
if (readable.push(input)) { | ||
return done(); | ||
done(); | ||
} else { | ||
readable.once("drain", done); | ||
readable.once('drain', done); | ||
} | ||
}; | ||
readable._read = function _read(n) { | ||
// no-op | ||
readable._read = function () { | ||
// Noop | ||
}; | ||
// simulate the readable thing closing after a bit | ||
writable.once("finish", function() { | ||
setTimeout(function() { | ||
// Simulate the readable thing closing after a bit | ||
writable.once('finish', () => { | ||
setTimeout(() => { | ||
readable.push(null); | ||
@@ -31,22 +39,22 @@ }, 500); | ||
var duplex = duplexer3(writable, readable); | ||
const duplex = duplexer3(writable, readable); | ||
duplex.on("data", function(e) { | ||
console.log("got data", JSON.stringify(e)); | ||
duplex.on('data', data => { | ||
console.log('got data', JSON.stringify(data)); | ||
}); | ||
duplex.on("finish", function() { | ||
console.log("got finish event"); | ||
duplex.on('finish', () => { | ||
console.log('got finish event'); | ||
}); | ||
duplex.on("end", function() { | ||
console.log("got end event"); | ||
duplex.on('end', () => { | ||
console.log('got end event'); | ||
}); | ||
duplex.write("oh, hi there", function() { | ||
console.log("finished writing"); | ||
duplex.write('oh, hi there', () => { | ||
console.log('finished writing'); | ||
}); | ||
duplex.end(function() { | ||
console.log("finished ending"); | ||
duplex.end(() => { | ||
console.log('finished ending'); | ||
}); | ||
@@ -56,3 +64,3 @@ ``` | ||
``` | ||
got data "oh, hi there" | ||
got data 'oh, hi there' | ||
finished writing | ||
@@ -64,39 +72,15 @@ got finish event | ||
## Overview | ||
This is a reimplementation of [duplexer](https://www.npmjs.com/package/duplexer) using the | ||
Streams3 API which is standard in Node as of v4. Everything largely | ||
works the same. | ||
## Install | ||
```sh | ||
npm install duplexer3 | ||
``` | ||
## API | ||
### duplexer3 | ||
### duplexer(options?, writableStream, readableStream) | ||
Creates a new `DuplexWrapper` object, which is the actual class that implements | ||
most of the fun stuff. All that fun stuff is hidden. DON'T LOOK. | ||
#### options | ||
```js | ||
duplexer3([options], writable, readable) | ||
``` | ||
Type: `object` | ||
```js | ||
const duplex = duplexer3(new stream.Writable(), new stream.Readable()); | ||
``` | ||
##### bubbleErrors | ||
Arguments | ||
Type: `boolean`\ | ||
Default: `true` | ||
* __options__ - an object specifying the regular `stream.Duplex` options, as | ||
well as the properties described below. | ||
* __writable__ - a writable stream | ||
* __readable__ - a readable stream | ||
Options | ||
* __bubbleErrors__ - a boolean value that specifies whether to bubble errors | ||
from the underlying readable/writable streams. Default is `true`. | ||
Whether to bubble errors from the underlying readable/writable streams. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
0
Yes
4917
2
56
84