Skip to content

Commit d3b7158

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

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

lib/print.js

Lines changed: 20 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,16 @@ 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+
let hashKey
318+
let hashValue
319+
if (sha512 != '') {
320+
hashKey = "sha512"
321+
hashValue = sha512
322+
} else {
323+
hashKey = "sha1"
324+
hashValue = sha1
325+
}
310326
// Some of the urls can look like "https://codeload.github.com/xolvio/cucumber-js/tar.gz/cf953cb5b5de30dbcc279f59e4ebff3aa040071c",
311327
// i.e. no extention given. That's why Nix unable to recognize the type of archive so we need to have
312328
// name specified explicitly to all Nix to infer the archive type.
@@ -339,12 +355,7 @@ class Package {
339355
: ''
340356
}
341357
url = "${url}";
342-
${
343-
sha1
344-
? `sha1 = "${sha1}"`
345-
: `sha256 = abort ''
346-
347-
358+
${hashKey} = ${hashValue ? `"${hashValue}"` : `abort ''
348359
Failed to infer \`sha256\` hash of the \`${this.renderKey()}\` package source from
349360
\`${url}\`.
350361

0 commit comments

Comments
 (0)