debugging-stream
Advanced tools
Comparing version 1.0.0 to 2.0.0
const Debugging = require('./') | ||
const l = new Debugging(process.stdin, { latency: { read: [1500, 2500], connect: 5000 } }) | ||
const l = new Debugging(process.stdin, { latency: [1500, 2000] }) | ||
@@ -5,0 +5,0 @@ l.on('data', function (data) { |
28
index.js
const { Duplex } = require('streamx') | ||
module.exports = class DebuggingStream extends Duplex { | ||
constructor (stream, { random = Math.random, latency = {} } = {}) { | ||
constructor (stream, { random = Math.random, latency = 0 } = {}) { | ||
super() | ||
const { read = 0, connect = 0 } = latency | ||
this._random = random | ||
this._latency = toRange(read) | ||
this._latency = toRange(latency) | ||
this._queued = [] | ||
this._ondrain = null | ||
this._connecting = true | ||
const c = toRange(connect) | ||
this._start = 0 | ||
this._connectLatency = c.start + Math.round(this._random() * c.variance) | ||
this.stream = stream | ||
@@ -52,17 +44,7 @@ | ||
_queue (evt) { | ||
let l = this._connectLatency + this._latency.start + Math.round(this._random() * this._latency.variance) | ||
const first = this._connecting | ||
const l = this._latency.start + Math.round(this._random() * this._latency.variance) | ||
if (first) { | ||
this._start = Date.now() | ||
} else { | ||
const delta = Math.min(Date.now() - this._start, this._connectLatency) | ||
l -= delta | ||
} | ||
this._connecting = false | ||
this._queued.push(evt) | ||
setTimeout(() => { | ||
if (first) this._connectLatency = 0 | ||
evt.pending = false | ||
@@ -112,6 +94,2 @@ this._drain() | ||
} | ||
_latency () { | ||
return this._start + Math.round(Math.random() * this._var) | ||
} | ||
} | ||
@@ -118,0 +96,0 @@ |
{ | ||
"name": "debugging-stream", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Debug streams, by adding latency etc", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,6 +15,3 @@ # debugging-stream | ||
const s = new DebuggingStream(anotherStream, { | ||
latency: { | ||
read: [100, 200] // add between 100-200ms read latency, | ||
connect: [300, 500] // add between 300-500ms connect latency | ||
} | ||
latency: [100, 200] // add between 100-200ms read latency, | ||
}) | ||
@@ -21,0 +18,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2825
86
24