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);
} else { }, () => util.hex_to_Uint8Array(hashInstance.digest('hex')));
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 { }, () => new Uint8Array([crc >> 16, crc >> 8, crc]));
crc &= 0xffffff;
return new Uint8Array([crc >> 16, (crc >> 8) & 0xFF, crc & 0xFF]);
}
});
} }
/** /**

View File

@ -39,10 +39,8 @@ 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 = [];
if (!done) {
const tl = value.length; const tl = value.length;
for (let n = 0; n < tl; n++) { for (let n = 0; n < tl; n++) {
c = value[n]; c = value[n];
@ -70,7 +68,9 @@ function s2r(t, u = false) {
s = 0; s = 0;
} }
} }
} else { return new Uint8Array(r);
}, () => {
const r = [];
if (s > 0) { if (s > 0) {
r.push(b64[a]); r.push(b64[a]);
l += 1; l += 1;
@ -88,7 +88,6 @@ function s2r(t, u = false) {
} }
r.push(61); // "=" r.push(61); // "="
} }
}
return new Uint8Array(r); return new Uint8Array(r);
}); });
} }
@ -108,8 +107,7 @@ 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++) {
@ -123,7 +121,6 @@ function r2s(t, u) {
} }
} }
return new Uint8Array(r); return new Uint8Array(r);
}
}); });
} }

View File

@ -80,8 +80,7 @@ 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) {
@ -92,10 +91,7 @@ List.prototype.write = function () {
bufferLength = buffer[0].length; bufferLength = buffer[0].length;
return bufferConcat.subarray(0, 1 + chunkSize); return bufferConcat.subarray(0, 1 + chunkSize);
} }
} else { }, () => util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer))));
return 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");
}
});
}, },
/** /**