globalThis.Buffer et l’export Buffer de node:buffer ont la même identité.
Les buffers sont des instances de Uint8Array avec un accès indexé et un
ArrayBuffer sous-jacent stable.
import { Buffer } from "node:buffer";
const bytes = Buffer.from("hello", "utf8");
console.log(bytes[0]); // 104
console.log(bytes.toString("hex")); // 68656c6c6f
console.log(bytes instanceof Uint8Array); // trueUtilisez Buffer.alloc, Buffer.allocUnsafe, Buffer.allocUnsafeSlow et
Buffer.from pour créer des buffers. Appeler Buffer() ou new Buffer()
fonctionne toujours par souci de compatibilité, mais émet un avertissement
d’obsolescence.
Les opérations Node courantes sont disponibles, notamment compare, copy,
equals, fill, includes, indexOf, slice, subarray, write, les
lectures/écritures numériques à largeur fixe et variable, les lectures/écritures
BigInt et les permutations d’octets.
Les encodages pris en charge incluent utf8, utf16le, latin1, ascii,
base64, base64url et hex. utf16be n’est pas accepté.
node:buffer exporte aussi Blob, File, SlowBuffer, atob, btoa,
isAscii, isUtf8, transcode, des constantes de taille et des utilitaires
associés.
Contrairement à Node, les implémentations de allocUnsafe d’Elide renvoient de
la mémoire remplie de zéros au lieu d’exposer des octets précédemment utilisés.
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., 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: 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.