-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathbuilder.js
executable file
·97 lines (88 loc) · 3.11 KB
/
builder.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
const linter = require('stremio-addon-linter')
// Implements a builder pattern, but we can built into a router or an interface
// the addonInterface is just an object: { manifest, get(resource, type, id, extra) }
function AddonBuilder(manifest) {
const handlers = {}
// Lint the manifest
const linterRes = linter.lintManifest(manifest)
if (!linterRes.valid) {
//console.error('Manifest issues:\n' + linterRes.errors.join('\n'))
throw linterRes.errors[0]
}
if (linterRes.warnings.length) {
linterRes.warnings.forEach(function(warning) {
console.log('WARNING:', warning.message)
})
}
Object.freeze(manifest)
// Check the manifest length
if (JSON.stringify(manifest).length > 8192) {
throw new Error('manifest size exceeds 8kb, which is incompatible with addonCollection API')
}
// Validation: called on building
const validate = function() {
const errors = []
const handlersInManifest = []
if (manifest.catalogs.length > 0) handlersInManifest.push('catalog')
manifest.resources.forEach((r) => handlersInManifest.push(r.name || r))
const handlersDefined = Object.keys(handlers)
handlersDefined.forEach(defined => {
if (!handlersInManifest.includes(defined)) {
if (defined == 'catalog') errors.push(new Error('manifest.catalogs is empty, catalog handler will never be called'))
else errors.push(new Error('manifest.resources does not contain: '+defined))
}
})
handlersInManifest.forEach(defined => {
if (!handlersDefined.includes(defined)) {
const capitalized = defined[0].toUpperCase() + defined.slice(1)
errors.push(new Error(
`manifest definition requires handler for ${defined},`
+` but it is not provided (use .define${capitalized}Handler())`
))
}
})
return errors
}
const validOrExit = function() {
const errors = validate()
if (errors.length) {
//errors.forEach(e => console.error(`ERROR: ${e.message}`))
throw errors[0]
}
}
// Public interface
this.defineResourceHandler = function(resource, handler) {
if (handlers[resource]) {
throw new Error('handler for '+resource+' already defined')
}
handlers[resource] = handler
return this
}
this.defineStreamHandler = this.defineResourceHandler.bind(this, 'stream')
this.defineMetaHandler = this.defineResourceHandler.bind(this, 'meta')
this.defineCatalogHandler = this.defineResourceHandler.bind(this, 'catalog')
this.defineSubtitlesHandler = this.defineResourceHandler.bind(this, 'subtitles')
this.definePlayerHandler = this.defineResourceHandler.bind(this, 'player')
this.defineLibraryHandler = this.defineResourceHandler.bind(this, 'library')
// build into an interface
this.getInterface = function() {
validOrExit()
return new AddonInterface(manifest, handlers)
}
return this
}
function AddonInterface(manifest, handlers) {
this.manifest = Object.freeze(Object.assign({}, manifest))
this.get = (resource, type, id, extra = {}, config = {}) => {
const handler = handlers[resource]
if (!handler) {
return Promise.reject({
message: `No handler for ${resource}`,
noHandler: true
})
}
return handler({ type, id, extra, config })
}
return this
}
module.exports = AddonBuilder