Plugin - Node Code - Help needed ! :))

Hi There,

Trying to design a plugin to create SHA256 ChechSum.,
but total lack of knowledge… :))

Here is the Code

async function(properties, context) {
const crypto = require(‘crypto’);
const fs = require(‘fs’);
const fileBuffer = fs.readFileSync(myfile);
const hashSum = crypto.createHash(‘sha256’);
hashSum.update(fileBuffer);
const hex = hashSum.digest(‘hex’);
}

I’ve got the file

And the Return

Any Help?

Your code is almost correct, try defining myfile. You should define the myfile variable with the path to the file you want to calculate the checksum for.
The provided code is an asynchronous function, but it’s not returning anything. If you want to use this function and get the result, you should return the calculated hash.

Of course the plugin is not executed on your pc but on bubble’s servers, so the file will not be on the filesystem, but it would be fetched from the network or passed to the plugin through the property object.

Hi Guys, some evolution based on ChatGPT… Incredible…

But I still got errors,

I passed a file adress like
https://78f05567d4b12333725d740a9d666836.cdn.bubble.io/f1704907230652x982316547212002200/unnamed.png

But gor an error
Specting a String, While we got an object

Why?

Here is the code

async function(properties, context) {

const fs = require('fs');
const crypto = require('crypto');

// Function to calculate SHA-256 hash of a file
function calculateHash(filePath) {
    return new Promise((resolve, reject) => {
        const hash = crypto.createHash('sha256');
        const stream = fs.createReadStream(filePath);

        stream.on('data', data => {
            hash.update(data);
        });

        stream.on('end', () => {
            const fileHash = hash.digest('hex');
            resolve(fileHash);
        });

        stream.on('error', error => {
            reject(error);
        });
    });
}

    //LOAD
    var hashedText = calculateHash(properties.filePath);
    
    //DO THE OPERATION
    return {
        hashedValue : hashedText
    }

}

Can’t use fs. You’re going to have to fetch the file url and store the bytes.

let url = properties.template_file.startsWith('https:') ? properties.template_file : 'https:' + properties.template_file;
let res = await fetch(url);
let fileBytes = await res.arrayBuffer();

and also bubble is expecting an object with a string, not a Promise. Try to awaitthe calculatehash function.
Generally speaking copy pasting code from gpt or our answers without understanding what’s going on it’s a good way to set up future problems. If you have time try learning a bit more about what all this is about.

1 Like

Tks !!! Solved !!!

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.