| 'use strict' |
|
|
| const { kConstruct } = require('./symbols') |
| const { urlEquals, fieldValues: getFieldValues } = require('./util') |
| const { kEnumerableProperty, isDisturbed } = require('../core/util') |
| const { kHeadersList } = require('../core/symbols') |
| const { webidl } = require('../fetch/webidl') |
| const { Response, cloneResponse } = require('../fetch/response') |
| const { Request } = require('../fetch/request') |
| const { kState, kHeaders, kGuard, kRealm } = require('../fetch/symbols') |
| const { fetching } = require('../fetch/index') |
| const { urlIsHttpHttpsScheme, createDeferredPromise, readAllBytes } = require('../fetch/util') |
| const assert = require('assert') |
| const { getGlobalDispatcher } = require('../global') |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| class Cache { |
| |
| |
| |
| |
| #relevantRequestResponseList |
|
|
| constructor () { |
| if (arguments[0] !== kConstruct) { |
| webidl.illegalConstructor() |
| } |
|
|
| this.#relevantRequestResponseList = arguments[1] |
| } |
|
|
| async match (request, options = {}) { |
| webidl.brandCheck(this, Cache) |
| webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.match' }) |
|
|
| request = webidl.converters.RequestInfo(request) |
| options = webidl.converters.CacheQueryOptions(options) |
|
|
| const p = await this.matchAll(request, options) |
|
|
| if (p.length === 0) { |
| return |
| } |
|
|
| return p[0] |
| } |
|
|
| async matchAll (request = undefined, options = {}) { |
| webidl.brandCheck(this, Cache) |
|
|
| if (request !== undefined) request = webidl.converters.RequestInfo(request) |
| options = webidl.converters.CacheQueryOptions(options) |
|
|
| |
| let r = null |
|
|
| |
| if (request !== undefined) { |
| if (request instanceof Request) { |
| |
| r = request[kState] |
|
|
| |
| if (r.method !== 'GET' && !options.ignoreMethod) { |
| return [] |
| } |
| } else if (typeof request === 'string') { |
| |
| r = new Request(request)[kState] |
| } |
| } |
|
|
| |
| |
| const responses = [] |
|
|
| |
| if (request === undefined) { |
| |
| for (const requestResponse of this.#relevantRequestResponseList) { |
| responses.push(requestResponse[1]) |
| } |
| } else { |
| |
| const requestResponses = this.#queryCache(r, options) |
|
|
| |
| for (const requestResponse of requestResponses) { |
| responses.push(requestResponse[1]) |
| } |
| } |
|
|
| |
| |
|
|
| |
| const responseList = [] |
|
|
| |
| for (const response of responses) { |
| |
| const responseObject = new Response(response.body?.source ?? null) |
| const body = responseObject[kState].body |
| responseObject[kState] = response |
| responseObject[kState].body = body |
| responseObject[kHeaders][kHeadersList] = response.headersList |
| responseObject[kHeaders][kGuard] = 'immutable' |
|
|
| responseList.push(responseObject) |
| } |
|
|
| |
| return Object.freeze(responseList) |
| } |
|
|
| async add (request) { |
| webidl.brandCheck(this, Cache) |
| webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.add' }) |
|
|
| request = webidl.converters.RequestInfo(request) |
|
|
| |
| const requests = [request] |
|
|
| |
| const responseArrayPromise = this.addAll(requests) |
|
|
| |
| return await responseArrayPromise |
| } |
|
|
| async addAll (requests) { |
| webidl.brandCheck(this, Cache) |
| webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.addAll' }) |
|
|
| requests = webidl.converters['sequence<RequestInfo>'](requests) |
|
|
| |
| const responsePromises = [] |
|
|
| |
| const requestList = [] |
|
|
| |
| for (const request of requests) { |
| if (typeof request === 'string') { |
| continue |
| } |
|
|
| |
| const r = request[kState] |
|
|
| |
| if (!urlIsHttpHttpsScheme(r.url) || r.method !== 'GET') { |
| throw webidl.errors.exception({ |
| header: 'Cache.addAll', |
| message: 'Expected http/s scheme when method is not GET.' |
| }) |
| } |
| } |
|
|
| |
| |
| const fetchControllers = [] |
|
|
| |
| for (const request of requests) { |
| |
| const r = new Request(request)[kState] |
|
|
| |
| if (!urlIsHttpHttpsScheme(r.url)) { |
| throw webidl.errors.exception({ |
| header: 'Cache.addAll', |
| message: 'Expected http/s scheme.' |
| }) |
| } |
|
|
| |
| r.initiator = 'fetch' |
| r.destination = 'subresource' |
|
|
| |
| requestList.push(r) |
|
|
| |
| const responsePromise = createDeferredPromise() |
|
|
| |
| fetchControllers.push(fetching({ |
| request: r, |
| dispatcher: getGlobalDispatcher(), |
| processResponse (response) { |
| |
| if (response.type === 'error' || response.status === 206 || response.status < 200 || response.status > 299) { |
| responsePromise.reject(webidl.errors.exception({ |
| header: 'Cache.addAll', |
| message: 'Received an invalid status code or the request failed.' |
| })) |
| } else if (response.headersList.contains('vary')) { |
| |
| const fieldValues = getFieldValues(response.headersList.get('vary')) |
|
|
| |
| for (const fieldValue of fieldValues) { |
| |
| if (fieldValue === '*') { |
| responsePromise.reject(webidl.errors.exception({ |
| header: 'Cache.addAll', |
| message: 'invalid vary field value' |
| })) |
|
|
| for (const controller of fetchControllers) { |
| controller.abort() |
| } |
|
|
| return |
| } |
| } |
| } |
| }, |
| processResponseEndOfBody (response) { |
| |
| if (response.aborted) { |
| responsePromise.reject(new DOMException('aborted', 'AbortError')) |
| return |
| } |
|
|
| |
| responsePromise.resolve(response) |
| } |
| })) |
|
|
| |
| responsePromises.push(responsePromise.promise) |
| } |
|
|
| |
| const p = Promise.all(responsePromises) |
|
|
| |
| const responses = await p |
|
|
| |
| const operations = [] |
|
|
| |
| let index = 0 |
|
|
| |
| for (const response of responses) { |
| |
| |
| const operation = { |
| type: 'put', |
| request: requestList[index], |
| response |
| } |
|
|
| operations.push(operation) |
|
|
| index++ |
| } |
|
|
| |
| const cacheJobPromise = createDeferredPromise() |
|
|
| |
| let errorData = null |
|
|
| |
| try { |
| this.#batchCacheOperations(operations) |
| } catch (e) { |
| errorData = e |
| } |
|
|
| |
| queueMicrotask(() => { |
| |
| if (errorData === null) { |
| cacheJobPromise.resolve(undefined) |
| } else { |
| |
| cacheJobPromise.reject(errorData) |
| } |
| }) |
|
|
| |
| return cacheJobPromise.promise |
| } |
|
|
| async put (request, response) { |
| webidl.brandCheck(this, Cache) |
| webidl.argumentLengthCheck(arguments, 2, { header: 'Cache.put' }) |
|
|
| request = webidl.converters.RequestInfo(request) |
| response = webidl.converters.Response(response) |
|
|
| |
| let innerRequest = null |
|
|
| |
| if (request instanceof Request) { |
| innerRequest = request[kState] |
| } else { |
| innerRequest = new Request(request)[kState] |
| } |
|
|
| |
| if (!urlIsHttpHttpsScheme(innerRequest.url) || innerRequest.method !== 'GET') { |
| throw webidl.errors.exception({ |
| header: 'Cache.put', |
| message: 'Expected an http/s scheme when method is not GET' |
| }) |
| } |
|
|
| |
| const innerResponse = response[kState] |
|
|
| |
| if (innerResponse.status === 206) { |
| throw webidl.errors.exception({ |
| header: 'Cache.put', |
| message: 'Got 206 status' |
| }) |
| } |
|
|
| |
| if (innerResponse.headersList.contains('vary')) { |
| |
| const fieldValues = getFieldValues(innerResponse.headersList.get('vary')) |
|
|
| |
| for (const fieldValue of fieldValues) { |
| |
| if (fieldValue === '*') { |
| throw webidl.errors.exception({ |
| header: 'Cache.put', |
| message: 'Got * vary field value' |
| }) |
| } |
| } |
| } |
|
|
| |
| if (innerResponse.body && (isDisturbed(innerResponse.body.stream) || innerResponse.body.stream.locked)) { |
| throw webidl.errors.exception({ |
| header: 'Cache.put', |
| message: 'Response body is locked or disturbed' |
| }) |
| } |
|
|
| |
| const clonedResponse = cloneResponse(innerResponse) |
|
|
| |
| const bodyReadPromise = createDeferredPromise() |
|
|
| |
| if (innerResponse.body != null) { |
| |
| const stream = innerResponse.body.stream |
|
|
| |
| const reader = stream.getReader() |
|
|
| |
| readAllBytes(reader).then(bodyReadPromise.resolve, bodyReadPromise.reject) |
| } else { |
| bodyReadPromise.resolve(undefined) |
| } |
|
|
| |
| |
| const operations = [] |
|
|
| |
| |
| const operation = { |
| type: 'put', |
| request: innerRequest, |
| response: clonedResponse |
| } |
|
|
| |
| operations.push(operation) |
|
|
| |
| const bytes = await bodyReadPromise.promise |
|
|
| if (clonedResponse.body != null) { |
| clonedResponse.body.source = bytes |
| } |
|
|
| |
| const cacheJobPromise = createDeferredPromise() |
|
|
| |
| let errorData = null |
|
|
| |
| try { |
| this.#batchCacheOperations(operations) |
| } catch (e) { |
| errorData = e |
| } |
|
|
| |
| queueMicrotask(() => { |
| |
| if (errorData === null) { |
| cacheJobPromise.resolve() |
| } else { |
| cacheJobPromise.reject(errorData) |
| } |
| }) |
|
|
| return cacheJobPromise.promise |
| } |
|
|
| async delete (request, options = {}) { |
| webidl.brandCheck(this, Cache) |
| webidl.argumentLengthCheck(arguments, 1, { header: 'Cache.delete' }) |
|
|
| request = webidl.converters.RequestInfo(request) |
| options = webidl.converters.CacheQueryOptions(options) |
|
|
| |
| |
| |
| let r = null |
|
|
| if (request instanceof Request) { |
| r = request[kState] |
|
|
| if (r.method !== 'GET' && !options.ignoreMethod) { |
| return false |
| } |
| } else { |
| assert(typeof request === 'string') |
|
|
| r = new Request(request)[kState] |
| } |
|
|
| |
| const operations = [] |
|
|
| |
| const operation = { |
| type: 'delete', |
| request: r, |
| options |
| } |
|
|
| operations.push(operation) |
|
|
| const cacheJobPromise = createDeferredPromise() |
|
|
| let errorData = null |
| let requestResponses |
|
|
| try { |
| requestResponses = this.#batchCacheOperations(operations) |
| } catch (e) { |
| errorData = e |
| } |
|
|
| queueMicrotask(() => { |
| if (errorData === null) { |
| cacheJobPromise.resolve(!!requestResponses?.length) |
| } else { |
| cacheJobPromise.reject(errorData) |
| } |
| }) |
|
|
| return cacheJobPromise.promise |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async keys (request = undefined, options = {}) { |
| webidl.brandCheck(this, Cache) |
|
|
| if (request !== undefined) request = webidl.converters.RequestInfo(request) |
| options = webidl.converters.CacheQueryOptions(options) |
|
|
| |
| let r = null |
|
|
| |
| if (request !== undefined) { |
| |
| if (request instanceof Request) { |
| |
| r = request[kState] |
|
|
| |
| if (r.method !== 'GET' && !options.ignoreMethod) { |
| return [] |
| } |
| } else if (typeof request === 'string') { |
| r = new Request(request)[kState] |
| } |
| } |
|
|
| |
| const promise = createDeferredPromise() |
|
|
| |
| |
| const requests = [] |
|
|
| |
| if (request === undefined) { |
| |
| for (const requestResponse of this.#relevantRequestResponseList) { |
| |
| requests.push(requestResponse[0]) |
| } |
| } else { |
| |
| const requestResponses = this.#queryCache(r, options) |
|
|
| |
| for (const requestResponse of requestResponses) { |
| |
| requests.push(requestResponse[0]) |
| } |
| } |
|
|
| |
| queueMicrotask(() => { |
| |
| const requestList = [] |
|
|
| |
| for (const request of requests) { |
| const requestObject = new Request('https://a') |
| requestObject[kState] = request |
| requestObject[kHeaders][kHeadersList] = request.headersList |
| requestObject[kHeaders][kGuard] = 'immutable' |
| requestObject[kRealm] = request.client |
|
|
| |
| requestList.push(requestObject) |
| } |
|
|
| |
| promise.resolve(Object.freeze(requestList)) |
| }) |
|
|
| return promise.promise |
| } |
|
|
| |
| |
| |
| |
| |
| #batchCacheOperations (operations) { |
| |
| const cache = this.#relevantRequestResponseList |
|
|
| |
| const backupCache = [...cache] |
|
|
| |
| const addedItems = [] |
|
|
| |
| const resultList = [] |
|
|
| try { |
| |
| for (const operation of operations) { |
| |
| if (operation.type !== 'delete' && operation.type !== 'put') { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'operation type does not match "delete" or "put"' |
| }) |
| } |
|
|
| |
| if (operation.type === 'delete' && operation.response != null) { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'delete operation should not have an associated response' |
| }) |
| } |
|
|
| |
| if (this.#queryCache(operation.request, operation.options, addedItems).length) { |
| throw new DOMException('???', 'InvalidStateError') |
| } |
|
|
| |
| let requestResponses |
|
|
| |
| if (operation.type === 'delete') { |
| |
| requestResponses = this.#queryCache(operation.request, operation.options) |
|
|
| |
| if (requestResponses.length === 0) { |
| return [] |
| } |
|
|
| |
| for (const requestResponse of requestResponses) { |
| const idx = cache.indexOf(requestResponse) |
| assert(idx !== -1) |
|
|
| |
| cache.splice(idx, 1) |
| } |
| } else if (operation.type === 'put') { |
| |
| if (operation.response == null) { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'put operation should have an associated response' |
| }) |
| } |
|
|
| |
| const r = operation.request |
|
|
| |
| if (!urlIsHttpHttpsScheme(r.url)) { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'expected http or https scheme' |
| }) |
| } |
|
|
| |
| if (r.method !== 'GET') { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'not get method' |
| }) |
| } |
|
|
| |
| if (operation.options != null) { |
| throw webidl.errors.exception({ |
| header: 'Cache.#batchCacheOperations', |
| message: 'options must not be defined' |
| }) |
| } |
|
|
| |
| requestResponses = this.#queryCache(operation.request) |
|
|
| |
| for (const requestResponse of requestResponses) { |
| const idx = cache.indexOf(requestResponse) |
| assert(idx !== -1) |
|
|
| |
| cache.splice(idx, 1) |
| } |
|
|
| |
| cache.push([operation.request, operation.response]) |
|
|
| |
| addedItems.push([operation.request, operation.response]) |
| } |
|
|
| |
| resultList.push([operation.request, operation.response]) |
| } |
|
|
| |
| return resultList |
| } catch (e) { |
| |
| this.#relevantRequestResponseList.length = 0 |
|
|
| |
| this.#relevantRequestResponseList = backupCache |
|
|
| |
| throw e |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| #queryCache (requestQuery, options, targetStorage) { |
| |
| const resultList = [] |
|
|
| const storage = targetStorage ?? this.#relevantRequestResponseList |
|
|
| for (const requestResponse of storage) { |
| const [cachedRequest, cachedResponse] = requestResponse |
| if (this.#requestMatchesCachedItem(requestQuery, cachedRequest, cachedResponse, options)) { |
| resultList.push(requestResponse) |
| } |
| } |
|
|
| return resultList |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| #requestMatchesCachedItem (requestQuery, request, response = null, options) { |
| |
| |
| |
|
|
| const queryURL = new URL(requestQuery.url) |
|
|
| const cachedURL = new URL(request.url) |
|
|
| if (options?.ignoreSearch) { |
| cachedURL.search = '' |
|
|
| queryURL.search = '' |
| } |
|
|
| if (!urlEquals(queryURL, cachedURL, true)) { |
| return false |
| } |
|
|
| if ( |
| response == null || |
| options?.ignoreVary || |
| !response.headersList.contains('vary') |
| ) { |
| return true |
| } |
|
|
| const fieldValues = getFieldValues(response.headersList.get('vary')) |
|
|
| for (const fieldValue of fieldValues) { |
| if (fieldValue === '*') { |
| return false |
| } |
|
|
| const requestValue = request.headersList.get(fieldValue) |
| const queryValue = requestQuery.headersList.get(fieldValue) |
|
|
| |
| |
| if (requestValue !== queryValue) { |
| return false |
| } |
| } |
|
|
| return true |
| } |
| } |
|
|
| Object.defineProperties(Cache.prototype, { |
| [Symbol.toStringTag]: { |
| value: 'Cache', |
| configurable: true |
| }, |
| match: kEnumerableProperty, |
| matchAll: kEnumerableProperty, |
| add: kEnumerableProperty, |
| addAll: kEnumerableProperty, |
| put: kEnumerableProperty, |
| delete: kEnumerableProperty, |
| keys: kEnumerableProperty |
| }) |
|
|
| const cacheQueryOptionConverters = [ |
| { |
| key: 'ignoreSearch', |
| converter: webidl.converters.boolean, |
| defaultValue: false |
| }, |
| { |
| key: 'ignoreMethod', |
| converter: webidl.converters.boolean, |
| defaultValue: false |
| }, |
| { |
| key: 'ignoreVary', |
| converter: webidl.converters.boolean, |
| defaultValue: false |
| } |
| ] |
|
|
| webidl.converters.CacheQueryOptions = webidl.dictionaryConverter(cacheQueryOptionConverters) |
|
|
| webidl.converters.MultiCacheQueryOptions = webidl.dictionaryConverter([ |
| ...cacheQueryOptionConverters, |
| { |
| key: 'cacheName', |
| converter: webidl.converters.DOMString |
| } |
| ]) |
|
|
| webidl.converters.Response = webidl.interfaceConverter(Response) |
|
|
| webidl.converters['sequence<RequestInfo>'] = webidl.sequenceConverter( |
| webidl.converters.RequestInfo |
| ) |
|
|
| module.exports = { |
| Cache |
| } |
|
|