Spaces:
Running
Running
File size: 8,145 Bytes
c592d77 | 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
MutableRequestCookiesAdapter: null,
ReadonlyRequestCookiesError: null,
RequestCookiesAdapter: null,
appendMutableCookies: null,
areCookiesMutableInCurrentPhase: null,
createCookiesWithMutableAccessCheck: null,
getModifiedCookieValues: null,
responseCookiesToRequestCookies: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
MutableRequestCookiesAdapter: function() {
return MutableRequestCookiesAdapter;
},
ReadonlyRequestCookiesError: function() {
return ReadonlyRequestCookiesError;
},
RequestCookiesAdapter: function() {
return RequestCookiesAdapter;
},
appendMutableCookies: function() {
return appendMutableCookies;
},
areCookiesMutableInCurrentPhase: function() {
return areCookiesMutableInCurrentPhase;
},
createCookiesWithMutableAccessCheck: function() {
return createCookiesWithMutableAccessCheck;
},
getModifiedCookieValues: function() {
return getModifiedCookieValues;
},
responseCookiesToRequestCookies: function() {
return responseCookiesToRequestCookies;
}
});
const _cookies = require("../cookies");
const _reflect = require("./reflect");
const _workasyncstorageexternal = require("../../../app-render/work-async-storage.external");
const _actionrevalidationkind = require("../../../../shared/lib/action-revalidation-kind");
class ReadonlyRequestCookiesError extends Error {
constructor(){
super('Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options');
}
static callable() {
throw new ReadonlyRequestCookiesError();
}
}
class RequestCookiesAdapter {
static seal(cookies) {
return new Proxy(cookies, {
get (target, prop, receiver) {
switch(prop){
case 'clear':
case 'delete':
case 'set':
return ReadonlyRequestCookiesError.callable;
default:
return _reflect.ReflectAdapter.get(target, prop, receiver);
}
}
});
}
}
const SYMBOL_MODIFY_COOKIE_VALUES = Symbol.for('next.mutated.cookies');
function getModifiedCookieValues(cookies) {
const modified = cookies[SYMBOL_MODIFY_COOKIE_VALUES];
if (!modified || !Array.isArray(modified) || modified.length === 0) {
return [];
}
return modified;
}
function appendMutableCookies(headers, mutableCookies) {
const modifiedCookieValues = getModifiedCookieValues(mutableCookies);
if (modifiedCookieValues.length === 0) {
return false;
}
// Return a new response that extends the response with
// the modified cookies as fallbacks. `res` cookies
// will still take precedence.
const resCookies = new _cookies.ResponseCookies(headers);
const returnedCookies = resCookies.getAll();
// Set the modified cookies as fallbacks.
for (const cookie of modifiedCookieValues){
resCookies.set(cookie);
}
// Set the original cookies as the final values.
for (const cookie of returnedCookies){
resCookies.set(cookie);
}
return true;
}
class MutableRequestCookiesAdapter {
static wrap(cookies, onUpdateCookies) {
const responseCookies = new _cookies.ResponseCookies(new Headers());
for (const cookie of cookies.getAll()){
responseCookies.set(cookie);
}
let modifiedValues = [];
const modifiedCookies = new Set();
const updateResponseCookies = ()=>{
// TODO-APP: change method of getting workStore
const workStore = _workasyncstorageexternal.workAsyncStorage.getStore();
if (workStore) {
workStore.pathWasRevalidated = _actionrevalidationkind.ActionDidRevalidateStaticAndDynamic;
}
const allCookies = responseCookies.getAll();
modifiedValues = allCookies.filter((c)=>modifiedCookies.has(c.name));
if (onUpdateCookies) {
const serializedCookies = [];
for (const cookie of modifiedValues){
const tempCookies = new _cookies.ResponseCookies(new Headers());
tempCookies.set(cookie);
serializedCookies.push(tempCookies.toString());
}
onUpdateCookies(serializedCookies);
}
};
const wrappedCookies = new Proxy(responseCookies, {
get (target, prop, receiver) {
switch(prop){
// A special symbol to get the modified cookie values
case SYMBOL_MODIFY_COOKIE_VALUES:
return modifiedValues;
// TODO: Throw error if trying to set a cookie after the response
// headers have been set.
case 'delete':
return function(...args) {
modifiedCookies.add(typeof args[0] === 'string' ? args[0] : args[0].name);
try {
target.delete(...args);
return wrappedCookies;
} finally{
updateResponseCookies();
}
};
case 'set':
return function(...args) {
modifiedCookies.add(typeof args[0] === 'string' ? args[0] : args[0].name);
try {
target.set(...args);
return wrappedCookies;
} finally{
updateResponseCookies();
}
};
default:
return _reflect.ReflectAdapter.get(target, prop, receiver);
}
}
});
return wrappedCookies;
}
}
function createCookiesWithMutableAccessCheck(requestStore) {
const wrappedCookies = new Proxy(requestStore.mutableCookies, {
get (target, prop, receiver) {
switch(prop){
case 'delete':
return function(...args) {
ensureCookiesAreStillMutable(requestStore, 'cookies().delete');
target.delete(...args);
return wrappedCookies;
};
case 'set':
return function(...args) {
ensureCookiesAreStillMutable(requestStore, 'cookies().set');
target.set(...args);
return wrappedCookies;
};
default:
return _reflect.ReflectAdapter.get(target, prop, receiver);
}
}
});
return wrappedCookies;
}
function areCookiesMutableInCurrentPhase(requestStore) {
return requestStore.phase === 'action';
}
/** Ensure that cookies() starts throwing on mutation
* if we changed phases and can no longer mutate.
*
* This can happen when going:
* 'render' -> 'after'
* 'action' -> 'render'
* */ function ensureCookiesAreStillMutable(requestStore, _callingExpression) {
if (!areCookiesMutableInCurrentPhase(requestStore)) {
// TODO: maybe we can give a more precise error message based on callingExpression?
throw new ReadonlyRequestCookiesError();
}
}
function responseCookiesToRequestCookies(responseCookies) {
const requestCookies = new _cookies.RequestCookies(new Headers());
for (const cookie of responseCookies.getAll()){
requestCookies.set(cookie);
}
return requestCookies;
}
//# sourceMappingURL=request-cookies.js.map |