@camstack/server 1.2.769 → 1.2.770

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.
Files changed (99) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/api/core/cap-providers.js +4 -193
  3. package/dist/api/trpc/generated-cap-routers.js +40 -30
  4. package/dist/core/addon/integration-visibility.js +0 -5
  5. package/package.json +35 -15
  6. package/system/dist/addon-utils.mjs +1 -1
  7. package/system/dist/{agent-https-C_rkCAus.mjs → agent-https-BGRPCKwT.mjs} +1 -1
  8. package/system/dist/{agent-https-Cw5Wc_ip.js → agent-https-mq6d71iq.js} +1 -1
  9. package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
  10. package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +4 -4
  11. package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
  12. package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +5 -5
  13. package/system/dist/builtins/alerts/alerts.addon.js +1 -1
  14. package/system/dist/builtins/alerts/alerts.addon.mjs +1 -1
  15. package/system/dist/builtins/autotrack/index.js +1 -1
  16. package/system/dist/builtins/autotrack/index.mjs +1 -1
  17. package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -2
  18. package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +5 -5
  19. package/system/dist/builtins/calendars/calendars.addon.js +1 -1
  20. package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
  21. package/system/dist/builtins/camera-grid/index.js +1 -1
  22. package/system/dist/builtins/camera-grid/index.mjs +1 -1
  23. package/system/dist/builtins/composer/composer.addon.js +3 -3
  24. package/system/dist/builtins/composer/composer.addon.mjs +3 -3
  25. package/system/dist/builtins/console-logging/index.js +1 -1
  26. package/system/dist/builtins/console-logging/index.mjs +1 -1
  27. package/system/dist/builtins/core-blocks/core-blocks.addon.js +3 -3
  28. package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +3 -3
  29. package/system/dist/builtins/device-history/device-history.addon.js +2 -2
  30. package/system/dist/builtins/device-history/device-history.addon.mjs +2 -2
  31. package/system/dist/builtins/device-manager/device-manager.addon.js +1 -1
  32. package/system/dist/builtins/device-manager/device-manager.addon.mjs +1 -1
  33. package/system/dist/builtins/hub-forwarder/index.js +1 -1
  34. package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
  35. package/system/dist/builtins/keychain/index.js +17 -10
  36. package/system/dist/builtins/keychain/index.mjs +17 -10
  37. package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
  38. package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
  39. package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
  40. package/system/dist/builtins/local-auth/local-auth.addon.mjs +5 -5
  41. package/system/dist/builtins/local-network/local-network.addon.js +2 -2
  42. package/system/dist/builtins/local-network/local-network.addon.mjs +2 -2
  43. package/system/dist/builtins/loki-logging/index.js +1 -1
  44. package/system/dist/builtins/loki-logging/index.mjs +1 -1
  45. package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
  46. package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +10 -10
  47. package/system/dist/builtins/platform-probe/index.js +1 -1
  48. package/system/dist/builtins/platform-probe/index.mjs +10 -10
  49. package/system/dist/builtins/record-entries/index.js +1 -1
  50. package/system/dist/builtins/record-entries/index.mjs +1 -1
  51. package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
  52. package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
  53. package/system/dist/builtins/snapshot/index.js +1 -1
  54. package/system/dist/builtins/snapshot/index.mjs +1 -1
  55. package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
  56. package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +20 -20
  57. package/system/dist/builtins/sqlite-storage/index.js +1 -1
  58. package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
  59. package/system/dist/builtins/sqlite-storage/integration-record-kinds.d.ts +3 -10
  60. package/system/dist/builtins/sqlite-storage/integration-registry.d.ts +1 -11
  61. package/system/dist/builtins/sqlite-storage/integration-rows-ports.d.ts +13 -15
  62. package/system/dist/builtins/sqlite-storage/migrations/legacy-integration-rows-door.d.ts +31 -0
  63. package/system/dist/builtins/sqlite-storage/migrations/migrations-provider.d.ts +39 -1
  64. package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +48 -5
  65. package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +48 -5
  66. package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
  67. package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
  68. package/system/dist/builtins/system-config/system-config.addon.js +3 -3
  69. package/system/dist/builtins/system-config/system-config.addon.mjs +115 -115
  70. package/system/dist/builtins/winston-logging/index.js +1 -1
  71. package/system/dist/builtins/winston-logging/index.mjs +1 -1
  72. package/system/dist/{capture-engine-DXSWU1PY.js → capture-engine--FQ8NUHh.js} +1 -1
  73. package/system/dist/{capture-engine-Kl46lSBi.mjs → capture-engine-Cmx7DIzV.mjs} +1 -1
  74. package/system/dist/{composition-sources-ClCP809M.mjs → composition-sources-Bd09D5f4.mjs} +1 -1
  75. package/system/dist/{composition-sources-Cq0IeRv9.js → composition-sources-cvZAN87H.js} +1 -1
  76. package/system/dist/{constants-CDxTuuXp.mjs → constants-Dce28BAx.mjs} +1 -1
  77. package/system/dist/{constants-CI5ySMIj.js → constants-gM9xHjyj.js} +1 -1
  78. package/system/dist/{coral-accelerators-BW3OcrLD.mjs → coral-accelerators-DBlgWB8k.mjs} +6 -6
  79. package/system/dist/{custom-action-registry-BIJZBObd.mjs → custom-action-registry-oHl1xltx.mjs} +37 -37
  80. package/system/dist/{device-ca-DPvU-3TE.js → device-ca-BmeH6Y9E.js} +1 -1
  81. package/system/dist/{device-ca-uRLJ8-jZ.mjs → device-ca-DcMuT-Yp.mjs} +1 -1
  82. package/system/dist/{dist-C_SNsYGY.mjs → dist-C3WXy3Ix.mjs} +78 -55
  83. package/system/dist/{dist-BDixk7cu.js → dist-Dtw1sIeQ.js} +78 -55
  84. package/system/dist/file-host-worker.js +1 -1
  85. package/system/dist/file-host-worker.mjs +1 -1
  86. package/system/dist/{hub-mdns-ciao-7uRbexmH.js → hub-mdns-ciao-C49eqyby.js} +2 -2
  87. package/system/dist/{hub-mdns-ciao-ea1EwFAF.mjs → hub-mdns-ciao-Dfue_0kR.mjs} +2 -2
  88. package/system/dist/index.d.ts +1 -1
  89. package/system/dist/index.js +42 -230
  90. package/system/dist/index.mjs +262 -450
  91. package/system/dist/legacy-integration-rows-door-B8_XCqUX.js +290 -0
  92. package/system/dist/legacy-integration-rows-door-DK42Bh2Q.mjs +255 -0
  93. package/system/dist/migrations/hub/2026-11-05-drop-integration-settings.d.ts +16 -0
  94. package/system/dist/{restore-archive-D0y_tFM2.mjs → restore-archive-C0YDO-qd.mjs} +94 -94
  95. package/system/dist/{restore-archive-CpTnlXUA.js → restore-archive-DuktQtye.js} +1 -1
  96. package/system/dist/{wal-maintenance-Dy4gxMdb.js → wal-maintenance-BEo-y-cQ.js} +0 -0
  97. package/system/dist/{wal-maintenance-BvxzVfje.mjs → wal-maintenance-DhuvKiGK.mjs} +0 -0
  98. package/system/package.json +1 -1
  99. package/dist/api/core/integration-secrets.js +0 -96
