Skip to content

Commit 9d27e27

Browse files
committed
Fix Node.js warnings "Warning: Closing file descriptor # on garbage collection"
1 parent 7bfd032 commit 9d27e27

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## next
2+
3+
- 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
4+
15
## 0.1.4 (2024-10-30)
26

37
- Added `repo.currentBranch()` method

src/packed-pack.ts

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const oidSize = 20;
5454
const buffers = new Array(5000);
5555
let reuseBufferCount = 0;
5656

57+
// Use FinalizationRegistry to close a file handler owned by a PackContent instance
58+
// once it loses all its references and is collected by GC.
59+
// This fixes Node.js warnings such as "Warning: Closing file descriptor # on garbage collection",
60+
// which is deprecated in Node.js 22 and will result in an error being thrown in the future.
61+
const fileHandlerRegistry = new FinalizationRegistry((fh: fsPromises.FileHandle) => {
62+
fh.close();
63+
});
64+
5765
export class PackContent {
5866
static buildReverseIndex(pack: PackContent) {
5967
const reverseIndex = new Uint32Array(pack.size);
@@ -82,6 +90,7 @@ export class PackContent {
8290
public reverseIndex: PackReverseIndex | null
8391
) {
8492
this.cache = new Map();
93+
fileHandlerRegistry.register(this, fh);
8594
}
8695

8796
getObjectOffset(hash: Buffer) {

0 commit comments

Comments
 (0)