Skip to content

Commit

Permalink
cacheStorage: separate matchAll logic (#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jan 8, 2024
1 parent 00f999d commit 03f419f
Showing 1 changed file with 67 additions and 59 deletions.
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

0 comments on commit 03f419f

Please sign in to comment.