MCPcopy Create free account
hub / github.com/XmirrorSecurity/OpenSCA-cli / ParsePackageJsonWithLockV3

Function ParsePackageJsonWithLockV3

opensca/sca/javascript/npm.go:265–345  ·  view source on GitHub ↗

ParsePackageJsonWithLockV3 借助package.lock(v3)解析package.json

(pkgjson *PackageJson, pkglock *PackageLock)

Source from the content-addressed store, hash-verified

263
264// ParsePackageJsonWithLockV3 借助package.lock(v3)解析package.json
265func ParsePackageJsonWithLockV3(pkgjson *PackageJson, pkglock *PackageLock) *model.DepGraph {
266
267 if pkglock.LockfileVersion != 3 {
268 return nil
269 }
270
271 for jspath, js := range pkglock.Packages {
272 if js.File == nil {
273 js.File = model.NewFile("", jspath)
274 }
275 }
276
277 root := &model.DepGraph{Name: pkgjson.Name, Version: pkgjson.Version, Path: pkgjson.File.Relpath()}
278 // root.AppendLicense(pkgjson.License)
279
280 type expand struct {
281 js *PackageJson
282 path string
283 }
284 root.Expand = expand{js: pkgjson, path: ""}
285
286 _dep := model.NewDepGraphMap(nil, func(s ...string) *model.DepGraph { return &model.DepGraph{Name: s[0], Version: s[1]} }).LoadOrStore
287
288 findDep := func(name, basedir string) *model.DepGraph {
289 jspath, subjs := findFromNodeModules(name, basedir, pkglock.Packages)
290 if subjs == nil {
291 return nil
292 }
293 dep := _dep(name, subjs.Version, subjs.File.Relpath())
294 // dep.AppendLicense(subjs.License)
295 if dep.Expand == nil {
296 dep.Develop = subjs.Develop
297 dep.Expand = expand{
298 path: jspath,
299 js: subjs,
300 }
301 } else {
302 if !subjs.Develop {
303 dep.Develop = false
304 }
305 }
306 return dep
307 }
308
309 root.ForEachPath(func(p, n *model.DepGraph) bool {
310
311 njs := n.Expand.(expand)
312 if njs.js == nil {
313 return false
314 }
315
316 for name := range njs.js.Dependencies {
317 n.AppendChild(findDep(name, njs.path))
318 }
319
320 for name := range njs.js.PeerDependencies {
321 if meta, ok := njs.js.PeerDependenciesMeta[name]; ok {
322 if meta.Optional {

Callers 1

ParsePackageJsonWithLockFunction · 0.85

Calls 6

ForEachPathMethod · 0.95
NewFileFunction · 0.92
NewDepGraphMapFunction · 0.92
findFromNodeModulesFunction · 0.85
RelpathMethod · 0.80
AppendChildMethod · 0.80

Tested by

no test coverage detected