Update to new API, add clonable API, add debug calls

master
Sven Slootweg 4 years ago
parent 487023b2c0
commit c6b231afde

@ -2,25 +2,46 @@
const Promise = require("bluebird"); const Promise = require("bluebird");
const ms = require("ms"); const ms = require("ms");
const promiseDelaySince = require("@joepie91/promise-delay-every"); const promiseDelayEvery = require("@joepie91/promise-delay-every");
const propagateAbort = require("@ppstreams/propagate-abort"); const propagateAbort = require("@promistream/propagate-abort");
const propagatePeek = require("@promistream/propagate-peek");
const debug = require("debug")("promistream:rate-limit");
module.exports = function rateLimitedStream(interval) { function makeClonableInstance(interval) {
let intervalInMilliseconds = (typeof interval === "number") let intervalInMilliseconds = (typeof interval === "number")
? interval ? interval
: ms(interval); : ms(interval);
let delayer = promiseDelaySince(intervalInMilliseconds); debug(`Creating clonable with interval of ${intervalInMilliseconds}ms`);
return { let delayer = promiseDelayEvery(intervalInMilliseconds);
description: `rate-limited stream (1 per ${interval})`,
read: (source) => { return function createClone () {
return Promise.try(() => { debug("Creating clone");
return delayer();
}).then(() => { return {
return source.read(); _promistreamVersion: 0,
}); description: `rate-limited stream (1 per ${interval})`,
}, read: (source) => {
abort: propagateAbort return Promise.try(() => {
} debug("Attempted read");
}; return delayer();
}).then(() => {
debug("Delay passed, completing read");
return source.read();
});
},
peek: propagatePeek,
abort: propagateAbort
};
};
}
module.exports = function rateLimitedStream(... args) {
// Internally, we treat a non-clonable stream as a clonable stream that will only ever have one clone - this makes implementation much simpler
let createClone = makeClonableInstance(... args);
return createClone();
};
module.exports.clonable = makeClonableInstance;

@ -7,8 +7,10 @@
"license": "WTFPL OR CC0-1.0", "license": "WTFPL OR CC0-1.0",
"dependencies": { "dependencies": {
"@joepie91/promise-delay-every": "^1.0.0", "@joepie91/promise-delay-every": "^1.0.0",
"@ppstreams/propagate-abort": "^0.1.2", "@promistream/propagate-abort": "^0.1.6",
"@promistream/propagate-peek": "^0.1.1",
"bluebird": "^3.5.4", "bluebird": "^3.5.4",
"debug": "^4.3.1",
"ms": "^2.1.1" "ms": "^2.1.1"
} }
} }

Loading…
Cancel
Save