@capybara-hq/plugin-device-local 0.1.0-rc.8 → 0.1.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.capybara-plugin/plugin.json +2 -2
- package/dist/index.mjs +167 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"name": "device-local",
|
|
4
|
-
"version": "0.1.0-rc.
|
|
4
|
+
"version": "0.1.0-rc.9",
|
|
5
5
|
"publisher": "Capybara",
|
|
6
6
|
"description": {
|
|
7
7
|
"en": "Devices on people's own computers: a machine connected with capybara-device serves the device over the dial-in channel, and its owner limits it there",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"entry": "./dist/index.mjs",
|
|
24
24
|
"manifest": {
|
|
25
25
|
"name": "device-local",
|
|
26
|
-
"version": "0.1.0-rc.
|
|
26
|
+
"version": "0.1.0-rc.9",
|
|
27
27
|
"sdk": "^0.1.0-rc.56",
|
|
28
28
|
"provides": [
|
|
29
29
|
"device-provider@1"
|
package/dist/index.mjs
CHANGED
|
@@ -4449,6 +4449,7 @@ import {
|
|
|
4449
4449
|
} from "@capybara-hq/plugin-sdk";
|
|
4450
4450
|
|
|
4451
4451
|
// src/frames.ts
|
|
4452
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
4452
4453
|
import {
|
|
4453
4454
|
FileSystemGoneError,
|
|
4454
4455
|
SandboxGoneError,
|
|
@@ -4549,6 +4550,61 @@ function failureOf(error, op) {
|
|
|
4549
4550
|
return new ToolError(`${ON_MACHINE_PREFIX}${message} (check the path or command and retry)`);
|
|
4550
4551
|
}
|
|
4551
4552
|
}
|
|
4553
|
+
var SERVICE_OP_DEADLINE_MS = 6e4;
|
|
4554
|
+
var ServiceOpRefusedError = class extends ToolError {
|
|
4555
|
+
constructor(message, refusal) {
|
|
4556
|
+
super(message);
|
|
4557
|
+
this.refusal = refusal;
|
|
4558
|
+
}
|
|
4559
|
+
refusal;
|
|
4560
|
+
};
|
|
4561
|
+
function filesDigestOf(files) {
|
|
4562
|
+
const h = createHash2("sha256");
|
|
4563
|
+
for (const path of Object.keys(files).sort()) {
|
|
4564
|
+
const data = files[path];
|
|
4565
|
+
h.update(`${path}\0${data.byteLength}\0`);
|
|
4566
|
+
h.update(data);
|
|
4567
|
+
}
|
|
4568
|
+
return `sha256:${h.digest("hex")}`;
|
|
4569
|
+
}
|
|
4570
|
+
function serviceFailure(error, op) {
|
|
4571
|
+
const message = typeof error.message === "string" ? error.message : "no message";
|
|
4572
|
+
switch (error.kind) {
|
|
4573
|
+
case "unsupported":
|
|
4574
|
+
return new SandboxPermissionUnsupportedError(
|
|
4575
|
+
`${message} (update capybara-device on that machine to use device services)`
|
|
4576
|
+
);
|
|
4577
|
+
case "denied":
|
|
4578
|
+
return new SandboxScopeDeniedError(message);
|
|
4579
|
+
case "files_needed":
|
|
4580
|
+
return new ServiceOpRefusedError(
|
|
4581
|
+
`The connected machine needs the service files for ${op} (retry with the files)`,
|
|
4582
|
+
"files_needed"
|
|
4583
|
+
);
|
|
4584
|
+
case "invalid":
|
|
4585
|
+
return new ServiceOpRefusedError(
|
|
4586
|
+
`The connected machine refused the ${op} request: ${message} (check the request and retry)`,
|
|
4587
|
+
"invalid"
|
|
4588
|
+
);
|
|
4589
|
+
default:
|
|
4590
|
+
return new ToolError(`${ON_MACHINE_PREFIX}${message} (check the service and retry)`);
|
|
4591
|
+
}
|
|
4592
|
+
}
|
|
4593
|
+
async function serviceCall(dialIn, deviceId, body, opts) {
|
|
4594
|
+
const raw = await dialIn.request(deviceId, body, {
|
|
4595
|
+
timeoutMs: opts.deadlineMs,
|
|
4596
|
+
signal: opts.signal
|
|
4597
|
+
});
|
|
4598
|
+
const res = raw;
|
|
4599
|
+
if (typeof res !== "object" || res === null || typeof res.ok !== "boolean")
|
|
4600
|
+
throw invalidAnswer(body.op, "not a response body");
|
|
4601
|
+
if (!res.ok) {
|
|
4602
|
+
const e = res.error;
|
|
4603
|
+
if (typeof e !== "object" || e === null) throw invalidAnswer(body.op, "no error body");
|
|
4604
|
+
throw serviceFailure(e, body.op);
|
|
4605
|
+
}
|
|
4606
|
+
return res.result;
|
|
4607
|
+
}
|
|
4552
4608
|
|
|
4553
4609
|
// src/store.ts
|
|
4554
4610
|
var viaOf = (c) => c ? { machineId: c.machineId, connectionId: c.connectionId } : null;
|
|
@@ -4706,6 +4762,21 @@ function commandLimits(h, scope) {
|
|
|
4706
4762
|
const network = scope.network.kind === "open-public" ? { kind: "open-public", hosts: [] } : scope.network.kind === "allowlist" ? { kind: "allowlist", hosts: [...scope.network.hosts] } : scope.network.kind === "none" ? { kind: "allowlist", hosts: [] } : { kind: h.limits.network.kind, hosts: [...h.limits.network.hosts] };
|
|
4707
4763
|
return { cpu, memoryMb, diskMb, network };
|
|
4708
4764
|
}
|
|
4765
|
+
var isAbsolutePath = (v) => typeof v === "string" && v.startsWith("/") && !v.includes("\0");
|
|
4766
|
+
function statusOf(v) {
|
|
4767
|
+
const s = v;
|
|
4768
|
+
if (typeof s !== "object" || s === null || typeof s.name !== "string" || s.state !== "running" && s.state !== "stopped" || s.startedAt !== null && typeof s.startedAt !== "string" || typeof s.stateDir !== "string" || s.codeDir !== void 0 && !isAbsolutePath(s.codeDir))
|
|
4769
|
+
throw new ToolError2(
|
|
4770
|
+
"The connected machine sent an invalid service status; update capybara-device on that machine and retry"
|
|
4771
|
+
);
|
|
4772
|
+
return {
|
|
4773
|
+
name: s.name,
|
|
4774
|
+
state: s.state,
|
|
4775
|
+
startedAt: s.startedAt,
|
|
4776
|
+
stateDir: s.stateDir,
|
|
4777
|
+
...s.codeDir !== void 0 && { codeDir: s.codeDir }
|
|
4778
|
+
};
|
|
4779
|
+
}
|
|
4709
4780
|
function requireLocal(h) {
|
|
4710
4781
|
const l = localHandle(h);
|
|
4711
4782
|
if (!l) throw new SandboxGoneError2("The instance handle is not a connected machine's instance");
|
|
@@ -4937,6 +5008,100 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4937
5008
|
async killTagged(_h, _tags, signal) {
|
|
4938
5009
|
signal.throwIfAborted();
|
|
4939
5010
|
},
|
|
5011
|
+
/**
|
|
5012
|
+
* Starts the service on the machine (or finds it running) with the device's current network
|
|
5013
|
+
* limits, which the machine compares with the running service's and restarts it on a change.
|
|
5014
|
+
* `ifRunning` starts nothing: a stopped service answers `stopped`.
|
|
5015
|
+
*/
|
|
5016
|
+
async ensureService(h, spec, signal, o) {
|
|
5017
|
+
const l = requireLocal(h);
|
|
5018
|
+
const network = { kind: l.limits.network.kind, hosts: [...l.limits.network.hosts] };
|
|
5019
|
+
const opts2 = { deadlineMs: SERVICE_OP_DEADLINE_MS, signal };
|
|
5020
|
+
if (o?.ifRunning)
|
|
5021
|
+
return statusOf(
|
|
5022
|
+
await serviceCall(
|
|
5023
|
+
ctx.dialIn,
|
|
5024
|
+
l.id,
|
|
5025
|
+
{ op: "service.ensure", spec: { name: spec.name }, network, ifRunning: true },
|
|
5026
|
+
opts2
|
|
5027
|
+
)
|
|
5028
|
+
);
|
|
5029
|
+
const filesDigest = filesDigestOf(spec.files);
|
|
5030
|
+
const send = async (files2) => statusOf(
|
|
5031
|
+
await serviceCall(
|
|
5032
|
+
ctx.dialIn,
|
|
5033
|
+
l.id,
|
|
5034
|
+
{
|
|
5035
|
+
op: "service.ensure",
|
|
5036
|
+
network,
|
|
5037
|
+
spec: {
|
|
5038
|
+
name: spec.name,
|
|
5039
|
+
entry: spec.entry,
|
|
5040
|
+
args: spec.args,
|
|
5041
|
+
...files2 && { files: files2 },
|
|
5042
|
+
...spec.env && { env: spec.env }
|
|
5043
|
+
},
|
|
5044
|
+
filesDigest
|
|
5045
|
+
},
|
|
5046
|
+
opts2
|
|
5047
|
+
)
|
|
5048
|
+
);
|
|
5049
|
+
try {
|
|
5050
|
+
return await send();
|
|
5051
|
+
} catch (e) {
|
|
5052
|
+
if (!(e instanceof ServiceOpRefusedError)) throw e;
|
|
5053
|
+
}
|
|
5054
|
+
const files = {};
|
|
5055
|
+
let size = 0;
|
|
5056
|
+
for (const [path, data] of Object.entries(spec.files)) {
|
|
5057
|
+
files[path] = b64Of(data);
|
|
5058
|
+
size += files[path].length + path.length;
|
|
5059
|
+
}
|
|
5060
|
+
const cap = await frameCapacity(ctx.dialIn, l.id);
|
|
5061
|
+
if (size + FRAME_ENVELOPE_BYTES > cap)
|
|
5062
|
+
throw new ToolError2(
|
|
5063
|
+
`The service files for ${spec.name} (${size} bytes encoded) do not fit in one frame of the connected machine (${cap} bytes); raise CAPYBARA_DEVICES__DIAL_IN__MAX_FRAME_BYTES on the deployment and the machine, or use a smaller service`
|
|
5064
|
+
);
|
|
5065
|
+
return await send(files);
|
|
5066
|
+
},
|
|
5067
|
+
async stopService(h, name, signal) {
|
|
5068
|
+
const l = requireLocal(h);
|
|
5069
|
+
await serviceCall(
|
|
5070
|
+
ctx.dialIn,
|
|
5071
|
+
l.id,
|
|
5072
|
+
{ op: "service.stop", name },
|
|
5073
|
+
{ deadlineMs: SERVICE_OP_DEADLINE_MS, signal }
|
|
5074
|
+
);
|
|
5075
|
+
},
|
|
5076
|
+
async serviceStatus(h, name, signal) {
|
|
5077
|
+
const l = requireLocal(h);
|
|
5078
|
+
return statusOf(
|
|
5079
|
+
await serviceCall(
|
|
5080
|
+
ctx.dialIn,
|
|
5081
|
+
l.id,
|
|
5082
|
+
{ op: "service.status", name },
|
|
5083
|
+
{ deadlineMs: OP_DEADLINE_MS, signal }
|
|
5084
|
+
)
|
|
5085
|
+
);
|
|
5086
|
+
},
|
|
5087
|
+
async serviceRequest(h, name, body, o) {
|
|
5088
|
+
const l = requireLocal(h);
|
|
5089
|
+
const result = await serviceCall(
|
|
5090
|
+
ctx.dialIn,
|
|
5091
|
+
l.id,
|
|
5092
|
+
{ op: "service.request", name, body: b64Of(body), timeoutMs: o.timeoutMs },
|
|
5093
|
+
{ deadlineMs: o.timeoutMs + EXEC_DEADLINE_MARGIN_MS, signal: o.signal }
|
|
5094
|
+
);
|
|
5095
|
+
if (typeof result?.body !== "string")
|
|
5096
|
+
throw new ToolError2(
|
|
5097
|
+
"The connected machine sent an invalid answer to service.request; update capybara-device on that machine and retry"
|
|
5098
|
+
);
|
|
5099
|
+
return bytesOf(result.body);
|
|
5100
|
+
},
|
|
5101
|
+
async openStream(h, target, signal) {
|
|
5102
|
+
const l = requireLocal(h);
|
|
5103
|
+
return await ctx.dialIn.openStream(l.id, { service: target.service }, { signal });
|
|
5104
|
+
},
|
|
4940
5105
|
exec: refuseRaw("exec"),
|
|
4941
5106
|
readFile: refuseRaw("readFile"),
|
|
4942
5107
|
writeFile: refuseRaw("writeFile"),
|
|
@@ -4949,7 +5114,7 @@ function createDeviceLocalProvider(ctx, store, opts = {}) {
|
|
|
4949
5114
|
// src/index.ts
|
|
4950
5115
|
var plugin = defineExtension({
|
|
4951
5116
|
name: "device-local",
|
|
4952
|
-
version: "0.1.0-rc.
|
|
5117
|
+
version: "0.1.0-rc.9",
|
|
4953
5118
|
// ctx.dialIn.capacity and the handlers' connection argument are the rc.56 SDK's
|
|
4954
5119
|
sdk: "^0.1.0-rc.56",
|
|
4955
5120
|
provides: [DEVICE_PROVIDER_EXTENSION_POINT],
|
|
@@ -4996,6 +5161,7 @@ export {
|
|
|
4996
5161
|
deviceIdOfName,
|
|
4997
5162
|
deviceReportOf,
|
|
4998
5163
|
failureOf,
|
|
5164
|
+
filesDigestOf,
|
|
4999
5165
|
frameCapacity,
|
|
5000
5166
|
machineCall,
|
|
5001
5167
|
maxReadBytes,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/plugin-device-local",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": "Devices on people's own computers: a machine connected with capybara-device serves the device over the dial-in channel, and its owner limits it there",
|