- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/ws/lib/websocket-server.js
r59329aa re29cc2e 2 2 3 3 const EventEmitter = require('events'); 4 const { createHash }= require('crypto');5 const { createServer, STATUS_CODES }= require('http');4 const crypto = require('crypto'); 5 const http = require('http'); 6 6 7 7 const PerMessageDeflate = require('./permessage-deflate'); 8 const extension = require('./extension'); 8 9 const WebSocket = require('./websocket'); 9 const { format, parse } = require('./extension'); 10 const { GUID, kWebSocket } = require('./constants'); 10 const { GUID } = require('./constants'); 11 11 12 12 const keyRegex = /^[+/0-9A-Za-z]{22}==$/; … … 22 22 * 23 23 * @param {Object} options Configuration options 24 * @param {Number} [options.backlog=511] The maximum length of the queue of 25 * pending connections 26 * @param {Boolean} [options.clientTracking=true] Specifies whether or not to 27 * track clients 28 * @param {Function} [options.handleProtocols] A hook to handle protocols 29 * @param {String} [options.host] The hostname where to bind the server 30 * @param {Number} [options.maxPayload=104857600] The maximum allowed message 31 * size 32 * @param {Boolean} [options.noServer=false] Enable no server mode 33 * @param {String} [options.path] Accept only connections matching this path 34 * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable 24 * @param {Number} options.backlog The maximum length of the queue of pending 25 * connections 26 * @param {Boolean} options.clientTracking Specifies whether or not to track 27 * clients 28 * @param {Function} options.handleProtocols An hook to handle protocols 29 * @param {String} options.host The hostname where to bind the server 30 * @param {Number} options.maxPayload The maximum allowed message size 31 * @param {Boolean} options.noServer Enable no server mode 32 * @param {String} options.path Accept only connections matching this path 33 * @param {(Boolean|Object)} options.perMessageDeflate Enable/disable 35 34 * permessage-deflate 36 * @param {Number} [options.port]The port where to bind the server37 * @param {http.Server} [options.server]A pre-created HTTP/S server to use38 * @param {Function} [options.verifyClient] Ahook to reject connections39 * @param {Function} [callback]A listener for the `listening` event35 * @param {Number} options.port The port where to bind the server 36 * @param {http.Server} options.server A pre-created HTTP/S server to use 37 * @param {Function} options.verifyClient An hook to reject connections 38 * @param {Function} callback A listener for the `listening` event 40 39 */ 41 40 constructor(options, callback) { 42 41 super(); 43 42 44 options = { 45 maxPayload: 100 * 1024 * 1024, 46 perMessageDeflate: false, 47 handleProtocols: null, 48 clientTracking: true, 49 verifyClient: null, 50 noServer: false, 51 backlog: null, // use default (511 as implemented in net.js) 52 server: null, 53 host: null, 54 path: null, 55 port: null, 56 ...options 57 }; 43 options = Object.assign( 44 { 45 maxPayload: 100 * 1024 * 1024, 46 perMessageDeflate: false, 47 handleProtocols: null, 48 clientTracking: true, 49 verifyClient: null, 50 noServer: false, 51 backlog: null, // use default (511 as implemented in net.js) 52 server: null, 53 host: null, 54 path: null, 55 port: null 56 }, 57 options 58 ); 58 59 59 60 if (options.port == null && !options.server && !options.noServer) { … … 64 65 65 66 if (options.port != null) { 66 this._server = createServer((req, res) => {67 const body = STATUS_CODES[426];67 this._server = http.createServer((req, res) => { 68 const body = http.STATUS_CODES[426]; 68 69 69 70 res.writeHead(426, { … … 84 85 85 86 if (this._server) { 86 const emitConnection = this.emit.bind(this, 'connection');87 88 87 this._removeListeners = addListeners(this._server, { 89 88 listening: this.emit.bind(this, 'listening'), 90 89 error: this.emit.bind(this, 'error'), 91 90 upgrade: (req, socket, head) => { 92 this.handleUpgrade(req, socket, head, emitConnection); 91 this.handleUpgrade(req, socket, head, (ws) => { 92 this.emit('connection', ws, req); 93 }); 93 94 } 94 95 }); … … 121 122 * Close the server. 122 123 * 123 * @param {Function} [cb]Callback124 * @param {Function} cb Callback 124 125 * @public 125 126 */ … … 208 209 209 210 try { 210 const offers = parse(req.headers['sec-websocket-extensions']);211 const offers = extension.parse(req.headers['sec-websocket-extensions']); 211 212 212 213 if (offers[PerMessageDeflate.extensionName]) { … … 226 227 origin: 227 228 req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`], 228 secure: !!(req. socket.authorized || req.socket.encrypted),229 secure: !!(req.connection.authorized || req.connection.encrypted), 229 230 req 230 231 }; … … 256 257 * @param {Buffer} head The first packet of the upgraded stream 257 258 * @param {Function} cb Callback 258 * @throws {Error} If called more than once with the same socket259 259 * @private 260 260 */ … … 265 265 if (!socket.readable || !socket.writable) return socket.destroy(); 266 266 267 if (socket[kWebSocket]) { 268 throw new Error( 269 'server.handleUpgrade() was called more than once with the same ' + 270 'socket, possibly due to a misconfiguration' 271 ); 272 } 273 274 const digest = createHash('sha1') 267 const digest = crypto 268 .createHash('sha1') 275 269 .update(key + GUID) 276 270 .digest('base64'); … … 284 278 285 279 const ws = new WebSocket(null); 286 letprotocol = req.headers['sec-websocket-protocol'];280 var protocol = req.headers['sec-websocket-protocol']; 287 281 288 282 if (protocol) { … … 300 294 if (protocol) { 301 295 headers.push(`Sec-WebSocket-Protocol: ${protocol}`); 302 ws. _protocol = protocol;296 ws.protocol = protocol; 303 297 } 304 298 } … … 306 300 if (extensions[PerMessageDeflate.extensionName]) { 307 301 const params = extensions[PerMessageDeflate.extensionName].params; 308 const value = format({302 const value = extension.format({ 309 303 [PerMessageDeflate.extensionName]: [params] 310 304 }); … … 328 322 } 329 323 330 cb(ws , req);324 cb(ws); 331 325 } 332 326 } … … 340 334 * @param {EventEmitter} server The event emitter 341 335 * @param {Object.<String, Function>} map The listeners to add 342 * @return {Function} A function that will remove the added listeners when 343 * called 336 * @return {Function} A function that will remove the added listeners when called 344 337 * @private 345 338 */ … … 384 377 function abortHandshake(socket, code, message, headers) { 385 378 if (socket.writable) { 386 message = message || STATUS_CODES[code]; 387 headers = { 388 Connection: 'close', 389 'Content-Type': 'text/html', 390 'Content-Length': Buffer.byteLength(message), 391 ...headers 392 }; 379 message = message || http.STATUS_CODES[code]; 380 headers = Object.assign( 381 { 382 Connection: 'close', 383 'Content-type': 'text/html', 384 'Content-Length': Buffer.byteLength(message) 385 }, 386 headers 387 ); 393 388 394 389 socket.write( 395 `HTTP/1.1 ${code} ${ STATUS_CODES[code]}\r\n` +390 `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` + 396 391 Object.keys(headers) 397 392 .map((h) => `${h}: ${headers[h]}`)
Note:
See TracChangeset
for help on using the changeset viewer.