Skip to content

Commit

Permalink
use utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Jan 18, 2024
1 parent 7e07591 commit a2c244e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
21 changes: 9 additions & 12 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, kSignal } = require('../fetch/symbols')
const { kState, kHeaders, kGuard, kRealm } = 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,15 +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
requestObject[kSignal] = new AbortController().signal
requestObject[kSignal][kRealm] = requestObject[kRealm]

const requestObject = fromInnerRequest(
request,
new AbortController().signal,
'immutable'
)
requestObject[kRealm] = { settingsObject: request.client }
// 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
18 changes: 17 additions & 1 deletion lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,22 @@ function cloneRequest (request) {
return newRequest
}

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

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

module.exports = { Request, makeRequest }
module.exports = { Request, makeRequest, fromInnerRequest }

0 comments on commit a2c244e

Please sign in to comment.