Skip to content

Commit

Permalink
refactor: use fromInnerResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Jan 24, 2024
1 parent e2652b7 commit 977ad73
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 52 deletions.
12 changes: 3 additions & 9 deletions lib/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
const { kConstruct } = require('./symbols')
const { urlEquals, fieldValues: getFieldValues } = require('./util')
const { kEnumerableProperty, isDisturbed } = require('../core/util')
const { kHeadersList } = require('../core/symbols')
const { webidl } = require('../fetch/webidl')
const { Response, cloneResponse } = require('../fetch/response')
const { Response, cloneResponse, fromInnerResponse } = require('../fetch/response')
const { Request, fromInnerRequest } = require('../fetch/request')
const { Headers } = require('../fetch/headers')
const { kState, kHeaders, kGuard } = require('../fetch/symbols')
const { kState } = require('../fetch/symbols')
const { fetching } = require('../fetch/index')
const { urlIsHttpHttpsScheme, createDeferredPromise, readAllBytes } = require('../fetch/util')
const assert = require('assert')
Expand Down Expand Up @@ -783,11 +781,7 @@ class Cache {
// 5.5.2
for (const response of responses) {
// 5.5.2.1
const responseObject = new Response(kConstruct)
responseObject[kState] = response
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = response.headersList
responseObject[kHeaders][kGuard] = 'immutable'
const responseObject = fromInnerResponse(response, 'immutable')

responseList.push(responseObject.clone())

Expand Down
17 changes: 5 additions & 12 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
'use strict'

const {
Response,
makeNetworkError,
makeAppropriateNetworkError,
filterResponse,
makeResponse
makeResponse,
fromInnerResponse
} = require('./response')
const { Headers, HeadersList } = require('./headers')
const { HeadersList } = require('./headers')
const { Request, makeRequest } = require('./request')
const zlib = require('zlib')
const {
Expand Down Expand Up @@ -45,7 +45,7 @@ const {
simpleRangeHeaderValue,
buildContentRange
} = require('./util')
const { kState, kHeaders, kGuard, kRealm } = require('./symbols')
const { kState } = require('./symbols')
const assert = require('assert')
const { safelyExtractBody, extractBody } = require('./body')
const {
Expand All @@ -55,7 +55,6 @@ const {
requestBodyHeader,
subresourceSet
} = require('./constants')
const { kHeadersList, kConstruct } = require('../core/symbols')
const EE = require('events')
const { Readable, pipeline } = require('stream')
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor, bufferToLowerCasedHeaderName } = require('../core/util')
Expand Down Expand Up @@ -232,13 +231,7 @@ function fetch (input, init = undefined) {

// 4. Set responseObject to the result of creating a Response object,
// given response, "immutable", and relevantRealm.
responseObject = new Response(kConstruct)
responseObject[kState] = response
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = response.headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm
responseObject = fromInnerResponse(response, 'immutable', relevantRealm)

// 5. Resolve p with responseObject.
p.resolve(responseObject)
Expand Down
55 changes: 24 additions & 31 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ class Response {
// The static error() method steps are to return the result of creating a
// Response object, given a new network error, "immutable", and this’s
// relevant Realm.
const responseObject = new Response(kConstruct)
responseObject[kState] = makeNetworkError()
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm
const responseObject = fromInnerResponse(makeNetworkError(), 'immutable', relevantRealm)

return responseObject
}

Expand All @@ -67,13 +62,7 @@ class Response {
// 3. Let responseObject be the result of creating a Response object, given a new response,
// "response", and this’s relevant Realm.
const relevantRealm = { settingsObject: {} }
const responseObject = new Response(kConstruct)
responseObject[kState] = makeResponse({})
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'response'
responseObject[kHeaders][kRealm] = relevantRealm
const responseObject = fromInnerResponse(makeResponse({}), 'response', relevantRealm)

// 4. Perform initialize a response given responseObject, init, and (body, "application/json").
initializeResponse(responseObject, init, { body: body[0], type: 'application/json' })
Expand Down Expand Up @@ -111,13 +100,7 @@ class Response {

// 4. Let responseObject be the result of creating a Response object,
// given a new response, "immutable", and this’s relevant Realm.
const responseObject = new Response(kConstruct)
responseObject[kState] = makeResponse({})
responseObject[kRealm] = relevantRealm
responseObject[kHeaders] = new Headers(kConstruct)
responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList
responseObject[kHeaders][kGuard] = 'immutable'
responseObject[kHeaders][kRealm] = relevantRealm
const responseObject = fromInnerResponse(makeResponse({}), 'immutable', relevantRealm)

// 5. Set responseObject’s response’s status to status.
responseObject[kState].status = status
Expand Down Expand Up @@ -269,15 +252,7 @@ class Response {

// 3. Return the result of creating a Response object, given
// clonedResponse, this’s headers’s guard, and this’s relevant Realm.
const clonedResponseObject = new Response(kConstruct)
clonedResponseObject[kState] = clonedResponse
clonedResponseObject[kRealm] = this[kRealm]
clonedResponseObject[kHeaders] = new Headers(kConstruct)
clonedResponseObject[kHeaders][kHeadersList] = clonedResponse.headersList
clonedResponseObject[kHeaders][kGuard] = this[kHeaders][kGuard]
clonedResponseObject[kHeaders][kRealm] = this[kHeaders][kRealm]

return clonedResponseObject
return fromInnerResponse(clonedResponse, this[kHeaders][kRealm], this[kRealm])
}
}

Expand Down Expand Up @@ -512,6 +487,23 @@ function initializeResponse (response, init, body) {
}
}

/**
* @param {any} innerResponse
* @param {'request' | 'immutable' | 'request-no-cors' | 'response' | 'none'} guard
* @param {any} [realm]
* @returns {Response}
*/
function fromInnerResponse (innerResponse, guard, realm) {
const response = new Response(kConstruct)
response[kState] = innerResponse
response[kRealm] = realm
response[kHeaders] = new Headers(kConstruct)
response[kHeaders][kHeadersList] = innerResponse.headersList
response[kHeaders][kGuard] = guard
response[kHeaders][kRealm] = realm
return response
}

webidl.converters.ReadableStream = webidl.interfaceConverter(
ReadableStream
)
Expand Down Expand Up @@ -588,5 +580,6 @@ module.exports = {
makeAppropriateNetworkError,
filterResponse,
Response,
cloneResponse
cloneResponse,
fromInnerResponse
}

0 comments on commit 977ad73

Please sign in to comment.