Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading browser built in JSDom environment #1518

Merged
merged 4 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/crypto/public_key/rsa.js
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 4 additions & 12 deletions src/util.js
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion test/general/openpgp.js
Expand Up @@ -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)',
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions test/general/streaming.js
Expand Up @@ -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)',
Expand Down Expand Up @@ -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 });
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down