From 8d1d8826a3c1e01ed93233bfd38061c2c62cf3c9 Mon Sep 17 00:00:00 2001 From: Andrew Marshall Date: Thu, 23 Feb 2023 18:48:17 -0500 Subject: [PATCH] feat: use integrity field from yarn.lock as sha512 for nix derivation Based off https://github.com/canva-public/js2nix/pull/11 --- lib/print.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/print.js b/lib/print.js index 03de42f..37dca3c 100644 --- a/lib/print.js +++ b/lib/print.js @@ -158,6 +158,7 @@ class Package { * type: 'url', * url: string, * sha1?: string, + * sha512?: string, * } | { * type: 'local', * }, @@ -201,7 +202,7 @@ class Package { static create( pkgInfo, // @ts-ignore because there is no optionalDependencies in the type - { version, resolved, dependencies = {}, optionalDependencies = {} } + { version, integrity, resolved, dependencies = {}, optionalDependencies = {} } ) { const { scope = '', name } = Id.parse(pkgInfo.name); @@ -211,11 +212,17 @@ class Package { const parsedUrl = new URL(resolved); // prettier-ignore if (['registry.yarnpkg.com', 'registry.npmjs.org'].includes(parsedUrl.host)) { + const sha1 = parsedUrl.hash.slice(1); + const sha512 = integrity; src = { - sha1: parsedUrl.hash.slice(1), // cut off the first ('#') character url: parsedUrl.origin + parsedUrl.pathname, type: 'url', }; + if (sha512 != '') { + src.sha512 = sha512; + } else if (sha1 != '') { + src.sha1 = sha1; + } } else { src = { sha1: undefined, // We don't provide sha in order to force to override it manually @@ -306,7 +313,9 @@ class Package { renderSrc() { switch (this.src.type) { case 'url': - const { url, sha1 } = this.src; + const { url, sha1, sha512 } = this.src; + const hashKey = sha512 ? "sha512" : "sha1" + const hashValue = sha512 || sha1 // Some of the urls can look like "https://codeload.github.com/xolvio/cucumber-js/tar.gz/cf953cb5b5de30dbcc279f59e4ebff3aa040071c", // i.e. no extention given. That's why Nix unable to recognize the type of archive so we need to have // name specified explicitly to all Nix to infer the archive type. @@ -339,12 +348,7 @@ class Package { : '' } url = "${url}"; - ${ - sha1 - ? `sha1 = "${sha1}"` - : `sha256 = abort '' - - + ${hashKey} = ${`"${hashValue}"` || `abort '' Failed to infer \`sha256\` hash of the \`${this.renderKey()}\` package source from \`${url}\`.