Commit db838ab
committed
fix: route ESM Lambdas through handler.mjs via CJS shim
AWS Lambda's CJS resolver picks `dist/handler.js` before `dist/handler.mjs`
when the handler string is `node_modules/datadog-lambda-js/dist/handler.handler`,
causing ESM user functions to fail with ERR_REQUIRE_ESM (or the surface
"Cannot find module" variant when _tryRequireSync swallows it).
Today `dist/handler.js` ships as a verbatim copy of `dist/handler.cjs`
(via `cp ./dist/handler.cjs ./dist/handler.js` in the publish scripts),
so it can never load ESM user code.
This replaces the copy with a real `src/handler.js` shim that:
- Detects ESM user code via the task root `package.json` `type` field or
a `.mjs` `DD_LAMBDA_HANDLER` / `_HANDLER`.
- For CJS user code: synchronously delegates to `handler.cjs`, preserving
the prior fast path (no cold-start regression).
- For ESM user code: exposes an async handler that lazily `import()`s
`handler.mjs` on the first invocation. `handler.mjs`'s async `load()`
transparently handles both CJS and ESM user modules.
`scripts/update_dist_version.sh` already copies `src/handler.*` into
`dist/`, so the shim ships automatically; the explicit
`cp ./dist/handler.cjs ./dist/handler.js` lines in `publish_npm.sh` and
`publish_prod.sh` are no longer needed and are removed.
Refs: #782
Refs: SLES-2888, SLES-2895, SVLS-92381 parent 99a1f4f commit db838ab
3 files changed
Lines changed: 63 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | 32 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
99 | 98 | | |
100 | 99 | | |
101 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
0 commit comments