| "use strict"; |
| Object.defineProperty(exports, "__esModule", { |
| value: true |
| }); |
| Object.defineProperty(exports, "cookies", { |
| enumerable: true, |
| get: function() { |
| return cookies; |
| } |
| }); |
| const _requestcookies = require("../web/spec-extension/adapters/request-cookies"); |
| const _cookies = require("../web/spec-extension/cookies"); |
| const _workasyncstorageexternal = require("../app-render/work-async-storage.external"); |
| const _workunitasyncstorageexternal = require("../app-render/work-unit-async-storage.external"); |
| const _dynamicrendering = require("../app-render/dynamic-rendering"); |
| const _staticgenerationbailout = require("../../client/components/static-generation-bailout"); |
| const _dynamicrenderingutils = require("../dynamic-rendering-utils"); |
| const _creatededupedbycallsiteservererrorlogger = require("../create-deduped-by-callsite-server-error-logger"); |
| const _utils = require("./utils"); |
| const _invarianterror = require("../../shared/lib/invariant-error"); |
| const _reflect = require("../web/spec-extension/adapters/reflect"); |
| function cookies() { |
| const callingExpression = 'cookies'; |
| const workStore = _workasyncstorageexternal.workAsyncStorage.getStore(); |
| const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore(); |
| if (workStore) { |
| if (workUnitStore && workUnitStore.phase === 'after' && !(0, _utils.isRequestAPICallableInsideAfter)()) { |
| throw Object.defineProperty(new Error( |
| `Route ${workStore.route} used "cookies" inside "after(...)". This is not supported. If you need this data inside an "after" callback, use "cookies" outside of the callback. See more info here: https://nextjs.org/docs/canary/app/api-reference/functions/after`), "__NEXT_ERROR_CODE", { |
| value: "E88", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| if (workStore.forceStatic) { |
| |
| |
| const underlyingCookies = createEmptyCookies(); |
| return makeUntrackedExoticCookies(underlyingCookies); |
| } |
| if (workStore.dynamicShouldError) { |
| throw Object.defineProperty(new _staticgenerationbailout.StaticGenBailoutError(`Route ${workStore.route} with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`), "__NEXT_ERROR_CODE", { |
| value: "E549", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| if (workUnitStore) { |
| switch(workUnitStore.type){ |
| case 'cache': |
| const error = Object.defineProperty(new Error(`Route ${workStore.route} used "cookies" inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache`), "__NEXT_ERROR_CODE", { |
| value: "E398", |
| enumerable: false, |
| configurable: true |
| }); |
| Error.captureStackTrace(error, cookies); |
| workStore.invalidDynamicUsageError ??= error; |
| throw error; |
| case 'unstable-cache': |
| throw Object.defineProperty(new Error(`Route ${workStore.route} used "cookies" inside a function cached with "unstable_cache(...)". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/app/api-reference/functions/unstable_cache`), "__NEXT_ERROR_CODE", { |
| value: "E157", |
| enumerable: false, |
| configurable: true |
| }); |
| case 'prerender': |
| return makeHangingCookies(workStore, workUnitStore); |
| case 'prerender-client': |
| const exportName = '`cookies`'; |
| throw Object.defineProperty(new _invarianterror.InvariantError(`${exportName} must not be used within a client component. Next.js should be preventing ${exportName} from being included in client components statically, but did not in this case.`), "__NEXT_ERROR_CODE", { |
| value: "E693", |
| enumerable: false, |
| configurable: true |
| }); |
| case 'prerender-ppr': |
| |
| |
| return (0, _dynamicrendering.postponeWithTracking)(workStore.route, callingExpression, workUnitStore.dynamicTracking); |
| case 'prerender-legacy': |
| |
| |
| return (0, _dynamicrendering.throwToInterruptStaticGeneration)(callingExpression, workStore, workUnitStore); |
| case 'prerender-runtime': |
| return (0, _dynamicrendering.delayUntilRuntimeStage)(workUnitStore, makeUntrackedCookies(workUnitStore.cookies)); |
| case 'private-cache': |
| if (process.env.__NEXT_CACHE_COMPONENTS) { |
| return makeUntrackedCookies(workUnitStore.cookies); |
| } |
| return makeUntrackedExoticCookies(workUnitStore.cookies); |
| case 'request': |
| (0, _dynamicrendering.trackDynamicDataInDynamicRender)(workUnitStore); |
| let underlyingCookies; |
| if ((0, _requestcookies.areCookiesMutableInCurrentPhase)(workUnitStore)) { |
| |
| |
| underlyingCookies = workUnitStore.userspaceMutableCookies; |
| } else { |
| underlyingCookies = workUnitStore.cookies; |
| } |
| if (process.env.NODE_ENV === 'development') { |
| |
| |
| |
| if (process.env.__NEXT_CACHE_COMPONENTS) { |
| return makeUntrackedCookiesWithDevWarnings(underlyingCookies, workStore == null ? void 0 : workStore.route); |
| } |
| return makeUntrackedExoticCookiesWithDevWarnings(underlyingCookies, workStore == null ? void 0 : workStore.route); |
| } else { |
| if (process.env.__NEXT_CACHE_COMPONENTS) { |
| return makeUntrackedCookies(underlyingCookies); |
| } |
| return makeUntrackedExoticCookies(underlyingCookies); |
| } |
| default: |
| workUnitStore; |
| } |
| } |
| } |
| |
| (0, _workunitasyncstorageexternal.throwForMissingRequestStore)(callingExpression); |
| } |
| function createEmptyCookies() { |
| return _requestcookies.RequestCookiesAdapter.seal(new _cookies.RequestCookies(new Headers({}))); |
| } |
| const CachedCookies = new WeakMap(); |
| function makeHangingCookies(workStore, prerenderStore) { |
| const cachedPromise = CachedCookies.get(prerenderStore); |
| if (cachedPromise) { |
| return cachedPromise; |
| } |
| const promise = (0, _dynamicrenderingutils.makeHangingPromise)(prerenderStore.renderSignal, workStore.route, '`cookies()`'); |
| CachedCookies.set(prerenderStore, promise); |
| return promise; |
| } |
| function makeUntrackedCookies(underlyingCookies) { |
| const cachedCookies = CachedCookies.get(underlyingCookies); |
| if (cachedCookies) { |
| return cachedCookies; |
| } |
| const promise = Promise.resolve(underlyingCookies); |
| CachedCookies.set(underlyingCookies, promise); |
| return promise; |
| } |
| function makeUntrackedExoticCookies(underlyingCookies) { |
| const cachedCookies = CachedCookies.get(underlyingCookies); |
| if (cachedCookies) { |
| return cachedCookies; |
| } |
| const promise = Promise.resolve(underlyingCookies); |
| CachedCookies.set(underlyingCookies, promise); |
| Object.defineProperties(promise, { |
| [Symbol.iterator]: { |
| value: underlyingCookies[Symbol.iterator] ? underlyingCookies[Symbol.iterator].bind(underlyingCookies) : |
| |
| |
| |
| polyfilledResponseCookiesIterator.bind(underlyingCookies) |
| }, |
| size: { |
| get () { |
| return underlyingCookies.size; |
| } |
| }, |
| get: { |
| value: underlyingCookies.get.bind(underlyingCookies) |
| }, |
| getAll: { |
| value: underlyingCookies.getAll.bind(underlyingCookies) |
| }, |
| has: { |
| value: underlyingCookies.has.bind(underlyingCookies) |
| }, |
| set: { |
| value: underlyingCookies.set.bind(underlyingCookies) |
| }, |
| delete: { |
| value: underlyingCookies.delete.bind(underlyingCookies) |
| }, |
| clear: { |
| value: |
| typeof underlyingCookies.clear === 'function' ? underlyingCookies.clear.bind(underlyingCookies) : |
| |
| |
| |
| polyfilledResponseCookiesClear.bind(underlyingCookies, promise) |
| }, |
| toString: { |
| value: underlyingCookies.toString.bind(underlyingCookies) |
| } |
| }); |
| return promise; |
| } |
| function makeUntrackedExoticCookiesWithDevWarnings(underlyingCookies, route) { |
| const cachedCookies = CachedCookies.get(underlyingCookies); |
| if (cachedCookies) { |
| return cachedCookies; |
| } |
| const promise = (0, _dynamicrenderingutils.makeDevtoolsIOAwarePromise)(underlyingCookies); |
| CachedCookies.set(underlyingCookies, promise); |
| Object.defineProperties(promise, { |
| [Symbol.iterator]: { |
| value: function() { |
| const expression = '`...cookies()` or similar iteration'; |
| syncIODev(route, expression); |
| return underlyingCookies[Symbol.iterator] ? underlyingCookies[Symbol.iterator].apply(underlyingCookies, arguments) : |
| |
| |
| |
| polyfilledResponseCookiesIterator.call(underlyingCookies); |
| }, |
| writable: false |
| }, |
| size: { |
| get () { |
| const expression = '`cookies().size`'; |
| syncIODev(route, expression); |
| return underlyingCookies.size; |
| } |
| }, |
| get: { |
| value: function get() { |
| let expression; |
| if (arguments.length === 0) { |
| expression = '`cookies().get()`'; |
| } else { |
| expression = `\`cookies().get(${describeNameArg(arguments[0])})\``; |
| } |
| syncIODev(route, expression); |
| return underlyingCookies.get.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| }, |
| getAll: { |
| value: function getAll() { |
| let expression; |
| if (arguments.length === 0) { |
| expression = '`cookies().getAll()`'; |
| } else { |
| expression = `\`cookies().getAll(${describeNameArg(arguments[0])})\``; |
| } |
| syncIODev(route, expression); |
| return underlyingCookies.getAll.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| }, |
| has: { |
| value: function get() { |
| let expression; |
| if (arguments.length === 0) { |
| expression = '`cookies().has()`'; |
| } else { |
| expression = `\`cookies().has(${describeNameArg(arguments[0])})\``; |
| } |
| syncIODev(route, expression); |
| return underlyingCookies.has.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| }, |
| set: { |
| value: function set() { |
| let expression; |
| if (arguments.length === 0) { |
| expression = '`cookies().set()`'; |
| } else { |
| const arg = arguments[0]; |
| if (arg) { |
| expression = `\`cookies().set(${describeNameArg(arg)}, ...)\``; |
| } else { |
| expression = '`cookies().set(...)`'; |
| } |
| } |
| syncIODev(route, expression); |
| return underlyingCookies.set.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| }, |
| delete: { |
| value: function() { |
| let expression; |
| if (arguments.length === 0) { |
| expression = '`cookies().delete()`'; |
| } else if (arguments.length === 1) { |
| expression = `\`cookies().delete(${describeNameArg(arguments[0])})\``; |
| } else { |
| expression = `\`cookies().delete(${describeNameArg(arguments[0])}, ...)\``; |
| } |
| syncIODev(route, expression); |
| return underlyingCookies.delete.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| }, |
| clear: { |
| value: function clear() { |
| const expression = '`cookies().clear()`'; |
| syncIODev(route, expression); |
| |
| return typeof underlyingCookies.clear === 'function' ? underlyingCookies.clear.apply(underlyingCookies, arguments) : |
| |
| |
| |
| polyfilledResponseCookiesClear.call(underlyingCookies, promise); |
| }, |
| writable: false |
| }, |
| toString: { |
| value: function toString() { |
| const expression = '`cookies().toString()` or implicit casting'; |
| syncIODev(route, expression); |
| return underlyingCookies.toString.apply(underlyingCookies, arguments); |
| }, |
| writable: false |
| } |
| }); |
| return promise; |
| } |
| |
| |
| function makeUntrackedCookiesWithDevWarnings(underlyingCookies, route) { |
| const cachedCookies = CachedCookies.get(underlyingCookies); |
| if (cachedCookies) { |
| return cachedCookies; |
| } |
| const promise = (0, _dynamicrenderingutils.makeDevtoolsIOAwarePromise)(underlyingCookies); |
| const proxiedPromise = new Proxy(promise, { |
| get (target, prop, receiver) { |
| switch(prop){ |
| case Symbol.iterator: |
| { |
| warnForSyncAccess(route, '`...cookies()` or similar iteration'); |
| break; |
| } |
| case 'size': |
| case 'get': |
| case 'getAll': |
| case 'has': |
| case 'set': |
| case 'delete': |
| case 'clear': |
| case 'toString': |
| { |
| warnForSyncAccess(route, `\`cookies().${prop}\``); |
| break; |
| } |
| default: |
| { |
| |
| } |
| } |
| return _reflect.ReflectAdapter.get(target, prop, receiver); |
| } |
| }); |
| CachedCookies.set(underlyingCookies, proxiedPromise); |
| return proxiedPromise; |
| } |
| function describeNameArg(arg) { |
| return typeof arg === 'object' && arg !== null && typeof arg.name === 'string' ? `'${arg.name}'` : typeof arg === 'string' ? `'${arg}'` : '...'; |
| } |
| function syncIODev(route, expression) { |
| const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore(); |
| if (workUnitStore) { |
| switch(workUnitStore.type){ |
| case 'request': |
| if (workUnitStore.prerenderPhase === true) { |
| |
| |
| (0, _dynamicrendering.trackSynchronousRequestDataAccessInDev)(workUnitStore); |
| } |
| break; |
| case 'prerender': |
| case 'prerender-client': |
| case 'prerender-runtime': |
| case 'prerender-ppr': |
| case 'prerender-legacy': |
| case 'cache': |
| case 'private-cache': |
| case 'unstable-cache': |
| break; |
| default: |
| workUnitStore; |
| } |
| } |
| |
| warnForSyncAccess(route, expression); |
| } |
| const warnForSyncAccess = (0, _creatededupedbycallsiteservererrorlogger.createDedupedByCallsiteServerErrorLoggerDev)(createCookiesAccessError); |
| function createCookiesAccessError(route, expression) { |
| const prefix = route ? `Route "${route}" ` : 'This route '; |
| return Object.defineProperty(new Error(`${prefix}used ${expression}. ` + `\`cookies()\` should be awaited before using its value. ` + `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`), "__NEXT_ERROR_CODE", { |
| value: "E223", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| function polyfilledResponseCookiesIterator() { |
| return this.getAll().map((c)=>[ |
| c.name, |
| c |
| ]).values(); |
| } |
| function polyfilledResponseCookiesClear(returnable) { |
| for (const cookie of this.getAll()){ |
| this.delete(cookie.name); |
| } |
| return returnable; |
| } |
|
|
| |