jedisct1 / libsodium.js

libsodium compiled to Webassembly and pure JavaScript, with convenient wrappers.
Other
968 stars 138 forks source link

TypeError: Cannot read property 'Qb' of undefined #293

Closed ehamery closed 11 months ago

ehamery commented 2 years ago

Hi, I am trying to use libsodium-wrappers in Typescript with:

import libsodium from 'libsodium-wrappers';

I am doing this in several files where I use libsodium. But then if I import more than 1 of those files in the same file, any call to await libsodium.ready throws an exception:

TypeError: Cannot read property 'Qb' of undefined
    at Object.I._crypto_sign_seedbytes (/test/node_modules/libsodium/dist/modules/libsodium.js:1:316118)
    at Object.Bt [as crypto_sign_seed_keypair] (/test/node_modules/libsodium-wrappers/dist/modules/libsodium-wrappers.js:1:64050)

The object that is not defined is I.asm

Any idea what may cause that? And how I can fix it? Any help would be much appreciated.

lmtr0 commented 1 year ago

are you using vite?

ehamery commented 1 year ago

@jedisct1 was the issue fixed?

jedisct1 commented 1 year ago

I don't know :)

Someone asked you a question and you never replied, so I guess you don't need this any more?

lmtr0 commented 1 year ago

It's surprising that after almost a year, the first question it was this fixed instead of helping us help you, ANYWAY! For anyone having the same issues and that are using vite, here is the solution that worked for me. The full source code is available here https://gitlab.com/higenku/apps/account/-/tree/dev/client?ref_type=heads

take a closer look into these files:

ehamery commented 11 months ago

It is even more surprising that nobody tried to reproduce the issue from the given description; and instead, after 10 months, wild guess is made to blame a third party module, ANYWAY!

I do not use vitejs if that was what @lmtr0 was referring to.

lmtr0 commented 11 months ago

I mean..... I'm not having that issue using vite with SvelteKit, as I described on the website that I was building using libsodium for encryption. Without a proper response to as which package bundler you are using (if using one at all) it's difficult to reproduce the issue. It's like trying to find hair in an egg without knowing what an egg is. You cannot expect developers to just go out of their way and test hundreds of javascript frameworks, package bundlers and UI libraries on hundreds of runtimes to try and find an error that YOU are having, IT IS A HUGE WASTE OF TIME. It's easier if you help us narrow down the problem, so we can be efficient

Moving on, are you using a package bundler at all? Are you using a static site generator, a framework or typescript? What are you trying to accomplish? a website, mobile app, desktop app with electron? Can you please describe your environment?

lmtr0 commented 11 months ago

@jedisct1 can you please reopen the issue?

lmtr0 commented 11 months ago

thx @jedisct1

ehamery commented 11 months ago

I am not sure why @lmtr0 you are insisting on adding extra parameters to the issue... So instead of wasting time in litanies you could just read at the description of the issue. To make it even clearer:

No extra package, framework or whatever is needed.

jedisct1 commented 11 months ago
import libsodium from 'libsodium-wrappers';

export async function test() {
    await libsodium.ready;
    console.log('libsodium is ready');
}
import libsodium from 'libsodium-wrappers';

export async function test2() {
    await libsodium.ready;
    console.log('libsodium is ready (2)');
}
import {test} from './file1';
import {test2} from './file2';

test();
test2();
{ "dependencies": { "libsodium-wrappers": "^0.7.13" } }

Running:

$ bun main.ts

libsodium is ready
libsodium is ready (2)

No issues here.

That being said, initializing it twice is not expected. You should only initialize it once once your application starts, and then use that instance everywhere afterwards.

If it's not an actual application with a start function, just do something like:

let _sodium: any;

export async function sodiumInstance() {
    if (!_sodium) {
        _sodium = await libsodium.ready;
    }
    return _sodium;
}
lmtr0 commented 11 months ago

at this point we can assume that this is not an issue anymore, great, lets move on! Unfortunately I cannot close this issue

lmtr0 commented 11 months ago

thank you and sorry for the extra work