-
Notifications
You must be signed in to change notification settings - Fork 39
/
util.js
88 lines (68 loc) · 1.94 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
/*!
* Copyright (c) 2019 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
const path = require('path');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function generate(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.generator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
if(stderr) {
throw new Error(stderr);
}
return JSON.parse(stdout);
}
async function generateJwt(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.generator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
if(stderr) {
throw new Error(stderr);
}
return stdout;
}
async function generatePresentation(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.presentationGenerator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
if(stderr) {
throw new Error(stderr);
}
return JSON.parse(stdout);
}
async function generatePresentationJwt(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.presentationGenerator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
if(stderr) {
throw new Error(stderr);
}
return stdout;
}
function hasType(doc, expectedType) {
if(!doc) {
return false;
}
let type = doc.type;
if(!Array.isArray(type)) {
type = [type];
}
return type.some(el => el === expectedType);
}
// RFC3999 regex
// Z and T can be lowercase
const RFC3339regex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
'(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):' +
'([0-5][0-9]):([0-5][0-9]|60)' +
'(\\.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):' +
'([0-5][0-9]))$', 'i');
module.exports = {
generate,
generatePresentation,
generateJwt,
generatePresentationJwt,
hasType,
RFC3339regex
};