create-app: fix fetchYarnLockSeedTask timeout + add test

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-02 12:43:28 +01:00
parent 58c0f2bcf0
commit e80a7338df
4 changed files with 21 additions and 1 deletions
+15
View File
@@ -471,5 +471,20 @@ describe('tasks', () => {
expect(mockDir.content()).toEqual({});
});
it('should time out if it takes too long to fetch', async () => {
worker.use(
rest.get(
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
(_, res, ctx) => res(ctx.delay(5000)),
),
);
mockDir.clear();
await expect(fetchYarnLockSeedTask(mockDir.path)).resolves.toBe(false);
expect(mockDir.content()).toEqual({});
});
});
});
+4 -1
View File
@@ -25,6 +25,7 @@ import {
resolve as resolvePath,
relative as relativePath,
} from 'path';
import fetch from 'node-fetch';
import { exec as execCb } from 'child_process';
import { packageVersions } from './versions';
import { promisify } from 'util';
@@ -317,13 +318,15 @@ export async function fetchYarnLockSeedTask(dir: string) {
try {
await Task.forItem('fetching', 'yarn.lock seed', async () => {
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
const timeout = setTimeout(() => controller.abort(), 3000);
const res = await fetch(
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
{
signal: controller.signal,
},
);
clearTimeout(timeout);
if (!res.ok) {
throw new Error(
`Request failed with status ${res.status} ${res.statusText}`,