Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
cachestorage.js153 linesDownload Raw Back to cache
1'use strict'
2
3const { kConstruct } = require('./symbols')
4const { Cache } = require('./cache')
5const { webidl } = require('../fetch/webidl')
6const { kEnumerableProperty } = require('../../core/util')
7
8class CacheStorage {
9  /**
10   * @see https://w3c.github.io/ServiceWorker/#dfn-relevant-name-to-cache-map
11   * @type {Map<string, import('./cache').requestResponseList}
12   */
13  #caches = new Map()
14
15  constructor () {
16    if (arguments[0] !== kConstruct) {
17      webidl.illegalConstructor()
18    }
19
20    webidl.util.markAsUncloneable(this)
21  }
22
23  async match (request, options = {}) {
24    webidl.brandCheck(this, CacheStorage)
25    webidl.argumentLengthCheck(arguments, 1, 'CacheStorage.match')
26
27    request = webidl.converters.RequestInfo(request)
28    options = webidl.converters.MultiCacheQueryOptions(options)
29
30    // 1.
31    if (options.cacheName != null) {
32      // 1.1.1.1
33      if (this.#caches.has(options.cacheName)) {
34        // 1.1.1.1.1
35        const cacheList = this.#caches.get(options.cacheName)
36        const cache = new Cache(kConstruct, cacheList)
37
38        return await cache.match(request, options)
39      }
40    } else { // 2.
41      // 2.2
42      for (const cacheList of this.#caches.values()) {
43        const cache = new Cache(kConstruct, cacheList)
44
45        // 2.2.1.2
46        const response = await cache.match(request, options)
47
48        if (response !== undefined) {
49          return response
50        }
51      }
52    }
53  }
54
55  /**
56   * @see https://w3c.github.io/ServiceWorker/#cache-storage-has
57   * @param {string} cacheName
58   * @returns {Promise<boolean>}
59   */
60  async has (cacheName) {
61    webidl.brandCheck(this, CacheStorage)
62
63    const prefix = 'CacheStorage.has'
64    webidl.argumentLengthCheck(arguments, 1, prefix)
65
66    cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
67
68    // 2.1.1
69    // 2.2
70    return this.#caches.has(cacheName)
71  }
72
73  /**
74   * @see https://w3c.github.io/ServiceWorker/#dom-cachestorage-open
75   * @param {string} cacheName
76   * @returns {Promise<Cache>}
77   */
78  async open (cacheName) {
79    webidl.brandCheck(this, CacheStorage)
80
81    const prefix = 'CacheStorage.open'
82    webidl.argumentLengthCheck(arguments, 1, prefix)
83
84    cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
85
86    // 2.1
87    if (this.#caches.has(cacheName)) {
88      // await caches.open('v1') !== await caches.open('v1')
89
90      // 2.1.1
91      const cache = this.#caches.get(cacheName)
92
93      // 2.1.1.1
94      return new Cache(kConstruct, cache)
95    }
96
97    // 2.2
98    const cache = []
99
100    // 2.3
101    this.#caches.set(cacheName, cache)
102
103    // 2.4
104    return new Cache(kConstruct, cache)
105  }
106
107  /**
108   * @see https://w3c.github.io/ServiceWorker/#cache-storage-delete
109   * @param {string} cacheName
110   * @returns {Promise<boolean>}
111   */
112  async delete (cacheName) {
113    webidl.brandCheck(this, CacheStorage)
114
115    const prefix = 'CacheStorage.delete'
116    webidl.argumentLengthCheck(arguments, 1, prefix)
117
118    cacheName = webidl.converters.DOMString(cacheName, prefix, 'cacheName')
119
120    return this.#caches.delete(cacheName)
121  }
122
123  /**
124   * @see https://w3c.github.io/ServiceWorker/#cache-storage-keys
125   * @returns {Promise<string[]>}
126   */
127  async keys () {
128    webidl.brandCheck(this, CacheStorage)
129
130    // 2.1
131    const keys = this.#caches.keys()
132
133    // 2.2
134    return [...keys]
135  }
136}
137
138Object.defineProperties(CacheStorage.prototype, {
139  [Symbol.toStringTag]: {
140    value: 'CacheStorage',
141    configurable: true
142  },
143  match: kEnumerableProperty,
144  has: kEnumerableProperty,
145  open: kEnumerableProperty,
146  delete: kEnumerableProperty,
147  keys: kEnumerableProperty
148})
149
150module.exports = {
151  CacheStorage
152}
153 
codekingpro/portable-devtools · Team Ai