Skip to content

Commit 1fa6a5e

Browse files
committed
feat: use integrity field from yarn.lock as sha512 for nix derivation
Based off canva-public#11
1 parent 0ebc118 commit 1fa6a5e

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/print.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class Package {
158158
* type: 'url',
159159
* url: string,
160160
* sha1?: string,
161+
* sha512?: string,
161162
* } | {
162163
* type: 'local',
163164
* },
@@ -201,7 +202,7 @@ class Package {
201202
static create(
202203
pkgInfo,
203204
// @ts-ignore because there is no optionalDependencies in the type
204-
{ version, resolved, dependencies = {}, optionalDependencies = {} }
205+
{ version, integrity, resolved, dependencies = {}, optionalDependencies = {} }
205206
) {
206207
const { scope = '', name } = Id.parse(pkgInfo.name);
207208

@@ -211,11 +212,17 @@ class Package {
211212
const parsedUrl = new URL(resolved);
212213
// prettier-ignore
213214
if (['registry.yarnpkg.com', 'registry.npmjs.org'].includes(parsedUrl.host)) {
215+
const sha1 = parsedUrl.hash.slice(1);
216+
const sha512 = integrity;
214217
src = {
215-
sha1: parsedUrl.hash.slice(1), // cut off the first ('#') character
216218
url: parsedUrl.origin + parsedUrl.pathname,
217219
type: 'url',
218220
};
221+
if (sha512 != '') {
222+
src.sha512 = sha512;
223+
} else if (sha1 != '') {
224+
src.sha1 = sha1;
225+
}
219226
} else {
220227
src = {
221228
sha1: undefined, // We don't provide sha in order to force to override it manually
@@ -306,7 +313,9 @@ class Package {
306313
renderSrc() {
307314
switch (this.src.type) {
308315
case 'url':
309-
const { url, sha1 } = this.src;
316+
const { url, sha1, sha512 } = this.src;
317+
const hashKey = sha512 ? "sha512" : "sha1"
318+
const hashValue = sha512 || sha1
310319
// Some of the urls can look like "https://codeload.github.com/xolvio/cucumber-js/tar.gz/cf953cb5b5de30dbcc279f59e4ebff3aa040071c",
311320
// i.e. no extention given. That's why Nix unable to recognize the type of archive so we need to have
312321
// name specified explicitly to all Nix to infer the archive type.
@@ -339,12 +348,7 @@ class Package {
339348
: ''
340349
}
341350
url = "${url}";
342-
${
343-
sha1
344-
? `sha1 = "${sha1}"`
345-
: `sha256 = abort ''
346-
347-
351+
${hashKey} = ${hashValue || `abort ''
348352
Failed to infer \`sha256\` hash of the \`${this.renderKey()}\` package source from
349353
\`${url}\`.
350354

0 commit comments

Comments
 (0)