diff --git a/packages/primitives/src/primitives/cache.js b/packages/primitives/src/primitives/cache.js index 5e7c89b0..f2fc5c4b 100644 --- a/packages/primitives/src/primitives/cache.js +++ b/packages/primitives/src/primitives/cache.js @@ -221,59 +221,19 @@ export function createCaches() { return { Cache, cacheStorage } } -function CacheStorage() { - if (!(this instanceof CacheStorage)) return new CacheStorage() - throw TypeError('Illegal constructor') -} - -function CacheStorageToString() { - return 'function CacheStorage() { [native code] }' -} - -Object.defineProperty(CacheStorageToString, 'name', { - configurable: true, - enumerable: false, - value: 'toString() { [native code] }', - writable: true, -}) - -Object.defineProperty(CacheStorage, 'toString', { - configurable: true, - enumerable: false, - value: CacheStorageToString, - writable: true, -}) - -/** - * @type Cache - */ -function Cache() { +export function Cache() { if (!(this instanceof Cache)) return new Cache() throw TypeError('Illegal constructor') } -function CacheToString() { - return 'function Cache() { [native code] }' +export function CacheStorage() { + if (!(this instanceof CacheStorage)) return new CacheStorage() + throw TypeError('Illegal constructor') } -Object.defineProperty(CacheToString, 'name', { - configurable: true, - enumerable: false, - value: 'toString() { [native code] }', - writable: true, -}) - -Object.defineProperty(Cache, 'toString', { - configurable: true, - enumerable: false, - value: CacheToString, - writable: true, -}) - -const cachesStorage = createCaches() -const CacheFromStorage = cachesStorage.Cache - -export const caches = cachesStorage.cacheStorage() - -export { CacheStorage } -export { CacheFromStorage as Cache } +export const caches = (() => { + const { cacheStorage } = createCaches() + const caches = cacheStorage() + caches.open('default') + return caches +})() diff --git a/packages/primitives/src/primitives/crypto.js b/packages/primitives/src/primitives/crypto.js index d5caac79..c412ad53 100644 --- a/packages/primitives/src/primitives/crypto.js +++ b/packages/primitives/src/primitives/crypto.js @@ -16,24 +16,6 @@ function SubtleCrypto() { throw TypeError('Illegal constructor') } -function SubtleCryptoToString() { - return 'function SubtleCrypto() { [native code] }' -} - -Object.defineProperty(SubtleCryptoToString, 'name', { - configurable: true, - enumerable: false, - value: 'toString() { [native code] }', - writable: true, -}) - -Object.defineProperty(SubtleCrypto, 'toString', { - configurable: true, - enumerable: false, - value: SubtleCryptoToString, - writable: true, -}) - export const crypto = new Crypto() export { Crypto } diff --git a/packages/primitives/tests/cache.test.ts b/packages/primitives/tests/cache.test.ts index 7aa9bcc8..66c268b3 100644 --- a/packages/primitives/tests/cache.test.ts +++ b/packages/primitives/tests/cache.test.ts @@ -1,8 +1,39 @@ import { fetch, Request, Response } from '../fetch' -import { createCaches } from '../cache' +import { + caches, + createCaches, + Cache as ICache, + CacheStorage as ICacheStorage, +} from '../cache' const { cacheStorage, Cache } = createCaches() +test('Cache is an interface', () => { + expect.assertions(2) + + try { + new ICache() + } catch (error: any) { + expect(error.name).toBe('TypeError') + expect(error.message).toBe('Illegal constructor') + } +}) + +test('CacheStorage is an interface', () => { + expect.assertions(2) + + try { + new ICacheStorage() + } catch (error: any) { + expect(error.name).toBe('TypeError') + expect(error.message).toBe('Illegal constructor') + } +}) + +test('the default caches object has a default namespace', async () => { + expect(Array.from(await caches.keys())).toEqual(['default']) +}) + test('caches.open', async () => { const caches = cacheStorage() const cache = await caches.open('my_cache')