Skip to content

Commit 6bad106

Browse files
authored
chore: Remove HttpBodyChunk in favor of HostString (#451)
Cleanup the host_api interface a bit, by standardizing more on HostString.
1 parent 7179c17 commit 6bad106

File tree

5 files changed

+24
-32
lines changed

5 files changed

+24
-32
lines changed

c-dependencies/js-compute-runtime/builtins/request-response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct ReadResult {
5959
// Returns a UniqueChars and the length of that string. The UniqueChars value is not
6060
// null-terminated.
6161
ReadResult read_from_handle_all(JSContext *cx, uint32_t handle) {
62-
std::vector<HttpBodyChunk> chunks;
62+
std::vector<HostString> chunks;
6363
size_t bytes_read = 0;
6464
HttpBody body{handle};
6565
while (true) {

c-dependencies/js-compute-runtime/builtins/request-response.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "builtin.h"
55
#include "builtins/headers.h"
6-
#include "c-at-e-world/c_at_e_world.h"
6+
#include "host_interface/host_api.h"
77

88
namespace builtins {
99

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Result<HttpBody> HttpBody::make() {
1818
return res;
1919
}
2020

21-
Result<HttpBodyChunk> HttpBody::read(uint32_t chunk_size) const {
22-
Result<HttpBodyChunk> res;
21+
Result<HostString> HttpBody::read(uint32_t chunk_size) const {
22+
Result<HostString> res;
2323

2424
fastly_list_u8_t ret;
2525
fastly_error_t err;

c-dependencies/js-compute-runtime/host_interface/host_api.h

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,27 @@ template <typename T> class Result final {
6767
T &unwrap() { return std::get<T>(this->result); }
6868
};
6969

70-
/// A single chunk read from an HttpBody.
71-
struct HttpBodyChunk final {
70+
/// A string allocated by the host interface. Holds ownership of the data.
71+
struct HostString final {
7272
JS::UniqueChars ptr;
7373
size_t len;
7474

75-
HttpBodyChunk() = default;
76-
HttpBodyChunk(JS::UniqueChars ptr, size_t len) : ptr{std::move(ptr)}, len{len} {}
75+
HostString() = default;
76+
explicit HostString(c_at_e_world_string_t str) : ptr{str.ptr}, len{str.len} {}
77+
HostString(JS::UniqueChars ptr, size_t len) : ptr{std::move(ptr)}, len{len} {}
78+
79+
using iterator = char *;
80+
81+
size_t size() const { return this->len; }
82+
83+
char *begin() { return this->ptr.get(); }
84+
char *end() { return this->begin() + this->len; }
85+
86+
const char *begin() const { return this->ptr.get(); }
87+
const char *end() const { return this->begin() + this->len; }
88+
89+
/// Conversion to a `std::string_view`.
90+
operator std::string_view() { return std::string_view(this->ptr.get(), this->len); }
7791
};
7892

7993
/// A convenience wrapper for the host calls involving http bodies.
@@ -95,7 +109,7 @@ class HttpBody final {
95109
static Result<HttpBody> make();
96110

97111
/// Read a chunk from this handle.
98-
Result<HttpBodyChunk> read(uint32_t chunk_size) const;
112+
Result<HostString> read(uint32_t chunk_size) const;
99113

100114
/// Write a chunk to this handle.
101115
Result<uint32_t> write(const uint8_t *bytes, size_t len) const;
@@ -113,28 +127,6 @@ class HttpBody final {
113127
Result<Void> close();
114128
};
115129

116-
struct HostString final {
117-
JS::UniqueChars ptr;
118-
size_t len;
119-
120-
HostString() = default;
121-
explicit HostString(c_at_e_world_string_t str) : ptr{str.ptr}, len{str.len} {}
122-
HostString(JS::UniqueChars ptr, size_t len) : ptr{std::move(ptr)}, len{len} {}
123-
124-
using iterator = char *;
125-
126-
size_t size() const { return this->len; }
127-
128-
char *begin() { return this->ptr.get(); }
129-
char *end() { return this->begin() + this->len; }
130-
131-
const char *begin() const { return this->ptr.get(); }
132-
const char *end() const { return this->begin() + this->len; }
133-
134-
/// Conversion to a `std::string_view`.
135-
operator std::string_view() { return std::string_view(this->ptr.get(), this->len); }
136-
};
137-
138130
class HttpBase {
139131
public:
140132
virtual ~HttpBase() = default;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct ReadResult {
161161
// Returns a UniqueChars and the length of that string. The UniqueChars value is not
162162
// null-terminated.
163163
ReadResult read_from_handle_all(JSContext *cx, uint32_t handle) {
164-
std::vector<HttpBodyChunk> chunks;
164+
std::vector<HostString> chunks;
165165
size_t bytes_read = 0;
166166
HttpBody body{handle};
167167
while (true) {

0 commit comments

Comments
 (0)