|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -import createLibsodium from "/*{{libsodium}}*/"; |
| 3 | +import libsodiumModuleOrFactory from "/*{{libsodium}}*/"; |
4 | 4 |
|
5 | 5 | const output_format = "uint8array"; |
6 | 6 |
|
7 | 7 | let libsodium; |
8 | 8 | const exports = {}; |
9 | 9 |
|
| 10 | +function _get_sodium_initializer(moduleOrFactory) { |
| 11 | + if (typeof moduleOrFactory === "function") { |
| 12 | + return moduleOrFactory; |
| 13 | + } |
| 14 | + if (moduleOrFactory != null && typeof moduleOrFactory.default === "function") { |
| 15 | + return moduleOrFactory.default; |
| 16 | + } |
| 17 | + return null; |
| 18 | +} |
| 19 | + |
| 20 | +function _get_sodium_module(moduleOrFactory) { |
| 21 | + if (moduleOrFactory != null && typeof moduleOrFactory.ready !== "undefined") { |
| 22 | + return moduleOrFactory; |
| 23 | + } |
| 24 | + if (moduleOrFactory != null && moduleOrFactory.default != null && typeof moduleOrFactory.default.ready !== "undefined") { |
| 25 | + return moduleOrFactory.default; |
| 26 | + } |
| 27 | + return null; |
| 28 | +} |
| 29 | + |
10 | 30 | if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.getRandomValues !== "function") { |
11 | 31 | throw new Error("globalThis.crypto.getRandomValues is not available. The ESM build of libsodium requires a secure random source (available in all browsers and Node.js 19+)."); |
12 | 32 | } |
13 | 33 |
|
14 | | -const ready = createLibsodium({ |
15 | | - getRandomValue: function() { |
16 | | - var buf = new Uint32Array(1); |
17 | | - globalThis.crypto.getRandomValues(buf); |
18 | | - return buf[0] >>> 0; |
19 | | - } |
20 | | -}).then(function (libsodiumModule) { |
| 34 | +const getRandomValue = function() { |
| 35 | + var buf = new Uint32Array(1); |
| 36 | + globalThis.crypto.getRandomValues(buf); |
| 37 | + return buf[0] >>> 0; |
| 38 | +}; |
| 39 | + |
| 40 | +const initializer = _get_sodium_initializer(libsodiumModuleOrFactory); |
| 41 | +const moduleInstance = _get_sodium_module(libsodiumModuleOrFactory); |
| 42 | + |
| 43 | +const ready = (initializer != null |
| 44 | + ? initializer({ |
| 45 | + getRandomValue: getRandomValue |
| 46 | + }) |
| 47 | + : moduleInstance != null |
| 48 | + ? moduleInstance.ready.then(function () { |
| 49 | + return moduleInstance; |
| 50 | + }) |
| 51 | + : Promise.reject(new Error("Unsupported libsodium ESM export shape")) |
| 52 | +).then(function (libsodiumModule) { |
21 | 53 | libsodium = libsodiumModule; |
22 | 54 | exports.libsodium = libsodium; |
23 | 55 |
|
|
0 commit comments