@@ -2686,25 +2686,45 @@ Result<ConfigStore> ConfigStore::open(std::string_view name) {
2686
2686
}
2687
2687
2688
2688
Result<std::optional<HostString>> ConfigStore::get (std::string_view name) {
2689
+ return this ->get (name, CONFIG_STORE_INITIAL_BUF_LEN);
2690
+ }
2691
+
2692
+ Result<std::optional<HostString>> ConfigStore::get (std::string_view name,
2693
+ uint32_t initial_buf_len) {
2689
2694
TRACE_CALL ()
2690
2695
Result<std::optional<HostString>> res;
2691
2696
2697
+ uint32_t buf_len{initial_buf_len};
2692
2698
auto name_str = string_view_to_world_string (name);
2693
2699
fastly::fastly_world_string ret;
2694
2700
fastly::fastly_host_error err;
2695
- ret.ptr = static_cast <uint8_t *>(cabi_malloc (CONFIG_STORE_ENTRY_MAX_LEN, 1 ));
2696
- if (!convert_result (fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
2697
- name_str.len , reinterpret_cast <char *>(ret.ptr ),
2698
- CONFIG_STORE_ENTRY_MAX_LEN, &ret.len ),
2699
- &err)) {
2701
+
2702
+ ret.ptr = static_cast <uint8_t *>(cabi_malloc (buf_len, 1 ));
2703
+
2704
+ bool succeeded{convert_result (
2705
+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2706
+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2707
+ &err)};
2708
+
2709
+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2710
+ buf_len = ret.len ;
2711
+ ret.len = 0 ;
2712
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , initial_buf_len, 1 , buf_len));
2713
+ succeeded = convert_result (
2714
+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2715
+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2716
+ &err);
2717
+ }
2718
+
2719
+ if (!succeeded) {
2700
2720
cabi_free (ret.ptr );
2701
2721
if (error_is_optional_none (err)) {
2702
2722
res.emplace (std::nullopt);
2703
2723
} else {
2704
2724
res.emplace_err (err);
2705
2725
}
2706
2726
} else {
2707
- ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , CONFIG_STORE_ENTRY_MAX_LEN , 1 , ret.len ));
2727
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , buf_len , 1 , ret.len ));
2708
2728
res.emplace (make_host_string (ret));
2709
2729
}
2710
2730
@@ -2842,25 +2862,41 @@ FastlyAsyncTask::Handle ObjectStorePendingDelete::async_handle() const {
2842
2862
}
2843
2863
2844
2864
Result<std::optional<HostBytes>> Secret::plaintext () const {
2865
+ return this ->plaintext (CONFIG_STORE_INITIAL_BUF_LEN);
2866
+ }
2867
+
2868
+ Result<std::optional<HostBytes>> Secret::plaintext (uint32_t initial_buf_len) const {
2845
2869
TRACE_CALL ()
2846
2870
Result<std::optional<HostBytes>> res;
2847
2871
2872
+ uint32_t buf_len{initial_buf_len};
2848
2873
fastly::fastly_world_list_u8 ret;
2849
2874
fastly::fastly_host_error err;
2850
- ret.ptr = static_cast <uint8_t *>(JS_malloc (CONTEXT, DICTIONARY_ENTRY_MAX_LEN));
2851
- if (!convert_result (fastly::secret_store_plaintext (this ->handle ,
2852
- reinterpret_cast <char *>(ret.ptr ),
2853
- DICTIONARY_ENTRY_MAX_LEN, &ret.len ),
2854
- &err)) {
2875
+ ret.ptr = static_cast <uint8_t *>(JS_malloc (CONTEXT, buf_len));
2876
+ bool succeeded{
2877
+ convert_result (fastly::secret_store_plaintext (this ->handle , reinterpret_cast <char *>(ret.ptr ),
2878
+ buf_len, &ret.len ),
2879
+ &err)};
2880
+
2881
+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2882
+ buf_len = ret.len ;
2883
+ ret.len = 0 ;
2884
+ ret.ptr = static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , initial_buf_len, buf_len));
2885
+ succeeded =
2886
+ convert_result (fastly::secret_store_plaintext (
2887
+ this ->handle , reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2888
+ &err);
2889
+ }
2890
+
2891
+ if (!succeeded) {
2855
2892
if (error_is_optional_none (err)) {
2856
2893
res.emplace (std::nullopt);
2857
2894
} else {
2858
2895
JS_free (CONTEXT, ret.ptr );
2859
2896
res.emplace_err (err);
2860
2897
}
2861
2898
} else {
2862
- ret.ptr =
2863
- static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , DICTIONARY_ENTRY_MAX_LEN, ret.len ));
2899
+ ret.ptr = static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , buf_len, ret.len ));
2864
2900
res.emplace (make_host_bytes (ret.ptr , ret.len ));
2865
2901
}
2866
2902
0 commit comments