Skip to content

Commit

Permalink
fix: add expiresAt field (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bodin committed Jul 5, 2021
1 parent 8ef8425 commit dba5d2a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/@types/pkg.ts
Expand Up @@ -75,6 +75,7 @@ export interface RawPkg {
moduleTypes: ModuleType[];
lastCrawl: string;
_searchInternal: {
expiresAt: string;
alternativeNames: string[];
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/__snapshots__/formatPkg.test.ts.snap
Expand Up @@ -8,6 +8,7 @@ Object {
"0.js",
"0js",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -721,6 +722,7 @@ Object {
"@atlaskit/inputjs",
"@atlaskit/input",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -921,6 +923,7 @@ Object {
"@atomic-package/tabjs",
"@atomic-package/tab",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1043,6 +1046,7 @@ Object {
"create-instantsearch-appjs",
"create-instantsearch-app",
],
"expiresAt": Any<String>,
},
"bin": Object {
"create-instantsearch-app": "src/cli/index.js",
Expand Down Expand Up @@ -1169,6 +1173,7 @@ Object {
"indexof.js",
"indexofjs",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1255,6 +1260,7 @@ Object {
"prismjs",
"prism",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down Expand Up @@ -1459,6 +1465,7 @@ Object {
"long-boyjs",
"long-boy",
],
"expiresAt": Any<String>,
},
"bin": Object {},
"computedKeywords": Array [],
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/formatPkg.test.ts
Expand Up @@ -62,6 +62,9 @@ it('transforms correctly', () => {
expect(formattedPackage).toMatchSnapshot(
{
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
},
},
formattedPackage.objectID
)
Expand Down Expand Up @@ -100,6 +103,9 @@ it('truncates long readmes', () => {
expect(formatted).toMatchSnapshot({
readme: expect.any(String),
lastCrawl: expect.any(String),
_searchInternal: {
expiresAt: expect.any(String),
},
});
});

Expand Down Expand Up @@ -720,6 +726,9 @@ describe('deprecated', () => {
deprecated: 'Yes this is deprecated',
isDeprecated: true,
deprecatedReason: 'Yes this is deprecated',
_searchInternal: {
expiresAt: expect.any(String),
},
});
});
});
4 changes: 4 additions & 0 deletions src/config.ts
Expand Up @@ -19,6 +19,8 @@ const indexSettings: Settings = {
'isDeprecated',
'types.ts',
'moduleTypes',
'popular',
'_searchInternal.expiresAt',
],
customRanking: [
'desc(_searchInternal.downloadsMagnitude)',
Expand Down Expand Up @@ -168,6 +170,8 @@ export const config = {
indexSettings,
indexSynonyms,
indexRules,
expiresAt: ms('30 days'),
popularExpiresAt: ms('7 days'),
};

export type Config = typeof config;
Expand Down
3 changes: 3 additions & 0 deletions src/formatPkg.ts
Expand Up @@ -177,6 +177,9 @@ export default function formatPkg(pkg: GetPackage): RawPkg | undefined {
lastCrawl: new Date().toISOString(),
_searchInternal: {
alternativeNames,
expiresAt: new Date(Date.now() + config.expiresAt)
.toISOString()
.split('T')[0],
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/npm/__tests__/index.test.ts
Expand Up @@ -152,6 +152,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: 'jest',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
},
}),
expect.objectContaining({
Expand All @@ -162,6 +163,7 @@ describe('getDownloads()', () => {
_searchInternal: {
popularName: '@angular/core',
downloadsMagnitude: expect.any(Number),
expiresAt: expect.any(String),
},
}),
expect.objectContaining({
Expand Down
8 changes: 7 additions & 1 deletion src/npm/index.ts
Expand Up @@ -224,6 +224,7 @@ async function getDownloads(pkgs: Array<Pick<RawPkg, 'name'>>): Promise<
downloadsRatio: number;
popular: boolean;
_searchInternal: {
expiresAt?: string;
popularName?: string;
downloadsMagnitude: number;
};
Expand Down Expand Up @@ -289,7 +290,12 @@ async function getDownloads(pkgs: Array<Pick<RawPkg, 'name'>>): Promise<
// if the package is popular, we copy its name to a dedicated attribute
// which will make popular records' `name` matches to be ranked higher than other matches
// see the `searchableAttributes` index setting
...(popular && { popularName: name }),
...(popular && {
popularName: name,
expiresAt: new Date(Date.now() + config.popularExpiresAt)
.toISOString()
.split('T')[0],
}),
downloadsMagnitude,
},
};
Expand Down

0 comments on commit dba5d2a

Please sign in to comment.