@ubercode/channelvault 0.2.0 → 0.3.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.md +31 -11
- package/dist/cli.js +753 -171
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +130 -19
- package/dist/index.js.map +1 -1
- package/docs/roadmap.md +7 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -145,6 +145,8 @@ interface MirthClientExt extends MirthClient {
|
|
|
145
145
|
putServerConfigurationXml(xml: string, options?: PutServerConfigurationOptions): Promise<void>;
|
|
146
146
|
/** GET /channels/{id}, unwrapped; includes the tag and dependency data the server configuration omits. */
|
|
147
147
|
getChannel(id: string): Promise<Record<string, unknown> | null>;
|
|
148
|
+
/** Standalone templates remain readable after being removed from a library. */
|
|
149
|
+
getCodeTemplate(id: string): Promise<Record<string, unknown> | null>;
|
|
148
150
|
/** PUT /channels/{id}; creates the channel if the id is new. */
|
|
149
151
|
putChannel(channel: Record<string, unknown>): Promise<void>;
|
|
150
152
|
/** Release pooled connections so the process can exit promptly. */
|
package/dist/index.js
CHANGED
|
@@ -252,7 +252,7 @@ var XmlConfigAdapter = class {
|
|
|
252
252
|
// src/explode/index.ts
|
|
253
253
|
import { AsyncLocalStorage } from "async_hooks";
|
|
254
254
|
import { existsSync } from "fs";
|
|
255
|
-
import { lstat, mkdir, readFile as readFile2, realpath, writeFile } from "fs/promises";
|
|
255
|
+
import { lstat, mkdir, readFile as readFile2, readdir, realpath, writeFile } from "fs/promises";
|
|
256
256
|
import * as path from "path";
|
|
257
257
|
|
|
258
258
|
// src/json.ts
|
|
@@ -264,9 +264,6 @@ function parseJson(text, file) {
|
|
|
264
264
|
throw new Error(`${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
|
-
async function readJson(file) {
|
|
268
|
-
return parseJson(await readFile(file, "utf8"), file);
|
|
269
|
-
}
|
|
270
267
|
|
|
271
268
|
// src/explode/index.ts
|
|
272
269
|
function asArray(x) {
|
|
@@ -625,12 +622,17 @@ async function splitCollection(parent, containerKey, elementKey, root, serverDir
|
|
|
625
622
|
const wasArray = Array.isArray(raw);
|
|
626
623
|
const elements = asArray(raw);
|
|
627
624
|
const refs = [];
|
|
625
|
+
const byName = (el) => `${slug(el["name"]).toLowerCase()}\0${String(el["id"] ?? "")}`;
|
|
626
|
+
const slugs = /* @__PURE__ */ new Map();
|
|
627
|
+
for (const el of elements.filter(isPlainObject).sort((a, b) => byName(a) < byName(b) ? -1 : byName(a) > byName(b) ? 1 : 0)) {
|
|
628
|
+
slugs.set(el, uniqueSlug(slug(el["name"]), usedSlugs));
|
|
629
|
+
}
|
|
628
630
|
for (const el of elements) {
|
|
629
631
|
if (!isPlainObject(el)) {
|
|
630
632
|
refs.push(el);
|
|
631
633
|
continue;
|
|
632
634
|
}
|
|
633
|
-
const slugName =
|
|
635
|
+
const slugName = slugs.get(el);
|
|
634
636
|
const { jsonFile, resourceDir } = targetFor(el["name"], slugName);
|
|
635
637
|
const transformed = await explodeResource(el, resourceDir);
|
|
636
638
|
await writeJson(jsonFile, transformed);
|
|
@@ -822,12 +824,13 @@ async function readWithinRoot(root, target, rel) {
|
|
|
822
824
|
}
|
|
823
825
|
return readFile2(realTarget, "utf8");
|
|
824
826
|
}
|
|
825
|
-
|
|
827
|
+
var pathKey = (file) => process.platform === "win32" ? file.toLowerCase() : file;
|
|
828
|
+
async function resolveMarkers(value, jsonDir, root, referenced) {
|
|
826
829
|
if (Array.isArray(value)) {
|
|
827
830
|
const out = [];
|
|
828
831
|
for (const item of value) {
|
|
829
832
|
if (isRefMarker(item) && !existsSync(resolveWithinRoot(root, jsonDir, item["@ref"]))) continue;
|
|
830
|
-
out.push(await resolveMarkers(item, jsonDir, root));
|
|
833
|
+
out.push(await resolveMarkers(item, jsonDir, root, referenced));
|
|
831
834
|
}
|
|
832
835
|
return out;
|
|
833
836
|
}
|
|
@@ -838,25 +841,81 @@ async function resolveMarkers(value, jsonDir, root) {
|
|
|
838
841
|
if (isRefMarker(value)) {
|
|
839
842
|
const target = resolveWithinRoot(root, jsonDir, value["@ref"]);
|
|
840
843
|
const text = await readWithinRoot(root, target, value["@ref"]);
|
|
844
|
+
referenced.add(pathKey(await realpath(target)));
|
|
841
845
|
const parsed = parseJson(text, target);
|
|
842
|
-
return resolveMarkers(parsed, path.dirname(target), root);
|
|
846
|
+
return resolveMarkers(parsed, path.dirname(target), root, referenced);
|
|
843
847
|
}
|
|
844
848
|
if (isPlainObject(value)) {
|
|
845
849
|
const out = {};
|
|
846
850
|
for (const [k, v] of Object.entries(value)) {
|
|
847
851
|
if (isRefMarker(v) && !existsSync(resolveWithinRoot(root, jsonDir, v["@ref"]))) continue;
|
|
848
|
-
out[k] = await resolveMarkers(v, jsonDir, root);
|
|
852
|
+
out[k] = await resolveMarkers(v, jsonDir, root, referenced);
|
|
849
853
|
}
|
|
850
854
|
return out;
|
|
851
855
|
}
|
|
852
856
|
return value;
|
|
853
857
|
}
|
|
858
|
+
async function discoverResources(config, root, referenced) {
|
|
859
|
+
const realRoot = await realpath(root);
|
|
860
|
+
const contained = async (file) => {
|
|
861
|
+
const real = await realpath(file);
|
|
862
|
+
if (real !== realRoot && !real.startsWith(realRoot + path.sep)) throw new Error(`resource path escapes the working tree through a link: ${file}`);
|
|
863
|
+
return real;
|
|
864
|
+
};
|
|
865
|
+
const layouts = [
|
|
866
|
+
{ folder: "channels", filename: "channel.json", container: "channels", member: "channel" },
|
|
867
|
+
{ folder: "codeTemplates", filename: "library.json", container: "codeTemplateLibraries", member: "codeTemplateLibrary" },
|
|
868
|
+
{ folder: "channelGroups", filename: null, container: "channelGroups", member: "channelGroup" }
|
|
869
|
+
];
|
|
870
|
+
for (const layout of layouts) {
|
|
871
|
+
const dir = path.join(root, layout.folder);
|
|
872
|
+
if (!existsSync(dir)) continue;
|
|
873
|
+
await contained(dir);
|
|
874
|
+
const entries = (await readdir(dir, { withFileTypes: true })).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
875
|
+
const additions = [];
|
|
876
|
+
for (const entry of entries) {
|
|
877
|
+
let file = path.join(dir, entry.name);
|
|
878
|
+
if (layout.filename) {
|
|
879
|
+
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
880
|
+
await contained(file);
|
|
881
|
+
file = path.join(file, layout.filename);
|
|
882
|
+
if (!existsSync(file)) continue;
|
|
883
|
+
} else if (!entry.name.endsWith(".json")) continue;
|
|
884
|
+
const real = await contained(file);
|
|
885
|
+
if (referenced.has(pathKey(real))) continue;
|
|
886
|
+
const parsed = parseJson(await readWithinRoot(root, file, file), file);
|
|
887
|
+
const resource = await resolveMarkers(parsed, path.dirname(file), root, referenced);
|
|
888
|
+
if (!isPlainObject(resource) || typeof resource["id"] !== "string" || !resource["id"].trim() || typeof resource["name"] !== "string" || !resource["name"].trim()) {
|
|
889
|
+
throw new Error(`new resource ${file} needs a nonempty id and name`);
|
|
890
|
+
}
|
|
891
|
+
additions.push(resource);
|
|
892
|
+
referenced.add(pathKey(real));
|
|
893
|
+
}
|
|
894
|
+
if (!additions.length) continue;
|
|
895
|
+
const container = isPlainObject(config[layout.container]) ? config[layout.container] : {};
|
|
896
|
+
const old = container[layout.member];
|
|
897
|
+
const members = [...old == null || old === "" ? [] : asArray(old), ...additions];
|
|
898
|
+
const ids = /* @__PURE__ */ new Set();
|
|
899
|
+
for (const member of members.filter(isPlainObject)) {
|
|
900
|
+
const id = String(member["id"]);
|
|
901
|
+
if (ids.has(id)) throw new Error(`duplicate ${layout.member} id ${id}: a copied resource keeps its source's id; give each copy a new UUID`);
|
|
902
|
+
ids.add(id);
|
|
903
|
+
}
|
|
904
|
+
config[layout.container] = { ...container, [layout.member]: members.length === 1 ? members[0] : members };
|
|
905
|
+
}
|
|
906
|
+
}
|
|
854
907
|
async function implode(opts) {
|
|
855
908
|
const root = path.resolve(opts.root);
|
|
909
|
+
if (existsSync(path.join(root, ".channelvault-transaction.json"))) {
|
|
910
|
+
throw new Error("working tree has a pending replacement; run a channelvault CLI command with the original --dotenv selection to recover it before reading");
|
|
911
|
+
}
|
|
856
912
|
const serverDir = path.join(root, "server");
|
|
857
913
|
const configPath = path.join(serverDir, "configuration.json");
|
|
858
|
-
const parsed = await
|
|
859
|
-
const
|
|
914
|
+
const parsed = parseJson(await readWithinRoot(root, configPath, configPath), configPath);
|
|
915
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
916
|
+
const resolved = await resolveMarkers(parsed, serverDir, root, referenced);
|
|
917
|
+
if (!isPlainObject(resolved)) throw new Error("server/configuration.json must hold a configuration object");
|
|
918
|
+
await discoverResources(resolved, root, referenced);
|
|
860
919
|
return resolved;
|
|
861
920
|
}
|
|
862
921
|
function createExplodeEngine() {
|
|
@@ -1037,6 +1096,41 @@ function redactSecretsInText(text) {
|
|
|
1037
1096
|
return out + value.slice(at);
|
|
1038
1097
|
}
|
|
1039
1098
|
|
|
1099
|
+
// src/client/validate.ts
|
|
1100
|
+
var object = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1101
|
+
function liveConfiguration(payload) {
|
|
1102
|
+
const bad = (detail) => {
|
|
1103
|
+
throw new Error(`invalid server configuration response: ${detail}; nothing should be written from this snapshot`);
|
|
1104
|
+
};
|
|
1105
|
+
if (!object(payload) || Object.keys(payload).length !== 1 || !("serverConfiguration" in payload)) bad("expected the serverConfiguration wrapper");
|
|
1106
|
+
const config = payload["serverConfiguration"];
|
|
1107
|
+
if (!object(config)) bad("expected a configuration object");
|
|
1108
|
+
const c = config;
|
|
1109
|
+
if (typeof c["@version"] !== "string" || !/^\d+\.\d+(?:\.\d+)*(?:[-+][A-Za-z0-9.-]+)?$/.test(c["@version"])) bad("missing or invalid engine version");
|
|
1110
|
+
if (!("channels" in c)) bad("missing channels collection");
|
|
1111
|
+
const collection = (container, key, where) => {
|
|
1112
|
+
if (container == null || container === "") return [];
|
|
1113
|
+
if (!object(container)) bad(`invalid ${where} collection`);
|
|
1114
|
+
if (!(key in container) && Object.keys(container).some((k) => !k.startsWith("@"))) bad(`missing resource list in ${where}`);
|
|
1115
|
+
const raw = container[key];
|
|
1116
|
+
if (raw == null || raw === "") return [];
|
|
1117
|
+
const items = Array.isArray(raw) ? raw : [raw];
|
|
1118
|
+
for (const item of items) {
|
|
1119
|
+
if (!object(item) || typeof item.id !== "string" || !item.id.trim() || typeof item.name !== "string" || !item.name.trim()) bad(`invalid resource in ${where}`);
|
|
1120
|
+
if ("revision" in item && (!Number.isSafeInteger(item.revision) || Number(item.revision) < 0)) bad(`invalid revision in ${where}`);
|
|
1121
|
+
}
|
|
1122
|
+
return items;
|
|
1123
|
+
};
|
|
1124
|
+
collection(c.channels, "channel", "channels");
|
|
1125
|
+
if ("codeTemplateLibraries" in c) {
|
|
1126
|
+
for (const library of collection(c.codeTemplateLibraries, "codeTemplateLibrary", "code template libraries")) {
|
|
1127
|
+
if ("codeTemplates" in library) collection(library.codeTemplates, "codeTemplate", "code templates");
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
if ("channelGroups" in c) collection(c.channelGroups, "channelGroup", "channel groups");
|
|
1131
|
+
return c;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1040
1134
|
// src/client/index.ts
|
|
1041
1135
|
function connectionError(url, err) {
|
|
1042
1136
|
let cause = err instanceof Error ? err.cause : void 0;
|
|
@@ -1142,16 +1236,20 @@ var MirthClientImpl = class {
|
|
|
1142
1236
|
/**
|
|
1143
1237
|
* GET /server/configuration -> canonical config.
|
|
1144
1238
|
*
|
|
1145
|
-
* Requests JSON
|
|
1146
|
-
*
|
|
1147
|
-
* a multi-key object is returned as-is).
|
|
1239
|
+
* Requests JSON and validates the envelope and core resource collections
|
|
1240
|
+
* before this snapshot can be used to replace a tree or plan server writes.
|
|
1148
1241
|
*/
|
|
1149
1242
|
async getServerConfiguration() {
|
|
1150
1243
|
const response = await this.request("GET", "/server/configuration", {
|
|
1151
1244
|
headers: { Accept: "application/json" }
|
|
1152
1245
|
});
|
|
1153
|
-
|
|
1154
|
-
|
|
1246
|
+
let json;
|
|
1247
|
+
try {
|
|
1248
|
+
json = await response.json();
|
|
1249
|
+
} catch {
|
|
1250
|
+
throw new Error("invalid server configuration response: expected JSON");
|
|
1251
|
+
}
|
|
1252
|
+
return liveConfiguration(json);
|
|
1155
1253
|
}
|
|
1156
1254
|
/**
|
|
1157
1255
|
* PUT /server/configuration <- canonical config.
|
|
@@ -1179,7 +1277,9 @@ var MirthClientImpl = class {
|
|
|
1179
1277
|
}
|
|
1180
1278
|
async getServerId() {
|
|
1181
1279
|
const response = await this.request("GET", "/server/id", { headers: { Accept: "text/plain" } });
|
|
1182
|
-
|
|
1280
|
+
const id = (await response.text()).trim();
|
|
1281
|
+
if (!/^[^\p{Cc}\p{Cf}]{1,255}$/u.test(id)) throw new Error("the server returned an invalid installation ID");
|
|
1282
|
+
return id;
|
|
1183
1283
|
}
|
|
1184
1284
|
async getServerConfigurationXml() {
|
|
1185
1285
|
const response = await this.request("GET", "/server/configuration", { headers: { Accept: "application/xml" } });
|
|
@@ -1196,12 +1296,23 @@ var MirthClientImpl = class {
|
|
|
1196
1296
|
// Override is on: Mirth does not reject a stale revision anyway (verified on
|
|
1197
1297
|
// 4.5.2), so conflict detection happens in the push planner instead.
|
|
1198
1298
|
async getChannel(id) {
|
|
1199
|
-
|
|
1299
|
+
return this.getResource(`/channels/${encodeURIComponent(id)}`);
|
|
1300
|
+
}
|
|
1301
|
+
async getCodeTemplate(id) {
|
|
1302
|
+
return this.getResource(`/codeTemplates/${encodeURIComponent(id)}`);
|
|
1303
|
+
}
|
|
1304
|
+
async getResource(resourcePath) {
|
|
1305
|
+
const response = await this.request("GET", resourcePath, {
|
|
1200
1306
|
headers: { Accept: "application/json" }
|
|
1307
|
+
}).catch((err) => {
|
|
1308
|
+
if (err.status === 404) return null;
|
|
1309
|
+
throw err;
|
|
1201
1310
|
});
|
|
1311
|
+
if (!response) return null;
|
|
1202
1312
|
const text = await response.text();
|
|
1203
1313
|
if (text.trim() === "") return null;
|
|
1204
|
-
|
|
1314
|
+
const parsed = JSON.parse(text);
|
|
1315
|
+
return parsed === null ? null : unwrapSingleKey(parsed);
|
|
1205
1316
|
}
|
|
1206
1317
|
async close() {
|
|
1207
1318
|
await this.dispatcher.close();
|