Skip to content

node:buffer

How Elide supports Node's Buffer global and node:buffer module: one object, with allocUnsafe and allocUnsafeSlow returning zero-filled memory.

On this page

Buffer is a global and the same object as the Buffer export of node:buffer, which follows Node’s buffer.

Example

js
// buffer.mjs
const greeting = Buffer.from("héllo");
console.log(greeting.length, greeting.toString("hex"));
console.log(greeting.toString("base64"), Buffer.from("aMOpbGxv", "base64").toString());

const header = Buffer.alloc(4);
header.writeUInt32BE(0xcafebabe);
console.log(header.toString("hex"), header.readUInt16LE(2));
console.log(Buffer.concat([greeting, Buffer.from("!")]).toString());
utf-8 · js · Lines: 9

elide run buffer.mjs prints:

text
6 68c3a96c6c6f
aMOpbGxv héllo
cafebabe 48826
héllo!
utf-8 · text · Lines: 4

Differences from Node

  • Buffer.allocUnsafe and Buffer.allocUnsafeSlow return zero-filled memory.

Reference

Status: Full · 56 of 62 suite tests pass (90.3%) · since 1.1.0

Exports

ExportKindStatusNotes
BufferclassFullBuffer.alloc(), Buffer.allocUnsafe(), Buffer.allocUnsafeSlow(), Buffer.from(), Buffer.isBuffer(), Buffer.isEncoding(), Buffer.byteLength(), Buffer.concat(), Buffer.compare(), length, buffer, byteOffset, compare(), copy(), equals(), fill(), includes(), indexOf(), lastIndexOf(), slice(), subarray(), write(), toString(), toJSON(), readUInt8(), readUInt16BE(), readUInt16LE(), readUInt32BE(), readUInt32LE(), readInt8(), readInt16BE(), readInt16LE(), readInt32BE(), readInt32LE(), readUIntBE(), readUIntLE(), readIntBE(), readIntLE(), readFloatBE(), readFloatLE(), readDoubleBE(), readDoubleLE(), writeUInt8(), writeUInt16BE(), writeUInt16LE(), writeUInt32BE(), writeUInt32LE(), writeInt8(), writeInt16BE(), writeInt16LE(), writeInt32BE(), writeInt32LE(), writeUIntBE(), writeUIntLE(), writeIntBE(), writeIntLE(), writeFloatBE(), writeFloatLE(), writeDoubleBE(), writeDoubleLE(), swap16(), swap32(), swap64()
atob()functionFullDecodes a base64-encoded ASCII string into a Latin-1 string
btoa()functionFullEncodes a Latin-1 string as base64 ASCII
isAscii()functionFullReturns true if every byte’s high bit is clear (the input contains only 7-bit ASCII)
isUtf8()functionFullReturns true if the bytes form a structurally valid UTF-8 sequence (no overlong forms, surrogates, or…
kMaxLengthpropertyFullLargest buffer size representable as a 32-bit JS integer (2^31 - 1)
kStringMaxLengthpropertyFullLargest string length the runtime will allocate (2^31 - 1)

Tests: node-api node:buffer: 56 pass, 6 fail, 0 error, 7 skipped. Failing: test-buffer-alloc.js, test-buffer-constructor-node-modules-paths.js, test-buffer-constructor-node-modules.js, test-buffer-constructor-outside-node-modules.js, test-buffer-indexof.js, test-buffer-pool-untransferable.js.

Was this page helpful?