انتقل إلى المحتوى

node:buffer

⁦Buffer⁩ المتوافق مع ⁦Node⁩ ومساعدات البيانات الثنائية.

في هذه الصفحة
Open in ChatGPTOpen in Claude

لـ ⁦globalThis.Buffer⁩ وللصادر ⁦Buffer⁩ من ⁦node:buffer⁩ الهوية نفسها. والمخازن المؤقتة نسخ من ⁦Uint8Array⁩ مع وصول مفهرس و⁦ArrayBuffer⁩ أساسي ثابت.

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); // true

استخدم ⁦Buffer.alloc⁩ و⁦Buffer.allocUnsafe⁩ و⁦Buffer.allocUnsafeSlow⁩ و ⁦Buffer.from⁩ لإنشاء المخازن المؤقتة. ولا يزال استدعاء ⁦Buffer()⁩ أو ⁦new Buffer()⁩ يعمل للتوافق لكنه يُصدر تحذير إهمال.

عمليات ⁦Node⁩ الشائعة متاحة، بما في ذلك ⁦compare⁩ و⁦copy⁩ و⁦equals⁩ و⁦fill⁩ و⁦includes⁩ و⁦indexOf⁩ و⁦slice⁩ و⁦subarray⁩ و⁦write⁩، وقراءة الأعداد وكتابتها بعرض ثابت ومتغير، وقراءة ⁦BigInt⁩ وكتابته، وتبديل ترتيب البايتات.

تشمل الترميزات المدعومة ⁦utf8⁩ و⁦utf16le⁩ و⁦latin1⁩ و⁦ascii⁩ و⁦base64⁩ و⁦base64url⁩ و⁦hex⁩. ولا يُقبل ⁦utf16be⁩.

تصدّر ⁦node:buffer⁩ أيضًا ⁦Blob⁩ و⁦File⁩ و⁦SlowBuffer⁩ و⁦atob⁩ و⁦btoa⁩ و⁦isAscii⁩ و⁦isUtf8⁩ و⁦transcode⁩ وثوابت الأحجام والمساعدات ذات الصلة.

بخلاف ⁦Node⁩، تعيد تنفيذات ⁦allocUnsafe⁩ في ⁦Elide⁩ ذاكرة مملوءة بالأصفار بدلًا من كشف بايتات مستخدمة سابقًا.

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.

هل كانت هذه الصفحة مفيدة؟