Skip to content

Commit

Permalink
fix(server): add waitUntil to the server context in Fastify integra…
Browse files Browse the repository at this point in the history
…tion (#1926)
  • Loading branch information
ardatan authored Dec 24, 2024
1 parent fb734f8 commit bae5de1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
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);
});
},
);
});

0 comments on commit bae5de1

Please sign in to comment.