diff --git a/src/core/modules/conduit.cc b/src/core/modules/conduit.cc index 66a170e4ab..c3531697de 100644 --- a/src/core/modules/conduit.cc +++ b/src/core/modules/conduit.cc @@ -383,11 +383,16 @@ namespace SSC { bytes, size, [client](const auto result) { - const auto data = result.json().str(); - const auto size = data.size(); - const auto payload = std::make_shared(size); - memcpy(payload.get(), data.c_str(), size); - client->emit({}, payload, size); + auto token = result.token; + if (result.post.body != nullptr && result.post.length > 0) { + client->emit({{"token", token}}, result.post.body, result.post.length); + } else { + const auto data = result.json().str(); + const auto size = data.size(); + const auto payload = std::make_shared(size); + memcpy(payload.get(), data.c_str(), size); + client->emit({{"token", token}}, payload, size); + } } ); }); diff --git a/src/ipc/routes.cc b/src/ipc/routes.cc index b101c3aff4..cd2a332576 100644 --- a/src/ipc/routes.cc +++ b/src/ipc/routes.cc @@ -2,6 +2,7 @@ #include "../cli/cli.hh" #include "../core/json.hh" #include "../core/resource.hh" +#include "../core/headers.hh" #include "../extension/extension.hh" #include "../window/window.hh" #include "ipc.hh" @@ -2441,6 +2442,60 @@ static void mapIPCRoutes (Router *router) { return reply(Result::Data { message, json }); }); + /** + * @param method + * @param scheme + * @param hostname + * @param pathname + * @param query + */ + router->map("serviceWorker.fetch", [=](auto message, auto router, auto reply) { + const auto fetch = ServiceWorkerContainer::FetchRequest { + message.get("method", "GET"), + message.get("scheme", "socket"), + message.get("hostname", router->bridge->userConfig["meta_bundle_identifier"]), + message.get("pathname", "/"), + message.get("query", ""), + Headers(message.get("headers", "")), + ServiceWorkerContainer::FetchBody { message.buffer.size, message.buffer.bytes }, + router->bridge->client + }; + + const auto fetched = router->bridge->navigator.serviceWorker.fetch(fetch, [=] (auto res) mutable { + if (res.statusCode == 0) { + return reply(Result::Err { + message, + JSON::Object::Entries { + {"message", "ServiceWorker request failed"} + } + }); + } else { + reply(Result { + message.seq, + message, + JSON::Object {}, + Post { + rand64(), + 0, + res.body.bytes, + res.body.size, + res.headers.str() + } + }); + } + }); + + if (!fetched) { + return reply(Result::Err { + message, + JSON::Object::Entries { + {"message", "Not found"}, + {"type", "NotFoundError"} + } + }); + } + }); + /** * Informs container that a service worker will skip waiting. * @param id