handleWithExpress = app => async (req, res, context) => {
req.app = app;
req.context = context;
res.cookie = function(name, value, options) {
var opts = options || {};
var secret = this.req.secret;
var signed = opts.signed;
if (signed && !secret) {
throw new Error('cookieParser("secret") required for signed cookies');
}
var val =
typeof value === 'object' ? 'j:' + JSON.stringify(value) : String(value);
if (signed) {
val = 's:' + cookie_signature.sign(val, secret);
}
if ('maxAge' in opts) {
opts.expires = new Date(Date.now() + opts.maxAge);
opts.maxAge /= 1000;
}
if (opts.path == null) {
opts.path = '/';
}
opts.encode = encodeURIComponent;
return this.header('Set-Cookie', cookie.serialize(name, String(val), opts));
};
res.clearCookie = function(name, options) {
var opts = { path: '/', ...options, expires: new Date(1) };
this.cookie(name, '', opts);
};
try {
await app(req, res, err =>
err ? res.status(500).send(err.message) : res.status(404).end()
);
} catch (err) {
req.status(500).send(err.message);
}
}