🐛 Wait with reading local file header until stream is ready
This commit is contained in:
parent
86ba51731c
commit
07de050588
3 changed files with 23 additions and 8 deletions
|
@ -38,7 +38,7 @@ export default async function getPatch(product: Product, from: number, to: numbe
|
|||
|
||||
//Extract newly added files
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).forEach(async (file) => {
|
||||
const fileStream = getFileFromDisks(diskFilenames, { diskStart: file.diskNumberStart, offset: file.offset, storedSize: file.compressedSize });
|
||||
const fileStream = await getFileFromDisks(diskFilenames, { diskStart: file.diskNumberStart, offset: file.offset, storedSize: file.compressedSize });
|
||||
const fileContents = extractFileStream(file, fileStream);
|
||||
console.debug(await streamToArrayBuffer(fileContents));
|
||||
//TODO: need to write to disk
|
||||
|
@ -46,7 +46,7 @@ export default async function getPatch(product: Product, from: number, to: numbe
|
|||
|
||||
//Extract changed files
|
||||
fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).forEach(async (file) => {
|
||||
const fileStream = getFileFromDisks(diskFilenames, { diskStart: file.diskNumberStart, offset: file.offset, storedSize: file.compressedSize });
|
||||
const fileStream = await getFileFromDisks(diskFilenames, { diskStart: file.diskNumberStart, offset: file.offset, storedSize: file.compressedSize });
|
||||
const fileContents = extractFileStream(file, fileStream);
|
||||
console.debug(await streamToArrayBuffer(fileContents));
|
||||
//TODO: need to apply diffing, then write to disk
|
||||
|
|
|
@ -16,7 +16,7 @@ function getStream(disks: string[], index: number, offset: number, length: numbe
|
|||
}
|
||||
|
||||
/** Takes a list of ReadableStreams (the disks), as well as the offset and length, and returns a stream for just one file. */
|
||||
export default function getFileFromDisks(disks: string[], { diskStart, offset, storedSize }: IGetFileFromDisksOptions): stream.Readable {
|
||||
export default async function getFileFromDisks(disks: string[], { diskStart, offset, storedSize }: IGetFileFromDisksOptions): Promise<stream.Readable> {
|
||||
let curDiskIndex = diskStart;
|
||||
let curDisk = getStream(disks, diskStart, offset);
|
||||
let localFileHeaderLength = 0;
|
||||
|
@ -56,6 +56,9 @@ export default function getFileFromDisks(disks: string[], { diskStart, offset, s
|
|||
//TODO
|
||||
}
|
||||
};
|
||||
const onError = (error: any) => {
|
||||
console.error(error);
|
||||
};
|
||||
const onEnd = () => {
|
||||
curDiskIndex += 1;
|
||||
//End if we are at end of file or end of disks
|
||||
|
@ -69,16 +72,13 @@ export default function getFileFromDisks(disks: string[], { diskStart, offset, s
|
|||
curDisk.on('error', onError);
|
||||
}
|
||||
};
|
||||
const onError = (error: any) => {
|
||||
console.error(error);
|
||||
};
|
||||
|
||||
curDisk.on('data', onData);
|
||||
curDisk.on('end', onEnd);
|
||||
curDisk.on('error', onError);
|
||||
|
||||
//Read local file header
|
||||
localFileHeaderLength = readLocalFileHeader(outputStream);
|
||||
localFileHeaderLength = await readLocalFileHeader(outputStream);
|
||||
|
||||
//TODO: now that local file header has been read, restrict length of stream to storedSize
|
||||
|
||||
|
|
|
@ -1,10 +1,24 @@
|
|||
import * as stream from 'stream';
|
||||
|
||||
/** Returns a promise that resolves as soon as the given stream has the given number of bytes ready for reading. */
|
||||
function waitReadableLength(inputStream: stream.Readable, minLength: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const interval = setInterval(() => {
|
||||
if (inputStream.readableLength >= minLength) {
|
||||
clearInterval(interval);
|
||||
resolve();
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
/** Reads the local file header, which is included before
|
||||
* each stored file, and advances the stream accordingly.
|
||||
* Returns length of the local file header.
|
||||
*/
|
||||
export default function readLocalFileHeader(inputStream: stream.Readable): number {
|
||||
export default async function readLocalFileHeader(inputStream: stream.Readable): Promise<number> {
|
||||
//TODO: need to wait until
|
||||
await waitReadableLength(inputStream, 30);
|
||||
const localFileHeader: Buffer = inputStream.read(30);
|
||||
|
||||
//Local file header signature
|
||||
|
@ -21,6 +35,7 @@ export default function readLocalFileHeader(inputStream: stream.Readable): numbe
|
|||
//skip local file name and extra field
|
||||
const additionalLength = localFilenameSize + localExtraSize;
|
||||
if (additionalLength > 0) {
|
||||
await waitReadableLength(inputStream, additionalLength);
|
||||
inputStream.read(additionalLength);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue