streamiterator
Advanced tools
Comparing version 1.1.6 to 1.2.0
@@ -14,2 +14,6 @@ "use strict"; | ||
var makeValueRecord = function makeValueRecord(value) { | ||
return { value: value, done: false }; | ||
}; | ||
function createStreamIterator(stream) { | ||
@@ -41,10 +45,6 @@ var _result; | ||
function pushResult(promise) { | ||
function onData(data) { | ||
if (!inputClosed) { | ||
results.push(promise); | ||
results.push(Promise.resolve(data).then(makeValueRecord)); | ||
} | ||
} | ||
function onData(value) { | ||
pushResult({ value: value, done: false }); | ||
copyResults(); | ||
@@ -54,3 +54,5 @@ } | ||
function onError(error) { | ||
pushResult(Promise.reject(error)); | ||
if (!inputClosed) { | ||
results.push(Promise.reject(error)); | ||
} | ||
close(); | ||
@@ -68,6 +70,2 @@ } | ||
.removeListener("data", onData).removeListener("error", onError).removeListener("end", onEnd); | ||
// .destroy?.() | ||
if (stream.destroy) { | ||
stream.destroy(); | ||
} | ||
} | ||
@@ -77,12 +75,29 @@ copyResults(); | ||
function afterCurrentResolvers(callback) { | ||
if (resolvers.length === 0) { | ||
callback(); | ||
} else { | ||
var last = resolvers.pop(); | ||
resolvers.push(function (value) { | ||
last(value); | ||
callback(); | ||
}); | ||
} | ||
function shutdown(onSaveResult, onStreamDestroy) { | ||
// using onSaveResult callback instead of a Promise to not leave rejected promise unhandled on top of event loop | ||
return new Promise(function (resolve) { | ||
function save() { | ||
onSaveResult(resolve); | ||
} | ||
function done() { | ||
outputClosed = true; | ||
if (inputClosed || typeof stream.destroy !== "function") { | ||
save(); | ||
close(); | ||
} else { | ||
close(); | ||
stream.destroy(null, save); | ||
} | ||
onStreamDestroy && onStreamDestroy(); | ||
} | ||
if (resolvers.length === 0) { | ||
done(); | ||
} else { | ||
var last = resolvers.pop(); | ||
resolvers.push(function (value) { | ||
last(value); | ||
done(); | ||
}); | ||
} | ||
}); | ||
} | ||
@@ -101,16 +116,10 @@ | ||
}), _defineProperty(_result, "throw", function _throw(error) { | ||
return new Promise(function (resolve) { | ||
afterCurrentResolvers(function () { | ||
stream.emit("error", error); | ||
}); | ||
resolvers.push(resolve); | ||
copyResults(); | ||
return shutdown(function (resolve) { | ||
return resolve(Promise.reject(error)); | ||
}, function () { | ||
return stream.emit("error", error); | ||
}); | ||
}), _defineProperty(_result, "return", function _return(value) { | ||
return new Promise(function (resolve) { | ||
afterCurrentResolvers(function () { | ||
outputClosed = true; | ||
resolve({ value: value, done: true }); | ||
close(); | ||
}); | ||
return shutdown(function (resolve) { | ||
return resolve({ value: value, done: true }); | ||
}); | ||
@@ -117,0 +126,0 @@ }), _result); |
{ | ||
"name": "streamiterator", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"description": "converts ReadableStream into AsyncIterator", | ||
@@ -20,6 +20,10 @@ "keywords": [ | ||
"prettier": "prettier --write $(cat .prettier) 'source/**/*.js' '*.js'", | ||
"prepare": "npm run prettier && npm run build", | ||
"test": "jest" | ||
"prepare": "npm run build", | ||
"test": "jest", | ||
"go": "npm run prettier && npm run build && npm run test" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/vadzim/streamiterator/issues" | ||
}, | ||
"devDependencies": { | ||
@@ -26,0 +30,0 @@ "babel-cli": "^6.26.0", |
@@ -45,3 +45,3 @@ # streamiterator | ||
But if you believe that writing `streamIterator(...)` everywhere is a bullshit and in your world streams have to be iterable from the scratch right now, then you can import `streamiterator/polyfill` in the root of your project and iterate just on streams: | ||
But if you believe that writing `streamIterator(...)` everywhere is a bullshit, and in your world streams have to be iterable from the scratch right now, then you can import `streamiterator/polyfill` in the root of your project and iterate just on streams: | ||
@@ -67,2 +67,2 @@ ```js | ||
Please, feel free to [open an issue](https://github.com/vadzim/streamiterator/issues) or email me to developer@vadzim.info me if you have any question. | ||
Please, feel free to [open an issue](https://github.com/vadzim/streamiterator/issues) or email me to developer@vadzim.info if you have any question. |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
114074
130
0