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 2 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 }
Loading