refactor: replace lodash/cloneDeep with native structuredClone#218
refactor: replace lodash/cloneDeep with native structuredClone#218andrerfneves wants to merge 1 commit into
Conversation
- Replace lodash/cloneDeep usage in bolt11.js with native structuredClone() - Remove lodash from package.json dependencies - Bundle size reduced by ~36 KB (1,813.88 kB → 1,777.73 kB) - structuredClone() is supported in all modern browsers and Node.js 17+
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3eb29d58d5
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| function encode (inputData, addDefaults) { | ||
| // we don't want to affect the data being passed in, so we copy the object | ||
| let data = cloneDeep(inputData) | ||
| let data = structuredClone(inputData) |
There was a problem hiding this comment.
Preserve Buffer instances when cloning encode input
When callers pass Buffer-valued fields to encode(), structuredClone() converts them into plain Uint8Arrays instead of preserving Buffer instances. That regresses inputs the existing code accepts: for example routing_info.short_channel_id as a Buffer now reaches the later shortId instanceof Buffer validation and is rejected, and a Buffer payeeNodeKey is serialized with Uint8Array#toString('hex') as comma-separated bytes rather than hex. cloneDeep preserved Buffers, so these valid encode inputs now fail or produce malformed output.
Useful? React with 👍 / 👎.
Summary
Replaces the single usage of
lodash/cloneDeepinsrc/lib/bolt11.jswith the nativestructuredClone()API, and removeslodashfrompackage.jsondependencies.Why
lodashwas a 4.9 MB production dependency used only for twocloneDeepcalls inbolt11.js(thesign()andencode()functions). Both functions clone plain JS objects/arrays, whichstructuredClone()handles correctly:inputPayReqObj— a plain Bolt11 payment request object with properties likecomplete,paymentRequest,payeeNodeKey,tags, etc.inputData— a plain data object for encoding with properties likecoinType,timestamp,signature, etc.Native
structuredClone()has been available in all major browsers since 2022 and Node.js since v17, well within the project's browserslist targets.Changes
src/lib/bolt11.js: Removedimport cloneDeep from 'lodash/cloneDeep'and replaced bothcloneDeep()calls withstructuredClone()package.json: Removed thelodashdependency entrypackage-lock.json: Updated automatically bynpm installTest Plan