Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): add waitUntil to the server context in Fastify integration #1926

Merged
merged 2 commits into from
Dec 24, 2024
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
8 changes: 8 additions & 0 deletions .changeset/blue-pugs-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@whatwg-node/server': patch
---

While calling `handleNodeRequest` or `handleNodeRequestAndResponse`, `waitUntil` is not added automatically as in `requestListener` for Node.js integration.
This change adds `waitUntil` into the `serverContext` if not present.

Fixes the issue with Fastify integration that uses the mentioned methods
28 changes: 17 additions & 11 deletions packages/server/src/createServerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,19 @@ function createServerAdapter<

function waitUntil(promiseLike: PromiseLike<unknown>) {
// Ensure that the disposable stack is created
ensureDisposableStack();
waitUntilPromises.add(promiseLike);
promiseLike.then(
() => {
waitUntilPromises.delete(promiseLike);
},
err => {
console.error(`Unexpected error while waiting: ${err.message || err}`);
waitUntilPromises.delete(promiseLike);
},
);
if (isPromise(promiseLike)) {
ensureDisposableStack();
waitUntilPromises.add(promiseLike);
promiseLike.then(
() => {
waitUntilPromises.delete(promiseLike);
},
err => {
console.error(`Unexpected error while waiting: ${err.message || err}`);
waitUntilPromises.delete(promiseLike);
},
);
}
}

if (options?.plugins != null) {
Expand Down Expand Up @@ -250,6 +252,10 @@ function createServerAdapter<
// TODO: Remove this on the next major version
function handleNodeRequest(nodeRequest: NodeRequest, ...ctx: Partial<TServerContext>[]) {
const serverContext = ctx.length > 1 ? completeAssign(...ctx) : ctx[0] || {};
// Ensure `waitUntil` is available in the server context
if (!serverContext.waitUntil) {
serverContext.waitUntil = waitUntil;
}
const request = normalizeNodeRequest(nodeRequest, fetchAPI);
return handleRequest(request, serverContext);
}
Expand Down
27 changes: 27 additions & 0 deletions packages/server/test/fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,33 @@ describe('Fastify', () => {
reqBody: 'TEST',
});
});

it('handles waitUntil', async () => {
const backgroundJob$ = createDeferredPromise<void>();
let backgroundJobDone = false;
backgroundJob$.promise.finally(() => {
backgroundJobDone = true;
});
serverAdapter = createServerAdapter((_request: Request, ctx) => {
ctx.waitUntil(backgroundJob$.promise);
return new Response('OK');
});
const res = await fastifyServer.inject({
url: '/mypath',
});
expect(res.body).toEqual('OK');
const dispose$ = Promise.resolve(serverAdapter.dispose());
let disposeDone = false;
dispose$.then(() => {
disposeDone = true;
});
expect(backgroundJobDone).toBe(false);
expect(disposeDone).toBe(false);
backgroundJob$.resolve();
await dispose$;
expect(backgroundJobDone).toBe(true);
expect(disposeDone).toBe(true);
});
},
);
});
Loading