Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to inject an arbitrary set of headers to KV requests #135

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/workerd/api/kv.c++
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ jsg::Promise<KvNamespace::GetWithMetadataResult> KvNamespace::getWithMetadata(
auto urlStr = url.toString(kj::Url::Context::HTTP_PROXY_REQUEST);
auto headers = kj::HttpHeaders(context.getHeaderTable());
headers.add(FLPROD_405_HEADER, kj::str(urlStr));
for (const auto& header: additionalHeaders) {
headers.add(header.name.asPtr(), header.value.asPtr());
}

return context.awaitIo(js,
client->request(kj::HttpMethod::GET, urlStr, headers).response,
Expand Down Expand Up @@ -211,6 +214,9 @@ jsg::Promise<jsg::Value> KvNamespace::list(
auto urlStr = url.toString(kj::Url::Context::HTTP_PROXY_REQUEST);
auto headers = kj::HttpHeaders(context.getHeaderTable());
headers.add(FLPROD_405_HEADER, kj::str(urlStr));
for (const auto& header: additionalHeaders) {
headers.add(header.name.asPtr(), header.value.asPtr());
}

return context.awaitIo(js,
client->request(kj::HttpMethod::GET, urlStr, headers).response,
Expand Down Expand Up @@ -311,6 +317,9 @@ jsg::Promise<void> KvNamespace::put(

auto urlStr = url.toString(kj::Url::Context::HTTP_PROXY_REQUEST);
headers.add(FLPROD_405_HEADER, kj::str(urlStr));
for (const auto& header: additionalHeaders) {
headers.add(header.name.asPtr(), header.value.asPtr());
}

auto promise = context.waitForOutputLocks()
.then([&context, client = kj::mv(client), urlStr = kj::mv(urlStr), headers = kj::mv(headers),
Expand Down Expand Up @@ -367,9 +376,13 @@ jsg::Promise<void> KvNamespace::delete_(jsg::Lock& js, kj::String name) {
auto client = context.getHttpClient(subrequestChannel, true, nullptr, "kv_delete"_kj);
auto urlStr = kj::str("https://fake-host/", kj::encodeUriComponent(name), "?urlencoded=true");
auto promise = context.waitForOutputLocks()
.then([&context, client = kj::mv(client), urlStr = kj::mv(urlStr)]() mutable {
.then([this, &context, client = kj::mv(client), urlStr = kj::mv(urlStr)]() mutable {
auto headers = kj::HttpHeaders(context.getHeaderTable());
headers.add(FLPROD_405_HEADER, kj::str(urlStr));
for (const auto& header: additionalHeaders) {
headers.add(header.name.asPtr(), header.value.asPtr());
}

return client->request(kj::HttpMethod::DELETE, urlStr, headers,
uint64_t(0))
.response.then([](kj::HttpClient::Response&& response) mutable {
Expand Down
10 changes: 9 additions & 1 deletion src/workerd/api/kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ class KvNamespace: public jsg::Object {
// A capability to a KV namespace.

public:
explicit KvNamespace(uint subrequestChannel): subrequestChannel(subrequestChannel) {}
struct AdditionalHeader {
kj::String name;
kj::String value;
};

explicit KvNamespace(kj::Array<AdditionalHeader> additionalHeaders, uint subrequestChannel)
: additionalHeaders(kj::mv(additionalHeaders)), subrequestChannel(subrequestChannel) {}
// `subrequestChannel` is what to pass to IoContext::getHttpClient() to get an HttpClient
// representing this namespace.
// `additionalHeaders` is what gets appended to every outbound request.

struct GetOptions {
jsg::Optional<kj::String> type;
Expand Down Expand Up @@ -90,6 +97,7 @@ class KvNamespace: public jsg::Object {
}

private:
kj::Array<AdditionalHeader> additionalHeaders;
uint subrequestChannel;
};

Expand Down
3 changes: 2 additions & 1 deletion src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ void WorkerdApiIsolate::compileGlobals(
}

KJ_CASE_ONEOF(ns, Global::KvNamespace) {
value = lock.wrap(context, jsg::alloc<api::KvNamespace>(ns.subrequestChannel));
value = lock.wrap(context, jsg::alloc<api::KvNamespace>(
kj::Array<api::KvNamespace::AdditionalHeader>{}, ns.subrequestChannel));
}

KJ_CASE_ONEOF(r2, Global::R2Bucket) {
Expand Down