@whatwg-node/fetch
Advanced tools
Comparing version 0.4.6-alpha-20220927131110-4cfdcdb to 0.4.6-alpha-20220927134607-bd5c990
# @whatwg-node/fetch | ||
## 0.4.6-alpha-20220927131110-4cfdcdb | ||
## 0.4.6-alpha-20220927134607-bd5c990 | ||
@@ -9,2 +9,7 @@ ### Patch Changes | ||
- [#148](https://github.com/ardatan/whatwg-node/pull/148) [`eb10500`](https://github.com/ardatan/whatwg-node/commit/eb105005fd01bd227eff8d52c22b39ea1a8c6700) Thanks [@ardatan](https://github.com/ardatan)! - - On Node 14, fix the return method of Response.body's AsyncIterator to close HTTP connection correctly | ||
- On Node 14, handle ReadableStream's cancel correctly if Response.body is a ReadableStream | ||
- Do not modify ReadableStream.cancel's behavior but handle it internally | ||
- On Node 18, do not combine Response.body's return and AbortController which causes a memory leak | ||
## 0.4.5 | ||
@@ -11,0 +16,0 @@ |
const handleFileRequest = require("./handle-file-request"); | ||
const readableStreamToReadable = require("./readableStreamToReadable"); | ||
@@ -82,45 +83,2 @@ module.exports = function createNodePonyfill(opts = {}) { | ||
// ReadableStream doesn't handle aborting properly, so we need to patch it | ||
ponyfills.ReadableStream = class PonyfillReadableStream extends ponyfills.ReadableStream { | ||
constructor(underlyingSource, ...opts) { | ||
super({ | ||
...underlyingSource, | ||
cancel: (e) => { | ||
this.cancelled = true; | ||
if (underlyingSource.cancel) { | ||
return underlyingSource.cancel(e); | ||
} | ||
} | ||
}, ...opts); | ||
this.underlyingSource = underlyingSource; | ||
} | ||
[Symbol.asyncIterator]() { | ||
const asyncIterator = super[Symbol.asyncIterator](); | ||
return { | ||
next: (...args) => asyncIterator.next(...args), | ||
throw: (...args) => asyncIterator.throw(...args), | ||
return: async (e) => { | ||
const originalResult = await asyncIterator.return(e); | ||
if (!this.cancelled) { | ||
this.cancelled = true; | ||
if (this.underlyingSource.cancel) { | ||
await this.underlyingSource.cancel(e); | ||
} | ||
} | ||
return originalResult; | ||
} | ||
} | ||
} | ||
async cancel(e) { | ||
const originalResult = !super.locked && await super.cancel(e); | ||
if (!this.cancelled) { | ||
this.cancelled = true; | ||
if (this.underlyingSource.cancel) { | ||
await this.underlyingSource.cancel(e); | ||
} | ||
} | ||
return originalResult; | ||
} | ||
} | ||
if (!ponyfills.crypto) { | ||
@@ -256,3 +214,3 @@ const cryptoModule = require("crypto"); | ||
if (options.body[Symbol.toStringTag] === 'ReadableStream') { | ||
options.body = streams.Readable.fromWeb ? streams.Readable.fromWeb(options.body) : streams.Readable.from(options.body); | ||
options.body = readableStreamToReadable(options.body); | ||
} | ||
@@ -275,3 +233,39 @@ } | ||
} | ||
return realFetch(requestOrUrl); | ||
const abortCtrl = new ponyfills.AbortController(); | ||
return realFetch(requestOrUrl, { | ||
...options, | ||
signal: abortCtrl.signal | ||
}).then(res => { | ||
return new Proxy(res, { | ||
get(target, prop, receiver) { | ||
if (prop === 'body') { | ||
return new Proxy(res.body, { | ||
get(target, prop, receiver) { | ||
if (prop === Symbol.asyncIterator) { | ||
return () => { | ||
const originalAsyncIterator = target[Symbol.asyncIterator](); | ||
return { | ||
next() { | ||
return originalAsyncIterator.next(); | ||
}, | ||
return() { | ||
abortCtrl.abort(); | ||
return originalAsyncIterator.return(); | ||
}, | ||
throw(error) { | ||
abortCtrl.abort(error); | ||
return originalAsyncIterator.throw(error); | ||
} | ||
} | ||
} | ||
} | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
}) | ||
} | ||
return Reflect.get(target, prop, receiver); | ||
} | ||
}) | ||
}); | ||
}; | ||
@@ -284,12 +278,3 @@ | ||
if (body != null && body[Symbol.toStringTag] === 'ReadableStream') { | ||
const actualBody = streams.Readable.fromWeb ? streams.Readable.fromWeb(body) : streams.Readable.from(body, { | ||
emitClose: true, | ||
autoDestroy: true, | ||
}); | ||
actualBody.on('pause', () => { | ||
body.cancel(); | ||
}) | ||
actualBody.on('close', () => { | ||
body.cancel(); | ||
}) | ||
const actualBody = readableStreamToReadable(body); | ||
// Polyfill ReadableStream is not working well with node-fetch's Response | ||
@@ -296,0 +281,0 @@ return new OriginalResponse(actualBody, init); |
{ | ||
"name": "@whatwg-node/fetch", | ||
"version": "0.4.6-alpha-20220927131110-4cfdcdb", | ||
"version": "0.4.6-alpha-20220927134607-bd5c990", | ||
"description": "Cross Platform Smart Fetch Ponyfill", | ||
@@ -5,0 +5,0 @@ "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>", |
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
27416
12
606