From 8731017415c200b70a44120640580c4064d0fff9 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 3 Aug 2021 16:24:12 +0200 Subject: [PATCH] fix: fetch redirect modes --- README.md | 2 +- lib/api/api-fetch.js | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a95f47ccf26..85da52bfa13 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ https://fetch.spec.whatwg.org/#request-class * **mode** *not supported* * **credentials** *not supported* * **cache** *not supported* -* **redirect** `RequestRedirect` *only `"follow"` supported* +* **redirect** `RequestRedirect` * **integrity** *not supported* * **keepalive** *not supported* * **signal** `AbortSignal?` diff --git a/lib/api/api-fetch.js b/lib/api/api-fetch.js index 7a7403b76ff..cf8acb3fec8 100644 --- a/lib/api/api-fetch.js +++ b/lib/api/api-fetch.js @@ -23,18 +23,18 @@ const kHeaders = Symbol('headers') class Response extends Body { constructor ({ - type = 'default', + type, url, resume, abort, - statusCode = 0, + statusCode, headers, context }) { super(resume, abort) - this[kType] = type - this[kStatus] = statusCode + this[kType] = type || 'default' + this[kStatus] = statusCode || 0 this[kUrlList] = [url] this[kHeaders] = headers @@ -160,15 +160,21 @@ class FetchHandler extends AsyncResource { type: 'opaqueredirect', url: this.url }) + // TODO (fix): Should be "null" body. + // TODO (fix): What to do with Readable? + util.destroy(body) } else { body = new Response({ type: 'error', url: this.url }) + // TODO (fix): Should be "null" body. + // TODO (fix): What to do with Readable? util.destroy(body, new Error('redirect')) } } else { body = new Response({ + type: 'default', url: this.url, resume, abort, @@ -260,13 +266,8 @@ function fetch (opts, callback) { throw new NotSupportedError() } - if ( - opts.redirect !== undefined && - opts.redirect !== 'follow' && - opts.redirect !== 'error' - ) { - // TODO: Implement - throw new NotSupportedError() + if (opts.redirect) { + // TODO: Validate } if (opts.integrity) { @@ -274,6 +275,10 @@ function fetch (opts, callback) { throw new NotSupportedError() } + if (opts.keepalive) { + // Ignore + } + const headers = new Headers(opts.headers) if (!headers.has('accept')) {