-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (173 loc) · 5.27 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var deps = require('r-constants').dependency_types;
var byline = require('byline');
var fs = require('fs');
var filetype = require('file-type');
var tar = require('tar-stream');
var gunzip = require('gunzip-maybe');
var unzip = require('unzipper');
var stream = require('stream');
function normalize_ws(x){
return x.trim().replace(/\s/g, ' ');
}
function parse_desc_stream(descstream, callback) {
var descstream = byline(descstream, { keepEmptyLines: true });
var desc = { };
var current = '';
var first = true;
descstream.setEncoding('utf8');
function reader(line) {
// First line is special
if (first) {
current = line;
first = false;
// Starts with space, same record, append
} else if (line.match(/^\s/)) {
current = current + '\n' + line.trim();
// New record, need to emit the previous one
} else {
var rec = split_record(current);
if (rec.key === '') {
// No way to close a stream, we just remove the listeners
// if an error happens
descstream.removeListener('data', reader);
descstream.removeListener('end', finisher);
return callback('Invalid record: ' + rec.value);
}
if (deps.indexOf(rec.key) > -1) {
rec.value = parse_dep_string(rec.value);
}
if (rec.key == 'Remotes') {
rec.value = parse_remotes(rec.value);
}
if (rec.key == 'Built') {
rec.value = parse_built(rec.value);
}
if (rec.key == 'Packaged') {
rec.value = parse_packaged(rec.value);
}
desc[ rec.key ] = rec.value;
current = line;
}
}
function finisher() {
callback(null, desc);
}
descstream.on('data', reader);
descstream.on('end', finisher);
}
function parse_dep_string(str) {
return str.split(/,[\s]*/s).filter(function(str){
return str.trim(); //filter out empty strings
}).map(function(str){
return str.match(/\(.+\)/s) ?
{
package: normalize_ws(str.replace(/\(.+\)/s, '')),
version: normalize_ws(str.replace(/.*\((.+)\)/s, '$1'))
} : {
package: normalize_ws(str)
};
});
}
function parse_remotes(str) {
return str.split(/,[\s]*/);
}
function parse_built(str) {
var built = str.split(/;[\s]*/);
return {
R: built[0].replace(/^R /, ''),
Platform: built[1],
Date: built[2],
OStype: built[3]
};
}
function parse_packaged(str) {
var packaged = str.split(/;[\s]*/);
return {
Date: packaged[0],
User: packaged[1]
};
}
function split_record(str) {
var colon = str.indexOf(":");
return { 'key': str.substr(0, colon).trim(),
'value': str.substr(colon + 1).trim() };
}
function parse_desc_file(path, callback) {
var descstream = fs.createReadStream(path);
parse_desc_stream(descstream, callback)
}
function parse_tar_stream(descstream, callback) {
var extract = tar.extract();
var done = false;
extract.on('entry', function(header, tarstream, tarcb) {
tarstream.on('end', tarcb);
if (!done && header.name.match(/^[^\/]+\/DESCRIPTION$/)) {
done = true;
parse_desc_stream(tarstream, function(err, d) {
callback(err, d);
});
} else {
tarstream.resume();
}
});
extract.on('finish', function() {
if (!done) { callback('No DESCRIPTION file in tar archive'); }
})
extract.on('error', function(err) {
callback(err);
})
descstream
.pipe(gunzip())
.pipe(extract);
}
function parse_tar_file(path, callback) {
var descstream = fs.createReadStream(path);
parse_tar_stream(descstream, callback)
}
function parse_zip_stream(descstream, callback) {
descstream
.pipe(unzip.Parse())
.on('entry', function (entry) {
const fileName = entry.path;
if (fileName.match(/^[^\/]+\/DESCRIPTION$/)) {
parse_desc_stream(entry, function(err, cb) {
callback(err, cb);
});
} else {
entry.autodrain();
}
});
}
function parse_zip_file(path, callback) {
var descstream = fs.createReadStream(path);
parse_zip_stream(descstream, callback)
}
function parse_stream(descstream, callback) {
filetype.stream(descstream).then(function(x) {
if (x.fileType !== undefined) {
var mime = x.fileType.mime;
if (mime === "application/gzip" || mime === "application/x-tar") {
parse_tar_stream(x, callback);
} else if (mime = "application/zip") {
parse_zip_stream(x, callback);
} else {
parse_desc_stream(x, callback);
}
} else {
parse_desc_stream(x, callback)
}
})
}
function parse_file(path, callback) {
var descstream = fs.createReadStream(path);
parse_stream(descstream, callback);
}
parse_desc_stream.parse_desc_file = parse_desc_file;
parse_desc_stream.parse_file = parse_file;
parse_desc_stream.parse_stream = parse_stream;
parse_desc_stream.parse_tar_file = parse_tar_file;
parse_desc_stream.parse_tar_stream = parse_tar_stream;
parse_desc_stream.parse_zip_file = parse_zip_file;
parse_desc_stream.parse_zip_stream = parse_zip_stream;
parse_desc_stream.parse_dep_string = parse_dep_string;
module.exports = parse_desc_stream;