Skip to content

Commit

Permalink
Merge pull request #32 from theweiweiway/master
Browse files Browse the repository at this point in the history
optional object parameter to `expressify(app, options)` to customize ready body timeout
  • Loading branch information
endel committed Oct 23, 2023
2 parents b5fb933 + 2797aac commit afe0368
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type EngineCallback = (path: string, options: object, callback: RenderCallback)

// const rootRegexpPath = pathToRegexp("/", [], { end: false, strict: false });

export type ApplicationOptions = { readBodyMaxTime?: number }
export class Application extends EventEmitter {
// middlewares: MiddlewareList = [];

Expand All @@ -39,7 +40,7 @@ export class Application extends EventEmitter {

private _router: any;

constructor(protected uWSApp: uWS.TemplatedApp) {
constructor(protected uWSApp: uWS.TemplatedApp, public opts?: ApplicationOptions) {
super();

// Alias app.delete() = app.del()
Expand Down
12 changes: 8 additions & 4 deletions src/IncomingMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import EventEmitter from "events";
import { URL } from "url";
import { Socket } from "./Socket";
import { request } from "express";

const READ_BODY_MAX_TIME = 500;
import { Application } from "./Application";

export class IncomingMessage extends EventEmitter implements http.IncomingMessage {
public url: string;
Expand All @@ -26,6 +25,7 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
private _rawbody: any;
private _remoteAddress: ArrayBuffer;
private _readableState = { pipes: [] };
private _readBodyMaxTime = 500;

public aborted: boolean;

Expand All @@ -38,7 +38,7 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
private req: uWS.HttpRequest,
private res: uWS.HttpResponse,
private parameterNames: string[],
private app: any,
private app: Application
) {
super();

Expand All @@ -63,6 +63,10 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
}

this.#_originalUrlParsed = new URL(`http://server${this.url}`);

if (this.app.opts?.readBodyMaxTime) {
this._readBodyMaxTime = this.app.opts.readBodyMaxTime;
}
}

get ip () {
Expand Down Expand Up @@ -163,7 +167,7 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
this.headers['content-length'] = String(body.length);
}
reject();
}, READ_BODY_MAX_TIME);
}, this._readBodyMaxTime);

this.res.onData((arrayBuffer, isLast) => {
const chunk = Buffer.from(arrayBuffer);
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import uWS from "uWebSockets.js";
import { Application } from "./Application";
import { Application, ApplicationOptions } from "./Application";

export default function (app: uWS.TemplatedApp) {
return new Application(app);
export default function (
app: uWS.TemplatedApp,
options?: ApplicationOptions
) {
return new Application(app, options);
}

export { Application };
Expand Down

0 comments on commit afe0368

Please sign in to comment.