Trying to use libsodium with web worker error TypeError: sodium.crypto_pwhash is not a function

591 Views Asked by At

I imported sodium.js using importScripts, but when web worker runs it gives error TypeError: sodium.crypto_pwhash is not a function. Console.log sodium.js in worker however prints the sodium object. What am i missing?

1

There are 1 best solutions below

0
On BEST ANSWER

As explained in this issue, you need to load the browser-sumo version so it includes crypto_pwhash.

const worker_script = `
  self.sodium = {
    onload: function (sodium) {
     postMessage( sodium.crypto_pwhash.toString() );
    }
  };
  importScripts('https://cdn.jsdelivr.net/gh/jedisct1/libsodium.js@master/dist/browsers-sumo/sodium.js');
`;
const worker = new Worker( URL.createObjectURL( new Blob( [ worker_script ], { type: "text/javascript" } ) ) );
worker.onmessage = ({data}) => console.log( data );