response-iterator
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -21,3 +21,3 @@ "use strict"; | ||
}); | ||
})["catch"](reject); | ||
}).catch(reject); | ||
}); | ||
@@ -24,0 +24,0 @@ } |
@@ -124,3 +124,3 @@ (function (global, factory) { | ||
}); | ||
})["catch"](reject); | ||
}).catch(reject); | ||
}); | ||
@@ -175,1 +175,2 @@ } | ||
})); | ||
//# sourceMappingURL=response-iterator.js.map |
@@ -1,173 +0,2 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.responseIterator = factory()); | ||
})(this, (function () { 'use strict'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function asyncIterator(source) { | ||
var iterator = source[Symbol.asyncIterator](); | ||
return _defineProperty({ | ||
next: function next() { | ||
return iterator.next(); | ||
} | ||
}, Symbol.asyncIterator, function() { | ||
return this; | ||
}); | ||
} | ||
var hasIterator$3 = typeof Symbol !== "undefined" && Symbol.asyncIterator; | ||
/* c8 ignore start */ function nodeStreamIterator(stream) { | ||
var onData = function onData(chunk) { | ||
if (error) return; | ||
if (waiting.length) return waiting.shift()[0]({ | ||
value: chunk, | ||
done: false | ||
}); | ||
data.push(chunk); | ||
}; | ||
var onError = function onError(err) { | ||
error = err; | ||
var all = waiting.slice(); | ||
all.forEach(function(pair) { | ||
pair[1](err); | ||
}); | ||
!cleanup || cleanup(); | ||
}; | ||
var onEnd = function onEnd() { | ||
done = true; | ||
var all = waiting.slice(); | ||
all.forEach(function(pair) { | ||
pair[0]({ | ||
value: undefined, | ||
done: true | ||
}); | ||
}); | ||
!cleanup || cleanup(); | ||
}; | ||
var getNext = function getNext() { | ||
return new Promise(function(resolve, reject) { | ||
if (error) return reject(error); | ||
if (data.length) return resolve({ | ||
value: data.shift(), | ||
done: false | ||
}); | ||
if (done) return resolve({ | ||
value: undefined, | ||
done: true | ||
}); | ||
waiting.push([ | ||
resolve, | ||
reject | ||
]); | ||
}); | ||
}; | ||
var cleanup = null; | ||
var error = null; | ||
var done = false; | ||
var data = []; | ||
var waiting = []; | ||
cleanup = function() { | ||
cleanup = null; | ||
stream.removeListener("data", onData); | ||
stream.removeListener("error", onError); | ||
stream.removeListener("end", onEnd); | ||
stream.removeListener("finish", onEnd); | ||
stream.removeListener("close", onEnd); | ||
}; | ||
stream.on("data", onData); | ||
stream.on("error", onError); | ||
stream.on("end", onEnd); | ||
stream.on("finish", onEnd); | ||
stream.on("close", onEnd); | ||
var iterator = { | ||
next: function next() { | ||
return getNext(); | ||
} | ||
}; | ||
if (hasIterator$3) { | ||
iterator[Symbol.asyncIterator] = function() { | ||
return this; | ||
}; | ||
} | ||
return iterator; | ||
} | ||
var hasIterator$2 = typeof Symbol !== "undefined" && Symbol.asyncIterator; | ||
/* c8 ignore start */ function promiseIterator(promise) { | ||
var resolved = false; | ||
var iterator = { | ||
next: function next() { | ||
if (resolved) return Promise.resolve({ | ||
value: undefined, | ||
done: true | ||
}); | ||
resolved = true; | ||
return new Promise(function(resolve, reject) { | ||
promise.then(function(value) { | ||
resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
})["catch"](reject); | ||
}); | ||
} | ||
}; | ||
if (hasIterator$2) { | ||
iterator[Symbol.asyncIterator] = function() { | ||
return this; | ||
}; | ||
} | ||
return iterator; | ||
} | ||
var hasIterator$1 = typeof Symbol !== "undefined" && Symbol.asyncIterator; | ||
/* c8 ignore start */ function readerIterator(reader) { | ||
var iterator = { | ||
next: function next() { | ||
return reader.read(); | ||
} | ||
}; | ||
if (hasIterator$1) { | ||
iterator[Symbol.asyncIterator] = function() { | ||
return this; | ||
}; | ||
} | ||
return iterator; | ||
} | ||
// @ts-ignore | ||
var hasIterator = typeof Symbol !== "undefined" && Symbol.asyncIterator; | ||
/** | ||
* @param response A response. Supports fetch, node-fetch, and cross-fetch | ||
*/ function responseIterator(response) { | ||
if (response === undefined) throw new Error("Missing response for responseIterator"); | ||
// determine the body | ||
var body = response; | ||
if (response.body) body = response.body; | ||
else if (response.data) body = response.data; | ||
else if (response._bodyBlob) body = response._bodyBlob; // cross-fetch | ||
/* c8 ignore stop */ // adapt the body | ||
if (hasIterator && body[Symbol.asyncIterator]) return asyncIterator(body); | ||
/* c8 ignore start */ if (body.getReader) return readerIterator(body.getReader()); | ||
if (body.stream) return readerIterator(body.stream().getReader()); | ||
if (body.arrayBuffer) return promiseIterator(body.arrayBuffer()); | ||
if (body.pipe) return nodeStreamIterator(body); | ||
/* c8 ignore stop */ throw new Error("Unknown body type for responseIterator. Maybe you are not passing a streamable response"); | ||
} | ||
return responseIterator; | ||
})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).responseIterator=n()}(this,(function(){"use strict";function e(e){var n,r,o,t=e[Symbol.asyncIterator]();return n={next:function(){return t.next()}},r=Symbol.asyncIterator,o=function(){return this},r in n?Object.defineProperty(n,r,{value:o,enumerable:!0,configurable:!0,writable:!0}):n[r]=o,n}var n="undefined"!=typeof Symbol&&Symbol.asyncIterator;function r(e){var r=function(e){if(!a)return s.length?s.shift()[0]({value:e,done:!1}):void f.push(e)},o=function(e){a=e,s.slice().forEach((function(n){n[1](e)})),!i||i()},t=function(){u=!0,s.slice().forEach((function(e){e[0]({value:void 0,done:!0})})),!i||i()},i=null,a=null,u=!1,f=[],s=[];i=function(){i=null,e.removeListener("data",r),e.removeListener("error",o),e.removeListener("end",t),e.removeListener("finish",t),e.removeListener("close",t)},e.on("data",r),e.on("error",o),e.on("end",t),e.on("finish",t),e.on("close",t);var l={next:function(){return new Promise((function(e,n){return a?n(a):f.length?e({value:f.shift(),done:!1}):u?e({value:void 0,done:!0}):void s.push([e,n])}))}};return n&&(l[Symbol.asyncIterator]=function(){return this}),l}var o="undefined"!=typeof Symbol&&Symbol.asyncIterator;var t="undefined"!=typeof Symbol&&Symbol.asyncIterator;function i(e){var n={next:function(){return e.read()}};return t&&(n[Symbol.asyncIterator]=function(){return this}),n}var a="undefined"!=typeof Symbol&&Symbol.asyncIterator;return function(n){if(void 0===n)throw new Error("Missing response for responseIterator");var t,u,f,s=n;if(n.body?s=n.body:n.data?s=n.data:n._bodyBlob&&(s=n._bodyBlob),a&&s[Symbol.asyncIterator])return e(s);if(s.getReader)return i(s.getReader());if(s.stream)return i(s.stream().getReader());if(s.arrayBuffer)return t=s.arrayBuffer(),u=!1,f={next:function(){return u?Promise.resolve({value:void 0,done:!0}):(u=!0,new Promise((function(e,n){t.then((function(n){e({value:n,done:!1})})).catch(n)})))}},o&&(f[Symbol.asyncIterator]=function(){return this}),f;if(s.pipe)return r(s);throw new Error("Unknown body type for responseIterator. Maybe you are not passing a streamable response")}})); | ||
//# sourceMappingURL=response-iterator.min.js.map |
{ | ||
"name": "response-iterator", | ||
"description": "Creates an async iterator for a variety of inputs in the browser and node. Supports fetch, node-fetch, and cross-fetch", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"author": { | ||
@@ -56,3 +56,3 @@ "email": "kmalakoff@gmail.com", | ||
"@types/axios": "^0.14.0", | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^18.0.1", | ||
"@types/node-fetch": "^3.0.3", | ||
@@ -64,3 +64,3 @@ "cross-fetch": "^3.1.5", | ||
"readable-stream-3": "npm:readable-stream@4.0.0", | ||
"ts-dev-stack": "^0.5.1", | ||
"ts-dev-stack": "^0.6.5", | ||
"undici": "^5.6.0" | ||
@@ -67,0 +67,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
67816
35
4
531