awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
7.76k stars 342 forks source link

JSON.stringify turns objects with functions into invalid JSON #295

Closed yspreen closed 3 months ago

yspreen commented 3 months ago

I'm getting 502 errors with Runtime exited without providing a reason Runtime.ExitError and no other info.

Is decodeURIComponent the culprit?

I'm also trying to run process.hrtime()[1] in order to get a sub-ms timer but that one surprisingly doesn't cause a crash.

yspreen commented 3 months ago

adding these lines causes a crash:

  const awsRequestId = JSON.parse(
    decodeURIComponent(req.headers["x-apigateway-context"] as string)
  ).awsRequestId.replace(/(...).*(...)/, "$1…$2");
yspreen commented 3 months ago

maybe JSON encoding is to blame. not sure. this definitely crashes a handler:

JSON.parse(decodeURIComponent(encodeURIComponent(JSON.stringify(context))))

works fine in node18

yspreen commented 3 months ago

it appears the bug is in the JSON encoder.

this line

getRemainingTimeInMillis: [function: getRemainingTimeInMillis],

turns into

"getRemainingTimeInMillis":,

which is not valid JSON.

richarddavison commented 3 months ago

Its most likely related to this issue: https://github.com/awslabs/llrt/pull/263 I'm creating a new release today

richarddavison commented 3 months ago

@yspreen released now: https://github.com/awslabs/llrt/releases/tag/v0.1.12-beta