-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
108 lines (96 loc) · 2.52 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
const NpmClient = require("npm-registry-client");
const api = new NpmClient();
const url = require("url");
const fs = require("fs");
const requestsPerSecond = 30;
let timeouts = [];
let download429 = false;
getDependents("marked");
function getDependents(packageName) {
// create dir if not exists
if (!fs.existsSync("./packages")) {
fs.mkdirSync("./packages");
}
const uri = url.format({
protocol: "https",
host: "skimdb.npmjs.com",
pathname: "/registry/_design/app/_view/dependedUpon",
query: {
group_level: 2,
startkey: JSON.stringify([packageName]),
endkey: JSON.stringify([packageName, {}]),
state: "update_after"
}
});
api.get(uri, {}, (err, data) => {
const packages = data.rows.map(r => r.key[1]);
console.log(`total dependents: ${packages.length}`);
let count = 0;
packages.forEach((name) => {
if (!fs.existsSync(`./packages/${name}.json`)) {
timeouts.push(setTimeout(() => {
getInfo(name, packageName);
}, 1000 / requestsPerSecond * count++));
}
});
});
}
function getInfo(name, packageName) {
if (download429) {
return;
}
let json = null;
let downloads = null;
api.get(`https://skimdb.npmjs.com/registry/${name}`, {}, (err, data) => {
if (err) {
console.error(`json connection error: ${name}`);
return;
}
json = data;
if (downloads) {
parseJson(json, downloads, name, packageName);
}
});
api.get(`https://api.npmjs.org/downloads/point/last-week/${name}`, {}, (err, data, raw, res) => {
if (err) {
console.error(`downloads connection error: ${name}`);
return;
}
if (res.statusCode !== 200) {
if (res.statusCode === 429) {
download429 = true;
if (timeouts) {
timeouts.forEach((t) => clearTimeout(t));
timeouts = null;
}
}
console.error(`downloads error ${res.statusCode}: ${name}`);
if (res.statusCode === 404) {
downloads = {downloads: 0};
if (json) {
parseJson(json, downloads, name, packageName);
}
}
return;
}
downloads = data;
if (json) {
parseJson(json, downloads, name, packageName);
}
});
}
function parseJson(json, downloads, name, packageName) {
const latest = json["dist-tags"].latest;
const pkg = json.versions[latest];
const info = {
name: pkg.name,
version: latest,
depVersion: pkg.dependencies[packageName],
maintainers: json.maintainers,
modified: json.time.modified,
released: json.time[latest],
downloadsLastWeek: downloads.downloads,
};
fs.writeFileSync(`./packages/${name}.json`, JSON.stringify(info, null, "\t"));
console.log(`Saved ${name}`);
}