Skip to content

Commit

Permalink
Remove dumb code that uses nix-hash to convert SHA512 hashes to base3…
Browse files Browse the repository at this point in the history
…2, because Nix now also supports the base64 notation that NPM uses
  • Loading branch information
svanderburg committed Jun 26, 2018
1 parent 475247f commit a9b41be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
7 changes: 6 additions & 1 deletion lib/sources/HTTPSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ HTTPSource.prototype.convertFromLockedDependency = function(dependencyObj, callb
};

this.url = dependencyObj.version;
this.convertIntegrityStringToNixHash(dependencyObj.integrity, callback);
try {
this.convertIntegrityStringToNixHash(dependencyObj.integrity);
callback();
} catch(err) {
callback(err);
}
};

/**
Expand Down
14 changes: 12 additions & 2 deletions lib/sources/NPMRegistrySource.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ NPMRegistrySource.prototype.fetch = function(callback) {
// Determine the output hash. If the package provides an integrity string, use it to compose a hash. Otherwise fall back to sha1

if(self.config.dist.integrity !== undefined) {
self.convertIntegrityStringToNixHash(self.config.dist.integrity, callback);
try {
self.convertIntegrityStringToNixHash(self.config.dist.integrity);
callback();
} catch(err) {
callback(err);
}
} else {
self.hashType = "sha1";
self.sha1 = self.config.dist.shasum; // SHA1 hashes are in hexadecimal notation which we can just adopt verbatim
Expand All @@ -144,7 +149,12 @@ NPMRegistrySource.prototype.convertFromLockedDependency = function(dependencyObj
}
};
this.identifier = this.dependencyName + "-" + dependencyObj.version;
this.convertIntegrityStringToNixHash(dependencyObj.integrity, callback);
try {
this.convertIntegrityStringToNixHash(dependencyObj.integrity);
callback();
} catch(err) {
callback(err);
}
};

/**
Expand Down
37 changes: 6 additions & 31 deletions lib/sources/Source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var url = require('url');
var child_process = require('child_process');
var semver = require('semver');
var nijs = require('nijs');
var inherit = require('nijs/lib/ast/util/inherit.js').inherit;
Expand Down Expand Up @@ -103,41 +102,17 @@ Source.prototype.convertFromLockedDependency = function(dependencyObj, callback)
*
* @method
* @param {String} integrity NPM integrity string
* @param {function{String}) callback Callback that gets invoked when the work is done. On error, the first parameter is set to the error message.
*/
Source.prototype.convertIntegrityStringToNixHash = function(integrity, callback) {
var self = this;

Source.prototype.convertIntegrityStringToNixHash = function(integrity) {
if(integrity.substr(0, 5) === "sha1-") {
var hash = base64js.toByteArray(integrity.substring(5));
self.hashType = "sha1";
self.sha1 = new Buffer(hash).toString('hex');
callback();
this.hashType = "sha1";
this.sha1 = new Buffer(hash).toString('hex');
} else if(integrity.substr(0, 7) === "sha512-") {
var hash = base64js.toByteArray(integrity.substring(7));
var sha512base16 = new Buffer(hash).toString('hex');
self.hashType = "sha512";
self.sha512 = "";

/* Execute nix-hash to convert hexadecimal notation to Nix's base 32 notation */
var nixHash = child_process.spawn("nix-hash", [ "--type", "sha512", "--to-base32", sha512base16 ]);

nixHash.stdout.on("data", function(data) {
self.sha512 += data;
});
nixHash.stderr.on("data", function(data) {
process.stderr.write(data);
});
nixHash.on("close", function(code) {
if(code == 0) {
self.sha512 = self.sha512.substring(0, self.sha512.length - 1);
callback();
} else {
callback("nix-hash exited with status: "+code);
}
});
this.hashType = "sha512";
this.sha512 = integrity.substring(7);
} else {
callback("Unknown integrity string: "+integrity);
throw "Unknown integrity string: "+integrity;
}
};

Expand Down

0 comments on commit a9b41be

Please sign in to comment.