File
Blob: src/workerd/api/streams/identity-transform-stream.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "transform.h" |
| 4 | |
| 5 | #include <workerd/jsg/jsg.h> |
| 6 | |
| 7 | namespace workerd::api { |
| 8 | |
| 9 | // The IdentityTransformStream is a non-standard TransformStream implementation that passes |
| 10 | // the exact bytes written to the writable side on to the readable side without modification. |
| 11 | // Unlike standard the TransformStream, the readable side of an IdentityTransformStream |
| 12 | // supports BYOB reads. |
| 13 | // |
| 14 | // The IdentityTransformStream is a kj-based implementation backed by a ReadableStreamSource |
| 15 | // and WritableStreamSink implementation. It is a legacy class that was created before the |
| 16 | // standard TransformStream constructor was available in workers. It is maintained for |
| 17 | // backwards compatibility but otherwise has no special significance. |
| 18 | class IdentityTransformStream: public TransformStream { |
| 19 | public: |
| 20 | using TransformStream::TransformStream; |
| 21 | |
| 22 | struct QueuingStrategy { |
| 23 | jsg::Optional<uint64_t> highWaterMark; |
| 24 | |
| 25 | JSG_STRUCT(highWaterMark); |
| 26 | }; |
| 27 | |
| 28 | static jsg::Ref<IdentityTransformStream> constructor( |
| 29 | jsg::Lock& js, jsg::Optional<QueuingStrategy> queuingStrategy = kj::none); |
| 30 | |
| 31 | JSG_RESOURCE_TYPE(IdentityTransformStream) { |
| 32 | JSG_INHERIT(TransformStream); |
| 33 | |
| 34 | JSG_TS_OVERRIDE(extends TransformStream<ArrayBuffer | ArrayBufferView, Uint8Array>); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | // Same as an IdentityTransformStream, except with a known length in bytes on the readable side. |
| 39 | // We don't currently enforce this limit -- it just convinces the kj-http layer to |
| 40 | // emit a Content-Length (assuming it doesn't get gzipped or anything). |
| 41 | class FixedLengthStream: public IdentityTransformStream { |
| 42 | public: |
| 43 | using IdentityTransformStream::IdentityTransformStream; |
| 44 | |
| 45 | static jsg::Ref<FixedLengthStream> constructor(jsg::Lock& js, |
| 46 | uint64_t expectedLength, |
| 47 | jsg::Optional<QueuingStrategy> queuingStrategy = kj::none); |
| 48 | |
| 49 | JSG_RESOURCE_TYPE(FixedLengthStream) { |
| 50 | JSG_INHERIT(IdentityTransformStream); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | struct OneWayPipe { |
| 55 | kj::Own<ReadableStreamSource> in; |
| 56 | kj::Own<WritableStreamSink> out; |
| 57 | }; |
| 58 | |
| 59 | OneWayPipe newIdentityPipe(kj::Maybe<uint64_t> expectedLength = kj::none); |
| 60 | |
| 61 | bool isIdentityTransformStream(WritableStreamSink& sink); |
| 62 | |
| 63 | } // namespace workerd::api |