Skip to content

Commit adff89d

Browse files
drgyHaroenv
authored andcommitted
feat(data): add jsDelivr hits (#263)
* Add jsDelivr hits * Removed interval for re-fetching data * Removed catch when loading jsDelivr hits * Record schema in README.md updated
1 parent 12d4ffa commit adff89d

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ For every single NPM package, we create a record in the Algolia index. The resul
4444
"downloadsLast30Days": 10978749,
4545
"downloadsRatio": 0.08310651682685861,
4646
"humanDownloadsLast30Days": "11m",
47+
"jsDelivrHits": 11684192,
4748
"popular": true,
4849
"version": "6.26.0",
4950
"versions": {

src/__tests__/__snapshots__/config.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Object {
8282
"type": "synonym",
8383
},
8484
],
85+
"jsDelivrHitsEndpoint": "https://data.jsdelivr.com/v1/stats/packages/month/all",
8586
"maxObjSize": 450000,
8687
"npmDownloadsEndpoint": "https://api.npmjs.org/downloads",
8788
"npmRegistryEndpoint": "https://replicate.npmjs.com/registry",

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ms from 'ms';
55
const defaultConfig = {
66
npmRegistryEndpoint: 'https://replicate.npmjs.com/registry',
77
npmDownloadsEndpoint: 'https://api.npmjs.org/downloads',
8+
jsDelivrHitsEndpoint: 'https://data.jsdelivr.com/v1/stats/packages/month/all',
89
maxObjSize: 450000,
910
popularDownloadsRatio: 0.005,
1011
appId: 'OFCNCOG2CU',

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import log from './log.js';
88
import ms from 'ms';
99
import cargo from 'async/cargo';
1010
import queue from 'async/queue';
11+
import { loadHits } from './jsDelivr';
1112

1213
log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰');
1314

@@ -96,6 +97,7 @@ async function bootstrap(state) {
9697

9798
if (state.bootstrapLastId) {
9899
log.info('⛷ Bootstrap: starting at doc %s', state.bootstrapLastId);
100+
await loadHits();
99101
return loop(state.bootstrapLastId);
100102
} else {
101103
const { taskID } = await client.deleteIndex(c.bootstrapIndexName);
@@ -105,6 +107,7 @@ async function bootstrap(state) {
105107
// first time this launches, we need to remember the last seq our bootstrap can trust
106108
await stateManager.save({ seq });
107109
await setSettings(bootstrapIndex);
110+
await loadHits();
108111
return loop(state.bootstrapLastId);
109112
}
110113

src/jsDelivr.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import got from 'got';
2+
import c from './config.js';
3+
4+
const hits = new Map();
5+
6+
function formatHits(pkg) {
7+
if (pkg.type !== 'npm') {
8+
return;
9+
}
10+
11+
hits.set(pkg.name, pkg.hits);
12+
}
13+
14+
export async function loadHits() {
15+
const hitsJSONpromise = got(c.jsDelivrHitsEndpoint, { json: true });
16+
const hitsJSON = (await hitsJSONpromise).body;
17+
hits.clear();
18+
hitsJSON.forEach(formatHits);
19+
}
20+
21+
export function getHits(pkgs) {
22+
return pkgs.map(({ name }) => ({ jsDelivrHits: hits.get(name) || 0 }));
23+
}

src/npm.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ export function info() {
1414
}));
1515
}
1616

17-
const logWarning = ({ error, type, packages }) => {
17+
const logWarning = ({ error, type, packagesStr }) => {
1818
log.warn(
19-
`Something went wrong asking the ${type} for \n${packages.join(
20-
','
21-
)} \n${error}`
19+
`Something went wrong asking the ${type} for \n${packagesStr} \n${error}`
2220
);
2321
};
2422

@@ -58,7 +56,7 @@ export async function getDownloads(pkgs) {
5856
logWarning({
5957
error,
6058
type: 'downloads',
61-
packages: pkgsNames,
59+
packagesStr: pkgsNames,
6260
});
6361
return { body: {} };
6462
})
@@ -72,7 +70,7 @@ export async function getDownloads(pkgs) {
7270
logWarning({
7371
error,
7472
type: 'scoped downloads',
75-
packages: [pkg],
73+
packagesStr: pkg,
7674
});
7775
return { body: {} };
7876
})

src/saveDocs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import formatPkg from './formatPkg.js';
22
import log from './log.js';
33
import { getDownloads, getDependents } from './npm.js';
44
import { getChangelogs } from './changelog.js';
5+
import { getHits } from './jsDelivr';
56

67
export default function saveDocs({ docs, index }) {
78
const rawPkgs = docs
@@ -24,17 +25,20 @@ function addMetaData(pkgs) {
2425
getDownloads(pkgs),
2526
getDependents(pkgs),
2627
getChangelogs(pkgs),
27-
]).then(([downloads, dependents, changelogs]) =>
28+
getHits(pkgs),
29+
]).then(([downloads, dependents, changelogs, hits]) =>
2830
pkgs.map((pkg, index) => ({
2931
...pkg,
3032
...downloads[index],
3133
...dependents[index],
3234
...changelogs[index],
35+
...hits[index],
3336
_searchInternal: {
3437
...pkg._searchInternal,
3538
...downloads[index]._searchInternal,
3639
...dependents[index]._searchInternal,
3740
...changelogs[index]._searchInternal,
41+
...hits[index]._searchInternal,
3842
},
3943
}))
4044
);

0 commit comments

Comments
 (0)