@yao-pkg/pkg 6.10.0 → 6.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -302,21 +302,25 @@ The startup time of the application might be reduced slightly.
302
302
 
303
303
  All pkg-cache [environment vars](https://github.com/yao-pkg/pkg-fetch#environment), plus:
304
304
 
305
- | Var | Description |
306
- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
307
- | `CHDIR` | Override process `chdir` |
308
- | `PKG_STRICT_VER` | Turn on some assertion in the walker code to assert that each file content/state that we appending to the virtual file system applies to a real file, not a symlink. |
309
- | `PKG_EXECPATH` | Used internally by `pkg`, do not override |
305
+ | Var | Description |
306
+ | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
307
+ | `CHDIR` | Override process `chdir` |
308
+ | `PKG_NATIVE_CACHE_PATH` | Override the base directory for caching extracted native addons at runtime (default: `~/.cache`) |
309
+ | `PKG_STRICT_VER` | Turn on some assertion in the walker code to assert that each file content/state that we appending to the virtual file system applies to a real file, not a symlink. |
310
+ | `PKG_EXECPATH` | Used internally by `pkg`, do not override |
310
311
 
311
312
  Examples
312
313
 
313
314
  ```bash
314
- # 1 - Using export
315
+ # 1 - Set cache path at build time (for pkg-fetch to cache Node.js binaries)
315
316
  export PKG_CACHE_PATH=/my/cache
316
317
  pkg app.js
317
318
 
318
- # 2 - Passing it before the script
319
- PKG_CACHE_PATH=/my/cache pkg app.js
319
+ # 2 - Set cache path at runtime (for packaged app to cache extracted native addons)
320
+ PKG_NATIVE_CACHE_PATH=/opt/myapp/cache ./myapp
321
+
322
+ # 3 - Both can be used together
323
+ PKG_CACHE_PATH=/build/cache PKG_NATIVE_CACHE_PATH=/runtime/cache pkg app.js
320
324
  ```
321
325
 
322
326
  ## Usage of packaged app
@@ -384,8 +388,17 @@ add the `.node` file directly in the `assets` field in `package.json`.
384
388
  The way Node.js requires native addon is different from a classic JS
385
389
  file. It needs to have a file on disk to load it, but `pkg` only generates
386
390
  one file. To circumvent this, `pkg` will extract native addon files to
387
- `$HOME/.cache/pkg/`. These files will stay on the disk after the process has
388
- exited and will be used again on the next process launch.
391
+ `$HOME/.cache/pkg/` by default. These files will stay on the disk after the
392
+ process has exited and will be used again on the next process launch.
393
+
394
+ You can customize the cache directory by setting the `PKG_NATIVE_CACHE_PATH`
395
+ environment variable. This is useful in enterprise environments where specific
396
+ directories may be restricted or monitored:
397
+
398
+ ```bash
399
+ # Set custom cache path for native addons
400
+ PKG_NATIVE_CACHE_PATH=/opt/myapp/cache ./myapp
401
+ ```
389
402
 
390
403
  When a package, that contains a native module, is being installed,
391
404
  the native module is compiled against current system-wide Node.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yao-pkg/pkg",
3
- "version": "6.10.0",
3
+ "version": "6.11.0",
4
4
  "description": "Package your Node.js project into an executable",
5
5
  "main": "lib-es5/index.js",
6
6
  "license": "MIT",
@@ -25,7 +25,7 @@
25
25
  "@babel/generator": "^7.23.0",
26
26
  "@babel/parser": "^7.23.0",
27
27
  "@babel/types": "^7.23.0",
28
- "@yao-pkg/pkg-fetch": "3.5.30",
28
+ "@yao-pkg/pkg-fetch": "3.5.31",
29
29
  "into-stream": "^6.0.0",
30
30
  "minimist": "^1.2.6",
31
31
  "multistream": "^4.1.0",
@@ -81,6 +81,7 @@
81
81
  "test:20": "node test/test.js node20 no-npm",
82
82
  "test:18": "node test/test.js node18 no-npm",
83
83
  "test:host": "node test/test.js host only-npm",
84
+ "bump:fetch": "yarn add @yao-pkg/pkg-fetch --exact",
84
85
  "release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it"
85
86
  },
86
87
  "simple-git-hooks": {
@@ -185,9 +185,7 @@ function copyFolderRecursiveSync(source, target) {
185
185
  const targetFolder = path.join(target, path.basename(source));
186
186
 
187
187
  // Check if target folder needs to be created or integrated
188
- if (!fs.existsSync(targetFolder)) {
189
- fs.mkdirSync(targetFolder);
190
- }
188
+ fs.mkdirSync(targetFolder, { recursive: true });
191
189
 
192
190
  // Copy
193
191
  if (fs.lstatSync(source).isDirectory()) {
@@ -249,13 +247,6 @@ function copyFolderRecursiveSync(source, target) {
249
247
  }
250
248
  }
251
249
 
252
- function createDirRecursively(dir) {
253
- if (!fs.existsSync(dir)) {
254
- createDirRecursively(path.join(dir, '..'));
255
- fs.mkdirSync(dir);
256
- }
257
- }
258
-
259
250
  /*
260
251
 
261
252
  // TODO move to some test
@@ -2196,6 +2187,15 @@ function payloadFileSync(pointer) {
2196
2187
  dlopen: process.dlopen,
2197
2188
  };
2198
2189
 
2190
+ // Allow users to override the cache base directory via PKG_NATIVE_CACHE_PATH environment variable
2191
+ // Default: path.join(homedir(), '.cache')
2192
+ // - Linux/macOS: /home/john/.cache or /Users/john/.cache
2193
+ // - Windows: C:\Users\John\.cache
2194
+ // Custom example: /opt/myapp/cache or C:\myapp\cache
2195
+ // Native addons will be extracted to: <PKG_NATIVE_CACHE_BASE>/pkg/<hash>
2196
+ const PKG_NATIVE_CACHE_BASE =
2197
+ process.env.PKG_NATIVE_CACHE_PATH || path.join(homedir(), '.cache');
2198
+
2199
2199
  function revertMakingLong(f) {
2200
2200
  if (/^\\\\\?\\/.test(f)) return f.slice(4);
2201
2201
  return f;
@@ -2215,10 +2215,9 @@ function payloadFileSync(pointer) {
2215
2215
  // the hash is needed to be sure we reload the module in case it changes
2216
2216
  const hash = createHash('sha256').update(moduleContent).digest('hex');
2217
2217
 
2218
- // Example: /home/john/.cache/pkg/<hash>
2219
- const tmpFolder = path.join(homedir(), '.cache/pkg', hash);
2218
+ const tmpFolder = path.join(PKG_NATIVE_CACHE_BASE, 'pkg', hash);
2220
2219
 
2221
- createDirRecursively(tmpFolder);
2220
+ fs.mkdirSync(tmpFolder, { recursive: true });
2222
2221
 
2223
2222
  // Example: moduleFolder = /snapshot/appname/node_modules/sharp/build/Release
2224
2223
  const parts = moduleFolder.split(path.sep);