Compatibilidade APIs Node.js - Buffer Explore a compatibilidade entre Azion Runtime e Node.js, incluindo APIs suportadas, exemplos de código e como usar polyfills no desenvolvimento Edge.

A API Buffer no Node.js é uma classe global usada para manipular dados binários. Ela permite que os desenvolvedores trabalhem diretamente com dados binários brutos, possibilitando a manipulação eficiente de fluxos de dados, operações de entrada/saída de arquivos e comunicações de rede.

/**
* An example of using the Node.js Buffer API in an Azion Edge Function.
* Support:
* - Partial support
* @module runtime-apis/nodejs/buffer/main
* @example
* // Execute with Azion Bundler:
* npx edge-functions build
* npx edge-functions dev
*/
import { Buffer } from "node:buffer";
/**
* Example of using the Node.js Buffer API
* Writes `string` to `buf` at `offset` according to the character encoding in`encoding`. The `length` parameter is the number of bytes to write. If `buf` did
* not contain enough space to fit the entire string, only part of `string` will be
* written. However, partially encoded characters will not be written.
* @param {*} event
* @returns {Promise<Response>}
*/
const main = async (event) => {
const helloBuffer = Buffer.from("Hello Edge!", "utf8");
console.log(helloBuffer.toString("hex"));
// 48656c6c6f204564676521
console.log(helloBuffer.toString("base64"));
// SGVsbG8gRWRnZSE=
helloBuffer.write("World", 6, 5, "utf8");
console.log(helloBuffer.toString());
// Hello World!
return new Response(helloBuffer.toString(), { status: 200 });
};
export default main;

Contribuidores