@tdacorp/hyle-sdk 0.21.0 → 0.22.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.
Files changed (58) hide show
  1. package/README.md +110 -18
  2. package/dist/check-routes-bin.js +154 -42
  3. package/dist/{chunk-X2TLYM7Y.js → chunk-7T5EEJHK.js} +1 -1
  4. package/dist/chunk-DUPT6XLB.js +57 -0
  5. package/dist/{chunk-56Y24XBV.js → chunk-IW3TUD4G.js} +4 -3
  6. package/dist/{chunk-75JFLML4.js → chunk-KU7LWK4E.js} +75 -12
  7. package/dist/{chunk-K5VDLROQ.js → chunk-SFFBZFEN.js} +34 -1
  8. package/dist/{chunk-HJX57NLI.js → chunk-ZIW6FDU2.js} +246 -225
  9. package/dist/{client-LwzHwqpM.d.ts → client-BGjmEWhN.d.cts} +7 -1
  10. package/dist/{client-41fU6OY2.d.cts → client-D0LTpr3Y.d.ts} +7 -1
  11. package/dist/code-copy.cjs +4 -2
  12. package/dist/code-copy.d.cts +5 -2
  13. package/dist/code-copy.d.ts +5 -2
  14. package/dist/code-copy.js +5 -3
  15. package/dist/comments.cjs +11 -6
  16. package/dist/comments.js +11 -6
  17. package/dist/docs-enhance.cjs +7 -4
  18. package/dist/docs-enhance.d.cts +5 -3
  19. package/dist/docs-enhance.d.ts +5 -3
  20. package/dist/docs-enhance.js +8 -5
  21. package/dist/{embed-BUFRQboD.d.ts → embed-CbtsbZs3.d.cts} +4 -2
  22. package/dist/{embed-CYt-q13P.d.cts → embed-Cf_2ZR5x.d.ts} +4 -2
  23. package/dist/html.cjs +11 -6
  24. package/dist/html.d.cts +1 -1
  25. package/dist/html.d.ts +1 -1
  26. package/dist/html.js +2 -2
  27. package/dist/index.cjs +65 -12
  28. package/dist/index.d.cts +13 -11
  29. package/dist/index.d.ts +13 -11
  30. package/dist/index.js +2 -2
  31. package/dist/mermaid-diagram.cjs +46 -6
  32. package/dist/mermaid-diagram.js +15 -6
  33. package/dist/mermaid-image.cjs +4 -1
  34. package/dist/mermaid-image.js +1 -1
  35. package/dist/next.cjs +410 -241
  36. package/dist/next.d.cts +39 -17
  37. package/dist/next.d.ts +39 -17
  38. package/dist/next.js +116 -32
  39. package/dist/print-coordinator.js +1 -1
  40. package/dist/react.cjs +230 -197
  41. package/dist/react.d.cts +17 -6
  42. package/dist/react.d.ts +17 -6
  43. package/dist/react.js +5 -3
  44. package/dist/{resolve-eI1muCKL.d.cts → resolve-BnIv7aB7.d.cts} +1 -1
  45. package/dist/{resolve-BjAK0k_Y.d.ts → resolve-BysHtZ_i.d.ts} +1 -1
  46. package/dist/{sanitize-ijiWjEm9.d.cts → sanitize-BpY_AdZh.d.cts} +1 -1
  47. package/dist/{sanitize-ijiWjEm9.d.ts → sanitize-BpY_AdZh.d.ts} +1 -1
  48. package/dist/styles.css +4 -4
  49. package/dist/toc-spy.cjs +2 -1
  50. package/dist/toc-spy.js +2 -1
  51. package/dist/{types-3F0wqD-o.d.cts → types-D09Z072G.d.cts} +32 -1
  52. package/dist/{types-3F0wqD-o.d.ts → types-D09Z072G.d.ts} +32 -1
  53. package/dist/use-cache.cjs +53 -3
  54. package/dist/use-cache.d.cts +3 -3
  55. package/dist/use-cache.d.ts +3 -3
  56. package/dist/use-cache.js +3 -3
  57. package/package.json +3 -3
  58. package/dist/chunk-67IOIAVW.js +0 -35
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createHyleClient,
3
3
  isValidCacheTag
