Skip to content

Commit

Permalink
Move hash computing function to the Source prototype and modify HTTPS…
Browse files Browse the repository at this point in the history
…ource and NPM Registry Source to use it
  • Loading branch information
svanderburg committed Feb 27, 2018
1 parent 22d0c19 commit bf87412
Show file tree
Hide file tree
Showing 15 changed files with 743 additions and 632 deletions.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file has been generated by node2nix 1.5.1. Do not edit!
# This file has been generated by node2nix 1.5.2. Do not edit!

{pkgs ? import <nixpkgs> {
inherit system;
Expand Down
22 changes: 8 additions & 14 deletions lib/sources/HTTPSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var tar = require('tar');
var nijs = require('nijs');
var Source = require('./Source.js').Source;
var inherit = require('nijs/lib/ast/util/inherit.js').inherit;
var base64js = require('base64-js');

/**
* Constructs a new HTTPSource instance.
Expand Down Expand Up @@ -91,7 +90,7 @@ HTTPSource.prototype.fetch = function(callback) {
entry.on("end", function() {
self.config = JSON.parse(packageJSON);

if(self.hash !== undefined) { // Only consider the work done if the hash has been computed as well
if(self.sha256 !== undefined) { // Only consider the work done if the hash has been computed as well
finish();
}
});
Expand All @@ -114,7 +113,7 @@ HTTPSource.prototype.fetch = function(callback) {
});
res.on("end", function() {
self.hashType = "sha256";
self.hash = computeHash.digest('hex');
self.sha256 = computeHash.digest('hex');

if(self.config !== undefined) { // Only consider the work done if the package configuration has been extracted as well
finish();
Expand All @@ -140,15 +139,7 @@ HTTPSource.prototype.convertFromLockedDependency = function(dependencyObj, callb
};

this.url = dependencyObj.version;

if(dependencyObj.integrity.substr(0, 5) === "sha1-") {
var hash = base64js.toByteArray(dependencyObj.integrity.substring(5));
this.hashType = "sha1";
this.hash = new Buffer(hash).toString('hex');
callback();
} else {
callback("Unknown integrity string: "+dependencyObj.integrity);
}
this.convertIntegrityStringToNixHash(dependencyObj.integrity, callback);
};

/**
Expand All @@ -164,10 +155,13 @@ HTTPSource.prototype.toNixAST = function() {

switch(this.hashType) {
case "sha256":
paramExpr["sha256"] = this.hash;
paramExpr["sha256"] = this.sha256;
break;
case "sha512":
paramExpr["sha512"] = this.sha512;
break;
case "sha1":
paramExpr["sha1"] = this.hash;
paramExpr["sha1"] = this.sha1;
break;
default:
throw "Unknown hash type: "+this.hashType;
Expand Down
46 changes: 0 additions & 46 deletions lib/sources/NPMRegistrySource.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var path = require('path');
var child_process = require('child_process');
var slasp = require('slasp');
var semver = require('semver');
var npmconf = require('npmconf');
var nijs = require('nijs');
var base64js = require('base64-js');
var RegClient = require('npm-registry-client');
var Source = require('./Source.js').Source;
var inherit = require('nijs/lib/ast/util/inherit.js').inherit;
Expand Down Expand Up @@ -53,50 +51,6 @@ function NPMRegistrySource(baseDir, dependencyName, versionSpec, registryURL) {
/* NPMRegistrySource inherits from Source */
inherit(Source, NPMRegistrySource);

/**
* Converts NPM's integrity strings that have the following format:
* <algorithm>-<base64 hash> to a hash representation that Nix understands.
*
* @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.
*/
NPMRegistrySource.prototype.convertIntegrityStringToNixHash = function(integrity, callback) {
var self = this;

if(integrity.substr(0, 5) === "sha1-") {
var hash = base64js.toByteArray(integrity.substring(5));
self.hashType = "sha1";
self.sha1 = new Buffer(hash).toString('hex');
callback();
} 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);
}
});
} else {
callback("Unknown integrity string: "+integrity);
}
};

/**
* @see Source#fetch
*/
Expand Down
46 changes: 46 additions & 0 deletions lib/sources/Source.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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;
var base64js = require('base64-js');

/**
* Creates a new source instance. This function should never be used directly,
Expand Down Expand Up @@ -93,6 +95,50 @@ Source.prototype.convertFromLockedDependency = function(dependencyObj, callback)
callback("convertFromLockedDependency() is not implemented, please use a prototype that inherits from Source");
};

/**
* Converts NPM's integrity strings that have the following format:
* <algorithm>-<base64 hash> to a hash representation that Nix understands.
*
* @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;

if(integrity.substr(0, 5) === "sha1-") {
var hash = base64js.toByteArray(integrity.substring(5));
self.hashType = "sha1";
self.sha1 = new Buffer(hash).toString('hex');
callback();
} 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);
}
});
} else {
callback("Unknown integrity string: "+integrity);
}
};

/**
* @see NixASTNode#toNixAST
*/
Expand Down
Loading

0 comments on commit bf87412

Please sign in to comment.