From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- .../examples/express-session-parse/index.js | 101 +++++++++++++++++++++ .../examples/express-session-parse/package.json | 11 +++ .../examples/express-session-parse/public/app.js | 67 ++++++++++++++ .../express-session-parse/public/index.html | 24 +++++ 4 files changed, 203 insertions(+) create mode 100644 testing/xpcshell/node-ws/examples/express-session-parse/index.js create mode 100644 testing/xpcshell/node-ws/examples/express-session-parse/package.json create mode 100644 testing/xpcshell/node-ws/examples/express-session-parse/public/app.js create mode 100644 testing/xpcshell/node-ws/examples/express-session-parse/public/index.html (limited to 'testing/xpcshell/node-ws/examples/express-session-parse') diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/index.js b/testing/xpcshell/node-ws/examples/express-session-parse/index.js new file mode 100644 index 0000000000..b62a2e4a5f --- /dev/null +++ b/testing/xpcshell/node-ws/examples/express-session-parse/index.js @@ -0,0 +1,101 @@ +'use strict'; + +const session = require('express-session'); +const express = require('express'); +const http = require('http'); +const uuid = require('uuid'); + +const { WebSocketServer } = require('../..'); + +const app = express(); +const map = new Map(); + +// +// We need the same instance of the session parser in express and +// WebSocket server. +// +const sessionParser = session({ + saveUninitialized: false, + secret: '$eCuRiTy', + resave: false +}); + +// +// Serve static files from the 'public' folder. +// +app.use(express.static('public')); +app.use(sessionParser); + +app.post('/login', function (req, res) { + // + // "Log in" user and set userId to session. + // + const id = uuid.v4(); + + console.log(`Updating session for user ${id}`); + req.session.userId = id; + res.send({ result: 'OK', message: 'Session updated' }); +}); + +app.delete('/logout', function (request, response) { + const ws = map.get(request.session.userId); + + console.log('Destroying session'); + request.session.destroy(function () { + if (ws) ws.close(); + + response.send({ result: 'OK', message: 'Session destroyed' }); + }); +}); + +// +// Create an HTTP server. +// +const server = http.createServer(app); + +// +// Create a WebSocket server completely detached from the HTTP server. +// +const wss = new WebSocketServer({ clientTracking: false, noServer: true }); + +server.on('upgrade', function (request, socket, head) { + console.log('Parsing session from request...'); + + sessionParser(request, {}, () => { + if (!request.session.userId) { + socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); + socket.destroy(); + return; + } + + console.log('Session is parsed!'); + + wss.handleUpgrade(request, socket, head, function (ws) { + wss.emit('connection', ws, request); + }); + }); +}); + +wss.on('connection', function (ws, request) { + const userId = request.session.userId; + + map.set(userId, ws); + + ws.on('message', function (message) { + // + // Here we can now use session parameters. + // + console.log(`Received message ${message} from user ${userId}`); + }); + + ws.on('close', function () { + map.delete(userId); + }); +}); + +// +// Start the server. +// +server.listen(8080, function () { + console.log('Listening on http://localhost:8080'); +}); diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/package.json b/testing/xpcshell/node-ws/examples/express-session-parse/package.json new file mode 100644 index 0000000000..406706ce8a --- /dev/null +++ b/testing/xpcshell/node-ws/examples/express-session-parse/package.json @@ -0,0 +1,11 @@ +{ + "author": "", + "name": "express-session-parse", + "version": "0.0.0", + "repository": "websockets/ws", + "dependencies": { + "express": "^4.16.4", + "express-session": "^1.16.1", + "uuid": "^8.3.2" + } +} diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js b/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js new file mode 100644 index 0000000000..f70dc21835 --- /dev/null +++ b/testing/xpcshell/node-ws/examples/express-session-parse/public/app.js @@ -0,0 +1,67 @@ +(function () { + const messages = document.querySelector('#messages'); + const wsButton = document.querySelector('#wsButton'); + const wsSendButton = document.querySelector('#wsSendButton'); + const logout = document.querySelector('#logout'); + const login = document.querySelector('#login'); + + function showMessage(message) { + messages.textContent += `\n${message}`; + messages.scrollTop = messages.scrollHeight; + } + + function handleResponse(response) { + return response.ok + ? response.json().then((data) => JSON.stringify(data, null, 2)) + : Promise.reject(new Error('Unexpected response')); + } + + login.onclick = function () { + fetch('/login', { method: 'POST', credentials: 'same-origin' }) + .then(handleResponse) + .then(showMessage) + .catch(function (err) { + showMessage(err.message); + }); + }; + + logout.onclick = function () { + fetch('/logout', { method: 'DELETE', credentials: 'same-origin' }) + .then(handleResponse) + .then(showMessage) + .catch(function (err) { + showMessage(err.message); + }); + }; + + let ws; + + wsButton.onclick = function () { + if (ws) { + ws.onerror = ws.onopen = ws.onclose = null; + ws.close(); + } + + ws = new WebSocket(`ws://${location.host}`); + ws.onerror = function () { + showMessage('WebSocket error'); + }; + ws.onopen = function () { + showMessage('WebSocket connection established'); + }; + ws.onclose = function () { + showMessage('WebSocket connection closed'); + ws = null; + }; + }; + + wsSendButton.onclick = function () { + if (!ws) { + showMessage('No WebSocket connection'); + return; + } + + ws.send('Hello World!'); + showMessage('Sent "Hello World!"'); + }; +})(); diff --git a/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html b/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html new file mode 100644 index 0000000000..c07aa2e87a --- /dev/null +++ b/testing/xpcshell/node-ws/examples/express-session-parse/public/index.html @@ -0,0 +1,24 @@ + + + + + Express session demo + + +

Choose an action.

+ + + + +

+    
+  
+
-- 
cgit v1.2.3