fix(ext/cache): illegal constructor (#16205)

This commit is contained in:
Marcos Casagrande 2022-10-16 19:33:17 +02:00 committed by GitHub
parent 07213dec94
commit 5252ff5dbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import {
assertEquals, assertEquals,
assertFalse, assertFalse,
assertRejects, assertRejects,
assertThrows,
} from "./test_util.ts"; } from "./test_util.ts";
Deno.test(async function cacheStorage() { Deno.test(async function cacheStorage() {
@ -95,6 +96,13 @@ Deno.test(async function cacheApi() {
assertFalse(await caches.has(cacheName)); assertFalse(await caches.has(cacheName));
}); });
Deno.test(function cacheIllegalConstructor() {
// @ts-expect-error illegal constructor
assertThrows(() => new Cache(), TypeError, "Illegal constructor");
// @ts-expect-error illegal constructor
assertThrows(() => new Cache("foo", "bar"), TypeError, "Illegal constructor");
});
Deno.test(async function cachePutReaderLock() { Deno.test(async function cachePutReaderLock() {
const cacheName = "cache-v1"; const cacheName = "cache-v1";
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);

17
ext/cache/01_cache.js vendored
View file

@ -33,7 +33,9 @@
context: "Argument 1", context: "Argument 1",
}); });
const cacheId = await core.opAsync("op_cache_storage_open", cacheName); const cacheId = await core.opAsync("op_cache_storage_open", cacheName);
return new Cache(cacheId); const cache = webidl.createBranded(Cache);
cache[_id] = cacheId;
return cache;
} }
async has(cacheName) { async has(cacheName) {
@ -59,18 +61,20 @@
} }
} }
const _matchAll = Symbol("[[matchAll]]");
const _id = Symbol("id"); const _id = Symbol("id");
class Cache { class Cache {
/** @type {number} */ /** @type {number} */
[_id]; [_id];
constructor(cacheId) { constructor() {
this[_id] = cacheId; webidl.illegalConstructor();
} }
/** See https://w3c.github.io/ServiceWorker/#dom-cache-put */ /** See https://w3c.github.io/ServiceWorker/#dom-cache-put */
async put(request, response) { async put(request, response) {
webidl.assertBranded(this, CachePrototype);
const prefix = "Failed to execute 'put' on 'Cache'"; const prefix = "Failed to execute 'put' on 'Cache'";
webidl.requiredArguments(arguments.length, 2, { prefix }); webidl.requiredArguments(arguments.length, 2, { prefix });
request = webidl.converters["RequestInfo_DOMString"](request, { request = webidl.converters["RequestInfo_DOMString"](request, {
@ -159,13 +163,14 @@
/** See https://w3c.github.io/ServiceWorker/#cache-match */ /** See https://w3c.github.io/ServiceWorker/#cache-match */
async match(request, options) { async match(request, options) {
webidl.assertBranded(this, CachePrototype);
const prefix = "Failed to execute 'match' on 'Cache'"; const prefix = "Failed to execute 'match' on 'Cache'";
webidl.requiredArguments(arguments.length, 1, { prefix }); webidl.requiredArguments(arguments.length, 1, { prefix });
request = webidl.converters["RequestInfo_DOMString"](request, { request = webidl.converters["RequestInfo_DOMString"](request, {
prefix, prefix,
context: "Argument 1", context: "Argument 1",
}); });
const p = await this.#matchAll(request, options); const p = await this[_matchAll](request, options);
if (p.length > 0) { if (p.length > 0) {
return p[0]; return p[0];
} else { } else {
@ -175,6 +180,7 @@
/** See https://w3c.github.io/ServiceWorker/#cache-delete */ /** See https://w3c.github.io/ServiceWorker/#cache-delete */
async delete(request, _options) { async delete(request, _options) {
webidl.assertBranded(this, CachePrototype);
const prefix = "Failed to execute 'delete' on 'Cache'"; const prefix = "Failed to execute 'delete' on 'Cache'";
webidl.requiredArguments(arguments.length, 1, { prefix }); webidl.requiredArguments(arguments.length, 1, { prefix });
request = webidl.converters["RequestInfo_DOMString"](request, { request = webidl.converters["RequestInfo_DOMString"](request, {
@ -208,7 +214,7 @@
* *
* The function will return an array of responses. * The function will return an array of responses.
*/ */
async #matchAll(request, _options) { async [_matchAll](request, _options) {
// Step 1. // Step 1.
let r = null; let r = null;
// Step 2. // Step 2.
@ -273,6 +279,7 @@
webidl.configurePrototype(CacheStorage); webidl.configurePrototype(CacheStorage);
webidl.configurePrototype(Cache); webidl.configurePrototype(Cache);
const CacheStoragePrototype = CacheStorage.prototype; const CacheStoragePrototype = CacheStorage.prototype;
const CachePrototype = Cache.prototype;
let cacheStorage; let cacheStorage;
window.__bootstrap.caches = { window.__bootstrap.caches = {

View file

@ -4562,8 +4562,6 @@
"NavigationPreloadManager interface: operation disable()", "NavigationPreloadManager interface: operation disable()",
"NavigationPreloadManager interface: operation setHeaderValue(ByteString)", "NavigationPreloadManager interface: operation setHeaderValue(ByteString)",
"NavigationPreloadManager interface: operation getState()", "NavigationPreloadManager interface: operation getState()",
"Cache interface: existence and properties of interface object",
"Cache interface object length",
"Cache interface: operation match(RequestInfo, optional CacheQueryOptions)", "Cache interface: operation match(RequestInfo, optional CacheQueryOptions)",
"Cache interface: operation matchAll(optional RequestInfo, optional CacheQueryOptions)", "Cache interface: operation matchAll(optional RequestInfo, optional CacheQueryOptions)",
"Cache interface: operation add(RequestInfo)", "Cache interface: operation add(RequestInfo)",
@ -4652,8 +4650,6 @@
"NavigationPreloadManager interface: operation disable()", "NavigationPreloadManager interface: operation disable()",
"NavigationPreloadManager interface: operation setHeaderValue(ByteString)", "NavigationPreloadManager interface: operation setHeaderValue(ByteString)",
"NavigationPreloadManager interface: operation getState()", "NavigationPreloadManager interface: operation getState()",
"Cache interface: existence and properties of interface object",
"Cache interface object length",
"Cache interface: operation match(RequestInfo, optional CacheQueryOptions)", "Cache interface: operation match(RequestInfo, optional CacheQueryOptions)",
"Cache interface: operation matchAll(optional RequestInfo, optional CacheQueryOptions)", "Cache interface: operation matchAll(optional RequestInfo, optional CacheQueryOptions)",
"Cache interface: operation add(RequestInfo)", "Cache interface: operation add(RequestInfo)",