Skip to content

Commit

Permalink
fix(uws): expose additional uWebSockets.js options (#634)
Browse files Browse the repository at this point in the history
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: #633
  • Loading branch information
yosiat authored and darrachequesne committed Jan 14, 2022
1 parent 8b4d6a8 commit 49bb7cf
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/userver.ts
Original file line number Diff line number Diff line change
@@ -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() {}
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 49bb7cf

Please sign in to comment.