-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.js
791 lines (710 loc) · 27.2 KB
/
util.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#!/usr/bin/env node
'use strict';
const request = require('request-promise-native'); //deprecated.. use axios
const axios = require('axios');
const config = require('./config');
const fs = require('fs');
const jsonwebtoken = require('jsonwebtoken');
const timeago = require('time-ago');
const async = require('async');
const tar = require('tar');
const path = require('path');
const mkdirp = require('mkdirp');
const colors = require('colors');
const delimiter = ',';
exports.login = async function(opt) {
let url = config.api.auth;
if(opt.ldap) url += "/ldap/auth";
else url += "/local/auth";
let jwt = null;
try {
const res = await axios.post(url, {username: opt.username, password: opt.password, ttl: 1000*60*60*24*(opt.ttl || 1)});
if(res.status != 200) throw new Error(res.data.message);
jwt = res.data.jwt;
} catch (err) {
throw new Error(err.response.data.message);
}
let dirname = path.dirname(config.path.jwt);
await mkdirp(dirname);
fs.chmodSync(dirname, '700');
fs.writeFileSync(config.path.jwt, jwt);
fs.chmodSync(config.path.jwt, '600');
return jwt;
}
exports.refresh = async function(opt, headers) {
let url = config.api.auth+"/refresh";
let res = await axios.post(url, {ttl: 1000*60*60*24*(opt.ttl || 1)}, {headers});
if(res.status != 200) throw new Error("Error: " + res.data.message);
let dirname = path.dirname(config.path.jwt);
await mkdirp(dirname);
fs.chmodSync(dirname, '700');
fs.writeFileSync(config.path.jwt, res.data.jwt);
fs.chmodSync(config.path.jwt, '600');
return res.data.jwt;
}
/**
* Load the user's jwt token
* @returns {Promise<string>}
*/
exports.loadJwt = function() {
return new Promise((resolve, reject) => {
fs.stat(config.path.jwt, (err, stat) => {
if (err) {
return reject("Couldn't find your access token. Please try logging in by running 'bl login'");
process.exit(1);
}
let jwt = fs.readFileSync(config.path.jwt, "ascii").trim();
let dec = jsonwebtoken.decode(jwt);
if(!dec) return reject("Failed to decode you access token. Please try logging in by running 'bl login'");
if(dec.exp < Date.now()/1000) return reject("You access token is expired. Please try logging in by running 'bl login'.");
resolve(jwt);
});
});
}
exports.queryProfiles = function(headers, query, opt) {
if(!query) query = {};
if(!opt) opt = {};
return new Promise(async (resolve, reject) => {
let body = await request(config.api.auth + '/profile/list', {
headers,
json: true,
qs: {
limit: opt.limit||0,
offset: opt.skip||0,
find: JSON.stringify({active: true}),
}
});
let profiles = body.profiles;
//TODO - I should apply search query to the API instad (I can't until I migrate to mongo)
if (query.id || query.search) {
profiles = profiles.filter(profile => {
let showProfile = false;
if (query.id) {
showProfile = showProfile || profile.sub == query.id;
}
if (query.search) {
let pattern = new RegExp(escapeRegExp(query.search), 'ig');
showProfile = showProfile ||
pattern.test(profile.fullname) ||
pattern.test(profile.email) ||
pattern.test(profile.username);
}
return showProfile;
});
}
resolve(profiles);
});
}
//TODO get rid of this - merged into queryProfiles?
exports.queryAllProfiles = function(headers) {
return request(config.api.auth + '/profile/list', {
headers,
json: true,
qs: {
limit: 0,
offset: 0,
where: JSON.stringify({active: true}),
}
}).then(body=>{
return body.profiles;
});
}
//TODO get rid of this
exports.resolveProfiles = function(headers, query, opt) {
if (!query) return new Promise(r => r([]));
if (exports.isValidObjectId(query)) return exports.queryProfiles(headers, { query }, opt);
else return exports.queryProfiles(headers, { search: query }, opt);
}
/**
* Query the list of datasets
*/
exports.queryDatasets = async function(headers, query, opt) {
if(!query) query = {};
if(!opt) opt = {};
let datatype = null;
let project = null;
if (query.datatype) {
let datatypeSearch = {};
let findQuery = {name: query.datatype};
if (exports.isValidObjectId(query.datatype)) findQuery = {_id: query.datatype};
let body = await request(config.api.warehouse + '/datatype', { headers, json: true, qs: {
find: JSON.stringify(findQuery),
limit: 1,
}});
if (body.datatypes.length != 1) throw new Error("No datatypes found matching '" + query.datatype + "'");
datatype = body.datatypes[0]._id;
}
if (query.project) {
let projectSearch = {};
let projects = await exports.resolveProjects(headers, query.project);
if (projects.length == 0) throw new Error("No projects found matching '" + query.project + "'");
if (projects.length > 1) throw new Error("Multiple projects found matching '" + query.project + "'");
project = projects[0]._id;
}
let find = {};
let andQueries = [];
let orQueries = [];
if(query.pub) {
andQueries.push({ publications: query.pub });
} else {
//hide removed dataset unless we are querying for publication. this is UGLY.. but I'd like to maintain
//common behavior across all queryXX which hides removed records by default.
find.removed = false;
}
if (query.id) {
if (!exports.isValidObjectId(query.id)) throw new Error('Not a valid object id: ' + query.id);
orQueries.push({ _id: query.id });
}
if (query.search) {
orQueries.push({ name: { $regex: escapeRegExp(query.search), $options: 'ig' } });
orQueries.push({ desc: { $regex: escapeRegExp(query.search), $options: 'ig' } });
}
if (query.tags) {
let pos_tags = [];
let neg_tags = [];
query.tags.forEach(tag => {
if (tag[0] != "!") pos_tags.push(tag);
else neg_tags.push(tag.substring(1));
});
if(pos_tags.length > 0) andQueries.push({tags: {$all:pos_tags}});
if(neg_tags.length > 0) andQueries.push({tags: {$nin:neg_tags}});
}
if (query.datatypeTags) {
let pos_tags = [];
let neg_tags = [];
query.datatypeTags.forEach(tag => {
if (tag[0] != "!") pos_tags.push(tag);
else neg_tags.push(tag.substring(1));
});
if(pos_tags.length > 0) andQueries.push({datatype_tags: {$all:pos_tags}});
if(neg_tags.length > 0) andQueries.push({datatype_tags: {$nin:neg_tags}});
}
if (project) andQueries.push({ project });
if (datatype) andQueries.push({ datatype });
if (query.subject) andQueries.push({ "meta.subject": query.subject });
if (query.session) andQueries.push({ "meta.session": query.session});
if (query.run) andQueries.push({ "meta.run": query.run});
if (query.taskId) {
if (!exports.isValidObjectId(query.taskId)) throw new Error("Not a valid task id: " + query.taskId);
andQueries.push({ 'prov.task_id': query.taskId });
}
if (orQueries.length > 0) andQueries.push({ $or: orQueries });
if (andQueries.length > 0) find.$and = andQueries;
return request(config.api.warehouse + '/dataset', { json: true, headers, qs: {
find: JSON.stringify(find),
skip: opt.skip || 0,
limit: opt.limit || 100
} }).then(body=>{
body.datasets.count = body.count;
return (body.datasets);
});
}
//TODO GET rid of this
exports.queryAllDatasets = function(headers) {
return request(config.api.warehouse + '/dataset', {
headers,
json: true,
qs: {
limit: 0,
offset: 0
}
});
}
//TODO - get rid of this
exports.resolveDatasets = function(headers, query, opt) {
if (!query) return new Promise(r => r([]));
if (exports.isValidObjectId(query)) return exports.queryDatasets(headers, { id: query }, opt);
else {
return exports.queryDatasets(headers, { search: query }, opt);
}
}
exports.queryProjects = async function(headers, query, opt) {
if(!query) query = {};
if(!opt) opt = {};
let projectAdmin = null;
let projectMember = null;
let projectGuest = null;
if (query.admin) projectAdmin = await exports.resolveProfiles(headers, query.admin);
if (query.member) projectMember = await exports.resolveProfiles(headers, query.member);
if (query.guest) projectGuest = await exports.resolvePRofiles(headers, query.guest);
let find = { removed: false }, andQueries = [], orQueries = [];
if (query.id) {
if (!exports.isValidObjectId(query.id)) throw new Error('Not a valid object id: ' + query.id);
orQueries.push({ _id: query.id });
}
if (query.search) {
orQueries.push({ name: { $regex: escapeRegExp(query.search), $options: 'ig' } });
orQueries.push({ desc: { $regex: escapeRegExp(query.search), $options: 'ig' } });
}
if (projectAdmin) {
andQueries.push({ admins: { $in: projectAdmin.map(p=>{return p.sub})} });
}
if (projectMember) {
andQueries.push({ members: { $in: projectMember.map(p=>{return p.sub})} });
}
if (projectGuest) {
andQueries.push({ quests: { $in: projectQuest.map(p=>{return p.sub})} });
}
if (orQueries.length > 0) andQueries.push({ $or: orQueries });
if (andQueries.length > 0) find.$and = andQueries;
return request(config.api.warehouse + '/project', { headers, json: true,
qs: {
find: JSON.stringify(find),
sort: JSON.stringify({ name: 1 }),
skip: opt.skip || 0,
limit: opt.limit || 100
}
}).then(body=>{
//else if (res.statusCode != 200) return throw new Error(res.body.message);
return body.projects;
});
}
exports.queryPubs = async function(headers, query, opt) {
if(!query) query = {};
if(!opt) opt = {};
let pubAuthors = null;
if (query.author) pubAuthors = await exports.resolveProfiles(headers, query.author);
let find = { removed: false }, andQueries = [], orQueries = [];
if (query.id) {
if (!exports.isValidObjectId(query.id)) throw new Error('Not a valid object id: ' + query.id);
orQueries.push({ _id: query.id });
}
if (query.search) {
orQueries.push({ name: { $regex: escapeRegExp(query.search), $options: 'ig' } });
orQueries.push({ desc: { $regex: escapeRegExp(query.search), $options: 'ig' } });
}
if (pubAuthors) {
andQueries.push({ authors: { $in: pubAuthors.map(p=>{return p.sub})} });
}
if (query.doi) {
andQueries.push({ doi: { $regex: escapeRegExp(query.doi), $options: 'ig'} });
}
if (orQueries.length > 0) andQueries.push({ $or: orQueries });
if (andQueries.length > 0) find.$and = andQueries;
return request(config.api.warehouse + '/pub', { headers, json: true,
qs: {
find: JSON.stringify(find),
sort: JSON.stringify({ name: 1 }),
skip: opt.skip || 0,
limit: opt.limit || 100
}
}).then(body=>{
return body.pubs;
});
}
//TODO get rid off this
exports.queryAllProjects = function(headers) {
return request(config.api.warehouse + '/project', {
headers,
json: true,
qs: {
limit: 0,
offset: 0
}
}).then(body=>{
return body.projects;
});
}
//TODO get rid off this with > let projects = await util.queryProjects(headers, {id: query.project, search: query.project});
exports.resolveProjects = function(headers, query, opt) {
if (!query) return new Promise(r => r([]));
if (exports.isValidObjectId(query)) return exports.queryProjects(headers, { id: query }, opt);
else return exports.queryProjects(headers, { search: query }, opt);
}
/**
* Query the list of apps
* @param {any} headers
* @param {Object} query
* @param {string} query.id
* @param {string} query.search
* @param {string} query.doi
* @param {string[]} query.inputs
* @param {string[]} query.outputs
* @param {Object} opt
* @param {number} opt.skip
* @param {number} opt.limit
* @returns {Promise<app[]>}
*/
exports.queryApps = async function(headers, query, opt) {
if(query === undefined) query = {};
if(opt === undefined) opt = {};
let input_datatypes = [];
let output_datatypes = [];
if (query.inputs) {
for (let input of query.inputs) {
input_datatypes.push(await exports.getDatatype(headers, input));
}
}
if (query.outputs) {
for (let output of query.outputs) {
output_datatypes.push(await exports.getDatatype(headers, output));
}
}
let andQueries = [];
let orQueries = [];
if (query.id) {
if (!exports.isValidObjectId(query.id)) throw new Error('Not a valid object id: ' + query.id);
orQueries.push({ _id: query.id });
}
if (query.search) {
orQueries.push({ name: { $regex: escapeRegExp(query.search), $options: 'ig' } });
orQueries.push({ desc: { $regex: escapeRegExp(query.search), $options: 'ig' } });
}
if (query.doi) {
andQueries.push({ doi: query.doi });
}
//TODO - I should probably use $all and $nin instead of $elemMAtch
if (input_datatypes.length > 0) {
andQueries = andQueries.concat(input_datatypes.map(datatype => {
if (datatype.not) {
return { inputs: { $not: { $elemMatch: { datatype: datatype._id } } } };
} else {
return { inputs: { $elemMatch: { datatype: datatype._id } } };
}
}));
}
if (output_datatypes.length > 0) {
andQueries = andQueries.concat(output_datatypes.map(datatype => {
return { outputs: { $elemMatch: { datatype: datatype._id } } };
}));
}
let find = { removed: false };
if (orQueries.length > 0) andQueries.push({ $or: orQueries });
if (andQueries.length > 0) find.$and = andQueries;
return request(config.api.warehouse + '/app', {
headers,
json: true,
qs: {
find: JSON.stringify(find),
sort: "name",
skip: opt.skip || 0,
limit: opt.limit || 100
}
}).then(res=>{
return res.apps;
});
}
exports.getDatatype = function(headers, query) {
return new Promise(async (resolve, reject) => {
const find = {};
if(exports.isValidObjectId(query)) find._id = query;
else find.name = query;
axios.get(config.api.warehouse + '/datatype', { headers,
params: {
find: JSON.stringify(find),
}
}).then(res=>{;
if(res.data.datatypes.length == 0) return reject("no matching datatype:"+query);
return resolve(res.data.datatypes[0]);
});
});
}
//TODO get rid off this
exports.resolveApps = function(headers, query, opt) {
if (!query) return new Promise(r => r([]));
if (exports.isValidObjectId(query)) return exports.queryApps(headers, { id: query }, opt);
else return exports.queryApps(headers, { search: query }, opt);
}
//TODO get rid of this
exports.queryAllDatatypes = function(headers) {
return request(config.api.warehouse + '/datatype', {
headers,
json: true,
qs: {
limit: 0,
offset: 0
}
}).then(body=>{
return body.datatypes;
});
}
/**
* Query the list of resources
* @param {any} headers
* @param {Object} query
* @param {string} query.id
* @param {string} query.search
* @param {string[]} query.status
* @param {string[]} query.service
* @param {Object} opt
* @param {number} opt.skip
* @param {number} opt.limit
* @returns {Promise<resource[]>}
*/
exports.queryResources = function(headers, query, opt) {
if(!query) query = {};
if(!opt) opt = {};
let find = {}, orQueries = [], andQueries = [];
if (query.id) {
if (!exports.isValidObjectId(query.id)) throw new Error('Not a valid object id: ' + query.id);
orQueries.push({ _id: query.id });
}
if (query.search) {
orQueries.push({ name: { $regex: escapeRegExp(query.search), $options: 'ig' } });
orQueries.push({ desc: { $regex: escapeRegExp(query.search), $options: 'ig' } });
}
if (query.status) {
andQueries.push({ status: query.status });
}
if (query.service) {
//TODO I think I can just do "config.services.name": query.service
andQueries.push({ "config.services": { $elemMatch: { "name": query.service } } });
}
if (orQueries.length > 0) andQueries.push({ $or: orQueries });
if (andQueries.length > 0) find.$and = andQueries;
return request(config.api.amaretti + '/resource', { headers, json: true,
qs: {
find: JSON.stringify(find),
sort: JSON.stringify({ name: 1 }),
skip: opt.skip || 0,
limit: opt.limit || 100
}
}).then(body=>{
return body.resources;
});
}
//TODO get rid of this
exports.resolveResources = function(headers, query, opt) {
if (!query) return new Promise(r => r([]));
if (exports.isValidObjectId(query)) return exports.queryResources(headers, { id: query }, opt);
else return exports.queryResources(headers, { search: query }, opt);
}
/**
* Find or create an instance for a service
* @param {any} headers
* @param {string} instanceName
* @param {Object} options
* @param {project} options.project
* @param {string} options.desc
* @returns {Promise<instance>}
*/
exports.findOrCreateInstance = function(headers, instanceName, options) {
return new Promise((resolve, reject)=>{
// get instance that might already exist
var find = { name: instanceName };
options = options || {};
request({url: config.api.amaretti + "/instance?find=" + JSON.stringify(find), headers: headers, json: true}, (err, res, body) => {
if(err) return reject(err);
if(res.statusCode != 200) return reject(res.statusCode);
if(body.instances[0]) resolve(body.instances[0]);
else {
// need to create new instance
let body = { name: instanceName, desc: options.desc };
if (options.project) {
body.config = { brainlife: true };
body.group_id = options.project.group_id;
}
request.post({url: config.api.amaretti + "/instance", headers: headers, json: true, body,
}, function(err, res, body) {
if (err) return reject(err);
else if (res.statusCode != 200) {
if (res.statusMessage == 'not member of the group you have specified') {
return reject("There was an error during instance creation. Please log in again.");
}
else return reject(res.body.message);
} else {
resolve(body);
}
});
}
});
});
}
//Wait for datasets from task to be archived
exports.waitForArchivedDatasets = function(headers, datasetCount, task, verbose, cb) {
request(config.api.warehouse+'/dataset', { json: true, headers, qs: {
find: JSON.stringify({'prov.task_id': task._id}),
} }, (err, res, body) => {
if (err) return cb(err);
if (res.statusCode != 200) return cb(res.body.message);
let failed = false;
let stored_datasets = body.datasets.filter(dataset=>{
if(verbose) console.debug(new Date(), "object:", dataset._id, dataset.status, dataset.status_msg);
if(dataset.status == "failed") failed = true;
return (dataset.status == "stored");
});
if(failed) return cb("failed to archive", null);
if(stored_datasets.length == datasetCount) {
//finished!
return cb(null, stored_datasets);
} else {
//if(verbose) console.log(stored_datasets.length+" of "+datasetCount+" datasets archived");
//not all datasets archived yet.. wait
return setTimeout(()=>{
exports.waitForArchivedDatasets(headers, datasetCount, task, verbose, cb);
}, 1000*5);
}
});
}
//wait for the task to finish
//2nd parameter for cb will be set to archive_task for output
exports.waitForFinish = function(headers, task, verbose, cb) {
var find = {_id: task._id};
axios.get(config.api.amaretti + "/task?find=" + JSON.stringify({_id: task._id}), {headers}).then(res=>{
if(res.status != 200) return cb(err);
//task might not yet be comitted to the db.. and length could be 0?
if(res.data.tasks.length == 1) {
let task = res.data.tasks[0];
if(verbose) console.debug(new Date, "task:", task._id, task.name, task.service, task.status, task.status_msg);
if (task.status == "finished") {
let datasetCount = 0;
if(task.config && task.config._outputs) {
task.config._outputs.forEach(output=>{
if(output.archive) datasetCount++;
});
}
if(datasetCount == 0) return cb(null, null, []); //no output for this task.
if(verbose) console.log("waiting for output to be archived...")
if(task.name == "__dtv") {
//check for validation result
if(verbose) console.debug("loading product for __dtv", task._id);
let params = {ids: [task._id]};
axios.get(config.api.amaretti+"/task/product", {headers, params}).then(res=>{
if(res.data.length == 0) return cb("couldn't find validation result");
let product = res.data[0].product;
if(verbose && product.warnings && product.warnings.length > 0) console.error("warnings", product.warnings);
if(product.errors && product.errors.length > 0) return cb(product.errors);
//now wait for the output to be archived
exports.waitForArchivedDatasets(headers, datasetCount, task, verbose, (err, datasets)=>{
return cb(err, task, datasets);
});
}).catch(err=>{
return cb(err);
});
} else {
//datatype without validator should immediately archive
exports.waitForArchivedDatasets(headers, datasetCount, task, verbose, (err, datasets)=>{
return cb(err, task, datasets);
});
}
return;
} else if (task.status == "failed") {
return cb(task.status_msg, null);
}
}
setTimeout(function() {
exports.waitForFinish(headers, task, verbose, cb);
}, 3000);
}).catch(err=>{
return cb(err, null);
});
}
/**
* Get a specific file from a task's output
* @param {any} headers
* @param {string} filename
* @param {task} task
*/
/*
exports.getFileFromTask = function(headers, filename, task) {
return new Promise(async (resolve, reject) => {
let fileBody = await request({
url: config.api.amaretti + '/task/ls/' + task._id,
headers,
json: true });
let files = fileBody.files;
let taskFile = null;
files.forEach(file => {
if (file.filename == filename) {
taskFile = file;
}
});
if (taskFile) {
let result = await request({
url: config.api.amaretti + '/task/download/' + task._id+'/'+taskFile.filename,
headers,
});
return resolve(result);
} else {
return reject("failed to load "+filename);
}
});
}
*/
//TODO - not very effective - as user can easily go around this check by directly accessing to our REST API.
/**
* Escapes a user input string to make it safe for regex matching
* @param {string} str
* @returns {string}
*/
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\/\^\$\|]/g, "\\$&");
}
/**
* Returns whether or not a given string is a valid object ID
* @param {string} str
* @returns {boolean}
*/
exports.isValidObjectId = function(id) {
return id.match(/^[0-9a-fA-F]{24}$/);
}
/**
* Return a pluralized string whether or not there are multiple objects
* @param {string} string
* @param {any[]} objects
* @returns {string}
*/
exports.pluralize = function(string, objects) {
if (objects.length == 1) return string;
if (string == 'was') return 'were';
return string + "s";
}
exports.collect = function(val, all) {
all.push(val);
return all;
}
//remove "." in object keys as it screws up mongo db
exports.escape_dot = function(obj) {
if(typeof obj == "object") {
for(let key in obj) {
exports.escape_dot(obj[key]);
if(key.includes(".")) {
let newkey = key.replace(/\./g, '-');
obj[newkey] = obj[key];
delete obj[key];
}
}
}
return obj;
}
exports.parseParticipantTSV = function(tsv) {
let participants = [];
tsv = tsv.map(line=>line.trim()); //remove \r
//console.log("loading participants.tsv (or -data.tsv)", root);
let tsv_head = exports.escape_dot(tsv.shift().split("\t"));
//look for subject header..
let subject_col = 0; //first one by default..
[ "Observations", "participant_id" ].forEach(key=>{
let col = tsv_head.indexOf(key);
if(~col) subject_col = col;
});
tsv.forEach(row=>{
let cols = row.trim().split("\t");
let subject = cols[subject_col];
if(subject.toLowerCase().startsWith("sub-")) subject = subject.substring(4);
let participant = {subject};
cols.forEach((col, idx)=>{
if(idx == subject_col) return;
participant[tsv_head[idx]] = col.trim();
});
participants.push(exports.escape_dot(participant));
});
return participants;
}
exports.handleAxiosError = function(err) {
if (err.response) {
if(err.response.data) {
if(err.response.data.message) console.error(err.response.data.message);
else console.error(err.response.data);
} else console.error(err.response);
} else if (err.request) {
// The request was made but no response was received
// `err.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(err.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error(err);
}
//console.error(err.config);
}