201 lines
6.4 KiB
JavaScript
201 lines
6.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
0 && (module.exports = {
|
|
ApiError: null,
|
|
COOKIE_NAME_PRERENDER_BYPASS: null,
|
|
COOKIE_NAME_PRERENDER_DATA: null,
|
|
RESPONSE_LIMIT_DEFAULT: null,
|
|
SYMBOL_CLEARED_COOKIES: null,
|
|
SYMBOL_PREVIEW_DATA: null,
|
|
checkIsOnDemandRevalidate: null,
|
|
clearPreviewData: null,
|
|
redirect: null,
|
|
sendError: null,
|
|
sendStatusCode: null,
|
|
setLazyProp: null,
|
|
wrapApiHandler: null
|
|
});
|
|
function _export(target, all) {
|
|
for(var name in all)Object.defineProperty(target, name, {
|
|
enumerable: true,
|
|
get: all[name]
|
|
});
|
|
}
|
|
_export(exports, {
|
|
ApiError: function() {
|
|
return ApiError;
|
|
},
|
|
COOKIE_NAME_PRERENDER_BYPASS: function() {
|
|
return COOKIE_NAME_PRERENDER_BYPASS;
|
|
},
|
|
COOKIE_NAME_PRERENDER_DATA: function() {
|
|
return COOKIE_NAME_PRERENDER_DATA;
|
|
},
|
|
RESPONSE_LIMIT_DEFAULT: function() {
|
|
return RESPONSE_LIMIT_DEFAULT;
|
|
},
|
|
SYMBOL_CLEARED_COOKIES: function() {
|
|
return SYMBOL_CLEARED_COOKIES;
|
|
},
|
|
SYMBOL_PREVIEW_DATA: function() {
|
|
return SYMBOL_PREVIEW_DATA;
|
|
},
|
|
checkIsOnDemandRevalidate: function() {
|
|
return checkIsOnDemandRevalidate;
|
|
},
|
|
clearPreviewData: function() {
|
|
return clearPreviewData;
|
|
},
|
|
redirect: function() {
|
|
return redirect;
|
|
},
|
|
sendError: function() {
|
|
return sendError;
|
|
},
|
|
sendStatusCode: function() {
|
|
return sendStatusCode;
|
|
},
|
|
setLazyProp: function() {
|
|
return setLazyProp;
|
|
},
|
|
wrapApiHandler: function() {
|
|
return wrapApiHandler;
|
|
}
|
|
});
|
|
const _headers = require("../web/spec-extension/adapters/headers");
|
|
const _constants = require("../../lib/constants");
|
|
const _tracer = require("../lib/trace/tracer");
|
|
const _constants1 = require("../lib/trace/constants");
|
|
function wrapApiHandler(page, handler) {
|
|
return (...args)=>{
|
|
(0, _tracer.getTracer)().setRootSpanAttribute('next.route', page);
|
|
// Call API route method
|
|
return (0, _tracer.getTracer)().trace(_constants1.NodeSpan.runHandler, {
|
|
spanName: `executing api route (pages) ${page}`
|
|
}, ()=>handler(...args));
|
|
};
|
|
}
|
|
function sendStatusCode(res, statusCode) {
|
|
res.statusCode = statusCode;
|
|
return res;
|
|
}
|
|
function redirect(res, statusOrUrl, url) {
|
|
if (typeof statusOrUrl === 'string') {
|
|
url = statusOrUrl;
|
|
statusOrUrl = 307;
|
|
}
|
|
if (typeof statusOrUrl !== 'number' || typeof url !== 'string') {
|
|
throw Object.defineProperty(new Error(`Invalid redirect arguments. Please use a single argument URL, e.g. res.redirect('/destination') or use a status code and URL, e.g. res.redirect(307, '/destination').`), "__NEXT_ERROR_CODE", {
|
|
value: "E389",
|
|
enumerable: false,
|
|
configurable: true
|
|
});
|
|
}
|
|
res.writeHead(statusOrUrl, {
|
|
Location: url
|
|
});
|
|
res.write(url);
|
|
res.end();
|
|
return res;
|
|
}
|
|
function checkIsOnDemandRevalidate(req, previewProps) {
|
|
const headers = _headers.HeadersAdapter.from(req.headers);
|
|
const previewModeId = headers.get(_constants.PRERENDER_REVALIDATE_HEADER);
|
|
const isOnDemandRevalidate = previewModeId === previewProps.previewModeId;
|
|
const revalidateOnlyGenerated = headers.has(_constants.PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER);
|
|
return {
|
|
isOnDemandRevalidate,
|
|
revalidateOnlyGenerated
|
|
};
|
|
}
|
|
const COOKIE_NAME_PRERENDER_BYPASS = `__prerender_bypass`;
|
|
const COOKIE_NAME_PRERENDER_DATA = `__next_preview_data`;
|
|
const RESPONSE_LIMIT_DEFAULT = 4 * 1024 * 1024;
|
|
const SYMBOL_PREVIEW_DATA = Symbol(COOKIE_NAME_PRERENDER_DATA);
|
|
const SYMBOL_CLEARED_COOKIES = Symbol(COOKIE_NAME_PRERENDER_BYPASS);
|
|
function clearPreviewData(res, options = {}) {
|
|
if (SYMBOL_CLEARED_COOKIES in res) {
|
|
return res;
|
|
}
|
|
const { serialize } = require('next/dist/compiled/cookie');
|
|
const previous = res.getHeader('Set-Cookie');
|
|
res.setHeader(`Set-Cookie`, [
|
|
...typeof previous === 'string' ? [
|
|
previous
|
|
] : Array.isArray(previous) ? previous : [],
|
|
serialize(COOKIE_NAME_PRERENDER_BYPASS, '', {
|
|
// To delete a cookie, set `expires` to a date in the past:
|
|
// https://tools.ietf.org/html/rfc6265#section-4.1.1
|
|
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
|
|
expires: new Date(0),
|
|
httpOnly: true,
|
|
sameSite: process.env.NODE_ENV !== 'development' ? 'none' : 'lax',
|
|
secure: process.env.NODE_ENV !== 'development',
|
|
path: '/',
|
|
...options.path !== undefined ? {
|
|
path: options.path
|
|
} : undefined
|
|
}),
|
|
serialize(COOKIE_NAME_PRERENDER_DATA, '', {
|
|
// To delete a cookie, set `expires` to a date in the past:
|
|
// https://tools.ietf.org/html/rfc6265#section-4.1.1
|
|
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
|
|
expires: new Date(0),
|
|
httpOnly: true,
|
|
sameSite: process.env.NODE_ENV !== 'development' ? 'none' : 'lax',
|
|
secure: process.env.NODE_ENV !== 'development',
|
|
path: '/',
|
|
...options.path !== undefined ? {
|
|
path: options.path
|
|
} : undefined
|
|
})
|
|
]);
|
|
Object.defineProperty(res, SYMBOL_CLEARED_COOKIES, {
|
|
value: true,
|
|
enumerable: false
|
|
});
|
|
return res;
|
|
}
|
|
class ApiError extends Error {
|
|
constructor(statusCode, message){
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
}
|
|
}
|
|
function sendError(res, statusCode, message) {
|
|
res.statusCode = statusCode;
|
|
res.statusMessage = message;
|
|
res.end(message);
|
|
}
|
|
function setLazyProp({ req }, prop, getter) {
|
|
const opts = {
|
|
configurable: true,
|
|
enumerable: true
|
|
};
|
|
const optsReset = {
|
|
...opts,
|
|
writable: true
|
|
};
|
|
Object.defineProperty(req, prop, {
|
|
...opts,
|
|
get: ()=>{
|
|
const value = getter();
|
|
// we set the property on the object to avoid recalculating it
|
|
Object.defineProperty(req, prop, {
|
|
...optsReset,
|
|
value
|
|
});
|
|
return value;
|
|
},
|
|
set: (value)=>{
|
|
Object.defineProperty(req, prop, {
|
|
...optsReset,
|
|
value
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
//# sourceMappingURL=index.js.map
|