Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too many open files #4

Open
justbrownbear opened this issue Jul 25, 2022 · 0 comments
Open

Too many open files #4

justbrownbear opened this issue Jul 25, 2022 · 0 comments

Comments

@justbrownbear
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant