| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 797ef40 commit 5821362
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3704,8 +3704,9 @@ Enable the [module compile cache][] for the Node.js instance. See the documentat | |||
| 3704 | 3704 | ||
| 3705 | 3705 | ### `NODE_COMPILE_CACHE_PORTABLE=1` | |
| 3706 | 3706 | ||
| 3707 | - When set to 1, the [module compile cache][] can be reused across different directory | ||
| 3708 | - locations as long as the module layout relative to the cache directory remains the same. | ||
| 3707 | + When set to 1, the [module compile cache][] can be reused across different directory | ||
| 3708 | + locations as long as the module layout relative to the cache directory remains the same, | ||
| 3709 | + and by any user (the cache subdirectory is not suffixed with the creating user's uid). | ||
| 3709 | 3710 | ||
| 3710 | 3711 | ### `NODE_DEBUG=module[,…]` | |
| 3711 | 3712 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -405,6 +405,14 @@ to the cache directory remains the same. This would be done on a best-effort bas | |||
| 405 | 405 | Node.js cannot compute the location of a module relative to the cache directory, the module | |
| 406 | 406 | will not be cached. | |
| 407 | 407 | ||
| 408 | + A portable cache is also not split by user: on platforms with uids the | ||
| 409 | + cache subdirectory of a non-portable cache is suffixed with the uid of the | ||
| 410 | + user who created it, so it is only found by that user, while a portable | ||
| 411 | + cache uses the same subdirectory for every user. This lets a cache generated | ||
| 412 | + once (for example at build time, then shipped read-only with an application) | ||
| 413 | + be read by whoever runs the code; a user who cannot write to the directory | ||
| 414 | + still reads it, and a failed write only means the module is compiled again. | ||
| 415 | + | ||
| 408 | 416 | There are two ways to enable the portable mode: | |
| 409 | 417 | ||
| 410 | 418 | 1. Using the portable option in [`module.enableCompileCache()`][]: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1858,8 +1858,9 @@ Enable the module compile cache for the Node.js instance. See the documentation | |||
| 1858 | 1858 | module compile cache for details. | |
| 1859 | 1859 | . | |
| 1860 | 1860 | .It Ev NODE_COMPILE_CACHE_PORTABLE Ar 1 | |
| 1861 | - When set to 1, the module compile cache can be reused across different directory | ||
| 1862 | - locations as long as the module layout relative to the cache directory remains the same. | ||
| 1861 | + When set to 1, the module compile cache can be reused across different directory | ||
| 1862 | + locations as long as the module layout relative to the cache directory remains the same, | ||
| 1863 | + and by any user (the cache subdirectory is not suffixed with the creating user's uid). | ||
| 1863 | 1864 | . | |
| 1864 | 1865 | .It Ev NODE_DEBUG Ar module[,…] | |
| 1865 | 1866 | \fB','\fR-separated list of core modules that should print debug information. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,16 +44,23 @@ uint32_t GetHash(const char* data, size_t size) { | |||
| 44 | 44 | return crc32(crc, reinterpret_cast<const Bytef*>(data), size); | |
| 45 | 45 | } | |
| 46 | 46 | ||
| 47 | - std::string GetCacheVersionTag() { | ||
| 47 | + std::string GetCacheVersionTag(EnableOption option) { | ||
| 48 | + std::string tag = std::string(NODE_VERSION) + '-' + std::string(NODE_ARCH) + | ||
| 49 | + '-' + Uint32ToHex(ScriptCompiler::CachedDataVersionTag()); | ||
| 50 | + #ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS | ||
| 48 | 51 | // On platforms where uids are available, use different folders for | |
| 49 | 52 | // different users to avoid cache miss due to permission incompatibility. | |
| 50 | 53 | // On platforms where uids are not available, bare with the cache miss. | |
| 51 | 54 | // This should be fine on Windows, as there local directories tend to be | |
| 52 | 55 | // user-specific. | |
| 53 | - std::string tag = std::string(NODE_VERSION) + '-' + std::string(NODE_ARCH) + | ||
| 54 | - '-' + Uint32ToHex(ScriptCompiler::CachedDataVersionTag()); | ||
| 55 | - #ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS | ||
| 56 | - tag += '-' + std::to_string(getuid()); | ||
| 56 | + // A portable cache is meant to be reused wherever the same layout is | ||
| 57 | + // found, including by other users (e.g. a cache generated at build time | ||
| 58 | + // and shipped read-only with an application), so it is not split by uid: | ||
| 59 | + // a user who cannot write to it still reads it, and a failed write is | ||
| 60 | + // only a cache miss. | ||
| 61 | + if (option != EnableOption::PORTABLE) { | ||
| 62 | + tag += '-' + std::to_string(getuid()); | ||
| 63 | + } | ||
| 57 | 64 | #endif | |
| 58 | 65 | return tag; | |
| 59 | 66 | } | |
@@ -532,11 +539,12 @@ CompileCacheHandler::CompileCacheHandler(Environment* env) | |||
| 532 | 539 | // Directory structure: | |
| 533 | 540 | // - Compile cache directory (from NODE_COMPILE_CACHE) | |
| 534 | 541 | // - $NODE_VERSION-$ARCH-$CACHE_DATA_VERSION_TAG-$UID | |
| 542 | + // ($UID is omitted for a portable cache) | ||
| 535 | 543 | // - $FILENAME_AND_MODULE_TYPE_HASH.cache: a hash of filename + module type | |
| 536 | 544 | CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env, | |
| 537 | 545 | const std::string& dir, | |
| 538 | 546 | EnableOption option) { | |
| 539 | - std::string cache_tag = GetCacheVersionTag(); | ||
| 547 | + std::string cache_tag = GetCacheVersionTag(option); | ||
| 540 | 548 | std::string absolute_cache_dir_base = PathResolve(env, {dir}); | |
| 541 | 549 | std::string cache_dir_with_tag = | |
| 542 | 550 | absolute_cache_dir_base + kPathSeparator + cache_tag; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,14 @@ const NODE_TEST_COMPILE_CACHE_OPTIONS = JSON.stringify({ directory: cacheRel, po | |||
| 57 | 57 | ); | |
| 58 | 58 | } | |
| 59 | 59 | ||
| 60 | + // The cache subdirectory of a portable cache is not suffixed with the uid, | ||
| 61 | + // so a cache generated by one user is found by another. | ||
| 62 | + { | ||
| 63 | + const [tag] = fs.readdirSync(path.join(workDir, cacheRel)); | ||
| 64 | + // $NODE_VERSION-$ARCH-$CACHE_DATA_VERSION_TAG, with no -$UID. | ||
| 65 | + assert.match(tag, new RegExp(`^${process.version}-${process.arch}-[0-9a-f]+$`)); | ||
| 66 | + } | ||
| 67 | + | ||
| 60 | 68 | // Second run — moved directory, but same relative cache path | |
| 61 | 69 | { | |
| 62 | 70 | const movedWorkDir = `${workDir}_moved`; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments