File size: 4,618 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
import DefaultCacheHandler from '../lib/cache-handlers/default.external'
import type { CacheHandlerCompat } from '../lib/cache-handlers/types'
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
? (message: string, ...args: any[]) => {
console.log(`use-cache: ${message}`, ...args)
}
: undefined
const handlersSymbol = Symbol.for('@next/cache-handlers')
const handlersMapSymbol = Symbol.for('@next/cache-handlers-map')
const handlersSetSymbol = Symbol.for('@next/cache-handlers-set')
/**
* The reference to the cache handlers. We store the cache handlers on the
* global object so that we can access the same instance across different
* boundaries (such as different copies of the same module).
*/
const reference: typeof globalThis & {
[handlersSymbol]?: {
RemoteCache?: CacheHandlerCompat
DefaultCache?: CacheHandlerCompat
}
[handlersMapSymbol]?: Map<string, CacheHandlerCompat>
[handlersSetSymbol]?: Set<CacheHandlerCompat>
} = globalThis
/**
* Initialize the cache handlers.
* @returns `true` if the cache handlers were initialized, `false` if they were already initialized.
*/
export function initializeCacheHandlers(): boolean {
// If the cache handlers have already been initialized, don't do it again.
if (reference[handlersMapSymbol]) {
debug?.('cache handlers already initialized')
return false
}
debug?.('initializing cache handlers')
reference[handlersMapSymbol] = new Map<string, CacheHandlerCompat>()
// Initialize the cache from the symbol contents first.
if (reference[handlersSymbol]) {
let fallback: CacheHandlerCompat
if (reference[handlersSymbol].DefaultCache) {
debug?.('setting "default" cache handler from symbol')
fallback = reference[handlersSymbol].DefaultCache
} else {
debug?.('setting "default" cache handler from default')
fallback = DefaultCacheHandler
}
reference[handlersMapSymbol].set('default', fallback)
if (reference[handlersSymbol].RemoteCache) {
debug?.('setting "remote" cache handler from symbol')
reference[handlersMapSymbol].set(
'remote',
reference[handlersSymbol].RemoteCache
)
} else {
debug?.('setting "remote" cache handler from default')
reference[handlersMapSymbol].set('remote', fallback)
}
} else {
debug?.('setting "default" cache handler from default')
reference[handlersMapSymbol].set('default', DefaultCacheHandler)
debug?.('setting "remote" cache handler from default')
reference[handlersMapSymbol].set('remote', DefaultCacheHandler)
}
// Create a set of the cache handlers.
reference[handlersSetSymbol] = new Set(reference[handlersMapSymbol].values())
return true
}
/**
* Get a cache handler by kind.
* @param kind - The kind of cache handler to get.
* @returns The cache handler, or `undefined` if it does not exist.
* @throws If the cache handlers are not initialized.
*/
export function getCacheHandler(kind: string): CacheHandlerCompat | undefined {
// This should never be called before initializeCacheHandlers.
if (!reference[handlersMapSymbol]) {
throw new Error('Cache handlers not initialized')
}
return reference[handlersMapSymbol].get(kind)
}
/**
* Get a set iterator over the cache handlers.
* @returns An iterator over the cache handlers, or `undefined` if they are not
* initialized.
*/
export function getCacheHandlers():
| SetIterator<CacheHandlerCompat>
| undefined {
if (!reference[handlersSetSymbol]) {
return undefined
}
return reference[handlersSetSymbol].values()
}
/**
* Get a map iterator over the cache handlers (keyed by kind).
* @returns An iterator over the cache handler entries, or `undefined` if they
* are not initialized.
* @throws If the cache handlers are not initialized.
*/
export function getCacheHandlerEntries():
| MapIterator<[string, CacheHandlerCompat]>
| undefined {
if (!reference[handlersMapSymbol]) {
return undefined
}
return reference[handlersMapSymbol].entries()
}
/**
* Set a cache handler by kind.
* @param kind - The kind of cache handler to set.
* @param cacheHandler - The cache handler to set.
*/
export function setCacheHandler(
kind: string,
cacheHandler: CacheHandlerCompat
): void {
// This should never be called before initializeCacheHandlers.
if (!reference[handlersMapSymbol] || !reference[handlersSetSymbol]) {
throw new Error('Cache handlers not initialized')
}
debug?.('setting cache handler for "%s"', kind)
reference[handlersMapSymbol].set(kind, cacheHandler)
reference[handlersSetSymbol].add(cacheHandler)
}
|