Skip to content

Commit a520361

Browse files
authored
chore: Switch to using the config-store host calls (#809)
Fixes #801 Use the config-store host calls for the ConfigStore api, instead of the deprecated dictionary host calls.
1 parent 3a959ff commit a520361

22 files changed

+663
-573
lines changed

runtime/fastly/builtins/config-store.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ using fastly::fastly::FastlyGetErrorMessage;
88

99
namespace fastly::config_store {
1010

11-
host_api::Dict ConfigStore::config_store_handle(JSObject *obj) {
11+
host_api::ConfigStore ConfigStore::config_store_handle(JSObject *obj) {
1212
JS::Value val = JS::GetReservedSlot(obj, ConfigStore::Slots::Handle);
13-
return host_api::Dict(val.toInt32());
13+
return host_api::ConfigStore(val.toInt32());
1414
}
1515

1616
bool ConfigStore::get(JSContext *cx, unsigned argc, JS::Value *vp) {
1717
METHOD_HEADER(1)
1818

1919
auto key = core::encode(cx, args[0]);
2020
// If the converted string has a length of 0 then we throw an Error
21-
// because Dictionary keys have to be at-least 1 character.
21+
// because config-store keys have to be at-least 1 character.
2222
if (!key || key.len == 0) {
2323
JS_ReportErrorNumberASCII(cx, FastlyGetErrorMessage, nullptr, JSMSG_CONFIG_STORE_KEY_EMPTY);
2424
return false;
@@ -73,14 +73,14 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
7373
auto name = core::encode(cx, args[0]);
7474

7575
// If the converted string has a length of 0 then we throw an Error
76-
// because Dictionary names have to be at-least 1 character.
76+
// because config-store names have to be at-least 1 character.
7777
if (!name) {
7878
JS_ReportErrorNumberASCII(cx, FastlyGetErrorMessage, nullptr, JSMSG_CONFIG_STORE_NAME_EMPTY);
7979
return false;
8080
}
8181

8282
// If the converted string has a length of more than 255 then we throw an Error
83-
// because Dictionary names have to be less than 255 characters.
83+
// because config-store names have to be less than 255 characters.
8484
if (name.size() > 255) {
8585
JS_ReportErrorNumberASCII(cx, FastlyGetErrorMessage, nullptr, JSMSG_CONFIG_STORE_NAME_TOO_LONG);
8686
return false;
@@ -105,7 +105,7 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
105105
}
106106

107107
JS::RootedObject config_store(cx, JS_NewObjectForConstructor(cx, &class_, args));
108-
auto open_res = host_api::Dict::open(name);
108+
auto open_res = host_api::ConfigStore::open(name);
109109
if (auto *err = open_res.to_err()) {
110110
if (host_api::error_is_bad_handle(*err)) {
111111
JS_ReportErrorNumberASCII(cx, FastlyGetErrorMessage, nullptr,

runtime/fastly/builtins/config-store.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ConfigStore : public builtins::BuiltinImpl<ConfigStore> {
2121

2222
static bool get(JSContext *cx, unsigned argc, JS::Value *vp);
2323

24-
static host_api::Dict config_store_handle(JSObject *obj);
24+
static host_api::ConfigStore config_store_handle(JSObject *obj);
2525
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
2626

2727
static bool init_class(JSContext *cx, JS::HandleObject global);

runtime/fastly/host-api/component/fastly_world.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ extern void __wasm_import_fastly_compute_at_edge_dictionary_open(uint8_t *, size
112112
__attribute__((__import_module__("fastly:compute-at-edge/dictionary"), __import_name__("get")))
113113
extern void __wasm_import_fastly_compute_at_edge_dictionary_get(int32_t, uint8_t *, size_t, uint8_t *);
114114

115+
// Imported Functions from `fastly:compute-at-edge/config-store`
116+
117+
__attribute__((__import_module__("fastly:compute-at-edge/config-store"), __import_name__("open")))
118+
extern void __wasm_import_fastly_compute_at_edge_config_store_open(uint8_t *, size_t, uint8_t *);
119+
120+
__attribute__((__import_module__("fastly:compute-at-edge/config-store"), __import_name__("get")))
121+
extern void __wasm_import_fastly_compute_at_edge_config_store_get(int32_t, uint8_t *, size_t, uint8_t *);
122+
115123
// Imported Functions from `fastly:compute-at-edge/edge-rate-limiter`
116124

117125
__attribute__((__import_module__("fastly:compute-at-edge/edge-rate-limiter"), __import_name__("check-rate")))
@@ -1353,6 +1361,73 @@ bool fastly_compute_at_edge_dictionary_get(fastly_compute_at_edge_dictionary_han
13531361
}
13541362
}
13551363

1364+
bool fastly_compute_at_edge_config_store_open(fastly_world_string_t *name, fastly_compute_at_edge_config_store_handle_t *ret, fastly_compute_at_edge_config_store_error_t *err) {
1365+
__attribute__((__aligned__(4)))
1366+
uint8_t ret_area[8];
1367+
uint8_t *ptr = (uint8_t *) &ret_area;
1368+
__wasm_import_fastly_compute_at_edge_config_store_open((uint8_t *) (*name).ptr, (*name).len, ptr);
1369+
fastly_compute_at_edge_config_store_result_handle_error_t result;
1370+
switch ((int32_t) *((uint8_t*) (ptr + 0))) {
1371+
case 0: {
1372+
result.is_err = false;
1373+
result.val.ok = (uint32_t) (*((int32_t*) (ptr + 4)));
1374+
break;
1375+
}
1376+
case 1: {
1377+
result.is_err = true;
1378+
result.val.err = (int32_t) *((uint8_t*) (ptr + 4));
1379+
break;
1380+
}
1381+
}
1382+
if (!result.is_err) {
1383+
*ret = result.val.ok;
1384+
return 1;
1385+
} else {
1386+
*err = result.val.err;
1387+
return 0;
1388+
}
1389+
}
1390+
1391+
bool fastly_compute_at_edge_config_store_get(fastly_compute_at_edge_config_store_handle_t h, fastly_world_string_t *key, fastly_world_option_string_t *ret, fastly_compute_at_edge_config_store_error_t *err) {
1392+
__attribute__((__aligned__(4)))
1393+
uint8_t ret_area[16];
1394+
uint8_t *ptr = (uint8_t *) &ret_area;
1395+
__wasm_import_fastly_compute_at_edge_config_store_get((int32_t) (h), (uint8_t *) (*key).ptr, (*key).len, ptr);
1396+
fastly_compute_at_edge_config_store_result_option_string_error_t result;
1397+
switch ((int32_t) *((uint8_t*) (ptr + 0))) {
1398+
case 0: {
1399+
result.is_err = false;
1400+
fastly_world_option_string_t option;
1401+
switch ((int32_t) *((uint8_t*) (ptr + 4))) {
1402+
case 0: {
1403+
option.is_some = false;
1404+
break;
1405+
}
1406+
case 1: {
1407+
option.is_some = true;
1408+
option.val = (fastly_world_string_t) { (uint8_t*)(*((uint8_t **) (ptr + 8))), (*((size_t*) (ptr + 12))) };
1409+
break;
1410+
}
1411+
}
1412+
1413+
result.val.ok = option;
1414+
break;
1415+
}
1416+
case 1: {
1417+
result.is_err = true;
1418+
result.val.err = (int32_t) *((uint8_t*) (ptr + 4));
1419+
break;
1420+
}
1421+
}
1422+
if (!result.is_err) {
1423+
*ret = result.val.ok;
1424+
return 1;
1425+
} else {
1426+
*err = result.val.err;
1427+
return 0;
1428+
}
1429+
}
1430+
13561431
bool fastly_compute_at_edge_edge_rate_limiter_check_rate(fastly_world_string_t *rate_counter_name, fastly_world_string_t *entry, uint32_t delta, uint32_t window, uint32_t limit, fastly_world_string_t *penalty_box_name, uint32_t time_to_live, bool *ret, fastly_compute_at_edge_edge_rate_limiter_error_t *err) {
13571432
__attribute__((__aligned__(1)))
13581433
uint8_t ret_area[2];

runtime/fastly/host-api/component/fastly_world.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,26 @@ typedef struct {
522522
} val;
523523
} fastly_compute_at_edge_dictionary_result_option_string_error_t;
524524

525+
typedef fastly_compute_at_edge_types_error_t fastly_compute_at_edge_config_store_error_t;
526+
527+
typedef uint32_t fastly_compute_at_edge_config_store_handle_t;
528+
529+
typedef struct {
530+
bool is_err;
531+
union {
532+
fastly_compute_at_edge_config_store_handle_t ok;
533+
fastly_compute_at_edge_config_store_error_t err;
534+
} val;
535+
} fastly_compute_at_edge_config_store_result_handle_error_t;
536+
537+
typedef struct {
538+
bool is_err;
539+
union {
540+
fastly_world_option_string_t ok;
541+
fastly_compute_at_edge_config_store_error_t err;
542+
} val;
543+
} fastly_compute_at_edge_config_store_result_option_string_error_t;
544+
525545
typedef fastly_compute_at_edge_types_error_t fastly_compute_at_edge_edge_rate_limiter_error_t;
526546

527547
typedef struct {
@@ -1296,6 +1316,15 @@ extern bool fastly_compute_at_edge_dictionary_get(fastly_compute_at_edge_diction
12961316
fastly_world_option_string_t *ret,
12971317
fastly_compute_at_edge_dictionary_error_t *err);
12981318

1319+
// Imported Functions from `fastly:compute-at-edge/config-store`
1320+
extern bool
1321+
fastly_compute_at_edge_config_store_open(fastly_world_string_t *name,
1322+
fastly_compute_at_edge_config_store_handle_t *ret,
1323+
fastly_compute_at_edge_config_store_error_t *err);
1324+
extern bool fastly_compute_at_edge_config_store_get(
1325+
fastly_compute_at_edge_config_store_handle_t h, fastly_world_string_t *key,
1326+
fastly_world_option_string_t *ret, fastly_compute_at_edge_config_store_error_t *err);
1327+
12991328
// Imported Functions from `fastly:compute-at-edge/edge-rate-limiter`
13001329
extern bool fastly_compute_at_edge_edge_rate_limiter_check_rate(
13011330
fastly_world_string_t *rate_counter_name, fastly_world_string_t *entry, uint32_t delta,
@@ -1599,7 +1628,7 @@ fastly_compute_at_edge_secret_store_open(fastly_world_string_t *name,
15991628
extern bool fastly_compute_at_edge_secret_store_get(
16001629
fastly_compute_at_edge_secret_store_store_handle_t store, fastly_world_string_t *key,
16011630
fastly_world_option_secret_handle_t *ret, fastly_compute_at_edge_secret_store_error_t *err);
1602-
bool fastly_compute_at_edge_secret_store_plaintext(
1631+
extern bool fastly_compute_at_edge_secret_store_plaintext(
16031632
fastly_compute_at_edge_secret_store_secret_handle_t secret, fastly_world_option_list_u8_t *ret,
16041633
fastly_compute_at_edge_secret_store_error_t *err);
16051634
extern bool fastly_compute_at_edge_secret_store_from_bytes(

runtime/fastly/host-api/component/fastly_world_adapter.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,36 @@ bool fastly_compute_at_edge_dictionary_get(fastly_compute_at_edge_dictionary_han
785785
return true;
786786
}
787787

788+
bool fastly_compute_at_edge_config_store_open(fastly_world_string_t *name,
789+
fastly_compute_at_edge_config_store_handle_t *ret,
790+
fastly_compute_at_edge_types_error_t *err) {
791+
return convert_result(
792+
fastly::config_store_open(reinterpret_cast<char *>(name->ptr), name->len, ret), err);
793+
}
794+
795+
bool fastly_compute_at_edge_config_store_get(fastly_compute_at_edge_config_store_handle_t h,
796+
fastly_world_string_t *key,
797+
fastly_world_option_string_t *ret,
798+
fastly_compute_at_edge_types_error_t *err) {
799+
ret->val.ptr = static_cast<uint8_t *>(cabi_malloc(CONFIG_STORE_ENTRY_MAX_LEN, 1));
800+
if (!convert_result(fastly::config_store_get(h, reinterpret_cast<char *>(key->ptr), key->len,
801+
reinterpret_cast<char *>(ret->val.ptr),
802+
CONFIG_STORE_ENTRY_MAX_LEN, &ret->val.len),
803+
err)) {
804+
if (*err == FASTLY_COMPUTE_AT_EDGE_TYPES_ERROR_OPTIONAL_NONE) {
805+
ret->is_some = false;
806+
return true;
807+
} else {
808+
cabi_free(ret->val.ptr);
809+
return false;
810+
}
811+
}
812+
ret->is_some = true;
813+
ret->val.ptr = static_cast<uint8_t *>(
814+
cabi_realloc(ret->val.ptr, CONFIG_STORE_ENTRY_MAX_LEN, 1, ret->val.len));
815+
return true;
816+
}
817+
788818
bool fastly_compute_at_edge_secret_store_open(
789819
fastly_world_string_t *name, fastly_compute_at_edge_secret_store_store_handle_t *ret,
790820
fastly_compute_at_edge_types_error_t *err) {
Binary file not shown.

runtime/fastly/host-api/fastly.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,15 @@ WASM_IMPORT("fastly_dictionary", "get")
352352
int dictionary_get(fastly_compute_at_edge_dictionary_handle_t dict_handle, const char *key,
353353
size_t key_len, char *value, size_t value_max_len, size_t *nwritten);
354354

355+
// Module fastly_config_store
356+
WASM_IMPORT("fastly_config_store", "open")
357+
int config_store_open(const char *name, size_t name_len,
358+
fastly_compute_at_edge_config_store_handle_t *dict_handle_out);
359+
360+
WASM_IMPORT("fastly_config_store", "get")
361+
int config_store_get(fastly_compute_at_edge_config_store_handle_t dict_handle, const char *key,
362+
size_t key_len, char *value, size_t value_max_len, size_t *nwritten);
363+
355364
// Module fastly_secret_store
356365
WASM_IMPORT("fastly_secret_store", "open")
357366
int secret_store_open(const char *name, size_t name_len,

runtime/fastly/host-api/host_api.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,38 @@ Result<std::optional<HostString>> Dict::get(std::string_view name) {
12521252
return res;
12531253
}
12541254

1255+
Result<ConfigStore> ConfigStore::open(std::string_view name) {
1256+
Result<ConfigStore> res;
1257+
1258+
auto name_str = string_view_to_world_string(name);
1259+
fastly_compute_at_edge_config_store_handle_t ret;
1260+
fastly_compute_at_edge_types_error_t err;
1261+
if (!fastly_compute_at_edge_config_store_open(&name_str, &ret, &err)) {
1262+
res.emplace_err(err);
1263+
} else {
1264+
res.emplace(ret);
1265+
}
1266+
1267+
return res;
1268+
}
1269+
1270+
Result<std::optional<HostString>> ConfigStore::get(std::string_view name) {
1271+
Result<std::optional<HostString>> res;
1272+
1273+
auto name_str = string_view_to_world_string(name);
1274+
fastly_world_option_string_t ret;
1275+
fastly_compute_at_edge_types_error_t err;
1276+
if (!fastly_compute_at_edge_config_store_get(this->handle, &name_str, &ret, &err)) {
1277+
res.emplace_err(err);
1278+
} else if (ret.is_some) {
1279+
res.emplace(make_host_string(ret.val));
1280+
} else {
1281+
res.emplace(std::nullopt);
1282+
}
1283+
1284+
return res;
1285+
}
1286+
12551287
Result<ObjectStore> ObjectStore::open(std::string_view name) {
12561288
Result<ObjectStore> res;
12571289

runtime/fastly/host-api/host_api_fastly.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,18 @@ class Dict final {
531531
Result<std::optional<HostString>> get(std::string_view name);
532532
};
533533

534+
class ConfigStore final {
535+
public:
536+
FastlyHandle handle = UINT32_MAX - 1;
537+
538+
ConfigStore() = default;
539+
explicit ConfigStore(FastlyHandle handle) : handle{handle} {}
540+
541+
static Result<ConfigStore> open(std::string_view name);
542+
543+
Result<std::optional<HostString>> get(std::string_view name);
544+
};
545+
534546
class ObjectStore final {
535547
public:
536548
FastlyHandle handle = UINT32_MAX - 1;

runtime/fastly/host-api/wit/deps/fastly/compute-at-edge.wit

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,20 @@ interface dictionary {
431431
get: func(h: handle, key: string) -> result<option<string>, error>;
432432
}
433433

434+
/*
435+
* Fastly Config Store
436+
*/
437+
interface config-store {
438+
439+
use types.{error};
440+
441+
type handle = u32;
442+
443+
open: func(name: string) -> result<handle, error>;
444+
445+
get: func(h: handle, key: string) -> result<option<string>, error>;
446+
}
447+
434448
/*
435449
* Fastly Geo
436450
*/

0 commit comments

Comments
 (0)