Polyfill WebSocket on Node

This commit is contained in:
Andras Schmelczer 2025-03-28 21:27:09 +00:00
parent fcd0cb84fe
commit 85648c9466
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 1427 additions and 272 deletions

File diff suppressed because it is too large Load diff

View file

@ -23,6 +23,7 @@
"@types/jest": "^29.5.14",
"@types/node": "^22.13.10",
"jest": "^29.7.0",
"sync_lib": "file:../../backend/sync_lib/pkg",
"ts-jest": "^29.2.6",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
@ -30,6 +31,6 @@
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1",
"webpack-merge": "^6.0.1",
"sync_lib": "file:../../backend/sync_lib/pkg"
"ws": "^8.18.1"
}
}
}

View file

@ -249,6 +249,15 @@ export class Syncer {
const wsUri = new URL(settings.remoteUri);
wsUri.protocol = wsUri.protocol === "https" ? "wss" : "ws";
wsUri.pathname = `/vaults/${settings.vaultName}/ws`;
if (
typeof globalThis !== "undefined" &&
typeof globalThis.WebSocket === "undefined"
) {
// polyfill for WebSocket in Node.js
globalThis.WebSocket = require("ws");
}
this.applyRemoteChangesWebSocket = new WebSocket(wsUri);
this.applyRemoteChangesWebSocket.onmessage = (event): void =>

View file

@ -20,7 +20,7 @@ const common = {
minimize: false
},
resolve: {
extensions: [".ts"],
extensions: [".ts", ".js"],
alias: {
root: __dirname,
src: path.resolve(__dirname, "src")
@ -42,6 +42,11 @@ module.exports = [
type: "umd"
},
globalObject: "this"
},
resolve: {
fallback: {
ws: false // Exclude `ws` from the browser bundle
}
}
}),
merge(common, {
@ -50,6 +55,10 @@ module.exports = [
path: path.resolve(__dirname, "dist"),
filename: "sync-client.node.js",
libraryTarget: "commonjs2"
},
externals: {
bufferutil: "bufferutil",
"utf-8-validate": "utf-8-validate" // required for ws: https://github.com/websockets/ws/issues/2245#issuecomment-2250318733
}
})
];

View file

@ -18,6 +18,7 @@
"typescript": "5.8.2",
"uuid": "^11.1.0",
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1"
"webpack-cli": "^6.0.1",
"bufferutil": "^4.0.9"
}
}
}