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

fix(cache): set AbortSignal #2612

Merged
merged 6 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 9 additions & 10 deletions lib/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const { kEnumerableProperty, isDisturbed } = require('../core/util')
const { kHeadersList } = require('../core/symbols')
const { webidl } = require('../fetch/webidl')
const { Response, cloneResponse } = require('../fetch/response')
const { Request } = require('../fetch/request')
const { Request, fromInnerRequest } = require('../fetch/request')
const { Headers } = require('../fetch/headers')
const { kState, kHeaders, kGuard, kRealm } = require('../fetch/symbols')
const { kState, kHeaders, kGuard } = require('../fetch/symbols')
const { fetching } = require('../fetch/index')
const { urlIsHttpHttpsScheme, createDeferredPromise, readAllBytes } = require('../fetch/util')
const assert = require('assert')
Expand Down Expand Up @@ -444,7 +444,7 @@ class Cache {
* @see https://w3c.github.io/ServiceWorker/#dom-cache-keys
* @param {any} request
* @param {import('../../types/cache').CacheQueryOptions} options
* @returns {readonly Request[]}
* @returns {Promise<readonly Request[]>}
*/
async keys (request = undefined, options = {}) {
webidl.brandCheck(this, Cache)
Expand Down Expand Up @@ -503,13 +503,12 @@ class Cache {

// 5.4.2
for (const request of requests) {
const requestObject = new Request(kConstruct)
requestObject[kState] = request
requestObject[kHeaders] = new Headers(kConstruct)
requestObject[kHeaders][kHeadersList] = request.headersList
requestObject[kHeaders][kGuard] = 'immutable'
requestObject[kRealm] = request.client

const requestObject = fromInnerRequest(
request,
new AbortController().signal,
'immutable',
{ settingsObject: request.client }
)
Comment on lines +506 to +511
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of utility can prevent such mistakes.

// 5.4.2.1
requestList.push(requestObject)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/cachestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class CacheStorage {

/**
* @see https://w3c.github.io/ServiceWorker/#cache-storage-keys
* @returns {string[]}
* @returns {Promise<string[]>}
*/
async keys () {
webidl.brandCheck(this, CacheStorage)
Expand Down
22 changes: 21 additions & 1 deletion lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,26 @@ function cloneRequest (request) {
return newRequest
}

/**
* @param {any} innerRequest
* @param {AbortSignal} signal
* @param {'request' | 'immutable' | 'request-no-cors' | 'response' | 'none'} guard
* @param {any} [realm]
* @returns {Request}
*/
function fromInnerRequest (innerRequest, signal, guard, realm) {
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
const request = new Request(kConstruct)
request[kState] = innerRequest
request[kRealm] = realm
request[kSignal] = signal
request[kSignal][kRealm] = realm
request[kHeaders] = new Headers(kConstruct)
request[kHeaders][kHeadersList] = innerRequest.headersList
request[kHeaders][kGuard] = guard
request[kHeaders][kRealm] = realm
return request
}

Object.defineProperties(Request.prototype, {
method: kEnumerableProperty,
url: kEnumerableProperty,
Expand Down Expand Up @@ -970,4 +990,4 @@ webidl.converters.RequestInit = webidl.dictionaryConverter([
}
])

module.exports = { Request, makeRequest }
module.exports = { Request, makeRequest, fromInnerRequest }
24 changes: 22 additions & 2 deletions test/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ const {
Headers,
fetch
} = require('../../')
const { fromInnerRequest, makeRequest } = require('../../lib/fetch/request')
const {
Blob: ThirdPartyBlob,
FormData: ThirdPartyFormData
} = require('formdata-node')
const { kState, kGuard, kRealm } = require('../../lib/fetch/symbols')
const { kHeadersList } = require('../../lib/core/symbols')

const hasSignalReason = 'reason' in AbortSignal.prototype

Expand Down Expand Up @@ -472,9 +475,8 @@ test('Clone the set-cookie header when Request is passed as the first parameter

// Tests for optimization introduced in https://github.com/nodejs/undici/pull/2456
test('keys to object prototypes method', (t) => {
const { ok } = tspl(t, { plan: 1 })
const request = new Request('http://localhost', { method: 'hasOwnProperty' })
ok(typeof request.method === 'string')
assert(typeof request.method === 'string')
})

// https://github.com/nodejs/undici/issues/2465
Expand All @@ -483,3 +485,21 @@ 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({})
const signal = new AbortController().signal
const realm = { settingsObject: {} }
const request = fromInnerRequest(innerRequest, signal, 'immutable', realm)

// check instance
assert(request instanceof Request)
assert(request.headers instanceof Headers)
assert(request.signal instanceof AbortSignal)

assert.strictEqual(request[kState], innerRequest)
assert.strictEqual(request[kRealm], realm)
assert.strictEqual(request.signal, signal)
assert.strictEqual(request.headers[kHeadersList], request.headersList)
assert.strictEqual(request.headers[kGuard], 'immutable')
})
Loading