-
Notifications
You must be signed in to change notification settings - Fork 24
/
worker.js
86 lines (77 loc) · 2.25 KB
/
worker.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
const EventEmitter = require('events')
const once = require('once') // eslint-disable-line no-unused-vars
const _ = require('lodash')
const DataStream = require('./dataStream')
const request = require('request')
const Logger = new (require('./logger'))()
class Worker extends EventEmitter {
constructor (guid, line) {
super()
// Declares
this.guid = guid
this.line = line
this.url = line.url
this.listeners = 0
this.stream = new DataStream()
// Bindings
this.end = this.end.bind(this)
this.unsubscribe = this.unsubscribe.bind(this)
this.subscribe = this.subscribe.bind(this)
this.requestFactory = this.requestFactory.bind(this)
// Init
this.request = this.requestFactory()
this.once('end', (guid) => {
this.stream.end()
})
this.stream.on('data', buffer => {
this.emit('data', buffer)
})
this.stream.once('end', () => {
this.request.abort()
})
}
end () {
Logger.verbose(`End of: ${this.line.internalUrl}`)
this.emit('end', this.guid)
}
unsubscribe () {
Logger.verbose(`Unsubscribe to: ${this.line.internalUrl}`)
this.listeners = this.listeners - 1
if (this.listeners <= 0) {
Logger.verbose('No more subscribers.')
this.listeners = 0
this.end()
}
}
subscribe () {
Logger.verbose(`Subscribe to: ${this.line.internalUrl}`)
this.listeners = this.listeners + 1
}
requestFactory () {
Logger.verbose(`Preloading: ${this.line.internalUrl} (${this.url})`)
let myRequest = request({
url: this.url,
headers: {
'User-Agent': 'vlc 3.0.3'
}
}, (error, response, body) => {
if (_.get(response, 'statusCode', 'Unknown') !== '200') {
Logger.warn(`Remote stream replied: ${_.get(response, 'statusMessage', 'Unknown Error')} (${_.get(response, 'statusCode', 'Unknown')})`)
}
if (error) {
Logger.error('Error occured:', error)
}
})
myRequest.once('end', () => {
if (!this.stream.isEnded) {
Logger.verbose(`Renew preloading: ${this.line.internalUrl}`)
this.request = this.requestFactory(this.url)
}
})
myRequest.on('data', (buffer) => {
this.stream.write(buffer)
})
return myRequest
}
}
module.exports = Worker