From c00f678cc4567bcf096808df586a2b70532437c4 Mon Sep 17 00:00:00 2001 From: maslow Date: Wed, 22 Nov 2023 20:25:13 +0800 Subject: [PATCH] fix(runtime): fix module.exports error; fix logs error while response.chunkedEncoding is true --- runtimes/nodejs/README.md | 10 ++++--- runtimes/nodejs/src/handler/invoke.ts | 30 ++++++++++++-------- runtimes/nodejs/src/support/engine/module.ts | 2 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/runtimes/nodejs/README.md b/runtimes/nodejs/README.md index ece9911cba..8a0955a4e6 100644 --- a/runtimes/nodejs/README.md +++ b/runtimes/nodejs/README.md @@ -28,15 +28,15 @@ cd runtimes/nodejs # connect the cluster if not connected -telepresence connect +telepresence connect -n laf-system export appid=your-app-id # proxy app cluster traffic to local, replace `APPID` with your prepared appid -telepresence intercept $appid -n laf-runtime -p 8000:8000 -e $(pwd)/.env +telepresence intercept $appid -p 8000:8000 -e $(pwd)/.env # after intercept command, you can use following command to check if intercept active -telepresence list -n laf-runtime +telepresence list # Start local service first, required nodejs version >= 18.0.0 npm install @@ -50,5 +50,7 @@ npm start > Clean up ```bash -telepresence leave $appid-laf-runtime +telepresence leave $appid +telepresence uninstall -a +telepresence quit ``` diff --git a/runtimes/nodejs/src/handler/invoke.ts b/runtimes/nodejs/src/handler/invoke.ts index ffe0d2dc96..d1cfcecf01 100644 --- a/runtimes/nodejs/src/handler/invoke.ts +++ b/runtimes/nodejs/src/handler/invoke.ts @@ -174,18 +174,24 @@ async function invokeDebug( debugConsole.error(result.error) } - const logs = debugConsole.getLogs() - if (ctx.request.get('x-laf-debug-data')) { - const compressed = pako.gzip(logs) - const base64Encoded = uint8ArrayToBase64(compressed) - ctx.response.set('x-laf-debug-logs', base64Encoded) - } else if (ctx.request.get('x-laf-func-data')) { - // keep compatible for old version clients(laf web & laf cli) - const encoded = encodeURIComponent(logs) - ctx.response.set('x-laf-func-logs', encoded) - } + // In the http module of Node.js, the chunkedEncoding property is used to + // indicate whether to use chunked transfer encoding. + // If set to true, Node.js automatically handles the splitting and sending of data chunks. + // If set to false, the headers have been sent, so do not send logs headers after that, otherwise an error will be reported. + if (ctx.response.chunkedEncoding === false) { + const logs = debugConsole.getLogs() + if (ctx.request.get('x-laf-debug-data')) { + const compressed = pako.gzip(logs) + const base64Encoded = uint8ArrayToBase64(compressed) + ctx.response.set('x-laf-debug-logs', base64Encoded) + } else if (ctx.request.get('x-laf-func-data')) { + // keep compatible for old version clients(laf web & laf cli) + const encoded = encodeURIComponent(logs) + ctx.response.set('x-laf-func-logs', encoded) + } - ctx.response.set('x-laf-debug-time-usage', result.time_usage.toString()) + ctx.response.set('x-laf-debug-time-usage', result.time_usage.toString()) + } if (result.error) { return ctx.response.status(500).send({ @@ -202,7 +208,7 @@ async function invokeDebug( return ctx.response.status(403).send({ error: 'Forbidden', requestId }) } - if (ctx.response.writableEnded === false) { + if (ctx.response.chunkedEncoding === false) { let data = result.data if (typeof result.data === 'number') { data = Number(result.data).toString() diff --git a/runtimes/nodejs/src/support/engine/module.ts b/runtimes/nodejs/src/support/engine/module.ts index c5278258e2..f4896ce7e0 100644 --- a/runtimes/nodejs/src/support/engine/module.ts +++ b/runtimes/nodejs/src/support/engine/module.ts @@ -81,7 +81,7 @@ export class FunctionModule { } ${code} - exports; + module.exports; ` }