File
Blob: src/workerd/io/actor-storage.h
| 1 | // Copyright (c) 2022-2023 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 <kj/common.h> |
| 8 | #include <kj/string.h> |
| 9 | |
| 10 | namespace workerd { |
| 11 | // This class wraps common values and functions for interacting durable object (actor) storage. |
| 12 | class ActorStorageLimits { |
| 13 | public: |
| 14 | // We grant some extra cushion on top of the advertised max size in order |
| 15 | // to avoid penalizing people for pushing right up against the advertised size. |
| 16 | // The v8 serialization method we use can add a few extra bytes for its type tag |
| 17 | // and other metadata, such as the length of a string or number of items in an |
| 18 | // array. The most important cases (where users are most likely to try to |
| 19 | // intentionally run right up against the limit) are Strings and ArrayBuffers, |
| 20 | // which each get 4 bytes of metadata attached when encoded. We throw a little |
| 21 | // extra on just for future proofing and an abundance of caution. |
| 22 | // |
| 23 | // If you're curious why we add 34 bytes of cushion -- we used to add 32, but |
| 24 | // then started writing v8 serialization headers, which are 2 bytes, and didn't |
| 25 | // want to stop accepting values that we accepted before writing headers. |
| 26 | |
| 27 | static constexpr size_t ADVERTISED_MAX_VALUE_SIZE = 128 * 1024; |
| 28 | static constexpr size_t ENFORCED_MAX_VALUE_SIZE = ADVERTISED_MAX_VALUE_SIZE + 34; |
| 29 | |
| 30 | static constexpr size_t MAX_KEY_SIZE = 2048; |
| 31 | |
| 32 | static void checkMaxKeySize(kj::StringPtr key); |
| 33 | static void checkMaxValueSize(kj::StringPtr key, kj::ArrayPtr<const kj::byte> value); |
| 34 | static void checkMaxPairsCount(size_t count); |
| 35 | }; |
| 36 | |
| 37 | } // namespace workerd |