Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ deploy:
secure: VTrqcB3d0y7jQWb3JuLO2TZTczArGnkOzJsik0vBVv17VB/nZyNkPlGfv1zdIIe5x3hMRwP2V/ual+MMEldLhMnGPI8359XYx6D/NAv+P8MmjwbsVbwo8hRG0ctXlDzHI9ni07zh6UaGsh/XiMEtm1sp1ZSzv8LMjNRetOEvP67SicqGvxD2mZHur30s8Fn0qTleamJddYVluJpvsCLYetobeGEghq+aS3l5A1zbnNDHsV54doyfBRjxMO05Awc8md84i4pZTbAGYFpGRxxkmCW6cyUzgWQnIlKUWSypDslwNRJOCzVIZwaXj8uyrnXCk/Bp0eZiooU5CiMjbLMoF2QMxd94vDyIxju/pYnq+yhMsTTdEHm2ZPZCJxdyMbqc0HscvouPi2va6MYXq3XRtR80kDvlAIWYWMJ8pPuSnlSRFUrk5AtQ6MygJeJxyANc+b45uyHQ1PtfBZd3TLm+PNhEZVFEoe2CAvK6IVaNoac7rMLurM/g/vtjrwbCDFBXvMMEUKuhpiVTyuig0V8lRN8IyEhytFnbMmsdFj0D+/B479fpZYOUAq2lXoJ9CxVn/qaZDYTx8DD7e/mP1xm9xJRcwCPB+RuQqDPkqGdRjXcVhkxzVSCQUHR9fOg2LyI1WHOVoc1gfaYt2kSJZ5seKmvATrp5u++yKPIdvkAR1AU=
on:
tags: true
repo: barracksiot/javascript-client
repo: barracksiot/javascript-client

notifications:
slack:
rooms:
secure: "ctLNliKum/SL5FhQ5PvrNkf67/fq6f6ud0V7XyZLBw5JHhn+ro8FoTY+IhZz3qh6F0AqcO0xItzBFEsY66D6gd3x+1QzYgdPvBkB42MDmE6abFrLy8LBrRpin1UEm1rtw9gQenwm+xMi8eGzytX8cSoZWR689h5mcYoN6tpWh8rxYny2XuY8BOHbLuTOJbbHpvDkAJqaZTgfhxlwhfIMKrCVvmenrvBRXuNP/mq/xxYo2Go2+H72d2IKOd1gOv7GsALhLOd3UAbhXeoK3pOUxw4jQCMYR7PjlT+LXjt4MVqElce998tbf+FAPTw9QVJc6y9jLXcto5jRBpeEdx/Wey457lBemaga375rzz3EM0gvSjwB+EhHZlq9nlhXB4iUVrZ3TiMRX6ZG6o3JoZXh7CTq2z8Mc9e0GqQ7/V21jHIuMxeUtCCTAjlRH+/5tlkwPGpEQ73UYDGdK2qjc9AWgkZZ08/ur5oEY4Mw6p5B2GcxPobgfVH/bp8ldhBrBJM0A4er7hIXimckRORvFZNg+jRTCNQYPLpyRSaIiJWGfGXaiitcaYO8z9lv835QpDbSxM3rEbGZOh+cDOHaoK7TUnw9kvai3mkgT6xkyKfj4ewC/3r0TpMvHlYpI3eRr0vkVkTLLGZedB7jOuYve31cHFDrlNSEDSw+Xwkcj1ukS8w="
on_success: change
on_failure: always
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var customClientData = {
}
};

