File
Blob: src/workerd/api/node/buffer.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 | #pragma once |
| 5 | |
| 6 | #include "i18n.h" |
| 7 | |
| 8 | #include <workerd/jsg/jsg.h> |
| 9 | |
| 10 | namespace workerd::api::node { |
| 11 | |
| 12 | // Implements utilities in support of the Node.js Buffer |
| 13 | class BufferUtil final: public jsg::Object { |
| 14 | public: |
| 15 | BufferUtil() = default; |
| 16 | BufferUtil(jsg::Lock&, const jsg::Url&) {} |
| 17 | |
| 18 | uint32_t byteLength(jsg::Lock& js, jsg::JsString str); |
| 19 | |
| 20 | struct CompareOptions { |
| 21 | jsg::Optional<uint32_t> aStart; |
| 22 | jsg::Optional<uint32_t> aEnd; |
| 23 | jsg::Optional<uint32_t> bStart; |
| 24 | jsg::Optional<uint32_t> bEnd; |
| 25 | |
| 26 | JSG_STRUCT(aStart, aEnd, bStart, bEnd); |
| 27 | }; |
| 28 | |
| 29 | int compare(jsg::Lock& js, |
| 30 | jsg::JsUint8Array one, |
| 31 | jsg::JsUint8Array two, |
| 32 | jsg::Optional<CompareOptions> maybeOptions); |
| 33 | |
| 34 | jsg::JsUint8Array concat(jsg::Lock& js, kj::Array<jsg::JsUint8Array> list, uint32_t length); |
| 35 | |
| 36 | jsg::JsUint8Array decodeString(jsg::Lock& js, jsg::JsString string, EncodingValue encoding); |
| 37 | |
| 38 | void fillImpl(jsg::Lock& js, |
| 39 | jsg::JsUint8Array buffer, |
| 40 | kj::OneOf<jsg::JsString, jsg::JsUint8Array> value, |
| 41 | uint32_t start, |
| 42 | uint32_t end, |
| 43 | jsg::Optional<EncodingValue> encoding); |
| 44 | |
| 45 | jsg::Optional<uint32_t> indexOf(jsg::Lock& js, |
| 46 | jsg::JsUint8Array buffer, |
| 47 | kj::OneOf<jsg::JsString, jsg::JsUint8Array> value, |
| 48 | int32_t byteOffset, |
| 49 | EncodingValue encoding, |
| 50 | bool isForward); |
| 51 | |
| 52 | void swap(jsg::Lock& js, jsg::JsUint8Array buffer, int size); |
| 53 | |
| 54 | jsg::JsString toString( |
| 55 | jsg::Lock& js, jsg::JsUint8Array bytes, uint32_t start, uint32_t end, EncodingValue encoding); |
| 56 | |
| 57 | uint32_t write(jsg::Lock& js, |
| 58 | jsg::JsUint8Array buffer, |
| 59 | jsg::JsString string, |
| 60 | uint32_t offset, |
| 61 | uint32_t length, |
| 62 | EncodingValue encoding); |
| 63 | |
| 64 | enum NativeDecoderFields { |
| 65 | kIncompleteCharactersStart = 0, |
| 66 | kIncompleteCharactersEnd = 4, |
| 67 | kMissingBytes = 4, |
| 68 | kBufferedBytes = 5, |
| 69 | kEncoding = 6, |
| 70 | kSize = 7, |
| 71 | }; |
| 72 | |
| 73 | jsg::JsString decode(jsg::Lock& js, jsg::JsUint8Array bytes, jsg::JsUint8Array state); |
| 74 | jsg::JsString flush(jsg::Lock& js, jsg::JsUint8Array state); |
| 75 | bool isAscii(jsg::JsUint8Array bytes); |
| 76 | bool isUtf8(jsg::JsUint8Array bytes); |
| 77 | jsg::JsUint8Array transcode(jsg::Lock& js, |
| 78 | jsg::JsUint8Array source, |
| 79 | EncodingValue rawFromEncoding, |
| 80 | EncodingValue rawToEncoding); |
| 81 | |
| 82 | JSG_RESOURCE_TYPE(BufferUtil) { |
| 83 | JSG_METHOD(byteLength); |
| 84 | JSG_METHOD(compare); |
| 85 | JSG_METHOD(concat); |
| 86 | JSG_METHOD(decodeString); |
| 87 | JSG_METHOD(fillImpl); |
| 88 | JSG_METHOD(indexOf); |
| 89 | JSG_METHOD(swap); |
| 90 | JSG_METHOD(toString); |
| 91 | JSG_METHOD(write); |
| 92 | JSG_METHOD(isAscii); |
| 93 | JSG_METHOD(isUtf8); |
| 94 | JSG_METHOD(transcode); |
| 95 | |
| 96 | // For StringDecoder |
| 97 | JSG_METHOD(decode); |
| 98 | JSG_METHOD(flush); |
| 99 | |
| 100 | JSG_STATIC_CONSTANT_NAMED(ASCII, static_cast<EncodingValue>(Encoding::ASCII)); |
| 101 | JSG_STATIC_CONSTANT_NAMED(LATIN1, static_cast<EncodingValue>(Encoding::LATIN1)); |
| 102 | JSG_STATIC_CONSTANT_NAMED(UTF8, static_cast<EncodingValue>(Encoding::UTF8)); |
| 103 | JSG_STATIC_CONSTANT_NAMED(UTF16LE, static_cast<EncodingValue>(Encoding::UTF16LE)); |
| 104 | JSG_STATIC_CONSTANT_NAMED(BASE64, static_cast<EncodingValue>(Encoding::BASE64)); |
| 105 | JSG_STATIC_CONSTANT_NAMED(BASE64URL, static_cast<EncodingValue>(Encoding::BASE64URL)); |
| 106 | JSG_STATIC_CONSTANT_NAMED(HEX, static_cast<EncodingValue>(Encoding::HEX)); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | #define EW_NODE_BUFFER_ISOLATE_TYPES api::node::BufferUtil, api::node::BufferUtil::CompareOptions |
| 111 | |
| 112 | } // namespace workerd::api::node |