Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/openpgpjs/openpgpjs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlarabr <larabr+github@protonmail.com>2022-05-12 18:29:52 +0300
committerGitHub <noreply@github.com>2022-05-12 18:29:52 +0300
commitcb8901c16d49d8c9652d34a0378ca8833089e8a1 (patch)
tree8f1e3dd571f9df0247f340d5b5b0ca011ec98a76
parent4713282bb124a9f30b325f2ebc1b2638606d622e (diff)
Fix loading browser built in JSDom environment (#1518)
-rw-r--r--src/crypto/public_key/rsa.js4
-rw-r--r--src/util.js16
-rw-r--r--test/general/openpgp.js4
-rw-r--r--test/general/streaming.js8
4 files changed, 14 insertions, 18 deletions
diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js
index 1c081316..1b766b0d 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 15b903bb..f3a57e11 100644
--- a/src/util.js
+++ b/src/util.js
@@ -382,14 +382,6 @@ const util = {
},
/**
- * Detect Node.js runtime.
- */
- detectNode: function() {
- return typeof globalThis.process === 'object' &&
- typeof globalThis.process.versions === 'object';
- },
-
- /**
* Detect native BigInt support
*/
detectBigInt: () => typeof BigInt !== 'undefined',
@@ -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 3f5aadd8..6fe34246 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 6839f1f4..5af963af 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() {