Writing partial lengths
This commit is contained in:
parent
9853d3d830
commit
b4f5976242
|
@ -68,6 +68,18 @@ export default {
|
||||||
return util.concatUint8Array([new Uint8Array([255]), util.writeNumber(length, 4)]);
|
return util.concatUint8Array([new Uint8Array([255]), util.writeNumber(length, 4)]);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
writePartialLength: function(power) {
|
||||||
|
if (power < 0 || power > 30) {
|
||||||
|
throw new Error('Partial Length power must be between 1 and 30');
|
||||||
|
}
|
||||||
|
return new Uint8Array([224 + power]);
|
||||||
|
},
|
||||||
|
|
||||||
|
writeTag: function(tag_type) {
|
||||||
|
/* we're only generating v4 packet headers here */
|
||||||
|
return new Uint8Array([0xC0 | tag_type]);
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a packet header version 4 with the given tag_type and length to a
|
* Writes a packet header version 4 with the given tag_type and length to a
|
||||||
* string
|
* string
|
||||||
|
@ -78,7 +90,7 @@ export default {
|
||||||
*/
|
*/
|
||||||
writeHeader: function(tag_type, length) {
|
writeHeader: function(tag_type, length) {
|
||||||
/* we're only generating v4 packet headers here */
|
/* we're only generating v4 packet headers here */
|
||||||
return util.concatUint8Array([new Uint8Array([0xC0 | tag_type]), this.writeSimpleLength(length)]);
|
return util.concatUint8Array([this.writeTag(tag_type), this.writeSimpleLength(length)]);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -72,8 +72,31 @@ List.prototype.write = function () {
|
||||||
|
|
||||||
for (let i = 0; i < this.length; i++) {
|
for (let i = 0; i < this.length; i++) {
|
||||||
const packetbytes = this[i].write();
|
const packetbytes = this[i].write();
|
||||||
arr.push(packetParser.writeHeader(this[i].tag, packetbytes.length));
|
if (util.isStream(packetbytes)) {
|
||||||
arr.push(packetbytes);
|
let buffer = [];
|
||||||
|
let bufferLength = 0;
|
||||||
|
const minLength = 512;
|
||||||
|
arr.push(packetParser.writeTag(this[i].tag));
|
||||||
|
arr.push(packetbytes.transform((done, value) => {
|
||||||
|
if (!done) {
|
||||||
|
buffer.push(value);
|
||||||
|
bufferLength += value.length;
|
||||||
|
if (bufferLength >= minLength) {
|
||||||
|
const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30);
|
||||||
|
const chunkSize = 2 ** powerOf2;
|
||||||
|
const bufferConcat = util.concatUint8Array([packetParser.writePartialLength(powerOf2)].concat(buffer));
|
||||||
|
buffer = [bufferConcat.subarray(1 + chunkSize)];
|
||||||
|
bufferLength = buffer[0].length;
|
||||||
|
return bufferConcat.subarray(0, 1 + chunkSize);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return util.concatUint8Array([packetParser.writeSimpleLength(bufferLength)].concat(buffer));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
arr.push(packetParser.writeHeader(this[i].tag, packetbytes.length));
|
||||||
|
arr.push(packetbytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.concatUint8Array(arr);
|
return util.concatUint8Array(arr);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user