All files / blong-int-adapter/http/sim echo.ts

95.91% Statements 47/49
88.88% Branches 8/9
100% Functions 3/3
95.91% Lines 47/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {adapter} from '@feasibleone/blong';
import type {IncomingMessage, Server, ServerResponse} from 'node:http';
import {createServer} from 'node:http';
 
let _server: Server | null = null;
 
export default adapter(() => ({
    activation: {
        default: {},
        'adapter.http': {
            namespace: 'sim',
            imports: [],
        },
    },
    async start() {
        _server = createServer((req: IncomingMessage, res: ServerResponse) => {
            let body = '';
            req.on('data', (chunk: Buffer) => {
                body += chunk.toString();
            });
            req.on('end', () => {
                res.writeHead(200, {'Content-Type': 'application/json'});
                res.end(
                    JSON.stringify({
                        echo: true,
                        url: req.url,
                        method: req.method,
                        body: body || undefined,
                    }),
                );
            });
        });
        await new Promise<void>(resolve =>
            _server!.listen(30088, '127.0.0.1', resolve as () => void),
        );
        super.connect();
        return super.start();
    },
    async stop(...params: unknown[]) {
        let result;
        try {
            await new Promise<void>(resolve => _server?.close(() => resolve()) ?? resolve());
        } finally {
            _server = null;
            result = await super.stop(...params);
        }
        return result;
    },
}));