-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.js
128 lines (113 loc) · 3.38 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var crypto = require('crypto');
var https = require('https');
var url = require('url');
const EventEmitter = require('events');
function sha1(data) {
var hasher = crypto.createHash('sha1');
hasher.update(data);
return hasher.digest('hex');
}
function request(options, callback, emitter) {
if (options.body) options.headers['content-length'] = options.body.length;
var retryCount = 0;
function call() {
retryCount++;
var req = https.request(options, function(res) {
var data = '';
res.on('data', a => data += a);
res.on('end', function() {
if (typeof res.headers['content-type'].includes('application/json')) data = JSON.parse(data);
switch (res.statusCode) {
case 429: // TOO MANY REQUESTS
var wait = (retryCount * 30);
console.warn(`B2: ${res.statusCode} ${data.code}, will retry in ${wait}s`);
setTimeout(call, wait * 1000);
break;
case 408: // REQUEST TIMEOUT
case 500: // INTERNAL ERROR
case 503: // SERVICE UNAVAILABLE
var wait = (retryCount * 2);
console.warn(`B2: ${res.statusCode} ${data.code}, will retry in ${wait}s`);
setTimeout(call, wait * 1000);
break;
case 200:
callback(null, data);
break;
default:
console.error('B2:', data);
callback(data.message);
}
});
});
req.on('error', function(err) {
console.error(`B2 problem with response: ${err.message}`);
});
if (options.body) {
var startTime = Date.now(), totalBytes = options.body.length, sentBytes = 0;
function uploadNextChunk(lastChunkSize) {
sentBytes += lastChunkSize;
var chunk = options.body.slice(sentBytes, sentBytes + 524288);
if (emitter) emitter.emit('progress', {
'sentBytes': sentBytes,
'totalBytes': totalBytes,
'elapsedTime': (Date.now() - startTime),
});
if (chunk.length > 0) {
req.write(chunk, () => uploadNextChunk(chunk.length));
} else {
req.end();
}
}
uploadNextChunk(0);
} else {
req.end();
}
};
call();
}
module.exports = {
authorizeAccount(auth, callback) {
request({
'hostname': 'api.backblazeb2.com',
'path': '/b2api/v1/b2_authorize_account',
'method': 'GET',
'auth': auth,
}, callback);
},
listBuckets(settings, callback) {
request({
'hostname': url.parse(settings.apiUrl).hostname,
'path': '/b2api/v1/b2_list_buckets',
'method': 'POST',
'body': JSON.stringify({'accountId': settings.accountId}),
'headers': {'Authorization': settings.authorizationToken},
}, callback);
},
getUploadUrl(settings, bucketId, callback) {
request({
'hostname': url.parse(settings.apiUrl).hostname,
'path': '/b2api/v1/b2_get_upload_url',
'method': 'POST',
'body': JSON.stringify({'bucketId': bucketId}),
'headers': {'Authorization': settings.authorizationToken},
}, callback);
},
uploadFile(uploadUrl, fileBuffer, fileName = '', callback, emitter) {
var urlObj = url.parse(uploadUrl.uploadUrl);
fileName = fileName.split('/').map(encodeURIComponent).join('/'); // URL safe filename
var fileHash = sha1(fileBuffer);
request({
'hostname': urlObj.hostname,
'path': urlObj.path,
'method': 'POST',
'headers': {
'Authorization': uploadUrl.authorizationToken,
'Content-Type': 'b2/x-auto',
'Content-Length': fileBuffer.byteLength,
'X-Bz-File-Name': fileName || fileHash,
'X-Bz-Content-Sha1': fileHash,
},
'body': fileBuffer,
}, callback, emitter);
},
};