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

cacheStorage: separate matchAll logic #2599

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
126 changes: 67 additions & 59 deletions lib/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Cache {
request = webidl.converters.RequestInfo(request)
options = webidl.converters.CacheQueryOptions(options)

const p = await this.matchAll(request, options)
const p = this.#internalMatchAll(request, options, 1)

if (p.length === 0) {
return
Expand All @@ -64,64 +64,7 @@ class Cache {
if (request !== undefined) request = webidl.converters.RequestInfo(request)
options = webidl.converters.CacheQueryOptions(options)

// 1.
let r = null

// 2.
if (request !== undefined) {
if (request instanceof Request) {
// 2.1.1
r = request[kState]

// 2.1.2
if (r.method !== 'GET' && !options.ignoreMethod) {
return []
}
} else if (typeof request === 'string') {
// 2.2.1
r = new Request(request)[kState]
}
}

// 5.
// 5.1
const responses = []

// 5.2
if (request === undefined) {
// 5.2.1
for (const requestResponse of this.#relevantRequestResponseList) {
responses.push(requestResponse[1])
}
} else { // 5.3
// 5.3.1
const requestResponses = this.#queryCache(r, options)

// 5.3.2
for (const requestResponse of requestResponses) {
responses.push(requestResponse[1])
}
}

// 5.4
// We don't implement CORs so we don't need to loop over the responses, yay!

// 5.5.1
const responseList = []

// 5.5.2
for (const response of responses) {
// 5.5.2.1
const responseObject = new Response(null)
responseObject[kState] = response
responseObject[kHeaders][kHeadersList] = response.headersList
responseObject[kHeaders][kGuard] = 'immutable'

responseList.push(responseObject.clone())
}

// 6.
return Object.freeze(responseList)
return this.#internalMatchAll(request, options)
}

async add (request) {
Expand Down Expand Up @@ -789,6 +732,71 @@ class Cache {

return true
}

#internalMatchAll (request, options, maxResponses = Infinity) {
// 1.
let r = null

// 2.
if (request !== undefined) {
if (request instanceof Request) {
// 2.1.1
r = request[kState]

// 2.1.2
if (r.method !== 'GET' && !options.ignoreMethod) {
return []
}
} else if (typeof request === 'string') {
// 2.2.1
r = new Request(request)[kState]
}
}

// 5.
// 5.1
const responses = []

// 5.2
if (request === undefined) {
// 5.2.1
for (const requestResponse of this.#relevantRequestResponseList) {
responses.push(requestResponse[1])
}
} else { // 5.3
// 5.3.1
const requestResponses = this.#queryCache(r, options)

// 5.3.2
for (const requestResponse of requestResponses) {
responses.push(requestResponse[1])
}
}

// 5.4
// We don't implement CORs so we don't need to loop over the responses, yay!

// 5.5.1
const responseList = []

// 5.5.2
for (const response of responses) {
// 5.5.2.1
const responseObject = new Response(null)
responseObject[kState] = response
responseObject[kHeaders][kHeadersList] = response.headersList
responseObject[kHeaders][kGuard] = 'immutable'

responseList.push(responseObject.clone())

if (responseList.length >= maxResponses) {
break
}
}

// 6.
return Object.freeze(responseList)
}
}

Object.defineProperties(Cache.prototype, {
Expand Down