Run Function, Javascript dependencies

I am building a worker that will be triggered by email, this worker will take the attachment url provided by mindstudio, access it’s content and transform the content into base64 for later use in the workflow. (This process will be done through a custom javascript function).

But a serious problem Iam facing, is that the node.js libraries needed for the function to download the file, transform it’s content into a base64 string and save it for later use in the workflow, are not recognized by the mindstudio node.js environment.

Libraries such as: -arrayBuffer / blop / Buffer / and others libraries cruicial for the function, and the overall completion possibility of my project.
(Everytime i get: undefined (“Buffer”) or “response.arrayBuffer is not a function”)

So please any help is greatly appreciated.
If more detail is needed about the error, Iam open for discussion.

Thank you,

Hi @Paraformal, you are correct that the NodeJS environment available in MindStudio does not have access to some of the NodeJS standard library for security reasons.

arrayBuffer should be available on fetch() results, though. Please try:

const request = await fetch('https://some-remote-url...');
const buffer = await request.arrayBuffer();
console.log(buffer.byteLength);

Additionally, btoa and atob are available to convert between Base64 and ASCII.

Can you share more about what you are trying to do with the attachment? If you need to extract text from it you can use the Extract Text block, which will be much easier and more reliable.

Sorry, Sean, for the late reply—I didn’t notice your response earlier.

Regarding the attachment, it is a ZIP file. I want to take the URL provided by MindStudio, which hosts the ZIP file sent via email, access this URL, and return its content as a Base64-encoded string. However, instead of encoding the URL text itself, I need to encode the actual the ZIP file.

Additionally, I am facing the same issue with other custom scripts I am writing, such as formatting request bodies for API calls, URL encoding, and using certain libraries. For example unzipping and extracting content of a zip file through the provided url of the file. This require some external libraries that i couldn’t find in mindstudio javascript environment.

I managed to find a workaround by hosting my code on a server and calling my own API from MindStudio. However, it would be preferable if the constraints on imports were more flexible or if an alternative solution could be provided.

Thank you!!

Hi @Paraformal, I wanted to update you that you can now extract zip files within custom functions using const fileUrls = await ai.unzipFile(url). Use the url of the attachment from the start block and this function will give you back a list of remote URLs to the files within. From there you can do whatever you like with them.

Hi @sean , thank you for the update, i will try it right away.
Thanks again for your time and support

1 Like