Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make cloned request inherit dispatcher #3631

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/web/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ class Cache {
for (const request of requests) {
const requestObject = fromInnerRequest(
request,
undefined,
new AbortController().signal,
'immutable'
)
Expand Down
6 changes: 4 additions & 2 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ class Request {
}

// 4. Return clonedRequestObject.
return fromInnerRequest(clonedRequest, ac.signal, getHeadersGuard(this[kHeaders]))
return fromInnerRequest(clonedRequest, this[kDispatcher], ac.signal, getHeadersGuard(this[kHeaders]))
}

[nodeUtil.inspect.custom] (depth, options) {
Expand Down Expand Up @@ -875,13 +875,15 @@ function cloneRequest (request) {
/**
* @see https://fetch.spec.whatwg.org/#request-create
* @param {any} innerRequest
* @param {import('../../dispatcher/agent')} dispatcher
* @param {AbortSignal} signal
* @param {'request' | 'immutable' | 'request-no-cors' | 'response' | 'none'} guard
* @returns {Request}
*/
function fromInnerRequest (innerRequest, signal, guard) {
function fromInnerRequest (innerRequest, dispatcher, signal, guard) {
const request = new Request(kConstruct)
request[kState] = innerRequest
request[kDispatcher] = dispatcher
request[kSignal] = signal
request[kHeaders] = new Headers(kConstruct)
setHeadersList(request[kHeaders], innerRequest.headersList)
Expand Down
12 changes: 12 additions & 0 deletions test/fetch/issue-3630.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { Request, Agent } = require('../..')
const { kDispatcher } = require('../../lib/web/fetch/symbols')

test('Cloned request should inherit its dispatcher', () => {
const agent = new Agent()
const request = new Request('https://a', { dispatcher: agent })
assert.strictEqual(request[kDispatcher], agent)
})
17 changes: 0 additions & 17 deletions test/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const {
Headers,
fetch
} = require('../../')
const { fromInnerRequest, makeRequest } = require('../../lib/web/fetch/request')
const { kState, kSignal, kHeaders } = require('../../lib/web/fetch/symbols')
const { getHeadersGuard, getHeadersList } = require('../../lib/web/fetch/headers')

const hasSignalReason = 'reason' in AbortSignal.prototype

Expand Down Expand Up @@ -462,17 +459,3 @@ test('Issue#2465', async (t) => {
const request = new Request('http://localhost', { body: new SharedArrayBuffer(0), method: 'POST' })
strictEqual(await request.text(), '[object SharedArrayBuffer]')
})

test('fromInnerRequest', () => {
const innerRequest = makeRequest({
urlList: [new URL('http://asd')]
})
const signal = new AbortController().signal
const request = fromInnerRequest(innerRequest, signal, 'immutable')

// check property
assert.strictEqual(request[kState], innerRequest)
assert.strictEqual(request[kSignal], signal)
assert.strictEqual(getHeadersList(request[kHeaders]), innerRequest.headersList)
assert.strictEqual(getHeadersGuard(request[kHeaders]), 'immutable')
})
Loading