diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js index 1c081316c..1b766b0d1 100644 --- a/src/crypto/public_key/rsa.js +++ b/src/crypto/public_key/rsa.js @@ -33,7 +33,7 @@ const nodeCrypto = util.getNodeCrypto(); const asn1 = nodeCrypto ? require('asn1.js') : undefined; /* eslint-disable no-invalid-this */ -const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function () { +const RSAPrivateKey = nodeCrypto ? asn1.define('RSAPrivateKey', function () { this.seq().obj( // used for native NodeJS crypto this.key('version').int(), // 0 this.key('modulus').int(), // n @@ -47,7 +47,7 @@ const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function ); }) : undefined; -const RSAPublicKey = util.detectNode() ? asn1.define('RSAPubliceKey', function () { +const RSAPublicKey = nodeCrypto ? asn1.define('RSAPubliceKey', function () { this.seq().obj( // used for native NodeJS crypto this.key('modulus').int(), // n this.key('publicExponent').int(), // e diff --git a/src/util.js b/src/util.js index 15b903bbc..f3a57e117 100644 --- a/src/util.js +++ b/src/util.js @@ -381,14 +381,6 @@ const util = { return typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.subtle; }, - /** - * Detect Node.js runtime. - */ - detectNode: function() { - return typeof globalThis.process === 'object' && - typeof globalThis.process.versions === 'object'; - }, - /** * Detect native BigInt support */ @@ -425,12 +417,12 @@ const util = { }, getHardwareConcurrency: function() { - if (util.detectNode()) { - const os = require('os'); - return os.cpus().length; + if (typeof navigator !== 'undefined') { + return navigator.hardwareConcurrency || 1; } - return (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) || 1; + const os = require('os'); // Assume we're on Node.js. + return os.cpus().length; }, isEmailAddress: function(data) { diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 3f5aadd8b..6fe34246c 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -17,6 +17,8 @@ chai.use(require('chai-as-promised')); const expect = chai.expect; +const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object'; + const pub_key = [ '-----BEGIN PGP PUBLIC KEY BLOCK-----', 'Version: GnuPG v2.0.19 (GNU/Linux)', @@ -2901,7 +2903,7 @@ aOU= expect(e.message).to.match(/Ascii armor integrity check failed/); expect(stepReached).to.equal( j === 0 ? 0 : - (openpgp.config.aeadChunkSizeByte === 0 && (j === 2 || util.detectNode() || util.getHardwareConcurrency() < 8)) || (!openpgp.config.aeadProtect && openpgp.config.allowUnauthenticatedStream) ? 2 : + (openpgp.config.aeadChunkSizeByte === 0 && (j === 2 || detectNode() || util.getHardwareConcurrency() < 8)) || (!openpgp.config.aeadProtect && openpgp.config.allowUnauthenticatedStream) ? 2 : 1 ); return; diff --git a/test/general/streaming.js b/test/general/streaming.js index 6839f1f4f..5af963afb 100644 --- a/test/general/streaming.js +++ b/test/general/streaming.js @@ -16,6 +16,8 @@ const stream = require('@openpgp/web-stream-tools'); const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); // eslint-disable-line no-new const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable; +const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object'; + const pub_key = [ '-----BEGIN PGP PUBLIC KEY BLOCK-----', 'Version: GnuPG v2.0.19 (GNU/Linux)', @@ -889,7 +891,7 @@ function tests() { it("Don't pull entire input stream when we're not pulling decrypted stream (AEAD)", async function() { let coresStub; - if (util.detectNode()) { + if (detectNode()) { coresStub = stub(require('os'), 'cpus'); coresStub.returns(new Array(2)); // Object.defineProperty(require('os'), 'cpus', { value: () => [,], configurable: true }); @@ -915,7 +917,7 @@ function tests() { await new Promise(resolve => setTimeout(resolve, 3000)); expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 300); } finally { - if (util.detectNode()) { + if (detectNode()) { coresStub.restore(); } else { delete navigator.hardwareConcurrency; @@ -1012,7 +1014,7 @@ module.exports = () => describe('Streaming', function() { tests(); - if (util.detectNode()) { + if (detectNode()) { const fs = require('fs'); it('Node: Encrypt and decrypt text message roundtrip', async function() {