Skip to content

Commit

Permalink
fix: 修复revalidate出错
Browse files Browse the repository at this point in the history
  • Loading branch information
烫宝 committed Mar 13, 2024
1 parent 6664189 commit 2470621
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/core/service/BinarySyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ export class BinarySyncerService extends AbstractService {
for (const item of existsItems) {
existsMap.set(item.name, item);
}
const latestItem = dir.endsWith(latestVersionParent) ?
sortBy(fetchItems, item => {
return new Date(item.date).getTime();
}).pop() : null;
const diffItems: { item: Binary; reason: string }[] = [];
for (const item of fetchItems) {
const existsItem = existsMap.get(item.name);
Expand All @@ -276,7 +280,7 @@ export class BinarySyncerService extends AbstractService {
existsItem.ignoreDownloadStatuses = item.ignoreDownloadStatuses;
existsItem.date = item.date;
} else if (dir.endsWith(latestVersionParent)) {
const isLatestItem = sortBy(fetchItems, [ 'date' ]).pop()?.name === item.name;
const isLatestItem = latestItem?.name === item.name;
if (isLatestItem && existsItem.isDir) {
diffItems.push({
item: existsItem,
Expand Down
43 changes: 40 additions & 3 deletions test/core/service/BinarySyncerService/executeTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
persist: false,
});
app.mockHttpclient('https://nodejs.org/dist/latest/docs/apilinks_old.json', 'GET', {
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
persist: false,
});
app.mockHttpclient('https://nodejs.org/dist/latest/docs/apilinks_old2.json', 'GET', {
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
persist: false,
});
await binarySyncerService.createTask('node', {});
let task = await binarySyncerService.findExecuteTask();
assert(task);
Expand All @@ -296,10 +304,39 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
return {
items: [
{ name: 'latest/', isDir: true, url: '', size: '-', date: '17-Dec-2021 23:17' },
{ name: 'old/', isDir: true, url: '', size: '-', date: '15-Dec-2021 23:17' },
// old2 使用 yyyy-mm-dd 日期格式,用于检查 diff 的日期排序
{ name: 'old2/', isDir: true, url: '', size: '-', date: '2021-11-10T05:49:35.321Z' },
{ name: 'index.json', isDir: false, url: 'https://nodejs.org/dist/index.json', size: '219862', date: '17-Dec-2021 23:16' },
],
};
}
if (dir === '/old/') {
return {
items: [
{
name: 'apilinks_old.json',
isDir: false,
url: 'https://nodejs.org/dist/latest/docs/apilinks_old.json',
size: '61606',
date: '17-Dec-2021 21:29',
},
],
};
}
if (dir === '/old2/') {
return {
items: [
{
name: 'apilinks_old2.json',
isDir: false,
url: 'https://nodejs.org/dist/latest/docs/apilinks_old2.json',
size: '61606',
date: '17-Dec-2021 21:29',
},
],
};
}
if (dir === '/latest/') {
return {
items: [
Expand All @@ -324,7 +361,7 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
assert(stream);
let log = await TestUtil.readStreamToLog(stream);
// console.log(log);
assert(log.includes('Syncing diff: 2 => 2'));
assert(log.includes('Syncing diff: 4 => 4'));
assert(log.includes('[/] 🟢 Synced dir success'));
assert(log.includes('[/latest/] 🟢 Synced dir success'));
assert(log.includes('[/latest/docs/] 🟢 Synced dir success'));
Expand All @@ -339,9 +376,9 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
log = await TestUtil.readStreamToLog(stream);
// console.log(log);
assert(log.includes('reason: revalidate latest version'));
assert(log.includes('Syncing diff: 2 => 1'));
assert(log.includes('Syncing diff: 4 => 1'));
assert(log.includes('[/] 🟢 Synced dir success'));

assert(log.includes('[/latest/] 🟢 Synced dir success'));
// mock version change
// console.log(binaryRepository.findBinary('node'));

Expand Down

0 comments on commit 2470621

Please sign in to comment.