Skip to content

Commit

Permalink
Do not warn with explicit --pqclean-backend=wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Dec 11, 2024
1 parent 979c3af commit de31ffc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
install-config.gen.js
native/gen/
node_modules/
package-lock.json
Expand Down
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
'use strict';

let kem, sign, KEM, Sign;
try {
// Try to load native bindings first.
({ kem, sign, KEM, Sign } = require('bindings')('node_pqclean'));
} catch (err) {
// If native bindings are not available, use WebAssembly instead.
({ kem, sign, KEM, Sign } = require('./wasm/'));
process.emitWarning(`Using WebAssembly backend: ${err.message}`);
}
const installConfig = require('./install-config.gen.js');

const { kem, sign, KEM, Sign } = (function loadBackend(backend) {
switch (backend) {
case 'prefer-native':
try {
return loadBackend('native');
} catch (err) {
// Use WebAssembly backend only if native bindings are not available.
process.emitWarning(`Using WebAssembly backend: ${err.message}`);
return loadBackend('wasm');
}
case 'native':
return require('bindings')('node_pqclean');
case 'wasm':
return require('./wasm/');
default:
throw new Error(`Unsupported backend: ${backend}`);
}
})(installConfig.backend);

// TODO: should we deep-freeze these?
Object.freeze(kem.supportedAlgorithms);
Expand Down
4 changes: 3 additions & 1 deletion scripts/install.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { spawn } from 'node:child_process';
import * as events from 'node:events';
import { access, rm } from 'node:fs/promises';
import { access, rm, writeFile } from 'node:fs/promises';

if (process.env.npm_lifecycle_event !== 'install') {
console.error('Please use "npm install".');
Expand Down Expand Up @@ -87,3 +87,5 @@ if (wasmRequired) {
}
}
}

await writeFile('install-config.gen.js', `module.exports.backend = '${backendName}';\n`);

0 comments on commit de31ffc

Please sign in to comment.