UNPKG

@mockyeah/server

Version:

A powerful service mocking, recording, and playback utility.

54 lines (42 loc) 1.32 kB
const WebSocket = require('ws'); const proxyRecord = require('../app/lib/proxyRecord'); const withAdminServer = ({ app, instance }) => { if (!app.config.noWebSocket) { const wss = new WebSocket.Server({ server: instance.adminServer }); wss.on('connection', ws => { ws.on('message', message => { const action = JSON.parse(message); if (action.type === 'recordPush') { const { req, reqUrl, startTime, body, headers, status } = action.payload; proxyRecord({ app, req, reqUrl, startTime, body, headers, status }); } }); const onRecord = () => { ws.send(JSON.stringify({ type: 'record' })); }; const onRecordStop = () => { ws.send(JSON.stringify({ type: 'recordStop' })); }; app.on('record', onRecord); app.on('recordStop', onRecordStop); ws.on('close', () => { app.off('record', onRecord); app.off('recordStop', onRecordStop); }); ws.send(JSON.stringify({ type: 'connected' })); // In case we didn't connect before the record event, send it again. if (app.locals.recording) { onRecord(); } }); } }; module.exports = withAdminServer;