@@ -0,0 +1,290 @@
1
+ const require_dist = require("./dist-Dtw1sIeQ.js");
2
+ //#region src/builtins/sqlite-storage/migrations/legacy-integration-rows-door.ts
3
+ /**
4
+ * Migration-only door for the leftover integration rows (planning#416).
5
+ *
6
+ * Addon copy migrations run in the addon process, so they cannot open the
7
+ * settings file (D679). This module is the read and the clear on the OWNER's
8
+ * connection, served only by `migrations.readLegacyIntegrationRows` and
9
+ * `migrations.clearLegacyIntegrationRows` (and the two writes the Home
10
+ * Assistant copy migrations still need). It is deleted with the last copy
11
+ * migration.
12
+ *
13
+ * A node that already dropped the store answers with no rows and accepts a
14
+ * clear as a no-op. It never throws because the table or the record kind is
15
+ * gone. Secrets stay markers here: sqlite-settings cannot speak for another
16
+ * addon's keychain (D787). The owning addon's ports resolve and release them
17
+ * with `resolveSecretMarkers`, the same call the old settings door used.
18
+ */
19
+ var LEGACY_INTEGRATION_ROWS_TABLE = "integration_settings";
20
+ var SETTINGS_TABLE = LEGACY_INTEGRATION_ROWS_TABLE;
21
+ var INTEGRATIONS_TABLE = "integrations";
22
+ var SETTINGS_KIND = "integration-settings";
23
+ var INTEGRATIONS_KIND = "integrations";
24
+ var KIND_OWNER = "integration-registry";
25
+ function hasTable(db, name) {
26
+ return db.prepare("SELECT 1 FROM sqlite_master WHERE type = ? AND name = ?").get("table", name) !== void 0;
27
+ }
28
+ function columnsOf(db, table) {
29
+ const names = /* @__PURE__ */ new Set();
30
+ for (const row of db.prepare(`PRAGMA table_info("${table}")`).all()) {
31
+ if (typeof row !== "object" || row === null || !("name" in row)) continue;
32
+ const name = Reflect.get(row, "name");
33
+ if (typeof name === "string") names.add(name);
34
+ }
35
+ return names;
36
+ }
37
+ function cell(row, key) {
38
+ if (typeof row !== "object" || row === null) return void 0;
39
+ return Reflect.get(row, key);
40
+ }
41
+ function textField(row, key) {
42
+ if (typeof row !== "object" || row === null) return null;
43
+ const value = Reflect.get(row, key);
44
+ return typeof value === "string" && value.length > 0 ? value : null;
45
+ }
46
+ function kindIdOf(db, kind) {
47
+ if (!hasTable(db, "_record_kinds")) return null;
48
+ const row = db.prepare(`SELECT kind_id FROM _record_kinds WHERE owner = ? AND kind = ? AND state <> 'dropped'`).get(KIND_OWNER, kind);
49
+ if (typeof row !== "object" || row === null) return null;
50
+ const kindId = Reflect.get(row, "kind_id");
51
+ return typeof kindId === "number" ? kindId : null;
52
+ }
53
+ function serializeSetting(value) {
54
+ if (typeof value === "number") return {
55
+ value: String(value),
56
+ valueType: "number"
57
+ };
58
+ if (typeof value === "boolean") return {
59
+ value: String(value),
60
+ valueType: "boolean"
61
+ };
62
+ if (typeof value === "string") return {
63
+ value,
64
+ valueType: "string"
65
+ };
66
+ return {
67
+ value: JSON.stringify(value),
68
+ valueType: "json"
69
+ };
70
+ }
71
+ /** Decode one stored cell. A `json` cell may already be an object (the collection surface parses it). */
72
+ function deserializeSetting(raw, valueType) {
73
+ if (valueType === "json") {
74
+ if (typeof raw !== "string") return raw;
75
+ try {
76
+ return JSON.parse(raw);
77
+ } catch {
78
+ return raw;
79
+ }
80
+ }
81
+ const value = String(raw ?? "");
82
+ if (valueType === "number") return Number(value);
83
+ if (valueType === "boolean") return value === "true";
84
+ return value;
85
+ }
86
+ function putCell(into, key, raw, valueType) {
87
+ if (key === null) return;
88
+ into[key] = deserializeSetting(raw, valueType ?? "string");
89
+ }
90
+ function readPhysical(db, integrationId) {
91
+ if (!hasTable(db, SETTINGS_TABLE)) return {};
92
+ const columns = columnsOf(db, SETTINGS_TABLE);
93
+ const out = {};
94
+ if (columns.has("integration_id")) {
95
+ for (const row of db.prepare("SELECT key, value, value_type AS valueType FROM integration_settings WHERE integration_id = ?").all(integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
96
+ return out;
97
+ }
98
+ if (!columns.has("data")) return {};
99
+ for (const row of db.prepare(`SELECT json_extract(data, '$.key') AS key, json_extract(data, '$.value') AS value,
100
+ json_extract(data, '$.value_type') AS valueType
101
+ FROM integration_settings WHERE json_extract(data, '$.integration_id') = ?`).all(integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
102
+ return out;
103
+ }
104
+ function readRecordKind(db, integrationId) {
105
+ const kindId = kindIdOf(db, SETTINGS_KIND);
106
+ if (kindId === null || !hasTable(db, "records")) return {};
107
+ const out = {};
108
+ for (const row of db.prepare(`SELECT json_extract(data, '$.key') AS key, json_extract(data, '$.value') AS value,
109
+ json_extract(data, '$.valueType') AS valueType
110
+ FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?`).all(kindId, integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
111
+ return out;
112
+ }
113
+ function storePresent(db) {
114
+ return hasTable(db, SETTINGS_TABLE) || kindIdOf(db, SETTINGS_KIND) !== null;
115
+ }
116
+ /** The rows of one integration. Empty when the store is gone or the integration has none. */
117
+ function readLegacyIntegrationRows(db, integrationId) {
118
+ return {
119
+ ...readPhysical(db, integrationId),
120
+ ...readRecordKind(db, integrationId)
121
+ };
122
+ }
123
+ function deletePhysical(db, integrationId) {
124
+ if (!hasTable(db, SETTINGS_TABLE)) return 0;
125
+ const columns = columnsOf(db, SETTINGS_TABLE);
126
+ if (columns.has("integration_id")) {
127
+ const result = db.prepare("DELETE FROM integration_settings WHERE integration_id = ?").run(integrationId);
128
+ return Number(result.changes);
129
+ }
130
+ if (!columns.has("data")) return 0;
131
+ const result = db.prepare(`DELETE FROM integration_settings WHERE json_extract(data, '$.integration_id') = ?`).run(integrationId);
132
+ return Number(result.changes);
133
+ }
134
+ function deleteRecordKind(db, integrationId) {
135
+ const kindId = kindIdOf(db, SETTINGS_KIND);
136
+ if (kindId === null || !hasTable(db, "records")) return 0;
137
+ const result = db.prepare(`DELETE FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?`).run(kindId, integrationId);
138
+ return Number(result.changes);
139
+ }
140
+ /**
141
+ * Delete one integration's rows. Does not touch the keychain: the caller
142
+ * releases the refs first, while the rows still point at them. A missing
143
+ * store is a no-op. `secrets` is how many marker refs the rows held.
144
+ */
145
+ function clearLegacyIntegrationRows(db, integrationId) {
146
+ const refs = require_dist.collectSecretMarkerRefs(readLegacyIntegrationRows(db, integrationId));
147
+ return {
148
+ rows: deletePhysical(db, integrationId) + deleteRecordKind(db, integrationId),
149
+ secrets: refs.length
150
+ };
151
+ }
152
+ function upsertPhysical(db, integrationId, key, encoded) {
153
+ if (!hasTable(db, SETTINGS_TABLE)) return false;
154
+ const columns = columnsOf(db, SETTINGS_TABLE);
155
+ const rowId = `${integrationId}:${key}`;
156
+ if (columns.has("integration_id")) {
157
+ db.prepare("DELETE FROM integration_settings WHERE integration_id = ? AND key = ?").run(integrationId, key);
158
+ db.prepare("INSERT INTO integration_settings (id, integration_id, key, value, value_type) VALUES (?, ?, ?, ?, ?)").run(rowId, integrationId, key, encoded.value, encoded.valueType);
159
+ return true;
160
+ }
161
+ if (!columns.has("data")) return false;
162
+ const body = JSON.stringify({
163
+ integration_id: integrationId,
164
+ key,
165
+ value: encoded.value,
166
+ value_type: encoded.valueType
167
+ });
168
+ const existing = db.prepare(`SELECT id FROM integration_settings
169
+ WHERE json_extract(data, '$.integration_id') = ? AND json_extract(data, '$.key') = ?`).get(integrationId, key);
170
+ const id = textField(existing, "id") ?? rowId;
171
+ if (existing !== void 0) db.prepare("UPDATE integration_settings SET data = ? WHERE id = ?").run(body, id);
172
+ else db.prepare("INSERT INTO integration_settings (id, data) VALUES (?, ?)").run(id, body);
173
+ return true;
174
+ }
175
+ function upsertRecordKind(db, integrationId, key, encoded) {
176
+ const kindId = kindIdOf(db, SETTINGS_KIND);
177
+ if (kindId === null || !hasTable(db, "records")) return false;
178
+ const rowId = `${integrationId}:${key}`;
179
+ const body = JSON.stringify({
180
+ integrationId,
181
+ key,
182
+ value: encoded.value,
183
+ valueType: encoded.valueType
184
+ });
185
+ const now = Date.now();
186
+ db.prepare(`INSERT INTO records (kind_id, id, rev, updated_at, data) VALUES (?, ?, 1, ?, ?)
187
+ ON CONFLICT(kind_id, id) DO UPDATE SET data = excluded.data, updated_at = excluded.updated_at, rev = rev + 1`).run(kindId, rowId, now, body);
188
+ return true;
189
+ }
190
+ /** Upsert only the keys in `patch`. Other keys, including secret markers, stay as stored. */
191
+ function patchLegacyIntegrationRows(db, integrationId, patch) {
192
+ if (!storePresent(db)) return { written: false };
193
+ for (const [key, value] of Object.entries(patch)) {
194
+ const encoded = serializeSetting(value);
195
+ const wroteRecord = upsertRecordKind(db, integrationId, key, encoded);
196
+ const wroteTable = upsertPhysical(db, integrationId, key, encoded);
197
+ if (!wroteRecord && !wroteTable) return { written: false };
198
+ }
199
+ return { written: true };
200
+ }
201
+ function deletePhysicalKeys(db, integrationId, keys) {
202
+ if (!hasTable(db, SETTINGS_TABLE) || keys.size === 0) return;
203
+ const columns = columnsOf(db, SETTINGS_TABLE);
204
+ if (columns.has("integration_id")) {
205
+ const statement = db.prepare("DELETE FROM integration_settings WHERE integration_id = ? AND key = ?");
206
+ for (const key of keys) statement.run(integrationId, key);
207
+ return;
208
+ }
209
+ if (!columns.has("data")) return;
210
+ const statement = db.prepare(`DELETE FROM integration_settings
211
+ WHERE json_extract(data, '$.integration_id') = ? AND json_extract(data, '$.key') = ?`);
212
+ for (const key of keys) statement.run(integrationId, key);
213
+ }
214
+ function deleteRecordKeys(db, integrationId, keys) {
215
+ const kindId = kindIdOf(db, SETTINGS_KIND);
216
+ if (kindId === null || !hasTable(db, "records") || keys.size === 0) return;
217
+ const statement = db.prepare(`DELETE FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?
218
+ AND json_extract(data, '$.key') = ?`);
219
+ for (const key of keys) statement.run(kindId, integrationId, key);
220
+ }
221
+ /** Remove named keys. `present` is false when the store is already gone. */
222
+ function deleteLegacyIntegrationRowKeys(db, integrationId, keys) {
223
+ if (!storePresent(db)) return {
224
+ present: false,
225
+ removed: {}
226
+ };
227
+ const stored = readLegacyIntegrationRows(db, integrationId);
228
+ const drop = new Set(keys);
229
+ const removed = {};
230
+ for (const key of drop) if (Object.prototype.hasOwnProperty.call(stored, key)) removed[key] = stored[key];
231
+ deletePhysicalKeys(db, integrationId, drop);
232
+ deleteRecordKeys(db, integrationId, drop);
233
+ return {
234
+ present: true,
235
+ removed
236
+ };
237
+ }
238
+ function addonFromTable(db, integrationId) {
239
+ if (!hasTable(db, INTEGRATIONS_TABLE)) return null;
240
+ const columns = columnsOf(db, INTEGRATIONS_TABLE);
241
+ if (columns.has("addon_id")) return textField(db.prepare("SELECT addon_id AS addonId FROM integrations WHERE id = ?").get(integrationId), "addonId");
242
+ if (!columns.has("data")) return null;
243
+ return textField(db.prepare(`SELECT json_extract(data, '$.addon_id') AS addonId FROM integrations WHERE id = ?`).get(integrationId), "addonId");
244
+ }
245
+ function addonFromRecords(db, integrationId) {
246
+ const kindId = kindIdOf(db, INTEGRATIONS_KIND);
247
+ if (kindId === null || !hasTable(db, "records")) return null;
248
+ return textField(db.prepare(`SELECT json_extract(data, '$.addonId') AS addonId FROM records WHERE kind_id = ? AND id = ?`).get(kindId, integrationId), "addonId");
249
+ }
250
+ /** The addon that owns the integration, or null when the integration is gone. */
251
+ function legacyIntegrationAddonId(db, integrationId) {
252
+ return addonFromRecords(db, integrationId) ?? addonFromTable(db, integrationId);
253
+ }
254
+ //#endregion
255
+ Object.defineProperty(exports, "LEGACY_INTEGRATION_ROWS_TABLE", {
256
+ enumerable: true,
257
+ get: function() {
258
+ return LEGACY_INTEGRATION_ROWS_TABLE;
259
+ }
260
+ });
261
+ Object.defineProperty(exports, "clearLegacyIntegrationRows", {
262
+ enumerable: true,
263
+ get: function() {
264
+ return clearLegacyIntegrationRows;
265
+ }
266
+ });
267
+ Object.defineProperty(exports, "deleteLegacyIntegrationRowKeys", {
268
+ enumerable: true,
269
+ get: function() {
270
+ return deleteLegacyIntegrationRowKeys;
271
+ }
272
+ });
273
+ Object.defineProperty(exports, "legacyIntegrationAddonId", {
274
+ enumerable: true,
275
+ get: function() {
276
+ return legacyIntegrationAddonId;
277
+ }
278
+ });
279
+ Object.defineProperty(exports, "patchLegacyIntegrationRows", {
280
+ enumerable: true,
281
+ get: function() {
282
+ return patchLegacyIntegrationRows;
283
+ }
284
+ });
285
+ Object.defineProperty(exports, "readLegacyIntegrationRows", {
286
+ enumerable: true,
287
+ get: function() {
288
+ return readLegacyIntegrationRows;
289
+ }
290
+ });
@@ -0,0 +1,255 @@
1
+ import { fs as collectSecretMarkerRefs } from "./dist-C3WXy3Ix.mjs";
2
+ //#region src/builtins/sqlite-storage/migrations/legacy-integration-rows-door.ts
3
+ /**
4
+ * Migration-only door for the leftover integration rows (planning#416).
5
+ *
6
+ * Addon copy migrations run in the addon process, so they cannot open the
7
+ * settings file (D679). This module is the read and the clear on the OWNER's
8
+ * connection, served only by `migrations.readLegacyIntegrationRows` and
9
+ * `migrations.clearLegacyIntegrationRows` (and the two writes the Home
10
+ * Assistant copy migrations still need). It is deleted with the last copy
11
+ * migration.
12
+ *
13
+ * A node that already dropped the store answers with no rows and accepts a
14
+ * clear as a no-op. It never throws because the table or the record kind is
15
+ * gone. Secrets stay markers here: sqlite-settings cannot speak for another
16
+ * addon's keychain (D787). The owning addon's ports resolve and release them
17
+ * with `resolveSecretMarkers`, the same call the old settings door used.
18
+ */
19
+ var LEGACY_INTEGRATION_ROWS_TABLE = "integration_settings";
20
+ var SETTINGS_TABLE = LEGACY_INTEGRATION_ROWS_TABLE;
21
+ var INTEGRATIONS_TABLE = "integrations";
22
+ var SETTINGS_KIND = "integration-settings";
23
+ var INTEGRATIONS_KIND = "integrations";
24
+ var KIND_OWNER = "integration-registry";
25
+ function hasTable(db, name) {
26
+ return db.prepare("SELECT 1 FROM sqlite_master WHERE type = ? AND name = ?").get("table", name) !== void 0;
27
+ }
28
+ function columnsOf(db, table) {
29
+ const names = /* @__PURE__ */ new Set();
30
+ for (const row of db.prepare(`PRAGMA table_info("${table}")`).all()) {
31
+ if (typeof row !== "object" || row === null || !("name" in row)) continue;
32
+ const name = Reflect.get(row, "name");
33
+ if (typeof name === "string") names.add(name);
34
+ }
35
+ return names;
36
+ }
37
+ function cell(row, key) {
38
+ if (typeof row !== "object" || row === null) return void 0;
39
+ return Reflect.get(row, key);
40
+ }
41
+ function textField(row, key) {
42
+ if (typeof row !== "object" || row === null) return null;
43
+ const value = Reflect.get(row, key);
44
+ return typeof value === "string" && value.length > 0 ? value : null;
45
+ }
46
+ function kindIdOf(db, kind) {
47
+ if (!hasTable(db, "_record_kinds")) return null;
48
+ const row = db.prepare(`SELECT kind_id FROM _record_kinds WHERE owner = ? AND kind = ? AND state <> 'dropped'`).get(KIND_OWNER, kind);
49
+ if (typeof row !== "object" || row === null) return null;
50
+ const kindId = Reflect.get(row, "kind_id");
51
+ return typeof kindId === "number" ? kindId : null;
52
+ }
53
+ function serializeSetting(value) {
54
+ if (typeof value === "number") return {
55
+ value: String(value),
56
+ valueType: "number"
57
+ };
58
+ if (typeof value === "boolean") return {
59
+ value: String(value),
60
+ valueType: "boolean"
61
+ };
62
+ if (typeof value === "string") return {
63
+ value,
64
+ valueType: "string"
65
+ };
66
+ return {
67
+ value: JSON.stringify(value),
68
+ valueType: "json"
69
+ };
70
+ }
71
+ /** Decode one stored cell. A `json` cell may already be an object (the collection surface parses it). */
72
+ function deserializeSetting(raw, valueType) {
73
+ if (valueType === "json") {
74
+ if (typeof raw !== "string") return raw;
75
+ try {
76
+ return JSON.parse(raw);
77
+ } catch {
78
+ return raw;
79
+ }
80
+ }
81
+ const value = String(raw ?? "");
82
+ if (valueType === "number") return Number(value);
83
+ if (valueType === "boolean") return value === "true";
84
+ return value;
85
+ }
86
+ function putCell(into, key, raw, valueType) {
87
+ if (key === null) return;
88
+ into[key] = deserializeSetting(raw, valueType ?? "string");
89
+ }
90
+ function readPhysical(db, integrationId) {
91
+ if (!hasTable(db, SETTINGS_TABLE)) return {};
92
+ const columns = columnsOf(db, SETTINGS_TABLE);
93
+ const out = {};
94
+ if (columns.has("integration_id")) {
95
+ for (const row of db.prepare("SELECT key, value, value_type AS valueType FROM integration_settings WHERE integration_id = ?").all(integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
96
+ return out;
97
+ }
98
+ if (!columns.has("data")) return {};
99
+ for (const row of db.prepare(`SELECT json_extract(data, '$.key') AS key, json_extract(data, '$.value') AS value,
100
+ json_extract(data, '$.value_type') AS valueType
101
+ FROM integration_settings WHERE json_extract(data, '$.integration_id') = ?`).all(integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
102
+ return out;
103
+ }
104
+ function readRecordKind(db, integrationId) {
105
+ const kindId = kindIdOf(db, SETTINGS_KIND);
106
+ if (kindId === null || !hasTable(db, "records")) return {};
107
+ const out = {};
108
+ for (const row of db.prepare(`SELECT json_extract(data, '$.key') AS key, json_extract(data, '$.value') AS value,
109
+ json_extract(data, '$.valueType') AS valueType
110
+ FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?`).all(kindId, integrationId)) putCell(out, textField(row, "key"), cell(row, "value"), textField(row, "valueType"));
111
+ return out;
112
+ }
113
+ function storePresent(db) {
114
+ return hasTable(db, SETTINGS_TABLE) || kindIdOf(db, SETTINGS_KIND) !== null;
115
+ }
116
+ /** The rows of one integration. Empty when the store is gone or the integration has none. */
117
+ function readLegacyIntegrationRows(db, integrationId) {
118
+ return {
119
+ ...readPhysical(db, integrationId),
120
+ ...readRecordKind(db, integrationId)
121
+ };
122
+ }
123
+ function deletePhysical(db, integrationId) {
124
+ if (!hasTable(db, SETTINGS_TABLE)) return 0;
125
+ const columns = columnsOf(db, SETTINGS_TABLE);
126
+ if (columns.has("integration_id")) {
127
+ const result = db.prepare("DELETE FROM integration_settings WHERE integration_id = ?").run(integrationId);
128
+ return Number(result.changes);
129
+ }
130
+ if (!columns.has("data")) return 0;
131
+ const result = db.prepare(`DELETE FROM integration_settings WHERE json_extract(data, '$.integration_id') = ?`).run(integrationId);
132
+ return Number(result.changes);
133
+ }
134
+ function deleteRecordKind(db, integrationId) {
135
+ const kindId = kindIdOf(db, SETTINGS_KIND);
136
+ if (kindId === null || !hasTable(db, "records")) return 0;
137
+ const result = db.prepare(`DELETE FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?`).run(kindId, integrationId);
138
+ return Number(result.changes);
139
+ }
140
+ /**
141
+ * Delete one integration's rows. Does not touch the keychain: the caller
142
+ * releases the refs first, while the rows still point at them. A missing
143
+ * store is a no-op. `secrets` is how many marker refs the rows held.
144
+ */
145
+ function clearLegacyIntegrationRows(db, integrationId) {
146
+ const refs = collectSecretMarkerRefs(readLegacyIntegrationRows(db, integrationId));
147
+ return {
148
+ rows: deletePhysical(db, integrationId) + deleteRecordKind(db, integrationId),
149
+ secrets: refs.length
150
+ };
151
+ }
152
+ function upsertPhysical(db, integrationId, key, encoded) {
153
+ if (!hasTable(db, SETTINGS_TABLE)) return false;
154
+ const columns = columnsOf(db, SETTINGS_TABLE);
155
+ const rowId = `${integrationId}:${key}`;
156
+ if (columns.has("integration_id")) {
157
+ db.prepare("DELETE FROM integration_settings WHERE integration_id = ? AND key = ?").run(integrationId, key);
158
+ db.prepare("INSERT INTO integration_settings (id, integration_id, key, value, value_type) VALUES (?, ?, ?, ?, ?)").run(rowId, integrationId, key, encoded.value, encoded.valueType);
159
+ return true;
160
+ }
161
+ if (!columns.has("data")) return false;
162
+ const body = JSON.stringify({
163
+ integration_id: integrationId,
164
+ key,
165
+ value: encoded.value,
166
+ value_type: encoded.valueType
167
+ });
168
+ const existing = db.prepare(`SELECT id FROM integration_settings
169
+ WHERE json_extract(data, '$.integration_id') = ? AND json_extract(data, '$.key') = ?`).get(integrationId, key);
170
+ const id = textField(existing, "id") ?? rowId;
171
+ if (existing !== void 0) db.prepare("UPDATE integration_settings SET data = ? WHERE id = ?").run(body, id);
172
+ else db.prepare("INSERT INTO integration_settings (id, data) VALUES (?, ?)").run(id, body);
173
+ return true;
174
+ }
175
+ function upsertRecordKind(db, integrationId, key, encoded) {
176
+ const kindId = kindIdOf(db, SETTINGS_KIND);
177
+ if (kindId === null || !hasTable(db, "records")) return false;
178
+ const rowId = `${integrationId}:${key}`;
179
+ const body = JSON.stringify({
180
+ integrationId,
181
+ key,
182
+ value: encoded.value,
183
+ valueType: encoded.valueType
184
+ });
185
+ const now = Date.now();
186
+ db.prepare(`INSERT INTO records (kind_id, id, rev, updated_at, data) VALUES (?, ?, 1, ?, ?)
187
+ ON CONFLICT(kind_id, id) DO UPDATE SET data = excluded.data, updated_at = excluded.updated_at, rev = rev + 1`).run(kindId, rowId, now, body);
188
+ return true;
189
+ }
190
+ /** Upsert only the keys in `patch`. Other keys, including secret markers, stay as stored. */
191
+ function patchLegacyIntegrationRows(db, integrationId, patch) {
192
+ if (!storePresent(db)) return { written: false };
193
+ for (const [key, value] of Object.entries(patch)) {
194
+ const encoded = serializeSetting(value);
195
+ const wroteRecord = upsertRecordKind(db, integrationId, key, encoded);
196
+ const wroteTable = upsertPhysical(db, integrationId, key, encoded);
197
+ if (!wroteRecord && !wroteTable) return { written: false };
198
+ }
199
+ return { written: true };
200
+ }
201
+ function deletePhysicalKeys(db, integrationId, keys) {
202
+ if (!hasTable(db, SETTINGS_TABLE) || keys.size === 0) return;
203
+ const columns = columnsOf(db, SETTINGS_TABLE);
204
+ if (columns.has("integration_id")) {
205
+ const statement = db.prepare("DELETE FROM integration_settings WHERE integration_id = ? AND key = ?");
206
+ for (const key of keys) statement.run(integrationId, key);
207
+ return;
208
+ }
209
+ if (!columns.has("data")) return;
210
+ const statement = db.prepare(`DELETE FROM integration_settings
211
+ WHERE json_extract(data, '$.integration_id') = ? AND json_extract(data, '$.key') = ?`);
212
+ for (const key of keys) statement.run(integrationId, key);
213
+ }
214
+ function deleteRecordKeys(db, integrationId, keys) {
215
+ const kindId = kindIdOf(db, SETTINGS_KIND);
216
+ if (kindId === null || !hasTable(db, "records") || keys.size === 0) return;
217
+ const statement = db.prepare(`DELETE FROM records WHERE kind_id = ? AND json_extract(data, '$.integrationId') = ?
218
+ AND json_extract(data, '$.key') = ?`);
219
+ for (const key of keys) statement.run(kindId, integrationId, key);
220
+ }
221
+ /** Remove named keys. `present` is false when the store is already gone. */
222
+ function deleteLegacyIntegrationRowKeys(db, integrationId, keys) {
223
+ if (!storePresent(db)) return {
224
+ present: false,
225
+ removed: {}
226
+ };
227
+ const stored = readLegacyIntegrationRows(db, integrationId);
228
+ const drop = new Set(keys);
229
+ const removed = {};
230
+ for (const key of drop) if (Object.prototype.hasOwnProperty.call(stored, key)) removed[key] = stored[key];
231
+ deletePhysicalKeys(db, integrationId, drop);
232
+ deleteRecordKeys(db, integrationId, drop);
233
+ return {
234
+ present: true,
235
+ removed
236
+ };
237
+ }
238
+ function addonFromTable(db, integrationId) {
239
+ if (!hasTable(db, INTEGRATIONS_TABLE)) return null;
240
+ const columns = columnsOf(db, INTEGRATIONS_TABLE);
241
+ if (columns.has("addon_id")) return textField(db.prepare("SELECT addon_id AS addonId FROM integrations WHERE id = ?").get(integrationId), "addonId");
242
+ if (!columns.has("data")) return null;
243
+ return textField(db.prepare(`SELECT json_extract(data, '$.addon_id') AS addonId FROM integrations WHERE id = ?`).get(integrationId), "addonId");
244
+ }
245
+ function addonFromRecords(db, integrationId) {
246
+ const kindId = kindIdOf(db, INTEGRATIONS_KIND);
247
+ if (kindId === null || !hasTable(db, "records")) return null;
248
+ return textField(db.prepare(`SELECT json_extract(data, '$.addonId') AS addonId FROM records WHERE kind_id = ? AND id = ?`).get(kindId, integrationId), "addonId");
249
+ }
250
+ /** The addon that owns the integration, or null when the integration is gone. */
251
+ function legacyIntegrationAddonId(db, integrationId) {
252
+ return addonFromRecords(db, integrationId) ?? addonFromTable(db, integrationId);
253
+ }
254
+ //#endregion
255
+ export { patchLegacyIntegrationRows as a, legacyIntegrationAddonId as i, clearLegacyIntegrationRows as n, readLegacyIntegrationRows as o, deleteLegacyIntegrationRowKeys as r, LEGACY_INTEGRATION_ROWS_TABLE as t };
@@ -0,0 +1,16 @@
1
+ import type { HubSqlMigration, MigrationDatabase } from "../../builtins/sqlite-storage/migrations/migration-types.js";
2
+ export declare const DROP_INTEGRATION_SETTINGS_MIGRATION_ID = "hub/2026-11-05-drop-integration-settings";
3
+ export interface UnmigratedIntegrationSettings {
4
+ readonly addonId: string;
5
+ readonly rowCount: number;
6
+ }
7
+ /** The drop refused: rows remain for an addon whose copy migration is not applied. */
8
+ export declare class IntegrationSettingsDropRefused extends Error {
9
+ readonly blocked: readonly UnmigratedIntegrationSettings[];
10
+ readonly addonId: string;
11
+ readonly rowCount: number;
12
+ constructor(blocked: readonly UnmigratedIntegrationSettings[]);
13
+ }
14
+ /** Rows of the legacy store, counted per owning addon. An unresolved owner is `(unknown)`. */
15
+ export declare function legacyIntegrationSettingsByAddon(db: MigrationDatabase): readonly UnmigratedIntegrationSettings[];
16
+ export declare const DROP_INTEGRATION_SETTINGS_MIGRATION: HubSqlMigration;