Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (req, res, next) => {
// Disable cache when env development
if (__development) return next();
const fullUrl = `${(req.headers["x-forwarded-protocol"] || req.headers["protocol"] || req.protocol)}://${(req.headers["x-host"] || req.headers["host"] || "")}${(req.originalUrl || req.url)}`;
const getExactRouteFromPath = require("../../utils/bundler").getExactRouteFromPath;
const bot = isBot(_.get(req, "headers.user-agent", ""));
let key = `__express__${fullUrl}`;
let headerKey = `__express__headers__${fullUrl}`;
if (bot) {
key = `BOT_${key}`;
headerKey = `BOT_${headerKey}`;
}
let cachedBody = mcache.get(key);
let cachedHeaders = mcache.get(headerKey) || {};
if (cachedBody) {
// eslint-disable-next-line
console.log(`Using cache to send ${bot?"(BOT)":""}: ${req.url}`);
_.each(cachedHeaders, (value, key) => {
res.setHeader(key, value);
requestLanguage,
supportedLangs: server.settings.app.supportedLangs,
initialNow: new Date().getTime(),
isProdApi: server.settings.app.api.isProd,
isQL: parseMemberCookie(state).ql === 'true',
memberId: parseMemberCookie(state).id, // deprecated, use member.id
// the member cookie is not structured the same way as the member object returned from /member/self
// be careful relying on it to have the same properties downstream
member: parseMemberCookie(state),
variants: getVariants(state),
entryPath: url.pathname, // the path that the user entered the app on
media: getMedia(userAgent, userAgentDevice),
browserId: parseBrowserIdCookie(state),
clientIp,
siftSessionId: parseSiftSessionCookie(state),
isBot: isBot(request.headers['user-agent']),
};
};
request: {
headers,
method,
uri: { href: url },
},
statusCode,
} = response;
const logBase = {
externalRequest: { headers, method, url }, // request to https://api.meetup.com/
};
if (
statusCode >= 500 || // REST API had an internal error
(method.toLowerCase() === 'get' && statusCode >= 400) // something fishy with a GET
) {
if (isBot(request.headers['user-agent'])) {
// don't log errors from bots - e.g. for deleted groups/events/whatever
return;
}
const logError = (statusCode < 500 ? logger.warn : logger.error).bind(logger);
let errorMessage;
try {
// well-behaved API errors return a JSON object with an `errors` array
const info = JSON.parse(body);
errorMessage = JSON.stringify(info.errors[0]) || body;
} catch (err) {
// probably not JSON, could be an HTML response
const titleContent = /<title>(.+?)<\/title>/.exec(body);
errorMessage = titleContent ? titleContent[1] : 'REST API error';
}
logError({
...logBase,</title>
const context = {
storage,
api,
pathname: req.path,
};
let html, statusCode = 200;
// Get seo details for the routes in an inherited manner
// i.e. get seo details of parent when feasible
let seoDetails = {};
let routerComponent = null;
const requestHost = `${(req.headers["x-forwarded-protocol"] || req.headers["protocol"] || req.protocol)}://${(req.headers["x-host"] || req.headers["host"] || "")}`;
const currentUrl = `${requestHost}${req.path}`;
const bot = isBot(_.get(req, "headers.user-agent", ""));
try {
// Also preload data required when asked
let promises = getPreloadDataPromises({
routes: currentRoutes,
storage,
api,
store,
host: requestHost
});
Promise.all(promises).then(() => {
// Once all data has been pre-loaded and processed
_.each(currentRoutes, r => {
seoDetails = _.defaults({}, _.get(r, "seo", {}), seoDetails);
router.get("/", async (context, next) => {
context.set("Content-Type", "text/html; charset=utf-8")
const encoding = context.acceptsEncodings(Object.keys(encodings)) as
| keyof typeof encodings
| false
if (encoding === false) return context.throw(406)
context.set("Content-Encoding", encoding)
const stream = encodings[encoding]()
context.body = stream
if (isBot(context.get("User-Agent"))) {
stream.write(templateStartBots)
} else {
stream.write(templateStart)
}
await new Promise((resolve, reject) => {
const nodeStream = renderToNodeStream()
nodeStream.on("data", chunk => stream.write(chunk))
nodeStream.once("end", resolve)
nodeStream.once("error", reject)
})
if (isBot(context.get("User-Agent"))) {
stream.write(templateEndBots)
} else {
export const goToLink: Handler = async (req, res, next) => {
const { host } = req.headers;
const reqestedId = req.params.id || req.body.id;
const address = reqestedId.replace("+", "");
const customDomain = host !== process.env.DEFAULT_DOMAIN && host;
const isBot = isbot(req.headers["user-agent"]);
let domain;
if (customDomain) {
domain = await getDomain({ address: customDomain });
}
const link = await findLink({ address, domain_id: domain && domain.id });
if (!link) {
if (host !== process.env.DEFAULT_DOMAIN) {
if (!domain || !domain.homepage) return next();
return res.redirect(301, domain.homepage);
}
return next();
}