4
- } from "./chunk-75JFLML4.js";
4
+ } from "./chunk-KU7LWK4E.js";
5
5
 
6
6
  // src/next/resolve.ts
7
7
  var DEFAULT_REVALIDATE = 86400;
@@ -0,0 +1,57 @@
1
+ // src/asset-path.ts
2
+ var ASSET_ROUTE_PREFIX = "/api/hyle/assets";
3
+ var ASSET_RESERVED_PATH = "/api/hyle/*";
4
+ var UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
5
+ var EXTENSIONS = "png|jpg|gif|webp|svg|pdf|mp4|webm|mp3|ogg|woff2";
6
+ var ASSET_FILE = new RegExp(`^${UUID}(?:\\.(?:${EXTENSIONS}))?$`);
7
+ var THEME_FILE = /^theme-([0-9a-f]{16})-([0-9a-f]{64})\.css$/;
8
+ var THEME_FONT = new RegExp(`^/assets/${UUID}\\.woff2$`);
9
+ function themeStylesheetHref({ scope, hash }) {
10
+ const file = `theme-${scope}-${hash}.css`;
11
+ return THEME_FILE.test(file) ? `${ASSET_ROUTE_PREFIX}/${file}` : void 0;
12
+ }
13
+ function themeFontHrefs(preload) {
14
+ return preload.flatMap((src) => typeof src === "string" && THEME_FONT.test(src) ? [hostAssetSrc(src)] : []);
15
+ }
16
+ var PREVIEW_TOKEN_SOURCE = "hyle_pv_[A-Za-z0-9_-]{43}";
17
+ var PREVIEW_TOKEN = new RegExp(`^${PREVIEW_TOKEN_SOURCE}$`);
18
+ var PREVIEW_HEADER = "cp-preview";
19
+ var PREVIEW_PARAM = "hyle_preview";
20
+ var PREVIEW_QUERY_SOURCE = `\\?${PREVIEW_PARAM}=${PREVIEW_TOKEN_SOURCE}`;
21
+ var HYLE_ASSET_URL = new RegExp(`^/assets/(${UUID}(?:\\.(?:${EXTENSIONS}))?)(${PREVIEW_QUERY_SOURCE})?$`, "i");
22
+ function hostAssetSrc(src) {
23
+ const [, file, query = ""] = HYLE_ASSET_URL.exec(src) ?? [];
24
+ return file ? `${ASSET_ROUTE_PREFIX}/${file.toLowerCase()}${query}` : src;
25
+ }
26
+ function hostAssetHtml(html) {
27
+ return html.replace(new RegExp(`(\\ssrc=")(/assets/${UUID}(?:\\.(?:${EXTENSIONS}))?)(")`, "gi"), (_m, open, url, close) => `${open}${hostAssetSrc(url)}${close}`);
28
+ }
29
+ var ASSET_CONTENT_TYPES = /* @__PURE__ */ new Set([
30
+ "image/png",
31
+ "image/jpeg",
32
+ "image/gif",
33
+ "image/webp",
34
+ "image/svg+xml",
35
+ "application/pdf",
36
+ "video/mp4",
37
+ "video/webm",
38
+ "audio/mpeg",
39
+ "audio/ogg",
40
+ "font/woff2"
41
+ ]);
42
+
43
+ export {
44
+ ASSET_ROUTE_PREFIX,
45
+ ASSET_RESERVED_PATH,
46
+ ASSET_FILE,
47
+ THEME_FILE,
48
+ themeStylesheetHref,
49
+ themeFontHrefs,
50
+ PREVIEW_TOKEN,
51
+ PREVIEW_HEADER,
52
+ PREVIEW_PARAM,
53
+ PREVIEW_QUERY_SOURCE,
54
+ hostAssetSrc,
55
+ hostAssetHtml,
56
+ ASSET_CONTENT_TYPES
57
+ };
@@ -1,6 +1,7 @@
1
1
  import {
2
+ PREVIEW_QUERY_SOURCE,
2
3
  hostAssetSrc
3
- } from "./chunk-67IOIAVW.js";
4
+ } from "./chunk-DUPT6XLB.js";
4
5
 
