forked from cloudflare/workerd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "workerd.h" | ||
#include <kj/compat/http.h> | ||
#include <capnp/compat/json.h> | ||
#include <workerd/util/http-util.h> | ||
|
||
namespace workerd::api { | ||
|
||
kj::Promise<uint> doCreateWorkerRequest(kj::Own<kj::HttpClient> client, kj::String serializedArgs) { | ||
auto& context = IoContext::current(); | ||
auto headers = kj::HttpHeaders(context.getHeaderTable()); | ||
auto req = client->request(kj::HttpMethod::POST, "http://workerd.local/workers"_kjc, headers, serializedArgs.size()); | ||
co_await req.body->write(serializedArgs.begin(), serializedArgs.size()); | ||
auto res = co_await req.response; | ||
auto resBody = co_await res.body->readAllText(); | ||
if (res.statusCode >= 400) { | ||
JSG_FAIL_REQUIRE(Error, resBody); | ||
} | ||
co_return atoi(resBody.cStr()); | ||
} | ||
|
||
jsg::Promise<jsg::Ref<Fetcher>> WorkerdApi::newWorker(jsg::Lock& js, jsg::JsValue args) { | ||
auto& context = IoContext::current(); | ||
kj::String serializedArgs = args.toJson(js); | ||
auto client = context.getHttpClient(serviceChannel, true, kj::none, "create_worker"_kjc); | ||
auto promise = doCreateWorkerRequest(kj::mv(client), kj::mv(serializedArgs)); | ||
return context.awaitIo(js, kj::mv(promise), [](jsg::Lock& js, uint chan) { | ||
return jsg::alloc<Fetcher>(chan, Fetcher::RequiresHostAndProtocol::NO, true); | ||
}); | ||
} | ||
|
||
} // namespace workerd::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <workerd/jsg/jsg.h> | ||
#include <workerd/api/http.h> | ||
|
||
namespace workerd::api { | ||
|
||
// A special binding object that allows for dynamic evaluation. | ||
class WorkerdApi: public jsg::Object { | ||
public: | ||
explicit WorkerdApi(uint serviceChannel): serviceChannel(serviceChannel) {} | ||
|
||
jsg::Promise<jsg::Ref<Fetcher>> newWorker(jsg::Lock& js, jsg::JsValue args); | ||
|
||
JSG_RESOURCE_TYPE(WorkerdApi) { | ||
JSG_METHOD(newWorker); | ||
} | ||
|
||
private: | ||
uint serviceChannel; | ||
}; | ||
#define EW_WORKERD_API_ISOLATE_TYPES \ | ||
api::WorkerdApi | ||
} // namespace workerd::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters