From a9b41be9482ddbc62e9330d2039da98cc59eab70 Mon Sep 17 00:00:00 2001 From: Sander van der Burg Date: Tue, 26 Jun 2018 22:48:35 +0200 Subject: [PATCH] Remove dumb code that uses nix-hash to convert SHA512 hashes to base32, because Nix now also supports the base64 notation that NPM uses --- lib/sources/HTTPSource.js | 7 +++++- lib/sources/NPMRegistrySource.js | 14 ++++++++++-- lib/sources/Source.js | 37 ++++++-------------------------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/lib/sources/HTTPSource.js b/lib/sources/HTTPSource.js index 8fdaa37..01feb19 100644 --- a/lib/sources/HTTPSource.js +++ b/lib/sources/HTTPSource.js @@ -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); + } }; /** diff --git a/lib/sources/NPMRegistrySource.js b/lib/sources/NPMRegistrySource.js index 9c21acf..b1647e8 100644 --- a/lib/sources/NPMRegistrySource.js +++ b/lib/sources/NPMRegistrySource.js @@ -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 @@ -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); + } }; /** diff --git a/lib/sources/Source.js b/lib/sources/Source.js index aede7ed..f20ded1 100644 --- a/lib/sources/Source.js +++ b/lib/sources/Source.js @@ -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; @@ -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; } };