node:buffer
The node:buffer module in Azion Runtime: the Buffer class for binary data, examples of encodings, concatenation, and request bodies, and the supported APIs.
The node:buffer module provides Buffer, the Node.js class that holds raw binary data as a sequence of bytes. Use it to convert text between encodings such as hex and base64, to combine and slice binary data, and to read a request body or a stream as bytes. In Azion Runtime, Buffer is also a global: the global and the class you import from node:buffer are the same object, and every buffer is a Uint8Array.
Examples
Each example is a complete function that imports Buffer from node:buffer. The response below each example is the one a deployed function returns.
Basic buffer operations
This function creates a buffer from a string, prints it in two encodings, and overwrites part of it:
The function responds with the overwritten text:
Buffer encoding conversions
This function converts one string to three encodings for data transmission, then decodes the base64 form back to text:
The function responds with each encoding of the string:
In Azion Runtime, base64url output keeps the = padding, as the base64url value shows, and Buffer.isEncoding("base64url") returns false.
Buffer concatenation and slicing
This function combines three buffers into one, then extracts and copies a portion of the result:
The function responds with the combined buffer and the two extracted portions:
Binary data processing
This function reads the request body as a buffer, searches it for a byte sequence, and compares and allocates buffers:
For a POST request whose body is Hello Azion Runtime, the function responds with the body length, its first byte, and the position of Azion:
JSON serialization with buffers
This function converts an object to a JSON buffer, uses the buffer size in the response, and sets Content-Length from it:
The function responds with the object and its buffer size, with Content-Length set to 78:
Supported APIs
The table lists the status of each node:buffer API in Azion Runtime:
| API | Status |
|---|---|
Buffer.alloc() | 🟢 Supported |
Buffer.allocUnsafe() | 🟢 Supported |
Buffer.byteLength() | 🟢 Supported |
Buffer.compare() | 🟢 Supported |
Buffer.concat() | 🟢 Supported |
Buffer.from() | 🟢 Supported |
Buffer.isBuffer() | 🟢 Supported |
Buffer.isEncoding() | 🟢 Supported |
buf.compare() | 🟢 Supported |
buf.copy() | 🟢 Supported |
buf.equals() | 🟢 Supported |
buf.fill() | 🟢 Supported |
buf.indexOf() | 🟢 Supported |
buf.includes() | 🟢 Supported |
buf.lastIndexOf() | 🟢 Supported |
buf.length | 🟢 Supported |
buf.readInt*() | 🟡 Partially supported |
buf.subarray() | 🟢 Supported |
buf.slice() | 🟡 Supported (deprecated, use subarray()) |
buf.toString() | 🟢 Supported |
buf.write() | 🟢 Supported |
buf.writeInt*() | 🟡 Partially supported |
buffer.isAscii() | 🔴 Not supported |
buffer.isUtf8() | 🔴 Not supported |
buffer.resolveObjectURL() | 🔴 Not supported |
buffer.transcode() | 🔴 Not supported |
APIs marked 🟡 Partially supported have limited functionality compared to the full Node.js implementation. Among them, writeUInt32BE(), readUInt32BE(), writeInt16LE(), and readInt16LE() return the values written, and readBigUInt64BE() is available. The module-level functions buffer.isAscii(), buffer.isUtf8(), buffer.resolveObjectURL(), and buffer.transcode() are not supported. Node.js deprecates buf.slice() from v17.5.0; use buf.subarray() instead.