Skip to content

feat: set json buffer directly#30

Merged
fengmk2 merged 1 commit intomasterfrom
set-in-support-buffer
Dec 3, 2025
Merged

feat: set json buffer directly#30
fengmk2 merged 1 commit intomasterfrom
set-in-support-buffer

Conversation

@fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 3, 2025

Summary by CodeRabbit

  • New Features
    • JSONBuilder now supports Buffer values, enabling binary data to be stored alongside strings, numbers, booleans, dates, and objects.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings December 3, 2025 11:59
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 3, 2025

Walkthrough

Updates CI workflow to test with Node 22 and 24 only, extends JSONBuilder.setIn to accept Buffer values with corresponding test coverage, and adds a combined "ready" script for building, linting, and testing.

Changes

Cohort / File(s) Summary
CI Configuration
.github/workflows/CI.yml
Removes Node 20 from test matrices; updates to test only with Node 22 and 24 across macOS/Windows and Linux binding jobs.
Builder Type & Implementation
js/builder.ts
Extends SetValue type to include Buffer. Updates setIn method to compute valueBuffer once and reuse for both Update and Add code paths, enabling binary values to be stored.
Builder Tests
__test__/builder.spec.ts
Adds new test case verifying that JSONBuilder.setIn correctly handles Buffer values, serializing them into strings or nested objects in the built JSON.
Package Configuration
package.json
Adds "ready" script that chains yarn build && yarn lint && yarn test.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • js/builder.ts: Verify Buffer handling logic in setIn method; ensure valueBuffer computation and reuse in both Update and Add paths is correct and doesn't introduce encoding issues.
  • test/builder.spec.ts: Confirm test coverage adequately validates Buffer serialization for both simple and nested object scenarios.

Poem

A rabbit hops through Node versions spry,
From twenty's dusk to twenty-four's bright sky,
With Buffers now embraced in builder's keep,
A "ready" script to build, lint, and sweep! 🐰✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: set json buffer directly' directly corresponds to the main change: extending SetValue to accept Buffer type and enabling binary values to be stored in JSON through the setIn method.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch set-in-support-buffer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @fengmk2, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the JSONBuilder by enabling it to directly accept Buffer objects for setting JSON values. This change streamlines the process of manipulating JSON data when values are already in a Buffer format, which can lead to improved efficiency by reducing redundant serialization operations. The update includes modifications to type definitions, refined logic within the setIn method to intelligently handle Buffer inputs, and a new comprehensive test case to ensure the feature functions as expected.

Highlights

  • Direct Buffer Support: The JSONBuilder now directly accepts Buffer objects as values when using the setIn method, avoiding unnecessary serialization.
  • Performance Improvement: By allowing Buffer inputs, the builder can bypass redundant JSON.stringify calls, potentially improving performance when working with pre-serialized JSON data.
  • Type Definition Update: The SetValue type in js/builder.ts has been updated to include Buffer as a valid input type.
  • New Test Case: A new test has been added to __test__/builder.spec.ts to validate the correct handling of Buffer inputs for setting JSON values.
  • New ready Script: A convenience script ready has been added to package.json to run build, lint, and test sequentially.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/CI.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a useful feature to set a JSON value directly from a Buffer in JSONBuilder, which can improve performance by avoiding unnecessary JSON.stringify calls. The implementation is correct and includes a new test case. My main feedback is to add documentation for the setIn method to clarify that when a Buffer is passed as a value, it must contain a valid JSON-encoded value. This will prevent potential misuse and improve the API's clarity.

return this
}

const valueBuffer = Buffer.isBuffer(value) ? value : Buffer.from(JSON.stringify(value))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a great optimization to allow passing pre-serialized JSON values as buffers. However, it introduces an implicit contract for the setIn method: when a Buffer is passed, it's assumed to contain a valid JSON value representation. An invalid representation (e.g., Buffer.from('some string') instead of Buffer.from('"some string"')) will lead to invalid JSON output.

This could be a source of bugs for users of this API. To improve maintainability and prevent misuse, please add JSDoc to the setIn method to make this contract explicit.

Here's a suggestion for the documentation:

/**
 * Sets a value at a specified path in the JSON object.
 * ...
 * @param {SetValue} value The value to set. If a Buffer is provided, it is assumed to be a valid JSON-encoded value and will be inserted directly. Other types will be converted to a JSON string.
 */
setIn(paths: string[], value: SetValue): this {
  // ...
}

@github-actions
Copy link

github-actions bot commented Dec 3, 2025

📦 napi-rs Build Size Comparison

File Base Size PR Size Diff %
packument.linux-x64-gnu.node 849.82 KB 849.82 KB 0 KB 0%

Total: 849.82 KB → 849.82 KB (0 KB, 0%)

Compared using x86_64-unknown-linux-gnu target

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds the ability to pass Buffer values directly to JSONBuilder.setIn(), avoiding the need for double JSON serialization. This optimization is particularly useful when working with pre-serialized JSON data, as it allows the Buffer to be inserted directly without the overhead of parsing and re-stringifying.

Key changes:

  • Extended SetValue type to include Buffer type
  • Modified implementation to detect and use Buffer values directly, avoiding JSON.stringify() when a Buffer is provided
  • Added test coverage for the new Buffer functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
