pull-stream
Advanced tools
| var pull = require('..') | ||
| var tape = require('tape') | ||
| tape('abort on drain', function (t) { | ||
| var c = 100 | ||
| var drain = pull.drain(function () { | ||
| if(c < 0) throw new Error('stream should have aborted') | ||
| if(!--c) return false //drain.abort() | ||
| }, function () { | ||
| t.end() | ||
| }) | ||
| pull(pull.infinite(), drain) | ||
| }) | ||
| function delay () { | ||
| return pull.asyncMap(function (e, cb) { | ||
| setTimeout(function () { cb(null, e) }) | ||
| }) | ||
| } | ||
| tape('abort on drain - async', function (t) { | ||
| var c = 100 | ||
| var drain = pull.drain(function () { | ||
| if(c < 0) throw new Error('stream should have aborted') | ||
| if(!--c) return drain.abort() | ||
| }, function () { | ||
| t.end() | ||
| }) | ||
| pull(pull.infinite(), delay(), drain) | ||
| }) | ||
| tape('abort on drain - sync', function (t) { | ||
| var c = 100 | ||
| var drain = pull.drain(function () { | ||
| if(c < 0) throw new Error('stream should have aborted') | ||
| if(!--c) return drain.abort() | ||
| }, function () { | ||
| t.end() | ||
| }) | ||
| pull(pull.infinite(), drain) | ||
| }) | ||
| tape('abort on drain - async, out of cb', function (t) { | ||
| var c = 0, ERR = new Error('test ABORT') | ||
| var drain = pull.drain(function () { | ||
| --c | ||
| }, function (err) { | ||
| t.ok(c < 0) | ||
| t.equal(err, ERR) | ||
| t.end() | ||
| }) | ||
| pull(pull.infinite(), delay(), drain) | ||
| setTimeout(function () { | ||
| drain.abort(ERR) | ||
| }, 100) | ||
| }) | ||
+1
-1
| { | ||
| "name": "pull-stream", | ||
| "description": "minimal pull stream", | ||
| "version": "3.0.1", | ||
| "version": "3.1.0", | ||
| "homepage": "https://github.com/dominictarr/pull-stream", | ||
@@ -6,0 +6,0 @@ "repository": { |
+10
-8
@@ -88,10 +88,12 @@ # pull-stream | ||
| //read source and log it. | ||
| var logger = function (read) { | ||
| read(null, function next(end, data) { | ||
| if(end === true) return | ||
| if(end) throw end | ||
| var logger = function () { | ||
| return function (read) { | ||
| read(null, function next(end, data) { | ||
| if(end === true) return | ||
| if(end) throw end | ||
| console.log(data) | ||
| read(null, next) | ||
| }) | ||
| console.log(data) | ||
| read(null, next) | ||
| }) | ||
| } | ||
| } | ||
@@ -103,3 +105,3 @@ ``` | ||
| ```js | ||
| var rand = random()) | ||
| var rand = random() | ||
| var log = logger() | ||
@@ -106,0 +108,0 @@ |
+36
-24
@@ -17,5 +17,7 @@ 'use strict' | ||
| var drain = exports.drain = function (op, done) { | ||
| var read, abort | ||
| return function (read) { | ||
| function sink (_read) { | ||
| read = _read | ||
| if(abort) return sink.abort() | ||
| //this function is much simpler to write if you | ||
@@ -25,29 +27,35 @@ //just use recursion, but by using a while loop | ||
| ;(function next() { | ||
| var loop = true, cbed = false | ||
| while(loop) { | ||
| cbed = false | ||
| read(null, function (end, data) { | ||
| cbed = true | ||
| if(end) { | ||
| var loop = true, cbed = false | ||
| while(loop) { | ||
| cbed = false | ||
| read(null, function (end, data) { | ||
| cbed = true | ||
| if(end = end || abort) { | ||
| loop = false | ||
| if(done) done(end === true ? null : end) | ||
| else if(end && end !== true) | ||
| throw end | ||
| } | ||
| else if(op && false === op(data) || abort) { | ||
| loop = false | ||
| read(abort || true, done || function () {}) | ||
| } | ||
| else if(!loop){ | ||
| next() | ||
| } | ||
| }) | ||
| if(!cbed) { | ||
| loop = false | ||
| if(done) done(end === true ? null : end) | ||
| else if(end && end !== true) | ||
| throw end | ||
| return | ||
| } | ||
| else if(op && false === op(data)) { | ||
| loop = false | ||
| read(true, done || function () {}) | ||
| } | ||
| else if(!loop){ | ||
| next() | ||
| } | ||
| }) | ||
| if(!cbed) { | ||
| loop = false | ||
| return | ||
| } | ||
| } | ||
| })() | ||
| })() | ||
| } | ||
| sink.abort = function (err) { | ||
| abort = err || true | ||
| if(read) return read(abort, function () {}) | ||
| } | ||
| return sink | ||
| } | ||
@@ -110,1 +118,5 @@ | ||
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
51782
3.25%32
3.23%1146
5.43%293
0.69%