File
Blob: src/workerd/api/streams/encoding.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 | |
| 7 | #include <workerd/api/encoding.h> |
| 8 | #include <workerd/api/streams/readable.h> |
| 9 | #include <workerd/api/streams/transform.h> |
| 10 | #include <workerd/api/streams/writable.h> |
| 11 | |
| 12 | namespace workerd::api { |
| 13 | |
| 14 | class TextEncoderStream: public TransformStream { |
| 15 | public: |
| 16 | using TransformStream::TransformStream; |
| 17 | |
| 18 | static jsg::Ref<TextEncoderStream> constructor(jsg::Lock& js); |
| 19 | |
| 20 | kj::StringPtr getEncoding() { |
| 21 | return "utf-8"_kj; |
| 22 | } |
| 23 | |
| 24 | JSG_RESOURCE_TYPE(TextEncoderStream) { |
| 25 | JSG_INHERIT(TransformStream); |
| 26 | |
| 27 | JSG_READONLY_PROTOTYPE_PROPERTY(encoding, getEncoding); |
| 28 | |
| 29 | JSG_TS_OVERRIDE(extends TransformStream<string, Uint8Array>); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | class TextDecoderStream: public TransformStream { |
| 34 | public: |
| 35 | struct TextDecoderStreamInit { |
| 36 | jsg::Optional<bool> fatal; |
| 37 | jsg::Optional<bool> ignoreBOM; |
| 38 | |
| 39 | JSG_STRUCT(fatal, ignoreBOM); |
| 40 | }; |
| 41 | |
| 42 | TextDecoderStream(jsg::Ref<TextDecoder> decoder, |
| 43 | jsg::Ref<ReadableStream> readable, |
| 44 | jsg::Ref<WritableStream> writable); |
| 45 | |
| 46 | static jsg::Ref<TextDecoderStream> constructor( |
| 47 | jsg::Lock& js, jsg::Optional<kj::String> label, jsg::Optional<TextDecoderStreamInit> options); |
| 48 | |
| 49 | kj::StringPtr getEncoding(); |
| 50 | bool getFatal(); |
| 51 | bool getIgnoreBOM(); |
| 52 | |
| 53 | JSG_RESOURCE_TYPE(TextDecoderStream) { |
| 54 | JSG_INHERIT(TransformStream); |
| 55 | JSG_READONLY_PROTOTYPE_PROPERTY(encoding, getEncoding); |
| 56 | JSG_READONLY_PROTOTYPE_PROPERTY(fatal, getFatal); |
| 57 | JSG_READONLY_PROTOTYPE_PROPERTY(ignoreBOM, getIgnoreBOM); |
| 58 | |
| 59 | JSG_TS_OVERRIDE(extends TransformStream<ArrayBuffer | ArrayBufferView, string>); |
| 60 | } |
| 61 | |
| 62 | void visitForMemoryInfo(jsg::MemoryTracker& tracker) const; |
| 63 | |
| 64 | private: |
| 65 | jsg::Ref<TextDecoder> decoder; |
| 66 | |
| 67 | void visitForGc(jsg::GcVisitor& visitor); |
| 68 | }; |
| 69 | |
| 70 | } // namespace workerd::api |