Skip to content

Commit

Permalink
Merge pull request #33 from jaruba/test-await-1
Browse files Browse the repository at this point in the history
  • Loading branch information
1313 authored Mar 19, 2024
2 parents 4bd4d7c + a6bea36 commit a6cfee6
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 33 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ const rarFilesPackage = new RarFilesPackage(localRarFiles);
async function writeInnerRarFilesToDisk() {
const innerFiles = await rarFilesPackage.parse();
for (const innerFile of innerFiles) {
innerFile
.createReadStream({ start: 0, end: innerFile.length - 1 })
.pipe(fs.createWriteStream(innerFile.name));
const stream = await innerFile.createReadStream({ start: 0, end: innerFile.length - 1 })
stream.pipe(fs.createWriteStream(innerFile.name));
}
}

Expand Down Expand Up @@ -109,10 +108,10 @@ Implements the [`FileMedia`](#filemedia-interface) interface.

#### Methods:

| Method | Description |
| ---------------------------------------------- | ----------------------------------------------------------------------- |
| createReadStream({start: number, end: number}) | Returns a `Readable` stream. The start and end interval is inclusive. |
| readToEnd | Returns a Promise with a Buffer containing all the content of the file. |
| Method | Description |
| ---------------------------------------------- | ------------------------------------------------------------------------------------ |
| createReadStream({start: number, end: number}) | Returns a Promise with a `Readable` stream. The start and end interval is inclusive. |
| readToEnd | Returns a Promise with a Buffer containing all the content of the file. |

#### Properties:

Expand All @@ -125,7 +124,7 @@ Implements the [`FileMedia`](#filemedia-interface) interface.

```
const innerFiles = await rarStreamPackage.parse();
const innerFileStream = innerFiles[0].createReadStream({ start: 0, end: 30});
const innerFileStream = await innerFiles[0].createReadStream({ start: 0, end: 30});
```

### _FileMedia Interface_
Expand All @@ -137,7 +136,7 @@ Should have the following shape:
```javascript
// FileMedia
{
createReadStream(interval: Interval): Readable,
createReadStream(interval: Interval): Promise<Readable>,
name: string,
length: number // Length or size of the file in bytes
}
Expand Down
4 changes: 2 additions & 2 deletions src/inner-file-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export class InnerFileStream extends Readable {
get isStarted() {
return !!this.stream;
}
next() {
async next() {
const chunk = this.rarFileChunks.shift();

if (!chunk) {
this.push(null);
} else {
this.stream = chunk.getStream();
this.stream = await chunk.getStream();
this.stream?.on("data", (data) => this.pushData(data));
this.stream?.on("end", () => this.next());
}
Expand Down
11 changes: 6 additions & 5 deletions src/inner-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export class InnerFile implements IFileMedia {

this.name = name;
}
readToEnd() {
return streamToBuffer(
this.createReadStream({ start: 0, end: this.length - 1 })
);
async readToEnd() {
const stream = await this.createReadStream({ start: 0, end: this.length - 1 });
return streamToBuffer(stream);
}
getChunksToStream(fileStart: number, fileEnd: number) {
const { index: startIndex, start: startOffset } =
Expand Down Expand Up @@ -58,7 +57,9 @@ export class InnerFile implements IFileMedia {
throw Error("Illegal start/end offset");
}

return new InnerFileStream(this.getChunksToStream(start, end));
return Promise.resolve(
new InnerFileStream(this.getChunksToStream(start, end))
);
}
calculateChunkMap(rarFileChunks: RarFileChunk[]) {
const chunkMap: ChunkMapEntry[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TerminatorHeaderParser } from "./parsing/terminator-header-parser.js";
export interface IFileMedia {
length: number;
name: string;
createReadStream(opts?: IReadInterval): NodeJS.ReadableStream;
createReadStream(opts?: IReadInterval): Promise<NodeJS.ReadableStream> | NodeJS.ReadableStream;
}
export interface IReadInterval {
start: number;
Expand Down
4 changes: 3 additions & 1 deletion src/local-file-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class LocalFileMedia implements IFileMedia {
this.length = statSync(path).size;
}
createReadStream(interval: IReadInterval) {
return createReadStream(this.path, interval);
return Promise.resolve(
createReadStream(this.path, interval)
);
}
}
4 changes: 3 additions & 1 deletion src/parsing/__mocks__/mock-file-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class MockFileMedia implements IFileMedia {
length: number;
name: string;
createReadStream(options: IReadInterval) {
return new MockFileStream(this.buffer, options);
return Promise.resolve(
new MockFileStream(this.buffer, options)
);
}
}
2 changes: 1 addition & 1 deletion src/rar-file-bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const newFileMedia = (name: string) =>
({
name,
length: 0,
createReadStream: () => new Readable(),
createReadStream: () => Promise.resolve(new Readable()),
} satisfies IFileMedia);

test("RarFileBundle length should be 0 with an empty array as input", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/rar-file-chunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test("RarFileChunk#getStream should return a stream from its FileMedia", async (
const bufferString = "123456789A";
const fileMedia = new MockFileMedia(bufferString);
const rarFileChunk = new RarFileChunk(fileMedia, 0, 5);
const stream = rarFileChunk.getStream();
const stream = await rarFileChunk.getStream();
const buffer = await streamToBuffer(stream);
expect(Buffer.from(bufferString, "hex")).toEqual(buffer);
});
Expand All @@ -16,7 +16,7 @@ test("RarFileChunk#getStream should return a stream with a subset stream of File
const bufferString = "123456789A";
const fileMedia = new MockFileMedia(bufferString);
const rarFileChunk = new RarFileChunk(fileMedia, 2, 5);
const stream = rarFileChunk.getStream();
const stream = await rarFileChunk.getStream();
const buffer = await streamToBuffer(stream);
expect(Buffer.from("56789A", "hex")).toEqual(buffer);
});
Expand All @@ -25,7 +25,7 @@ test("RarFileChunk#getStream should return a stream with another subset stream o
const bufferString = "123456789A";
const fileMedia = new MockFileMedia(bufferString);
const rarFileChunk = new RarFileChunk(fileMedia, 1, 3);
const stream = rarFileChunk.getStream();
const stream = await rarFileChunk.getStream();
const buffer = await streamToBuffer(stream);
expect(Buffer.from("3456", "hex")).toEqual(buffer);
});
Expand Down
18 changes: 9 additions & 9 deletions src/rar-files-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ test("single rar file with one inner files can be read in parts", async () => {
const rarPackage = new RarFilesPackage(singleFileRarWithOneInnerFile);

const [file] = await rarPackage.parse();
const rarFileInterval = file?.createReadStream(interval);
const rarFileInterval = await file?.createReadStream(interval);
const singleFileInterval = fs.createReadStream(singleFilePath, interval);
const rarFileBuffer = await streamToBuffer(rarFileInterval!);
const singleFileBuffer = await streamToBuffer(singleFileInterval);
Expand Down Expand Up @@ -150,13 +150,13 @@ test("single rar file with many inner files can be read in parts", async () => {
const [rarFile1, rarFile2, rarFile3] = await rarPackage.parse();

const rarFile1Buffer = await streamToBuffer(
rarFile1!.createReadStream(interval)
await rarFile1!.createReadStream(interval)
);
const rarFile2Buffer = await streamToBuffer(
rarFile2!.createReadStream(interval)
await rarFile2!.createReadStream(interval)
);
const rarFile3Buffer = await streamToBuffer(
rarFile3!.createReadStream(interval)
await rarFile3!.createReadStream(interval)
);

const splittedFile1Buffer = await streamToBuffer(
Expand Down Expand Up @@ -192,7 +192,7 @@ test("multiple rar file with one inner can be read as in parts", async () => {
const rarPackage = new RarFilesPackage(multipleRarFileWithOneInnerFile);

const [file] = await rarPackage.parse();
const rarFileBuffer = await streamToBuffer(file!.createReadStream(interval));
const rarFileBuffer = await streamToBuffer(await file!.createReadStream(interval));
const multiFileBuffer = await streamToBuffer(
fs.createReadStream(multiFilePath, interval)
);
Expand Down Expand Up @@ -225,16 +225,16 @@ test("multi rar file with many inner files can be read in parts", async () => {
const [rarFile1, rarFile2, rarFile3, rarFile4] = await rarPackage.parse();

const rarFile1Buffer = await streamToBuffer(
rarFile1!.createReadStream(interval)
await rarFile1!.createReadStream(interval)
);
const rarFile2Buffer = await streamToBuffer(
rarFile2!.createReadStream(interval)
await rarFile2!.createReadStream(interval)
);
const rarFile3Buffer = await streamToBuffer(
rarFile3!.createReadStream(interval)
await rarFile3!.createReadStream(interval)
);
const rarFile4Buffer = await streamToBuffer(
rarFile4!.createReadStream(interval)
await rarFile4!.createReadStream(interval)
);

const splittedFile1Buffer = await streamToBuffer(
Expand Down
2 changes: 1 addition & 1 deletion src/rar-files-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const parseHeader = async <T extends IParsers>(
fileMedia: IFileMedia,
offset = 0
) => {
const stream = fileMedia.createReadStream({
const stream = await fileMedia.createReadStream({
start: offset,
end: offset + Parser.HEADER_SIZE,
});
Expand Down

0 comments on commit a6cfee6

Please sign in to comment.