File
Blob: src/workerd/api/cf-property.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 | // Common functionality to manage cf headers and properties. |
| 8 | |
| 9 | #include <workerd/jsg/jsg.h> |
| 10 | |
| 11 | namespace workerd::api { |
| 12 | |
| 13 | // A holder for Cf header property value. |
| 14 | // The string header is parsed on demand and the parsed value cached. |
| 15 | class CfProperty { |
| 16 | |
| 17 | public: |
| 18 | KJ_DISALLOW_COPY(CfProperty); |
| 19 | |
| 20 | explicit CfProperty() {} |
| 21 | CfProperty(decltype(nullptr)) {} |
| 22 | CfProperty(CfProperty&&) = default; |
| 23 | CfProperty& operator=(CfProperty&&) = default; |
| 24 | |
| 25 | explicit CfProperty(kj::Maybe<kj::StringPtr> unparsed); |
| 26 | |
| 27 | explicit CfProperty(jsg::Lock& js, const jsg::JsObject& object); |
| 28 | |
| 29 | explicit CfProperty(kj::Maybe<jsg::JsRef<jsg::JsObject>>&& parsed); |
| 30 | |
| 31 | // Get parsed value |
| 32 | jsg::Optional<jsg::JsObject> get(jsg::Lock& js); |
| 33 | |
| 34 | // Get parsed value as a global ref |
| 35 | jsg::Optional<jsg::JsRef<jsg::JsObject>> getRef(jsg::Lock& js); |
| 36 | |
| 37 | // Serialize to string |
| 38 | kj::Maybe<kj::String> serialize(jsg::Lock& js); |
| 39 | |
| 40 | // Clone by deep cloning parsed v8 object (if any). |
| 41 | CfProperty deepClone(jsg::Lock& js); |
| 42 | |
| 43 | void visitForGc(jsg::GcVisitor& visitor); |
| 44 | |
| 45 | JSG_MEMORY_INFO(CfProperty) { |
| 46 | KJ_IF_SOME(v, value) { |
| 47 | KJ_SWITCH_ONEOF(v) { |
| 48 | KJ_CASE_ONEOF(str, kj::String) { |
| 49 | tracker.trackField("value", str); |
| 50 | } |
| 51 | KJ_CASE_ONEOF(obj, jsg::JsRef<jsg::JsObject>) { |
| 52 | tracker.trackField("value", obj); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | kj::Maybe<kj::OneOf<kj::String, jsg::JsRef<jsg::JsObject>>> value; |
| 60 | }; |
| 61 | |
| 62 | } // namespace workerd::api |