Unexport openpgp.stream (#1291)

This change allows us to only load the `ReadableStream` polyfill when
needed without behaving inconsistently in the external API.

Users of the library should use the global `ReadableStream` or Node.js
`stream.Readable` instead, or import a polyfill if needed. This patch
loosens the detection criteria such that polyfilled streams are better
detected.
This commit is contained in:
Daniel Huigens 2021-05-05 20:20:20 +02:00 committed by GitHub
parent 31fe960261
commit 93b77669bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 182 additions and 178 deletions

View File

@ -341,7 +341,7 @@ Where the value can be any of:
```js ```js
(async () => { (async () => {
const readableStream = new openpgp.stream.ReadableStream({ const readableStream = new ReadableStream({
start(controller) { start(controller) {
controller.enqueue(new Uint8Array([0x01, 0x02, 0x03])); controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));
controller.close(); controller.close();
@ -358,24 +358,19 @@ Where the value can be any of:
// Either pipe the above stream somewhere, pass it to another function, // Either pipe the above stream somewhere, pass it to another function,
// or read it manually as follows: // or read it manually as follows:
const reader = openpgp.stream.getReader(encrypted); for await (const chunk of encrypted) {
while (true) { console.log('new chunk:', chunk); // Uint8Array
const { done, value } = await reader.read();
if (done) break;
console.log('new chunk:', value); // Uint8Array
} }
// Or, in Node.js, you can pipe the above stream as follows:
const nodeStream = openpgp.stream.webToNode(encrypted);
nodeStream.pipe(nodeWritableStream);
})(); })();
``` ```
For more information on creating ReadableStreams, see [the MDN Documentation on `new For more information on using ReadableStreams, see [the MDN Documentation on the
ReadableStream()`](https://developer.mozilla.org/docs/Web/API/ReadableStream/ReadableStream). Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).
For more information on reading streams using `openpgp.stream`, see the documentation of
[the web-stream-tools dependency](https://openpgpjs.org/web-stream-tools/), particularly You can also pass a [Node.js `Readable`
its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html). stream](https://nodejs.org/api/stream.html#stream_class_stream_readable), in
which case OpenPGP.js will return a Node.js `Readable` stream as well, which you
can `.pipe()` to a `Writable` stream, for example.
#### Streaming encrypt and decrypt *String* data with PGP keys #### Streaming encrypt and decrypt *String* data with PGP keys
@ -397,7 +392,7 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html).
passphrase passphrase
}); });
const readableStream = new openpgp.stream.ReadableStream({ const readableStream = new ReadableStream({
start(controller) { start(controller) {
controller.enqueue('Hello, world!'); controller.enqueue('Hello, world!');
controller.close(); controller.close();
@ -419,7 +414,11 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html).
publicKeys: publicKey, // for verification (optional) publicKeys: publicKey, // for verification (optional)
privateKeys: privateKey // for decryption privateKeys: privateKey // for decryption
}); });
const plaintext = await openpgp.stream.readToEnd(decrypted.data); const chunks = [];
for await (const chunk of decrypted.data) {
chunks.push(chunk);
}
const plaintext = chunks.join('');
console.log(plaintext); // 'Hello, World!' console.log(plaintext); // 'Hello, World!'
})(); })();
``` ```
@ -574,7 +573,7 @@ Using the private key:
```js ```js
(async () => { (async () => {
var readableStream = new openpgp.stream.ReadableStream({ var readableStream = new ReadableStream({
start(controller) { start(controller) {
controller.enqueue(new Uint8Array([0x01, 0x02, 0x03])); controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));
controller.close(); controller.close();
@ -606,7 +605,7 @@ Using the private key:
publicKeys: await openpgp.readKey({ armoredKey: publicKeyArmored }) // for verification publicKeys: await openpgp.readKey({ armoredKey: publicKeyArmored }) // for verification
}); });
await openpgp.stream.readToEnd(verified.data); for await (const chunk of verified.data) {}
// Note: you *have* to read `verified.data` in some way or other, // Note: you *have* to read `verified.data` in some way or other,
// even if you don't need it, as that is what triggers the // even if you don't need it, as that is what triggers the
// verification of the data. // verification of the data.

6
package-lock.json generated
View File

@ -261,9 +261,9 @@
"dev": true "dev": true
}, },
"@openpgp/web-stream-tools": { "@openpgp/web-stream-tools": {
"version": "0.0.4", "version": "0.0.5",
"resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.4.tgz", "resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.5.tgz",
"integrity": "sha512-u6KEn/J4A2ojBkQ0qc9Jgs4nNVK+u7snY3h/SlvwdE8+STh/R3vVXJgkIgsAmWfweE0Ng4TjakrNY9tBI31VJQ==", "integrity": "sha512-tdUCdiMi5ogmZlAbR4cQXZDbK34QB8iEnJ434m9bj4P7sxvKg2KKKbEiB4EQb2AWhj/SNKcoNUHhT9WxTqKimQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.5", "@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.5",

View File

@ -57,7 +57,7 @@
"@openpgp/pako": "^1.0.11", "@openpgp/pako": "^1.0.11",
"@openpgp/seek-bzip": "^1.0.5-git", "@openpgp/seek-bzip": "^1.0.5-git",
"@openpgp/tweetnacl": "^1.0.3", "@openpgp/tweetnacl": "^1.0.3",
"@openpgp/web-stream-tools": "0.0.4", "@openpgp/web-stream-tools": "0.0.5",
"@rollup/plugin-commonjs": "^11.1.0", "@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3", "@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.2", "@rollup/plugin-replace": "^2.3.2",

View File

@ -340,6 +340,11 @@ export function unarmor(input, config = defaultConfig) {
} catch (e) { } catch (e) {
reject(e); reject(e);
} }
}).then(async result => {
if (stream.isArrayStream(result.data)) {
result.data = await stream.readToEnd(result.data);
}
return result;
}); });
} }

View File

@ -21,9 +21,6 @@ export { CleartextMessage, readCleartextMessage, createCleartextMessage } from '
export * from './packet'; export * from './packet';
import * as streamMod from '@openpgp/web-stream-tools'; // eslint-disable-line
export const stream = streamMod;
export * from './encoding/armor'; export * from './encoding/armor';
export { default as enums } from './enums'; export { default as enums } from './enums';

View File

