From 2fffc76060fd99f7a14364ef240cfce1d8b35f9c Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Wed, 11 Dec 2019 12:21:00 +0100 Subject: [PATCH] Don't overwrite Web Streams Polyfill to globals Instead, use the ponyfill that's now included in web-stream-tools. Also, convert back to native ReadableStream when we used the ponyfill. --- Gruntfile.js | 2 +- README.md | 6 +- package-lock.json | 23 ++-- package.json | 4 +- src/openpgp.js | 120 +++++++++++---------- src/packet/packet.js | 2 +- src/packet/sym_encrypted_aead_protected.js | 2 +- src/polyfills.js | 3 - src/util.js | 6 +- src/worker/async_proxy.js | 2 +- test/general/openpgp.js | 13 +-- test/general/packet.js | 4 +- test/general/signature.js | 10 +- test/general/streaming.js | 5 +- test/unittests.js | 3 - 15 files changed, 108 insertions(+), 97 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 83879ed2..fa3663ec 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -66,7 +66,7 @@ module.exports = function(grunt) { ["babelify", { global: true, // Only babelify web-streams-polyfill, web-stream-tools, asmcrypto, email-addresses and seek-bzip in node_modules - only: /^(?:.*\/node_modules\/@mattiasbuelens\/web-streams-polyfill\/|.*\/node_modules\/web-stream-tools\/|.*\/node_modules\/asmcrypto\.js\/|.*\/node_modules\/email-addresses\/|.*\/node_modules\/seek-bzip\/|(?!.*\/node_modules\/)).*$/, + only: /^(?:.*\/node_modules\/web-streams-polyfill\/|.*\/node_modules\/web-stream-tools\/|.*\/node_modules\/asmcrypto\.js\/|.*\/node_modules\/email-addresses\/|.*\/node_modules\/seek-bzip\/|(?!.*\/node_modules\/)).*$/, ignore: ['*.min.js'], plugins, presets diff --git a/README.md b/README.md index 7dfa559e..2641c5ce 100644 --- a/README.md +++ b/README.md @@ -280,7 +280,7 @@ Where the value can be any of: ```js (async () => { - const readableStream = new ReadableStream({ + const readableStream = new openpgp.stream.ReadableStream({ start(controller) { controller.enqueue(new Uint8Array([0x01, 0x02, 0x03])); controller.close(); @@ -331,7 +331,7 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html). const privateKey = (await openpgp.key.readArmored([privateKeyArmored])).keys[0]; await privateKey.decrypt(passphrase); - const readableStream = new ReadableStream({ + const readableStream = new openpgp.stream.ReadableStream({ start(controller) { controller.enqueue('Hello, world!'); controller.close(); @@ -513,7 +513,7 @@ Using the private key: ```js (async () => { - var readableStream = new ReadableStream({ + var readableStream = new openpgp.stream.ReadableStream({ start(controller) { controller.enqueue(new Uint8Array([0x01, 0x02, 0x03])); controller.close(); diff --git a/package-lock.json b/package-lock.json index 0302fd99..6c1bc8ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -186,11 +186,10 @@ } } }, - "@mattiasbuelens/web-streams-polyfill": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/@mattiasbuelens/web-streams-polyfill/-/web-streams-polyfill-0.3.1.tgz", - "integrity": "sha512-IW+tCurEH2NL2tA3KKFAMXa90WWmTJMZksnQWMABe3bMijV7hEiOLthy4tKGTnUTV8qVf8WTCApiGmKb75Bxkg==", - "dev": true + "@mattiasbuelens/web-streams-adapter": { + "version": "0.1.0-alpha.3", + "resolved": "https://registry.npmjs.org/@mattiasbuelens/web-streams-adapter/-/web-streams-adapter-0.1.0-alpha.3.tgz", + "integrity": "sha512-zZ5DOPokJLzmvklk151Rk9e79+xos5d9V0azVLy6jX+28113ffoCR0FYUVu0YQr+J4L/qFZI46jXsbv1a9velA==" }, "@sinonjs/commons": { "version": "1.7.1", @@ -8959,8 +8958,18 @@ } }, "web-stream-tools": { - "version": "github:openpgpjs/web-stream-tools#619abcbe2d9115bb005047f609b72d71108edadb", - "from": "github:openpgpjs/web-stream-tools#619abcbe2d9115bb005047f609b72d71108edadb", + "version": "github:openpgpjs/web-stream-tools#699d88c8d8f78d1513769e16cf557929c97bf83c", + "from": "github:openpgpjs/web-stream-tools#699d88c8d8f78d1513769e16cf557929c97bf83c", + "dev": true, + "requires": { + "@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.3", + "web-streams-polyfill": "~2.0.6" + } + }, + "web-streams-polyfill": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-2.0.6.tgz", + "integrity": "sha512-nXOi4fBykO4LzyQhZX3MAGib635KGZBoNTkNXrNIkz0zthEf2QokEWxRb0H632xNLDWtHFb1R6dFGzksjYMSDw==", "dev": true }, "websocket-driver": { diff --git a/package.json b/package.json index 61c971d1..ead1542c 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,6 @@ "sinon": "^4.3.0", "text-encoding-utf-8": "^1.0.2", "whatwg-fetch": "^2.0.3", - "@mattiasbuelens/web-streams-polyfill": "^0.3.1", "asmcrypto.js": "github:openpgpjs/asmcrypto#475cffa5ccb2cf2556427056679414acf3610d1b", "bn.js": "^4.11.8", "buffer": "^5.0.8", @@ -79,10 +78,11 @@ "pako": "^1.0.6", "seek-bzip": "github:openpgpjs/seek-bzip#6187fc025851d35c4e104a25ea15a10b9b8d6f7d", "tweetnacl": "github:openpgpjs/tweetnacl-js#3dae25bd3eaa77173f3405676b595721dde92eec", - "web-stream-tools": "github:openpgpjs/web-stream-tools#619abcbe2d9115bb005047f609b72d71108edadb", + "web-stream-tools": "github:openpgpjs/web-stream-tools#699d88c8d8f78d1513769e16cf557929c97bf83c", "email-addresses": "3.1.0" }, "dependencies": { + "@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.3", "asn1.js": "^5.0.0", "node-fetch": "^2.1.2", "node-localstorage": "~1.3.0" diff --git a/src/openpgp.js b/src/openpgp.js index 020f762d..21452b15 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -40,6 +40,7 @@ */ import stream from 'web-stream-tools'; +import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'; import * as messageLib from './message'; import { CleartextMessage } from './cleartext'; import { generate, reformat } from './key'; @@ -49,6 +50,8 @@ import './polyfills'; import util from './util'; import AsyncProxy from './worker/async_proxy'; +const toNativeReadable = global.ReadableStream && createReadableStreamWrapper(global.ReadableStream); + ////////////////////////// // // // Web Worker setup // @@ -284,22 +287,22 @@ export function encryptKey({ privateKey, passphrase }) { /** * Encrypts message text/data with public keys, passwords or both at once. At least either public keys or passwords * must be specified. If private keys are specified, those will be used to sign the message. - * @param {Message} message message to be encrypted as created by openpgp.message.fromText or openpgp.message.fromBinary - * @param {Key|Array} publicKeys (optional) array of keys or single key, used to encrypt the message - * @param {Key|Array} privateKeys (optional) private keys for signing. If omitted message will not be signed - * @param {String|Array} passwords (optional) array of passwords or a single password to encrypt the message - * @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String } - * @param {module:enums.compression} compression (optional) which compression algorithm to compress the message with, defaults to what is specified in config - * @param {Boolean} armor (optional) whether the return values should be ascii armored (true, the default) or binary (false) - * @param {'web'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. - * @param {Boolean} detached (optional) if the signature should be detached (if true, signature will be added to returned object) - * @param {Signature} signature (optional) a detached signature to add to the encrypted message - * @param {Boolean} returnSessionKey (optional) if the unencrypted session key should be added to returned object - * @param {Boolean} wildcard (optional) use a key ID of 0 instead of the public key IDs - * @param {Date} date (optional) override the creation date of the message signature - * @param {Array} fromUserIds (optional) array of user IDs to sign with, one per key in `privateKeys`, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }] - * @param {Array} toUserIds (optional) array of user IDs to encrypt for, one per key in `publicKeys`, e.g. [{ name:'Robert Receiver', email:'robert@openpgp.org' }] - * @returns {Promise} Object containing encrypted (and optionally signed) message in the form: + * @param {Message} message message to be encrypted as created by openpgp.message.fromText or openpgp.message.fromBinary + * @param {Key|Array} publicKeys (optional) array of keys or single key, used to encrypt the message + * @param {Key|Array} privateKeys (optional) private keys for signing. If omitted message will not be signed + * @param {String|Array} passwords (optional) array of passwords or a single password to encrypt the message + * @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String } + * @param {module:enums.compression} compression (optional) which compression algorithm to compress the message with, defaults to what is specified in config + * @param {Boolean} armor (optional) whether the return values should be ascii armored (true, the default) or binary (false) + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. + * @param {Boolean} detached (optional) if the signature should be detached (if true, signature will be added to returned object) + * @param {Signature} signature (optional) a detached signature to add to the encrypted message + * @param {Boolean} returnSessionKey (optional) if the unencrypted session key should be added to returned object + * @param {Boolean} wildcard (optional) use a key ID of 0 instead of the public key IDs + * @param {Date} date (optional) override the creation date of the message signature + * @param {Array} fromUserIds (optional) array of user IDs to sign with, one per key in `privateKeys`, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }] + * @param {Array} toUserIds (optional) array of user IDs to encrypt for, one per key in `publicKeys`, e.g. [{ name:'Robert Receiver', email:'robert@openpgp.org' }] + * @returns {Promise} Object containing encrypted (and optionally signed) message in the form: * * { * data: String|ReadableStream|NodeStream, (if `armor` was true, the default) @@ -345,16 +348,16 @@ export function encrypt({ message, publicKeys, privateKeys, passwords, sessionKe /** * Decrypts a message with the user's private key, a session key or a password. Either a private key, * a session key or a password must be specified. - * @param {Message} message the message object with the encrypted data - * @param {Key|Array} privateKeys (optional) private keys with decrypted secret key data or session key - * @param {String|Array} passwords (optional) passwords to decrypt the message - * @param {Object|Array} sessionKeys (optional) session keys in the form: { data:Uint8Array, algorithm:String } - * @param {Key|Array} publicKeys (optional) array of public keys or single key, to verify signatures - * @param {'utf8'|'binary'} format (optional) whether to return data as a string(Stream) or Uint8Array(Stream). If 'utf8' (the default), also normalize newlines. - * @param {'web'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. - * @param {Signature} signature (optional) detached signature for verification - * @param {Date} date (optional) use the given date for verification instead of the current time - * @returns {Promise} Object containing decrypted and verified message in the form: + * @param {Message} message the message object with the encrypted data + * @param {Key|Array} privateKeys (optional) private keys with decrypted secret key data or session key + * @param {String|Array} passwords (optional) passwords to decrypt the message + * @param {Object|Array} sessionKeys (optional) session keys in the form: { data:Uint8Array, algorithm:String } + * @param {Key|Array} publicKeys (optional) array of public keys or single key, to verify signatures + * @param {'utf8'|'binary'} format (optional) whether to return data as a string(Stream) or Uint8Array(Stream). If 'utf8' (the default), also normalize newlines. + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. + * @param {Signature} signature (optional) detached signature for verification + * @param {Date} date (optional) use the given date for verification instead of the current time + * @returns {Promise} Object containing decrypted and verified message in the form: * * { * data: String|ReadableStream|NodeStream, (if format was 'utf8', the default) @@ -404,14 +407,14 @@ export function decrypt({ message, privateKeys, passwords, sessionKeys, publicKe /** * Signs a cleartext message. - * @param {CleartextMessage|Message} message (cleartext) message to be signed - * @param {Key|Array} privateKeys array of keys or single key with decrypted secret key data to sign cleartext - * @param {Boolean} armor (optional) whether the return values should be ascii armored (true, the default) or binary (false) - * @param {'web'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. - * @param {Boolean} detached (optional) if the return value should contain a detached signature - * @param {Date} date (optional) override the creation date of the signature - * @param {Array} fromUserIds (optional) array of user IDs to sign with, one per key in `privateKeys`, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }] - * @returns {Promise} Object containing signed message in the form: + * @param {CleartextMessage|Message} message (cleartext) message to be signed + * @param {Key|Array} privateKeys array of keys or single key with decrypted secret key data to sign cleartext + * @param {Boolean} armor (optional) whether the return values should be ascii armored (true, the default) or binary (false) + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. + * @param {Boolean} detached (optional) if the return value should contain a detached signature + * @param {Date} date (optional) override the creation date of the signature + * @param {Array} fromUserIds (optional) array of user IDs to sign with, one per key in `privateKeys`, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }] + * @returns {Promise} Object containing signed message in the form: * * { * data: String|ReadableStream|NodeStream, (if `armor` was true, the default) @@ -459,13 +462,13 @@ export function sign({ message, privateKeys, armor = true, streaming = message & /** * Verifies signatures of cleartext signed message - * @param {Key|Array} publicKeys array of publicKeys or single key, to verify signatures - * @param {CleartextMessage|Message} message (cleartext) message object with signatures - * @param {'utf8'|'binary'} format (optional) whether to return data as a string(Stream) or Uint8Array(Stream). If 'utf8' (the default), also normalize newlines. - * @param {'web'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. - * @param {Signature} signature (optional) detached signature for verification - * @param {Date} date (optional) use the given date for verification instead of the current time - * @returns {Promise} Object containing verified message in the form: + * @param {Key|Array} publicKeys array of publicKeys or single key, to verify signatures + * @param {CleartextMessage|Message} message (cleartext) message object with signatures + * @param {'utf8'|'binary'} format (optional) whether to return data as a string(Stream) or Uint8Array(Stream). If 'utf8' (the default), also normalize newlines. + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return data as a stream. Defaults to the type of stream `message` was created from, if any. + * @param {Signature} signature (optional) detached signature for verification + * @param {Date} date (optional) use the given date for verification instead of the current time + * @returns {Promise} Object containing verified message in the form: * * { * data: String|ReadableStream|NodeStream, (if `message` was a CleartextMessage) @@ -614,37 +617,38 @@ function toArray(param) { /** * Convert data to or from Stream - * @param {Object} data the data to convert - * @param {'web'|'node'|false} streaming (optional) whether to return a ReadableStream - * @param {'utf8'|'binary'} encoding (optional) how to return data in Node Readable streams - * @returns {Object} the data in the respective format + * @param {Object} data the data to convert + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return a ReadableStream, and of what type + * @param {'utf8'|'binary'} encoding (optional) how to return data in Node Readable streams + * @returns {Object} the data in the respective format */ async function convertStream(data, streaming, encoding = 'utf8') { - if (!streaming && util.isStream(data)) { + let streamType = util.isStream(data); + if (!streaming && streamType) { return stream.readToEnd(data); } - if (streaming && !util.isStream(data)) { - data = new ReadableStream({ - start(controller) { - controller.enqueue(data); - controller.close(); - } - }); + if (streaming && !streamType) { + data = stream.toStream(data); + streamType = util.isStream(data); } if (streaming === 'node') { data = stream.webToNode(data); if (encoding !== 'binary') data.setEncoding(encoding); + return data; + } + if (streaming === 'web' && streamType === 'ponyfill' && toNativeReadable) { + return toNativeReadable(data); } return data; } /** * Convert object properties from Stream - * @param {Object} obj the data to convert - * @param {'web'|'node'|false} streaming (optional) whether to return ReadableStreams - * @param {'utf8'|'binary'} encoding (optional) how to return data in Node Readable streams - * @param {Array} keys (optional) which keys to return as streams, if possible - * @returns {Object} the data in the respective format + * @param {Object} obj the data to convert + * @param {'web'|'ponyfill'|'node'|false} streaming (optional) whether to return ReadableStreams, and of what type + * @param {'utf8'|'binary'} encoding (optional) how to return data in Node Readable streams + * @param {Array} keys (optional) which keys to return as streams, if possible + * @returns {Object} the data in the respective format */ async function convertStreams(obj, streaming, encoding = 'utf8', keys = []) { if (Object.prototype.isPrototypeOf(obj) && !Uint8Array.prototype.isPrototypeOf(obj)) { diff --git a/src/packet/packet.js b/src/packet/packet.js index b50c15fa..af399620 100644 --- a/src/packet/packet.js +++ b/src/packet/packet.js @@ -153,7 +153,7 @@ export default { const supportsStreaming = this.supportsStreaming(tag); let packet = null; if (streaming && supportsStreaming) { - const transform = new TransformStream(); + const transform = new stream.TransformStream(); writer = stream.getWriter(transform.writable); packet = transform.readable; callbackReturned = callback({ tag, packet }); diff --git a/src/packet/sym_encrypted_aead_protected.js b/src/packet/sym_encrypted_aead_protected.js index 88564f8d..f5032bec 100644 --- a/src/packet/sym_encrypted_aead_protected.js +++ b/src/packet/sym_encrypted_aead_protected.js @@ -139,7 +139,7 @@ SymEncryptedAEADProtected.prototype.crypt = async function (fn, key, data, strea const iv = this.iv; return stream.transformPair(data, async (readable, writable) => { const reader = stream.getReader(readable); - const buffer = new TransformStream({}, { + const buffer = new stream.TransformStream({}, { highWaterMark: streaming ? util.getHardwareConcurrency() * 2 ** (this.chunkSizeByte + 6) : Infinity, size: array => array.length }); diff --git a/src/polyfills.js b/src/polyfills.js index 27b88917..c0b37e3f 100644 --- a/src/polyfills.js +++ b/src/polyfills.js @@ -49,9 +49,6 @@ if (typeof global !== 'undefined') { } catch (e) {} } -if (typeof TransformStream === 'undefined') { - require('@mattiasbuelens/web-streams-polyfill/es6'); -} if (typeof TextEncoder === 'undefined') { const nodeUtil = util.nodeRequire('util') || {}; global.TextEncoder = nodeUtil.TextEncoder; diff --git a/src/util.js b/src/util.js index a531db82..2da21f88 100644 --- a/src/util.js +++ b/src/util.js @@ -113,11 +113,11 @@ export default { * @param {Object} obj * @returns {Object} */ - restoreStreams: function(obj) { + restoreStreams: function(obj, streaming) { if (Object.prototype.isPrototypeOf(obj) && !Uint8Array.prototype.isPrototypeOf(obj)) { Object.entries(obj).forEach(([key, value]) => { // recursively search all children if (Object.prototype.toString.call(value) === '[object MessagePort]') { - obj[key] = new ReadableStream({ + obj[key] = new (streaming === 'web' ? global.ReadableStream : stream.ReadableStream)({ pull(controller) { return new Promise(resolve => { value.onmessage = evt => { @@ -143,7 +143,7 @@ export default { }, { highWaterMark: 0 }); return; } - util.restoreStreams(value); + util.restoreStreams(value, streaming); }); } return obj; diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 059f794b..adf08396 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -183,7 +183,7 @@ AsyncProxy.prototype.delegate = function(method, options) { this.workers[workerId].requests++; // remember to handle parsing cloned packets from worker - this.tasks[id] = { resolve: data => resolve(packet.clone.parseClonedPackets(util.restoreStreams(data), method)), reject }; + this.tasks[id] = { resolve: data => resolve(packet.clone.parseClonedPackets(util.restoreStreams(data, options.streaming), method)), reject }; }); }; diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 1458f05f..b3429d8f 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1758,7 +1758,7 @@ describe('OpenPGP.js public api tests', function() { await Promise.all([ encrypted, openpgp.stream.toStream(encrypted), - new ReadableStream({ + new openpgp.stream.ReadableStream({ start() { this.remaining = encrypted.split('\n'); }, @@ -1998,7 +1998,7 @@ describe('OpenPGP.js public api tests', function() { it('Streaming encrypt and decrypt small message roundtrip', async function() { let plaintext = []; let i = 0; - const data = new ReadableStream({ + const data = new (global.ReadableStream || openpgp.stream.ReadableStream)({ async pull(controller) { if (i++ < 4) { let randomBytes = await openpgp.crypto.random.getRandomBytes(10); @@ -2011,8 +2011,9 @@ describe('OpenPGP.js public api tests', function() { }); const encrypted = await openpgp.encrypt(modifyCompressionEncryptOptions({ message: openpgp.message.fromBinary(data), - passwords: ['test'], + passwords: ['test'] })); + expect(openpgp.util.isStream(encrypted.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); const msgAsciiArmored = encrypted.data; const message = await openpgp.message.readArmored(msgAsciiArmored); @@ -2021,7 +2022,7 @@ describe('OpenPGP.js public api tests', function() { message, format: 'binary' }); - expect(openpgp.util.isStream(decrypted.data)).to.equal('web'); + expect(openpgp.util.isStream(decrypted.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); expect(await openpgp.stream.readToEnd(decrypted.data)).to.deep.equal(openpgp.util.concatUint8Array(plaintext)); }); }); @@ -2328,7 +2329,7 @@ describe('OpenPGP.js public api tests', function() { format: 'binary' }; return openpgp.sign(signOpt).then(async function (signed) { - expect(openpgp.util.isStream(signed.data)).to.equal('web'); + expect(openpgp.util.isStream(signed.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); const message = await openpgp.message.read(signed.data); message.packets.concat(await openpgp.stream.readToEnd(message.packets.stream, _ => _)); const packets = new openpgp.packet.List(); @@ -2337,7 +2338,7 @@ describe('OpenPGP.js public api tests', function() { verifyOpt.message = new openpgp.message.Message(packets); return openpgp.verify(verifyOpt); }).then(async function (verified) { - expect(openpgp.stream.isStream(verified.data)).to.equal('web'); + expect(openpgp.stream.isStream(verified.data)).to.equal(global.ReadableStream ? 'web' : 'ponyfill'); expect([].slice.call(await openpgp.stream.readToEnd(verified.data))).to.deep.equal([].slice.call(data)); expect(await verified.signatures[0].verified).to.be.true; expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid)) diff --git a/test/general/packet.js b/test/general/packet.js index aacf9048..52b00c57 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -759,7 +759,7 @@ describe("Packet", function() { expect(payload[2].verify( key[0], openpgp.enums.signature.binary, payload[1] )).to.eventually.be.true, - openpgp.stream.pipe(payload[1].getBytes(), new WritableStream()) + openpgp.stream.pipe(payload[1].getBytes(), new openpgp.stream.WritableStream()) ]); }); }); @@ -942,7 +942,7 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+ await Promise.all([ expect(signed2[1].verify(key, openpgp.enums.signature.text, signed2[0])).to.eventually.be.true, - openpgp.stream.pipe(signed2[0].getBytes(), new WritableStream()) + openpgp.stream.pipe(signed2[0].getBytes(), new openpgp.stream.WritableStream()) ]); }); }); diff --git a/test/general/signature.js b/test/general/signature.js index f54e3500..ea005e99 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -956,7 +956,7 @@ bwM= const sMsg = await openpgp.message.readArmored(signedArmor); const pub_key = (await openpgp.key.readArmored(pub_key_arm2)).keys[0]; const verified = await sMsg.verify([pub_key]); - openpgp.stream.pipe(sMsg.getLiteralData(), new WritableStream()); + openpgp.stream.pipe(sMsg.getLiteralData(), new openpgp.stream.WritableStream()); expect(verified).to.exist; expect(verified).to.have.length(1); expect(await verified[0].verified).to.be.true; @@ -1064,7 +1064,7 @@ bwM= const sMsg = await openpgp.message.readArmored(signature_with_critical_notation); const pub_key = (await openpgp.key.readArmored(pub_key_arm2)).keys[0]; const verified = await sMsg.verify([pub_key]); - openpgp.stream.pipe(sMsg.getLiteralData(), new WritableStream()); + openpgp.stream.pipe(sMsg.getLiteralData(), new openpgp.stream.WritableStream()); expect(await verified[0].verified).to.be.true; } finally { openpgp.config.known_notations.pop(); @@ -1240,7 +1240,7 @@ yYDnCgA= -----END PGP MESSAGE-----`.split(''); const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; - const sMsg = await openpgp.message.readArmored(new ReadableStream({ + const sMsg = await openpgp.message.readArmored(new openpgp.stream.ReadableStream({ async pull(controller) { await new Promise(setTimeout); controller.enqueue(msg_armor.shift()); @@ -1306,7 +1306,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA -----END PGP MESSAGE-----`.split(''); const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; - const sMsg = await openpgp.message.readArmored(new ReadableStream({ + const sMsg = await openpgp.message.readArmored(new openpgp.stream.ReadableStream({ async pull(controller) { await new Promise(setTimeout); controller.enqueue(msg_armor.shift()); @@ -1608,7 +1608,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA const msg = openpgp.message.fromText(content); await msg.appendSignature(detachedSig); return msg.verify(publicKeys).then(async result => { - openpgp.stream.pipe(msg.getLiteralData(), new WritableStream()); + openpgp.stream.pipe(msg.getLiteralData(), new openpgp.stream.WritableStream()); expect(await result[0].verified).to.be.true; }); }); diff --git a/test/general/streaming.js b/test/general/streaming.js index df6b634d..731051ce 100644 --- a/test/general/streaming.js +++ b/test/general/streaming.js @@ -9,6 +9,8 @@ const { expect } = chai; const { stream, util } = openpgp; +const ReadableStream = global.ReadableStream || openpgp.stream.ReadableStream; + const pub_key = ['-----BEGIN PGP PUBLIC KEY BLOCK-----', 'Version: GnuPG v2.0.19 (GNU/Linux)', @@ -605,6 +607,7 @@ function tests() { }); await new Promise(resolve => setTimeout(resolve)); await stream.cancel(transformed); + await new Promise(resolve => setTimeout(resolve)); expect(canceled).to.be.true; }); @@ -933,7 +936,7 @@ describe('Streaming', function() { tryTests('WhatWG Streams', tests, { if: true, beforeEach: function() { - expectedType = 'web'; + expectedType = global.ReadableStream ? 'web' : 'ponyfill'; } }); diff --git a/test/unittests.js b/test/unittests.js index c639037c..24015b74 100644 --- a/test/unittests.js +++ b/test/unittests.js @@ -5,9 +5,6 @@ if (typeof Symbol === 'undefined') { if (typeof Promise === 'undefined') { require('core-js/fn/promise'); } -if (typeof TransformStream === 'undefined') { - require('@mattiasbuelens/web-streams-polyfill'); -} (typeof window !== 'undefined' ? window : global).resolves = function(val) { return new Promise(function(res) { res(val); });