5
6
  // src/react/sanitize.ts
6
7
  var HastLimitError = class extends Error {
@@ -77,7 +78,7 @@ var ENTITIES = { "&#x3c;": "<", "&#x26;": "&", "&lt;": "<", "&gt;": ">", "&amp;"
77
78
  function mermaidSource(srcDoc) {
78
79
  return MERMAID_SRCDOC.exec(srcDoc)?.[1]?.replace(/&(?:#x3c|#x26|#x27|lt|gt|amp|quot);/gi, (e) => ENTITIES[e.toLowerCase()]);
79
80
  }
80
- var RENDER_REF = /^(\/assets\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.svg) ([1-9]\d{0,5}) ([1-9]\d{0,5})$/;
81
+ var RENDER_REF = new RegExp(`^(/assets/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\.svg(?:${PREVIEW_QUERY_SOURCE})?) ([1-9]\\d{0,5}) ([1-9]\\d{0,5})$`);
81
82
  var isRenderRef = matches(RENDER_REF);
82
83
  function diagramRender(value) {
83
84
  const m = typeof value === "string" ? RENDER_REF.exec(value) : null;
@@ -214,7 +215,7 @@ function sanitizeHast(tree, title, description) {
214
215
  const [first] = elements;
215
216
  const skip = title !== void 0 && isObject(first) && first.tagName === "h1" && collapse(textOf(first)) === collapse(title) ? first : void 0;
216
217
  const lead = elements[skip ? 1 : 0];
217
- const repeat = description && isObject(lead) && lead.tagName === "p" && collapse(textOf(lead)).startsWith(collapse(description)) && !holdsEmbed(lead) ? lead : void 0;
218
+ const repeat = description && isObject(lead) && lead.tagName === "p" && collapse(textOf(lead)) === collapse(description) && !holdsEmbed(lead) ? lead : void 0;
218
219
  const budget = { nodes: 0, text: 0 };
219
220
  return { type: "root", children: raw.filter((n) => n !== skip && n !== repeat).flatMap((n) => clean(n, 0, { katex: false }, budget)) };
220
221
  }
@@ -1,8 +1,11 @@
1
1
  import {
2
2
  ASSET_FILE,
3
3
  ASSET_RESERVED_PATH,
4
- ASSET_ROUTE_PREFIX
5
- } from "./chunk-67IOIAVW.js";
4
+ ASSET_ROUTE_PREFIX,
5
+ PREVIEW_HEADER,
6
+ PREVIEW_PARAM,
7
+ PREVIEW_TOKEN
8
+ } from "./chunk-DUPT6XLB.js";
6
9
 
7
10
  // src/errors.ts
8
11
  var Cp1ClientError = class extends Error {
@@ -20,6 +23,30 @@ var Cp1ClientError = class extends Error {
20
23
  }
21
24
  };
22
25
 
26
+ // src/preview.ts
27
+ var ASSET_PATH = /^\/assets\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?:\.[a-z0-9]+)?(?= |$)/i;
28
+ var ASSET_PROPERTIES = ["src", "dataLight", "dataDark"];
29
+ var MAX_DEPTH = 200;
30
+ function mapNode(node, mark, depth) {
31
+ if (typeof node !== "object" || node === null || depth > MAX_DEPTH) return node;
32
+ const { properties, children } = node;
33
+ const marked = properties && Object.fromEntries(ASSET_PROPERTIES.filter((name) => name in properties).map((name) => [name, mark(properties[name])]));
34
+ return {
35
+ ...node,
36
+ ...properties ? { properties: { ...properties, ...marked } } : {},
37
+ ...Array.isArray(children) ? { children: children.map((child) => mapNode(child, mark, depth + 1)) } : {}
38
+ };
39
+ }
40
+ function withPreviewAssets(article, token) {
41
+ const mark = (value) => typeof value === "string" ? value.replace(ASSET_PATH, (path) => `${path}?${PREVIEW_PARAM}=${token}`) : value;
42
+ const { publication, content } = article;
43
+ return {
44
+ ...article,
45
+ publication: publication.cover ? { ...publication, cover: { path: String(mark(publication.cover.path)) } } : publication,
46
+ content: content.format === "hast" ? { ...content, tree: mapNode(content.tree, mark, 0) } : content
47
+ };
48
+ }
49
+
23
50
  // src/client.ts
24
51
  var RENDERER_VERSION = "4";
25
52
  function applyCredential(headers, credential, method, url) {
@@ -55,6 +82,17 @@ async function readCapped(res, max) {
55
82
  }
56
83
  return Buffer.concat(chunks).toString("utf8");
57
84
  }
85
+ function isNotFoundBody(status, body) {
86
+ if (status !== 404) return false;
87
+ try {
88
+ return JSON.parse(body)?.error?.code === "NOT_FOUND";
89
+ } catch {
90
+ return false;
91
+ }
92
+ }
93
+ async function isNotFoundResponse(res) {
94
+ return isNotFoundBody(res.status, await readCapped(res, 64 * 1024).catch(() => ""));
95
+ }
58
96
  var KINDS = /* @__PURE__ */ new Set(["article", "list", "redirect", "notfound", "disabled"]);
59
97
  var isStrings = (value) => Array.isArray(value) && value.every((v) => typeof v === "string");
60
98
  function isMounts(data) {
@@ -132,8 +170,20 @@ function createHyleClient(options) {
132
170
  if (data?.cp === 1 && data.kind === "push_posts" && Array.isArray(data.posts) && typeof data.complete === "boolean") return data;
133
171
  throw failure(reply);
134
172
  }
173
+ async function preview(token) {
174
+ if (typeof token !== "string" || !PREVIEW_TOKEN.test(token)) return { kind: "notfound" };
175
+ const reply = await get("/cp1/read/_preview", new URLSearchParams(), { headers: { [PREVIEW_HEADER]: token }, cache: "no-store" });
176
+ if (reply.status === 404 && reply.parsed?.error?.code === "NOT_FOUND") return { kind: "notfound" };
177
+ const data = reply.parsed?.data;
178
+ if (data?.cp === 1 && data.kind === "disabled") return { kind: "disabled" };
179
+ const article = data?.article;
180
+ if (data?.cp !== 1 || data.kind !== "preview" || typeof data.expires_at !== "string" || article?.kind !== "article" || article.content?.format !== "hast" || !article.publication) throw failure(reply);
181
+ return { kind: "preview", expires_at: data.expires_at, article: withPreviewAssets(article, token) };
182
+ }
135
183
  async function asset(file, opts = {}) {
136
184
  if (!ASSET_FILE.test(file)) throw new TypeError("asset file must be <uuid>[.<ext>]");
185
+ const { previewToken } = opts;
186
+ if (previewToken !== void 0 && !PREVIEW_TOKEN.test(previewToken)) throw new TypeError("previewToken is not a preview token");
137
187
  const url = `${base}/cp1/read/assets/${file}`;
138
188
  const headers = new Headers();
139
189
  const applied = applyCredential(headers, options, "GET", url);
@@ -142,23 +192,30 @@ function createHyleClient(options) {
142
192
  const value = opts.headers?.[name];
143
193
  if (value) headers.set(name, value);
144
194
  }
195
+ if (previewToken !== void 0) headers.set(PREVIEW_HEADER, previewToken);
196
+ return fetchImpl(url, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(opts.timeoutMs ?? options.timeoutMs ?? DEFAULT_TIMEOUT_MS), ...previewToken === void 0 ? {} : { cache: "no-store" } });
197
+ }
198
+ async function theme(opts = {}) {
199
+ const url = `${base}/cp1/read/theme.css`;
200
+ const headers = new Headers();
201
+ const applied = applyCredential(headers, options, "GET", url);
202
+ if (applied) await applied;
145
203
  return fetchImpl(url, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(opts.timeoutMs ?? options.timeoutMs ?? DEFAULT_TIMEOUT_MS) });
146
204
  }
147
- return { resolve, mounts, pushPosts, asset };
205
+ return { resolve, mounts, pushPosts, preview, asset, theme };
148
206
  }
149
207
 
150
208
  // src/host-sync.ts
151
209
  var CONTROL_TOKEN_PREFIX = "hyle_tc_";
152
210
  var DEFAULT_TIMEOUT_MS2 = 1e4;
153
211
  var MAX_RESPONSE_BYTES = 1024 * 1024;
154
- var PATH = "/cp1/control/workspace/host-reserved-paths";
155
- async function syncHostReserved(input) {
212
+ async function put(input, path, body, read) {
156
213
  checkBaseUrl(input.baseUrl, input.allowInsecureHttp);
157
214
  if (input.token !== void 0 && !input.token.startsWith(CONTROL_TOKEN_PREFIX)) {
158
215
  throw new TypeError("syncing needs a control credential (hyle_tc_...); a target token is read-only and cannot write");
159
216
  }
160
217
  const fetchImpl = input.fetch ?? fetch;
161
- const url = `${input.baseUrl.replace(/\/+$/, "")}${PATH}`;
218
+ const url = `${input.baseUrl.replace(/\/+$/, "")}${path}`;
162
219
  const headers = new Headers({ "content-type": "application/json", "idempotency-key": `sync-${crypto.randomUUID()}` });
163
220
  await applyCredential(headers, input.auth ? { auth: input.auth, workspaceId: input.workspaceId } : { token: input.token }, "PUT", url);
164
221
  headers.set("cp-workspace", input.workspaceId);
@@ -168,7 +225,7 @@ async function syncHostReserved(input) {
168
225
  redirect: "error",
169
226
  signal: AbortSignal.timeout(input.timeoutMs ?? DEFAULT_TIMEOUT_MS2),
170
227
  headers,
171
- body: JSON.stringify({ paths: input.paths, host: input.host, build_id: input.buildId })
228
+ body: JSON.stringify(body)
172
229
  });
173
230
  const text = await readCapped(res, MAX_RESPONSE_BYTES);
174
231
  let parsed;
@@ -177,14 +234,18 @@ async function syncHostReserved(input) {
177
234
  } catch {
178
235
  parsed = void 0;
179
236
  }
180
- const data = parsed?.data;
181
- const conflicts = data?.conflicts;
182
- if (res.ok && data && Array.isArray(data.paths) && typeof data.changed === "boolean" && typeof data.synced_at === "string" && typeof conflicts?.total === "number" && Array.isArray(conflicts.offenders)) {
183
- return { paths: data.paths, changed: data.changed, syncedAt: data.synced_at, conflicts: { total: conflicts.total, offenders: conflicts.offenders } };
184
- }
237
+ const result = res.ok && parsed?.data ? read(parsed.data) : void 0;
238
+ if (result !== void 0) return result;
185
239
  const err = parsed?.error;
186
240
  throw new Cp1ClientError(err?.code ?? "INVALID_RESPONSE", err?.message ?? `Unexpected response from hyle (HTTP ${res.status})`, res.status, err?.request_id ?? "", err?.details);
187
241
  }
242
+ function syncHostReserved(input) {
243
+ return put(input, "/cp1/control/workspace/host-reserved-paths", { paths: input.paths, host: input.host, build_id: input.buildId }, (data) => {
244
+ const conflicts = data.conflicts;
245
+ if (!Array.isArray(data.paths) || typeof data.changed !== "boolean" || typeof data.synced_at !== "string" || typeof conflicts?.total !== "number" || !Array.isArray(conflicts.offenders)) return void 0;
246
+ return { paths: data.paths, changed: data.changed, syncedAt: data.synced_at, conflicts: { total: conflicts.total, offenders: conflicts.offenders } };
247
+ });
248
+ }
188
249
 
189
250
  // src/delivery-dedupe.ts
190
251
  var DeliveryDedupe = class {
@@ -724,6 +785,8 @@ function cleanDisplayName(raw) {
724
785
  export {
725
786
  Cp1ClientError,
726
787
  readCapped,
788
+ isNotFoundBody,
789
+ isNotFoundResponse,
727
790
  checkBaseUrl,
728
791
  createHyleClient,
729
792
  syncHostReserved,
@@ -9,6 +9,37 @@ var AUTO_RENDER_GROUPS = 25;
9
9
  var CLAMP_LINES = 12;
10
10
  var isClampedSource = (source) => source.trimEnd().split("\n").length > CLAMP_LINES;
11
11
  var DARK_VARIABLES = { edgeLabelBackground: "#333333" };
12
+ var DIAGRAM_COLOUR_ROLES = ["bg", "node", "node-text", "node-border", "line", "accent", "muted"];
13
+ var THEME_COLOUR = /^(?:#[0-9a-f]{6}(?:[0-9a-f]{2})?|transparent)$/;
14
+ function diagramThemeVariables(colours, dark) {
15
+ const [bg, node, text, border, line, accent, muted] = colours;
16
+ return {
17
+ darkMode: dark,
18
+ background: bg,
19
+ primaryColor: node,
20
+ primaryTextColor: text,
21
+ primaryBorderColor: border,
22
+ secondaryColor: bg,
23
+ secondaryTextColor: text,
24
+ secondaryBorderColor: muted,
25
+ tertiaryColor: bg,
26
+ tertiaryTextColor: text,
27
+ tertiaryBorderColor: muted,
28
+ lineColor: line,
29
+ textColor: text,
30
+ titleColor: text,
31
+ edgeLabelBackground: bg,
32
+ clusterBkg: bg,
33
+ clusterBorder: muted,
34
+ noteBkgColor: bg,
35
+ noteTextColor: text,
36
+ noteBorderColor: accent,
37
+ activationBorderColor: accent,
38
+ actorLineColor: muted,
39
+ signalColor: line,
40
+ signalTextColor: text
41
+ };
42
+ }
12
43
  var COLOR = /^(rgba?|oklab|oklch|lab|lch)\(([^)]*)\)$|^color\(srgb ([^)]*)\)$/;
13
44
  function isDark(css) {
14
45
  const m = COLOR.exec(css.trim());
@@ -31,7 +62,8 @@ function watchPageTheme(el, onTheme) {
31
62
  const scheme = window.matchMedia("(prefers-color-scheme: dark)");
32
63
  const update = () => {
33
64
  const style = getComputedStyle(el);
34
- onTheme(pickMermaidTheme(style.backgroundColor, scheme.matches, style.colorScheme));
65
+ const colours = DIAGRAM_COLOUR_ROLES.map((role) => style.getPropertyValue(`--hyle-diagram-${role}`).trim());
66
+ onTheme(pickMermaidTheme(style.backgroundColor, scheme.matches, style.colorScheme), colours.every((c) => THEME_COLOUR.test(c)) ? colours.join(" ") : "");
35
67
  };
36
68
  update();
37
69
  const observer = new MutationObserver(update);
@@ -134,6 +166,7 @@ export {
134
166
  MAX_MERMAID_EDGES,
135
167
  isClampedSource,
136
168
  DARK_VARIABLES,
169
+ diagramThemeVariables,
137
170
  watchPageTheme,
138
171
  RENDER_TIMEOUT_MS,
139
172
  stripMermaidConfig,