@ -382,7 +382,7 @@ NJCB6+LWtabSoVIjNVgKwyKqyTLaESNwC2ogZwkdE8qPGiDFEHo4Gg9zuRof
`; `;
const { type, data } = await openpgp.unarmor(pubKey); const { type, data } = await openpgp.unarmor(pubKey);
const armor = await openpgp.stream.readToEnd(openpgp.armor(type, data)); const armor = await openpgp.armor(type, data);
expect( expect(
armor armor
.replace(/^(Version|Comment): .*$\n/mg, '') .replace(/^(Version|Comment): .*$\n/mg, '')

View File

@ -7,6 +7,8 @@ const random = require('../../src/crypto/random');
const util = require('../../src/util'); const util = require('../../src/util');
const keyIDType = require('../../src/type/keyid'); const keyIDType = require('../../src/type/keyid');
const stream = require('@openpgp/web-stream-tools');
const spy = require('sinon/lib/sinon/spy'); const spy = require('sinon/lib/sinon/spy');
const input = require('./testInputs.js'); const input = require('./testInputs.js');
const chai = require('chai'); const chai = require('chai');
@ -1065,12 +1067,12 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey publicKeys: publicKey
}); });
const { data: streamedData, signatures } = await openpgp.decrypt({ const { data: streamedData, signatures } = await openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: openpgp.stream.toStream(encrypted) }), message: await openpgp.readMessage({ armoredMessage: stream.toStream(encrypted) }),
privateKeys: privateKey, privateKeys: privateKey,
publicKeys: publicKey, publicKeys: publicKey,
expectSigned: true expectSigned: true
}); });
const data = await openpgp.stream.readToEnd(streamedData); const data = await stream.readToEnd(streamedData);
expect(data).to.equal(plaintext); expect(data).to.equal(plaintext);
expect(await signatures[0].verified).to.be.true; expect(await signatures[0].verified).to.be.true;
}); });
@ -1088,7 +1090,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
privateKeys: privateKey privateKeys: privateKey
}); });
await expect(openpgp.decrypt({ await expect(openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: openpgp.stream.toStream(encrypted) }), message: await openpgp.readMessage({ armoredMessage: stream.toStream(encrypted) }),
privateKeys: privateKey, privateKeys: privateKey,
expectSigned: true expectSigned: true
})).to.be.eventually.rejectedWith(/Public keys are required/); })).to.be.eventually.rejectedWith(/Public keys are required/);
@ -1106,7 +1108,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey publicKeys: publicKey
}); });
await expect(openpgp.decrypt({ await expect(openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: openpgp.stream.toStream(encrypted) }), message: await openpgp.readMessage({ armoredMessage: stream.toStream(encrypted) }),
privateKeys: privateKey, privateKeys: privateKey,
publicKeys: publicKey, publicKeys: publicKey,
expectSigned: true expectSigned: true
@ -1127,13 +1129,13 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
privateKeys: privateKey privateKeys: privateKey
}); });
const { data: streamedData } = await openpgp.decrypt({ const { data: streamedData } = await openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: openpgp.stream.toStream(encrypted) }), message: await openpgp.readMessage({ armoredMessage: stream.toStream(encrypted) }),
privateKeys: privateKey, privateKeys: privateKey,
publicKeys: wrongPublicKey, publicKeys: wrongPublicKey,
expectSigned: true expectSigned: true
}); });
await expect( await expect(
openpgp.stream.readToEnd(streamedData) stream.readToEnd(streamedData)
).to.be.eventually.rejectedWith(/Could not find signing key/); ).to.be.eventually.rejectedWith(/Could not find signing key/);
}); });
}); });
@ -1229,11 +1231,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
privateKeys: privateKey privateKeys: privateKey
}); });
const { data: streamedData, signatures } = await openpgp.verify({ const { data: streamedData, signatures } = await openpgp.verify({
message: await readMessage({ armoredMessage: openpgp.stream.toStream(signed) }), message: await readMessage({ armoredMessage: stream.toStream(signed) }),
publicKeys: publicKey, publicKeys: publicKey,
expectSigned: true expectSigned: true
}); });
const data = await openpgp.stream.readToEnd(streamedData); const data = await stream.readToEnd(streamedData);
expect(data).to.equal(text); expect(data).to.equal(text);
expect(await signatures[0].verified).to.be.true; expect(await signatures[0].verified).to.be.true;
}); });
@ -1244,7 +1246,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const publicKey = await openpgp.readKey({ armoredKey: pub_key }); const publicKey = await openpgp.readKey({ armoredKey: pub_key });
await expect(openpgp.verify({ await expect(openpgp.verify({
message: await createMessage({ text: openpgp.stream.toStream(text) }), message: await createMessage({ text: stream.toStream(text) }),
publicKeys: publicKey, publicKeys: publicKey,
expectSigned: true expectSigned: true
})).to.be.eventually.rejectedWith(/Message is not signed/); })).to.be.eventually.rejectedWith(/Message is not signed/);
@ -1264,12 +1266,12 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
privateKeys: privateKey privateKeys: privateKey
}); });
const { data: streamedData } = await openpgp.verify({ const { data: streamedData } = await openpgp.verify({
message: await readMessage({ armoredMessage: openpgp.stream.toStream(signed) }), message: await readMessage({ armoredMessage: stream.toStream(signed) }),
publicKeys: wrongPublicKey, publicKeys: wrongPublicKey,
expectSigned: true expectSigned: true
}); });
await expect( await expect(
openpgp.stream.readToEnd(streamedData) stream.readToEnd(streamedData)
).to.be.eventually.rejectedWith(/Could not find signing key/); ).to.be.eventually.rejectedWith(/Could not find signing key/);
}); });
} }
@ -2152,13 +2154,19 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
throw new Error("Was not able to successfully modify checksum"); throw new Error("Was not able to successfully modify checksum");
} }
const badBodyEncrypted = data.replace(/\n=([a-zA-Z0-9/+]{4})/, 'aaa\n=$1'); const badBodyEncrypted = data.replace(/\n=([a-zA-Z0-9/+]{4})/, 'aaa\n=$1');
await stream.loadStreamsPonyfill();
for (let allow_streaming = 1; allow_streaming >= 0; allow_streaming--) { for (let allow_streaming = 1; allow_streaming >= 0; allow_streaming--) {
openpgp.config.allowUnauthenticatedStream = !!allow_streaming; openpgp.config.allowUnauthenticatedStream = !!allow_streaming;
await Promise.all([badSumEncrypted, badBodyEncrypted].map(async (encrypted, i) => { await Promise.all([badSumEncrypted, badBodyEncrypted].map(async (encrypted, i) => {
await Promise.all([ await Promise.all([
encrypted, encrypted,
openpgp.stream.toStream(encrypted), new stream.ReadableStream({
new openpgp.stream.ReadableStream({ start(controller) {
controller.enqueue(encrypted);
controller.close();
}
}),
new stream.ReadableStream({
start() { start() {
this.remaining = encrypted.split('\n'); this.remaining = encrypted.split('\n');
}, },
@ -2178,7 +2186,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
stepReached = 1; stepReached = 1;
const { data: decrypted } = await openpgp.decrypt({ message: message, privateKeys: [key] }); const { data: decrypted } = await openpgp.decrypt({ message: message, privateKeys: [key] });
stepReached = 2; stepReached = 2;
await openpgp.stream.readToEnd(decrypted); await stream.readToEnd(decrypted);
} catch (e) { } catch (e) {
expect(e.message).to.match(/Ascii armor integrity check on message failed/); expect(e.message).to.match(/Ascii armor integrity check on message failed/);
expect(stepReached).to.equal( expect(stepReached).to.equal(
@ -2407,7 +2415,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const plaintext = []; const plaintext = [];
let i = 0; let i = 0;
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 ReadableStream = useNativeStream ? global.ReadableStream : openpgp.stream.ReadableStream; await stream.loadStreamsPonyfill();
const ReadableStream = useNativeStream ? global.ReadableStream : stream.ReadableStream;
const data = new ReadableStream({ const data = new ReadableStream({
async pull(controller) { async pull(controller) {
if (i++ < 4) { if (i++ < 4) {
@ -2423,7 +2432,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
})); }));
expect(openpgp.stream.isStream(encrypted)).to.equal(useNativeStream ? 'web' : 'ponyfill'); expect(stream.isStream(encrypted)).to.equal(useNativeStream ? 'web' : 'web-like');
const message = await openpgp.readMessage({ armoredMessage: encrypted }); const message = await openpgp.readMessage({ armoredMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -2431,8 +2440,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(useNativeStream ? 'web' : 'ponyfill'); expect(stream.isStream(decrypted.data)).to.equal(useNativeStream ? 'web' : 'web-like');
expect(await openpgp.stream.readToEnd(decrypted.data)).to.deep.equal(util.concatUint8Array(plaintext)); expect(await stream.readToEnd(decrypted.data)).to.deep.equal(util.concatUint8Array(plaintext));
}); });
}); });
}); });
@ -2719,7 +2728,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
}; };
return openpgp.sign(signOpt).then(async function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
const message = await openpgp.readMessage({ binaryMessage: signed }); const message = await openpgp.readMessage({ binaryMessage: signed });
message.packets.push(...await openpgp.stream.readToEnd(message.packets.stream, _ => _)); message.packets.push(...await stream.readToEnd(message.packets.stream, _ => _));
const packets = new openpgp.PacketList(); const packets = new openpgp.PacketList();
packets.push(message.packets.findPacket(openpgp.enums.packet.signature)); packets.push(message.packets.findPacket(openpgp.enums.packet.signature));
packets.push(message.packets.findPacket(openpgp.enums.packet.literalData)); packets.push(message.packets.findPacket(openpgp.enums.packet.literalData));
@ -2757,21 +2766,21 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
format: 'binary' format: 'binary'
}; };
return openpgp.sign(signOpt).then(async function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
expect(openpgp.stream.isStream(signed)).to.equal(global.ReadableStream ? 'web' : 'node'); expect(stream.isStream(signed)).to.equal(global.ReadableStream ? 'web' : 'node');
const message = await openpgp.readMessage({ binaryMessage: signed }); const message = await openpgp.readMessage({ binaryMessage: signed });
message.packets.push(...await openpgp.stream.readToEnd(message.packets.stream, _ => _)); message.packets.push(...await stream.readToEnd(message.packets.stream, _ => _));
const packets = new openpgp.PacketList(); const packets = new openpgp.PacketList();
packets.push(message.packets.findPacket(openpgp.enums.packet.signature)); packets.push(message.packets.findPacket(openpgp.enums.packet.signature));
packets.push(message.packets.findPacket(openpgp.enums.packet.literalData)); packets.push(message.packets.findPacket(openpgp.enums.packet.literalData));
verifyOpt.message = await openpgp.readMessage({ verifyOpt.message = await openpgp.readMessage({
binaryMessage: openpgp.stream[ binaryMessage: stream[
global.ReadableStream ? (global.ReadableStream === openpgp.stream.ReadableStream ? 'toStream' : 'toNativeReadable') : 'webToNode' global.ReadableStream ? (global.ReadableStream === stream.ReadableStream ? 'toStream' : 'toNativeReadable') : 'webToNode'
](packets.write()) ](packets.write())
}); });
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(openpgp.stream.isStream(verified.data)).to.equal(global.ReadableStream ? 'web' : 'node'); expect(stream.isStream(verified.data)).to.equal(global.ReadableStream ? 'web' : 'node');
expect([].slice.call(await openpgp.stream.readToEnd(verified.data))).to.deep.equal([].slice.call(data)); expect([].slice.call(await stream.readToEnd(verified.data))).to.deep.equal([].slice.call(data));
expect(await verified.signatures[0].verified).to.be.true; expect(await verified.signatures[0].verified).to.be.true;
expect(await privateKey.getSigningKey(verified.signatures[0].keyID)) expect(await privateKey.getSigningKey(verified.signatures[0].keyID))
.to.be.not.null; .to.be.not.null;
@ -2795,7 +2804,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literalData); const literals = packets.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
expect(+literals[0].date).to.equal(+future); expect(+literals[0].date).to.equal(+future);
expect(await openpgp.stream.readToEnd(packets.getText())).to.equal(plaintext); expect(await stream.readToEnd(packets.getText())).to.equal(plaintext);
}); });
}); });
@ -2816,7 +2825,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literalData); const literals = packets.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
expect(+literals[0].date).to.equal(+past); expect(+literals[0].date).to.equal(+past);
expect(await openpgp.stream.readToEnd(packets.getLiteralData())).to.deep.equal(data); expect(await stream.readToEnd(packets.getLiteralData())).to.deep.equal(data);
}); });
}); });
@ -2838,7 +2847,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
expect(+literals[0].date).to.equal(+past); expect(+literals[0].date).to.equal(+past);
const signatures = await message.verify([publicKey_2000_2008], past, undefined, openpgp.config); const signatures = await message.verify([publicKey_2000_2008], past, undefined, openpgp.config);
expect(await openpgp.stream.readToEnd(message.getText())).to.equal(plaintext); expect(await stream.readToEnd(message.getText())).to.equal(plaintext);
expect(+(await signatures[0].signature).packets[0].created).to.equal(+past); expect(+(await signatures[0].signature).packets[0].created).to.equal(+past);
expect(await signatures[0].verified).to.be.true; expect(await signatures[0].verified).to.be.true;
expect(await privateKey_2000_2008.getSigningKey(signatures[0].keyID, past)) expect(await privateKey_2000_2008.getSigningKey(signatures[0].keyID, past))
@ -2867,7 +2876,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
expect(literals[0].format).to.equal('binary'); expect(literals[0].format).to.equal('binary');
expect(+literals[0].date).to.equal(+future); expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config); const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await openpgp.stream.readToEnd(message.getLiteralData())).to.deep.equal(data); expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
expect(+(await signatures[0].signature).packets[0].created).to.equal(+future); expect(+(await signatures[0].signature).packets[0].created).to.equal(+future);
expect(await signatures[0].verified).to.be.true; expect(await signatures[0].verified).to.be.true;
expect(await privateKey_2038_2045.getSigningKey(signatures[0].keyID, future)) expect(await privateKey_2038_2045.getSigningKey(signatures[0].keyID, future))
@ -2896,7 +2905,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
expect(literals[0].format).to.equal('mime'); expect(literals[0].format).to.equal('mime');
expect(+literals[0].date).to.equal(+future); expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config); const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await openpgp.stream.readToEnd(message.getLiteralData())).to.deep.equal(data); expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
expect(+(await signatures[0].signature).packets[0].created).to.equal(+future); expect(+(await signatures[0].signature).packets[0].created).to.equal(+future);
expect(await signatures[0].verified).to.be.true; expect(await signatures[0].verified).to.be.true;
expect(await privateKey_2038_2045.getSigningKey(signatures[0].keyID, future)) expect(await privateKey_2038_2045.getSigningKey(signatures[0].keyID, future))
@ -3202,7 +3211,7 @@ Bw==
-----END PGP PRIVATE KEY BLOCK-----` -----END PGP PRIVATE KEY BLOCK-----`
}); });
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: openpgp.stream.toStream(`-----BEGIN PGP MESSAGE----- armoredMessage: stream.toStream(`-----BEGIN PGP MESSAGE-----
wV4D+3VwOibHmagSAQdATlMJlvrkaq46zMkbIuKBOJO5X3ugVwZpEyAterQC wV4D+3VwOibHmagSAQdATlMJlvrkaq46zMkbIuKBOJO5X3ugVwZpEyAterQC
/RUw0OPWeO+4swh/U7ZurV8cRr/fPnyGUUKI7rI+va3kWUZv4RRpUs7eYE57 /RUw0OPWeO+4swh/U7ZurV8cRr/fPnyGUUKI7rI+va3kWUZv4RRpUs7eYE57
@ -3214,7 +3223,7 @@ bsZgJWVlAa5eil6J9ePX2xbo1vVAkLQdzE9+1jL+l7PRIZuVBQ==
-----END PGP MESSAGE-----`) -----END PGP MESSAGE-----`)
}); });
const decrypted = await openpgp.decrypt({ message, privateKeys: key, publicKeys: key, config: { allowUnauthenticatedMessages: true } }); const decrypted = await openpgp.decrypt({ message, privateKeys: key, publicKeys: key, config: { allowUnauthenticatedMessages: true } });
const data = await openpgp.stream.readToEnd(decrypted.data); const data = await stream.readToEnd(decrypted.data);
expect(data).to.equal('test'); expect(data).to.equal('test');
}); });
}); });

