jscu icon indicating copy to clipboard operation
jscu copied to clipboard

Platform Detection Error on Node

Open bootexe opened this issue 5 months ago • 0 comments

Hello first I want to thank you for this amazing work you've done unifying both webcrypto and node crypto API I just encountered an error when I was trying to execute aes encrpytion with js-crpyto-aes on my ExpressJs server i was getting an error saying that my iv was not an instance of Buffer or ArrayBuffer even when it is a Buffer Instance and then I saw in my StackTrace that the error comes from a webapi.js file so I investigate some file in node_modules and i found this js-crypto-env library which seem to detect incorrectly the platform iam running btw iam running on node 22.3.0 npm 10.8.1 so i force the use of require('crypto') in the index.js fil of js-crypto-env and now everything seems to work for me

this is the new function I replaced

var getCrypto = function () {
    var webCrypto = getWebCrypto();
    var nodeCrypto = getNodeCrypto();
    nodeCrypto = require('crypto'); // I added this line
    console.log(nodeCrypto)
    if (typeof nodeCrypto !== 'undefined'){
        console.error('Crypto NODE')//this was to be sure what I was modifying 
        return { name: 'nodeCrypto', crypto: nodeCrypto };
    }
        
    else if (typeof webCrypto !== 'undefined'){
        console.error('Crypto WEB') //this was to be sure what I was modifying 
        return { name: 'webCrypto', crypto: webCrypto };
    }
        
    else
        return { name: undefined };
};

can you fix this method of platform detection in the next update please that would be amazing !

iam not an expert enough to correctly distinguish both platform myself :/

bootexe avatar Sep 22 '24 21:09 bootexe