Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Cloudflare Docs
Log in

TheBuffer API in Node.js is one of the most commonly used Node.js APIs for manipulating binary data. EveryBuffer instance extends from the standardUint8Array class, but adds a range of unique capabilities such as built-in base64 and hex encoding/decoding, byte-order manipulation, and encoding-aware substring searching.

JavaScript
import{Buffer} from"node:buffer";
constbuf=Buffer.from("hello world","utf8");
console.log(buf.toString("hex"));
// Prints: 68656c6c6f20776f726c64
console.log(buf.toString("base64"));
// Prints: aGVsbG8gd29ybGQ=

A Buffer extends fromUint8Array. Therefore, it can be used in any Workers API that currently acceptsUint8Array, such as creating a new Response:

JavaScript
constresponse=newResponse(Buffer.from("hello world"));

You can also use theBuffer API when interacting with streams:

JavaScript
constwritable=getWritableStreamSomehow();
constwriter=writable.getWriter();
writer.write(Buffer.from("hello world"));

One key difference between the Workers implementation ofBuffer and the Node.jsimplementation is that some methods of creating aBuffer in Node.js will allocatethose from a global memory pool as a performance optimization. The Workers implementationdoes not use a memory pool and allBuffer instances are allocated independently.

Further, in Node.js it is possible to allocate aBuffer with uninitialized memoryusing theBuffer.allocUnsafe() method. This is not supported in Workers andBufferinstances are always initialized so that theBuffer is always filledwith null bytes (0x00) when allocated.

Refer to theNode.js documentation forBuffer for more information.


[8]
ページ先頭

©2009-2026 Movatter.jp