|
| 1 | +import bodyParser from 'body-parser'; |
| 2 | +import express, { Request, Response } from 'express'; |
| 3 | +import { Server } from 'http'; |
| 4 | + |
| 5 | +import { ClientPool } from '@launchdarkly/js-contract-test-utils'; |
| 6 | + |
| 7 | +import { Log } from './log.js'; |
| 8 | +import { badCommandError, newSdkClientEntity, SdkClientEntity } from './sdkClientEntity.js'; |
| 9 | + |
| 10 | +const app = express(); |
| 11 | +let server: Server | null = null; |
| 12 | + |
| 13 | +const port = 8000; |
| 14 | + |
| 15 | +const clients = new ClientPool<SdkClientEntity>(); |
| 16 | + |
| 17 | +const mainLog = Log('service'); |
| 18 | + |
| 19 | +app.use(bodyParser.json()); |
| 20 | + |
| 21 | +app.get('/', (req: Request, res: Response) => { |
| 22 | + res.header('Content-Type', 'application/json'); |
| 23 | + res.json({ |
| 24 | + capabilities: [ |
| 25 | + 'client-side', |
| 26 | + 'service-endpoints', |
| 27 | + 'tags', |
| 28 | + 'user-type', |
| 29 | + 'inline-context', |
| 30 | + 'inline-context-all', |
| 31 | + 'client-prereq-events', |
| 32 | + 'client-per-context-summaries', |
| 33 | + 'evaluation-hooks', |
| 34 | + 'track-hooks', |
| 35 | + 'anonymous-redaction', |
| 36 | + 'strongly-typed', |
| 37 | + 'event-gzip', |
| 38 | + 'flag-change-listeners', |
| 39 | + 'tls:skip-verify-peer', |
| 40 | + 'tls:custom-ca', |
| 41 | + 'wrapper', |
| 42 | + ], |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +app.delete('/', (req: Request, res: Response) => { |
| 47 | + mainLog.info('Test service has told us to exit'); |
| 48 | + res.status(204); |
| 49 | + res.send(); |
| 50 | + |
| 51 | + // Defer the following actions till after the response has been sent |
| 52 | + setTimeout(() => { |
| 53 | + if (server) { |
| 54 | + server.close(() => process.exit()); |
| 55 | + } |
| 56 | + // We force-quit with process.exit because, even after closing the server, there could be some |
| 57 | + // scheduled tasks lingering if an SDK instance didn't get cleaned up properly. |
| 58 | + }, 1); |
| 59 | +}); |
| 60 | + |
| 61 | +app.post('/', async (req: Request, res: Response) => { |
| 62 | + const options = req.body; |
| 63 | + |
| 64 | + try { |
| 65 | + const client = await newSdkClientEntity(options); |
| 66 | + const clientId = clients.add(client); |
| 67 | + |
| 68 | + res.status(201); |
| 69 | + res.set('Location', `/clients/${clientId}`); |
| 70 | + } catch (e) { |
| 71 | + res.status(500); |
| 72 | + const message = e instanceof Error ? e.message : JSON.stringify(e); |
| 73 | + mainLog.error(`Error creating client: ${message}`); |
| 74 | + res.write(message); |
| 75 | + } |
| 76 | + res.send(); |
| 77 | +}); |
| 78 | + |
| 79 | +app.post('/clients/:id', async (req: Request, res: Response) => { |
| 80 | + const client = clients.get(req.params.id); |
| 81 | + if (!client) { |
| 82 | + res.status(404); |
| 83 | + } else { |
| 84 | + try { |
| 85 | + const respValue = await client.doCommand(req.body); |
| 86 | + if (respValue) { |
| 87 | + res.status(200); |
| 88 | + res.write(JSON.stringify(respValue)); |
| 89 | + } else { |
| 90 | + res.status(204); |
| 91 | + } |
| 92 | + } catch (e) { |
| 93 | + const isBadRequest = e === badCommandError; |
| 94 | + res.status(isBadRequest ? 400 : 500); |
| 95 | + const message = e instanceof Error ? e.message : JSON.stringify(e); |
| 96 | + res.write(message); |
| 97 | + if (!isBadRequest && e instanceof Error && e.stack) { |
| 98 | + // eslint-disable-next-line no-console |
| 99 | + console.log(e.stack); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + res.send(); |
| 104 | +}); |
| 105 | + |
| 106 | +app.delete('/clients/:id', async (req: Request, res: Response) => { |
| 107 | + const client = clients.get(req.params.id); |
| 108 | + if (!client) { |
| 109 | + res.status(404); |
| 110 | + res.send(); |
| 111 | + } else { |
| 112 | + await client.close(); |
| 113 | + clients.remove(req.params.id); |
| 114 | + res.status(204); |
| 115 | + res.send(); |
| 116 | + } |
| 117 | +}); |
| 118 | + |
| 119 | +server = app.listen(port, () => { |
| 120 | + // eslint-disable-next-line no-console |
| 121 | + console.log('Listening on port %d', port); |
| 122 | +}); |
0 commit comments