-
Notifications
You must be signed in to change notification settings - Fork 0
/
blankon.js
52 lines (46 loc) · 1.31 KB
/
blankon.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
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var request = require('request');
var byline = require('byline');
module.exports = BlankonPackage;
function BlankonPackage(options) {
if (!(this instanceof BlankonPackage))
return new BlankonPackage(options);
options = options || {};
options.arch = options.arch || 'i386';
// fixme: build this url based on options
this.url = options.url;
this.lineStream = byline.createStream();
EventEmitter.call(this);
var that = this;
this.httpStream = request(this.url);
this.lineStream.on('data', function(data){
that.handleLineStream(that, data);
});
this.packages = {};
this.httpStream.on('error', function(err){
that.emit('error', err);
});
that.httpStream.on('end', function(){
that.emit('end');
that.started = false;
that.packages = {};
});
}
util.inherits(BlankonPackage, EventEmitter);
BlankonPackage.prototype.handleLineStream = function(self, line) {
line = line.toString();
var arr = line.split(' ');
var package = {
name: arr[0].trim(),
version: arr[1].trim()
};
this.packages[package.name] = package;
self.emit('data', package);
}
BlankonPackage.prototype.resume = function() {
if (!this.started) {
this.httpStream.pipe(this.lineStream);
this.started = true;
}
}