-
Notifications
You must be signed in to change notification settings - Fork 0
/
debian.js
69 lines (60 loc) · 1.88 KB
/
debian.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
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var request = require('request');
var byline = require('byline');
var zlib = require('zlib');
module.exports = DebianPackage;
function DebianPackage(options) {
if (!(this instanceof DebianPackage))
return new DebianPackage(options);
options = options || {};
options.arch = options.arch || 'i386';
// fixme: build this url based on options
this.url = 'http://ftp.debian.org/debian/dists/sid/main/binary-amd64/Packages.gz';
this.lineStream = byline.createStream();
this.httpStream = request(this.url);
EventEmitter.call(this);
var that = this;
this.lineStream.on('data', function(data){
that.handleLineStream(that, data);
});
this.lineStream.on('error', function(err){
this.emit('error', err);
});
this.gunzip = zlib.createGunzip();
this.currentPackage = {};
this.isPackageLine = false;
this.packages = {};
this.httpStream.on('error', function(err){
that.emit('error', err);
});
this.gunzip.on('error', function(err){
that.emit('error', err);
});
this.gunzip.on('end', function(){
that.currentPackage = {};
that.isPackageLine = false;
that.started = false;
that.emit('end');
});
}
util.inherits(DebianPackage, EventEmitter);
DebianPackage.prototype.handleLineStream = function(self, line) {
line = line.toString();
if (line.indexOf('Package:') == 0) {
self.currentPackage.name = line.split(':')[1].trim();
self.isPackageLine = true;
}
if (line.indexOf('Version:') == 0) {
self.currentPackage.version = line.split(':')[1].trim();
self.packages[this.currentPackage.name] = this.currentPackage;
self.isPackageLine = false;
self.emit('data', this.currentPackage);
}
}
DebianPackage.prototype.resume = function() {
if (!this.started) {
this.httpStream.pipe(this.gunzip).pipe(this.lineStream);
this.started = true;
}
}