feat(zip-it-and-ship-it): add support for event handlers#6976
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughChanges add detection and extraction of event subscription slugs from default exports into the Node in-source config parser, by returning and analyzing the default export expression (including CJS and re-exported defaults). Extracted eventSubscriptions are added to StaticAnalysisResult, propagated into FunctionResult/manifest composition, and written into the manifest when present. New tests and an ESM fixture validate extraction from object literals, bindings, CJS defaults, and re-exports. A dependency version for Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Comment |
|
This pull request adds or modifies JavaScript ( |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts (1)
125-126: Use an own-property check for handler matching.On Lines 125-126,
name in eventHandlerscan match prototype properties. Prefer an own-key check to avoid false positives in subscription extraction.Proposed change
- if (name && name in eventHandlers) { + if (name && Object.prototype.hasOwnProperty.call(eventHandlers, name)) { events.push(eventHandlers[name as keyof typeof eventHandlers]) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts` around lines 125 - 126, The current check uses "name in eventHandlers" which can match prototype properties; change it to an own-property check when deciding to push handlers: verify the key exists on eventHandlers itself (e.g., use Object.prototype.hasOwnProperty.call(eventHandlers, name) or equivalent) before calling events.push(eventHandlers[name as keyof typeof eventHandlers]) so only own keys are matched.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts`:
- Line 30: The import "eventHandlers" is invalid because
`@netlify/serverless-functions-api` exports getLambdaHandler, getNetlifyGlobal,
getPath and Context; replace the broken import of eventHandlers with the correct
exported symbol you actually use in this module (for example import
getLambdaHandler if you're wiring a lambda handler, getNetlifyGlobal if you need
Netlify globals, getPath for path resolution, or import type { Context } for
types), then update any usages of eventHandlers in this file to the chosen
exported symbol (or adjust the implementation to call
getLambdaHandler/getNetlifyGlobal/getPath as appropriate).
---
Nitpick comments:
In `@packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts`:
- Around line 125-126: The current check uses "name in eventHandlers" which can
match prototype properties; change it to an own-property check when deciding to
push handlers: verify the key exists on eventHandlers itself (e.g., use
Object.prototype.hasOwnProperty.call(eventHandlers, name) or equivalent) before
calling events.push(eventHandlers[name as keyof typeof eventHandlers]) so only
own keys are matched.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4842c411-fd59-43c3-8f96-0b73750fee40
📒 Files selected for processing (10)
packages/zip-it-and-ship-it/src/feature_flags.tspackages/zip-it-and-ship-it/src/manifest.tspackages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.tspackages/zip-it-and-ship-it/src/runtimes/node/parser/exports.tspackages/zip-it-and-ship-it/src/utils/format_result.tspackages/zip-it-and-ship-it/src/zip.tspackages/zip-it-and-ship-it/tests/fixtures-esm/v2-api-event-handlers/function.jspackages/zip-it-and-ship-it/tests/fixtures-esm/v2-api-event-handlers/package.jsonpackages/zip-it-and-ship-it/tests/main.test.tspackages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts (1)
827-892: LGTM! Comprehensive test coverage for event subscriptions extraction.The tests cover the key scenarios well:
- Object literal and binding-resolved default exports
- CJS
exports.defaultpatternexport { x as default }re-export syntax- Negative case for function exports
- Filtering of unknown property names
The
user*→identity_*slug mapping (e.g.,userLogin→identity_login) aligns with Netlify Identity events.Consider adding a test for when all properties are unrecognized to verify
eventSubscriptionsremainsundefined:💡 Optional: Add edge case test
test('Does not set eventSubscriptions when all properties are unknown', () => { const source = `export default { helperOne() {}, helperTwo() {} }` const isc = parseSource(source, options) expect(isc.eventSubscriptions).toBeUndefined() expect(isc.runtimeAPIVersion).toBe(2) }),
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts` around lines 827 - 892, Add an edge-case unit test in the same "Event subscriptions" describe block that verifies parseSource (used as parseSource(source, options)) does not set eventSubscriptions when the default-exported object only contains unknown helper methods; create a test named like "Does not set eventSubscriptions when all properties are unknown", use a source such as `export default { helperOne() {}, helperTwo() {} }`, call const isc = parseSource(source, options) and assert isc.eventSubscriptions is undefined and isc.runtimeAPIVersion is 2.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts`:
- Around line 827-892: Add an edge-case unit test in the same "Event
subscriptions" describe block that verifies parseSource (used as
parseSource(source, options)) does not set eventSubscriptions when the
default-exported object only contains unknown helper methods; create a test
named like "Does not set eventSubscriptions when all properties are unknown",
use a source such as `export default { helperOne() {}, helperTwo() {} }`, call
const isc = parseSource(source, options) and assert isc.eventSubscriptions is
undefined and isc.runtimeAPIVersion is 2.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 866d29cd-74a1-4798-bb27-6bbc8dbc8b73
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (3)
packages/zip-it-and-ship-it/package.jsonpackages/zip-it-and-ship-it/tests/main.test.tspackages/zip-it-and-ship-it/tests/unit/runtimes/node/in_source_config.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/zip-it-and-ship-it/package.json
- packages/zip-it-and-ship-it/tests/main.test.ts
Part of https://linear.app/netlify/issue/RUN-2652/improve-syntax-of-event-triggered-functions.