|
13 | 13 | #include <unistd.h> // getuid
|
14 | 14 | #endif
|
15 | 15 |
|
| 16 | +#ifdef _WIN32 |
| 17 | +#include <windows.h> |
| 18 | +#endif |
16 | 19 | namespace node {
|
17 | 20 |
|
18 | 21 | using v8::Function;
|
@@ -223,13 +226,52 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
|
223 | 226 | Debug(" success, size=%d\n", total_read);
|
224 | 227 | }
|
225 | 228 |
|
| 229 | +static std::string GetRelativePath(std::string_view path, |
| 230 | + std::string_view base) { |
| 231 | +// On Windows, the native encoding is UTF-16, so we need to convert |
| 232 | +// the paths to wide strings before using std::filesystem::path. |
| 233 | +// On other platforms, std::filesystem::path can handle UTF-8 directly. |
| 234 | +#ifdef _WIN32 |
| 235 | + std::filesystem::path module_path( |
| 236 | + ConvertToWideString(std::string(path), CP_UTF8)); |
| 237 | + std::filesystem::path base_path( |
| 238 | + ConvertToWideString(std::string(base), CP_UTF8)); |
| 239 | +#else |
| 240 | + std::filesystem::path module_path(path); |
| 241 | + std::filesystem::path base_path(base); |
| 242 | +#endif |
| 243 | + std::filesystem::path relative = module_path.lexically_relative(base_path); |
| 244 | + auto u8str = relative.u8string(); |
| 245 | + return std::string(u8str.begin(), u8str.end()); |
| 246 | +} |
| 247 | + |
226 | 248 | CompileCacheEntry* CompileCacheHandler::GetOrInsert(Local<String> code,
|
227 | 249 | Local<String> filename,
|
228 | 250 | CachedCodeType type) {
|
229 | 251 | DCHECK(!compile_cache_dir_.empty());
|
230 | 252 |
|
| 253 | + Environment* env = Environment::GetCurrent(isolate_->GetCurrentContext()); |
231 | 254 | Utf8Value filename_utf8(isolate_, filename);
|
232 |
| - uint32_t key = GetCacheKey(filename_utf8.ToStringView(), type); |
| 255 | + std::string file_path = filename_utf8.ToString(); |
| 256 | + // If the portable cache is enabled and it seems possible to compute the |
| 257 | + // relative position from an absolute path, we use the relative position |
| 258 | + // in the cache key. |
| 259 | + if (portable_ == EnableOption::PORTABLE && IsAbsoluteFilePath(file_path)) { |
| 260 | + // Normalize the path to ensure it is consistent. |
| 261 | + std::string normalized_file_path = NormalizeFileURLOrPath(env, file_path); |
| 262 | + if (normalized_file_path.empty()) { |
| 263 | + return nullptr; |
| 264 | + } |
| 265 | + std::string relative_path = |
| 266 | + GetRelativePath(normalized_file_path, normalized_compile_cache_dir_); |
| 267 | + if (!relative_path.empty()) { |
| 268 | + file_path = relative_path; |
| 269 | + Debug("[compile cache] using relative path %s from %s\n", |
| 270 | + file_path.c_str(), |
| 271 | + compile_cache_dir_.c_str()); |
| 272 | + } |
| 273 | + } |
| 274 | + uint32_t key = GetCacheKey(file_path, type); |
233 | 275 |
|
234 | 276 | // TODO(joyeecheung): don't encode this again into UTF8. If we read the
|
235 | 277 | // UTF8 content on disk as raw buffer (from the JS layer, while watching out
|
@@ -500,7 +542,8 @@ CompileCacheHandler::CompileCacheHandler(Environment* env)
|
500 | 542 | // - $NODE_VERSION-$ARCH-$CACHE_DATA_VERSION_TAG-$UID
|
501 | 543 | // - $FILENAME_AND_MODULE_TYPE_HASH.cache: a hash of filename + module type
|
502 | 544 | CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env,
|
503 |
| - const std::string& dir) { |
| 545 | + const std::string& dir, |
| 546 | + EnableOption option) { |
504 | 547 | std::string cache_tag = GetCacheVersionTag();
|
505 | 548 | std::string absolute_cache_dir_base = PathResolve(env, {dir});
|
506 | 549 | std::string cache_dir_with_tag =
|
@@ -548,6 +591,11 @@ CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env,
|
548 | 591 |
|
549 | 592 | result.cache_directory = absolute_cache_dir_base;
|
550 | 593 | compile_cache_dir_ = cache_dir_with_tag;
|
| 594 | + portable_ = option; |
| 595 | + if (option == EnableOption::PORTABLE) { |
| 596 | + normalized_compile_cache_dir_ = |
| 597 | + NormalizeFileURLOrPath(env, compile_cache_dir_); |
| 598 | + } |
551 | 599 | result.status = CompileCacheEnableStatus::ENABLED;
|
552 | 600 | return result;
|
553 | 601 | }
|
|
0 commit comments