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

[feat] support for streaming via Async Iterator endpoints #2212

Closed
wants to merge 19 commits into from

Conversation

kjmph
Copy link

@kjmph kjmph commented Aug 15, 2021

This commit demonstrates the ability to easily add Async Iterator
SvelteKit Endpoints. By having an endpoint return an async iterator as
the body, SvelteKit will keep emitting writes for as long as the
endpoint yields values. Or, for as long as the client holds the
request open; which ever ends first.

Most of the discussion is occurring over on #1563; opening pull request to be illustrative. I believe there are a few type errors that will need to be cleaned up before merging.

@changeset-bot
Copy link

changeset-bot bot commented Aug 15, 2021

⚠️ No Changeset found

Latest commit: 3005b8d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@jesseskinner
Copy link

Today I've added some test coverage for async iterator support, including for the dev server (basics), adapter-static and adapter-node, and uncovered & fixed some bugs as well as added support for adapter-static in the process. I also rebased. I think the PR is now in good shape - the tests have definitely added confidence that things should be working well. Looking forward to any feedback whether any additional changes are needed.

Copy link
Member

@mrkishi mrkishi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your PR looks good but could you check my notes? Try running the following stream server and serving { body: await fetch('/stream').body } from SvelteKit. It looks to keep streaming after disconnection, unless I'm messing something up?

function handle_request(request, response) {
	if (request.url === '/stream') {
		console.log('request opened');

		let interval = 0;
		const stream = new Readable({
			construct(callback) {
				console.log('stream opened');

				const send = () => {
					const chunk = Math.random().toString();
					console.log('stream push', chunk);
					this.push(chunk);
				}

				interval = setInterval(send, 1000);
				callback();
			},

			read() {},

			destroy(error, callback) {
				console.log('stream closed');
				clearInterval(interval);
				callback(error);
			},
		});

		response.on('close', () => {
			console.log('request closed');
			stream.destroy();
		});

		response.writeHead(200);
		stream.pipe(response);
	}
}

const server = http.createServer(handle_request);
server.listen(8080);

packages/adapter-node/src/server.js Outdated Show resolved Hide resolved
@benmccann benmccann changed the title Add support for Async Iterator endpoints [feat] support for Async Iterator endpoints Aug 18, 2021
@benmccann benmccann changed the title [feat] support for Async Iterator endpoints [feat] support for streaming via Async Iterator endpoints Oct 28, 2021
@AndreasHald
Copy link

@jesseskinner anything I can do to help get this PR over the finish line?

@jesseskinner
Copy link

@AndreasHald I think it's done, just waiting for a review. @mrkishi had some comments for @kjmph but I'm not sure how critical those bits are for getting this merged. 🤷‍♂️

@kjmph
Copy link
Author

kjmph commented Dec 6, 2021

I'll make the relevant changes, my apologies on my delay. I think @mrkishi is correct, we should handle the cancel of the ReadableStream for Cloudflare support as well as the closure of the pipe for Node.js support. I'll make the change when free (today or so).

kjmph and others added 10 commits December 16, 2021 10:35
This commit demonstrates the ability to easily add Async Iterator
SvelteKit Endpoints. By having an endpoint return an async iterator as
the body, SvelteKit will keep emitting writes for as long as the
endpoint yields values. Or, for as long as the client holds the
request open; which ever ends first.
I don't have Cloudflare workers setup, so this still needs testing.
@kjmph
Copy link
Author

kjmph commented Dec 16, 2021

Forgive my whack a mole, I see the bar for pull requests has been raised on this project. Good on ya. Anyway, I cleaned up everything I can find, I clearly don't understand how to run the tests on my own machine, yet I was able to fix everything I can find short of the timeout. I'm uncertain if the timeout is due to these changes. Note, there was an Either/Or type added to calm down TypeScript checking; perhaps that causes tests to take longer?

@kjmph
Copy link
Author

kjmph commented Dec 16, 2021

Oh would you like documentation updated in this pull request? Or, a new nav item added to demonstrate a streaming endpoint? Now that the preview is auto-deployed (Thanks Vercel), it could be helpful for maintainers.

@Rich-Harris
Copy link
Member

Thank you for putting all this together. A lot has changed since this PR began — per discussion elsewhere we decided in favour of using ReadableStream rather than async iterables; between that and the many changes in #3384 it probably makes sense to close this in favour of #3419

@kjmph
Copy link
Author

kjmph commented Jan 20, 2022

Thanks @Rich-Harris!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants