Skip to content

Commit 2470621

Browse files
author
烫宝
committed
fix: 修复revalidate出错
1 parent 6664189 commit 2470621

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

app/core/service/BinarySyncerService.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ export class BinarySyncerService extends AbstractService {
250250
for (const item of existsItems) {
251251
existsMap.set(item.name, item);
252252
}
253+
const latestItem = dir.endsWith(latestVersionParent) ?
254+
sortBy(fetchItems, item => {
255+
return new Date(item.date).getTime();
256+
}).pop() : null;
253257
const diffItems: { item: Binary; reason: string }[] = [];
254258
for (const item of fetchItems) {
255259
const existsItem = existsMap.get(item.name);
@@ -276,7 +280,7 @@ export class BinarySyncerService extends AbstractService {
276280
existsItem.ignoreDownloadStatuses = item.ignoreDownloadStatuses;
277281
existsItem.date = item.date;
278282
} else if (dir.endsWith(latestVersionParent)) {
279-
const isLatestItem = sortBy(fetchItems, [ 'date' ]).pop()?.name === item.name;
283+
const isLatestItem = latestItem?.name === item.name;
280284
if (isLatestItem && existsItem.isDir) {
281285
diffItems.push({
282286
item: existsItem,

test/core/service/BinarySyncerService/executeTask.test.ts

+40-3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
288288
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
289289
persist: false,
290290
});
291+
app.mockHttpclient('https://nodejs.org/dist/latest/docs/apilinks_old.json', 'GET', {
292+
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
293+
persist: false,
294+
});
295+
app.mockHttpclient('https://nodejs.org/dist/latest/docs/apilinks_old2.json', 'GET', {
296+
data: await TestUtil.readFixturesFile('nodejs.org/site/latest/docs/apilinks.json'),
297+
persist: false,
298+
});
291299
await binarySyncerService.createTask('node', {});
292300
let task = await binarySyncerService.findExecuteTask();
293301
assert(task);
@@ -296,10 +304,39 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
296304
return {
297305
items: [
298306
{ name: 'latest/', isDir: true, url: '', size: '-', date: '17-Dec-2021 23:17' },
307+
{ name: 'old/', isDir: true, url: '', size: '-', date: '15-Dec-2021 23:17' },
308+
// old2 使用 yyyy-mm-dd 日期格式,用于检查 diff 的日期排序
309+
{ name: 'old2/', isDir: true, url: '', size: '-', date: '2021-11-10T05:49:35.321Z' },
299310
{ name: 'index.json', isDir: false, url: 'https://nodejs.org/dist/index.json', size: '219862', date: '17-Dec-2021 23:16' },
300311
],
301312
};
302313
}
314+
if (dir === '/old/') {
315+
return {
316+
items: [
317+
{
318+
name: 'apilinks_old.json',
319+
isDir: false,
320+
url: 'https://nodejs.org/dist/latest/docs/apilinks_old.json',
321+
size: '61606',
322+
date: '17-Dec-2021 21:29',
323+
},
324+
],
325+
};
326+
}
327+
if (dir === '/old2/') {
328+
return {
329+
items: [
330+
{
331+
name: 'apilinks_old2.json',
332+
isDir: false,
333+
url: 'https://nodejs.org/dist/latest/docs/apilinks_old2.json',
334+
size: '61606',
335+
date: '17-Dec-2021 21:29',
336+
},
337+
],
338+
};
339+
}
303340
if (dir === '/latest/') {
304341
return {
305342
items: [
@@ -324,7 +361,7 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
324361
assert(stream);
325362
let log = await TestUtil.readStreamToLog(stream);
326363
// console.log(log);
327-
assert(log.includes('Syncing diff: 2 => 2'));
364+
assert(log.includes('Syncing diff: 4 => 4'));
328365
assert(log.includes('[/] 🟢 Synced dir success'));
329366
assert(log.includes('[/latest/] 🟢 Synced dir success'));
330367
assert(log.includes('[/latest/docs/] 🟢 Synced dir success'));
@@ -339,9 +376,9 @@ describe('test/core/service/BinarySyncerService/executeTask.test.ts', () => {
339376
log = await TestUtil.readStreamToLog(stream);
340377
// console.log(log);
341378
assert(log.includes('reason: revalidate latest version'));
342-
assert(log.includes('Syncing diff: 2 => 1'));
379+
assert(log.includes('Syncing diff: 4 => 1'));
343380
assert(log.includes('[/] 🟢 Synced dir success'));
344-
381+
assert(log.includes('[/latest/] 🟢 Synced dir success'));
345382
// mock version change
346383
// console.log(binaryRepository.findBinary('node'));
347384

0 commit comments

Comments
 (0)