Skip to content

Commit

Permalink
feat: use integrity field from yarn.lock as sha512 for nix derivation
Browse files Browse the repository at this point in the history
Based off canva-public#11
  • Loading branch information
andrew-vts committed Feb 23, 2023
1 parent 0ebc118 commit 8d1d882
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Package {
* type: 'url',
* url: string,
* sha1?: string,
* sha512?: string,
* } | {
* type: 'local',
* },
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}\`.
Expand Down

0 comments on commit 8d1d882

Please sign in to comment.