FiloSottile / typage

A TypeScript implementation of the age file encryption format, based on libsodium.
BSD 3-Clause "New" or "Revised" License
64 stars 9 forks source link

Incompatibilities with `node` and `bun` #9

Closed lgarron closed 8 months ago

lgarron commented 8 months ago

When trying to run the passphrase example from the README in node, I get:

import { base64_variants, from_base64, from_string, to_base64, to_string } from "libsodium-wrappers-sumo";
         ^^^^^^^^^^^^^^^
SyntaxError: Named export 'base64_variants' 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 { base64_variants, from_base64, from_string, to_base64, to_string } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v21.1.0

My suspicion was that this was because I'm running this in a project set to "type": "module", but it happens even when I run it in a .mjs file without "type": "module".


When I try to run it in bun, I get:

70 |     }
71 |     encrypt(file) {
72 |         if (typeof file === "string") {
73 |             file = from_string(file);
74 |         }
75 |         const fileKey = sodium.randombytes_buf(16);
                            ^
TypeError: sodium.randombytes_buf is not a function. (In 'sodium.randombytes_buf(16)', 'sodium.randombytes_buf' is undefined)
      at encrypt (/Users/lgarron/Code/git/github.com/cubing/scramble-table/node_modules/age-encryption/dist/index.js:75:24)
      at /Users/lgarron/Code/git/github.com/cubing/scramble-table/age.js:14:19

I'm about to send a PR that includes one possible fix.