File
Blob: src/workerd/api/system-streams.h
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #pragma once |
| 6 | // Implementations of ReadableStreamSource / WritableStreamSink which wrap system streams (sockets), |
| 7 | // handle encoding/decoding, and optimize pumping between them when possible. |
| 8 | |
| 9 | #include "http.h" |
| 10 | |
| 11 | #include <workerd/api/streams/common.h> // for StreamEncoding, ... |
| 12 | #include <workerd/io/compatibility-date.capnp.h> |
| 13 | #include <workerd/io/io-context.h> |
| 14 | |
| 15 | namespace workerd::api { |
| 16 | |
| 17 | // A ReadableStreamSource which automatically decodes its underlying stream. It does so lazily -- if |
| 18 | // one of the `tryRead()` overloads is never called, then a `pumpTo()` to a WritableStreamSink |
| 19 | // returned by `newSystemStream()` of the same encoding will not cause any decoding/encoding steps. |
| 20 | // |
| 21 | // NOTE: `inner` must be wholly-owned. In particular, it cannot contain references to JavaScript |
| 22 | // heap objects, as the stream is allowed to outlive the isolate, especially in the case of |
| 23 | // deferred proxying. If the inner stream for some reason contains JS references, you'll need |
| 24 | // to provide your own implementation of ReadableStreamSource. |
| 25 | kj::Own<ReadableStreamSource> newSystemStream(kj::Own<kj::AsyncInputStream> inner, |
| 26 | StreamEncoding encoding, |
| 27 | IoContext& context = IoContext::current()); |
| 28 | |
| 29 | // A WritableStreamSink which automatically encodes its underlying stream. |
| 30 | // |
| 31 | // NOTE: As with the other overload of newSystemStream(), `inner` must be wholly owned. |
| 32 | kj::Own<WritableStreamSink> newSystemStream(kj::Own<kj::AsyncOutputStream> inner, |
| 33 | StreamEncoding encoding, |
| 34 | IoContext& context = IoContext::current()); |
| 35 | |
| 36 | struct SystemMultiStream { |
| 37 | kj::Own<ReadableStreamSource> readable; |
| 38 | kj::Own<WritableStreamSink> writable; |
| 39 | }; |
| 40 | |
| 41 | // A combo ReadableStreamSource and WritableStreamSink. |
| 42 | SystemMultiStream newSystemMultiStream(kj::RefcountedWrapper<kj::Own<kj::AsyncIoStream>>& stream, |
| 43 | IoContext& context = IoContext::current()); |
| 44 | |
| 45 | struct ContentEncodingOptions { |
| 46 | bool brotliEnabled = false; |
| 47 | ContentEncodingOptions() = default; |
| 48 | ContentEncodingOptions(CompatibilityFlags::Reader flags); |
| 49 | }; |
| 50 | |
| 51 | // Get the Content-Encoding header from an HttpHeaders object as a StreamEncoding enum. Unsupported |
| 52 | // encodings return IDENTITY. |
| 53 | StreamEncoding getContentEncoding(IoContext& context, |
| 54 | const kj::HttpHeaders& headers, |
| 55 | Response::BodyEncoding bodyEncoding = Response::BodyEncoding::AUTO, |
| 56 | ContentEncodingOptions options = {}); |
| 57 | |
| 58 | } // namespace workerd::api |