Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(context) => {
const { content } = context;
const res = setdefault(context, 'response', {});
// this is the main function (normally it would be the template function)
// but we use it to assert that pre-processing has happened
assert.ok(content.body);
assert.ok(content.mdast);
assert.ok(content.meta);
assert.equal(content.meta.template, 'Medium');
assert.equal(content.intro, 'Project Helix');
assert.equal(content.title, 'Bill, Welcome to the future');
// and return a different status code
res.status = 201;
res.body = { foo: 'bar' };
setdefault(res, 'headers', {})['Content-Type'] = 'text/plain+json';
},
{},
// if a status is already default, keep it.
if (res.status) {
return;
}
if (!err) {
res.status = 200;
return;
}
// error handling
logger.debug('context.error -> 500');
res.status = 500;
res.body = '';
if (!isProduction() || setdefault(request, 'headers', {})['x-debug']) {
res.body = `<h1>500</h1><pre>${err}</pre>`;
headers['Content-Type'] = 'text/html';
}
};
// if there is a github token, send it in the Authorization header
const token = secrets.GITHUB_TOKEN || (request.headers ? request.headers['x-github-token'] : '');
if (token) {
setdefault(options, 'headers', {
Authorization: `token ${token}`,
});
}
logger.debug(`fetching Markdown from ${options.uri}`);
try {
content.body = await client(options);
if (branch && branch !== ref) {
setdefault(content, 'sources', []).push(uri(rootPath, owner, repo, branch, path));
} else {
setdefault(content, 'sources', []).push(options.uri);
}
} catch (e) {
if (e.statusCode === 404) {
logger.error(`Could not find Markdown at ${options.uri}`);
setdefault(context, 'response', {}).status = 404;
} else if ((e.response && e.response.elapsedTime && e.response.elapsedTime > timeout) || (e.cause && e.cause.code && (e.cause.code === 'ESOCKETTIMEDOUT' || e.cause.code === 'ETIMEDOUT'))) {
// return gateway timeout
logger.error(`Gateway timeout of ${timeout} milliseconds exceeded for ${options.uri}`);
setdefault(context, 'response', {}).status = 504;
} else {
logger.error(`Error while fetching Markdown from ${options.uri} with the following `
+ `options:\n${inspect(options, { depth: null })}`);
setdefault(context, 'response', {}).status = 502;
}
context.error = e;
}
return ({ response = {}, error }, { logger, request = {} }) => {
// if a status is already default, keep it.
if (response.status) {
return {};
}
if (!error) {
return {
response: {
status: 200,
},
};
}
// error handling
logger.debug('context.error -> 500');
if (prod && !setdefault(request, 'headers', {})['x-debug']) {
return {
response: {
status: 500,
body: '',
},
};
}
return setVerboseError(error);
};
}
// if a status is already default, keep it.
if (res.status) {
return;
}
if (!err) {
res.status = 200;
return;
}
logger.debug('context.error -> 500');
res.status = 500;
res.body = '';
if (!isProduction() || setdefault(request, 'headers', {})['x-debug']) {
res.body = `<code>500</code>${err.trim()}`;
headers['Content-Type'] = 'application/xml';
}
}
const selectStatus = (context, { logger, request = {} }) => {
const res = setdefault(context, 'response', {});
const headers = setdefault(res, 'headers', {});
const err = context.error;
// if a status is already default, keep it.
if (res.status) {
return;
}
if (!err) {
res.status = 200;
return;
}
// error handling
logger.debug('context.error -> 500');
res.status = 500;
async function record(context, action, index, name) {
const debug = setdefault(action, 'debug', { contextDumps: [] });
const info = {
index,
name: name ? name.split(':').pop() : 'undef',
time: new Date(),
json: JSON.stringify(context, null, 2),
};
debug.contextDumps.push(info);
if (action.logger.level === 'silly' && !disableDisk()) {
await ensureDumpDir(context, action);
await writeDump(context, action, info);
}
}
function emit(context, { secrets, logger }) {
const cont = setdefault(context, 'content', {});
const res = setdefault(context, 'response', {});
if (res.body) {
logger.debug('Response body already exists');
return;
}
if (!cont.xml) {
logger.debug('No XML to emit');
return;
}
logger.debug(`Emitting XML from ${typeof cont.xml}`);
const xml = builder.create(cont.xml, { encoding: 'utf-8' });
res.body = xml.end({ pretty: !!secrets.XML_PRETTY });
}
return function setmime(context) {
const res = setdefault(context, 'response', {});
const head = setdefault(res, 'headers', {});
setdefault(head, 'Content-Type', mime);
};
}
function selectstrain(context, { request, logger }) {
const cont = setdefault(context, 'content', { mdast: {} });
const params = request.params || {};
const { strain } = params;
const sections = cont.mdast.children;
if (!strain || !sections) {
return;
}
logger.debug(`Filtering sections not intended for strain ${strain}`);
each(sections, (section) => {
const meta = setdefault(section, 'meta', {});
const strainList = type(meta.strain) === Array ? meta.strain : [meta.strain];
meta.hidden = Boolean(meta.strain && meta.strain !== [] && !strainList.includes(strain));
});
}