| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,9 +6,7 @@ const { | |||
| 6 | 6 | ObjectDefineProperty, | |
| 7 | 7 | RegExpPrototypeExec, | |
| 8 | 8 | SafeMap, | |
| 9 | - StringPrototypeEndsWith, | ||
| 10 | 9 | StringPrototypeIndexOf, | |
| 11 | - StringPrototypeLastIndexOf, | ||
| 12 | 10 | StringPrototypeSlice, | |
| 13 | 11 | } = primordials; | |
| 14 | 12 | const { | |
@@ -28,11 +26,9 @@ const { | |||
| 28 | 26 | const { kEmptyObject } = require('internal/util'); | |
| 29 | 27 | const modulesBinding = internalBinding('modules'); | |
| 30 | 28 | const path = require('path'); | |
| 31 | - const permission = require('internal/process/permission'); | ||
| 32 | 29 | const { validateString } = require('internal/validators'); | |
| 33 | 30 | const internalFsBinding = internalBinding('fs'); | |
| 34 | 31 | ||
| 35 | - const nearestParentPackageJSONCache = new SafeMap(); | ||
| 36 | 32 | ||
| 37 | 33 | /** | |
| 38 | 34 | * @typedef {import('typings/internalBinding/modules').DeserializedPackageConfig} DeserializedPackageConfig | |
@@ -68,28 +64,41 @@ function deserializePackageJSON(path, contents) { | |||
| 68 | 64 | ||
| 69 | 65 | const pjsonPath = optionalFilePath ?? path; | |
| 70 | 66 | ||
| 71 | - return { | ||
| 72 | - data: { | ||
| 67 | + const data = { | ||
| 68 | + __proto__: null, | ||
| 69 | + ...(name != null && { name }), | ||
| 70 | + ...(main != null && { main }), | ||
| 71 | + ...(type != null && { type }), | ||
| 72 | + }; | ||
| 73 | + | ||
| 74 | + if (plainExports !== null) { | ||
| 75 | + ObjectDefineProperty(data, 'exports', { | ||
| 76 | + __proto__: null, | ||
| 77 | + configurable: true, | ||
| 78 | + enumerable: true, | ||
| 79 | + get() { | ||
| 80 | + const value = requiresJSONParse(plainExports) ? JSONParse(plainExports) : plainExports; | ||
| 81 | + ObjectDefineProperty(data, 'exports', { __proto__: null, enumerable: true, value }); | ||
| 82 | + return value; | ||
| 83 | + }, | ||
| 84 | + }); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + if (plainImports !== null) { | ||
| 88 | + ObjectDefineProperty(data, 'imports', { | ||
| 73 | 89 | __proto__: null, | |
| 74 | - ...(name != null && { name }), | ||
| 75 | - ...(main != null && { main }), | ||
| 76 | - ...(type != null && { type }), | ||
| 77 | - ...(plainImports != null && { | ||
| 78 | - // This getters are used to lazily parse the imports and exports fields. | ||
| 79 | - get imports() { | ||
| 80 | - const value = requiresJSONParse(plainImports) ? JSONParse(plainImports) : plainImports; | ||
| 81 | - ObjectDefineProperty(this, 'imports', { __proto__: null, value }); | ||
| 82 | - return this.imports; | ||
| 83 | - }, | ||
| 84 | - }), | ||
| 85 | - ...(plainExports != null && { | ||
| 86 | - get exports() { | ||
| 87 | - const value = requiresJSONParse(plainExports) ? JSONParse(plainExports) : plainExports; | ||
| 88 | - ObjectDefineProperty(this, 'exports', { __proto__: null, value }); | ||
| 89 | - return this.exports; | ||
| 90 | - }, | ||
| 91 | - }), | ||
| 92 | - }, | ||
| 90 | + configurable: true, | ||
| 91 | + enumerable: true, | ||
| 92 | + get() { | ||
| 93 | + const value = requiresJSONParse(plainImports) ? JSONParse(plainImports) : plainImports; | ||
| 94 | + ObjectDefineProperty(data, 'imports', { __proto__: null, enumerable: true, value }); | ||
| 95 | + return value; | ||
| 96 | + }, | ||
| 97 | + }); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + return { | ||
| 101 | + data, | ||
| 93 | 102 | exists: true, | |
| 94 | 103 | path: pjsonPath, | |
| 95 | 104 | }; | |
@@ -131,43 +140,23 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) { | |||
| 131 | 140 | } | |
| 132 | 141 | ||
| 133 | 142 | /** | |
| 134 | - * Given a file path, walk the filesystem upwards until we find its closest parent | ||
| 135 | - * `package.json` file, stopping when: | ||
| 136 | - * 1. we find a `package.json` file; | ||
| 137 | - * 2. we find a path that we do not have permission to read; | ||
| 138 | - * 3. we find a containing `node_modules` directory; | ||
| 139 | - * 4. or, we reach the filesystem root | ||
| 140 | - * @returns {undefined | string} | ||
| 143 | + * A cache mapping a module's path to its parent `package.json` file's path. | ||
| 144 | + * This is used in concert with `deserializedPackageJSONCache` to improve | ||
| 145 | + * the performance of `getNearestParentPackageJSON` when called repeatedly | ||
| 146 | + * on the same module paths. | ||
| 141 | 147 | */ | |
| 142 | - function findParentPackageJSON(checkPath) { | ||
| 143 | - const enabledPermission = permission.isEnabled(); | ||
| 144 | - | ||
| 145 | - const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, path.sep); | ||
| 146 | - let separatorIndex; | ||
| 147 | - | ||
| 148 | - do { | ||
| 149 | - separatorIndex = StringPrototypeLastIndexOf(checkPath, path.sep); | ||
| 150 | - checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex); | ||
| 148 | + const moduleToParentPackageJSONCache = new SafeMap(); | ||
| 151 | 149 | ||
| 152 | - if (enabledPermission && !permission.has('fs.read', checkPath + path.sep)) { | ||
| 153 | - return undefined; | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | - if (StringPrototypeEndsWith(checkPath, path.sep + 'node_modules')) { | ||
| 157 | - return undefined; | ||
| 158 | - } | ||
| 159 | - | ||
| 160 | - const maybePackageJSONPath = checkPath + path.sep + 'package.json'; | ||
| 161 | - const stat = internalFsBinding.internalModuleStat(checkPath + path.sep + 'package.json'); | ||
| 162 | - | ||
| 163 | - const packageJSONExists = stat === 0; | ||
| 164 | - if (packageJSONExists) { | ||
| 165 | - return maybePackageJSONPath; | ||
| 166 | - } | ||
| 167 | - } while (separatorIndex > rootSeparatorIndex); | ||
| 168 | - | ||
| 169 | - return undefined; | ||
| 170 | - } | ||
| 150 | + /** | ||
| 151 | + * A cache mapping the path of a `package.json` file to its | ||
| 152 | + * {@link DeserializedPackageConfig deserialized representation}, | ||
| 153 | + * as produced by {@link deserializedPackageJSONCache}. The purpose of this | ||
| 154 | + * cache is to ensure that we always return the same | ||
| 155 | + * {@link DeserializedPackageConfig} instance for a given `package.json`, | ||
| 156 | + * which is necessary to ensure that we don't re-parse `imports` and | ||
| 157 | + * `exports` redundantly. | ||
| 158 | + */ | ||
| 159 | + const deserializedPackageJSONCache = new SafeMap(); | ||
| 171 | 160 | ||
| 172 | 161 | /** | |
| 173 | 162 | * Get the nearest parent package.json file from a given path. | |
@@ -176,26 +165,22 @@ function findParentPackageJSON(checkPath) { | |||
| 176 | 165 | * @returns {undefined | DeserializedPackageConfig} | |
| 177 | 166 | */ | |
| 178 | 167 | function getNearestParentPackageJSON(checkPath) { | |
| 179 | - const nearestParentPackageJSON = findParentPackageJSON(checkPath); | ||
| 180 | - | ||
| 181 | - if (nearestParentPackageJSON === undefined) { | ||
| 182 | - return undefined; | ||
| 168 | + const parentPackageJSONPath = moduleToParentPackageJSONCache.get(checkPath); | ||
| 169 | + if (parentPackageJSONPath !== undefined) { | ||
| 170 | + return deserializedPackageJSONCache.get(parentPackageJSONPath); | ||
| 183 | 171 | } | |
| 184 | 172 | ||
| 185 | - if (nearestParentPackageJSONCache.has(nearestParentPackageJSON)) { | ||
| 186 | - return nearestParentPackageJSONCache.get(nearestParentPackageJSON); | ||
| 187 | - } | ||
| 173 | + const result = modulesBinding.getNearestParentPackageJSON(checkPath); | ||
| 174 | + const packageConfig = deserializePackageJSON(checkPath, result); | ||
| 188 | 175 | ||
| 189 | - const result = modulesBinding.readPackageJSON(nearestParentPackageJSON); | ||
| 176 | + moduleToParentPackageJSONCache.set(checkPath, packageConfig.path); | ||
| 190 | 177 | ||
| 191 | - if (result === undefined) { | ||
| 192 | - nearestParentPackageJSONCache.set(checkPath, undefined); | ||
| 193 | - return undefined; | ||
| 178 | + const maybeCachedPackageConfig = deserializedPackageJSONCache.get(packageConfig.path); | ||
| 179 | + if (maybeCachedPackageConfig !== undefined) { | ||
| 180 | + return maybeCachedPackageConfig; | ||
| 194 | 181 | } | |
| 195 | 182 | ||
| 196 | - const packageConfig = deserializePackageJSON(checkPath, result); | ||
| 197 | - nearestParentPackageJSONCache.set(nearestParentPackageJSON, packageConfig); | ||
| 198 | - | ||
| 183 | + deserializedPackageJSONCache.set(packageConfig.path, packageConfig); | ||
| 199 | 184 | return packageConfig; | |
| 200 | 185 | } | |
| 201 | 186 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,15 +97,27 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( | |||
| 97 | 97 | ||
| 98 | 98 | auto cache_entry = binding_data->package_configs_.find(path.data()); | |
| 99 | 99 | if (cache_entry != binding_data->package_configs_.end()) { | |
| 100 | - return &cache_entry->second; | ||
| 100 | + auto& cache_value = cache_entry->second; | ||
| 101 | + if (cache_value) { | ||
| 102 | + return &*cache_value; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + // If we have a cache entry without a value, we've already | ||
| 106 | + // attempted to open and read this path and couldn't (it most | ||
| 107 | + // likely doesn't exist) | ||
| 108 | + return nullptr; | ||
| 101 | 109 | } | |
| 102 | 110 | ||
| 103 | 111 | PackageConfig package_config{}; | |
| 104 | 112 | package_config.file_path = path; | |
| 105 | 113 | // No need to exclude BOM since simdjson will skip it. | |
| 106 | 114 | if (ReadFileSync(&package_config.raw_json, path.data()) < 0) { | |
| 115 | + // Add `nullopt` to the package config cache so that we don't | ||
| 116 | + // need to open and attempt to read this path again | ||
| 117 | + binding_data->package_configs_.insert({std::string(path), std::nullopt}); | ||
| 107 | 118 | return nullptr; | |
| 108 | 119 | } | |
| 120 | + | ||
| 109 | 121 | simdjson::ondemand::document document; | |
| 110 | 122 | simdjson::ondemand::object main_object; | |
| 111 | 123 | simdjson::error_code error = | |
@@ -238,7 +250,7 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( | |||
| 238 | 250 | auto cached = binding_data->package_configs_.insert( | |
| 239 | 251 | {std::string(path), std::move(package_config)}); | |
| 240 | 252 | ||
| 241 | - return &cached.first->second; | ||
| 253 | + return &*cached.first->second; | ||
| 242 | 254 | } | |
| 243 | 255 | ||
| 244 | 256 | void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) { | |
@@ -321,24 +333,49 @@ const BindingData::PackageConfig* BindingData::TraverseParent( | |||
| 321 | 333 | return nullptr; | |
| 322 | 334 | } | |
| 323 | 335 | ||
| 324 | - void BindingData::GetNearestParentPackageJSONType( | ||
| 325 | - const FunctionCallbackInfo<Value>& args) { | ||
| 336 | + const std::filesystem::path BindingData::NormalizePath( | ||
| 337 | + Realm* realm, BufferValue* path_value) { | ||
| 338 | + // Check if the path has a trailing slash. If so, add it after | ||
| 339 | + // ToNamespacedPath() as it will be deleted by ToNamespacedPath() | ||
| 340 | + bool slashCheck = path_value->ToStringView().ends_with(kPathSeparator); | ||
| 341 | + | ||
| 342 | + ToNamespacedPath(realm->env(), path_value); | ||
| 343 | + | ||
| 344 | + auto path = path_value->ToPath(); | ||
| 345 | + | ||
| 346 | + if (slashCheck) { | ||
| 347 | + path /= ""; | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + return path; | ||
| 351 | + } | ||
| 352 | + | ||
| 353 | + void BindingData::GetNearestParentPackageJSON( | ||
| 354 | + const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
| 326 | 355 | CHECK_GE(args.Length(), 1); | |
| 327 | 356 | CHECK(args[0]->IsString()); | |
| 328 | 357 | ||
| 329 | 358 | Realm* realm = Realm::GetCurrent(args); | |
| 330 | 359 | BufferValue path_value(realm->isolate(), args[0]); | |
| 331 | - // Check if the path has a trailing slash. If so, add it after | ||
| 332 | - // ToNamespacedPath() as it will be deleted by ToNamespacedPath() | ||
| 333 | - bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator); | ||
| 334 | 360 | ||
| 335 | - ToNamespacedPath(realm->env(), &path_value); | ||
| 361 | + auto path = NormalizePath(realm, &path_value); | ||
| 336 | 362 | ||
| 337 | - auto path = path_value.ToPath(); | ||
| 363 | + auto package_json = TraverseParent(realm, path); | ||
| 338 | 364 | ||
| 339 | - if (slashCheck) { | ||
| 340 | - path /= ""; | ||
| 365 | + if (package_json != nullptr) { | ||
| 366 | + args.GetReturnValue().Set(package_json->Serialize(realm)); | ||
| 341 | 367 | } | |
| 368 | + } | ||
| 369 | + | ||
| 370 | + void BindingData::GetNearestParentPackageJSONType( | ||
| 371 | + const FunctionCallbackInfo<Value>& args) { | ||
| 372 | + CHECK_GE(args.Length(), 1); | ||
| 373 | + CHECK(args[0]->IsString()); | ||
| 374 | + | ||
| 375 | + Realm* realm = Realm::GetCurrent(args); | ||
| 376 | + BufferValue path_value(realm->isolate(), args[0]); | ||
| 377 | + | ||
| 378 | + auto path = NormalizePath(realm, &path_value); | ||
| 342 | 379 | ||
| 343 | 380 | auto package_json = TraverseParent(realm, path); | |
| 344 | 381 | ||
@@ -569,6 +606,10 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, | |||
| 569 | 606 | target, | |
| 570 | 607 | "getNearestParentPackageJSONType", | |
| 571 | 608 | GetNearestParentPackageJSONType); | |
| 609 | + SetMethod(isolate, | ||
| 610 | + target, | ||
| 611 | + "getNearestParentPackageJSON", | ||
| 612 | + GetNearestParentPackageJSON); | ||
| 572 | 613 | SetMethod( | |
| 573 | 614 | isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>); | |
| 574 | 615 | SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>); | |
@@ -624,6 +665,7 @@ void BindingData::RegisterExternalReferences( | |||
| 624 | 665 | ExternalReferenceRegistry* registry) { | |
| 625 | 666 | registry->Register(ReadPackageJSON); | |
| 626 | 667 | registry->Register(GetNearestParentPackageJSONType); | |
| 668 | + registry->Register(GetNearestParentPackageJSON); | ||
| 627 | 669 | registry->Register(GetPackageScopeConfig<false>); | |
| 628 | 670 | registry->Register(GetPackageScopeConfig<true>); | |
| 629 | 671 | registry->Register(EnableCompileCache); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,8 @@ class BindingData : public SnapshotableObject { | |||
| 55 | 55 | SET_MEMORY_INFO_NAME(BindingData) | |
| 56 | 56 | ||
| 57 | 57 | static void ReadPackageJSON(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 58 | + static void GetNearestParentPackageJSON( | ||
| 59 | + const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| 58 | 60 | static void GetNearestParentPackageJSONType( | |
| 59 | 61 | const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 60 | 62 | template <bool return_only_type> | |
@@ -72,8 +74,17 @@ class BindingData : public SnapshotableObject { | |||
| 72 | 74 | static void RegisterExternalReferences(ExternalReferenceRegistry* registry); | |
| 73 | 75 | ||
| 74 | 76 | private: | |
| 75 | - std::unordered_map<std::string, PackageConfig> package_configs_; | ||
| 77 | + /* | ||
| 78 | + * This map caches `PackageConfig` values by `package.json` path. | ||
| 79 | + * An empty optional value indicates that no `package.json` file | ||
| 80 | + * at the given path exists, which we cache to avoid repeated | ||
| 81 | + * attempts to open the same non-existent paths. | ||
| 82 | + */ | ||
| 83 | + std::unordered_map<std::string, std::optional<PackageConfig> > | ||
| 84 | + package_configs_; | ||
| 76 | 85 | simdjson::ondemand::parser json_parser; | |
| 86 | + static const std::filesystem::path NormalizePath(Realm* realm, | ||
| 87 | + BufferValue* path_value); | ||
| 77 | 88 | // returns null on error | |
| 78 | 89 | static const PackageConfig* GetPackageJSON( | |
| 79 | 90 | Realm* realm, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ export type SerializedPackageConfig = [ | |||
| 23 | 23 | export interface ModulesBinding { | |
| 24 | 24 | readPackageJSON(path: string): SerializedPackageConfig | undefined; | |
| 25 | 25 | getNearestParentPackageJSONType(path: string): PackageConfig['type'] | |
| 26 | + getNearestParentPackageJSON(path: string): SerializedPackageConfig | undefined | ||
| 26 | 27 | getPackageScopeConfig(path: string): SerializedPackageConfig | undefined | |
| 27 | 28 | getPackageType(path: string): PackageConfig['type'] | undefined | |
| 28 | 29 | enableCompileCache(path?: string): { status: number, message?: string, directory?: string } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments