Skip to content

Documentation / @agentick/shared / bufferToBase64Source

Function: bufferToBase64Source() ​

bufferToBase64Source(buffer, mimeType?): Base64Source

Defined in: blocks.ts:580

Convert a Buffer or Uint8Array to a serializable Base64Source.

Browser-compatible: works in both Node.js and browser environments. Uses globalThis to avoid requiring @types/node in browser environments.

Parameters ​

buffer ​

Uint8Array<ArrayBufferLike> | { buffer: ArrayBufferLike; byteLength: number; byteOffset: number; }

mimeType? ​

string

Returns ​

Base64Source

Example ​

typescript
// Node.js
const imageBuffer = await fs.readFile('image.png');
const imageBlock: ImageBlock = {
  type: 'image',
  source: bufferToBase64Source(imageBuffer, 'image/png'),
};

// Browser
const response = await fetch('image.png');
const arrayBuffer = await response.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
const imageBlock: ImageBlock = {
  type: 'image',
  source: bufferToBase64Source(uint8Array, 'image/png'),
};

Released under the ISC License.