-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAkamaiJSLib.js
371 lines (320 loc) · 9.3 KB
/
AkamaiJSLib.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* Licensed under the Apache Licence 2.0
*
* Copyright 2016 Nicholas Ball at Incorpleo Ltd
*
* @author Nicholas Ball (incorpleo.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://github.com/nicholasball/AkamaiNativeJSLib
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Akamai namespace
//var _akamai = _akamai || {};
// the class itself
var _akamai = Object.create(null, {
config: {
writable: true,
value: {
key: null,
keyName: null,
host: null,
ssl: false,
verbose: false
}
}
});
/**
* Updates the config object
*
* @param {Object} conf
* @returns {_akamai}
*/
_akamai.setConfig = function (conf) {
this.config = this.deepmerge(this.config, conf);
return this;
};
/**
* Generates a random number
*
* @returns {String}
*/
_akamai.getUniqueId = function () {
var str = '';
for (var i = 0, r; i < 6; i++) {
if ((i & 0x03) === 0) {
r = Math.random() * 0x100000000;
}
str += r >>> ((i & 0x03) << 3) & 0xff;
}
return str; //+ process.pid; TODO: BROWSER UNIQUE?
};
/**
* Returns the config object
*
* @returns {Object}
*/
_akamai.getConfig = function () {
return this.config;
};
/**
* Returns a set of headers for the authentication process
*
* @param {String} path
* @param {Object} queryObj
* @returns {Object}
*/
_akamai.getHeaders = function (path, queryObj) {
var authData, authSign, query;
query = querystring.stringify(this.deepmerge({version: 1, action: 'du', format: 'xml'}, queryObj || {}));
authData = [
5, '0.0.0.0', '0.0.0.0', parseInt(Date.now() / 1000, 10), this.getUniqueId(),
this.getConfig().keyName
].join(', ');
authSign = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, this.getConfig().key)
.update([authData + path.replace(/\/$/, ''), 'x-akamai-acs-action:' + query, null].join("\n"))
.finalize()
.toString(CryptoJS.enc.Base64);
return {
'X-Akamai-ACS-Action': query,
'X-Akamai-ACS-Auth-Data': authData,
'X-Akamai-ACS-Auth-Sign': authSign
};
};
/**
* Adds http or https to the host
*
* @param {String} path
* @returns {String}
*/
_akamai.getUri = function (path) {
var host = ['http', this.getConfig().ssl ? 's' : '', '://', this.getConfig().host].join('');
return [host, path.replace(/(^\/|\/$)/g, '')].join('/');
};
/**
* Converts a xml string to an object
*
* @param {String} data
* @param {Function} cb
*/
_akamai.getObjectFromXml = function (data, cb) {
// Create x2js instance with default config
var x2js = new X2JS();
var jsonObj = x2js.xml_str2json(data);
if (!jsonObj) {
var errMsg = 'Unable to parse the response text';
if (this.getConfig().verbose && data) {
errMsg += '. Data: ' + data;
}
return cb(new Error(errMsg));
}
cb(null, {result: jsonObj});
};
/**
* Returns a request object for streaming TODO: Rename and change function
*
* @param {String} path
* @param {Object} params
* @param {Function} cb
* @returns {request}
*/
_akamai.getRequestObject = function (path, params, cb) {
var self = this,
callback = function () {},
options = this.deepmerge(
{url: this.getUri(path), headers: this.getHeaders(path, params.headers)},
params.request || {}
);
// Set the request method - PUT,POST,GET,HEAD,OPTIONS
var request_method = options.method || "GET";
var xmlhttp = this.createRequest(request_method, options.url);
if (!xmlhttp) {
var errMsg = 'Unable to create CORS request';
return cb(new Error(errMsg));
}
// Set the generated request headers for Akamai auth
for (var header in options.headers) {
xmlhttp.setRequestHeader(header, options.headers[header]);
}
if (typeof(cb) === 'function') {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
var body = xmlhttp.responseText;
// wrong response code
if (xmlhttp.status >= 300) {
var errMsg = 'The server sent us the ' + xmlhttp.status + ' code';
if (self.getConfig().verbose && body) {
errMsg += '. Response: ' + body;
}
return cb(new Error(errMsg));
}
if (!body.match(/^<\?xml\s+/)) {
return cb(null, {status: xmlhttp.status});
}
self.getObjectFromXml(body, cb);
}
}
}
xmlhttp.setRequestHeader("Accept", "text/xml");
if (options.method == "put" && params.file) {
// Make the call to Akamai with file data
xmlhttp.send(params.file.data);
} else {
// Make the call to Akamai
xmlhttp.send(params);
}
};
/** Custom helpers **/
_akamai.fileExists = function (path, cb) {
return this.stat(path, function (err, data) {
if (err && err.message.indexOf('404 code') !== -1) {
return cb(null, false);
}
if (data && data.stat && data.stat.file) {
return cb(null, true);
}
return cb(err);
});
};
_akamai.createRequest = function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, false); // change to true for async
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
};
_akamai.deepmerge = function(target, src) {
var array = Array.isArray(src);
var dst = array && [] || {};
if (array) {
target = target || [];
dst = dst.concat(target);
src.forEach(function(e, i) {
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object') {
dst[i] = deepmerge(target[i], e);
} else {
if (target.indexOf(e) === -1) {
dst.push(e);
}
}
});
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
dst[key] = target[key];
})
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
if (!target[key]) {
dst[key] = src[key];
} else {
dst[key] = deepmerge(target[key], src[key]);
}
}
});
}
return dst;
}
/** Api functions **/
// TODO: PIPES WONT WORK IN JS > CHANGE
/*_akamai.upload = function (stream, path, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'upload', 'upload-type': 'binary'}
};
stream.pipe(this.getRequestObject(path, options, cb));
return this;
};*/
// TODO: PIPES WONT WORK IN JS > CHANGE
/*_akamai.download = function (path, stream, cb) {
this.getRequestObject(path, {headers: {action: 'download'}}, cb).pipe(stream);
return this;
};*/
_akamai.upload = function (content, path, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'upload', 'upload-type': 'binary'},
file: {data: content}
};
this.getRequestObject(path, options, cb);
return this;
};
_akamai.download = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'download'}}, cb);
return this;
};
_akamai.stat = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'stat'}}, cb);
return this;
};
_akamai.du = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'du'}}, cb);
return this;
};
_akamai.dir = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'dir'}}, cb);
return this;
};
_akamai.delete = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'delete'}}, cb);
return this;
};
_akamai.mkdir = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'mkdir'}}, cb);
return this;
};
_akamai.rmdir = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'rmdir'}}, cb);
return this;
};
_akamai.rename = function (pathFrom, pathTo, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'rename', destination: pathTo}
};
this.getRequestObject(pathFrom, options, cb);
return this;
};
_akamai.symlink = function (pathFileTo, pathFileFrom, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'symlink', target: pathFileTo}
};
this.getRequestObject(pathFileFrom, options, cb);
return this;
};
_akamai.mtime = function (path, date, cb) {
if (!(date instanceof Date)) {
return cb(new TypeError('The date has to be an instance of Date'));
}
var options = {
request: {method: 'put'},
headers: {action: 'mtime', mtime: parseInt(date.getTime() / 1000, 10)}
};
this.getRequestObject(path, options, cb);
return this;
};