Skip to content

Assigning functions to constants before ready fails #327

@FiloSottile

Description

@FiloSottile

I know sodium.ready needs to be called before using the library, but doing so at import time is difficult in libraries (where the await should be delegated to the application) or where lacking top-level await support.

At the same time, a common import pattern, even recommended by the Node error message in the details below (which might be fixable as a separate issue), is

import sodium from 'libsodium-wrappers-sumo'
const { crypto_hash_sha256, from_hex, to_hex } = sodium
file:///Users/filippo/src/filippo.io/age.ts/dist/hkdf.js:2
import { from_string } from "libsodium-wrappers-sumo";
         ^^^^^^^^^^^
SyntaxError: Named export 'from_string' not found. The requested module 'libsodium-wrappers-sumo' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'libsodium-wrappers-sumo';
const { from_string } = pkg;

When using that pattern, crypto_hash_sha256 is referenced before ready, which leads to intermittent errors.

https://github.com/FiloSottile/age.ts/actions/runs/6743827419/job/18332547466

Would it be possible to make it so calling functions before ready fails, but not simply referencing them?

(Also, if the failure was a clearer error, it would help. Debugging mysterious errors about _malloc to incorrect use of ready is pretty hard.)

See FiloSottile/typage@0e7577f.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions