Skip to content

Commit

Permalink
Prevent previous request processing result to affect the next request
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Oct 15, 2020
1 parent 39c5de6 commit 1d4ce83
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/http/http-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,37 @@ export function httpListener<TRequest extends IncomingMessage, TResponse extends
}

const { log = console } = config;
const [incomingHandler, whenResponded] = incomingHttpHandler(fullHttpHandler(config, handler));
const incomingHandler = incomingHttpHandler(fullHttpHandler(config, handler));
const processor = requestProcessor<IncomingHttpMeans<TRequest, TResponse>>({
handler: incomingHandler,
next(handler, context): Promise<boolean> {
next(
handler,
context,
): Promise<boolean> {

const { response } = context;

return Promise.race([
whenResponded,
Promise.resolve(context)
.then(handler)
.then(() => response.writableEnded),
]);
return Promise.resolve(context)
.then(handler)
.then(() => response.writableEnded);
},
});

return (request: TRequest, response: TResponse): void => {
processor({ request, response, log })
.catch(
error => log.error(`[${request.method} ${request.url}]`, 'Unhandled error', error),
);
new Promise<boolean>((onResponse, onError) => {
processor({
request,
response,
log,
onResponse,
onError,
}).then(
onResponse,
onError,
);
}).catch(error => {
log.error(`[${request.method} ${request.url}]`, 'Unhandled error', error);
});
};
}

Expand All @@ -131,26 +141,22 @@ interface IncomingHttpMeans<TRequest extends IncomingMessage, TResponse extends
readonly request: TRequest;
readonly response: TResponse;
readonly log: Console;
onResponse(this: void): void;
onError(this: void, error: any): void;
}

/**
* @internal
*/
function incomingHttpHandler<TRequest extends IncomingMessage, TResponse extends ServerResponse>(
handler: RequestHandler<HttpMeans<TRequest, TResponse>>,
): [RequestHandler<IncomingHttpMeans<TRequest, TResponse>>, Promise<boolean>] {

let onResponse: () => void;
let onError: (error: any) => void;
const whenResponded = new Promise<boolean>((resolve, reject) => {
onResponse = () => resolve(true);
onError = reject;
});

const incomingHandler: RequestHandler<IncomingHttpMeans<TRequest, TResponse>> = ({
): RequestHandler<IncomingHttpMeans<TRequest, TResponse>> {
return ({
request,
response,
next,
onResponse,
onError,
}) => {
response.once('error', onError);
response.once('finish', onResponse);
Expand Down Expand Up @@ -187,8 +193,6 @@ function incomingHttpHandler<TRequest extends IncomingMessage, TResponse extends
},
);
};

return [incomingHandler, whenResponded];
}

/**
Expand Down

0 comments on commit 1d4ce83

Please sign in to comment.