cachestorage.js 3.6 KB

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