barracks.checkUpdate(packages, customClientData).then(function (packagesInfo) {
barracks.getDevicePackages(packages, customClientData).then(function (packagesInfo) {
packagesInfo.available.forEach(function (packageInfo) {
// Do something with the newly available packages
});
Expand All @@ -84,7 +84,7 @@ barracks.checkUpdate(packages, customClientData).then(function (packagesInfo) {
});
```

The ```checkUpdate``` response is always as follow :
The ```getDevicePackages``` response is always as follow :

```js
{
Expand All @@ -94,8 +94,9 @@ The ```checkUpdate``` response is always as follow :
reference: "abc.edf",
version: "0.0.1",
url: "https://app.barracks.io/path/to/package/version/",
size: 42,
md5: "deadbeefbadc0ffee",
size: 42,
filename: 'aFile.sh',
download: function (filePath) {} // Function to download package
}
],
Expand All @@ -105,8 +106,9 @@ The ```checkUpdate``` response is always as follow :
reference: "abc.edf",
version: "0.0.1",
url: "https://app.barracks.io/path/to/package/version/",
size: 42,
md5: "deadbeefbadc0ffee",
size: 42,
filename: 'aFile.sh',
download: function (filePath) {} // Function to download package
}
],
Expand All @@ -128,7 +130,10 @@ The ```checkUpdate``` response is always as follow :

### Download a package

Once you have the response from checkUpdate, you'll be able to download file for all packages that are available for the device (packages that are in the ```available```, and ```changed``` lists of the response).
Once you have the response from getDevicePackages, you'll be able to download file for all packages that are available for the device (packages that are in the ```available```, and ```changed``` lists of the response).

The ```filePath``` argument of the download function is optionnal. The default value will be as follow:
```<random-uuid>_<original-filename>```

```js
var packages = [
Expand All @@ -142,7 +147,7 @@ var packages = [
}
];

barracks.checkUpdate(packages, customClientData).then(function (packagesInfo) {
barracks.getDevicePackages(packages, customClientData).then(function (packagesInfo) {
var downloadAvailablePackagesPromise = Promise.all(
packagesInfo.available.map(function (packageInfo) {
return packageInfo.download('/tmp/' + package.filename); // Return a Promise
Expand Down Expand Up @@ -180,12 +185,13 @@ All errors returned by the SDK follow the same object format:

Error type can be one of the the following:

* `REQUEST_FAILED`, is returned by both `Barracks.checkUpdate()` and `Barracks.checkUpdateAndDownload()` methods if the check update request fails. The error object also contains one additional property `requestError` that is the `Error` object returned by the [request](https://www.npmjs.com/package/request) library.
* `UNEXPECTED_SERVER_RESPONSE`, is returned by both `Barracks.checkUpdate()` and `Barracks.checkUpdateAndDownload()` methods if the HTTP response code is not `200` (a new update is available) or `204` (no update available).
* `DOWNLOAD_FAILED`, is returned by both `Update.download()` and `Barracks.checkUpdateAndDownload()` methods if the download of an update package fails.
* `DELETE_FILE_FAILED`, is returned by both `Update.download()` and `Barracks.checkUpdateAndDownload()` methods if the SDK fail to delete an update package that did not pass the MD5 checksum verification.
* `CHECKSUM_VERIFICATION_FAILED`, is returned by both `Update.download()` and `Barracks.checkUpdateAndDownload()` methods if the MD5 checksum verification of the update package downloaded fails.
* `MD5_HASH_CREATION_FAILED`, is returned by both `Update.download()` and `Barracks.checkUpdateAndDownload()` methods if the SDK is not able to generate the MD5 checksum of the update package downloaded.
* `MISSING_MANDATORY_ARGUMENT`, is returned by both `Barracks.getDevicePackages()` and `Package.download()`. It indicate that one or more of the mandatory arguments are missing.
* `REQUEST_FAILED`, is returned by `Barracks.getDevicePackages()` method if the getDevicePackage request fails. The error object also contains one additional property `requestError` that is the `Error` object returned by the [request](https://www.npmjs.com/package/request) library.
* `UNEXPECTED_SERVER_RESPONSE`, is returned by `Barracks.getDevicePackages()` method if the HTTP response code is not `200`.
* `DOWNLOAD_FAILED`, is returned by `Package.download()` method if the download of a package fails.
* `DELETE_FILE_FAILED`, is returned by `Package.download()` method if the SDK fail to delete a package that did not pass the MD5 checksum verification.
* `CHECKSUM_VERIFICATION_FAILED`, is returned by `Package.download()` method if the MD5 checksum verification of the package downloaded fails.
* `MD5_HASH_CREATION_FAILED`, is returned by `Package.download()` method if the SDK is not able to generate the MD5 checksum of the package downloaded.

## Docs & Community

Expand Down
9 changes: 7 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ if (!barracksApiKey) {
}

var unitId = 'SDK-example-unit';
var customClientData = {
type: 'alpha',
extra: {
app2: true
}
};

var packages = [
{
Expand All @@ -43,7 +49,6 @@ var packages = [
var barracks = new Barracks({
baseURL: barracksBaseUrl,
apiKey: barracksApiKey,
unitId: unitId,
allowSelfSigned: (isSelfSigned ? (isSelfSigned === '1') : false)
});

Expand Down Expand Up @@ -91,7 +96,7 @@ function handleUnavailablePackages(packages) {

function waitAndDisplayUpdate() {
setTimeout(function () {
barracks.checkUpdate(packages, { test: 'coucou' }).then(function (response) {
barracks.getDevicePackages(unitId, packages, customClientData).then(function (response) {
handleAvailablePackages(response.available);
handleChangedPackages(response.changed);
handleUnchangedPackages(response.unchanged);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"dependencies": {
"es6-promise": "4.0.5",
"md5-file": "3.1.1",
"request": "2.74.0"
"request": "2.74.0",
"uuid": "^3.0.1"
},
"engines": {
"node": ">=0.10 <7.0"
Expand Down
59 changes: 38 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
'use strict';

var ERROR_REQUEST_FAILED = 'REQUEST_FAILED';
var ERROR_DOWNLOAD_FAILED = 'DOWNLOAD_FAILED';
var ERROR_UNEXPECTED_SERVER_RESPONSE = 'UNEXPECTED_SERVER_RESPONSE';
var ERROR_REQUEST_FAILED = 'REQUEST_FAILED';
var ERROR_DOWNLOAD_FAILED = 'DOWNLOAD_FAILED';
var ERROR_UNEXPECTED_SERVER_RESPONSE = 'UNEXPECTED_SERVER_RESPONSE';
var ERROR_MISSING_MANDATORY_ARGUMENT = 'MISSING_MANDATORY_ARGUMENT';

var DEFAULT_BARRACKS_BASE_URL = 'https://app.barracks.io';
var CHECK_UPDATE_ENDPOINT = '/api/device/resolve';
var DEFAULT_BARRACKS_BASE_URL = 'https://app.barracks.io';
var GET_DEVICE_PACKAGES_ENDPOINT = '/api/device/resolve';

require('es6-promise').polyfill();
var responseBuilder = require('./responseBuilder');
var fs = require('fs');
var request = require('request');
var fileHelper = require('./fileHelper');
var uuid = require('uuid/v1');

function Barracks(options) {
this.options = {
baseURL: options.baseURL || DEFAULT_BARRACKS_BASE_URL,
apiKey: options.apiKey,
unitId: options.unitId
apiKey: options.apiKey
};

if (options.allowSelfSigned && options.allowSelfSigned === true) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
}

Barracks.prototype.checkUpdate = function (packages, customClientData) {
Barracks.prototype.getDevicePackages = function(unitId, packages, customClientData) {
var that = this;
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
if (!unitId || !packages) {
reject({
type: ERROR_MISSING_MANDATORY_ARGUMENT,
message: 'missing or empty unitId or packages arguments'
});
}

var requestOptions = {
url: that.options.baseURL + CHECK_UPDATE_ENDPOINT,
url: that.options.baseURL + GET_DEVICE_PACKAGES_ENDPOINT,
method: 'POST',
headers: {
'Authorization': that.options.apiKey,
'Content-type': 'application/json'
},
body: JSON.stringify({
unitId: that.options.unitId,
unitId: unitId,
customClientData: customClientData,
components: packages
packages: packages
})
};

request(requestOptions, function (error, response, body) {
request(requestOptions, function(error, response, body) {
if (error) {
reject({
type: ERROR_REQUEST_FAILED,
Expand All @@ -61,9 +69,18 @@ Barracks.prototype.checkUpdate = function (packages, customClientData) {
});
};

Barracks.prototype.downloadPackage = function (packageInfo, filePath) {
Barracks.prototype.downloadPackage = function(packageInfo, filePath) {
var that = this;
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
if (!packageInfo) {
reject({
type: ERROR_MISSING_MANDATORY_ARGUMENT,
message: 'missing or empty packageInfo argument'
});
}

filePath = filePath || uuid() + '_' + packageInfo.filename;

var downloadParams = {
url: packageInfo.url,
method: 'GET',
Expand All @@ -73,25 +90,25 @@ Barracks.prototype.downloadPackage = function (packageInfo, filePath) {
};

var fileStream = fs.createWriteStream(filePath);
request(downloadParams).on('response', function (response) {
request(downloadParams).on('response', function(response) {
if (response.statusCode != 200) {
fileStream.emit('error', {
type: ERROR_DOWNLOAD_FAILED,
message: 'Server replied with HTTP ' + response.statusCode
});
}
}).pipe(fileStream).on('close', function () {
fileHelper.checkMd5(filePath, packageInfo.md5).then(function () {
}).pipe(fileStream).on('close', function() {
fileHelper.checkMd5(filePath, packageInfo.md5).then(function() {
resolve(filePath);
}).catch(function (err) {
}).catch(function(err) {
fileHelper.deleteFile(filePath, reject);
reject(err);
});
}).on('error', function (err) {
}).on('error', function(err) {
fileHelper.deleteFile(filePath, reject);
reject(err);
});
});
};

module.exports = Barracks;
module.exports = Barracks;
Loading