Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish native Buffer implementation #155

Closed
84 of 85 tasks
Jarred-Sumner opened this issue May 4, 2022 · 3 comments
Closed
84 of 85 tasks

Finish native Buffer implementation #155

Jarred-Sumner opened this issue May 4, 2022 · 3 comments
Labels
tracking An umbrella issue for tracking big features

Comments

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented May 4, 2022

Bun's Buffer implementation is incomplete and I think it'd be good to have an issue where this is tracked.

Buffer static functions:

  • Buffer.alloc
  • Buffer.allocUnsafe
  • Buffer.allocUnsafeSlow
  • Buffer.byteLength
  • Buffer.compare
  • Buffer.concat
  • Buffer.from
  • Buffer.isBuffer
  • Buffer.isEncoding

Buffer.prototype functions:

  • fill
  • includes
  • indexOf
  • lastIndexOf
  • swap16
  • swap32
  • swap64
  • asciiSlice
  • asciiWrite
  • base64Slice
  • base64urlSlice
  • base64urlWrite
  • base64Write
  • compare
  • copy
  • copy
  • equals
  • hexSlice
  • hexWrite
  • latin1Slice
  • latin1Write
  • readBigInt64BE
  • readBigInt64LE
  • readBigUInt64BE
  • readBigUInt64LE
  • readDoubleBE
  • readDoubleLE
  • readFloatBE
  • readFloatLE
  • readInt16BE
  • readInt16LE
  • readInt32BE
  • readInt32LE
  • readInt8
  • readUInt16BE
  • readUInt16LE
  • readUInt32BE
  • readUInt32LE
  • readUInt8
  • setBigUint64
  • slice
  • subarray
  • toJSON
  • toString
  • ucs2Slice
  • ucs2Write
  • utf16leSlice
  • utf16leWrite
  • utf8Slice
  • utf8Write
  • write
  • writeBigInt64BE
  • writeBigInt64LE
  • writeBigUInt64BE
  • writeBigUInt64LE
  • writeDoubleBE
  • writeDoubleLE
  • writeFloatBE
  • writeFloatLE
  • writeInt16BE
  • writeInt16LE
  • writeInt32BE
  • writeInt32LE
  • writeInt8
  • writeUInt16BE
  • writeUInt16LE
  • writeUInt32BE
  • writeUInt32LE
  • writeUInt8

Bugs

  • buffer.write(foo, "base64") loses the last letter sometimes
  • buffer.write(foo, "base64url") is too slow
  • Buffer.concat in a tight loop is not yet faster than Node. I expect it to improve once pooling is added, but I don't think that's the entire story here.

Once finished:

  • Port Node's tests for Buffer
  • "buffer" package should point to the native instance
  • buffer.Blob in "buffer" module should point to globalThis.Blob
  • "crypto" polyfill should use the native Buffer implementation
@myndzi
Copy link

myndzi commented Sep 12, 2022

Just an extra comment related to some of the other issues:

console.log(Buffer.from('').constructor === Buffer);
console.log(Object.getPrototypeOf(Buffer.from('')).constructor === Buffer);

On Node.js, this will log "true" both times. In Bun it logs "false" both times. Obviously this is because Buffer in Bun is basically an API layered on top of Typed Arrays, but if you want full transparent parity, the constructors will need to be arranged correctly too.

(I just happened to stumble into this while doing something admitteddly a little weird)

... in Node, it appears to be the case that Buffer is simply a child of Uint8Array:

> Object.getPrototypeOf(Buffer.prototype) === Uint8Array.prototype
true
> Object.getPrototypeOf(Buffer) === Uint8Array
true

(edit) TIL how prototypes get set up when using "extends".

While Node seems to be manually setting things up rather than using "class" syntax, the net effect is equivalent to what you'd get by writing class Buffer extends Uint8Array { ... }.

Bun's Buffer, instead, appears to behave like class Buffer { (no extends), except its prototype has been pointed at Uint8Array's prototype

@asilvas
Copy link
Contributor

asilvas commented Feb 17, 2023

List is incomplete.

readIntBE
readIntLE
writeIntBE
writeIntLE

@Jarred-Sumner
Copy link
Collaborator Author

@asilvas those functions will land in the next canary build (later tonight), thanks to @alexlamsl in #2341 (along with previously failing tests imported from Node.js that have been fixed)

The remaining todo here is to speed up base64 and base64url, which is being tracked in #2330

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracking An umbrella issue for tracking big features
Projects
None yet
Development

No branches or pull requests

3 participants