Skip to content

Commit c3831d1

Browse files
ronagKhafraDev
authored andcommitted
refactor: move web stuff into their own folder (nodejs#2793)
Refs: nodejs#2732
1 parent c6a1b9b commit c3831d1

36 files changed

+1739
-2892
lines changed

lib/web/cache/cache.js

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { kState } = require('../fetch/symbols')
1010
const { fetching } = require('../fetch/index')
1111
const { urlIsHttpHttpsScheme, createDeferredPromise, readAllBytes } = require('../fetch/util')
1212
const assert = require('node:assert')
13+
const { getGlobalDispatcher } = require('../../global')
1314

1415
/**
1516
* @see https://w3c.github.io/ServiceWorker/#dfn-cache-batch-operation
@@ -42,12 +43,10 @@ class Cache {
4243

4344
async match (request, options = {}) {
4445
webidl.brandCheck(this, Cache)
46+
webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.match' })
4547

46-
const prefix = 'Cache.match'
47-
webidl.argumentLengthCheck(arguments, 1, prefix)
48-
49-
request = webidl.converters.RequestInfo(request, prefix, 'request')
50-
options = webidl.converters.CacheQueryOptions(options, prefix, 'options')
48+
request = webidl.converters.RequestInfo(request)
49+
options = webidl.converters.CacheQueryOptions(options)
5150

5251
const p = this.#internalMatchAll(request, options, 1)
5352

@@ -61,20 +60,17 @@ class Cache {
6160
async matchAll (request = undefined, options = {}) {
6261
webidl.brandCheck(this, Cache)
6362

64-
const prefix = 'Cache.matchAll'
65-
if (request !== undefined) request = webidl.converters.RequestInfo(request, prefix, 'request')
66-
options = webidl.converters.CacheQueryOptions(options, prefix, 'options')
63+
if (request !== undefined) request = webidl.converters.RequestInfo(request)
64+
options = webidl.converters.CacheQueryOptions(options)
6765

6866
return this.#internalMatchAll(request, options)
6967
}
7068

7169
async add (request) {
7270
webidl.brandCheck(this, Cache)
71+
webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.add' })
7372

74-
const prefix = 'Cache.add'
75-
webidl.argumentLengthCheck(arguments, 1, prefix)
76-
77-
request = webidl.converters.RequestInfo(request, prefix, 'request')
73+
request = webidl.converters.RequestInfo(request)
7874

7975
// 1.
8076
const requests = [request]
@@ -88,9 +84,7 @@ class Cache {
8884

8985
async addAll (requests) {
9086
webidl.brandCheck(this, Cache)
91-
92-
const prefix = 'Cache.addAll'
93-
webidl.argumentLengthCheck(arguments, 1, prefix)
87+
webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.addAll' })
9488

9589
// 1.
9690
const responsePromises = []
@@ -102,7 +96,7 @@ class Cache {
10296
for (let request of requests) {
10397
if (request === undefined) {
10498
throw webidl.errors.conversionFailed({
105-
prefix,
99+
prefix: 'Cache.addAll',
106100
argument: 'Argument 1',
107101
types: ['undefined is not allowed']
108102
})
@@ -120,7 +114,7 @@ class Cache {
120114
// 3.2
121115
if (!urlIsHttpHttpsScheme(r.url) || r.method !== 'GET') {
122116
throw webidl.errors.exception({
123-
header: prefix,
117+
header: 'Cache.addAll',
124118
message: 'Expected http/s scheme when method is not GET.'
125119
})
126120
}
@@ -138,7 +132,7 @@ class Cache {
138132
// 5.2
139133
if (!urlIsHttpHttpsScheme(r.url)) {
140134
throw webidl.errors.exception({
141-
header: prefix,
135+
header: 'Cache.addAll',
142136
message: 'Expected http/s scheme.'
143137
})
144138
}
@@ -156,6 +150,7 @@ class Cache {
156150
// 5.7
157151
fetchControllers.push(fetching({
158152
request: r,
153+
dispatcher: getGlobalDispatcher(),
159154
processResponse (response) {
160155
// 1.
161156
if (response.type === 'error' || response.status === 206 || response.status < 200 || response.status > 299) {
@@ -258,12 +253,10 @@ class Cache {
258253

259254
async put (request, response) {
260255
webidl.brandCheck(this, Cache)
256+
webidl.argumentLengthCheck(arguments, 2, { header: 'Cache.put' })
261257

262-
const prefix = 'Cache.put'
263-
webidl.argumentLengthCheck(arguments, 2, prefix)
264-
265-
request = webidl.converters.RequestInfo(request, prefix, 'request')
266-
response = webidl.converters.Response(response, prefix, 'response')
258+
request = webidl.converters.RequestInfo(request)
259+
response = webidl.converters.Response(response)
267260

268261
// 1.
269262
let innerRequest = null
@@ -278,7 +271,7 @@ class Cache {
278271
// 4.
279272
if (!urlIsHttpHttpsScheme(innerRequest.url) || innerRequest.method !== 'GET') {
280273
throw webidl.errors.exception({
281-
header: prefix,
274+
header: 'Cache.put',
282275
message: 'Expected an http/s scheme when method is not GET'
283276
})
284277
}
@@ -289,7 +282,7 @@ class Cache {
289282
// 6.
290283
if (innerResponse.status === 206) {
291284
throw webidl.errors.exception({
292-
header: prefix,
285+
header: 'Cache.put',
293286
message: 'Got 206 status'
294287
})
295288
}
@@ -304,7 +297,7 @@ class Cache {
304297
// 7.2.1
305298
if (fieldValue === '*') {
306299
throw webidl.errors.exception({
307-
header: prefix,
300+
header: 'Cache.put',
308301
message: 'Got * vary field value'
309302
})
310303
}
@@ -314,7 +307,7 @@ class Cache {
314307
// 8.
315308
if (innerResponse.body && (isDisturbed(innerResponse.body.stream) || innerResponse.body.stream.locked)) {
316309
throw webidl.errors.exception({
317-
header: prefix,
310+
header: 'Cache.put',
318311
message: 'Response body is locked or disturbed'
319312
})
320313
}
@@ -389,12 +382,10 @@ class Cache {
389382

390383
async delete (request, options = {}) {
391384
webidl.brandCheck(this, Cache)
385+
webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.delete' })
392386

393-
const prefix = 'Cache.delete'
394-
webidl.argumentLengthCheck(arguments, 1, prefix)
395-
396-
request = webidl.converters.RequestInfo(request, prefix, 'request')
397-
options = webidl.converters.CacheQueryOptions(options, prefix, 'options')
387+
request = webidl.converters.RequestInfo(request)
388+
options = webidl.converters.CacheQueryOptions(options)
398389

399390
/**
400391
* @type {Request}
@@ -456,10 +447,8 @@ class Cache {
456447
async keys (request = undefined, options = {}) {
457448
webidl.brandCheck(this, Cache)
458449

459-
const prefix = 'Cache.keys'
460-
461-
if (request !== undefined) request = webidl.converters.RequestInfo(request, prefix, 'request')
462-
options = webidl.converters.CacheQueryOptions(options, prefix, 'options')
450+
if (request !== undefined) request = webidl.converters.RequestInfo(request)
451+
options = webidl.converters.CacheQueryOptions(options)
463452

464453
// 1.
465454
let r = null
@@ -515,7 +504,8 @@ class Cache {
515504
const requestObject = fromInnerRequest(
516505
request,
517506
new AbortController().signal,
518-
'immutable'
507+
'immutable',
508+
{ settingsObject: request.client }
519509
)
520510
// 5.4.2.1
521511
requestList.push(requestObject)
@@ -791,7 +781,7 @@ class Cache {
791781
// 5.5.2
792782
for (const response of responses) {
793783
// 5.5.2.1
794-
const responseObject = fromInnerResponse(response, 'immutable')
784+
const responseObject = fromInnerResponse(response, 'immutable', { settingsObject: {} })
795785

796786
responseList.push(responseObject.clone())
797787

@@ -823,17 +813,17 @@ const cacheQueryOptionConverters = [
823813
{
824814
key: 'ignoreSearch',
825815
converter: webidl.converters.boolean,
826-
defaultValue: () => false
816+
defaultValue: false
827817
},
828818
{
829819
key: 'ignoreMethod',
830820
converter: webidl.converters.boolean,
831-
defaultValue: () => false
821+
defaultValue: false
832822
},
833823
{
834824
key: 'ignoreVary',
835825
converter: webidl.converters.boolean,
836-
defaultValue: () => false
826+
defaultValue: false
837827
}
838828
]
839829

lib/web/cache/cachestorage.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CacheStorage {
2020

2121
async match (request, options = {}) {
2222
webidl.brandCheck(this, CacheStorage)
23-
webidl.argumentLengthCheck(arguments, 1, 'CacheStorage.match')
23+
webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.match' })
2424

2525
request = webidl.converters.RequestInfo(request)
2626
options = webidl.converters.MultiCacheQueryOptions(options)
@@ -57,11 +57,9 @@ class CacheStorage {
5757
*/
5858
async has (cacheName) {
5959
webidl.brandCheck(this, CacheStorage)
60+
webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.has' })
6061

61-
const prefix = 'CacheStorage.has'
62-
webidl.argumentLengthCheck(arguments, 1, prefix)
63-
64-
cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
62+
cacheName = webidl.converters.DOMString(cacheName)
6563

6664
// 2.1.1
6765
// 2.2
@@ -75,11 +73,9 @@ class CacheStorage {
7573
*/
7674
async open (cacheName) {
7775
webidl.brandCheck(this, CacheStorage)
76+
webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.open' })
7877

79-
const prefix = 'CacheStorage.open'
80-
webidl.argumentLengthCheck(arguments, 1, prefix)
81-
82-
cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
78+
cacheName = webidl.converters.DOMString(cacheName)
8379

8480
// 2.1
8581
if (this.#caches.has(cacheName)) {
@@ -109,11 +105,9 @@ class CacheStorage {
109105
*/
110106
async delete (cacheName) {
111107
webidl.brandCheck(this, CacheStorage)
108+
webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.delete' })
112109

113-
const prefix = 'CacheStorage.delete'
114-
webidl.argumentLengthCheck(arguments, 1, prefix)
115-
116-
cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
110+
cacheName = webidl.converters.DOMString(cacheName)
117111

118112
return this.#caches.delete(cacheName)
119113
}

lib/web/cache/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const assert = require('node:assert')
4-
const { URLSerializer } = require('../fetch/data-url')
4+
const { URLSerializer } = require('../fetch/dataURL')
55
const { isValidHeaderName } = require('../fetch/util')
66

77
/**

0 commit comments

Comments
 (0)