summaryrefslogtreecommitdiffstats
path: root/test/wpt/server/websocket.mjs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/wpt/server/websocket.mjs46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/wpt/server/websocket.mjs b/test/wpt/server/websocket.mjs
new file mode 100644
index 0000000..cc8ce78
--- /dev/null
+++ b/test/wpt/server/websocket.mjs
@@ -0,0 +1,46 @@
+import { WebSocketServer } from 'ws'
+import { server } from './server.mjs'
+
+// The file router server handles sending the url, closing,
+// and sending messages back to the main process for us.
+// The types for WebSocketServer don't include a `request`
+// event, so I'm unsure if we can stop relying on server.
+
+const wss = new WebSocketServer({
+ server,
+ handleProtocols: (protocols) => [...protocols].join(', ')
+})
+
+wss.on('connection', (ws, request) => {
+ ws.on('message', (data, isBinary) => {
+ const str = data.toString('utf-8')
+
+ if (request.url === '/receive-many-with-backpressure') {
+ setTimeout(() => {
+ ws.send(str.length.toString(), { binary: false })
+ }, 100)
+ return
+ }
+
+ if (str === 'Goodbye') {
+ // Close-server-initiated-close.any.js sends a "Goodbye" message
+ // when it wants the server to close the connection.
+ ws.close(1000)
+ return
+ }
+
+ ws.send(data, { binary: isBinary })
+ })
+
+ // Some tests, such as `Create-blocked-port.any.js` do NOT
+ // close the connection automatically.
+ const timeout = setTimeout(() => {
+ if (ws.readyState !== ws.CLOSED && ws.readyState !== ws.CLOSING) {
+ ws.close()
+ }
+ }, 2500)
+
+ ws.on('close', () => {
+ clearTimeout(timeout)
+ })
+})