Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Improve coverage and code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoTheThird committed Mar 6, 2018
1 parent e7b29dd commit d2f249b
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ typings/


# End of https://www.gitignore.io/api/node


# ignore test output
test
66 changes: 42 additions & 24 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ const os = require("os");
const fs = require("fs");
const path = require("path");
const mkdirp = require('mkdirp');
const common = require("./common.js")

const time = () => Math.floor(new Date() / 1000);

const startCommands = "format system\n\
load_keyring image-master.tar.xz image-master.tar.xz.asc\n\
load_keyring image-signing.tar.xz image-signing.tar.xz.asc\n\
mount system"
const endCommands = "\nunmount system\n"
mount system";
const endCommands = "\nunmount system\n";
const DEFAULT_HOST = "https://system-image.ubports.com/";
const DEFAULT_CACHE_TIME = 180; // 3 minutes
const downloadPath = "./test";
const ubuntuCommandFile = "ubuntu_command";
const ubuntuPushDir = "/cache/recovery/"
const gpg = ["image-signing.tar.xz", "image-signing.tar.xz.asc", "image-master.tar.xz", "image-master.tar.xz.asc"]
const ubuntuPushDir = "/cache/recovery/";
const gpg = ["image-signing.tar.xz", "image-signing.tar.xz.asc", "image-master.tar.xz", "image-master.tar.xz.asc"];

class Client {
constructor(options) {
Expand All @@ -47,32 +48,45 @@ class Client {
this.channelsIndex = {};
this.channelsIndexCache = 0;

// accept options
if (options) {
if (options.host)
this.host = options.host
if (options.port)
this.port = options.port
if (options.path)
this.path = options.path
if (options.cache_time)
if (options.host) {
// validate URL
if (options.host.match(/https?:\/\/(www\.)?[-a-z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-z0-9@:%_\+.~#?&//=]*)/i)) {
// ensure https
if (!options.allow_insecure && options.host.includes("http://")) {
throw new Error("Insecure URL! Call with allow_insecure to ignore.");
}
// ensure trailing slash
this.host = options.host + (options.host.slice(-1) != "/" ? "/" : "");
} else {
throw new Error("Host is not a valid URL!");
}
}
if (options.path) {
this.path = options.path;
}
if (options.cache_time) {
this.cache_time = options.cache_time;
}
}
}

// Install commands
// QUESTION Should this be public? Maybe call it from createInstallCommandsFile
createInstallCommands(files, installerCheck, wipe, enable) {
var cmd = startCommands;
if (wipe === true) cmd += "\nformat data"
if (wipe === true) cmd += "\nformat data";
if (files.constructor !== Array)
return false;
files.forEach((file) => {
cmd += "\nupdate " + path.basename(file.path) + " " + path.basename(file.signature);
})
});
if (enable) {
if (enable.constructor === Array) {
enable.forEach((en) => {
cmd += "\nenable " + en;
})
});
}
}
cmd += endCommands;
Expand All @@ -84,12 +98,13 @@ class Client {
if (!fs.existsSync(downloadPath + "/commandfile/")) {
mkdirp.sync(downloadPath + "/commandfile/");
}
var file = downloadPath + "/commandfile/" + ubuntuCommandFile + device + getRandomInt(1000, 9999);
var file = downloadPath + "/commandfile/" + ubuntuCommandFile + device + common.getRandomInt(1000, 9999);
fs.writeFileSync(file, cmds);
return file;
}

// HTTP functions
// QUESTION Should this be public?
getChannelsIndex() {
const _this = this;
return new Promise(function(resolve, reject) {
Expand All @@ -111,12 +126,13 @@ class Client {
});
}

// QUESTION Should this be public?
getDeviceIndex(device, channel) {
var _this = this;
return new Promise(function(resolve, reject) {
var now=time();
if (_this.deviceIndexCache > now)
return resolve()
return resolve();
http.get({
url: _this.host + channel + "/" + device + "/index.json",
json: true
Expand Down Expand Up @@ -144,7 +160,6 @@ class Client {
});
}


getDeviceChannels(device) {
return this.getChannelsIndex().then((channels) => {
var deviceChannels = [];
Expand All @@ -159,6 +174,7 @@ class Client {
});
}

// FIXME: Some people might perfer to get the latest *version* instead
getLatestVesion(device, channel) {
return this.getDeviceIndex(device, channel).then((index) => {
//TODO optimize with searching in reverse, but foreach is safer
Expand All @@ -168,7 +184,7 @@ class Client {
if (img.type === "full" && (!latest || latest.version < img.version)) {
latest = img;
}
})
});
return latest;
});
}
Expand All @@ -179,8 +195,8 @@ class Client {
gpgUrls.push({
url: this.host + "/gpg/" + g,
path: downloadPath + "gpg"
})
})
});
});
return gpgUrls;
}

Expand All @@ -191,15 +207,16 @@ class Client {
url: this.host + file.path,
path: downloadPath + "pool",
checksum: file.checksum
})
});
ret.push({
url: this.host + file.signature,
path: downloadPath + "pool"
})
})
});
});
return ret;
}

// QUESTION Is this still needed?
getFileBasenameArray(urls) {
var files = [];
urls.forEach((url) => {
Expand All @@ -208,6 +225,7 @@ class Client {
return files;
}

// QUESTION Is this still needed?
getFilePathArray(urls) {
var files = [];
urls.forEach((url) => {
Expand All @@ -228,4 +246,4 @@ class Client {
}
}

module.exports = Client
module.exports = Client;
2 changes: 2 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ function getRandomInt(min, max) {
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}

module.exports = { getRandomInt: getRandomInt };
1 change: 1 addition & 0 deletions tests/test-data/commandfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"format system\nload_keyring image-master.tar.xz image-master.tar.xz.asc\nload_keyring image-signing.tar.xz image-signing.tar.xz.asc\nmount system\nformat data\nupdate ubports-e5cf0885f061c9615931cf863a43f65d8590aa71027315ad71999252ce118868.tar.xz ubports-e5cf0885f061c9615931cf863a43f65d8590aa71027315ad71999252ce118868.tar.xz.asc\nupdate device-f6863fe2f1f8b47cd90f5721af05ccb0ab4d41f2e1cc318075458646eeda28ee.tar.xz device-f6863fe2f1f8b47cd90f5721af05ccb0ab4d41f2e1cc318075458646eeda28ee.tar.xz.asc\nupdate keyring-4c4e7ef380ebcfa2c31084efa199138e93bfed8fc58aa3eb06bdf75a78af9b57.tar.xz keyring-4c4e7ef380ebcfa2c31084efa199138e93bfed8fc58aa3eb06bdf75a78af9b57.tar.xz.asc\nupdate version-261.tar.xz version-261.tar.xz.asc\nenable 1\nenable 2\nenable 3\nunmount system\n\ninstaller_check"
Loading

0 comments on commit d2f249b

Please sign in to comment.