@ubercode/channelvault 0.2.1 → 0.3.0-rc.2
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 +38 -0
- package/README.md +26 -7
- package/dist/cli.js +836 -190
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +124 -18
- package/dist/index.js.map +1 -1
- 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) {
|
|
@@ -827,12 +824,13 @@ async function readWithinRoot(root, target, rel) {
|
|
|
827
824
|
}
|
|
828
825
|
return readFile2(realTarget, "utf8");
|
|
829
826
|
}
|
|
830
|
-
|
|
827
|
+
var pathKey = (file) => process.platform === "win32" ? file.toLowerCase() : file;
|
|
828
|
+
async function resolveMarkers(value, jsonDir, root, referenced) {
|
|
831
829
|
if (Array.isArray(value)) {
|
|
832
830
|
const out = [];
|
|
833
831
|
for (const item of value) {
|
|
834
832
|
if (isRefMarker(item) && !existsSync(resolveWithinRoot(root, jsonDir, item["@ref"]))) continue;
|
|
835
|
-
out.push(await resolveMarkers(item, jsonDir, root));
|
|
833
|
+
out.push(await resolveMarkers(item, jsonDir, root, referenced));
|
|
836
834
|
}
|
|
837
835
|
return out;
|
|
838
836
|
}
|
|
@@ -843,25 +841,81 @@ async function resolveMarkers(value, jsonDir, root) {
|
|
|
843
841
|
if (isRefMarker(value)) {
|
|
844
842
|
const target = resolveWithinRoot(root, jsonDir, value["@ref"]);
|
|
845
843
|
const text = await readWithinRoot(root, target, value["@ref"]);
|
|
844
|
+
referenced.add(pathKey(await realpath(target)));
|
|
846
845
|
const parsed = parseJson(text, target);
|
|
847
|
-
return resolveMarkers(parsed, path.dirname(target), root);
|
|
846
|
+
return resolveMarkers(parsed, path.dirname(target), root, referenced);
|
|
848
847
|
}
|
|
849
848
|
if (isPlainObject(value)) {
|
|
850
849
|
const out = {};
|
|
851
850
|
for (const [k, v] of Object.entries(value)) {
|
|
852
851
|
if (isRefMarker(v) && !existsSync(resolveWithinRoot(root, jsonDir, v["@ref"]))) continue;
|
|
853
|
-
out[k] = await resolveMarkers(v, jsonDir, root);
|
|
852
|
+
out[k] = await resolveMarkers(v, jsonDir, root, referenced);
|
|
854
853
|
}
|
|
855
854
|
return out;
|
|
856
855
|
}
|
|
857
856
|
return value;
|
|
858
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
|
+
}
|
|
859
907
|
async function implode(opts) {
|
|
860
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
|
+
}
|
|
861
912
|
const serverDir = path.join(root, "server");
|
|
862
913
|
const configPath = path.join(serverDir, "configuration.json");
|
|
863
|
-
const parsed = await
|
|
864
|
-
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);
|
|
865
919
|
return resolved;
|
|
866
920
|
}
|
|
867
921
|
function createExplodeEngine() {
|
|
@@ -1042,6 +1096,41 @@ function redactSecretsInText(text) {
|
|
|
1042
1096
|
return out + value.slice(at);
|
|
1043
1097
|
}
|
|
1044
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
|
+
|
|
1045
1134
|
// src/client/index.ts
|
|
1046
1135
|
function connectionError(url, err) {
|
|
1047
1136
|
let cause = err instanceof Error ? err.cause : void 0;
|
|
@@ -1147,16 +1236,20 @@ var MirthClientImpl = class {
|
|
|
1147
1236
|
/**
|
|
1148
1237
|
* GET /server/configuration -> canonical config.
|
|
1149
1238
|
*
|
|
1150
|
-
* Requests JSON
|
|
1151
|
-
*
|
|
1152
|
-
* 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.
|
|
1153
1241
|
*/
|
|
1154
1242
|
async getServerConfiguration() {
|
|
1155
1243
|
const response = await this.request("GET", "/server/configuration", {
|
|
1156
1244
|
headers: { Accept: "application/json" }
|
|
1157
1245
|
});
|
|
1158
|
-
|
|
1159
|
-
|
|
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);
|
|
1160
1253
|
}
|
|
1161
1254
|
/**
|
|
1162
1255
|
* PUT /server/configuration <- canonical config.
|
|
@@ -1184,7 +1277,9 @@ var MirthClientImpl = class {
|
|
|
1184
1277
|
}
|
|
1185
1278
|
async getServerId() {
|
|
1186
1279
|
const response = await this.request("GET", "/server/id", { headers: { Accept: "text/plain" } });
|
|
1187
|
-
|
|
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;
|
|
1188
1283
|
}
|
|
1189
1284
|
async getServerConfigurationXml() {
|
|
1190
1285
|
const response = await this.request("GET", "/server/configuration", { headers: { Accept: "application/xml" } });
|
|
@@ -1201,12 +1296,23 @@ var MirthClientImpl = class {
|
|
|
1201
1296
|
// Override is on: Mirth does not reject a stale revision anyway (verified on
|
|
1202
1297
|
// 4.5.2), so conflict detection happens in the push planner instead.
|
|
1203
1298
|
async getChannel(id) {
|
|
1204
|
-
|
|
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, {
|
|
1205
1306
|
headers: { Accept: "application/json" }
|
|
1307
|
+
}).catch((err) => {
|
|
1308
|
+
if (err.status === 404) return null;
|
|
1309
|
+
throw err;
|
|
1206
1310
|
});
|
|
1311
|
+
if (!response) return null;
|
|
1207
1312
|
const text = await response.text();
|
|
1208
1313
|
if (text.trim() === "") return null;
|
|
1209
|
-
|
|
1314
|
+
const parsed = JSON.parse(text);
|
|
1315
|
+
return parsed === null ? null : unwrapSingleKey(parsed);
|
|
1210
1316
|
}
|
|
1211
1317
|
async close() {
|
|
1212
1318
|
await this.dispatcher.close();
|