11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { kConstruct } = require('./symbols')
41cb0ef41Sopenharmony_ciconst { Cache } = require('./cache')
51cb0ef41Sopenharmony_ciconst { webidl } = require('../fetch/webidl')
61cb0ef41Sopenharmony_ciconst { kEnumerableProperty } = require('../core/util')
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciclass CacheStorage {
91cb0ef41Sopenharmony_ci  /**
101cb0ef41Sopenharmony_ci   * @see https://w3c.github.io/ServiceWorker/#dfn-relevant-name-to-cache-map
111cb0ef41Sopenharmony_ci   * @type {Map<string, import('./cache').requestResponseList}
121cb0ef41Sopenharmony_ci   */
131cb0ef41Sopenharmony_ci  #caches = new Map()
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  constructor () {
161cb0ef41Sopenharmony_ci    if (arguments[0] !== kConstruct) {
171cb0ef41Sopenharmony_ci      webidl.illegalConstructor()
181cb0ef41Sopenharmony_ci    }
191cb0ef41Sopenharmony_ci  }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  async match (request, options = {}) {
221cb0ef41Sopenharmony_ci    webidl.brandCheck(this, CacheStorage)
231cb0ef41Sopenharmony_ci    webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.match' })
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    request = webidl.converters.RequestInfo(request)
261cb0ef41Sopenharmony_ci    options = webidl.converters.MultiCacheQueryOptions(options)
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    // 1.
291cb0ef41Sopenharmony_ci    if (options.cacheName != null) {
301cb0ef41Sopenharmony_ci      // 1.1.1.1
311cb0ef41Sopenharmony_ci      if (this.#caches.has(options.cacheName)) {
321cb0ef41Sopenharmony_ci        // 1.1.1.1.1
331cb0ef41Sopenharmony_ci        const cacheList = this.#caches.get(options.cacheName)
341cb0ef41Sopenharmony_ci        const cache = new Cache(kConstruct, cacheList)
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci        return await cache.match(request, options)
371cb0ef41Sopenharmony_ci      }
381cb0ef41Sopenharmony_ci    } else { // 2.
391cb0ef41Sopenharmony_ci      // 2.2
401cb0ef41Sopenharmony_ci      for (const cacheList of this.#caches.values()) {
411cb0ef41Sopenharmony_ci        const cache = new Cache(kConstruct, cacheList)
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci        // 2.2.1.2
441cb0ef41Sopenharmony_ci        const response = await cache.match(request, options)
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci        if (response !== undefined) {
471cb0ef41Sopenharmony_ci          return response
481cb0ef41Sopenharmony_ci        }
491cb0ef41Sopenharmony_ci      }
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  /**
541cb0ef41Sopenharmony_ci   * @see https://w3c.github.io/ServiceWorker/#cache-storage-has
551cb0ef41Sopenharmony_ci   * @param {string} cacheName
561cb0ef41Sopenharmony_ci   * @returns {Promise<boolean>}
571cb0ef41Sopenharmony_ci   */
581cb0ef41Sopenharmony_ci  async has (cacheName) {
591cb0ef41Sopenharmony_ci    webidl.brandCheck(this, CacheStorage)
601cb0ef41Sopenharmony_ci    webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.has' })
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    cacheName = webidl.converters.DOMString(cacheName)
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    // 2.1.1
651cb0ef41Sopenharmony_ci    // 2.2
661cb0ef41Sopenharmony_ci    return this.#caches.has(cacheName)
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  /**
701cb0ef41Sopenharmony_ci   * @see https://w3c.github.io/ServiceWorker/#dom-cachestorage-open
711cb0ef41Sopenharmony_ci   * @param {string} cacheName
721cb0ef41Sopenharmony_ci   * @returns {Promise<Cache>}
731cb0ef41Sopenharmony_ci   */
741cb0ef41Sopenharmony_ci  async open (cacheName) {
751cb0ef41Sopenharmony_ci    webidl.brandCheck(this, CacheStorage)
761cb0ef41Sopenharmony_ci    webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.open' })
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    cacheName = webidl.converters.DOMString(cacheName)
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    // 2.1
811cb0ef41Sopenharmony_ci    if (this.#caches.has(cacheName)) {
821cb0ef41Sopenharmony_ci      // await caches.open('v1') !== await caches.open('v1')
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci      // 2.1.1
851cb0ef41Sopenharmony_ci      const cache = this.#caches.get(cacheName)
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci      // 2.1.1.1
881cb0ef41Sopenharmony_ci      return new Cache(kConstruct, cache)
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    // 2.2
921cb0ef41Sopenharmony_ci    const cache = []
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci    // 2.3
951cb0ef41Sopenharmony_ci    this.#caches.set(cacheName, cache)
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci    // 2.4
981cb0ef41Sopenharmony_ci    return new Cache(kConstruct, cache)
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  /**
1021cb0ef41Sopenharmony_ci   * @see https://w3c.github.io/ServiceWorker/#cache-storage-delete
1031cb0ef41Sopenharmony_ci   * @param {string} cacheName
1041cb0ef41Sopenharmony_ci   * @returns {Promise<boolean>}
1051cb0ef41Sopenharmony_ci   */
1061cb0ef41Sopenharmony_ci  async delete (cacheName) {
1071cb0ef41Sopenharmony_ci    webidl.brandCheck(this, CacheStorage)
1081cb0ef41Sopenharmony_ci    webidl.argumentLengthCheck(arguments, 1, { header: 'CacheStorage.delete' })
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci    cacheName = webidl.converters.DOMString(cacheName)
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci    return this.#caches.delete(cacheName)
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  /**
1161cb0ef41Sopenharmony_ci   * @see https://w3c.github.io/ServiceWorker/#cache-storage-keys
1171cb0ef41Sopenharmony_ci   * @returns {string[]}
1181cb0ef41Sopenharmony_ci   */
1191cb0ef41Sopenharmony_ci  async keys () {
1201cb0ef41Sopenharmony_ci    webidl.brandCheck(this, CacheStorage)
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci    // 2.1
1231cb0ef41Sopenharmony_ci    const keys = this.#caches.keys()
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    // 2.2
1261cb0ef41Sopenharmony_ci    return [...keys]
1271cb0ef41Sopenharmony_ci  }
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ciObject.defineProperties(CacheStorage.prototype, {
1311cb0ef41Sopenharmony_ci  [Symbol.toStringTag]: {
1321cb0ef41Sopenharmony_ci    value: 'CacheStorage',
1331cb0ef41Sopenharmony_ci    configurable: true
1341cb0ef41Sopenharmony_ci  },
1351cb0ef41Sopenharmony_ci  match: kEnumerableProperty,
1361cb0ef41Sopenharmony_ci  has: kEnumerableProperty,
1371cb0ef41Sopenharmony_ci  open: kEnumerableProperty,
1381cb0ef41Sopenharmony_ci  delete: kEnumerableProperty,
1391cb0ef41Sopenharmony_ci  keys: kEnumerableProperty
1401cb0ef41Sopenharmony_ci})
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_cimodule.exports = {
1431cb0ef41Sopenharmony_ci  CacheStorage
1441cb0ef41Sopenharmony_ci}
145