Skip to content

Commit 03f419f

Browse files
authored
cacheStorage: separate matchAll logic (#2599)
1 parent 00f999d commit 03f419f

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

lib/cache/cache.js

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Cache {
4949
request = webidl.converters.RequestInfo(request)
5050
options = webidl.converters.CacheQueryOptions(options)
5151

52-
const p = await this.matchAll(request, options)
52+
const p = this.#internalMatchAll(request, options, 1)
5353

5454
if (p.length === 0) {
5555
return
@@ -64,64 +64,7 @@ class Cache {
6464
if (request !== undefined) request = webidl.converters.RequestInfo(request)
6565
options = webidl.converters.CacheQueryOptions(options)
6666

67-
// 1.
68-
let r = null
69-
70-
// 2.
71-
if (request !== undefined) {
72-
if (request instanceof Request) {
73-
// 2.1.1
74-
r = request[kState]
75-
76-
// 2.1.2
77-
if (r.method !== 'GET' && !options.ignoreMethod) {
78-
return []
79-
}
80-
} else if (typeof request === 'string') {
81-
// 2.2.1
82-
r = new Request(request)[kState]
83-
}
84-
}
85-
86-
// 5.
87-
// 5.1
88-
const responses = []
89-
90-
// 5.2
91-
if (request === undefined) {
92-
// 5.2.1
93-
for (const requestResponse of this.#relevantRequestResponseList) {
94-
responses.push(requestResponse[1])
95-
}
96-
} else { // 5.3
97-
// 5.3.1
98-
const requestResponses = this.#queryCache(r, options)
99-
100-
// 5.3.2
101-
for (const requestResponse of requestResponses) {
102-
responses.push(requestResponse[1])
103-
}
104-
}
105-
106-
// 5.4
107-
// We don't implement CORs so we don't need to loop over the responses, yay!
108-
109-
// 5.5.1
110-
const responseList = []
111-
112-
// 5.5.2
113-
for (const response of responses) {
114-
// 5.5.2.1
115-
const responseObject = new Response(null)
116-
responseObject[kState] = response
117-
responseObject[kHeaders][kHeadersList] = response.headersList
118-
responseObject[kHeaders][kGuard] = 'immutable'
119-
120-
responseList.push(responseObject.clone())
121-
}
122-
123-
// 6.
124-
return Object.freeze(responseList)
67+
return this.#internalMatchAll(request, options)
12568
}
12669

12770
async add (request) {
@@ -789,6 +732,71 @@ class Cache {
789732

790733
return true
791734
}
735+
736+
#internalMatchAll (request, options, maxResponses = Infinity) {
737+
// 1.
738+
let r = null
739+
740+
// 2.
741+
if (request !== undefined) {
742+
if (request instanceof Request) {
743+
// 2.1.1
744+
r = request[kState]
745+
746+
// 2.1.2
747+
if (r.method !== 'GET' && !options.ignoreMethod) {
748+
return []
749+
}
750+
} else if (typeof request === 'string') {
751+
// 2.2.1
752+
r = new Request(request)[kState]
753+
}
754+
}
755+
756+
// 5.
757+
// 5.1
758+
const responses = []
759+
760+
// 5.2
761+
if (request === undefined) {
762+
// 5.2.1
763+
for (const requestResponse of this.#relevantRequestResponseList) {
764+
responses.push(requestResponse[1])
765+
}
766+
} else { // 5.3
767+
// 5.3.1
768+
const requestResponses = this.#queryCache(r, options)
769+
770+
// 5.3.2
771+
for (const requestResponse of requestResponses) {
772+
responses.push(requestResponse[1])
773+
}
774+
}
775+
776+
// 5.4
777+
// We don't implement CORs so we don't need to loop over the responses, yay!
778+
779+
// 5.5.1
780+
const responseList = []
781+
782+
// 5.5.2
783+
for (const response of responses) {
784+
// 5.5.2.1
785+
const responseObject = new Response(null)
786+
responseObject[kState] = response
787+
responseObject[kHeaders][kHeadersList] = response.headersList
788+
responseObject[kHeaders][kGuard] = 'immutable'
789+
790+
responseList.push(responseObject.clone())
791+
792+
if (responseList.length >= maxResponses) {
793+
break
794+
}
795+
}
796+
797+
// 6.
798+
return Object.freeze(responseList)
799+
}
792800
}
793801

794802
Object.defineProperties(Cache.prototype, {

0 commit comments

Comments
 (0)