Files
order-notification-automation/tests/fixtures/signalr-hub.cjs
T

16 lines
3.0 KiB
JavaScript

'use strict';
const http=require('node:http'),crypto=require('node:crypto');
function frame(text){const bytes=Buffer.from(text);if(bytes.length<126)return Buffer.concat([Buffer.from([0x81,bytes.length]),bytes]);const header=Buffer.alloc(4);header[0]=0x81;header[1]=126;header.writeUInt16BE(bytes.length,2);return Buffer.concat([header,bytes]);}
async function signalrHub(t,{handshake=true,pings=true,authStatus=200}={}){
const sockets=new Set(),messages=[];let pingsSent=0;
const server=http.createServer((req,res)=>{if(req.url==='/'){res.end('<script>Yiez.clientId="fixture-token";</script>');return;}if(req.url==='/hubs/system/negotiate?negotiateVersion=1'){if(req.method!=='POST'||req.headers.authorization!=='Bearer fixture-token'){res.writeHead(401);return res.end();}res.writeHead(authStatus,{'Content-Type':'application/json'});return res.end(JSON.stringify({connectionToken:'fixture-connection',availableTransports:[{transport:'WebSockets'}]}));}res.writeHead(404);res.end();});
server.on('upgrade',(req,socket)=>{const url=new URL(req.url,'http://fixture');if(url.pathname!=='/hubs/system'||url.searchParams.get('access_token')!=='fixture-token'||url.searchParams.get('id')!=='fixture-connection'){socket.end('HTTP/1.1 401 Unauthorized\r\n\r\n');return;}
const accept=crypto.createHash('sha1').update(req.headers['sec-websocket-key']+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64');socket.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: '+accept+'\r\n\r\n');sockets.add(socket);let pending=Buffer.alloc(0);
socket.on('data',chunk=>{pending=Buffer.concat([pending,chunk]);while(pending.length>=2){let length=pending[1]&127,at=2;if(length===126){if(pending.length<4)return;length=pending.readUInt16BE(2);at=4;}if(length===127){socket.destroy();return;}const masked=!!(pending[1]&128);if(pending.length<at+(masked?4:0)+length)return;const opcode=pending[0]&15,mask=masked?pending.subarray(at,at+4):null;at+=masked?4:0;const data=Buffer.from(pending.subarray(at,at+length));if(mask)for(let i=0;i<data.length;i++)data[i]^=mask[i%4];pending=pending.subarray(at+length);if(opcode===8){socket.end(Buffer.from([0x88,0]));return;}if(opcode!==1)continue;for(const part of data.toString().split('\x1e').filter(Boolean)){const value=JSON.parse(part);messages.push(value);if(value.protocol&&handshake){socket.write(frame('{}\x1e'));if(pings){pingsSent++;socket.write(frame('{"type":6}\x1e'));}}}}});
socket.on('close',()=>sockets.delete(socket));socket.on('error',()=>{});
});
await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{for(const socket of sockets)socket.destroy();await new Promise(r=>server.close(r));});
return {baseURL:'http://127.0.0.1:'+server.address().port+'/',messages,sockets,get pingsSent(){return pingsSent;},emit(value){for(const socket of sockets)socket.write(frame(JSON.stringify(value)+'\x1e'));},raw(text){for(const socket of sockets)socket.write(frame(text));},disconnect(){for(const socket of sockets)socket.destroy();}};
}
module.exports={signalrHub};