Skip to content

Commit

Permalink
Fix Node.js warnings "Warning: Closing file descriptor # on garbage c…
Browse files Browse the repository at this point in the history
…ollection"
  • Loading branch information
lahmatiy committed Nov 15, 2024
1 parent 7bfd032 commit 9d27e27
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## next

- Fixed Node.js warnings such as "Warning: Closing file descriptor # on garbage collection", which is deprecated in Node.js 22 and will result in an error being thrown in the future

## 0.1.4 (2024-10-30)

- Added `repo.currentBranch()` method
Expand Down
9 changes: 9 additions & 0 deletions src/packed-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ const oidSize = 20;
const buffers = new Array(5000);
let reuseBufferCount = 0;

// Use FinalizationRegistry to close a file handler owned by a PackContent instance
// once it loses all its references and is collected by GC.
// This fixes Node.js warnings such as "Warning: Closing file descriptor # on garbage collection",
// which is deprecated in Node.js 22 and will result in an error being thrown in the future.
const fileHandlerRegistry = new FinalizationRegistry((fh: fsPromises.FileHandle) => {

Check failure on line 61 in src/packed-pack.ts

View workflow job for this annotation

GitHub Actions / TS check

Cannot find name 'FinalizationRegistry'.
fh.close();
});

export class PackContent {
static buildReverseIndex(pack: PackContent) {
const reverseIndex = new Uint32Array(pack.size);
Expand Down Expand Up @@ -82,6 +90,7 @@ export class PackContent {
public reverseIndex: PackReverseIndex | null
) {
this.cache = new Map();
fileHandlerRegistry.register(this, fh);
}

getObjectOffset(hash: Buffer) {
Expand Down

0 comments on commit 9d27e27

Please sign in to comment.