# node:buffer

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

`Buffer` is a global and the same object as the `Buffer` export of `node:buffer`, which follows
[Node's `buffer`](https://nodejs.org/api/buffer.html).

## 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());
```

`elide run buffer.mjs` prints:

```text
6 68c3a96c6c6f
aMOpbGxv héllo
cafebabe 48826
héllo!
```

## Differences from Node

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

<!-- api-reference:start 929299403fab -->
## Reference

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

### Exports

| Export | Kind | Status | Notes |
| --- | --- | --- | --- |
| `Buffer` | class | Full | `Buffer.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()` | function | Full | Decodes a base64-encoded ASCII string into a Latin-1 string |
| `btoa()` | function | Full | Encodes a Latin-1 string as base64 ASCII |
| `isAscii()` | function | Full | Returns true if every byte's high bit is clear (the input contains only 7-bit ASCII) |
| `isUtf8()` | function | Full | Returns true if the bytes form a structurally valid UTF-8 sequence (no overlong forms, surrogates, or… |
| `kMaxLength` | property | Full | Largest buffer size representable as a 32-bit JS integer (2^31 - 1) |
| `kStringMaxLength` | property | Full | Largest string length the runtime will allocate (2^31 - 1) |

**Tests:** [node-api `node:buffer`](https://github.com/elide-dev/testsuite/blob/main/reports/1.6.0%2B6e718933f/4cbcae6c33a8/node-api/node-api.md): 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`.
<!-- api-reference:end -->
