feat(device-plane): add typed B2 service ping transport
This commit is contained in:
@@ -56,6 +56,7 @@ export function createControlCoreApp({
|
||||
managementToken = "",
|
||||
gatewayIngest = null,
|
||||
edgeChannelStatusProvider = null,
|
||||
typedCommandRuntime = null,
|
||||
} = {}) {
|
||||
if (!repository || typeof repository.health !== "function") {
|
||||
throw new TypeError("device_repository_required");
|
||||
@@ -97,6 +98,15 @@ export function createControlCoreApp({
|
||||
) {
|
||||
throw new TypeError("device_gateway_ingest_invalid");
|
||||
}
|
||||
if (
|
||||
typedCommandRuntime != null
|
||||
&& (
|
||||
typeof typedCommandRuntime.planServicePing !== "function"
|
||||
|| typeof typedCommandRuntime.status !== "function"
|
||||
)
|
||||
) {
|
||||
throw new TypeError("device_typed_command_runtime_invalid");
|
||||
}
|
||||
if (
|
||||
edgeChannelStatusProvider != null
|
||||
&& typeof edgeChannelStatusProvider !== "function"
|
||||
@@ -126,7 +136,47 @@ export function createControlCoreApp({
|
||||
edgeChannels: edgeChannelStatusProvider
|
||||
? edgeChannelStatusProvider()
|
||||
: { enabled: false, configured: 0, accepted: 0, degraded: 0 },
|
||||
commandTransport: "disabled",
|
||||
commandTransport: typedCommandRuntime
|
||||
? "typed-service-ping-v1"
|
||||
: "disabled",
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
request.method === "POST"
|
||||
&& requestUrl.pathname === "/internal/v1/commands:service-ping"
|
||||
) {
|
||||
if (!managementApiEnabled || !typedCommandRuntime) {
|
||||
return writeJson(response, 404, {
|
||||
ok: false,
|
||||
error: "device_command_transport_disabled",
|
||||
});
|
||||
}
|
||||
if (!matchesBearer(request.headers.authorization, managementToken)) {
|
||||
return writeJson(response, 401, {
|
||||
ok: false,
|
||||
error: "device_management_auth_required",
|
||||
});
|
||||
}
|
||||
const idempotencyKey = normalizeIdempotencyKey(
|
||||
request.headers["idempotency-key"],
|
||||
);
|
||||
const actor = managementActorFromHeaders(request.headers);
|
||||
const input = await readJsonBody(request, 8 * 1024);
|
||||
const execution = await typedCommandRuntime.planServicePing({
|
||||
idempotencyKey,
|
||||
actor,
|
||||
input,
|
||||
});
|
||||
response.setHeader("Idempotency-Key", idempotencyKey);
|
||||
response.setHeader(
|
||||
"Idempotency-Replayed",
|
||||
execution.replayed ? "true" : "false",
|
||||
);
|
||||
return writeJson(response, 200, {
|
||||
ok: true,
|
||||
replayed: execution.replayed,
|
||||
result: execution.command,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -232,6 +282,11 @@ export function createControlCoreApp({
|
||||
const workspace = await repository.getProjectWorkspace(
|
||||
actor,
|
||||
workspaceProjectId,
|
||||
{
|
||||
commandTransport: typedCommandRuntime
|
||||
? "typed-service-ping-v1"
|
||||
: "disabled",
|
||||
},
|
||||
);
|
||||
return writeJson(response, 200, { ok: true, workspace });
|
||||
}
|
||||
@@ -280,7 +335,8 @@ export function createControlCoreApp({
|
||||
}
|
||||
|
||||
const input = await readJsonBody(request, 1024 * 1024);
|
||||
const acceptance = await ingest.acceptMessage(input);
|
||||
const receipt = await ingest.acceptMessage(input);
|
||||
const acceptance = receipt.value;
|
||||
return writeJson(response, acceptance.replayed ? 200 : 201, {
|
||||
ok: true,
|
||||
acceptance,
|
||||
|
||||
Reference in New Issue
Block a user