-
Notifications
You must be signed in to change notification settings - Fork 16k
Create digest-stream.md #4919
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
Closed
Closed
Create digest-stream.md #4919
Changes from all commits
Commits
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
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,64 @@ | ||
| --- | ||
| type: example | ||
| summary: Generate an ETag from a ReadableStream via DigestStream. | ||
| tags: | ||
| - WebCrypto | ||
| pcx-content-type: configuration | ||
| title: ETag Generation with DigestStream | ||
| weight: 1001 | ||
| layout: example | ||
| --- | ||
|
|
||
| ```ts | ||
| interface Env { | ||
| KV: KVNamespace; | ||
| } | ||
|
|
||
| const onFetch: ExportedHandlerFetchHandler<Env> = async (request, env) => { | ||
| const encoder = new TextEncoder(); | ||
|
|
||
| const etagStream = new crypto.DigestStream('SHA-1'); | ||
| const { readable, writable } = new TransformStream(); | ||
|
|
||
| const etagWriter = etagStream.getWriter(); | ||
| const resultWriter = writable.getWriter(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative approach here is to tee the streams. const { writable, readable } = new IdentityTransformStream();
const [ branch1, branch2 ] = readable.tee();
env.KV.put('result', branch1);
async getDigest(readable) {
const etagStream = new crypto.DigestStream('SHA-1');
await readable.pipeTo(etagStream);
return etagStream.digest;
}
const writer = writable.getWriter();
writer.write('hello');
writer.close();
const etag = await getDigest(branch2);
// ... |
||
|
|
||
| /** | ||
| * We're not awaiting this because we'll start streaming into | ||
| * the writable side of the transform stream after. | ||
| */ | ||
| env.KV.put('result', readable); | ||
|
|
||
| /** | ||
| * `processStream` would be some utility function that reads | ||
| * a ReadableStream, decoding each chunk and passing the data | ||
| * to the callback. | ||
| */ | ||
| await processStream(request.body, (data: string) => { | ||
| // ... do whatever with the data here | ||
|
|
||
| const encoded = encoder.encode(data); | ||
|
|
||
| etagWriter.write(encoded); | ||
| resultWriter.write(encoded); | ||
| } | ||
|
|
||
| await Promise.all([etagWriter.close(), resultWriter.close()]); | ||
|
|
||
| const etagBuffer = await etagStream.digest; | ||
|
|
||
| const etag = Array.from(new Uint8Array(etagBuffer)) | ||
| .map((byte) => byte.toString(16).padStart(2, '0')) | ||
| .join(''); | ||
|
|
||
| await env.KV.put('etag', etag); | ||
|
|
||
| return new Response('done'); | ||
| } | ||
|
|
||
| const worker: ExportedHandler<Env> = { | ||
| fetch: onFetch, | ||
| }; | ||
|
|
||
| export default worker; | ||
| ``` | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use
new IdentityTransformStream()here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it's been too long, looking at this again now. Can you explain the difference, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is using the
TransformStreamessentially as an identity passthrough. It is not actually transforming any bytes. This is the exact use case forIdentityTransformStream. Soon, the default implementation ofnew TransformStream()is going to change to be more spec compliant. It will still be an identity passthrough by default but the underlying implementation is a bit more complex. To maintain the very simple use case you have here,IdentityTransformStreamis the right choice.