UNPKG

69.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7require('babel-polyfill');
8
9var _glob = require('glob');
10
11var _glob2 = _interopRequireDefault(_glob);
12
13var _path = require('path');
14
15var _path2 = _interopRequireDefault(_path);
16
17var _axios = require('axios');
18
19var _axios2 = _interopRequireDefault(_axios);
20
21var _lodash = require('lodash.defaults');
22
23var _lodash2 = _interopRequireDefault(_lodash);
24
25var _fs = require('fs');
26
27var _fs2 = _interopRequireDefault(_fs);
28
29var _config2 = require('./config');
30
31var _config3 = _interopRequireDefault(_config2);
32
33var _generateSignedParams = require('./generate-signed-params');
34
35var _generateSignedParams2 = _interopRequireDefault(_generateSignedParams);
36
37var _client = require('./client');
38
39var _client2 = _interopRequireDefault(_client);
40
41var _mutations = require('./mutations');
42
43var mutations = _interopRequireWildcard(_mutations);
44
45var _queries = require('./queries');
46
47var queries = _interopRequireWildcard(_queries);
48
49var _zip = require('./zip');
50
51var _introspection = require('./introspection');
52
53var introspection = _interopRequireWildcard(_introspection);
54
55var _getProtectionDefaultFragments = require('./get-protection-default-fragments');
56
57var _getProtectionDefaultFragments2 = _interopRequireDefault(_getProtectionDefaultFragments);
58
59function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
60
61function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
62
63function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
64
65function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /* eslint-disable no-console */
66
67
68var intoObjectType = introspection.intoObjectType;
69
70
71var debug = !!process.env.DEBUG;
72var APP_URL = 'https://app.jscrambler.com';
73
74function errorHandler(res) {
75 if (res.errors && res.errors.length) {
76 res.errors.forEach(function (error) {
77 throw new Error('Error: ' + error.message);
78 });
79 }
80
81 if (res.data && res.data.errors) {
82 res.data.errors.forEach(function (e) {
83 return console.error(e.message);
84 });
85 throw new Error('GraphQL Query Error');
86 }
87
88 if (res.message) {
89 throw new Error('Error: ' + res.message);
90 }
91
92 return res;
93}
94
95function printSourcesErrors(errors) {
96 console.error('Application sources errors:');
97 console.error(JSON.stringify(errors, null, 2));
98 console.error('');
99}
100
101function normalizeParameters(parameters) {
102 var result = void 0;
103
104 if (!Array.isArray(parameters)) {
105 result = [];
106 Object.keys(parameters).forEach(function (name) {
107 result.push({
108 name: name,
109 options: parameters[name]
110 });
111 });
112 } else {
113 result = parameters;
114 }
115
116 return result;
117}
118
119function buildFinalConfig(configPathOrObject) {
120 var _config = typeof configPathOrObject === 'string' ? require(configPathOrObject) : configPathOrObject;
121
122 return (0, _lodash2.default)(_config, _config3.default);
123}
124
125exports.default = {
126 Client: _client2.default,
127 config: _config3.default,
128 generateSignedParams: _generateSignedParams2.default,
129 /**
130 * Remove and Add application sources
131 * @param {object} client
132 * @param {string} applicationId
133 * @param {{
134 * sources: Array.<{filename: string, content: string}>,
135 * filesSrc: Array.<string>,
136 * cwd: string,
137 * appProfiling: ?object
138 * }} opts
139 * @returns {Promise<{extension: string, filename: string, content: *}>}
140 */
141 updateApplicationSources: function () {
142 var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(client, applicationId, _ref) {
143 var sources = _ref.sources,
144 filesSrc = _ref.filesSrc,
145 cwd = _ref.cwd,
146 appProfiling = _ref.appProfiling;
147
148 var removeSourceRes, zipped, source, _filesSrc, i, l, content;
149
150 return regeneratorRuntime.wrap(function _callee$(_context) {
151 while (1) {
152 switch (_context.prev = _context.next) {
153 case 0:
154 if (!(sources || filesSrc && filesSrc.length)) {
155 _context.next = 7;
156 break;
157 }
158
159 if (!(appProfiling && appProfiling.data.state === 'READY')) {
160 _context.next = 3;
161 break;
162 }
163
164 throw new Error('You have a finished Profiling for this application so you are NOT ALLOWED to update sources. To override this behavior use *--remove-profiling-data* or *--skip-sources*.');
165
166 case 3:
167 _context.next = 5;
168 return this.removeSourceFromApplication(client, '', applicationId);
169
170 case 5:
171 removeSourceRes = _context.sent;
172
173
174 errorHandler(removeSourceRes);
175
176 case 7:
177 zipped = void 0;
178 source = void 0;
179
180 if (!(filesSrc && filesSrc.length)) {
181 _context.next = 18;
182 break;
183 }
184
185 _filesSrc = [];
186
187 for (i = 0, l = filesSrc.length; i < l; i += 1) {
188 if (typeof filesSrc[i] === 'string') {
189 // TODO Replace `glob.sync` with async version
190 _filesSrc = _filesSrc.concat(_glob2.default.sync(filesSrc[i], {
191 dot: true
192 }));
193 } else {
194 _filesSrc.push(filesSrc[i]);
195 }
196 }
197
198 if (debug) {
199 console.log('Creating zip from source files');
200 }
201
202 _context.next = 15;
203 return (0, _zip.zip)(_filesSrc, cwd);
204
205 case 15:
206 zipped = _context.sent;
207 _context.next = 23;
208 break;
209
210 case 18:
211 if (!sources) {
212 _context.next = 23;
213 break;
214 }
215
216 if (debug) {
217 console.log('Creating zip from sources');
218 }
219
220 _context.next = 22;
221 return (0, _zip.zipSources)(sources);
222
223 case 22:
224 zipped = _context.sent;
225
226 case 23:
227 if (!zipped) {
228 _context.next = 32;
229 break;
230 }
231
232 content = zipped.generate({
233 type: 'nodebuffer'
234 }).toString('base64');
235
236
237 if (debug) {
238 console.log('Adding sources to application');
239 }
240
241 source = {
242 content: content,
243 filename: 'application.zip',
244 extension: 'zip'
245 };
246
247 _context.t0 = errorHandler;
248 _context.next = 30;
249 return this.addApplicationSource(client, applicationId, source);
250
251 case 30:
252 _context.t1 = _context.sent;
253 (0, _context.t0)(_context.t1);
254
255 case 32:
256 return _context.abrupt('return', source);
257
258 case 33:
259 case 'end':
260 return _context.stop();
261 }
262 }
263 }, _callee, this);
264 }));
265
266 function updateApplicationSources(_x, _x2, _x3) {
267 return _ref2.apply(this, arguments);
268 }
269
270 return updateApplicationSources;
271 }(),
272
273 // This method is a shortcut method that accepts an object with everything needed
274 // for the entire process of requesting an application protection and downloading
275 // that same protection when the same ends.
276 //
277 // `configPathOrObject` can be a path to a JSON or directly an object containing
278 // the following structure:
279 //
280 // ```json
281 // {
282 // "keys": {
283 // "accessKey": "",
284 // "secretKey": ""
285 // },
286 // "applicationId": "",
287 // "filesDest": ""
288 // }
289 // ```
290 //
291 // Also the following optional parameters are accepted:
292 //
293 // ```json
294 // {
295 // "filesSrc": [""],
296 // "params": {},
297 // "cwd": "",
298 // "host": "api.jscrambler.com",
299 // "port": "443",
300 // "basePath": ""
301 // }
302 // ```
303 //
304 // `filesSrc` supports glob patterns, and if it's provided it will replace the
305 // entire application sources.
306 //
307 // `params` if provided will replace all the application transformation parameters.
308 //
309 // `cwd` allows you to set the current working directory to resolve problems with
310 // relative paths with your `filesSrc` is outside the current working directory.
311 //
312 // Finally, `host` and `port` can be overridden if you to engage with a different
313 // endpoint than the default one, useful if you're running an enterprise version of
314 // Jscrambler or if you're provided access to beta features of our product.
315 //
316 protectAndDownload: function () {
317 var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(configPathOrObject, destCallback) {
318 var finalConfig, applicationId, host, port, basePath, protocol, cafile, keys, sources, _finalConfig$stream, stream, cwd, params, applicationTypes, languageSpecifications, sourceMaps, randomizationSeed, areSubscribersOrdered, useRecommendedOrder, _finalConfig$bail, bail, jscramblerVersion, debugMode, proxy, clientId, tolerateMinification, codeHardeningThreshold, useProfilingData, browsers, useAppClassification, profilingDataMode, removeProfilingData, skipSources, inputSymbolTable, accessKey, secretKey, client, filesSrc, filesDest, source, appProfiling, updateData, applicationUpdate, updateApplicationRes, protectionOptions, inputSymbolTableContents, createApplicationProtectionRes, protectionId, protection, sourcesErrors, download;
319
320 return regeneratorRuntime.wrap(function _callee2$(_context2) {
321 while (1) {
322 switch (_context2.prev = _context2.next) {
323 case 0:
324 finalConfig = buildFinalConfig(configPathOrObject);
325 applicationId = finalConfig.applicationId, host = finalConfig.host, port = finalConfig.port, basePath = finalConfig.basePath, protocol = finalConfig.protocol, cafile = finalConfig.cafile, keys = finalConfig.keys, sources = finalConfig.sources, _finalConfig$stream = finalConfig.stream, stream = _finalConfig$stream === undefined ? true : _finalConfig$stream, cwd = finalConfig.cwd, params = finalConfig.params, applicationTypes = finalConfig.applicationTypes, languageSpecifications = finalConfig.languageSpecifications, sourceMaps = finalConfig.sourceMaps, randomizationSeed = finalConfig.randomizationSeed, areSubscribersOrdered = finalConfig.areSubscribersOrdered, useRecommendedOrder = finalConfig.useRecommendedOrder, _finalConfig$bail = finalConfig.bail, bail = _finalConfig$bail === undefined ? true : _finalConfig$bail, jscramblerVersion = finalConfig.jscramblerVersion, debugMode = finalConfig.debugMode, proxy = finalConfig.proxy, clientId = finalConfig.clientId, tolerateMinification = finalConfig.tolerateMinification, codeHardeningThreshold = finalConfig.codeHardeningThreshold, useProfilingData = finalConfig.useProfilingData, browsers = finalConfig.browsers, useAppClassification = finalConfig.useAppClassification, profilingDataMode = finalConfig.profilingDataMode, removeProfilingData = finalConfig.removeProfilingData, skipSources = finalConfig.skipSources, inputSymbolTable = finalConfig.inputSymbolTable;
326 accessKey = keys.accessKey, secretKey = keys.secretKey;
327 client = new this.Client({
328 accessKey: accessKey,
329 secretKey: secretKey,
330 host: host,
331 port: port,
332 basePath: basePath,
333 protocol: protocol,
334 cafile: cafile,
335 jscramblerVersion: jscramblerVersion,
336 proxy: proxy,
337 clientId: clientId
338 });
339 filesSrc = finalConfig.filesSrc;
340 filesDest = finalConfig.filesDest;
341
342
343 if (sources) {
344 filesSrc = undefined;
345 }
346
347 if (destCallback) {
348 filesDest = undefined;
349 }
350
351 if (applicationId) {
352 _context2.next = 10;
353 break;
354 }
355
356 throw new Error('Required *applicationId* not provided');
357
358 case 10:
359 if (!(!filesDest && !destCallback)) {
360 _context2.next = 12;
361 break;
362 }
363
364 throw new Error('Required *filesDest* not provided');
365
366 case 12:
367 source = void 0;
368
369 if (skipSources) {
370 _context2.next = 26;
371 break;
372 }
373
374 _context2.next = 16;
375 return this.getApplicationProfiling(client, applicationId).catch(function (e) {
376 if (![404, 403].includes(e.statusCode)) throw e;
377 });
378
379 case 16:
380 appProfiling = _context2.sent;
381
382 if (!(appProfiling && removeProfilingData)) {
383 _context2.next = 21;
384 break;
385 }
386
387 _context2.next = 20;
388 return this.deleteProfiling(client, appProfiling.data.id);
389
390 case 20:
391 appProfiling.data.state = 'DELETED';
392
393 case 21:
394 _context2.next = 23;
395 return this.updateApplicationSources(client, applicationId, {
396 sources: sources,
397 filesSrc: filesSrc,
398 cwd: cwd,
399 appProfiling: appProfiling
400 });
401
402 case 23:
403 source = _context2.sent;
404 _context2.next = 27;
405 break;
406
407 case 26:
408 console.log('Update source files SKIPPED');
409
410 case 27:
411 updateData = {
412 _id: applicationId,
413 debugMode: !!debugMode,
414 tolerateMinification: tolerateMinification,
415 codeHardeningThreshold: codeHardeningThreshold
416 };
417
418
419 if (params && Object.keys(params).length) {
420 updateData.parameters = normalizeParameters(params);
421 updateData.areSubscribersOrdered = Array.isArray(params);
422 }
423
424 if (typeof areSubscribersOrdered !== 'undefined') {
425 updateData.areSubscribersOrdered = areSubscribersOrdered;
426 }
427
428 if (applicationTypes) {
429 updateData.applicationTypes = applicationTypes;
430 }
431
432 if (typeof useRecommendedOrder !== 'undefined') {
433 updateData.useRecommendedOrder = useRecommendedOrder;
434 }
435
436 if (languageSpecifications) {
437 updateData.languageSpecifications = languageSpecifications;
438 }
439
440 if (typeof sourceMaps !== 'undefined') {
441 updateData.sourceMaps = sourceMaps;
442 }
443
444 if (useProfilingData !== undefined) {
445 updateData.useProfilingData = useProfilingData;
446 }
447 if (profilingDataMode !== undefined) {
448 updateData.profilingDataMode = profilingDataMode;
449 }
450 if (useAppClassification !== undefined) {
451 updateData.useAppClassification = useAppClassification;
452 }
453
454 if (browsers) {
455 updateData.browsers = browsers;
456 }
457
458 if (!(updateData.parameters || updateData.applicationTypes || updateData.languageSpecifications || updateData.browsers || typeof updateData.areSubscribersOrdered !== 'undefined')) {
459 _context2.next = 48;
460 break;
461 }
462
463 if (debug) {
464 console.log('Updating parameters of protection');
465 }
466
467 _context2.next = 42;
468 return intoObjectType(client, updateData, 'Application');
469
470 case 42:
471 applicationUpdate = _context2.sent;
472 _context2.next = 45;
473 return this.updateApplication(client, applicationUpdate);
474
475 case 45:
476 updateApplicationRes = _context2.sent;
477
478 if (debug) {
479 console.log('Finished updating parameters of protection');
480 console.error(updateApplicationRes);
481 }
482 errorHandler(updateApplicationRes);
483
484 case 48:
485
486 if (debug) {
487 console.log('Creating Application Protection');
488 }
489
490 delete updateData._id;
491 protectionOptions = Object.assign({ bail: bail, randomizationSeed: randomizationSeed, tolerateMinification: tolerateMinification, source: source, inputSymbolTable: inputSymbolTable }, updateData);
492
493
494 if (finalConfig.inputSymbolTable) {
495 // Note: we can not use the fs.promises API because some users may not have node 10.
496 // Once node 10 is old enough to be safe to assume that all users will have it, this
497 // should be safe to replace with `await fs.promises.readFile`.
498 inputSymbolTableContents = _fs2.default.readFileSync(finalConfig.inputSymbolTable, 'utf-8');
499
500 protectionOptions.inputSymbolTable = inputSymbolTableContents;
501 }
502
503 _context2.next = 54;
504 return this.createApplicationProtection(client, applicationId, protectionOptions);
505
506 case 54:
507 createApplicationProtectionRes = _context2.sent;
508
509 errorHandler(createApplicationProtectionRes);
510
511 protectionId = createApplicationProtectionRes.data.createApplicationProtection._id;
512 _context2.t0 = this;
513 _context2.t1 = client;
514 _context2.t2 = applicationId;
515 _context2.t3 = protectionId;
516 _context2.next = 63;
517 return (0, _getProtectionDefaultFragments2.default)(client);
518
519 case 63:
520 _context2.t4 = _context2.sent;
521 _context2.next = 66;
522 return _context2.t0.pollProtection.call(_context2.t0, _context2.t1, _context2.t2, _context2.t3, _context2.t4);
523
524 case 66:
525 protection = _context2.sent;
526
527 if (protection.growthWarning) {
528 console.warn('Warning: Your protected application has surpassed a reasonable file growth.\nFor more information on what might have caused this, please see the Protection Report.\nLink: ' + APP_URL + '.');
529 }
530 if (debug) {
531 console.log('Finished protecting');
532 }
533
534 if (protection.deprecations) {
535 protection.deprecations.forEach(function (deprecation) {
536 if (deprecation.type === 'Transformation') {
537 console.warn('Warning: ' + deprecation.type + ' ' + deprecation.entity + ' is no longer maintained. Please consider removing it from your configuration.');
538 } else if (deprecation.type && deprecation.entity) {
539 console.warn('Warning: ' + deprecation.type + ' ' + deprecation.entity + ' is deprecated.');
540 }
541 });
542 }
543
544 sourcesErrors = [];
545
546
547 protection.sources.forEach(function (s) {
548 if (s.errorMessages && s.errorMessages.length > 0) {
549 sourcesErrors.push.apply(sourcesErrors, _toConsumableArray(s.errorMessages.map(function (e) {
550 return Object.assign({
551 filename: s.filename
552 }, e);
553 })));
554 }
555 });
556
557 if (!(protection.state === 'errored')) {
558 _context2.next = 80;
559 break;
560 }
561
562 console.error('Global protection errors:');
563 console.error('- ' + protection.errorMessage);
564 console.error('');
565 if (sourcesErrors.length > 0) {
566 printSourcesErrors(sourcesErrors);
567 }
568 throw new Error('Protection failed. For more information visit: ' + APP_URL + '.');
569
570 case 80:
571 if (!(sourcesErrors.length > 0)) {
572 _context2.next = 87;
573 break;
574 }
575
576 if (!protection.bail) {
577 _context2.next = 86;
578 break;
579 }
580
581 printSourcesErrors(sourcesErrors);
582 throw new Error('Your protection has failed.');
583
584 case 86:
585 sourcesErrors.forEach(function (e) {
586 return console.warn('Non-fatal error: "' + e.message + '" in ' + e.filename);
587 });
588
589 case 87:
590
591 if (debug) {
592 console.log('Downloading protection result');
593 }
594 _context2.next = 90;
595 return this.downloadApplicationProtection(client, protectionId);
596
597 case 90:
598 download = _context2.sent;
599
600
601 errorHandler(download);
602
603 if (debug) {
604 console.log('Unzipping files');
605 }
606
607 (0, _zip.unzip)(download, filesDest || destCallback, stream);
608
609 if (debug) {
610 console.log('Finished unzipping files');
611 }
612
613 console.log(protectionId);
614
615 return _context2.abrupt('return', protectionId);
616
617 case 97:
618 case 'end':
619 return _context2.stop();
620 }
621 }
622 }, _callee2, this);
623 }));
624
625 function protectAndDownload(_x4, _x5) {
626 return _ref3.apply(this, arguments);
627 }
628
629 return protectAndDownload;
630 }(),
631
632 /**
633 * Instrument and download application sources for profiling purposes
634 * @param {object} configPathOrObject
635 * @param {function} [destCallback]
636 * @returns {Promise<string>}
637 */
638 instrumentAndDownload: function () {
639 var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(configPathOrObject, destCallback) {
640 var finalConfig, applicationId, host, port, basePath, protocol, cafile, keys, sources, _finalConfig$stream2, stream, cwd, jscramblerVersion, proxy, skipSources, clientId, accessKey, secretKey, client, filesSrc, filesDest, instrumentation, download;
641
642 return regeneratorRuntime.wrap(function _callee3$(_context3) {
643 while (1) {
644 switch (_context3.prev = _context3.next) {
645 case 0:
646 finalConfig = buildFinalConfig(configPathOrObject);
647 applicationId = finalConfig.applicationId, host = finalConfig.host, port = finalConfig.port, basePath = finalConfig.basePath, protocol = finalConfig.protocol, cafile = finalConfig.cafile, keys = finalConfig.keys, sources = finalConfig.sources, _finalConfig$stream2 = finalConfig.stream, stream = _finalConfig$stream2 === undefined ? true : _finalConfig$stream2, cwd = finalConfig.cwd, jscramblerVersion = finalConfig.jscramblerVersion, proxy = finalConfig.proxy, skipSources = finalConfig.skipSources, clientId = finalConfig.clientId;
648 accessKey = keys.accessKey, secretKey = keys.secretKey;
649 client = new this.Client({
650 accessKey: accessKey,
651 secretKey: secretKey,
652 host: host,
653 port: port,
654 basePath: basePath,
655 protocol: protocol,
656 cafile: cafile,
657 jscramblerVersion: jscramblerVersion,
658 proxy: proxy,
659 clientId: clientId
660 });
661 filesSrc = finalConfig.filesSrc, filesDest = finalConfig.filesDest;
662
663
664 if (sources) {
665 filesSrc = undefined;
666 }
667
668 if (destCallback) {
669 filesDest = undefined;
670 }
671
672 if (applicationId) {
673 _context3.next = 9;
674 break;
675 }
676
677 throw new Error('Required *applicationId* not provided');
678
679 case 9:
680 if (!(!filesDest && !destCallback)) {
681 _context3.next = 11;
682 break;
683 }
684
685 throw new Error('Required *filesDest* not provided');
686
687 case 11:
688 if (skipSources) {
689 _context3.next = 16;
690 break;
691 }
692
693 _context3.next = 14;
694 return this.updateApplicationSources(client, applicationId, {
695 sources: sources,
696 filesSrc: filesSrc,
697 cwd: cwd
698 });
699
700 case 14:
701 _context3.next = 17;
702 break;
703
704 case 16:
705 console.log('Update source files SKIPPED');
706
707 case 17:
708 _context3.next = 19;
709 return this.startInstrumentation(client, applicationId);
710
711 case 19:
712 instrumentation = _context3.sent;
713
714 errorHandler(instrumentation);
715
716 _context3.next = 23;
717 return this.pollInstrumentation(client, instrumentation.data.id);
718
719 case 23:
720 instrumentation = _context3.sent;
721
722 if (debug) {
723 console.log('Finished instrumention with id ' + instrumentation.data.id + '. Downloading...');
724 }
725
726 _context3.next = 27;
727 return this.downloadApplicationInstrumented(client, instrumentation.data.id);
728
729 case 27:
730 download = _context3.sent;
731
732 errorHandler(download);
733
734 if (debug) {
735 console.log('Unzipping files');
736 }
737
738 (0, _zip.unzip)(download, filesDest || destCallback, stream);
739
740 if (debug) {
741 console.log('Finished unzipping files');
742 }
743
744 console.warn('\n WARNING: DO NOT SEND THIS CODE TO PRODUCTION AS IT IS NOT PROTECTED\n ');
745
746 console.log('Application ' + applicationId + ' was instrumented. Bootstrap your instrumented application and run *--start-profiling* command.');
747
748 return _context3.abrupt('return', instrumentation.data.id);
749
750 case 35:
751 case 'end':
752 return _context3.stop();
753 }
754 }
755 }, _callee3, this);
756 }));
757
758 function instrumentAndDownload(_x6, _x7) {
759 return _ref4.apply(this, arguments);
760 }
761
762 return instrumentAndDownload;
763 }(),
764
765
766 /**
767 * Change the profiling run stat.
768 * @param configPathOrObject
769 * @param state
770 * @param label
771 * @param {string} nextStepMessage
772 * @returns {Promise<string>} The previous state
773 */
774 setProfilingState: function () {
775 var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(configPathOrObject, state, label, nextStepMessage) {
776 var finalConfig, keys, host, port, basePath, protocol, cafile, applicationId, proxy, jscramblerVersion, clientId, accessKey, secretKey, client, instrumentation, previousState;
777 return regeneratorRuntime.wrap(function _callee4$(_context4) {
778 while (1) {
779 switch (_context4.prev = _context4.next) {
780 case 0:
781 finalConfig = buildFinalConfig(configPathOrObject);
782 keys = finalConfig.keys, host = finalConfig.host, port = finalConfig.port, basePath = finalConfig.basePath, protocol = finalConfig.protocol, cafile = finalConfig.cafile, applicationId = finalConfig.applicationId, proxy = finalConfig.proxy, jscramblerVersion = finalConfig.jscramblerVersion, clientId = finalConfig.clientId;
783 accessKey = keys.accessKey, secretKey = keys.secretKey;
784 client = new this.Client({
785 accessKey: accessKey,
786 secretKey: secretKey,
787 host: host,
788 port: port,
789 basePath: basePath,
790 protocol: protocol,
791 cafile: cafile,
792 proxy: proxy,
793 jscramblerVersion: jscramblerVersion,
794 clientId: clientId
795 });
796 _context4.next = 6;
797 return client.get('/profiling-run', { applicationId: applicationId }).catch(function (e) {
798 if (e.statusCode !== 404) throw e;
799 });
800
801 case 6:
802 instrumentation = _context4.sent;
803
804 if (instrumentation) {
805 _context4.next = 9;
806 break;
807 }
808
809 throw new Error('There is no active profiling run. Instrument your application first.');
810
811 case 9:
812 previousState = instrumentation.data.state;
813
814 if (!(previousState === state)) {
815 _context4.next = 13;
816 break;
817 }
818
819 console.log('Profiling was already ' + label + ' for application ' + applicationId + '. ' + nextStepMessage);
820 return _context4.abrupt('return');
821
822 case 13:
823 _context4.next = 15;
824 return client.patch('/profiling-run/' + instrumentation.data.id, {
825 state: state
826 });
827
828 case 15:
829
830 console.log('Profiling was ' + label + ' for application ' + applicationId + '. ' + nextStepMessage);
831
832 case 16:
833 case 'end':
834 return _context4.stop();
835 }
836 }
837 }, _callee4, this);
838 }));
839
840 function setProfilingState(_x8, _x9, _x10, _x11) {
841 return _ref5.apply(this, arguments);
842 }
843
844 return setProfilingState;
845 }(),
846 downloadSourceMaps: function () {
847 var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(configs, destCallback) {
848 var keys, host, port, basePath, protocol, cafile, _configs$stream, stream, filesDest, filesSrc, protectionId, jscramblerVersion, proxy, accessKey, secretKey, client, download;
849
850 return regeneratorRuntime.wrap(function _callee5$(_context5) {
851 while (1) {
852 switch (_context5.prev = _context5.next) {
853 case 0:
854 keys = configs.keys, host = configs.host, port = configs.port, basePath = configs.basePath, protocol = configs.protocol, cafile = configs.cafile, _configs$stream = configs.stream, stream = _configs$stream === undefined ? true : _configs$stream, filesDest = configs.filesDest, filesSrc = configs.filesSrc, protectionId = configs.protectionId, jscramblerVersion = configs.jscramblerVersion, proxy = configs.proxy;
855 accessKey = keys.accessKey, secretKey = keys.secretKey;
856 client = new this.Client({
857 accessKey: accessKey,
858 secretKey: secretKey,
859 host: host,
860 port: port,
861 basePath: basePath,
862 protocol: protocol,
863 cafile: cafile,
864 jscramblerVersion: jscramblerVersion,
865 proxy: proxy
866 });
867
868 if (!(!filesDest && !destCallback)) {
869 _context5.next = 5;
870 break;
871 }
872
873 throw new Error('Required *filesDest* not provided');
874
875 case 5:
876 if (protectionId) {
877 _context5.next = 7;
878 break;
879 }
880
881 throw new Error('Required *protectionId* not provided');
882
883 case 7:
884
885 if (filesSrc) {
886 console.warn('[Warning] Ignoring sources supplied. Downloading source maps of given protection');
887 }
888 download = void 0;
889 _context5.prev = 9;
890 _context5.next = 12;
891 return this.downloadSourceMapsRequest(client, protectionId);
892
893 case 12:
894 download = _context5.sent;
895 _context5.next = 18;
896 break;
897
898 case 15:
899 _context5.prev = 15;
900 _context5.t0 = _context5['catch'](9);
901
902 errorHandler(_context5.t0);
903
904 case 18:
905 (0, _zip.unzip)(download, filesDest || destCallback, stream);
906
907 case 19:
908 case 'end':
909 return _context5.stop();
910 }
911 }
912 }, _callee5, this, [[9, 15]]);
913 }));
914
915 function downloadSourceMaps(_x12, _x13) {
916 return _ref6.apply(this, arguments);
917 }
918
919 return downloadSourceMaps;
920 }(),
921 downloadSymbolTable: function () {
922 var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(configs, destCallback) {
923 var keys, host, port, basePath, protocol, cafile, _configs$stream2, stream, filesDest, filesSrc, protectionId, jscramblerVersion, proxy, accessKey, secretKey, client, download;
924
925 return regeneratorRuntime.wrap(function _callee6$(_context6) {
926 while (1) {
927 switch (_context6.prev = _context6.next) {
928 case 0:
929 keys = configs.keys, host = configs.host, port = configs.port, basePath = configs.basePath, protocol = configs.protocol, cafile = configs.cafile, _configs$stream2 = configs.stream, stream = _configs$stream2 === undefined ? true : _configs$stream2, filesDest = configs.filesDest, filesSrc = configs.filesSrc, protectionId = configs.protectionId, jscramblerVersion = configs.jscramblerVersion, proxy = configs.proxy;
930 accessKey = keys.accessKey, secretKey = keys.secretKey;
931 client = new this.Client({
932 accessKey: accessKey,
933 secretKey: secretKey,
934 host: host,
935 port: port,
936 basePath: basePath,
937 protocol: protocol,
938 cafile: cafile,
939 jscramblerVersion: jscramblerVersion,
940 proxy: proxy
941 });
942
943 if (!(!filesDest && !destCallback)) {
944 _context6.next = 5;
945 break;
946 }
947
948 throw new Error('Required *filesDest* not provided');
949
950 case 5:
951 if (protectionId) {
952 _context6.next = 7;
953 break;
954 }
955
956 throw new Error('Required *protectionId* not provided');
957
958 case 7:
959
960 if (filesSrc) {
961 console.warn('[Warning] Ignoring sources supplied. Downloading symbol table of given protection');
962 }
963 download = void 0;
964 _context6.prev = 9;
965 _context6.next = 12;
966 return this.downloadSymbolTableRequest(client, protectionId);
967
968 case 12:
969 download = _context6.sent;
970 _context6.next = 18;
971 break;
972
973 case 15:
974 _context6.prev = 15;
975 _context6.t0 = _context6['catch'](9);
976
977 errorHandler(_context6.t0);
978
979 case 18:
980
981 if (typeof destCallback === 'function') {
982 destCallback(download, filesDest);
983 } else {
984 (0, _zip.outputFileSync)(_path2.default.join(filesDest, protectionId + '_symbolTable.json'), download);
985 }
986
987 case 19:
988 case 'end':
989 return _context6.stop();
990 }
991 }
992 }, _callee6, this, [[9, 15]]);
993 }));
994
995 function downloadSymbolTable(_x14, _x15) {
996 return _ref7.apply(this, arguments);
997 }
998
999 return downloadSymbolTable;
1000 }(),
1001
1002 /**
1003 * Polls a instrumentation every 500ms until the state be equal to
1004 * FINISHED_INSTRUMENTATION, FAILED_INSTRUMENTATION or DELETED
1005 * @param {object} client
1006 * @param {string} instrumentationId
1007 * @returns {Promise<object>}
1008 * @throws {Error} due to errors in instrumentation process or user cancel the operation
1009 */
1010 pollInstrumentation: function () {
1011 var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(client, instrumentationId) {
1012 var _this = this;
1013
1014 var poll;
1015 return regeneratorRuntime.wrap(function _callee8$(_context8) {
1016 while (1) {
1017 switch (_context8.prev = _context8.next) {
1018 case 0:
1019 poll = function () {
1020 var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
1021 var instrumentation;
1022 return regeneratorRuntime.wrap(function _callee7$(_context7) {
1023 while (1) {
1024 switch (_context7.prev = _context7.next) {
1025 case 0:
1026 _context7.next = 2;
1027 return _this.getInstrumentation(client, instrumentationId);
1028
1029 case 2:
1030 instrumentation = _context7.sent;
1031 _context7.t0 = instrumentation.data.state;
1032 _context7.next = _context7.t0 === 'DELETED' ? 6 : _context7.t0 === 'FAILED_INSTRUMENTATION' ? 7 : _context7.t0 === 'FINISHED_INSTRUMENTATION' ? 9 : 10;
1033 break;
1034
1035 case 6:
1036 throw new Error('Protection canceled by user');
1037
1038 case 7:
1039 instrumentation.errors = instrumentation.errors.concat(instrumentation.data.instrumentationErrors.map(function (e) {
1040 return {
1041 message: e.message + ' at ' + e.fileName + ':' + e.lineNumber
1042 };
1043 }));
1044 return _context7.abrupt('return', errorHandler(instrumentation));
1045
1046 case 9:
1047 return _context7.abrupt('return', instrumentation);
1048
1049 case 10:
1050 _context7.next = 12;
1051 return new Promise(function (resolve) {
1052 return setTimeout(resolve, 500);
1053 });
1054
1055 case 12:
1056 return _context7.abrupt('return', poll());
1057
1058 case 13:
1059 case 'end':
1060 return _context7.stop();
1061 }
1062 }
1063 }, _callee7, _this);
1064 }));
1065
1066 return function poll() {
1067 return _ref9.apply(this, arguments);
1068 };
1069 }();
1070
1071 return _context8.abrupt('return', poll());
1072
1073 case 2:
1074 case 'end':
1075 return _context8.stop();
1076 }
1077 }
1078 }, _callee8, this);
1079 }));
1080
1081 function pollInstrumentation(_x16, _x17) {
1082 return _ref8.apply(this, arguments);
1083 }
1084
1085 return pollInstrumentation;
1086 }(),
1087 pollProtection: function () {
1088 var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(client, applicationId, protectionId, fragments) {
1089 var _this2 = this;
1090
1091 var poll;
1092 return regeneratorRuntime.wrap(function _callee10$(_context10) {
1093 while (1) {
1094 switch (_context10.prev = _context10.next) {
1095 case 0:
1096 poll = function () {
1097 var _ref11 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
1098 var applicationProtection, state;
1099 return regeneratorRuntime.wrap(function _callee9$(_context9) {
1100 while (1) {
1101 switch (_context9.prev = _context9.next) {
1102 case 0:
1103 _context9.next = 2;
1104 return _this2.getApplicationProtection(client, applicationId, protectionId, fragments);
1105
1106 case 2:
1107 applicationProtection = _context9.sent;
1108
1109 if (!applicationProtection.errors) {
1110 _context9.next = 8;
1111 break;
1112 }
1113
1114 console.log('Error polling protection', applicationProtection.errors);
1115
1116 throw new Error('Protection failed. For more information visit: ' + APP_URL + '.');
1117
1118 case 8:
1119 state = applicationProtection.data.applicationProtection.state;
1120
1121 if (!(state !== 'finished' && state !== 'errored' && state !== 'canceled')) {
1122 _context9.next = 15;
1123 break;
1124 }
1125
1126 _context9.next = 12;
1127 return new Promise(function (resolve) {
1128 return setTimeout(resolve, 500);
1129 });
1130
1131 case 12:
1132 return _context9.abrupt('return', poll());
1133
1134 case 15:
1135 if (!(state === 'canceled')) {
1136 _context9.next = 19;
1137 break;
1138 }
1139
1140 throw new Error('Protection canceled by user');
1141
1142 case 19:
1143 return _context9.abrupt('return', applicationProtection.data.applicationProtection);
1144
1145 case 20:
1146 case 'end':
1147 return _context9.stop();
1148 }
1149 }
1150 }, _callee9, _this2);
1151 }));
1152
1153 return function poll() {
1154 return _ref11.apply(this, arguments);
1155 };
1156 }();
1157
1158 return _context10.abrupt('return', poll());
1159
1160 case 2:
1161 case 'end':
1162 return _context10.stop();
1163 }
1164 }
1165 }, _callee10, this);
1166 }));
1167
1168 function pollProtection(_x18, _x19, _x20, _x21) {
1169 return _ref10.apply(this, arguments);
1170 }
1171
1172 return pollProtection;
1173 }(),
1174
1175 //
1176 createApplication: function () {
1177 var _ref12 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11(client, data, fragments) {
1178 return regeneratorRuntime.wrap(function _callee11$(_context11) {
1179 while (1) {
1180 switch (_context11.prev = _context11.next) {
1181 case 0:
1182 return _context11.abrupt('return', client.post('/application', mutations.createApplication(data, fragments)));
1183
1184 case 1:
1185 case 'end':
1186 return _context11.stop();
1187 }
1188 }
1189 }, _callee11, this);
1190 }));
1191
1192 function createApplication(_x22, _x23, _x24) {
1193 return _ref12.apply(this, arguments);
1194 }
1195
1196 return createApplication;
1197 }(),
1198
1199 //
1200 duplicateApplication: function () {
1201 var _ref13 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12(client, data, fragments) {
1202 return regeneratorRuntime.wrap(function _callee12$(_context12) {
1203 while (1) {
1204 switch (_context12.prev = _context12.next) {
1205 case 0:
1206 return _context12.abrupt('return', client.post('/application', mutations.duplicateApplication(data, fragments)));
1207
1208 case 1:
1209 case 'end':
1210 return _context12.stop();
1211 }
1212 }
1213 }, _callee12, this);
1214 }));
1215
1216 function duplicateApplication(_x25, _x26, _x27) {
1217 return _ref13.apply(this, arguments);
1218 }
1219
1220 return duplicateApplication;
1221 }(),
1222
1223 //
1224 removeApplication: function () {
1225 var _ref14 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13(client, id) {
1226 return regeneratorRuntime.wrap(function _callee13$(_context13) {
1227 while (1) {
1228 switch (_context13.prev = _context13.next) {
1229 case 0:
1230 return _context13.abrupt('return', client.post('/application', mutations.removeApplication(id)));
1231
1232 case 1:
1233 case 'end':
1234 return _context13.stop();
1235 }
1236 }
1237 }, _callee13, this);
1238 }));
1239
1240 function removeApplication(_x28, _x29) {
1241 return _ref14.apply(this, arguments);
1242 }
1243
1244 return removeApplication;
1245 }(),
1246
1247 //
1248 removeProtection: function () {
1249 var _ref15 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee14(client, id, appId, fragments) {
1250 return regeneratorRuntime.wrap(function _callee14$(_context14) {
1251 while (1) {
1252 switch (_context14.prev = _context14.next) {
1253 case 0:
1254 return _context14.abrupt('return', client.post('/application', mutations.removeProtection(id, appId, fragments)));
1255
1256 case 1:
1257 case 'end':
1258 return _context14.stop();
1259 }
1260 }
1261 }, _callee14, this);
1262 }));
1263
1264 function removeProtection(_x30, _x31, _x32, _x33) {
1265 return _ref15.apply(this, arguments);
1266 }
1267
1268 return removeProtection;
1269 }(),
1270
1271 //
1272 cancelProtection: function () {
1273 var _ref16 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee15(client, id, appId, fragments) {
1274 var mutation;
1275 return regeneratorRuntime.wrap(function _callee15$(_context15) {
1276 while (1) {
1277 switch (_context15.prev = _context15.next) {
1278 case 0:
1279 _context15.next = 2;
1280 return mutations.cancelProtection(id, appId, fragments);
1281
1282 case 2:
1283 mutation = _context15.sent;
1284 return _context15.abrupt('return', client.post('/application', mutation));
1285
1286 case 4:
1287 case 'end':
1288 return _context15.stop();
1289 }
1290 }
1291 }, _callee15, this);
1292 }));
1293
1294 function cancelProtection(_x34, _x35, _x36, _x37) {
1295 return _ref16.apply(this, arguments);
1296 }
1297
1298 return cancelProtection;
1299 }(),
1300
1301 //
1302 updateApplication: function () {
1303 var _ref17 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee16(client, application, fragments) {
1304 var mutation;
1305 return regeneratorRuntime.wrap(function _callee16$(_context16) {
1306 while (1) {
1307 switch (_context16.prev = _context16.next) {
1308 case 0:
1309 _context16.next = 2;
1310 return mutations.updateApplication(application, fragments);
1311
1312 case 2:
1313 mutation = _context16.sent;
1314 return _context16.abrupt('return', client.post('/application', mutation));
1315
1316 case 4:
1317 case 'end':
1318 return _context16.stop();
1319 }
1320 }
1321 }, _callee16, this);
1322 }));
1323
1324 function updateApplication(_x38, _x39, _x40) {
1325 return _ref17.apply(this, arguments);
1326 }
1327
1328 return updateApplication;
1329 }(),
1330
1331 //
1332 unlockApplication: function () {
1333 var _ref18 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17(client, application, fragments) {
1334 var mutation;
1335 return regeneratorRuntime.wrap(function _callee17$(_context17) {
1336 while (1) {
1337 switch (_context17.prev = _context17.next) {
1338 case 0:
1339 _context17.next = 2;
1340 return mutations.unlockApplication(application, fragments);
1341
1342 case 2:
1343 mutation = _context17.sent;
1344 return _context17.abrupt('return', client.post('/application', mutation));
1345
1346 case 4:
1347 case 'end':
1348 return _context17.stop();
1349 }
1350 }
1351 }, _callee17, this);
1352 }));
1353
1354 function unlockApplication(_x41, _x42, _x43) {
1355 return _ref18.apply(this, arguments);
1356 }
1357
1358 return unlockApplication;
1359 }(),
1360
1361 //
1362 getApplication: function () {
1363 var _ref19 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18(client, applicationId, fragments, params) {
1364 var query;
1365 return regeneratorRuntime.wrap(function _callee18$(_context18) {
1366 while (1) {
1367 switch (_context18.prev = _context18.next) {
1368 case 0:
1369 _context18.next = 2;
1370 return queries.getApplication(applicationId, fragments, params);
1371
1372 case 2:
1373 query = _context18.sent;
1374 return _context18.abrupt('return', client.get('/application', query));
1375
1376 case 4:
1377 case 'end':
1378 return _context18.stop();
1379 }
1380 }
1381 }, _callee18, this);
1382 }));
1383
1384 function getApplication(_x44, _x45, _x46, _x47) {
1385 return _ref19.apply(this, arguments);
1386 }
1387
1388 return getApplication;
1389 }(),
1390
1391 //
1392 getApplicationSource: function () {
1393 var _ref20 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19(client, sourceId, fragments, limits) {
1394 var query;
1395 return regeneratorRuntime.wrap(function _callee19$(_context19) {
1396 while (1) {
1397 switch (_context19.prev = _context19.next) {
1398 case 0:
1399 _context19.next = 2;
1400 return queries.getApplicationSource(sourceId, fragments, limits);
1401
1402 case 2:
1403 query = _context19.sent;
1404 return _context19.abrupt('return', client.get('/application', query));
1405
1406 case 4:
1407 case 'end':
1408 return _context19.stop();
1409 }
1410 }
1411 }, _callee19, this);
1412 }));
1413
1414 function getApplicationSource(_x48, _x49, _x50, _x51) {
1415 return _ref20.apply(this, arguments);
1416 }
1417
1418 return getApplicationSource;
1419 }(),
1420
1421 //
1422 getApplicationProtections: function () {
1423 var _ref21 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20(client, applicationId, params, fragments) {
1424 var query;
1425 return regeneratorRuntime.wrap(function _callee20$(_context20) {
1426 while (1) {
1427 switch (_context20.prev = _context20.next) {
1428 case 0:
1429 _context20.next = 2;
1430 return queries.getApplicationProtections(applicationId, params, fragments);
1431
1432 case 2:
1433 query = _context20.sent;
1434 return _context20.abrupt('return', client.get('/application', query));
1435
1436 case 4:
1437 case 'end':
1438 return _context20.stop();
1439 }
1440 }
1441 }, _callee20, this);
1442 }));
1443
1444 function getApplicationProtections(_x52, _x53, _x54, _x55) {
1445 return _ref21.apply(this, arguments);
1446 }
1447
1448 return getApplicationProtections;
1449 }(),
1450
1451 //
1452 getApplicationProtectionsCount: function () {
1453 var _ref22 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee21(client, applicationId, fragments) {
1454 var query;
1455 return regeneratorRuntime.wrap(function _callee21$(_context21) {
1456 while (1) {
1457 switch (_context21.prev = _context21.next) {
1458 case 0:
1459 _context21.next = 2;
1460 return queries.getApplicationProtectionsCount(applicationId, fragments);
1461
1462 case 2:
1463 query = _context21.sent;
1464 return _context21.abrupt('return', client.get('/application', query));
1465
1466 case 4:
1467 case 'end':
1468 return _context21.stop();
1469 }
1470 }
1471 }, _callee21, this);
1472 }));
1473
1474 function getApplicationProtectionsCount(_x56, _x57, _x58) {
1475 return _ref22.apply(this, arguments);
1476 }
1477
1478 return getApplicationProtectionsCount;
1479 }(),
1480
1481 //
1482 createTemplate: function () {
1483 var _ref23 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee22(client, template, fragments) {
1484 var mutation;
1485 return regeneratorRuntime.wrap(function _callee22$(_context22) {
1486 while (1) {
1487 switch (_context22.prev = _context22.next) {
1488 case 0:
1489 _context22.next = 2;
1490 return mutations.createTemplate(template, fragments);
1491
1492 case 2:
1493 mutation = _context22.sent;
1494 return _context22.abrupt('return', client.post('/application', mutation));
1495
1496 case 4:
1497 case 'end':
1498 return _context22.stop();
1499 }
1500 }
1501 }, _callee22, this);
1502 }));
1503
1504 function createTemplate(_x59, _x60, _x61) {
1505 return _ref23.apply(this, arguments);
1506 }
1507
1508 return createTemplate;
1509 }(),
1510
1511 //
1512 removeTemplate: function () {
1513 var _ref24 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee23(client, id) {
1514 var mutation;
1515 return regeneratorRuntime.wrap(function _callee23$(_context23) {
1516 while (1) {
1517 switch (_context23.prev = _context23.next) {
1518 case 0:
1519 _context23.next = 2;
1520 return mutations.removeTemplate(id);
1521
1522 case 2:
1523 mutation = _context23.sent;
1524 return _context23.abrupt('return', client.post('/application', mutation));
1525
1526 case 4:
1527 case 'end':
1528 return _context23.stop();
1529 }
1530 }
1531 }, _callee23, this);
1532 }));
1533
1534 function removeTemplate(_x62, _x63) {
1535 return _ref24.apply(this, arguments);
1536 }
1537
1538 return removeTemplate;
1539 }(),
1540
1541 //
1542 getTemplates: function () {
1543 var _ref25 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee24(client, fragments) {
1544 var query;
1545 return regeneratorRuntime.wrap(function _callee24$(_context24) {
1546 while (1) {
1547 switch (_context24.prev = _context24.next) {
1548 case 0:
1549 _context24.next = 2;
1550 return queries.getTemplates(fragments);
1551
1552 case 2:
1553 query = _context24.sent;
1554 return _context24.abrupt('return', client.get('/application', query));
1555
1556 case 4:
1557 case 'end':
1558 return _context24.stop();
1559 }
1560 }
1561 }, _callee24, this);
1562 }));
1563
1564 function getTemplates(_x64, _x65) {
1565 return _ref25.apply(this, arguments);
1566 }
1567
1568 return getTemplates;
1569 }(),
1570
1571 //
1572 getApplications: function () {
1573 var _ref26 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee25(client, fragments, params) {
1574 var query;
1575 return regeneratorRuntime.wrap(function _callee25$(_context25) {
1576 while (1) {
1577 switch (_context25.prev = _context25.next) {
1578 case 0:
1579 _context25.next = 2;
1580 return queries.getApplications(fragments, params);
1581
1582 case 2:
1583 query = _context25.sent;
1584 return _context25.abrupt('return', client.get('/application', query));
1585
1586 case 4:
1587 case 'end':
1588 return _context25.stop();
1589 }
1590 }
1591 }, _callee25, this);
1592 }));
1593
1594 function getApplications(_x66, _x67, _x68) {
1595 return _ref26.apply(this, arguments);
1596 }
1597
1598 return getApplications;
1599 }(),
1600
1601 //
1602 addApplicationSource: function () {
1603 var _ref27 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee26(client, applicationId, applicationSource, fragments) {
1604 var mutation;
1605 return regeneratorRuntime.wrap(function _callee26$(_context26) {
1606 while (1) {
1607 switch (_context26.prev = _context26.next) {
1608 case 0:
1609 _context26.next = 2;
1610 return mutations.addApplicationSource(applicationId, applicationSource, fragments);
1611
1612 case 2:
1613 mutation = _context26.sent;
1614 return _context26.abrupt('return', client.post('/application', mutation));
1615
1616 case 4:
1617 case 'end':
1618 return _context26.stop();
1619 }
1620 }
1621 }, _callee26, this);
1622 }));
1623
1624 function addApplicationSource(_x69, _x70, _x71, _x72) {
1625 return _ref27.apply(this, arguments);
1626 }
1627
1628 return addApplicationSource;
1629 }(),
1630
1631 //
1632 addApplicationSourceFromURL: function () {
1633 var _ref28 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee27(client, applicationId, url, fragments) {
1634 var file, mutation;
1635 return regeneratorRuntime.wrap(function _callee27$(_context27) {
1636 while (1) {
1637 switch (_context27.prev = _context27.next) {
1638 case 0:
1639 _context27.next = 2;
1640 return getFileFromUrl(client, url);
1641
1642 case 2:
1643 file = _context27.sent;
1644 _context27.next = 5;
1645 return mutations.addApplicationSource(applicationId, file, fragments);
1646
1647 case 5:
1648 mutation = _context27.sent;
1649 return _context27.abrupt('return', client.post('/application', mutation));
1650
1651 case 7:
1652 case 'end':
1653 return _context27.stop();
1654 }
1655 }
1656 }, _callee27, this);
1657 }));
1658
1659 function addApplicationSourceFromURL(_x73, _x74, _x75, _x76) {
1660 return _ref28.apply(this, arguments);
1661 }
1662
1663 return addApplicationSourceFromURL;
1664 }(),
1665
1666 //
1667 updateApplicationSource: function () {
1668 var _ref29 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee28(client, applicationSource, fragments) {
1669 var mutation;
1670 return regeneratorRuntime.wrap(function _callee28$(_context28) {
1671 while (1) {
1672 switch (_context28.prev = _context28.next) {
1673 case 0:
1674 _context28.next = 2;
1675 return mutations.updateApplicationSource(applicationSource, fragments);
1676
1677 case 2:
1678 mutation = _context28.sent;
1679 return _context28.abrupt('return', client.post('/application', mutation));
1680
1681 case 4:
1682 case 'end':
1683 return _context28.stop();
1684 }
1685 }
1686 }, _callee28, this);
1687 }));
1688
1689 function updateApplicationSource(_x77, _x78, _x79) {
1690 return _ref29.apply(this, arguments);
1691 }
1692
1693 return updateApplicationSource;
1694 }(),
1695
1696 //
1697 removeSourceFromApplication: function () {
1698 var _ref30 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee29(client, sourceId, applicationId, fragments) {
1699 var mutation;
1700 return regeneratorRuntime.wrap(function _callee29$(_context29) {
1701 while (1) {
1702 switch (_context29.prev = _context29.next) {
1703 case 0:
1704 _context29.next = 2;
1705 return mutations.removeSourceFromApplication(sourceId, applicationId, fragments);
1706
1707 case 2:
1708 mutation = _context29.sent;
1709 return _context29.abrupt('return', client.post('/application', mutation));
1710
1711 case 4:
1712 case 'end':
1713 return _context29.stop();
1714 }
1715 }
1716 }, _callee29, this);
1717 }));
1718
1719 function removeSourceFromApplication(_x80, _x81, _x82, _x83) {
1720 return _ref30.apply(this, arguments);
1721 }
1722
1723 return removeSourceFromApplication;
1724 }(),
1725
1726 //
1727 applyTemplate: function () {
1728 var _ref31 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee30(client, templateId, appId, fragments) {
1729 var mutation;
1730 return regeneratorRuntime.wrap(function _callee30$(_context30) {
1731 while (1) {
1732 switch (_context30.prev = _context30.next) {
1733 case 0:
1734 _context30.next = 2;
1735 return mutations.applyTemplate(templateId, appId, fragments);
1736
1737 case 2:
1738 mutation = _context30.sent;
1739 return _context30.abrupt('return', client.post('/application', mutation));
1740
1741 case 4:
1742 case 'end':
1743 return _context30.stop();
1744 }
1745 }
1746 }, _callee30, this);
1747 }));
1748
1749 function applyTemplate(_x84, _x85, _x86, _x87) {
1750 return _ref31.apply(this, arguments);
1751 }
1752
1753 return applyTemplate;
1754 }(),
1755
1756 //
1757 updateTemplate: function () {
1758 var _ref32 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee31(client, template, fragments) {
1759 var mutation;
1760 return regeneratorRuntime.wrap(function _callee31$(_context31) {
1761 while (1) {
1762 switch (_context31.prev = _context31.next) {
1763 case 0:
1764 _context31.next = 2;
1765 return mutations.updateTemplate(template, fragments);
1766
1767 case 2:
1768 mutation = _context31.sent;
1769 return _context31.abrupt('return', client.post('/application', mutation));
1770
1771 case 4:
1772 case 'end':
1773 return _context31.stop();
1774 }
1775 }
1776 }, _callee31, this);
1777 }));
1778
1779 function updateTemplate(_x88, _x89, _x90) {
1780 return _ref32.apply(this, arguments);
1781 }
1782
1783 return updateTemplate;
1784 }(),
1785 getApplicationProfiling: function () {
1786 var _ref33 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee32(client, applicationId) {
1787 return regeneratorRuntime.wrap(function _callee32$(_context32) {
1788 while (1) {
1789 switch (_context32.prev = _context32.next) {
1790 case 0:
1791 return _context32.abrupt('return', client.get('/profiling-run', { applicationId: applicationId }));
1792
1793 case 1:
1794 case 'end':
1795 return _context32.stop();
1796 }
1797 }
1798 }, _callee32, this);
1799 }));
1800
1801 function getApplicationProfiling(_x91, _x92) {
1802 return _ref33.apply(this, arguments);
1803 }
1804
1805 return getApplicationProfiling;
1806 }(),
1807 deleteProfiling: function () {
1808 var _ref34 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee33(client, profilingId) {
1809 return regeneratorRuntime.wrap(function _callee33$(_context33) {
1810 while (1) {
1811 switch (_context33.prev = _context33.next) {
1812 case 0:
1813 return _context33.abrupt('return', client.patch('/profiling-run/' + profilingId, {
1814 state: 'DELETED'
1815 }));
1816
1817 case 1:
1818 case 'end':
1819 return _context33.stop();
1820 }
1821 }
1822 }, _callee33, this);
1823 }));
1824
1825 function deleteProfiling(_x93, _x94) {
1826 return _ref34.apply(this, arguments);
1827 }
1828
1829 return deleteProfiling;
1830 }(),
1831
1832 /**
1833 * Starts a new instrumentation process.
1834 * Previous instrumentation must be deleted, before starting a new one.
1835 * @param client
1836 * @param applicationId
1837 * @returns {Promise<*>}
1838 */
1839 startInstrumentation: function () {
1840 var _ref35 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee34(client, applicationId) {
1841 var instrumentation;
1842 return regeneratorRuntime.wrap(function _callee34$(_context34) {
1843 while (1) {
1844 switch (_context34.prev = _context34.next) {
1845 case 0:
1846 _context34.next = 2;
1847 return this.getApplicationProfiling(client, applicationId).catch(function (e) {
1848 if (e.statusCode !== 404) throw e;
1849 });
1850
1851 case 2:
1852 instrumentation = _context34.sent;
1853
1854 if (!instrumentation) {
1855 _context34.next = 6;
1856 break;
1857 }
1858
1859 _context34.next = 6;
1860 return this.deleteProfiling(client, instrumentation.data.id);
1861
1862 case 6:
1863 return _context34.abrupt('return', client.post('/profiling-run', { applicationId: applicationId }));
1864
1865 case 7:
1866 case 'end':
1867 return _context34.stop();
1868 }
1869 }
1870 }, _callee34, this);
1871 }));
1872
1873 function startInstrumentation(_x95, _x96) {
1874 return _ref35.apply(this, arguments);
1875 }
1876
1877 return startInstrumentation;
1878 }(),
1879
1880 //
1881 createApplicationProtection: function () {
1882 var _ref36 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee35(client, applicationId, protectionOptions, fragments) {
1883 var _ref37, args, mutation;
1884
1885 return regeneratorRuntime.wrap(function _callee35$(_context35) {
1886 while (1) {
1887 switch (_context35.prev = _context35.next) {
1888 case 0:
1889 _context35.next = 2;
1890 return introspection.mutation(client, 'createApplicationProtection');
1891
1892 case 2:
1893 _ref37 = _context35.sent;
1894 args = _ref37.args;
1895 _context35.next = 6;
1896 return mutations.createApplicationProtection(applicationId, fragments, protectionOptions, args);
1897
1898 case 6:
1899 mutation = _context35.sent;
1900 return _context35.abrupt('return', client.post('/application', mutation));
1901
1902 case 8:
1903 case 'end':
1904 return _context35.stop();
1905 }
1906 }
1907 }, _callee35, this);
1908 }));
1909
1910 function createApplicationProtection(_x97, _x98, _x99, _x100) {
1911 return _ref36.apply(this, arguments);
1912 }
1913
1914 return createApplicationProtection;
1915 }(),
1916
1917 /**
1918 * @param {object} client
1919 * @param {string} instrumentationId
1920 * @returns {Promise<object>}
1921 */
1922 getInstrumentation: function () {
1923 var _ref38 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee36(client, instrumentationId) {
1924 return regeneratorRuntime.wrap(function _callee36$(_context36) {
1925 while (1) {
1926 switch (_context36.prev = _context36.next) {
1927 case 0:
1928 return _context36.abrupt('return', client.get('/profiling-run/' + instrumentationId));
1929
1930 case 1:
1931 case 'end':
1932 return _context36.stop();
1933 }
1934 }
1935 }, _callee36, this);
1936 }));
1937
1938 function getInstrumentation(_x101, _x102) {
1939 return _ref38.apply(this, arguments);
1940 }
1941
1942 return getInstrumentation;
1943 }(),
1944
1945 //
1946 getApplicationProtection: function () {
1947 var _ref39 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee37(client, applicationId, protectionId, fragments) {
1948 var query;
1949 return regeneratorRuntime.wrap(function _callee37$(_context37) {
1950 while (1) {
1951 switch (_context37.prev = _context37.next) {
1952 case 0:
1953 _context37.next = 2;
1954 return queries.getProtection(applicationId, protectionId, fragments);
1955
1956 case 2:
1957 query = _context37.sent;
1958 return _context37.abrupt('return', client.get('/application', query));
1959
1960 case 4:
1961 case 'end':
1962 return _context37.stop();
1963 }
1964 }
1965 }, _callee37, this);
1966 }));
1967
1968 function getApplicationProtection(_x103, _x104, _x105, _x106) {
1969 return _ref39.apply(this, arguments);
1970 }
1971
1972 return getApplicationProtection;
1973 }(),
1974
1975 //
1976 downloadSourceMapsRequest: function () {
1977 var _ref40 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee38(client, protectionId) {
1978 return regeneratorRuntime.wrap(function _callee38$(_context38) {
1979 while (1) {
1980 switch (_context38.prev = _context38.next) {
1981 case 0:
1982 return _context38.abrupt('return', client.get('/application/sourceMaps/' + protectionId, null, false));
1983
1984 case 1:
1985 case 'end':
1986 return _context38.stop();
1987 }
1988 }
1989 }, _callee38, this);
1990 }));
1991
1992 function downloadSourceMapsRequest(_x107, _x108) {
1993 return _ref40.apply(this, arguments);
1994 }
1995
1996 return downloadSourceMapsRequest;
1997 }(),
1998 downloadSymbolTableRequest: function () {
1999 var _ref41 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee39(client, protectionId) {
2000 return regeneratorRuntime.wrap(function _callee39$(_context39) {
2001 while (1) {
2002 switch (_context39.prev = _context39.next) {
2003 case 0:
2004 return _context39.abrupt('return', client.get('/application/symbolTable/' + protectionId, null, false));
2005
2006 case 1:
2007 case 'end':
2008 return _context39.stop();
2009 }
2010 }
2011 }, _callee39, this);
2012 }));
2013
2014 function downloadSymbolTableRequest(_x109, _x110) {
2015 return _ref41.apply(this, arguments);
2016 }
2017
2018 return downloadSymbolTableRequest;
2019 }(),
2020
2021 //
2022 downloadApplicationProtection: function () {
2023 var _ref42 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee40(client, protectionId) {
2024 return regeneratorRuntime.wrap(function _callee40$(_context40) {
2025 while (1) {
2026 switch (_context40.prev = _context40.next) {
2027 case 0:
2028 return _context40.abrupt('return', client.get('/application/download/' + protectionId, null, false));
2029
2030 case 1:
2031 case 'end':
2032 return _context40.stop();
2033 }
2034 }
2035 }, _callee40, this);
2036 }));
2037
2038 function downloadApplicationProtection(_x111, _x112) {
2039 return _ref42.apply(this, arguments);
2040 }
2041
2042 return downloadApplicationProtection;
2043 }(),
2044
2045 /**
2046 * @param {object} client
2047 * @param {string} instrumentationId
2048 * @returns {*}
2049 */
2050 downloadApplicationInstrumented: function downloadApplicationInstrumented(client, instrumentationId) {
2051 return client.get('/profiling-run/' + instrumentationId + '/instrumented-bundle', null, false);
2052 }
2053};
2054
2055
2056function getFileFromUrl(client, url) {
2057 return _axios2.default.get(url).then(function (res) {
2058 return {
2059 content: res.data,
2060 filename: _path2.default.basename(url),
2061 extension: _path2.default.extname(url).substr(1)
2062 };
2063 });
2064}
\No newline at end of file