diff --git a/assets/pasted-20260216-072603-dff6b275.jpg b/assets/pasted-20260216-072603-dff6b275.jpg new file mode 100644 index 0000000..0ee6158 Binary files /dev/null and b/assets/pasted-20260216-072603-dff6b275.jpg differ diff --git a/bot/bot.log b/bot/bot.log index 7d8be63..5761667 100644 --- a/bot/bot.log +++ b/bot/bot.log @@ -1268,3 +1268,132 @@ Alarm scheduled at 0 30 03 * * * [2026-02-16T07:06:43.786Z] Alarm scheduled at 0 30 03 * * * [2026-02-16T07:12:30.682Z] Ready! Logged in as AsepXiaoQin#6954 [2026-02-16T07:12:30.922Z] Alarm scheduled at 0 30 03 * * * +[2026-02-16T07:18:44.998Z] Bot berhasil login! +[2026-02-16T07:18:45.002Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:18:45.007Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:18:46.502Z] Failed to schedule alarm: Too few fields +[2026-02-16T07:19:20.513Z] Bot berhasil login! +[2026-02-16T07:19:20.514Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:19:20.514Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:19:21.251Z] Failed to schedule alarm: sahurTime is not defined +[2026-02-16T07:20:05.257Z] Bot berhasil login! +[2026-02-16T07:20:05.260Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:20:05.262Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:20:05.746Z] Alarm scheduled at 30 3 * * * +[2026-02-16T07:20:37.264Z] Bot berhasil login! +[2026-02-16T07:20:37.266Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:20:37.267Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:20:38.018Z] Alarm scheduled at 30 3 * * * +[2026-02-16T07:23:54.764Z] Interaction Error: There is no playing queue in this guild +[2026-02-16T07:25:00.894Z] Bot berhasil login! +[2026-02-16T07:25:00.895Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:25:00.896Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:25:01.155Z] Alarm scheduled at 30 3 * * * +[dotenv@17.3.1] injecting env (4) from .env -- tip: βš™οΈ write to custom object with { processEnv: myObject } +Dependency Report: +-------------------------------------------------- +Core Dependencies +- @discordjs/voice: 0.19.0 +- prism-media: 1.3.5 + +Opus Libraries +- @discordjs/opus: 0.9.0 +- opusscript: 0.0.8 + +Encryption Libraries +- native crypto support for aes-256-gcm: yes +- sodium-native: 5.0.10 +- sodium: not found +- libsodium-wrappers: 0.7.16 +- @stablelib/xchacha20poly1305: 2.0.1 +- @noble/ciphers: not found + +DAVE Libraries +- @snazzah/davey: 0.1.9 + +FFmpeg +- version: 7.0.2-static https://johnvansickle.com/ffmpeg/ +- libopus: yes +-------------------------------------------------- +[2026-02-16T07:32:47.310Z] Encryption library (libsodium) is ready. +Encryption library (libsodium) is ready. +[2026-02-16T07:32:49.307Z] Bot berhasil login! +Bot berhasil login! +[2026-02-16T07:32:49.541Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:32:49.541Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +Ready! Logged in as AsepXiaoQin#6954 +FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:32:51.062Z] Alarm scheduled at 30 3 * * * +Alarm scheduled at 30 3 * * * +[dotenv@17.3.1] injecting env (4) from .env -- tip: πŸ” prevent building .env in docker: https://dotenvx.com/prebuild +Dependency Report: +-------------------------------------------------- +Core Dependencies +- @discordjs/voice: 0.19.0 +- prism-media: 1.3.5 + +Opus Libraries +- @discordjs/opus: 0.9.0 +- opusscript: 0.0.8 + +Encryption Libraries +- native crypto support for aes-256-gcm: yes +- sodium-native: 5.0.10 +- sodium: not found +- libsodium-wrappers: 0.7.16 +- @stablelib/xchacha20poly1305: 2.0.1 +- @noble/ciphers: not found + +DAVE Libraries +- @snazzah/davey: 0.1.9 + +FFmpeg +- version: 7.0.2-static https://johnvansickle.com/ffmpeg/ +- libopus: yes +-------------------------------------------------- +[2026-02-16T07:35:04.554Z] Encryption library (libsodium) is ready. +Encryption library (libsodium) is ready. +[2026-02-16T07:35:05.001Z] Bot berhasil login! +[2026-02-16T07:35:05.001Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:35:05.002Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +Bot berhasil login! +Ready! Logged in as AsepXiaoQin#6954 +FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:35:05.079Z] Alarm scheduled at 30 3 * * * +Alarm scheduled at 30 3 * * * +[dotenv@17.3.1] injecting env (4) from .env -- tip: πŸ” encrypt with Dotenvx: https://dotenvx.com +Dependency Report: +-------------------------------------------------- +Core Dependencies +- @discordjs/voice: 0.19.0 +- prism-media: 1.3.5 + +Opus Libraries +- @discordjs/opus: 0.9.0 +- opusscript: 0.0.8 + +Encryption Libraries +- native crypto support for aes-256-gcm: yes +- sodium-native: 5.0.10 +- sodium: not found +- libsodium-wrappers: 0.7.16 +- @stablelib/xchacha20poly1305: 2.0.1 +- @noble/ciphers: not found + +DAVE Libraries +- @snazzah/davey: 0.1.9 + +FFmpeg +- version: 7.0.2-static https://johnvansickle.com/ffmpeg/ +- libopus: yes +-------------------------------------------------- +[2026-02-16T07:37:25.997Z] Encryption library (libsodium) is ready. +Encryption library (libsodium) is ready. +[2026-02-16T07:37:26.881Z] Bot berhasil login! +[2026-02-16T07:37:26.882Z] Ready! Logged in as AsepXiaoQin#6954 +[2026-02-16T07:37:26.882Z] FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +Bot berhasil login! +Ready! Logged in as AsepXiaoQin#6954 +FFMPEG Path: /home/ubuntu/executor/workspace/bot/node_modules/ffmpeg-static/ffmpeg +[2026-02-16T07:37:26.937Z] Alarm scheduled at 30 3 * * * +Alarm scheduled at 30 3 * * * diff --git a/bot/index.js b/bot/index.js index fef7606..57c2647 100644 --- a/bot/index.js +++ b/bot/index.js @@ -1,20 +1,29 @@ -process.env.FFMPEG_PATH = require('ffmpeg-static'); +const { Client, GatewayIntentBits, Events, EmbedBuilder, PermissionsBitField } = require('discord.js'); +const { DisTube } = require('distube'); +const { YtDlpPlugin } = require('@distube/yt-dlp'); +const { SoundCloudPlugin } = require('@distube/soundcloud'); +const { SpotifyPlugin } = require('@distube/spotify'); +const { CronJob } = require('cron'); const path = require('path'); const fs = require('fs'); -const { Client, GatewayIntentBits, Events } = require('discord.js'); -const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, NoSubscriberBehavior } = require('@discordjs/voice'); -const { CronJob } = require('cron'); -const play = require('play-dl'); -// Ensure .env is loaded early +// Load environment variables require('dotenv').config({ path: path.join(__dirname, '.env') }); -// Tambahkan folder ffmpeg ke PATH -const ffmpegDir = path.dirname(process.env.FFMPEG_PATH); +// Setup FFMPEG +const ffmpegStatic = require('ffmpeg-static'); +process.env.FFMPEG_PATH = ffmpegStatic; +const ffmpegDir = path.dirname(ffmpegStatic); if (!process.env.PATH.includes(ffmpegDir)) { process.env.PATH = `${ffmpegDir}${path.delimiter}${process.env.PATH}`; } +// Generate dependency report for debugging +try { + const { generateDependencyReport } = require('@discordjs/voice'); + console.log("Dependency Report:\n" + generateDependencyReport()); +} catch (e) {} + const client = new Client({ intents: [ GatewayIntentBits.Guilds, @@ -24,13 +33,30 @@ const client = new Client({ ] }); +// Detect libsodium +try { + const sodium = require('libsodium-wrappers'); + sodium.ready.then(() => { + logToFile("Encryption library (libsodium) is ready."); + }); +} catch (e) { + logToFile("Warning: libsodium-wrappers not found or failed to load."); +} + +const distube = new DisTube(client, { + plugins: [ + new SoundCloudPlugin(), + new SpotifyPlugin(), + new YtDlpPlugin(), + ], + emitNewSongOnly: true, + emitAddSongWhenCreatingQueue: false, +}); + const VC_ID = process.env.VC_ID; const AUDIO_PATH = path.join(__dirname, 'assets/audio/sahur.mp3'); const PREFIX = '!'; -// Global state -const queues = new Map(); // guildId -> { queue: [], player, connection } - function logToFile(message) { const logMessage = `[${new Date().toISOString()}] ${message}\n`; try { @@ -42,15 +68,19 @@ function logToFile(message) { } client.once(Events.ClientReady, () => { - console.log('Bot berhasil login!'); + logToFile(`Bot berhasil login!`); logToFile(`Ready! Logged in as ${client.user.tag}`); - + logToFile(`FFMPEG Path: ${process.env.FFMPEG_PATH}`); + // Sahur Alarm - const sahurTime = process.env.SAHUR_TIME || '30 03 * * *'; - let cronTime = sahurTime; - if (sahurTime.includes(':') && !sahurTime.includes('*')) { - const [hour, minute] = sahurTime.split(':'); - cronTime = `0 ${minute} ${hour} * * *`; + const rawSahurTime = process.env.SAHUR_TIME || '03:30'; + let cronTime = '30 3 * * *'; + + if (rawSahurTime.includes(':')) { + const [hour, minute] = rawSahurTime.split(':'); + cronTime = `${parseInt(minute)} ${parseInt(hour)} * * *`; + } else { + cronTime = rawSahurTime; } try { @@ -59,8 +89,13 @@ client.once(Events.ClientReady, () => { const guild = client.guilds.cache.first(); if (!guild) return; const channel = guild.channels.cache.get(VC_ID); - if (channel) { - playLocal(channel, AUDIO_PATH); + if (channel && fs.existsSync(AUDIO_PATH)) { + distube.voices.join(channel).then(voice => { + const stream = fs.createReadStream(AUDIO_PATH); + voice.play(stream); + }).catch(err => { + logToFile(`Alarm join error: ${err.message}`); + }); } }, null, true, 'Asia/Jakarta'); logToFile(`Alarm scheduled at ${cronTime}`); @@ -69,28 +104,86 @@ client.once(Events.ClientReady, () => { } }); -async function playLocal(channel, filePath) { +// Slash Command Handler +client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isChatInputCommand()) return; + + const { commandName } = interaction; + const voiceChannel = interaction.member?.voice.channel; + try { - const connection = joinVoiceChannel({ - channelId: channel.id, - guildId: channel.guild.id, - adapterCreator: channel.guild.voiceAdapterCreator, - }); + if (commandName === 'join') { + if (!voiceChannel) return interaction.reply({ content: 'Anda harus berada di voice channel!', ephemeral: true }); + await interaction.deferReply(); + await distube.voices.join(voiceChannel); + await interaction.editReply('Sudah join! 🎧'); + } - const player = createAudioPlayer(); - const resource = createAudioResource(filePath); - player.play(resource); - connection.subscribe(player); + else if (commandName === 'testsahur') { + if (!voiceChannel) return interaction.reply({ content: 'Anda harus berada di voice channel!', ephemeral: true }); + if (!fs.existsSync(AUDIO_PATH)) return interaction.reply({ content: `File audio tidak ditemukan di ${AUDIO_PATH}`, ephemeral: true }); + + await interaction.deferReply(); + logToFile(`Playing local file using stream: ${AUDIO_PATH}`); + try { + const voice = await distube.voices.join(voiceChannel); + const stream = fs.createReadStream(AUDIO_PATH); + voice.play(stream); + await interaction.editReply('Memainkan suara sahur... πŸ“’'); + } catch (e) { + logToFile(`Error in testsahur: ${e.message}`); + await interaction.editReply(`Gagal memutar audio: ${e.message}`); + } + } - player.once(AudioPlayerStatus.Idle, () => { - connection.destroy(); - }); - } catch (err) { - logToFile(`Error playing local file: ${err.message}`); + else if (commandName === 'play') { + const query = interaction.options.getString('query'); + if (!voiceChannel) return interaction.reply({ content: 'Anda harus berada di voice channel!', ephemeral: true }); + + await interaction.deferReply(); + logToFile(`Fetching stream for: ${query}`); + distube.play(voiceChannel, query, { + member: interaction.member, + textChannel: interaction.channel, + interaction + }).catch(e => { + logToFile(`Play Error: ${e.message}`); + interaction.editReply(`Terjadi kesalahan saat memutar: ${e.message}`); + }); + await interaction.editReply(`Mencari: **${query}**... πŸ”`); + } + + else if (commandName === 'pause') { + distube.pause(interaction.guildId); + await interaction.reply('Musik dipause. ⏸️'); + } + + else if (commandName === 'resume') { + distube.resume(interaction.guildId); + await interaction.reply('Musik dilanjutkan. ▢️'); + } + + else if (commandName === 'skip') { + await distube.skip(interaction.guildId); + await interaction.reply('Lagu dilewati! ⏭️'); + } + + else if (commandName === 'stop') { + await distube.stop(interaction.guildId); + await interaction.reply('Musik dihentikan dan antrean dihapus. πŸ‘‹'); + } + } catch (error) { + logToFile(`Interaction Error (${commandName}): ${error.message}`); + if (!interaction.replied && !interaction.deferred) { + await interaction.reply({ content: `Terjadi kesalahan: ${error.message}`, ephemeral: true }); + } else { + await interaction.editReply({ content: `Terjadi kesalahan: ${error.message}`, ephemeral: true }); + } } -} +}); -client.on(Events.MessageCreate, async (message) => { +// Prefix Command Handler (Legacy/Backup) +client.on(Events.MessageCreate, async message => { if (message.author.bot || !message.content.startsWith(PREFIX)) return; const args = message.content.slice(PREFIX.length).trim().split(/ +/); @@ -98,134 +191,64 @@ client.on(Events.MessageCreate, async (message) => { const voiceChannel = message.member?.voice.channel; try { - if (command === 'join') { - if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); - joinVoiceChannel({ - channelId: voiceChannel.id, - guildId: message.guild.id, - adapterCreator: message.guild.voiceAdapterCreator, - }); - message.reply('Sudah join! 🎧'); - } - - else if (command === 'testsahur') { - if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); - if (!fs.existsSync(AUDIO_PATH)) return message.reply(`File audio tidak ditemukan di ${AUDIO_PATH}`); - playLocal(voiceChannel, AUDIO_PATH); - message.reply('Memainkan suara sahur... πŸ“’'); - } - - else if (command === 'play') { - if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); + if (command === 'play') { const query = args.join(' '); - if (!query) return message.reply('Berikan link atau nama lagu!'); - - let serverQueue = queues.get(message.guild.id); - - if (!serverQueue) { - serverQueue = { - songs: [], - connection: null, - player: createAudioPlayer({ - behaviors: { noSubscriber: NoSubscriberBehavior.Play } - }), - }; - queues.set(message.guild.id, serverQueue); + if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); + if (!query) return message.reply('Masukkan judul atau link lagu!'); + distube.play(voiceChannel, query, { + member: message.member, + textChannel: message.channel, + message + }); + } else if (['skip', 'stop', 'pause', 'resume'].includes(command)) { + if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); + const method = command === 'skip' ? 'skip' : (command === 'stop' ? 'stop' : (command === 'pause' ? 'pause' : 'resume')); + await distube[method](message.guildId); + message.reply(`${command} berhasil!`); + } else if (command === 'testsahur') { + if (!voiceChannel) return message.reply('Anda harus berada di voice channel!'); + try { + const voice = await distube.voices.join(voiceChannel); + const stream = fs.createReadStream(AUDIO_PATH); + voice.play(stream); + message.reply('Mengetes suara sahur... πŸ“’'); + } catch (e) { + message.reply(`❌ Error: ${e.message}`); } - - message.reply('Mencari lagu... πŸ”'); - - let songInfo; - if (play.sp_validate(query) === 'track') { - const sp_data = await play.spotify(query); - const search = await play.search(`${sp_data.name} ${sp_data.artists[0].name}`, { limit: 1 }); - songInfo = { title: sp_data.name, url: search[0].url }; - } else if (play.so_validate(query)) { - const so_data = await play.soundcloud(query); - songInfo = { title: so_data.name, url: so_data.url }; - } else { - const yt_info = await play.search(query, { limit: 1 }); - if (yt_info.length === 0) return message.channel.send('Lagu tidak ditemukan!'); - songInfo = { title: yt_info[0].title, url: yt_info[0].url }; - } - - serverQueue.songs.push(songInfo); - - if (serverQueue.songs.length === 1) { - playSong(message.guild.id, voiceChannel); - message.channel.send(`🎡 Sekarang memutar: **${songInfo.title}**`); - } else { - message.channel.send(`βœ… **${songInfo.title}** ditambahkan ke antrean.`); - } - } - - else if (command === 'skip') { - const serverQueue = queues.get(message.guild.id); - if (!serverQueue) return message.reply('Tidak ada lagu yang sedang diputar!'); - serverQueue.player.stop(); - message.reply('Lagu dilewati! ⏭️'); - } - - else if (command === 'stop') { - const serverQueue = queues.get(message.guild.id); - if (serverQueue) { - serverQueue.songs = []; - serverQueue.player.stop(); - if (serverQueue.connection) serverQueue.connection.destroy(); - queues.delete(message.guild.id); - } - message.reply('Musik dihentikan. πŸ‘‹'); - } - - else if (command === 'pause') { - const serverQueue = queues.get(message.guild.id); - if (serverQueue) serverQueue.player.pause(); - message.reply('Dipause. ⏸️'); - } - - else if (command === 'resume') { - const serverQueue = queues.get(message.guild.id); - if (serverQueue) serverQueue.player.unpause(); - message.reply('Dilanjutkan. ▢️'); } } catch (error) { - logToFile(`Command Error (${command}): ${error.stack || error.message}`); - message.reply(`❌ Terjadi kesalahan: ${error.message}`); + logToFile(`Message Command Error: ${error.message}`); + message.reply(`❌ Error: ${error.message}`); } }); -async function playSong(guildId, channel) { - const serverQueue = queues.get(guildId); - if (!serverQueue || serverQueue.songs.length === 0) { - return; - } - - try { - const song = serverQueue.songs[0]; - const stream = await play.stream(song.url); - const resource = createAudioResource(stream.stream, { inputType: stream.type }); - - if (!serverQueue.connection) { - serverQueue.connection = joinVoiceChannel({ - channelId: channel.id, - guildId: guildId, - adapterCreator: channel.guild.voiceAdapterCreator, - }); - serverQueue.connection.subscribe(serverQueue.player); +// DisTube Events +distube + .on('playSong', (queue, song) => { + queue.textChannel.send(`🎡 Sekarang memutar: **${song.name}** - \`${song.formatDuration}\`\nRequested by: ${song.user}`); + }) + .on('addSong', (queue, song) => { + queue.textChannel.send(`βœ… **${song.name}** ditambahkan ke antrean oleh ${song.user}`); + }) + .on('error', (channel, e) => { + logToFile(`DisTube Error: ${e.message}`); + if (e.message.includes('Sign in to confirm you’re not a bot')) { + if (channel) channel.send(`❌ YouTube memblokir akses bot. Silakan coba link dari SoundCloud atau Spotify.`); + } else if (channel) { + channel.send(`❌ Terjadi kesalahan: ${e.message.slice(0, 2000)}`); } + }); - serverQueue.player.play(resource); +// Handle uncaught errors to prevent crash +process.on('unhandledRejection', error => { + logToFile(`Unhandled Rejection: ${error.message}`); + console.error(error); +}); - serverQueue.player.once(AudioPlayerStatus.Idle, () => { - serverQueue.songs.shift(); - playSong(guildId, channel); - }); - } catch (err) { - logToFile(`Playback Error: ${err.message}`); - serverQueue.songs.shift(); - playSong(guildId, channel); - } -} +process.on('uncaughtException', error => { + logToFile(`Uncaught Exception: ${error.message}`); + console.error(error); +}); -// Login at the very bottom +// Login client.login(process.env.DISCORD_TOKEN); diff --git a/bot/node_modules/.package-lock.json b/bot/node_modules/.package-lock.json index 6d61e48..5881d17 100644 --- a/bot/node_modules/.package-lock.json +++ b/bot/node_modules/.package-lock.json @@ -529,6 +529,47 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, + "node_modules/bare-addon-resolve": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/bare-addon-resolve/-/bare-addon-resolve-1.10.0.tgz", + "integrity": "sha512-sSd0jieRJlDaODOzj0oe0RjFVC1QI0ZIjGIdPkbrTXsdVVtENg14c+lHHAhHwmWCZ2nQlMhy8jA3Y5LYPc/isA==", + "license": "Apache-2.0", + "dependencies": { + "bare-module-resolve": "^1.10.0", + "bare-semver": "^1.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } + }, + "node_modules/bare-module-resolve": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/bare-module-resolve/-/bare-module-resolve-1.12.1.tgz", + "integrity": "sha512-hbmAPyFpEq8FoZMd5sFO3u6MC5feluWoGE8YKlA8fCrl6mNtx68Wjg4DTiDJcqRJaovTvOYKfYngoBUnbaT7eg==", + "license": "Apache-2.0", + "dependencies": { + "bare-semver": "^1.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } + }, + "node_modules/bare-semver": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bare-semver/-/bare-semver-1.0.2.tgz", + "integrity": "sha512-ESVaN2nzWhcI5tf3Zzcq9aqCZ676VWzqw07eEZ0qxAcEOAFYBa0pWq8sK34OQeHLY3JsfKXZS9mDyzyxGjeLzA==", + "license": "Apache-2.0" + }, "node_modules/brace-expansion": { "version": "1.1.12", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz", @@ -1524,6 +1565,18 @@ "node": ">= 6" } }, + "node_modules/require-addon": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/require-addon/-/require-addon-1.2.0.tgz", + "integrity": "sha512-VNPDZlYgIYQwWp9jMTzljx+k0ZtatKlcvOhktZ/anNPI3dQ9NXk7cq2U4iJ1wd9IrytRnYhyEocFWbkdPb+MYA==", + "license": "Apache-2.0", + "dependencies": { + "bare-addon-resolve": "^1.3.0" + }, + "engines": { + "bare": ">=1.10.0" + } + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -1656,6 +1709,19 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, + "node_modules/sodium-native": { + "version": "5.0.10", + "resolved": "https://registry.npmjs.org/sodium-native/-/sodium-native-5.0.10.tgz", + "integrity": "sha512-UIw+0AbpCQRuTJF88JWrZomP4O+PXhlWvdopiAJOsUivTyHTf3korMyStxkZuPngSbBEtEfDdc4ewEd8/T4/lA==", + "license": "MIT", + "dependencies": { + "require-addon": "^1.1.0", + "which-runtime": "^1.2.1" + }, + "engines": { + "bare": ">=1.16.0" + } + }, "node_modules/soundcloud.ts": { "version": "0.6.9", "resolved": "https://registry.npmjs.org/soundcloud.ts/-/soundcloud.ts-0.6.9.tgz", @@ -1836,6 +1902,12 @@ "webidl-conversions": "^3.0.0" } }, + "node_modules/which-runtime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/which-runtime/-/which-runtime-1.3.2.tgz", + "integrity": "sha512-5kwCfWml7+b2NO7KrLMhYihjRx0teKkd3yGp1Xk5Vaf2JGdSh+rgVhEALAD9c/59dP+YwJHXoEO7e8QPy7gOkw==", + "license": "Apache-2.0" + }, "node_modules/wide-align": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz", diff --git a/bot/node_modules/bare-addon-resolve/LICENSE b/bot/node_modules/bare-addon-resolve/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bot/node_modules/bare-addon-resolve/README.md b/bot/node_modules/bare-addon-resolve/README.md new file mode 100644 index 0000000..b33f097 --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/README.md @@ -0,0 +1,156 @@ +# bare-addon-resolve + +Low-level addon resolution algorithm for Bare. The algorithm is implemented as a generator function that yields either package manifests to be read or resolution candidates to be tested by the caller. As a convenience, the main export is a synchronous and asynchronous iterable that relies on package manifests being read by a callback. For asynchronous iteration, the callback may return promises which will be awaited before being passed to the generator. + +``` +npm i bare-addon-resolve +``` + +## Usage + +For synchronous resolution: + +```js +const resolve = require('bare-addon-resolve') + +function readPackage(url) { + // Read and parse `url` if it exists, otherwise `null` +} + +for (const resolution of resolve('./addon', new URL('file:///directory/'), readPackage)) { + console.log(resolution) +} +``` + +For asynchronous resolution: + +```js +const resolve = require('bare-addon-resolve') + +async function readPackage(url) { + // Read and parse `url` if it exists, otherwise `null` +} + +for await (const resolution of resolve('./addon', new URL('file:///directory/'), readPackage)) { + console.log(resolution) +} +``` + +## API + +#### `const resolver = resolve(specifier, parentURL[, options][, readPackage])` + +Resolve `specifier` relative to `parentURL`, which must be a WHATWG `URL` instance. `readPackage` is called with a `URL` instance for every package manifest to be read and must either return the parsed JSON package manifest, if it exists, or `null`. If `readPackage` returns a promise, synchronous iteration is not supported. + +Options include: + +```js +options = { + // A list of builtin addon specifiers. If matched, the protocol of the + // resolved URL will be `builtinProtocol`. + builtins: [], + // The protocol to use for resolved builtin addon specifiers. + builtinProtocol: 'builtin:', + // Whether or not addons linked ahead-of-time should be resolved. + linked: true, + // The protocol to use for addons linked ahead-of-time. + linkedProtocol: 'linked:', + // The supported import conditions. "default" is always recognized. + conditions: [], + // An array reference which will contain the matched conditions when yielding + // resolutions. + matchedConditions: [], + // The `-` combinations to look for when resolving dynamic + // addons. If empty, only builtin specifiers can be resolved. In Bare, + // pass `[Bare.Addon.host]`. + hosts: [], + // The file extensions to look for when resolving dynamic addons. + extensions: [], + // A map of preresolved imports with keys being serialized directory URLs and + // values being "imports" maps. + resolutions +} +``` + +#### `for (const resolution of resolver)` + +Synchronously iterate the addon resolution candidates. The resolved addon is the first candidate that exists as a file on the file system. + +#### `for await (const resolution of resolver)` + +Asynchronously iterate the addon resolution candidates. If `readPackage` returns promises, these will be awaited. The same comments as `for (const resolution of resolver)` apply. + +### Algorithm + +The following generator functions implement the resolution algorithm. The yielded values have the following shape: + +**Package manifest** + +```js +next.value = { + package: URL +} +``` + +If the package manifest identified by `next.value.package` exists, `generator.next()` must be passed the parsed JSON value of the manifest. If it does not exist, pass `null` instead. + +**Resolution candidate** + +```js +next.value = { + resolution: URL +} +``` + +If the addon identified by `next.value.resolution` exists, `generator.next()` may be passed `true` to signal that the resolution for the current set of conditions has been identified. If it does not exist, pass `false` instead. + +To drive the generator functions, a loop like the following can be used: + +```js +const generator = resolve.addon(specifier, parentURL) + +let next = generator.next() + +while (next.done !== true) { + const value = next.value + + if (value.package) { + // Read and parse `value.package` if it exists, otherwise `null` + let info + + next = generator.next(info) + } else { + const resolution = value.resolution + + // `true` if `resolution` was the correct candidate, otherwise `false` + let resolved + + next = generator.next(resolved) + } +} +``` + +Options are the same as `resolve()` for all functions. + +> [!WARNING] +> These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency. + +#### `const generator = resolve.addon(specifier, parentURL[, options])` + +#### `const generator = resolve.url(url, parentURL[, options])` + +#### `const generator = resolve.package(packageSpecifier, packageVersion, parentURL[, options])` + +#### `const generator = resolve.packageSelf(packageName, packageSubpath, packageVersion, parentURL[, options])` + +#### `const generator = resolve.preresolved(directoryURL, resolutions[, options])` + +#### `const generator = resolve.file(filename, parentURL[, options])` + +#### `const generator = resolve.directory(dirname, version, parentURL[, options])` + +#### `const generator = resolve.linked(name, version[, options])` + +## License + +Apache-2.0 diff --git a/bot/node_modules/bare-addon-resolve/index.d.ts b/bot/node_modules/bare-addon-resolve/index.d.ts new file mode 100644 index 0000000..6b490cd --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/index.d.ts @@ -0,0 +1,85 @@ +import URL from 'bare-url' +import { constants, type Builtins, type Conditions, type ResolutionsMap } from 'bare-module-resolve' + +type JSON = string | number | boolean | JSON[] | { [key: string]: JSON } + +interface ResolveOptions { + builtinProtocol?: string + builtins?: Builtins + conditions?: Conditions + extensions?: string[] + host?: string + hosts?: string[] + linked?: boolean + linkedProtocol?: string + matchedConditions?: string[] + resolutions?: ResolutionsMap +} + +declare function resolve( + specifier: string, + parentURL: URL, + readPackage?: (url: URL) => JSON | null +): Iterable + +declare function resolve( + specifier: string, + parentURL: URL, + readPackage: (url: URL) => Promise +): AsyncIterable + +declare function resolve( + specifier: string, + parentURL: URL, + opts: ResolveOptions, + readPackage?: (url: URL) => JSON | null +): Iterable + +declare function resolve( + specifier: string, + parentURL: URL, + opts: ResolveOptions, + readPackage: (url: URL) => Promise +): AsyncIterable + +declare namespace resolve { + export { constants, type ResolveOptions } + + export type Resolver = Generator< + { resolution: URL } | { package: URL }, + number, + void | boolean | JSON | null + > + + export function addon(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function url(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function package( + packageSpecifier: string, + packageVersion: string, + parentURL: URL, + opts?: ResolveOptions + ): Resolver + + export function packageSelf( + packageName: string, + packageSubpath: string, + packageVersion: string, + parentURL: URL, + opts?: ResolveOptions + ): Resolver + + export function file(filename: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function directory( + dirname: string, + version: string, + parentURL: URL, + opts?: ResolveOptions + ): Resolver + + export function linked(name: string, version?: string, opts?: ResolveOptions): Resolver +} + +export = resolve diff --git a/bot/node_modules/bare-addon-resolve/index.js b/bot/node_modules/bare-addon-resolve/index.js new file mode 100644 index 0000000..c501b4d --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/index.js @@ -0,0 +1,471 @@ +const resolve = require('bare-module-resolve') +const { Version } = require('bare-semver') +const errors = require('./lib/errors') + +module.exports = exports = function resolve(specifier, parentURL, opts, readPackage) { + if (typeof opts === 'function') { + readPackage = opts + opts = {} + } else if (typeof readPackage !== 'function') { + readPackage = defaultReadPackage + } + + return { + *[Symbol.iterator]() { + const generator = exports.addon(specifier, parentURL, opts) + + let next = generator.next() + + while (next.done !== true) { + const value = next.value + + if (value.package) { + next = generator.next(readPackage(value.package)) + } else { + next = generator.next(yield value.resolution) + } + } + + return next.value + }, + + async *[Symbol.asyncIterator]() { + const generator = exports.addon(specifier, parentURL, opts) + + let next = generator.next() + + while (next.done !== true) { + const value = next.value + + if (value.package) { + next = generator.next(await readPackage(value.package)) + } else { + next = generator.next(yield value.resolution) + } + } + + return next.value + } + } +} + +function defaultReadPackage() { + return null +} + +const { UNRESOLVED, YIELDED, RESOLVED } = resolve.constants + +exports.constants = { + UNRESOLVED, + YIELDED, + RESOLVED +} + +exports.addon = function* (specifier, parentURL, opts = {}) { + const { resolutions = null } = opts + + if (exports.startsWithWindowsDriveLetter(specifier)) { + specifier = '/' + specifier + } + + let status + + if (resolutions) { + status = yield* resolve.preresolved(specifier, resolutions, parentURL, opts) + + if (status) return status + } + + status = yield* exports.url(specifier, parentURL, opts) + + if (status) return status + + let version = null + + const i = specifier.lastIndexOf('@') + + if (i > 0) { + version = specifier.substring(i + 1) + + try { + Version.parse(version) + + specifier = specifier.substring(0, i) + } catch { + version = null + } + } + + if ( + specifier === '.' || + specifier === '..' || + specifier[0] === '/' || + specifier[0] === '\\' || + specifier.startsWith('./') || + specifier.startsWith('.\\') || + specifier.startsWith('../') || + specifier.startsWith('..\\') + ) { + status = yield* exports.file(specifier, parentURL, opts) + + if (status === RESOLVED) return status + + return yield* exports.directory(specifier, version, parentURL, opts) + } + + return yield* exports.package(specifier, version, parentURL, opts) +} + +exports.url = function* (url, parentURL, opts = {}) { + let resolution + try { + resolution = new URL(url) + } catch { + return UNRESOLVED + } + + const resolved = yield { resolution } + + return resolved ? RESOLVED : YIELDED +} + +exports.package = function* (packageSpecifier, packageVersion, parentURL, opts = {}) { + if (packageSpecifier === '') { + throw errors.INVALID_ADDON_SPECIFIER( + `Addon specifier '${packageSpecifier}' is not a valid package name` + ) + } + + let packageName + + if (packageSpecifier[0] !== '@') { + packageName = packageSpecifier.split('/', 1).join() + } else { + if (!packageSpecifier.includes('/')) { + throw errors.INVALID_ADDON_SPECIFIER( + `Addon specifier '${packageSpecifier}' is not a valid package name` + ) + } + + packageName = packageSpecifier.split('/', 2).join('/') + } + + if (packageName[0] === '.' || packageName.includes('\\') || packageName.includes('%')) { + throw errors.INVALID_ADDON_SPECIFIER( + `Addon specifier '${packageSpecifier}' is not a valid package name` + ) + } + + const packageSubpath = '.' + packageSpecifier.substring(packageName.length) + + const status = yield* exports.packageSelf( + packageName, + packageSubpath, + packageVersion, + parentURL, + opts + ) + + if (status) return status + + parentURL = new URL(parentURL.href) + + do { + const packageURL = new URL('node_modules/' + packageName + '/', parentURL) + + parentURL.pathname = parentURL.pathname.substring(0, parentURL.pathname.lastIndexOf('/')) + + const info = yield { package: new URL('package.json', packageURL) } + + if (info) { + return yield* exports.directory(packageSubpath, packageVersion, packageURL, opts) + } + } while (parentURL.pathname !== '' && parentURL.pathname !== '/') + + return UNRESOLVED +} + +exports.packageSelf = function* ( + packageName, + packageSubpath, + packageVersion, + parentURL, + opts = {} +) { + for (const packageURL of resolve.lookupPackageScope(parentURL, opts)) { + const info = yield { package: packageURL } + + if (info) { + if (info.name === packageName) { + return yield* exports.directory(packageSubpath, packageVersion, packageURL, opts) + } + + break + } + } + + return UNRESOLVED +} + +exports.lookupPrebuildsScope = function* lookupPrebuildsScope(url, opts = {}) { + const scopeURL = new URL(url.href) + + do { + yield new URL('prebuilds/', scopeURL) + + scopeURL.pathname = scopeURL.pathname.substring(0, scopeURL.pathname.lastIndexOf('/')) + + if ( + scopeURL.pathname.length === 3 && + exports.isWindowsDriveLetter(scopeURL.pathname.substring(1)) + ) { + break + } + } while (scopeURL.pathname !== '' && scopeURL.pathname !== '/') +} + +exports.file = function* (filename, parentURL, opts = {}) { + if ( + filename === '.' || + filename === '..' || + filename[filename.length - 1] === '/' || + filename[filename.length - 1] === '\\' + ) { + return UNRESOLVED + } + + if (parentURL.protocol === 'file:' && /%2f|%5c/i.test(filename)) { + throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${filename}' is invalid`) + } + + const { extensions = [] } = opts + + let status = UNRESOLVED + + for (let ext of extensions) { + if (filename.endsWith(ext)) ext = '' + + if (yield { resolution: new URL(filename + ext, parentURL) }) { + return RESOLVED + } + + status = YIELDED + } + + return status +} + +exports.directory = function* (dirname, version, parentURL, opts = {}) { + const { + host = null, // Shorthand for single host resolution + hosts = host !== null ? [host] : [], + builtins = [], + matchedConditions = [] + } = opts + + let directoryURL + + if (dirname[dirname.length - 1] === '/' || dirname[dirname.length - 1] === '\\') { + directoryURL = new URL(dirname, parentURL) + } else { + directoryURL = new URL(dirname + '/', parentURL) + } + + const unversioned = version === null + + let name = null + + const info = yield { package: new URL('package.json', directoryURL) } + + if (info) { + if (typeof info.name === 'string' && info.name !== '') { + if (info.name.includes('__')) { + throw errors.INVALID_PACKAGE_NAME(`Package name '${info.name}' is invalid`) + } + + name = info.name.replace(/\//g, '__').replace(/^@/, '') + } else { + return UNRESOLVED + } + + if (typeof info.version === 'string' && info.version !== '') { + if (version !== null && info.version !== version) return UNRESOLVED + + version = info.version + } + } else { + return UNRESOLVED + } + + let status + + status = yield* resolve.builtinTarget(name, version, builtins, opts) + + if (status) return status + + for (const prebuildsURL of exports.lookupPrebuildsScope(directoryURL, opts)) { + status = UNRESOLVED + + for (const host of hosts) { + const conditions = host.split('-') + + const universal = supportsUniversalPrebuilds(host) + ? conditions.with(1, 'universal').join('-') + : null + + matchedConditions.push(...conditions) + + if (version !== null) { + status |= yield* exports.file(host + '/' + name + '@' + version, prebuildsURL, opts) + + if (universal) { + status |= yield* exports.file(universal + '/' + name + '@' + version, prebuildsURL, opts) + } + } + + if (unversioned) { + status |= yield* exports.file(host + '/' + name, prebuildsURL, opts) + + if (universal) { + status |= yield* exports.file(universal + '/' + name, prebuildsURL, opts) + } + } + + for (const _ of conditions) matchedConditions.pop() + } + + if (status === RESOLVED) return status + } + + return yield* exports.linked(name, version, opts) +} + +exports.linked = function* (name, version = null, opts = {}) { + const { + linked = true, + host = null, // Shorthand for single host resolution + hosts = host !== null ? [host] : [], + matchedConditions = [] + } = opts + + if (linked === false || hosts.length === 0) return UNRESOLVED + + let status = UNRESOLVED + + for (const host of hosts) { + const [platform = null] = host.split('-', 1) + + if (platform === null) continue + + matchedConditions.push(platform) + + status |= yield* platformArtefact(name, version, platform, opts) + + matchedConditions.pop() + } + + return status +} + +function* platformArtefact(name, version = null, platform, opts = {}) { + const { linkedProtocol = 'linked:' } = opts + + if (platform === 'darwin' || platform === 'ios') { + if (version !== null) { + if ( + yield { + resolution: new URL(`${linkedProtocol}${name}.${version}.framework/${name}.${version}`) + } + ) { + return RESOLVED + } + + if (platform === 'darwin') { + if ( + yield { + resolution: new URL(`${linkedProtocol}lib${name}.${version}.dylib`) + } + ) { + return RESOLVED + } + } + } + + if ( + yield { + resolution: new URL(`${linkedProtocol}${name}.framework/${name}`) + } + ) { + return RESOLVED + } + + if (platform === 'darwin') { + if ( + yield { + resolution: new URL(`${linkedProtocol}lib${name}.dylib`) + } + ) { + return RESOLVED + } + } + + return YIELDED + } + + if (platform === 'linux' || platform === 'android') { + if (version !== null) { + if ( + yield { + resolution: new URL(`${linkedProtocol}lib${name}.${version}.so`) + } + ) { + return RESOLVED + } + } + + if ( + yield { + resolution: new URL(`${linkedProtocol}lib${name}.so`) + } + ) { + return RESOLVED + } + + return YIELDED + } + + if (platform === 'win32') { + if (version !== null) { + if ( + yield { + resolution: new URL(`${linkedProtocol}${name}-${version}.dll`) + } + ) { + return RESOLVED + } + } + + if ( + yield { + resolution: new URL(`${linkedProtocol}${name}.dll`) + } + ) { + return RESOLVED + } + } + + return UNRESOLVED +} + +exports.isWindowsDriveLetter = resolve.isWindowsDriveLetter + +exports.startsWithWindowsDriveLetter = resolve.startsWithWindowsDriveLetter + +function supportsUniversalPrebuilds(host) { + return ( + host === 'darwin-arm64' || + host === 'darwin-x64' || + host === 'ios-arm64-simulator' || + host === 'ios-x64-simulator' + ) +} diff --git a/bot/node_modules/bare-addon-resolve/lib/errors.d.ts b/bot/node_modules/bare-addon-resolve/lib/errors.d.ts new file mode 100644 index 0000000..ecf2223 --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/lib/errors.d.ts @@ -0,0 +1,8 @@ +declare class AddonResolveError extends Error { + readonly code: string + + static INVALID_ADDON_SPECIFIER(msg: string): AddonResolveError + static INVALID_PACKAGE_NAME(msg: string): AddonResolveError +} + +export = AddonResolveError diff --git a/bot/node_modules/bare-addon-resolve/lib/errors.js b/bot/node_modules/bare-addon-resolve/lib/errors.js new file mode 100644 index 0000000..96b6637 --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/lib/errors.js @@ -0,0 +1,30 @@ +module.exports = class AddonResolveError extends Error { + constructor(msg, code, fn = AddonResolveError) { + super(`${code}: ${msg}`) + this.code = code + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, fn) + } + } + + get name() { + return 'AddonResolveError' + } + + static INVALID_ADDON_SPECIFIER(msg) { + return new AddonResolveError( + msg, + 'INVALID_ADDON_SPECIFIER', + AddonResolveError.INVALID_ADDON_SPECIFIER + ) + } + + static INVALID_PACKAGE_NAME(msg) { + return new AddonResolveError( + msg, + 'INVALID_PACKAGE_NAME', + AddonResolveError.INVALID_PACKAGE_NAME + ) + } +} diff --git a/bot/node_modules/bare-addon-resolve/package.json b/bot/node_modules/bare-addon-resolve/package.json new file mode 100644 index 0000000..ce86b15 --- /dev/null +++ b/bot/node_modules/bare-addon-resolve/package.json @@ -0,0 +1,52 @@ +{ + "name": "bare-addon-resolve", + "version": "1.10.0", + "description": "Low-level addon resolution algorithm for Bare", + "exports": { + "./package": "./package.json", + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "./errors": { + "types": "./lib/errors.d.ts", + "default": "./lib/errors.js" + } + }, + "files": [ + "index.js", + "index.d.ts", + "lib" + ], + "scripts": { + "test": "prettier . --check && bare test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/holepunchto/bare-addon-resolve.git" + }, + "author": "Holepunch", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/holepunchto/bare-addon-resolve/issues" + }, + "homepage": "https://github.com/holepunchto/bare-addon-resolve#readme", + "dependencies": { + "bare-module-resolve": "^1.10.0", + "bare-semver": "^1.0.0" + }, + "devDependencies": { + "bare-url": "^2.1.3", + "brittle": "^3.2.1", + "prettier": "^3.3.3", + "prettier-config-holepunch": "^2.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } +} diff --git a/bot/node_modules/bare-module-resolve/LICENSE b/bot/node_modules/bare-module-resolve/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/bot/node_modules/bare-module-resolve/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bot/node_modules/bare-module-resolve/README.md b/bot/node_modules/bare-module-resolve/README.md new file mode 100644 index 0000000..e6f8f45 --- /dev/null +++ b/bot/node_modules/bare-module-resolve/README.md @@ -0,0 +1,352 @@ +# bare-module-resolve + +Low-level module resolution algorithm for Bare. The algorithm is implemented as a generator function that yields either package manifests to be read or resolution candidates to be tested by the caller. As a convenience, the main export is a synchronous and asynchronous iterable that relies on package manifests being read by a callback. For asynchronous iteration, the callback may return promises which will be awaited before being passed to the generator. + +``` +npm i bare-module-resolve +``` + +## Usage + +For synchronous resolution: + +```js +const resolve = require('bare-module-resolve') + +function readPackage(url) { + // Read and parse `url` if it exists, otherwise `null` +} + +for (const resolution of resolve('./file.js', new URL('file:///directory/'), readPackage)) { + console.log(resolution) +} +``` + +For asynchronous resolution: + +```js +const resolve = require('bare-module-resolve') + +async function readPackage(url) { + // Read and parse `url` if it exists, otherwise `null` +} + +for await (const resolution of resolve('./file.js', new URL('file:///directory/'), readPackage)) { + console.log(resolution) +} +``` + +## API + +#### `const resolver = resolve(specifier, parentURL[, options][, readPackage])` + +Resolve `specifier` relative to `parentURL`, which must be a WHATWG `URL` instance. `readPackage` is called with a `URL` instance for every package manifest to be read and must either return the parsed JSON package manifest, if it exists, or `null`. If `readPackage` returns a promise, synchronous iteration is not supported. + +Options include: + +```js +options = { + // A default "imports" map to apply to all specifiers. Follows the same + // syntax and rules as the "imports" property defined in `package.json`. + imports, + // A list of builtin module specifiers. If matched, the protocol of the + // resolved URL will be `builtinProtocol`. + builtins: [], + // The protocol to use for resolved builtin module specifiers. + builtinProtocol: 'builtin:', + // A list of module specifiers whose resolution should be deferred. If matched, + // the protocol of the resolved URL will be `deferredProtocol`. + defer: [], + // The protocol to use for resolved deferred module specifiers. + deferredProtocol: 'deferred:', + // The supported import conditions. "default" is always recognized. + conditions: [], + // An array reference which will contain the matched conditions when yielding + // resolutions. + matchedConditions: [], + // The supported engine versions. + engines: {}, + // The file extensions to look for. Must be provided to support extensionless + // specifier resolution and directory support, such as resolving './foo' to + // './foo.js' or './foo/index.js'. + extensions: [], + // A map of preresolved imports with keys being serialized parent URLs and + // values being "imports" maps. + resolutions +} +``` + +#### `for (const resolution of resolver)` + +Synchronously iterate the module resolution candidates. The resolved module is the first candidate that exists, either as a file on a file system, a resource at a URL, or something else entirely. + +#### `for await (const resolution of resolver)` + +Asynchronously iterate the module resolution candidates. If `readPackage` returns promises, these will be awaited. The same comments as `for (const resolution of resolver)` apply. + +### Algorithm + +The following generator functions implement the resolution algorithm, which has been adapted from the Node.js resolution algorithms for CommonJS and ES modules. Unlike Node.js, Bare uses the same resolution algorithm for both module formats. The yielded values have the following shape: + +**Package manifest** + +```js +next.value = { + package: URL +} +``` + +If the package manifest identified by `next.value.package` exists, `generator.next()` must be passed the parsed JSON value of the manifest. If it does not exist, pass `null` instead. + +**Resolution candidate** + +```js +next.value = { + resolution: URL +} +``` + +If the module identified by `next.value.resolution` exists, `generator.next()` may be passed `true` to signal that the resolution for the current set of conditions has been identified. If it does not exist, pass `false` instead. + +To drive the generator functions, a loop like the following can be used: + +```js +const generator = resolve.module(specifier, parentURL) + +let next = generator.next() + +while (next.done !== true) { + const value = next.value + + if (value.package) { + // Read and parse `value.package` if it exists, otherwise `null` + let info + + next = generator.next(info) + } else { + const resolution = value.resolution + + // `true` if `resolution` was the correct candidate, otherwise `false` + let resolved + + next = generator.next(resolved) + } +} +``` + +Options are the same as `resolve()` for all functions. + +> [!WARNING] +> These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency. + +#### `const generator = resolve.module(specifier, parentURL[, options])` + +1. If `specifier` [starts with a Windows drive letter](https://url.spec.whatwg.org/#start-with-a-windows-drive-letter): + 1. Prepend `/` to `specifier`. +2. If `options.resolutions` is set: + 1. If `preresolved(specifier, options.resolutions, parentURL, options)` yields, return. +3. If `url(specifier, parentURL, options)` yields, return. +4. If `packageImports(specifier, parentURL, options)` yields, return. +5. If `specifier` equals `.` or `..`, or if `specifier` starts with `/`, `\`, `./`, `.\`, `../`, or `..\`: + 1. If `options.imports` is set: + 1. If `packageImportsExports(specifier, options.imports, parentURL, true, options)` yields, return. + 2. If `deferred(specifier, options)` yields, return. + 3. If `file(specifier, parentURL, false, options)` resolves, return. + 4. Return `directory(specifier, parentURL, options)`. +6. Return `package(specifier, parentURL, options)`. + +#### `const generator = resolve.url(url, parentURL[, options])` + +1. If `url` is not a valid URL, return. +2. If `options.imports` is set: + 1. If `packageImportsExports(url.href, options.imports, parentURL, true, options)` yields, return. +3. If `url.protocol` equals `options.deferredProtocol`: + 1. Let `specifier` be `url.pathname`. + 2. If `options.resolutions` is set: + 1. Let `imports` be `options.resolutions[parentURL]`. + 2. If `imports` is a non-`null` object: + 1. Set `imports[specifier]` to `null`. + 3. Return `module(specifier, parentURL, options)`. +4. If `url.protocol` equals `node:`: + 1. Let `specifier` be `url.pathname`. + 2. If `specifier` equals `.` or `..`, or if `specifier` starts with `/`, `\`, `./`, `.\`, `../`, or `..\`, throw. + 3. Return `package(specifier, parentURL, options)`. +5. Yield `url`. + +#### `const generator = resolve.preresolved(specifier, resolutions, parentURL[, options])` + +1. Let `imports` be `resolutions[parentURL]`. +2. If `imports` is a non-`null` object: + 1. Return `packageImportsExports(specifier, imports, parentURL, true, options)`. + +#### `const generator = resolve.deferred(specifier[, options])` + +1. If `options.defer` includes `specifier`: + 1. Yield `options.deferredProtocol` concatenated with `specifier` and return. + +#### `const generator = resolve.package(packageSpecifier, parentURL[, options])` + +1. If `packageSpecifier` is the empty string, throw. +2. If `packageSpecifier` does not start with `@`: + 1. Set `packageName` to the substring of `packageSpecifier` until the first `/` or the end of the string. +3. Let `packageName` be `undefined`. +4. Otherwise: + 1. If `packageSpecifier` does not include `/`, throw. + 2. Set `packageName` to the substring of `packageSpecifier` until the second `/` or the end of the string. +5. If `packageName` starts with `.` or includes `\` or `%`, throw. +6. If `builtinTarget(packageSpecifier, null, options.builtins, options)` yields, return. +7. If `deferred(packageSpecifier, options)` yields, return. +8. Let `packageSubpath` be `.` concatenated with the substring of `packageSpecifier` from the position at the length of `packageName`. +9. If `packageSelf(packageName, packageSubpath, parentURL, options)` yields, return. +10. For each value `packageURL` of `lookupPackageRoot(packageName, parentURL, options)`: + 1. Let `info` be the result of yielding `packageURL`. + 2. If `info` is not `null`: + 1. If `info.engines` is set: + 1. Call `validateEngines(packageURL, info.engines, options)`. + 2. If `info.exports` is set: + 1. Return `packageExports(packageURL, packageSubpath, info.exports, options)`. + 3. If `packageSubpath` is `.`: + 1. If `info.main` is a non-empty string: + 1. Set `packageSubpath` to `info.main`. + 2. Otherwise: + 1. Return `file('index', packageURL, true, options)`. + 4. If `file(packageSubpath, packageURL, false, options)` resolves, return. + 5. Return `directory(packageSubpath, packageURL, options)`. + +#### `const generator = resolve.packageSelf(packageName, packageSubpath, parentURL[, options])` + +1. For each value `packageURL` of `lookupPackageScope(parentURL, options)`: + 1. Let `info` be the result of yielding `packageURL`. + 2. If `info` is not `null`: + 1. If `info.name` does not equal `packageName`, return. + 2. If `info.exports` is set: + 1. Return `packageExports(packageURL, packageSubpath, info.exports, options)`. + 3. If `packageSubpath` is `.`: + 1. If `info.main` is a non-empty string: + 1. Set `packageSubpath` to `info.main`. + 2. Otherwise: + 1. Return `file('index', packageURL, true, options)`. + 4. If `file(packageSubpath, packageURL, false, options)` resolves, return. + 5. Return `directory(packageSubpath, packageURL, options)`. + +#### `const generator = resolve.packageExports(packageURL, subpath, exports[, options])` + +1. If `subpath` is `.`: + 1. Let `mainExport` be `undefined`. + 2. If `exports` is a string or an array: + 1. Set `mainExport` to `exports`. + 3. If `exports` is a non-`null` object: + 1. If some keys of `exports` start with `.`: + 1. If `.` is a key of `exports`: + 1. Set `mainExport` to `exports['.']`. + 2. Otherwise: + 1. Set `mainExport` to `exports`. + 4. If `mainExport` is not `undefined`: + 1. If `packageTarget(packageURL, mainExport, null, false, options)` yields, return. +2. Otherwise, if `exports` is a non-`null` object: + 1. If every key of `exports` starts with `.`: + 1. If `packageImportsExports(subpath, exports, packageURL, false, options)` yields, return. +3. Throw. + +#### `const generator = resolve.packageImports(specifier, parentURL[, options])` + +1. If `specifier` is `#` or starts with `#/`, throw. +2. For each value `packageURL` of `lookupPackageScope(parentURL, options)`: + 1. Let `info` be the result of yielding `packageURL`. + 2. If `info` is not `null`: + 1. If `info.imports` is set: + 1. If `packageImportsExports(specifier, info.imports, packageURL, true, options)` yields, return. + 2. If specifier starts with `#`, throw. + 3. Return. +3. If `options.imports` is set: + 1. If `packageImportsExports(url.href, options.imports, parentURL, true, options)` yields, return. + +#### `const generator = resolve.packageImportsExports(matchKey, matchObject, packageURL, isImports[, options])` + +1. If `matchKey` is a key of `matchObject` and `matchKey` does not include `*`: + 1. Let `target` be `matchObject[matchKey]`. + 2. Return `packageTarget(packageURL, target, null, isImports, options)`. +2. Let `expansionKeys` be the keys of `matchObject` that include `*` sorted by `patternKeyCompare`. +3. For each value `expansionKey` of `expansionKeys`: + 1. Let `patternBase` be the substring of `expansionKey` until the first `*`. + 2. If `matchKey` starts with but isn't equal to `patternBase`: + 1. Let `patternTrailer` be the substring of `expansionKey` from the position at the index after the first `*`. + 2. If `patternTrailer` is the empty string, or if `matchKey` ends with `patternTrailer` and the length of `matchKey` is greater than or equal to the length of `expansionKey`: + 1. Let `target` be `matchObject[expansionKey]`. + 2. Let `patternMatch` be the substring of `matchKey` from the position at the length of `patternBase` until the length of `matchKey` minus the length of `patternTrailer`. + 3. Return `packageTarget(packageURL, target, patternMatch, isImports, options)`. + +#### `const generator = resolve.packageTarget(packageURL, target, patternMatch, isImports[, options])` + +1. If `target` is a string: + 1. If `target` does not start with `./` and `isImports` is `false`, throw. + 2. If `patternMatch` is not `null`: + 1. Replace every instance of `*` in `target` with `patternMatch`. + 3. If `url(target, packageURL, options)` yields, return. + 4. If `target` equals `.` or `..`, or if `target` starts with `/`, `./`, or `../`: + 1. Yield the resolution of `target` relative to `packageURL` and return. + 5. Return `package(target, packageURL, options)`. +2. If `target` is an array: + 1. For each value `targetValue` of `target`: + 1. If `packageTarget(packageURL, targetValue, patternMatch, isImports, options)` yields, return. +3. If `target` is a non-`null` object: + 1. For each key `condition` of `target`: + 1. If `condition` equals `default` or if `options.conditions` includes `condition`: + 1. Let `targetValue` be `target[condition]`. + 2. Return `packageTarget(packageURL, targetValue, patternMatch, isImports, options)`. + +#### `const generator = resolve.builtinTarget(packageSpecifier, packageVersion, target[, options])` + +1. If `target` is a string: + 1. If `target` does not start with `@`: + 1. Let `targetName` be the substring of `target` until the first `@` or the end of the string. + 2. Let `targetVersion` be the substring of `target` from the character following the first `@` and to the end of string, or `null` if no such substring exists. + 2. Otherwise: + 1. Let `targetName` be the substring of `target` until the second `@` or the end of the string. + 2. Let `targetVersion` be the substring of `target` from the character following the second `@` and to the end of string, or `null` if no such substring exists. + 3. If `packageSpecifier` equals `targetName`: + 1. If `packageVersion` is `null` and `targetVersion` is `null`: + 1. Yield `options.builtinProtocol` concatenated with `packageSpecifier` and return. + 2. Let `version` be `null`. + 3. If `packageVersion` is `null`, let `version` be `targetVersion`. + 4. Otherwise, if `targetVersion` is either `null` or equals `packageVersion`, let `version` be `packageVersion` + 5. If `version` is not `null`: + 1. Yield `options.builtinProtocol` concatenated with `packageSpecifier`, `@`, and `version` and return. +2. If `target` is an array: + 1. For each value `targetValue` of `target`: + 1. If `builtinTarget(packageSpecifier, packageVersion, targetValue, options)` yields, return. +3. If `target` is a non-`null` object: + 1. For each key `condition` of `target`: + 1. If `condition` equals `default` or if `options.conditions` includes `condition`: + 1. Let `targetValue` be `target[condition]`. + 2. Return `builtinTarget(packageSpecifier, packageVersion, targetValue, options)`. + +#### `const generator = resolve.file(filename, parentURL, isIndex[, options])` + +1. If `filename` equals `.` or `..`, or if `filename` ends with `/` or `\`, return. +2. If `parentURL` is a `file:` URL and `filename` includes encoded `/` or `\`, throw. +3. If `isIndex` is `false`: + 1. Yield the resolution of `filename` relative to `parentURL`. +4. For each value `ext` of `options.extensions`: + 1. If `filename` ends with `ext`, continue. + 2. Yield the resolution of `filename` concatenated with `ext` relative to `parentURL`. + +#### `const generator = resolve.directory(dirname, parentURL[, options])` + +1. Let `directoryURL` be `undefined`. +2. If `dirname` ends with `/` or `\`: + 1. Set `directoryURL` to the resolution of `dirname` relative to `parentURL`. +3. Otherwise: + 1. Set `directoryURL` to the resolution of `dirname` concatenated with `/` relative to `parentURL`. +4. Let `info` be the result of yielding the resolution of `package.json` relative to `directoryURL`. +5. If `info` is not `null`: + 1. If `info.exports` is set: + 1. Return `packageExports(directoryURL, '.', info.exports, options)`. + 2. If `info.main` is a non-empty string: + 1. If `file(info.main, directoryURL, false, options)` resolves, return. + 2. Return `directory(info.main, directoryURL, options)`. +6. Return `file('index', directoryURL, true, options)`. + +## License + +Apache-2.0 diff --git a/bot/node_modules/bare-module-resolve/index.d.ts b/bot/node_modules/bare-module-resolve/index.d.ts new file mode 100644 index 0000000..c3a6f90 --- /dev/null +++ b/bot/node_modules/bare-module-resolve/index.d.ts @@ -0,0 +1,147 @@ +import URL from 'bare-url' + +type ConditionalSpecifier = + | string + | ConditionalSpecifier[] + | { [condition: string]: ConditionalSpecifier } + +type ImportsMap = { [specifier: string]: ConditionalSpecifier } + +type ExportsMap = ImportsMap + +type ResolutionsMap = { [href: string]: ImportsMap } + +type Builtins = ConditionalSpecifier[] + +type Conditions = string[] | Conditions[] + +type Engines = { [name: string]: string } + +type JSON = string | number | boolean | JSON[] | { [key: string]: JSON } + +interface ResolveOptions { + builtinProtocol?: string + builtins?: Builtins + conditions?: Conditions + defer?: string[] + deferredProtocol?: string + engines?: Engines + extensions?: string[] + imports?: ImportsMap + matchedConditions?: string[] + resolutions?: ResolutionsMap +} + +declare function resolve( + specifier: string, + parentURL: URL, + readPackage?: (url: URL) => JSON | null +): Iterable + +declare function resolve( + specifier: string, + parentURL: URL, + readPackage: (url: URL) => Promise +): AsyncIterable + +declare function resolve( + specifier: string, + parentURL: URL, + opts: ResolveOptions, + readPackage?: (url: URL) => JSON | null +): Iterable + +declare function resolve( + specifier: string, + parentURL: URL, + opts: ResolveOptions, + readPackage: (url: URL) => Promise +): AsyncIterable + +declare namespace resolve { + export { + type ConditionalSpecifier, + type ImportsMap, + type ExportsMap, + type ResolutionsMap, + type Builtins, + type Conditions, + type Engines, + type ResolveOptions + } + + export const constants: { + UNRESOLVED: number + YIELDED: number + RESOLVED: number + } + + export type Resolver = Generator< + { package: URL } | { resolution: URL }, + number, + void | boolean | JSON | null + > + + export function module(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function url(url: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function preresolved( + specifier: string, + resolutions: ResolutionsMap, + parentURL: URL, + opts?: ResolveOptions + ): Resolver + + export function package(packageSpecifier: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function packageSelf( + packageName: string, + packageSubpath: string, + parentURL: URL, + opts?: ResolveOptions + ): Resolver + + export function packageExports( + packageURL: URL, + subpath: string, + packageExports: ExportsMap, + opts?: ResolveOptions + ): Resolver + + export function packageImports(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver + + export function packageImportsExports( + matchKey: string, + matchObject: ImportsMap | ExportsMap, + packageURL: URL, + isImports: boolean, + opts?: ResolveOptions + ): Resolver + + export function packageTarget( + packageURL: URL, + target: ConditionalSpecifier, + patternMatch: string, + isImports: boolean, + opts?: ResolveOptions + ): Resolver + + export function builtinTarget( + packageSpecifier: string, + packageVersion: string, + target: ConditionalSpecifier, + opts?: ResolveOptions + ): Resolver + + export function file( + filename: string, + parentURL: URL, + isIndex: boolean, + opts?: ResolveOptions + ): Resolver + + export function directory(dirname: string, parentURL: URL, opts?: ResolveOptions): Resolver +} + +export = resolve diff --git a/bot/node_modules/bare-module-resolve/index.js b/bot/node_modules/bare-module-resolve/index.js new file mode 100644 index 0000000..abcc28f --- /dev/null +++ b/bot/node_modules/bare-module-resolve/index.js @@ -0,0 +1,796 @@ +const { satisfies } = require('bare-semver') +const errors = require('./lib/errors') + +module.exports = exports = function resolve(specifier, parentURL, opts, readPackage) { + if (typeof opts === 'function') { + readPackage = opts + opts = {} + } else if (typeof readPackage !== 'function') { + readPackage = defaultReadPackage + } + + return { + *[Symbol.iterator]() { + const generator = exports.module(specifier, parentURL, opts) + + let next = generator.next() + + while (next.done !== true) { + const value = next.value + + if (value.package) { + next = generator.next(readPackage(value.package)) + } else { + next = generator.next(yield value.resolution) + } + } + + return next.value + }, + + async *[Symbol.asyncIterator]() { + const generator = exports.module(specifier, parentURL, opts) + + let next = generator.next() + + while (next.done !== true) { + const value = next.value + + if (value.package) { + next = generator.next(await readPackage(value.package)) + } else { + next = generator.next(yield value.resolution) + } + } + + return next.value + } + } +} + +function defaultReadPackage() { + return null +} + +// No resolution candidate was yielded +const UNRESOLVED = 0x0 +// At least 1 resolution candidate was yielded +const YIELDED = 0x1 +// At least 1 resolution candidate was yielded and resolved +const RESOLVED = YIELDED | 0x2 + +exports.constants = { + UNRESOLVED, + YIELDED, + RESOLVED +} + +exports.module = function* (specifier, parentURL, opts = {}) { + const { resolutions = null, imports = null } = opts + + if (exports.startsWithWindowsDriveLetter(specifier)) { + specifier = '/' + specifier + } + + let status + + if (resolutions) { + status = yield* exports.preresolved(specifier, resolutions, parentURL, opts) + + if (status) return status + } + + status = yield* exports.url(specifier, parentURL, opts) + + if (status) return status + + status = yield* exports.packageImports(specifier, parentURL, opts) + + if (status) return status + + if ( + specifier === '.' || + specifier === '..' || + specifier[0] === '/' || + specifier[0] === '\\' || + specifier.startsWith('./') || + specifier.startsWith('.\\') || + specifier.startsWith('../') || + specifier.startsWith('..\\') + ) { + if (imports) { + status = yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts) + + if (status) return status + } + + status = yield* exports.deferred(specifier, opts) + + if (status) return status + + status = yield* exports.file(specifier, parentURL, false, opts) + + if (status === RESOLVED) return status + + return yield* exports.directory(specifier, parentURL, opts) + } + + return yield* exports.package(specifier, parentURL, opts) +} + +exports.url = function* (url, parentURL, opts = {}) { + const { imports = null, deferredProtocol = 'deferred:', resolutions = null } = opts + + let resolution + try { + resolution = new URL(url) + } catch { + return UNRESOLVED + } + + if (imports) { + const status = yield* exports.packageImportsExports( + resolution.href, + imports, + parentURL, + true, + opts + ) + + if (status) return status + } + + if (resolution.protocol === deferredProtocol) { + const specifier = resolution.pathname + + if (resolutions) { + const imports = resolutions[parentURL.href] + + if (typeof imports === 'object' && imports !== null) { + opts = { + ...opts, + resolutions: { ...resolutions, [parentURL.href]: { ...imports, [specifier]: null } } + } + } + } + + return yield* exports.module(specifier, parentURL, opts) + } + + if (resolution.protocol === 'node:') { + const specifier = resolution.pathname + + if ( + specifier === '.' || + specifier === '..' || + specifier[0] === '/' || + specifier.startsWith('./') || + specifier.startsWith('../') + ) { + throw errors.INVALID_MODULE_SPECIFIER(`Module specifier '${url}' is not a valid package name`) + } + + return yield* exports.package(specifier, parentURL, opts) + } + + const resolved = yield { resolution } + + return resolved ? RESOLVED : YIELDED +} + +exports.preresolved = function* (specifier, resolutions, parentURL, opts = {}) { + const imports = resolutions[parentURL.href] + + if (typeof imports === 'object' && imports !== null) { + return yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts) + } + + return UNRESOLVED +} + +exports.deferred = function* (specifier, opts = {}) { + const { deferredProtocol = 'deferred:', defer = [] } = opts + + if (defer.includes(specifier)) { + const resolved = yield { resolution: new URL(deferredProtocol + specifier) } + + return resolved ? RESOLVED : YIELDED + } + + return UNRESOLVED +} + +exports.package = function* (packageSpecifier, parentURL, opts = {}) { + const { builtins = [] } = opts + + if (packageSpecifier === '') { + throw errors.INVALID_MODULE_SPECIFIER( + `Module specifier '${packageSpecifier}' is not a valid package name` + ) + } + + let packageName + + if (packageSpecifier[0] !== '@') { + packageName = packageSpecifier.split('/', 1).join() + } else { + if (!packageSpecifier.includes('/')) { + throw errors.INVALID_MODULE_SPECIFIER( + `Module specifier '${packageSpecifier}' is not a valid package name` + ) + } + + packageName = packageSpecifier.split('/', 2).join('/') + } + + if (packageName[0] === '.' || packageName.includes('\\') || packageName.includes('%')) { + throw errors.INVALID_MODULE_SPECIFIER( + `Module specifier '${packageSpecifier}' is not a valid package name` + ) + } + + let status + + status = yield* exports.builtinTarget(packageSpecifier, null, builtins, opts) + + if (status) return status + + status = yield* exports.deferred(packageSpecifier, opts) + + if (status) return status + + let packageSubpath = '.' + packageSpecifier.substring(packageName.length) + + status = yield* exports.packageSelf(packageName, packageSubpath, parentURL, opts) + + if (status) return status + + parentURL = new URL(parentURL.href) + + for (const packageURL of exports.lookupPackageRoot(packageName, parentURL)) { + const info = yield { package: packageURL } + + if (info) { + if (info.engines) exports.validateEngines(packageURL, info.engines, opts) + + if (info.exports) { + return yield* exports.packageExports(packageURL, packageSubpath, info.exports, opts) + } + + if (packageSubpath === '.') { + if (typeof info.main === 'string' && info.main !== '') { + packageSubpath = info.main + } else { + return yield* exports.file('index', packageURL, true, opts) + } + } + + status = yield* exports.file(packageSubpath, packageURL, false, opts) + + if (status === RESOLVED) return status + + return yield* exports.directory(packageSubpath, packageURL, opts) + } + } + + return UNRESOLVED +} + +exports.packageSelf = function* (packageName, packageSubpath, parentURL, opts = {}) { + for (const packageURL of exports.lookupPackageScope(parentURL, opts)) { + const info = yield { package: packageURL } + + if (info) { + if (info.name !== packageName) return false + + if (info.exports) { + return yield* exports.packageExports(packageURL, packageSubpath, info.exports, opts) + } + + if (packageSubpath === '.') { + if (typeof info.main === 'string' && info.main !== '') { + packageSubpath = info.main + } else { + return yield* exports.file('index', packageURL, true, opts) + } + } + + const status = yield* exports.file(packageSubpath, packageURL, false, opts) + + if (status === RESOLVED) return status + + return yield* exports.directory(packageSubpath, packageURL, opts) + } + } + + return UNRESOLVED +} + +exports.packageExports = function* (packageURL, subpath, packageExports, opts = {}) { + if (subpath === '.') { + let mainExport + + if (typeof packageExports === 'string' || Array.isArray(packageExports)) { + mainExport = packageExports + } else if (typeof packageExports === 'object' && packageExports !== null) { + const keys = Object.keys(packageExports) + + if (keys.some((key) => key.startsWith('.'))) { + if ('.' in packageExports) mainExport = packageExports['.'] + } else { + mainExport = packageExports + } + } + + if (mainExport) { + const status = yield* exports.packageTarget(packageURL, mainExport, null, false, opts) + + if (status) return status + } + } else if (typeof packageExports === 'object' && packageExports !== null) { + const keys = Object.keys(packageExports) + + if (keys.every((key) => key.startsWith('.'))) { + const status = yield* exports.packageImportsExports( + subpath, + packageExports, + packageURL, + false, + opts + ) + + if (status) return status + } + } + + throw errors.PACKAGE_PATH_NOT_EXPORTED( + `Package subpath '${subpath}' is not defined by "exports" in '${packageURL}'` + ) +} + +exports.packageImports = function* (specifier, parentURL, opts = {}) { + const { imports = null } = opts + + if (specifier === '#' || specifier.startsWith('#/')) { + throw errors.INVALID_MODULE_SPECIFIER( + `Module specifier '${specifier}' is not a valid internal imports specifier` + ) + } + + for (const packageURL of exports.lookupPackageScope(parentURL, opts)) { + const info = yield { package: packageURL } + + if (info) { + if (info.imports) { + const status = yield* exports.packageImportsExports( + specifier, + info.imports, + packageURL, + true, + opts + ) + + if (status) return status + } + + if (specifier.startsWith('#')) { + throw errors.PACKAGE_IMPORT_NOT_DEFINED( + `Package import specifier '${specifier}' is not defined by "imports" in '${packageURL}'` + ) + } + + break + } + } + + if (imports) { + const status = yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts) + + if (status) return status + } + + return UNRESOLVED +} + +exports.packageImportsExports = function* ( + matchKey, + matchObject, + packageURL, + isImports, + opts = {} +) { + if (matchKey in matchObject && !matchKey.includes('*')) { + const target = matchObject[matchKey] + + return yield* exports.packageTarget(packageURL, target, null, isImports, opts) + } + + const expansionKeys = Object.keys(matchObject) + .filter((key) => key.includes('*')) + .sort(exports.patternKeyCompare) + + for (const expansionKey of expansionKeys) { + const patternIndex = expansionKey.indexOf('*') + const patternBase = expansionKey.substring(0, patternIndex) + + if (matchKey.startsWith(patternBase) && matchKey !== patternBase) { + const patternTrailer = expansionKey.substring(patternIndex + 1) + + if ( + patternTrailer === '' || + (matchKey.endsWith(patternTrailer) && matchKey.length >= expansionKey.length) + ) { + const target = matchObject[expansionKey] + + const patternMatch = matchKey.substring( + patternBase.length, + matchKey.length - patternTrailer.length + ) + + return yield* exports.packageTarget(packageURL, target, patternMatch, isImports, opts) + } + } + } + + return UNRESOLVED +} + +exports.validateEngines = function validateEngines(packageURL, packageEngines, opts = {}) { + const { engines = {} } = opts + + for (const [engine, range] of Object.entries(packageEngines)) { + if (engine in engines) { + const version = engines[engine] + + if (!satisfies(version, range)) { + throw errors.UNSUPPORTED_ENGINE( + `Package not compatible with engine '${engine}' ${version}, requires range '${range}' defined by "engines" in '${packageURL}'` + ) + } + } + } +} + +exports.patternKeyCompare = function patternKeyCompare(keyA, keyB) { + const patternIndexA = keyA.indexOf('*') + const patternIndexB = keyB.indexOf('*') + const baseLengthA = patternIndexA === -1 ? keyA.length : patternIndexA + 1 + const baseLengthB = patternIndexB === -1 ? keyB.length : patternIndexB + 1 + if (baseLengthA > baseLengthB) return -1 + if (baseLengthB > baseLengthA) return 1 + if (patternIndexA === -1) return 1 + if (patternIndexB === -1) return -1 + if (keyA.length > keyB.length) return -1 + if (keyB.length > keyA.length) return 1 + return 0 +} + +exports.packageTarget = function* (packageURL, target, patternMatch, isImports, opts = {}) { + const { conditions = [], matchedConditions = [] } = opts + + if (typeof target === 'string') { + if (!target.startsWith('./') && !isImports) { + throw errors.INVALID_PACKAGE_TARGET( + `Invalid target '${target}' defined by "exports" in '${packageURL}'` + ) + } + + if (patternMatch !== null) { + target = target.replaceAll('*', patternMatch) + } + + const status = yield* exports.url(target, packageURL, opts) + + if (status) return status + + if ( + target === '.' || + target === '..' || + target[0] === '/' || + target.startsWith('./') || + target.startsWith('../') + ) { + const resolved = yield { resolution: new URL(target, packageURL) } + + return resolved ? RESOLVED : YIELDED + } + + return yield* exports.package(target, packageURL, opts) + } + + if (Array.isArray(target)) { + for (const targetValue of target) { + const status = yield* exports.packageTarget( + packageURL, + targetValue, + patternMatch, + isImports, + opts + ) + + if (status) return status + } + } else if (typeof target === 'object' && target !== null) { + let status = UNRESOLVED + + for (const [condition, targetValue, subset] of exports.conditionMatches( + target, + conditions, + opts + )) { + matchedConditions.push(condition) + + status |= yield* exports.packageTarget(packageURL, targetValue, patternMatch, isImports, { + ...opts, + conditions: subset + }) + + matchedConditions.pop() + } + + if (status) return status + } + + return UNRESOLVED +} + +exports.builtinTarget = function* (packageSpecifier, packageVersion, target, opts = {}) { + const { builtinProtocol = 'builtin:', conditions = [], matchedConditions = [] } = opts + + if (typeof target === 'string') { + const targetParts = target.split('@') + + let targetName + let targetVersion + + if (target[0] !== '@') { + targetName = targetParts[0] + targetVersion = targetParts[1] || null + } else { + targetName = targetParts.slice(0, 2).join('@') + targetVersion = targetParts[2] || null + } + + if (packageSpecifier === targetName) { + if (packageVersion === null && targetVersion === null) { + const resolved = yield { + resolution: new URL(builtinProtocol + packageSpecifier) + } + + return resolved ? RESOLVED : YIELDED + } + + let version = null + + if (packageVersion === null) { + version = targetVersion + } else if (targetVersion === null || packageVersion === targetVersion) { + version = packageVersion + } + + if (version !== null) { + const resolved = yield { + resolution: new URL(builtinProtocol + packageSpecifier + '@' + version) + } + + return resolved ? RESOLVED : YIELDED + } + } + } else if (Array.isArray(target)) { + for (const targetValue of target) { + const status = yield* exports.builtinTarget( + packageSpecifier, + packageVersion, + targetValue, + opts + ) + + if (status) return status + } + } else if (typeof target === 'object' && target !== null) { + let status = UNRESOLVED + + for (const [condition, targetValue, subset] of exports.conditionMatches( + target, + conditions, + opts + )) { + matchedConditions.push(condition) + + status |= yield* exports.builtinTarget(packageSpecifier, packageVersion, targetValue, { + ...opts, + conditions: subset + }) + + matchedConditions.pop() + } + + if (status) return status + } + + return UNRESOLVED +} + +exports.conditionMatches = function* conditionMatches(target, conditions, opts = {}) { + if (conditions.every((condition) => typeof condition === 'string')) { + const keys = Object.keys(target) + + for (const condition of keys) { + if (condition === 'default' || conditions.includes(condition)) { + yield [condition, target[condition], conditions] + + return true + } + } + + return false + } + + let yielded = false + + for (const subset of conditions) { + if (yield* conditionMatches(target, subset, opts)) { + yielded = true + } + } + + return yielded +} + +exports.lookupPackageRoot = function* (packageName, parentURL) { + parentURL = new URL(parentURL.href) + + do { + const packageURL = new URL('node_modules/' + packageName + '/', parentURL) + + const info = yield new URL('package.json', packageURL) + + if (info) return info + + parentURL.pathname = parentURL.pathname.substring(0, parentURL.pathname.lastIndexOf('/')) + + if ( + parentURL.pathname.length === 3 && + exports.isWindowsDriveLetter(parentURL.pathname.substring(1)) + ) { + break + } + } while (parentURL.pathname !== '' && parentURL.pathname !== '/') + + return null +} + +exports.lookupPackageScope = function* lookupPackageScope(scopeURL, opts = {}) { + const { resolutions = null } = opts + + if (resolutions) { + for (const { resolution } of exports.preresolved('#package', resolutions, scopeURL, opts)) { + if (resolution) return yield resolution + } + } + + scopeURL = new URL(scopeURL.href) + + do { + if (scopeURL.pathname.endsWith('/node_modules')) break + + const info = yield new URL('package.json', scopeURL) + + if (info) return info + + scopeURL.pathname = scopeURL.pathname.substring(0, scopeURL.pathname.lastIndexOf('/')) + + if ( + scopeURL.pathname.length === 3 && + exports.isWindowsDriveLetter(scopeURL.pathname.substring(1)) + ) { + break + } + } while (scopeURL.pathname !== '' && scopeURL.pathname !== '/') + + return null +} + +exports.file = function* (filename, parentURL, isIndex, opts = {}) { + if ( + filename === '.' || + filename === '..' || + filename[filename.length - 1] === '/' || + filename[filename.length - 1] === '\\' + ) { + return UNRESOLVED + } + + if (parentURL.protocol === 'file:' && /%2f|%5c/i.test(filename)) { + throw errors.INVALID_MODULE_SPECIFIER(`Module specifier '${filename}' is invalid`) + } + + const { extensions = [] } = opts + + let status = UNRESOLVED + + if (!isIndex) { + if (yield { resolution: new URL(filename, parentURL) }) { + return RESOLVED + } + + status = YIELDED + } + + for (const ext of extensions) { + if (filename.endsWith(ext)) continue + + if (yield { resolution: new URL(filename + ext, parentURL) }) { + return RESOLVED + } + + status = YIELDED + } + + return status +} + +exports.directory = function* (dirname, parentURL, opts = {}) { + let directoryURL + + if (dirname[dirname.length - 1] === '/' || dirname[dirname.length - 1] === '\\') { + directoryURL = new URL(dirname, parentURL) + } else { + directoryURL = new URL(dirname + '/', parentURL) + } + + const info = yield { package: new URL('package.json', directoryURL) } + + if (info) { + if (info.exports) { + return yield* exports.packageExports(directoryURL, '.', info.exports, opts) + } + + if (typeof info.main === 'string' && info.main !== '') { + const status = yield* exports.file(info.main, directoryURL, false, opts) + + if (status === RESOLVED) return status + + return yield* exports.directory(info.main, directoryURL, opts) + } + } + + return yield* exports.file('index', directoryURL, true, opts) +} + +// https://infra.spec.whatwg.org/#ascii-upper-alpha +function isASCIIUpperAlpha(c) { + return c >= 0x41 && c <= 0x5a +} + +// https://infra.spec.whatwg.org/#ascii-lower-alpha +function isASCIILowerAlpha(c) { + return c >= 0x61 && c <= 0x7a +} + +// https://infra.spec.whatwg.org/#ascii-alpha +function isASCIIAlpha(c) { + return isASCIIUpperAlpha(c) || isASCIILowerAlpha(c) +} + +// https://url.spec.whatwg.org/#windows-drive-letter +exports.isWindowsDriveLetter = function isWindowsDriveLetter(input) { + return ( + input.length >= 2 && + isASCIIAlpha(input.charCodeAt(0)) && + (input.charCodeAt(1) === 0x3a || input.charCodeAt(1) === 0x7c) + ) +} + +// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter +exports.startsWithWindowsDriveLetter = function startsWithWindowsDriveLetter(input) { + return ( + input.length >= 2 && + exports.isWindowsDriveLetter(input) && + (input.length === 2 || + input.charCodeAt(2) === 0x2f || + input.charCodeAt(2) === 0x5c || + input.charCodeAt(2) === 0x3f || + input.charCodeAt(2) === 0x23) + ) +} diff --git a/bot/node_modules/bare-module-resolve/lib/errors.d.ts b/bot/node_modules/bare-module-resolve/lib/errors.d.ts new file mode 100644 index 0000000..e700a15 --- /dev/null +++ b/bot/node_modules/bare-module-resolve/lib/errors.d.ts @@ -0,0 +1,11 @@ +declare class ModuleResolveError extends Error { + readonly code: string + + static INVALID_MODULE_SPECIFIER(msg: string): ModuleResolveError + static INVALID_PACKAGE_TARGET(msg: string): ModuleResolveError + static PACKAGE_PATH_NOT_EXPORTED(msg: string): ModuleResolveError + static PACKAGE_IMPORT_NOT_DEFINED(msg: string): ModuleResolveError + static UNSUPPORTED_ENGINE(msg: string): ModuleResolveError +} + +export = ModuleResolveError diff --git a/bot/node_modules/bare-module-resolve/lib/errors.js b/bot/node_modules/bare-module-resolve/lib/errors.js new file mode 100644 index 0000000..565bbef --- /dev/null +++ b/bot/node_modules/bare-module-resolve/lib/errors.js @@ -0,0 +1,50 @@ +module.exports = class ModuleResolveError extends Error { + constructor(msg, code, fn = ModuleResolveError) { + super(`${code}: ${msg}`) + this.code = code + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, fn) + } + } + + get name() { + return 'ModuleResolveError' + } + + static INVALID_MODULE_SPECIFIER(msg) { + return new ModuleResolveError( + msg, + 'INVALID_MODULE_SPECIFIER', + ModuleResolveError.INVALID_MODULE_SPECIFIER + ) + } + + static INVALID_PACKAGE_TARGET(msg) { + return new ModuleResolveError( + msg, + 'INVALID_PACKAGE_TARGET', + ModuleResolveError.INVALID_PACKAGE_TARGET + ) + } + + static PACKAGE_PATH_NOT_EXPORTED(msg) { + return new ModuleResolveError( + msg, + 'PACKAGE_PATH_NOT_EXPORTED', + ModuleResolveError.PACKAGE_PATH_NOT_EXPORTED + ) + } + + static PACKAGE_IMPORT_NOT_DEFINED(msg) { + return new ModuleResolveError( + msg, + 'PACKAGE_IMPORT_NOT_DEFINED', + ModuleResolveError.PACKAGE_IMPORT_NOT_DEFINED + ) + } + + static UNSUPPORTED_ENGINE(msg) { + return new ModuleResolveError(msg, 'UNSUPPORTED_ENGINE', ModuleResolveError.UNSUPPORTED_ENGINE) + } +} diff --git a/bot/node_modules/bare-module-resolve/package.json b/bot/node_modules/bare-module-resolve/package.json new file mode 100644 index 0000000..e22916d --- /dev/null +++ b/bot/node_modules/bare-module-resolve/package.json @@ -0,0 +1,51 @@ +{ + "name": "bare-module-resolve", + "version": "1.12.1", + "description": "Low-level module resolution algorithm for Bare", + "exports": { + "./package": "./package.json", + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "./errors": { + "types": "./lib/errors.d.ts", + "default": "./lib/errors.js" + } + }, + "files": [ + "index.js", + "index.d.ts", + "lib" + ], + "scripts": { + "test": "prettier . --check && bare test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/holepunchto/bare-module-resolve.git" + }, + "author": "Holepunch", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/holepunchto/bare-module-resolve/issues" + }, + "homepage": "https://github.com/holepunchto/bare-module-resolve#readme", + "dependencies": { + "bare-semver": "^1.0.0" + }, + "devDependencies": { + "bare-url": "^2.1.3", + "brittle": "^3.2.1", + "prettier": "^3.3.3", + "prettier-config-holepunch": "^2.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } +} diff --git a/bot/node_modules/bare-semver/LICENSE b/bot/node_modules/bare-semver/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/bot/node_modules/bare-semver/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bot/node_modules/bare-semver/README.md b/bot/node_modules/bare-semver/README.md new file mode 100644 index 0000000..6003ceb --- /dev/null +++ b/bot/node_modules/bare-semver/README.md @@ -0,0 +1,19 @@ +# bare-semver + +Minimal semantic versioning library for Bare. + +``` +npm i bare-semver +``` + +## Usage + +```js +const { Version } = require('bare-semver') + +Version.parse('1.2.3') +``` + +## License + +Apache-2.0 diff --git a/bot/node_modules/bare-semver/index.d.ts b/bot/node_modules/bare-semver/index.d.ts new file mode 100644 index 0000000..9b9b6cb --- /dev/null +++ b/bot/node_modules/bare-semver/index.d.ts @@ -0,0 +1,61 @@ +export const constants: { EQ: 1; LT: 2; LTE: 3; GT: 4; GTE: 5 } + +declare class SemVerError extends Error { + static INVALID_VERSION(msg: string, fn?: Function): SemVerError + + static INVALID_RANGE(msg: string, fn?: Function): SemVerError +} + +export { SemVerError as errors } + +export function satisfies(version: Version, range: Range): boolean + +export class Version { + constructor( + major: number, + minor: number, + patch: number, + opts?: { prerelease?: string[]; build?: string[] } + ) + + major: number + minor: number + patch: number + prerealease: string[] + build: string[] + + compare(version: Version): boolean + + toString(): string +} + +export namespace Version { + export function parse(input: string): Version + + export function compare(a: Version, b: Version): number +} + +export class Comparator { + constructor(operator: number, version: Version) + + operator: number + version: Version + + test(version: Version): boolean + + toString(): string +} + +export class Range { + constructor(comparators?: Comparator[][]) + + comparators: Comparator[][] + + test(version: Version): boolean + + toString(): string +} + +export namespace Range { + export function parse(input: string): Range +} diff --git a/bot/node_modules/bare-semver/index.js b/bot/node_modules/bare-semver/index.js new file mode 100644 index 0000000..e278fa3 --- /dev/null +++ b/bot/node_modules/bare-semver/index.js @@ -0,0 +1,13 @@ +exports.constants = require('./lib/constants') +exports.errors = require('./lib/errors') + +const Version = (exports.Version = require('./lib/version')) +const Range = (exports.Range = require('./lib/range')) +exports.Comparator = require('./lib/comparator') + +exports.satisfies = function satisfies(version, range) { + if (typeof version === 'string') version = Version.parse(version) + if (typeof range === 'string') range = Range.parse(range) + + return range.test(version) +} diff --git a/bot/node_modules/bare-semver/lib/comparator.js b/bot/node_modules/bare-semver/lib/comparator.js new file mode 100644 index 0000000..bd90a2f --- /dev/null +++ b/bot/node_modules/bare-semver/lib/comparator.js @@ -0,0 +1,37 @@ +const constants = require('./constants') + +const symbols = { + [constants.EQ]: '=', + [constants.LT]: '<', + [constants.LTE]: '<=', + [constants.GT]: '>', + [constants.GTE]: '>=' +} + +module.exports = class Comparator { + constructor(operator, version) { + this.operator = operator + this.version = version + } + + test(version) { + const result = version.compare(this.version) + + switch (this.operator) { + case constants.LT: + return result < 0 + case constants.LTE: + return result <= 0 + case constants.GT: + return result > 0 + case constants.GTE: + return result >= 0 + default: + return result === 0 + } + } + + toString() { + return symbols[this.operator] + this.version + } +} diff --git a/bot/node_modules/bare-semver/lib/constants.js b/bot/node_modules/bare-semver/lib/constants.js new file mode 100644 index 0000000..e33513f --- /dev/null +++ b/bot/node_modules/bare-semver/lib/constants.js @@ -0,0 +1,7 @@ +module.exports = { + EQ: 1, + LT: 2, + LTE: 3, + GT: 4, + GTE: 5 +} diff --git a/bot/node_modules/bare-semver/lib/errors.js b/bot/node_modules/bare-semver/lib/errors.js new file mode 100644 index 0000000..92c2f36 --- /dev/null +++ b/bot/node_modules/bare-semver/lib/errors.js @@ -0,0 +1,22 @@ +module.exports = class SemVerError extends Error { + constructor(msg, code, fn = SemVerError) { + super(`${code}: ${msg}`) + this.code = code + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, fn) + } + } + + get name() { + return 'SemVerError' + } + + static INVALID_VERSION(msg, fn = SemVerError.INVALID_VERSION) { + return new SemVerError(msg, 'INVALID_VERSION', fn) + } + + static INVALID_RANGE(msg, fn = SemVerError.INVALID_RANGE) { + return new SemVerError(msg, 'INVALID_RANGE', fn) + } +} diff --git a/bot/node_modules/bare-semver/lib/range.js b/bot/node_modules/bare-semver/lib/range.js new file mode 100644 index 0000000..09f379a --- /dev/null +++ b/bot/node_modules/bare-semver/lib/range.js @@ -0,0 +1,121 @@ +const constants = require('./constants') +const errors = require('./errors') +const Version = require('./version') +const Comparator = require('./comparator') + +class Range { + constructor(comparators = []) { + this.comparators = comparators + } + + test(version) { + for (const set of this.comparators) { + let matches = true + + for (const comparator of set) { + if (comparator.test(version)) continue + matches = false + break + } + + if (matches) return true + } + + return false + } + + toString() { + let result = '' + let first = true + + for (const set of this.comparators) { + if (first) first = false + else result += ' || ' + + result += set.join(' ') + } + + return result + } +} + +module.exports = exports = Range + +exports.parse = function parse(input, state = { position: 0, partial: false }) { + let i = state.position + let c + + const unexpected = (expected) => { + let msg + + if (i >= input.length) { + msg = `Unexpected end of input in '${input}'` + } else { + msg = `Unexpected token '${input[i]}' in '${input}' at position ${i}` + } + + if (expected) msg += `, ${expected}` + + throw errors.INVALID_VERSION(msg, unexpected) + } + + const comparators = [] + + while (i < input.length) { + const set = [] + + while (i < input.length) { + c = input[i] + + let operator = constants.EQ + + if (c === '<') { + operator = constants.LT + c = input[++i] + + if (c === '=') { + operator = constants.LTE + c = input[++i] + } + } else if (c === '>') { + operator = constants.GT + c = input[++i] + + if (c === '=') { + operator = constants.GTE + c = input[++i] + } + } else if (c === '=') { + c = input[++i] + } + + const state = { position: i, partial: true, range: true } + + set.push(new Comparator(operator, Version.parse(input, state))) + + c = input[(i = state.position)] + + while (c === ' ') c = input[++i] + + if (c === '|' && input[i + 1] === '|') { + c = input[(i += 2)] + + while (c === ' ') c = input[++i] + + break + } + + if (c && c !== '<' && c !== '>') unexpected("expected '||', '<', or '>'") + } + + if (set.length) comparators.push(set) + } + + if (i < input.length && state.partial === false) { + unexpected('expected end of input') + } + + state.position = i + + return new Range(comparators) +} diff --git a/bot/node_modules/bare-semver/lib/version.js b/bot/node_modules/bare-semver/lib/version.js new file mode 100644 index 0000000..aef68f1 --- /dev/null +++ b/bot/node_modules/bare-semver/lib/version.js @@ -0,0 +1,208 @@ +const errors = require('./errors') + +class Version { + constructor(major, minor, patch, opts = {}) { + const { prerelease = [], build = [] } = opts + + this.major = major + this.minor = minor + this.patch = patch + this.prerelease = prerelease + this.build = build + } + + compare(version) { + return exports.compare(this, version) + } + + toString() { + let result = `${this.major}.${this.minor}.${this.patch}` + + if (this.prerelease.length) { + result += '-' + this.prerelease.join('.') + } + + if (this.build.length) { + result += '+' + this.build.join('.') + } + + return result + } +} + +module.exports = exports = Version + +exports.parse = function parse( + input, + state = { position: 0, partial: false, range: false } +) { + let i = state.position + let c + + const unexpected = (expected) => { + let msg + + if (i >= input.length) { + msg = `Unexpected end of input in '${input}'` + } else { + msg = `Unexpected token '${input[i]}' in '${input}' at position ${i}` + } + + if (expected) msg += `, ${expected}` + + throw errors.INVALID_VERSION(msg, unexpected) + } + + const components = [0, 0, 0] + + let k = 0 + + while (k < 3) { + c = input[i] + + if (k > 0) { + if (c === '.') c = input[++i] + else if (state.range) break + else unexpected("expected '.'") + } + + if (c === '0') { + i++ + k++ + } else if (c >= '1' && c <= '9') { + let j = 0 + do c = input[i + ++j] + while (c >= '0' && c <= '9') + + components[k++] = parseInt(input.substring(i, i + j)) + + i += j + } else unexpected('expected /[0-9]/') + } + + const prerelease = [] + + if (k === 3 && input[i] === '-') { + i++ + + while (true) { + c = input[i] + + let tag = '' + let j = 0 + + while (c >= '0' && c <= '9') c = input[i + ++j] + + let isNumeric = false + + if (j) { + tag += input.substring(i, i + j) + + c = input[(i += j)] + + isNumeric = tag[0] !== '0' || tag.length === 1 + } + + j = 0 + + while ( + (c >= '0' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + c === '-' + ) + c = input[i + ++j] + + if (j) { + tag += input.substring(i, i + j) + + c = input[(i += j)] + } else if (!isNumeric) unexpected('expected /[a-zA-Z-]/') + + prerelease.push(tag) + + if (c === '.') c = input[++i] + else break + } + } + + const build = [] + + if (k === 3 && input[i] === '+') { + i++ + + while (true) { + c = input[i] + + let tag = '' + let j = 0 + + while ( + (c >= '0' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + c === '-' + ) + c = input[i + ++j] + + if (j) { + tag += input.substring(i, i + j) + + c = input[(i += j)] + } else unexpected('expected /[0-9a-zA-Z-]/') + + build.push(tag) + + if (c === '.') c = input[++i] + else break + } + } + + if (i < input.length && state.partial === false) { + unexpected('expected end of input') + } + + state.position = i + + return new Version(...components, { prerelease, build }) +} + +const integer = /^[0-9]+$/ + +exports.compare = function compare(a, b) { + if (a.major > b.major) return 1 + if (a.major < b.major) return -1 + + if (a.minor > b.minor) return 1 + if (a.minor < b.minor) return -1 + + if (a.patch > b.patch) return 1 + if (a.patch < b.patch) return -1 + + if (a.prerelease.length === 0) return b.prerelease.length === 0 ? 0 : 1 + if (b.prerelease.length === 0) return -1 + + let i = 0 + do { + let x = a.prerelease[i] + let y = b.prerelease[i] + + if (x === undefined) return y === undefined ? 0 : -1 + if (y === undefined) return 1 + + if (x === y) continue + + const xInt = integer.test(x) + const yInt = integer.test(y) + + if (xInt && yInt) { + x = +x + y = +y + } else { + if (xInt) return -1 + if (yInt) return 1 + } + + return x > y ? 1 : -1 + } while (++i) +} diff --git a/bot/node_modules/bare-semver/package.json b/bot/node_modules/bare-semver/package.json new file mode 100644 index 0000000..6a86482 --- /dev/null +++ b/bot/node_modules/bare-semver/package.json @@ -0,0 +1,40 @@ +{ + "name": "bare-semver", + "version": "1.0.2", + "description": "Minimal semantic versioning library for Bare", + "exports": { + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "./package": "./package.json", + "./constants": "./lib/constants.js", + "./errors": "./lib/errors.js", + "./version": "./lib/version.js", + "./range": "./lib/range.js", + "./comparator": "./lib/comparator.js" + }, + "files": [ + "index.js", + "index.d.ts", + "lib" + ], + "scripts": { + "test": "prettier . --check && bare test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/holepunchto/bare-semver.git" + }, + "author": "Holepunch", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/holepunchto/bare-semver/issues" + }, + "homepage": "https://github.com/holepunchto/bare-semver#readme", + "devDependencies": { + "brittle": "^3.2.1", + "prettier": "^3.4.2", + "prettier-config-standard": "^7.0.0" + } +} diff --git a/bot/node_modules/require-addon/LICENSE b/bot/node_modules/require-addon/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/bot/node_modules/require-addon/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bot/node_modules/require-addon/README.md b/bot/node_modules/require-addon/README.md new file mode 100644 index 0000000..9655759 --- /dev/null +++ b/bot/node_modules/require-addon/README.md @@ -0,0 +1,19 @@ +# require-addon + +Import native addons across JavaScript runtimes. + +``` +npm i require-addon +``` + +## Usage + +```js +require.addon = require('require-addon') + +const bindings = require.addon('.', __filename) +``` + +## License + +Apache-2.0 diff --git a/bot/node_modules/require-addon/lib/bare.js b/bot/node_modules/require-addon/lib/bare.js new file mode 100644 index 0000000..37597a5 --- /dev/null +++ b/bot/node_modules/require-addon/lib/bare.js @@ -0,0 +1 @@ +module.exports = require.addon.bind(require) diff --git a/bot/node_modules/require-addon/lib/default.js b/bot/node_modules/require-addon/lib/default.js new file mode 100644 index 0000000..2bed0a5 --- /dev/null +++ b/bot/node_modules/require-addon/lib/default.js @@ -0,0 +1,7 @@ +if (typeof require.addon === 'function') { + module.exports = require.addon.bind(require) +} else { + module.exports = function addon(specifier, parentURL) { + throw new Error(`Cannot find addon '${specifier}' imported from '${parentURL}'`) + } +} diff --git a/bot/node_modules/require-addon/lib/node.js b/bot/node_modules/require-addon/lib/node.js new file mode 100644 index 0000000..21a7682 --- /dev/null +++ b/bot/node_modules/require-addon/lib/node.js @@ -0,0 +1,71 @@ +if (typeof require.addon === 'function') { + module.exports = require.addon.bind(require) +} else { + const url = require('url') + const fs = require('fs') + const resolve = require('bare-addon-resolve') + + let host = process.platform + '-' + process.arch + const conditions = ['addon', 'node', process.platform, process.arch] + const extensions = ['.node'] + + if (isAlpine()) { + host += '-musl' + conditions.push('musl') + } + + module.exports = function addon(specifier, parentURL) { + if (typeof parentURL === 'string') parentURL = url.pathToFileURL(parentURL) + + const candidates = [] + + let cause + + for (const resolution of resolve( + specifier, + parentURL, + { host, conditions, extensions }, + readPackage + )) { + candidates.push(resolution) + + switch (resolution.protocol) { + case 'file:': + try { + return require(url.fileURLToPath(resolution)) + } catch (err) { + cause = err + continue + } + } + } + + let message = `Cannot find addon '${specifier}' imported from '${parentURL.href}'` + + if (candidates.length > 0) { + message += '\nCandidates:' + message += '\n' + candidates.map((url) => '- ' + url.href).join('\n') + } + + const err = new Error(message, cause ? { cause } : {}) + + err.code = 'ADDON_NOT_FOUND' + err.specifier = specifier + err.referrer = parentURL + err.candidates = candidates + + throw err + } + + function readPackage(packageURL) { + try { + return require(url.fileURLToPath(packageURL)) + } catch (err) { + return null + } + } + + function isAlpine() { + return process.platform === 'linux' && fs.existsSync('/etc/alpine-release') + } +} diff --git a/bot/node_modules/require-addon/package.json b/bot/node_modules/require-addon/package.json new file mode 100644 index 0000000..2d72e87 --- /dev/null +++ b/bot/node_modules/require-addon/package.json @@ -0,0 +1,63 @@ +{ + "name": "require-addon", + "version": "1.2.0", + "description": "Import native addons across JavaScript runtimes", + "exports": { + "./package": "./package.json", + ".": { + "bare": "./lib/bare.js", + "node": "./lib/node.js", + "default": "./lib/default.js" + } + }, + "imports": { + "fs": { + "bare": "bare-fs", + "default": "fs" + }, + "path": { + "bare": "bare-path", + "default": "path" + }, + "url": { + "bare": "bare-url", + "default": "url" + } + }, + "files": [ + "lib" + ], + "scripts": { + "test": "npm run lint && npm run test:bare && npm run test:node", + "test:bare": "bare test.js", + "test:node": "node test.js", + "lint": "prettier --check .", + "format": "prettier --write ." + }, + "repository": { + "type": "git", + "url": "git+https://github.com/holepunchto/require-addon.git" + }, + "author": "Holepunch", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/holepunchto/require-addon/issues" + }, + "homepage": "https://github.com/holepunchto/require-addon#readme", + "engines": { + "bare": ">=1.10.0" + }, + "dependencies": { + "bare-addon-resolve": "^1.3.0" + }, + "devDependencies": { + "bare-bundle": "^1.8.1", + "bare-bundle-evaluate": "^1.1.0", + "bare-fs": "^4.0.0", + "bare-path": "^3.0.0", + "bare-url": "^2.1.0", + "brittle": "^3.7.0", + "prettier": "^3.6.2", + "prettier-config-holepunch": "^2.0.0" + } +} diff --git a/bot/node_modules/sodium-native/CMakeLists.txt b/bot/node_modules/sodium-native/CMakeLists.txt new file mode 100644 index 0000000..4331ab0 --- /dev/null +++ b/bot/node_modules/sodium-native/CMakeLists.txt @@ -0,0 +1,278 @@ +cmake_minimum_required(VERSION 3.25) + +find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare) +find_package(cmake-fetch REQUIRED PATHS node_modules/cmake-fetch) +find_package(cmake-napi REQUIRED PATHS node_modules/cmake-napi) +find_package(cmake-npm REQUIRED PATHS node_modules/cmake-npm) + +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +project(sodium_native C ASM CXX) + +fetch_package("github:jedisct1/libsodium#stable" SOURCE_DIR sodium) +fetch_package("github:holepunchto/libjstl#098664c") + +bare_target(target) + +if(target MATCHES "win32") + add_compile_options(/MT$<$:d>) +endif() + +file(COPY_FILE "${sodium}/builds/msvc/version.h" "${sodium}/src/libsodium/include/sodium/version.h") + +file(GLOB_RECURSE sodium_headers CONFIGURE_DEPENDS "${sodium}/src/libsodium/**/*.h") +file(GLOB_RECURSE sodium_sources CONFIGURE_DEPENDS "${sodium}/src/libsodium/**/*.c") +file(GLOB_RECURSE sodium_asm_sources CONFIGURE_DEPENDS "${sodium}/src/libsodium/**/*.S") + +add_library(sodium OBJECT) + +target_sources( + sodium + INTERFACE + ${sodium_headers} + PRIVATE + ${sodium_sources} +) + +target_include_directories( + sodium + INTERFACE + "${sodium}/src/libsodium/include" + PRIVATE + "${sodium}/src/libsodium/include/sodium" +) + +if(NOT target MATCHES "win32") + target_compile_options( + sodium + PRIVATE + -fvisibility=hidden + -fno-strict-aliasing + -fwrapv + -flax-vector-conversions + ) +endif() + +target_compile_definitions( + sodium + PUBLIC + SODIUM_STATIC=1 + PRIVATE + _GNU_SOURCE=1 + CONFIGURED=1 + DEV_MODE=1 + HAVE_ATOMIC_OPS=1 + HAVE_C11_MEMORY_FENCES=1 + HAVE_CET_H=1 + HAVE_GCC_MEMORY_FENCES=1 + HAVE_INLINE_ASM=1 + HAVE_INTTYPES_H=1 + HAVE_STDINT_H=1 + HAVE_TI_MODE=1 +) + +if(target MATCHES "darwin|ios") + target_compile_definitions( + sodium + PRIVATE + ASM_HIDE_SYMBOL=.private_extern + TLS=_Thread_local + HAVE_ARC4RANDOM=1 + HAVE_ARC4RANDOM_BUF=1 + HAVE_CATCHABLE_ABRT=1 + HAVE_CATCHABLE_SEGV=1 + HAVE_CLOCK_GETTIME=1 + HAVE_GETPID=1 + HAVE_MADVISE=1 + HAVE_MEMSET_S=1 + HAVE_MLOCK=1 + HAVE_MMAP=1 + HAVE_MPROTECT=1 + HAVE_NANOSLEEP=1 + HAVE_POSIX_MEMALIGN=1 + HAVE_PTHREAD=1 + HAVE_PTHREAD_PRIO_INHERIT=1 + HAVE_RAISE=1 + HAVE_SYSCONF=1 + HAVE_SYS_MMAN_H=1 + HAVE_SYS_PARAM_H=1 + HAVE_WEAK_SYMBOLS=1 + ) + + if(NOT target MATCHES "ios") + target_compile_definitions( + sodium + PRIVATE + HAVE_GETENTROPY=1 + HAVE_SYS_RANDOM_H=1 + ) + endif() +endif() + +if(target MATCHES "linux") + target_compile_definitions( + sodium + PRIVATE + ASM_HIDE_SYMBOL=.hidden + TLS=_Thread_local + HAVE_CATCHABLE_ABRT=1 + HAVE_CATCHABLE_SEGV=1 + HAVE_CLOCK_GETTIME=1 + HAVE_GETPID=1 + HAVE_MADVISE=1 + HAVE_MLOCK=1 + HAVE_MMAP=1 + HAVE_MPROTECT=1 + HAVE_NANOSLEEP=1 + HAVE_POSIX_MEMALIGN=1 + HAVE_PTHREAD_PRIO_INHERIT=1 + HAVE_PTHREAD=1 + HAVE_RAISE=1 + HAVE_SYSCONF=1 + HAVE_SYS_AUXV_H=1 + HAVE_SYS_MMAN_H=1 + HAVE_SYS_PARAM_H=1 + HAVE_SYS_RANDOM_H=1 + HAVE_WEAK_SYMBOLS=1 + ) +endif() + +if(target MATCHES "win32") + target_compile_definitions( + sodium + PRIVATE + _CRT_SECURE_NO_WARNINGS=1 + HAVE_RAISE=1 + ) +endif() + +if(target MATCHES "x64") + target_compile_definitions( + sodium + PRIVATE + HAVE_CPUID=1 + HAVE_RDRAND=1 + HAVE_EMMINTRIN_H=1 # SSE2 + HAVE_PMMINTRIN_H=1 # SSE3 + HAVE_TMMINTRIN_H=1 # SSSE3 + HAVE_SMMINTRIN_H=1 # SSE4.1 + HAVE_WMMINTRIN_H=1 # AES + HAVE_AVXINTRIN_H=1 # AVX + HAVE_AVX2INTRIN_H=1 # AVX2 + HAVE_AVX512FINTRIN_H # AVX512F + ) + + if(NOT target MATCHES "win32") + target_compile_definitions( + sodium + PRIVATE + HAVE_AMD64_ASM=1 + HAVE_AVX_ASM=1 + ) + + target_sources( + sodium + PRIVATE + ${sodium_asm_sources} + ) + endif() +endif() + +if(target MATCHES "arm64") + target_compile_definitions( + sodium + PRIVATE + HAVE_ARMCRYPTO=1 + ) +endif() + +if(CMAKE_C_BYTE_ORDER MATCHES "BIG_ENDIAN") + target_compile_definitions( + sodium + PRIVATE + NATIVE_BIG_ENDIAN=1 + ) +else() + target_compile_definitions( + sodium + PRIVATE + NATIVE_LITTLE_ENDIAN=1 + ) +endif() + +if(target MATCHES "linux|android") + target_link_options( + sodium + PUBLIC + -Wl,-z,noexecstack + ) +endif() + +add_library(sodium_extensions OBJECT) + +target_sources( + sodium_extensions + PRIVATE + extensions/tweak/tweak.c + extensions/tweak/tweak.h + extensions/pbkdf2/pbkdf2.c + extensions/pbkdf2/pbkdf2.h +) + +target_link_libraries( + sodium_extensions + PUBLIC + sodium +) + +add_bare_module(sodium_native_bare) + +target_sources( + ${sodium_native_bare} + PRIVATE + binding.cc +) + +target_link_libraries( + ${sodium_native_bare} + PRIVATE + $ + $ + jstl + PUBLIC + sodium + sodium_extensions +) + +add_napi_module(sodium_native_node) + +target_sources( + ${sodium_native_node} + PRIVATE + binding.cc +) + +target_compile_definitions( + ${sodium_native_node} + PRIVATE + NAPI_VERSION=9 +) + +target_link_libraries( + ${sodium_native_node} + PRIVATE + $ + $ + jstl + PUBLIC + sodium + sodium_extensions +) + +resolve_node_module(bare-compat-napi compat) + +target_include_directories( + ${sodium_native_node} + PRIVATE + "${compat}/include" +) diff --git a/bot/node_modules/sodium-native/LICENSE b/bot/node_modules/sodium-native/LICENSE new file mode 100644 index 0000000..aebbe8d --- /dev/null +++ b/bot/node_modules/sodium-native/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Mathias Buus and Emil Bay + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/bot/node_modules/sodium-native/README.md b/bot/node_modules/sodium-native/README.md new file mode 100644 index 0000000..c20ec16 --- /dev/null +++ b/bot/node_modules/sodium-native/README.md @@ -0,0 +1,53 @@ +# sodium-native + +Low level bindings for [libsodium](https://github.com/jedisct1/libsodium). + +``` +npm install sodium-native +``` + +The goal of this project is to be thin, stable, unopionated wrapper around libsodium. + +All methods exposed are more or less a direct translation of the libsodium c-api. +This means that most data types are buffers and you have to manage allocating return values and passing them in as arguments instead of receiving them as return values. + +This makes this API harder to use than other libsodium wrappers out there, but also means that you'll be able to get a lot of perf / memory improvements as you can do stuff like inline encryption / decryption, reuse buffers etc. + +This also makes this library useful as a foundation for more high level crypto abstractions that you want to make. + +## Usage + +```js +var sodium = require('sodium-native') + +var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) +var key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES) // secure buffer +var message = Buffer.from('Hello, World!') +var ciphertext = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES) + +sodium.randombytes_buf(nonce) // insert random data into nonce +sodium.randombytes_buf(key) // insert random data into key + +// encrypted message is stored in ciphertext. +sodium.crypto_secretbox_easy(ciphertext, message, nonce, key) + +console.log('Encrypted message:', ciphertext) + +var plainText = Buffer.alloc( + ciphertext.length - sodium.crypto_secretbox_MACBYTES +) + +if (!sodium.crypto_secretbox_open_easy(plainText, ciphertext, nonce, key)) { + console.log('Decryption failed!') +} else { + console.log('Decrypted message:', plainText, '(' + plainText.toString() + ')') +} +``` + +## Documentation + +Complete documentation may be found on the [sodium-friends website](https://sodium-friends.github.io/docs/docs/getstarted) + +## License + +MIT diff --git a/bot/node_modules/sodium-native/binding.cc b/bot/node_modules/sodium-native/binding.cc new file mode 100644 index 0000000..0a4e7d7 --- /dev/null +++ b/bot/node_modules/sodium-native/binding.cc @@ -0,0 +1,4094 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "extensions/pbkdf2/pbkdf2.h" +#include "extensions/tweak/tweak.h" + +#define assert_bounds(arraybuffer) \ + assert(arraybuffer##_offset + arraybuffer##_len <= arraybuffer.size()) + +static inline void +sn_sodium_memzero( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> buf +) { + sodium_memzero(buf.data(), buf.size_bytes()); +} + +static inline int +sn_sodium_mlock( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> buf +) { + return sodium_mlock(buf.data(), buf.size_bytes()); +} + +static inline int +sn_sodium_munlock( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> buf +) { + return sodium_munlock(buf.data(), buf.size_bytes()); +} + +static void +sn_external_arraybuffer_finalize(js_env_t *env, uint8_t *ptr) { + sodium_free(ptr); +} + +static js_arraybuffer_t +sn_sodium_malloc(js_env_t *env, js_receiver_t, uint32_t len) { + int err; + + auto ptr = reinterpret_cast(sodium_malloc(len)); + + js_arraybuffer_t buffer; + + err = js_create_external_arraybuffer(env, ptr, len, buffer); + assert(err == 0); + + return buffer; +} + +static void +sn_sodium_free(js_env_t *env, js_receiver_t, js_arraybuffer_t buf) { + int err; + + std::span view; + err = js_get_arraybuffer_info(env, buf, view); + assert(err == 0); + + if (view.empty()) return; + + err = js_detach_arraybuffer(env, buf); + assert(err == 0); +} + +static inline int +sn_sodium_mprotect_noaccess(js_env_t *env, js_receiver_t, js_arraybuffer_span_t buf) { + return sodium_mprotect_noaccess(buf.data()); +} + +static inline int +sn_sodium_mprotect_readonly(js_env_t *env, js_receiver_t, js_arraybuffer_span_t buf) { + return sodium_mprotect_readonly(buf.data()); +} + +static inline int +sn_sodium_mprotect_readwrite(js_env_t *env, js_receiver_t, js_arraybuffer_span_t buf) { + return sodium_mprotect_readwrite(buf.data()); +} + +static inline uint32_t // TODO: test envless +sn_randombytes_random(js_env_t *env, js_receiver_t) { + return randombytes_random(); +} + +static inline uint32_t // TODO: test envless +sn_randombytes_uniform(js_env_t *env, js_receiver_t, uint32_t upper_bound) { + return randombytes_uniform(upper_bound); +} + +static inline void +sn_randombytes_buf( + js_env_t *env, + js_receiver_t, + js_arraybuffer_span_t buf, + uint32_t buf_offset, + uint32_t buf_len +) { + assert_bounds(buf); + + randombytes_buf(&buf[buf_offset], buf_len); +} + +static inline void +sn_randombytes_buf_deterministic( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t buf, + uint32_t buf_offset, + uint32_t buf_len, + + js_arraybuffer_span_t seed, + uint32_t seed_offset, + uint32_t seed_len +) { + assert_bounds(buf); + assert_bounds(seed); + + assert(seed_len == randombytes_SEEDBYTES); + + randombytes_buf_deterministic(&buf[buf_offset], buf_len, &seed[seed_offset]); +} + +static inline bool +sn_sodium_memcmp(js_env_t *, js_receiver_t, js_typedarray_span_t<> a, js_typedarray_span_t<> b) { + if (a.size_bytes() != b.size_bytes()) return false; + + return sodium_memcmp(a.data(), b.data(), a.size_bytes()) == 0; +} + +static inline void +sn_sodium_increment(js_env_t *, js_receiver_t, js_typedarray_span_t<> n) { + sodium_increment(n.data(), n.size_bytes()); +} + +static inline void +sn_sodium_add(js_env_t *, js_receiver_t, js_typedarray_span_t<> a, js_typedarray_span_t<> b) { + sodium_add(a.data(), b.data(), a.size_bytes()); +} + +static inline void +sn_sodium_sub(js_env_t *, js_receiver_t, js_typedarray_span_t<> a, js_typedarray_span_t<> b) { + sodium_sub(a.data(), b.data(), a.size_bytes()); +} + +static inline int32_t +sn_sodium_compare(js_env_t *, js_receiver_t, js_typedarray_span_t<> a, js_typedarray_span_t<> b) { + return sodium_compare(a.data(), b.data(), a.size_bytes()); +} + +static inline bool +sn_sodium_is_zero(js_env_t *, js_receiver_t, js_typedarray_span_t<> buffer, uint32_t len) { + assert(len <= buffer.size_bytes()); + + return sodium_is_zero(buffer.data(), len) != 0; +} + +static inline uint32_t +sn_sodium_pad(js_env_t *, js_receiver_t, js_typedarray_span_t<> buf, uint32_t unpadded_buflen, uint32_t blocksize) { + size_t padded_buflen; + + sodium_pad(&padded_buflen, buf.data(), unpadded_buflen, blocksize, buf.size_bytes()); + + return padded_buflen; +} + +static inline uint32_t +sn_sodium_unpad(js_env_t *, js_receiver_t, js_typedarray_span_t<> buf, uint32_t padded_buflen, uint32_t blocksize) { + size_t unpadded_buflen; + + sodium_unpad(&unpadded_buflen, buf.data(), padded_buflen, blocksize); + + return unpadded_buflen; +} + +static inline int +sn_crypto_sign_keypair(js_env_t *, js_receiver_t, js_typedarray_span_t<> pk, js_typedarray_span_t<> sk) { + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + + return crypto_sign_keypair(pk.data(), sk.data()); +} + +static inline int +sn_crypto_sign_seed_keypair( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk, + js_typedarray_span_t<> seed +) { + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + assert(seed.size_bytes() == crypto_sign_SEEDBYTES); + + return crypto_sign_seed_keypair(pk.data(), sk.data(), seed.data()); +} + +static inline int +sn_crypto_sign( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> sm, + js_typedarray_span_t<> m, + js_typedarray_span_t<> sk +) { + assert(sm.size_bytes() == crypto_sign_BYTES + m.size_bytes()); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + + return crypto_sign(sm.data(), NULL, m.data(), m.size_bytes(), sk.data()); +} + +static inline bool +sn_crypto_sign_open( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> sm, + js_typedarray_span_t<> pk +) { + assert(m.size_bytes() == sm.size_bytes() - crypto_sign_BYTES); + assert(sm.size_bytes() >= crypto_sign_BYTES); + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + + return crypto_sign_open(m.data(), NULL, sm.data(), sm.size_bytes(), pk.data()) == 0; +} + +static inline int +sn_crypto_sign_detached( + js_env_t *env, + js_receiver_t, + js_typedarray_span_t<> sig, + js_typedarray_span_t<> m, + js_typedarray_span_t<> sk +) { + assert(sig.size_bytes() == crypto_sign_BYTES); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + + return crypto_sign_detached(sig.data(), NULL, m.data(), m.size_bytes(), sk.data()); +} + +static inline bool +sn_crypto_sign_verify_detached( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t sig, + uint32_t sig_offset, + uint32_t sig_len, + + js_arraybuffer_span_t m, + uint32_t m_offset, + uint32_t m_len, + + js_arraybuffer_span_t pk, + uint32_t pk_offset, + uint32_t pk_len +) { + assert_bounds(sig); + assert_bounds(m); + assert_bounds(pk); + + assert(sig_len >= crypto_sign_BYTES); + assert(pk_len == crypto_sign_PUBLICKEYBYTES); + + return crypto_sign_verify_detached(&sig[sig_offset], &m[m_offset], m_len, &pk[pk_offset]) == 0; +} + +static inline int +sn_crypto_sign_ed25519_sk_to_pk( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + + return crypto_sign_ed25519_sk_to_pk(pk.data(), sk.data()); +} + +static inline int +sn_crypto_sign_ed25519_pk_to_curve25519( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> x25519_pk, + js_typedarray_span_t<> ed25519_pk +) { + assert(x25519_pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(ed25519_pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + + return crypto_sign_ed25519_pk_to_curve25519(x25519_pk.data(), ed25519_pk.data()); +} + +static inline int +sn_crypto_sign_ed25519_sk_to_curve25519( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> x25519_sk, + js_typedarray_span_t<> ed25519_sk +) { + assert(x25519_sk.size_bytes() == crypto_box_SECRETKEYBYTES); + assert( + ed25519_sk.size_bytes() == crypto_sign_SECRETKEYBYTES || + ed25519_sk.size_bytes() == crypto_box_SECRETKEYBYTES + ); + + return crypto_sign_ed25519_sk_to_curve25519(x25519_sk.data(), ed25519_sk.data()); +} + +static inline int +sn_crypto_generichash( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_span_t in, + uint32_t in_offset, + uint32_t in_len, + + js_arraybuffer_span_t key, + uint32_t key_offset, + uint32_t key_len +) { + assert_bounds(out); + assert( + out_len >= crypto_generichash_BYTES_MIN && + out_len <= crypto_generichash_BYTES_MAX + ); + + assert_bounds(in); + + uint8_t *key_data = NULL; + if (key_len) { + assert_bounds(key); + assert( + key_len >= crypto_generichash_KEYBYTES_MIN && + key_len <= crypto_generichash_KEYBYTES_MAX + ); + key_data = &key[key_offset]; + } + + return crypto_generichash(&out[out_offset], out_len, &in[in_offset], in_len, key_data, key_len); +} + +static inline int +sn_crypto_generichash_batch( + js_env_t *env, + js_receiver_t, + js_typedarray_t out, + std::vector> batch, + bool use_key, + js_typedarray_t key +) { + int err; + + uint8_t *out_data; + size_t out_len; + err = js_get_typedarray_info(env, out, out_data, out_len); + assert(err == 0); + assert( + out_len >= crypto_generichash_BYTES_MIN && + out_len <= crypto_generichash_BYTES_MAX + ); + + uint8_t *key_data = NULL; + size_t key_len = 0; + if (use_key) { + err = js_get_typedarray_info(env, key, key_data, key_len); + assert(err == 0); + assert( + key_len >= crypto_generichash_KEYBYTES_MIN && + key_len <= crypto_generichash_KEYBYTES_MAX + ); + } + + crypto_generichash_state state; + err = crypto_generichash_init(&state, key_data, key_len, out_len); + if (err != 0) return err; + + for (auto &buf : batch) { + err = crypto_generichash_update(&state, buf.data(), buf.size()); + if (err != 0) return err; + } + + return crypto_generichash_final(&state, out_data, out_len); +} + +static inline void +sn_crypto_generichash_keygen( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t key, + uint32_t key_offset, + uint32_t key_len +) { + assert_bounds(key); + assert(key_len == crypto_generichash_KEYBYTES); + + crypto_generichash_keygen(&key[key_offset]); +} + +static inline int +sn_crypto_generichash_init( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t key, + uint32_t key_offset, + uint32_t key_len, + + uint32_t out_len +) { + assert_bounds(state); + + assert(state_len == sizeof(crypto_generichash_state)); + + uint8_t *key_data = NULL; + if (key_len) { + assert_bounds(key); + assert( + key_len >= crypto_generichash_KEYBYTES_MIN && + key_len <= crypto_generichash_KEYBYTES_MAX + ); + key_data = &key[key_offset]; + } + + auto state_data = reinterpret_cast(&state[state_offset]); + + return crypto_generichash_init(state_data, key_data, key_len, out_len); +} + +static inline int +sn_crypto_generichash_update( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t in, + uint32_t in_offset, + uint32_t in_len +) { + assert_bounds(state); + assert_bounds(in); + + assert(state_len == sizeof(crypto_generichash_state)); + + auto state_data = reinterpret_cast(&state[state_offset]); + + return crypto_generichash_update(state_data, &in[in_offset], in_len); +} + +static inline int +sn_crypto_generichash_final( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t out, + uint32_t out_offset, + uint32_t out_len +) { + assert_bounds(state); + assert_bounds(out); + + assert(state_len == sizeof(crypto_generichash_state)); + + auto state_data = reinterpret_cast(&state[state_offset]); + + return crypto_generichash_final(state_data, &out[out_offset], out_len); +} + +static inline int +sn_crypto_box_keypair(js_env_t *, js_receiver_t, js_typedarray_span_t<> pk, js_typedarray_span_t<> sk) { + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + + return crypto_box_keypair(pk.data(), sk.data()); +} + +static inline int +sn_crypto_box_seed_keypair( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk, + js_typedarray_span_t<> seed +) { + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + assert(seed.size_bytes() == crypto_box_SEEDBYTES); + + return crypto_box_seed_keypair(pk.data(), sk.data(), seed.data()); +} + +static inline int +sn_crypto_box_easy( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(c.size_bytes() == m.size_bytes() + crypto_box_MACBYTES); + assert(n.size_bytes() == crypto_box_NONCEBYTES); + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + + return crypto_box_easy(c.data(), m.data(), m.size_bytes(), n.data(), pk.data(), sk.data()); +} + +static inline bool +sn_crypto_box_open_easy( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(c.size_bytes() >= crypto_box_MACBYTES); + assert(m.size_bytes() == c.size_bytes() - crypto_box_MACBYTES); + assert(n.size_bytes() == crypto_box_NONCEBYTES); + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + + return crypto_box_open_easy(m.data(), c.data(), c.size_bytes(), n.data(), pk.data(), sk.data()) == 0; +} + +static inline int +sn_crypto_box_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(c.size_bytes() == m.size_bytes()); + assert(mac.size_bytes() == crypto_box_MACBYTES); + assert(n.size_bytes() == crypto_box_NONCEBYTES); + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + + return crypto_box_detached(c.data(), mac.data(), m.data(), m.size_bytes(), n.data(), pk.data(), sk.data()); +} + +static inline bool +sn_crypto_box_open_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> n, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(m.size_bytes() == c.size_bytes()); + assert(mac.size_bytes() == crypto_box_MACBYTES); + assert(n.size_bytes() == crypto_box_NONCEBYTES); + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_box_SECRETKEYBYTES); + + return crypto_box_open_detached(m.data(), c.data(), mac.data(), c.size_bytes(), n.data(), pk.data(), sk.data()) == 0; +} + +static inline int +sn_crypto_box_seal( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> pk +) { + assert(c.size_bytes() == m.size_bytes() + crypto_box_SEALBYTES); + assert(pk.size_bytes() == crypto_box_PUBLICKEYBYTES); + + return crypto_box_seal(c.data(), m.data(), m.size_bytes(), pk.data()); +} + +static inline bool +sn_crypto_box_seal_open( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t m, + uint32_t m_offset, + uint32_t m_len, + + js_arraybuffer_span_t c, + uint32_t c_offset, + uint32_t c_len, + + js_arraybuffer_span_t pk, + uint32_t pk_offset, + uint32_t pk_len, + + js_arraybuffer_span_t sk, + uint32_t sk_offset, + uint32_t sk_len +) { + assert_bounds(m); + assert_bounds(c); + assert_bounds(pk); + assert_bounds(sk); + + assert(m_len == c_len - crypto_box_SEALBYTES); + assert(c_len >= crypto_box_SEALBYTES); + assert(sk_len == crypto_box_SECRETKEYBYTES); + assert(pk_len == crypto_box_PUBLICKEYBYTES); + + return crypto_box_seal_open(&m[m_offset], &c[c_offset], c_len, &pk[pk_offset], &sk[sk_offset]) == 0; +} + +static inline int +sn_crypto_secretbox_easy( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes() + crypto_secretbox_MACBYTES); + assert(n.size_bytes() == crypto_secretbox_NONCEBYTES); + assert(k.size_bytes() == crypto_secretbox_KEYBYTES); + + return crypto_secretbox_easy(c.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline bool +sn_crypto_secretbox_open_easy( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes() - crypto_secretbox_MACBYTES); + assert(c.size_bytes() >= crypto_secretbox_MACBYTES); + assert(n.size_bytes() == crypto_secretbox_NONCEBYTES); + assert(k.size_bytes() == crypto_secretbox_KEYBYTES); + + return crypto_secretbox_open_easy(m.data(), c.data(), c.size_bytes(), n.data(), k.data()) == 0; +} + +static inline int +sn_crypto_secretbox_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(mac.size_bytes() == crypto_secretbox_MACBYTES); + assert(n.size_bytes() == crypto_secretbox_NONCEBYTES); + assert(k.size_bytes() == crypto_secretbox_KEYBYTES); + + return crypto_secretbox_detached(c.data(), mac.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline bool +sn_crypto_secretbox_open_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes()); + assert(mac.size_bytes() == crypto_secretbox_MACBYTES); + assert(n.size_bytes() == crypto_secretbox_NONCEBYTES); + assert(k.size_bytes() == crypto_secretbox_KEYBYTES); + + return crypto_secretbox_open_detached(m.data(), c.data(), mac.data(), c.size_bytes(), n.data(), k.data()) == 0; +} + +static inline int +sn_crypto_stream( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_KEYBYTES); + + return crypto_stream(c.data(), c.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_xor( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t c, + uint32_t c_offset, + uint32_t c_len, + + js_arraybuffer_span_t m, + uint32_t m_offset, + uint32_t m_len, + + js_arraybuffer_span_t n, + uint32_t n_offset, + uint32_t n_len, + + js_arraybuffer_span_t k, + uint32_t k_offset, + uint32_t k_len +) { + assert_bounds(c); + assert_bounds(m); + assert_bounds(n); + assert_bounds(k); + + assert(c_len == m_len); + assert(n_len == crypto_stream_NONCEBYTES); + assert(k_len == crypto_stream_KEYBYTES); + + return crypto_stream_xor(&c[c_offset], &m[m_offset], m_len, &n[n_offset], &k[k_offset]); +} + +static inline int +sn_crypto_stream_chacha20( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_chacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_KEYBYTES); + + return crypto_stream_chacha20(c.data(), c.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_chacha20_xor( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_chacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_KEYBYTES); + + return crypto_stream_chacha20_xor(c.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_chacha20_xor_ic( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + uint32_t ic, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_chacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_KEYBYTES); + + return crypto_stream_chacha20_xor_ic(c.data(), m.data(), m.size_bytes(), n.data(), ic, k.data()); +} + +static inline int +sn_crypto_stream_chacha20_ietf( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_chacha20_ietf_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_ietf_KEYBYTES); + + return crypto_stream_chacha20_ietf(c.data(), c.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_chacha20_ietf_xor( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_chacha20_ietf_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_ietf_KEYBYTES); + + return crypto_stream_chacha20_ietf_xor(c.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_chacha20_ietf_xor_ic( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + uint32_t ic, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_chacha20_ietf_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_ietf_KEYBYTES); + + return crypto_stream_chacha20_ietf_xor_ic(c.data(), m.data(), m.size_bytes(), n.data(), ic, k.data()); +} + +static inline int +sn_crypto_stream_xchacha20( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_xchacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_xchacha20_KEYBYTES); + + return crypto_stream_xchacha20(c.data(), c.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_xchacha20_xor( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_xchacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_xchacha20_KEYBYTES); + + return crypto_stream_xchacha20_xor(c.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_xchacha20_xor_ic( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + uint32_t ic, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_xchacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_xchacha20_KEYBYTES); + + return crypto_stream_xchacha20_xor_ic(c.data(), m.data(), m.size_bytes(), n.data(), ic, k.data()); +} + +static inline int +sn_crypto_stream_salsa20( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_salsa20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_salsa20_KEYBYTES); + + return crypto_stream_salsa20(c.data(), c.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_salsa20_xor( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_salsa20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_salsa20_KEYBYTES); + + return crypto_stream_salsa20_xor(c.data(), m.data(), m.size_bytes(), n.data(), k.data()); +} + +static inline int +sn_crypto_stream_salsa20_xor_ic( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + js_typedarray_span_t<> n, + uint32_t ic, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(n.size_bytes() == crypto_stream_salsa20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_salsa20_KEYBYTES); + + return crypto_stream_salsa20_xor_ic(c.data(), m.data(), m.size_bytes(), n.data(), ic, k.data()); +} + +static inline int +sn_crypto_auth( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in, + js_typedarray_span_t<> k +) { + assert(out.size_bytes() == crypto_auth_BYTES); + assert(k.size_bytes() == crypto_auth_KEYBYTES); + + return crypto_auth(out.data(), in.data(), in.size_bytes(), k.data()); +} + +static inline bool +sn_crypto_auth_verify( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> h, + js_typedarray_span_t<> in, + js_typedarray_span_t<> k +) { + assert(h.size_bytes() == crypto_auth_BYTES); + assert(k.size_bytes() == crypto_auth_KEYBYTES); + + return crypto_auth_verify(h.data(), in.data(), in.size_bytes(), k.data()) == 0; +} + +static inline int +sn_crypto_onetimeauth( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in, + js_typedarray_span_t<> k +) { + assert(out.size_bytes() == crypto_onetimeauth_BYTES); + assert(k.size_bytes() == crypto_onetimeauth_KEYBYTES); + + return crypto_onetimeauth(out.data(), in.data(), in.size_bytes(), k.data()); +} + +static inline int +sn_crypto_onetimeauth_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> k +) { + assert(k.size_bytes() == crypto_onetimeauth_KEYBYTES); + + return crypto_onetimeauth_init(&*state, k.data()); +} + +static inline int +sn_crypto_onetimeauth_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> in +) { + return crypto_onetimeauth_update(&*state, in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_onetimeauth_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> out +) { + assert(out.size_bytes() == crypto_onetimeauth_BYTES); + + return crypto_onetimeauth_final(&*state, out.data()); +} + +static inline bool +sn_crypto_onetimeauth_verify( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> h, + js_typedarray_span_t<> in, + js_typedarray_span_t<> k +) { + assert(h.size_bytes() == crypto_onetimeauth_BYTES); + assert(k.size_bytes() == crypto_onetimeauth_KEYBYTES); + + return crypto_onetimeauth_verify(h.data(), in.data(), in.size_bytes(), k.data()) == 0; +} + +// CHECK: memlimit can be >32bit +static inline int +sn_crypto_pwhash( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_of_t passwd, + js_typedarray_span_t<> salt, + uint64_t opslimit, + uint64_t memlimit, + int32_t alg +) { + assert(out.size_bytes() >= crypto_pwhash_BYTES_MIN); + assert(out.size_bytes() <= crypto_pwhash_BYTES_MAX); + assert(salt.size_bytes() == crypto_pwhash_SALTBYTES); + assert(opslimit >= crypto_pwhash_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_MEMLIMIT_MAX); + assert(alg == 1 || alg == 2); // Argon2i or Argon2id + + return crypto_pwhash( + out.data(), + out.size_bytes(), + passwd.data(), + passwd.size_bytes(), + salt.data(), + opslimit, + memlimit, + alg + ); +} + +static inline int +sn_crypto_pwhash_str( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t out, + js_typedarray_span_of_t passwd, + uint64_t opslimit, + uint64_t memlimit +) { + assert(out.size_bytes() == crypto_pwhash_STRBYTES); + assert(opslimit >= crypto_pwhash_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_MEMLIMIT_MAX); + + return crypto_pwhash_str( + out.data(), + passwd.data(), + passwd.size_bytes(), + opslimit, + memlimit + ); +} + +static inline bool +sn_crypto_pwhash_str_verify( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t str, + js_typedarray_span_of_t passwd +) { + assert(str.size_bytes() == crypto_pwhash_STRBYTES); + + return crypto_pwhash_str_verify(str.data(), passwd.data(), passwd.size_bytes()) == 0; +} + +// CHECK: returns 1, 0, -1 +static inline bool +sn_crypto_pwhash_str_needs_rehash( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t str, + uint64_t opslimit, + uint64_t memlimit +) { + assert(str.size_bytes() == crypto_pwhash_STRBYTES); + assert(opslimit >= crypto_pwhash_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_MEMLIMIT_MAX); + + return crypto_pwhash_str_needs_rehash(str.data(), opslimit, memlimit) != 0; +} + +// CHECK: memlimit can be >32bit +static inline int +sn_crypto_pwhash_scryptsalsa208sha256( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_of_t passwd, + js_typedarray_span_t<> salt, + uint64_t opslimit, + uint64_t memlimit +) { + assert(out.size_bytes() >= crypto_pwhash_scryptsalsa208sha256_BYTES_MIN); + assert(out.size_bytes() <= crypto_pwhash_scryptsalsa208sha256_BYTES_MAX); + assert(salt.size_bytes() == crypto_pwhash_scryptsalsa208sha256_SALTBYTES); + assert(opslimit >= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX); + + return crypto_pwhash_scryptsalsa208sha256( + out.data(), + out.size_bytes(), + passwd.data(), + passwd.size_bytes(), + salt.data(), + opslimit, + memlimit + ); +} + +static inline int +sn_crypto_pwhash_scryptsalsa208sha256_str( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t out, + js_typedarray_span_of_t passwd, + uint64_t opslimit, + uint64_t memlimit +) { + assert(out.size_bytes() == crypto_pwhash_scryptsalsa208sha256_STRBYTES); + assert(opslimit >= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX); + + return crypto_pwhash_scryptsalsa208sha256_str( + out.data(), + passwd.data(), + passwd.size_bytes(), + opslimit, + memlimit + ); +} + +static inline bool +sn_crypto_pwhash_scryptsalsa208sha256_str_verify( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t str, + js_typedarray_span_of_t passwd +) { + assert(str.size_bytes() == crypto_pwhash_scryptsalsa208sha256_STRBYTES); + + return crypto_pwhash_scryptsalsa208sha256_str_verify(str.data(), passwd.data(), passwd.size_bytes()) == 0; +} + +static inline bool +sn_crypto_pwhash_scryptsalsa208sha256_str_needs_rehash( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t str, + uint64_t opslimit, + uint64_t memlimit +) { + assert(str.size_bytes() == crypto_pwhash_scryptsalsa208sha256_STRBYTES); + assert(opslimit >= crypto_pwhash_OPSLIMIT_MIN); + assert(opslimit <= crypto_pwhash_OPSLIMIT_MAX); + assert(memlimit >= crypto_pwhash_MEMLIMIT_MIN); + assert(memlimit <= crypto_pwhash_MEMLIMIT_MAX); + + return crypto_pwhash_scryptsalsa208sha256_str_needs_rehash(str.data(), opslimit, memlimit) != 0; +} + +static inline int +sn_crypto_kx_keypair( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk +) { + assert(pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_kx_SECRETKEYBYTES); + + return crypto_kx_keypair(pk.data(), sk.data()); +} + +static inline int +sn_crypto_kx_seed_keypair( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> sk, + js_typedarray_span_t<> seed +) { + assert(pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + assert(sk.size_bytes() == crypto_kx_SECRETKEYBYTES); + assert(seed.size_bytes() == crypto_kx_SEEDBYTES); + + return crypto_kx_seed_keypair(pk.data(), sk.data(), seed.data()); +} + +static inline int +sn_crypto_kx_client_session_keys( + js_env_t *, + js_receiver_t, + std::optional> rx, + std::optional> tx, + js_typedarray_span_t<> client_pk, + js_typedarray_span_t<> client_sk, + js_typedarray_span_t<> server_pk +) { + assert(rx.has_value() || tx.has_value()); + + if (rx) assert(rx->size_bytes() == crypto_kx_SESSIONKEYBYTES); + if (tx) assert(tx->size_bytes() == crypto_kx_SESSIONKEYBYTES); + + assert(client_pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + assert(client_sk.size_bytes() == crypto_kx_SECRETKEYBYTES); + assert(server_pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + + return crypto_kx_client_session_keys( + rx ? rx->data() : nullptr, + tx ? tx->data() : nullptr, + client_pk.data(), + client_sk.data(), + server_pk.data() + ); +} + +static inline int +sn_crypto_kx_server_session_keys( + js_env_t *, + js_receiver_t, + std::optional> rx, + std::optional> tx, + js_typedarray_span_t<> server_pk, + js_typedarray_span_t<> server_sk, + js_typedarray_span_t<> client_pk +) { + assert(rx.has_value() || tx.has_value()); + + if (rx) assert(rx->size_bytes() == crypto_kx_SESSIONKEYBYTES); + if (tx) assert(tx->size_bytes() == crypto_kx_SESSIONKEYBYTES); + + assert(server_pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + assert(server_sk.size_bytes() == crypto_kx_SECRETKEYBYTES); + assert(client_pk.size_bytes() == crypto_kx_PUBLICKEYBYTES); + + return crypto_kx_server_session_keys( + rx ? rx->data() : nullptr, + tx ? tx->data() : nullptr, + server_pk.data(), + server_sk.data(), + client_pk.data() + ); +} + +static inline int +sn_crypto_scalarmult_base( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n +) { + assert(q.size_bytes() == crypto_scalarmult_BYTES); + assert(n.size_bytes() == crypto_scalarmult_SCALARBYTES); + + return crypto_scalarmult_base(q.data(), n.data()); +} + +static inline int +sn_crypto_scalarmult( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n, + js_typedarray_span_t<> p +) { + assert(q.size_bytes() == crypto_scalarmult_BYTES); + assert(n.size_bytes() == crypto_scalarmult_SCALARBYTES); + assert(p.size_bytes() == crypto_scalarmult_BYTES); + + return crypto_scalarmult(q.data(), n.data(), p.data()); +} + +static inline int +sn_crypto_scalarmult_ed25519_base( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n +) { + assert(q.size_bytes() == crypto_scalarmult_ed25519_BYTES); + assert(n.size_bytes() == crypto_scalarmult_ed25519_SCALARBYTES); + + return crypto_scalarmult_ed25519_base(q.data(), n.data()); +} + +static inline int +sn_crypto_scalarmult_ed25519( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n, + js_typedarray_span_t<> p +) { + assert(q.size_bytes() == crypto_scalarmult_ed25519_BYTES); + assert(n.size_bytes() == crypto_scalarmult_ed25519_SCALARBYTES); + assert(p.size_bytes() == crypto_scalarmult_ed25519_BYTES); + + return crypto_scalarmult_ed25519(q.data(), n.data(), p.data()); +} + +static inline bool +sn_crypto_core_ed25519_is_valid_point( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> p +) { + assert(p.size_bytes() == crypto_core_ed25519_BYTES); + + return crypto_core_ed25519_is_valid_point(p.data()) != 0; +} + +static inline int +sn_crypto_core_ed25519_from_uniform( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> p, + js_typedarray_span_t<> r +) { + assert(p.size_bytes() == crypto_core_ed25519_BYTES); + assert(r.size_bytes() == crypto_core_ed25519_UNIFORMBYTES); + + return crypto_core_ed25519_from_uniform(p.data(), r.data()); +} + +static inline int +sn_crypto_scalarmult_ed25519_base_noclamp( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n +) { + assert(q.size_bytes() == crypto_scalarmult_ed25519_BYTES); + assert(n.size_bytes() == crypto_scalarmult_ed25519_SCALARBYTES); + + return crypto_scalarmult_ed25519_base_noclamp(q.data(), n.data()); +} + +static inline int +sn_crypto_scalarmult_ed25519_noclamp( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> q, + js_typedarray_span_t<> n, + js_typedarray_span_t<> p +) { + assert(q.size_bytes() == crypto_scalarmult_ed25519_BYTES); + assert(n.size_bytes() == crypto_scalarmult_ed25519_SCALARBYTES); + assert(p.size_bytes() == crypto_scalarmult_ed25519_BYTES); + + return crypto_scalarmult_ed25519_noclamp(q.data(), n.data(), p.data()); +} + +static inline int +sn_crypto_core_ed25519_add( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> r, + js_typedarray_span_t<> p, + js_typedarray_span_t<> q +) { + assert(r.size_bytes() == crypto_core_ed25519_BYTES); + assert(p.size_bytes() == crypto_core_ed25519_BYTES); + assert(q.size_bytes() == crypto_core_ed25519_BYTES); + + return crypto_core_ed25519_add(r.data(), p.data(), q.data()); +} + +static inline int +sn_crypto_core_ed25519_sub( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> r, + js_typedarray_span_t<> p, + js_typedarray_span_t<> q +) { + assert(r.size_bytes() == crypto_core_ed25519_BYTES); + assert(p.size_bytes() == crypto_core_ed25519_BYTES); + assert(q.size_bytes() == crypto_core_ed25519_BYTES); + + return crypto_core_ed25519_sub(r.data(), p.data(), q.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_random( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> r +) { + assert(r.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_random(r.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_reduce( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> r, + js_typedarray_span_t<> s +) { + assert(r.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(s.size_bytes() == crypto_core_ed25519_NONREDUCEDSCALARBYTES); + + crypto_core_ed25519_scalar_reduce(r.data(), s.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_invert( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> recip, + js_typedarray_span_t<> s +) { + assert(recip.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(s.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_invert(recip.data(), s.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_negate( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> neg, + js_typedarray_span_t<> s +) { + assert(neg.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(s.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_negate(neg.data(), s.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_complement( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> comp, + js_typedarray_span_t<> s +) { + assert(comp.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(s.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_complement(comp.data(), s.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_add( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> z, + js_typedarray_span_t<> x, + js_typedarray_span_t<> y +) { + assert(z.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(x.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(y.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_add(z.data(), x.data(), y.data()); +} + +static inline void +sn_crypto_core_ed25519_scalar_sub( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> z, + js_typedarray_span_t<> x, + js_typedarray_span_t<> y +) { + assert(z.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(x.size_bytes() == crypto_core_ed25519_SCALARBYTES); + assert(y.size_bytes() == crypto_core_ed25519_SCALARBYTES); + + crypto_core_ed25519_scalar_sub(z.data(), x.data(), y.data()); +} + +static inline int +sn_crypto_shorthash( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in, + js_typedarray_span_t<> k +) { + assert(out.size_bytes() == crypto_shorthash_BYTES); + assert(k.size_bytes() == crypto_shorthash_KEYBYTES); + + return crypto_shorthash(out.data(), in.data(), in.size_bytes(), k.data()); +} + +static inline void +sn_crypto_kdf_keygen( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> key +) { + assert(key.size_bytes() == crypto_kdf_KEYBYTES); + + crypto_kdf_keygen(key.data()); +} + +static inline int +sn_crypto_kdf_derive_from_key( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> subkey, + uint64_t subkey_id, + js_typedarray_span_of_t ctx, + js_typedarray_span_t<> key +) { + assert(subkey.size_bytes() >= crypto_kdf_BYTES_MIN); + assert(subkey.size_bytes() <= crypto_kdf_BYTES_MAX); + assert(ctx.size_bytes() == crypto_kdf_CONTEXTBYTES); + assert(key.size_bytes() == crypto_kdf_KEYBYTES); + + return crypto_kdf_derive_from_key( + subkey.data(), + subkey.size_bytes(), + subkey_id, + ctx.data(), + key.data() + ); +} + +static inline int +sn_crypto_hash( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in +) { + assert(out.size_bytes() == crypto_hash_BYTES); + + return crypto_hash(out.data(), in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_hash_sha256( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in +) { + assert(out.size_bytes() == crypto_hash_sha256_BYTES); + + return crypto_hash_sha256(out.data(), in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_hash_sha256_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + return crypto_hash_sha256_init(&*state); +} + +static inline int +sn_crypto_hash_sha256_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> in +) { + return crypto_hash_sha256_update(&*state, in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_hash_sha256_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> out +) { + assert(out.size_bytes() == crypto_hash_sha256_BYTES); + + return crypto_hash_sha256_final(&*state, out.data()); +} + +static inline int +sn_crypto_hash_sha512( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> in +) { + assert(out.size_bytes() == crypto_hash_sha512_BYTES); + + return crypto_hash_sha512(out.data(), in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_hash_sha512_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + return crypto_hash_sha512_init(&*state); +} + +static inline int +sn_crypto_hash_sha512_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> in +) { + return crypto_hash_sha512_update(&*state, in.data(), in.size_bytes()); +} + +static inline int +sn_crypto_hash_sha512_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> out +) { + assert(out.size_bytes() == crypto_hash_sha512_BYTES); + + return crypto_hash_sha512_final(&*state, out.data()); +} + +static inline void +sn_crypto_aead_xchacha20poly1305_ietf_keygen( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> k +) { + assert(k.size_bytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + + crypto_aead_xchacha20poly1305_ietf_keygen(k.data()); +} + +static inline uint64_t +sn_crypto_aead_xchacha20poly1305_ietf_encrypt( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes() + crypto_aead_xchacha20poly1305_ietf_ABYTES); + assert(c.size_bytes() <= 0xffffffff); + assert(npub.size_bytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long clen = 0; + int status = crypto_aead_xchacha20poly1305_ietf_encrypt( + c.data(), + &clen, + m.data(), + m.size_bytes(), + ad_data, + ad_size, + nullptr, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return clen; +} + +static inline uint64_t +sn_crypto_aead_xchacha20poly1305_ietf_decrypt( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes() - crypto_aead_xchacha20poly1305_ietf_ABYTES); + assert(m.size_bytes() <= 0xffffffff); + assert(npub.size_bytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long mlen = 0; + int status = crypto_aead_xchacha20poly1305_ietf_decrypt( + m.data(), + &mlen, + nullptr, + c.data(), + c.size_bytes(), + ad_data, + ad_size, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return mlen; +} + +static inline uint64_t +sn_crypto_aead_xchacha20poly1305_ietf_encrypt_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> m, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(mac.size_bytes() == crypto_aead_xchacha20poly1305_ietf_ABYTES); + assert(npub.size_bytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long maclen = 0; + int status = crypto_aead_xchacha20poly1305_ietf_encrypt_detached( + c.data(), + mac.data(), + &maclen, + m.data(), + m.size_bytes(), + ad_data, + ad_size, + nullptr, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return maclen; +} + +static inline int +sn_crypto_aead_xchacha20poly1305_ietf_decrypt_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes()); + assert(mac.size_bytes() == crypto_aead_xchacha20poly1305_ietf_ABYTES); + assert(npub.size_bytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + return crypto_aead_xchacha20poly1305_ietf_decrypt_detached( + m.data(), + nullptr, + c.data(), + c.size_bytes(), + mac.data(), + ad_data, + ad_size, + npub.data(), + k.data() + ); +} + +static inline void +sn_crypto_aead_chacha20poly1305_ietf_keygen( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> k +) { + assert(k.size_bytes() == crypto_aead_chacha20poly1305_ietf_KEYBYTES); + + crypto_aead_chacha20poly1305_ietf_keygen(k.data()); +} + +static inline uint64_t +sn_crypto_aead_chacha20poly1305_ietf_encrypt( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes() + crypto_aead_chacha20poly1305_ietf_ABYTES); + assert(c.size_bytes() <= 0xffffffff); + assert(npub.size_bytes() == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_chacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long clen = 0; + int status = crypto_aead_chacha20poly1305_ietf_encrypt( + c.data(), + &clen, + m.data(), + m.size_bytes(), + ad_data, + ad_size, + nullptr, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return clen; +} + +static inline uint64_t +sn_crypto_aead_chacha20poly1305_ietf_decrypt( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes() - crypto_aead_chacha20poly1305_ietf_ABYTES); + assert(m.size_bytes() <= 0xffffffff); + assert(npub.size_bytes() == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_chacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long mlen = 0; + int status = crypto_aead_chacha20poly1305_ietf_decrypt( + m.data(), + &mlen, + nullptr, + c.data(), + c.size_bytes(), + ad_data, + ad_size, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return mlen; +} + +static inline uint64_t +sn_crypto_aead_chacha20poly1305_ietf_encrypt_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + js_typedarray_span_t<> m, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(c.size_bytes() == m.size_bytes()); + assert(mac.size_bytes() == crypto_aead_chacha20poly1305_ietf_ABYTES); + assert(npub.size_bytes() == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_chacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + unsigned long long maclen = 0; + int status = crypto_aead_chacha20poly1305_ietf_encrypt_detached( + c.data(), + mac.data(), + &maclen, + m.data(), + m.size_bytes(), + ad_data, + ad_size, + nullptr, + npub.data(), + k.data() + ); + + if (status < 0) return status; + + return maclen; +} + +static inline int +sn_crypto_aead_chacha20poly1305_ietf_decrypt_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> m, + js_typedarray_span_t<> c, + js_typedarray_span_t<> mac, + std::optional> ad, + js_typedarray_span_t<> npub, + js_typedarray_span_t<> k +) { + assert(m.size_bytes() == c.size_bytes()); + assert(mac.size_bytes() == crypto_aead_chacha20poly1305_ietf_ABYTES); + assert(npub.size_bytes() == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); + assert(k.size_bytes() == crypto_aead_chacha20poly1305_ietf_KEYBYTES); + + uint8_t *ad_data = nullptr; + size_t ad_size = 0; + if (ad) { + ad_data = ad->data(); + ad_size = ad->size_bytes(); + } + + return crypto_aead_chacha20poly1305_ietf_decrypt_detached( + m.data(), + nullptr, + c.data(), + c.size_bytes(), + mac.data(), + ad_data, + ad_size, + npub.data(), + k.data() + ); +} + +static inline void +sn_crypto_secretstream_xchacha20poly1305_keygen( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t k, + uint32_t k_offset, + uint32_t k_len +) { + assert_bounds(k); + assert(k_len == crypto_secretstream_xchacha20poly1305_KEYBYTES); + + crypto_secretstream_xchacha20poly1305_keygen(&k[k_offset]); +} + +static inline int +sn_crypto_secretstream_xchacha20poly1305_init_push( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t header, + uint32_t header_offset, + uint32_t header_len, + + js_arraybuffer_span_t k, + uint32_t k_offset, + uint32_t k_len +) { + assert_bounds(state); + assert_bounds(header); + assert_bounds(k); + + assert(state_len == sizeof(crypto_secretstream_xchacha20poly1305_state)); + assert(header_len == crypto_secretstream_xchacha20poly1305_HEADERBYTES); + assert(k_len == crypto_secretstream_xchacha20poly1305_KEYBYTES); + + auto state_data = reinterpret_cast(&state[state_offset]); + + return crypto_secretstream_xchacha20poly1305_init_push(state_data, &header[header_offset], &k[k_offset]); +} + +static inline uint64_t +sn_crypto_secretstream_xchacha20poly1305_push( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t c, + uint32_t c_offset, + uint32_t c_len, + + js_arraybuffer_span_t m, + uint32_t m_offset, + uint32_t m_len, + + js_arraybuffer_span_t ad, + uint32_t ad_offset, + uint32_t ad_len, + + uint32_t tag +) { + assert_bounds(state); + assert_bounds(c); + assert_bounds(m); + + assert(state_len == sizeof(crypto_secretstream_xchacha20poly1305_state)); + assert(c_len == m_len + crypto_secretstream_xchacha20poly1305_ABYTES); + assert(c_len <= 0xffffffff && "32bit integer"); + + auto state_data = reinterpret_cast(&state[state_offset]); + + uint8_t *ad_data = NULL; + if (ad_len) { + assert_bounds(ad); + ad_data = &ad[ad_offset]; + } + + unsigned long long clen = 0; + int status = crypto_secretstream_xchacha20poly1305_push( + state_data, + &c[c_offset], + &clen, + &m[m_offset], + m_len, + ad_data, + ad_len, + tag + ); + + if (status < 0) return status; + + return clen; +} + +static inline int +sn_crypto_secretstream_xchacha20poly1305_init_pull( + js_env_t *, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t header, + uint32_t header_offset, + uint32_t header_len, + + js_arraybuffer_span_t k, + uint32_t k_offset, + uint32_t k_len +) { + assert_bounds(state); + assert_bounds(header); + assert_bounds(k); + + assert(state_len == sizeof(crypto_secretstream_xchacha20poly1305_state)); + assert(header_len == crypto_secretstream_xchacha20poly1305_HEADERBYTES); + assert(k_len == crypto_secretstream_xchacha20poly1305_KEYBYTES); + + auto state_data = reinterpret_cast(&state[state_offset]); + + return crypto_secretstream_xchacha20poly1305_init_pull(state_data, &header[header_offset], &k[k_offset]); +} + +static inline uint64_t +sn_crypto_secretstream_xchacha20poly1305_pull( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len, + + js_arraybuffer_span_t m, + uint32_t m_offset, + uint32_t m_len, + + js_arraybuffer_span_t tag, + uint32_t tag_offset, + uint32_t tag_len, + + js_arraybuffer_span_t c, + uint32_t c_offset, + uint32_t c_len, + + js_arraybuffer_span_t ad, + uint32_t ad_offset, + uint32_t ad_len +) { + assert_bounds(state); + assert_bounds(m); + assert_bounds(tag); + assert_bounds(c); + + assert(state_len == sizeof(crypto_secretstream_xchacha20poly1305_state)); + assert(c_len >= crypto_secretstream_xchacha20poly1305_ABYTES); + assert(tag_len == 1); + assert(m_len == c_len - crypto_secretstream_xchacha20poly1305_ABYTES); + assert(m_len <= 0xffffffff); + + auto state_data = reinterpret_cast(&state[state_offset]); + + uint8_t *ad_data = NULL; + if (ad_len) { + assert_bounds(ad); + ad_data = &ad[ad_offset]; + } + + unsigned long long mlen = 0; + int status = crypto_secretstream_xchacha20poly1305_pull( + state_data, + &m[m_offset], + &mlen, + &tag[tag_offset], + &c[c_offset], + c_len, + ad_data, + ad_len + ); + + if (status < 0) return status; + + return mlen; +} + +static inline void +sn_crypto_secretstream_xchacha20poly1305_rekey( + js_env_t *, + js_receiver_t, + + js_arraybuffer_span_t state, + uint32_t state_offset, + uint32_t state_len +) { + assert_bounds(state); + + assert(state_len == sizeof(crypto_secretstream_xchacha20poly1305_state)); + + auto state_data = reinterpret_cast(&state[state_offset]); + + crypto_secretstream_xchacha20poly1305_rekey(state_data); +} + +struct sn_async_task { + uv_work_t task; + js_env_t *env; + js_persistent_t> cb; + js_deferred_teardown_t *teardown; + int code = 0; + bool exiting = false; + + sn_async_task(js_env_t *env) : env(env) { + int err; + err = js_add_teardown_callback(env, this, teardown); + assert(err == 0); + } + + ~sn_async_task() { + int err; + err = js_finish_teardown_callback(teardown); + assert(err == 0); + } + +private: + static void + on_teardown(js_deferred_teardown_t *teardown, sn_async_task *req) { + req->exiting = true; + } +}; + +struct sn_async_pwhash_request : sn_async_task { + js_persistent_t out_ref; + std::span out; + + js_persistent_t pwd_ref; + std::span pwd; + + js_persistent_t salt_ref; + std::span salt; + + uint64_t opslimit; + size_t memlimit; + int alg; + + sn_async_pwhash_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_execute(uv_work_t *uv_req) { + auto req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash( + req->out.data(), + req->out.size(), + req->pwd.data(), + req->pwd.size(), + req->salt.data(), + req->opslimit, + req->memlimit, + req->alg + ); +} + +static void +async_pwhash_complete(uv_work_t *uv_req, int status) { + int err; + + auto req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + js_arraybuffer_t salt, + uint32_t salt_offset, + uint32_t salt_len, + + uint64_t opslimit, + uint64_t memlimit, + uint32_t alg, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_request(env); + + std::span out_view; + err = js_get_arraybuffer_info(env, out, out_view); + assert(err == 0); + assert(out_offset + out_len <= out_view.size()); + + req->out = {&out_view[out_offset], out_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + std::span salt_view; + err = js_get_arraybuffer_info(env, salt, salt_view); + assert(err == 0); + assert(salt_offset + salt_len <= salt_view.size()); + + req->salt = {&salt_view[salt_offset], salt_len}; + + req->opslimit = opslimit; + req->memlimit = memlimit; + req->alg = alg; + + err = js_create_reference(env, out, req->out_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, salt, req->salt_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_execute, async_pwhash_complete); + assert(err == 0); +} + +struct sn_async_pwhash_str_request : sn_async_task { + js_persistent_t out_ref; + std::span out; + + js_persistent_t pwd_ref; + std::span pwd; + + uint64_t opslimit; + size_t memlimit; + + sn_async_pwhash_str_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_str_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash_str( + req->out.data(), + req->pwd.data(), + req->pwd.size(), + req->opslimit, + req->memlimit + ); +} + +static void +async_pwhash_str_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_str_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + uint64_t opslimit, + uint64_t memlimit, + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_str_request(env); + + std::span out_view; + err = js_get_arraybuffer_info(env, out, out_view); + assert(err == 0); + assert(out_offset + out_len <= out_view.size()); + + req->out = {&out_view[out_offset], out_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + req->opslimit = opslimit; + req->memlimit = memlimit; + + err = js_create_reference(env, out, req->out_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_str_execute, async_pwhash_str_complete); + assert(err == 0); +} + +struct sn_async_pwhash_str_verify_request : sn_async_task { + js_persistent_t str_ref; + std::span str; + + js_persistent_t pwd_ref; + std::span pwd; + + sn_async_pwhash_str_verify_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_str_verify_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash_str_verify(req->str.data(), req->pwd.data(), req->pwd.size()); +} + +static void +async_pwhash_str_verify_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_str_verify_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t str, + uint32_t str_offset, + uint32_t str_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_str_verify_request(env); + + std::span str_view; + err = js_get_arraybuffer_info(env, str, str_view); + assert(err == 0); + assert(str_offset + str_len <= str_view.size()); + + req->str = {&str_view[str_offset], str_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + err = js_create_reference(env, str, req->str_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_str_verify_execute, async_pwhash_str_verify_complete); + assert(err == 0); +} + +struct sn_async_pwhash_scryptsalsa208sha256_request : sn_async_task { + js_persistent_t out_ref; + std::span out; + + js_persistent_t pwd_ref; + std::span pwd; + + js_persistent_t salt_ref; + std::span salt; + + uint64_t opslimit; + size_t memlimit; + + sn_async_pwhash_scryptsalsa208sha256_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_scryptsalsa208sha256_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash_scryptsalsa208sha256( + req->out.data(), + req->out.size(), + req->pwd.data(), + req->pwd.size(), + req->salt.data(), + req->opslimit, + req->memlimit + ); +} + +static void +async_pwhash_scryptsalsa208sha256_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_scryptsalsa208sha256_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + js_arraybuffer_t salt, + uint32_t salt_offset, + uint32_t salt_len, + + uint64_t opslimit, + uint64_t memlimit, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_scryptsalsa208sha256_request(env); + + std::span out_view; + err = js_get_arraybuffer_info(env, out, out_view); + assert(err == 0); + assert(out_offset + out_len <= out_view.size()); + + req->out = {&out_view[out_offset], out_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + std::span salt_view; + err = js_get_arraybuffer_info(env, salt, salt_view); + assert(err == 0); + assert(salt_offset + salt_len <= salt_view.size()); + assert(salt_len == crypto_pwhash_scryptsalsa208sha256_SALTBYTES); + + req->salt = {&salt_view[salt_offset], salt_len}; + + req->opslimit = opslimit; + req->memlimit = memlimit; + + err = js_create_reference(env, out, req->out_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, salt, req->salt_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_scryptsalsa208sha256_execute, async_pwhash_scryptsalsa208sha256_complete); + assert(err == 0); +} + +struct sn_async_pwhash_scryptsalsa208sha256_str_request : sn_async_task { + js_persistent_t out_ref; + std::span out; + + js_persistent_t pwd_ref; + std::span pwd; + + uint64_t opslimit; + size_t memlimit; + + sn_async_pwhash_scryptsalsa208sha256_str_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_scryptsalsa208sha256_str_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash_scryptsalsa208sha256_str( + req->out.data(), + req->pwd.data(), + req->pwd.size(), + req->opslimit, + req->memlimit + ); +} + +static void +async_pwhash_scryptsalsa208sha256_str_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_scryptsalsa208sha256_str_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + uint64_t opslimit, + uint64_t memlimit, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_scryptsalsa208sha256_str_request(env); + + std::span out_view; + err = js_get_arraybuffer_info(env, out, out_view); + assert(err == 0); + assert(out_offset + out_len <= out_view.size()); + + req->out = {&out_view[out_offset], out_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + req->opslimit = opslimit; + req->memlimit = memlimit; + + err = js_create_reference(env, out, req->out_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_scryptsalsa208sha256_str_execute, async_pwhash_scryptsalsa208sha256_str_complete); + assert(err == 0); +} + +struct sn_async_pwhash_scryptsalsa208sha256_str_verify_request : sn_async_task { + js_persistent_t str_ref; + std::span str; + + js_persistent_t pwd_ref; + std::span pwd; + + sn_async_pwhash_scryptsalsa208sha256_str_verify_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pwhash_scryptsalsa208sha256_str_verify_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = crypto_pwhash_scryptsalsa208sha256_str_verify(req->str.data(), req->pwd.data(), req->pwd.size()); +} + +static void +async_pwhash_scryptsalsa208sha256_str_verify_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_crypto_pwhash_scryptsalsa208sha256_str_verify_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t str, + uint32_t str_offset, + uint32_t str_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pwhash_scryptsalsa208sha256_str_verify_request(env); + + std::span str_view; + err = js_get_arraybuffer_info(env, str, str_view); + assert(err == 0); + assert(str_offset + str_len <= str_view.size()); + + req->str = {&str_view[str_offset], str_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + err = js_create_reference(env, str, req->str_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pwhash_scryptsalsa208sha256_str_verify_execute, async_pwhash_scryptsalsa208sha256_str_verify_complete); + assert(err == 0); +} + +struct sn_crypto_stream_xor_state { + unsigned char n[crypto_stream_NONCEBYTES]; + unsigned char k[crypto_stream_KEYBYTES]; + unsigned char next_block[64]; + int remainder; + uint64_t block_counter; +}; + +static inline void +sn_crypto_stream_xor_wrap_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_KEYBYTES); + + state->remainder = 0; + state->block_counter = 0; + memcpy(state->n, n.data(), crypto_stream_NONCEBYTES); + memcpy(state->k, k.data(), crypto_stream_KEYBYTES); +} + +static inline void +sn_crypto_stream_xor_wrap_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m +) { + assert(c.size_bytes() == m.size_bytes()); + + auto next_block = state->next_block; + + size_t m_size = m.size_bytes(); + auto *c_ptr = c.data(); + auto *m_ptr = m.data(); + + if (state->remainder) { + uint64_t offset = 0; + int rem = state->remainder; + + while (rem < 64 && offset < m_size) { + c_ptr[offset] = next_block[rem] ^ m_ptr[offset]; + ++offset; + ++rem; + } + + c_ptr += offset; + m_ptr += offset; + m_size -= offset; + state->remainder = (rem == 64) ? 0 : rem; + + if (m_size == 0) return; + } + + state->remainder = m_size & 63; + size_t main_len = m_size - state->remainder; + + crypto_stream_xsalsa20_xor_ic(c_ptr, m_ptr, main_len, state->n, state->block_counter, state->k); + state->block_counter += main_len / 64; + + if (state->remainder) { + sodium_memzero(next_block + state->remainder, 64 - state->remainder); + memcpy(next_block, m_ptr + main_len, state->remainder); + + crypto_stream_xsalsa20_xor_ic( + next_block, next_block, 64, state->n, state->block_counter, state->k + ); + memcpy(c_ptr + main_len, next_block, state->remainder); + + state->block_counter++; + } +} + +static inline void +sn_crypto_stream_xor_wrap_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + sodium_memzero(state->n, sizeof(state->n)); + sodium_memzero(state->k, sizeof(state->k)); + sodium_memzero(state->next_block, sizeof(state->next_block)); + state->remainder = 0; +} + +struct sn_crypto_stream_chacha20_xor_state { + unsigned char n[crypto_stream_chacha20_NONCEBYTES]; + unsigned char k[crypto_stream_chacha20_KEYBYTES]; + unsigned char next_block[64]; + int remainder; + uint64_t block_counter; +}; + +static inline void +sn_crypto_stream_chacha20_xor_wrap_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_chacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_KEYBYTES); + + state->remainder = 0; + state->block_counter = 0; + memcpy(state->n, n.data(), crypto_stream_chacha20_NONCEBYTES); + memcpy(state->k, k.data(), crypto_stream_chacha20_KEYBYTES); +} + +static inline void +sn_crypto_stream_chacha20_xor_wrap_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m +) { + assert(c.size_bytes() == m.size_bytes()); + + auto *next_block = state->next_block; + + size_t m_size = m.size_bytes(); + auto *c_ptr = c.data(); + auto *m_ptr = m.data(); + + if (state->remainder) { + uint64_t offset = 0; + int rem = state->remainder; + + while (rem < 64 && offset < m_size) { + c_ptr[offset] = next_block[rem] ^ m_ptr[offset]; + offset++; + rem++; + } + + c_ptr += offset; + m_ptr += offset; + m_size -= offset; + state->remainder = (rem == 64) ? 0 : rem; + + if (m_size == 0) return; + } + + state->remainder = m_size & 63; + size_t main_len = m_size - state->remainder; + + crypto_stream_chacha20_xor_ic( + c_ptr, m_ptr, main_len, state->n, state->block_counter, state->k + ); + + state->block_counter += main_len / 64; + + if (state->remainder) { + sodium_memzero(next_block + state->remainder, 64 - state->remainder); + memcpy(next_block, m_ptr + main_len, state->remainder); + + crypto_stream_chacha20_xor_ic( + next_block, next_block, 64, state->n, state->block_counter, state->k + ); + memcpy(c_ptr + main_len, next_block, state->remainder); + + state->block_counter++; + } +} + +static inline void +sn_crypto_stream_chacha20_xor_wrap_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + sodium_memzero(state->n, sizeof(state->n)); + sodium_memzero(state->k, sizeof(state->k)); + sodium_memzero(state->next_block, sizeof(state->next_block)); + state->remainder = 0; +} + +struct sn_crypto_stream_chacha20_ietf_xor_state { + unsigned char n[crypto_stream_chacha20_ietf_NONCEBYTES]; + unsigned char k[crypto_stream_chacha20_ietf_KEYBYTES]; + unsigned char next_block[64]; + int remainder; + uint64_t block_counter; +}; + +static inline void +sn_crypto_stream_chacha20_ietf_xor_wrap_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_chacha20_ietf_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_chacha20_ietf_KEYBYTES); + + state->remainder = 0; + state->block_counter = 0; + memcpy(state->n, n.data(), crypto_stream_chacha20_ietf_NONCEBYTES); + memcpy(state->k, k.data(), crypto_stream_chacha20_ietf_KEYBYTES); +} + +static inline void +sn_crypto_stream_chacha20_ietf_xor_wrap_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m +) { + assert(c.size_bytes() == m.size_bytes()); + + auto *next_block = state->next_block; + + size_t m_size = m.size_bytes(); + auto *c_ptr = c.data(); + auto *m_ptr = m.data(); + + if (state->remainder) { + uint64_t offset = 0; + int rem = state->remainder; + + while (rem < 64 && offset < m_size) { + c_ptr[offset] = next_block[rem] ^ m_ptr[offset]; + offset++; + rem++; + } + + c_ptr += offset; + m_ptr += offset; + m_size -= offset; + state->remainder = (rem == 64) ? 0 : rem; + + if (m_size == 0) return; + } + + state->remainder = m_size & 63; + size_t main_len = m_size - state->remainder; + + crypto_stream_chacha20_ietf_xor_ic( + c_ptr, m_ptr, main_len, state->n, state->block_counter, state->k + ); + + state->block_counter += main_len / 64; + + if (state->remainder) { + sodium_memzero(next_block + state->remainder, 64 - state->remainder); + memcpy(next_block, m_ptr + main_len, state->remainder); + + crypto_stream_chacha20_ietf_xor_ic( + next_block, next_block, 64, state->n, state->block_counter, state->k + ); + memcpy(c_ptr + main_len, next_block, state->remainder); + + state->block_counter++; + } +} + +static inline void +sn_crypto_stream_chacha20_ietf_xor_wrap_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + sodium_memzero(state->n, sizeof(state->n)); + sodium_memzero(state->k, sizeof(state->k)); + sodium_memzero(state->next_block, sizeof(state->next_block)); + state->remainder = 0; +} + +struct sn_crypto_stream_xchacha20_xor_state { + unsigned char n[crypto_stream_xchacha20_NONCEBYTES]; + unsigned char k[crypto_stream_xchacha20_KEYBYTES]; + unsigned char next_block[64]; + int remainder; + uint64_t block_counter; +}; + +static inline void +sn_crypto_stream_xchacha20_xor_wrap_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_xchacha20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_xchacha20_KEYBYTES); + + state->remainder = 0; + state->block_counter = 0; + memcpy(state->n, n.data(), crypto_stream_xchacha20_NONCEBYTES); + memcpy(state->k, k.data(), crypto_stream_xchacha20_KEYBYTES); +} + +static inline void +sn_crypto_stream_xchacha20_xor_wrap_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m +) { + assert(c.size_bytes() == m.size_bytes()); + + auto *next_block = state->next_block; + + size_t m_size = m.size_bytes(); + auto *c_ptr = c.data(); + auto *m_ptr = m.data(); + + if (state->remainder) { + uint64_t offset = 0; + int rem = state->remainder; + + while (rem < 64 && offset < m_size) { + c_ptr[offset] = next_block[rem] ^ m_ptr[offset]; + ++offset; + ++rem; + } + + c_ptr += offset; + m_ptr += offset; + m_size -= offset; + state->remainder = (rem == 64) ? 0 : rem; + + if (m_size == 0) return; + } + + state->remainder = m_size & 63; + size_t main_len = m_size - state->remainder; + + crypto_stream_xchacha20_xor_ic( + c_ptr, m_ptr, main_len, state->n, state->block_counter, state->k + ); + + state->block_counter += main_len / 64; + + if (state->remainder) { + sodium_memzero(next_block + state->remainder, 64 - state->remainder); + memcpy(next_block, m_ptr + main_len, state->remainder); + + crypto_stream_xchacha20_xor_ic( + next_block, next_block, 64, state->n, state->block_counter, state->k + ); + memcpy(c_ptr + main_len, next_block, state->remainder); + + state->block_counter++; + } +} + +static inline void +sn_crypto_stream_xchacha20_xor_wrap_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + sodium_memzero(state->n, sizeof(state->n)); + sodium_memzero(state->k, sizeof(state->k)); + sodium_memzero(state->next_block, sizeof(state->next_block)); + state->remainder = 0; +} + +struct sn_crypto_stream_salsa20_xor_state { + unsigned char n[crypto_stream_salsa20_NONCEBYTES]; + unsigned char k[crypto_stream_salsa20_KEYBYTES]; + unsigned char next_block[64]; + int remainder; + uint64_t block_counter; +}; + +static inline void +sn_crypto_stream_salsa20_xor_wrap_init( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> n, + js_typedarray_span_t<> k +) { + assert(n.size_bytes() == crypto_stream_salsa20_NONCEBYTES); + assert(k.size_bytes() == crypto_stream_salsa20_KEYBYTES); + + state->remainder = 0; + state->block_counter = 0; + memcpy(state->n, n.data(), crypto_stream_salsa20_NONCEBYTES); + memcpy(state->k, k.data(), crypto_stream_salsa20_KEYBYTES); +} + +static inline void +sn_crypto_stream_salsa20_xor_wrap_update( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state, + js_typedarray_span_t<> c, + js_typedarray_span_t<> m +) { + assert(c.size_bytes() == m.size_bytes()); + + auto *next_block = state->next_block; + + size_t m_size = m.size_bytes(); + auto *c_ptr = c.data(); + auto *m_ptr = m.data(); + + if (state->remainder) { + uint64_t offset = 0; + int rem = state->remainder; + + while (rem < 64 && offset < m_size) { + c_ptr[offset] = next_block[rem] ^ m_ptr[offset]; + ++offset; + ++rem; + } + + c_ptr += offset; + m_ptr += offset; + m_size -= offset; + state->remainder = (rem == 64) ? 0 : rem; + + if (m_size == 0) return; + } + + state->remainder = m_size & 63; + size_t main_len = m_size - state->remainder; + + crypto_stream_salsa20_xor_ic( + c_ptr, m_ptr, main_len, state->n, state->block_counter, state->k + ); + + state->block_counter += main_len / 64; + + if (state->remainder) { + sodium_memzero(next_block + state->remainder, 64 - state->remainder); + memcpy(next_block, m_ptr + main_len, state->remainder); + + crypto_stream_salsa20_xor_ic( + next_block, next_block, 64, state->n, state->block_counter, state->k + ); + memcpy(c_ptr + main_len, next_block, state->remainder); + + state->block_counter++; + } +} + +static inline void +sn_crypto_stream_salsa20_xor_wrap_final( + js_env_t *, + js_receiver_t, + js_typedarray_span_of_t state +) { + sodium_memzero(state->n, sizeof(state->n)); + sodium_memzero(state->k, sizeof(state->k)); + sodium_memzero(state->next_block, sizeof(state->next_block)); + state->remainder = 0; +} + +// Experimental API + +static inline void +sn_extension_tweak_ed25519_base( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> n, + js_typedarray_span_t<> p, + js_typedarray_span_t<> ns +) { + assert(n.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(p.size_bytes() == sn__extension_tweak_ed25519_BYTES); + + sn__extension_tweak_ed25519_base(p.data(), n.data(), ns.data(), ns.size_bytes()); +} + +static inline int +sn_extension_tweak_ed25519_sign_detached( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> sig, + js_typedarray_span_t<> m, + js_typedarray_span_t<> scalar, + std::optional> pk +) { + assert(sig.size_bytes() == crypto_sign_BYTES); + assert(scalar.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + + uint8_t *pk_data = nullptr; + if (pk) { + assert(pk->size_bytes() == crypto_sign_PUBLICKEYBYTES); + pk_data = pk->data(); + } + + return sn__extension_tweak_ed25519_sign_detached( + sig.data(), + nullptr, + m.data(), + m.size_bytes(), + scalar.data(), + pk_data + ); +} + +static inline void +sn_extension_tweak_ed25519_sk_to_scalar( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> n, + js_typedarray_span_t<> sk +) { + assert(n.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(sk.size_bytes() == crypto_sign_SECRETKEYBYTES); + + sn__extension_tweak_ed25519_sk_to_scalar(n.data(), sk.data()); +} + +static inline void +sn_extension_tweak_ed25519_scalar( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> scalar_out, + js_typedarray_span_t<> scalar, + js_typedarray_span_t<> ns +) { + assert(scalar_out.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(scalar.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + + sn__extension_tweak_ed25519_scalar(scalar_out.data(), scalar.data(), ns.data(), ns.size_bytes()); +} + +static inline int +sn_extension_tweak_ed25519_pk( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> tpk, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> ns +) { + assert(tpk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + + return sn__extension_tweak_ed25519_pk(tpk.data(), pk.data(), ns.data(), ns.size_bytes()); +} + +static inline void +sn_extension_tweak_ed25519_keypair( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> scalar_out, + js_typedarray_span_t<> scalar_in, + js_typedarray_span_t<> ns +) { + assert(pk.size_bytes() == sn__extension_tweak_ed25519_BYTES); + assert(scalar_out.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(scalar_in.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + + sn__extension_tweak_ed25519_keypair( + pk.data(), + scalar_out.data(), + scalar_in.data(), + ns.data(), + ns.size_bytes() + ); +} + +static inline void +sn_extension_tweak_ed25519_scalar_add( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> scalar_out, + js_typedarray_span_t<> scalar, + js_typedarray_span_t<> n +) { + assert(scalar_out.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(scalar.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(n.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + + sn__extension_tweak_ed25519_scalar_add(scalar_out.data(), scalar.data(), n.data()); +} + +static inline int +sn_extension_tweak_ed25519_pk_add( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> tpk, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> p +) { + assert(tpk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(pk.size_bytes() == crypto_sign_PUBLICKEYBYTES); + assert(p.size_bytes() == crypto_sign_PUBLICKEYBYTES); + + return sn__extension_tweak_ed25519_pk_add(tpk.data(), pk.data(), p.data()); +} + +static inline int +sn_extension_tweak_ed25519_keypair_add( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> pk, + js_typedarray_span_t<> scalar_out, + js_typedarray_span_t<> scalar_in, + js_typedarray_span_t<> tweak +) { + assert(pk.size_bytes() == sn__extension_tweak_ed25519_BYTES); + assert(scalar_out.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(scalar_in.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + assert(tweak.size_bytes() == sn__extension_tweak_ed25519_SCALARBYTES); + + return sn__extension_tweak_ed25519_keypair_add( + pk.data(), + scalar_out.data(), + scalar_in.data(), + tweak.data() + ); +} + +static inline int +sn_extension_pbkdf2_sha512( + js_env_t *, + js_receiver_t, + js_typedarray_span_t<> out, + js_typedarray_span_t<> passwd, + js_typedarray_span_t<> salt, + uint64_t iter, + uint64_t outlen +) { + + assert(iter >= sn__extension_pbkdf2_sha512_ITERATIONS_MIN); + assert(outlen <= sn__extension_pbkdf2_sha512_BYTES_MAX); + assert(out.size_bytes() >= static_cast(outlen)); + + return sn__extension_pbkdf2_sha512( + passwd.data(), + passwd.size_bytes(), + salt.data(), + salt.size_bytes(), + iter, + out.data(), + outlen + ); +} + +struct sn_async_pbkdf2_sha512_request : sn_async_task { + js_persistent_t out_ref; + std::span out; + + size_t outlen; + + js_persistent_t pwd_ref; + std::span pwd; + + js_persistent_t salt_ref; + std::span salt; + + uint64_t iter; + + sn_async_pbkdf2_sha512_request(js_env_t *env) : sn_async_task(env) {} +}; + +static void +async_pbkdf2_sha512_execute(uv_work_t *uv_req) { + auto *req = reinterpret_cast(uv_req); + + req->code = sn__extension_pbkdf2_sha512( + req->pwd.data(), + req->pwd.size(), + req->salt.data(), + req->salt.size(), + req->iter, + req->out.data(), + req->outlen + ); +} + +static void +async_pbkdf2_sha512_complete(uv_work_t *uv_req, int status) { + int err; + + auto *req = reinterpret_cast(uv_req); + + if (req->exiting) return delete req; + + js_handle_scope_t *scope; + err = js_open_handle_scope(req->env, &scope); + assert(err == 0); + + js_function_t callback; + err = js_get_reference_value(req->env, req->cb, callback); + assert(err == 0); + + err = js_call_function_with_checkpoint(req->env, callback, req->code); + assert(err != js_pending_exception); + + err = js_close_handle_scope(req->env, scope); + assert(err == 0); + + delete req; +} + +static inline void +sn_extension_pbkdf2_sha512_async( + js_env_t *env, + js_receiver_t, + + js_arraybuffer_t out, + uint32_t out_offset, + uint32_t out_len, + + js_arraybuffer_t pwd, + uint32_t pwd_offset, + uint32_t pwd_len, + + js_arraybuffer_t salt, + uint32_t salt_offset, + uint32_t salt_len, + + uint64_t iter, + uint64_t outlen, + + js_function_t callback +) { + int err; + + auto *req = new sn_async_pbkdf2_sha512_request(env); + + std::span out_view; + err = js_get_arraybuffer_info(env, out, out_view); + assert(err == 0); + assert(out_offset + out_len <= out_view.size()); + + req->out = {&out_view[out_offset], out_len}; + + std::span pwd_view; + err = js_get_arraybuffer_info(env, pwd, pwd_view); + assert(err == 0); + assert(pwd_offset + pwd_len <= pwd_view.size()); + + req->pwd = {&pwd_view[pwd_offset], pwd_len}; + + std::span salt_view; + err = js_get_arraybuffer_info(env, salt, salt_view); + assert(err == 0); + assert(salt_offset + salt_len <= salt_view.size()); + + req->salt = {&salt_view[salt_offset], salt_len}; + + req->iter = iter; + req->outlen = outlen; + + err = js_create_reference(env, out, req->out_ref); + assert(err == 0); + + err = js_create_reference(env, pwd, req->pwd_ref); + assert(err == 0); + + err = js_create_reference(env, salt, req->salt_ref); + assert(err == 0); + + err = js_create_reference(env, callback, req->cb); + assert(err == 0); + + uv_loop_t *loop; + err = js_get_env_loop(env, &loop); + assert(err == 0); + + err = uv_queue_work(loop, &req->task, async_pbkdf2_sha512_execute, async_pbkdf2_sha512_complete); + assert(err == 0); +} + +static js_value_t * +sodium_native_exports(js_env_t *env, js_value_t *exports) { + int err; + + err = sodium_init(); + assert(err >= 0); + +#define V_FUNCTION(name, fn) \ + err = js_set_property(env, exports, name); \ + assert(err == 0); + +#define V_FUNCTION_NOSCOPE(name, fn) \ + err = js_set_property(env, exports, name); \ + assert(err == 0); + +#define V_UINT32(name, constant) \ + err = js_set_property(env, exports, name, static_cast(constant)); \ + assert(err == 0); + +#define V_UINT64(name, constant) \ + err = js_set_property(env, exports, name, static_cast(std::min(constant, js_max_safe_integer))); \ + assert(err == 0); + +#define V_STRING(name, str) \ + err = js_set_property(env, exports, name, str); \ + assert(err == 0); + + // memory + + V_FUNCTION("sodium_memzero", sn_sodium_memzero); + V_FUNCTION("sodium_mlock", sn_sodium_mlock); + V_FUNCTION("sodium_munlock", sn_sodium_munlock); + V_FUNCTION("sodium_malloc", sn_sodium_malloc); + V_FUNCTION("sodium_free", sn_sodium_free); + V_FUNCTION("sodium_mprotect_noaccess", sn_sodium_mprotect_noaccess); + V_FUNCTION("sodium_mprotect_readonly", sn_sodium_mprotect_readonly); + V_FUNCTION("sodium_mprotect_readwrite", sn_sodium_mprotect_readwrite); + + // randombytes + + V_FUNCTION_NOSCOPE("randombytes_buf", sn_randombytes_buf); + V_FUNCTION_NOSCOPE("randombytes_buf_deterministic", sn_randombytes_buf_deterministic); + V_FUNCTION_NOSCOPE("randombytes_random", sn_randombytes_random); + V_FUNCTION_NOSCOPE("randombytes_uniform", sn_randombytes_uniform); + + V_UINT32("randombytes_SEEDBYTES", randombytes_SEEDBYTES); + + // sodium helpers + + V_FUNCTION("sodium_memcmp", sn_sodium_memcmp); + V_FUNCTION("sodium_increment", sn_sodium_increment); + V_FUNCTION("sodium_add", sn_sodium_add); + V_FUNCTION("sodium_sub", sn_sodium_sub); + V_FUNCTION("sodium_compare", sn_sodium_compare); + V_FUNCTION("sodium_is_zero", sn_sodium_is_zero); + V_FUNCTION("sodium_pad", sn_sodium_pad); + V_FUNCTION("sodium_unpad", sn_sodium_unpad); + + // crypto_aead + + V_FUNCTION("crypto_aead_xchacha20poly1305_ietf_keygen", sn_crypto_aead_xchacha20poly1305_ietf_keygen); + V_FUNCTION("crypto_aead_xchacha20poly1305_ietf_encrypt", sn_crypto_aead_xchacha20poly1305_ietf_encrypt); + V_FUNCTION("crypto_aead_xchacha20poly1305_ietf_decrypt", sn_crypto_aead_xchacha20poly1305_ietf_decrypt); + V_FUNCTION("crypto_aead_xchacha20poly1305_ietf_encrypt_detached", sn_crypto_aead_xchacha20poly1305_ietf_encrypt_detached); + V_FUNCTION("crypto_aead_xchacha20poly1305_ietf_decrypt_detached", sn_crypto_aead_xchacha20poly1305_ietf_decrypt_detached); + V_UINT32("crypto_aead_xchacha20poly1305_ietf_ABYTES", crypto_aead_xchacha20poly1305_ietf_ABYTES); + V_UINT32("crypto_aead_xchacha20poly1305_ietf_KEYBYTES", crypto_aead_xchacha20poly1305_ietf_KEYBYTES); + V_UINT32("crypto_aead_xchacha20poly1305_ietf_NPUBBYTES", crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); + V_UINT32("crypto_aead_xchacha20poly1305_ietf_NSECBYTES", crypto_aead_xchacha20poly1305_ietf_NSECBYTES); + V_UINT64("crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX", crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX); + + V_FUNCTION("crypto_aead_chacha20poly1305_ietf_keygen", sn_crypto_aead_chacha20poly1305_ietf_keygen); + V_FUNCTION("crypto_aead_chacha20poly1305_ietf_encrypt", sn_crypto_aead_chacha20poly1305_ietf_encrypt); + V_FUNCTION("crypto_aead_chacha20poly1305_ietf_decrypt", sn_crypto_aead_chacha20poly1305_ietf_decrypt); + V_FUNCTION("crypto_aead_chacha20poly1305_ietf_encrypt_detached", sn_crypto_aead_chacha20poly1305_ietf_encrypt_detached); + V_FUNCTION("crypto_aead_chacha20poly1305_ietf_decrypt_detached", sn_crypto_aead_chacha20poly1305_ietf_decrypt_detached); + V_UINT32("crypto_aead_chacha20poly1305_ietf_ABYTES", crypto_aead_chacha20poly1305_ietf_ABYTES); + V_UINT32("crypto_aead_chacha20poly1305_ietf_KEYBYTES", crypto_aead_chacha20poly1305_ietf_KEYBYTES); + V_UINT32("crypto_aead_chacha20poly1305_ietf_NPUBBYTES", crypto_aead_chacha20poly1305_ietf_NPUBBYTES); + V_UINT32("crypto_aead_chacha20poly1305_ietf_NSECBYTES", crypto_aead_chacha20poly1305_ietf_NSECBYTES); + V_UINT64("crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX", crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX); + + // crypto_auth + + V_FUNCTION("crypto_auth", sn_crypto_auth); + V_FUNCTION("crypto_auth_verify", sn_crypto_auth_verify); + V_UINT32("crypto_auth_BYTES", crypto_auth_BYTES); + V_UINT32("crypto_auth_KEYBYTES", crypto_auth_KEYBYTES); + V_STRING("crypto_auth_PRIMITIVE", crypto_auth_PRIMITIVE); + + // crypto_box + + V_FUNCTION("crypto_box_keypair", sn_crypto_box_keypair); + V_FUNCTION("crypto_box_seed_keypair", sn_crypto_box_seed_keypair); + V_FUNCTION("crypto_box_easy", sn_crypto_box_easy); + V_FUNCTION("crypto_box_open_easy", sn_crypto_box_open_easy); + V_FUNCTION("crypto_box_detached", sn_crypto_box_detached); + V_FUNCTION("crypto_box_open_detached", sn_crypto_box_open_detached); + V_FUNCTION("crypto_box_seal", sn_crypto_box_seal); + V_FUNCTION_NOSCOPE("crypto_box_seal_open", sn_crypto_box_seal_open); + + V_UINT32("crypto_box_SEEDBYTES", crypto_box_SEEDBYTES); + V_UINT32("crypto_box_PUBLICKEYBYTES", crypto_box_PUBLICKEYBYTES); + V_UINT32("crypto_box_SECRETKEYBYTES", crypto_box_SECRETKEYBYTES); + V_UINT32("crypto_box_NONCEBYTES", crypto_box_NONCEBYTES); + V_UINT32("crypto_box_MACBYTES", crypto_box_MACBYTES); + V_UINT32("crypto_box_SEALBYTES", crypto_box_SEALBYTES); + V_STRING("crypto_box_PRIMITIVE", crypto_box_PRIMITIVE); + + // crypto_core + + V_FUNCTION("crypto_core_ed25519_is_valid_point", sn_crypto_core_ed25519_is_valid_point); + V_FUNCTION("crypto_core_ed25519_from_uniform", sn_crypto_core_ed25519_from_uniform); + V_FUNCTION("crypto_core_ed25519_add", sn_crypto_core_ed25519_add); + V_FUNCTION("crypto_core_ed25519_sub", sn_crypto_core_ed25519_sub); + V_FUNCTION("crypto_core_ed25519_scalar_random", sn_crypto_core_ed25519_scalar_random); + V_FUNCTION("crypto_core_ed25519_scalar_reduce", sn_crypto_core_ed25519_scalar_reduce); + V_FUNCTION("crypto_core_ed25519_scalar_invert", sn_crypto_core_ed25519_scalar_invert); + V_FUNCTION("crypto_core_ed25519_scalar_negate", sn_crypto_core_ed25519_scalar_negate); + V_FUNCTION("crypto_core_ed25519_scalar_complement", sn_crypto_core_ed25519_scalar_complement); + V_FUNCTION("crypto_core_ed25519_scalar_add", sn_crypto_core_ed25519_scalar_add); + V_FUNCTION("crypto_core_ed25519_scalar_sub", sn_crypto_core_ed25519_scalar_sub); + V_UINT32("crypto_core_ed25519_BYTES", crypto_core_ed25519_BYTES); + V_UINT32("crypto_core_ed25519_UNIFORMBYTES", crypto_core_ed25519_UNIFORMBYTES); + V_UINT32("crypto_core_ed25519_SCALARBYTES", crypto_core_ed25519_SCALARBYTES); + V_UINT32("crypto_core_ed25519_NONREDUCEDSCALARBYTES", crypto_core_ed25519_NONREDUCEDSCALARBYTES); + + // crypto_kdf + + V_FUNCTION("crypto_kdf_keygen", sn_crypto_kdf_keygen); + V_FUNCTION("crypto_kdf_derive_from_key", sn_crypto_kdf_derive_from_key); + V_UINT32("crypto_kdf_BYTES_MIN", crypto_kdf_BYTES_MIN); + V_UINT32("crypto_kdf_BYTES_MAX", crypto_kdf_BYTES_MAX); + V_UINT32("crypto_kdf_CONTEXTBYTES", crypto_kdf_CONTEXTBYTES); + V_UINT32("crypto_kdf_KEYBYTES", crypto_kdf_KEYBYTES); + V_STRING("crypto_kdf_PRIMITIVE", crypto_kdf_PRIMITIVE); + + // crypto_kx + + V_FUNCTION("crypto_kx_keypair", sn_crypto_kx_keypair); + V_FUNCTION("crypto_kx_seed_keypair", sn_crypto_kx_seed_keypair); + V_FUNCTION("crypto_kx_client_session_keys", sn_crypto_kx_client_session_keys); + V_FUNCTION("crypto_kx_server_session_keys", sn_crypto_kx_server_session_keys); + V_UINT32("crypto_kx_PUBLICKEYBYTES", crypto_kx_PUBLICKEYBYTES); + V_UINT32("crypto_kx_SECRETKEYBYTES", crypto_kx_SECRETKEYBYTES); + V_UINT32("crypto_kx_SEEDBYTES", crypto_kx_SEEDBYTES); + V_UINT32("crypto_kx_SESSIONKEYBYTES", crypto_kx_SESSIONKEYBYTES); + V_STRING("crypto_kx_PRIMITIVE", crypto_kx_PRIMITIVE); + + // crypto_generichash + + V_FUNCTION_NOSCOPE("crypto_generichash", sn_crypto_generichash); + V_FUNCTION("crypto_generichash_batch", sn_crypto_generichash_batch); + V_FUNCTION_NOSCOPE("crypto_generichash_keygen", sn_crypto_generichash_keygen); + V_FUNCTION_NOSCOPE("crypto_generichash_init", sn_crypto_generichash_init); + V_FUNCTION_NOSCOPE("crypto_generichash_update", sn_crypto_generichash_update); + V_FUNCTION_NOSCOPE("crypto_generichash_final", sn_crypto_generichash_final); + + V_UINT32("crypto_generichash_STATEBYTES", sizeof(crypto_generichash_state)); + V_STRING("crypto_generichash_PRIMITIVE", crypto_generichash_PRIMITIVE); + V_UINT32("crypto_generichash_BYTES_MIN", crypto_generichash_BYTES_MIN); + V_UINT32("crypto_generichash_BYTES_MAX", crypto_generichash_BYTES_MAX); + V_UINT32("crypto_generichash_BYTES", crypto_generichash_BYTES); + V_UINT32("crypto_generichash_KEYBYTES_MIN", crypto_generichash_KEYBYTES_MIN); + V_UINT32("crypto_generichash_KEYBYTES_MAX", crypto_generichash_KEYBYTES_MAX); + V_UINT32("crypto_generichash_KEYBYTES", crypto_generichash_KEYBYTES); + + // crypto_hash + + V_FUNCTION("crypto_hash", sn_crypto_hash); + V_UINT32("crypto_hash_BYTES", crypto_hash_BYTES); + V_STRING("crypto_hash_PRIMITIVE", crypto_hash_PRIMITIVE); + + V_FUNCTION("crypto_hash_sha256", sn_crypto_hash_sha256); + V_FUNCTION("crypto_hash_sha256_init", sn_crypto_hash_sha256_init); + V_FUNCTION("crypto_hash_sha256_update", sn_crypto_hash_sha256_update); + V_FUNCTION("crypto_hash_sha256_final", sn_crypto_hash_sha256_final); + V_UINT32("crypto_hash_sha256_STATEBYTES", sizeof(crypto_hash_sha256_state)); + V_UINT32("crypto_hash_sha256_BYTES", crypto_hash_sha256_BYTES); + + V_FUNCTION("crypto_hash_sha512", sn_crypto_hash_sha512); + V_FUNCTION("crypto_hash_sha512_init", sn_crypto_hash_sha512_init); + V_FUNCTION("crypto_hash_sha512_update", sn_crypto_hash_sha512_update); + V_FUNCTION("crypto_hash_sha512_final", sn_crypto_hash_sha512_final); + V_UINT32("crypto_hash_sha512_STATEBYTES", sizeof(crypto_hash_sha512_state)); + V_UINT32("crypto_hash_sha512_BYTES", crypto_hash_sha512_BYTES); + + // crypto_onetimeauth + + V_FUNCTION("crypto_onetimeauth", sn_crypto_onetimeauth); + V_FUNCTION("crypto_onetimeauth_verify", sn_crypto_onetimeauth_verify); + V_FUNCTION("crypto_onetimeauth_init", sn_crypto_onetimeauth_init); + V_FUNCTION("crypto_onetimeauth_update", sn_crypto_onetimeauth_update); + V_FUNCTION("crypto_onetimeauth_final", sn_crypto_onetimeauth_final); + V_UINT32("crypto_onetimeauth_STATEBYTES", sizeof(crypto_onetimeauth_state)); + V_UINT32("crypto_onetimeauth_BYTES", crypto_onetimeauth_BYTES); + V_UINT32("crypto_onetimeauth_KEYBYTES", crypto_onetimeauth_KEYBYTES); + V_STRING("crypto_onetimeauth_PRIMITIVE", crypto_onetimeauth_PRIMITIVE); + + // crypto_pwhash + + V_FUNCTION("crypto_pwhash", sn_crypto_pwhash); + V_FUNCTION("crypto_pwhash_str", sn_crypto_pwhash_str); + V_FUNCTION("crypto_pwhash_str_verify", sn_crypto_pwhash_str_verify); + V_FUNCTION("crypto_pwhash_str_needs_rehash", sn_crypto_pwhash_str_needs_rehash); + V_FUNCTION("crypto_pwhash_async", sn_crypto_pwhash_async); + V_FUNCTION("crypto_pwhash_str_async", sn_crypto_pwhash_str_async); + V_FUNCTION("crypto_pwhash_str_verify_async", sn_crypto_pwhash_str_verify_async); + V_UINT32("crypto_pwhash_ALG_ARGON2I13", crypto_pwhash_ALG_ARGON2I13); + V_UINT32("crypto_pwhash_ALG_ARGON2ID13", crypto_pwhash_ALG_ARGON2ID13); + V_UINT32("crypto_pwhash_ALG_DEFAULT", crypto_pwhash_ALG_DEFAULT); + V_UINT32("crypto_pwhash_BYTES_MIN", crypto_pwhash_BYTES_MIN); + V_UINT32("crypto_pwhash_BYTES_MAX", crypto_pwhash_BYTES_MAX); + V_UINT32("crypto_pwhash_PASSWD_MIN", crypto_pwhash_PASSWD_MIN); + V_UINT32("crypto_pwhash_PASSWD_MAX", crypto_pwhash_PASSWD_MAX); + V_UINT32("crypto_pwhash_SALTBYTES", crypto_pwhash_SALTBYTES); + V_UINT32("crypto_pwhash_STRBYTES", crypto_pwhash_STRBYTES); + V_STRING("crypto_pwhash_STRPREFIX", crypto_pwhash_STRPREFIX); + V_UINT32("crypto_pwhash_OPSLIMIT_MIN", crypto_pwhash_OPSLIMIT_MIN); + V_UINT32("crypto_pwhash_OPSLIMIT_MAX", crypto_pwhash_OPSLIMIT_MAX); + V_UINT64("crypto_pwhash_MEMLIMIT_MIN", crypto_pwhash_MEMLIMIT_MIN); + V_UINT64("crypto_pwhash_MEMLIMIT_MAX", crypto_pwhash_MEMLIMIT_MAX); + V_UINT32("crypto_pwhash_OPSLIMIT_INTERACTIVE", crypto_pwhash_OPSLIMIT_INTERACTIVE); + V_UINT64("crypto_pwhash_MEMLIMIT_INTERACTIVE", crypto_pwhash_MEMLIMIT_INTERACTIVE); + V_UINT32("crypto_pwhash_OPSLIMIT_MODERATE", crypto_pwhash_OPSLIMIT_MODERATE); + V_UINT64("crypto_pwhash_MEMLIMIT_MODERATE", crypto_pwhash_MEMLIMIT_MODERATE); + V_UINT32("crypto_pwhash_OPSLIMIT_SENSITIVE", crypto_pwhash_OPSLIMIT_SENSITIVE); + V_UINT64("crypto_pwhash_MEMLIMIT_SENSITIVE", crypto_pwhash_MEMLIMIT_SENSITIVE); + V_STRING("crypto_pwhash_PRIMITIVE", crypto_pwhash_PRIMITIVE); + + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256", sn_crypto_pwhash_scryptsalsa208sha256); + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_str", sn_crypto_pwhash_scryptsalsa208sha256_str); + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_str_verify", sn_crypto_pwhash_scryptsalsa208sha256_str_verify); + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_str_needs_rehash", sn_crypto_pwhash_scryptsalsa208sha256_str_needs_rehash); + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_async", sn_crypto_pwhash_scryptsalsa208sha256_async); + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_str_async", sn_crypto_pwhash_scryptsalsa208sha256_str_async) + V_FUNCTION("crypto_pwhash_scryptsalsa208sha256_str_verify_async", sn_crypto_pwhash_scryptsalsa208sha256_str_verify_async); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_BYTES_MIN", crypto_pwhash_scryptsalsa208sha256_BYTES_MIN); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_BYTES_MAX", crypto_pwhash_scryptsalsa208sha256_BYTES_MAX); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN", crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX", crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_SALTBYTES", crypto_pwhash_scryptsalsa208sha256_SALTBYTES); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_STRBYTES", crypto_pwhash_scryptsalsa208sha256_STRBYTES); + V_STRING("crypto_pwhash_scryptsalsa208sha256_STRPREFIX", crypto_pwhash_scryptsalsa208sha256_STRPREFIX); + V_UINT32("crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN); + V_UINT32("crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX); + V_UINT32("crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); + V_UINT32("crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE", crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE); + V_UINT64("crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE", crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE); + + // crypto_scalarmult + + V_FUNCTION("crypto_scalarmult_base", sn_crypto_scalarmult_base); + V_FUNCTION("crypto_scalarmult", sn_crypto_scalarmult); + V_STRING("crypto_scalarmult_PRIMITIVE", crypto_scalarmult_PRIMITIVE); + V_UINT32("crypto_scalarmult_BYTES", crypto_scalarmult_BYTES); + V_UINT32("crypto_scalarmult_SCALARBYTES", crypto_scalarmult_SCALARBYTES); + + V_FUNCTION("crypto_scalarmult_ed25519_base", sn_crypto_scalarmult_ed25519_base); + V_FUNCTION("crypto_scalarmult_ed25519", sn_crypto_scalarmult_ed25519); + V_FUNCTION("crypto_scalarmult_ed25519_base_noclamp", sn_crypto_scalarmult_ed25519_base_noclamp); + V_FUNCTION("crypto_scalarmult_ed25519_noclamp", sn_crypto_scalarmult_ed25519_noclamp); + V_UINT32("crypto_scalarmult_ed25519_BYTES", crypto_scalarmult_ed25519_BYTES); + V_UINT32("crypto_scalarmult_ed25519_SCALARBYTES", crypto_scalarmult_ed25519_SCALARBYTES); + + // crypto_secretbox + + V_FUNCTION("crypto_secretbox_easy", sn_crypto_secretbox_easy); + V_FUNCTION("crypto_secretbox_open_easy", sn_crypto_secretbox_open_easy); + V_FUNCTION("crypto_secretbox_detached", sn_crypto_secretbox_detached); + V_FUNCTION("crypto_secretbox_open_detached", sn_crypto_secretbox_open_detached); + V_UINT32("crypto_secretbox_KEYBYTES", crypto_secretbox_KEYBYTES); + V_UINT32("crypto_secretbox_NONCEBYTES", crypto_secretbox_NONCEBYTES); + V_UINT32("crypto_secretbox_MACBYTES", crypto_secretbox_MACBYTES); + V_STRING("crypto_secretbox_PRIMITIVE", crypto_secretbox_PRIMITIVE); + + // crypto_secretstream + + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_keygen", sn_crypto_secretstream_xchacha20poly1305_keygen); + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_init_push", sn_crypto_secretstream_xchacha20poly1305_init_push); + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_init_pull", sn_crypto_secretstream_xchacha20poly1305_init_pull); + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_push", sn_crypto_secretstream_xchacha20poly1305_push); + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_pull", sn_crypto_secretstream_xchacha20poly1305_pull); + V_FUNCTION_NOSCOPE("crypto_secretstream_xchacha20poly1305_rekey", sn_crypto_secretstream_xchacha20poly1305_rekey); + + V_UINT32("crypto_secretstream_xchacha20poly1305_STATEBYTES", sizeof(crypto_secretstream_xchacha20poly1305_state)); + V_UINT32("crypto_secretstream_xchacha20poly1305_ABYTES", crypto_secretstream_xchacha20poly1305_ABYTES); + V_UINT32("crypto_secretstream_xchacha20poly1305_HEADERBYTES", crypto_secretstream_xchacha20poly1305_HEADERBYTES); + V_UINT32("crypto_secretstream_xchacha20poly1305_KEYBYTES", crypto_secretstream_xchacha20poly1305_KEYBYTES); + V_UINT32("crypto_secretstream_xchacha20poly1305_TAGBYTES", 1); + V_UINT64("crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX", crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX); + V_UINT32("crypto_secretstream_xchacha20poly1305_TAG_MESSAGE", crypto_secretstream_xchacha20poly1305_TAG_MESSAGE); + V_UINT32("crypto_secretstream_xchacha20poly1305_TAG_PUSH", crypto_secretstream_xchacha20poly1305_TAG_PUSH); + V_UINT32("crypto_secretstream_xchacha20poly1305_TAG_REKEY", crypto_secretstream_xchacha20poly1305_TAG_REKEY); + V_UINT32("crypto_secretstream_xchacha20poly1305_TAG_FINAL", crypto_secretstream_xchacha20poly1305_TAG_FINAL); + + // crypto_shorthash + + V_FUNCTION("crypto_shorthash", sn_crypto_shorthash); + V_UINT32("crypto_shorthash_BYTES", crypto_shorthash_BYTES); + V_UINT32("crypto_shorthash_KEYBYTES", crypto_shorthash_KEYBYTES); + V_STRING("crypto_shorthash_PRIMITIVE", crypto_shorthash_PRIMITIVE); + + // crypto_sign + + V_FUNCTION("crypto_sign_keypair", sn_crypto_sign_keypair); + V_FUNCTION("crypto_sign_seed_keypair", sn_crypto_sign_seed_keypair); + V_FUNCTION("crypto_sign", sn_crypto_sign); + V_FUNCTION("crypto_sign_open", sn_crypto_sign_open); + V_FUNCTION("crypto_sign_detached", sn_crypto_sign_detached); + V_FUNCTION_NOSCOPE("crypto_sign_verify_detached", sn_crypto_sign_verify_detached); + V_FUNCTION("crypto_sign_ed25519_sk_to_pk", sn_crypto_sign_ed25519_sk_to_pk); + V_FUNCTION("crypto_sign_ed25519_pk_to_curve25519", sn_crypto_sign_ed25519_pk_to_curve25519); + V_FUNCTION("crypto_sign_ed25519_sk_to_curve25519", sn_crypto_sign_ed25519_sk_to_curve25519); + + V_UINT32("crypto_sign_SEEDBYTES", crypto_sign_SEEDBYTES); + V_UINT32("crypto_sign_PUBLICKEYBYTES", crypto_sign_PUBLICKEYBYTES); + V_UINT32("crypto_sign_SECRETKEYBYTES", crypto_sign_SECRETKEYBYTES); + V_UINT32("crypto_sign_BYTES", crypto_sign_BYTES); + + // crypto_stream + + V_FUNCTION("crypto_stream", sn_crypto_stream); + V_UINT32("crypto_stream_KEYBYTES", crypto_stream_KEYBYTES); + V_UINT32("crypto_stream_NONCEBYTES", crypto_stream_NONCEBYTES); + V_STRING("crypto_stream_PRIMITIVE", crypto_stream_PRIMITIVE); + + V_FUNCTION_NOSCOPE("crypto_stream_xor", sn_crypto_stream_xor); + V_FUNCTION("crypto_stream_xor_init", sn_crypto_stream_xor_wrap_init); + V_FUNCTION("crypto_stream_xor_update", sn_crypto_stream_xor_wrap_update); + V_FUNCTION("crypto_stream_xor_final", sn_crypto_stream_xor_wrap_final); + V_UINT32("crypto_stream_xor_STATEBYTES", sizeof(sn_crypto_stream_xor_state)); + + V_FUNCTION("crypto_stream_chacha20", sn_crypto_stream_chacha20); + V_UINT32("crypto_stream_chacha20_KEYBYTES", crypto_stream_chacha20_KEYBYTES); + V_UINT32("crypto_stream_chacha20_NONCEBYTES", crypto_stream_chacha20_NONCEBYTES); + V_UINT64("crypto_stream_chacha20_MESSAGEBYTES_MAX", crypto_stream_chacha20_MESSAGEBYTES_MAX); + + V_FUNCTION("crypto_stream_chacha20_xor", sn_crypto_stream_chacha20_xor); + V_FUNCTION("crypto_stream_chacha20_xor_ic", sn_crypto_stream_chacha20_xor_ic); + V_FUNCTION("crypto_stream_chacha20_xor_init", sn_crypto_stream_chacha20_xor_wrap_init); + V_FUNCTION("crypto_stream_chacha20_xor_update", sn_crypto_stream_chacha20_xor_wrap_update); + V_FUNCTION("crypto_stream_chacha20_xor_final", sn_crypto_stream_chacha20_xor_wrap_final); + V_UINT32("crypto_stream_chacha20_xor_STATEBYTES", sizeof(sn_crypto_stream_chacha20_xor_state)); + + V_FUNCTION("crypto_stream_chacha20_ietf", sn_crypto_stream_chacha20_ietf); + V_UINT32("crypto_stream_chacha20_ietf_KEYBYTES", crypto_stream_chacha20_ietf_KEYBYTES); + V_UINT32("crypto_stream_chacha20_ietf_NONCEBYTES", crypto_stream_chacha20_ietf_NONCEBYTES); + V_UINT64("crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX", crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX); + V_UINT32("crypto_stream_chacha20_ietf_xor_STATEBYTES", sizeof(sn_crypto_stream_chacha20_ietf_xor_state)); + + V_FUNCTION("crypto_stream_chacha20_ietf_xor", sn_crypto_stream_chacha20_ietf_xor); + V_FUNCTION("crypto_stream_chacha20_ietf_xor_ic", sn_crypto_stream_chacha20_ietf_xor_ic); + V_FUNCTION("crypto_stream_chacha20_ietf_xor_init", sn_crypto_stream_chacha20_ietf_xor_wrap_init); + V_FUNCTION("crypto_stream_chacha20_ietf_xor_update", sn_crypto_stream_chacha20_ietf_xor_wrap_update); + V_FUNCTION("crypto_stream_chacha20_ietf_xor_final", sn_crypto_stream_chacha20_ietf_xor_wrap_final); + + V_FUNCTION("crypto_stream_xchacha20", sn_crypto_stream_xchacha20); + V_UINT32("crypto_stream_xchacha20_KEYBYTES", crypto_stream_xchacha20_KEYBYTES); + V_UINT32("crypto_stream_xchacha20_NONCEBYTES", crypto_stream_xchacha20_NONCEBYTES); + V_UINT64("crypto_stream_xchacha20_MESSAGEBYTES_MAX", crypto_stream_xchacha20_MESSAGEBYTES_MAX); + + V_FUNCTION("crypto_stream_xchacha20_xor", sn_crypto_stream_xchacha20_xor); + V_FUNCTION("crypto_stream_xchacha20_xor_ic", sn_crypto_stream_xchacha20_xor_ic); + V_FUNCTION("crypto_stream_xchacha20_xor_init", sn_crypto_stream_xchacha20_xor_wrap_init); + V_FUNCTION("crypto_stream_xchacha20_xor_update", sn_crypto_stream_xchacha20_xor_wrap_update); + V_FUNCTION("crypto_stream_xchacha20_xor_final", sn_crypto_stream_xchacha20_xor_wrap_final); + V_FUNCTION("crypto_stream_xchacha20", sn_crypto_stream_xchacha20); + V_UINT32("crypto_stream_xchacha20_xor_STATEBYTES", sizeof(sn_crypto_stream_xchacha20_xor_state)); + + V_FUNCTION("crypto_stream_salsa20", sn_crypto_stream_salsa20); + V_UINT32("crypto_stream_salsa20_KEYBYTES", crypto_stream_salsa20_KEYBYTES); + V_UINT32("crypto_stream_salsa20_NONCEBYTES", crypto_stream_salsa20_NONCEBYTES); + V_UINT64("crypto_stream_salsa20_MESSAGEBYTES_MAX", crypto_stream_salsa20_MESSAGEBYTES_MAX); + + V_FUNCTION("crypto_stream_salsa20_xor", sn_crypto_stream_salsa20_xor); + V_FUNCTION("crypto_stream_salsa20_xor_ic", sn_crypto_stream_salsa20_xor_ic); + V_FUNCTION("crypto_stream_salsa20_xor_init", sn_crypto_stream_salsa20_xor_wrap_init); + V_FUNCTION("crypto_stream_salsa20_xor_update", sn_crypto_stream_salsa20_xor_wrap_update); + V_FUNCTION("crypto_stream_salsa20_xor_final", sn_crypto_stream_salsa20_xor_wrap_final); + V_UINT32("crypto_stream_salsa20_xor_STATEBYTES", sizeof(sn_crypto_stream_salsa20_xor_state)); + + // extensions + + // tweak + + V_FUNCTION("extension_tweak_ed25519_base", sn_extension_tweak_ed25519_base); + V_FUNCTION("extension_tweak_ed25519_sign_detached", sn_extension_tweak_ed25519_sign_detached); + V_FUNCTION("extension_tweak_ed25519_sk_to_scalar", sn_extension_tweak_ed25519_sk_to_scalar); + V_FUNCTION("extension_tweak_ed25519_scalar", sn_extension_tweak_ed25519_scalar); + V_FUNCTION("extension_tweak_ed25519_pk", sn_extension_tweak_ed25519_pk); + V_FUNCTION("extension_tweak_ed25519_keypair", sn_extension_tweak_ed25519_keypair); + V_FUNCTION("extension_tweak_ed25519_scalar_add", sn_extension_tweak_ed25519_scalar_add); + V_FUNCTION("extension_tweak_ed25519_pk_add", sn_extension_tweak_ed25519_pk_add); + V_FUNCTION("extension_tweak_ed25519_keypair_add", sn_extension_tweak_ed25519_keypair_add); + V_UINT32("extension_tweak_ed25519_BYTES", sn__extension_tweak_ed25519_BYTES); + V_UINT32("extension_tweak_ed25519_SCALARBYTES", sn__extension_tweak_ed25519_SCALARBYTES); + + // pbkdf2 + + V_FUNCTION("extension_pbkdf2_sha512", sn_extension_pbkdf2_sha512); + V_FUNCTION("extension_pbkdf2_sha512_async", sn_extension_pbkdf2_sha512_async); + V_UINT32("extension_pbkdf2_sha512_SALTBYTES", sn__extension_pbkdf2_sha512_SALTBYTES); + V_UINT32("extension_pbkdf2_sha512_HASHBYTES", sn__extension_pbkdf2_sha512_HASHBYTES); + V_UINT32("extension_pbkdf2_sha512_ITERATIONS_MIN", sn__extension_pbkdf2_sha512_ITERATIONS_MIN); + V_UINT64("extension_pbkdf2_sha512_BYTES_MAX", sn__extension_pbkdf2_sha512_BYTES_MAX); + +#undef V_FUNCTION +#undef V_FUNCTION_NOSCOPE +#undef V_UINT32 +#undef V_UINT64 +#undef V_STRING + + return exports; +} + +BARE_MODULE(sodium_native, sodium_native_exports) diff --git a/bot/node_modules/sodium-native/binding.js b/bot/node_modules/sodium-native/binding.js new file mode 100644 index 0000000..3e753f9 --- /dev/null +++ b/bot/node_modules/sodium-native/binding.js @@ -0,0 +1,2 @@ +require.addon = require('require-addon') +module.exports = require.addon('.', __filename) diff --git a/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.c b/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.c new file mode 100644 index 0000000..c1bad30 --- /dev/null +++ b/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.c @@ -0,0 +1,90 @@ +/*- + * Copyright 2005,2007,2009 Colin Percival + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Adapted from libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf-sha256.c + */ + +#include +#include + +#include "pbkdf2.h" + +/** + * pbkdf2_sha512(passwd, passwdlen, salt, saltlen, c, buf, dkLen): + * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and + * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). + */ +int +sn__extension_pbkdf2_sha512(const unsigned char *passwd, size_t passwdlen, + const unsigned char *salt, size_t saltlen, uint64_t c, + unsigned char *buf, size_t dkLen) +{ + crypto_auth_hmacsha512_state PShctx, hctx; + size_t i; + unsigned char ivec[4]; + unsigned char U[64]; + unsigned char T[64]; + uint64_t j; + unsigned int k; + size_t clen; + + if (dkLen > sn__extension_pbkdf2_sha512_BYTES_MAX) { + return -1; + } + + crypto_auth_hmacsha512_init(&PShctx, passwd, passwdlen); + crypto_auth_hmacsha512_update(&PShctx, salt, saltlen); + + for (i = 0; i * crypto_auth_hmacsha512_BYTES < dkLen; i++) { + SN_PBKDF2_STORE32_BE(ivec, (uint32_t)(i + 1)); + memcpy(&hctx, &PShctx, sizeof(crypto_auth_hmacsha512_state)); + crypto_auth_hmacsha512_update(&hctx, ivec, 4); + crypto_auth_hmacsha512_final(&hctx, U); + + memcpy(T, U, crypto_auth_hmacsha512_BYTES); + /* LCOV_EXCL_START */ + for (j = 2; j <= c; j++) { + crypto_auth_hmacsha512_init(&hctx, passwd, passwdlen); + crypto_auth_hmacsha512_update(&hctx, U, crypto_auth_hmacsha512_BYTES); + crypto_auth_hmacsha512_final(&hctx, U); + + for (k = 0; k < crypto_auth_hmacsha512_BYTES; k++) { + T[k] ^= U[k]; + } + } + /* LCOV_EXCL_STOP */ + + clen = dkLen - i * crypto_auth_hmacsha512_BYTES; + if (clen > crypto_auth_hmacsha512_BYTES) { + clen = crypto_auth_hmacsha512_BYTES; + } + memcpy(&buf[i * crypto_auth_hmacsha512_BYTES], T, clen); + } + sodium_memzero((void *) &PShctx, sizeof PShctx); + + return 0; +} diff --git a/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.h b/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.h new file mode 100644 index 0000000..f1af48c --- /dev/null +++ b/bot/node_modules/sodium-native/extensions/pbkdf2/pbkdf2.h @@ -0,0 +1,60 @@ +/*- + * Copyright 2005,2007,2009 Colin Percival + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* + * Adapted from libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf-sha256.c + */ +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define SN_PBKDF2_STORE32_BE(buf, n32) \ + buf[0] = n32 >> 24 & 0xff; \ + buf[1] = n32 >> 16 & 0xff; \ + buf[2] = n32 >> 8 & 0xff; \ + buf[3] = n32 >> 0 & 0xff; + +#define sn__extension_pbkdf2_sha512_SALTBYTES 16U + +#define sn__extension_pbkdf2_sha512_HASHBYTES crypto_hash_sha512_BYTES + +#define sn__extension_pbkdf2_sha512_ITERATIONS_MIN 1U + +#define sn__extension_pbkdf2_sha512_BYTES_MAX 0x3fffffffc0ULL + +/** + * extension_pbkdf2_sha512(passwd, passwdlen, salt, saltlen, c, buf, dkLen): + * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and + * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). + */ +int sn__extension_pbkdf2_sha512(const unsigned char *, size_t, const unsigned char *, size_t, + uint64_t, unsigned char *, size_t); +#ifdef __cplusplus +}; +#endif diff --git a/bot/node_modules/sodium-native/extensions/tweak/tweak.c b/bot/node_modules/sodium-native/extensions/tweak/tweak.c new file mode 100644 index 0000000..a4813a0 --- /dev/null +++ b/bot/node_modules/sodium-native/extensions/tweak/tweak.c @@ -0,0 +1,206 @@ +#include "tweak.h" + +/* + *EXPERIMENTAL API* + + This module is an experimental implementation of a key tweaking protocol + over ed25519 keys. The signature algorithm has been reimplemented from + libsodium, but the nonce generation algorithm is *non-standard*. + + Use at your own risk +*/ + +static void _extension_tweak_nonce (unsigned char *nonce, const unsigned char *n, + const unsigned char *m, unsigned long long mlen) +{ + // dom2(x, y) with x = 0 (not prehashed) and y = "crypto_tweak_ed25519" + static const unsigned char TWEAK_PREFIX[32 + 2 + 20] = { + 'S', 'i', 'g', 'E', 'd', '2', '5', '5', '1', '9', ' ', + 'n', 'o', ' ', 'E', 'd', '2', '5', '5', '1', '9', ' ', + 'c', 'o', 'l', 'l', 'i', 's', 'i', 'o', 'n', 's', 0, + 20, 'c', 'r', 'y', 'p', 't', 'o', '_', 't', 'w', 'e', + 'a', 'k', '_', 'e', 'd', '2', '5', '5', '1', '9' + }; + + crypto_hash_sha512_state hs; + + crypto_hash_sha512_init(&hs); + crypto_hash_sha512_update(&hs, TWEAK_PREFIX, sizeof TWEAK_PREFIX); + crypto_hash_sha512_update(&hs, n, 32); + crypto_hash_sha512_update(&hs, m, mlen); + crypto_hash_sha512_final(&hs, nonce); +} + +static inline void +_crypto_sign_ed25519_clamp(unsigned char k[32]) +{ + k[0] &= 248; + k[31] &= 127; + k[31] |= 64; +} + +static void _extension_tweak_ed25519(unsigned char *q, unsigned char *n, + const unsigned char *ns, unsigned long long nslen) +{ + sodium_memzero(q, sizeof q); + + crypto_hash(n, ns, nslen); + n[31] &= 127; // clear highest bit + + crypto_scalarmult_ed25519_base_noclamp(q, n); + + // hash tweak until we get a valid tweaked q + while (crypto_core_ed25519_is_valid_point(q) != 1) { + crypto_hash(n, n, 32); + n[31] &= 127; // clear highest bit + + crypto_scalarmult_ed25519_base_noclamp(q, n); + } +} + +void sn__extension_tweak_ed25519_base(unsigned char *pk, unsigned char *scalar, + const unsigned char *ns, unsigned long long nslen) +{ + unsigned char n64[64]; + + _extension_tweak_ed25519(pk, n64, ns, nslen); + + SN_TWEAK_COPY_32(scalar, n64) +} + +int sn__extension_tweak_ed25519_sign_detached(unsigned char *sig, unsigned long long *siglen_p, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, unsigned char *pk) +{ + crypto_hash_sha512_state hs; + + unsigned char nonce[64]; + unsigned char R[32]; + unsigned char hram[64]; + unsigned char _pk[32]; + + // check if pk was passed + if (pk == NULL) { + pk = _pk; + + // derive pk from scalar + if (crypto_scalarmult_ed25519_base_noclamp(pk, n) != 0) { + return -1; + } + } + + _extension_tweak_nonce(nonce, n, m, mlen); + crypto_core_ed25519_scalar_reduce(nonce, nonce); + + // R = G ^ nonce : curve point from nonce + if (crypto_scalarmult_ed25519_base_noclamp(R, nonce) != 0) { + return -1; + } + + // generate challenge as h(ram) = hash(R, pk, message) + crypto_hash_sha512_init(&hs); + crypto_hash_sha512_update(&hs, R, 32); + crypto_hash_sha512_update(&hs, pk, 32); + crypto_hash_sha512_update(&hs, m, mlen); + + crypto_hash_sha512_final(&hs, hram); + + crypto_core_ed25519_scalar_reduce(hram, hram); + + // sig = nonce + n * h(ram) + crypto_core_ed25519_scalar_mul(sig, hram, n); + crypto_core_ed25519_scalar_add(sig + 32, nonce, sig); + + SN_TWEAK_COPY_32(sig, R) + + if (siglen_p != NULL) { + *siglen_p = 64U; + } + + return 0; +} + +// tweak a secret key +void sn__extension_tweak_ed25519_sk_to_scalar(unsigned char *n, const unsigned char *sk) +{ + unsigned char n64[64]; + + // get sk scalar from seed, cf. crypto_sign_keypair_seed + crypto_hash(n64, sk, 32); + _crypto_sign_ed25519_clamp(n64); + + SN_TWEAK_COPY_32(n, n64) +} + +// tweak a secret key +void sn__extension_tweak_ed25519_scalar(unsigned char *scalar_out, + const unsigned char *scalar, + const unsigned char *ns, + unsigned long long nslen) +{ + unsigned char n[64]; + unsigned char q[32]; + + _extension_tweak_ed25519(q, n, ns, nslen); + crypto_core_ed25519_scalar_add(scalar_out, scalar, n); +} + +// tweak a public key +int sn__extension_tweak_ed25519_pk(unsigned char *tpk, + const unsigned char *pk, + const unsigned char *ns, + unsigned long long nslen) +{ + unsigned char n[64]; + unsigned char q[32]; + + _extension_tweak_ed25519(q, n, ns, nslen); + return crypto_core_ed25519_add(tpk, q, pk); +} + + +void sn__extension_tweak_ed25519_keypair(unsigned char *pk, unsigned char *scalar_out, + unsigned char *scalar, const unsigned char *ns, + unsigned long long nslen) +{ + unsigned char n64[64]; + + crypto_hash(n64, ns, nslen); + n64[31] &= 127; // clear highest bit + + sn__extension_tweak_ed25519_scalar_add(scalar_out, scalar, n64); + crypto_scalarmult_ed25519_base_noclamp(pk, scalar_out); + + // hash tweak until we get a valid tweaked point + while (crypto_core_ed25519_is_valid_point(pk) != 1) { + crypto_hash(n64, n64, 32); + n64[31] &= 127; // clear highest bit + + sn__extension_tweak_ed25519_scalar_add(scalar_out, scalar, n64); + crypto_scalarmult_ed25519_base_noclamp(pk, scalar_out); + } +} + +// add tweak to scalar +void sn__extension_tweak_ed25519_scalar_add(unsigned char *scalar_out, + const unsigned char *scalar, + const unsigned char *n) +{ + crypto_core_ed25519_scalar_add(scalar_out, scalar, n); +} + +// add tweak point to public key +int sn__extension_tweak_ed25519_pk_add(unsigned char *tpk, + const unsigned char *pk, + const unsigned char *q) +{ + return crypto_core_ed25519_add(tpk, pk, q); +} + + +int sn__extension_tweak_ed25519_keypair_add(unsigned char *pk, unsigned char *scalar_out, + unsigned char *scalar, const unsigned char *tweak) +{ + sn__extension_tweak_ed25519_scalar_add(scalar_out, scalar, tweak); + return crypto_scalarmult_ed25519_base_noclamp(pk, scalar_out); +} \ No newline at end of file diff --git a/bot/node_modules/sodium-native/extensions/tweak/tweak.h b/bot/node_modules/sodium-native/extensions/tweak/tweak.h new file mode 100644 index 0000000..7fdb2ab --- /dev/null +++ b/bot/node_modules/sodium-native/extensions/tweak/tweak.h @@ -0,0 +1,62 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#include + +// copy 32 bytes using int64_t pointers +#define SN_TWEAK_COPY_32(a, b) \ + { \ + long long *dst = (long long *) a; \ + long long *src = (long long *) b; \ + dst[0] = src[0]; \ + dst[1] = src[1]; \ + dst[2] = src[2]; \ + dst[3] = src[3]; \ + } + +#define sn__extension_tweak_ed25519_BYTES crypto_sign_ed25519_PUBLICKEYBYTES + +#define sn__extension_tweak_ed25519_SCALARBYTES crypto_scalarmult_ed25519_SCALARBYTES + +int sn__extension_tweak_ed25519_sign_detached(unsigned char *sig, unsigned long long *siglen_p, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, unsigned char *pk); + +void sn__extension_tweak_ed25519_base(unsigned char *pk, unsigned char *scalar, + const unsigned char *ns, unsigned long long nslen); + +void sn__extension_tweak_ed25519_sk_to_scalar(unsigned char *scalar, const unsigned char *sk); + +// tweak a secret key +void sn__extension_tweak_ed25519_scalar(unsigned char *scalar_out, + const unsigned char *scalar, + const unsigned char *ns, + unsigned long long nslen); + +// tweak a public key +int sn__extension_tweak_ed25519_pk(unsigned char *tpk, + const unsigned char *pk, + const unsigned char *ns, + unsigned long long nslen); + +void sn__extension_tweak_ed25519_keypair(unsigned char *pk, unsigned char *scalar_out, + unsigned char *scalar, const unsigned char *ns, + unsigned long long nslen); + +// add tweak scalar to private key +void sn__extension_tweak_ed25519_scalar_add(unsigned char *scalar_out, + const unsigned char *scalar, + const unsigned char *n); + +// add tweak point to public key +int sn__extension_tweak_ed25519_pk_add(unsigned char *tpk, + const unsigned char *pk, + const unsigned char *q); + +int sn__extension_tweak_ed25519_keypair_add(unsigned char *pk, unsigned char *scalar_out, + unsigned char *scalar, const unsigned char *tweak); + +#ifdef __cplusplus +}; +#endif diff --git a/bot/node_modules/sodium-native/index.js b/bot/node_modules/sodium-native/index.js new file mode 100644 index 0000000..017021d --- /dev/null +++ b/bot/node_modules/sodium-native/index.js @@ -0,0 +1,2272 @@ +const binding = require('./binding') +const { isNode } = require('which-runtime') + +const OPTIONAL = Buffer.from(new ArrayBuffer(0)) + +module.exports = exports = { ...binding } + +// memory + +exports.sodium_memzero = function (buf) { + binding.sodium_memzero(buf) +} + +exports.sodium_mlock = function (buf) { + const res = binding.sodium_mlock(buf) + if (res !== 0) throw new Error('memory lock failed') +} + +exports.sodium_munlock = function (buf) { + const res = binding.sodium_munlock(buf) + if (res !== 0) throw new Error('memory unlock failed') +} + +exports.sodium_malloc = function (size) { + if (size < 0) throw new Error('invalid size') + const buf = Buffer.from(binding.sodium_malloc(size)) + buf.secure = true + + return buf +} + +exports.sodium_free = function (buf) { + if (!buf?.secure) return + + binding.sodium_free(buf.buffer) +} + +exports.sodium_mprotect_noaccess = function (buf) { + const res = binding.sodium_mprotect_noaccess(buf.buffer) + + if (res !== 0) throw new Error('failed to lock buffer') +} + +exports.sodium_mprotect_readonly = function (buf) { + const res = binding.sodium_mprotect_readonly(buf.buffer) + + if (res !== 0) throw new Error('failed to unlock buffer') +} + +exports.sodium_mprotect_readwrite = function (buf) { + const res = binding.sodium_mprotect_readwrite(buf.buffer) + + if (res !== 0) throw new Error('failed to unlock buffer') +} + +// crypto_randombytes + +exports.randombytes_buf = function (buffer) { + binding.randombytes_buf(buffer.buffer, buffer.byteOffset, buffer.byteLength) +} + +exports.randombytes_buf_deterministic = function (buffer, seed) { + binding.randombytes_buf_deterministic( + buffer.buffer, + buffer.byteOffset, + buffer.byteLength, + + seed.buffer, + seed.byteOffset, + seed.byteLength + ) +} + +// sodium_helpers + +exports.sodium_memcmp = function (a, b) { + if (a?.byteLength !== b?.byteLength) + throw new Error('buffers must be of same length"') + return binding.sodium_memcmp(a, b) +} + +exports.sodium_add = function (a, b) { + if (a?.byteLength !== b?.byteLength) + throw new Error('buffers must be of same length"') + binding.sodium_add(a, b) +} + +exports.sodium_sub = function (a, b) { + if (a?.byteLength !== b?.byteLength) + throw new Error('buffers must be of same length"') + binding.sodium_sub(a, b) +} + +exports.sodium_compare = function (a, b) { + if (a?.byteLength !== b?.byteLength) + throw new Error('buffers must be of same length"') + return binding.sodium_compare(a, b) +} + +exports.sodium_is_zero = function (buffer, length) { + if (!buffer) throw new Error('invalid buffer') + length ??= buffer.byteLength + if (length > buffer.byteLength || length < 0) + throw new Error('invalid length') + + return binding.sodium_is_zero(buffer, length) +} + +exports.sodium_pad = function (buffer, unpaddedBuflen, blockSize) { + if (unpaddedBuflen > buffer.byteLength) + throw new Error('unpadded length cannot exceed buffer length') + if (blockSize > buffer.byteLength) + throw new Error('block size cannot exceed buffer length') + if (blockSize < 1) throw new Error('block sizemust be at least 1 byte') + if ( + buffer?.byteLength < + unpaddedBuflen + (blockSize - (unpaddedBuflen % blockSize)) + ) + throw new Error('buf not long enough') + + return binding.sodium_pad(buffer, unpaddedBuflen, blockSize) +} + +exports.sodium_unpad = function (buffer, paddedBuflen, blockSize) { + if (paddedBuflen > buffer.byteLength) + throw new Error('unpadded length cannot exceed buffer length') + if (blockSize > buffer.byteLength) + throw new Error('block size cannot exceed buffer length') + if (blockSize < 1) throw new Error('block size must be at least 1 byte') + + return binding.sodium_unpad(buffer, paddedBuflen, blockSize) +} + +// crypto_sign + +exports.crypto_sign_keypair = function (pk, sk) { + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + const res = binding.crypto_sign_keypair(pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_seed_keypair = function (pk, sk, seed) { + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + const res = binding.crypto_sign_seed_keypair(pk, sk, seed) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign = function (sm, m, sk) { + if (sm?.byteLength !== binding.crypto_sign_BYTES + m.byteLength) + throw new Error('sm must be "m.byteLength + crypto_sign_BYTES" bytes') + if (sk?.byteLength !== binding.crypto_sign_SECRETKEYBYTES) + throw new Error('sk') + + const res = binding.crypto_sign(sm, m, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_open = function (m, sm, pk) { + if (sm?.byteLength < binding.crypto_sign_BYTES) throw new Error('sm') + if (m?.byteLength !== sm.byteLength - binding.crypto_sign_BYTES) + throw new Error('m must be "sm.byteLength - crypto_sign_BYTES" bytes') + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + const res = binding.crypto_sign_open(m, sm, pk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_open = function (m, sm, pk) { + if (sm?.byteLength < binding.crypto_sign_BYTES) throw new Error('sm') + if (m?.byteLength !== sm.byteLength - binding.crypto_sign_BYTES) + throw new Error('m must be "sm.byteLength - crypto_sign_BYTES" bytes') + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + return binding.crypto_sign_open(m, sm, pk) +} + +exports.crypto_sign_detached = function (sig, m, sk) { + if (sig?.byteLength !== binding.crypto_sign_BYTES) throw new Error('sig') + if (sk?.byteLength !== binding.crypto_sign_SECRETKEYBYTES) + throw new Error('sk') + + const res = binding.crypto_sign_detached(sig, m, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_verify_detached = function (sig, m, pk) { + return binding.crypto_sign_verify_detached( + sig.buffer, + sig.byteOffset, + sig.byteLength, + + m.buffer, + m.byteOffset, + m.byteLength, + + pk.buffer, + pk.byteOffset, + pk.byteLength + ) +} + +exports.crypto_sign_ed25519_sk_to_pk = function (pk, sk) { + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + if (sk?.byteLength !== binding.crypto_sign_SECRETKEYBYTES) + throw new Error('sk') + + const res = binding.crypto_sign_ed25519_sk_to_pk(pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_ed25519_pk_to_curve25519 = function (x25519pk, ed25519pk) { + if (x25519pk?.byteLength !== binding.crypto_box_PUBLICKEYBYTES) + throw new Error('x25519_pk') + if (ed25519pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('ed25519_pk') + + const res = binding.crypto_sign_ed25519_pk_to_curve25519(x25519pk, ed25519pk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_sign_ed25519_sk_to_curve25519 = function (x25519sk, ed25519sk) { + if (x25519sk?.byteLength !== binding.crypto_box_SECRETKEYBYTES) + throw new Error('x25519_sk') + + const edLen = ed25519sk.byteLength + + if ( + edLen !== binding.crypto_sign_SECRETKEYBYTES && + edLen !== binding.crypto_box_SECRETKEYBYTES + ) { + throw new Error( + "ed25519_sk should either be 'crypto_sign_SECRETKEYBYTES' bytes or 'crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES' bytes" + ) + } + + const res = binding.crypto_sign_ed25519_sk_to_curve25519(x25519sk, ed25519sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_box + +exports.crypto_box_keypair = function (pk, sk) { + if (pk?.byteLength !== binding.crypto_box_PUBLICKEYBYTES) + throw new Error('pk') + if (sk?.byteLength !== binding.crypto_box_SECRETKEYBYTES) + throw new Error('sk') + + const res = binding.crypto_box_keypair(pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_box_seed_keypair = function (pk, sk, seed) { + if (pk?.byteLength !== binding.crypto_box_PUBLICKEYBYTES) + throw new Error('pk') + if (sk?.byteLength !== binding.crypto_box_SECRETKEYBYTES) + throw new Error('sk') + + const res = binding.crypto_box_seed_keypair(pk, sk, seed) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_box_easy = function (c, m, n, pk, sk) { + const res = binding.crypto_box_easy(c, m, n, pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_box_detached = function (c, mac, m, n, pk, sk) { + const res = binding.crypto_box_detached(c, mac, m, n, pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_box_seal = function (c, m, pk) { + const res = binding.crypto_box_seal(c, m, pk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_box_seal_open = function (m, c, pk, sk) { + return binding.crypto_box_seal_open( + m.buffer, + m.byteOffset, + m.byteLength, + + c.buffer, + c.byteOffset, + c.byteLength, + + pk.buffer, + pk.byteOffset, + pk.byteLength, + + sk.buffer, + sk.byteOffset, + sk.byteLength + ) +} + +// crypto_secretbox + +exports.crypto_secretbox_easy = function (c, m, n, k) { + if (c?.byteLength !== m.byteLength + binding.crypto_secretbox_MACBYTES) + throw new Error( + 'c must be "m.byteLength + crypto_secretbox_MACBYTES" bytes' + ) + if (n?.byteLength !== binding.crypto_secretbox_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_secretbox_KEYBYTES) throw new Error('k') + + const res = binding.crypto_secretbox_easy(c, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_secretbox_open_easy = function (m, c, n, k) { + if (m?.byteLength !== c.byteLength - binding.crypto_secretbox_MACBYTES) + throw new Error('m must be "c - crypto_secretbox_MACBYTES" bytes') + if (c?.byteLength < binding.crypto_secretbox_MACBYTES) throw new Error('c') + if (n?.byteLength !== binding.crypto_secretbox_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_secretbox_KEYBYTES) throw new Error('k') + + return binding.crypto_secretbox_open_easy(m, c, n, k) +} + +exports.crypto_secretbox_detached = function (c, mac, m, n, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('c must "m.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_secretbox_MACBYTES) + throw new Error('mac') + if (n?.byteLength !== binding.crypto_secretbox_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_secretbox_KEYBYTES) throw new Error('k') + + const res = binding.crypto_secretbox_detached(c, mac, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_secretbox_open_detached = function (m, c, mac, n, k) { + if (m?.byteLength !== c.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_secretbox_MACBYTES) + throw new Error('mac') + if (n?.byteLength !== binding.crypto_secretbox_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_secretbox_KEYBYTES) throw new Error('k') + + return binding.crypto_secretbox_open_detached(m, c, mac, n, k) +} + +// crypto_generichash + +exports.crypto_generichash = function (output, input, key = OPTIONAL) { + const res = binding.crypto_generichash( + output.buffer, + output.byteOffset, + output.byteLength, + + input.buffer, + input.byteOffset, + input.byteLength, + + key.buffer, + key.byteOffset, + key.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_generichash_batch = function (output, batch, key) { + if (isNode || batch.length < 4) { + const res = binding.crypto_generichash_batch( + output, + batch, + !!key, + key || OPTIONAL + ) + if (res !== 0) throw new Error('status: ' + res) + } else { + const state = Buffer.alloc(binding.crypto_generichash_STATEBYTES) + + exports.crypto_generichash_init(state, key, output.byteLength) + + for (const buf of batch) { + exports.crypto_generichash_update(state, buf) + } + + exports.crypto_generichash_final(state, output) + } +} + +exports.crypto_generichash_keygen = function (key) { + const res = binding.crypto_generichash_keygen( + key.buffer, + key.byteOffset, + key.byteLength + ) + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_generichash_init = function (state, key, outputLength) { + key ||= OPTIONAL + + const res = binding.crypto_generichash_init( + state.buffer, + state.byteOffset, + state.byteLength, + + key.buffer, + key.byteOffset, + key.byteLength, + + outputLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_generichash_update = function (state, input) { + const res = binding.crypto_generichash_update( + state.buffer, + state.byteOffset, + state.byteLength, + + input.buffer, + input.byteOffset, + input.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_generichash_final = function (state, output) { + const res = binding.crypto_generichash_final( + state.buffer, + state.byteOffset, + state.byteLength, + + output.buffer, + output.byteOffset, + output.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +// secretstream + +exports.crypto_secretstream_xchacha20poly1305_keygen = function (k) { + binding.crypto_secretstream_xchacha20poly1305_keygen( + k.buffer, + k.byteOffset, + k.byteLength + ) +} + +exports.crypto_secretstream_xchacha20poly1305_init_push = function ( + state, + header, + k +) { + const res = binding.crypto_secretstream_xchacha20poly1305_init_push( + state.buffer, + state.byteOffset, + state.byteLength, + + header.buffer, + header.byteOffset, + header.byteLength, + + k.buffer, + k.byteOffset, + k.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_secretstream_xchacha20poly1305_init_pull = function ( + state, + header, + k +) { + const res = binding.crypto_secretstream_xchacha20poly1305_init_pull( + state.buffer, + state.byteOffset, + state.byteLength, + + header.buffer, + header.byteOffset, + header.byteLength, + + k.buffer, + k.byteOffset, + k.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_secretstream_xchacha20poly1305_push = function ( + state, + c, + m, + ad, + tag +) { + ad ||= OPTIONAL + + const res = binding.crypto_secretstream_xchacha20poly1305_push( + state.buffer, + state.byteOffset, + state.byteLength, + + c.buffer, + c.byteOffset, + c.byteLength, + + m.buffer, + m.byteOffset, + m.byteLength, + + ad.buffer, + ad.byteOffset, + ad.byteLength, + + tag + ) + + if (res < 0) throw new Error('push failed') + + return res +} + +exports.crypto_secretstream_xchacha20poly1305_pull = function ( + state, + m, + tag, + c, + ad +) { + ad ||= OPTIONAL + + if (c?.byteLength < binding.crypto_secretstream_xchacha20poly1305_ABYTES) + throw new Error('invalid cipher length') + if ( + m?.byteLength !== + c.byteLength - binding.crypto_secretstream_xchacha20poly1305_ABYTES + ) + throw new Error('invalid message length') + + const res = binding.crypto_secretstream_xchacha20poly1305_pull( + state.buffer, + state.byteOffset, + state.byteLength, + + m.buffer, + m.byteOffset, + m.byteLength, + + tag.buffer, + tag.byteOffset, + tag.byteLength, + + c.buffer, + c.byteOffset, + c.byteLength, + + ad.buffer, + ad.byteOffset, + ad.byteLength + ) + + if (res < 0) throw new Error('pull failed') + + return res +} + +exports.crypto_secretstream_xchacha20poly1305_rekey = function (state) { + binding.crypto_secretstream_xchacha20poly1305_rekey( + state.buffer, + state.byteOffset, + state.byteLength + ) +} + +// crypto_stream + +exports.crypto_stream = function (c, n, k) { + if (n?.byteLength !== binding.crypto_stream_NONCEBYTES) throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_KEYBYTES) throw new Error('k') + + const res = binding.crypto_stream(c, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_xor = function (c, m, n, k) { + const res = binding.crypto_stream_xor( + c.buffer, + c.byteOffset, + c.byteLength, + + m.buffer, + m.byteOffset, + m.byteLength, + + n.buffer, + n.byteOffset, + n.byteLength, + + k.buffer, + k.byteOffset, + k.byteLength + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20 = function (c, n, k) { + if (n?.byteLength !== binding.crypto_stream_chacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20(c, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20_xor = function (c, m, n, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_chacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20_xor(c, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20_xor_ic = function (c, m, n, ic, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_chacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20_xor_ic(c, m, n, ic, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20_ietf = function (c, n, k) { + if (n?.byteLength !== binding.crypto_stream_chacha20_ietf_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20_ietf(c, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20_ietf_xor = function (c, m, n, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_chacha20_ietf_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20_ietf_xor(c, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_chacha20_ietf_xor_ic = function (c, m, n, ic, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_chacha20_ietf_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_chacha20_ietf_xor_ic(c, m, n, ic, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_xchacha20 = function (c, n, k) { + if (n?.byteLength !== binding.crypto_stream_xchacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_xchacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_xchacha20(c, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_xchacha20_xor = function (c, m, n, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_xchacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_xchacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_xchacha20_xor(c, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_xchacha20_xor_ic = function (c, m, n, ic, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_xchacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_xchacha20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_xchacha20_xor_ic(c, m, n, ic, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_salsa20 = function (c, n, k) { + if (n?.byteLength !== binding.crypto_stream_salsa20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_salsa20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_salsa20(c, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_salsa20_xor = function (c, m, n, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_salsa20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_salsa20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_salsa20_xor(c, m, n, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_stream_salsa20_xor_ic = function (c, m, n, ic, k) { + if (c?.byteLength !== m.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (n?.byteLength !== binding.crypto_stream_salsa20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_salsa20_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_stream_salsa20_xor_ic(c, m, n, ic, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_auth + +exports.crypto_auth = function (out, input, k) { + if (out?.byteLength !== binding.crypto_auth_BYTES) throw new Error('out') + if (k?.byteLength !== binding.crypto_auth_KEYBYTES) throw new Error('k') + + const res = binding.crypto_auth(out, input, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_auth_verify = function (h, input, k) { + if (h?.byteLength !== binding.crypto_auth_BYTES) throw new Error('h') + if (k?.byteLength !== binding.crypto_auth_KEYBYTES) throw new Error('k') + + return binding.crypto_auth_verify(h, input, k) +} + +// crypto_onetimeauth + +exports.crypto_onetimeauth = function (out, input, k) { + if (out?.byteLength !== binding.crypto_onetimeauth_BYTES) + throw new Error('out') + if (k?.byteLength !== binding.crypto_onetimeauth_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_onetimeauth(out, input, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_onetimeauth_init = function (state, k) { + if (state?.byteLength !== binding.crypto_onetimeauth_STATEBYTES) + throw new Error("state must be 'crypto_onetimeauth_STATEBYTES' bytes") + if (k?.byteLength !== binding.crypto_onetimeauth_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_onetimeauth_init(state, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_onetimeauth_update = function (state, input) { + if (state?.byteLength !== binding.crypto_onetimeauth_STATEBYTES) + throw new Error("state must be 'crypto_onetimeauth_STATEBYTES' bytes") + + const res = binding.crypto_onetimeauth_update(state, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_onetimeauth_final = function (state, out) { + if (state?.byteLength !== binding.crypto_onetimeauth_STATEBYTES) + throw new Error("state must be 'crypto_onetimeauth_STATEBYTES' bytes") + if (out?.byteLength !== binding.crypto_onetimeauth_BYTES) + throw new Error('out') + + const res = binding.crypto_onetimeauth_final(state, out) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_onetimeauth_verify = function (h, input, k) { + if (h?.byteLength !== binding.crypto_onetimeauth_BYTES) throw new Error('h') + if (k?.byteLength !== binding.crypto_onetimeauth_KEYBYTES) + throw new Error('k') + + return binding.crypto_onetimeauth_verify(h, input, k) +} + +// crypto_pwhash + +exports.crypto_pwhash = function (out, passwd, salt, opslimit, memlimit, alg) { + if (out?.byteLength < binding.crypto_pwhash_BYTES_MIN) throw new Error('out') + if (out?.byteLength > binding.crypto_pwhash_BYTES_MAX) throw new Error('out') + if (salt?.byteLength !== binding.crypto_pwhash_SALTBYTES) + throw new Error('salt') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + if (alg < 1 || alg > 2) + throw new Error('alg must be either Argon2i 1.3 or Argon2id 1.3') + + const res = binding.crypto_pwhash(out, passwd, salt, opslimit, memlimit, alg) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_pwhash_async = function ( + out, + passwd, + salt, + opslimit, + memlimit, + alg, + callback = undefined +) { + if (out?.byteLength < binding.crypto_pwhash_BYTES_MIN) throw new Error('out') + if (out?.byteLength > binding.crypto_pwhash_BYTES_MAX) throw new Error('out') + if (salt?.byteLength !== binding.crypto_pwhash_SALTBYTES) + throw new Error('salt') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + if (alg < 1 || alg > 2) + throw new Error('alg must be either Argon2i 1.3 or Argon2id 1.3') + + const [done, promise] = checkStatus(callback) + + binding.crypto_pwhash_async( + out.buffer, + out.byteOffset, + out.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + salt.buffer, + salt.byteOffset, + salt.byteLength, + + opslimit, + memlimit, + alg, + + done + ) + + return promise +} + +exports.crypto_pwhash_str = function (out, passwd, opslimit, memlimit) { + if (out?.byteLength !== binding.crypto_pwhash_STRBYTES) throw new Error('out') + if (typeof opslimit !== 'number') throw new Error('opslimit') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (typeof memlimit !== 'number') throw new Error('memlimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + + const res = binding.crypto_pwhash_str(out, passwd, opslimit, memlimit) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_pwhash_str_async = function ( + out, + passwd, + opslimit, + memlimit, + callback = undefined +) { + if (out?.byteLength !== binding.crypto_pwhash_STRBYTES) throw new Error('out') + if (!passwd?.byteLength) throw new Error('passwd') + if (typeof opslimit !== 'number') throw new Error('opslimit') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (typeof memlimit !== 'number') throw new Error('memlimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + + const [done, promise] = checkStatus(callback) + + binding.crypto_pwhash_str_async( + out.buffer, + out.byteOffset, + out.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + opslimit, + memlimit, + + done + ) + + return promise +} + +exports.crypto_pwhash_str_verify = function (str, passwd) { + if (str?.byteLength !== binding.crypto_pwhash_STRBYTES) throw new Error('str') + + return binding.crypto_pwhash_str_verify(str, passwd) +} + +exports.crypto_pwhash_str_verify_async = function ( + str, + passwd, + callback = undefined +) { + if (str?.byteLength !== binding.crypto_pwhash_STRBYTES) throw new Error('str') + if (!passwd?.byteLength) throw new Error('passwd') + + const [done, promise] = checkStatus(callback, true) + + binding.crypto_pwhash_str_verify_async( + str.buffer, + str.byteOffset, + str.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + done + ) + + return promise +} + +exports.crypto_pwhash_str_needs_rehash = function (str, opslimit, memlimit) { + if (str?.byteLength !== binding.crypto_pwhash_STRBYTES) throw new Error('str') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + + return binding.crypto_pwhash_str_needs_rehash(str, opslimit, memlimit) +} + +exports.crypto_pwhash_scryptsalsa208sha256 = function ( + out, + passwd, + salt, + opslimit, + memlimit +) { + if (out?.byteLength < binding.crypto_pwhash_scryptsalsa208sha256_BYTES_MIN) + throw new Error('out') + if (out?.byteLength > binding.crypto_pwhash_scryptsalsa208sha256_BYTES_MAX) + throw new Error('out') + if (salt?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_SALTBYTES) + throw new Error('salt') + if (opslimit < binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN) + throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX) + throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN) + throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX) + throw new Error('memlimit') + + const res = binding.crypto_pwhash_scryptsalsa208sha256( + out, + passwd, + salt, + opslimit, + memlimit + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_pwhash_scryptsalsa208sha256_async = function ( + out, + passwd, + salt, + opslimit, + memlimit, + callback = undefined +) { + if (out?.byteLength < binding.crypto_pwhash_scryptsalsa208sha256_BYTES_MIN) + throw new Error('out') + if (out?.byteLength > binding.crypto_pwhash_scryptsalsa208sha256_BYTES_MAX) + throw new Error('out') + if (!passwd?.byteLength) throw new Error('passwd') + if (salt?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_SALTBYTES) + throw new Error('salt') + if (opslimit < binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN) + throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX) + throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN) + throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX) + throw new Error('memlimit') + + const [done, promise] = checkStatus(callback) + + binding.crypto_pwhash_scryptsalsa208sha256_async( + out.buffer, + out.byteOffset, + out.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + salt.buffer, + salt.byteOffset, + salt.byteLength, + + opslimit, + memlimit, + + done + ) + + return promise +} + +exports.crypto_pwhash_scryptsalsa208sha256_str_async = function ( + out, + passwd, + opslimit, + memlimit, + callback = undefined +) { + if (out?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_STRBYTES) + throw new Error('out') + if (!passwd?.byteLength) throw new Error('passwd') + if (opslimit < binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN) + throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX) + throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN) + throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX) + throw new Error('memlimit') + + const [done, promise] = checkStatus(callback) + + binding.crypto_pwhash_scryptsalsa208sha256_str_async( + out.buffer, + out.byteOffset, + out.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + opslimit, + memlimit, + + done + ) + + return promise +} + +exports.crypto_pwhash_scryptsalsa208sha256_str = function ( + out, + passwd, + opslimit, + memlimit +) { + if (out?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_STRBYTES) + throw new Error('out') + if (!passwd?.byteLength) throw new Error('passwd') + if (opslimit < binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN) + throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX) + throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN) + throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX) + throw new Error('memlimit') + + const res = binding.crypto_pwhash_scryptsalsa208sha256_str( + out, + passwd, + opslimit, + memlimit + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_pwhash_scryptsalsa208sha256_str_verify_async = function ( + str, + passwd, + callback = undefined +) { + if (str?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_STRBYTES) + throw new Error('str') + if (!passwd?.byteLength) throw new Error('passwd') + + const [done, promise] = checkStatus(callback, true) + + binding.crypto_pwhash_scryptsalsa208sha256_str_verify_async( + str.buffer, + str.byteOffset, + str.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + done + ) + + return promise +} + +exports.crypto_pwhash_scryptsalsa208sha256_str_verify = function (str, passwd) { + if (str?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_STRBYTES) + throw new Error('str') + if (!passwd?.byteLength) throw new Error('passwd') + + return binding.crypto_pwhash_scryptsalsa208sha256_str_verify(str, passwd) +} + +exports.crypto_pwhash_scryptsalsa208sha256_str_needs_rehash = function ( + str, + opslimit, + memlimit +) { + if (str?.byteLength !== binding.crypto_pwhash_scryptsalsa208sha256_STRBYTES) + throw new Error('str') + if (opslimit < binding.crypto_pwhash_OPSLIMIT_MIN) throw new Error('opslimit') + if (opslimit > binding.crypto_pwhash_OPSLIMIT_MAX) throw new Error('opslimit') + if (memlimit < binding.crypto_pwhash_MEMLIMIT_MIN) throw new Error('memlimit') + if (memlimit > binding.crypto_pwhash_MEMLIMIT_MAX) throw new Error('memlimit') + + return binding.crypto_pwhash_scryptsalsa208sha256_str_needs_rehash( + str, + opslimit, + memlimit + ) +} + +// crypto_kx + +exports.crypto_kx_keypair = function (pk, sk) { + if (pk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) throw new Error('pk') + if (sk?.byteLength !== binding.crypto_kx_SECRETKEYBYTES) throw new Error('sk') + + const res = binding.crypto_kx_keypair(pk, sk) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_kx_seed_keypair = function (pk, sk, seed) { + if (pk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) throw new Error('pk') + if (sk?.byteLength !== binding.crypto_kx_SECRETKEYBYTES) throw new Error('sk') + if (seed?.byteLength !== binding.crypto_kx_SEEDBYTES) throw new Error('seed') + + const res = binding.crypto_kx_seed_keypair(pk, sk, seed) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_kx_client_session_keys = function ( + rx, + tx, + clientPk, + clientSk, + serverPk +) { + // match `std::optional` by coercing null to undefined + rx ??= undefined + tx ??= undefined + + if (!rx && !tx) throw new Error('at least one session key must be specified') + + if (rx) { + if (rx?.byteLength !== binding.crypto_kx_SESSIONKEYBYTES) + throw new Error( + 'receiving key buffer must be "crypto_kx_SESSIONKEYBYTES" bytes or null' + ) + } else { + if (tx?.byteLength !== binding.crypto_kx_SESSIONKEYBYTES) + throw new Error( + 'transmitting key buffer must be "crypto_kx_SESSIONKEYBYTES" bytes or null' + ) + } + + if (clientPk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) + throw new Error('client_pk') + if (clientSk?.byteLength !== binding.crypto_kx_SECRETKEYBYTES) + throw new Error('client_sk') + if (serverPk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) + throw new Error('server_pk') + + const res = binding.crypto_kx_client_session_keys( + rx, + tx, + clientPk, + clientSk, + serverPk + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_kx_server_session_keys = function ( + rx, + tx, + serverPk, + serverSk, + clientPk +) { + rx ??= undefined + tx ??= undefined + + if (!rx && !tx) throw new Error('at least one session key must be specified') + + if (rx) { + if (rx?.byteLength !== binding.crypto_kx_SESSIONKEYBYTES) + throw new Error( + 'receiving key buffer must be "crypto_kx_SESSIONKEYBYTES" bytes or null' + ) + } else { + if (tx?.byteLength !== binding.crypto_kx_SESSIONKEYBYTES) + throw new Error( + 'transmitting key buffer must be "crypto_kx_SESSIONKEYBYTES" bytes or null' + ) + } + + if (serverPk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) + throw new Error('server_pk') + if (serverSk?.byteLength !== binding.crypto_kx_SECRETKEYBYTES) + throw new Error('server_sk') + if (clientPk?.byteLength !== binding.crypto_kx_PUBLICKEYBYTES) + throw new Error('client_pk') + + const res = binding.crypto_kx_server_session_keys( + rx, + tx, + serverPk, + serverSk, + clientPk + ) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_scalarmult + +exports.crypto_scalarmult_base = function (q, n) { + if (q?.byteLength !== binding.crypto_scalarmult_BYTES) throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_SCALARBYTES) + throw new Error('n') + + const res = binding.crypto_scalarmult_base(q, n) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_scalarmult = function (q, n, p) { + if (q?.byteLength !== binding.crypto_scalarmult_BYTES) throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_SCALARBYTES) + throw new Error('n') + if (p?.byteLength !== binding.crypto_scalarmult_BYTES) throw new Error('p') + + const res = binding.crypto_scalarmult(q, n, p) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_scalarmult_ed25519_base = function (q, n) { + if (q?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_ed25519_SCALARBYTES) + throw new Error('n') + + const res = binding.crypto_scalarmult_ed25519_base(q, n) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_scalarmult_ed25519 = function (q, n, p) { + if (q?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_ed25519_SCALARBYTES) + throw new Error('n') + if (p?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('p') + + const res = binding.crypto_scalarmult_ed25519(q, n, p) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_core_ed25519_is_valid_point = function (p) { + if (p?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('p') + + return binding.crypto_core_ed25519_is_valid_point(p) +} + +exports.crypto_core_ed25519_from_uniform = function (p, r) { + if (p?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('p') + if (r?.byteLength !== binding.crypto_core_ed25519_UNIFORMBYTES) + throw new Error('r') + + const res = binding.crypto_core_ed25519_from_uniform(p, r) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_scalarmult_ed25519_base_noclamp = function (q, n) { + if (q?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_ed25519_SCALARBYTES) + throw new Error('n') + + const res = binding.crypto_scalarmult_ed25519_base_noclamp(q, n) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_scalarmult_ed25519_noclamp = function (q, n, p) { + if (q?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('q') + if (n?.byteLength !== binding.crypto_scalarmult_ed25519_SCALARBYTES) + throw new Error('n') + if (p?.byteLength !== binding.crypto_scalarmult_ed25519_BYTES) + throw new Error('p') + + const res = binding.crypto_scalarmult_ed25519_noclamp(q, n, p) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_core + +exports.crypto_core_ed25519_add = function (r, p, q) { + if (r?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('r') + if (p?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('p') + if (q?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('q') + + const res = binding.crypto_core_ed25519_add(r, p, q) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_core_ed25519_sub = function (r, p, q) { + if (r?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('r') + if (p?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('p') + if (q?.byteLength !== binding.crypto_core_ed25519_BYTES) throw new Error('q') + + const res = binding.crypto_core_ed25519_sub(r, p, q) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_core_ed25519_scalar_random = function (r) { + if (r?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('r') + + binding.crypto_core_ed25519_scalar_random(r) +} + +exports.crypto_core_ed25519_scalar_reduce = function (r, s) { + if (r?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('r') + if (s?.byteLength !== binding.crypto_core_ed25519_NONREDUCEDSCALARBYTES) + throw new Error('s') + + binding.crypto_core_ed25519_scalar_reduce(r, s) +} + +exports.crypto_core_ed25519_scalar_invert = function (recip, s) { + if (recip?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('recip') + if (s?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('s') + + binding.crypto_core_ed25519_scalar_invert(recip, s) +} + +exports.crypto_core_ed25519_scalar_negate = function (neg, s) { + if (neg?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('neg') + if (s?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('s') + + binding.crypto_core_ed25519_scalar_negate(neg, s) +} + +exports.crypto_core_ed25519_scalar_complement = function (comp, s) { + if (comp?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('comp') + if (s?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('s') + + binding.crypto_core_ed25519_scalar_complement(comp, s) +} + +exports.crypto_core_ed25519_scalar_add = function (z, x, y) { + if (z?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('z') + if (x?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('x') + if (y?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('y') + + binding.crypto_core_ed25519_scalar_add(z, x, y) +} + +exports.crypto_core_ed25519_scalar_sub = function (z, x, y) { + if (z?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('z') + if (x?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('x') + if (y?.byteLength !== binding.crypto_core_ed25519_SCALARBYTES) + throw new Error('y') + + binding.crypto_core_ed25519_scalar_sub(z, x, y) +} + +// crypto_shorthash + +exports.crypto_shorthash = function (out, input, k) { + if (out?.byteLength !== binding.crypto_shorthash_BYTES) throw new Error('out') + if (k?.byteLength !== binding.crypto_shorthash_KEYBYTES) throw new Error('k') + + const res = binding.crypto_shorthash(out, input, k) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_kdf + +exports.crypto_kdf_keygen = function (key) { + if (key?.byteLength !== binding.crypto_kdf_KEYBYTES) throw new Error('key') + + binding.crypto_kdf_keygen(key) +} + +exports.crypto_kdf_derive_from_key = function (subkey, subkeyId, ctx, key) { + if (subkey?.byteLength < binding.crypto_kdf_BYTES_MIN) + throw new Error('subkey') + if (subkey?.byteLength > binding.crypto_kdf_BYTES_MAX) + throw new Error('subkey') + if (ctx?.byteLength !== binding.crypto_kdf_CONTEXTBYTES) + throw new Error('ctx') + if (key?.byteLength !== binding.crypto_kdf_KEYBYTES) throw new Error('key') + + const res = binding.crypto_kdf_derive_from_key(subkey, subkeyId, ctx, key) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_hash + +exports.crypto_hash = function (out, input) { + if (out?.byteLength !== binding.crypto_hash_BYTES) throw new Error('out') + + const res = binding.crypto_hash(out, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha256 = function (out, input) { + if (out?.byteLength !== binding.crypto_hash_sha256_BYTES) + throw new Error('out') + + const res = binding.crypto_hash_sha256(out, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha256_init = function (state) { + if (state?.byteLength !== binding.crypto_hash_sha256_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha256_STATEBYTES' bytes") + } + + const res = binding.crypto_hash_sha256_init(state) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha256_update = function (state, input) { + if (state?.byteLength !== binding.crypto_hash_sha256_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha256_STATEBYTES' bytes") + } + + const res = binding.crypto_hash_sha256_update(state, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha256_final = function (state, out) { + if (state?.byteLength !== binding.crypto_hash_sha256_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha256_STATEBYTES' bytes") + } + if (out?.byteLength !== binding.crypto_hash_sha256_BYTES) + throw new Error('state') + + const res = binding.crypto_hash_sha256_final(state, out) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha512 = function (out, input) { + if (out?.byteLength !== binding.crypto_hash_sha512_BYTES) + throw new Error('out') + + const res = binding.crypto_hash_sha512(out, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha512_init = function (state) { + if (state?.byteLength !== binding.crypto_hash_sha512_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha512_STATEBYTES' bytes") + } + + const res = binding.crypto_hash_sha512_init(state) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha512_update = function (state, input) { + if (state?.byteLength !== binding.crypto_hash_sha512_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha512_STATEBYTES' bytes") + } + + const res = binding.crypto_hash_sha512_update(state, input) + + if (res !== 0) throw new Error('status: ' + res) +} + +exports.crypto_hash_sha512_final = function (state, out) { + if (state?.byteLength !== binding.crypto_hash_sha512_STATEBYTES) { + throw new Error("state must be 'crypto_hash_sha512_STATEBYTES' bytes") + } + if (out?.byteLength !== binding.crypto_hash_sha512_BYTES) + throw new Error('out') + + const res = binding.crypto_hash_sha512_final(state, out) + + if (res !== 0) throw new Error('status: ' + res) +} + +// crypto_aead + +exports.crypto_aead_xchacha20poly1305_ietf_keygen = function (k) { + if (k?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + binding.crypto_aead_xchacha20poly1305_ietf_keygen(k) +} + +exports.crypto_aead_xchacha20poly1305_ietf_encrypt = function ( + c, + m, + ad, + nsec = null, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if ( + c?.byteLength !== + m.byteLength + binding.crypto_aead_xchacha20poly1305_ietf_ABYTES + ) + throw new Error( + 'c must "m.byteLength + crypto_aead_xchacha20poly1305_ietf_ABYTES" bytes' + ) + if (c?.byteLength > 0xffffffff) + throw new Error('c.byteLength must be a 32bit integer') + if (npub?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_xchacha20poly1305_ietf_encrypt( + c, + m, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not encrypt data') + + return res +} + +exports.crypto_aead_xchacha20poly1305_ietf_decrypt = function ( + m, + nsec = null, + c, + ad, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if ( + m?.byteLength !== + c.byteLength - binding.crypto_aead_xchacha20poly1305_ietf_ABYTES + ) + throw new Error( + 'm must "c.byteLength - crypto_aead_xchacha20poly1305_ietf_ABYTES" bytes' + ) + if (m?.byteLength > 0xffffffff) + throw new Error('m.byteLength must be a 32bit integer') + if (npub?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_xchacha20poly1305_ietf_decrypt( + m, + c, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not verify data') + + return res +} + +exports.crypto_aead_xchacha20poly1305_ietf_encrypt_detached = function ( + c, + mac, + m, + ad, + nsec = null, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_ABYTES) + throw new Error('mac') + if (npub?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_xchacha20poly1305_ietf_encrypt_detached( + c, + mac, + m, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not encrypt data') + + return res +} + +exports.crypto_aead_xchacha20poly1305_ietf_decrypt_detached = function ( + m, + nsec = null, + c, + mac, + ad, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if (m?.byteLength !== c.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_ABYTES) + throw new Error('mac') + if (npub?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_xchacha20poly1305_ietf_decrypt_detached( + m, + c, + mac, + ad, + npub, + k + ) + if (res !== 0) throw new Error('could not verify data') +} + +exports.crypto_aead_chacha20poly1305_ietf_keygen = function (k) { + if (k?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + binding.crypto_aead_chacha20poly1305_ietf_keygen(k) +} + +exports.crypto_aead_chacha20poly1305_ietf_encrypt = function ( + c, + m, + ad, + nsec = null, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if ( + c?.byteLength !== + m.byteLength + binding.crypto_aead_chacha20poly1305_ietf_ABYTES + ) + throw new Error( + 'c must "m.byteLength + crypto_aead_chacha20poly1305_ietf_ABYTES" bytes' + ) + if (c?.byteLength > 0xffffffff) + throw new Error('c.byteLength must be a 32bit integer') + if (npub?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_chacha20poly1305_ietf_encrypt( + c, + m, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not encrypt data') + + return res +} + +exports.crypto_aead_chacha20poly1305_ietf_decrypt = function ( + m, + nsec = null, + c, + ad, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if ( + m?.byteLength !== + c.byteLength - binding.crypto_aead_chacha20poly1305_ietf_ABYTES + ) + throw new Error( + 'm must "c.byteLength - crypto_aead_chacha20poly1305_ietf_ABYTES" bytes' + ) + if (m?.byteLength > 0xffffffff) + throw new Error('m.byteLength must be a 32bit integer') + if (npub?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_chacha20poly1305_ietf_decrypt( + m, + c, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not verify data') + + return res +} + +exports.crypto_aead_chacha20poly1305_ietf_encrypt_detached = function ( + c, + mac, + m, + ad, + nsec = null, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_ABYTES) + throw new Error('mac') + if (npub?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_chacha20poly1305_ietf_encrypt_detached( + c, + mac, + m, + ad, + npub, + k + ) + if (res < 0) throw new Error('could not encrypt data') + + return res +} + +exports.crypto_aead_chacha20poly1305_ietf_decrypt_detached = function ( + m, + nsec = null, + c, + mac, + ad, + npub, + k +) { + ad ??= undefined + if (nsec !== null) throw new Error('nsec must always be set to null') + if (m?.byteLength !== c.byteLength) + throw new Error('m must be "c.byteLength" bytes') + if (mac?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_ABYTES) + throw new Error('mac') + if (npub?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_NPUBBYTES) + throw new Error('npub') + if (k?.byteLength !== binding.crypto_aead_chacha20poly1305_ietf_KEYBYTES) + throw new Error('k') + + const res = binding.crypto_aead_chacha20poly1305_ietf_decrypt_detached( + m, + c, + mac, + ad, + npub, + k + ) + if (res !== 0) throw new Error('could not verify data') +} + +// crypto_stream + +exports.crypto_stream_xor_wrap_init = function (state, n, k) { + if (state?.byteLength !== binding.sn_crypto_stream_xor_STATEBYTES) { + throw new Error("state must be 'sn_crypto_stream_xor_STATEBYTES' bytes") + } + if (n?.byteLength !== binding.crypto_stream_NONCEBYTES) throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_KEYBYTES) throw new Error('k') + + binding.crypto_stream_xor_wrap_init(state, n, k) +} + +exports.crypto_stream_xor_wrap_update = function (state, c, m) { + if (state?.byteLength !== binding.sn_crypto_stream_xor_STATEBYTES) { + throw new Error("state must be 'sn_crypto_stream_xor_STATEBYTES' bytes") + } + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + + binding.crypto_stream_xor_wrap_update(state, c, m) +} + +exports.crypto_stream_xor_wrap_final = function (state) { + if (state?.byteLength !== binding.sn_crypto_stream_xor_STATEBYTES) { + throw new Error("state must be 'sn_crypto_stream_xor_STATEBYTES' bytes") + } + + binding.crypto_stream_xor_wrap_final(state) +} + +exports.crypto_stream_chacha20_xor_wrap_init = function (state, n, k) { + if (state?.byteLength !== binding.crypto_stream_chacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_chacha20_xor_STATEBYTES' bytes" + ) + } + if (n?.byteLength !== binding.crypto_stream_chacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_KEYBYTES) + throw new Error('k') + + binding.crypto_stream_chacha20_xor_wrap_init(state, n, k) +} + +exports.crypto_stream_chacha20_xor_wrap_update = function (state, c, m) { + if (state?.byteLength !== binding.crypto_stream_chacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_chacha20_xor_STATEBYTES' bytes" + ) + } + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + + binding.crypto_stream_chacha20_xor_wrap_update(state, c, m) +} + +exports.crypto_stream_chacha20_xor_wrap_final = function (state) { + if (state?.byteLength !== binding.crypto_stream_chacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_chacha20_xor_STATEBYTES' bytes" + ) + } + + binding.crypto_stream_chacha20_xor_wrap_final(state) +} + +exports.crypto_stream_chacha20_ietf_xor_wrap_init = function (state, n, k) { + if ( + state?.byteLength !== binding.crypto_stream_chacha20_ietf_xor_STATEBYTES + ) { + throw new Error( + "state must be 'crypto_stream_chacha20_ietf_xor_STATEBYTES' bytes" + ) + } + if (n?.byteLength !== binding.crypto_stream_chacha20_ietf_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_chacha20_ietf_KEYBYTES) + throw new Error('k') + + binding.crypto_stream_chacha20_ietf_xor_wrap_init(state, n, k) +} + +exports.crypto_stream_chacha20_ietf_xor_wrap_update = function (state, c, m) { + if ( + state?.byteLength !== binding.crypto_stream_chacha20_ietf_xor_STATEBYTES + ) { + throw new Error( + "state must be 'crypto_stream_chacha20_ietf_xor_STATEBYTES' bytes" + ) + } + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + + binding.crypto_stream_chacha20_ietf_xor_wrap_update(state, c, m) +} + +exports.crypto_stream_chacha20_ietf_xor_wrap_final = function (state) { + if ( + state?.byteLength !== binding.crypto_stream_chacha20_ietf_xor_STATEBYTES + ) { + throw new Error( + "state must be 'crypto_stream_chacha20_ietf_xor_STATEBYTES' bytes" + ) + } + + binding.crypto_stream_chacha20_ietf_xor_wrap_final(state) +} + +exports.crypto_stream_xchacha20_xor_wrap_init = function (state, n, k) { + if (state?.byteLength !== binding.crypto_stream_xchacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_xchacha20_xor_STATEBYTES' bytes" + ) + } + if (n?.byteLength !== binding.crypto_stream_xchacha20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_xchacha20_KEYBYTES) + throw new Error('k') + + binding.crypto_stream_xchacha20_xor_wrap_init(state, n, k) +} + +exports.crypto_stream_xchacha20_xor_wrap_update = function (state, c, m) { + if (state?.byteLength !== binding.crypto_stream_xchacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_xchacha20_xor_STATEBYTES' bytes" + ) + } + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + + binding.crypto_stream_xchacha20_xor_wrap_update(state, c, m) +} + +exports.crypto_stream_xchacha20_xor_wrap_final = function (state) { + if (state?.byteLength !== binding.crypto_stream_xchacha20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_xchacha20_xor_STATEBYTES' bytes" + ) + } + + binding.crypto_stream_xchacha20_xor_wrap_final(state) +} + +exports.crypto_stream_salsa20_xor_wrap_init = function (state, n, k) { + if (state?.byteLength !== binding.crypto_stream_salsa20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_salsa20_xor_STATEBYTES' bytes" + ) + } + if (n?.byteLength !== binding.crypto_stream_salsa20_NONCEBYTES) + throw new Error('n') + if (k?.byteLength !== binding.crypto_stream_salsa20_KEYBYTES) + throw new Error('k') + + binding.crypto_stream_salsa20_xor_wrap_init(state, n, k) +} + +exports.crypto_stream_salsa20_xor_wrap_update = function (state, c, m) { + if (state?.byteLength !== binding.crypto_stream_salsa20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_salsa20_xor_STATEBYTES' bytes" + ) + } + if (c?.byteLength !== m.byteLength) + throw new Error('c must be "m.byteLength" bytes') + + binding.crypto_stream_salsa20_xor_wrap_update(state, c, m) +} + +exports.crypto_stream_salsa20_xor_wrap_final = function (state) { + if (state?.byteLength !== binding.crypto_stream_salsa20_xor_STATEBYTES) { + throw new Error( + "state must be 'crypto_stream_salsa20_xor_STATEBYTES' bytes" + ) + } + + binding.crypto_stream_salsa20_xor_wrap_final(state) +} + +// experimental + +exports.extension_tweak_ed25519_base = function (n, p, ns) { + if (n?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('n') + if (p?.byteLength !== binding.extension_tweak_ed25519_BYTES) + throw new Error('p') + + binding.extension_tweak_ed25519_base(n, p, ns) +} + +exports.extension_tweak_ed25519_sign_detached = function (sig, m, scalar, pk) { + if (sig?.byteLength !== binding.crypto_sign_BYTES) throw new Error('sig') + if (scalar?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar') + if (pk && pk.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + const res = binding.extension_tweak_ed25519_sign_detached(sig, m, scalar, pk) + if (res !== 0) throw new Error('failed to compute signature') +} + +exports.extension_tweak_ed25519_sk_to_scalar = function (n, sk) { + if (n?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('n') + if (sk?.byteLength !== binding.crypto_sign_SECRETKEYBYTES) + throw new Error('sk') + + binding.extension_tweak_ed25519_sk_to_scalar(n, sk) +} + +exports.extension_tweak_ed25519_scalar = function (scalarOut, scalar, ns) { + if (scalarOut?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_out') + if (scalar?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar') + + binding.extension_tweak_ed25519_scalar(scalarOut, scalar, ns) +} + +exports.extension_tweak_ed25519_pk = function (tpk, pk, ns) { + if (tpk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('tpk') + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + + const res = binding.extension_tweak_ed25519_pk(tpk, pk, ns) + if (res !== 0) throw new Error('failed to tweak public key') +} + +exports.extension_tweak_ed25519_keypair = function ( + pk, + scalarOut, + scalarIn, + ns +) { + if (pk?.byteLength !== binding.extension_tweak_ed25519_BYTES) + throw new Error('pk') + if (scalarOut?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_out') + if (scalarIn?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_in') + + binding.extension_tweak_ed25519_keypair(pk, scalarOut, scalarIn, ns) +} + +exports.extension_tweak_ed25519_scalar_add = function (scalarOut, scalar, n) { + if (scalarOut?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_out') + if (scalar?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar') + if (n?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('n') + + binding.extension_tweak_ed25519_scalar_add(scalarOut, scalar, n) +} + +exports.extension_tweak_ed25519_pk_add = function (tpk, pk, p) { + if (tpk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('tpk') + if (pk?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) + throw new Error('pk') + if (p?.byteLength !== binding.crypto_sign_PUBLICKEYBYTES) throw new Error('p') + + const res = binding.extension_tweak_ed25519_pk_add(tpk, pk, p) + if (res !== 0) throw new Error('failed to add tweak to public key') +} + +exports.extension_tweak_ed25519_keypair_add = function ( + pk, + scalarOut, + scalarIn, + tweak +) { + if (pk?.byteLength !== binding.extension_tweak_ed25519_BYTES) + throw new Error('pk') + if (scalarOut?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_out') + if (scalarIn?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('scalar_in') + if (tweak?.byteLength !== binding.extension_tweak_ed25519_SCALARBYTES) + throw new Error('tweak') + + const res = binding.extension_tweak_ed25519_keypair_add( + pk, + scalarOut, + scalarIn, + tweak + ) + if (res !== 0) throw new Error('failed to add tweak to keypair') +} + +exports.extension_pbkdf2_sha512_async = function ( + out, + passwd, + salt, + iter, + outlen, + callback = undefined +) { + if (iter < binding.extension_pbkdf2_sha512_ITERATIONS_MIN) + throw new Error('iterations') + if (outlen > binding.extension_pbkdf2_sha512_BYTES_MAX) + throw new Error('outlen') + if (out?.byteLength < outlen) throw new Error('out') + if (!out?.byteLength) throw new Error('out') + if (!passwd?.byteLength) throw new Error('passwd') + if (!salt?.byteLength) throw new Error('salt') + + const [done, promise] = checkStatus(callback) + + binding.extension_pbkdf2_sha512_async( + out.buffer, + out.byteOffset, + out.byteLength, + + passwd.buffer, + passwd.byteOffset, + passwd.byteLength, + + salt.buffer, + salt.byteOffset, + salt.byteLength, + + iter, + outlen, + + done + ) + + return promise +} + +exports.extension_pbkdf2_sha512 = function (out, passwd, salt, iter, outlen) { + if (iter < binding.extension_pbkdf2_sha512_ITERATIONS_MIN) + throw new Error('iterations') + if (outlen > binding.extension_pbkdf2_sha512_BYTES_MAX) + throw new Error('outlen') + if (out?.byteLength < outlen) throw new Error('out') + + const res = binding.extension_pbkdf2_sha512(out, passwd, salt, iter, outlen) + + if (res !== 0) throw new Error('failed to add tweak to public key') +} + +function checkStatus(callback, booleanResult = false) { + let done, promise + + if (typeof callback === 'function') { + done = function (status) { + if (booleanResult) callback(null, status === 0) + else if (status === 0) callback(null) + else callback(new Error('status: ' + status)) + } + } else { + promise = new Promise(function (resolve, reject) { + done = function (status) { + if (booleanResult) resolve(status === 0) + else if (status === 0) resolve() + else reject(new Error('status: ' + status)) + } + }) + } + + return [done, promise] +} diff --git a/bot/node_modules/sodium-native/package.json b/bot/node_modules/sodium-native/package.json new file mode 100644 index 0000000..4908817 --- /dev/null +++ b/bot/node_modules/sodium-native/package.json @@ -0,0 +1,55 @@ +{ + "name": "sodium-native", + "version": "5.0.10", + "description": "Low level bindings for libsodium", + "main": "index.js", + "files": [ + "index.js", + "binding.cc", + "binding.js", + "extensions", + "prebuilds", + "CMakeLists.txt" + ], + "addon": true, + "dependencies": { + "require-addon": "^1.1.0", + "which-runtime": "^1.2.1" + }, + "devDependencies": { + "bare-compat-napi": "^1.3.5", + "brittle": "^3.16.2", + "cmake-bare": "^1.6.1", + "cmake-fetch": "^1.4.3", + "cmake-napi": "^1.2.1", + "prettier": "^3.6.2", + "prettier-config-holepunch": "^1.0.0" + }, + "scripts": { + "test": "prettier . --check && npm run test:node && npm run test:bare", + "test:node": "node test/all.js", + "test:bare": "bare test/all.js" + }, + "standard": { + "ignore": [ + "/test/fixtures/*.js" + ] + }, + "engines": { + "bare": ">=1.16.0" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/holepunchto/sodium-native.git" + }, + "contributors": [ + "Emil Bay (http://bayes.dk)", + "Mathias Buus (https://mafinto.sh)", + "Christophe Diederichs " + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/holepunchto/sodium-native/issues" + }, + "homepage": "https://github.com/holepunchto/sodium-native" +} diff --git a/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.bare new file mode 100644 index 0000000..2414b8c Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.node new file mode 100644 index 0000000..cc0bc1a Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-arm/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.bare new file mode 100644 index 0000000..ec3b72c Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.node new file mode 100644 index 0000000..3e67b4a Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-arm64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.bare new file mode 100644 index 0000000..14e80f8 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.node new file mode 100644 index 0000000..46a852f Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-ia32/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.bare new file mode 100644 index 0000000..d4a708b Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.node new file mode 100644 index 0000000..d0cc316 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/android-x64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.bare new file mode 100644 index 0000000..c000386 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.node new file mode 100644 index 0000000..352fdd1 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/darwin-arm64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.bare new file mode 100644 index 0000000..ebbea8d Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.node new file mode 100644 index 0000000..1a1f11f Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/darwin-x64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/ios-arm64-simulator/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/ios-arm64-simulator/sodium-native.bare new file mode 100644 index 0000000..5041e3a Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/ios-arm64-simulator/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/ios-arm64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/ios-arm64/sodium-native.bare new file mode 100644 index 0000000..b05d587 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/ios-arm64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/ios-x64-simulator/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/ios-x64-simulator/sodium-native.bare new file mode 100644 index 0000000..5fb202d Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/ios-x64-simulator/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.bare new file mode 100644 index 0000000..ee3a735 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.node new file mode 100644 index 0000000..4f27a87 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/linux-arm64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.bare new file mode 100644 index 0000000..13e87b3 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.node new file mode 100644 index 0000000..fc0200a Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/linux-x64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.bare new file mode 100644 index 0000000..186155a Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.node new file mode 100644 index 0000000..198c1f7 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/win32-arm64/sodium-native.node differ diff --git a/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.bare b/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.bare new file mode 100644 index 0000000..21bb8c9 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.bare differ diff --git a/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.node b/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.node new file mode 100644 index 0000000..c8b04e4 Binary files /dev/null and b/bot/node_modules/sodium-native/prebuilds/win32-x64/sodium-native.node differ diff --git a/bot/node_modules/which-runtime/LICENSE b/bot/node_modules/which-runtime/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/bot/node_modules/which-runtime/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bot/node_modules/which-runtime/README.md b/bot/node_modules/which-runtime/README.md new file mode 100644 index 0000000..941d1e6 --- /dev/null +++ b/bot/node_modules/which-runtime/README.md @@ -0,0 +1,29 @@ +# which-runtime + +Detect if you are in Bare or Node and which os etc + +``` +npm install which-runtime +``` + +## Usage + +``` js +import { + runtime, // bare, node, or browser + platform, // the platform string, ie darwin, win32, etc + arch, // which arch, arm64, ia32, x64 etc + isBrowser, + isBare, + isNode, + isLinux, + isWindows, + isMac, + isIOS, + isAndroid +} from 'which-runtime' +``` + +## License + +Apache-2.0 diff --git a/bot/node_modules/which-runtime/index.js b/bot/node_modules/which-runtime/index.js new file mode 100644 index 0000000..ec98c4a --- /dev/null +++ b/bot/node_modules/which-runtime/index.js @@ -0,0 +1,24 @@ +const { runtime, platform, arch } = typeof Bare !== 'undefined' + ? { runtime: 'bare', platform: global.Bare.platform, arch: global.Bare.arch } + : typeof process !== 'undefined' + ? { runtime: 'node', platform: global.process.platform, arch: global.process.arch } + : typeof Window !== 'undefined' + ? { runtime: 'browser', platform: 'unknown', arch: 'unknown' } + : { runtime: 'unknown', platform: 'unknown', arch: 'unknown' } + +exports.runtime = runtime +exports.platform = platform +exports.arch = arch +exports.isBare = runtime === 'bare' +exports.isBareKit = exports.isBare && typeof BareKit !== 'undefined' +exports.isPear = typeof Pear !== 'undefined' +exports.isNode = runtime === 'node' +exports.isBrowser = runtime === 'browser' +exports.isWindows = platform === 'win32' +exports.isLinux = platform === 'linux' +exports.isMac = platform === 'darwin' +exports.isIOS = platform === 'ios' || platform === 'ios-simulator' +exports.isAndroid = platform === 'android' +exports.isElectron = typeof process !== 'undefined' && !!global.process.versions?.electron +exports.isElectronRenderer = exports.isElectron && global.process.type === 'renderer' +exports.isElectronWorker = exports.isElectron && global.process.type === 'worker' diff --git a/bot/node_modules/which-runtime/package.json b/bot/node_modules/which-runtime/package.json new file mode 100644 index 0000000..fce0d78 --- /dev/null +++ b/bot/node_modules/which-runtime/package.json @@ -0,0 +1,26 @@ +{ + "name": "which-runtime", + "version": "1.3.2", + "description": "Detect if you are in Bare or Node and which os etc", + "main": "index.js", + "files": [ + "index.js" + ], + "dependencies": {}, + "devDependencies": { + "standard": "^17.0.0" + }, + "scripts": { + "test": "standard" + }, + "repository": { + "type": "git", + "url": "https://github.com/holepunchto/which-runtime.git" + }, + "author": "Holepunch Inc.", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/holepunchto/which-runtime/issues" + }, + "homepage": "https://github.com/holepunchto/which-runtime" +} diff --git a/bot/package-lock.json b/bot/package-lock.json index 62e808f..5a331b1 100644 --- a/bot/package-lock.json +++ b/bot/package-lock.json @@ -21,7 +21,8 @@ "dotenv": "^17.3.1", "ffmpeg-static": "^5.3.0", "libsodium-wrappers": "^0.7.16", - "play-dl": "^1.9.7" + "play-dl": "^1.9.7", + "sodium-native": "^5.0.10" } }, "node_modules/@derhuerst/http-basic": { @@ -798,6 +799,47 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, + "node_modules/bare-addon-resolve": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/bare-addon-resolve/-/bare-addon-resolve-1.10.0.tgz", + "integrity": "sha512-sSd0jieRJlDaODOzj0oe0RjFVC1QI0ZIjGIdPkbrTXsdVVtENg14c+lHHAhHwmWCZ2nQlMhy8jA3Y5LYPc/isA==", + "license": "Apache-2.0", + "dependencies": { + "bare-module-resolve": "^1.10.0", + "bare-semver": "^1.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } + }, + "node_modules/bare-module-resolve": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/bare-module-resolve/-/bare-module-resolve-1.12.1.tgz", + "integrity": "sha512-hbmAPyFpEq8FoZMd5sFO3u6MC5feluWoGE8YKlA8fCrl6mNtx68Wjg4DTiDJcqRJaovTvOYKfYngoBUnbaT7eg==", + "license": "Apache-2.0", + "dependencies": { + "bare-semver": "^1.0.0" + }, + "peerDependencies": { + "bare-url": "*" + }, + "peerDependenciesMeta": { + "bare-url": { + "optional": true + } + } + }, + "node_modules/bare-semver": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bare-semver/-/bare-semver-1.0.2.tgz", + "integrity": "sha512-ESVaN2nzWhcI5tf3Zzcq9aqCZ676VWzqw07eEZ0qxAcEOAFYBa0pWq8sK34OQeHLY3JsfKXZS9mDyzyxGjeLzA==", + "license": "Apache-2.0" + }, "node_modules/brace-expansion": { "version": "1.1.12", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz", @@ -1793,6 +1835,18 @@ "node": ">= 6" } }, + "node_modules/require-addon": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/require-addon/-/require-addon-1.2.0.tgz", + "integrity": "sha512-VNPDZlYgIYQwWp9jMTzljx+k0ZtatKlcvOhktZ/anNPI3dQ9NXk7cq2U4iJ1wd9IrytRnYhyEocFWbkdPb+MYA==", + "license": "Apache-2.0", + "dependencies": { + "bare-addon-resolve": "^1.3.0" + }, + "engines": { + "bare": ">=1.10.0" + } + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -1925,6 +1979,19 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, + "node_modules/sodium-native": { + "version": "5.0.10", + "resolved": "https://registry.npmjs.org/sodium-native/-/sodium-native-5.0.10.tgz", + "integrity": "sha512-UIw+0AbpCQRuTJF88JWrZomP4O+PXhlWvdopiAJOsUivTyHTf3korMyStxkZuPngSbBEtEfDdc4ewEd8/T4/lA==", + "license": "MIT", + "dependencies": { + "require-addon": "^1.1.0", + "which-runtime": "^1.2.1" + }, + "engines": { + "bare": ">=1.16.0" + } + }, "node_modules/soundcloud.ts": { "version": "0.6.9", "resolved": "https://registry.npmjs.org/soundcloud.ts/-/soundcloud.ts-0.6.9.tgz", @@ -2105,6 +2172,12 @@ "webidl-conversions": "^3.0.0" } }, + "node_modules/which-runtime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/which-runtime/-/which-runtime-1.3.2.tgz", + "integrity": "sha512-5kwCfWml7+b2NO7KrLMhYihjRx0teKkd3yGp1Xk5Vaf2JGdSh+rgVhEALAD9c/59dP+YwJHXoEO7e8QPy7gOkw==", + "license": "Apache-2.0" + }, "node_modules/wide-align": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz", diff --git a/bot/package.json b/bot/package.json index 1bceed9..87b5a83 100644 --- a/bot/package.json +++ b/bot/package.json @@ -17,6 +17,7 @@ "dotenv": "^17.3.1", "ffmpeg-static": "^5.3.0", "libsodium-wrappers": "^0.7.16", - "play-dl": "^1.9.7" + "play-dl": "^1.9.7", + "sodium-native": "^5.0.10" } }