From 30f93beb47e52d012a92320e0d43bb79ae90eee8 Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 11:38:47 +0200 Subject: [PATCH 1/6] Replace request with axios --- package-lock.json | 207 +++++++++++++++++++++++++++------------------- package.json | 3 +- src/client.js | 70 +++++++--------- 3 files changed, 155 insertions(+), 125 deletions(-) diff --git a/package-lock.json b/package-lock.json index 26dd0df..25a0cac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -292,6 +292,14 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.9.1.tgz", "integrity": "sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -334,9 +342,9 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==" }, "buffer": { - "version": "5.4.3", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.3.tgz", - "integrity": "sha512-zvj65TkFeIt3i6aj5bIvJDzjjQQGs4o/sNoezg1F1kYap9Nu2jcUdpwzRSJTHMMzG0H7bZkn4rNQpImhuxWX2A==", + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.5.0.tgz", + "integrity": "sha512-9FTEDjLjwoAkEwyMGDjYJQN2gfRgOKBKRfiglhvibGbpeeU/pQn1bJxQqm32OD/AIeEuHxU9roxXxg34Byp/Ww==", "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" @@ -380,6 +388,11 @@ "responselike": "1.0.2" }, "dependencies": { + "get-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", + "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=" + }, "lowercase-keys": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.0.tgz", @@ -430,17 +443,6 @@ "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" }, - "caw": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/caw/-/caw-2.0.1.tgz", - "integrity": "sha512-Cg8/ZSBEa8ZVY9HspcGUYaK63d/bN7rqS3CYCzEGUxuYv6UlmcjzDUz2fCFFHyTvUW5Pk0I+3hkA3iXlIj6guA==", - "requires": { - "get-proxy": "^2.0.0", - "isurl": "^1.0.0-alpha5", - "tunnel-agent": "^0.6.0", - "url-to-options": "^1.0.1" - } - }, "chai": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", @@ -590,15 +592,6 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, - "config-chain": { - "version": "1.1.12", - "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.12.tgz", - "integrity": "sha512-a1eOIcu8+7lUInge4Rpf/n4Krkf3Dd9lqhljRzII1/Zno/kRtUWnznPO3jOKBmTEktkt3fkxisUcivoj0ebzoA==", - "requires": { - "ini": "^1.3.4", - "proto-list": "~1.2.1" - } - }, "content-disposition": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", @@ -711,9 +704,9 @@ "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=" }, "decompress": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/decompress/-/decompress-4.2.0.tgz", - "integrity": "sha1-eu3YVCflqS2s/lVnSnxQXpbQH50=", + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/decompress/-/decompress-4.2.1.tgz", + "integrity": "sha512-e48kc2IjU+2Zw8cTb6VZcJQ3lgVbS4uuB1TfCHbiZIP/haNXm+SVyhu+87jts5/3ROpd82GSVCoNs/z8l4ZOaQ==", "requires": { "decompress-tar": "^4.0.0", "decompress-tarbz2": "^4.0.0", @@ -725,6 +718,21 @@ "strip-dirs": "^2.0.0" }, "dependencies": { + "make-dir": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", + "integrity": "sha512-2w31R7SJtieJJnQtGc7RVL2StM2vGYVfqUOvUDxH6bC6aJTxPxTF0GnIgCyu7tjockiUWAYQRbxa7vKn34s5sQ==", + "requires": { + "pify": "^3.0.0" + }, + "dependencies": { + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" + } + } + }, "pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", @@ -875,22 +883,28 @@ } }, "download": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/download/-/download-7.1.0.tgz", - "integrity": "sha512-xqnBTVd/E+GxJVrX5/eUJiLYjCGPwMpdL+jGhGU57BvtcA7wwhtHVbXBeUk51kOpW3S7Jn3BQbN9Q1R1Km2qDQ==", + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/download/-/download-8.0.0.tgz", + "integrity": "sha512-ASRY5QhDk7FK+XrQtQyvhpDKanLluEEQtWl/J7Lxuf/b+i8RYh997QeXvL85xitrmRKVlx9c7eTrcRdq2GS4eA==", "requires": { "archive-type": "^4.0.0", - "caw": "^2.0.1", "content-disposition": "^0.5.2", - "decompress": "^4.2.0", + "decompress": "^4.2.1", "ext-name": "^5.0.0", - "file-type": "^8.1.0", - "filenamify": "^2.0.0", - "get-stream": "^3.0.0", + "file-type": "^11.1.0", + "filenamify": "^3.0.0", + "get-stream": "^4.1.0", "got": "^8.3.1", - "make-dir": "^1.2.0", + "make-dir": "^2.1.0", "p-event": "^2.1.0", - "pify": "^3.0.0" + "pify": "^4.0.1" + }, + "dependencies": { + "pify": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" + } } }, "duplexer3": { @@ -1232,9 +1246,9 @@ } }, "file-type": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-8.1.0.tgz", - "integrity": "sha512-qyQ0pzAy78gVoJsmYeNgl8uH8yKhr1lVhW7JbzJmnlRi0I4R2eEDEJZVKG8agpDnLpacwNbDhLNG/LMdxHD2YQ==" + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-11.1.0.tgz", + "integrity": "sha512-rM0UO7Qm9K7TWTtA6AShI/t7H5BPjDeGVDaNyg9BjHAj3PysKy7+8C8D137R88jnR3rFJZQB/tFgydl5sN5m7g==" }, "filename-reserved-regex": { "version": "2.0.0", @@ -1242,9 +1256,9 @@ "integrity": "sha1-q/c9+rc10EVECr/qLZHzieu/oik=" }, "filenamify": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/filenamify/-/filenamify-2.1.0.tgz", - "integrity": "sha512-ICw7NTT6RsDp2rnYKVd8Fu4cr6ITzGy3+u4vUujPkabyaz+03F24NWEX7fs5fp+kBonlaqPH8fAO2NM+SXt/JA==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/filenamify/-/filenamify-3.0.0.tgz", + "integrity": "sha512-5EFZ//MsvJgXjBAFJ+Bh2YaCTRF/VP1YOmGrgt+KJ4SFRLjI87EIdwLLuT6wQX0I4F9W41xutobzczjsOKlI/g==", "requires": { "filename-reserved-regex": "^2.0.0", "strip-outer": "^1.0.0", @@ -1321,6 +1335,29 @@ "integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==", "dev": true }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "foreground-child": { "version": "1.5.6", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-1.5.6.tgz", @@ -1385,14 +1422,6 @@ "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=" }, - "get-proxy": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/get-proxy/-/get-proxy-2.1.0.tgz", - "integrity": "sha512-zmZIaQTWnNQb4R4fJUEp/FC51eZsc6EkErspy3xtIYStaq8EB/hDIWipxsal+E8rz0qD7f2sL/NA9Xee4RInJw==", - "requires": { - "npm-conf": "^1.1.0" - } - }, "get-stdin": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz", @@ -1400,9 +1429,12 @@ "dev": true }, "get-stream": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", - "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=" + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "requires": { + "pump": "^3.0.0" + } }, "getpass": { "version": "0.1.7", @@ -1461,6 +1493,13 @@ "timed-out": "^4.0.1", "url-parse-lax": "^3.0.0", "url-to-options": "^1.0.1" + }, + "dependencies": { + "get-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz", + "integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=" + } } }, "graceful-fs": { @@ -1610,11 +1649,6 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, - "ini": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" - }, "inquirer": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-7.1.0.tgz", @@ -2081,11 +2115,19 @@ } }, "make-dir": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", - "integrity": "sha512-2w31R7SJtieJJnQtGc7RVL2StM2vGYVfqUOvUDxH6bC6aJTxPxTF0GnIgCyu7tjockiUWAYQRbxa7vKn34s5sQ==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", + "integrity": "sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==", "requires": { - "pify": "^3.0.0" + "pify": "^4.0.1", + "semver": "^5.6.0" + }, + "dependencies": { + "pify": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" + } } }, "merge-source-map": { @@ -2104,9 +2146,9 @@ } }, "mime-db": { - "version": "1.40.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", - "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" + "version": "1.43.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz", + "integrity": "sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ==" }, "mime-types": { "version": "2.1.26", @@ -2324,15 +2366,6 @@ } } }, - "npm-conf": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/npm-conf/-/npm-conf-1.1.3.tgz", - "integrity": "sha512-Yic4bZHJOt9RCFbRP3GgpqhScOY4HH3V2P8yBj6CeYq118Qr+BLXqT2JvpJ00mryLESpgOxf5XlFv4ZjXxLScw==", - "requires": { - "config-chain": "^1.1.11", - "pify": "^3.0.0" - } - }, "nyc": { "version": "14.1.1", "resolved": "https://registry.npmjs.org/nyc/-/nyc-14.1.1.tgz", @@ -2674,11 +2707,6 @@ "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", "dev": true }, - "proto-list": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", - "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=" - }, "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", @@ -2689,6 +2717,15 @@ "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" }, + "pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -2729,9 +2766,9 @@ } }, "readable-stream": { - "version": "2.3.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", - "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", "requires": { "core-util-is": "~1.0.0", "inherits": "~2.0.3", @@ -3321,9 +3358,9 @@ "dev": true }, "unbzip2-stream": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.3.3.tgz", - "integrity": "sha512-fUlAF7U9Ah1Q6EieQ4x4zLNejrRvDWUYmxXUpN3uziFYCHapjWFaCAnreY9bGgxzaMCFAPPpYNng57CypwJVhg==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.4.0.tgz", + "integrity": "sha512-kVx7CDAsdBSWVf404Mw7oI9i09w5/mTT/Ruk+RWa64PLYKvsAucLLFHvQtnvjeADM4ZizxrvG5SHnF4Te4T2Cg==", "requires": { "buffer": "^5.2.1", "through": "^2.3.8" diff --git a/package.json b/package.json index 975e290..f669581 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,11 @@ }, "homepage": "https://github.com/ubports/system-image-node-module#readme", "dependencies": { + "axios": "^0.19.2", "chai": "^4.2.0", "checksum": "^0.1.1", "coveralls": "^3.0.11", - "download": "^7.1.0", + "download": "^8.0.0", "mkdirp": "^0.5.4", "mocha": "^6.2.3", "nyc": "^14.1.1", diff --git a/src/client.js b/src/client.js index b0ba3b6..ff28881 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,9 @@ "use strict"; /* +* Copyright (C) 2017-2020 UBports Foundation * Copyright (C) 2017-2018 Marius Gripsgard + * Copyright (C) 2017-2020 Jan Sprinz * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +19,7 @@ * along with this program. If not, see . */ -const http = require("request"); +const axios = require("axios"); const os = require("os"); const fs = require("fs"); const path = require("path"); @@ -253,23 +255,18 @@ class Client { const _this = this; return new Promise(function(resolve, reject) { var now = time(); - if (_this.channelsIndexCache && _this.channelsIndexCache.expire > now) - return resolve(_this.channelsIndexCache.data); - http.get( - { - url: _this.host + "channels.json", - json: true - }, - (err, res, bod) => { - if (err || res.statusCode !== 200) { - reject(err); - return; - } - _this.channelsIndexCache.data = bod; - _this.channelsIndexCache.expire = time() + _this.cache_time; - resolve(_this.channelsIndexCache.data); - } - ); + if (_this.channelsIndexCache && _this.channelsIndexCache.expire > now) { + resolve(_this.channelsIndexCache.data); + } else { + axios + .get(`${_this.host}channels.json`) + .then(response => { + _this.channelsIndexCache.data = response.data; + _this.channelsIndexCache.expire = time() + _this.cache_time; + resolve(_this.channelsIndexCache.data); + }) + .catch(reject); + } }); } @@ -287,27 +284,22 @@ class Client { _this.deviceIndexCache[device] && _this.deviceIndexCache[device][channel] && _this.deviceIndexCache[device][channel].expire > now - ) - return resolve(_this.deviceIndexCache[device][channel].data); - http.get( - { - url: _this.host + channel + "/" + device + "/index.json", - json: true - }, - (err, res, bod) => { - if (err || res.statusCode !== 200) { - reject(err); - return; - } - if (!_this.deviceIndexCache[device]) - _this.deviceIndexCache[device] = {}; - _this.deviceIndexCache[device][channel] = {}; - _this.deviceIndexCache[device][channel].data = bod; - _this.deviceIndexCache[device][channel].expire = - time() + _this.cache_time; - resolve(_this.deviceIndexCache[device][channel].data); - } - ); + ) { + resolve(_this.deviceIndexCache[device][channel].data); + } else { + axios + .get(`${_this.host}${channel}/${device}/index.json`) + .then(response => { + if (!_this.deviceIndexCache[device]) + _this.deviceIndexCache[device] = {}; + _this.deviceIndexCache[device][channel] = {}; + _this.deviceIndexCache[device][channel].data = response.data; + _this.deviceIndexCache[device][channel].expire = + time() + _this.cache_time; + resolve(_this.deviceIndexCache[device][channel].data); + }) + .catch(reject); + } }); } From cf1a0e60e6116421279ce268e5c4621dce13c941 Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 14:50:47 +0200 Subject: [PATCH 2/6] Make tests proper unit-tests and test axios --- .gitignore | 3 + package-lock.json | 6 + package.json | 1 + src/client.js | 2 +- tests/unit-tests/test_client.js | 347 ++++++++++++++++++-------------- tests/unit-tests/test_setup.js | 5 +- 6 files changed, 207 insertions(+), 157 deletions(-) diff --git a/.gitignore b/.gitignore index b42f6a7..2fe9464 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,6 @@ typings/ # ignore test output test + +# ignore test file +test.js \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 25a0cac..d778369 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2286,6 +2286,12 @@ } } }, + "moxios": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/moxios/-/moxios-0.4.0.tgz", + "integrity": "sha1-/A2ixlR31yXKa5Z51YNw7QxS9Ts=", + "dev": true + }, "ms": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", diff --git a/package.json b/package.json index f669581..3c8f2db 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "eslint": "^6.8.0", "eslint-config-prettier": "^6.10.1", "eslint-plugin-prettier": "^3.1.2", + "moxios": "^0.4.0", "prettier": "^1.19.1" } } diff --git a/src/client.js b/src/client.js index ff28881..d215a9b 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,7 @@ "use strict"; /* -* Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2020 UBports Foundation * Copyright (C) 2017-2018 Marius Gripsgard * Copyright (C) 2017-2020 Jan Sprinz * diff --git a/tests/unit-tests/test_client.js b/tests/unit-tests/test_client.js index 95d3386..750e5dd 100644 --- a/tests/unit-tests/test_client.js +++ b/tests/unit-tests/test_client.js @@ -18,9 +18,11 @@ */ const fs = require("fs"); -const request = require("request"); +const axios = require("axios"); +const moxios = require("moxios"); const chai = require("chai"); +const sinon = require("sinon"); var sinonChai = require("sinon-chai"); var expect = chai.expect; chai.use(sinonChai); @@ -32,6 +34,8 @@ const baconLatestVersionJson = require("../test-data/bacon-latest-version.json") const commandfileJson = require("../test-data/commandfile.json"); const filesUrlsJson = require("../test-data/files-urls.json"); +const time = () => Math.floor(new Date() / 1000); + describe("Client module", function() { describe("constructor()", function() { it("should create default client", function() { @@ -101,6 +105,186 @@ describe("Client module", function() { }); }); + describe("getChannelsIndex()", function() { + it("should resolve channels index and store cache", function(done) { + const sic = new SystemImageClient(); + const thenSpy = sinon.spy(); + const catchSpy = sinon.spy(); + expect(sic.channelsIndexCache).to.eql({ expire: 0 }); + sic + .getChannelsIndex() + .then(thenSpy) + .catch(catchSpy); + + moxios.wait(function() { + let request = moxios.requests.mostRecent(); + request + .respondWith({ + status: 200, + response: channelJson + }) + .then(function() { + try { + expect(thenSpy).to.have.been.calledOnceWith(channelJson); + expect(sic.channelsIndexCache.data).to.eql(channelJson); + expect(thenSpy).to.not.have.been.calledTwice; + expect(catchSpy).to.not.have.been.called; + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }); + }); + }); + + it("should use cache", function(done) { + const sic = new SystemImageClient(); + sic.channelsIndexCache = { + expire: time() + 180, + data: "hello :)" + }; + sic + .getChannelsIndex() + .then(r => { + try { + expect(r).to.eql("hello :)"); + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }) + .catch(e => done(`unfulfilled assertions: ${e}`)); + }); + + it("cache should expire", function(done) { + const sic = new SystemImageClient(); + sic.channelsIndexCache = { + expire: time() - 180, + data: "hello :)" + }; + const thenSpy = sinon.spy(); + const catchSpy = sinon.spy(); + sic + .getChannelsIndex() + .then(thenSpy) + .catch(catchSpy); + + moxios.wait(function() { + let request = moxios.requests.mostRecent(); + request + .respondWith({ + status: 200, + response: channelJson + }) + .then(function() { + try { + expect(thenSpy).to.have.been.calledOnceWith(channelJson); + expect(sic.channelsIndexCache.data).to.eql(channelJson); + expect(thenSpy).to.not.have.been.calledTwice; + expect(catchSpy).to.not.have.been.called; + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }); + }); + }); + }); + + describe("getDeviceIndex()", function() { + it("should resolve device index and store cache", function(done) { + const sic = new SystemImageClient(); + const thenSpy = sinon.spy(); + const catchSpy = sinon.spy(); + expect(sic.deviceIndexCache).to.eql({}); + sic + .getDeviceIndex("bacon", "ubports-touch/15.04/stable") + .then(thenSpy) + .catch(catchSpy); + + moxios.wait(function() { + let request = moxios.requests.mostRecent(); + request + .respondWith({ + status: 200, + response: baconIndexJson + }) + .then(function() { + try { + expect(thenSpy).to.have.been.calledOnceWith(baconIndexJson); + expect( + sic.deviceIndexCache["bacon"]["ubports-touch/15.04/stable"].data + ).to.eql(baconIndexJson); + expect(thenSpy).to.not.have.been.calledTwice; + expect(catchSpy).to.not.have.been.called; + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }); + }); + }); + + it("should use cache", function(done) { + const sic = new SystemImageClient(); + sic.deviceIndexCache.bacon = { + "ubports-touch/15.04/stable": { + expire: time() + 180, + data: "hello :)" + } + }; + sic + .getDeviceIndex("bacon", "ubports-touch/15.04/stable") + .then(r => { + try { + expect(r).to.eql("hello :)"); + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }) + .catch(e => done(`unfulfilled assertions: ${e}`)); + }); + + it("cache should expire", function(done) { + const sic = new SystemImageClient(); + sic.deviceIndexCache.bacon = { + "ubports-touch/15.04/stable": { + expire: time() - 180, + data: "hello :)" + } + }; + const thenSpy = sinon.spy(); + const catchSpy = sinon.spy(); + sic + .getDeviceIndex("bacon", "ubports-touch/15.04/stable") + .then(thenSpy) + .catch(catchSpy); + + moxios.wait(function() { + let request = moxios.requests.mostRecent(); + request + .respondWith({ + status: 200, + response: baconIndexJson + }) + .then(function() { + try { + expect(thenSpy).to.have.been.calledOnceWith(baconIndexJson); + expect( + sic.deviceIndexCache["bacon"]["ubports-touch/15.04/stable"].data + ).to.eql(baconIndexJson); + expect(thenSpy).to.not.have.been.calledTwice; + expect(catchSpy).to.not.have.been.called; + done(); + } catch (e) { + done(`unfulfilled assertions: ${e}`); + } + }); + }); + }); + }); + describe("createInstallCommands()", function() { it("should return install commands", function() { const sic = new SystemImageClient(); @@ -169,83 +353,21 @@ describe("Client module", function() { }); describe("getReleaseDate()", function() { - it("should return release date", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(false, { statusCode: 200 }, baconIndexJson); - }); - + it("should resolve release date", function() { const sic = new SystemImageClient(); + sic.getDeviceIndex = sinon.fake.resolves(baconIndexJson); return sic .getReleaseDate("bacon", "ubports-touch/15.04/stable") .then(result => { expect(result).to.eql("Mon Dec 18 08:54:59 UTC 2017"); - expect(requestStub).to.have.been.calledWith({ - url: - "https://system-image.ubports.com/ubports-touch/15.04/stable/bacon/index.json", - json: true - }); - }); - }); - - it("should use cache", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(false, { statusCode: 200 }, baconIndexJson); - }); - - const sic = new SystemImageClient({ cache_time: 0 }); - return sic - .getReleaseDate("bacon", "ubports-touch/15.04/stable") - .then(result1 => { - return sic - .getReleaseDate("bacon", "ubports-touch/15.04/stable") - .then(result2 => { - expect(result1).to.eql("Mon Dec 18 08:54:59 UTC 2017"); - expect(result1).to.eql(result2); - expect(requestStub).to.have.been.calledOnce; - expect(requestStub).to.have.been.calledWith({ - url: - "https://system-image.ubports.com/ubports-touch/15.04/stable/bacon/index.json", - json: true - }); - }); - }); - }); - - it("should return error", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(true, { statusCode: 500 }, baconIndexJson); - }); - - const sic = new SystemImageClient(); - return sic - .getReleaseDate("bacon", "ubports-touch/15.04/stable") - .then(() => {}) - .catch(err => { - expect(err).to.eql(true); - expect(requestStub).to.have.been.calledWith({ - url: - "https://system-image.ubports.com/ubports-touch/15.04/stable/bacon/index.json", - json: true - }); }); }); }); describe("getChannels()", function() { - it("should return channels", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(false, { statusCode: 200 }, channelJson); - }); - + it("should resolve channels", function() { const sic = new SystemImageClient(); + sic.getChannelsIndex = sinon.fake.resolves(channelJson); return sic.getChannels().then(result => { expect(result).to.eql([ "ubports-touch/15.04/devel", @@ -253,115 +375,32 @@ describe("Client module", function() { "ubports-touch/15.04/stable", "ubports-touch/16.04/devel" ]); - expect(requestStub).to.have.been.calledWith({ - url: "https://system-image.ubports.com/channels.json", - json: true - }); }); }); - - it("should return error", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(true, { statusCode: 500 }, channelJson); - }); - - const sic = new SystemImageClient(); - return sic - .getChannels() - .then(() => {}) - .catch(err => { - expect(err).to.eql(true); - expect(requestStub).to.have.been.calledWith({ - url: "https://system-image.ubports.com/channels.json", - json: true - }); - }); - }); }); describe("getDeviceChannels()", function() { - it("should return device channels", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(false, { statusCode: 200 }, channelJson); - }); - + it("should resolve device channels", function() { const sic = new SystemImageClient(); + sic.getChannelsIndex = sinon.fake.resolves(channelJson); return sic.getDeviceChannels("krillin").then(result => { expect(result).to.eql([ "ubports-touch/15.04/devel", "ubports-touch/15.04/rc", "ubports-touch/15.04/stable" ]); - expect(requestStub).to.have.been.calledWith({ - url: "https://system-image.ubports.com/channels.json", - json: true - }); }); }); - - it("should return error", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(true, { statusCode: 500 }, channelJson); - }); - - const sic = new SystemImageClient(); - return sic - .getDeviceChannels("krillin") - .then(() => {}) - .catch(err => { - expect(err).to.eql(true); - expect(requestStub).to.have.been.calledWith({ - url: "https://system-image.ubports.com/channels.json", - json: true - }); - }); - }); }); describe("getLatestVersion()", function() { - it("should return latest version", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(false, { statusCode: 200 }, baconIndexJson); - }); - + it("should resolve latest version", function() { const sic = new SystemImageClient(); + sic.getDeviceIndex = sinon.fake.resolves(baconIndexJson); return sic .getLatestVersion("bacon", "ubports-touch/15.04/devel") .then(result => { expect(result).to.eql(baconLatestVersionJson); - expect(requestStub).to.have.been.calledWith({ - url: - "https://system-image.ubports.com/ubports-touch/15.04/devel/bacon/index.json", - json: true - }); - }); - }); - - it("should return error", function() { - const requestStub = this.sandbox - .stub(request, "get") - .callsFake(function(url, cb) { - cb(true, { statusCode: 500 }, baconIndexJson); - }); - const sic = new SystemImageClient(); - return sic - .getLatestVersion("bacon", "ubports-touch/15.04/devel") - .then(() => {}) - .catch(err => { - expect(err).to.eql(true); - expect(requestStub).to.have.been.calledWith({ - url: - "https://system-image.ubports.com/ubports-touch/15.04/devel/bacon/index.json", - json: true - }); }); }); }); diff --git a/tests/unit-tests/test_setup.js b/tests/unit-tests/test_setup.js index 617fb65..3357d74 100644 --- a/tests/unit-tests/test_setup.js +++ b/tests/unit-tests/test_setup.js @@ -17,11 +17,12 @@ const sinon = require("sinon"); const chai = require("chai"); +const moxios = require("moxios"); beforeEach(function() { - this.sandbox = sinon.createSandbox(); + moxios.install(); }); afterEach(function() { - this.sandbox.restore(); + moxios.install(); }); From 776d546e2c2a4d8806e82d58d93991aa5c70f2b0 Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 14:58:44 +0200 Subject: [PATCH 3/6] Bump version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index d778369..93c7d43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "system-image-node-module", - "version": "1.0.10", + "version": "1.0.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3c8f2db..90e6189 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "system-image-node-module", - "version": "1.0.10", + "version": "1.0.11", "description": "UBports System image client/server module", "main": "./src/module.js", "scripts": { From 30961e647f8b06551b47691e262e2b47807f6f31 Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 16:25:47 +0200 Subject: [PATCH 4/6] Update scripts --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 90e6189..cda41d6 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "scripts": { "test": "mocha ./tests/unit-tests/*", "test-travis": "./node_modules/nyc/bin/nyc.js --reporter=lcov ./node_modules/mocha/bin/_mocha -- -R spec ./tests/unit-tests/*", - "lint": "./node_modules/.bin/eslint tests/unit-tests/*.js src/*.js", - "lint-fix": "./node_modules/.bin/eslint tests/unit-tests/*.js src/*.js --fix" + "lint": "npx eslint tests/unit-tests/*.js src/*.js", + "lint-fix": "npm run lint -- --fix" }, "repository": { "type": "git", From f62b7657ee2e9923244b1fb6aac8aeb593ba1e3f Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 16:37:21 +0200 Subject: [PATCH 5/6] Proper coverage reports --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cda41d6..90a576c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "./src/module.js", "scripts": { "test": "mocha ./tests/unit-tests/*", - "test-travis": "./node_modules/nyc/bin/nyc.js --reporter=lcov ./node_modules/mocha/bin/_mocha -- -R spec ./tests/unit-tests/*", + "test-travis": "./node_modules/nyc/bin/nyc.js --exclude='tests/**/*.js' --reporter=lcov ./node_modules/mocha/bin/_mocha -- -R spec ./tests/unit-tests/*", "lint": "npx eslint tests/unit-tests/*.js src/*.js", "lint-fix": "npm run lint -- --fix" }, From 200821f0b862105826edd85985c59ccfa929df57 Mon Sep 17 00:00:00 2001 From: Jan Sprinz Date: Thu, 2 Apr 2020 16:49:00 +0200 Subject: [PATCH 6/6] Actually proper coverage --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 90a576c..b6f1fa1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "./src/module.js", "scripts": { "test": "mocha ./tests/unit-tests/*", - "test-travis": "./node_modules/nyc/bin/nyc.js --exclude='tests/**/*.js' --reporter=lcov ./node_modules/mocha/bin/_mocha -- -R spec ./tests/unit-tests/*", + "test-travis": "./node_modules/nyc/bin/nyc.js --exclude='**/test_*.js' --reporter=lcov ./node_modules/mocha/bin/_mocha -- -R spec ./tests/unit-tests/*", "lint": "npx eslint tests/unit-tests/*.js src/*.js", "lint-fix": "npm run lint -- --fix" },