-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
257 lines (229 loc) · 7.71 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { buildChart } from './barchart.js';
const npmDownloadStatsBaseUrl = `https://api.npmjs.org/downloads/point/`;
const timeRanges = [
'last-day',
'last-week',
'last-month',
'last-year'
];
/**
* @typedef {Object} NpmPackageInfo
* @property {string} package
* @property {number} downloads
* @property {string} start
* @property {string} end
*/
/**
* @typedef {Object} PackageInfo
* @property {NpmPackageInfo[]} last-day
* @property {NpmPackageInfo[]} last-week
* @property {NpmPackageInfo[]} last-month
* @property {NpmPackageInfo[]} last-year
*/
/**
* @typedef {Object} PackageInfoCharts
* @property {string} last-day
* @property {string} last-week
* @property {string} last-month
* @property {string} last-year
*/
/**
* @typedef {Object} PackageInfoChartsResult
* @property {PackageInfoCharts} charts
* @property {string} error
*/
async function getFetch() {
// If we are in a browser, we will just use the
// native window.fetch function
//
// If we are in Node.js, we will use node-fetch
//
// This isn't fool proof, but should work for most cases
if (typeof window === 'object' &&
typeof window.fetch === 'function') {
return window.fetch;
} else {
return (await import('node-fetch')).default;
}
}
/**
* @param {string} packageName
* @returns {boolean}
*/
function isScopedPackage(packageName) {
return (packageName || '')[0] === '@';
}
/**
* @param {any} packageInfo
* @returns {boolean}
*/
function isPackageInfo(packageInfo) {
return (
typeof packageInfo === 'object' &&
typeof packageInfo.downloads === 'number' &&
typeof packageInfo.start === 'string' &&
typeof packageInfo.end === 'string' &&
typeof packageInfo.package === 'string'
);
}
/**
* @param {string|string[]} packageNames
* @returns {string[]}
*/
function normalizeNpmPackageNames(packageNames) {
if (Array.isArray(packageNames)) {
return packageNames;
}
return [packageNames];
}
/**
* @returns {PackageInfo}
*/
function normalizeNpmPackageInfo(packageInfo) {
const normalizedPackageInfo = {};
timeRanges
.forEach(timeRange => {
const packageInfoForTimeRange = packageInfo[timeRange];
if (isPackageInfo(packageInfoForTimeRange)) {
normalizedPackageInfo[timeRange] = [packageInfoForTimeRange];
} else {
normalizedPackageInfo[timeRange] = Object.values(packageInfoForTimeRange);
}
});
return normalizedPackageInfo;
}
/**
* @param {PackageInfo} bulkPackageInfo
* @param {PackageInfo} individualPackageInfo
* @returns {PackageInfo}
*/
function mergeNpmPackageInfo(bulkPackageInfo, individualPackageInfo) {
if (!bulkPackageInfo) {
bulkPackageInfo = {};
}
if (!individualPackageInfo) {
individualPackageInfo = {};
}
const mergedPackageInfo = {};
timeRanges
.forEach(timeRange => {
mergedPackageInfo[timeRange] = []
.concat(bulkPackageInfo[timeRange] || [])
.concat(individualPackageInfo[timeRange] || [])
.filter(packageInfo => !!packageInfo);
});
return mergedPackageInfo;
}
/**
* @param {string} packageName
* @returns {Promise<PackageInfo>}
*/
async function _getNpmPackageInfo(packageName) {
try {
const fetch = await getFetch();
const downloadStatsRequestUrls = timeRanges
.map(timeRange => `${npmDownloadStatsBaseUrl}${timeRange}/${packageName}`);
const downloadStatsRequestPromises = downloadStatsRequestUrls
.map((downloadStatsRequestUrl, i) =>
fetch(downloadStatsRequestUrl)
.then(response => response.json())
.then(result => ({
[timeRanges[i]]: result,
})));
const downloadStatsRequestResults = await Promise.all(downloadStatsRequestPromises);
return normalizeNpmPackageInfo(downloadStatsRequestResults
.reduce((downloadStatsResults, downloadStatsResult) => ({
...downloadStatsResults,
...downloadStatsResult,
}), {}));
} catch (err) {
return null;
}
}
/**
* @param {string[]} packageNames
* @returns {Promise<PackageInfo>}
*/
async function _getBulkNpmPackageInfo(packageNames) {
if (packageNames.length <= 0) {
return null;
}
// construct a "package name" from all the packageNames provided and leverage
// the _getNpmPackageInfo utility function above
const bulkPackageName = packageNames.join(',');
return await _getNpmPackageInfo(bulkPackageName);
}
/**
* @param {string|string[]} packageNames
* @returns {Promise<PackageInfo>}
*/
export async function getNpmPackageInfo(packageNames) {
const scopedPackages = [];
const nonScopedPackages = [];
// splitting scoped vs. non-scoped here
// since non-scoped packages support bulk queries
// while scoped packages do not
// https://github.com/npm/registry/blob/master/docs/download-counts.md#bulk-queries
normalizeNpmPackageNames(packageNames)
.forEach(packageName => {
if (isScopedPackage(packageName)) {
scopedPackages.push(packageName);
} else {
nonScopedPackages.push(packageName);
}
});
const bulkPackageInfoPromise = _getBulkNpmPackageInfo(nonScopedPackages);
const individualPackageInfoPromises = scopedPackages
.map(scopedPackage => _getNpmPackageInfo(scopedPackage));
const [
bulkPackageResults,
individualPackageResults
] = await Promise.all([
bulkPackageInfoPromise,
...individualPackageInfoPromises
]);
return mergeNpmPackageInfo(bulkPackageResults, individualPackageResults);
}
/**
* @param {string|string[]} packageNames
* @returns {Promise<PackageInfoChartsResult>}
*/
export async function buildNpmPackageInfoCharts(packageNames) {
const result = {
charts: [],
error: '',
};
try {
const npmPackageInfoResults = await getNpmPackageInfo(packageNames);
const packageNamesSuccessfullyFetched = new Set(npmPackageInfoResults[timeRanges[0]]
.map(packageInfo => packageInfo.package));
const packageNamesFailedToFetch = packageNames.filter(packageName => !packageNamesSuccessfullyFetched.has(packageName));
// Yay! We got package data, let's form a pretty chart string
if (packageNamesSuccessfullyFetched.size > 0) {
timeRanges
.forEach(timeRange => {
let startDate, endDate;
const labels = [];
const values = [];
npmPackageInfoResults[timeRange]
.forEach(packageInfo => {
if (!startDate || !endDate) {
startDate = packageInfo.start;
endDate = packageInfo.end;
}
labels.push(packageInfo.package);
values.push(packageInfo.downloads);
});
const title = `Number of downloads in ${timeRange} (${startDate} - ${endDate})`;
result.charts[timeRange] = buildChart(labels, values, 50, title);
});
}
// Inform about packages we failed to info for
if (packageNamesFailedToFetch.length > 0) {
result.error = `Failed to get download stats for the following packages: ${packageNamesFailedToFetch.join(', ')}`;
}
} catch (err) {
result.error = `An error occurred while attempting to collect npm package info: ${err}`;
}
return result;
}