Skip to content

Commit 7271578

Browse files
elliotttJakeChampion
authored andcommitted
Further restrict use of the fastly.h header
1 parent ce50327 commit 7271578

File tree

12 files changed

+60
-42
lines changed

12 files changed

+60
-42
lines changed

runtime/js-compute-runtime/builtins/cache-override.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "cache-override.h"
1111
#include "host_interface/fastly.h"
12+
#include "host_interface/host_api.h"
1213
#include "host_interface/host_call.h"
1314
#include "js-compute-builtins.h"
1415

@@ -75,7 +76,7 @@ void CacheOverride::set_pci(JSObject *self, bool pci) {
7576
JS::SetReservedSlot(self, CacheOverride::Slots::PCI, JS::BooleanValue(pci));
7677
}
7778

78-
fastly_compute_at_edge_fastly_http_cache_override_tag_t CacheOverride::abi_tag(JSObject *self) {
79+
CacheOverrideTag CacheOverride::abi_tag(JSObject *self) {
7980
MOZ_ASSERT(is_instance(self));
8081
switch (CacheOverride::mode(self)) {
8182
case CacheOverride::CacheOverrideMode::None:
@@ -85,7 +86,7 @@ fastly_compute_at_edge_fastly_http_cache_override_tag_t CacheOverride::abi_tag(J
8586
default:;
8687
}
8788

88-
fastly_compute_at_edge_fastly_http_cache_override_tag_t tag = 0;
89+
CacheOverrideTag tag = 0;
8990
if (!ttl(self).isUndefined())
9091
tag |= FASTLY_COMPUTE_AT_EDGE_FASTLY_HTTP_CACHE_OVERRIDE_TAG_TTL;
9192
if (!swr(self).isUndefined())

runtime/js-compute-runtime/builtins/config-store.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "config-store.h"
2-
#include "host_interface/fastly.h"
32
#include "host_interface/host_api.h"
43

54
namespace builtins {
@@ -106,7 +105,7 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
106105
JS::RootedObject config_store(cx, JS_NewObjectForConstructor(cx, &class_, args));
107106
auto open_res = Dict::open(name);
108107
if (auto *err = open_res.to_err()) {
109-
if (*err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_BAD_HANDLE) {
108+
if (error_is_bad_handle(*err)) {
110109
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_DOES_NOT_EXIST,
111110
name.data());
112111
return false;

runtime/js-compute-runtime/builtins/dictionary.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "dictionary.h"
2-
#include "host_interface/fastly.h"
32
#include "host_interface/host_api.h"
43

54
namespace builtins {
@@ -117,7 +116,7 @@ bool Dictionary::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
117116

118117
auto res = Dict::open(name_view);
119118
if (auto *err = res.to_err()) {
120-
if (*err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_BAD_HANDLE) {
119+
if (error_is_bad_handle(*err)) {
121120
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_DICTIONARY_DOES_NOT_EXIST,
122121
name_view.data());
123122
return false;

runtime/js-compute-runtime/builtins/fetch-event.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define BUILTINS_FETCH_EVENT_H
33

44
#include "builtin.h"
5-
#include "host_interface/fastly.h"
65
#include "host_interface/host_api.h"
76

87
namespace builtins {

runtime/js-compute-runtime/builtins/kv-store.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "builtins/kv-store.h"
1919
#include "builtins/native-stream-source.h"
2020
#include "builtins/shared/url.h"
21-
#include "host_interface/fastly.h"
2221
#include "host_interface/host_api.h"
22+
#include "host_interface/host_call.h"
2323
#include "js-compute-builtins.h"
2424

2525
namespace builtins {
@@ -434,7 +434,7 @@ bool KVStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
434434

435435
auto res = ObjectStore::open(name_str);
436436
if (auto *err = res.to_err()) {
437-
if (*err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_INVALID_ARGUMENT) {
437+
if (error_is_invalid_argument(*err)) {
438438
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_KV_STORE_DOES_NOT_EXIST,
439439
name_str.data());
440440
return false;

runtime/js-compute-runtime/builtins/secret-store.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "secret-store.h"
2-
#include "host_interface/fastly.h"
32
#include "host_interface/host_api.h"
43

54
namespace builtins {
@@ -195,7 +194,7 @@ bool SecretStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
195194

196195
auto res = host_api::SecretStore::open(name);
197196
if (auto *err = res.to_err()) {
198-
if (*err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_OPTIONAL_NONE) {
197+
if (error_is_optional_none(*err)) {
199198
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_DOES_NOT_EXIST,
200199
name.data());
201200
return false;

runtime/js-compute-runtime/host_interface/host_api.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ static_assert(
4040
static_assert(std::is_same_v<Dict::Handle, fastly_compute_at_edge_fastly_dictionary_handle_t>);
4141
static_assert(
4242
std::is_same_v<ObjectStore::Handle, fastly_compute_at_edge_fastly_object_store_handle_t>);
43+
static_assert(std::is_same_v<HttpVersion, fastly_compute_at_edge_fastly_http_version_t>);
44+
static_assert(
45+
std::is_same_v<CacheOverrideTag, fastly_compute_at_edge_fastly_http_cache_override_tag_t>);
46+
static_assert(std::is_same_v<TlsVersion, fastly_compute_at_edge_fastly_tls_version_t>);
4347

4448
Result<bool> AsyncHandle::is_ready() const {
4549
Result<bool> res;

runtime/js-compute-runtime/host_interface/host_call.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11

22
#include <type_traits>
33

4+
#include "fastly-world/fastly_world.h"
45
#include "host_interface/fastly.h"
56
#include "host_interface/host_call.h"
67

78
// Ensure that our type synonyms match what's generated by wit-bindgen.
89
static_assert(std::is_same_v<FastlyError, fastly_compute_at_edge_fastly_error_t>);
910

11+
bool error_is_generic(FastlyError e) {
12+
return e == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_GENERIC_ERROR;
13+
}
14+
15+
bool error_is_invalid_argument(FastlyError e) {
16+
return e == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_INVALID_ARGUMENT;
17+
}
18+
19+
bool error_is_optional_none(FastlyError e) {
20+
return e == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_OPTIONAL_NONE;
21+
}
22+
23+
bool error_is_bad_handle(FastlyError e) {
24+
return e == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_BAD_HANDLE;
25+
}
26+
1027
/* Returns false if an exception is set on `cx` and the caller should
1128
immediately return to propagate the exception. */
1229
void handle_fastly_error(JSContext *cx, FastlyError err, int line, const char *func) {

runtime/js-compute-runtime/host_interface/host_call.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
using FastlyError = uint8_t;
1414

15+
bool error_is_generic(FastlyError e);
16+
bool error_is_invalid_argument(FastlyError e);
17+
bool error_is_optional_none(FastlyError e);
18+
bool error_is_bad_handle(FastlyError e);
19+
1520
void handle_fastly_error(JSContext *cx, FastlyError err, int line, const char *func);
1621

1722
#define HANDLE_ERROR(cx, err) handle_fastly_error(cx, err, __LINE__, __func__)

runtime/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <strings.h>
1010
#include <vector>
1111

12-
#include "fastly-world/fastly_world.h"
1312
#include "js-compute-builtins.h"
1413
#include "rust-url/rust-url.h"
1514

@@ -33,6 +32,7 @@
3332
#include "js/shadow/Object.h"
3433
#include "zlib.h"
3534

35+
#include "host_interface/fastly.h"
3636
#include "host_interface/host_api.h"
3737
#include "host_interface/host_call.h"
3838

@@ -806,10 +806,11 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
806806
}
807807
}
808808

809-
size_t backend_len;
810-
UniqueChars backend_chars = encode(cx, backend, &backend_len);
811-
if (!backend_chars)
809+
HostString backend_chars;
810+
backend_chars.ptr = encode(cx, backend, &backend_chars.len);
811+
if (!backend_chars.ptr) {
812812
return ReturnPromiseRejectedWithPendingError(cx, args);
813+
}
813814

814815
RootedObject response_promise(cx, JS::NewPromiseObject(cx, nullptr));
815816
if (!response_promise)
@@ -824,32 +825,24 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
824825
return false;
825826
}
826827

827-
fastly_world_string_t backend_str = {backend_chars.get(), backend_len};
828-
829-
fastly_compute_at_edge_fastly_pending_request_handle_t pending_handle = INVALID_HANDLE;
828+
HttpPendingReq pending_handle;
830829
{
831-
fastly_compute_at_edge_fastly_error_t err;
832-
bool ok;
833830
auto request_handle = builtins::Request::request_handle(request);
834831
auto body = builtins::RequestOrResponse::body_handle(request);
835-
if (streaming) {
836-
ok = fastly_compute_at_edge_fastly_http_req_send_async_streaming(
837-
request_handle.handle, body.handle, &backend_str, &pending_handle, &err);
838-
} else {
839-
ok = fastly_compute_at_edge_fastly_http_req_send_async(request_handle.handle, body.handle,
840-
&backend_str, &pending_handle, &err);
841-
}
832+
auto res = streaming ? request_handle.send_async_streaming(body, backend_chars)
833+
: request_handle.send_async(body, backend_chars);
842834

843-
if (!ok) {
844-
if (err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_GENERIC_ERROR ||
845-
err == FASTLY_COMPUTE_AT_EDGE_FASTLY_ERROR_INVALID_ARGUMENT) {
835+
if (auto *err = res.to_err()) {
836+
if (error_is_generic(*err) || error_is_invalid_argument(*err)) {
846837
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
847-
JSMSG_REQUEST_BACKEND_DOES_NOT_EXIST, backend_chars.get());
838+
JSMSG_REQUEST_BACKEND_DOES_NOT_EXIST, backend_chars.ptr.get());
848839
} else {
849-
HANDLE_ERROR(cx, err);
840+
HANDLE_ERROR(cx, *err);
850841
}
851842
return ReturnPromiseRejectedWithPendingError(cx, args);
852843
}
844+
845+
pending_handle = res.unwrap();
853846
}
854847

855848
// If the request body is streamed, we need to wait for streaming to complete before marking the
@@ -860,7 +853,7 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
860853
}
861854

862855
JS::SetReservedSlot(request, static_cast<uint32_t>(builtins::Request::Slots::PendingRequest),
863-
JS::Int32Value(pending_handle));
856+
JS::Int32Value(pending_handle.handle));
864857
JS::SetReservedSlot(request, static_cast<uint32_t>(builtins::Request::Slots::ResponsePromise),
865858
JS::ObjectValue(*response_promise));
866859

0 commit comments

Comments
 (0)