Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): fix module.exports error; fix logs error while response.chunkedEncoding is true #1707

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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