-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.js
47 lines (42 loc) · 1.12 KB
/
stream.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
var Promise = require('promise');
var request = require('request');
var URLToolkit = require('url-toolkit');
var i = 0;
function Stream(config) {
var self = this;
this.name = config.name || ('s' + ++i);
this.url = config.url;
this.content = null;
this.refreshInterval = config.refreshInterval || 1500;
this.intervalRefresh = function () {
setTimeout(function () {
self.refresh().then(self.intervalRefresh, self.intervalRefresh);
}, self.refreshInterval);
}
}
/**
* @returns {Promise}
*/
Stream.prototype.refresh = function () {
var self = this;
var promise = new Promise(function (resolve, reject) {
request.get(self.url, {timeout: 1000}, function (err, response, body) {
if (err) {
reject(err);
return;
}
if (response.statusCode !== 200) {
reject(response.statusCode);
}
resolve(body);
});
});
promise.then(function (value) {
self.content = value.replace(/.*\.ts/g, function (match) {
return URLToolkit.buildAbsoluteURL(self.url, './' + match);
});
return self.content;
});
return promise;
};
module.exports = Stream;