Skip to main content

Handling Binary Files

Integrations in Prismatic generally process serialized JSON, XML or other simple strings and pass deserialized JavaScript objects between steps. However, there are situations when you may want to process and pass binary data between steps. By "binary data", we mean files that are not plain text - PDF files, images, MP3 audio, etc.

Within an integration, a binary file is represented as an object with two properties:

  • data which is a Node.js Buffer that contains the file's data
  • contentType which is a string representing the file's MIME type. See Mozilla's documentation for a list of common file MIME types.
{ data: Buffer.from("..."), contentType: "application/pdf" };

Processing binary data as an input

Inputs for binary files are similar to any other input you might create, though you can use the util.types.toBufferDataPayload function to ensure that the input has the form { data: Buffer, contentType: string }:

{
inputs: {
myFile: input({
label: "My File",
type: "data",
required: true,
clean: util.types.toBufferDataPayload,
}),
}
}

The myFile property that comes in to your perform function will have the form of a binary file, with data and contentType properties that you can reference.

{
perform: async (context, params) => {
axios.post("http://my-endpoint", params.myFile.data, {
headers: { "Content-Type": params.myFile.contentType },
});
};
}

Returning binary data from an action

To return a binary file from your action, return the data as a Buffer and optionally include a contentType property alongside data that indicates its MIME type. For example, if your custom component returns a rendered PDF file and the PDF contents are saved in a Buffer variable named pdfContents, the return block might look like this:

return {
data: pdfContents,
contentType: "application/pdf",
};

You can return multiple files, or binary files in a nested object with a similar structure:

return {
myKey: "myValue",
myPdf: {
data: pdfBuffer,
contentType: "application/pdf",
},
myPng: {
data: pngBuffer,
contentType: "image/png",
},
};

Fetching binary data with the Spectral HTTP client

When fetching binary data from an API, you must configure your HTTP client to expect binary data and to write the data to a Buffer. For the HTTP client (which is Axios-based), use the responseType: "arraybuffer" configuration option to ensure the data property returned is a Buffer:

{
perform: async (context, params) => {
const client = createBambooClient(params.connection);
const { data, headers } = await client.get(`/v1/files/${params.fileId}`, {
responseType: "arraybuffer",
});
return { data, contentType: headers["content-type"] };
};
}