From bc17169ee35a72dab7580844bf3f0f4b33ac5b04 Mon Sep 17 00:00:00 2001 From: Snazzah Date: Mon, 16 Sep 2024 18:03:57 -0500 Subject: [PATCH] fix: update servers to parse responses better --- src/servers/bun.ts | 2 +- src/servers/cfworker.ts | 2 +- src/servers/express.ts | 43 ++++++++++++++++++++++------------------- src/servers/lambda.ts | 2 +- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/servers/bun.ts b/src/servers/bun.ts index bee249f7..8f910a28 100644 --- a/src/servers/bun.ts +++ b/src/servers/bun.ts @@ -50,7 +50,7 @@ export class BunServer extends Server { ); } else resolve( - new Response(JSON.stringify(response.body), { + new Response(response.body ? JSON.stringify(response.body) : null, { status: response.status || 200, headers: { ...((response.headers || {}) as Record), diff --git a/src/servers/cfworker.ts b/src/servers/cfworker.ts index dd3ff477..464e8f73 100644 --- a/src/servers/cfworker.ts +++ b/src/servers/cfworker.ts @@ -45,7 +45,7 @@ export class CloudflareWorkerServer extends Server { ); } else resolve( - new Response(JSON.stringify(response.body), { + new Response(response.body ? JSON.stringify(response.body) : null, { status: response.status || 200, headers: { ...((response.headers || {}) as Record), diff --git a/src/servers/express.ts b/src/servers/express.ts index 03898aab..7a3f1445 100644 --- a/src/servers/express.ts +++ b/src/servers/express.ts @@ -29,26 +29,29 @@ export class ExpressServer extends Server { /** @private */ createEndpoint(path: string, handler: ServerRequestHandler) { - this.app.post(path, (req: any, res: any) => - handler( - { - headers: req.headers, - body: req.body, - request: req, - response: res - }, - async (response) => { - res.status(response.status || 200); - if (response.headers) for (const key in response.headers) res.set(key, response.headers[key]); - if (response.files) { - const data = new MultipartData(); - res.set('Content-Type', 'multipart/form-data; boundary=' + data.boundary); - for (const i in response.files) data.attach(`files[${i}]`, response.files[i].file, response.files[i].name); - data.attach('payload_json', JSON.stringify(response.body)); - res.send(Buffer.concat(data.finish())); - } else res.send(response.body); - } - ) + this.app.post( + path, + (req: any, res: any) => + void handler( + { + headers: req.headers, + body: req.body, + request: req, + response: res + }, + async (response) => { + res.status(response.status || 200); + if (response.headers) for (const key in response.headers) res.set(key, response.headers[key]); + if (response.files) { + const data = new MultipartData(); + res.set('Content-Type', 'multipart/form-data; boundary=' + data.boundary); + for (const i in response.files) + data.attach(`files[${i}]`, response.files[i].file, response.files[i].name); + data.attach('payload_json', JSON.stringify(response.body)); + res.send(Buffer.concat(data.finish())); + } else res.send(response.body); + } + ) ); } diff --git a/src/servers/lambda.ts b/src/servers/lambda.ts index ac993560..dfea4898 100644 --- a/src/servers/lambda.ts +++ b/src/servers/lambda.ts @@ -57,7 +57,7 @@ export class AWSLambdaServer extends Server { callback(null, { statusCode: response.status || 200, headers: responseHeaders, - body: JSON.stringify(response.body) + body: response.body ? JSON.stringify(response.body) : undefined }); } }