Skip to content

Commit

Permalink
fix: fetch redirect modes
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 3, 2021
1 parent f01d2ac commit da556ab
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions lib/api/api-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,39 @@ const { AsyncResource } = require('async_hooks')
const { addSignal, removeSignal } = require('./abort-signal')
const { isBlob } = require('buffer')

const kType = Symbol('type')
const kStatus = Symbol('status')
const kUrlList = Symbol('url list')
const kHeaders = Symbol('headers')

class Response extends Body {
constructor ({
type = 'default',
url,
resume,
abort,
statusCode,
statusCode = 0,
headers,
context
}) {
super(resume, abort)

this[kType] = type
this[kStatus] = statusCode
this[kUrlList] = [url]
this[kHeaders] = headers

if (context && context.history) {
this[kUrlList].push(...context.history)
}

this.on('error', function () {
// Swallow error? Avoid unhandled exception.
})
}

get type () {
return 'default'
return this[kType]
}

get url () {
Expand Down Expand Up @@ -80,7 +87,6 @@ class Response extends Body {
}
}

// TODO: https://fetch.spec.whatwg.org/#concept-http-redirect-fetch
class FetchHandler extends AsyncResource {
constructor (opts, callback) {
if (!opts || typeof opts !== 'object') {
Expand Down Expand Up @@ -146,19 +152,32 @@ class FetchHandler extends AsyncResource {

headers = new Headers(headers)

if (this.redirect === 'follow' && headers.get('location')) {
// TODO: network error
let body

if (headers.has('location')) {
if (this.redirect === 'manual') {
body = new Response({
type: 'opaqueredirect',
url: this.url
})
} else {
body = new Response({
type: 'error',
url: this.url
})
util.destroy(body, new Error('redirect'))
}
} else {
body = new Response({
url: this.url,
resume,
abort,
statusCode,
headers,
context
})
}

const body = new Response({
url: this.url,
resume,
abort,
statusCode,
headers,
context
})

this.callback = null
this.res = body

Expand Down Expand Up @@ -241,7 +260,11 @@ function fetch (opts, callback) {
throw new NotSupportedError()
}

if (opts.redirect !== undefined && opts.redirect !== 'follow') {
if (
opts.redirect !== undefined &&
opts.redirect !== 'follow' &&
opts.redirect !== 'error'
) {
// TODO: Implement
throw new NotSupportedError()
}
Expand Down

0 comments on commit da556ab

Please sign in to comment.