-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
readConfigStream.js
41 lines (38 loc) · 1.49 KB
/
readConfigStream.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
const yaml = require('js-yaml')
const Joi = require('@hapi/joi')
const options = require('./options')
const schema = Joi.object().keys({
ctfFramework: Joi.string().optional().valid(options.ctfdFramework, options.fbctfFramework, options.rtbFramework),
juiceShopUrl: [Joi.string().uri().required(), Joi.string().ip().required()],
countryMapping: Joi.string().when('ctfFramework', { is: options.fbctfFramework, then: Joi.required(), otherwise: Joi.optional() }),
ctfKey: Joi.string().required(),
insertHints: Joi.any().valid('none', 'free', 'paid').required(),
insertHintUrls: Joi.any().valid('none', 'free', 'paid').when('ctfFramework', { is: options.fbctfFramework, then: Joi.optional(), otherwise: Joi.required() })
})
const hintsMap = { none: 0, free: 1, paid: 2 }
function readConfigStream (stream) {
return new Promise((resolve, reject) => {
let data = ''
stream.on('data', (chunk) => {
data = data + chunk
})
stream.on('end', () => {
try {
yaml.safeLoadAll(data, (doc) => {
const validation = schema.validate(doc)
if (validation.error) {
reject(validation.error)
} else {
const result = validation.value
result.insertHints = hintsMap[result.insertHints]
result.insertHintUrls = result.insertHintUrls ? hintsMap[result.insertHintUrls] : 0
resolve(result)
}
})
} catch (error) {
reject(error)
}
})
})
}
module.exports = readConfigStream