Skip to content

Commit

Permalink
refactor(ipc): support service worker fetch over IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Nov 23, 2024
1 parent ab839c8 commit 32f2a8f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/modules/conduit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char[]>(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<char[]>(size);
memcpy(payload.get(), data.c_str(), size);
client->emit({{"token", token}}, payload, size);
}
}
);
});
Expand Down
55 changes: 55 additions & 0 deletions src/ipc/routes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32f2a8f

Please sign in to comment.