cachestorage.js 3.4 KB

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