Skip to main content

Managing file with buffer

With a Buffer, you can store files directly inside the Spell's memory.

Here is a snippet to store a remote file inside a Buffer:

const imageInBuffer: Buffer = await axios.get(
"https://images.unsplash.com/photo-1546776310-eef45dd6d63c?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=739&q=80",
{
responseType: "arraybuffer",
}
);

You can also create a Buffer from a string (utf8) or an object:

const fileInBuffer = new Buffer.from(myObject);

Or a base64 string:

const fileFromBase64 = new Buffer(base64String, "base64");

Then you can manipulate this variable to output a file or to link it inside a POST request, etc.

danger

Memory are limited inside a Spell. If you tried to store too large files inside a Buffer, you'll break your Spell's execution.

By default, memory inside a Spell is limited to 128MB