Skip to content

Commit

Permalink
fix(runtime): fix module.exports error; fix logs error while response…
Browse files Browse the repository at this point in the history
….chunkedEncoding is true (#1707)
  • Loading branch information
maslow committed Nov 23, 2023
1 parent 29cd145 commit 8ea5ad0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
10 changes: 6 additions & 4 deletions runtimes/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,5 +50,7 @@ npm start
> Clean up
```bash
telepresence leave $appid-laf-runtime
telepresence leave $appid
telepresence uninstall -a
telepresence quit
```
30 changes: 18 additions & 12 deletions runtimes/nodejs/src/handler/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion runtimes/nodejs/src/support/engine/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class FunctionModule {
}
${code}
exports;
module.exports;
`
}

Expand Down

0 comments on commit 8ea5ad0

Please sign in to comment.