forked from mcnicholls/node-jscrambler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jscrambler.js
362 lines (354 loc) · 10.8 KB
/
jscrambler.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
/**
* A facade to access JScrambler API using JScramblerClient.
* @namespace jScramblerFacade
* @author José Magalhães (magalhas@gmail.com)
* @license MIT <http://opensource.org/licenses/MIT>
*/
'use strict';
var assign = require('lodash.assign');
var config = require('./lib/config');
var defaults = require('lodash.defaults');
var fs = require('fs-extra');
var glob = require('glob');
var JScramblerClient = require('./jscrambler-client');
var JSZip = require('jszip');
var omit = require('lodash.omit');
var path = require('flavored-path');
var Q = require('q');
var size = require('lodash.size');
var temp = require('temp').track();
var util = require('util');
var debug = !!process.env.DEBUG;
exports = module.exports =
/** @lends jScramblerFacade */
{
Client: JScramblerClient,
config: config,
/**
* Downloads code through the API.
* @param {JScramblerClient} client
* @param {String} projectId
* @param {String} [sourceId]
* @returns {Q.promise}
*/
downloadCode: function (client, projectId, sourceId) {
var deferred = Q.defer();
debug && console.log('Downloading code', projectId, sourceId);
this
.pollProject(client, projectId)
.then(function () {
var path = '/code/' + projectId;
if (sourceId) {
if (!/^.*\..*$/.test(sourceId))
throw new Error('Source extension missing');
else path += '/' + sourceId;
} else if (!/^.*\.zip$/.test(projectId))
path += '.zip';
client.get(path, null, function (err, res, body) {
try {
if (err) deferred.reject(err);
else if (res.statusCode >= 400) {
if (Buffer.isBuffer(body)) {
deferred.reject(JSON.parse(body));
} else {
deferred.reject(body);
}
} else {
deferred.resolve(body);
}
} catch (ex) {
deferred.reject(body);
}
});
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
return deferred.promise;
},
/**
* Gets projects info.
* @param {JScramblerClient} client
* @returns {Q.promise}
*/
getInfo: function (client, projectId) {
var deferred = Q.defer();
var path = projectId ? '/code/' + projectId + '.json' : '/code.json';
debug && console.log('Getting info', projectId);
client.get(path, null, function (err, res, body) {
try {
if (err) deferred.reject(err);
else if (res.statusCode >= 400) {
if (Buffer.isBuffer(body)) {
deferred.reject(JSON.parse(body));
} else {
deferred.reject(body);
}
} else {
if (Buffer.isBuffer(body)) {
deferred.resolve(JSON.parse(body));
} else {
deferred.resolve(body);
}
}
} catch (ex) {
deferred.reject(body);
}
});
return deferred.promise;
},
/**
* Poll project until the build finishes.
*/
pollProject: function (client, projectId) {
var deferred = Q.defer();
var isFinished = function () {
debug && console.log('Polling project', projectId);
this
.getInfo(client, projectId)
.then(function (res) {
// Did it finish?
if (res.finished_at) {
if (res.error_id && res.error_id !== '0') {
deferred.reject(res);
} else {
deferred.resolve(res);
}
return;
}
// Try again later...
setTimeout(isFinished, 1000);
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
}.bind(this);
isFinished();
return deferred.promise;
},
/**
* Uploads code through the API.
* @param {JScramblerClient} client
* @param {Object} params
* @returns {Q.promise}
*/
uploadCode: function (client, params) {
var deferred = Q.defer();
params = assign({}, params);
// If there are no params fallback to `cfg`
var rawParams = omit(params, ['files', 'cwd', 'apiVersion', 'port', 'deleteProject']);
if (Object.keys(rawParams).length === 0) {
params = defaults(params, this.config.params);
}
params.files = params.files.slice();
this.zipProject(params.files, params.cwd);
delete params.cwd;
debug && console.log('Uploading code', util.inspect(params));
client.post('/code.json', params, function (err, res, body) {
try {
if (err) deferred.reject(err);
else if (res.statusCode >= 400) {
if (Buffer.isBuffer(body)) {
deferred.reject(JSON.parse(body));
} else {
deferred.reject(body);
}
} else {
if (Buffer.isBuffer(body)) {
deferred.resolve(JSON.parse(body));
} else {
deferred.resolve(body);
}
}
} catch (ex) {
deferred.reject(body);
}
}.bind(this));
return deferred.promise;
},
/**
* Deletes code through the API.
* @param {JScramblerClient} client
* @param {String} projectId
* @returns {Q.promise}
*/
deleteCode: function (client, projectId) {
var deferred = Q.defer();
debug && console.log('Deleting project', projectId);
client.delete('/code/' + projectId + '.zip', null, function (err, res, body) {
try {
if (err) deferred.reject(err);
else if (res.statusCode >= 400) {
if (Buffer.isBuffer(body)) {
deferred.reject(JSON.parse(body));
} else {
deferred.reject(body);
}
}
else {
if (Buffer.isBuffer(body)) {
deferred.resolve(JSON.parse(body));
} else {
deferred.resolve(body);
}
}
} catch (ex) {
deferred.reject(body);
}
});
return deferred.promise;
},
/**
* Common operation sequence intended when using the client. First it
* uploads a project, then it polls the server to download and unzip the
* project into a folder.
* @param {String|Object} configPathOrObject
* @returns {Q.promise}
*/
process: function (configPathOrObject, destCallback) {
var config = typeof configPathOrObject === 'string' ?
require(configPathOrObject) : configPathOrObject;
var accessKey = config.keys.accessKey;
var secretKey = config.keys.secretKey;
var host = config.host;
var port = config.port;
var apiVersion = config.apiVersion;
// Instance a JScrambler client
var client = new this.Client({
accessKey: accessKey,
secretKey: secretKey,
host: host,
port: port,
apiVersion: apiVersion
});
// Check for source files and add them to the parameters
if (!config.filesSrc) {
throw new Error('Source files must be provided.');
}
// Check if output directory was provided
if (!config.filesDest && !destCallback) {
throw new Error('Output directory must be provided.');
}
var filesSrc = [];
for (var i = 0, l = config.filesSrc.length; i < l; ++i) {
if (typeof config.filesSrc[i] === 'string') {
filesSrc = filesSrc.concat(glob.sync(config.filesSrc[i], {dot: true}));
} else {
filesSrc.push(config.filesSrc[i]);
}
}
// Prepare object to post
var params = config.params || {};
params.files = filesSrc;
var self = this;
var projectId;
return this
.uploadCode(client, params)
.then(function (res) {
projectId = res.id;
return self.downloadCode(client, res.id);
})
.then(function (res) {
return self.unzipProject(res, config.filesDest || destCallback);
})
.then(function () {
if (config.deleteProject) {
return self.deleteCode(client, projectId);
}
});
},
/**
* It zips all files inside the passed parameter into a single zip file. It
* accepts an optional `cwd` parameter.
*/
zipProject: function (files, cwd) {
debug && console.log('Zipping files', util.inspect(files));
// Flag to detect if any file was added to the zip archive
var hasFiles = false;
// Sanitize `cwd`
if (cwd) {
cwd = path.normalize(cwd);
}
// If it's already a zip file
if (files.length === 1 && /^.*\.zip$/.test(files[0])) {
hasFiles = true;
fs.outputFileSync(temp.openSync({suffix: '.zip'}).path, fs.readFileSync(files[0]));
} else {
var zip = new JSZip();
for (var i = 0, l = files.length; i < l; ++i) {
// Sanitise path
if (typeof files[i] === 'string') {
files[i] = path.normalize(files[i]);
if (files[i].indexOf('../') === 0) {
files[i] = path.resolve(files[i]);
}
}
// Bypass unwanted patterns from `files`
if (/.*\.(git|hg)(\/.*|$)/.test(files[i].path || files[i])) {
continue;
}
var buffer, name;
var sPath;
if (cwd && files[i].indexOf && files[i].indexOf(cwd) !== 0) {
sPath = path.join(cwd, files[i]);
} else {
sPath = files[i];
}
// If buffer
if (files[i].contents) {
name = path.relative(files[i].cwd, files[i].path);
buffer = files[i].contents;
}
// Else if it's a path and not a directory
else if (!fs.statSync(sPath).isDirectory()) {
if (cwd && files[i].indexOf && files[i].indexOf(cwd) === 0) {
name = files[i].substring(cwd.length);
} else {
name = files[i];
}
buffer = fs.readFileSync(sPath);
}
// Else if it's a directory path
else {
zip.folder(sPath);
}
if (name) {
hasFiles = true;
zip.file(name, buffer);
}
}
if (hasFiles) {
var tempFile = temp.openSync({suffix: '.zip'});
fs.outputFileSync(tempFile.path, zip.generate({type: 'nodebuffer'}), {encoding: 'base64'});
files[0] = tempFile.path;
files.length = 1;
} else {
throw new Error('No source files found. If you intend to send a whole directory sufix your path with "**" (e.g. ./my-directory/**)');
}
}
},
/**
* It unzips a zip file to the given destination.
*/
unzipProject: function (zipFile, dest) {
var zip = new JSZip(zipFile);
var _size = size(zip.files);
for (var file in zip.files) {
if (!zip.files[file].options.dir) {
var buffer = zip.file(file).asNodeBuffer();
if (typeof dest === 'function') {
dest(buffer, file);
} else if (dest) {
var lastDestChar = dest[dest.length - 1];
var destPath;
if (_size === 1 && lastDestChar !== '/' && lastDestChar !== '\\') {
destPath = dest;
} else {
destPath = path.join(dest, file);
}
fs.outputFileSync(destPath, buffer);
}
}
}
}
};