3
3
#include " base_object-inl.h"
4
4
#include " node_errors.h"
5
5
#include " node_external_reference.h"
6
+ #include " node_process-inl.h"
6
7
#include " node_url.h"
7
8
#include " permission/permission.h"
8
9
#include " permission/permission_base.h"
@@ -219,6 +220,21 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
219
220
if (field_value == " commonjs" || field_value == " module" ) {
220
221
package_config.type = field_value;
221
222
}
223
+ } else if (key == " scripts" ) {
224
+ if (value.type ().get (field_type)) {
225
+ return throw_invalid_package_config ();
226
+ }
227
+ switch (field_type) {
228
+ case simdjson::ondemand::json_type::object: {
229
+ if (value.raw_json ().get (field_value)) {
230
+ return throw_invalid_package_config ();
231
+ }
232
+ package_config.scripts = field_value;
233
+ break ;
234
+ }
235
+ default :
236
+ break ;
237
+ }
222
238
}
223
239
}
224
240
// package_config could be quite large, so we should move it instead of
@@ -344,6 +360,28 @@ void BindingData::GetNearestParentPackageJSONType(
344
360
args.GetReturnValue ().Set (Array::New (realm->isolate (), values, 3 ));
345
361
}
346
362
363
+ void BindingData::GetPackageJSONScripts (
364
+ const FunctionCallbackInfo<Value>& args) {
365
+ Realm* realm = Realm::GetCurrent (args);
366
+ std::string_view path = " package.json" ;
367
+
368
+ THROW_IF_INSUFFICIENT_PERMISSIONS (
369
+ realm->env (), permission::PermissionScope::kFileSystemRead , path);
370
+
371
+ auto package_json = GetPackageJSON (realm, path);
372
+ if (package_json == nullptr ) {
373
+ printf (" Can't read package.json\n " );
374
+ return ;
375
+ } else if (!package_json->scripts .has_value ()) {
376
+ printf (" Can't read package.json \" scripts\" object\n " );
377
+ return ;
378
+ }
379
+
380
+ args.GetReturnValue ().Set (
381
+ ToV8Value (realm->context (), package_json->scripts .value ())
382
+ .ToLocalChecked ());
383
+ }
384
+
347
385
void BindingData::GetPackageScopeConfig (
348
386
const FunctionCallbackInfo<Value>& args) {
349
387
CHECK_GE (args.Length (), 1 );
@@ -424,6 +462,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
424
462
" getNearestParentPackageJSON" ,
425
463
GetNearestParentPackageJSON);
426
464
SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
465
+ SetMethod (isolate, target, " getPackageJSONScripts" , GetPackageJSONScripts);
427
466
}
428
467
429
468
void BindingData::CreatePerContextProperties (Local<Object> target,
@@ -440,6 +479,7 @@ void BindingData::RegisterExternalReferences(
440
479
registry->Register (GetNearestParentPackageJSONType);
441
480
registry->Register (GetNearestParentPackageJSON);
442
481
registry->Register (GetPackageScopeConfig);
482
+ registry->Register (GetPackageJSONScripts);
443
483
}
444
484
445
485
} // namespace modules
0 commit comments