-
Notifications
You must be signed in to change notification settings - Fork 10k
[DO] Adding that ReadableStream cancellation propagates from Worker to DO #23478
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/content/docs/durable-objects/examples/readable-stream.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| --- | ||
| type: example | ||
| summary: Stream ReadableStream from Durable Objects. | ||
| pcx_content_type: example | ||
| title: Use ReadableStream with Durable Object and Workers | ||
| sidebar: | ||
| order: 3 | ||
| description: Stream ReadableStream from Durable Objects. | ||
| --- | ||
|
|
||
| import { GlossaryTooltip, WranglerConfig, TypeScriptExample } from "~/components"; | ||
|
|
||
| This example demonstrates: | ||
|
|
||
| - A Worker receives a request, and forwards it to a Durable Object `my-id`. | ||
| - The Durable Object streams an incrementing number every second, until it receives `AbortSignal`. | ||
| - The Worker reads and logs the values from the stream. | ||
| - The Worker then cancels the stream after 5 values. | ||
|
|
||
| <TypeScriptExample> | ||
| ```ts | ||
| import { DurableObject } from 'cloudflare:workers'; | ||
|
|
||
| // Send incremented counter value every second | ||
| async function* dataSource(signal: AbortSignal) { | ||
Oxyjun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let counter = 0; | ||
| while (!signal.aborted) { | ||
| yield counter++; | ||
| await new Promise((resolve) => setTimeout(resolve, 1_000)); | ||
| } | ||
|
|
||
| console.log('Data source cancelled'); | ||
| } | ||
|
|
||
| export class MyDurableObject extends DurableObject<Env> { | ||
| async fetch(request: Request): Promise<Response> { | ||
| const abortController = new AbortController(); | ||
|
|
||
| const stream = new ReadableStream({ | ||
| async start(controller) { | ||
| if (request.signal.aborted) { | ||
| controller.close(); | ||
| abortController.abort(); | ||
| return; | ||
| } | ||
|
|
||
| for await (const value of dataSource(abortController.signal)) { | ||
| controller.enqueue(new TextEncoder().encode(String(value))); | ||
| } | ||
| }, | ||
| cancel() { | ||
| console.log('Stream cancelled'); | ||
| abortController.abort(); | ||
| }, | ||
| }); | ||
|
|
||
| const headers = new Headers({ | ||
| 'Content-Type': 'application/octet-stream', | ||
| }); | ||
|
|
||
| return new Response(stream, { headers }); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('my-id'); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id); | ||
| const response = await stub.fetch(request, { ...request }); | ||
| if (!response.ok || !response.body) { | ||
| return new Response('Invalid response', { status: 500 }); | ||
| } | ||
|
|
||
| const reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); | ||
|
|
||
| let data = [] as string[]; | ||
| let i = 0; | ||
| while (true) { | ||
| // Cancel the stream after 5 messages | ||
| if (i > 5) { | ||
| reader.cancel(); | ||
| break; | ||
| } | ||
| const { value, done } = await reader.read(); | ||
|
|
||
| if (value) { | ||
| console.log(`Got value ${value}`); | ||
| data = [...data, value]; | ||
| } | ||
|
|
||
| if (done) { | ||
| break; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| return Response.json(data); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| :::note | ||
|
|
||
| In a setup where a Durable Object returns a readable stream to a Worker, if the Worker cancels the Durable Object's readable stream, the cancellation propagates to the Durable Object. | ||
|
|
||
| ::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.