Skip to content

Commit

Permalink
- WIP: update for B2 v2 API
Browse files Browse the repository at this point in the history
- refactor to use native Promise class
- remove q dependency
- update syntax to ES6
- update eslint
- update unit tests
  • Loading branch information
cbess committed Jan 13, 2019
1 parent 0184e51 commit d61dc17
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 744 deletions.
13 changes: 13 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"esversion": 6,
"undef": true,
"unused": true,
"globals": {
"console": true,
"require": true,
"process": true,
"module": true,
"global": true,
"exports": true
}
}
4 changes: 2 additions & 2 deletions conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
API_AUTHORIZE__URL: 'https://api.backblaze.com/b2api/v1/b2_authorize_account',
API_VERSION_URL: '/b2api/v1',
API_AUTHORIZE__URL: 'https://api.backblaze.com/b2api/v2/b2_authorize_account',
API_VERSION_URL: '/b2api/v2',
MAX_INFO_HEADERS: 10 // maximum number of custom x-bz-info-* headers
};
20 changes: 9 additions & 11 deletions lib/actions/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.uploadFile = function(b2, args) {
headers: {
Authorization: uploadAuthToken,
'Content-Type': mime || 'b2/x-auto',
'Content-Length': data.byteLength ? data.byteLength : data.length,
'Content-Length': data.byteLength || data.length,
'X-Bz-File-Name': filename,
'X-Bz-Content-Sha1': hash || (data ? sha1(data) : null)
},
Expand Down Expand Up @@ -64,7 +64,7 @@ exports.uploadPart = function(b2, args) {
method: 'POST',
headers: {
Authorization: args.uploadAuthToken,
'Content-Length': args.data.byteLength ? args.data.byteLength : args.data.length,
'Content-Length': args.data.byteLength || args.data.length,
'X-Bz-Part-Number': args.partNumber,
'X-Bz-Content-Sha1': args.data ? sha1(args.data) : null
},
Expand Down Expand Up @@ -107,18 +107,16 @@ exports.listFileNames = function(b2, args) {
var prefix = args.prefix;
var delimiter = args.delimiter;


var options = {
url: getListFilesUrl(b2),
method: 'POST',
headers: utils.getAuthHeaderObjectWithToken(b2),
data: {
bucketId: bucketId,
startFileName: startFileName ? startFileName : '',
maxFileCount: maxFileCount ? maxFileCount : 100,
prefix: prefix ? prefix : '',
delimiter: delimiter ? delimiter : null

bucketId,
startFileName: startFileName || '',
maxFileCount: maxFileCount || 100,
prefix: prefix || '',
delimiter: delimiter || null
}
};
return request.sendRequest(options, utils.getProcessFileSuccess(options));
Expand All @@ -135,8 +133,8 @@ exports.listFileVersions = function(b2, args) {
headers: utils.getAuthHeaderObjectWithToken(b2),
data: {
bucketId: bucketId,
startFileName: startFileName ? startFileName : '',
maxFileCount: maxFileCount ? maxFileCount : 100
startFileName: startFileName || '',
maxFileCount: maxFileCount || 100
}
};
return request.sendRequest(options);
Expand Down
9 changes: 2 additions & 7 deletions lib/b2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var actions = require('./actions');
const actions = require('./actions');

function B2(options) {
this.accountId = options.accountId;
Expand Down Expand Up @@ -128,13 +128,8 @@ B2.prototype.uploadPart = function(args) {
return actions.file.uploadPart(this, args);
};

var q = require('q');
function notYetImplemented() {
var deferred = q.defer();

deferred.reject('Feature not yet implemented');

return deferred.promise;
return Promise.reject('Feature not yet implemented');
}

module.exports = B2;
12 changes: 6 additions & 6 deletions lib/headers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var conf = require('../conf');
const conf = require('../conf');

exports.addInfoHeaders = function(options, info) {
var MAX_INFO_HEADERS = conf.MAX_INFO_HEADERS;
var invalidKeys = [];
const MAX_INFO_HEADERS = conf.MAX_INFO_HEADERS;
let invalidKeys = [];
if (info) {
var keys = Object.keys(info);
let keys = Object.keys(info);

if (keys.length > MAX_INFO_HEADERS) {
throw new Error('Too many info headers: maximum of ' + MAX_INFO_HEADERS + ' allowed');
throw new Error(`Too many info headers: maximum of ${MAX_INFO_HEADERS} allowed`);
}

keys.forEach(addInfoHeader);
Expand All @@ -18,7 +18,7 @@ exports.addInfoHeaders = function(options, info) {
}

function isValidHeader(header) {
return /^[a-z0-9\-]+$/i.test(header);
return /^[a-z0-9-]+$/i.test(header);
}

function addInfoHeader(infoKey) {
Expand Down
20 changes: 18 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/* global Buffer */

// use: let deferred = new Deferred();
/** Backwards compatible Promise.defer() function */
exports.Deferred = function() {
// credit: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#Backwards_and_forwards_compatible_helper
this.resolve = null;
this.reject = null;
this.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this));

Object.freeze(this);
};

exports.getAuthHeaderObject = function(accountId, applicationKey) {
if (!accountId) {
throw new Error('Invalid accountId');
}
if (!applicationKey) {
throw new Error('Invalid applicationKey');
}
var base64 = new Buffer(accountId + ':' + applicationKey).toString('base64');
let base64 = Buffer.from(accountId + ':' + applicationKey).toString('base64');
return {
Authorization: 'Basic ' + base64
};
Expand Down Expand Up @@ -56,7 +72,7 @@ exports.processResponseGeneric = function (deferred) {
if (error) {
return deferred.reject(error);
} else {
var genericResponse = exports.parseJson(body);
let genericResponse = exports.parseJson(body);
deferred.resolve(genericResponse);
}
};
Expand Down
Loading

0 comments on commit d61dc17

Please sign in to comment.