-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Handling streaming request/response bodies #3419
Comments
We may be able to make life easier in future by swapping out |
Looks like there's a PR for adding stream support to |
Hello, import { readFileSync, createWriteStream } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import busboy from 'busboy';
import { pipeline } from 'stream/promises';
import type { RequestHandler } from '@sveltejs/kit';
export const post: RequestHandler = async ({ request }) => {
const content = request.headers.get('content-type');
const saveTo = join(tmpdir(), 'testFile');
const bb = busboy({
headers: {
'content-type': content
}
});
bb.on('file', (name, file, info) => {
const { filename, encoding, mimeType } = info;
console.log(
`File [${name}]: filename: %j, encoding: %j, mimeType: %j`,
filename,
encoding,
mimeType
);
file.pipe(createWriteStream(saveTo));
});
bb.on('field', (name, val, info) => {
console.log(`Field [${name}]: value: %j`, val);
});
bb.on('close', () => {
console.log('Done parsing form!');
});
await pipeline(request.body as any, bb);
return {
status: 302,
headers: {
location: '/uploader'
}
};
}; uploader.svelte is <form method="POST" enctype="multipart/form-data" action="/authentification/uploader">
<input type="file" name="filefield" /><br />
<input type="text" name="textfield" /><br />
<input type="submit" />
</form> |
|
This seems like a good idea to me. The main issue I see is that it's only supported on Node 16.5+ and AWS Lambda doesn't yet support Node 16 so neither does Netlify or anything else built on top of Lambda. |
We use GraphQL Yoga uses So as GraphQL Yoga maintainers, we can support different platforms besides NodeJS like CF Workers, Deno etc. |
Is this feature already available in svelte-kit or waiting for the next release? 🤔 |
I'm not involved with the Svelte project so my answer will only reflects my own opinion. I think having to dependencies to do something that similar would only add maintenance (because you have to dependencies to maintain), increase attack surface (two dependencies for the same thing means two security checks to do when you could have only one), create some conditional code where you can use one dependency instead of two with an IDK why the |
🎉 🎉 🎉 🎉 🎉 🙏 |
I hope this is relevant here. From the conversations I see that this might be related here - how would I with the new feature enable the package to use ReadableStream? |
Describe the problem
As of #3384, hooks and endpoints receive a standard
Request
object, which among other things makes it possible to POST files:The file is buffered in memory, however, which means there's a practical limit on how large it can be.
Similarly, there's no good way to read a stream of binary data, or respond with a stream.
Describe the proposed solution
We need to solve this at a high level and at a low level. At the high level, it would be good to have utility functions for dealing with multipart form data specifically — something like this, perhaps:
For octet streams it might look like this:
At the low level, it should be possible to create your own stream reader and return your own
ReadableStream
bodies:Unfortunately there's a wrinkle:
ReadableStream
isn't available in older versions of Node, and theRequest
andResponse
objects (as polyfilled bynode-fetch
) use node streams instead ofReadableStream
. It may therefore be necessary to polyfillReadableStream
during development (and inadapter-node
, and lambdas) and convert between that and node streams. (On therequest
object, this would probably mean creating a Proxy that replacesbody
with aReadableStream
with the same contents as the node stream. Hopefully this is actually possible.)Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: