Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(primitives): remove fake native code #53

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 10 additions & 50 deletions packages/primitives/src/primitives/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})()
18 changes: 0 additions & 18 deletions packages/primitives/src/primitives/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
33 changes: 32 additions & 1 deletion packages/primitives/tests/cache.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down