Fix loading browser built in JSDom environment (#1518)
This commit is contained in:
parent
4713282bb1
commit
cb8901c16d
|
@ -33,7 +33,7 @@ const nodeCrypto = util.getNodeCrypto();
|
||||||
const asn1 = nodeCrypto ? require('asn1.js') : undefined;
|
const asn1 = nodeCrypto ? require('asn1.js') : undefined;
|
||||||
|
|
||||||
/* eslint-disable no-invalid-this */
|
/* 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.seq().obj( // used for native NodeJS crypto
|
||||||
this.key('version').int(), // 0
|
this.key('version').int(), // 0
|
||||||
this.key('modulus').int(), // n
|
this.key('modulus').int(), // n
|
||||||
|
@ -47,7 +47,7 @@ const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function
|
||||||
);
|
);
|
||||||
}) : undefined;
|
}) : 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.seq().obj( // used for native NodeJS crypto
|
||||||
this.key('modulus').int(), // n
|
this.key('modulus').int(), // n
|
||||||
this.key('publicExponent').int(), // e
|
this.key('publicExponent').int(), // e
|
||||||
|
|
16
src/util.js
16
src/util.js
|
@ -381,14 +381,6 @@ const util = {
|
||||||
return typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.subtle;
|
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
|
* Detect native BigInt support
|
||||||
*/
|
*/
|
||||||
|
@ -425,12 +417,12 @@ const util = {
|
||||||
},
|
},
|
||||||
|
|
||||||
getHardwareConcurrency: function() {
|
getHardwareConcurrency: function() {
|
||||||
if (util.detectNode()) {
|
if (typeof navigator !== 'undefined') {
|
||||||
const os = require('os');
|
return navigator.hardwareConcurrency || 1;
|
||||||
return os.cpus().length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) || 1;
|
const os = require('os'); // Assume we're on Node.js.
|
||||||
|
return os.cpus().length;
|
||||||
},
|
},
|
||||||
|
|
||||||
isEmailAddress: function(data) {
|
isEmailAddress: function(data) {
|
||||||
|
|
|
@ -17,6 +17,8 @@ chai.use(require('chai-as-promised'));
|
||||||
|
|
||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
|
|
||||||
|
const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object';
|
||||||
|
|
||||||
const pub_key = [
|
const pub_key = [
|
||||||
'-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
'-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
||||||
'Version: GnuPG v2.0.19 (GNU/Linux)',
|
'Version: GnuPG v2.0.19 (GNU/Linux)',
|
||||||
|
@ -2901,7 +2903,7 @@ aOU=
|
||||||
expect(e.message).to.match(/Ascii armor integrity check failed/);
|
expect(e.message).to.match(/Ascii armor integrity check failed/);
|
||||||
expect(stepReached).to.equal(
|
expect(stepReached).to.equal(
|
||||||
j === 0 ? 0 :
|
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
|
1
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -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 useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); // eslint-disable-line no-new
|
||||||
const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable;
|
const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable;
|
||||||
|
|
||||||
|
const detectNode = () => typeof globalThis.process === 'object' && typeof globalThis.process.versions === 'object';
|
||||||
|
|
||||||
const pub_key = [
|
const pub_key = [
|
||||||
'-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
'-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
||||||
'Version: GnuPG v2.0.19 (GNU/Linux)',
|
'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() {
|
it("Don't pull entire input stream when we're not pulling decrypted stream (AEAD)", async function() {
|
||||||
let coresStub;
|
let coresStub;
|
||||||
if (util.detectNode()) {
|
if (detectNode()) {
|
||||||
coresStub = stub(require('os'), 'cpus');
|
coresStub = stub(require('os'), 'cpus');
|
||||||
coresStub.returns(new Array(2));
|
coresStub.returns(new Array(2));
|
||||||
// Object.defineProperty(require('os'), 'cpus', { value: () => [,], configurable: true });
|
// Object.defineProperty(require('os'), 'cpus', { value: () => [,], configurable: true });
|
||||||
|
@ -915,7 +917,7 @@ function tests() {
|
||||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||||
expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 300);
|
expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 300);
|
||||||
} finally {
|
} finally {
|
||||||
if (util.detectNode()) {
|
if (detectNode()) {
|
||||||
coresStub.restore();
|
coresStub.restore();
|
||||||
} else {
|
} else {
|
||||||
delete navigator.hardwareConcurrency;
|
delete navigator.hardwareConcurrency;
|
||||||
|
@ -1012,7 +1014,7 @@ module.exports = () => describe('Streaming', function() {
|
||||||
|
|
||||||
tests();
|
tests();
|
||||||
|
|
||||||
if (util.detectNode()) {
|
if (detectNode()) {
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
it('Node: Encrypt and decrypt text message roundtrip', async function() {
|
it('Node: Encrypt and decrypt text message roundtrip', async function() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user