Skip to content

Too many open files #4

@justbrownbear

Description

@justbrownbear

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch vpk@0.2.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/vpk/index.js b/node_modules/vpk/index.js
index 17ba42e..7fef5dd 100644
--- a/node_modules/vpk/index.js
+++ b/node_modules/vpk/index.js
@@ -148,9 +148,11 @@ class VPK {
 
         let file = new Buffer(entry.preloadBytes + entry.entryLength);
 
+        let fileDescriptor = undefined;
+
         if (entry.preloadBytes > 0) {
-            let directoryFile = fs.openSync(this.directoryPath, 'r');
-            fs.readSync(directoryFile, file, 0, entry.preloadBytes, entry.preloadOffset);
+            fileDescriptor = fs.openSync(this.directoryPath, 'r');
+            fs.readSync(fileDescriptor, file, 0, entry.preloadBytes, entry.preloadOffset);
         }
 
         if (entry.entryLength > 0) {
@@ -164,18 +166,21 @@ class VPK {
                     offset += HEADER_2_LENGTH;
                 }
 
-                let directoryFile = fs.openSync(this.directoryPath, 'r');
-                fs.readSync(directoryFile, file, entry.preloadBytes, entry.entryLength, offset + entry.entryOffset);
+                fileDescriptor = fs.openSync(this.directoryPath, 'r');
+                fs.readSync(fileDescriptor, file, entry.preloadBytes, entry.entryLength, offset + entry.entryOffset);
             }
             else {
                 let fileIndex = ('000' + entry.archiveIndex).slice(-3);
                 let archivePath = this.directoryPath.replace(/_dir\.vpk$/, '_' + fileIndex + '.vpk');
 
-                let archiveFile = fs.openSync(archivePath, 'r');
-                fs.readSync(archiveFile, file, entry.preloadBytes, entry.entryLength, entry.entryOffset);
+                fileDescriptor = fs.openSync(archivePath, 'r');
+                fs.readSync(fileDescriptor, file, entry.preloadBytes, entry.entryLength, entry.entryOffset);
             }
         }
 
+        if( fileDescriptor )
+            fs.closeSync( fileDescriptor );
+
         if (crc.crc32(file) !== entry.crc) {
             throw new Error('CRC does not match');
         }

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions