Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/abort-controller.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
import { Response } from 'express';
import { AbortControllerOptions, AbortControllerRequest } from './types';
import { ABORT_CONTROLLER, ABORT_SIGNAL } from './constants';

const LOG_MESSAGES = {
CLIENT_DISCONNECTED: 'Client disconnected, aborting request',
Expand All @@ -24,8 +25,8 @@ export class AbortControllerMiddleware implements NestMiddleware {
const timeout = AbortControllerMiddleware.options.timeout ?? 30000;
const enableLogging = AbortControllerMiddleware.options.enableLogging ?? false;

req.abortController = controller;
req.abortSignal = controller.signal;
req[ABORT_CONTROLLER] = controller;
req[ABORT_SIGNAL] = controller.signal;

req.on('close', () => {
if (enableLogging) {
Expand Down
3 changes: 2 additions & 1 deletion src/abort-signal.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { ABORT_SIGNAL } from './constants';

export const NestAbortSignal = createParamDecorator((_, ctx: ExecutionContext): AbortSignal => {
const req = ctx.switchToHttp().getRequest();
return req.abortSignal;
return req[ABORT_SIGNAL];
});
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ABORT_CONTROLLER = Symbol('abortController');
export const ABORT_SIGNAL = Symbol('abortSignal');
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './abort-signal.decorator';
export * from './abort.module';
export * from './types';
export * from './throw-if-aborted';
export * from './constants';
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Request } from 'express';
import { ABORT_CONTROLLER, ABORT_SIGNAL } from './constants';

export interface AbortControllerRequest extends Request {
abortController?: AbortController;
abortSignal?: AbortSignal;
[ABORT_CONTROLLER]?: AbortController;
[ABORT_SIGNAL]?: AbortSignal;
}

export interface AbortControllerOptions {
Expand Down