| "use strict"; |
| Object.defineProperty(exports, "__esModule", { |
| value: true |
| }); |
| Object.defineProperty(exports, "cache", { |
| enumerable: true, |
| get: function() { |
| return cache; |
| } |
| }); |
| const _server = require("react-server-dom-webpack/server"); |
| const _client = require("react-server-dom-webpack/client"); |
| const _static = require("react-server-dom-webpack/static"); |
| const _workasyncstorageexternal = require("../app-render/work-async-storage.external"); |
| const _workunitasyncstorageexternal = require("../app-render/work-unit-async-storage.external"); |
| const _dynamicrenderingutils = require("../dynamic-rendering-utils"); |
| const _encryptionutils = require("../app-render/encryption-utils"); |
| const _encryption = require("../app-render/encryption"); |
| const _invarianterror = require("../../shared/lib/invariant-error"); |
| const _createerrorhandler = require("../app-render/create-error-handler"); |
| const _constants = require("./constants"); |
| const _handlers = require("./handlers"); |
| const _usecacheerrors = require("./use-cache-errors"); |
| const _dynamicrendering = require("../app-render/dynamic-rendering"); |
| const _searchparams = require("../request/search-params"); |
| const _react = _interop_require_default(require("react")); |
| const _lazyresult = require("../lib/lazy-result"); |
| const _dynamicaccessasyncstorageexternal = require("../app-render/dynamic-access-async-storage.external"); |
| const _reactlargeshellerror = require("../app-render/react-large-shell-error"); |
| function _interop_require_default(obj) { |
| return obj && obj.__esModule ? obj : { |
| default: obj |
| }; |
| } |
| const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'; |
| const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE ? console.debug.bind(console, 'use-cache:') : undefined; |
| const filterStackFrame = process.env.NODE_ENV !== 'production' ? require('../lib/source-maps').filterStackFrameDEV : undefined; |
| const findSourceMapURL = process.env.NODE_ENV !== 'production' ? require('../lib/source-maps').findSourceMapURLDEV : undefined; |
| function generateCacheEntry(workStore, cacheContext, clientReferenceManifest, encodedArguments, fn, sharedErrorStack) { |
| |
| |
| |
| |
| |
| return workStore.runInCleanSnapshot(generateCacheEntryWithRestoredWorkStore, workStore, cacheContext, clientReferenceManifest, encodedArguments, fn, sharedErrorStack); |
| } |
| function generateCacheEntryWithRestoredWorkStore(workStore, cacheContext, clientReferenceManifest, encodedArguments, fn, sharedErrorStack) { |
| |
| |
| |
| |
| |
| |
| |
| return _workasyncstorageexternal.workAsyncStorage.run(workStore, generateCacheEntryWithCacheContext, workStore, cacheContext, clientReferenceManifest, encodedArguments, fn, sharedErrorStack); |
| } |
| function createUseCacheStore(workStore, cacheContext, defaultCacheLife) { |
| if (cacheContext.kind === 'private') { |
| const outerWorkUnitStore = cacheContext.outerWorkUnitStore; |
| return { |
| type: 'private-cache', |
| phase: 'render', |
| implicitTags: outerWorkUnitStore == null ? void 0 : outerWorkUnitStore.implicitTags, |
| revalidate: defaultCacheLife.revalidate, |
| expire: defaultCacheLife.expire, |
| stale: defaultCacheLife.stale, |
| explicitRevalidate: undefined, |
| explicitExpire: undefined, |
| explicitStale: undefined, |
| tags: null, |
| hmrRefreshHash: (0, _workunitasyncstorageexternal.getHmrRefreshHash)(workStore, outerWorkUnitStore), |
| isHmrRefresh: (0, _workunitasyncstorageexternal.isHmrRefresh)(workStore, outerWorkUnitStore), |
| serverComponentsHmrCache: (0, _workunitasyncstorageexternal.getServerComponentsHmrCache)(workStore, outerWorkUnitStore), |
| forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore), |
| runtimeStagePromise: (0, _workunitasyncstorageexternal.getRuntimeStagePromise)(outerWorkUnitStore), |
| draftMode: (0, _workunitasyncstorageexternal.getDraftModeProviderForCacheScope)(workStore, outerWorkUnitStore), |
| rootParams: outerWorkUnitStore.rootParams, |
| cookies: outerWorkUnitStore.cookies |
| }; |
| } else { |
| let useCacheOrRequestStore; |
| const outerWorkUnitStore = cacheContext.outerWorkUnitStore; |
| if (outerWorkUnitStore) { |
| switch(outerWorkUnitStore == null ? void 0 : outerWorkUnitStore.type){ |
| case 'cache': |
| case 'private-cache': |
| case 'request': |
| useCacheOrRequestStore = outerWorkUnitStore; |
| break; |
| case 'prerender-runtime': |
| case 'prerender': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'unstable-cache': |
| break; |
| default: |
| outerWorkUnitStore; |
| } |
| } |
| return { |
| type: 'cache', |
| phase: 'render', |
| implicitTags: outerWorkUnitStore == null ? void 0 : outerWorkUnitStore.implicitTags, |
| revalidate: defaultCacheLife.revalidate, |
| expire: defaultCacheLife.expire, |
| stale: defaultCacheLife.stale, |
| explicitRevalidate: undefined, |
| explicitExpire: undefined, |
| explicitStale: undefined, |
| tags: null, |
| hmrRefreshHash: outerWorkUnitStore && (0, _workunitasyncstorageexternal.getHmrRefreshHash)(workStore, outerWorkUnitStore), |
| isHmrRefresh: (useCacheOrRequestStore == null ? void 0 : useCacheOrRequestStore.isHmrRefresh) ?? false, |
| serverComponentsHmrCache: useCacheOrRequestStore == null ? void 0 : useCacheOrRequestStore.serverComponentsHmrCache, |
| forceRevalidate: shouldForceRevalidate(workStore, outerWorkUnitStore), |
| draftMode: outerWorkUnitStore && (0, _workunitasyncstorageexternal.getDraftModeProviderForCacheScope)(workStore, outerWorkUnitStore) |
| }; |
| } |
| } |
| function assertDefaultCacheLife(defaultCacheLife) { |
| if (!defaultCacheLife || defaultCacheLife.revalidate == null || defaultCacheLife.expire == null || defaultCacheLife.stale == null) { |
| throw Object.defineProperty(new _invarianterror.InvariantError('A default cacheLife profile must always be provided.'), "__NEXT_ERROR_CODE", { |
| value: "E750", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| } |
| function generateCacheEntryWithCacheContext(workStore, cacheContext, clientReferenceManifest, encodedArguments, fn, sharedErrorStack) { |
| if (!workStore.cacheLifeProfiles) { |
| throw Object.defineProperty(new _invarianterror.InvariantError('cacheLifeProfiles should always be provided.'), "__NEXT_ERROR_CODE", { |
| value: "E748", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| const defaultCacheLife = workStore.cacheLifeProfiles['default']; |
| assertDefaultCacheLife(defaultCacheLife); |
| |
| const cacheStore = createUseCacheStore(workStore, cacheContext, defaultCacheLife); |
| return _workunitasyncstorageexternal.workUnitAsyncStorage.run(cacheStore, ()=>_dynamicaccessasyncstorageexternal.dynamicAccessAsyncStorage.run({ |
| abortController: new AbortController() |
| }, generateCacheEntryImpl, workStore, cacheContext, cacheStore, clientReferenceManifest, encodedArguments, fn, sharedErrorStack)); |
| } |
| function propagateCacheLifeAndTagsToRevalidateStore(revalidateStore, entry) { |
| const outerTags = revalidateStore.tags ??= []; |
| for (const tag of entry.tags){ |
| if (!outerTags.includes(tag)) { |
| outerTags.push(tag); |
| } |
| } |
| if (revalidateStore.stale > entry.stale) { |
| revalidateStore.stale = entry.stale; |
| } |
| if (revalidateStore.revalidate > entry.revalidate) { |
| revalidateStore.revalidate = entry.revalidate; |
| } |
| if (revalidateStore.expire > entry.expire) { |
| revalidateStore.expire = entry.expire; |
| } |
| } |
| function propagateCacheLifeAndTags(cacheContext, entry) { |
| if (cacheContext.kind === 'private') { |
| switch(cacheContext.outerWorkUnitStore.type){ |
| case 'prerender-runtime': |
| case 'private-cache': |
| propagateCacheLifeAndTagsToRevalidateStore(cacheContext.outerWorkUnitStore, entry); |
| break; |
| case 'request': |
| case undefined: |
| break; |
| default: |
| cacheContext.outerWorkUnitStore; |
| } |
| } else { |
| var _cacheContext_outerWorkUnitStore; |
| switch((_cacheContext_outerWorkUnitStore = cacheContext.outerWorkUnitStore) == null ? void 0 : _cacheContext_outerWorkUnitStore.type){ |
| case 'cache': |
| case 'private-cache': |
| case 'prerender': |
| case 'prerender-runtime': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| propagateCacheLifeAndTagsToRevalidateStore(cacheContext.outerWorkUnitStore, entry); |
| break; |
| case 'request': |
| case 'unstable-cache': |
| case undefined: |
| break; |
| default: |
| cacheContext.outerWorkUnitStore; |
| } |
| } |
| } |
| async function collectResult(savedStream, workStore, cacheContext, innerCacheStore, startTime, errors) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const buffer = []; |
| const reader = savedStream.getReader(); |
| try { |
| for(let entry; !(entry = await reader.read()).done;){ |
| buffer.push(entry.value); |
| } |
| } catch (error) { |
| errors.push(error); |
| } |
| let idx = 0; |
| const bufferStream = new ReadableStream({ |
| pull (controller) { |
| if (workStore.invalidDynamicUsageError) { |
| controller.error(workStore.invalidDynamicUsageError); |
| } else if (idx < buffer.length) { |
| controller.enqueue(buffer[idx++]); |
| } else if (errors.length > 0) { |
| |
| controller.error(errors[0]); |
| } else { |
| controller.close(); |
| } |
| } |
| }); |
| const collectedTags = innerCacheStore.tags; |
| |
| |
| |
| const collectedRevalidate = innerCacheStore.explicitRevalidate !== undefined ? innerCacheStore.explicitRevalidate : innerCacheStore.revalidate; |
| const collectedExpire = innerCacheStore.explicitExpire !== undefined ? innerCacheStore.explicitExpire : innerCacheStore.expire; |
| const collectedStale = innerCacheStore.explicitStale !== undefined ? innerCacheStore.explicitStale : innerCacheStore.stale; |
| const entry = { |
| value: bufferStream, |
| timestamp: startTime, |
| revalidate: collectedRevalidate, |
| expire: collectedExpire, |
| stale: collectedStale, |
| tags: collectedTags === null ? [] : collectedTags |
| }; |
| |
| if (cacheContext) { |
| propagateCacheLifeAndTags(cacheContext, entry); |
| } |
| const cacheSignal = cacheContext.outerWorkUnitStore ? (0, _workunitasyncstorageexternal.getCacheSignal)(cacheContext.outerWorkUnitStore) : null; |
| if (cacheSignal) { |
| cacheSignal.endRead(); |
| } |
| return entry; |
| } |
| async function generateCacheEntryImpl(workStore, cacheContext, innerCacheStore, clientReferenceManifest, encodedArguments, fn, sharedErrorStack) { |
| const temporaryReferences = (0, _server.createTemporaryReferenceSet)(); |
| const outerWorkUnitStore = cacheContext.outerWorkUnitStore; |
| const [, , args] = typeof encodedArguments === 'string' ? await (0, _server.decodeReply)(encodedArguments, (0, _encryptionutils.getServerModuleMap)(), { |
| temporaryReferences |
| }) : await (0, _server.decodeReplyFromAsyncIterable)({ |
| async *[Symbol.asyncIterator] () { |
| for (const entry of encodedArguments){ |
| yield entry; |
| } |
| if (outerWorkUnitStore) { |
| switch(outerWorkUnitStore.type){ |
| case 'prerender-runtime': |
| case 'prerender': |
| |
| |
| |
| |
| |
| await new Promise((resolve)=>{ |
| if (outerWorkUnitStore.renderSignal.aborted) { |
| resolve(); |
| } else { |
| outerWorkUnitStore.renderSignal.addEventListener('abort', ()=>resolve(), { |
| once: true |
| }); |
| } |
| }); |
| break; |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| outerWorkUnitStore; |
| } |
| } |
| } |
| }, (0, _encryptionutils.getServerModuleMap)(), { |
| temporaryReferences |
| }); |
| |
| const startTime = performance.timeOrigin + performance.now(); |
| |
| |
| |
| |
| const resultPromise = (0, _lazyresult.createLazyResult)(()=>fn.apply(null, args)); |
| let errors = []; |
| |
| |
| |
| |
| const handleError = (error)=>{ |
| const digest = (0, _createerrorhandler.getDigestForWellKnownError)(error); |
| if (digest) { |
| return digest; |
| } |
| if ((0, _reactlargeshellerror.isReactLargeShellError)(error)) { |
| |
| console.error(error); |
| return undefined; |
| } |
| if (process.env.NODE_ENV !== 'development') { |
| |
| |
| |
| console.error(error); |
| } |
| errors.push(error); |
| }; |
| let stream; |
| switch(outerWorkUnitStore == null ? void 0 : outerWorkUnitStore.type){ |
| case 'prerender-runtime': |
| case 'prerender': |
| var _dynamicAccessAsyncStorage_getStore; |
| const timeoutAbortController = new AbortController(); |
| |
| |
| |
| const timer = setTimeout(()=>{ |
| const error = new _usecacheerrors.UseCacheTimeoutError(); |
| if (sharedErrorStack) { |
| error.stack = error.name + ': ' + error.message + sharedErrorStack; |
| } |
| workStore.invalidDynamicUsageError = error; |
| timeoutAbortController.abort(error); |
| }, 50000); |
| const dynamicAccessAbortSignal = (_dynamicAccessAsyncStorage_getStore = _dynamicaccessasyncstorageexternal.dynamicAccessAsyncStorage.getStore()) == null ? void 0 : _dynamicAccessAsyncStorage_getStore.abortController.signal; |
| const abortSignal = dynamicAccessAbortSignal ? AbortSignal.any([ |
| dynamicAccessAbortSignal, |
| outerWorkUnitStore.renderSignal, |
| timeoutAbortController.signal |
| ]) : timeoutAbortController.signal; |
| const { prelude } = await (0, _static.unstable_prerender)(resultPromise, clientReferenceManifest.clientModules, { |
| environmentName: 'Cache', |
| filterStackFrame, |
| signal: abortSignal, |
| temporaryReferences, |
| onError (error) { |
| if (abortSignal.aborted && abortSignal.reason === error) { |
| return undefined; |
| } |
| return handleError(error); |
| } |
| }); |
| clearTimeout(timer); |
| if (timeoutAbortController.signal.aborted) { |
| |
| |
| |
| |
| |
| |
| stream = new ReadableStream({ |
| start (controller) { |
| controller.error(timeoutAbortController.signal.reason); |
| } |
| }); |
| } else if (dynamicAccessAbortSignal == null ? void 0 : dynamicAccessAbortSignal.aborted) { |
| |
| |
| |
| const hangingPromise = (0, _dynamicrenderingutils.makeHangingPromise)(outerWorkUnitStore.renderSignal, workStore.route, abortSignal.reason); |
| if (outerWorkUnitStore.cacheSignal) { |
| outerWorkUnitStore.cacheSignal.endRead(); |
| } |
| return { |
| type: 'prerender-dynamic', |
| hangingPromise |
| }; |
| } else { |
| stream = prelude; |
| } |
| break; |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| case undefined: |
| stream = (0, _server.renderToReadableStream)(resultPromise, clientReferenceManifest.clientModules, { |
| environmentName: 'Cache', |
| filterStackFrame, |
| temporaryReferences, |
| onError: handleError |
| }); |
| break; |
| default: |
| return outerWorkUnitStore; |
| } |
| const [returnStream, savedStream] = stream.tee(); |
| const pendingCacheEntry = collectResult(savedStream, workStore, cacheContext, innerCacheStore, startTime, errors); |
| return { |
| type: 'cached', |
| |
| |
| |
| stream: returnStream, |
| pendingCacheEntry |
| }; |
| } |
| function cloneCacheEntry(entry) { |
| const [streamA, streamB] = entry.value.tee(); |
| entry.value = streamA; |
| const clonedEntry = { |
| value: streamB, |
| timestamp: entry.timestamp, |
| revalidate: entry.revalidate, |
| expire: entry.expire, |
| stale: entry.stale, |
| tags: entry.tags |
| }; |
| return [ |
| entry, |
| clonedEntry |
| ]; |
| } |
| async function clonePendingCacheEntry(pendingCacheEntry) { |
| const entry = await pendingCacheEntry; |
| return cloneCacheEntry(entry); |
| } |
| async function getNthCacheEntry(split, i) { |
| return (await split)[i]; |
| } |
| async function encodeFormData(formData) { |
| let result = ''; |
| for (let [key, value] of formData){ |
| |
| |
| |
| |
| |
| result += key.length.toString(16) + ':' + key; |
| let stringValue; |
| if (typeof value === 'string') { |
| stringValue = value; |
| } else { |
| |
| |
| |
| const arrayBuffer = await value.arrayBuffer(); |
| if (arrayBuffer.byteLength % 2 === 0) { |
| stringValue = String.fromCodePoint(...new Uint16Array(arrayBuffer)); |
| } else { |
| stringValue = String.fromCodePoint(...new Uint16Array(arrayBuffer, 0, (arrayBuffer.byteLength - 1) / 2)) + String.fromCodePoint(new Uint8Array(arrayBuffer, arrayBuffer.byteLength - 1, 1)[0]); |
| } |
| } |
| result += stringValue.length.toString(16) + ':' + stringValue; |
| } |
| return result; |
| } |
| function createTrackedReadableStream(stream, cacheSignal) { |
| const reader = stream.getReader(); |
| return new ReadableStream({ |
| async pull (controller) { |
| const { done, value } = await reader.read(); |
| if (done) { |
| controller.close(); |
| cacheSignal.endRead(); |
| } else { |
| controller.enqueue(value); |
| } |
| } |
| }); |
| } |
| function wrapAsInvalidDynamicUsageError(error, sharedErrorStack, workStore) { |
| if (sharedErrorStack) { |
| error.stack = error.name + ': ' + error.message + sharedErrorStack; |
| } |
| workStore.invalidDynamicUsageError ??= error; |
| return error; |
| } |
| function cache(kind, id, boundArgsLength, originalFn) { |
| var _sharedError_stack; |
| const isPrivate = kind === 'private'; |
| |
| |
| const cacheHandler = isPrivate ? undefined : (0, _handlers.getCacheHandler)(kind); |
| if (!isPrivate && !cacheHandler) { |
| throw Object.defineProperty(new Error('Unknown cache handler: ' + kind), "__NEXT_ERROR_CODE", { |
| value: "E248", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| |
| const sharedError = new Error(); |
| Error.captureStackTrace(sharedError, cache); |
| const sharedErrorStack = (_sharedError_stack = sharedError.stack) == null ? void 0 : _sharedError_stack.slice(sharedError.stack.indexOf('\n')); |
| const name = originalFn.name; |
| const cachedFn = { |
| [name]: async function(...args) { |
| const workStore = _workasyncstorageexternal.workAsyncStorage.getStore(); |
| if (workStore === undefined) { |
| throw Object.defineProperty(new Error('"use cache" cannot be used outside of App Router. Expected a WorkStore.'), "__NEXT_ERROR_CODE", { |
| value: "E279", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| let fn = originalFn; |
| const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore(); |
| let cacheContext; |
| if (isPrivate) { |
| const expression = '"use cache: private"'; |
| switch(workUnitStore == null ? void 0 : workUnitStore.type){ |
| |
| case 'prerender': |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, expression); |
| case 'prerender-ppr': |
| return (0, _dynamicrendering.postponeWithTracking)(workStore.route, expression, workUnitStore.dynamicTracking); |
| case 'prerender-legacy': |
| return (0, _dynamicrendering.throwToInterruptStaticGeneration)(expression, workStore, workUnitStore); |
| case 'prerender-client': |
| throw Object.defineProperty(new _invarianterror.InvariantError(`${expression} must not be used within a client component. Next.js should be preventing ${expression} from being allowed in client components statically, but did not in this case.`), "__NEXT_ERROR_CODE", { |
| value: "E741", |
| enumerable: false, |
| configurable: true |
| }); |
| case 'unstable-cache': |
| { |
| throw wrapAsInvalidDynamicUsageError(Object.defineProperty(new Error( |
| `${expression} must not be used within \`unstable_cache()\`.`), "__NEXT_ERROR_CODE", { |
| value: "E744", |
| enumerable: false, |
| configurable: true |
| }), sharedErrorStack, workStore); |
| } |
| case 'cache': |
| { |
| throw wrapAsInvalidDynamicUsageError(Object.defineProperty(new Error( |
| `${expression} must not be used within "use cache". It can only be nested inside of another ${expression}.`), "__NEXT_ERROR_CODE", { |
| value: "E735", |
| enumerable: false, |
| configurable: true |
| }), sharedErrorStack, workStore); |
| } |
| case 'request': |
| case 'prerender-runtime': |
| case 'private-cache': |
| cacheContext = { |
| kind: 'private', |
| outerWorkUnitStore: workUnitStore |
| }; |
| break; |
| case undefined: |
| throw wrapAsInvalidDynamicUsageError(Object.defineProperty(new Error( |
| `${expression} cannot be used outside of a request context.`), "__NEXT_ERROR_CODE", { |
| value: "E754", |
| enumerable: false, |
| configurable: true |
| }), sharedErrorStack, workStore); |
| default: |
| workUnitStore; |
| |
| |
| throw Object.defineProperty(new _invarianterror.InvariantError(`Unexpected work unit store.`), "__NEXT_ERROR_CODE", { |
| value: "E737", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| } else { |
| switch(workUnitStore == null ? void 0 : workUnitStore.type){ |
| case 'prerender-client': |
| const expression = '"use cache"'; |
| throw Object.defineProperty(new _invarianterror.InvariantError(`${expression} must not be used within a client component. Next.js should be preventing ${expression} from being allowed in client components statically, but did not in this case.`), "__NEXT_ERROR_CODE", { |
| value: "E741", |
| enumerable: false, |
| configurable: true |
| }); |
| case 'prerender': |
| case 'prerender-runtime': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| |
| |
| case 'unstable-cache': |
| case undefined: |
| cacheContext = { |
| kind: 'public', |
| outerWorkUnitStore: workUnitStore |
| }; |
| break; |
| default: |
| workUnitStore; |
| |
| |
| throw Object.defineProperty(new _invarianterror.InvariantError(`Unexpected work unit store.`), "__NEXT_ERROR_CODE", { |
| value: "E737", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| } |
| |
| |
| const clientReferenceManifest = (0, _encryptionutils.getClientReferenceManifestForRsc)(); |
| |
| |
| |
| |
| const buildId = workStore.buildId; |
| |
| |
| |
| |
| |
| const hmrRefreshHash = workUnitStore && (0, _workunitasyncstorageexternal.getHmrRefreshHash)(workStore, workUnitStore); |
| const hangingInputAbortSignal = workUnitStore ? (0, _dynamicrendering.createHangingInputAbortSignal)(workUnitStore) : undefined; |
| |
| |
| if (cacheContext.kind === 'private') { |
| const runtimeStagePromise = (0, _workunitasyncstorageexternal.getRuntimeStagePromise)(cacheContext.outerWorkUnitStore); |
| if (runtimeStagePromise) { |
| await runtimeStagePromise; |
| } |
| } |
| let isPageOrLayout = false; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (isPageComponent(args)) { |
| isPageOrLayout = true; |
| const [{ params: outerParams, searchParams: outerSearchParams }] = args; |
| const props = { |
| params: outerParams |
| }; |
| if (isPrivate) { |
| |
| |
| props.searchParams = outerSearchParams; |
| } |
| args = [ |
| props |
| ]; |
| fn = ({ |
| [name]: async ({ params: _innerParams, searchParams: innerSearchParams })=>originalFn.apply(null, [ |
| { |
| params: outerParams, |
| searchParams: innerSearchParams ?? |
| |
| |
| |
| |
| |
| |
| (0, _searchparams.makeErroringSearchParamsForUseCache)(workStore) |
| } |
| ]) |
| })[name]; |
| } else if (isLayoutComponent(args)) { |
| isPageOrLayout = true; |
| const [{ params: outerParams, $$isLayoutComponent, ...outerSlots }] = args; |
| |
| args = [ |
| { |
| params: outerParams, |
| ...outerSlots |
| } |
| ]; |
| fn = ({ |
| [name]: async ({ params: _innerParams, ...innerSlots })=>originalFn.apply(null, [ |
| { |
| params: outerParams, |
| ...innerSlots |
| } |
| ]) |
| })[name]; |
| } |
| if (boundArgsLength > 0) { |
| if (args.length === 0) { |
| throw Object.defineProperty(new _invarianterror.InvariantError(`Expected the "use cache" function ${JSON.stringify(fn.name)} to receive its encrypted bound arguments as the first argument.`), "__NEXT_ERROR_CODE", { |
| value: "E524", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| const encryptedBoundArgs = args.shift(); |
| const boundArgs = await (0, _encryption.decryptActionBoundArgs)(id, encryptedBoundArgs); |
| if (!Array.isArray(boundArgs)) { |
| throw Object.defineProperty(new _invarianterror.InvariantError(`Expected the bound arguments of "use cache" function ${JSON.stringify(fn.name)} to deserialize into an array, got ${typeof boundArgs} instead.`), "__NEXT_ERROR_CODE", { |
| value: "E581", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| if (boundArgsLength !== boundArgs.length) { |
| throw Object.defineProperty(new _invarianterror.InvariantError(`Expected the "use cache" function ${JSON.stringify(fn.name)} to receive ${boundArgsLength} bound arguments, got ${boundArgs.length} instead.`), "__NEXT_ERROR_CODE", { |
| value: "E559", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| args.unshift(boundArgs); |
| } |
| const temporaryReferences = (0, _client.createTemporaryReferenceSet)(); |
| |
| |
| |
| |
| |
| |
| |
| |
| const cacheKeyParts = hmrRefreshHash ? [ |
| buildId, |
| id, |
| args, |
| hmrRefreshHash |
| ] : [ |
| buildId, |
| id, |
| args |
| ]; |
| const encodeCacheKeyParts = ()=>(0, _client.encodeReply)(cacheKeyParts, { |
| temporaryReferences, |
| signal: hangingInputAbortSignal |
| }); |
| let encodedCacheKeyParts; |
| switch(workUnitStore == null ? void 0 : workUnitStore.type){ |
| case 'prerender-runtime': |
| |
| |
| |
| |
| |
| |
| |
| |
| case 'prerender': |
| if (!isPageOrLayout) { |
| |
| |
| |
| |
| |
| |
| |
| const dynamicAccessAbortController = new AbortController(); |
| encodedCacheKeyParts = await _dynamicaccessasyncstorageexternal.dynamicAccessAsyncStorage.run({ |
| abortController: dynamicAccessAbortController |
| }, encodeCacheKeyParts); |
| if (dynamicAccessAbortController.signal.aborted) { |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, dynamicAccessAbortController.signal.reason.message); |
| } |
| break; |
| } |
| |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| case undefined: |
| encodedCacheKeyParts = await encodeCacheKeyParts(); |
| break; |
| default: |
| return workUnitStore; |
| } |
| const serializedCacheKey = typeof encodedCacheKeyParts === 'string' ? |
| encodedCacheKeyParts : await encodeFormData(encodedCacheKeyParts); |
| let stream = undefined; |
| |
| const prerenderResumeDataCache = workUnitStore ? (0, _workunitasyncstorageexternal.getPrerenderResumeDataCache)(workUnitStore) : null; |
| const renderResumeDataCache = workUnitStore ? (0, _workunitasyncstorageexternal.getRenderResumeDataCache)(workUnitStore) : null; |
| if (renderResumeDataCache) { |
| const cacheSignal = workUnitStore ? (0, _workunitasyncstorageexternal.getCacheSignal)(workUnitStore) : null; |
| if (cacheSignal) { |
| cacheSignal.beginRead(); |
| } |
| const cachedEntry = renderResumeDataCache.cache.get(serializedCacheKey); |
| if (cachedEntry !== undefined) { |
| const existingEntry = await cachedEntry; |
| propagateCacheLifeAndTags(cacheContext, existingEntry); |
| if (workUnitStore !== undefined && existingEntry !== undefined) { |
| if (existingEntry.revalidate === 0 || existingEntry.expire < _constants.DYNAMIC_EXPIRE) { |
| switch(workUnitStore.type){ |
| case 'prerender': |
| |
| |
| |
| |
| |
| |
| if (cacheSignal) { |
| cacheSignal.endRead(); |
| } |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, 'dynamic "use cache"'); |
| case 'prerender-runtime': |
| { |
| |
| |
| if (workUnitStore.runtimeStagePromise) { |
| await workUnitStore.runtimeStagePromise; |
| } |
| break; |
| } |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| if (existingEntry.stale < _constants.RUNTIME_PREFETCH_DYNAMIC_STALE) { |
| switch(workUnitStore.type){ |
| case 'prerender-runtime': |
| |
| |
| |
| |
| if (cacheSignal) { |
| cacheSignal.endRead(); |
| } |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, 'dynamic "use cache"'); |
| case 'prerender': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| } |
| const [streamA, streamB] = existingEntry.value.tee(); |
| existingEntry.value = streamB; |
| if (cacheSignal) { |
| |
| |
| stream = createTrackedReadableStream(streamA, cacheSignal); |
| } else { |
| stream = streamA; |
| } |
| } else { |
| if (cacheSignal) { |
| cacheSignal.endRead(); |
| } |
| if (workUnitStore) { |
| switch(workUnitStore.type){ |
| case 'prerender': |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (workUnitStore.allowEmptyStaticShell) { |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, 'dynamic "use cache"'); |
| } |
| break; |
| case 'prerender-runtime': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| } |
| } |
| if (stream === undefined) { |
| const cacheSignal = workUnitStore ? (0, _workunitasyncstorageexternal.getCacheSignal)(workUnitStore) : null; |
| if (cacheSignal) { |
| |
| |
| cacheSignal.beginRead(); |
| } |
| const lazyRefreshTags = workStore.refreshTagsByCacheKind.get(kind); |
| if (lazyRefreshTags && !(0, _lazyresult.isResolvedLazyResult)(lazyRefreshTags)) { |
| await lazyRefreshTags; |
| } |
| let entry; |
| |
| if (cacheHandler && !shouldForceRevalidate(workStore, workUnitStore)) { |
| var _workUnitStore_implicitTags; |
| entry = await cacheHandler.get(serializedCacheKey, (workUnitStore == null ? void 0 : (_workUnitStore_implicitTags = workUnitStore.implicitTags) == null ? void 0 : _workUnitStore_implicitTags.tags) ?? []); |
| } |
| if (entry) { |
| var _workUnitStore_implicitTags1; |
| const implicitTags = (workUnitStore == null ? void 0 : (_workUnitStore_implicitTags1 = workUnitStore.implicitTags) == null ? void 0 : _workUnitStore_implicitTags1.tags) ?? []; |
| let implicitTagsExpiration = 0; |
| if (workUnitStore == null ? void 0 : workUnitStore.implicitTags) { |
| const lazyExpiration = workUnitStore.implicitTags.expirationsByCacheKind.get(kind); |
| if (lazyExpiration) { |
| const expiration = (0, _lazyresult.isResolvedLazyResult)(lazyExpiration) ? lazyExpiration.value : await lazyExpiration; |
| |
| |
| |
| |
| |
| if (expiration < Infinity) { |
| implicitTagsExpiration = expiration; |
| } |
| } |
| } |
| if (shouldDiscardCacheEntry(entry, workStore, workUnitStore, implicitTags, implicitTagsExpiration)) { |
| debug == null ? void 0 : debug('discarding stale entry', serializedCacheKey); |
| entry = undefined; |
| } |
| } |
| const currentTime = performance.timeOrigin + performance.now(); |
| if (workUnitStore !== undefined && entry !== undefined && (entry.revalidate === 0 || entry.expire < _constants.DYNAMIC_EXPIRE)) { |
| switch(workUnitStore.type){ |
| case 'prerender': |
| |
| |
| |
| |
| |
| if (cacheSignal) { |
| cacheSignal.endRead(); |
| } |
| return (0, _dynamicrenderingutils.makeHangingPromise)(workUnitStore.renderSignal, workStore.route, 'dynamic "use cache"'); |
| case 'prerender-runtime': |
| |
| |
| if (workUnitStore.runtimeStagePromise) { |
| await workUnitStore.runtimeStagePromise; |
| } |
| break; |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| if (entry === undefined || currentTime > entry.timestamp + entry.expire * 1000 || workStore.isStaticGeneration && currentTime > entry.timestamp + entry.revalidate * 1000) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (entry) { |
| if (currentTime > entry.timestamp + entry.expire * 1000) { |
| debug == null ? void 0 : debug('entry is expired', serializedCacheKey); |
| } |
| if (workStore.isStaticGeneration && currentTime > entry.timestamp + entry.revalidate * 1000) { |
| debug == null ? void 0 : debug('static generation, entry is stale', serializedCacheKey); |
| } |
| } |
| const result = await generateCacheEntry(workStore, cacheContext, clientReferenceManifest, encodedCacheKeyParts, fn, sharedErrorStack); |
| if (result.type === 'prerender-dynamic') { |
| return result.hangingPromise; |
| } |
| const { stream: newStream, pendingCacheEntry } = result; |
| |
| if (!workStore.isDraftMode) { |
| let savedCacheEntry; |
| if (prerenderResumeDataCache) { |
| |
| const split = clonePendingCacheEntry(pendingCacheEntry); |
| savedCacheEntry = getNthCacheEntry(split, 0); |
| prerenderResumeDataCache.cache.set(serializedCacheKey, getNthCacheEntry(split, 1)); |
| } else { |
| savedCacheEntry = pendingCacheEntry; |
| } |
| if (cacheHandler) { |
| const promise = cacheHandler.set(serializedCacheKey, savedCacheEntry); |
| workStore.pendingRevalidateWrites ??= []; |
| workStore.pendingRevalidateWrites.push(promise); |
| } |
| } |
| stream = newStream; |
| } else { |
| |
| |
| if (cacheContext.kind === 'private') { |
| throw Object.defineProperty(new _invarianterror.InvariantError(`A private cache entry must not be retrieved from the cache handler.`), "__NEXT_ERROR_CODE", { |
| value: "E749", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| propagateCacheLifeAndTags(cacheContext, entry); |
| |
| stream = entry.value; |
| |
| |
| if (prerenderResumeDataCache) { |
| const [entryLeft, entryRight] = cloneCacheEntry(entry); |
| if (cacheSignal) { |
| stream = createTrackedReadableStream(entryLeft.value, cacheSignal); |
| } else { |
| stream = entryLeft.value; |
| } |
| prerenderResumeDataCache.cache.set(serializedCacheKey, Promise.resolve(entryRight)); |
| } else { |
| |
| |
| |
| cacheSignal == null ? void 0 : cacheSignal.endRead(); |
| } |
| if (currentTime > entry.timestamp + entry.revalidate * 1000) { |
| |
| |
| |
| const result = await generateCacheEntry(workStore, |
| { |
| kind: cacheContext.kind, |
| outerWorkUnitStore: undefined |
| }, clientReferenceManifest, encodedCacheKeyParts, fn, sharedErrorStack); |
| if (result.type === 'cached') { |
| const { stream: ignoredStream, pendingCacheEntry } = result; |
| let savedCacheEntry; |
| if (prerenderResumeDataCache) { |
| const split = clonePendingCacheEntry(pendingCacheEntry); |
| savedCacheEntry = getNthCacheEntry(split, 0); |
| prerenderResumeDataCache.cache.set(serializedCacheKey, getNthCacheEntry(split, 1)); |
| } else { |
| savedCacheEntry = pendingCacheEntry; |
| } |
| if (cacheHandler) { |
| const promise = cacheHandler.set(serializedCacheKey, savedCacheEntry); |
| workStore.pendingRevalidateWrites ??= []; |
| workStore.pendingRevalidateWrites.push(promise); |
| } |
| await ignoredStream.cancel(); |
| } |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| const replayConsoleLogs = true; |
| const serverConsumerManifest = { |
| |
| |
| |
| moduleLoading: null, |
| moduleMap: isEdgeRuntime ? clientReferenceManifest.edgeRscModuleMapping : clientReferenceManifest.rscModuleMapping, |
| serverModuleMap: (0, _encryptionutils.getServerModuleMap)() |
| }; |
| return (0, _client.createFromReadableStream)(stream, { |
| findSourceMapURL, |
| serverConsumerManifest, |
| temporaryReferences, |
| replayConsoleLogs, |
| environmentName: 'Cache' |
| }); |
| } |
| }[name]; |
| return _react.default.cache(cachedFn); |
| } |
| function isPageComponent(args) { |
| if (args.length !== 2) { |
| return false; |
| } |
| const [props, ref] = args; |
| return ref === undefined && |
| props !== null && typeof props === 'object' && props.$$isPageComponent; |
| } |
| function isLayoutComponent(args) { |
| if (args.length !== 2) { |
| return false; |
| } |
| const [props, ref] = args; |
| return ref === undefined && |
| props !== null && typeof props === 'object' && props.$$isLayoutComponent; |
| } |
| function shouldForceRevalidate(workStore, workUnitStore) { |
| if (workStore.isOnDemandRevalidate || workStore.isDraftMode) { |
| return true; |
| } |
| if (workStore.dev && workUnitStore) { |
| switch(workUnitStore.type){ |
| case 'request': |
| return workUnitStore.headers.get('cache-control') === 'no-cache'; |
| case 'cache': |
| case 'private-cache': |
| return workUnitStore.forceRevalidate; |
| case 'prerender-runtime': |
| case 'prerender': |
| case 'prerender-client': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| return false; |
| } |
| function shouldDiscardCacheEntry(entry, workStore, workUnitStore, implicitTags, implicitTagsExpiration) { |
| |
| |
| if (entry.timestamp <= implicitTagsExpiration) { |
| debug == null ? void 0 : debug('entry was created at', entry.timestamp, 'before implicit tags were revalidated at', implicitTagsExpiration); |
| return true; |
| } |
| |
| |
| |
| |
| |
| if (workUnitStore) { |
| switch(workUnitStore.type){ |
| case 'prerender': |
| return false; |
| case 'prerender-runtime': |
| case 'prerender-client': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'request': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| |
| |
| if (entry.tags.some((tag)=>isRecentlyRevalidatedTag(tag, workStore))) { |
| return true; |
| } |
| |
| |
| if (implicitTags.some((tag)=>isRecentlyRevalidatedTag(tag, workStore))) { |
| return true; |
| } |
| return false; |
| } |
| function isRecentlyRevalidatedTag(tag, workStore) { |
| const { previouslyRevalidatedTags, pendingRevalidatedTags } = workStore; |
| |
| if (previouslyRevalidatedTags.includes(tag)) { |
| debug == null ? void 0 : debug('tag', tag, 'was previously revalidated'); |
| return true; |
| } |
| |
| |
| |
| |
| if (pendingRevalidatedTags == null ? void 0 : pendingRevalidatedTags.includes(tag)) { |
| debug == null ? void 0 : debug('tag', tag, 'was just revalidated'); |
| return true; |
| } |
| return false; |
| } |
|
|
| |