Skip to content

Commit

Permalink
lib,src: improve package.json parsing for modules
Browse files Browse the repository at this point in the history
Extend Node's use of simdjson to pull the fields that we need so we can
populate the object sent to create the package structure.

PR-URL: #191
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
trevnorris committed Oct 11, 2024
1 parent 8bab9fb commit a2f089b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 13 deletions.
16 changes: 15 additions & 1 deletion lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ function deserializePackageJSON(path, contents) {
3: plainImports,
4: plainExports,
5: optionalFilePath,
6: nsolid,
7: dependencies,
8: optionalDependencies,
9: bundledDependencies,
10: version,
} = contents;

// This is required to be used in getPackageScopeConfig.
Expand Down Expand Up @@ -67,6 +72,15 @@ function deserializePackageJSON(path, contents) {
ObjectDefineProperty(this, 'exports', { __proto__: null, value });
return this.exports;
},
nsolid: nsolid ? JSONParse(nsolid) : null,
dependencies: dependencies ? JSONParse(dependencies) : null,
optionalDependencies: optionalDependencies ?
JSONParse(optionalDependencies) :
null,
bundledDependencies: bundledDependencies ?
JSONParse(bundledDependencies) :
null,
version: version ? JSONParse(version) : null,
};
}

Expand All @@ -93,7 +107,7 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {

const result = deserializePackageJSON(jsonPath, parsed);
if (result.name) {
addPackage(jsonPath.slice(0, -12), null, true);
addPackage(jsonPath.slice(0, -12), result, true);
}

return result;
Expand Down
10 changes: 0 additions & 10 deletions lib/internal/nsolid_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ function addPackage(path, json, required = false) {
path = fixPath(path);
let info = packagePaths[path];

if (!json) {
try {
const binding = internalBinding('fs');
const { fs: { O_RDONLY } } = internalBinding('constants');
json = JSONParse(binding.readFileUtf8(path + '/package.json', O_RDONLY));
} catch {
return;
}
}

if (!json.name) {
debug('module at %s does not have a name', path);
}
Expand Down
92 changes: 90 additions & 2 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ Local<Array> BindingData::PackageConfig::Serialize(Realm* realm) const {
isolate, input.data(), NewStringType::kNormal, input.size())
.ToLocalChecked();
};
Local<Value> values[6] = {
Local<Value> values[11] = {
name.has_value() ? ToString(*name) : Undefined(isolate),
main.has_value() ? ToString(*main) : Undefined(isolate),
ToString(type),
imports.has_value() ? ToString(*imports) : Undefined(isolate),
exports.has_value() ? ToString(*exports) : Undefined(isolate),
ToString(file_path),
nsolid.has_value() ? ToString(*nsolid) : Undefined(isolate),
dependencies.has_value() ? ToString(*dependencies) : Undefined(isolate),
optionalDependencies.has_value() ?
ToString(*optionalDependencies) :
Undefined(isolate),
bundledDependencies.has_value() ?
ToString(*bundledDependencies) :
Undefined(isolate),
version.has_value() ? ToString(*version) : Undefined(isolate),
};
return Array::New(isolate, values, 6);
return Array::New(isolate, values, 11);
}

const BindingData::PackageConfig* BindingData::GetPackageJSON(
Expand Down Expand Up @@ -225,6 +234,85 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
default:
break;
}
} else if (key == "nsolid") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::object: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.nsolid = field_value;
break;
}
default:
break;
}
} else if (key == "dependencies") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::object:
case simdjson::ondemand::json_type::array: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.dependencies = field_value;
break;
}
default:
break;
}
} else if (key == "optionalDependencies") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::object:
case simdjson::ondemand::json_type::array: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.optionalDependencies = field_value;
break;
}
default:
break;
}
} else if (key == "bundledDependencies") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::object:
case simdjson::ondemand::json_type::array: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.bundledDependencies = field_value;
break;
}
default:
break;
}
} else if (key == "version") {
if (value.type().get(field_type)) {
return throw_invalid_package_config();
}
switch (field_type) {
case simdjson::ondemand::json_type::string:
case simdjson::ondemand::json_type::object: {
if (value.raw_json().get(field_value)) {
return throw_invalid_package_config();
}
package_config.version = field_value;
break;
}
default:
break;
}
}
}
// package_config could be quite large, so we should move it instead of
Expand Down
5 changes: 5 additions & 0 deletions src/node_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class BindingData : public SnapshotableObject {
std::optional<std::string> exports;
std::optional<std::string> imports;
std::optional<std::string> scripts;
std::optional<std::string> nsolid;
std::optional<std::string> version;
std::optional<std::string> dependencies;
std::optional<std::string> optionalDependencies;
std::optional<std::string> bundledDependencies;
std::string raw_json;

v8::Local<v8::Array> Serialize(Realm* realm) const;
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/node_modules/nsolid-type-module/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a2f089b

Please sign in to comment.