-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (185 loc) · 5.18 KB
/
index.js
File metadata and controls
215 lines (185 loc) · 5.18 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import rConstants from "r-constants";
import byline from "byline";
import fs from "node:fs";
import {fileTypeStream} from 'file-type';
import tar from "tar-stream";
import zlib from "node:zlib";
import unzip from "unzipper";
import stream from "node:stream";
const deps = rConstants.dependency_types;
function normalize_ws(x) {
return x.trim().replace(/\s/g, " ");
}
export function parse_desc_stream(descstream) {
return new Promise((resolve, reject) => {
var lines = byline(descstream, { keepEmptyLines: true });
var desc = {};
var current = "";
var first = true;
lines.setEncoding("utf8");
function reader(line) {
// First line is special
if (first) {
current = line;
first = false;
// Starts with space, same record, append
} else if(line.startsWith("#")){
// comment
} 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
lines.removeListener("data", reader);
lines.removeListener("end", finisher);
return reject(new Error("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() {
resolve(desc);
}
lines.on("data", reader);
lines.on("end", finisher);
});
}
export 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(),
};
}
export function parse_desc_file(path) {
var descstream = fs.createReadStream(path);
return parse_desc_stream(descstream);
}
function parse_raw_tar_stream(input) {
return new Promise((resolve, reject) => {
var extract = tar.extract();
var done = false;
extract.on("entry", function (header, tarstream, tarcb) {
tarstream.on("end", tarcb);
tarstream.on("error", function (err) {
done = true;
reject(err);
});
if (!done && header.name.match(/^[^\/]+\/DESCRIPTION$/)) {
done = true;
parse_desc_stream(tarstream).then(resolve).catch(reject);
} else {
tarstream.resume();
}
});
input.on("error", reject);
extract.on("error", reject);
extract.on("finish", function () {
if (!done) {
reject(new Error("No DESCRIPTION file in tar archive"));
}
});
input.pipe(extract);
});
}
function parse_zip_stream(input) {
return new Promise((resolve, reject) => {
var done = false;
var extract = input.pipe(unzip.Parse());
extract.on("entry", function (entry) {
const fileName = entry.path;
if (fileName.match(/^[^\/]+\/DESCRIPTION$/)) {
done = true;
parse_desc_stream(entry).then(resolve).catch(reject);
} else {
entry.autodrain();
}
});
input.on("error", reject);
extract.on("error", reject);
extract.on("finish", function () {
if (!done) {
reject(new Error("No DESCRIPTION file in zip file"));
}
});
});
}
export function parse_stream(descstream) {
return fileTypeStream(descstream).then(function (x) {
// For no type it is assumed a plain text file
if (x.fileType === undefined) {
return parse_desc_stream(x);
}
// The type of file
var mime = x.fileType.mime;
// .zip file
if (mime === "application/zip") {
return parse_zip_stream(x);
}
// .tar.gz file
if (mime === "application/gzip") {
return parse_raw_tar_stream(x.pipe(zlib.createGunzip()));
}
if (mime === "application/zstd") {
return parse_raw_tar_stream(x.pipe(zlib.createZstdDecompress()));
}
// Default is assuming no compression
return parse_raw_tar_stream(x);
});
}
export function parse_file(path) {
var descstream = fs.createReadStream(path);
return parse_stream(descstream);
}