Skip to content

Commit

Permalink
Make file streams disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
kayahr committed Mar 16, 2024
1 parent 3c9fb15 commit e85b4ba
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"projects": [
{
"displayName": "node",
"testEnvironment": "jest-environment-node-single-context",
"testMatch": [
"<rootDir>/lib/test/**/*.test.js"
]
Expand Down Expand Up @@ -79,6 +80,7 @@
"eslint-plugin-deprecation": "2.0.0",
"eslint-plugin-simple-import-sort": "12.0.0",
"jest": "29.7.0",
"jest-environment-node-single-context": "29.4.0",
"rimraf": "5.0.5",
"source-map-support": "0.5.21",
"tmp-promise": "3.0.3",
Expand Down
7 changes: 6 additions & 1 deletion src/main/node/FileInputStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FileHandle, open } from "fs/promises";
/**
* File input stream for Node.js.
*/
export class FileInputStream extends ReadableStream<Uint8Array> {
export class FileInputStream extends ReadableStream<Uint8Array> implements AsyncDisposable {
/** The open file handle. Null if not yet opened. */
private file: FileHandle | null;

Expand Down Expand Up @@ -55,4 +55,9 @@ export class FileInputStream extends ReadableStream<Uint8Array> {
this.file = null;
}
}

/** @inheritDoc */
public [Symbol.asyncDispose](): PromiseLike<void> {
return this.close();
}
}
7 changes: 6 additions & 1 deletion src/main/node/FileOutputStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FileHandle, open } from "fs/promises";
/**
* File output stream for Node.js.
*/
export class FileOutputStream extends WritableStream<Uint8Array> {
export class FileOutputStream extends WritableStream<Uint8Array> implements AsyncDisposable {
/**
* Creates a new file output stream writing to the given file.
*
Expand All @@ -30,4 +30,9 @@ export class FileOutputStream extends WritableStream<Uint8Array> {
}
});
}

/** @inheritDoc */
public [Symbol.asyncDispose](): PromiseLike<void> {
return this.close();
}
}
9 changes: 9 additions & 0 deletions src/test/node/FileInputStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ describe("FileInputStream", () => {
it("can read bytes from given file with custom chunk size (500)", async () => {
await testStream(new FileInputStream(resolve(__dirname, `../../../src/test/data/iliad_utf-8.txt`), 500), 500);
});

it("is disposable", async () => {
let spy: jest.SpyInstance | null = null;
if (spy == null /* Always true, just here to create a block on which end the stream is disposed */) {
await using stream = new FileInputStream(resolve(__dirname, `../../../src/test/data/iliad_utf-8.txt`));
spy = jest.spyOn(stream, "close");
}
expect(spy).toHaveBeenCalledTimes(1);
});
});
12 changes: 12 additions & 0 deletions src/test/node/FileOutputStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ describe("FileOutputStream", () => {
await rm(tmpFile);
}
});

it("is disposable", async () => {
let spy: jest.SpyInstance;
const tmpFile = await tmpName();
try {
await using stream = new FileOutputStream(tmpFile);
spy = jest.spyOn(stream, "close");
} finally {
await rm(tmpFile);
}
expect(spy).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strict": true,
"strictPropertyInitialization": false,
"strictPropertyInitialization": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"skipLibCheck": true,
Expand Down

0 comments on commit e85b4ba

Please sign in to comment.