View File

@ -4,6 +4,8 @@ const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp
const crypto = require('../../src/crypto'); const crypto = require('../../src/crypto');
const util = require('../../src/util'); const util = require('../../src/util');
const stream = require('@openpgp/web-stream-tools');
const stub = require('sinon/lib/sinon/stub'); const stub = require('sinon/lib/sinon/stub');
const chai = require('chai'); const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
@ -12,8 +14,8 @@ const { expect } = chai;
const input = require('./testInputs.js'); const input = require('./testInputs.js');
function stringify(array) { function stringify(array) {
if (openpgp.stream.isStream(array)) { if (stream.isStream(array)) {
return openpgp.stream.readToEnd(array).then(stringify); return stream.readToEnd(array).then(stringify);
} }
if (!util.isUint8Array(array)) { if (!util.isUint8Array(array)) {
@ -174,7 +176,7 @@ module.exports = () => describe("Packet", function() {
await msg2.read(msg.write(), allAllowedPackets); await msg2.read(msg.write(), allAllowedPackets);
return msg2[0].decrypt(algo, key); return msg2[0].decrypt(algo, key);
}).then(async function() { }).then(async function() {
expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); expect(await stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data);
}); });
} finally { } finally {
openpgp.config.aeadProtect = aeadProtectVal; openpgp.config.aeadProtect = aeadProtectVal;
@ -227,7 +229,7 @@ module.exports = () => describe("Packet", function() {
await enc.encrypt(algo, key, { ...openpgp.config, aeadChunkSizeByte: 0 }); await enc.encrypt(algo, key, { ...openpgp.config, aeadChunkSizeByte: 0 });
await msg2.read(msg.write(), allAllowedPackets); await msg2.read(msg.write(), allAllowedPackets);
await msg2[0].decrypt(algo, key); await msg2[0].decrypt(algo, key);
expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); expect(await stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data);
expect(encryptStub.callCount > 1).to.be.true; expect(encryptStub.callCount > 1).to.be.true;
expect(decryptStub.callCount > 1).to.be.true; expect(decryptStub.callCount > 1).to.be.true;
} finally { } finally {
@ -271,10 +273,10 @@ module.exports = () => describe("Packet", function() {
try { try {
await enc.encrypt(algo, key, { ...openpgp.config, aeadChunkSizeByte: 14 }); await enc.encrypt(algo, key, { ...openpgp.config, aeadChunkSizeByte: 14 });
const data = msg.write(); const data = msg.write();
expect(await openpgp.stream.readToEnd(openpgp.stream.clone(data))).to.deep.equal(packetBytes); expect(await stream.readToEnd(stream.clone(data))).to.deep.equal(packetBytes);
await msg2.read(data, allAllowedPackets); await msg2.read(data, allAllowedPackets);
await msg2[0].decrypt(algo, key); await msg2[0].decrypt(algo, key);
expect(await openpgp.stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data); expect(await stream.readToEnd(msg2[0].packets[0].data)).to.deep.equal(literal.data);
} finally { } finally {
randomBytesStub.restore(); randomBytesStub.restore();
} }
@ -575,7 +577,7 @@ module.exports = () => describe("Packet", function() {
await encData.encrypt(algo, key, undefined, openpgp.config); await encData.encrypt(algo, key, undefined, openpgp.config);
const data = msg.write(); const data = msg.write();
expect(await openpgp.stream.readToEnd(openpgp.stream.clone(data))).to.deep.equal(packetBytes); expect(await stream.readToEnd(stream.clone(data))).to.deep.equal(packetBytes);
const msg2 = new openpgp.PacketList(); const msg2 = new openpgp.PacketList();
await msg2.read(data, allAllowedPackets); await msg2.read(data, allAllowedPackets);
@ -653,7 +655,7 @@ module.exports = () => describe("Packet", function() {
await enc.encrypt(algo, key, undefined, openpgp.config); await enc.encrypt(algo, key, undefined, openpgp.config);
const data = msg.write(); const data = msg.write();
expect(await openpgp.stream.readToEnd(openpgp.stream.clone(data))).to.deep.equal(packetBytes); expect(await stream.readToEnd(stream.clone(data))).to.deep.equal(packetBytes);
const msg2 = new openpgp.PacketList(); const msg2 = new openpgp.PacketList();
await msg2.read(data, allAllowedPackets); await msg2.read(data, allAllowedPackets);
@ -745,13 +747,13 @@ module.exports = () => describe("Packet", function() {
await encData.decrypt(pkesk.sessionKeyAlgorithm, pkesk.sessionKey); await encData.decrypt(pkesk.sessionKeyAlgorithm, pkesk.sessionKey);
const payload = encData.packets[0].packets; const payload = encData.packets[0].packets;
payload.push(...await openpgp.stream.readToEnd(payload.stream, arr => arr)); payload.push(...await stream.readToEnd(payload.stream, arr => arr));
const literal = payload[1]; const literal = payload[1];
const signature = payload[2]; const signature = payload[2];
await Promise.all([ await Promise.all([
signature.verify(keyPacket, openpgp.enums.signature.binary, literal), signature.verify(keyPacket, openpgp.enums.signature.binary, literal),
openpgp.stream.readToEnd(literal.getBytes()) stream.readToEnd(literal.getBytes())
]); ]);
}); });
}); });
@ -939,11 +941,11 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+
const signed2 = new openpgp.PacketList(); const signed2 = new openpgp.PacketList();
await signed2.read(raw, allAllowedPackets); await signed2.read(raw, allAllowedPackets);
signed2.push(...await openpgp.stream.readToEnd(signed2.stream, arr => arr)); signed2.push(...await stream.readToEnd(signed2.stream, arr => arr));
await Promise.all([ await Promise.all([
signed2[1].verify(key, openpgp.enums.signature.text, signed2[0]), signed2[1].verify(key, openpgp.enums.signature.text, signed2[0]),
openpgp.stream.readToEnd(signed2[0].getBytes()) stream.readToEnd(signed2[0].getBytes())
]); ]);
}); });
}); });

