🐛 Fix end of file reached error

This commit is contained in:
C-3PO 2018-07-05 18:41:24 +02:00
parent 5c915776aa
commit 68348b3673
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
6 changed files with 42 additions and 5 deletions

View file

@ -24,7 +24,7 @@ interface ISsnFileEntry {
name: string;
/** Uncompressed size */
size: number;
/** Stored size (size of compressed data + 12 byte encryption header if applicable) */
/** Stored size (size of compressed data + 12 byte encryption header if applicable). Does not include the local ZIP file header, which is slightly longer than 30 bytes. */
compressedSize: number;
/** Decryption keys needed to decrypt the file */
decryptionKeys: [number, number, number] | undefined;

View file

@ -4,6 +4,7 @@ import * as stream from 'stream';
import * as zlib from 'zlib';
import { ISsnFileEntry } from '../interfaces/ISsnFileEntry';
import decryptStream from './streams/decryptStream';
import streamSetMaxLength from './streams/streamSetMaxLength';
/** Extracts the file with the given metadata from the stream.
* The stream must already start at the .zip's local file header
@ -32,9 +33,11 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
let curStream = inputStream;
curStream = streamSetMaxLength(curStream, file.compressedSize);
//pipe into decryption if file is encrypted
if (file.decryptionKeys !== undefined) {
const decryptTransform = decryptStream([...file.decryptionKeys] as [number, number, number]);
const decryptTransform = decryptStream(file.decryptionKeys);
curStream = curStream.pipe(decryptTransform);
}

View file

@ -32,7 +32,7 @@ export default async function getManifest(product: Product): Promise<IManifest>
throw new Error(`Expected .patchmanifest to contain a file called manifest.xml but it is called "${firstFile.name}".`);
}
const stream = arrayBufferToStream(ssnFile, firstFile.offset, firstFile.compressedSize);
const stream = arrayBufferToStream(ssnFile, firstFile.offset);
//Extract manifest.xml file
const patchmanifestStream = extractFileStream(firstFile, stream);

View file

@ -37,7 +37,7 @@ export default async function getSolidpkg(product: Product, from: number, to: nu
throw new Error(`Expected .solidpkg to contain a file called metafile.solid but it is called "${firstFile.name}".`);
}
const stream = arrayBufferToStream(ssnFile, firstFile.offset, firstFile.compressedSize);
const stream = arrayBufferToStream(ssnFile, firstFile.offset);
//Extract metafile.solid file
const solidFileStream = extractFileStream(firstFile, stream);

View file

@ -11,7 +11,7 @@ export default function arrayBufferToStream(arrayBuffer: ArrayBuffer, offset: nu
}
let position = offset;
const endPosition = (length !== undefined) ? offset + length : arrayBuffer.byteLength;
const endPosition = (length !== undefined) ? (offset + length) : arrayBuffer.byteLength;
const outStream = new stream.Readable({
read(size) {
const chunkSize = Math.min(size || BUFFER_SIZE, endPosition - position); //TODO: we can probably remove BUFFER_SIZE

View file

@ -0,0 +1,34 @@
import * as stream from 'stream';
/** Takes the given ReadableStream and returns a ReadableStream with the same contents but that terminates after the given length. */
export default function streamSetMaxLength(inputStream: stream.Readable, maxLength: number): stream.Readable {
if (maxLength <= 0) {
throw new RangeError('maxLength is out of bounds.');
}
let remaining = maxLength;
const outStream = new stream.Readable({
read(size) {
//If no size is provided, just pass through all remaining bytes
if (size === undefined) {
this.push(inputStream.read(remaining));
remaining = 0;
//End is reached, terminate stream
this.push(null);
} else {
//Otherwise, pass through however many bytes we can
const clampedSize = Math.min(size, remaining);
this.push(inputStream.read(clampedSize));
remaining -= clampedSize;
//If end is reached, terminate stream
if (remaining <= 0) {
this.push(null);
}
}
},
});
return outStream;
}