123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 'use strict'
- const { kConstruct } = require('./symbols')
- const { Cache } = require('./cache')
- const { webidl } = require('../fetch/webidl')
- const { kEnumerableProperty } = require('../core/util')
- class CacheStorage {
- /**
- * @see https://w3c.github.io/ServiceWorker/#dfn-relevant-name-to-cache-map
- * @type {Map<string, import('./cache').requestResponseList}
- */
- #caches = new Map()
- constructor () {
- if (arguments[0] !== kConstruct) {
- webidl.illegalConstructor()
- }
- }
- async match (request, options = {}) {
- webidl.brandCheck(this, CacheStorage)
- webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.match' })
- request = webidl.converters.RequestInfo(request)
- options = webidl.converters.MultiCacheQueryOptions(options)
- // 1.
- if (options.cacheName != null) {
- // 1.1.1.1
- if (this.#caches.has(options.cacheName)) {
- // 1.1.1.1.1
- const cacheList = this.#caches.get(options.cacheName)
- const cache = new Cache(kConstruct, cacheList)
- return await cache.match(request, options)
- }
- } else { // 2.
- // 2.2
- for (const cacheList of this.#caches.values()) {
- const cache = new Cache(kConstruct, cacheList)
- // 2.2.1.2
- const response = await cache.match(request, options)
- if (response !== undefined) {
- return response
- }
- }
- }
- }
- /**
- * @see https://w3c.github.io/ServiceWorker/#cache-storage-has
- * @param {string} cacheName
- * @returns {Promise<boolean>}
- */
- async has (cacheName) {
- webidl.brandCheck(this, CacheStorage)
- webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.has' })
- cacheName = webidl.converters.DOMString(cacheName)
- // 2.1.1
- // 2.2
- return this.#caches.has(cacheName)
- }
- /**
- * @see https://w3c.github.io/ServiceWorker/#dom-cachestorage-open
- * @param {string} cacheName
- * @returns {Promise<Cache>}
- */
- async open (cacheName) {
- webidl.brandCheck(this, CacheStorage)
- webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.open' })
- cacheName = webidl.converters.DOMString(cacheName)
- // 2.1
- if (this.#caches.has(cacheName)) {
- // await caches.open('v1') !== await caches.open('v1')
- // 2.1.1
- const cache = this.#caches.get(cacheName)
- // 2.1.1.1
- return new Cache(kConstruct, cache)
- }
- // 2.2
- const cache = []
- // 2.3
- this.#caches.set(cacheName, cache)
- // 2.4
- return new Cache(kConstruct, cache)
- }
- /**
- * @see https://w3c.github.io/ServiceWorker/#cache-storage-delete
- * @param {string} cacheName
- * @returns {Promise<boolean>}
- */
- async delete (cacheName) {
- webidl.brandCheck(this, CacheStorage)
- webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.delete' })
- cacheName = webidl.converters.DOMString(cacheName)
- return this.#caches.delete(cacheName)
- }
- /**
- * @see https://w3c.github.io/ServiceWorker/#cache-storage-keys
- * @returns {string[]}
- */
- async keys () {
- webidl.brandCheck(this, CacheStorage)
- // 2.1
- const keys = this.#caches.keys()
- // 2.2
- return [...keys]
- }
- }
- Object.defineProperties(CacheStorage.prototype, {
- [Symbol.toStringTag]: {
- value: 'CacheStorage',
- configurable: true
- },
- match: kEnumerableProperty,
- has: kEnumerableProperty,
- open: kEnumerableProperty,
- delete: kEnumerableProperty,
- keys: kEnumerableProperty
- })
- module.exports = {
- CacheStorage
- }
|