Skip to content

Introduce buffer relative methods #58348

Open
@araujogui

Description

@araujogui

What is the problem this feature will solve?

Sequential buffer operations require manual offset tracking. This is error-prone and verbose. Many developers already create abstractions around Buffer to avoid it completely.

@nodejs/buffer

What is the feature you are proposing to solve the problem?

Introduce a new class, RelativeDataView, with relative read and write methods that automatically track a internal position. This simplifies sequential data access and reduces boilerplate.

Inspired by abstractions like Java’s ByteBuffer and Rust’s ByteBuffer.

The class should:

  • Can be constructed from an TypedArray or Buffer with an optional default byte order (be or le).
  • Expose relative read methods (e.g. readInt16 and readInt32).
  • Expose relative write methods (e.g. writeInt16 and writeInt32), supporting method chaining.
  • Expose position control methods:
position() – get/set current position.
seek(offset) – move position by offset.
mark() / reset() / rewind() – mark and restore position.
clear() – reset position to zero.
remaining() - Remaining bytes between current position and limit.

Example usage:

const buf = Buffer.allocUnsafe(16); // new ArrayBuffer(16);
const view = new RelativeDataView(buf, ByteOrder.BE);

view
  .writeInt16(42)
  .writeInt32(9000)
  .writeFloat(3.14);

view.clear(); // reset position to 0

const a = view.readInt16();    // 42
const b = view.readInt32();   // 9000
const c = view.readFloat();    // 3.14

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions