Use ArrayStreams

This commit is contained in:
Daniel Huigens 2021-03-24 14:21:13 +01:00
parent 6cff19c44a
commit ff8d274b4d
7 changed files with 35 additions and 18 deletions

6
package-lock.json generated
View File

@ -261,9 +261,9 @@
"dev": true
},
"@openpgp/web-stream-tools": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.1.tgz",
"integrity": "sha512-k3MqSRRjWkODJzsQdHeYxjG3IdmmtUXkXrz6XMq7aDT7GFTZPV/ZTbTHN93YkViPJNO3G0pS+FdO4Qy+UgGZpw==",
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.2.tgz",
"integrity": "sha512-6RDYscGNmPEB/blocMH6bbyu4x0SXZKVHe1n1jHTonGyDJSMhPub99G+zFzRfXVjWamim2mXUPa7gfyK3tKJ7g==",
"dev": true,
"requires": {
"@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.5",

View File

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

View File

@ -570,6 +570,10 @@ async function convertStream(data, streaming, encoding = 'utf8') {
if (!streaming && streamType) {
return stream.readToEnd(data);
}
if (streamType === 'array') {
data = await stream.readToEnd(data);
streamType = false;
}
if (streaming && !streamType) {
data = stream.toStream(data);
streamType = util.isStream(data);

View File

@ -144,13 +144,16 @@ class AEADEncryptedDataPacket {
let queuedBytes = 0;
const iv = this.iv;
return stream.transformPair(data, async (readable, writable) => {
if (util.isStream(readable) !== 'array') {
const buffer = new stream.TransformStream({}, {
highWaterMark: streaming ? util.getHardwareConcurrency() * 2 ** (this.chunkSizeByte + 6) : Infinity,
size: array => array.length
});
stream.pipe(buffer.readable, writable);
writable = buffer.writable;
}
const reader = stream.getReader(readable);
const buffer = new stream.TransformStream({}, {
highWaterMark: streaming ? util.getHardwareConcurrency() * 2 ** (this.chunkSizeByte + 6) : Infinity,
size: array => array.length
});
stream.pipe(buffer.readable, writable);
const writer = stream.getWriter(buffer.writable);
const writer = stream.getWriter(writable);
try {
while (true) {
let chunk = await reader.readBytes(chunkSize + tagLengthIfDecrypting) || new Uint8Array();

View File

@ -148,7 +148,11 @@ function uncompressed(data) {
function node_zlib(func, options = {}) {
return function (data) {
return stream.nodeToWeb(stream.webToNode(data).pipe(func(options)));
const webStream = stream.nodeToWeb(stream.webToNode(data).pipe(func(options)));
if (stream.isStream(data) === 'array') {
return stream.fromAsync(() => stream.readToEnd(webStream));
}
return webStream;
};
}

View File

@ -150,9 +150,15 @@ export async function readPackets(input, streaming, callback) {
const packetSupportsStreaming = supportsStreaming(tag);
let packet = null;
if (streaming && packetSupportsStreaming) {
const transform = new stream.TransformStream();
writer = stream.getWriter(transform.writable);
packet = transform.readable;
if (util.isStream(input) === 'array') {
const arrayStream = new stream.ArrayStream();
writer = stream.getWriter(arrayStream);
packet = arrayStream;
} else {
const transform = new stream.TransformStream();
writer = stream.getWriter(transform.writable);
packet = transform.readable;
}
callbackReturned = callback({ tag, packet });
} else {
packet = [];

View File

@ -1828,9 +1828,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const { key } = await openpgp.generateKey({ userIDs: {} });
const message = await openpgp.encrypt({ message: openpgp.Message.fromText('test'), publicKeys: key, privateKeys: key, armor: false });
const encrypted = util.concat([message, new Uint8Array([11])]);
await expect(
openpgp.decrypt({ message: await openpgp.readMessage({ binaryMessage: encrypted }), privateKeys: key, publicKeys: key })
).to.be.rejectedWith('Error during parsing. This message / key probably does not conform to a valid OpenPGP format.');
await expect((async () => {
await openpgp.decrypt({ message: await openpgp.readMessage({ binaryMessage: encrypted }), privateKeys: key, publicKeys: key });
})()).to.be.rejectedWith('Error during parsing. This message / key probably does not conform to a valid OpenPGP format.');
});
});
@ -2380,7 +2380,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
const packets = new openpgp.PacketList();
packets.push(message.packets.findPacket(openpgp.enums.packet.signature));
packets.push(message.packets.findPacket(openpgp.enums.packet.literalData));
verifyOpt.message = new openpgp.Message(packets);
verifyOpt.message = await openpgp.readMessage({ binaryMessage: packets.write() });
return openpgp.verify(verifyOpt);
}).then(async function (verified) {
expect(openpgp.stream.isStream(verified.data)).to.equal(useNativeStream ? 'web' : 'ponyfill');