Skip to content

Bug: jmespath custom functions introspection breaks when minifying #2383

Closed
@dreamorosi

Description

@dreamorosi

Expected Behaviour

When using the JMESPath utility I should be able to minify my code and all the utility's features should work.

Current Behaviour

When the function including the utility is minified the utility is not able to introspect its own methods and correctly register the functions available, which causes a runtime error:

2024-04-17T08:03:11.119Z        a71a8744-df90-4187-bff6-d8f8e0d0478d    ERROR   Invoke Error    {"errorType":"TypeError","errorMessage":"t.introspectMethods is not a function or its return value is not iterable","stack":["TypeError: t.introspectMethods is not a function or its return value is not iterable","    at Object.introspectMethods (/var/task/index.js:2:12877)","    at l.introspectMethods (/var/task/index.js:2:12877)","    at U.introspectMethods (/var/task/index.js:2:12877)","    at new ue (/var/task/index.js:2:16337)","    at he.search (/var/task/index.js:2:20094)","    at Le (/var/task/index.js:2:30000)","    at Runtime.Sr [as handler] (/var/task/index.js:2:31173)","    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"]}

This affects both ESM and CJS builds and can be mitigated only by not aliasing functions/methods names in the minified bundle - in esbuild this corresponds to the keepNames field.

Code snippet

import { search } from "@aws-lambda-powertools/jmespath";
import { PowertoolsFunctions } from "@aws-lambda-powertools/jmespath/functions";

export const handler = async (event: any): Promise<any> => {
  return search("powertools_json(foo).bar", event, {
    customFunctions: new PowertoolsFunctions(),
  });
};

Steps to Reproduce

Bundle the function above and minify it:

npx esbuild --bundle index.ts --platform=node --outdir=out --format=esm --minify

Then deploy and observe the error.

On the other hand, if you also enable the --keepNames flag, the error won't occur.

Possible Solution

Refactor this method used to register the available methods to not rely on the prototype, or statically define the methods beforehand if that's not possible.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

Lambda Layers, npm

Execution logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedjmespathThis item relates to the JMESPath Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions