-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Open
Labels
bufferIssues and PRs related to the buffer subsystem.Issues and PRs related to the buffer subsystem.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.
Description
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
orBuffer
with an optional default byte order (be
orle
). - Expose relative read methods (e.g.
readInt16
andreadInt32
). - Expose relative write methods (e.g.
writeInt16
andwriteInt32
), 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
geeksilva97 and miguelmarcondesfjimmywarting and sindresorhus
Metadata
Metadata
Assignees
Labels
bufferIssues and PRs related to the buffer subsystem.Issues and PRs related to the buffer subsystem.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.
Type
Projects
Status
Awaiting Triage