2024-03-31 15:49:19 +02:00
|
|
|
import * as nodeCrypto from 'node:crypto'
|
2023-10-10 14:32:10 +02:00
|
|
|
import { vi } from 'vitest'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto
|
|
|
|
*/
|
2024-03-31 15:49:19 +02:00
|
|
|
if (!globalThis.crypto) {
|
|
|
|
vi.stubGlobal('crypto', nodeCrypto)
|
|
|
|
vi.stubGlobal('CryptoKey', nodeCrypto.webcrypto.CryptoKey)
|
|
|
|
}
|
2023-10-10 14:32:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache API
|
|
|
|
*/
|
|
|
|
type StoreMap = Map<string | Request, Response>
|
|
|
|
|
|
|
|
class MockCache {
|
|
|
|
name: string
|
|
|
|
store: StoreMap
|
|
|
|
|
|
|
|
constructor(name: string, store: StoreMap) {
|
|
|
|
this.name = name
|
|
|
|
this.store = store
|
|
|
|
}
|
|
|
|
|
|
|
|
async match(key: Request | string): Promise<Response | null> {
|
|
|
|
return this.store.get(key) || null
|
|
|
|
}
|
|
|
|
|
2024-05-13 00:05:00 +02:00
|
|
|
async keys() {
|
|
|
|
return this.store.keys()
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:32:10 +02:00
|
|
|
async put(key: Request | string, response: Response): Promise<void> {
|
|
|
|
this.store.set(key, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalStore: Map<string | Request, Response> = new Map()
|
|
|
|
|
|
|
|
const caches = {
|
|
|
|
open: (name: string) => {
|
|
|
|
return new MockCache(name, globalStore)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
vi.stubGlobal('caches', caches)
|