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 24, 2023
1 parent 0ebc118 commit 2d9bf2e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Package {
* src: {
* type: 'url',
* url: string,
* sha1?: string,
* hash?: string,
* } | {
* type: 'local',
* },
Expand Down Expand Up @@ -200,25 +200,36 @@ class Package {
*/
static create(
pkgInfo,
// @ts-ignore because there is no optionalDependencies in the type
{ version, resolved, dependencies = {}, optionalDependencies = {} }
// @ts-ignore because type lacks: integrity, optionalDependencies
{ version, integrity, resolved, dependencies = {}, optionalDependencies = {} }
) {
const { scope = '', name } = Id.parse(pkgInfo.name);

/** @type {Package['src']} */
let src;
if (resolved && resolved.startsWith('https')) {
const parsedUrl = new URL(resolved);
let hash = integrity;
let sha1;

// prettier-ignore
if (['registry.yarnpkg.com', 'registry.npmjs.org'].includes(parsedUrl.host)) {
if (!hash && ['registry.yarnpkg.com', 'registry.npmjs.org'].includes(parsedUrl.host)) {
const urlSha1 = parsedUrl.hash.slice(1);
if (urlSha1) {
sha1 = `sha1-${urlSha1}`;
}
}

if (hash || sha1) {
src = {
sha1: parsedUrl.hash.slice(1), // cut off the first ('#') character
hash,
sha1,
url: parsedUrl.origin + parsedUrl.pathname,
type: 'url',
};
} else {
src = {
sha1: undefined, // We don't provide sha in order to force to override it manually
hash: undefined, // We don't provide hash in order to force to override it manually
url: parsedUrl.origin + parsedUrl.pathname,
type: 'url',
};
Expand Down Expand Up @@ -306,7 +317,9 @@ class Package {
renderSrc() {
switch (this.src.type) {
case 'url':
const { url, sha1 } = this.src;
const { url, hash } = this.src;
const hashKey = hash ? "hash" : "sha1";
const hashValue = this.src[hashKey];
// 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,13 +352,8 @@ class Package {
: ''
}
url = "${url}";
${
sha1
? `sha1 = "${sha1}"`
: `sha256 = abort ''
Failed to infer \`sha256\` hash of the \`${this.renderKey()}\` package source from
${hashKey} = ${hashValue ? `"${hashValue}"` : `abort ''
Failed to infer hash of the \`${this.renderKey()}\` package source from
\`${url}\`.
Override \`"${this.renderKey()}".src.sha256\` attribute in order to provide this missing piece to Nix.
Expand Down

0 comments on commit 2d9bf2e

Please sign in to comment.