@camstack/server 1.2.821 → 1.2.823
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/CHANGELOG.md +12 -0
- package/dist/api/trpc/generated-cap-routers.js +20 -0
- package/package.json +9 -10
- package/system/dist/addon-runner.js +18 -13
- package/system/dist/addon-runner.mjs +19 -14
- package/system/dist/addon-utils.mjs +1 -1
- package/system/dist/{agent-https-N6Or6UAu.mjs → agent-https-CDl7mB_F.mjs} +1 -1
- package/system/dist/{agent-https-Bp5yzi8b.js → agent-https-Df53Fvk1.js} +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +4 -4
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +5 -5
- package/system/dist/builtins/alerts/alerts.addon.js +1 -1
- package/system/dist/builtins/alerts/alerts.addon.mjs +3 -3
- package/system/dist/builtins/autotrack/index.js +1 -1
- package/system/dist/builtins/autotrack/index.mjs +1 -1
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -2
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +5 -5
- package/system/dist/builtins/calendars/calendars.addon.js +1 -1
- package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
- package/system/dist/builtins/camera-grid/index.js +1 -1
- package/system/dist/builtins/camera-grid/index.mjs +1 -1
- package/system/dist/builtins/composer/composed-device-set.d.ts +5 -1
- package/system/dist/builtins/composer/composed-device.d.ts +4 -0
- package/system/dist/builtins/composer/composed-name.d.ts +30 -0
- package/system/dist/builtins/composer/composer.addon.js +94 -6
- package/system/dist/builtins/composer/composer.addon.mjs +94 -6
- package/system/dist/builtins/composer/composer.d.ts +13 -0
- package/system/dist/builtins/console-logging/index.js +1 -1
- package/system/dist/builtins/console-logging/index.mjs +1 -1
- package/system/dist/builtins/core-blocks/actions-service.d.ts +16 -0
- package/system/dist/builtins/core-blocks/composition-api.d.ts +6 -0
- package/system/dist/builtins/core-blocks/core-blocks.addon.js +214 -8
- package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +214 -8
- package/system/dist/builtins/core-blocks/device-template-door.d.ts +25 -0
- package/system/dist/builtins/core-blocks/fan-presets-service.d.ts +5 -0
- package/system/dist/builtins/core-blocks/rename-composition-block.d.ts +16 -0
- package/system/dist/builtins/core-blocks/scheduler/scheduler-engine.d.ts +5 -0
- package/system/dist/builtins/core-blocks/step-condition-door.d.ts +19 -0
- package/system/dist/builtins/core-blocks/sync-composed-device-name.d.ts +19 -0
- package/system/dist/builtins/device-history/device-history.addon.js +2 -2
- package/system/dist/builtins/device-history/device-history.addon.mjs +2 -2
- package/system/dist/builtins/device-manager/device-manager.addon.d.ts +7 -0
- package/system/dist/builtins/device-manager/device-manager.addon.js +691 -4
- package/system/dist/builtins/device-manager/device-manager.addon.mjs +689 -3
- package/system/dist/builtins/device-manager/device-template/template-host.d.ts +60 -0
- package/system/dist/builtins/device-manager/device-template-actions.d.ts +33 -0
- package/system/dist/builtins/hub-forwarder/index.js +1 -1
- package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
- package/system/dist/builtins/keychain/index.js +4 -4
- package/system/dist/builtins/keychain/index.mjs +4 -4
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
- package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
- package/system/dist/builtins/local-auth/local-auth.addon.mjs +5 -5
- package/system/dist/builtins/local-network/local-network.addon.js +2 -2
- package/system/dist/builtins/local-network/local-network.addon.mjs +2 -2
- package/system/dist/builtins/loki-logging/index.js +1 -1
- package/system/dist/builtins/loki-logging/index.mjs +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +10 -10
- package/system/dist/builtins/platform-probe/index.js +1 -1
- package/system/dist/builtins/platform-probe/index.mjs +10 -10
- package/system/dist/builtins/record-entries/index.js +1 -1
- package/system/dist/builtins/record-entries/index.mjs +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/snapshot/index.js +1 -1
- package/system/dist/builtins/snapshot/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +20 -20
- package/system/dist/builtins/sqlite-storage/index.js +1 -1
- package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +3 -3
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +3 -3
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/system-config/system-config.addon.js +9 -9
- package/system/dist/builtins/system-config/system-config.addon.mjs +287 -287
- package/system/dist/builtins/winston-logging/index.js +1 -1
- package/system/dist/builtins/winston-logging/index.mjs +1 -1
- package/system/dist/{capture-engine-4lB0sLCf.js → capture-engine-C0Z5rwCO.js} +1 -1
- package/system/dist/{capture-engine-Cz7z5d36.mjs → capture-engine-D_GQBgrZ.mjs} +1 -1
- package/system/dist/{composition-sources-C9gEj25e.js → composition-sources-DIZx3mW3.js} +1 -1
- package/system/dist/{composition-sources-C0e2g_VP.mjs → composition-sources-mx-2yCDv.mjs} +1 -1
- package/system/dist/{constants-CZMy7dor.mjs → constants-BQlx9HDp.mjs} +1 -1
- package/system/dist/{constants-Cq3B9yzj.js → constants-CAOcKiWT.js} +1 -1
- package/system/dist/{coral-accelerators-DBlgWB8k.mjs → coral-accelerators-BW3OcrLD.mjs} +6 -6
- package/system/dist/{custom-action-registry-B9zFBNVa.mjs → custom-action-registry-N7flBDwT.mjs} +37 -37
- package/system/dist/{device-ca-BenyQXJV.js → device-ca-93LsCF_Q.js} +1 -1
- package/system/dist/{device-ca-BmQ6N-A-.mjs → device-ca-CK6GTnNh.mjs} +1 -1
- package/system/dist/device-template-worker.js +5 -7032
- package/system/dist/device-template-worker.mjs +1 -7026
- package/system/dist/{dist-DuKMbGBM.mjs → dist-BQiyo22b.mjs} +667 -50
- package/system/dist/{dist-M5VX8Hqf.js → dist-izXDzt4g.js} +714 -49
- package/system/dist/file-host-worker.js +1 -1
- package/system/dist/file-host-worker.mjs +1 -1
- package/system/dist/{hub-mdns-ciao-k3pOKWyZ.js → hub-mdns-ciao-Dt_tgvol.js} +2 -2
- package/system/dist/{hub-mdns-ciao-CXBfBwAy.mjs → hub-mdns-ciao-srpOrqIP.mjs} +2 -2
- package/system/dist/index.js +26 -21
- package/system/dist/index.mjs +238 -233
- package/system/dist/{legacy-integration-rows-door-mIpR3e6X.js → legacy-integration-rows-door-BH43eDu6.js} +1 -1
- package/system/dist/{legacy-integration-rows-door-C13OeuW9.mjs → legacy-integration-rows-door-wu2GWY6U.mjs} +1 -1
- package/system/dist/rename-composition-block-6oLQVzft.js +112 -0
- package/system/dist/rename-composition-block-CH6ZoSJA.mjs +101 -0
- package/system/dist/{restore-archive-DDeQFJN5.mjs → restore-archive-C4rR2SYV.mjs} +154 -154
- package/system/dist/{restore-archive-CCgt7SZ8.js → restore-archive-DpZ2PbLN.js} +7 -7
- package/system/dist/template-protocol-BmMM5VBf.mjs +7033 -0
- package/system/dist/template-protocol-CTP4eAo_.js +7059 -0
- package/system/dist/{wal-maintenance-Cj7xe4wN.mjs → wal-maintenance-CNfp0HHX.mjs} +0 -0
- package/system/dist/{wal-maintenance-D5M6hl53.js → wal-maintenance-DAA2qJja.js} +0 -0
- package/system/package.json +1 -1
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { $
|
|
1
|
+
import { $ as DeviceStatusSchema, $s as collectZodSecretPaths$1, Aa as resolveTemplateRef, Bi as normalizeUnit, Bn as checkDeviceAlias, Cr as deviceStatusCapability, Dc as isSecretMarkerLike, Dr as enumerateSchemaFields, Ec as isSecretMarker, Er as enumerateItemArrayFields, Et as MINUTES_PER_DAY, Gr as generateDeviceAlias, Is as SECRET_CLEAR, J as DEVICE_TEMPLATE_MAX_OUTPUT, Jc as toSecretFieldView, Js as auditedFields, K as DEVICE_TEMPLATE_FILTERS, Ka as siteZoneKey, Kc as sleep$2, L as DAY_NAMES, Ln as canonicalIconKey, Mr as expandContainer, Na as runtimeFieldUnit, O as ConditionMirror, Pa as runtimeStatePolicyFor, Pn as buildStreamParamsConfigSchema, Sc as isKeychainStoredField, Sr as deviceStateCapability, Uc as secretTreeFromPaths, Ur as formatMinutes, Vc as resolveSecretMarkers, Vs as WELL_KNOWN_TAB_MAP, Xc as userError, Yc as transformSecretWrite, Zt as STREAM_PROFILE_META, _ as CAMERA_SWITCH_ORDER, _c as isCommand$1, _r as deviceExtensionCapability, al as EventCategory$1, ar as createDeviceExtensionProvider, dc as expandCapMethods$1, dt as FieldClaimSchema, ec as commandEffect, el as DeviceFeature, et as DeviceTombstoneSchema, fr as defineExtensionSlotProvider, nl as DeviceType$1, os as BaseAddon, pi as legacyAutoLinkIds, q as DEVICE_TEMPLATE_MAX_LENGTH, qi as parseStreamParamsFormPatch, rl as errMsg$1, s as ALL_CAPABILITY_DEFINITIONS, sc as defineRecordKinds, tl as DeviceRole, tt as DiscoveryProposalBodySchema, ut as FieldClaimModeSchema, vi as locationIconKey, wc as isSecretClear, y as CAP_NAMES_WITH_STATUS, yc as isDeviceConfigCap, yr as deviceManagerCapability } from "../../dist-BQiyo22b.mjs";
|
|
2
2
|
import { t as require_read_all_rows } from "../../read-all-rows-TaPqdWNF.mjs";
|
|
3
3
|
import { a as redactLogText, i as redactLogMeta, n as isSecretArgumentKey } from "../../log-redactor-XLDrPzOi.mjs";
|
|
4
|
+
import { n as renameCompositionBlock, t as composedBlockIdOf } from "../../rename-composition-block-CH6ZoSJA.mjs";
|
|
4
5
|
import { t as prefixRange } from "../../prefix-range-Dl0znowN.mjs";
|
|
5
6
|
import { t as loadPrefixed } from "../../prefix-walk-DAOBSyH1.mjs";
|
|
6
7
|
import { n as purgeRetiredSettingsRows } from "../../retired-settings-keys-u6xQA6r7.mjs";
|
|
8
|
+
import { n as isRenderResponse, r as analyzeTemplate } from "../../template-protocol-BmMM5VBf.mjs";
|
|
7
9
|
import { s as normalizeServiceType } from "../../dns-wire-Dm3H_7ST.mjs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
8
11
|
import { z } from "zod";
|
|
9
12
|
import { randomUUID } from "node:crypto";
|
|
10
13
|
import { canonicalDeviceFingerprint } from "@camstack/types/node";
|
|
14
|
+
import * as path$1 from "node:path";
|
|
11
15
|
import { isCommand, readCommandOrigin } from "@camstack/types/addon";
|
|
16
|
+
import { Worker } from "node:worker_threads";
|
|
12
17
|
//#region src/builtins/device-manager/adoption-job-engine.ts
|
|
13
18
|
/** How many finished jobs are kept per (addonId, integrationId). */
|
|
14
19
|
var JOB_HISTORY = 20;
|
|
@@ -6874,6 +6879,7 @@ async function loadMeta(pctx, input) {
|
|
|
6874
6879
|
addonId,
|
|
6875
6880
|
type: m.type,
|
|
6876
6881
|
name: m.name,
|
|
6882
|
+
...m.userNamed !== void 0 ? { userNamed: m.userNamed } : {},
|
|
6877
6883
|
location: m.location ?? null,
|
|
6878
6884
|
disabled: m.disabled ?? false,
|
|
6879
6885
|
parentDeviceId: m.parentDeviceId,
|
|
@@ -6892,17 +6898,28 @@ async function loadMeta(pctx, input) {
|
|
|
6892
6898
|
*/
|
|
6893
6899
|
async function setName(pctx, input) {
|
|
6894
6900
|
const { deviceId, name } = input;
|
|
6901
|
+
const stamp = input.stampUserNamed !== false;
|
|
6895
6902
|
const cascaded = [];
|
|
6903
|
+
let keptOperatorName = null;
|
|
6904
|
+
let blockId = null;
|
|
6896
6905
|
await pctx.metaStore.withMetaWriteLock(async () => {
|
|
6897
6906
|
const persisted = await pctx.metaStore.resolvePersistedById(deviceId);
|
|
6898
6907
|
if (!persisted) throw new Error(`[device-manager] setName: unknown device id=${deviceId}`);
|
|
6899
6908
|
const { meta: m } = persisted;
|
|
6900
6909
|
const oldName = m.name;
|
|
6901
|
-
|
|
6910
|
+
if (!stamp && m.userNamed === true && oldName !== name) {
|
|
6911
|
+
keptOperatorName = oldName;
|
|
6912
|
+
return;
|
|
6913
|
+
}
|
|
6914
|
+
blockId = stamp ? composedBlockIdOf(m.addonId, m.stableId) : null;
|
|
6915
|
+
await pctx.metaStore.rows.patch(deviceId, stamp ? {
|
|
6902
6916
|
name,
|
|
6903
6917
|
userNamed: true
|
|
6918
|
+
} : {
|
|
6919
|
+
name,
|
|
6920
|
+
userNamed: false
|
|
6904
6921
|
});
|
|
6905
|
-
if (oldName.length > 0 && oldName !== name) {
|
|
6922
|
+
if (stamp && oldName.length > 0 && oldName !== name) {
|
|
6906
6923
|
const candidates = /* @__PURE__ */ new Map();
|
|
6907
6924
|
for (const row of await pctx.metaStore.rows.listByParent(deviceId)) candidates.set(row.meta.id, row.meta);
|
|
6908
6925
|
for (const row of await pctx.metaStore.rows.listAll()) if (row.meta.linkDeviceId === deviceId) candidates.set(row.meta.id, row.meta);
|
|
@@ -6918,6 +6935,16 @@ async function setName(pctx, input) {
|
|
|
6918
6935
|
}
|
|
6919
6936
|
}
|
|
6920
6937
|
});
|
|
6938
|
+
if (keptOperatorName !== null) {
|
|
6939
|
+
pctx.host.ctx.logger.info("composed device keeps the name the operator gave it", {
|
|
6940
|
+
tags: { deviceId },
|
|
6941
|
+
meta: {
|
|
6942
|
+
name: keptOperatorName,
|
|
6943
|
+
requested: name
|
|
6944
|
+
}
|
|
6945
|
+
});
|
|
6946
|
+
return;
|
|
6947
|
+
}
|
|
6921
6948
|
pctx.host.ctx.eventBus.emit({
|
|
6922
6949
|
id: randomUUID(),
|
|
6923
6950
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -6946,6 +6973,17 @@ async function setName(pctx, input) {
|
|
|
6946
6973
|
value: child.name
|
|
6947
6974
|
}
|
|
6948
6975
|
});
|
|
6976
|
+
if (blockId !== null) {
|
|
6977
|
+
const api = pctx.host.ctx.api;
|
|
6978
|
+
if (api === void 0) {
|
|
6979
|
+
pctx.host.ctx.logger.warn("composed rename did not reach the block — no api on this node, retried on the next apply", {
|
|
6980
|
+
tags: { deviceId },
|
|
6981
|
+
meta: { blockId }
|
|
6982
|
+
});
|
|
6983
|
+
return;
|
|
6984
|
+
}
|
|
6985
|
+
await renameCompositionBlock(api, pctx.host.ctx.logger, deviceId, blockId, name);
|
|
6986
|
+
}
|
|
6949
6987
|
}
|
|
6950
6988
|
/**
|
|
6951
6989
|
* Every descendant of `rootId`, walked via `parentDeviceId` chains.
|
|
@@ -8005,6 +8043,617 @@ async function runLinkedDevicesManualBackfillAtBoot(pctx) {
|
|
|
8005
8043
|
} });
|
|
8006
8044
|
}
|
|
8007
8045
|
//#endregion
|
|
8046
|
+
//#region src/builtins/device-manager/device-template/mirror-resolver.ts
|
|
8047
|
+
/** The slices the mirror must hold so these devices' templates can render. */
|
|
8048
|
+
function templateMirrorSources(devices) {
|
|
8049
|
+
return devices.flatMap((device) => device.caps.map((cap) => ({
|
|
8050
|
+
kind: "device",
|
|
8051
|
+
deviceId: device.deviceId,
|
|
8052
|
+
cap
|
|
8053
|
+
})));
|
|
8054
|
+
}
|
|
8055
|
+
function mirrorTemplateResolver(mirror, devices, unitOf) {
|
|
8056
|
+
const byAlias = /* @__PURE__ */ new Map();
|
|
8057
|
+
for (const device of devices) if (!byAlias.has(device.alias)) byAlias.set(device.alias, device);
|
|
8058
|
+
return { viewOf(alias) {
|
|
8059
|
+
const device = byAlias.get(alias);
|
|
8060
|
+
if (device === void 0) return void 0;
|
|
8061
|
+
const slices = {};
|
|
8062
|
+
for (const cap of device.caps) {
|
|
8063
|
+
const slice = mirror.sliceOf(device.deviceId, cap);
|
|
8064
|
+
if (slice !== void 0) slices[cap] = slice;
|
|
8065
|
+
}
|
|
8066
|
+
return {
|
|
8067
|
+
deviceId: device.deviceId,
|
|
8068
|
+
addonId: device.addonId,
|
|
8069
|
+
stableId: device.stableId,
|
|
8070
|
+
slices,
|
|
8071
|
+
...unitOf !== void 0 ? { unitOf } : {}
|
|
8072
|
+
};
|
|
8073
|
+
} };
|
|
8074
|
+
}
|
|
8075
|
+
//#endregion
|
|
8076
|
+
//#region src/builtins/device-manager/device-template/template-host.ts
|
|
8077
|
+
/**
|
|
8078
|
+
* The hub side of device value templates (D1021 run-time contract): the one
|
|
8079
|
+
* place a consumer's template becomes text. It names the devices a template
|
|
8080
|
+
* refers to (aliases), refuses the ones the caller was not proven to see,
|
|
8081
|
+
* fills a `ConditionMirror` with the slices those devices hold NOW, and hands
|
|
8082
|
+
* the renderer a resolver over that mirror. A consumer (an action step, a
|
|
8083
|
+
* scheduler point, a notification rule, a generated automation block) reaches
|
|
8084
|
+
* it only through `device-manager.renderDeviceTemplates`; there is no second
|
|
8085
|
+
* renderer and no second copy of the state.
|
|
8086
|
+
*/
|
|
8087
|
+
var DeviceTemplateHost = class {
|
|
8088
|
+
deps;
|
|
8089
|
+
constructor(deps) {
|
|
8090
|
+
this.deps = deps;
|
|
8091
|
+
}
|
|
8092
|
+
/**
|
|
8093
|
+
* Which devices a template names, without rendering it. `allowed` is the set
|
|
8094
|
+
* of device ids the caller was proven to see; `undefined` means no
|
|
8095
|
+
* restriction (an admin, a system run). A restricted caller gets the same
|
|
8096
|
+
* answer for an alias that does not exist and one it may not see.
|
|
8097
|
+
*/
|
|
8098
|
+
async check(source, allowed) {
|
|
8099
|
+
const analysis = this.deps.renderer.validate(source);
|
|
8100
|
+
if (!analysis.ok) return {
|
|
8101
|
+
ok: false,
|
|
8102
|
+
reason: analysis.refusal.reason,
|
|
8103
|
+
message: analysis.refusal.message
|
|
8104
|
+
};
|
|
8105
|
+
const aliases = [];
|
|
8106
|
+
for (const alias of new Set(analysis.refs.map((ref) => ref.alias))) {
|
|
8107
|
+
const row = await this.deps.findByAlias(alias);
|
|
8108
|
+
aliases.push({
|
|
8109
|
+
alias,
|
|
8110
|
+
deviceId: row === null ? null : row.deviceId,
|
|
8111
|
+
allowed: allowed === void 0 || row !== null && allowed.has(row.deviceId),
|
|
8112
|
+
row
|
|
8113
|
+
});
|
|
8114
|
+
}
|
|
8115
|
+
return {
|
|
8116
|
+
ok: true,
|
|
8117
|
+
aliases
|
|
8118
|
+
};
|
|
8119
|
+
}
|
|
8120
|
+
/** Render every template; answers are index-aligned with the questions. */
|
|
8121
|
+
async render(sources, allowed) {
|
|
8122
|
+
const checks = await Promise.all(sources.map((source) => this.check(source, allowed)));
|
|
8123
|
+
const rows = /* @__PURE__ */ new Map();
|
|
8124
|
+
for (const check of checks) {
|
|
8125
|
+
if (!check.ok) continue;
|
|
8126
|
+
for (const use of check.aliases) {
|
|
8127
|
+
if (!use.allowed || use.row === null || rows.has(use.alias)) continue;
|
|
8128
|
+
rows.set(use.alias, {
|
|
8129
|
+
...use.row,
|
|
8130
|
+
caps: await this.deps.capsOf(use.row.deviceId)
|
|
8131
|
+
});
|
|
8132
|
+
}
|
|
8133
|
+
}
|
|
8134
|
+
const entries = [...rows.values()];
|
|
8135
|
+
const mirror = new ConditionMirror(templateMirrorSources(entries));
|
|
8136
|
+
const report = await mirror.refresh({
|
|
8137
|
+
fetchSlice: (deviceId, cap) => this.deps.fetchSlice(deviceId, cap),
|
|
8138
|
+
fetchStableIds: async () => /* @__PURE__ */ new Map()
|
|
8139
|
+
});
|
|
8140
|
+
if (report.kept > 0) this.deps.logger.debug("template mirror kept no value for some slices", { meta: { ...report } });
|
|
8141
|
+
const resolver = mirrorTemplateResolver(mirror, entries, this.deps.unitOf);
|
|
8142
|
+
const out = [];
|
|
8143
|
+
let warned = false;
|
|
8144
|
+
for (const [index, source] of sources.entries()) {
|
|
8145
|
+
const check = checks[index];
|
|
8146
|
+
if (check === void 0 || !check.ok) {
|
|
8147
|
+
out.push({
|
|
8148
|
+
ok: false,
|
|
8149
|
+
refusal: {
|
|
8150
|
+
reason: check === void 0 ? "render-failed" : check.reason,
|
|
8151
|
+
message: check === void 0 ? "no check" : check.message
|
|
8152
|
+
},
|
|
8153
|
+
trace: []
|
|
8154
|
+
});
|
|
8155
|
+
continue;
|
|
8156
|
+
}
|
|
8157
|
+
const refused = check.aliases.find((use) => !use.allowed);
|
|
8158
|
+
if (refused !== void 0) {
|
|
8159
|
+
if (!warned) {
|
|
8160
|
+
warned = true;
|
|
8161
|
+
this.deps.logger.warn("device template refused — it names a device the caller was not proven to see", {
|
|
8162
|
+
...refused.deviceId === null ? {} : { tags: { deviceId: refused.deviceId } },
|
|
8163
|
+
meta: { alias: refused.alias }
|
|
8164
|
+
});
|
|
8165
|
+
}
|
|
8166
|
+
out.push({
|
|
8167
|
+
ok: false,
|
|
8168
|
+
refusal: {
|
|
8169
|
+
reason: "out-of-scope",
|
|
8170
|
+
message: `the template names "${refused.alias}", a device this caller cannot see`
|
|
8171
|
+
},
|
|
8172
|
+
trace: []
|
|
8173
|
+
});
|
|
8174
|
+
continue;
|
|
8175
|
+
}
|
|
8176
|
+
const rendered = await this.deps.renderer.render(source, resolver);
|
|
8177
|
+
out.push(rendered.ok ? {
|
|
8178
|
+
ok: true,
|
|
8179
|
+
text: rendered.text,
|
|
8180
|
+
trace: [...rendered.trace]
|
|
8181
|
+
} : {
|
|
8182
|
+
ok: false,
|
|
8183
|
+
refusal: rendered.refusal,
|
|
8184
|
+
trace: [...rendered.trace]
|
|
8185
|
+
});
|
|
8186
|
+
}
|
|
8187
|
+
return out;
|
|
8188
|
+
}
|
|
8189
|
+
};
|
|
8190
|
+
//#endregion
|
|
8191
|
+
//#region src/builtins/device-manager/device-template/template-renderer.ts
|
|
8192
|
+
/**
|
|
8193
|
+
* The hub's device value-template renderer (D1021).
|
|
8194
|
+
*
|
|
8195
|
+
* Owner: device-manager. It owns the device rows and the aliases a template
|
|
8196
|
+
* names, and this directory is the only one allowed to import nunjucks
|
|
8197
|
+
* (guard `check-device-template-grammar`).
|
|
8198
|
+
*
|
|
8199
|
+
* A render is: vet the template (`analyzeTemplate`), resolve every reference
|
|
8200
|
+
* against the devices' current slices, build a context of only the values the
|
|
8201
|
+
* template names, and hand it to a worker thread under a wall-time budget. A
|
|
8202
|
+
* worker that overruns is terminated and respawned on the next render. What a
|
|
8203
|
+
* reference cannot resolve renders `—` and goes into the trace; an ambiguous
|
|
8204
|
+
* `alias.field` refuses the whole render, naming the capabilities.
|
|
8205
|
+
*/
|
|
8206
|
+
/** `dist/device-template-worker.js` at the package root, found through its own `package.json`. */
|
|
8207
|
+
function defaultTemplateWorkerPath() {
|
|
8208
|
+
const require_ = createRequire(import.meta.url);
|
|
8209
|
+
const packageRoot = path$1.dirname(require_.resolve("@camstack/server/system/package.json"));
|
|
8210
|
+
return path$1.join(packageRoot, "dist", "device-template-worker.js");
|
|
8211
|
+
}
|
|
8212
|
+
/** Room for the hidden source-unit arguments the renderer adds to `unit()` calls. */
|
|
8213
|
+
var REWRITE_ALLOWANCE = 2048;
|
|
8214
|
+
/**
|
|
8215
|
+
* The cold start of the worker thread (loading nunjucks) is NOT part of a
|
|
8216
|
+
* render's budget: the budget timer starts only once the worker has announced
|
|
8217
|
+
* `ready`. A thread that never does is killed after this long, and the next
|
|
8218
|
+
* render respawns it.
|
|
8219
|
+
*/
|
|
8220
|
+
var SPAWN_TIMEOUT_MS = 1e4;
|
|
8221
|
+
var DEFAULT_MAX_QUEUE = 16;
|
|
8222
|
+
var DEFAULT_BREAKER_THRESHOLD = 3;
|
|
8223
|
+
var DEFAULT_BREAKER_WINDOW_MS = 6e4;
|
|
8224
|
+
var BREAKER_MAX_TEMPLATES = 256;
|
|
8225
|
+
function isReadyMessage(value) {
|
|
8226
|
+
return typeof value === "object" && value !== null && Reflect.get(value, "ready") === true;
|
|
8227
|
+
}
|
|
8228
|
+
/** Copy a value as plain data only: primitives, arrays and plain objects. Anything else (functions, class instances) is dropped. */
|
|
8229
|
+
function plainCopy(value, depth = 0) {
|
|
8230
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
8231
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
8232
|
+
if (depth > 8) return void 0;
|
|
8233
|
+
if (Array.isArray(value)) return value.map((item) => plainCopy(item, depth + 1));
|
|
8234
|
+
if (typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
|
|
8235
|
+
const out = Object.create(null);
|
|
8236
|
+
for (const key of Object.keys(value)) out[key] = plainCopy(Reflect.get(value, key), depth + 1);
|
|
8237
|
+
return out;
|
|
8238
|
+
}
|
|
8239
|
+
}
|
|
8240
|
+
/** The context under construction: containers this builder made, so a longer path never writes into a caller's value. */
|
|
8241
|
+
var ContextTree = class {
|
|
8242
|
+
root = Object.create(null);
|
|
8243
|
+
ours = /* @__PURE__ */ new WeakSet();
|
|
8244
|
+
constructor() {
|
|
8245
|
+
this.ours.add(this.root);
|
|
8246
|
+
}
|
|
8247
|
+
isOurs(value) {
|
|
8248
|
+
return typeof value === "object" && value !== null && this.ours.has(value);
|
|
8249
|
+
}
|
|
8250
|
+
place(alias, keys, value) {
|
|
8251
|
+
let cursor = this.root;
|
|
8252
|
+
let key = alias;
|
|
8253
|
+
for (const next of keys) {
|
|
8254
|
+
const existing = cursor[key];
|
|
8255
|
+
if (existing !== void 0 && !this.isOurs(existing)) return;
|
|
8256
|
+
let child;
|
|
8257
|
+
if (this.isOurs(existing)) child = existing;
|
|
8258
|
+
else {
|
|
8259
|
+
child = Object.create(null);
|
|
8260
|
+
this.ours.add(child);
|
|
8261
|
+
cursor[key] = child;
|
|
8262
|
+
}
|
|
8263
|
+
cursor = child;
|
|
8264
|
+
key = String(next);
|
|
8265
|
+
}
|
|
8266
|
+
cursor[key] = value;
|
|
8267
|
+
}
|
|
8268
|
+
};
|
|
8269
|
+
function offsetOf(source, lineno, colno) {
|
|
8270
|
+
const lines = source.split("\n");
|
|
8271
|
+
if (lineno < 0 || lineno >= lines.length) return void 0;
|
|
8272
|
+
let offset = 0;
|
|
8273
|
+
for (let i = 0; i < lineno; i += 1) offset += (lines[i]?.length ?? 0) + 1;
|
|
8274
|
+
return offset + colno;
|
|
8275
|
+
}
|
|
8276
|
+
/** Insert `"<source unit>", ` as the first argument of each `unit(` call so the worker converts from the field's base unit. */
|
|
8277
|
+
function withUnitHints(source, hints) {
|
|
8278
|
+
const located = hints.map((entry) => ({
|
|
8279
|
+
entry,
|
|
8280
|
+
offset: offsetOf(source, entry.site.lineno, entry.site.colno)
|
|
8281
|
+
})).filter((item) => item.offset !== void 0).toSorted((a, b) => b.offset - a.offset);
|
|
8282
|
+
let out = source;
|
|
8283
|
+
for (const { entry, offset } of located) {
|
|
8284
|
+
if (!out.startsWith("unit", offset)) continue;
|
|
8285
|
+
const open = out.indexOf("(", offset + 4);
|
|
8286
|
+
if (open < 0) continue;
|
|
8287
|
+
out = `${out.slice(0, open + 1)}${JSON.stringify(entry.hint)}, ${out.slice(open + 1)}`;
|
|
8288
|
+
}
|
|
8289
|
+
return out;
|
|
8290
|
+
}
|
|
8291
|
+
function refText(ref) {
|
|
8292
|
+
return [ref.alias, ...ref.path.map(String)].join(".");
|
|
8293
|
+
}
|
|
8294
|
+
var DeviceTemplateRenderer = class {
|
|
8295
|
+
workerPath;
|
|
8296
|
+
execArgv;
|
|
8297
|
+
budgetMs;
|
|
8298
|
+
now;
|
|
8299
|
+
worker = null;
|
|
8300
|
+
ready = null;
|
|
8301
|
+
nextId = 1;
|
|
8302
|
+
pending = /* @__PURE__ */ new Map();
|
|
8303
|
+
queue = Promise.resolve();
|
|
8304
|
+
waiting = 0;
|
|
8305
|
+
spawnTimeoutMs;
|
|
8306
|
+
maxQueue;
|
|
8307
|
+
breakerThreshold;
|
|
8308
|
+
breakerWindowMs;
|
|
8309
|
+
breakers = /* @__PURE__ */ new Map();
|
|
8310
|
+
constructor(options = {}) {
|
|
8311
|
+
this.workerPath = options.workerPath ?? defaultTemplateWorkerPath();
|
|
8312
|
+
this.execArgv = options.workerExecArgv;
|
|
8313
|
+
this.budgetMs = options.budgetMs ?? 50;
|
|
8314
|
+
this.now = options.now ?? Date.now;
|
|
8315
|
+
this.spawnTimeoutMs = options.spawnTimeoutMs ?? SPAWN_TIMEOUT_MS;
|
|
8316
|
+
this.maxQueue = options.maxQueue ?? DEFAULT_MAX_QUEUE;
|
|
8317
|
+
this.breakerThreshold = options.breakerThreshold ?? DEFAULT_BREAKER_THRESHOLD;
|
|
8318
|
+
this.breakerWindowMs = options.breakerWindowMs ?? DEFAULT_BREAKER_WINDOW_MS;
|
|
8319
|
+
}
|
|
8320
|
+
/** Vet a template without rendering it: its references, or the refusal. */
|
|
8321
|
+
validate(source) {
|
|
8322
|
+
return analyzeTemplate(source, DEVICE_TEMPLATE_FILTERS, DEVICE_TEMPLATE_MAX_LENGTH);
|
|
8323
|
+
}
|
|
8324
|
+
async render(source, resolver) {
|
|
8325
|
+
const analysis = this.validate(source);
|
|
8326
|
+
if (!analysis.ok) return {
|
|
8327
|
+
ok: false,
|
|
8328
|
+
refusal: analysis.refusal,
|
|
8329
|
+
trace: []
|
|
8330
|
+
};
|
|
8331
|
+
const prepared = this.prepare(source, analysis, resolver);
|
|
8332
|
+
if ("refusal" in prepared) return {
|
|
8333
|
+
ok: false,
|
|
8334
|
+
refusal: prepared.refusal,
|
|
8335
|
+
trace: [...prepared.trace]
|
|
8336
|
+
};
|
|
8337
|
+
const open = this.breakers.get(source);
|
|
8338
|
+
if (open !== void 0 && open.openUntil > Date.now()) return {
|
|
8339
|
+
ok: false,
|
|
8340
|
+
refusal: {
|
|
8341
|
+
reason: "budget-exceeded",
|
|
8342
|
+
message: `this template overran its ${this.budgetMs} ms budget ${open.timeouts} times in a row; it is not run again for ${Math.ceil((open.openUntil - Date.now()) / 1e3)} s`
|
|
8343
|
+
},
|
|
8344
|
+
trace: [...prepared.trace]
|
|
8345
|
+
};
|
|
8346
|
+
if (this.waiting >= this.maxQueue) return {
|
|
8347
|
+
ok: false,
|
|
8348
|
+
refusal: {
|
|
8349
|
+
reason: "busy",
|
|
8350
|
+
message: `${this.maxQueue} renders are already waiting`
|
|
8351
|
+
},
|
|
8352
|
+
trace: [...prepared.trace]
|
|
8353
|
+
};
|
|
8354
|
+
this.waiting += 1;
|
|
8355
|
+
let response;
|
|
8356
|
+
try {
|
|
8357
|
+
response = await this.enqueue(prepared);
|
|
8358
|
+
} finally {
|
|
8359
|
+
this.waiting -= 1;
|
|
8360
|
+
}
|
|
8361
|
+
this.noteOutcome(source, response);
|
|
8362
|
+
if (response.ok) {
|
|
8363
|
+
const poisoned = Array.from({ length: response.poisoned }, () => ({
|
|
8364
|
+
ref: "<expression>",
|
|
8365
|
+
reason: "unavailable"
|
|
8366
|
+
}));
|
|
8367
|
+
return {
|
|
8368
|
+
ok: true,
|
|
8369
|
+
text: response.text,
|
|
8370
|
+
trace: [...prepared.trace, ...poisoned]
|
|
8371
|
+
};
|
|
8372
|
+
}
|
|
8373
|
+
return {
|
|
8374
|
+
ok: false,
|
|
8375
|
+
refusal: {
|
|
8376
|
+
reason: response.reason,
|
|
8377
|
+
message: response.message
|
|
8378
|
+
},
|
|
8379
|
+
trace: [...prepared.trace]
|
|
8380
|
+
};
|
|
8381
|
+
}
|
|
8382
|
+
noteOutcome(source, response) {
|
|
8383
|
+
if (response.ok || response.reason !== "timeout") {
|
|
8384
|
+
this.breakers.delete(source);
|
|
8385
|
+
return;
|
|
8386
|
+
}
|
|
8387
|
+
const state = this.breakers.get(source) ?? {
|
|
8388
|
+
timeouts: 0,
|
|
8389
|
+
openUntil: 0
|
|
8390
|
+
};
|
|
8391
|
+
state.timeouts += 1;
|
|
8392
|
+
if (state.timeouts >= this.breakerThreshold) state.openUntil = Date.now() + this.breakerWindowMs;
|
|
8393
|
+
this.breakers.delete(source);
|
|
8394
|
+
this.breakers.set(source, state);
|
|
8395
|
+
for (const key of this.breakers.keys()) {
|
|
8396
|
+
if (this.breakers.size <= BREAKER_MAX_TEMPLATES) break;
|
|
8397
|
+
this.breakers.delete(key);
|
|
8398
|
+
}
|
|
8399
|
+
}
|
|
8400
|
+
dispose() {
|
|
8401
|
+
this.killWorker("disposed");
|
|
8402
|
+
}
|
|
8403
|
+
prepare(source, analysis, resolver) {
|
|
8404
|
+
const trace = [];
|
|
8405
|
+
const tree = new ContextTree();
|
|
8406
|
+
const resolvedFor = /* @__PURE__ */ new Map();
|
|
8407
|
+
for (const ref of analysis.refs) {
|
|
8408
|
+
const view = resolver.viewOf(ref.alias);
|
|
8409
|
+
if (view === void 0) {
|
|
8410
|
+
trace.push({
|
|
8411
|
+
ref: refText(ref),
|
|
8412
|
+
reason: "unknown-alias"
|
|
8413
|
+
});
|
|
8414
|
+
continue;
|
|
8415
|
+
}
|
|
8416
|
+
const resolved = resolveTemplateRef(view, ref.path);
|
|
8417
|
+
if (resolved.kind === "ambiguous") return {
|
|
8418
|
+
refusal: {
|
|
8419
|
+
reason: "ambiguous-field",
|
|
8420
|
+
message: `"${ref.alias}.${resolved.field}" is ambiguous: capabilities ${resolved.caps.join(", ")} all carry "${resolved.field}"; write ${ref.alias}.<capability>.${resolved.field}`
|
|
8421
|
+
},
|
|
8422
|
+
trace
|
|
8423
|
+
};
|
|
8424
|
+
if (resolved.kind === "missing") {
|
|
8425
|
+
trace.push({
|
|
8426
|
+
ref: refText(ref),
|
|
8427
|
+
reason: resolved.reason
|
|
8428
|
+
});
|
|
8429
|
+
continue;
|
|
8430
|
+
}
|
|
8431
|
+
resolvedFor.set(JSON.stringify([ref.alias, ref.path]), {
|
|
8432
|
+
view,
|
|
8433
|
+
cap: resolved.cap,
|
|
8434
|
+
fieldPath: resolved.fieldPath
|
|
8435
|
+
});
|
|
8436
|
+
if (resolved.value === null) trace.push({
|
|
8437
|
+
ref: refText(ref),
|
|
8438
|
+
reason: "unavailable"
|
|
8439
|
+
});
|
|
8440
|
+
tree.place(ref.alias, ref.path, plainCopy(resolved.value));
|
|
8441
|
+
}
|
|
8442
|
+
return {
|
|
8443
|
+
source: withUnitHints(source, analysis.unitSites.map((site) => {
|
|
8444
|
+
const hit = site.ref === void 0 ? void 0 : resolvedFor.get(JSON.stringify([site.ref.alias, site.ref.path]));
|
|
8445
|
+
return {
|
|
8446
|
+
site,
|
|
8447
|
+
hint: hit?.view.unitOf?.(hit.cap, hit.fieldPath) ?? ""
|
|
8448
|
+
};
|
|
8449
|
+
})),
|
|
8450
|
+
context: tree.root,
|
|
8451
|
+
trace
|
|
8452
|
+
};
|
|
8453
|
+
}
|
|
8454
|
+
enqueue(prepared) {
|
|
8455
|
+
const run = async () => this.renderOne(prepared);
|
|
8456
|
+
const result = this.queue.then(run, run);
|
|
8457
|
+
this.queue = result.catch(() => void 0);
|
|
8458
|
+
return result;
|
|
8459
|
+
}
|
|
8460
|
+
async renderOne(prepared) {
|
|
8461
|
+
const id = this.nextId;
|
|
8462
|
+
this.nextId += 1;
|
|
8463
|
+
let worker;
|
|
8464
|
+
try {
|
|
8465
|
+
worker = await this.ensureWorker();
|
|
8466
|
+
} catch (err) {
|
|
8467
|
+
return {
|
|
8468
|
+
id,
|
|
8469
|
+
ok: false,
|
|
8470
|
+
reason: "render-failed",
|
|
8471
|
+
message: err instanceof Error ? err.message : String(err)
|
|
8472
|
+
};
|
|
8473
|
+
}
|
|
8474
|
+
const response = new Promise((resolve) => {
|
|
8475
|
+
this.pending.set(id, {
|
|
8476
|
+
worker,
|
|
8477
|
+
resolve
|
|
8478
|
+
});
|
|
8479
|
+
});
|
|
8480
|
+
const request = {
|
|
8481
|
+
id,
|
|
8482
|
+
source: prepared.source,
|
|
8483
|
+
context: prepared.context,
|
|
8484
|
+
nowMs: this.now()
|
|
8485
|
+
};
|
|
8486
|
+
try {
|
|
8487
|
+
worker.postMessage(request);
|
|
8488
|
+
} catch (err) {
|
|
8489
|
+
this.pending.delete(id);
|
|
8490
|
+
return {
|
|
8491
|
+
id,
|
|
8492
|
+
ok: false,
|
|
8493
|
+
reason: "render-failed",
|
|
8494
|
+
message: err instanceof Error ? err.message : String(err)
|
|
8495
|
+
};
|
|
8496
|
+
}
|
|
8497
|
+
let timer;
|
|
8498
|
+
const timeout = new Promise((resolve) => {
|
|
8499
|
+
timer = setTimeout(() => {
|
|
8500
|
+
resolve({
|
|
8501
|
+
id,
|
|
8502
|
+
ok: false,
|
|
8503
|
+
reason: "timeout",
|
|
8504
|
+
message: `render took longer than ${this.budgetMs} ms`
|
|
8505
|
+
});
|
|
8506
|
+
}, this.budgetMs);
|
|
8507
|
+
});
|
|
8508
|
+
const outcome = await Promise.race([response, timeout]);
|
|
8509
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
8510
|
+
this.pending.delete(id);
|
|
8511
|
+
if (!outcome.ok && outcome.reason === "timeout") this.killWorker("budget overrun");
|
|
8512
|
+
return outcome;
|
|
8513
|
+
}
|
|
8514
|
+
ensureWorker() {
|
|
8515
|
+
const existing = this.worker;
|
|
8516
|
+
if (existing !== null && this.ready !== null) return this.ready.then(() => existing);
|
|
8517
|
+
const limits = {
|
|
8518
|
+
allowedFilters: DEVICE_TEMPLATE_FILTERS,
|
|
8519
|
+
maxSourceLength: DEVICE_TEMPLATE_MAX_LENGTH + REWRITE_ALLOWANCE,
|
|
8520
|
+
maxOutput: DEVICE_TEMPLATE_MAX_OUTPUT,
|
|
8521
|
+
unavailable: "—"
|
|
8522
|
+
};
|
|
8523
|
+
const worker = new Worker(this.workerPath, {
|
|
8524
|
+
workerData: limits,
|
|
8525
|
+
...this.execArgv !== void 0 ? { execArgv: [...this.execArgv] } : {},
|
|
8526
|
+
resourceLimits: {
|
|
8527
|
+
maxOldGenerationSizeMb: 64,
|
|
8528
|
+
maxYoungGenerationSizeMb: 16,
|
|
8529
|
+
stackSizeMb: 4
|
|
8530
|
+
}
|
|
8531
|
+
});
|
|
8532
|
+
this.worker = worker;
|
|
8533
|
+
this.ready = new Promise((resolve, reject) => {
|
|
8534
|
+
const timer = setTimeout(() => {
|
|
8535
|
+
reject(/* @__PURE__ */ new Error("template worker did not start"));
|
|
8536
|
+
if (this.worker === worker) this.dropWorker();
|
|
8537
|
+
worker.terminate();
|
|
8538
|
+
}, this.spawnTimeoutMs);
|
|
8539
|
+
worker.on("message", (message) => {
|
|
8540
|
+
if (isReadyMessage(message)) {
|
|
8541
|
+
clearTimeout(timer);
|
|
8542
|
+
resolve();
|
|
8543
|
+
return;
|
|
8544
|
+
}
|
|
8545
|
+
if (isRenderResponse(message)) this.pending.get(message.id)?.resolve(message);
|
|
8546
|
+
});
|
|
8547
|
+
worker.on("error", (err) => {
|
|
8548
|
+
clearTimeout(timer);
|
|
8549
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
8550
|
+
this.failPending(worker, err instanceof Error ? err.message : String(err));
|
|
8551
|
+
if (this.worker === worker) this.dropWorker();
|
|
8552
|
+
});
|
|
8553
|
+
worker.on("exit", () => {
|
|
8554
|
+
clearTimeout(timer);
|
|
8555
|
+
this.failPending(worker, "template worker exited");
|
|
8556
|
+
if (this.worker === worker) this.dropWorker();
|
|
8557
|
+
});
|
|
8558
|
+
});
|
|
8559
|
+
worker.unref();
|
|
8560
|
+
return this.ready.then(() => worker);
|
|
8561
|
+
}
|
|
8562
|
+
/** Fail only the renders that were running on THIS worker: a killed worker's late `exit` must not fail the next one. */
|
|
8563
|
+
failPending(worker, message) {
|
|
8564
|
+
for (const [id, entry] of this.pending) {
|
|
8565
|
+
if (entry.worker !== worker) continue;
|
|
8566
|
+
entry.resolve({
|
|
8567
|
+
id,
|
|
8568
|
+
ok: false,
|
|
8569
|
+
reason: "render-failed",
|
|
8570
|
+
message
|
|
8571
|
+
});
|
|
8572
|
+
this.pending.delete(id);
|
|
8573
|
+
}
|
|
8574
|
+
}
|
|
8575
|
+
dropWorker() {
|
|
8576
|
+
this.worker = null;
|
|
8577
|
+
this.ready = null;
|
|
8578
|
+
}
|
|
8579
|
+
killWorker(_why) {
|
|
8580
|
+
const worker = this.worker;
|
|
8581
|
+
this.dropWorker();
|
|
8582
|
+
if (worker !== null) worker.terminate();
|
|
8583
|
+
}
|
|
8584
|
+
};
|
|
8585
|
+
//#endregion
|
|
8586
|
+
//#region src/builtins/device-manager/device-template-actions.ts
|
|
8587
|
+
/**
|
|
8588
|
+
* `device-manager.renderDeviceTemplates` / `checkDeviceTemplates` (D1021): the
|
|
8589
|
+
* provider side of the ONE door to the template renderer. The renderer is built
|
|
8590
|
+
* on first use (a worker is spawned lazily by it) and disposed at shutdown.
|
|
8591
|
+
*/
|
|
8592
|
+
/** The D944 base unit of a numeric runtime field, from the cap's own schema. */
|
|
8593
|
+
var baseUnitOfRuntimeField = (cap, path) => {
|
|
8594
|
+
const field = path[0];
|
|
8595
|
+
if (typeof field !== "string") return void 0;
|
|
8596
|
+
return runtimeFieldUnit(cap, field);
|
|
8597
|
+
};
|
|
8598
|
+
var DeviceTemplateActions = class {
|
|
8599
|
+
logger;
|
|
8600
|
+
deps;
|
|
8601
|
+
makeRenderer;
|
|
8602
|
+
renderer = null;
|
|
8603
|
+
host = null;
|
|
8604
|
+
constructor(logger, deps, makeRenderer = () => new DeviceTemplateRenderer()) {
|
|
8605
|
+
this.logger = logger;
|
|
8606
|
+
this.deps = deps;
|
|
8607
|
+
this.makeRenderer = makeRenderer;
|
|
8608
|
+
}
|
|
8609
|
+
hostOf() {
|
|
8610
|
+
if (this.host === null) {
|
|
8611
|
+
this.renderer = this.makeRenderer();
|
|
8612
|
+
this.host = new DeviceTemplateHost({
|
|
8613
|
+
renderer: this.renderer,
|
|
8614
|
+
findByAlias: this.deps.findByAlias,
|
|
8615
|
+
capsOf: this.deps.capsOf,
|
|
8616
|
+
fetchSlice: this.deps.fetchSlice,
|
|
8617
|
+
unitOf: baseUnitOfRuntimeField,
|
|
8618
|
+
logger: this.logger.child("templates")
|
|
8619
|
+
});
|
|
8620
|
+
}
|
|
8621
|
+
return this.host;
|
|
8622
|
+
}
|
|
8623
|
+
async render(input) {
|
|
8624
|
+
const allowed = input.allowedDeviceIds === void 0 ? void 0 : new Set(input.allowedDeviceIds);
|
|
8625
|
+
return { results: await this.hostOf().render(input.templates, allowed) };
|
|
8626
|
+
}
|
|
8627
|
+
async check(input) {
|
|
8628
|
+
const allowed = input.allowedDeviceIds === void 0 ? void 0 : new Set(input.allowedDeviceIds);
|
|
8629
|
+
const host = this.hostOf();
|
|
8630
|
+
const results = [];
|
|
8631
|
+
for (const source of input.templates) {
|
|
8632
|
+
const check = await host.check(source, allowed);
|
|
8633
|
+
results.push(check.ok ? {
|
|
8634
|
+
ok: true,
|
|
8635
|
+
aliases: check.aliases.map((use) => ({
|
|
8636
|
+
alias: use.alias,
|
|
8637
|
+
deviceId: use.deviceId,
|
|
8638
|
+
allowed: use.allowed
|
|
8639
|
+
}))
|
|
8640
|
+
} : {
|
|
8641
|
+
ok: false,
|
|
8642
|
+
refusal: {
|
|
8643
|
+
reason: check.reason,
|
|
8644
|
+
message: check.message
|
|
8645
|
+
}
|
|
8646
|
+
});
|
|
8647
|
+
}
|
|
8648
|
+
return { results };
|
|
8649
|
+
}
|
|
8650
|
+
dispose() {
|
|
8651
|
+
this.renderer?.dispose();
|
|
8652
|
+
this.renderer = null;
|
|
8653
|
+
this.host = null;
|
|
8654
|
+
}
|
|
8655
|
+
};
|
|
8656
|
+
//#endregion
|
|
8008
8657
|
//#region src/builtins/device-manager/device-meta-types.ts
|
|
8009
8658
|
/**
|
|
8010
8659
|
* Decode the raw `ctx.settings.readAddonStore()` record into {@link AddonStore}.
|
|
@@ -11952,6 +12601,7 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
11952
12601
|
removalFence = createRemovalFence();
|
|
11953
12602
|
/** Store-by-store clean-up of a removed device, with a bounded retry (D864). */
|
|
11954
12603
|
removalCleanupImpl = null;
|
|
12604
|
+
templateActions = null;
|
|
11955
12605
|
removalHooksCleanupImpl = null;
|
|
11956
12606
|
/** Declared secret config paths per device, as last registered (D783). */
|
|
11957
12607
|
secretConfigPaths = createSecretConfigPathRegistry();
|
|
@@ -12241,6 +12891,7 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
12241
12891
|
const bootRows = await deviceRows.listAll();
|
|
12242
12892
|
for (const row of bootRows) idToAddonId.set(row.meta.id, row.meta.addonId);
|
|
12243
12893
|
const stampIntegrationId$1 = (deviceId, integrationId) => stampIntegrationId(metaStore, this.ctx, deviceId, integrationId);
|
|
12894
|
+
const templates = this.makeTemplateActions();
|
|
12244
12895
|
const pctx = {
|
|
12245
12896
|
host: this.providerHost,
|
|
12246
12897
|
metaStore,
|
|
@@ -12311,6 +12962,8 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
12311
12962
|
loadMeta: (input) => loadMeta(pctx, input),
|
|
12312
12963
|
setName: (input) => setName(pctx, input),
|
|
12313
12964
|
setAlias: (input) => setAlias(pctx, input),
|
|
12965
|
+
renderDeviceTemplates: (input) => templates.render(input),
|
|
12966
|
+
checkDeviceTemplates: (input) => templates.check(input),
|
|
12314
12967
|
setLocation: (input) => setLocation(pctx, input),
|
|
12315
12968
|
setType: (input) => setType(pctx, input),
|
|
12316
12969
|
setIntegrationId: (input) => setIntegrationId(pctx, input),
|
|
@@ -12538,7 +13191,40 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
12538
13191
|
});
|
|
12539
13192
|
return this.removalHooksCleanupImpl;
|
|
12540
13193
|
}
|
|
13194
|
+
/**
|
|
13195
|
+
* The template actions read device rows (alias), bindings (caps) and the
|
|
13196
|
+
* runtime-state slices (`device-state`) — all through what this addon already
|
|
13197
|
+
* owns. Built once per boot;
|
|
13198
|
+
*/
|
|
13199
|
+
makeTemplateActions() {
|
|
13200
|
+
this.templateActions?.dispose();
|
|
13201
|
+
const actions = new DeviceTemplateActions(this.ctx.logger, {
|
|
13202
|
+
findByAlias: async (alias) => {
|
|
13203
|
+
const row = await this.deviceRows?.findByAlias(alias);
|
|
13204
|
+
if (row === void 0 || row === null) return null;
|
|
13205
|
+
return {
|
|
13206
|
+
deviceId: row.meta.id,
|
|
13207
|
+
addonId: row.meta.addonId,
|
|
13208
|
+
stableId: row.meta.stableId,
|
|
13209
|
+
alias
|
|
13210
|
+
};
|
|
13211
|
+
},
|
|
13212
|
+
capsOf: async (deviceId) => (await this.getBindings({ deviceId })).entries.map((entry) => entry.capName),
|
|
13213
|
+
fetchSlice: async (deviceId, cap) => {
|
|
13214
|
+
const api = this.ctx.api;
|
|
13215
|
+
if (api === void 0) throw new Error("ctx.api not ready");
|
|
13216
|
+
return api.deviceState.getCapSlice.query({
|
|
13217
|
+
deviceId,
|
|
13218
|
+
capName: cap
|
|
13219
|
+
});
|
|
13220
|
+
}
|
|
13221
|
+
});
|
|
13222
|
+
this.templateActions = actions;
|
|
13223
|
+
return actions;
|
|
13224
|
+
}
|
|
12541
13225
|
async onShutdown() {
|
|
13226
|
+
this.templateActions?.dispose();
|
|
13227
|
+
this.templateActions = null;
|
|
12542
13228
|
await this.transitions?.stop();
|
|
12543
13229
|
this.transitions = null;
|
|
12544
13230
|
this.removalCleanupImpl?.dispose();
|