View File

@ -4,6 +4,8 @@
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..'); const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..');
const util = require('../../src/util'); const util = require('../../src/util');
const stream = require('@openpgp/web-stream-tools');
const chai = require('chai'); const chai = require('chai');
chai.use(require('chai-as-promised')); chai.use(require('chai-as-promised'));
@ -946,7 +948,7 @@ hUhMKMuiM3pRwdIyDOItkUWQmjEEw7/XmhgInkXsCw==
const sMsg = await openpgp.readMessage({ armoredMessage: signedArmor }); const sMsg = await openpgp.readMessage({ armoredMessage: signedArmor });
const pub_key = await openpgp.readKey({ armoredKey: pub_key_arm2 }); const pub_key = await openpgp.readKey({ armoredKey: pub_key_arm2 });
const verified = await sMsg.verify([pub_key]); const verified = await sMsg.verify([pub_key]);
openpgp.stream.readToEnd(sMsg.getLiteralData()); stream.readToEnd(sMsg.getLiteralData());
expect(verified).to.exist; expect(verified).to.exist;
expect(verified).to.have.length(1); expect(verified).to.have.length(1);
expect(await verified[0].verified).to.be.true; expect(await verified[0].verified).to.be.true;
@ -1115,7 +1117,7 @@ PAAeuQTUrcJdZeJ86eQ9cCUB216HCwSKOWTQRzL+hBWKXij4WD4=
const pubKey = await openpgp.readKey({ armoredKey: pub_latin1_msg }); const pubKey = await openpgp.readKey({ armoredKey: pub_latin1_msg });
return message.verify([pubKey]).then(async verifiedSig => { return message.verify([pubKey]).then(async verifiedSig => {
expect(await openpgp.stream.readToEnd(message.getLiteralData())).to.equal(latin1Binary); expect(await stream.readToEnd(message.getLiteralData())).to.equal(latin1Binary);
expect(verifiedSig).to.exist; expect(verifiedSig).to.exist;
expect(verifiedSig).to.have.length(1); expect(verifiedSig).to.have.length(1);
expect(await verifiedSig[0].verified).to.be.true; expect(await verifiedSig[0].verified).to.be.true;
@ -1210,26 +1212,14 @@ yYDnCgA=
-----END PGP MESSAGE-----`.split(''); -----END PGP MESSAGE-----`.split('');
const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t ';
await stream.loadStreamsPonyfill();
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: globalThis.ReadableStream ? new globalThis.ReadableStream({ armoredMessage: new stream.ReadableStream({
async pull(controller) { async pull(controller) {
await new Promise(setTimeout); await new Promise(setTimeout);
controller.enqueue(armoredMessage.shift()); controller.enqueue(armoredMessage.shift());
if (!armoredMessage.length) controller.close(); if (!armoredMessage.length) controller.close();
} }
}) : new (require('stream').Readable)({
encoding: 'utf8',
async read() {
while (true) {
await new Promise(setTimeout);
if (!armoredMessage.length) {
return this.push(null);
}
if (!this.push(armoredMessage.shift())) {
break;
}
}
}
}) })
}); });
const pubKey = await openpgp.readKey({ armoredKey: pub_key_arm2 }); const pubKey = await openpgp.readKey({ armoredKey: pub_key_arm2 });
@ -1239,7 +1229,7 @@ yYDnCgA=
return openpgp.verify({ publicKeys: [pubKey], message, config: { minRSABits: 1024 } }).then(async function(cleartextSig) { return openpgp.verify({ publicKeys: [pubKey], message, config: { minRSABits: 1024 } }).then(async function(cleartextSig) {
expect(cleartextSig).to.exist; expect(cleartextSig).to.exist;
expect(await openpgp.stream.readToEnd(cleartextSig.data)).to.equal(plaintext); expect(await stream.readToEnd(cleartextSig.data)).to.equal(plaintext);
expect(cleartextSig.signatures).to.have.length(1); expect(cleartextSig.signatures).to.have.length(1);
if (!openpgp.config.rejectMessageHashAlgorithms.has(openpgp.enums.hash.sha1)) { if (!openpgp.config.rejectMessageHashAlgorithms.has(openpgp.enums.hash.sha1)) {
expect(await cleartextSig.signatures[0].verified).to.be.true; expect(await cleartextSig.signatures[0].verified).to.be.true;
@ -1288,8 +1278,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA
-----END PGP MESSAGE-----`.split(''); -----END PGP MESSAGE-----`.split('');
const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t ';
await stream.loadStreamsPonyfill();
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: new openpgp.stream.ReadableStream({ armoredMessage: new stream.ReadableStream({
async pull(controller) { async pull(controller) {
await new Promise(setTimeout); await new Promise(setTimeout);
controller.enqueue(armoredMessage.shift()); controller.enqueue(armoredMessage.shift());
@ -1303,7 +1294,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA
expect(pubKey.getKeys(keyIDs[0])).to.not.be.empty; expect(pubKey.getKeys(keyIDs[0])).to.not.be.empty;
return openpgp.verify({ publicKeys: [pubKey], message, config: { minRSABits: 1024 } }).then(async ({ data, signatures }) => { return openpgp.verify({ publicKeys: [pubKey], message, config: { minRSABits: 1024 } }).then(async ({ data, signatures }) => {
expect(await openpgp.stream.readToEnd(data)).to.equal(plaintext); expect(await stream.readToEnd(data)).to.equal(plaintext);
expect(signatures).to.have.length(1); expect(signatures).to.have.length(1);
await expect(signatures[0].verified).to.be.rejectedWith('Corresponding signature packet missing'); await expect(signatures[0].verified).to.be.rejectedWith('Corresponding signature packet missing');
expect((await signatures[0].signature).packets.length).to.equal(0); expect((await signatures[0].signature).packets.length).to.equal(0);

View File

@ -11,10 +11,9 @@ const input = require('./testInputs.js');
const { expect } = chai; const { expect } = chai;
const { stream } = openpgp; 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 ReadableStream = useNativeStream ? global.ReadableStream : openpgp.stream.ReadableStream;
const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable; const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable;
const pub_key = [ const pub_key = [
@ -180,7 +179,7 @@ let dataArrived;
function tests() { function tests() {
it('Encrypt small message', async function() { it('Encrypt small message', async function() {
dataArrived(); // Do not wait until data arrived. dataArrived(); // Do not wait until data arrived.
const data = ReadableStream ? new ReadableStream({ const data = global.ReadableStream ? new global.ReadableStream({
start(controller) { start(controller) {
controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('hello '));
controller.enqueue(util.stringToUint8Array('world')); controller.enqueue(util.stringToUint8Array('world'));
@ -197,7 +196,7 @@ function tests() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
}); });
const msgAsciiArmored = await openpgp.stream.readToEnd(encrypted); const msgAsciiArmored = await stream.readToEnd(encrypted);
const message = await openpgp.readMessage({ armoredMessage: msgAsciiArmored }); const message = await openpgp.readMessage({ armoredMessage: msgAsciiArmored });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
@ -211,11 +210,11 @@ function tests() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
}); });
const reader = openpgp.stream.getReader(encrypted); const reader = stream.getReader(encrypted);
expect(await reader.peekBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/); expect(await reader.peekBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/);
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
const msgAsciiArmored = await openpgp.stream.readToEnd(encrypted); const msgAsciiArmored = await stream.readToEnd(encrypted);
const message = await openpgp.readMessage({ armoredMessage: msgAsciiArmored }); const message = await openpgp.readMessage({ armoredMessage: msgAsciiArmored });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
@ -230,11 +229,11 @@ function tests() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
}); });
const reader = openpgp.stream.getReader(encrypted); const reader = stream.getReader(encrypted);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/);
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
await openpgp.stream.cancel(encrypted); await stream.cancel(encrypted);
expect(canceled).to.be.true; expect(canceled).to.be.true;
}); });
@ -244,11 +243,11 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
const reader = openpgp.stream.getReader(signed); const reader = stream.getReader(signed);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/);
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
await openpgp.stream.cancel(signed); await stream.cancel(signed);
expect(canceled).to.be.true; expect(canceled).to.be.true;
}); });
@ -260,7 +259,7 @@ function tests() {
passwords: ['test'], passwords: ['test'],
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
setTimeout(dataArrived, 3000); // Do not wait until data arrived, but wait a bit to check that it doesn't arrive early. setTimeout(dataArrived, 3000); // Do not wait until data arrived, but wait a bit to check that it doesn't arrive early.
@ -269,8 +268,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
if (i <= 10) throw new Error('Data arrived early.'); if (i <= 10) throw new Error('Data arrived early.');
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -288,7 +287,7 @@ function tests() {
passwords: ['test'], passwords: ['test'],
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -296,9 +295,9 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
expect(openpgp.stream.isStream(decrypted.signatures)).to.be.false; expect(stream.isStream(decrypted.signatures)).to.be.false;
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -320,7 +319,7 @@ function tests() {
armor: false, armor: false,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -329,8 +328,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -355,7 +354,7 @@ function tests() {
privateKeys: priv, privateKeys: priv,
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -364,8 +363,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -390,7 +389,7 @@ function tests() {
privateKeys: priv, privateKeys: priv,
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -399,8 +398,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -419,10 +418,10 @@ function tests() {
message: await openpgp.createMessage({ binary: data, filename: 'msg.bin' }), message: await openpgp.createMessage({ binary: data, filename: 'msg.bin' }),
passwords: ['test'] passwords: ['test']
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: openpgp.stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === openpgp.stream.ReadableStream ? 'toStream' : 'toNativeReadable'](openpgp.stream.transform(encrypted, value => { armoredMessage: stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === stream.ReadableStream ? 'toStream' : 'toNativeReadable'](stream.transform(encrypted, value => {
value += ''; value += '';
if (value === '=' || value.length === 5) return; // Remove checksum if (value === '=' || value.length === 5) return; // Remove checksum
const newlineIndex = value.indexOf('\n', 500); const newlineIndex = value.indexOf('\n', 500);
@ -435,8 +434,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await expect(reader.readToEnd()).to.be.rejectedWith('Modification detected.'); await expect(reader.readToEnd()).to.be.rejectedWith('Modification detected.');
@ -457,10 +456,10 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: openpgp.stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === openpgp.stream.ReadableStream ? 'toStream' : 'toNativeReadable'](openpgp.stream.transform(encrypted, value => { armoredMessage: stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === stream.ReadableStream ? 'toStream' : 'toNativeReadable'](stream.transform(encrypted, value => {
value += ''; value += '';
const newlineIndex = value.indexOf('\n', 500); const newlineIndex = value.indexOf('\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex); if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
@ -473,8 +472,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed'); await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed');
@ -494,10 +493,10 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: openpgp.stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === openpgp.stream.ReadableStream ? 'toStream' : 'toNativeReadable'](openpgp.stream.transform(encrypted, value => { armoredMessage: stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === stream.ReadableStream ? 'toStream' : 'toNativeReadable'](stream.transform(encrypted, value => {
value += ''; value += '';
const newlineIndex = value.indexOf('\n', 500); const newlineIndex = value.indexOf('\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex); if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
@ -509,8 +508,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed'); await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed');
@ -527,10 +526,10 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const message = await openpgp.readMessage({ const message = await openpgp.readMessage({
armoredMessage: openpgp.stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === openpgp.stream.ReadableStream ? 'toStream' : 'toNativeReadable'](openpgp.stream.transform(signed, value => { armoredMessage: stream[expectedType === 'node' ? 'webToNode' : global.ReadableStream === stream.ReadableStream ? 'toStream' : 'toNativeReadable'](stream.transform(signed, value => {
value += ''; value += '';
const newlineIndex = value.indexOf('\n', 500); const newlineIndex = value.indexOf('\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex); if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
@ -543,8 +542,8 @@ function tests() {
format: 'binary', format: 'binary',
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(verified.data)).to.equal(expectedType); expect(stream.isStream(verified.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(verified.data); const reader = stream.getReader(verified.data);
expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).not.to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed'); await expect(reader.readToEnd()).to.be.rejectedWith('Ascii armor integrity check on message failed');
@ -582,7 +581,7 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const message = await openpgp.readMessage({ armoredMessage: signed }); const message = await openpgp.readMessage({ armoredMessage: signed });
const verified = await openpgp.verify({ const verified = await openpgp.verify({
@ -591,12 +590,12 @@ function tests() {
format: 'binary', format: 'binary',
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(verified.data)).to.equal(expectedType); expect(stream.isStream(verified.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(verified.data); const reader = stream.getReader(verified.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
await openpgp.stream.cancel(verified.data, new Error('canceled by test')); await stream.cancel(verified.data, new Error('canceled by test'));
expect(canceled).to.be.true; expect(canceled).to.be.true;
expect(verified.signatures).to.exist.and.have.length(1); expect(verified.signatures).to.exist.and.have.length(1);
await expect(verified.signatures[0].verified).to.be.rejectedWith('canceled'); await expect(verified.signatures[0].verified).to.be.rejectedWith('canceled');
@ -607,9 +606,9 @@ function tests() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const reader = openpgp.stream.getReader(encrypted); const reader = stream.getReader(encrypted);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/);
dataArrived(); dataArrived();
await new Promise(resolve => setTimeout(resolve, 3000)); await new Promise(resolve => setTimeout(resolve, 3000));
@ -622,9 +621,9 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed); const reader = stream.getReader(signed);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\n/);
dataArrived(); dataArrived();
await new Promise(resolve => setTimeout(resolve, 3000)); await new Promise(resolve => setTimeout(resolve, 3000));
@ -637,15 +636,15 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const message = await openpgp.readMessage({ armoredMessage: signed }); const message = await openpgp.readMessage({ armoredMessage: signed });
const verified = await openpgp.verify({ const verified = await openpgp.verify({
publicKeys: pubKey, publicKeys: pubKey,
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(verified.data)).to.equal(expectedType); expect(stream.isStream(verified.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(verified.data); const reader = stream.getReader(verified.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await new Promise(resolve => setTimeout(resolve, 3000)); await new Promise(resolve => setTimeout(resolve, 3000));
@ -654,7 +653,7 @@ function tests() {
it('Detached sign small message', async function() { it('Detached sign small message', async function() {
dataArrived(); // Do not wait until data arrived. dataArrived(); // Do not wait until data arrived.
const data = ReadableStream ? new ReadableStream({ const data = global.ReadableStream ? new global.ReadableStream({
start(controller) { start(controller) {
controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('hello '));
controller.enqueue(util.stringToUint8Array('world')); controller.enqueue(util.stringToUint8Array('world'));
@ -673,8 +672,8 @@ function tests() {
detached: true, detached: true,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const armoredSignature = await openpgp.stream.readToEnd(signed); const armoredSignature = await stream.readToEnd(signed);
const signature = await openpgp.readSignature({ armoredSignature }); const signature = await openpgp.readSignature({ armoredSignature });
const verified = await openpgp.verify({ const verified = await openpgp.verify({
signature, signature,
@ -689,7 +688,7 @@ function tests() {
it('Detached sign small message using brainpool curve keys', async function() { it('Detached sign small message using brainpool curve keys', async function() {
dataArrived(); // Do not wait until data arrived. dataArrived(); // Do not wait until data arrived.
const data = ReadableStream ? new ReadableStream({ const data = global.ReadableStream ? new global.ReadableStream({
start(controller) { start(controller) {
controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('hello '));
controller.enqueue(util.stringToUint8Array('world')); controller.enqueue(util.stringToUint8Array('world'));
@ -713,8 +712,8 @@ function tests() {
privateKeys: priv, privateKeys: priv,
detached: true detached: true
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const armoredSignature = await openpgp.stream.readToEnd(signed); const armoredSignature = await stream.readToEnd(signed);
const signature = await openpgp.readSignature({ armoredSignature }); const signature = await openpgp.readSignature({ armoredSignature });
const verified = await openpgp.verify({ const verified = await openpgp.verify({
signature, signature,
@ -728,7 +727,7 @@ function tests() {
it('Detached sign small message using x25519 curve keys', async function() { it('Detached sign small message using x25519 curve keys', async function() {
dataArrived(); // Do not wait until data arrived. dataArrived(); // Do not wait until data arrived.
const data = ReadableStream ? new ReadableStream({ const data = global.ReadableStream ? new global.ReadableStream({
async start(controller) { async start(controller) {
controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('hello '));
controller.enqueue(util.stringToUint8Array('world')); controller.enqueue(util.stringToUint8Array('world'));
@ -752,8 +751,8 @@ function tests() {
privateKeys: priv, privateKeys: priv,
detached: true detached: true
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const armoredSignature = await openpgp.stream.readToEnd(signed); const armoredSignature = await stream.readToEnd(signed);
const signature = await openpgp.readSignature({ armoredSignature }); const signature = await openpgp.readSignature({ armoredSignature });
const verified = await openpgp.verify({ const verified = await openpgp.verify({
signature, signature,
@ -772,8 +771,8 @@ function tests() {
detached: true, detached: true,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed); const reader = stream.getReader(signed);
expect((await reader.readBytes(30)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\n'); expect((await reader.readBytes(30)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\n');
dataArrived(); dataArrived();
await new Promise(resolve => setTimeout(resolve, 3000)); await new Promise(resolve => setTimeout(resolve, 3000));
@ -787,12 +786,12 @@ function tests() {
detached: true, detached: true,
config: { minRSABits: 1024 } config: { minRSABits: 1024 }
}); });
expect(openpgp.stream.isStream(signed)).to.equal(expectedType); expect(stream.isStream(signed)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed); const reader = stream.getReader(signed);
expect((await reader.readBytes(30)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\n'); expect((await reader.readBytes(30)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\n');
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
await openpgp.stream.cancel(signed, new Error('canceled by test')); await stream.cancel(signed, new Error('canceled by test'));
expect(canceled).to.be.true; expect(canceled).to.be.true;
}); });
@ -817,7 +816,7 @@ function tests() {
passwords: ['test'], passwords: ['test'],
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -825,8 +824,8 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.peekBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext)); expect(await reader.readToEnd()).to.deep.equal(util.concatUint8Array(plaintext));
@ -837,7 +836,7 @@ function tests() {
const plaintext = []; const plaintext = [];
let i = 0; let i = 0;
const data = ReadableStream ? new ReadableStream({ const data = global.ReadableStream ? new global.ReadableStream({
async pull(controller) { async pull(controller) {
await new Promise(resolve => setTimeout(resolve, 10)); await new Promise(resolve => setTimeout(resolve, 10));
if (i++ < 10) { if (i++ < 10) {
@ -867,15 +866,15 @@ function tests() {
message: await openpgp.createMessage({ text: data }), message: await openpgp.createMessage({ text: data }),
passwords: ['test'] passwords: ['test']
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ armoredMessage: encrypted }); const message = await openpgp.readMessage({ armoredMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
message message
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect((await reader.peekBytes(plaintext[0].length * 4)).toString('utf8').substr(0, plaintext[0].length)).to.equal(plaintext[0]); expect((await reader.peekBytes(plaintext[0].length * 4)).toString('utf8').substr(0, plaintext[0].length)).to.equal(plaintext[0]);
dataArrived(); dataArrived();
expect((await reader.readToEnd()).toString('utf8')).to.equal(util.concat(plaintext)); expect((await reader.readToEnd()).toString('utf8')).to.equal(util.concat(plaintext));
@ -896,15 +895,15 @@ function tests() {
message: await openpgp.createMessage({ binary: data }), message: await openpgp.createMessage({ binary: data }),
passwords: ['test'] passwords: ['test']
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); expect(stream.isStream(encrypted)).to.equal(expectedType);
const message = await openpgp.readMessage({ armoredMessage: encrypted }); const message = await openpgp.readMessage({ armoredMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
await new Promise(resolve => setTimeout(resolve, 3000)); await new Promise(resolve => setTimeout(resolve, 3000));
@ -930,12 +929,12 @@ function tests() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal(expectedType); expect(stream.isStream(decrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(decrypted.data); const reader = stream.getReader(decrypted.data);
expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]);
dataArrived(); dataArrived();
reader.releaseLock(); reader.releaseLock();
await openpgp.stream.cancel(decrypted.data, new Error('canceled by test')); await stream.cancel(decrypted.data, new Error('canceled by test'));
await new Promise(setTimeout); await new Promise(setTimeout);
expect(canceled).to.be.true; expect(canceled).to.be.true;
}); });
@ -951,6 +950,8 @@ module.exports = () => describe('Streaming', function() {
privateKey: await openpgp.readKey({ armoredKey: priv_key }), privateKey: await openpgp.readKey({ armoredKey: priv_key }),
passphrase: 'hello world' passphrase: 'hello world'
}); });
await stream.loadStreamsPonyfill();
}); });
beforeEach(function() { beforeEach(function() {
@ -962,7 +963,7 @@ module.exports = () => describe('Streaming', function() {
plaintext = []; plaintext = [];
i = 0; i = 0;
canceled = false; canceled = false;
data = ReadableStream ? new ReadableStream({ data = global.ReadableStream ? new global.ReadableStream({
async pull(controller) { async pull(controller) {
await new Promise(setTimeout); await new Promise(setTimeout);
if (test === currentTest && i < (expectedType === 'web' ? 100 : 500)) { if (test === currentTest && i < (expectedType === 'web' ? 100 : 500)) {
@ -1000,7 +1001,7 @@ module.exports = () => describe('Streaming', function() {
canceled = true; canceled = true;
} }
}); });
expectedType = ReadableStream ? 'web' : 'node'; expectedType = global.ReadableStream ? 'web' : 'node';
}); });
tests(); tests();
@ -1016,15 +1017,15 @@ module.exports = () => describe('Streaming', function() {
message: await openpgp.createMessage({ text: data }), message: await openpgp.createMessage({ text: data }),
passwords: ['test'] passwords: ['test']
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal('node'); expect(stream.isStream(encrypted)).to.equal('node');
const message = await openpgp.readMessage({ armoredMessage: encrypted }); const message = await openpgp.readMessage({ armoredMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
message message
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal('node'); expect(stream.isStream(decrypted.data)).to.equal('node');
expect(await openpgp.stream.readToEnd(decrypted.data)).to.equal(plaintext); expect(await stream.readToEnd(decrypted.data)).to.equal(plaintext);
}); });
it('Node: Encrypt and decrypt binary message roundtrip', async function() { it('Node: Encrypt and decrypt binary message roundtrip', async function() {
@ -1036,7 +1037,7 @@ module.exports = () => describe('Streaming', function() {
passwords: ['test'], passwords: ['test'],
armor: false armor: false
}); });
expect(openpgp.stream.isStream(encrypted)).to.equal('node'); expect(stream.isStream(encrypted)).to.equal('node');
const message = await openpgp.readMessage({ binaryMessage: encrypted }); const message = await openpgp.readMessage({ binaryMessage: encrypted });
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -1044,8 +1045,8 @@ module.exports = () => describe('Streaming', function() {
message, message,
format: 'binary' format: 'binary'
}); });
expect(openpgp.stream.isStream(decrypted.data)).to.equal('node'); expect(stream.isStream(decrypted.data)).to.equal('node');
expect(await openpgp.stream.readToEnd(decrypted.data)).to.deep.equal(plaintext); expect(await stream.readToEnd(decrypted.data)).to.deep.equal(plaintext);
}); });
} }
}); });