|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 4 | +import bodyParser from 'body-parser'; |
| 5 | +import cors from 'cors'; |
| 6 | +import { randomUUID } from 'crypto'; |
| 7 | +import express from 'express'; |
| 8 | +import http from 'node:http'; |
| 9 | +import util from 'node:util'; |
| 10 | +import { WebSocketServer } from 'ws'; |
| 11 | + |
| 12 | +let server: http.Server | undefined; |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const wss = new WebSocketServer({ port: 8001 }); |
| 16 | + const waiters: Record<string, (data: unknown) => void> = {}; |
| 17 | + |
| 18 | + console.log('Running contract test harness adapter.'); |
| 19 | + wss.on('connection', async (ws) => { |
| 20 | + ws.on('error', console.error); |
| 21 | + |
| 22 | + ws.on('message', (stringData: string) => { |
| 23 | + const data = JSON.parse(stringData); |
| 24 | + if (Object.prototype.hasOwnProperty.call(waiters, data.reqId)) { |
| 25 | + waiters[data.reqId](data); |
| 26 | + delete waiters[data.reqId]; |
| 27 | + } else { |
| 28 | + console.error('Did not find outstanding request', data.reqId); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + const send = (data: { [key: string]: unknown; reqId: string }): Promise<any> => { |
| 33 | + let resolver: (data: unknown) => void; |
| 34 | + const waiter = new Promise((resolve) => { |
| 35 | + resolver = resolve; |
| 36 | + }); |
| 37 | + // @ts-expect-error The body of the above assignment runs sequentially. |
| 38 | + waiters[data.reqId] = resolver; |
| 39 | + ws.send(JSON.stringify(data)); |
| 40 | + return waiter; |
| 41 | + }; |
| 42 | + |
| 43 | + if (server) { |
| 44 | + await util.promisify(server.close).call(server); |
| 45 | + server = undefined; |
| 46 | + } |
| 47 | + |
| 48 | + const app = express(); |
| 49 | + |
| 50 | + const port = 8000; |
| 51 | + |
| 52 | + app.use( |
| 53 | + cors({ |
| 54 | + origin: '*', |
| 55 | + allowedHeaders: '*', |
| 56 | + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], |
| 57 | + }), |
| 58 | + ); |
| 59 | + app.use(bodyParser.json()); |
| 60 | + |
| 61 | + app.get('/', async (_req, res) => { |
| 62 | + const commandResult = await send({ command: 'getCapabilities', reqId: randomUUID() }); |
| 63 | + res.header('Content-Type', 'application/json'); |
| 64 | + res.json(commandResult); |
| 65 | + }); |
| 66 | + |
| 67 | + app.delete('/', () => { |
| 68 | + process.exit(); |
| 69 | + }); |
| 70 | + |
| 71 | + app.post('/', async (req, res) => { |
| 72 | + const commandResult = await send({ |
| 73 | + command: 'createClient', |
| 74 | + body: req.body, |
| 75 | + reqId: randomUUID(), |
| 76 | + }); |
| 77 | + if (commandResult.resourceUrl) { |
| 78 | + res.set('Location', commandResult.resourceUrl); |
| 79 | + } |
| 80 | + if (commandResult.status) { |
| 81 | + res.status(commandResult.status); |
| 82 | + } |
| 83 | + res.send(); |
| 84 | + }); |
| 85 | + |
| 86 | + app.post('/clients/:id', async (req, res) => { |
| 87 | + const commandResult = await send({ |
| 88 | + command: 'runCommand', |
| 89 | + id: req.params.id, |
| 90 | + body: req.body, |
| 91 | + reqId: randomUUID(), |
| 92 | + }); |
| 93 | + if (commandResult.status) { |
| 94 | + res.status(commandResult.status); |
| 95 | + } |
| 96 | + if (commandResult.body) { |
| 97 | + res.write(JSON.stringify(commandResult.body)); |
| 98 | + } |
| 99 | + res.send(); |
| 100 | + }); |
| 101 | + |
| 102 | + app.delete('/clients/:id', async (req, res) => { |
| 103 | + await send({ command: 'deleteClient', id: req.params.id, reqId: randomUUID() }); |
| 104 | + res.send(); |
| 105 | + }); |
| 106 | + |
| 107 | + server = app.listen(port, () => { |
| 108 | + console.log('Listening on port %d', port); |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
| 112 | +main(); |
0 commit comments