PHP 8.2.30
Preview: inventory.js Size: 3.20 KB
/proc/self/root/opt/alt/alt-nodejs19/root/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/inventory.js

// a class to manage an inventory and set of indexes of
// a set of objects based on specific fields.
// primary is the primary index key.
// keys is the set of fields to be able to query.
const _primaryKey = Symbol('_primaryKey')
const _index = Symbol('_index')
const defaultKeys = ['name', 'license', 'funding', 'realpath', 'packageName']
const { hasOwnProperty } = Object.prototype
const debug = require('./debug.js')

// handling for the outdated "licenses" array, just pick the first one
// also support the alternative spelling "licence"
const getLicense = pkg => {
  if (pkg) {
    const lic = pkg.license || pkg.licence
    if (lic) {
      return lic
    }
    const lics = pkg.licenses || pkg.licences
    if (Array.isArray(lics)) {
      return lics[0]
    }
  }
}

class Inventory extends Map {
  constructor (opt = {}) {
    const { primary, keys } = opt
    super()
    this[_primaryKey] = primary || 'location'
    this[_index] = (keys || defaultKeys).reduce((index, i) => {
      index.set(i, new Map())
      return index
    }, new Map())
  }

  get primaryKey () {
    return this[_primaryKey]
  }

  get indexes () {
    return [...this[_index].keys()]
  }

  * filter (fn) {
    for (const node of this.values()) {
      if (fn(node)) {
        yield node
      }
    }
  }

  add (node) {
    const root = super.get('')
    if (root && node.root !== root && node.root !== root.root) {
      debug(() => {
        throw Object.assign(new Error('adding external node to inventory'), {
          root: root.path,
          node: node.path,
          nodeRoot: node.root.path,
        })
      })
      return
    }

    const current = super.get(node[this.primaryKey])
    if (current) {
      if (current === node) {
        return
      }
      this.delete(current)
    }
    super.set(node[this.primaryKey], node)
    for (const [key, map] of this[_index].entries()) {
      // if the node has the value, but it's false, then use that
      const val_ = hasOwnProperty.call(node, key) ? node[key]
        : key === 'license' ? getLicense(node.package)
        : node[key] ? node[key]
        : node.package && node.package[key]
      const val = typeof val_ === 'string' ? val_
        : !val_ || typeof val_ !== 'object' ? val_
        : key === 'license' ? val_.type
        : key === 'funding' ? val_.url
        : /* istanbul ignore next - not used */ val_
      const set = map.get(val) || new Set()
      set.add(node)
      map.set(val, set)
    }
  }

  delete (node) {
    if (!this.has(node)) {
      return
    }

    super.delete(node[this.primaryKey])
    for (const [key, map] of this[_index].entries()) {
      const val = node[key] !== undefined ? node[key]
        : (node[key] || (node.package && node.package[key]))
      const set = map.get(val)
      if (set) {
        set.delete(node)
        if (set.size === 0) {
          map.delete(node[key])
        }
      }
    }
  }

  query (key, val) {
    const map = this[_index].get(key)
    return map && (arguments.length === 2 ? map.get(val) : map.keys()) ||
      new Set()
  }

  has (node) {
    return super.get(node[this.primaryKey]) === node
  }

  set (k, v) {
    throw new Error('direct set() not supported, use inventory.add(node)')
  }
}

module.exports = Inventory

Directory Contents

Dirs: 1 × Files: 38

Name Size Perms Modified Actions
arborist DIR
- drwxr-xr-x 2024-03-03 22:54:11
Edit Download
4.89 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
11.99 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.07 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
13.94 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.32 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.29 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.20 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
691 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.94 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
9.57 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
6.83 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.04 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.26 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
863 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
353 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.20 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.06 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
43.26 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.32 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
225 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.12 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.57 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
19.76 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
5.09 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
26.17 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.58 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
131 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
638 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
491 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
36.60 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.19 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.35 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
874 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.29 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.02 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.45 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
5.93 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
10.58 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).