@camstack/kernel 0.1.1 → 0.1.3
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/dist/addon-installer-COAMHJ2E.mjs +6 -0
- package/dist/chunk-GVWOO5YX.mjs +494 -0
- package/dist/chunk-GVWOO5YX.mjs.map +1 -0
- package/dist/chunk-LZOMFHX3.mjs +38 -0
- package/dist/chunk-RHK5CCAL.mjs +154 -0
- package/dist/{chunk-S5YWNNPK.mjs.map → chunk-RHK5CCAL.mjs.map} +1 -1
- package/dist/index.d.mts +20 -18
- package/dist/index.d.ts +20 -18
- package/dist/index.js +2037 -1694
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1462 -1230
- package/dist/index.mjs.map +1 -1
- package/dist/worker/addon-worker-entry.js +431 -272
- package/dist/worker/addon-worker-entry.js.map +1 -1
- package/dist/worker/addon-worker-entry.mjs +8 -5
- package/dist/worker/addon-worker-entry.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/addon-installer-RE7RJMVP.mjs +0 -8
- package/dist/chunk-BJTO5JO5.mjs +0 -11
- package/dist/chunk-REHQJR3P.mjs +0 -387
- package/dist/chunk-REHQJR3P.mjs.map +0 -1
- package/dist/chunk-S5YWNNPK.mjs +0 -135
- /package/dist/{addon-installer-RE7RJMVP.mjs.map → addon-installer-COAMHJ2E.mjs.map} +0 -0
- /package/dist/{chunk-BJTO5JO5.mjs.map → chunk-LZOMFHX3.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
8
11
|
var __export = (target, all) => {
|
|
9
12
|
for (var name in all)
|
|
10
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -27,1785 +30,2125 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
30
|
));
|
|
28
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
32
|
|
|
30
|
-
// src/
|
|
31
|
-
var
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
isSourceNewer: () => isSourceNewer,
|
|
52
|
-
stripCamstackDeps: () => stripCamstackDeps,
|
|
53
|
-
symlinkAddonsToNodeModules: () => symlinkAddonsToNodeModules
|
|
54
|
-
});
|
|
55
|
-
module.exports = __toCommonJS(src_exports);
|
|
56
|
-
|
|
57
|
-
// src/addon-loader.ts
|
|
58
|
-
var fs = __toESM(require("fs"));
|
|
59
|
-
var path = __toESM(require("path"));
|
|
60
|
-
function resolveAddonClass(mod) {
|
|
61
|
-
let candidate = mod["default"] ?? mod[Object.keys(mod)[0]];
|
|
62
|
-
if (candidate && typeof candidate === "object" && "default" in candidate) {
|
|
63
|
-
candidate = candidate["default"];
|
|
64
|
-
}
|
|
65
|
-
return typeof candidate === "function" ? candidate : void 0;
|
|
66
|
-
}
|
|
67
|
-
var AddonLoader = class {
|
|
68
|
-
addons = /* @__PURE__ */ new Map();
|
|
69
|
-
/** Scan addons directory and load all addon packages.
|
|
70
|
-
* Supports scoped layout: addons/@scope/package-name/ */
|
|
71
|
-
async loadFromDirectory(addonsDir) {
|
|
72
|
-
if (!fs.existsSync(addonsDir)) return;
|
|
73
|
-
const entries = fs.readdirSync(addonsDir, { withFileTypes: true });
|
|
74
|
-
for (const entry of entries) {
|
|
75
|
-
if (!entry.isDirectory()) continue;
|
|
76
|
-
if (entry.name.startsWith("@")) {
|
|
77
|
-
const scopeDir = path.join(addonsDir, entry.name);
|
|
78
|
-
const scopeEntries = fs.readdirSync(scopeDir, { withFileTypes: true });
|
|
79
|
-
for (const scopeEntry of scopeEntries) {
|
|
80
|
-
if (!scopeEntry.isDirectory()) continue;
|
|
81
|
-
await this.tryLoadAddon(path.join(scopeDir, scopeEntry.name));
|
|
82
|
-
}
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
await this.tryLoadAddon(path.join(addonsDir, entry.name));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
async tryLoadAddon(addonDir) {
|
|
89
|
-
const pkgJsonPath = path.join(addonDir, "package.json");
|
|
90
|
-
if (!fs.existsSync(pkgJsonPath)) return;
|
|
91
|
-
try {
|
|
92
|
-
await this.loadFromAddonDir(addonDir);
|
|
93
|
-
} catch (err) {
|
|
94
|
-
console.warn(`Failed to load addon from ${addonDir}: ${err}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/** Load addon from a specific directory (package.json + dist/) */
|
|
98
|
-
async loadFromAddonDir(addonDir) {
|
|
99
|
-
const pkgJsonPath = path.join(addonDir, "package.json");
|
|
100
|
-
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
|
|
101
|
-
const packageName = pkgJson["name"];
|
|
102
|
-
const packageVersion = pkgJson["version"] ?? "0.0.0";
|
|
103
|
-
const manifest = pkgJson["camstack"];
|
|
104
|
-
if (!manifest?.addons?.length) return;
|
|
105
|
-
for (const declaration of manifest.addons) {
|
|
106
|
-
const entryFile = declaration.entry.replace(/^\.\//, "").replace(/^src\//, "dist/").replace(/\.ts$/, ".js");
|
|
107
|
-
let entryPath = path.resolve(addonDir, entryFile);
|
|
108
|
-
if (!fs.existsSync(entryPath)) {
|
|
109
|
-
const base = entryPath.replace(/\.(js|cjs|mjs)$/, "");
|
|
110
|
-
const alternatives = [
|
|
111
|
-
`${base}.cjs`,
|
|
112
|
-
`${base}.mjs`,
|
|
113
|
-
path.resolve(addonDir, "dist", "index.js"),
|
|
114
|
-
path.resolve(addonDir, "dist", "index.cjs"),
|
|
115
|
-
path.resolve(addonDir, "dist", "index.mjs"),
|
|
116
|
-
path.resolve(addonDir, declaration.entry)
|
|
117
|
-
];
|
|
118
|
-
entryPath = alternatives.find((p) => fs.existsSync(p)) ?? entryPath;
|
|
119
|
-
}
|
|
120
|
-
if (!fs.existsSync(entryPath)) {
|
|
121
|
-
throw new Error(`Entry not found: ${entryPath}`);
|
|
122
|
-
}
|
|
123
|
-
const mod = await import(entryPath);
|
|
124
|
-
const AddonClass = resolveAddonClass(mod);
|
|
125
|
-
if (!AddonClass) {
|
|
126
|
-
throw new Error(`No addon class in ${entryPath}`);
|
|
127
|
-
}
|
|
128
|
-
this.addons.set(declaration.id, {
|
|
129
|
-
declaration,
|
|
130
|
-
packageName,
|
|
131
|
-
packageVersion,
|
|
132
|
-
addonClass: AddonClass
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/** Load addon from a direct path (for development/testing) */
|
|
137
|
-
async loadFromPath(addonId, modulePath, packageName, declaration, packageVersion = "0.0.0") {
|
|
138
|
-
const mod = await import(modulePath);
|
|
139
|
-
const AddonClass = resolveAddonClass(mod);
|
|
140
|
-
if (!AddonClass) {
|
|
141
|
-
throw new Error(`Module ${modulePath} has no default export`);
|
|
142
|
-
}
|
|
143
|
-
this.addons.set(addonId, {
|
|
144
|
-
declaration: {
|
|
145
|
-
id: addonId,
|
|
146
|
-
entry: modulePath,
|
|
147
|
-
slot: "detector",
|
|
148
|
-
...declaration
|
|
149
|
-
},
|
|
150
|
-
packageName,
|
|
151
|
-
packageVersion,
|
|
152
|
-
addonClass: AddonClass
|
|
33
|
+
// src/addon-loader.js
|
|
34
|
+
var require_addon_loader = __commonJS({
|
|
35
|
+
"src/addon-loader.js"(exports2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
38
|
+
if (k2 === void 0) k2 = k;
|
|
39
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
40
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
41
|
+
desc = { enumerable: true, get: function() {
|
|
42
|
+
return m[k];
|
|
43
|
+
} };
|
|
44
|
+
}
|
|
45
|
+
Object.defineProperty(o, k2, desc);
|
|
46
|
+
}) : (function(o, m, k, k2) {
|
|
47
|
+
if (k2 === void 0) k2 = k;
|
|
48
|
+
o[k2] = m[k];
|
|
49
|
+
}));
|
|
50
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
51
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
52
|
+
}) : function(o, v) {
|
|
53
|
+
o["default"] = v;
|
|
153
54
|
});
|
|
55
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
56
|
+
var ownKeys = function(o) {
|
|
57
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
58
|
+
var ar = [];
|
|
59
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
60
|
+
return ar;
|
|
61
|
+
};
|
|
62
|
+
return ownKeys(o);
|
|
63
|
+
};
|
|
64
|
+
return function(mod) {
|
|
65
|
+
if (mod && mod.__esModule) return mod;
|
|
66
|
+
var result = {};
|
|
67
|
+
if (mod != null) {
|
|
68
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
69
|
+
}
|
|
70
|
+
__setModuleDefault(result, mod);
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
})();
|
|
74
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
75
|
+
exports2.AddonLoader = void 0;
|
|
76
|
+
var fs = __importStar(require("fs"));
|
|
77
|
+
var path = __importStar(require("path"));
|
|
78
|
+
function resolveAddonClass(mod) {
|
|
79
|
+
let candidate = mod["default"] ?? mod[Object.keys(mod)[0]];
|
|
80
|
+
if (candidate && typeof candidate === "object" && "default" in candidate) {
|
|
81
|
+
candidate = candidate["default"];
|
|
82
|
+
}
|
|
83
|
+
return typeof candidate === "function" ? candidate : void 0;
|
|
84
|
+
}
|
|
85
|
+
var AddonLoader2 = class {
|
|
86
|
+
addons = /* @__PURE__ */ new Map();
|
|
87
|
+
/** Scan addons directory and load all addon packages.
|
|
88
|
+
* Supports scoped layout: addons/@scope/package-name/ */
|
|
89
|
+
async loadFromDirectory(addonsDir) {
|
|
90
|
+
if (!fs.existsSync(addonsDir))
|
|
91
|
+
return;
|
|
92
|
+
const entries = fs.readdirSync(addonsDir, { withFileTypes: true });
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
if (!entry.isDirectory())
|
|
95
|
+
continue;
|
|
96
|
+
if (entry.name.startsWith("@")) {
|
|
97
|
+
const scopeDir = path.join(addonsDir, entry.name);
|
|
98
|
+
const scopeEntries = fs.readdirSync(scopeDir, { withFileTypes: true });
|
|
99
|
+
for (const scopeEntry of scopeEntries) {
|
|
100
|
+
if (!scopeEntry.isDirectory())
|
|
101
|
+
continue;
|
|
102
|
+
await this.tryLoadAddon(path.join(scopeDir, scopeEntry.name));
|
|
103
|
+
}
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
await this.tryLoadAddon(path.join(addonsDir, entry.name));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async tryLoadAddon(addonDir) {
|
|
110
|
+
const pkgJsonPath = path.join(addonDir, "package.json");
|
|
111
|
+
if (!fs.existsSync(pkgJsonPath))
|
|
112
|
+
return;
|
|
113
|
+
try {
|
|
114
|
+
await this.loadFromAddonDir(addonDir);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
console.warn(`Failed to load addon from ${addonDir}: ${err}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** Load addon from a specific directory (package.json + dist/) */
|
|
120
|
+
async loadFromAddonDir(addonDir) {
|
|
121
|
+
const pkgJsonPath = path.join(addonDir, "package.json");
|
|
122
|
+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
|
|
123
|
+
const packageName = pkgJson["name"];
|
|
124
|
+
const packageVersion = pkgJson["version"] ?? "0.0.0";
|
|
125
|
+
const manifest = pkgJson["camstack"];
|
|
126
|
+
if (!manifest?.addons?.length)
|
|
127
|
+
return;
|
|
128
|
+
for (const declaration of manifest.addons) {
|
|
129
|
+
const entryFile = declaration.entry.replace(/^\.\//, "").replace(/^src\//, "dist/").replace(/\.ts$/, ".js");
|
|
130
|
+
let entryPath = path.resolve(addonDir, entryFile);
|
|
131
|
+
if (!fs.existsSync(entryPath)) {
|
|
132
|
+
const base = entryPath.replace(/\.(js|cjs|mjs)$/, "");
|
|
133
|
+
const alternatives = [
|
|
134
|
+
`${base}.cjs`,
|
|
135
|
+
`${base}.mjs`,
|
|
136
|
+
path.resolve(addonDir, "dist", "index.js"),
|
|
137
|
+
path.resolve(addonDir, "dist", "index.cjs"),
|
|
138
|
+
path.resolve(addonDir, "dist", "index.mjs"),
|
|
139
|
+
path.resolve(addonDir, declaration.entry)
|
|
140
|
+
];
|
|
141
|
+
entryPath = alternatives.find((p) => fs.existsSync(p)) ?? entryPath;
|
|
142
|
+
}
|
|
143
|
+
if (!fs.existsSync(entryPath)) {
|
|
144
|
+
throw new Error(`Entry not found: ${entryPath}`);
|
|
145
|
+
}
|
|
146
|
+
const mod = await Promise.resolve(`${entryPath}`).then((s) => __importStar(require(s)));
|
|
147
|
+
const AddonClass = resolveAddonClass(mod);
|
|
148
|
+
if (!AddonClass) {
|
|
149
|
+
throw new Error(`No addon class in ${entryPath}`);
|
|
150
|
+
}
|
|
151
|
+
this.addons.set(declaration.id, {
|
|
152
|
+
declaration,
|
|
153
|
+
packageName,
|
|
154
|
+
packageVersion,
|
|
155
|
+
packageDisplayName: manifest.displayName,
|
|
156
|
+
addonClass: AddonClass
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Load addon from a direct path (for development/testing) */
|
|
161
|
+
async loadFromPath(addonId, modulePath, packageName, declaration, packageVersion = "0.0.0") {
|
|
162
|
+
const mod = await Promise.resolve(`${modulePath}`).then((s) => __importStar(require(s)));
|
|
163
|
+
const AddonClass = resolveAddonClass(mod);
|
|
164
|
+
if (!AddonClass) {
|
|
165
|
+
throw new Error(`Module ${modulePath} has no default export`);
|
|
166
|
+
}
|
|
167
|
+
this.addons.set(addonId, {
|
|
168
|
+
declaration: {
|
|
169
|
+
id: addonId,
|
|
170
|
+
entry: modulePath,
|
|
171
|
+
slot: "detector",
|
|
172
|
+
...declaration
|
|
173
|
+
},
|
|
174
|
+
packageName,
|
|
175
|
+
packageVersion,
|
|
176
|
+
addonClass: AddonClass
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/** Get a registered addon by ID */
|
|
180
|
+
getAddon(addonId) {
|
|
181
|
+
return this.addons.get(addonId);
|
|
182
|
+
}
|
|
183
|
+
/** List all registered addons */
|
|
184
|
+
listAddons() {
|
|
185
|
+
return [...this.addons.values()];
|
|
186
|
+
}
|
|
187
|
+
/** Check if an addon is registered */
|
|
188
|
+
hasAddon(addonId) {
|
|
189
|
+
return this.addons.has(addonId);
|
|
190
|
+
}
|
|
191
|
+
/** Create a new instance of an addon (not yet initialized) */
|
|
192
|
+
createInstance(addonId) {
|
|
193
|
+
const registered = this.addons.get(addonId);
|
|
194
|
+
if (!registered) {
|
|
195
|
+
throw new Error(`Addon "${addonId}" is not registered`);
|
|
196
|
+
}
|
|
197
|
+
return new registered.addonClass();
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
exports2.AddonLoader = AddonLoader2;
|
|
154
201
|
}
|
|
155
|
-
|
|
156
|
-
getAddon(addonId) {
|
|
157
|
-
return this.addons.get(addonId);
|
|
158
|
-
}
|
|
159
|
-
/** List all registered addons */
|
|
160
|
-
listAddons() {
|
|
161
|
-
return [...this.addons.values()];
|
|
162
|
-
}
|
|
163
|
-
/** Check if an addon is registered */
|
|
164
|
-
hasAddon(addonId) {
|
|
165
|
-
return this.addons.has(addonId);
|
|
166
|
-
}
|
|
167
|
-
/** Create a new instance of an addon (not yet initialized) */
|
|
168
|
-
createInstance(addonId) {
|
|
169
|
-
const registered = this.addons.get(addonId);
|
|
170
|
-
if (!registered) {
|
|
171
|
-
throw new Error(`Addon "${addonId}" is not registered`);
|
|
172
|
-
}
|
|
173
|
-
return new registered.addonClass();
|
|
174
|
-
}
|
|
175
|
-
};
|
|
202
|
+
});
|
|
176
203
|
|
|
177
|
-
// src/addon-engine-manager.
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
204
|
+
// src/addon-engine-manager.js
|
|
205
|
+
var require_addon_engine_manager = __commonJS({
|
|
206
|
+
"src/addon-engine-manager.js"(exports2) {
|
|
207
|
+
"use strict";
|
|
208
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
209
|
+
exports2.AddonEngineManager = void 0;
|
|
210
|
+
var node_crypto_1 = require("crypto");
|
|
211
|
+
var AddonEngineManager2 = class {
|
|
212
|
+
loader;
|
|
213
|
+
baseContext;
|
|
214
|
+
engines = /* @__PURE__ */ new Map();
|
|
215
|
+
constructor(loader, baseContext) {
|
|
216
|
+
this.loader = loader;
|
|
217
|
+
this.baseContext = baseContext;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Get or create an addon engine for the given effective config.
|
|
221
|
+
* Cameras with the same addonId + effective config share the same engine.
|
|
222
|
+
*/
|
|
223
|
+
async getOrCreateEngine(addonId, globalConfig, cameraOverride) {
|
|
224
|
+
const effectiveConfig = { ...globalConfig, ...cameraOverride };
|
|
225
|
+
const configKey = `${addonId}:${this.hashConfig(effectiveConfig)}`;
|
|
226
|
+
const existing = this.engines.get(configKey);
|
|
227
|
+
if (existing)
|
|
228
|
+
return existing;
|
|
229
|
+
const addon = this.loader.createInstance(addonId);
|
|
230
|
+
await addon.initialize({ ...this.baseContext, addonConfig: effectiveConfig });
|
|
231
|
+
this.engines.set(configKey, addon);
|
|
232
|
+
return addon;
|
|
233
|
+
}
|
|
234
|
+
/** Get all active engines */
|
|
235
|
+
getActiveEngines() {
|
|
236
|
+
return new Map(this.engines);
|
|
237
|
+
}
|
|
238
|
+
/** Shutdown a specific engine by its config key */
|
|
239
|
+
async shutdownEngine(configKey) {
|
|
240
|
+
const engine = this.engines.get(configKey);
|
|
241
|
+
if (engine) {
|
|
242
|
+
await engine.shutdown();
|
|
243
|
+
this.engines.delete(configKey);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** Shutdown all engines */
|
|
247
|
+
async shutdownAll() {
|
|
248
|
+
for (const [, engine] of this.engines) {
|
|
249
|
+
await engine.shutdown();
|
|
250
|
+
}
|
|
251
|
+
this.engines.clear();
|
|
252
|
+
}
|
|
253
|
+
/** Compute a deterministic config key (visible for tests) */
|
|
254
|
+
computeConfigKey(addonId, effectiveConfig) {
|
|
255
|
+
return `${addonId}:${this.hashConfig(effectiveConfig)}`;
|
|
256
|
+
}
|
|
257
|
+
hashConfig(config) {
|
|
258
|
+
const sorted = JSON.stringify(config, Object.keys(config).sort());
|
|
259
|
+
return (0, node_crypto_1.createHash)("md5").update(sorted).digest("hex").slice(0, 12);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
exports2.AddonEngineManager = AddonEngineManager2;
|
|
225
263
|
}
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
// src/addon-installer.ts
|
|
229
|
-
var import_node_child_process2 = require("child_process");
|
|
230
|
-
var import_node_util = require("util");
|
|
231
|
-
var fs3 = __toESM(require("fs"));
|
|
232
|
-
var path3 = __toESM(require("path"));
|
|
233
|
-
var os = __toESM(require("os"));
|
|
264
|
+
});
|
|
234
265
|
|
|
235
|
-
// src/fs-utils.
|
|
236
|
-
var
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const result = { ...pkg };
|
|
257
|
-
for (const depType of ["dependencies", "peerDependencies", "devDependencies"]) {
|
|
258
|
-
const deps = result[depType];
|
|
259
|
-
if (deps) {
|
|
260
|
-
const filtered = {};
|
|
261
|
-
for (const [name, version] of Object.entries(deps)) {
|
|
262
|
-
if (!name.startsWith("@camstack/")) {
|
|
263
|
-
filtered[name] = version;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
result[depType] = Object.keys(filtered).length > 0 ? filtered : void 0;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
delete result.devDependencies;
|
|
270
|
-
return result;
|
|
271
|
-
}
|
|
272
|
-
function copyExtraFileDirs(pkgJson, sourceDir, destDir) {
|
|
273
|
-
const files = pkgJson.files;
|
|
274
|
-
if (!files) return;
|
|
275
|
-
for (const fileEntry of files) {
|
|
276
|
-
if (fileEntry === "dist" || fileEntry.includes("*")) continue;
|
|
277
|
-
const srcPath = path2.join(sourceDir, fileEntry);
|
|
278
|
-
if (!fs2.existsSync(srcPath)) continue;
|
|
279
|
-
const destPath = path2.join(destDir, fileEntry);
|
|
280
|
-
const stat = fs2.statSync(srcPath);
|
|
281
|
-
if (stat.isDirectory()) {
|
|
282
|
-
copyDirRecursive(srcPath, destPath);
|
|
283
|
-
} else if (stat.isFile()) {
|
|
284
|
-
ensureDir(path2.dirname(destPath));
|
|
285
|
-
fs2.copyFileSync(srcPath, destPath);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
function symlinkAddonsToNodeModules(addonsDir, nodeModulesDir) {
|
|
290
|
-
const camstackDir = path2.join(nodeModulesDir, "@camstack");
|
|
291
|
-
ensureDir(camstackDir);
|
|
292
|
-
const packagesToLink = ["core"];
|
|
293
|
-
for (const pkg of packagesToLink) {
|
|
294
|
-
const addonDir = path2.join(addonsDir, "@camstack", pkg);
|
|
295
|
-
const linkPath = path2.join(camstackDir, pkg);
|
|
296
|
-
if (!fs2.existsSync(addonDir)) continue;
|
|
297
|
-
try {
|
|
298
|
-
const stat = fs2.lstatSync(linkPath);
|
|
299
|
-
if (stat.isSymbolicLink() || stat.isDirectory()) {
|
|
300
|
-
fs2.rmSync(linkPath, { recursive: true, force: true });
|
|
301
|
-
}
|
|
302
|
-
} catch {
|
|
303
|
-
}
|
|
304
|
-
fs2.symlinkSync(addonDir, linkPath, "dir");
|
|
305
|
-
console.log(`[symlink] node_modules/@camstack/${pkg} -> ${addonDir}`);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
function isSourceNewer(packageDir) {
|
|
309
|
-
const srcDir = path2.join(packageDir, "src");
|
|
310
|
-
const distDir = path2.join(packageDir, "dist");
|
|
311
|
-
if (!fs2.existsSync(srcDir) || !fs2.existsSync(distDir)) return true;
|
|
312
|
-
try {
|
|
313
|
-
const distMtime = fs2.statSync(distDir).mtimeMs;
|
|
314
|
-
const entries = fs2.readdirSync(srcDir, { withFileTypes: true, recursive: true });
|
|
315
|
-
for (const entry of entries) {
|
|
316
|
-
if (!entry.isFile()) continue;
|
|
317
|
-
const filePath = path2.join(entry.parentPath ?? entry.path, entry.name);
|
|
318
|
-
if (fs2.statSync(filePath).mtimeMs > distMtime) return true;
|
|
319
|
-
}
|
|
320
|
-
return false;
|
|
321
|
-
} catch {
|
|
322
|
-
return true;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
function ensureLibraryBuilt(packageName, packagesDir) {
|
|
326
|
-
const dirName = packageName.replace("@camstack/", "");
|
|
327
|
-
const sourceDir = path2.join(packagesDir, dirName);
|
|
328
|
-
if (!fs2.existsSync(sourceDir)) return;
|
|
329
|
-
const distDir = path2.join(sourceDir, "dist");
|
|
330
|
-
const hasIndex = fs2.existsSync(path2.join(distDir, "index.js")) || fs2.existsSync(path2.join(distDir, "index.mjs"));
|
|
331
|
-
if (hasIndex) return;
|
|
332
|
-
console.warn(`[ensureLibraryBuilt] ${packageName} has no dist/ \u2014 run 'npm run build' first`);
|
|
333
|
-
}
|
|
334
|
-
function installPackageFromNpmSync(packageName, targetDir) {
|
|
335
|
-
const os2 = require("os");
|
|
336
|
-
const tmpDir = fs2.mkdtempSync(path2.join(os2.tmpdir(), "camstack-install-"));
|
|
337
|
-
try {
|
|
338
|
-
const stdout = (0, import_node_child_process.execFileSync)("npm", ["pack", packageName, "--pack-destination", tmpDir], {
|
|
339
|
-
timeout: 12e4,
|
|
340
|
-
encoding: "utf-8"
|
|
266
|
+
// src/fs-utils.js
|
|
267
|
+
var require_fs_utils = __commonJS({
|
|
268
|
+
"src/fs-utils.js"(exports2) {
|
|
269
|
+
"use strict";
|
|
270
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
271
|
+
if (k2 === void 0) k2 = k;
|
|
272
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
273
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
274
|
+
desc = { enumerable: true, get: function() {
|
|
275
|
+
return m[k];
|
|
276
|
+
} };
|
|
277
|
+
}
|
|
278
|
+
Object.defineProperty(o, k2, desc);
|
|
279
|
+
}) : (function(o, m, k, k2) {
|
|
280
|
+
if (k2 === void 0) k2 = k;
|
|
281
|
+
o[k2] = m[k];
|
|
282
|
+
}));
|
|
283
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
284
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
285
|
+
}) : function(o, v) {
|
|
286
|
+
o["default"] = v;
|
|
341
287
|
});
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
try {
|
|
358
|
-
const npmPkg = JSON.parse(fs2.readFileSync(path2.join(srcPkgJsonDir, "package.json"), "utf-8"));
|
|
359
|
-
copyExtraFileDirs(npmPkg, srcPkgJsonDir, targetDir);
|
|
360
|
-
} catch {
|
|
361
|
-
}
|
|
362
|
-
} finally {
|
|
363
|
-
fs2.rmSync(tmpDir, { recursive: true, force: true });
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// src/addon-installer.ts
|
|
368
|
-
var execFileAsync = (0, import_node_util.promisify)(import_node_child_process2.execFile);
|
|
369
|
-
var AddonInstaller = class _AddonInstaller {
|
|
370
|
-
addonsDir;
|
|
371
|
-
registry;
|
|
372
|
-
workspacePackagesDir;
|
|
373
|
-
constructor(config) {
|
|
374
|
-
this.addonsDir = config.addonsDir;
|
|
375
|
-
this.registry = config.registry;
|
|
376
|
-
this.workspacePackagesDir = config.workspacePackagesDir;
|
|
377
|
-
}
|
|
378
|
-
/** Required addon packages that must be installed for the server to function */
|
|
379
|
-
static REQUIRED_PACKAGES = [
|
|
380
|
-
"@camstack/core",
|
|
381
|
-
"@camstack/addon-pipeline",
|
|
382
|
-
"@camstack/addon-vision",
|
|
383
|
-
"@camstack/addon-admin-ui",
|
|
384
|
-
"@camstack/addon-webrtc-adaptive",
|
|
385
|
-
"@camstack/addon-pipeline-analysis",
|
|
386
|
-
"@camstack/addon-scene-intelligence",
|
|
387
|
-
"@camstack/addon-advanced-notifier"
|
|
388
|
-
];
|
|
389
|
-
/** Ensure the addons directory exists */
|
|
390
|
-
async initialize() {
|
|
391
|
-
ensureDir(this.addonsDir);
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Ensure all required packages are installed in the addons directory.
|
|
395
|
-
* This replaces the standalone first-boot-installer.ts.
|
|
396
|
-
*/
|
|
397
|
-
async ensureRequiredPackages() {
|
|
398
|
-
ensureDir(this.addonsDir);
|
|
399
|
-
if (this.workspacePackagesDir) {
|
|
400
|
-
console.log(`[AddonInstaller] Workspace detected: ${this.workspacePackagesDir}`);
|
|
401
|
-
ensureLibraryBuilt("@camstack/kernel", this.workspacePackagesDir);
|
|
402
|
-
ensureLibraryBuilt("@camstack/types", this.workspacePackagesDir);
|
|
403
|
-
}
|
|
404
|
-
for (const packageName of _AddonInstaller.REQUIRED_PACKAGES) {
|
|
405
|
-
const addonDir = path3.join(this.addonsDir, packageName);
|
|
406
|
-
const pkgJsonPath = path3.join(addonDir, "package.json");
|
|
407
|
-
if (fs3.existsSync(pkgJsonPath)) {
|
|
408
|
-
if (!this.workspacePackagesDir) {
|
|
409
|
-
continue;
|
|
288
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
289
|
+
var ownKeys = function(o) {
|
|
290
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
291
|
+
var ar = [];
|
|
292
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
293
|
+
return ar;
|
|
294
|
+
};
|
|
295
|
+
return ownKeys(o);
|
|
296
|
+
};
|
|
297
|
+
return function(mod) {
|
|
298
|
+
if (mod && mod.__esModule) return mod;
|
|
299
|
+
var result = {};
|
|
300
|
+
if (mod != null) {
|
|
301
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
410
302
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
303
|
+
__setModuleDefault(result, mod);
|
|
304
|
+
return result;
|
|
305
|
+
};
|
|
306
|
+
})();
|
|
307
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
308
|
+
exports2.ensureDir = ensureDir2;
|
|
309
|
+
exports2.copyDirRecursive = copyDirRecursive2;
|
|
310
|
+
exports2.stripCamstackDeps = stripCamstackDeps2;
|
|
311
|
+
exports2.copyExtraFileDirs = copyExtraFileDirs2;
|
|
312
|
+
exports2.symlinkAddonsToNodeModules = symlinkAddonsToNodeModules2;
|
|
313
|
+
exports2.isSourceNewer = isSourceNewer2;
|
|
314
|
+
exports2.ensureLibraryBuilt = ensureLibraryBuilt2;
|
|
315
|
+
exports2.installPackageFromNpmSync = installPackageFromNpmSync2;
|
|
316
|
+
var node_child_process_1 = require("child_process");
|
|
317
|
+
var fs = __importStar(require("fs"));
|
|
318
|
+
var path = __importStar(require("path"));
|
|
319
|
+
function ensureDir2(dirPath) {
|
|
320
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
321
|
+
}
|
|
322
|
+
function copyDirRecursive2(src, dest) {
|
|
323
|
+
ensureDir2(dest);
|
|
324
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
325
|
+
for (const entry of entries) {
|
|
326
|
+
const srcPath = path.join(src, entry.name);
|
|
327
|
+
const destPath = path.join(dest, entry.name);
|
|
328
|
+
if (entry.isDirectory()) {
|
|
329
|
+
copyDirRecursive2(srcPath, destPath);
|
|
330
|
+
} else {
|
|
331
|
+
fs.copyFileSync(srcPath, destPath);
|
|
414
332
|
}
|
|
415
333
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
334
|
+
}
|
|
335
|
+
function stripCamstackDeps2(pkg) {
|
|
336
|
+
const result = { ...pkg };
|
|
337
|
+
for (const depType of ["dependencies", "peerDependencies", "devDependencies"]) {
|
|
338
|
+
const deps = result[depType];
|
|
339
|
+
if (deps) {
|
|
340
|
+
const filtered = {};
|
|
341
|
+
for (const [name, version] of Object.entries(deps)) {
|
|
342
|
+
if (!name.startsWith("@camstack/")) {
|
|
343
|
+
filtered[name] = version;
|
|
344
|
+
}
|
|
422
345
|
}
|
|
346
|
+
result[depType] = Object.keys(filtered).length > 0 ? filtered : void 0;
|
|
423
347
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
348
|
+
}
|
|
349
|
+
delete result.devDependencies;
|
|
350
|
+
return result;
|
|
351
|
+
}
|
|
352
|
+
function copyExtraFileDirs2(pkgJson, sourceDir, destDir) {
|
|
353
|
+
const files = pkgJson.files;
|
|
354
|
+
if (!files)
|
|
355
|
+
return;
|
|
356
|
+
for (const fileEntry of files) {
|
|
357
|
+
if (fileEntry === "dist" || fileEntry.includes("*"))
|
|
358
|
+
continue;
|
|
359
|
+
const srcPath = path.join(sourceDir, fileEntry);
|
|
360
|
+
if (!fs.existsSync(srcPath))
|
|
361
|
+
continue;
|
|
362
|
+
const destPath = path.join(destDir, fileEntry);
|
|
363
|
+
const stat = fs.statSync(srcPath);
|
|
364
|
+
if (stat.isDirectory()) {
|
|
365
|
+
copyDirRecursive2(srcPath, destPath);
|
|
366
|
+
} else if (stat.isFile()) {
|
|
367
|
+
ensureDir2(path.dirname(destPath));
|
|
368
|
+
fs.copyFileSync(srcPath, destPath);
|
|
429
369
|
}
|
|
430
|
-
console.error(`[AddonInstaller] Failed to install ${packageName}: ${msg}`);
|
|
431
370
|
}
|
|
432
371
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
372
|
+
function symlinkAddonsToNodeModules2(addonsDir, nodeModulesDir) {
|
|
373
|
+
const camstackDir = path.join(nodeModulesDir, "@camstack");
|
|
374
|
+
ensureDir2(camstackDir);
|
|
375
|
+
const packagesToLink = ["core"];
|
|
376
|
+
for (const pkg of packagesToLink) {
|
|
377
|
+
const addonDir = path.join(addonsDir, "@camstack", pkg);
|
|
378
|
+
const linkPath = path.join(camstackDir, pkg);
|
|
379
|
+
if (!fs.existsSync(addonDir))
|
|
380
|
+
continue;
|
|
440
381
|
try {
|
|
441
|
-
const
|
|
442
|
-
if (
|
|
382
|
+
const stat = fs.lstatSync(linkPath);
|
|
383
|
+
if (stat.isSymbolicLink() || stat.isDirectory()) {
|
|
384
|
+
fs.rmSync(linkPath, { recursive: true, force: true });
|
|
385
|
+
}
|
|
443
386
|
} catch {
|
|
444
387
|
}
|
|
388
|
+
fs.symlinkSync(addonDir, linkPath, "dir");
|
|
389
|
+
console.log(`[symlink] node_modules/@camstack/${pkg} -> ${addonDir}`);
|
|
445
390
|
}
|
|
446
391
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
try {
|
|
453
|
-
await execFileAsync("tar", ["-xzf", tgzPath, "-C", tmpDir], { timeout: 3e4 });
|
|
454
|
-
const extractedDir = path3.join(tmpDir, "package");
|
|
455
|
-
const pkgJsonPath = fs3.existsSync(path3.join(extractedDir, "package.json")) ? path3.join(extractedDir, "package.json") : path3.join(tmpDir, "package.json");
|
|
456
|
-
if (!fs3.existsSync(pkgJsonPath)) {
|
|
457
|
-
throw new Error("No package.json found in tgz archive");
|
|
458
|
-
}
|
|
459
|
-
const pkgJson = JSON.parse(fs3.readFileSync(pkgJsonPath, "utf-8"));
|
|
460
|
-
if (!pkgJson.camstack?.addons) {
|
|
461
|
-
throw new Error(`Package ${pkgJson.name} has no camstack.addons manifest`);
|
|
462
|
-
}
|
|
463
|
-
const targetDir = path3.join(this.addonsDir, pkgJson.name);
|
|
464
|
-
fs3.rmSync(targetDir, { recursive: true, force: true });
|
|
465
|
-
ensureDir(targetDir);
|
|
466
|
-
const sourceDir = path3.dirname(pkgJsonPath);
|
|
467
|
-
fs3.copyFileSync(pkgJsonPath, path3.join(targetDir, "package.json"));
|
|
468
|
-
const sourceDist = path3.join(sourceDir, "dist");
|
|
469
|
-
if (fs3.existsSync(sourceDist)) {
|
|
470
|
-
copyDirRecursive(sourceDist, path3.join(targetDir, "dist"));
|
|
471
|
-
}
|
|
472
|
-
copyExtraFileDirs(pkgJson, sourceDir, targetDir);
|
|
473
|
-
return { name: pkgJson.name, version: pkgJson.version };
|
|
474
|
-
} finally {
|
|
475
|
-
fs3.rmSync(tmpDir, { recursive: true, force: true });
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Install addon — prefers workspace if available, falls back to npm.
|
|
480
|
-
* This ensures dev builds (with vite output, etc.) are used when in workspace mode.
|
|
481
|
-
*/
|
|
482
|
-
async install(packageName, version) {
|
|
483
|
-
if (this.workspacePackagesDir) {
|
|
484
|
-
const workspaceDirName = packageName.replace("@camstack/", "");
|
|
485
|
-
const sourceDir = path3.join(this.workspacePackagesDir, workspaceDirName);
|
|
486
|
-
if (fs3.existsSync(path3.join(sourceDir, "package.json"))) {
|
|
487
|
-
return this.installFromWorkspace(packageName);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
return this.installFromNpm(packageName, version);
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Install addon from workspace by copying package.json + dist/ to addons dir.
|
|
494
|
-
* Does NOT build — expects dist/ to already exist (run `npm run build` separately).
|
|
495
|
-
*/
|
|
496
|
-
async installFromWorkspace(packageName) {
|
|
497
|
-
if (!this.workspacePackagesDir) {
|
|
498
|
-
throw new Error("Workspace packages directory not configured");
|
|
499
|
-
}
|
|
500
|
-
const workspaceDirName = packageName.replace("@camstack/", "");
|
|
501
|
-
const sourceDir = path3.join(this.workspacePackagesDir, workspaceDirName);
|
|
502
|
-
const sourcePkgJson = path3.join(sourceDir, "package.json");
|
|
503
|
-
if (!fs3.existsSync(sourcePkgJson)) {
|
|
504
|
-
throw new Error(`Workspace package not found: ${sourceDir}`);
|
|
505
|
-
}
|
|
506
|
-
const distDir = path3.join(sourceDir, "dist");
|
|
507
|
-
if (!fs3.existsSync(distDir)) {
|
|
508
|
-
throw new Error(`${packageName} has no dist/ directory \u2014 run 'npm run build' first`);
|
|
509
|
-
}
|
|
510
|
-
const targetDir = path3.join(this.addonsDir, packageName);
|
|
511
|
-
fs3.rmSync(targetDir, { recursive: true, force: true });
|
|
512
|
-
ensureDir(targetDir);
|
|
513
|
-
const pkgData = JSON.parse(fs3.readFileSync(sourcePkgJson, "utf-8"));
|
|
514
|
-
const strippedPkg = stripCamstackDeps(pkgData);
|
|
515
|
-
fs3.writeFileSync(path3.join(targetDir, "package.json"), JSON.stringify(strippedPkg, null, 2));
|
|
516
|
-
copyDirRecursive(distDir, path3.join(targetDir, "dist"));
|
|
517
|
-
copyExtraFileDirs(pkgData, sourceDir, targetDir);
|
|
518
|
-
try {
|
|
519
|
-
await execFileAsync("npm", ["install", "--omit=dev", "--ignore-scripts=false"], {
|
|
520
|
-
cwd: targetDir,
|
|
521
|
-
timeout: 12e4
|
|
522
|
-
});
|
|
523
|
-
} catch {
|
|
524
|
-
}
|
|
525
|
-
return { name: pkgData.name, version: pkgData.version };
|
|
526
|
-
}
|
|
527
|
-
/** Install addon from npm (download tgz, then extract) */
|
|
528
|
-
async installFromNpm(packageName, version) {
|
|
529
|
-
const tmpDir = fs3.mkdtempSync(path3.join(os.tmpdir(), "camstack-addon-npm-"));
|
|
530
|
-
const packageSpec = version ? `${packageName}@${version}` : packageName;
|
|
531
|
-
const args = ["pack", packageSpec, "--pack-destination", tmpDir];
|
|
532
|
-
if (this.registry) {
|
|
533
|
-
args.push("--registry", this.registry);
|
|
534
|
-
}
|
|
535
|
-
try {
|
|
536
|
-
const { stdout } = await execFileAsync("npm", args, {
|
|
537
|
-
timeout: 12e4
|
|
538
|
-
});
|
|
539
|
-
const tgzFiles = fs3.readdirSync(tmpDir).filter((f) => f.endsWith(".tgz"));
|
|
540
|
-
if (tgzFiles.length === 0) {
|
|
541
|
-
throw new Error(`npm pack produced no tgz file for ${packageSpec}. stdout: ${stdout.trim()}`);
|
|
542
|
-
}
|
|
543
|
-
const tgzPath = path3.join(tmpDir, tgzFiles[0]);
|
|
544
|
-
const result = await this.installFromTgz(tgzPath);
|
|
545
|
-
return result;
|
|
546
|
-
} finally {
|
|
547
|
-
fs3.rmSync(tmpDir, { recursive: true, force: true });
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
/** Uninstall addon (delete directory) */
|
|
551
|
-
async uninstall(packageName) {
|
|
552
|
-
const addonDir = path3.join(this.addonsDir, packageName);
|
|
553
|
-
if (fs3.existsSync(addonDir)) {
|
|
554
|
-
fs3.rmSync(addonDir, { recursive: true, force: true });
|
|
555
|
-
return;
|
|
556
|
-
}
|
|
557
|
-
const legacyDir = path3.join(this.addonsDir, packageName.replace(/^@[^/]+\//, ""));
|
|
558
|
-
if (fs3.existsSync(legacyDir)) {
|
|
559
|
-
fs3.rmSync(legacyDir, { recursive: true, force: true });
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
/** List installed addons (directories with package.json containing camstack.addons) */
|
|
563
|
-
listInstalled() {
|
|
564
|
-
if (!fs3.existsSync(this.addonsDir)) return [];
|
|
565
|
-
return fs3.readdirSync(this.addonsDir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => {
|
|
566
|
-
const pkgPath = path3.join(this.addonsDir, d.name, "package.json");
|
|
567
|
-
if (!fs3.existsSync(pkgPath)) return null;
|
|
392
|
+
function isSourceNewer2(packageDir) {
|
|
393
|
+
const srcDir = path.join(packageDir, "src");
|
|
394
|
+
const distDir = path.join(packageDir, "dist");
|
|
395
|
+
if (!fs.existsSync(srcDir) || !fs.existsSync(distDir))
|
|
396
|
+
return true;
|
|
568
397
|
try {
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
|
|
398
|
+
const distMtime = fs.statSync(distDir).mtimeMs;
|
|
399
|
+
const entries = fs.readdirSync(srcDir, { withFileTypes: true, recursive: true });
|
|
400
|
+
for (const entry of entries) {
|
|
401
|
+
if (!entry.isFile())
|
|
402
|
+
continue;
|
|
403
|
+
const filePath = path.join(entry.parentPath ?? entry.path, entry.name);
|
|
404
|
+
if (fs.statSync(filePath).mtimeMs > distMtime)
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
return false;
|
|
572
408
|
} catch {
|
|
573
|
-
return
|
|
409
|
+
return true;
|
|
574
410
|
}
|
|
575
|
-
}).filter((item) => item !== null);
|
|
576
|
-
}
|
|
577
|
-
/** Check if an addon is installed */
|
|
578
|
-
isInstalled(packageName) {
|
|
579
|
-
if (fs3.existsSync(path3.join(this.addonsDir, packageName, "package.json"))) return true;
|
|
580
|
-
const legacy = packageName.replace(/^@[^/]+\//, "");
|
|
581
|
-
return fs3.existsSync(path3.join(this.addonsDir, legacy, "package.json"));
|
|
582
|
-
}
|
|
583
|
-
/** Get installed package info */
|
|
584
|
-
getInstalledPackage(packageName) {
|
|
585
|
-
let pkgPath = path3.join(this.addonsDir, packageName, "package.json");
|
|
586
|
-
if (!fs3.existsSync(pkgPath)) {
|
|
587
|
-
pkgPath = path3.join(this.addonsDir, packageName.replace(/^@[^/]+\//, ""), "package.json");
|
|
588
|
-
}
|
|
589
|
-
if (!fs3.existsSync(pkgPath)) return null;
|
|
590
|
-
try {
|
|
591
|
-
const pkg = JSON.parse(fs3.readFileSync(pkgPath, "utf-8"));
|
|
592
|
-
return { name: pkg.name, version: pkg.version, dir: path3.dirname(pkgPath) };
|
|
593
|
-
} catch {
|
|
594
|
-
return null;
|
|
595
411
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
412
|
+
function ensureLibraryBuilt2(packageName, packagesDir) {
|
|
413
|
+
const dirName = packageName.replace("@camstack/", "");
|
|
414
|
+
const sourceDir = path.join(packagesDir, dirName);
|
|
415
|
+
if (!fs.existsSync(sourceDir))
|
|
416
|
+
return;
|
|
417
|
+
const distDir = path.join(sourceDir, "dist");
|
|
418
|
+
const hasIndex = fs.existsSync(path.join(distDir, "index.js")) || fs.existsSync(path.join(distDir, "index.mjs"));
|
|
419
|
+
if (hasIndex)
|
|
420
|
+
return;
|
|
421
|
+
console.warn(`[ensureLibraryBuilt] ${packageName} has no dist/ \u2014 run 'npm run build' first`);
|
|
422
|
+
}
|
|
423
|
+
function installPackageFromNpmSync2(packageName, targetDir) {
|
|
424
|
+
const os = require("os");
|
|
425
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "camstack-install-"));
|
|
609
426
|
try {
|
|
610
|
-
const
|
|
611
|
-
|
|
612
|
-
|
|
427
|
+
const stdout = (0, node_child_process_1.execFileSync)("npm", ["pack", packageName, "--pack-destination", tmpDir], {
|
|
428
|
+
timeout: 12e4,
|
|
429
|
+
encoding: "utf-8"
|
|
430
|
+
});
|
|
431
|
+
const tgzFilename = stdout.trim().split("\n").pop()?.trim();
|
|
432
|
+
if (!tgzFilename)
|
|
433
|
+
throw new Error("npm pack produced no output");
|
|
434
|
+
const tgzPath = path.join(tmpDir, tgzFilename);
|
|
435
|
+
const extractDir = path.join(tmpDir, "extracted");
|
|
436
|
+
ensureDir2(extractDir);
|
|
437
|
+
(0, node_child_process_1.execFileSync)("tar", ["-xzf", tgzPath, "-C", extractDir], { timeout: 3e4 });
|
|
438
|
+
const packageSubDir = path.join(extractDir, "package");
|
|
439
|
+
const srcPkgJsonDir = fs.existsSync(path.join(packageSubDir, "package.json")) ? packageSubDir : extractDir;
|
|
440
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
441
|
+
ensureDir2(targetDir);
|
|
442
|
+
fs.copyFileSync(path.join(srcPkgJsonDir, "package.json"), path.join(targetDir, "package.json"));
|
|
443
|
+
const distSrc = path.join(srcPkgJsonDir, "dist");
|
|
444
|
+
if (fs.existsSync(distSrc)) {
|
|
445
|
+
copyDirRecursive2(distSrc, path.join(targetDir, "dist"));
|
|
613
446
|
}
|
|
614
|
-
|
|
447
|
+
try {
|
|
448
|
+
const npmPkg = JSON.parse(fs.readFileSync(path.join(srcPkgJsonDir, "package.json"), "utf-8"));
|
|
449
|
+
copyExtraFileDirs2(npmPkg, srcPkgJsonDir, targetDir);
|
|
450
|
+
} catch {
|
|
451
|
+
}
|
|
452
|
+
} finally {
|
|
453
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
615
454
|
}
|
|
616
455
|
}
|
|
617
456
|
}
|
|
618
|
-
|
|
619
|
-
}
|
|
457
|
+
});
|
|
620
458
|
|
|
621
|
-
// src/
|
|
622
|
-
var
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
declaration,
|
|
643
|
-
available: /* @__PURE__ */ new Map(),
|
|
644
|
-
activeAddonId: null,
|
|
645
|
-
activeProvider: null,
|
|
646
|
-
activeCollection: [],
|
|
647
|
-
consumers: /* @__PURE__ */ new Set()
|
|
459
|
+
// src/addon-installer.js
|
|
460
|
+
var require_addon_installer = __commonJS({
|
|
461
|
+
"src/addon-installer.js"(exports2) {
|
|
462
|
+
"use strict";
|
|
463
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
464
|
+
if (k2 === void 0) k2 = k;
|
|
465
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
466
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
467
|
+
desc = { enumerable: true, get: function() {
|
|
468
|
+
return m[k];
|
|
469
|
+
} };
|
|
470
|
+
}
|
|
471
|
+
Object.defineProperty(o, k2, desc);
|
|
472
|
+
}) : (function(o, m, k, k2) {
|
|
473
|
+
if (k2 === void 0) k2 = k;
|
|
474
|
+
o[k2] = m[k];
|
|
475
|
+
}));
|
|
476
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
477
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
478
|
+
}) : function(o, v) {
|
|
479
|
+
o["default"] = v;
|
|
648
480
|
});
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
481
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
482
|
+
var ownKeys = function(o) {
|
|
483
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
484
|
+
var ar = [];
|
|
485
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
486
|
+
return ar;
|
|
487
|
+
};
|
|
488
|
+
return ownKeys(o);
|
|
489
|
+
};
|
|
490
|
+
return function(mod) {
|
|
491
|
+
if (mod && mod.__esModule) return mod;
|
|
492
|
+
var result = {};
|
|
493
|
+
if (mod != null) {
|
|
494
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
495
|
+
}
|
|
496
|
+
__setModuleDefault(result, mod);
|
|
497
|
+
return result;
|
|
498
|
+
};
|
|
499
|
+
})();
|
|
500
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
501
|
+
exports2.AddonInstaller = void 0;
|
|
502
|
+
var node_child_process_1 = require("child_process");
|
|
503
|
+
var node_util_1 = require("util");
|
|
504
|
+
var fs = __importStar(require("fs"));
|
|
505
|
+
var path = __importStar(require("path"));
|
|
506
|
+
var os = __importStar(require("os"));
|
|
507
|
+
var fs_utils_js_1 = require_fs_utils();
|
|
508
|
+
var execFileAsync = (0, node_util_1.promisify)(node_child_process_1.execFile);
|
|
509
|
+
var AddonInstaller2 = class _AddonInstaller {
|
|
510
|
+
addonsDir;
|
|
511
|
+
registry;
|
|
512
|
+
workspacePackagesDir;
|
|
513
|
+
constructor(config) {
|
|
514
|
+
this.addonsDir = config.addonsDir;
|
|
515
|
+
this.registry = config.registry;
|
|
516
|
+
this.workspacePackagesDir = config.workspacePackagesDir;
|
|
517
|
+
}
|
|
518
|
+
/** Required addon packages that must be installed for the server to function */
|
|
519
|
+
static REQUIRED_PACKAGES = [
|
|
520
|
+
"@camstack/core",
|
|
521
|
+
"@camstack/addon-pipeline",
|
|
522
|
+
"@camstack/addon-vision",
|
|
523
|
+
"@camstack/addon-admin-ui",
|
|
524
|
+
"@camstack/addon-webrtc-adaptive",
|
|
525
|
+
"@camstack/addon-pipeline-analysis",
|
|
526
|
+
"@camstack/addon-scene-intelligence",
|
|
527
|
+
"@camstack/addon-advanced-notifier"
|
|
528
|
+
];
|
|
529
|
+
/** Ensure the addons directory exists */
|
|
530
|
+
async initialize() {
|
|
531
|
+
(0, fs_utils_js_1.ensureDir)(this.addonsDir);
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Ensure all required packages are installed in the addons directory.
|
|
535
|
+
* This replaces the standalone first-boot-installer.ts.
|
|
536
|
+
*/
|
|
537
|
+
async ensureRequiredPackages() {
|
|
538
|
+
(0, fs_utils_js_1.ensureDir)(this.addonsDir);
|
|
539
|
+
if (this.workspacePackagesDir) {
|
|
540
|
+
console.log(`[AddonInstaller] Workspace detected: ${this.workspacePackagesDir}`);
|
|
541
|
+
(0, fs_utils_js_1.ensureLibraryBuilt)("@camstack/kernel", this.workspacePackagesDir);
|
|
542
|
+
(0, fs_utils_js_1.ensureLibraryBuilt)("@camstack/types", this.workspacePackagesDir);
|
|
543
|
+
}
|
|
544
|
+
for (const packageName of _AddonInstaller.REQUIRED_PACKAGES) {
|
|
545
|
+
const addonDir = path.join(this.addonsDir, packageName);
|
|
546
|
+
const pkgJsonPath = path.join(addonDir, "package.json");
|
|
547
|
+
const forceNpm = process.env["CAMSTACK_INSTALL_SOURCE"] === "npm";
|
|
548
|
+
const useWorkspace = this.workspacePackagesDir && !forceNpm;
|
|
549
|
+
if (fs.existsSync(pkgJsonPath)) {
|
|
550
|
+
if (!useWorkspace) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const srcPkgDir = this.findWorkspacePackage(packageName);
|
|
554
|
+
if (srcPkgDir && !(0, fs_utils_js_1.isSourceNewer)(srcPkgDir)) {
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
675
558
|
try {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
559
|
+
if (useWorkspace) {
|
|
560
|
+
const pkgDir = this.findWorkspacePackage(packageName);
|
|
561
|
+
if (pkgDir) {
|
|
562
|
+
await this.installFromWorkspace(packageName);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
await this.installFromNpm(packageName);
|
|
567
|
+
} catch (err) {
|
|
568
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
569
|
+
if (packageName === "@camstack/core") {
|
|
570
|
+
throw new Error(`Required package ${packageName} failed to install: ${msg}`);
|
|
571
|
+
}
|
|
572
|
+
console.error(`[AddonInstaller] Failed to install ${packageName}: ${msg}`);
|
|
680
573
|
}
|
|
681
574
|
}
|
|
682
575
|
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
if (!state) return;
|
|
691
|
-
const provider = state.available.get(addonId);
|
|
692
|
-
state.available.delete(addonId);
|
|
693
|
-
if (state.declaration.mode === "singleton") {
|
|
694
|
-
if (state.activeAddonId === addonId) {
|
|
695
|
-
state.activeAddonId = null;
|
|
696
|
-
state.activeProvider = null;
|
|
697
|
-
this.logger.info(`Singleton deactivated: ${capability} (was ${addonId})`);
|
|
698
|
-
}
|
|
699
|
-
} else {
|
|
700
|
-
const idx = state.activeCollection.findIndex((e) => e.addonId === addonId);
|
|
701
|
-
if (idx !== -1) {
|
|
702
|
-
state.activeCollection.splice(idx, 1);
|
|
703
|
-
for (const consumer of state.consumers) {
|
|
704
|
-
if (consumer.onRemoved && provider !== void 0) {
|
|
576
|
+
findWorkspacePackage(packageName) {
|
|
577
|
+
if (!this.workspacePackagesDir)
|
|
578
|
+
return null;
|
|
579
|
+
const shortName = packageName.replace("@camstack/", "");
|
|
580
|
+
for (const dirName of [shortName, `addon-${shortName}`, shortName.replace("addon-", "")]) {
|
|
581
|
+
const candidate = path.join(this.workspacePackagesDir, dirName);
|
|
582
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) {
|
|
705
583
|
try {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
584
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(candidate, "package.json"), "utf-8"));
|
|
585
|
+
if (pkg.name === packageName)
|
|
586
|
+
return candidate;
|
|
587
|
+
} catch {
|
|
710
588
|
}
|
|
711
589
|
}
|
|
712
590
|
}
|
|
591
|
+
return null;
|
|
713
592
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
* Register a consumer that wants to be notified when providers change.
|
|
718
|
-
* If a provider is already active, the consumer is immediately notified.
|
|
719
|
-
* Returns a disposer function for cleanup.
|
|
720
|
-
*/
|
|
721
|
-
registerConsumer(registration) {
|
|
722
|
-
const state = this.capabilities.get(registration.capability);
|
|
723
|
-
if (!state) {
|
|
724
|
-
this.logger.debug(`Consumer registered for undeclared capability "${registration.capability}" \u2014 auto-declaring`);
|
|
725
|
-
this.declareCapability({ name: registration.capability, mode: "singleton" });
|
|
726
|
-
return this.registerConsumer(registration);
|
|
727
|
-
}
|
|
728
|
-
const untypedReg = registration;
|
|
729
|
-
state.consumers.add(untypedReg);
|
|
730
|
-
if (state.declaration.mode === "singleton") {
|
|
731
|
-
if (state.activeProvider !== null && registration.onSet) {
|
|
593
|
+
/** Install addon from a tgz file (uploaded or downloaded) */
|
|
594
|
+
async installFromTgz(tgzPath) {
|
|
595
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "camstack-addon-install-"));
|
|
732
596
|
try {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
const
|
|
736
|
-
|
|
597
|
+
await execFileAsync("tar", ["-xzf", tgzPath, "-C", tmpDir], { timeout: 3e4 });
|
|
598
|
+
const extractedDir = path.join(tmpDir, "package");
|
|
599
|
+
const pkgJsonPath = fs.existsSync(path.join(extractedDir, "package.json")) ? path.join(extractedDir, "package.json") : path.join(tmpDir, "package.json");
|
|
600
|
+
if (!fs.existsSync(pkgJsonPath)) {
|
|
601
|
+
throw new Error("No package.json found in tgz archive");
|
|
602
|
+
}
|
|
603
|
+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
|
|
604
|
+
if (!pkgJson.camstack?.addons) {
|
|
605
|
+
throw new Error(`Package ${pkgJson.name} has no camstack.addons manifest`);
|
|
606
|
+
}
|
|
607
|
+
const targetDir = path.join(this.addonsDir, pkgJson.name);
|
|
608
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
609
|
+
(0, fs_utils_js_1.ensureDir)(targetDir);
|
|
610
|
+
const sourceDir = path.dirname(pkgJsonPath);
|
|
611
|
+
fs.copyFileSync(pkgJsonPath, path.join(targetDir, "package.json"));
|
|
612
|
+
const sourceDist = path.join(sourceDir, "dist");
|
|
613
|
+
if (fs.existsSync(sourceDist)) {
|
|
614
|
+
(0, fs_utils_js_1.copyDirRecursive)(sourceDist, path.join(targetDir, "dist"));
|
|
615
|
+
}
|
|
616
|
+
(0, fs_utils_js_1.copyExtraFileDirs)(pkgJson, sourceDir, targetDir);
|
|
617
|
+
return { name: pkgJson.name, version: pkgJson.version };
|
|
618
|
+
} finally {
|
|
619
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
737
620
|
}
|
|
738
621
|
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
622
|
+
/**
|
|
623
|
+
* Install addon — prefers workspace if available, falls back to npm.
|
|
624
|
+
* This ensures dev builds (with vite output, etc.) are used when in workspace mode.
|
|
625
|
+
*/
|
|
626
|
+
async install(packageName, version) {
|
|
627
|
+
if (this.workspacePackagesDir) {
|
|
628
|
+
const workspaceDirName = packageName.replace("@camstack/", "");
|
|
629
|
+
const sourceDir = path.join(this.workspacePackagesDir, workspaceDirName);
|
|
630
|
+
if (fs.existsSync(path.join(sourceDir, "package.json"))) {
|
|
631
|
+
return this.installFromWorkspace(packageName);
|
|
747
632
|
}
|
|
748
633
|
}
|
|
634
|
+
return this.installFromNpm(packageName, version);
|
|
749
635
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
}
|
|
785
|
-
const provider = state.available.get(addonId);
|
|
786
|
-
if (!provider) {
|
|
787
|
-
throw new Error(`No provider "${addonId}" registered for capability "${capability}"`);
|
|
788
|
-
}
|
|
789
|
-
if (immediate) {
|
|
790
|
-
await this.activateSingletonAsync(state, addonId, provider);
|
|
791
|
-
}
|
|
792
|
-
this.logger.info(`Singleton preference set: ${capability} \u2192 ${addonId}`);
|
|
793
|
-
}
|
|
794
|
-
/**
|
|
795
|
-
* Get the mode declared for a capability.
|
|
796
|
-
*/
|
|
797
|
-
getMode(capability) {
|
|
798
|
-
return this.capabilities.get(capability)?.declaration.mode;
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* List all registered capabilities with their providers.
|
|
802
|
-
*/
|
|
803
|
-
listCapabilities() {
|
|
804
|
-
const result = [];
|
|
805
|
-
for (const [name, state] of this.capabilities) {
|
|
806
|
-
result.push({
|
|
807
|
-
name,
|
|
808
|
-
mode: state.declaration.mode,
|
|
809
|
-
providers: [...state.available.keys()],
|
|
810
|
-
activeProvider: state.activeAddonId
|
|
811
|
-
});
|
|
812
|
-
}
|
|
813
|
-
return result;
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* Check if all dependencies for a capability are satisfied (have active providers).
|
|
817
|
-
*/
|
|
818
|
-
areDependenciesMet(declaration) {
|
|
819
|
-
if (!declaration.dependsOn?.length) return true;
|
|
820
|
-
return declaration.dependsOn.every((dep) => {
|
|
821
|
-
const state = this.capabilities.get(dep);
|
|
822
|
-
if (!state) return false;
|
|
823
|
-
if (state.declaration.mode === "singleton") {
|
|
824
|
-
return state.activeProvider !== null;
|
|
825
|
-
}
|
|
826
|
-
return state.activeCollection.length > 0;
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
/**
|
|
830
|
-
* Get the dependency-ordered list of capability names for boot sequencing.
|
|
831
|
-
* Returns capabilities sorted topologically by dependsOn.
|
|
832
|
-
* Throws if a cycle is detected.
|
|
833
|
-
*/
|
|
834
|
-
getBootOrder() {
|
|
835
|
-
const visited = /* @__PURE__ */ new Set();
|
|
836
|
-
const visiting = /* @__PURE__ */ new Set();
|
|
837
|
-
const order = [];
|
|
838
|
-
const visit = (name) => {
|
|
839
|
-
if (visited.has(name)) return;
|
|
840
|
-
if (visiting.has(name)) {
|
|
841
|
-
throw new Error(`Circular dependency detected involving capability "${name}"`);
|
|
842
|
-
}
|
|
843
|
-
visiting.add(name);
|
|
844
|
-
const state = this.capabilities.get(name);
|
|
845
|
-
if (state?.declaration.dependsOn) {
|
|
846
|
-
for (const dep of state.declaration.dependsOn) {
|
|
847
|
-
visit(dep);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
visiting.delete(name);
|
|
851
|
-
visited.add(name);
|
|
852
|
-
order.push(name);
|
|
853
|
-
};
|
|
854
|
-
for (const name of this.capabilities.keys()) {
|
|
855
|
-
visit(name);
|
|
856
|
-
}
|
|
857
|
-
return order;
|
|
858
|
-
}
|
|
859
|
-
// ---- Per-device overrides ----
|
|
860
|
-
/**
|
|
861
|
-
* Set a per-device singleton override. When resolveForDevice is called for
|
|
862
|
-
* this device + capability, the specified addon's provider is returned
|
|
863
|
-
* instead of the global singleton.
|
|
864
|
-
*/
|
|
865
|
-
setDeviceOverride(deviceId, capability, addonId) {
|
|
866
|
-
const state = this.capabilities.get(capability);
|
|
867
|
-
if (!state) {
|
|
868
|
-
this.logger.warn(`Cannot set device override for undeclared capability "${capability}"`);
|
|
869
|
-
return;
|
|
870
|
-
}
|
|
871
|
-
if (!state.available.has(addonId)) {
|
|
872
|
-
this.logger.warn(`Cannot set device override: addon "${addonId}" not registered for "${capability}"`);
|
|
873
|
-
return;
|
|
874
|
-
}
|
|
875
|
-
let deviceMap = this.deviceOverrides.get(deviceId);
|
|
876
|
-
if (!deviceMap) {
|
|
877
|
-
deviceMap = /* @__PURE__ */ new Map();
|
|
878
|
-
this.deviceOverrides.set(deviceId, deviceMap);
|
|
879
|
-
}
|
|
880
|
-
deviceMap.set(capability, addonId);
|
|
881
|
-
this.logger.info(`Device override set: ${deviceId} \u2192 ${capability} = ${addonId}`);
|
|
882
|
-
}
|
|
883
|
-
/**
|
|
884
|
-
* Clear a per-device singleton override, reverting to the global singleton.
|
|
885
|
-
*/
|
|
886
|
-
clearDeviceOverride(deviceId, capability) {
|
|
887
|
-
const deviceMap = this.deviceOverrides.get(deviceId);
|
|
888
|
-
if (!deviceMap) return;
|
|
889
|
-
deviceMap.delete(capability);
|
|
890
|
-
if (deviceMap.size === 0) {
|
|
891
|
-
this.deviceOverrides.delete(deviceId);
|
|
892
|
-
}
|
|
893
|
-
this.logger.info(`Device override cleared: ${deviceId} \u2192 ${capability}`);
|
|
894
|
-
}
|
|
895
|
-
/**
|
|
896
|
-
* Get all per-device singleton overrides for a device.
|
|
897
|
-
* Returns a Map of capability name to addon ID.
|
|
898
|
-
*/
|
|
899
|
-
getDeviceOverrides(deviceId) {
|
|
900
|
-
return new Map(this.deviceOverrides.get(deviceId) ?? []);
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Resolve a singleton provider for a specific device.
|
|
904
|
-
* 1. Check device override — return that addon's provider
|
|
905
|
-
* 2. Fallback to global singleton
|
|
906
|
-
*/
|
|
907
|
-
resolveForDevice(capability, deviceId) {
|
|
908
|
-
const state = this.capabilities.get(capability);
|
|
909
|
-
if (!state || state.declaration.mode !== "singleton") return null;
|
|
910
|
-
const deviceMap = this.deviceOverrides.get(deviceId);
|
|
911
|
-
if (deviceMap) {
|
|
912
|
-
const overrideAddonId = deviceMap.get(capability);
|
|
913
|
-
if (overrideAddonId) {
|
|
914
|
-
const provider = state.available.get(overrideAddonId);
|
|
915
|
-
if (provider) return provider;
|
|
916
|
-
this.logger.warn(
|
|
917
|
-
`Device override for ${deviceId}/${capability} references unregistered addon "${overrideAddonId}" \u2014 falling back to global`
|
|
918
|
-
);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
return state.activeProvider ?? null;
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Set a per-device collection filter. When resolveCollectionForDevice is called
|
|
925
|
-
* for this device + capability, only providers from the specified addon IDs
|
|
926
|
-
* are returned instead of the full collection.
|
|
927
|
-
*/
|
|
928
|
-
setDeviceCollectionFilter(deviceId, capability, addonIds) {
|
|
929
|
-
const state = this.capabilities.get(capability);
|
|
930
|
-
if (!state) {
|
|
931
|
-
this.logger.warn(`Cannot set device collection filter for undeclared capability "${capability}"`);
|
|
932
|
-
return;
|
|
933
|
-
}
|
|
934
|
-
let deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
935
|
-
if (!deviceMap) {
|
|
936
|
-
deviceMap = /* @__PURE__ */ new Map();
|
|
937
|
-
this.deviceCollectionFilters.set(deviceId, deviceMap);
|
|
938
|
-
}
|
|
939
|
-
deviceMap.set(capability, [...addonIds]);
|
|
940
|
-
this.logger.info(`Device collection filter set: ${deviceId} \u2192 ${capability} = [${addonIds.join(", ")}]`);
|
|
941
|
-
}
|
|
942
|
-
/**
|
|
943
|
-
* Clear a per-device collection filter, reverting to the full collection.
|
|
944
|
-
*/
|
|
945
|
-
clearDeviceCollectionFilter(deviceId, capability) {
|
|
946
|
-
const deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
947
|
-
if (!deviceMap) return;
|
|
948
|
-
deviceMap.delete(capability);
|
|
949
|
-
if (deviceMap.size === 0) {
|
|
950
|
-
this.deviceCollectionFilters.delete(deviceId);
|
|
951
|
-
}
|
|
952
|
-
this.logger.info(`Device collection filter cleared: ${deviceId} \u2192 ${capability}`);
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Resolve collection providers for a specific device.
|
|
956
|
-
* If a filter exists for the device + capability, only those addon's providers are returned.
|
|
957
|
-
* If no filter exists, the full collection is returned.
|
|
958
|
-
*/
|
|
959
|
-
resolveCollectionForDevice(capability, deviceId) {
|
|
960
|
-
const state = this.capabilities.get(capability);
|
|
961
|
-
if (!state || state.declaration.mode !== "collection") return [];
|
|
962
|
-
const deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
963
|
-
if (deviceMap) {
|
|
964
|
-
const filterAddonIds = deviceMap.get(capability);
|
|
965
|
-
if (filterAddonIds) {
|
|
966
|
-
const filterSet = new Set(filterAddonIds);
|
|
967
|
-
return state.activeCollection.filter((e) => filterSet.has(e.addonId)).map((e) => e.provider);
|
|
636
|
+
/**
|
|
637
|
+
* Install addon from workspace by copying package.json + dist/ to addons dir.
|
|
638
|
+
* Does NOT build — expects dist/ to already exist (run `npm run build` separately).
|
|
639
|
+
*/
|
|
640
|
+
async installFromWorkspace(packageName) {
|
|
641
|
+
if (!this.workspacePackagesDir) {
|
|
642
|
+
throw new Error("Workspace packages directory not configured");
|
|
643
|
+
}
|
|
644
|
+
const workspaceDirName = packageName.replace("@camstack/", "");
|
|
645
|
+
const sourceDir = path.join(this.workspacePackagesDir, workspaceDirName);
|
|
646
|
+
const sourcePkgJson = path.join(sourceDir, "package.json");
|
|
647
|
+
if (!fs.existsSync(sourcePkgJson)) {
|
|
648
|
+
throw new Error(`Workspace package not found: ${sourceDir}`);
|
|
649
|
+
}
|
|
650
|
+
const distDir = path.join(sourceDir, "dist");
|
|
651
|
+
if (!fs.existsSync(distDir)) {
|
|
652
|
+
throw new Error(`${packageName} has no dist/ directory \u2014 run 'npm run build' first`);
|
|
653
|
+
}
|
|
654
|
+
const targetDir = path.join(this.addonsDir, packageName);
|
|
655
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
656
|
+
(0, fs_utils_js_1.ensureDir)(targetDir);
|
|
657
|
+
const pkgData = JSON.parse(fs.readFileSync(sourcePkgJson, "utf-8"));
|
|
658
|
+
const strippedPkg = (0, fs_utils_js_1.stripCamstackDeps)(pkgData);
|
|
659
|
+
fs.writeFileSync(path.join(targetDir, "package.json"), JSON.stringify(strippedPkg, null, 2));
|
|
660
|
+
(0, fs_utils_js_1.copyDirRecursive)(distDir, path.join(targetDir, "dist"));
|
|
661
|
+
(0, fs_utils_js_1.copyExtraFileDirs)(pkgData, sourceDir, targetDir);
|
|
662
|
+
try {
|
|
663
|
+
await execFileAsync("npm", ["install", "--omit=dev", "--ignore-scripts=false"], {
|
|
664
|
+
cwd: targetDir,
|
|
665
|
+
timeout: 12e4
|
|
666
|
+
});
|
|
667
|
+
} catch {
|
|
668
|
+
}
|
|
669
|
+
return { name: pkgData.name, version: pkgData.version };
|
|
968
670
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
const state = this.capabilities.get(capability);
|
|
978
|
-
if (!state) return null;
|
|
979
|
-
const provider = state.available.get(addonId);
|
|
980
|
-
return provider ?? null;
|
|
981
|
-
}
|
|
982
|
-
activateSingleton(state, addonId, provider) {
|
|
983
|
-
state.activeAddonId = addonId;
|
|
984
|
-
state.activeProvider = provider;
|
|
985
|
-
this.logger.info(`Singleton activated: ${state.declaration.name} \u2192 ${addonId}`);
|
|
986
|
-
for (const consumer of state.consumers) {
|
|
987
|
-
if (consumer.onSet) {
|
|
671
|
+
/** Install addon from npm (download tgz, then extract) */
|
|
672
|
+
async installFromNpm(packageName, version) {
|
|
673
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "camstack-addon-npm-"));
|
|
674
|
+
const packageSpec = version ? `${packageName}@${version}` : packageName;
|
|
675
|
+
const args = ["pack", packageSpec, "--pack-destination", tmpDir];
|
|
676
|
+
if (this.registry) {
|
|
677
|
+
args.push("--registry", this.registry);
|
|
678
|
+
}
|
|
988
679
|
try {
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
680
|
+
const { stdout } = await execFileAsync("npm", args, {
|
|
681
|
+
timeout: 12e4
|
|
682
|
+
});
|
|
683
|
+
const tgzFiles = fs.readdirSync(tmpDir).filter((f) => f.endsWith(".tgz"));
|
|
684
|
+
if (tgzFiles.length === 0) {
|
|
685
|
+
throw new Error(`npm pack produced no tgz file for ${packageSpec}. stdout: ${stdout.trim()}`);
|
|
686
|
+
}
|
|
687
|
+
const tgzPath = path.join(tmpDir, tgzFiles[0]);
|
|
688
|
+
const result = await this.installFromTgz(tgzPath);
|
|
689
|
+
return result;
|
|
690
|
+
} finally {
|
|
691
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
993
692
|
}
|
|
994
693
|
}
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
694
|
+
/** Uninstall addon (delete directory) */
|
|
695
|
+
async uninstall(packageName) {
|
|
696
|
+
const addonDir = path.join(this.addonsDir, packageName);
|
|
697
|
+
if (fs.existsSync(addonDir)) {
|
|
698
|
+
fs.rmSync(addonDir, { recursive: true, force: true });
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
const legacyDir = path.join(this.addonsDir, packageName.replace(/^@[^/]+\//, ""));
|
|
702
|
+
if (fs.existsSync(legacyDir)) {
|
|
703
|
+
fs.rmSync(legacyDir, { recursive: true, force: true });
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/** List installed addons (directories with package.json containing camstack.addons) */
|
|
707
|
+
listInstalled() {
|
|
708
|
+
if (!fs.existsSync(this.addonsDir))
|
|
709
|
+
return [];
|
|
710
|
+
return fs.readdirSync(this.addonsDir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => {
|
|
711
|
+
const pkgPath = path.join(this.addonsDir, d.name, "package.json");
|
|
712
|
+
if (!fs.existsSync(pkgPath))
|
|
713
|
+
return null;
|
|
714
|
+
try {
|
|
715
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
716
|
+
if (!pkg.camstack?.addons)
|
|
717
|
+
return null;
|
|
718
|
+
return { name: pkg.name, version: pkg.version, dir: path.join(this.addonsDir, d.name) };
|
|
719
|
+
} catch {
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
}).filter((item) => item !== null);
|
|
723
|
+
}
|
|
724
|
+
/** Check if an addon is installed */
|
|
725
|
+
isInstalled(packageName) {
|
|
726
|
+
if (fs.existsSync(path.join(this.addonsDir, packageName, "package.json")))
|
|
727
|
+
return true;
|
|
728
|
+
const legacy = packageName.replace(/^@[^/]+\//, "");
|
|
729
|
+
return fs.existsSync(path.join(this.addonsDir, legacy, "package.json"));
|
|
730
|
+
}
|
|
731
|
+
/** Get installed package info */
|
|
732
|
+
getInstalledPackage(packageName) {
|
|
733
|
+
let pkgPath = path.join(this.addonsDir, packageName, "package.json");
|
|
734
|
+
if (!fs.existsSync(pkgPath)) {
|
|
735
|
+
pkgPath = path.join(this.addonsDir, packageName.replace(/^@[^/]+\//, ""), "package.json");
|
|
736
|
+
}
|
|
737
|
+
if (!fs.existsSync(pkgPath))
|
|
738
|
+
return null;
|
|
1003
739
|
try {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
740
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
741
|
+
return { name: pkg.name, version: pkg.version, dir: path.dirname(pkgPath) };
|
|
742
|
+
} catch {
|
|
743
|
+
return null;
|
|
1008
744
|
}
|
|
1009
745
|
}
|
|
1010
|
-
}
|
|
746
|
+
};
|
|
747
|
+
exports2.AddonInstaller = AddonInstaller2;
|
|
1011
748
|
}
|
|
1012
|
-
};
|
|
1013
|
-
|
|
1014
|
-
// src/infra-capabilities.ts
|
|
1015
|
-
var INFRA_CAPABILITIES = [
|
|
1016
|
-
{ name: "storage", required: true },
|
|
1017
|
-
{ name: "settings-store", required: true },
|
|
1018
|
-
{ name: "log-destination", required: false }
|
|
1019
|
-
];
|
|
1020
|
-
var infraNames = new Set(INFRA_CAPABILITIES.map((c) => c.name));
|
|
1021
|
-
function isInfraCapability(name) {
|
|
1022
|
-
return infraNames.has(name);
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
// src/config-manager.ts
|
|
1026
|
-
var fs5 = __toESM(require("fs"));
|
|
1027
|
-
var yaml = __toESM(require("js-yaml"));
|
|
1028
|
-
|
|
1029
|
-
// src/config-schema.ts
|
|
1030
|
-
var import_zod = require("zod");
|
|
1031
|
-
var DEFAULT_DATA_PATH = "camstack-data";
|
|
1032
|
-
var bootstrapSchema = import_zod.z.object({
|
|
1033
|
-
/** Server mode: 'hub' (full server) or 'agent' (worker node) */
|
|
1034
|
-
mode: import_zod.z.enum(["hub", "agent"]).default("hub"),
|
|
1035
|
-
server: import_zod.z.object({
|
|
1036
|
-
port: import_zod.z.number().default(4443),
|
|
1037
|
-
host: import_zod.z.string().default("0.0.0.0"),
|
|
1038
|
-
dataPath: import_zod.z.string().default(DEFAULT_DATA_PATH)
|
|
1039
|
-
}).default({}),
|
|
1040
|
-
auth: import_zod.z.object({
|
|
1041
|
-
jwtSecret: import_zod.z.string().nullable().default(null),
|
|
1042
|
-
adminUsername: import_zod.z.string().default("admin"),
|
|
1043
|
-
adminPassword: import_zod.z.string().default(process.env.ADMIN_PASSWORD ?? "changeme")
|
|
1044
|
-
}).default({}),
|
|
1045
|
-
/** Hub connection config — only used when mode='agent' */
|
|
1046
|
-
hub: import_zod.z.object({
|
|
1047
|
-
url: import_zod.z.string().default("ws://localhost:4443/agent"),
|
|
1048
|
-
token: import_zod.z.string().default("")
|
|
1049
|
-
}).default({}),
|
|
1050
|
-
/** Agent-specific config — only used when mode='agent' */
|
|
1051
|
-
agent: import_zod.z.object({
|
|
1052
|
-
name: import_zod.z.string().default(""),
|
|
1053
|
-
/** Port for the agent status page (minimal HTML) */
|
|
1054
|
-
statusPort: import_zod.z.number().default(4444)
|
|
1055
|
-
}).default({}),
|
|
1056
|
-
/** TLS configuration */
|
|
1057
|
-
tls: import_zod.z.object({
|
|
1058
|
-
/** Enable HTTPS (default: true) */
|
|
1059
|
-
enabled: import_zod.z.boolean().default(true),
|
|
1060
|
-
/** Path to custom cert file (PEM). If not set, auto-generates self-signed. */
|
|
1061
|
-
certPath: import_zod.z.string().optional(),
|
|
1062
|
-
/** Path to custom key file (PEM). Required if certPath is set. */
|
|
1063
|
-
keyPath: import_zod.z.string().optional()
|
|
1064
|
-
}).default({})
|
|
1065
749
|
});
|
|
1066
|
-
var RUNTIME_DEFAULTS = {
|
|
1067
|
-
"features.streaming": true,
|
|
1068
|
-
"features.notifications": true,
|
|
1069
|
-
"features.objectDetection": false,
|
|
1070
|
-
"features.remoteAccess": true,
|
|
1071
|
-
"features.agentCluster": false,
|
|
1072
|
-
"features.smartHome": true,
|
|
1073
|
-
"features.recordings": true,
|
|
1074
|
-
"features.backup": true,
|
|
1075
|
-
"features.repl": true,
|
|
1076
|
-
"retention.detectionEventsDays": 30,
|
|
1077
|
-
"retention.audioLevelsDays": 7,
|
|
1078
|
-
"logging.level": "info",
|
|
1079
|
-
"logging.retentionDays": 30,
|
|
1080
|
-
"eventBus.ringBufferSize": 1e4,
|
|
1081
|
-
"storage.provider": "sqlite-storage",
|
|
1082
|
-
"storage.locations": {
|
|
1083
|
-
data: "camstack-data/data",
|
|
1084
|
-
media: "camstack-data/media",
|
|
1085
|
-
recordings: "camstack-data/recordings",
|
|
1086
|
-
cache: "/tmp/camstack-cache",
|
|
1087
|
-
logs: "camstack-data/logs",
|
|
1088
|
-
models: "camstack-data/models"
|
|
1089
|
-
},
|
|
1090
|
-
"providers": [],
|
|
1091
|
-
// Recording
|
|
1092
|
-
"recording.segmentDurationSeconds": 4,
|
|
1093
|
-
"recording.defaultRetentionDays": 30,
|
|
1094
|
-
// Streaming ports are addon-specific (go2rtc owns its defaults)
|
|
1095
|
-
// FFmpeg
|
|
1096
|
-
"ffmpeg.binaryPath": "ffmpeg",
|
|
1097
|
-
"ffmpeg.hwAccel": "auto",
|
|
1098
|
-
"ffmpeg.threadCount": 0,
|
|
1099
|
-
// Detection defaults
|
|
1100
|
-
"detection.defaultMotionFps": 2,
|
|
1101
|
-
"detection.defaultDetectionFps": 5,
|
|
1102
|
-
"detection.defaultCooldownSeconds": 10,
|
|
1103
|
-
"detection.defaultConfidenceThreshold": 0.4,
|
|
1104
|
-
"detection.trackerMaxAgeFrames": 30,
|
|
1105
|
-
"detection.trackerMinHits": 3,
|
|
1106
|
-
"detection.trackerIouThreshold": 0.3,
|
|
1107
|
-
// Backup retention is addon-specific (local-backup owns its default)
|
|
1108
|
-
// Auth (runtime)
|
|
1109
|
-
"auth.tokenExpiry": "24h"
|
|
1110
|
-
};
|
|
1111
750
|
|
|
1112
|
-
// src/
|
|
1113
|
-
var
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
this.configPath = configPath;
|
|
1124
|
-
const rawYaml = this.loadYaml();
|
|
1125
|
-
const merged = this.applyEnvOverrides(rawYaml);
|
|
1126
|
-
this.bootstrapConfig = bootstrapSchema.parse(merged);
|
|
1127
|
-
this.warnDefaultCredentials();
|
|
1128
|
-
}
|
|
1129
|
-
// Non-readonly so update() can sync the in-memory view after a write.
|
|
1130
|
-
bootstrapConfig;
|
|
1131
|
-
settingsStore = null;
|
|
1132
|
-
/** Called by main.ts after the SQLite DB is ready (Phase 2). */
|
|
1133
|
-
setSettingsStore(store) {
|
|
1134
|
-
this.settingsStore = store;
|
|
1135
|
-
}
|
|
1136
|
-
/**
|
|
1137
|
-
* Get a config value by dot-notation path.
|
|
1138
|
-
* Priority: bootstrap config -> SQL system_settings -> RUNTIME_DEFAULTS fallback.
|
|
1139
|
-
*/
|
|
1140
|
-
get(path5) {
|
|
1141
|
-
const bootstrapValue = this.getFromBootstrap(path5);
|
|
1142
|
-
if (bootstrapValue !== void 0) {
|
|
1143
|
-
return bootstrapValue;
|
|
1144
|
-
}
|
|
1145
|
-
if (this.settingsStore !== null) {
|
|
1146
|
-
const sqlValue = this.settingsStore.getSystem(path5);
|
|
1147
|
-
if (sqlValue !== void 0) {
|
|
1148
|
-
return sqlValue;
|
|
751
|
+
// src/workspace-detect.js
|
|
752
|
+
var require_workspace_detect = __commonJS({
|
|
753
|
+
"src/workspace-detect.js"(exports2) {
|
|
754
|
+
"use strict";
|
|
755
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
756
|
+
if (k2 === void 0) k2 = k;
|
|
757
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
758
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
759
|
+
desc = { enumerable: true, get: function() {
|
|
760
|
+
return m[k];
|
|
761
|
+
} };
|
|
1149
762
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
763
|
+
Object.defineProperty(o, k2, desc);
|
|
764
|
+
}) : (function(o, m, k, k2) {
|
|
765
|
+
if (k2 === void 0) k2 = k;
|
|
766
|
+
o[k2] = m[k];
|
|
767
|
+
}));
|
|
768
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
769
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
770
|
+
}) : function(o, v) {
|
|
771
|
+
o["default"] = v;
|
|
772
|
+
});
|
|
773
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
774
|
+
var ownKeys = function(o) {
|
|
775
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
776
|
+
var ar = [];
|
|
777
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
778
|
+
return ar;
|
|
779
|
+
};
|
|
780
|
+
return ownKeys(o);
|
|
781
|
+
};
|
|
782
|
+
return function(mod) {
|
|
783
|
+
if (mod && mod.__esModule) return mod;
|
|
784
|
+
var result = {};
|
|
785
|
+
if (mod != null) {
|
|
786
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
787
|
+
}
|
|
788
|
+
__setModuleDefault(result, mod);
|
|
789
|
+
return result;
|
|
790
|
+
};
|
|
791
|
+
})();
|
|
792
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
793
|
+
exports2.detectWorkspacePackagesDir = detectWorkspacePackagesDir2;
|
|
794
|
+
var fs = __importStar(require("fs"));
|
|
795
|
+
var path = __importStar(require("path"));
|
|
796
|
+
function detectWorkspacePackagesDir2(startDir) {
|
|
797
|
+
let current = path.resolve(startDir);
|
|
798
|
+
for (let i = 0; i < 6; i++) {
|
|
799
|
+
current = path.dirname(current);
|
|
800
|
+
const packagesDir = path.join(current, "packages");
|
|
801
|
+
const rootPkgJson = path.join(current, "package.json");
|
|
802
|
+
if (fs.existsSync(packagesDir) && fs.existsSync(rootPkgJson)) {
|
|
803
|
+
try {
|
|
804
|
+
const rootPkg = JSON.parse(fs.readFileSync(rootPkgJson, "utf-8"));
|
|
805
|
+
if (rootPkg.workspaces || rootPkg.name === "camstack-server" || rootPkg.name === "camstack") {
|
|
806
|
+
return packagesDir;
|
|
807
|
+
}
|
|
808
|
+
} catch {
|
|
809
|
+
}
|
|
810
|
+
}
|
|
1153
811
|
}
|
|
1154
|
-
|
|
1155
|
-
if (path5 in RUNTIME_DEFAULTS) {
|
|
1156
|
-
return RUNTIME_DEFAULTS[path5];
|
|
1157
|
-
}
|
|
1158
|
-
const nested = this.getFromRuntimeDefaults(path5);
|
|
1159
|
-
if (nested !== void 0) {
|
|
1160
|
-
return nested;
|
|
1161
|
-
}
|
|
1162
|
-
return void 0;
|
|
1163
|
-
}
|
|
1164
|
-
/**
|
|
1165
|
-
* Write a value to SQL system_settings.
|
|
1166
|
-
* Throws if the settings store is not yet wired.
|
|
1167
|
-
*/
|
|
1168
|
-
set(key, value) {
|
|
1169
|
-
if (this.settingsStore === null) {
|
|
1170
|
-
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1171
|
-
}
|
|
1172
|
-
this.settingsStore.setSystem(key, value);
|
|
1173
|
-
}
|
|
1174
|
-
/**
|
|
1175
|
-
* Bulk-read all system_settings keys that belong to a logical section.
|
|
1176
|
-
* A "section" is the first segment of a dot-notation key (e.g. 'features', 'logging').
|
|
1177
|
-
*/
|
|
1178
|
-
getSection(section) {
|
|
1179
|
-
if (this.settingsStore !== null) {
|
|
1180
|
-
const nested = this.getNestedFromSystemSettings(section);
|
|
1181
|
-
if (nested !== void 0) return nested;
|
|
1182
|
-
}
|
|
1183
|
-
const bootstrapValue = this.bootstrapConfig[section];
|
|
1184
|
-
if (bootstrapValue !== void 0 && bootstrapValue !== null && typeof bootstrapValue === "object") {
|
|
1185
|
-
return bootstrapValue;
|
|
1186
|
-
}
|
|
1187
|
-
return this.getFromRuntimeDefaults(section) ?? {};
|
|
1188
|
-
}
|
|
1189
|
-
/**
|
|
1190
|
-
* Bulk-write a section of runtime settings to SQL system_settings.
|
|
1191
|
-
* Each entry in `data` is stored as `section.key`.
|
|
1192
|
-
*/
|
|
1193
|
-
setSection(section, data) {
|
|
1194
|
-
if (this.settingsStore === null) {
|
|
1195
|
-
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1196
|
-
}
|
|
1197
|
-
for (const [key, value] of Object.entries(data)) {
|
|
1198
|
-
this.settingsStore.setSystem(`${section}.${key}`, value);
|
|
812
|
+
return null;
|
|
1199
813
|
}
|
|
1200
814
|
}
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
// src/capability-registry.js
|
|
818
|
+
var require_capability_registry = __commonJS({
|
|
819
|
+
"src/capability-registry.js"(exports2) {
|
|
820
|
+
"use strict";
|
|
821
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
822
|
+
exports2.CapabilityRegistry = void 0;
|
|
823
|
+
var CapabilityRegistry2 = class {
|
|
824
|
+
logger;
|
|
825
|
+
configReader;
|
|
826
|
+
capabilities = /* @__PURE__ */ new Map();
|
|
827
|
+
/** Per-device singleton overrides: deviceId → (capability → addonId) */
|
|
828
|
+
deviceOverrides = /* @__PURE__ */ new Map();
|
|
829
|
+
/** Per-device collection filters: deviceId → (capability → addonIds[]) */
|
|
830
|
+
deviceCollectionFilters = /* @__PURE__ */ new Map();
|
|
831
|
+
constructor(logger, configReader) {
|
|
832
|
+
this.logger = logger;
|
|
833
|
+
this.configReader = configReader;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Declare a capability (typically called when addon manifests are loaded).
|
|
837
|
+
* Must be called before registerProvider/registerConsumer for that capability.
|
|
838
|
+
*/
|
|
839
|
+
declareCapability(declaration) {
|
|
840
|
+
if (this.capabilities.has(declaration.name)) {
|
|
841
|
+
this.logger.debug(`Capability "${declaration.name}" already declared, skipping`);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
this.capabilities.set(declaration.name, {
|
|
845
|
+
declaration,
|
|
846
|
+
available: /* @__PURE__ */ new Map(),
|
|
847
|
+
activeAddonId: null,
|
|
848
|
+
activeProvider: null,
|
|
849
|
+
activeCollection: [],
|
|
850
|
+
consumers: /* @__PURE__ */ new Set()
|
|
851
|
+
});
|
|
852
|
+
this.logger.debug(`Capability declared: ${declaration.name} (mode=${declaration.mode})`);
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Register a capability provider (called by addon loader when addon is enabled).
|
|
856
|
+
* For singleton: auto-activates if user-preferred or first registered.
|
|
857
|
+
* For collection: adds to active set and notifies consumers.
|
|
858
|
+
*/
|
|
859
|
+
registerProvider(capability, addonId, provider) {
|
|
860
|
+
const state = this.capabilities.get(capability);
|
|
861
|
+
if (!state) {
|
|
862
|
+
this.logger.warn(`Cannot register provider for undeclared capability "${capability}"`);
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
state.available.set(addonId, provider);
|
|
866
|
+
this.logger.info(`Provider registered: ${addonId} \u2192 ${capability}`);
|
|
867
|
+
if (state.declaration.mode === "singleton") {
|
|
868
|
+
const userChoice = this.configReader(capability);
|
|
869
|
+
if (userChoice === addonId) {
|
|
870
|
+
this.activateSingleton(state, addonId, provider);
|
|
871
|
+
} else if (userChoice === void 0 && state.activeAddonId === null) {
|
|
872
|
+
this.activateSingleton(state, addonId, provider);
|
|
873
|
+
}
|
|
874
|
+
} else {
|
|
875
|
+
state.activeCollection.push({ addonId, provider });
|
|
876
|
+
for (const consumer of state.consumers) {
|
|
877
|
+
if (consumer.onAdded) {
|
|
878
|
+
try {
|
|
879
|
+
consumer.onAdded(provider);
|
|
880
|
+
} catch (error) {
|
|
881
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
882
|
+
this.logger.error(`Consumer onAdded failed for ${capability}: ${msg}`);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Unregister a provider (called when addon is disabled/uninstalled).
|
|
890
|
+
*/
|
|
891
|
+
unregisterProvider(capability, addonId) {
|
|
892
|
+
const state = this.capabilities.get(capability);
|
|
893
|
+
if (!state)
|
|
894
|
+
return;
|
|
895
|
+
const provider = state.available.get(addonId);
|
|
896
|
+
state.available.delete(addonId);
|
|
897
|
+
if (state.declaration.mode === "singleton") {
|
|
898
|
+
if (state.activeAddonId === addonId) {
|
|
899
|
+
state.activeAddonId = null;
|
|
900
|
+
state.activeProvider = null;
|
|
901
|
+
this.logger.info(`Singleton deactivated: ${capability} (was ${addonId})`);
|
|
902
|
+
}
|
|
903
|
+
} else {
|
|
904
|
+
const idx = state.activeCollection.findIndex((e) => e.addonId === addonId);
|
|
905
|
+
if (idx !== -1) {
|
|
906
|
+
state.activeCollection.splice(idx, 1);
|
|
907
|
+
for (const consumer of state.consumers) {
|
|
908
|
+
if (consumer.onRemoved && provider !== void 0) {
|
|
909
|
+
try {
|
|
910
|
+
consumer.onRemoved(provider);
|
|
911
|
+
} catch (error) {
|
|
912
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
913
|
+
this.logger.error(`Consumer onRemoved failed for ${capability}: ${msg}`);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Register a consumer that wants to be notified when providers change.
|
|
922
|
+
* If a provider is already active, the consumer is immediately notified.
|
|
923
|
+
* Returns a disposer function for cleanup.
|
|
924
|
+
*/
|
|
925
|
+
registerConsumer(registration) {
|
|
926
|
+
const state = this.capabilities.get(registration.capability);
|
|
927
|
+
if (!state) {
|
|
928
|
+
this.logger.debug(`Consumer registered for undeclared capability "${registration.capability}" \u2014 auto-declaring`);
|
|
929
|
+
this.declareCapability({ name: registration.capability, mode: "singleton" });
|
|
930
|
+
return this.registerConsumer(registration);
|
|
931
|
+
}
|
|
932
|
+
const untypedReg = registration;
|
|
933
|
+
state.consumers.add(untypedReg);
|
|
934
|
+
if (state.declaration.mode === "singleton") {
|
|
935
|
+
if (state.activeProvider !== null && registration.onSet) {
|
|
936
|
+
try {
|
|
937
|
+
registration.onSet(state.activeProvider);
|
|
938
|
+
} catch (error) {
|
|
939
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
940
|
+
this.logger.error(`Consumer onSet (immediate) failed for ${registration.capability}: ${msg}`);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
} else {
|
|
944
|
+
if (registration.onAdded) {
|
|
945
|
+
for (const entry of state.activeCollection) {
|
|
946
|
+
try {
|
|
947
|
+
registration.onAdded(entry.provider);
|
|
948
|
+
} catch (error) {
|
|
949
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
950
|
+
this.logger.error(`Consumer onAdded (immediate) failed for ${registration.capability}: ${msg}`);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
return () => {
|
|
956
|
+
state.consumers.delete(untypedReg);
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Get the active singleton provider for a capability.
|
|
961
|
+
* Returns null if none set.
|
|
962
|
+
*/
|
|
963
|
+
getSingleton(capability) {
|
|
964
|
+
const state = this.capabilities.get(capability);
|
|
965
|
+
if (!state || state.declaration.mode !== "singleton")
|
|
966
|
+
return null;
|
|
967
|
+
return state.activeProvider ?? null;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Get all active collection providers for a capability.
|
|
971
|
+
*/
|
|
972
|
+
getCollection(capability) {
|
|
973
|
+
const state = this.capabilities.get(capability);
|
|
974
|
+
if (!state || state.declaration.mode !== "collection")
|
|
975
|
+
return [];
|
|
976
|
+
return state.activeCollection.map((e) => e.provider);
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Set which addon should be the active singleton for a capability.
|
|
980
|
+
* Call with `immediate: true` to also swap the runtime provider now
|
|
981
|
+
* (consumers' onSet will be awaited).
|
|
982
|
+
*/
|
|
983
|
+
async setActiveSingleton(capability, addonId, immediate = false) {
|
|
984
|
+
const state = this.capabilities.get(capability);
|
|
985
|
+
if (!state) {
|
|
986
|
+
throw new Error(`Unknown capability: ${capability}`);
|
|
987
|
+
}
|
|
988
|
+
if (state.declaration.mode !== "singleton") {
|
|
989
|
+
throw new Error(`Capability "${capability}" is not a singleton`);
|
|
990
|
+
}
|
|
991
|
+
const provider = state.available.get(addonId);
|
|
992
|
+
if (!provider) {
|
|
993
|
+
throw new Error(`No provider "${addonId}" registered for capability "${capability}"`);
|
|
994
|
+
}
|
|
995
|
+
if (immediate) {
|
|
996
|
+
await this.activateSingletonAsync(state, addonId, provider);
|
|
997
|
+
}
|
|
998
|
+
this.logger.info(`Singleton preference set: ${capability} \u2192 ${addonId}`);
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Get the mode declared for a capability.
|
|
1002
|
+
*/
|
|
1003
|
+
getMode(capability) {
|
|
1004
|
+
return this.capabilities.get(capability)?.declaration.mode;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* List all registered capabilities with their providers.
|
|
1008
|
+
*/
|
|
1009
|
+
listCapabilities() {
|
|
1010
|
+
const result = [];
|
|
1011
|
+
for (const [name, state] of this.capabilities) {
|
|
1012
|
+
result.push({
|
|
1013
|
+
name,
|
|
1014
|
+
mode: state.declaration.mode,
|
|
1015
|
+
providers: [...state.available.keys()],
|
|
1016
|
+
activeProvider: state.activeAddonId
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
return result;
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* Check if all dependencies for a capability are satisfied (have active providers).
|
|
1023
|
+
*/
|
|
1024
|
+
areDependenciesMet(declaration) {
|
|
1025
|
+
if (!declaration.dependsOn?.length)
|
|
1026
|
+
return true;
|
|
1027
|
+
return declaration.dependsOn.every((dep) => {
|
|
1028
|
+
const state = this.capabilities.get(dep);
|
|
1029
|
+
if (!state)
|
|
1030
|
+
return false;
|
|
1031
|
+
if (state.declaration.mode === "singleton") {
|
|
1032
|
+
return state.activeProvider !== null;
|
|
1033
|
+
}
|
|
1034
|
+
return state.activeCollection.length > 0;
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Get the dependency-ordered list of capability names for boot sequencing.
|
|
1039
|
+
* Returns capabilities sorted topologically by dependsOn.
|
|
1040
|
+
* Throws if a cycle is detected.
|
|
1041
|
+
*/
|
|
1042
|
+
getBootOrder() {
|
|
1043
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1044
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
1045
|
+
const order = [];
|
|
1046
|
+
const visit = (name) => {
|
|
1047
|
+
if (visited.has(name))
|
|
1048
|
+
return;
|
|
1049
|
+
if (visiting.has(name)) {
|
|
1050
|
+
throw new Error(`Circular dependency detected involving capability "${name}"`);
|
|
1051
|
+
}
|
|
1052
|
+
visiting.add(name);
|
|
1053
|
+
const state = this.capabilities.get(name);
|
|
1054
|
+
if (state?.declaration.dependsOn) {
|
|
1055
|
+
for (const dep of state.declaration.dependsOn) {
|
|
1056
|
+
visit(dep);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
visiting.delete(name);
|
|
1060
|
+
visited.add(name);
|
|
1061
|
+
order.push(name);
|
|
1062
|
+
};
|
|
1063
|
+
for (const name of this.capabilities.keys()) {
|
|
1064
|
+
visit(name);
|
|
1065
|
+
}
|
|
1066
|
+
return order;
|
|
1067
|
+
}
|
|
1068
|
+
// ---- Per-device overrides ----
|
|
1069
|
+
/**
|
|
1070
|
+
* Set a per-device singleton override. When resolveForDevice is called for
|
|
1071
|
+
* this device + capability, the specified addon's provider is returned
|
|
1072
|
+
* instead of the global singleton.
|
|
1073
|
+
*/
|
|
1074
|
+
setDeviceOverride(deviceId, capability, addonId) {
|
|
1075
|
+
const state = this.capabilities.get(capability);
|
|
1076
|
+
if (!state) {
|
|
1077
|
+
this.logger.warn(`Cannot set device override for undeclared capability "${capability}"`);
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
if (!state.available.has(addonId)) {
|
|
1081
|
+
this.logger.warn(`Cannot set device override: addon "${addonId}" not registered for "${capability}"`);
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
let deviceMap = this.deviceOverrides.get(deviceId);
|
|
1085
|
+
if (!deviceMap) {
|
|
1086
|
+
deviceMap = /* @__PURE__ */ new Map();
|
|
1087
|
+
this.deviceOverrides.set(deviceId, deviceMap);
|
|
1088
|
+
}
|
|
1089
|
+
deviceMap.set(capability, addonId);
|
|
1090
|
+
this.logger.info(`Device override set: ${deviceId} \u2192 ${capability} = ${addonId}`);
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Clear a per-device singleton override, reverting to the global singleton.
|
|
1094
|
+
*/
|
|
1095
|
+
clearDeviceOverride(deviceId, capability) {
|
|
1096
|
+
const deviceMap = this.deviceOverrides.get(deviceId);
|
|
1097
|
+
if (!deviceMap)
|
|
1098
|
+
return;
|
|
1099
|
+
deviceMap.delete(capability);
|
|
1100
|
+
if (deviceMap.size === 0) {
|
|
1101
|
+
this.deviceOverrides.delete(deviceId);
|
|
1102
|
+
}
|
|
1103
|
+
this.logger.info(`Device override cleared: ${deviceId} \u2192 ${capability}`);
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Get all per-device singleton overrides for a device.
|
|
1107
|
+
* Returns a Map of capability name to addon ID.
|
|
1108
|
+
*/
|
|
1109
|
+
getDeviceOverrides(deviceId) {
|
|
1110
|
+
return new Map(this.deviceOverrides.get(deviceId) ?? []);
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Resolve a singleton provider for a specific device.
|
|
1114
|
+
* 1. Check device override — return that addon's provider
|
|
1115
|
+
* 2. Fallback to global singleton
|
|
1116
|
+
*/
|
|
1117
|
+
resolveForDevice(capability, deviceId) {
|
|
1118
|
+
const state = this.capabilities.get(capability);
|
|
1119
|
+
if (!state || state.declaration.mode !== "singleton")
|
|
1120
|
+
return null;
|
|
1121
|
+
const deviceMap = this.deviceOverrides.get(deviceId);
|
|
1122
|
+
if (deviceMap) {
|
|
1123
|
+
const overrideAddonId = deviceMap.get(capability);
|
|
1124
|
+
if (overrideAddonId) {
|
|
1125
|
+
const provider = state.available.get(overrideAddonId);
|
|
1126
|
+
if (provider)
|
|
1127
|
+
return provider;
|
|
1128
|
+
this.logger.warn(`Device override for ${deviceId}/${capability} references unregistered addon "${overrideAddonId}" \u2014 falling back to global`);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
return state.activeProvider ?? null;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Set a per-device collection filter. When resolveCollectionForDevice is called
|
|
1135
|
+
* for this device + capability, only providers from the specified addon IDs
|
|
1136
|
+
* are returned instead of the full collection.
|
|
1137
|
+
*/
|
|
1138
|
+
setDeviceCollectionFilter(deviceId, capability, addonIds) {
|
|
1139
|
+
const state = this.capabilities.get(capability);
|
|
1140
|
+
if (!state) {
|
|
1141
|
+
this.logger.warn(`Cannot set device collection filter for undeclared capability "${capability}"`);
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
let deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
1145
|
+
if (!deviceMap) {
|
|
1146
|
+
deviceMap = /* @__PURE__ */ new Map();
|
|
1147
|
+
this.deviceCollectionFilters.set(deviceId, deviceMap);
|
|
1148
|
+
}
|
|
1149
|
+
deviceMap.set(capability, [...addonIds]);
|
|
1150
|
+
this.logger.info(`Device collection filter set: ${deviceId} \u2192 ${capability} = [${addonIds.join(", ")}]`);
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Clear a per-device collection filter, reverting to the full collection.
|
|
1154
|
+
*/
|
|
1155
|
+
clearDeviceCollectionFilter(deviceId, capability) {
|
|
1156
|
+
const deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
1157
|
+
if (!deviceMap)
|
|
1158
|
+
return;
|
|
1159
|
+
deviceMap.delete(capability);
|
|
1160
|
+
if (deviceMap.size === 0) {
|
|
1161
|
+
this.deviceCollectionFilters.delete(deviceId);
|
|
1162
|
+
}
|
|
1163
|
+
this.logger.info(`Device collection filter cleared: ${deviceId} \u2192 ${capability}`);
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Resolve collection providers for a specific device.
|
|
1167
|
+
* If a filter exists for the device + capability, only those addon's providers are returned.
|
|
1168
|
+
* If no filter exists, the full collection is returned.
|
|
1169
|
+
*/
|
|
1170
|
+
resolveCollectionForDevice(capability, deviceId) {
|
|
1171
|
+
const state = this.capabilities.get(capability);
|
|
1172
|
+
if (!state || state.declaration.mode !== "collection")
|
|
1173
|
+
return [];
|
|
1174
|
+
const deviceMap = this.deviceCollectionFilters.get(deviceId);
|
|
1175
|
+
if (deviceMap) {
|
|
1176
|
+
const filterAddonIds = deviceMap.get(capability);
|
|
1177
|
+
if (filterAddonIds) {
|
|
1178
|
+
const filterSet = new Set(filterAddonIds);
|
|
1179
|
+
return state.activeCollection.filter((e) => filterSet.has(e.addonId)).map((e) => e.provider);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return state.activeCollection.map((e) => e.provider);
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Get a specific addon's provider by addon ID, regardless of whether it's the active singleton.
|
|
1186
|
+
* Useful for per-device overrides that need to look up any registered provider.
|
|
1187
|
+
*/
|
|
1188
|
+
getProviderByAddonId(capability, addonId) {
|
|
1189
|
+
const state = this.capabilities.get(capability);
|
|
1190
|
+
if (!state)
|
|
1191
|
+
return null;
|
|
1192
|
+
const provider = state.available.get(addonId);
|
|
1193
|
+
return provider ?? null;
|
|
1194
|
+
}
|
|
1195
|
+
activateSingleton(state, addonId, provider) {
|
|
1196
|
+
state.activeAddonId = addonId;
|
|
1197
|
+
state.activeProvider = provider;
|
|
1198
|
+
this.logger.info(`Singleton activated: ${state.declaration.name} \u2192 ${addonId}`);
|
|
1199
|
+
for (const consumer of state.consumers) {
|
|
1200
|
+
if (consumer.onSet) {
|
|
1201
|
+
try {
|
|
1202
|
+
consumer.onSet(provider);
|
|
1203
|
+
} catch (error) {
|
|
1204
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1205
|
+
this.logger.error(`Consumer onSet failed for ${state.declaration.name}: ${msg}`);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
async activateSingletonAsync(state, addonId, provider) {
|
|
1211
|
+
state.activeAddonId = addonId;
|
|
1212
|
+
state.activeProvider = provider;
|
|
1213
|
+
this.logger.info(`Singleton activated (async): ${state.declaration.name} \u2192 ${addonId}`);
|
|
1214
|
+
for (const consumer of state.consumers) {
|
|
1215
|
+
if (consumer.onSet) {
|
|
1216
|
+
try {
|
|
1217
|
+
await consumer.onSet(provider);
|
|
1218
|
+
} catch (error) {
|
|
1219
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1220
|
+
this.logger.error(`Consumer onSet (async) failed for ${state.declaration.name}: ${msg}`);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
exports2.CapabilityRegistry = CapabilityRegistry2;
|
|
1238
1227
|
}
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
// src/infra-capabilities.js
|
|
1231
|
+
var require_infra_capabilities = __commonJS({
|
|
1232
|
+
"src/infra-capabilities.js"(exports2) {
|
|
1233
|
+
"use strict";
|
|
1234
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
1235
|
+
exports2.INFRA_CAPABILITIES = void 0;
|
|
1236
|
+
exports2.isInfraCapability = isInfraCapability2;
|
|
1237
|
+
exports2.INFRA_CAPABILITIES = [
|
|
1238
|
+
{ name: "storage", required: true },
|
|
1239
|
+
{ name: "settings-store", required: true },
|
|
1240
|
+
{ name: "log-destination", required: false }
|
|
1241
|
+
];
|
|
1242
|
+
var infraNames = new Set(exports2.INFRA_CAPABILITIES.map((c) => c.name));
|
|
1243
|
+
function isInfraCapability2(name) {
|
|
1244
|
+
return infraNames.has(name);
|
|
1243
1245
|
}
|
|
1244
|
-
this.settingsStore.setDevice(deviceId, key, value);
|
|
1245
|
-
}
|
|
1246
|
-
/** Get a value from the parsed bootstrap config */
|
|
1247
|
-
getBootstrap(path5) {
|
|
1248
|
-
return this.getFromBootstrap(path5);
|
|
1249
1246
|
}
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
// src/config-schema.js
|
|
1250
|
+
var require_config_schema = __commonJS({
|
|
1251
|
+
"src/config-schema.js"(exports2) {
|
|
1252
|
+
"use strict";
|
|
1253
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
1254
|
+
exports2.RUNTIME_DEFAULTS = exports2.bootstrapSchema = exports2.DEFAULT_DATA_PATH = void 0;
|
|
1255
|
+
var zod_1 = require("zod");
|
|
1256
|
+
exports2.DEFAULT_DATA_PATH = "camstack-data";
|
|
1257
|
+
exports2.bootstrapSchema = zod_1.z.object({
|
|
1258
|
+
/** Server mode: 'hub' (full server) or 'agent' (worker node) */
|
|
1259
|
+
mode: zod_1.z.enum(["hub", "agent"]).default("hub"),
|
|
1260
|
+
server: zod_1.z.object({
|
|
1261
|
+
port: zod_1.z.number().default(4443),
|
|
1262
|
+
host: zod_1.z.string().default("0.0.0.0"),
|
|
1263
|
+
dataPath: zod_1.z.string().default(exports2.DEFAULT_DATA_PATH)
|
|
1264
|
+
}).default({}),
|
|
1265
|
+
auth: zod_1.z.object({
|
|
1266
|
+
jwtSecret: zod_1.z.string().nullable().default(null),
|
|
1267
|
+
adminUsername: zod_1.z.string().default("admin"),
|
|
1268
|
+
adminPassword: zod_1.z.string().default(process.env.ADMIN_PASSWORD ?? "changeme")
|
|
1269
|
+
}).default({}),
|
|
1270
|
+
/** Hub connection config — only used when mode='agent' */
|
|
1271
|
+
hub: zod_1.z.object({
|
|
1272
|
+
url: zod_1.z.string().default("ws://localhost:4443/agent"),
|
|
1273
|
+
token: zod_1.z.string().default("")
|
|
1274
|
+
}).default({}),
|
|
1275
|
+
/** Agent-specific config — only used when mode='agent' */
|
|
1276
|
+
agent: zod_1.z.object({
|
|
1277
|
+
name: zod_1.z.string().default(""),
|
|
1278
|
+
/** Port for the agent status page (minimal HTML) */
|
|
1279
|
+
statusPort: zod_1.z.number().default(4444)
|
|
1280
|
+
}).default({}),
|
|
1281
|
+
/** TLS configuration */
|
|
1282
|
+
tls: zod_1.z.object({
|
|
1283
|
+
/** Enable HTTPS (default: true) */
|
|
1284
|
+
enabled: zod_1.z.boolean().default(true),
|
|
1285
|
+
/** Path to custom cert file (PEM). If not set, auto-generates self-signed. */
|
|
1286
|
+
certPath: zod_1.z.string().optional(),
|
|
1287
|
+
/** Path to custom key file (PEM). Required if certPath is set. */
|
|
1288
|
+
keyPath: zod_1.z.string().optional()
|
|
1289
|
+
}).default({})
|
|
1290
|
+
});
|
|
1291
|
+
exports2.RUNTIME_DEFAULTS = {
|
|
1292
|
+
"features.streaming": true,
|
|
1293
|
+
"features.notifications": true,
|
|
1294
|
+
"features.objectDetection": false,
|
|
1295
|
+
"features.remoteAccess": true,
|
|
1296
|
+
"features.agentCluster": false,
|
|
1297
|
+
"features.smartHome": true,
|
|
1298
|
+
"features.recordings": true,
|
|
1299
|
+
"features.backup": true,
|
|
1300
|
+
"features.repl": true,
|
|
1301
|
+
"retention.detectionEventsDays": 30,
|
|
1302
|
+
"retention.audioLevelsDays": 7,
|
|
1303
|
+
"logging.level": "info",
|
|
1304
|
+
"logging.retentionDays": 30,
|
|
1305
|
+
"eventBus.ringBufferSize": 1e4,
|
|
1306
|
+
"storage.provider": "sqlite-storage",
|
|
1307
|
+
"storage.locations": {
|
|
1308
|
+
data: "camstack-data/data",
|
|
1309
|
+
media: "camstack-data/media",
|
|
1310
|
+
recordings: "camstack-data/recordings",
|
|
1311
|
+
cache: "/tmp/camstack-cache",
|
|
1312
|
+
logs: "camstack-data/logs",
|
|
1313
|
+
models: "camstack-data/models"
|
|
1297
1314
|
},
|
|
1298
|
-
providers:
|
|
1315
|
+
"providers": [],
|
|
1316
|
+
// Recording
|
|
1317
|
+
"recording.segmentDurationSeconds": 4,
|
|
1318
|
+
"recording.defaultRetentionDays": 30,
|
|
1319
|
+
// Streaming ports are addon-specific (go2rtc owns its defaults)
|
|
1320
|
+
// FFmpeg
|
|
1321
|
+
"ffmpeg.binaryPath": "ffmpeg",
|
|
1322
|
+
"ffmpeg.hwAccel": "auto",
|
|
1323
|
+
"ffmpeg.threadCount": 0,
|
|
1324
|
+
// Detection defaults
|
|
1325
|
+
"detection.defaultMotionFps": 2,
|
|
1326
|
+
"detection.defaultDetectionFps": 5,
|
|
1327
|
+
"detection.defaultCooldownSeconds": 10,
|
|
1328
|
+
"detection.defaultConfidenceThreshold": 0.4,
|
|
1329
|
+
"detection.trackerMaxAgeFrames": 30,
|
|
1330
|
+
"detection.trackerMinHits": 3,
|
|
1331
|
+
"detection.trackerIouThreshold": 0.3,
|
|
1332
|
+
// Backup retention is addon-specific (local-backup owns its default)
|
|
1333
|
+
// Auth (runtime)
|
|
1334
|
+
"auth.tokenExpiry": "24h"
|
|
1299
1335
|
};
|
|
1300
1336
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1337
|
+
});
|
|
1338
|
+
|
|
1339
|
+
// src/config-manager.js
|
|
1340
|
+
var require_config_manager = __commonJS({
|
|
1341
|
+
"src/config-manager.js"(exports2) {
|
|
1342
|
+
"use strict";
|
|
1343
|
+
var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
1344
|
+
if (k2 === void 0) k2 = k;
|
|
1345
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1346
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1347
|
+
desc = { enumerable: true, get: function() {
|
|
1348
|
+
return m[k];
|
|
1349
|
+
} };
|
|
1350
|
+
}
|
|
1351
|
+
Object.defineProperty(o, k2, desc);
|
|
1352
|
+
}) : (function(o, m, k, k2) {
|
|
1353
|
+
if (k2 === void 0) k2 = k;
|
|
1354
|
+
o[k2] = m[k];
|
|
1355
|
+
}));
|
|
1356
|
+
var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
1357
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
1358
|
+
}) : function(o, v) {
|
|
1359
|
+
o["default"] = v;
|
|
1360
|
+
});
|
|
1361
|
+
var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
|
|
1362
|
+
var ownKeys = function(o) {
|
|
1363
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
1364
|
+
var ar = [];
|
|
1365
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
1366
|
+
return ar;
|
|
1367
|
+
};
|
|
1368
|
+
return ownKeys(o);
|
|
1369
|
+
};
|
|
1370
|
+
return function(mod) {
|
|
1371
|
+
if (mod && mod.__esModule) return mod;
|
|
1372
|
+
var result = {};
|
|
1373
|
+
if (mod != null) {
|
|
1374
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
1375
|
+
}
|
|
1376
|
+
__setModuleDefault(result, mod);
|
|
1377
|
+
return result;
|
|
1378
|
+
};
|
|
1379
|
+
})();
|
|
1380
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
1381
|
+
exports2.ConfigManager = void 0;
|
|
1382
|
+
var fs = __importStar(require("fs"));
|
|
1383
|
+
var yaml = __importStar(require("js-yaml"));
|
|
1384
|
+
var config_schema_js_1 = require_config_schema();
|
|
1385
|
+
var ENV_VAR_MAP = {
|
|
1386
|
+
CAMSTACK_PORT: "server.port",
|
|
1387
|
+
CAMSTACK_HOST: "server.host",
|
|
1388
|
+
CAMSTACK_DATA: "server.dataPath",
|
|
1389
|
+
CAMSTACK_JWT_SECRET: "auth.jwtSecret",
|
|
1390
|
+
CAMSTACK_ADMIN_USER: "auth.adminUsername",
|
|
1391
|
+
CAMSTACK_ADMIN_PASS: "auth.adminPassword"
|
|
1392
|
+
};
|
|
1393
|
+
var ConfigManager2 = class _ConfigManager {
|
|
1394
|
+
configPath;
|
|
1395
|
+
// Non-readonly so update() can sync the in-memory view after a write.
|
|
1396
|
+
bootstrapConfig;
|
|
1397
|
+
settingsStore = null;
|
|
1398
|
+
constructor(configPath) {
|
|
1399
|
+
this.configPath = configPath;
|
|
1400
|
+
const rawYaml = this.loadYaml();
|
|
1401
|
+
const merged = this.applyEnvOverrides(rawYaml);
|
|
1402
|
+
this.bootstrapConfig = config_schema_js_1.bootstrapSchema.parse(merged);
|
|
1403
|
+
this.warnDefaultCredentials();
|
|
1404
|
+
}
|
|
1405
|
+
/** Called by main.ts after the SQLite DB is ready (Phase 2). */
|
|
1406
|
+
setSettingsStore(store) {
|
|
1407
|
+
this.settingsStore = store;
|
|
1408
|
+
}
|
|
1409
|
+
/**
|
|
1410
|
+
* Get a config value by dot-notation path.
|
|
1411
|
+
* Priority: bootstrap config -> SQL system_settings -> RUNTIME_DEFAULTS fallback.
|
|
1412
|
+
*/
|
|
1413
|
+
get(path) {
|
|
1414
|
+
const bootstrapValue = this.getFromBootstrap(path);
|
|
1415
|
+
if (bootstrapValue !== void 0) {
|
|
1416
|
+
return bootstrapValue;
|
|
1417
|
+
}
|
|
1418
|
+
if (this.settingsStore !== null) {
|
|
1419
|
+
const sqlValue = this.settingsStore.getSystem(path);
|
|
1420
|
+
if (sqlValue !== void 0) {
|
|
1421
|
+
return sqlValue;
|
|
1422
|
+
}
|
|
1423
|
+
const sqlNested = this.getNestedFromSystemSettings(path);
|
|
1424
|
+
if (sqlNested !== void 0) {
|
|
1425
|
+
return sqlNested;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
if (path in config_schema_js_1.RUNTIME_DEFAULTS) {
|
|
1429
|
+
return config_schema_js_1.RUNTIME_DEFAULTS[path];
|
|
1430
|
+
}
|
|
1431
|
+
const nested = this.getFromRuntimeDefaults(path);
|
|
1432
|
+
if (nested !== void 0) {
|
|
1433
|
+
return nested;
|
|
1434
|
+
}
|
|
1383
1435
|
return void 0;
|
|
1384
1436
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
if (key.startsWith(prefix)) {
|
|
1395
|
-
const subKey = key.slice(prefix.length);
|
|
1396
|
-
result[subKey] = value;
|
|
1397
|
-
found = true;
|
|
1437
|
+
/**
|
|
1438
|
+
* Write a value to SQL system_settings.
|
|
1439
|
+
* Throws if the settings store is not yet wired.
|
|
1440
|
+
*/
|
|
1441
|
+
set(key, value) {
|
|
1442
|
+
if (this.settingsStore === null) {
|
|
1443
|
+
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1444
|
+
}
|
|
1445
|
+
this.settingsStore.setSystem(key, value);
|
|
1398
1446
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1447
|
+
/**
|
|
1448
|
+
* Bulk-read all system_settings keys that belong to a logical section.
|
|
1449
|
+
* A "section" is the first segment of a dot-notation key (e.g. 'features', 'logging').
|
|
1450
|
+
*/
|
|
1451
|
+
getSection(section) {
|
|
1452
|
+
if (this.settingsStore !== null) {
|
|
1453
|
+
const nested = this.getNestedFromSystemSettings(section);
|
|
1454
|
+
if (nested !== void 0)
|
|
1455
|
+
return nested;
|
|
1456
|
+
}
|
|
1457
|
+
const bootstrapValue = this.bootstrapConfig[section];
|
|
1458
|
+
if (bootstrapValue !== void 0 && bootstrapValue !== null && typeof bootstrapValue === "object") {
|
|
1459
|
+
return bootstrapValue;
|
|
1460
|
+
}
|
|
1461
|
+
return this.getFromRuntimeDefaults(section) ?? {};
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* Bulk-write a section of runtime settings to SQL system_settings.
|
|
1465
|
+
* Each entry in `data` is stored as `section.key`.
|
|
1466
|
+
*/
|
|
1467
|
+
setSection(section, data) {
|
|
1468
|
+
if (this.settingsStore === null) {
|
|
1469
|
+
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1470
|
+
}
|
|
1471
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1472
|
+
this.settingsStore.setSystem(`${section}.${key}`, value);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
// ---------------------------------------------------------------------------
|
|
1476
|
+
// Addon / Provider / Device scoped config
|
|
1477
|
+
// ---------------------------------------------------------------------------
|
|
1478
|
+
/** Read all config for an addon from addon_settings. */
|
|
1479
|
+
getAddonConfig(addonId) {
|
|
1480
|
+
if (this.settingsStore !== null) {
|
|
1481
|
+
return this.settingsStore.getAllAddon(addonId);
|
|
1482
|
+
}
|
|
1483
|
+
return this.getFromBootstrap(`addons.${addonId}`) ?? {};
|
|
1484
|
+
}
|
|
1485
|
+
/** Write (bulk-replace) config for an addon to addon_settings. */
|
|
1486
|
+
setAddonConfig(addonId, config) {
|
|
1487
|
+
if (this.settingsStore === null) {
|
|
1488
|
+
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1489
|
+
}
|
|
1490
|
+
this.settingsStore.setAllAddon(addonId, config);
|
|
1491
|
+
}
|
|
1492
|
+
/** Read all config for a provider from provider_settings. */
|
|
1493
|
+
getProviderConfig(providerId) {
|
|
1494
|
+
if (this.settingsStore !== null) {
|
|
1495
|
+
return this.settingsStore.getAllProvider(providerId);
|
|
1496
|
+
}
|
|
1497
|
+
return {};
|
|
1498
|
+
}
|
|
1499
|
+
/** Write (upsert) a single key for a provider to provider_settings. */
|
|
1500
|
+
setProviderConfig(providerId, key, value) {
|
|
1501
|
+
if (this.settingsStore === null) {
|
|
1502
|
+
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1503
|
+
}
|
|
1504
|
+
this.settingsStore.setProvider(providerId, key, value);
|
|
1505
|
+
}
|
|
1506
|
+
/** Read all config for a device from device_settings. */
|
|
1507
|
+
getDeviceConfig(deviceId) {
|
|
1508
|
+
if (this.settingsStore !== null) {
|
|
1509
|
+
return this.settingsStore.getAllDevice(deviceId);
|
|
1510
|
+
}
|
|
1511
|
+
return {};
|
|
1512
|
+
}
|
|
1513
|
+
/** Write (upsert) a single key for a device to device_settings. */
|
|
1514
|
+
setDeviceConfig(deviceId, key, value) {
|
|
1515
|
+
if (this.settingsStore === null) {
|
|
1516
|
+
throw new Error("[ConfigManager] SettingsStore not initialized -- call setSettingsStore() first");
|
|
1517
|
+
}
|
|
1518
|
+
this.settingsStore.setDevice(deviceId, key, value);
|
|
1519
|
+
}
|
|
1520
|
+
/** Get a value from the parsed bootstrap config */
|
|
1521
|
+
getBootstrap(path) {
|
|
1522
|
+
return this.getFromBootstrap(path);
|
|
1523
|
+
}
|
|
1524
|
+
/** Features accessor -- reads from SQL when available, falls back to RUNTIME_DEFAULTS */
|
|
1525
|
+
get features() {
|
|
1526
|
+
const g = (key) => this.get(`features.${key}`) ?? config_schema_js_1.RUNTIME_DEFAULTS[`features.${key}`];
|
|
1527
|
+
return {
|
|
1528
|
+
streaming: g("streaming"),
|
|
1529
|
+
notifications: g("notifications"),
|
|
1530
|
+
objectDetection: g("objectDetection"),
|
|
1531
|
+
remoteAccess: g("remoteAccess"),
|
|
1532
|
+
agentCluster: g("agentCluster"),
|
|
1533
|
+
smartHome: g("smartHome"),
|
|
1534
|
+
recordings: g("recordings"),
|
|
1535
|
+
backup: g("backup"),
|
|
1536
|
+
repl: g("repl")
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* Returns a merged view of bootstrap config + runtime defaults for backward compat.
|
|
1541
|
+
*/
|
|
1542
|
+
get raw() {
|
|
1543
|
+
const features = {
|
|
1544
|
+
streaming: config_schema_js_1.RUNTIME_DEFAULTS["features.streaming"],
|
|
1545
|
+
notifications: config_schema_js_1.RUNTIME_DEFAULTS["features.notifications"],
|
|
1546
|
+
objectDetection: config_schema_js_1.RUNTIME_DEFAULTS["features.objectDetection"],
|
|
1547
|
+
remoteAccess: config_schema_js_1.RUNTIME_DEFAULTS["features.remoteAccess"],
|
|
1548
|
+
agentCluster: config_schema_js_1.RUNTIME_DEFAULTS["features.agentCluster"],
|
|
1549
|
+
smartHome: config_schema_js_1.RUNTIME_DEFAULTS["features.smartHome"],
|
|
1550
|
+
recordings: config_schema_js_1.RUNTIME_DEFAULTS["features.recordings"],
|
|
1551
|
+
backup: config_schema_js_1.RUNTIME_DEFAULTS["features.backup"],
|
|
1552
|
+
repl: config_schema_js_1.RUNTIME_DEFAULTS["features.repl"]
|
|
1553
|
+
};
|
|
1554
|
+
return {
|
|
1555
|
+
...this.bootstrapConfig,
|
|
1556
|
+
features,
|
|
1557
|
+
storage: config_schema_js_1.RUNTIME_DEFAULTS["storage.locations"] !== void 0 ? {
|
|
1558
|
+
provider: config_schema_js_1.RUNTIME_DEFAULTS["storage.provider"],
|
|
1559
|
+
locations: config_schema_js_1.RUNTIME_DEFAULTS["storage.locations"]
|
|
1560
|
+
} : { provider: "sqlite-storage", locations: {} },
|
|
1561
|
+
logging: {
|
|
1562
|
+
level: config_schema_js_1.RUNTIME_DEFAULTS["logging.level"],
|
|
1563
|
+
retentionDays: config_schema_js_1.RUNTIME_DEFAULTS["logging.retentionDays"]
|
|
1564
|
+
},
|
|
1565
|
+
eventBus: {
|
|
1566
|
+
ringBufferSize: config_schema_js_1.RUNTIME_DEFAULTS["eventBus.ringBufferSize"]
|
|
1567
|
+
},
|
|
1568
|
+
retention: {
|
|
1569
|
+
detectionEventsDays: config_schema_js_1.RUNTIME_DEFAULTS["retention.detectionEventsDays"],
|
|
1570
|
+
audioLevelsDays: config_schema_js_1.RUNTIME_DEFAULTS["retention.audioLevelsDays"]
|
|
1571
|
+
},
|
|
1572
|
+
providers: config_schema_js_1.RUNTIME_DEFAULTS["providers"]
|
|
1573
|
+
};
|
|
1574
|
+
}
|
|
1575
|
+
/** Sections that live in config.yaml. Everything else goes to SQL. */
|
|
1576
|
+
static BOOTSTRAP_SECTIONS = /* @__PURE__ */ new Set(["server", "auth", "mode"]);
|
|
1577
|
+
/**
|
|
1578
|
+
* Atomically update one top-level section of config.yaml and sync in-memory.
|
|
1579
|
+
* Only bootstrap sections (server, auth, mode) are written to YAML.
|
|
1580
|
+
* Runtime settings must use setSection() which writes to SQL.
|
|
1581
|
+
*/
|
|
1582
|
+
update(section, data) {
|
|
1583
|
+
if (!_ConfigManager.BOOTSTRAP_SECTIONS.has(section)) {
|
|
1584
|
+
throw new Error(`[ConfigManager] Section "${section}" is a runtime setting \u2014 use setSection() to write to DB, not update() which writes to config.yaml`);
|
|
1585
|
+
}
|
|
1586
|
+
let raw = {};
|
|
1587
|
+
if (fs.existsSync(this.configPath)) {
|
|
1588
|
+
raw = yaml.load(fs.readFileSync(this.configPath, "utf-8")) ?? {};
|
|
1589
|
+
}
|
|
1590
|
+
const existing = raw[section] ?? {};
|
|
1591
|
+
raw[section] = { ...existing, ...data };
|
|
1592
|
+
const validation = config_schema_js_1.bootstrapSchema.safeParse(raw);
|
|
1593
|
+
if (!validation.success) {
|
|
1594
|
+
throw new Error(`[ConfigManager] Invalid config update for section "${section}": ${validation.error.message}`);
|
|
1595
|
+
}
|
|
1596
|
+
const tmpPath = `${this.configPath}.tmp`;
|
|
1597
|
+
fs.writeFileSync(tmpPath, yaml.dump(raw, { lineWidth: 120, indent: 2, quotingType: '"' }), "utf-8");
|
|
1598
|
+
fs.renameSync(tmpPath, this.configPath);
|
|
1599
|
+
this.bootstrapConfig = validation.data;
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* Deep-set a value in a nested plain object using a dot-notation path.
|
|
1603
|
+
* Returns a new object (immutable).
|
|
1604
|
+
*/
|
|
1605
|
+
setNested(obj, path, value) {
|
|
1606
|
+
const [head, ...rest] = path.split(".");
|
|
1607
|
+
if (!head)
|
|
1608
|
+
return obj;
|
|
1609
|
+
if (rest.length === 0) {
|
|
1610
|
+
return { ...obj, [head]: value };
|
|
1611
|
+
}
|
|
1612
|
+
const child = obj[head] ?? {};
|
|
1613
|
+
return { ...obj, [head]: this.setNested(child, rest.join("."), value) };
|
|
1614
|
+
}
|
|
1615
|
+
/**
|
|
1616
|
+
* Apply env var overrides onto the raw YAML object.
|
|
1617
|
+
* Only bootstrap-level env vars are applied.
|
|
1618
|
+
*/
|
|
1619
|
+
applyEnvOverrides(raw) {
|
|
1620
|
+
let result = { ...raw };
|
|
1621
|
+
for (const [envKey, configPath] of Object.entries(ENV_VAR_MAP)) {
|
|
1622
|
+
const envValue = process.env[envKey];
|
|
1623
|
+
if (envValue === void 0 || envValue === "")
|
|
1624
|
+
continue;
|
|
1625
|
+
const coerced = configPath === "server.port" ? Number(envValue) : envValue;
|
|
1626
|
+
result = this.setNested(result, configPath, coerced);
|
|
1627
|
+
console.log(`[ConfigManager] Env override applied: ${envKey} \u2192 ${configPath}`);
|
|
1628
|
+
}
|
|
1629
|
+
return result;
|
|
1630
|
+
}
|
|
1631
|
+
loadYaml() {
|
|
1632
|
+
if (!fs.existsSync(this.configPath)) {
|
|
1633
|
+
console.warn(`[ConfigManager] Config file not found at: ${this.configPath}
|
|
1634
|
+
\u2192 Using built-in defaults. Set CONFIG_PATH env var or create the file.
|
|
1635
|
+
\u2192 Example path from project root: ./server/backend/data/config.yaml`);
|
|
1636
|
+
return {};
|
|
1637
|
+
}
|
|
1638
|
+
const content = fs.readFileSync(this.configPath, "utf-8");
|
|
1639
|
+
const parsed = yaml.load(content) ?? {};
|
|
1640
|
+
console.log(`[ConfigManager] Loaded config from: ${this.configPath}`);
|
|
1641
|
+
return parsed;
|
|
1642
|
+
}
|
|
1643
|
+
warnDefaultCredentials() {
|
|
1644
|
+
if (this.bootstrapConfig.auth.adminPassword === "changeme") {
|
|
1645
|
+
console.warn(`[ConfigManager] Warning: Using default admin password "changeme". Set auth.adminPassword in your config.yaml or the ADMIN_PASSWORD env var.`);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
getFromBootstrap(path) {
|
|
1649
|
+
const keys = path.split(".");
|
|
1650
|
+
let current = this.bootstrapConfig;
|
|
1651
|
+
for (const key of keys) {
|
|
1652
|
+
if (current === null || current === void 0 || typeof current !== "object") {
|
|
1653
|
+
return void 0;
|
|
1654
|
+
}
|
|
1655
|
+
current = current[key];
|
|
1656
|
+
}
|
|
1657
|
+
return current;
|
|
1658
|
+
}
|
|
1659
|
+
getFromRuntimeDefaults(path) {
|
|
1660
|
+
const prefix = path + ".";
|
|
1661
|
+
const result = {};
|
|
1662
|
+
let found = false;
|
|
1663
|
+
for (const [key, value] of Object.entries(config_schema_js_1.RUNTIME_DEFAULTS)) {
|
|
1664
|
+
if (key.startsWith(prefix)) {
|
|
1665
|
+
const subKey = key.slice(prefix.length);
|
|
1666
|
+
result[subKey] = value;
|
|
1667
|
+
found = true;
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
return found ? result : void 0;
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Perform a prefix-based nested lookup against SQL system_settings.
|
|
1674
|
+
* e.g. path='features' matches keys 'features.streaming', 'features.notifications', etc.
|
|
1675
|
+
* Returns an object keyed by the sub-key, or undefined if nothing is found.
|
|
1676
|
+
*/
|
|
1677
|
+
getNestedFromSystemSettings(path) {
|
|
1678
|
+
if (this.settingsStore === null)
|
|
1679
|
+
return void 0;
|
|
1680
|
+
const all = this.settingsStore.getAllSystem();
|
|
1681
|
+
const prefix = path + ".";
|
|
1682
|
+
const result = {};
|
|
1683
|
+
let found = false;
|
|
1684
|
+
for (const [key, value] of Object.entries(all)) {
|
|
1685
|
+
if (key.startsWith(prefix)) {
|
|
1686
|
+
result[key.slice(prefix.length)] = value;
|
|
1687
|
+
found = true;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
return found ? result : void 0;
|
|
1417
1691
|
}
|
|
1418
|
-
}
|
|
1419
|
-
return found ? result : void 0;
|
|
1420
|
-
}
|
|
1421
|
-
};
|
|
1422
|
-
|
|
1423
|
-
// src/worker/addon-worker-host.ts
|
|
1424
|
-
var import_node_child_process3 = require("child_process");
|
|
1425
|
-
var INFRA_ADDON_IDS = /* @__PURE__ */ new Set([
|
|
1426
|
-
"filesystem-storage",
|
|
1427
|
-
"sqlite-settings",
|
|
1428
|
-
"winston-logging"
|
|
1429
|
-
]);
|
|
1430
|
-
var AddonWorkerHost = class {
|
|
1431
|
-
workers = /* @__PURE__ */ new Map();
|
|
1432
|
-
options;
|
|
1433
|
-
heartbeatTimer = null;
|
|
1434
|
-
/** Set of addons that failed to fork and fell back to in-process */
|
|
1435
|
-
fallbackInProcess = /* @__PURE__ */ new Set();
|
|
1436
|
-
constructor(options) {
|
|
1437
|
-
this.options = {
|
|
1438
|
-
workerEntryPath: options.workerEntryPath,
|
|
1439
|
-
devMode: options.devMode ?? false,
|
|
1440
|
-
forceInProcess: options.forceInProcess ?? process.env.CAMSTACK_FORCE_INPROCESS === "true",
|
|
1441
|
-
heartbeatIntervalMs: options.heartbeatIntervalMs ?? 5e3,
|
|
1442
|
-
heartbeatTimeoutMs: options.heartbeatTimeoutMs ?? 3e4,
|
|
1443
|
-
maxCrashesInWindow: options.maxCrashesInWindow ?? 3,
|
|
1444
|
-
crashWindowMs: options.crashWindowMs ?? 6e4,
|
|
1445
|
-
shutdownTimeoutMs: options.shutdownTimeoutMs ?? 1e4,
|
|
1446
|
-
onWorkerLog: options.onWorkerLog
|
|
1447
1692
|
};
|
|
1693
|
+
exports2.ConfigManager = ConfigManager2;
|
|
1448
1694
|
}
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
const workerEnv = {
|
|
1482
|
-
PATH: process.env.PATH ?? "",
|
|
1483
|
-
HOME: process.env.HOME ?? "",
|
|
1484
|
-
NODE_ENV: process.env.NODE_ENV ?? "production",
|
|
1485
|
-
NODE_TLS_REJECT_UNAUTHORIZED: "0",
|
|
1486
|
-
// Accept self-signed cert
|
|
1487
|
-
CAMSTACK_WORKER_HUB_URL: `wss://localhost:${process.env.CAMSTACK_PORT ?? "4443"}/trpc`,
|
|
1488
|
-
CAMSTACK_WORKER_TOKEN: workerToken ?? "",
|
|
1489
|
-
CAMSTACK_ADDON_ID: addonId,
|
|
1490
|
-
CAMSTACK_ADDON_DIR: addonDir,
|
|
1491
|
-
CAMSTACK_ADDON_CONFIG: JSON.stringify(config),
|
|
1492
|
-
CAMSTACK_DATA_DIR: dataDir ?? `camstack-data/addons-data/${addonId}`,
|
|
1493
|
-
CAMSTACK_LOCATION_PATHS: JSON.stringify(locationPaths ?? storagePaths)
|
|
1494
|
-
};
|
|
1495
|
-
const forkOptions = {
|
|
1496
|
-
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
1497
|
-
execArgv: [],
|
|
1498
|
-
env: workerEnv
|
|
1499
|
-
};
|
|
1500
|
-
const child = (0, import_node_child_process3.fork)(this.options.workerEntryPath, [], forkOptions);
|
|
1501
|
-
const worker = {
|
|
1502
|
-
addonId,
|
|
1503
|
-
process: child,
|
|
1504
|
-
state: "starting",
|
|
1505
|
-
startedAt: Date.now(),
|
|
1506
|
-
lastHeartbeat: Date.now(),
|
|
1507
|
-
restartCount: 0,
|
|
1508
|
-
crashTimestamps: [],
|
|
1509
|
-
cpuPercent: 0,
|
|
1510
|
-
memoryRss: 0,
|
|
1511
|
-
workerToken
|
|
1512
|
-
};
|
|
1513
|
-
this.workers.set(addonId, worker);
|
|
1514
|
-
child.on("message", (msg) => {
|
|
1515
|
-
if (msg.type === "LOG" && this.options.onWorkerLog) {
|
|
1516
|
-
this.options.onWorkerLog({
|
|
1517
|
-
addonId,
|
|
1518
|
-
level: msg.level,
|
|
1519
|
-
message: msg.message,
|
|
1520
|
-
scope: ["hub", addonId],
|
|
1521
|
-
meta: msg.context
|
|
1522
|
-
});
|
|
1523
|
-
} else if (msg.type === "STATS") {
|
|
1524
|
-
worker.cpuPercent = msg.cpu;
|
|
1525
|
-
worker.memoryRss = msg.memory;
|
|
1695
|
+
});
|
|
1696
|
+
|
|
1697
|
+
// src/worker/addon-worker-host.js
|
|
1698
|
+
var require_addon_worker_host = __commonJS({
|
|
1699
|
+
"src/worker/addon-worker-host.js"(exports2) {
|
|
1700
|
+
"use strict";
|
|
1701
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
1702
|
+
exports2.AddonWorkerHost = void 0;
|
|
1703
|
+
var node_child_process_1 = require("child_process");
|
|
1704
|
+
var INFRA_ADDON_IDS = /* @__PURE__ */ new Set([
|
|
1705
|
+
"filesystem-storage",
|
|
1706
|
+
"sqlite-settings",
|
|
1707
|
+
"winston-logging"
|
|
1708
|
+
]);
|
|
1709
|
+
var AddonWorkerHost2 = class {
|
|
1710
|
+
workers = /* @__PURE__ */ new Map();
|
|
1711
|
+
options;
|
|
1712
|
+
heartbeatTimer = null;
|
|
1713
|
+
/** Set of addons that failed to fork and fell back to in-process */
|
|
1714
|
+
fallbackInProcess = /* @__PURE__ */ new Set();
|
|
1715
|
+
constructor(options) {
|
|
1716
|
+
this.options = {
|
|
1717
|
+
workerEntryPath: options.workerEntryPath,
|
|
1718
|
+
devMode: options.devMode ?? false,
|
|
1719
|
+
forceInProcess: options.forceInProcess ?? process.env.CAMSTACK_FORCE_INPROCESS === "true",
|
|
1720
|
+
heartbeatIntervalMs: options.heartbeatIntervalMs ?? 5e3,
|
|
1721
|
+
heartbeatTimeoutMs: options.heartbeatTimeoutMs ?? 3e4,
|
|
1722
|
+
maxCrashesInWindow: options.maxCrashesInWindow ?? 3,
|
|
1723
|
+
crashWindowMs: options.crashWindowMs ?? 6e4,
|
|
1724
|
+
shutdownTimeoutMs: options.shutdownTimeoutMs ?? 1e4,
|
|
1725
|
+
onWorkerLog: options.onWorkerLog
|
|
1726
|
+
};
|
|
1526
1727
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
if (!lines) return;
|
|
1531
|
-
if (this.options.onWorkerLog) {
|
|
1532
|
-
this.options.onWorkerLog({
|
|
1533
|
-
addonId,
|
|
1534
|
-
level: "info",
|
|
1535
|
-
message: lines,
|
|
1536
|
-
scope: ["hub", addonId, "__stdout"]
|
|
1537
|
-
});
|
|
1538
|
-
} else {
|
|
1539
|
-
console.log(`[Worker:${addonId}] ${lines}`);
|
|
1728
|
+
/** Check if an addon is infrastructure (must stay in-process) */
|
|
1729
|
+
isInfraAddon(addonId) {
|
|
1730
|
+
return INFRA_ADDON_IDS.has(addonId);
|
|
1540
1731
|
}
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1732
|
+
/** Check if an addon fell back to in-process after fork failure */
|
|
1733
|
+
isFallbackInProcess(addonId) {
|
|
1734
|
+
return this.fallbackInProcess.has(addonId);
|
|
1735
|
+
}
|
|
1736
|
+
/** Mark an addon as fallen back to in-process */
|
|
1737
|
+
markFallbackInProcess(addonId) {
|
|
1738
|
+
this.fallbackInProcess.add(addonId);
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Determine if an addon should be forked.
|
|
1742
|
+
* Default: fork everything except infra addons.
|
|
1743
|
+
* Addons can opt out with `inProcess: true` in declaration.
|
|
1744
|
+
* Emergency fallback: CAMSTACK_FORCE_INPROCESS=true disables all forking.
|
|
1745
|
+
*/
|
|
1746
|
+
shouldFork(addonId, declaration) {
|
|
1747
|
+
if (this.options.forceInProcess)
|
|
1748
|
+
return false;
|
|
1749
|
+
if (this.options.devMode)
|
|
1750
|
+
return false;
|
|
1751
|
+
if (this.isInfraAddon(addonId))
|
|
1752
|
+
return false;
|
|
1753
|
+
if (this.fallbackInProcess.has(addonId))
|
|
1754
|
+
return false;
|
|
1755
|
+
if (declaration?.inProcess === true)
|
|
1756
|
+
return false;
|
|
1757
|
+
if (declaration?.forkable !== true)
|
|
1758
|
+
return false;
|
|
1759
|
+
return true;
|
|
1760
|
+
}
|
|
1761
|
+
/** Fork a worker for an addon */
|
|
1762
|
+
async forkWorker(addonId, addonDir, config, storagePaths, dataDir, locationPaths, workerToken) {
|
|
1763
|
+
if (this.workers.has(addonId)) {
|
|
1764
|
+
throw new Error(`Worker for addon "${addonId}" already exists`);
|
|
1765
|
+
}
|
|
1766
|
+
const workerEnv = {
|
|
1767
|
+
PATH: process.env.PATH ?? "",
|
|
1768
|
+
HOME: process.env.HOME ?? "",
|
|
1769
|
+
NODE_ENV: process.env.NODE_ENV ?? "production",
|
|
1770
|
+
NODE_TLS_REJECT_UNAUTHORIZED: "0",
|
|
1771
|
+
// Accept self-signed cert
|
|
1772
|
+
CAMSTACK_WORKER_HUB_URL: `wss://localhost:${process.env.CAMSTACK_PORT ?? "4443"}/trpc`,
|
|
1773
|
+
CAMSTACK_WORKER_TOKEN: workerToken ?? "",
|
|
1774
|
+
CAMSTACK_ADDON_ID: addonId,
|
|
1775
|
+
CAMSTACK_ADDON_DIR: addonDir,
|
|
1776
|
+
CAMSTACK_ADDON_CONFIG: JSON.stringify(config),
|
|
1777
|
+
CAMSTACK_DATA_DIR: dataDir ?? `camstack-data/addons-data/${addonId}`,
|
|
1778
|
+
CAMSTACK_LOCATION_PATHS: JSON.stringify(locationPaths ?? storagePaths)
|
|
1779
|
+
};
|
|
1780
|
+
const forkOptions = {
|
|
1781
|
+
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
1782
|
+
execArgv: [],
|
|
1783
|
+
env: workerEnv
|
|
1784
|
+
};
|
|
1785
|
+
const child = (0, node_child_process_1.fork)(this.options.workerEntryPath, [], forkOptions);
|
|
1786
|
+
const worker = {
|
|
1547
1787
|
addonId,
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1788
|
+
process: child,
|
|
1789
|
+
state: "starting",
|
|
1790
|
+
startedAt: Date.now(),
|
|
1791
|
+
lastHeartbeat: Date.now(),
|
|
1792
|
+
restartCount: 0,
|
|
1793
|
+
crashTimestamps: [],
|
|
1794
|
+
cpuPercent: 0,
|
|
1795
|
+
memoryRss: 0,
|
|
1796
|
+
workerToken
|
|
1797
|
+
};
|
|
1798
|
+
this.workers.set(addonId, worker);
|
|
1799
|
+
child.on("message", (msg) => {
|
|
1800
|
+
if (msg.type === "LOG" && this.options.onWorkerLog) {
|
|
1801
|
+
this.options.onWorkerLog({
|
|
1802
|
+
addonId,
|
|
1803
|
+
level: msg.level,
|
|
1804
|
+
message: msg.message,
|
|
1805
|
+
scope: ["hub", addonId],
|
|
1806
|
+
meta: msg.context
|
|
1807
|
+
});
|
|
1808
|
+
} else if (msg.type === "STATS") {
|
|
1809
|
+
worker.cpuPercent = msg.cpu;
|
|
1810
|
+
worker.memoryRss = msg.memory;
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
child.stdout?.on("data", (chunk) => {
|
|
1814
|
+
const lines = chunk.toString().trim();
|
|
1815
|
+
if (!lines)
|
|
1816
|
+
return;
|
|
1817
|
+
if (this.options.onWorkerLog) {
|
|
1818
|
+
this.options.onWorkerLog({
|
|
1819
|
+
addonId,
|
|
1820
|
+
level: "info",
|
|
1821
|
+
message: lines,
|
|
1822
|
+
scope: ["hub", addonId, "__stdout"]
|
|
1823
|
+
});
|
|
1824
|
+
} else {
|
|
1825
|
+
console.log(`[Worker:${addonId}] ${lines}`);
|
|
1826
|
+
}
|
|
1827
|
+
});
|
|
1828
|
+
child.stderr?.on("data", (chunk) => {
|
|
1829
|
+
const lines = chunk.toString().trim();
|
|
1830
|
+
if (!lines)
|
|
1831
|
+
return;
|
|
1832
|
+
if (this.options.onWorkerLog) {
|
|
1833
|
+
this.options.onWorkerLog({
|
|
1834
|
+
addonId,
|
|
1835
|
+
level: "error",
|
|
1836
|
+
message: lines,
|
|
1837
|
+
scope: ["hub", addonId, "__stderr"]
|
|
1838
|
+
});
|
|
1839
|
+
} else {
|
|
1840
|
+
console.error(`[Worker:${addonId}:ERR] ${lines}`);
|
|
1841
|
+
}
|
|
1842
|
+
});
|
|
1843
|
+
child.on("exit", (code) => {
|
|
1844
|
+
this.handleWorkerExit(addonId, code, addonDir, config, storagePaths, dataDir, locationPaths, workerToken);
|
|
1845
|
+
});
|
|
1846
|
+
child.on("error", (err) => {
|
|
1847
|
+
console.error(`[WorkerHost] Worker ${addonId} error:`, err.message);
|
|
1848
|
+
});
|
|
1849
|
+
await new Promise((resolve, reject) => {
|
|
1850
|
+
const timeout = setTimeout(() => {
|
|
1851
|
+
reject(new Error(`Worker ${addonId} did not become ready within 30s`));
|
|
1852
|
+
}, 3e4);
|
|
1853
|
+
const readyCheck = (msg) => {
|
|
1854
|
+
if (msg.type === "READY") {
|
|
1855
|
+
clearTimeout(timeout);
|
|
1856
|
+
child.off("message", readyCheck);
|
|
1857
|
+
worker.state = "running";
|
|
1858
|
+
resolve();
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
child.on("message", readyCheck);
|
|
1551
1862
|
});
|
|
1552
|
-
} else {
|
|
1553
|
-
console.error(`[Worker:${addonId}:ERR] ${lines}`);
|
|
1554
1863
|
}
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1864
|
+
/** List all workers with stats */
|
|
1865
|
+
listWorkers() {
|
|
1866
|
+
return [...this.workers.values()].map((w) => ({
|
|
1867
|
+
addonId: w.addonId,
|
|
1868
|
+
pid: w.process.pid ?? 0,
|
|
1869
|
+
state: w.state,
|
|
1870
|
+
cpuPercent: w.cpuPercent,
|
|
1871
|
+
memoryRss: w.memoryRss,
|
|
1872
|
+
uptimeSeconds: Math.round((Date.now() - w.startedAt) / 1e3),
|
|
1873
|
+
restartCount: w.restartCount,
|
|
1874
|
+
subProcesses: []
|
|
1875
|
+
}));
|
|
1876
|
+
}
|
|
1877
|
+
/** Gracefully shutdown a single worker by addon ID */
|
|
1878
|
+
async shutdownWorkerById(addonId) {
|
|
1879
|
+
const worker = this.workers.get(addonId);
|
|
1880
|
+
if (!worker) {
|
|
1881
|
+
throw new Error(`No worker found for addon "${addonId}"`);
|
|
1572
1882
|
}
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
if (worker.state
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
}
|
|
1633
|
-
handleWorkerExit(addonId, code, addonDir, config, storagePaths, dataDir, locationPaths, workerToken) {
|
|
1634
|
-
const worker = this.workers.get(addonId);
|
|
1635
|
-
if (!worker) return;
|
|
1636
|
-
if (worker.state === "stopping") {
|
|
1637
|
-
worker.state = "stopped";
|
|
1638
|
-
return;
|
|
1639
|
-
}
|
|
1640
|
-
worker.state = "crashed";
|
|
1641
|
-
console.error(`[WorkerHost] Worker ${addonId} exited with code ${code}`);
|
|
1642
|
-
const now = Date.now();
|
|
1643
|
-
worker.crashTimestamps.push(now);
|
|
1644
|
-
const recentCrashes = worker.crashTimestamps.filter(
|
|
1645
|
-
(t) => now - t < this.options.crashWindowMs
|
|
1646
|
-
);
|
|
1647
|
-
worker.crashTimestamps = recentCrashes;
|
|
1648
|
-
if (recentCrashes.length >= this.options.maxCrashesInWindow) {
|
|
1649
|
-
console.error(`[WorkerHost] Worker ${addonId} crashed ${recentCrashes.length} times in ${this.options.crashWindowMs}ms \u2014 not restarting`);
|
|
1650
|
-
return;
|
|
1651
|
-
}
|
|
1652
|
-
const backoff = Math.min(2e3 * (worker.restartCount + 1), 3e4);
|
|
1653
|
-
console.log(`[WorkerHost] Restarting worker ${addonId} in ${backoff}ms`);
|
|
1654
|
-
setTimeout(() => {
|
|
1655
|
-
worker.restartCount++;
|
|
1656
|
-
this.workers.delete(addonId);
|
|
1657
|
-
this.forkWorker(addonId, addonDir, config, storagePaths, dataDir, locationPaths, workerToken).catch((err) => {
|
|
1658
|
-
console.error(`[WorkerHost] Failed to restart worker ${addonId}:`, err);
|
|
1659
|
-
});
|
|
1660
|
-
}, backoff);
|
|
1661
|
-
}
|
|
1662
|
-
async shutdownWorker(addonId, worker) {
|
|
1663
|
-
worker.state = "stopping";
|
|
1664
|
-
worker.process.kill("SIGTERM");
|
|
1665
|
-
await new Promise((resolve3) => {
|
|
1666
|
-
const timeout = setTimeout(() => {
|
|
1667
|
-
console.warn(`[WorkerHost] Worker ${addonId} did not exit within ${this.options.shutdownTimeoutMs}ms \u2014 SIGKILL`);
|
|
1668
|
-
worker.process.kill("SIGKILL");
|
|
1669
|
-
resolve3();
|
|
1670
|
-
}, this.options.shutdownTimeoutMs);
|
|
1671
|
-
worker.process.on("exit", () => {
|
|
1672
|
-
clearTimeout(timeout);
|
|
1673
|
-
resolve3();
|
|
1674
|
-
});
|
|
1675
|
-
});
|
|
1676
|
-
}
|
|
1677
|
-
};
|
|
1678
|
-
|
|
1679
|
-
// src/worker/worker-process-manager.ts
|
|
1680
|
-
var import_node_child_process4 = require("child_process");
|
|
1681
|
-
var WorkerProcessManager = class {
|
|
1682
|
-
constructor(sendToMain) {
|
|
1683
|
-
this.sendToMain = sendToMain;
|
|
1684
|
-
}
|
|
1685
|
-
processes = /* @__PURE__ */ new Map();
|
|
1686
|
-
async spawn(config) {
|
|
1687
|
-
const child = (0, import_node_child_process4.spawn)(config.command, [...config.args ?? []], {
|
|
1688
|
-
cwd: config.cwd,
|
|
1689
|
-
env: config.env ? { ...process.env, ...config.env } : void 0,
|
|
1690
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1691
|
-
});
|
|
1692
|
-
const managed = new ManagedProcess(child, config, this.sendToMain);
|
|
1693
|
-
this.processes.set(child.pid, managed);
|
|
1694
|
-
this.sendToMain({
|
|
1695
|
-
type: "SUB_PROCESS_SPAWNED",
|
|
1696
|
-
pid: child.pid,
|
|
1697
|
-
name: config.name,
|
|
1698
|
-
command: config.command
|
|
1699
|
-
});
|
|
1700
|
-
child.on("exit", (code) => {
|
|
1701
|
-
this.sendToMain({
|
|
1702
|
-
type: "SUB_PROCESS_EXITED",
|
|
1703
|
-
pid: child.pid,
|
|
1704
|
-
code
|
|
1705
|
-
});
|
|
1706
|
-
if (config.autoRestart && managed.restartCount < (config.maxRestarts ?? 3)) {
|
|
1707
|
-
managed.restartCount++;
|
|
1883
|
+
await this.shutdownWorker(addonId, worker);
|
|
1884
|
+
this.workers.delete(addonId);
|
|
1885
|
+
}
|
|
1886
|
+
/** Gracefully shutdown all workers */
|
|
1887
|
+
async shutdownAll() {
|
|
1888
|
+
if (this.heartbeatTimer) {
|
|
1889
|
+
clearInterval(this.heartbeatTimer);
|
|
1890
|
+
this.heartbeatTimer = null;
|
|
1891
|
+
}
|
|
1892
|
+
const shutdowns = [...this.workers.entries()].map(([addonId, worker]) => this.shutdownWorker(addonId, worker));
|
|
1893
|
+
await Promise.allSettled(shutdowns);
|
|
1894
|
+
this.workers.clear();
|
|
1895
|
+
}
|
|
1896
|
+
/** Start heartbeat monitoring — workers now report heartbeat via tRPC, not IPC PING */
|
|
1897
|
+
startHeartbeatMonitoring() {
|
|
1898
|
+
this.heartbeatTimer = setInterval(() => {
|
|
1899
|
+
const now = Date.now();
|
|
1900
|
+
for (const [addonId, worker] of this.workers) {
|
|
1901
|
+
if (worker.state !== "running")
|
|
1902
|
+
continue;
|
|
1903
|
+
if (now - worker.lastHeartbeat > this.options.heartbeatTimeoutMs) {
|
|
1904
|
+
console.warn(`[WorkerHost] Worker ${addonId} heartbeat timeout \u2014 marking unresponsive`);
|
|
1905
|
+
worker.state = "crashed";
|
|
1906
|
+
worker.process.kill("SIGKILL");
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
}, this.options.heartbeatIntervalMs);
|
|
1910
|
+
}
|
|
1911
|
+
/** Update heartbeat timestamp for a worker (called when agent.heartbeat arrives) */
|
|
1912
|
+
updateWorkerHeartbeat(addonId, cpuPercent, memoryRss) {
|
|
1913
|
+
const worker = this.workers.get(addonId);
|
|
1914
|
+
if (!worker)
|
|
1915
|
+
return;
|
|
1916
|
+
worker.lastHeartbeat = Date.now();
|
|
1917
|
+
if (cpuPercent !== void 0)
|
|
1918
|
+
worker.cpuPercent = cpuPercent;
|
|
1919
|
+
if (memoryRss !== void 0)
|
|
1920
|
+
worker.memoryRss = memoryRss;
|
|
1921
|
+
}
|
|
1922
|
+
handleWorkerExit(addonId, code, addonDir, config, storagePaths, dataDir, locationPaths, workerToken) {
|
|
1923
|
+
const worker = this.workers.get(addonId);
|
|
1924
|
+
if (!worker)
|
|
1925
|
+
return;
|
|
1926
|
+
if (worker.state === "stopping") {
|
|
1927
|
+
worker.state = "stopped";
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
worker.state = "crashed";
|
|
1931
|
+
console.error(`[WorkerHost] Worker ${addonId} exited with code ${code}`);
|
|
1932
|
+
const now = Date.now();
|
|
1933
|
+
worker.crashTimestamps.push(now);
|
|
1934
|
+
const recentCrashes = worker.crashTimestamps.filter((t) => now - t < this.options.crashWindowMs);
|
|
1935
|
+
worker.crashTimestamps = recentCrashes;
|
|
1936
|
+
if (recentCrashes.length >= this.options.maxCrashesInWindow) {
|
|
1937
|
+
console.error(`[WorkerHost] Worker ${addonId} crashed ${recentCrashes.length} times in ${this.options.crashWindowMs}ms \u2014 not restarting`);
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
const backoff = Math.min(2e3 * (worker.restartCount + 1), 3e4);
|
|
1941
|
+
console.log(`[WorkerHost] Restarting worker ${addonId} in ${backoff}ms`);
|
|
1708
1942
|
setTimeout(() => {
|
|
1709
|
-
|
|
1943
|
+
worker.restartCount++;
|
|
1944
|
+
this.workers.delete(addonId);
|
|
1945
|
+
this.forkWorker(addonId, addonDir, config, storagePaths, dataDir, locationPaths, workerToken).catch((err) => {
|
|
1946
|
+
console.error(`[WorkerHost] Failed to restart worker ${addonId}:`, err);
|
|
1710
1947
|
});
|
|
1711
|
-
},
|
|
1948
|
+
}, backoff);
|
|
1949
|
+
}
|
|
1950
|
+
async shutdownWorker(addonId, worker) {
|
|
1951
|
+
worker.state = "stopping";
|
|
1952
|
+
worker.process.kill("SIGTERM");
|
|
1953
|
+
await new Promise((resolve) => {
|
|
1954
|
+
const timeout = setTimeout(() => {
|
|
1955
|
+
console.warn(`[WorkerHost] Worker ${addonId} did not exit within ${this.options.shutdownTimeoutMs}ms \u2014 SIGKILL`);
|
|
1956
|
+
worker.process.kill("SIGKILL");
|
|
1957
|
+
resolve();
|
|
1958
|
+
}, this.options.shutdownTimeoutMs);
|
|
1959
|
+
worker.process.on("exit", () => {
|
|
1960
|
+
clearTimeout(timeout);
|
|
1961
|
+
resolve();
|
|
1962
|
+
});
|
|
1963
|
+
});
|
|
1712
1964
|
}
|
|
1713
|
-
});
|
|
1714
|
-
return managed;
|
|
1715
|
-
}
|
|
1716
|
-
listProcesses() {
|
|
1717
|
-
return [...this.processes.values()].map((p) => p.toInfo());
|
|
1718
|
-
}
|
|
1719
|
-
getWorkerStats() {
|
|
1720
|
-
const mem = process.memoryUsage();
|
|
1721
|
-
const cpu = process.cpuUsage();
|
|
1722
|
-
return {
|
|
1723
|
-
pid: process.pid,
|
|
1724
|
-
cpuPercent: (cpu.user + cpu.system) / 1e6,
|
|
1725
|
-
memoryRss: mem.rss,
|
|
1726
|
-
heapUsed: mem.heapUsed,
|
|
1727
|
-
uptimeSeconds: Math.round(process.uptime()),
|
|
1728
|
-
restartCount: 0,
|
|
1729
|
-
state: "running"
|
|
1730
1965
|
};
|
|
1966
|
+
exports2.AddonWorkerHost = AddonWorkerHost2;
|
|
1731
1967
|
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
};
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1968
|
+
});
|
|
1969
|
+
|
|
1970
|
+
// src/worker/worker-process-manager.js
|
|
1971
|
+
var require_worker_process_manager = __commonJS({
|
|
1972
|
+
"src/worker/worker-process-manager.js"(exports2) {
|
|
1973
|
+
"use strict";
|
|
1974
|
+
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
1975
|
+
exports2.WorkerProcessManager = void 0;
|
|
1976
|
+
var node_child_process_1 = require("child_process");
|
|
1977
|
+
var WorkerProcessManager2 = class {
|
|
1978
|
+
sendToMain;
|
|
1979
|
+
processes = /* @__PURE__ */ new Map();
|
|
1980
|
+
constructor(sendToMain) {
|
|
1981
|
+
this.sendToMain = sendToMain;
|
|
1982
|
+
}
|
|
1983
|
+
async spawn(config) {
|
|
1984
|
+
const child = (0, node_child_process_1.spawn)(config.command, [...config.args ?? []], {
|
|
1985
|
+
cwd: config.cwd,
|
|
1986
|
+
env: config.env ? { ...process.env, ...config.env } : void 0,
|
|
1987
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
1988
|
+
});
|
|
1989
|
+
const managed = new ManagedProcess(child, config, this.sendToMain);
|
|
1990
|
+
this.processes.set(child.pid, managed);
|
|
1991
|
+
this.sendToMain({
|
|
1992
|
+
type: "SUB_PROCESS_SPAWNED",
|
|
1993
|
+
pid: child.pid,
|
|
1994
|
+
name: config.name,
|
|
1995
|
+
command: config.command
|
|
1996
|
+
});
|
|
1997
|
+
child.on("exit", (code) => {
|
|
1998
|
+
this.sendToMain({
|
|
1999
|
+
type: "SUB_PROCESS_EXITED",
|
|
2000
|
+
pid: child.pid,
|
|
2001
|
+
code
|
|
2002
|
+
});
|
|
2003
|
+
if (config.autoRestart && managed.restartCount < (config.maxRestarts ?? 3)) {
|
|
2004
|
+
managed.restartCount++;
|
|
2005
|
+
setTimeout(() => {
|
|
2006
|
+
this.spawn(config).catch(() => {
|
|
2007
|
+
});
|
|
2008
|
+
}, 2e3);
|
|
2009
|
+
}
|
|
2010
|
+
});
|
|
2011
|
+
return managed;
|
|
2012
|
+
}
|
|
2013
|
+
listProcesses() {
|
|
2014
|
+
return [...this.processes.values()].map((p) => p.toInfo());
|
|
2015
|
+
}
|
|
2016
|
+
getWorkerStats() {
|
|
2017
|
+
const mem = process.memoryUsage();
|
|
2018
|
+
const cpu = process.cpuUsage();
|
|
2019
|
+
return {
|
|
2020
|
+
pid: process.pid,
|
|
2021
|
+
cpuPercent: (cpu.user + cpu.system) / 1e6,
|
|
2022
|
+
memoryRss: mem.rss,
|
|
2023
|
+
heapUsed: mem.heapUsed,
|
|
2024
|
+
uptimeSeconds: Math.round(process.uptime()),
|
|
2025
|
+
restartCount: 0,
|
|
2026
|
+
state: "running"
|
|
2027
|
+
};
|
|
2028
|
+
}
|
|
2029
|
+
async killAll() {
|
|
2030
|
+
for (const [, proc] of this.processes) {
|
|
2031
|
+
proc.kill("SIGTERM");
|
|
2032
|
+
}
|
|
2033
|
+
this.processes.clear();
|
|
2034
|
+
}
|
|
1767
2035
|
};
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
2036
|
+
exports2.WorkerProcessManager = WorkerProcessManager2;
|
|
2037
|
+
var ManagedProcess = class {
|
|
2038
|
+
child;
|
|
2039
|
+
config;
|
|
2040
|
+
sendToMain;
|
|
2041
|
+
pid;
|
|
2042
|
+
name;
|
|
2043
|
+
restartCount = 0;
|
|
2044
|
+
startedAt = Date.now();
|
|
2045
|
+
exitHandlers = [];
|
|
2046
|
+
errorHandlers = [];
|
|
2047
|
+
constructor(child, config, sendToMain) {
|
|
2048
|
+
this.child = child;
|
|
2049
|
+
this.config = config;
|
|
2050
|
+
this.sendToMain = sendToMain;
|
|
2051
|
+
this.pid = child.pid;
|
|
2052
|
+
this.name = config.name;
|
|
2053
|
+
child.on("exit", (code) => {
|
|
2054
|
+
for (const handler of this.exitHandlers)
|
|
2055
|
+
handler(code);
|
|
2056
|
+
});
|
|
2057
|
+
child.on("error", (err) => {
|
|
2058
|
+
for (const handler of this.errorHandlers)
|
|
2059
|
+
handler(err);
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
getStats() {
|
|
2063
|
+
return {
|
|
2064
|
+
pid: this.pid,
|
|
2065
|
+
cpuPercent: 0,
|
|
2066
|
+
memoryRss: 0,
|
|
2067
|
+
uptimeSeconds: Math.round((Date.now() - this.startedAt) / 1e3),
|
|
2068
|
+
restartCount: this.restartCount,
|
|
2069
|
+
state: this.child.exitCode !== null ? "stopped" : "running"
|
|
2070
|
+
};
|
|
2071
|
+
}
|
|
2072
|
+
write(data) {
|
|
2073
|
+
this.child.stdin?.write(data);
|
|
2074
|
+
}
|
|
2075
|
+
get stdout() {
|
|
2076
|
+
return this.child.stdout;
|
|
2077
|
+
}
|
|
2078
|
+
get stderr() {
|
|
2079
|
+
return this.child.stderr;
|
|
2080
|
+
}
|
|
2081
|
+
kill(signal = "SIGTERM") {
|
|
2082
|
+
this.child.kill(signal);
|
|
2083
|
+
}
|
|
2084
|
+
wait() {
|
|
2085
|
+
if (this.child.exitCode !== null) {
|
|
2086
|
+
return Promise.resolve({ code: this.child.exitCode, signal: null });
|
|
2087
|
+
}
|
|
2088
|
+
return new Promise((resolve) => {
|
|
2089
|
+
this.child.on("exit", (code, signal) => {
|
|
2090
|
+
resolve({ code, signal });
|
|
2091
|
+
});
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
onExit(handler) {
|
|
2095
|
+
this.exitHandlers.push(handler);
|
|
2096
|
+
}
|
|
2097
|
+
onError(handler) {
|
|
2098
|
+
this.errorHandlers.push(handler);
|
|
2099
|
+
}
|
|
2100
|
+
toInfo() {
|
|
2101
|
+
return {
|
|
2102
|
+
pid: this.pid,
|
|
2103
|
+
name: this.name,
|
|
2104
|
+
command: this.config.command,
|
|
2105
|
+
state: this.child.exitCode !== null ? "stopped" : "running",
|
|
2106
|
+
cpuPercent: 0,
|
|
2107
|
+
memoryRss: 0,
|
|
2108
|
+
uptimeSeconds: Math.round((Date.now() - this.startedAt) / 1e3)
|
|
2109
|
+
};
|
|
2110
|
+
}
|
|
1806
2111
|
};
|
|
1807
2112
|
}
|
|
1808
|
-
};
|
|
2113
|
+
});
|
|
2114
|
+
|
|
2115
|
+
// src/index.ts
|
|
2116
|
+
var src_exports = {};
|
|
2117
|
+
__export(src_exports, {
|
|
2118
|
+
AddonEngineManager: () => import_addon_engine_manager.AddonEngineManager,
|
|
2119
|
+
AddonInstaller: () => import_addon_installer.AddonInstaller,
|
|
2120
|
+
AddonLoader: () => import_addon_loader.AddonLoader,
|
|
2121
|
+
AddonWorkerHost: () => import_addon_worker_host.AddonWorkerHost,
|
|
2122
|
+
CapabilityRegistry: () => import_capability_registry.CapabilityRegistry,
|
|
2123
|
+
ConfigManager: () => import_config_manager.ConfigManager,
|
|
2124
|
+
DEFAULT_DATA_PATH: () => import_config_schema.DEFAULT_DATA_PATH,
|
|
2125
|
+
INFRA_CAPABILITIES: () => import_infra_capabilities.INFRA_CAPABILITIES,
|
|
2126
|
+
RUNTIME_DEFAULTS: () => import_config_schema.RUNTIME_DEFAULTS,
|
|
2127
|
+
WorkerProcessManager: () => import_worker_process_manager.WorkerProcessManager,
|
|
2128
|
+
bootstrapSchema: () => import_config_schema.bootstrapSchema,
|
|
2129
|
+
copyDirRecursive: () => import_fs_utils.copyDirRecursive,
|
|
2130
|
+
copyExtraFileDirs: () => import_fs_utils.copyExtraFileDirs,
|
|
2131
|
+
detectWorkspacePackagesDir: () => import_workspace_detect.detectWorkspacePackagesDir,
|
|
2132
|
+
ensureDir: () => import_fs_utils.ensureDir,
|
|
2133
|
+
ensureLibraryBuilt: () => import_fs_utils.ensureLibraryBuilt,
|
|
2134
|
+
installPackageFromNpmSync: () => import_fs_utils.installPackageFromNpmSync,
|
|
2135
|
+
isInfraCapability: () => import_infra_capabilities.isInfraCapability,
|
|
2136
|
+
isSourceNewer: () => import_fs_utils.isSourceNewer,
|
|
2137
|
+
stripCamstackDeps: () => import_fs_utils.stripCamstackDeps,
|
|
2138
|
+
symlinkAddonsToNodeModules: () => import_fs_utils.symlinkAddonsToNodeModules
|
|
2139
|
+
});
|
|
2140
|
+
module.exports = __toCommonJS(src_exports);
|
|
2141
|
+
var import_addon_loader = __toESM(require_addon_loader());
|
|
2142
|
+
var import_addon_engine_manager = __toESM(require_addon_engine_manager());
|
|
2143
|
+
var import_addon_installer = __toESM(require_addon_installer());
|
|
2144
|
+
var import_fs_utils = __toESM(require_fs_utils());
|
|
2145
|
+
var import_workspace_detect = __toESM(require_workspace_detect());
|
|
2146
|
+
var import_capability_registry = __toESM(require_capability_registry());
|
|
2147
|
+
var import_infra_capabilities = __toESM(require_infra_capabilities());
|
|
2148
|
+
var import_config_manager = __toESM(require_config_manager());
|
|
2149
|
+
var import_config_schema = __toESM(require_config_schema());
|
|
2150
|
+
var import_addon_worker_host = __toESM(require_addon_worker_host());
|
|
2151
|
+
var import_worker_process_manager = __toESM(require_worker_process_manager());
|
|
1809
2152
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1810
2153
|
0 && (module.exports = {
|
|
1811
2154
|
AddonEngineManager,
|