-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
37 lines (31 loc) · 1.24 KB
/
index.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
var fs = require('fs')
function isInvalidStrategy(pageStrategy) {
const allowedStrategies = /(default)|(hbbtv)|(hbbtv2)|(html5hbbtvhybrid)|(html5hbbtvhybridappshow)|(htmlbroadcastvideo)|(playstation3)|(samsungmaple)|(samsungstreaming)|(uwp)/
return pageStrategy && !pageStrategy.match(allowedStrategies)
}
function isInvalidElement(element) {
const allowedElements = /(body)|(doctype)|(header)|(mimetype)|(rootelement)/
return element && !element.match(allowedElements)
}
function getPageStrategyElement (pageStrategy, element) {
if (!fs.existsSync(__dirname + '/' + pageStrategy + '/' + element) || isInvalidStrategy(pageStrategy) || isInvalidElement(element)) {
return {
noSuchStrategy: "file does not exist or is not allowed: " + __dirname + '/' + pageStrategy + '/' + element
}
}
var pageStrategyPath = __dirname + '/' + pageStrategy + '/' + element;
return {
data: fs.readFileSync(pageStrategyPath).toString()
}
}
function getOrDefault (strategyName, element) {
var strategy = getPageStrategyElement(strategyName, element)
if (strategy.noSuchStrategy) {
return getPageStrategyElement('default', element)
}
return strategy
}
module.exports = {
getPageStrategyElement,
getOrDefault
}