| 15 | |
| 16 | const transport = { |
| 17 | request(options: Record<string, any>, onResponse: (res: PassThrough) => void) { |
| 18 | const req = new EventEmitter() as Record<string, any>; |
| 19 | const chunks: Buffer[] = []; |
| 20 | |
| 21 | req.destroyed = false; |
| 22 | req.setTimeout = () => {}; |
| 23 | req.write = (chunk?: unknown) => { |
| 24 | if (chunk !== undefined) { |
| 25 | chunks.push(Buffer.from(chunk as string)); |
| 26 | } |
| 27 | return true; |
| 28 | }; |
| 29 | req.destroy = () => { |
| 30 | req.destroyed = true; |
| 31 | }; |
| 32 | req.close = req.destroy; |
| 33 | req.end = (chunk?: unknown) => { |
| 34 | if (chunk !== undefined) { |
| 35 | chunks.push(Buffer.from(chunk as string)); |
| 36 | } |
| 37 | |
| 38 | const body = Buffer.concat(chunks); |
| 39 | calls.push({ options, body }); |
| 40 | |
| 41 | const response = responseFactory ? responseFactory(body, options) : {}; |
| 42 | const res = new PassThrough() as PassThrough & Record<string, any>; |
| 43 | res.statusCode = response.statusCode ?? 200; |
| 44 | res.statusMessage = response.statusMessage ?? class="st">'OK'; |
| 45 | res.headers = response.headers ?? { class="st">'content-type': class="st">'application/json' }; |
| 46 | res.req = req; |
| 47 | |
| 48 | onResponse(res); |
| 49 | res.end(response.body ?? JSON.stringify({ ok: true })); |
| 50 | }; |
| 51 | |
| 52 | return req; |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | return { |