Split stream.transform into using two helper functions

This commit is contained in:
Daniel Huigens 2018-05-15 20:19:33 +02:00
parent 4ada3fa590
commit 1f30556674
7 changed files with 76 additions and 106 deletions

View File

@ -38,13 +38,9 @@ function node_hash(type) {
function hashjs_hash(hash) { function hashjs_hash(hash) {
return function(data) { return function(data) {
const hashInstance = hash(); const hashInstance = hash();
return stream.transform(data, (done, value) => { return stream.transform(data, value => {
if (!done) { hashInstance.update(value);
hashInstance.update(value); }, () => util.hex_to_Uint8Array(hashInstance.digest('hex')));
} else {
return util.hex_to_Uint8Array(hashInstance.digest('hex'));
}
});
}; };
} }

View File

@ -168,16 +168,11 @@ const crc_table = [
function createcrc24(input) { function createcrc24(input) {
let crc = 0xB704CE; let crc = 0xB704CE;
return stream.transform(input, (done, value) => { return stream.transform(input, value => {
if (!done) { for (let index = 0; index < value.length; index++) {
for (let index = 0; index < value.length; index++) { crc = (crc << 8) ^ crc_table[((crc >> 16) ^ value[index]) & 0xff];
crc = (crc << 8) ^ crc_table[((crc >> 16) ^ value[index]) & 0xff];
}
} else {
crc &= 0xffffff;
return new Uint8Array([crc >> 16, (crc >> 8) & 0xFF, crc & 0xFF]);
} }
}); }, () => new Uint8Array([crc >> 16, crc >> 8, crc]));
} }
/** /**

View File

@ -39,56 +39,55 @@ function s2r(t, u = false) {
let l = 0; let l = 0;
let s = 0; let s = 0;
return stream.transform(t, (done, value) => { return stream.transform(t, value => {
const r = []; const r = [];
const tl = value.length;
if (!done) { for (let n = 0; n < tl; n++) {
const tl = value.length; c = value[n];
for (let n = 0; n < tl; n++) { if (s === 0) {
c = value[n]; r.push(b64[(c >> 2) & 63]);
if (s === 0) { a = (c & 3) << 4;
r.push(b64[(c >> 2) & 63]); } else if (s === 1) {
a = (c & 3) << 4; r.push(b64[a | ((c >> 4) & 15)]);
} else if (s === 1) { a = (c & 15) << 2;
r.push(b64[a | ((c >> 4) & 15)]); } else if (s === 2) {
a = (c & 15) << 2; r.push(b64[a | ((c >> 6) & 3)]);
} else if (s === 2) {
r.push(b64[a | ((c >> 6) & 3)]);
l += 1;
if ((l % 60) === 0 && !u) {
r.push(10); // "\n"
}
r.push(b64[c & 63]);
}
l += 1; l += 1;
if ((l % 60) === 0 && !u) { if ((l % 60) === 0 && !u) {
r.push(10); // "\n" r.push(10); // "\n"
} }
r.push(b64[c & 63]);
}
l += 1;
if ((l % 60) === 0 && !u) {
r.push(10); // "\n"
}
s += 1; s += 1;
if (s === 3) { if (s === 3) {
s = 0; s = 0;
}
} }
} else { }
if (s > 0) { return new Uint8Array(r);
r.push(b64[a]); }, () => {
l += 1; const r = [];
if ((l % 60) === 0 && !u) { if (s > 0) {
r.push(10); // "\n" r.push(b64[a]);
} l += 1;
if (!u) { if ((l % 60) === 0 && !u) {
r.push(61); // "=" r.push(10); // "\n"
l += 1;
}
} }
if (s === 1 && !u) { if (!u) {
if ((l % 60) === 0 && !u) {
r.push(10); // "\n"
}
r.push(61); // "=" r.push(61); // "="
l += 1;
} }
} }
if (s === 1 && !u) {
if ((l % 60) === 0 && !u) {
r.push(10); // "\n"
}
r.push(61); // "="
}
return new Uint8Array(r); return new Uint8Array(r);
}); });
} }
@ -108,22 +107,20 @@ function r2s(t, u) {
let s = 0; let s = 0;
let a = 0; let a = 0;
return stream.transform(t, (done, value) => { return stream.transform(t, value => {
if (!done) { const r = [];
const r = []; const tl = value.length;
const tl = value.length; for (let n = 0; n < tl; n++) {
for (let n = 0; n < tl; n++) { c = b64.indexOf(value[n]);
c = b64.indexOf(value[n]); if (c >= 0) {
if (c >= 0) { if (s) {
if (s) { r.push(a | ((c >> (6 - s)) & 255));
r.push(a | ((c >> (6 - s)) & 255));
}
s = (s + 2) & 7;
a = (c << s) & 255;
} }
s = (s + 2) & 7;
a = (c << s) & 255;
} }
return new Uint8Array(r);
} }
return new Uint8Array(r);
}); });
} }

View File

@ -80,22 +80,18 @@ List.prototype.write = function () {
let bufferLength = 0; let bufferLength = 0;
const minLength = 512; const minLength = 512;
arr.push(packetParser.writeTag(this[i].tag)); arr.push(packetParser.writeTag(this[i].tag));
arr.push(stream.transform(packetbytes, (done, value) => { arr.push(stream.transform(packetbytes, value => {
if (!done) { buffer.push(value);
buffer.push(value); bufferLength += value.length;
bufferLength += value.length; if (bufferLength >= minLength) {
if (bufferLength >= minLength) { const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30);
const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30); const chunkSize = 2 ** powerOf2;
const chunkSize = 2 ** powerOf2; const bufferConcat = util.concatUint8Array([packetParser.writePartialLength(powerOf2)].concat(buffer));
const bufferConcat = util.concatUint8Array([packetParser.writePartialLength(powerOf2)].concat(buffer)); buffer = [bufferConcat.subarray(1 + chunkSize)];
buffer = [bufferConcat.subarray(1 + chunkSize)]; bufferLength = buffer[0].length;
bufferLength = buffer[0].length; return bufferConcat.subarray(0, 1 + chunkSize);
return bufferConcat.subarray(0, 1 + chunkSize);
}
} else {
return util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer));
} }
})); }, () => util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer))));
} else { } else {
arr.push(packetParser.writeHeader(this[i].tag, packetbytes.length)); arr.push(packetParser.writeHeader(this[i].tag, packetbytes.length));
arr.push(packetbytes); arr.push(packetbytes);

View File

@ -158,12 +158,7 @@ function aesEncrypt(algo, pt, key) {
return nodeEncrypt(algo, pt, key); return nodeEncrypt(algo, pt, key);
} // asm.js fallback } // asm.js fallback
const cfb = new AES_CFB_Encrypt(key); const cfb = new AES_CFB_Encrypt(key);
return stream.transform(pt, (done, value) => { return stream.transform(pt, value => cfb.process(value).result, () => cfb.finish().result);
if (!done) {
return cfb.process(value).result;
}
return cfb.finish().result;
});
} }
function aesDecrypt(algo, ct, key) { function aesDecrypt(algo, ct, key) {
@ -172,12 +167,7 @@ function aesDecrypt(algo, ct, key) {
pt = nodeDecrypt(algo, ct, key); pt = nodeDecrypt(algo, ct, key);
} else { // asm.js fallback } else { // asm.js fallback
const cfb = new AES_CFB_Decrypt(key); const cfb = new AES_CFB_Decrypt(key);
pt = stream.transform(ct, (done, value) => { pt = stream.transform(ct, value => cfb.process(value).result, () => cfb.finish().result);
if (!done) {
return cfb.process(value).result;
}
return cfb.finish().result;
});
} }
return stream.subarray(pt, crypto.cipher[algo].blockSize + 2); // Remove random prefix return stream.subarray(pt, crypto.cipher[algo].blockSize + 2); // Remove random prefix
} }

View File

@ -21,14 +21,14 @@ function getReader(input) {
return new Reader(input); return new Reader(input);
} }
function transform(input, fn) { function transform(input, process = () => undefined, finish = () => undefined) {
if (util.isStream(input)) { if (util.isStream(input)) {
const reader = getReader(input); const reader = getReader(input);
return new ReadableStream({ return new ReadableStream({
async pull(controller) { async pull(controller) {
try { try {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
const result = await fn(done, value); const result = await (!done ? process : finish)(value);
if (result) controller.enqueue(result); if (result) controller.enqueue(result);
else if (!done) await this.pull(controller); // ??? Chrome bug? else if (!done) await this.pull(controller); // ??? Chrome bug?
if (done) controller.close(); if (done) controller.close();
@ -38,8 +38,8 @@ function transform(input, fn) {
} }
}); });
} }
const result1 = fn(false, input); const result1 = process(input);
const result2 = fn(true, undefined); const result2 = finish(undefined);
if (result1 && result2) return util.concatUint8Array([result1, result2]); if (result1 && result2) return util.concatUint8Array([result1, result2]);
return result1 || result2; return result1 || result2;
} }

View File

@ -29,7 +29,7 @@ import rfc2822 from 'address-rfc2822';
import config from './config'; import config from './config';
import util from './util'; // re-import module to access util functions import util from './util'; // re-import module to access util functions
import b64 from './encoding/base64'; import b64 from './encoding/base64';
import Stream from './stream'; import stream from './stream';
const isIE11 = typeof navigator !== 'undefined' && !!navigator.userAgent.match(/Trident\/7\.0.*rv:([0-9.]+).*\).*Gecko$/); const isIE11 = typeof navigator !== 'undefined' && !!navigator.userAgent.match(/Trident\/7\.0.*rv:([0-9.]+).*\).*Gecko$/);
@ -290,7 +290,7 @@ export default {
let totalLength = 0; let totalLength = 0;
for (let i = 0; i < arrays.length; i++) { for (let i = 0; i < arrays.length; i++) {
if (util.isStream(arrays[i])) { if (util.isStream(arrays[i])) {
return Stream.concat(arrays); return stream.concat(arrays);
} }
if (!util.isUint8Array(arrays[i])) { if (!util.isUint8Array(arrays[i])) {
throw new Error('concatUint8Array: Data must be in the form of a Uint8Array'); throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
@ -628,11 +628,7 @@ export default {
* Normalize line endings to \r\n * Normalize line endings to \r\n
*/ */
canonicalizeEOL: function(text) { canonicalizeEOL: function(text) {
return stream.transform(text, (done, value) => { return stream.transform(text, value => value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n"));
if (!done) {
return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n");
}
});
}, },
/** /**