Skip to content

Commit

Permalink
Fix empty response body when streaming (#682)
Browse files Browse the repository at this point in the history
## Summary
Fix a bug where streaming would send back an empty body. This happened
because we stringified an unawaited `Promise` within `createStream`

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [ ] ~Added a [docs PR](https://github.com/inngest/website) that
references this PR~ N/A Bug fix
- [x] Added unit/integration tests
- [x] Added changesets if applicable

---------

Co-authored-by: Jack Williams <[email protected]>
  • Loading branch information
amh4r and jpwilliams authored Aug 21, 2024
1 parent a3c85d7 commit 2019fe2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-elephants-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Fix empty response body when streaming
8 changes: 8 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ jobs:
fail-fast: false
matrix:
example: ${{ fromJson(needs.examples-matrix.outputs.matrix) }}
streaming: [""]
include:
- example: framework-remix
streaming: force
- example: framework-nextjs-app-router
streaming: force
steps:
# Checkout the repo
- name: Checkout SDK
Expand All @@ -204,3 +210,5 @@ jobs:

- name: Run integration tests
run: pnpm run itest ${{ matrix.example }}
env:
INNGEST_STREAMING: ${{ matrix.streaming }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { functions, inngest } from "@/inngest";
import { serve } from "inngest/next";

/**
* Try to automatically choose the edge runtime if `INNGEST_STREAMING` is set.
*
* See https://innge.st/streaming.
*/
export const runtime =
process.env.INNGEST_STREAMING?.toLowerCase() === "force" ? "edge" : "nodejs";

export const { GET, POST, PUT } = serve({
client: inngest,
functions,
Expand Down
10 changes: 8 additions & 2 deletions packages/inngest/src/helpers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ export const createStream = (opts?: {

const finalize = (data: unknown) => {
clearInterval(heartbeat);
controller.enqueue(encoder.encode(stringify(data)));
controller.close();

// `data` may be a `Promise`. If it is, we need to wait for it to
// resolve before sending it. To support this elegantly we'll always
// assume it's a promise and handle that case.
void Promise.resolve(data).then((resolvedData) => {
controller.enqueue(encoder.encode(stringify(resolvedData)));
controller.close();
});
};

passFinalize(finalize);
Expand Down

0 comments on commit 2019fe2

Please sign in to comment.