Skip to content

Commit

Permalink
feat: migrate to ws-multiplex
Browse files Browse the repository at this point in the history
Switches to the ws-multiplex package for websocket multiplexing.

BREAKING CHANGE: This brings in a protocol version bump and is not compatible with older clients.
  • Loading branch information
fredriklindberg committed Aug 30, 2023
1 parent 2c58edf commit d19a8a3
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 1,005 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build.env
build.js
exposrd.mjs
dist
.pkg-cache
out
8 changes: 8 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extension": ["js", "ts"],
"require": "ts-node/register",
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Makefile"
],
"dependencies": {
"@exposr/ws-multiplex": "^1.2.0",
"better-sqlite3": "^8.3.0",
"content-type": "^1.0.5",
"koa": "^2.14.1",
Expand All @@ -40,18 +41,26 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@types/node": "^20.5.0",
"@types/ws": "^8.5.5",
"commit-and-tag-version": "^11.2.1",
"mocha": "^10.2.0",
"rollup": "^3.18.0",
"sinon": "^15.0.3",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"yaml": "^2.2.2"
},
"scripts": {
"prepack": "yarn run version && yarn run bundle",
"postpack": "rm build.env exposrd.mjs",
"release": "standard-version",
"version": "scripts/build-version.sh",
"bundle": "rollup exposrd.js --file exposrd.mjs --format es -p @rollup/plugin-commonjs -p @rollup/plugin-json",
"prebuild": "rm -fr out && mkdir out && cp package.json out",
"build": "tsc",
"dev": "ts-node-esm exposrd.js",
"prebundle": "yarn run build",
"bundle": "rollup out/exposrd.js --file exposrd.mjs --format es -p @rollup/plugin-commonjs -p @rollup/plugin-json",
"dist": "scripts/build-dist.sh",
"test": "scripts/run-test.sh"
}
Expand Down
1 change: 1 addition & 0 deletions src/ingress/http-ingress.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class HttpIngress {
_createAgent(tunnelId) {
const agent = new Agent({
keepAlive: true,
timeout: this._agent_ttl * 1000,
});

agent.createConnection = (opts, callback) => {
Expand Down
1 change: 0 additions & 1 deletion src/package.cjs

This file was deleted.

15 changes: 11 additions & 4 deletions src/transport/ws/ws-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class WebSocketEndpoint {
this.tunnelService = new TunnelService();
this.wss = new WebSocketServer({ noServer: true });
this.destroyed = false;
this.connections = [];

this._upgradeHandler = this.httpListener.use('upgrade', { logger: this.logger }, async (ctx, next) => {
if (!await this.handleUpgrade(ctx.req, ctx.sock, ctx.head)) {
Expand Down Expand Up @@ -56,9 +57,13 @@ class WebSocketEndpoint {
async destroy() {
this.destroyed = true;
this.httpListener.removeHandler('upgrade', this._upgradeHandler);
this.wss.clients.forEach((client) => {
client.close(1001, "Server shutting down");
});
for (const connection of this.connections) {
const {wst, ws} = connection;
await wst.destroy();
ws.close(1001, "Server shutting down");
}
this.connections = [];
this.wss.close();
return Promise.allSettled([
this.tunnelService.destroy(),
Listener.release('http', this.opts.port),
Expand Down Expand Up @@ -167,8 +172,10 @@ class WebSocketEndpoint {
operation: 'upgrade',
msg: 'failed to connect transport'
});
ws.close(1008, "unable to establish tunnel");
transport.destroy();
ws.close(1008, "unable to establish tunnel");
} else {
this.connections.push({transport, ws});
}
});
return true;
Expand Down
Loading

0 comments on commit d19a8a3

Please sign in to comment.