From 49bb7cf66518d4b49baf883a16ee1fe1ed8aed28 Mon Sep 17 00:00:00 2001 From: Yosi Attias Date: Sun, 9 Jan 2022 19:52:29 +0200 Subject: [PATCH] fix(uws): expose additional uWebSockets.js options (#634) You can now pass additional options: ```js const { App } = require("uWebSockets.js"); const { uServer } = require("engine.io"); const app = new App(); const server = new uServer(); server.attach(app, { compression: uWS.DEDICATED_COMPRESSOR_128KB, // defaults to none idleTimeout: 60, // defaults to 120 maxBackpressure: 8 * 1024 // defaults to 1024 * 1024 }); app.listen(3000); ``` Related: https://github.com/socketio/engine.io/issues/633 --- lib/userver.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/userver.ts b/lib/userver.ts index d28b5ff1..38c9b5a8 100644 --- a/lib/userver.ts +++ b/lib/userver.ts @@ -1,10 +1,28 @@ import debugModule from "debug"; import { AttachOptions, BaseServer, Server } from "./server"; -import { HttpRequest, HttpResponse } from "uWebSockets.js"; +import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js"; import transports from "./transports-uws"; const debug = debugModule("engine:uws"); +export interface uOptions { + /** + * What permessage-deflate compression to use. uWS.DISABLED, uWS.SHARED_COMPRESSOR or any of the uWS.DEDICATED_COMPRESSOR_xxxKB. + * @default uWS.DISABLED + */ + compression?: number; + /** + * Maximum amount of seconds that may pass without sending or getting a message. Connection is closed if this timeout passes. Resolution (granularity) for timeouts are typically 4 seconds, rounded to closest. Disable by using 0. + * @default 120 + */ + idleTimeout?: number; + /** + * Maximum length of allowed backpressure per socket when publishing or sending messages. Slow receivers with too high backpressure will be skipped until they catch up or timeout. + * @default 1024 * 1024 + */ + maxBackpressure?: number; +} + export class uServer extends BaseServer { protected init() {} protected cleanup() {} @@ -43,13 +61,18 @@ export class uServer extends BaseServer { * @param app * @param options */ - public attach(app /* : TemplatedApp */, options: AttachOptions = {}) { + public attach( + app /* : TemplatedApp */, + options: AttachOptions & uOptions = {} + ) { const path = (options.path || "/engine.io").replace(/\/$/, "") + "/"; - - app + (app as TemplatedApp) .any(path, this.handleRequest.bind(this)) // .ws(path, { + compression: options.compression, + idleTimeout: options.idleTimeout, + maxBackpressure: options.maxBackpressure, maxPayloadLength: this.opts.maxHttpBufferSize, upgrade: this.handleUpgrade.bind(this), open: ws => {