package.json Added "ready" convenience script that runs build, lint, and test
js/builder.ts Extended SetValue type to include Buffer and updated implementation to handle Buffer values directly
test/builder.spec.ts Added new test case for setting values using Buffer objects
.github/workflows/CI.yml Updated Node.js version from 22 to 24 and removed Node 20 from test matrix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +194 to +215
test('should set value with buffer', () => {
const data = Buffer.from('{}')
const builder = new JSONBuilder(data)
builder.setIn(['info', 'name'], Buffer.from('"John"'))
expect(builder.build().toString()).toBe('{"info":{"name":"John"}}')
expect(JSON.parse(builder.build().toString())).toEqual({
info: { name: 'John' },
})
builder.setIn(
['a', 'b'],
Buffer.from(
JSON.stringify({
foo: 'bar',
}),
),
)
expect(builder.build().toString()).toBe('{"info":{"name":"John"},"a":{"b":{"foo":"bar"}}}')
expect(JSON.parse(builder.build().toString())).toEqual({
info: { name: 'John' },
a: { b: { foo: 'bar' } },
})
})
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only covers the "Add" case where new properties are created with Buffer values. Consider adding a test case for the "Update" case where an existing property is updated with a Buffer value. For example:

const data = Buffer.from('{"name":"John"}')
const builder = new JSONBuilder(data)
builder.setIn(['name'], Buffer.from('"Jane"'))
expect(builder.build().toString()).toBe('{"name":"Jane"}')

This will ensure that the Update path (line 45-51 in builder.ts) works correctly with Buffer values.

Copilot uses AI. Check for mistakes.
Comment on lines +194 to +215
test('should set value with buffer', () => {
const data = Buffer.from('{}')
const builder = new JSONBuilder(data)
builder.setIn(['info', 'name'], Buffer.from('"John"'))
expect(builder.build().toString()).toBe('{"info":{"name":"John"}}')
expect(JSON.parse(builder.build().toString())).toEqual({
info: { name: 'John' },
})
builder.setIn(
['a', 'b'],
Buffer.from(
JSON.stringify({
foo: 'bar',
}),
),
)
expect(builder.build().toString()).toBe('{"info":{"name":"John"},"a":{"b":{"foo":"bar"}}}')
expect(JSON.parse(builder.build().toString())).toEqual({
info: { name: 'John' },
a: { b: { foo: 'bar' } },
})
})
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test case for the scenario where a Buffer value is set for a nested property that doesn't exist yet (ParentNotFound case). For example:

const data = Buffer.from('{}')
const builder = new JSONBuilder(data)
builder.setIn(['user', 'profile', 'name'], Buffer.from('"Alice"'))
expect(builder.build().toString()).toBe('{"user":{"profile":{"name":"Alice"}}}')

This will ensure that the recursive parent creation logic (lines 36-42 in builder.ts) works correctly when the final value is a Buffer.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
js/builder.ts (1)

9-9: Document the expected Buffer format.

The SetValue type now includes Buffer, which is a useful addition. However, consider adding JSDoc comments to clarify that the Buffer should contain pre-serialized JSON data (e.g., Buffer.from('"value"') for strings, Buffer.from(JSON.stringify(obj)) for objects).

Example documentation:

/**
 * Value types that can be set in the JSON structure.
 * - Primitive types (string, number, boolean, Date) will be JSON.stringify'd
 * - Objects will be JSON.stringify'd
 * - Buffer should contain pre-serialized JSON (e.g., Buffer.from(JSON.stringify(value)))
 */
export type SetValue = string | number | boolean | Date | object | Buffer
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c235f73 and 085dcf7.

📒 Files selected for processing (4)
  • .github/workflows/CI.yml (1 hunks)
  • __test__/builder.spec.ts (1 hunks)
  • js/builder.ts (3 hunks)
  • package.json (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
__test__/builder.spec.ts (1)
js/builder.ts (1)
  • JSONBuilder (19-113)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Agent
  • GitHub Check: stable - aarch64-pc-windows-msvc - node@24
  • GitHub Check: stable - x86_64-pc-windows-msvc - node@24
  • GitHub Check: Analyze (rust)
🔇 Additional comments (5)
package.json (1)

55-55: LGTM! Useful convenience script.

The "ready" script provides a convenient way to run the full build, lint, and test pipeline in sequence, which is helpful for pre-commit/pre-push workflows.

.github/workflows/CI.yml (1)

87-87: LGTM! CI changes align with Node version requirements.

The workflow now tests on Node 22 and 24 only, which is consistent with the package.json engines requirement of >= 22.0.0. The job name update to node@24 appropriately reflects the primary build version.

Also applies to: 163-164, 201-202

js/builder.ts (2)

44-44: Consider validating Buffer contents or documenting the requirement.

The code assumes that Buffer values contain valid JSON data. If an invalid Buffer is passed (e.g., Buffer.from('not valid json')), it will corrupt the JSON structure.

Since validation might impact performance, at minimum, document this requirement clearly in the JSDoc comments for the setIn method or the SetValue type. Alternatively, consider adding optional validation in development mode.

Example of documentation:

/**
 * Sets a value at the specified path in the JSON structure.
 * @param paths - Array of property names representing the path
 * @param value - The value to set. If a Buffer is provided, it must contain valid pre-serialized JSON.
 * @returns this for method chaining
 */
setIn(paths: string[], value: SetValue): this {

44-60: Implementation looks good for the happy path.

The shared valueBuffer approach is efficient - it computes the buffer once and reuses it for both Update and Add paths. The Buffer concatenation logic correctly constructs the JSON key-value pairs.

__test__/builder.spec.ts (1)

194-215: LGTM! Good test coverage for Buffer support.

The test case validates Buffer handling for both string and object values, and verifies the final JSON structure is correct. The test covers:

  • Setting a Buffer containing a JSON string ("John")
  • Setting a Buffer containing a serialized object
  • Nested path operations with Buffer values
  • Both string representation and parsed JSON validation

@fengmk2 fengmk2 merged commit 880887d into master Dec 3, 2025
43 checks passed
@fengmk2 fengmk2 deleted the set-in-support-buffer branch December 3, 2025 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants