File
Blob: src/workerd/api/encoding-shared.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 | // Shared types used by encoding.h and encoding-legacy.h. |
| 6 | // Extracted to break circular dependencies between the two headers. |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <workerd/jsg/jsg.h> |
| 11 | #include <workerd/util/strong-bool.h> |
| 12 | |
| 13 | namespace workerd::api { |
| 14 | |
| 15 | WD_STRONG_BOOL(DecoderFatal); |
| 16 | WD_STRONG_BOOL(DecoderIgnoreBom); |
| 17 | |
| 18 | // The encodings listed here are defined as required by the Encoding spec. |
| 19 | // The first label is enum we use to identify the encoding in code, while |
| 20 | // the second label is the public identifier. |
| 21 | #define EW_ENCODINGS(V) \ |
| 22 | V(Utf8, "utf-8") \ |
| 23 | V(Ibm866, "ibm866") \ |
| 24 | V(Iso8859_2, "iso-8859-2") \ |
| 25 | V(Iso8859_3, "iso-8859-3") \ |
| 26 | V(Iso8859_4, "iso-8859-4") \ |
| 27 | V(Iso8859_5, "iso-8859-5") \ |
| 28 | V(Iso8859_6, "iso-8859-6") \ |
| 29 | V(Iso8859_7, "iso-8859-7") \ |
| 30 | V(Iso8859_8, "iso-8859-8") \ |
| 31 | V(Iso8859_8i, "iso-8859-8-i") \ |
| 32 | V(Iso8859_10, "iso-8859-10") \ |
| 33 | V(Iso8859_13, "iso-8859-13") \ |
| 34 | V(Iso8859_14, "iso-8859-14") \ |
| 35 | V(Iso8859_15, "iso-8859-15") \ |
| 36 | V(Iso8859_16, "iso-8859-16") \ |
| 37 | V(Ko18_r, "koi8-r") \ |
| 38 | V(Koi8_u, "koi8-u") \ |
| 39 | V(Macintosh, "macintosh") \ |
| 40 | V(Windows_874, "windows-874") \ |
| 41 | V(Windows_1250, "windows-1250") \ |
| 42 | V(Windows_1251, "windows-1251") \ |
| 43 | V(Windows_1252, "windows-1252") \ |
| 44 | V(Windows_1253, "windows-1253") \ |
| 45 | V(Windows_1254, "windows-1254") \ |
| 46 | V(Windows_1255, "windows-1255") \ |
| 47 | V(Windows_1256, "windows-1256") \ |
| 48 | V(Windows_1257, "windows-1257") \ |
| 49 | V(Windows_1258, "windows-1258") \ |
| 50 | V(X_Mac_Cyrillic, "x-mac-cyrillic") \ |
| 51 | V(Gbk, "gbk") \ |
| 52 | V(Gb18030, "gb18030") \ |
| 53 | V(Big5, "big5") \ |
| 54 | V(Euc_Jp, "euc-jp") \ |
| 55 | V(Iso2022_Jp, "iso-2022-jp") \ |
| 56 | V(Shift_Jis, "shift_jis") \ |
| 57 | V(Euc_Kr, "euc-kr") \ |
| 58 | V(Replacement, "replacement") \ |
| 59 | V(Utf16be, "utf-16be") \ |
| 60 | V(Utf16le, "utf-16le") \ |
| 61 | V(X_User_Defined, "x-user-defined") |
| 62 | |
| 63 | enum class Encoding { |
| 64 | INVALID, |
| 65 | #define V(name, _) name, |
| 66 | EW_ENCODINGS(V) |
| 67 | #undef V |
| 68 | }; |
| 69 | |
| 70 | // A Decoder provides the underlying implementation of a TextDecoder. |
| 71 | class Decoder { |
| 72 | public: |
| 73 | virtual ~Decoder() noexcept(true) {} |
| 74 | virtual Encoding getEncoding() = 0; |
| 75 | virtual kj::Maybe<jsg::JsString> decode( |
| 76 | jsg::Lock& js, kj::ArrayPtr<const kj::byte> buffer, bool flush = false) = 0; |
| 77 | |
| 78 | virtual void reset() {} |
| 79 | }; |
| 80 | |
| 81 | } // namespace workerd::api |