summary refs log tree commit diff
path: root/pkgs/development/tools/yarn2nix-moretea/yarn2nix/lib/fixPkgAddMissingSha1.js
blob: 2826f36e326063191efedfabc42d680db77a0d42 (plain) (blame)
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
const https = require('https')
const crypto = require('crypto')

// TODO:
// make test case where getSha1 function is used, i.e. the case when resolved is without sha1?
// consider using https://github.com/request/request-promise-native

function getSha1(url) {
  return new Promise((resolve, reject) => {
    https.get(url, res => {
      const { statusCode } = res
      const hash = crypto.createHash('sha1')

      if (statusCode !== 200) {
        const err = new Error(`Request Failed.\nStatus Code: ${statusCode}`)

        // consume response data to free up memory
        res.resume()

        reject(err)
      }

      res.on('data', chunk => {
        hash.update(chunk)
      })

      res.on('end', () => {
        resolve(hash.digest('hex'))
      })

      res.on('error', reject)
    })
  })
}

// Object -> Object
async function fixPkgAddMissingSha1(pkg) {
  // local dependency

  if (!pkg.resolved) {
    console.error(
      `yarn2nix: can't find "resolved" field for package ${
        pkg.nameWithVersion
      }, you probably required it using "file:...", this feature is not supported, ignoring`,
    )
    return pkg
  }

  const [url, sha1] = pkg.resolved.split('#', 2)

  if (sha1) {
    return pkg
  }

  // if there is no sha1 in resolved url
  // (this could happen if yarn.lock was generated by older version of yarn)
  // - request it from registry by https and add it to pkg
  const newSha1 = await getSha1(url)

  return {
    ...pkg,
    resolved: `${url}#${newSha1}`,
  }
}

module.exports = fixPkgAddMissingSha1