summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/libnpmfund/index.js
blob: 58aba028e362214aed614461fbcbb4638a245580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict'

const URL = require('url').URL
const Arborist = require('@npmcli/arborist')

// supports object funding and string shorthand, or an array of these
// if original was an array, returns an array; else returns the lone item
function normalizeFunding (funding) {
  const normalizeItem = item =>
    typeof item === 'string' ? { url: item } : item
  const sources = [].concat(funding || []).map(normalizeItem)
  return Array.isArray(funding) ? sources : sources[0]
}

// Is the value of a `funding` property of a `package.json`
// a valid type+url for `npm fund` to display?
function isValidFunding (funding) {
  if (!funding) return false

  if (Array.isArray(funding)) {
    return funding.every(f => !Array.isArray(f) && isValidFunding(f))
  }

  try {
    var parsed = new URL(funding.url || funding)
  } catch (error) {
    return false
  }

  if (
    parsed.protocol !== 'https:' &&
    parsed.protocol !== 'http:'
  ) return false

  return Boolean(parsed.host)
}

const empty = () => Object.create(null)

function readTree (tree, opts) {
  let packageWithFundingCount = 0
  const seen = new Set()
  const { countOnly } = opts || {}
  const _trailingDependencies = Symbol('trailingDependencies')

  function tracked (name, version) {
    const key = String(name) + String(version)
    if (seen.has(key)) {
      return true
    }
    seen.add(key)
  }

  function retrieveDependencies (dependencies) {
    const trailing = dependencies[_trailingDependencies]

    if (trailing) {
      return Object.assign(
        empty(),
        dependencies,
        trailing
      )
    }

    return dependencies
  }

  function hasDependencies (dependencies) {
    return dependencies && (
      Object.keys(dependencies).length ||
      dependencies[_trailingDependencies]
    )
  }

  function attachFundingInfo (target, funding) {
    if (funding && isValidFunding(funding)) {
      target.funding = normalizeFunding(funding)
      packageWithFundingCount++
    }
  }

  function getFundingDependencies (tree) {
    const edges = tree && tree.edgesOut && tree.edgesOut.values()
    if (!edges) return empty()

    const directDepsWithFunding = Array.from(edges).map(edge => {
      if (!edge || !edge.to) return empty()

      const node = edge.to.target || edge.to
      if (!node.package) return empty()

      const { name, funding, version } = node.package

      // avoids duplicated items within the funding tree
      if (tracked(name, version)) return empty()

      const fundingItem = {}

      if (version) {
        fundingItem.version = version
      }

      attachFundingInfo(fundingItem, funding)

      return {
        node,
        fundingItem
      }
    })

    return directDepsWithFunding.reduce(
      (res, { node, fundingItem }, i) => {
        if (!fundingItem ||
          fundingItem.length === 0 ||
          !node) return res

        // recurse
        const transitiveDependencies = node.edgesOut &&
          node.edgesOut.size > 0 &&
          getFundingDependencies(node)

        // if we're only counting items there's no need
        // to add all the data to the resulting object
        if (countOnly) return null

        if (hasDependencies(transitiveDependencies)) {
          fundingItem.dependencies =
            retrieveDependencies(transitiveDependencies)
        }

        if (isValidFunding(fundingItem.funding)) {
          res[node.package.name] = fundingItem
        } else if (hasDependencies(fundingItem.dependencies)) {
          res[_trailingDependencies] =
            Object.assign(
              empty(),
              res[_trailingDependencies],
              fundingItem.dependencies
            )
        }

        return res
      }, countOnly ? null : empty())
  }

  const treeDependencies = getFundingDependencies(tree)
  const result = {
    length: packageWithFundingCount
  }

  if (!countOnly) {
    const name =
      (tree && tree.package && tree.package.name) ||
      (tree && tree.name)
    result.name = name || (tree && tree.path)

    if (tree && tree.package && tree.package.version) {
      result.version = tree.package.version
    }

    if (tree && tree.package && tree.package.funding) {
      result.funding = normalizeFunding(tree.package.funding)
    }

    result.dependencies = retrieveDependencies(treeDependencies)
  }

  return result
}

async function read (opts) {
  const arb = new Arborist(opts)
  const tree = await arb.loadActual()

  return readTree(tree, opts)
}

module.exports = {
  read,
  readTree,
  normalizeFunding,
  isValidFunding
}