Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions packages/vovk-cli/src/dev/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export class VovkDev {
return this.#selfSignedDispatcher;
}

#requestSchema = debounceWithArgs(async (segmentName: string) => {
#requestSchemaNow = async (segmentName: string) => {
const result = await this.#fetchSchema(segmentName);
if (result.isError || result.isRefused) {
this.#failedSegmentNames.add(segmentName);
Expand All @@ -397,7 +397,9 @@ export class VovkDev {
this.#failedSegmentNames.delete(segmentName);
}
return result;
}, 500);
};

#requestSchema = debounceWithArgs(this.#requestSchemaNow, 500);

// a segment without a schema would hold back the client of the others; --exit generates from fresh schemas only
async #useLastKnownSchema(segmentName: string) {
Expand Down Expand Up @@ -632,32 +634,34 @@ export class VovkDev {
const MAX_ATTEMPTS = 5;
const DELAY = 5000;

setTimeout(() => {
setTimeout(async () => {
// one at a time: next dev compiles a segment on its first request, and two compilations at once can answer 500
const failedSegmentNames: string[] = [];
for (const { segmentName } of this.#segments) {
const { isError } = await this.#requestSchemaNow(segmentName);
if (isError) failedSegmentNames.push(segmentName);
}
for (const segmentName of failedSegmentNames) {
let attempts = 0;
void this.#requestSchema(segmentName).then(({ isError }) => {
if (isError) {
const interval = setInterval(() => {
attempts++;
if (attempts >= MAX_ATTEMPTS) {
clearInterval(interval);
log.error(
`Failed to request schema for ${formatLoggedSegmentName(segmentName)} after ${MAX_ATTEMPTS} attempts`
);
this.#failExitRun();
return;
}
void this.#requestSchema(segmentName).then(({ isError: isError2 }) => {
if (!isError2) {
clearInterval(interval);
log.info(
`Requested schema for ${formatLoggedSegmentName(segmentName)} after ${attempts} attempt${attempts === 1 ? '' : 's'}`
);
}
});
}, DELAY);
const interval = setInterval(() => {
attempts++;
if (attempts >= MAX_ATTEMPTS) {
clearInterval(interval);
log.error(
`Failed to request schema for ${formatLoggedSegmentName(segmentName)} after ${MAX_ATTEMPTS} attempts`
);
this.#failExitRun();
return;
}
});
void this.#requestSchema(segmentName).then(({ isError }) => {
if (!isError) {
clearInterval(interval);
log.info(
`Requested schema for ${formatLoggedSegmentName(segmentName)} after ${attempts} attempt${attempts === 1 ? '' : 's'}`
);
}
});
}, DELAY);
}
}, DELAY);

Expand Down
39 changes: 39 additions & 0 deletions packages/vovk-cli/test/spec/dev/dev-without-next.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ await describe('vovk dev in a project without Next.js', async () => {
}
});

await it('Requests the schema of one segment at a time when it starts', async () => {
// next dev compiles a segment on its first request, and two compilations at once can answer 500
const requestedSegments: string[] = [];
let inFlight = 0;
let maxInFlight = 0;
const server = http.createServer((req, res) => {
const segmentName = (req.url ?? '').replace(/^\/api\/?/, '').replace(/\/?_schema_$/, '');
requestedSegments.push(segmentName);
maxInFlight = Math.max(maxInFlight, ++inFlight);
setTimeout(() => {
inFlight--;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ schema: makeSegmentSchema(segmentName, `${segmentName || 'root'}RPC`) }));
}, 300);
});
await new Promise<void>((resolve) => server.listen(0, resolve));

try {
await createProject(projectDir, {
'package.json': { name: 'app', version: '1.0.0', type: 'module' },
'vovk.config.mjs': configFile({ composedClient: { prettifyClient: false } }),
'src/app/api/[[...vovk]]/route.ts': '',
'src/app/api/first/[[...vovk]]/route.ts': '',
'src/app/api/second/[[...vovk]]/route.ts': '',
});

const dev = startCLI(['dev', '--exit'], {
cwd: projectDir,
env: { PORT: String((server.address() as AddressInfo).port) },
});

assert.strictEqual(await dev.exitCode, 0, dev.getOutput());
assert.deepStrictEqual(requestedSegments.toSorted(), ['', 'first', 'second'], dev.getOutput());
assert.strictEqual(maxInFlight, 1, dev.getOutput());
} finally {
server.close();
}
});

await it('Names the schema URL and a Next.js basePath once when the schema request gets a 404', async () => {
// what a Next.js app with a basePath answers at /api/_schema_
const thirdRequest = Promise.withResolvers<void>();
Expand Down
Loading