Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export async function runFunction<T extends LambdaRequest, K>(
req: T,
fn: (req: T) => K | Promise<K>,
): Promise<K | LambdaHttpResponse> {
if (!req.timer.timers.has('lambda')) req.timer.start('lambda');

try {
return await fn(req);
} catch (error) {
Expand All @@ -49,14 +51,7 @@ export async function runFunction<T extends LambdaRequest, K>(
}
}

export async function execute<T extends LambdaRequest, K>(
req: T,
fn: (req: T) => K | Promise<K>,
): Promise<K | LambdaHttpResponse> {
req.timer.start('lambda');

const res = await runFunction(req, fn);

function after<T extends LambdaRequest, K>(req: T, res: K): K | LambdaHttpResponse {
let status = 200;
if (LambdaHttpResponse.is(res)) {
status = res.status;
Expand All @@ -70,7 +65,7 @@ export async function execute<T extends LambdaRequest, K>(

if (versionInfo.hash) req.set('package', versionInfo);

const duration = req.timer.end('lambda');
const duration = req.timer.timers.has('lambda') ? req.timer.end('lambda') : 0;
req.set('unfinished', req.timer.unfinished);
req.set('duration', duration);

Expand Down Expand Up @@ -154,10 +149,14 @@ export class lf {
const lambdaId = context.awsRequestId;
req.set('aws', { lambdaId });

execute(req, fn).then((res) => {
if (opts.rejectOnError && LambdaHttpResponse.is(res)) {
if (req.logContext['err']) return callback(req.logContext['err'] as Error);
if (res.status > 399) return callback(req.toResponse(res) as unknown as string);
runFunction(req, fn).then((res) => {
after(req, res);

if (LambdaHttpResponse.is(res)) {
if (opts.rejectOnError) {
if (req.logContext['err']) return callback(req.logContext['err'] as Error);
if (res.status > 399) return callback(req.toResponse(res) as unknown as string);
}
}
return callback(null, req.toResponse(res));
});
Expand Down Expand Up @@ -202,6 +201,8 @@ export class lf {

if (lf.ServerName) res.header(HttpHeader.Server, `${lf.ServerName}-${version}`);

after(req, res);

callback(null, req.toResponse(res));
});
}
Expand Down
26 changes: 24 additions & 2 deletions src/http/__test__/router.hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Context } from 'aws-lambda';
import { ALBResult, Context } from 'aws-lambda';
import o from 'ospec';
import sinon from 'sinon';
import { UrlExample } from '../../__test__/examples.js';
import { lf } from '../../function.js';
import { AlbExample, UrlExample } from '../../__test__/examples.js';
import { fakeLog } from '../../__test__/log.js';
import { LambdaUrlRequest } from '../request.url.js';
import { LambdaHttpResponse } from '../response.http.js';
Expand Down Expand Up @@ -94,5 +95,26 @@ o.spec('RouterHook', () => {
o(res.status).equals(500);
o(res.statusDescription).equals('Internal Server Error');
});

o('should log after the response hook', async () => {
fakeLog.logs = [];
const http = lf.http(fakeLog);

http.router.hook('response', (req, res) => {
if (res.status !== 404) throw new Error('status should be 404');
res.status = 200; // Convert the response to a 200!
req.set('logParam', 'response'); // Add a new log parameter to be logged
});

const res = await new Promise<ALBResult>((r) => http(AlbExample, fakeContext, (err, res) => r(res as ALBResult)));

o(fakeLog.logs.length).equals(1);
const [firstLog] = fakeLog.logs;
o(firstLog.logParam).equals('response');
o(firstLog['@type']).equals('report');
o(firstLog['status']).equals(200);

o(res.statusCode).equals(200);
});
});
});
6 changes: 3 additions & 3 deletions src/http/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execute, runFunction } from '../function.js';
import FindMyWay from 'find-my-way';
import { runFunction } from '../function.js';
import { LambdaHttpRequest, RequestTypes } from './request.http.js';
import { LambdaHttpResponse } from './response.http.js';
import FindMyWay from 'find-my-way';

export type Route<T extends RequestTypes = RequestTypes> = (
req: LambdaHttpRequest<T>,
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Router {
this.router.on(method, path, async (req: unknown, res, params) => {
if (!(req instanceof LambdaHttpRequest)) return new LambdaHttpResponse(500, 'Internal server error');
req.params = params;
return execute(req, fn);
return runFunction(req, fn);
});
}

Expand Down