fix(core): update
This commit is contained in:
84
ts/bzip2/index.ts
Normal file
84
ts/bzip2/index.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
import { Bzip2 } from './bzip2.js';
|
||||
import { bitIterator } from './bititerator.js';
|
||||
|
||||
export function unbzip2Stream() {
|
||||
const bzip2Instance = new Bzip2();
|
||||
var bufferQueue = [];
|
||||
var hasBytes = 0;
|
||||
var blockSize = 0;
|
||||
var broken = false;
|
||||
var done = false;
|
||||
var bitReader = null;
|
||||
var streamCRC = null;
|
||||
|
||||
function decompressBlock() {
|
||||
if (!blockSize) {
|
||||
blockSize = bzip2Instance.header(bitReader);
|
||||
streamCRC = 0;
|
||||
} else {
|
||||
var bufsize = 100000 * blockSize;
|
||||
var buf = new Int32Array(bufsize);
|
||||
|
||||
var chunk = [];
|
||||
var f = function (b) {
|
||||
chunk.push(b);
|
||||
};
|
||||
|
||||
streamCRC = bzip2Instance.decompress(bitReader, f, buf, bufsize, streamCRC);
|
||||
if (streamCRC === null) {
|
||||
// reset for next bzip2 header
|
||||
blockSize = 0;
|
||||
return;
|
||||
} else {
|
||||
return Buffer.from(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var outlength = 0;
|
||||
const decompressAndPush = async (smartDuplexStream: plugins.smartstream.SmartDuplex) => {
|
||||
if (broken) return;
|
||||
try {
|
||||
const resultChunk = decompressBlock();
|
||||
smartDuplexStream.push(resultChunk);
|
||||
if (resultChunk) {
|
||||
//console.error('write at', outlength.toString(16));
|
||||
outlength += resultChunk.length;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
smartDuplexStream.emit('error', e);
|
||||
broken = true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
return new plugins.smartstream.SmartDuplex({
|
||||
writeFunction: async function (data, streamTools) {
|
||||
//console.error('received', data.length,'bytes in', typeof data);
|
||||
bufferQueue.push(data);
|
||||
hasBytes += data.length;
|
||||
if (bitReader === null) {
|
||||
bitReader = bitIterator(function () {
|
||||
return bufferQueue.shift();
|
||||
});
|
||||
}
|
||||
while (!broken && hasBytes - bitReader.bytesRead + 1 >= (25000 + 100000 * blockSize || 4)) {
|
||||
//console.error('decompressing with', hasBytes - bitReader.bytesRead + 1, 'bytes in buffer');
|
||||
await decompressAndPush(this);
|
||||
}
|
||||
},
|
||||
finalFunction: async function (streamTools) {
|
||||
//console.error(x,'last compressing with', hasBytes, 'bytes in buffer');
|
||||
while (!broken && bitReader && hasBytes > bitReader.bytesRead) {
|
||||
await decompressAndPush(this);
|
||||
}
|
||||
if (!broken) {
|
||||
if (streamCRC !== null) this.emit('error', new Error('input stream ended prematurely'));
|
||||
this.queue(null);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user