@histoire/devframe 0.0.0-stage → 1.0.0-beta.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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -2
  3. package/dist/client/attach.d.ts +15 -0
  4. package/dist/client/attach.d.ts.map +1 -0
  5. package/dist/client/attach.js +238 -0
  6. package/dist/client/diagnostics.d.ts +14 -0
  7. package/dist/client/diagnostics.d.ts.map +1 -0
  8. package/dist/client/diagnostics.js +31 -0
  9. package/dist/client/events.d.ts +11 -0
  10. package/dist/client/events.d.ts.map +1 -0
  11. package/dist/client/events.js +52 -0
  12. package/dist/client/highlight-operations.d.ts +14 -0
  13. package/dist/client/highlight-operations.d.ts.map +1 -0
  14. package/dist/client/highlight-operations.js +74 -0
  15. package/dist/client/index.d.ts +3 -0
  16. package/dist/client/index.d.ts.map +1 -0
  17. package/dist/client/index.js +2 -0
  18. package/dist/client/inspection.d.ts +9 -0
  19. package/dist/client/inspection.d.ts.map +1 -0
  20. package/dist/client/inspection.js +32 -0
  21. package/dist/client/pairing-budget.d.ts +3 -0
  22. package/dist/client/pairing-budget.d.ts.map +1 -0
  23. package/dist/client/pairing-budget.js +36 -0
  24. package/dist/host/index.d.ts +35 -0
  25. package/dist/host/index.d.ts.map +1 -0
  26. package/dist/host/index.js +86 -0
  27. package/dist/node/broker.d.ts +37 -0
  28. package/dist/node/broker.d.ts.map +1 -0
  29. package/dist/node/broker.js +349 -0
  30. package/dist/node/devframe.d.ts +64 -0
  31. package/dist/node/devframe.d.ts.map +1 -0
  32. package/dist/node/devframe.js +193 -0
  33. package/dist/node/grants.d.ts +24 -0
  34. package/dist/node/grants.d.ts.map +1 -0
  35. package/dist/node/grants.js +58 -0
  36. package/dist/node/index.d.ts +8 -0
  37. package/dist/node/index.d.ts.map +1 -0
  38. package/dist/node/index.js +4 -0
  39. package/dist/node/mcp.d.ts +10 -0
  40. package/dist/node/mcp.d.ts.map +1 -0
  41. package/dist/node/mcp.js +150 -0
  42. package/dist/node/retirements.d.ts +8 -0
  43. package/dist/node/retirements.d.ts.map +1 -0
  44. package/dist/node/retirements.js +34 -0
  45. package/dist/node/rpc-budget.d.ts +6 -0
  46. package/dist/node/rpc-budget.d.ts.map +1 -0
  47. package/dist/node/rpc-budget.js +26 -0
  48. package/dist/node/types.d.ts +65 -0
  49. package/dist/node/types.d.ts.map +1 -0
  50. package/dist/node/types.js +1 -0
  51. package/dist/node/validation.d.ts +10 -0
  52. package/dist/node/validation.d.ts.map +1 -0
  53. package/dist/node/validation.js +50 -0
  54. package/dist/report/content.d.ts +33 -0
  55. package/dist/report/content.d.ts.map +1 -0
  56. package/dist/report/content.js +64 -0
  57. package/dist/report/index.d.ts +60 -0
  58. package/dist/report/index.d.ts.map +1 -0
  59. package/dist/report/index.js +78 -0
  60. package/dist/shared/diagnostics.d.ts +9 -0
  61. package/dist/shared/diagnostics.d.ts.map +1 -0
  62. package/dist/shared/diagnostics.js +20 -0
  63. package/dist/shared/events.d.ts +27 -0
  64. package/dist/shared/events.d.ts.map +1 -0
  65. package/dist/shared/events.js +49 -0
  66. package/dist/shared/services.d.ts +413 -0
  67. package/dist/shared/services.d.ts.map +1 -0
  68. package/dist/shared/services.js +113 -0
  69. package/package.json +53 -3
@@ -0,0 +1,50 @@
1
+ import { assertInspectionJson, measureWireValue } from '@histoire/protocol';
2
+ /** Bound JSON payloads before they cross a trust or transport boundary. */
3
+ export function bounded(value, maxBytes) {
4
+ try {
5
+ measureWireValue(value, { maxBytes, maxDepth: 32 });
6
+ assertInspectionJson(value);
7
+ return structuredClone(value);
8
+ }
9
+ catch {
10
+ throw new Error('Invalid payload');
11
+ }
12
+ }
13
+ /** Verify the private owner tuple returned by the trusted host. */
14
+ export function verifiedIdentity(value) {
15
+ const owner = bounded(value, 16 * 1024);
16
+ if (!owner || typeof owner !== 'object' || !owner.target || typeof owner.target !== 'object')
17
+ throw new Error('Pairing unavailable');
18
+ for (const key of ['projectId', 'sourceId', 'epoch', 'revision', 'mountId', 'runtimeId']) {
19
+ if (typeof owner[key] !== 'string' || !owner[key] || owner[key].length > 2048)
20
+ throw new Error('Pairing unavailable');
21
+ }
22
+ if (typeof owner.target.storyId !== 'string' || !owner.target.storyId || typeof owner.target.variantId !== 'string' || !owner.target.variantId)
23
+ throw new Error('Pairing unavailable');
24
+ const allowed = ['projectId', 'sourceId', 'epoch', 'revision', 'mountId', 'runtimeId', 'target'];
25
+ if (Object.keys(owner).some(key => !allowed.includes(key)) || Object.keys(owner.target).some(key => !['storyId', 'variantId'].includes(key)))
26
+ throw new Error('Pairing unavailable');
27
+ return owner;
28
+ }
29
+ /** Read a finite host revision without leaking host callback errors. */
30
+ export function readAuthorityVersion(callback) {
31
+ let value;
32
+ try {
33
+ value = callback();
34
+ }
35
+ catch {
36
+ throw new Error('Session unavailable');
37
+ }
38
+ if (!Number.isSafeInteger(value) || value < 0)
39
+ throw new Error('Session unavailable');
40
+ return value;
41
+ }
42
+ /** Treat host source-policy errors as denial. */
43
+ export function isSourceAllowed(callback, url) {
44
+ try {
45
+ return callback(url) === true;
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ }
@@ -0,0 +1,33 @@
1
+ import type { HistoireInspectionResult, HistoireJsonValue } from '@histoire/protocol';
2
+ /** Caller explicitly selects fields and supplies all sanitization; it owns sensitivity decisions. */
3
+ export interface InspectionContentOptions {
4
+ /** Fields and nodes selected per result; unselected data is never copied. */
5
+ selections: readonly ({
6
+ index: number;
7
+ stateKeys: readonly string[];
8
+ } | {
9
+ index: number;
10
+ nodeIndexes: readonly number[];
11
+ })[];
12
+ /** Sanitize a detached selected value before export. Must return bounded JSON. */
13
+ sanitize: (value: HistoireJsonValue, context: {
14
+ resultIndex: number;
15
+ kind: 'state' | 'dom';
16
+ key: string | number;
17
+ }) => HistoireJsonValue;
18
+ }
19
+ /** Explicit selected content with no attributes, selectors, URLs or raw runtime handles. */
20
+ export interface InspectionReportContent {
21
+ /** Selected caller-sanitized state fields. */
22
+ state?: Record<string, HistoireJsonValue>;
23
+ /** Selected caller-sanitized node text and bounded structural identity. */
24
+ dom?: {
25
+ index: number;
26
+ parentIndex: number | null;
27
+ tag: string;
28
+ text: string;
29
+ }[];
30
+ }
31
+ /** Validate and apply explicit selections to already validated results. */
32
+ export declare function selectReportContent(options: InspectionContentOptions, results: readonly HistoireInspectionResult[]): Map<number, InspectionReportContent>;
33
+ //# sourceMappingURL=content.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../../src/report/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAGrF,qGAAqG;AACrG,MAAM,WAAW,wBAAwB;IACvC,6EAA6E;IAC7E,UAAU,EAAE,SAAS,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,CAAC,EAAE,CAAA;IAC5H,kFAAkF;IAClF,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,EAAE,OAAO,EAAE;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,OAAO,GAAG,KAAK,CAAC;QAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,KAAK,iBAAiB,CAAA;CACzI;AAED,4FAA4F;AAC5F,MAAM,WAAW,uBAAuB;IACtC,8CAA8C;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAA;IACzC,2EAA2E;IAC3E,GAAG,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CACjF;AAWD,2EAA2E;AAC3E,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,wBAAwB,EAAE,OAAO,EAAE,SAAS,wBAAwB,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,uBAAuB,CAAC,CA8CzJ"}
@@ -0,0 +1,64 @@
1
+ import { assertInspectionJson, measureWireValue } from '@histoire/protocol';
2
+ const TAGS = new Set(['a', 'article', 'aside', 'button', 'div', 'footer', 'form', 'h1', 'h2', 'h3', 'header', 'input', 'label', 'li', 'main', 'nav', 'ol', 'option', 'p', 'section', 'select', 'span', 'strong', 'table', 'tbody', 'td', 'textarea', 'th', 'thead', 'tr', 'ul']);
3
+ /** Clone JSON after validation; neither source nor caller-owned sanitizer result is retained. */
4
+ function detached(value) {
5
+ assertInspectionJson(value);
6
+ measureWireValue(value, { maxBytes: 16 * 1024, maxDepth: 16 });
7
+ return JSON.parse(JSON.stringify(value));
8
+ }
9
+ /** Validate and apply explicit selections to already validated results. */
10
+ export function selectReportContent(options, results) {
11
+ const fields = options && Object.getOwnPropertyDescriptors(options);
12
+ const selections = fields?.selections && 'value' in fields.selections ? fields.selections.value : undefined;
13
+ const sanitize = fields?.sanitize && 'value' in fields.sanitize ? fields.sanitize.value : undefined;
14
+ if (!fields || Object.keys(fields).some(key => !['selections', 'sanitize'].includes(key)) || !Array.isArray(selections) || selections.length > 32 || typeof sanitize !== 'function')
15
+ throw new Error('Selected report content requires explicit selections and sanitizer');
16
+ assertInspectionJson(selections);
17
+ const output = new Map();
18
+ let contentBytes = 0;
19
+ /** Stop while constructing content, before aggregate data can grow beyond the report budget. */
20
+ function retain(value, key) {
21
+ const copy = detached(value);
22
+ contentBytes += measureWireValue(copy, { maxBytes: 16 * 1024, maxDepth: 16 }) + new TextEncoder().encode(key).length;
23
+ if (contentBytes > 48 * 1024)
24
+ throw new Error('Selected report content exceeds size limit');
25
+ return copy;
26
+ }
27
+ for (const selection of selections) {
28
+ if (!selection || typeof selection !== 'object' || Array.isArray(selection) || Object.keys(selection).some(key => !['index', 'stateKeys', 'nodeIndexes'].includes(key)))
29
+ throw new Error('Invalid report selection');
30
+ const { index, stateKeys, nodeIndexes } = selection;
31
+ if (!Number.isSafeInteger(index) || index < 0 || index >= results.length || output.has(index))
32
+ throw new Error('Invalid report result index');
33
+ const result = results[index];
34
+ if (result.inspection === 'variant' && Array.isArray(stateKeys) && nodeIndexes === undefined && stateKeys.length <= 100) {
35
+ const fields = Object.create(null);
36
+ for (const key of stateKeys) {
37
+ if (typeof key !== 'string' || key.length > 256 || !Object.hasOwn(result.state, key) || Object.hasOwn(fields, key) || ['__proto__', 'prototype', 'constructor'].includes(key))
38
+ throw new Error('Invalid report state field');
39
+ fields[key] = retain(sanitize(detached(result.state[key]), { resultIndex: index, kind: 'state', key }), key);
40
+ }
41
+ output.set(index, { state: fields });
42
+ }
43
+ else if (result.inspection === 'dom' && Array.isArray(nodeIndexes) && stateKeys === undefined && nodeIndexes.length <= 100) {
44
+ const seen = new Set();
45
+ const nodes = nodeIndexes.map((nodeIndex) => {
46
+ if (!Number.isSafeInteger(nodeIndex) || seen.has(nodeIndex))
47
+ throw new Error('Invalid report DOM node');
48
+ const node = result.nodes.find(item => item.index === nodeIndex);
49
+ if (!node)
50
+ throw new Error('Invalid report DOM node');
51
+ seen.add(nodeIndex);
52
+ const text = retain(sanitize(node.text, { resultIndex: index, kind: 'dom', key: node.index }), node.tag);
53
+ if (typeof text !== 'string')
54
+ throw new Error('DOM sanitizer must return text');
55
+ return { index: node.index, parentIndex: node.parentIndex, tag: TAGS.has(node.tag) ? node.tag : 'other', text };
56
+ });
57
+ output.set(index, { dom: nodes });
58
+ }
59
+ else {
60
+ throw new Error('Selection does not match inspection result');
61
+ }
62
+ }
63
+ return output;
64
+ }
@@ -0,0 +1,60 @@
1
+ import type { InspectionContentOptions, InspectionReportContent } from './content.js';
2
+ export type { InspectionContentOptions, InspectionReportContent } from './content.js';
3
+ /** Credential-free historical identity represented by stable SHA-256 fingerprints. */
4
+ export interface InspectionReportEntryBase {
5
+ /** Runtime or isolated origin of the observation. */
6
+ mode: 'live' | 'isolated';
7
+ /** Original observation time in UTC. */
8
+ observedAt: string;
9
+ /** Fingerprint of source identity. */
10
+ source: string;
11
+ /** Fingerprint of source epoch. */
12
+ epoch: string;
13
+ /** Fingerprint of source revision. */
14
+ revision: string;
15
+ /** Fingerprint of runtime document identity. */
16
+ runtime: string;
17
+ /** Fingerprint of story identity. */
18
+ story: string;
19
+ /** Fingerprint of variant identity. */
20
+ variant: string;
21
+ /** Explicitly selected caller-sanitized content, absent by default. */
22
+ content?: InspectionReportContent;
23
+ /** Inspection was truncated. */
24
+ truncated: boolean;
25
+ }
26
+ /** Summary fields are always present; selected sanitized content is optional. */
27
+ export type InspectionReportEntry = InspectionReportEntryBase & ({
28
+ inspection: 'variant';
29
+ stateFields: number;
30
+ components: number;
31
+ omittedValues: number;
32
+ } | {
33
+ inspection: 'dom';
34
+ matched: boolean;
35
+ elements: number;
36
+ });
37
+ /** Versioned detached historical summary. */
38
+ export interface InspectionReport {
39
+ /** Report format version. */
40
+ version: 1;
41
+ /** UTC export timestamp. */
42
+ generatedAt: string;
43
+ /** Explicitly selected bounded summaries. */
44
+ entries: InspectionReportEntry[];
45
+ }
46
+ /**
47
+ * Explicitly export selected validated results as detached metadata and standalone HTML.
48
+ * Rejects oversized or unsupported results; does not read sessions or write files.
49
+ * @param options Report input.
50
+ * @param options.results Results the caller intentionally selected.
51
+ * @param options.content Optional explicitly selected content with caller-provided sanitization.
52
+ */
53
+ export declare function createInspectionReport(options: {
54
+ results: readonly unknown[];
55
+ content?: InspectionContentOptions;
56
+ }): {
57
+ data: InspectionReport;
58
+ html: string;
59
+ };
60
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/report/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AAMrF,YAAY,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAA;AAErF,sFAAsF;AACtF,MAAM,WAAW,yBAAyB;IACxC,qDAAqD;IACrD,IAAI,EAAE,MAAM,GAAG,UAAU,CAAA;IACzB,wCAAwC;IACxC,UAAU,EAAE,MAAM,CAAA;IAClB,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAA;IACd,mCAAmC;IACnC,KAAK,EAAE,MAAM,CAAA;IACb,sCAAsC;IACtC,QAAQ,EAAE,MAAM,CAAA;IAChB,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAA;IACf,qCAAqC;IACrC,KAAK,EAAE,MAAM,CAAA;IACb,uCAAuC;IACvC,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,OAAO,CAAC,EAAE,uBAAuB,CAAA;IACjC,gCAAgC;IAChC,SAAS,EAAE,OAAO,CAAA;CACnB;AAED,iFAAiF;AACjF,MAAM,MAAM,qBAAqB,GAAG,yBAAyB,GAAG,CAC5D;IAAE,UAAU,EAAE,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,GACzF;IAAE,UAAU,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAC5D,CAAA;AAED,6CAA6C;AAC7C,MAAM,WAAW,gBAAgB;IAC/B,6BAA6B;IAC7B,OAAO,EAAE,CAAC,CAAA;IACV,4BAA4B;IAC5B,WAAW,EAAE,MAAM,CAAA;IACnB,6CAA6C;IAC7C,OAAO,EAAE,qBAAqB,EAAE,CAAA;CACjC;AA6CD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE;IAAE,OAAO,EAAE,SAAS,OAAO,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,wBAAwB,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAqB7J"}
@@ -0,0 +1,78 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { createHash } from 'node:crypto';
3
+ import { validateInspectionResult } from '@histoire/protocol';
4
+ import { selectReportContent } from './content.js';
5
+ /** Produces a one-way, consistently encoded identifier without storing its original text. */
6
+ function fingerprint(value) {
7
+ return createHash('sha256').update(value, 'utf8').digest('hex');
8
+ }
9
+ /** Map one validated result to a metadata-only summary. */
10
+ function summarize(result) {
11
+ if (result.inspection !== 'variant' && result.inspection !== 'dom')
12
+ throw new Error('Unsupported inspection report result');
13
+ const { owner } = result;
14
+ const base = {
15
+ mode: result.mode,
16
+ observedAt: new Date(result.observedAt).toISOString(),
17
+ source: fingerprint(owner.sourceId),
18
+ epoch: fingerprint(owner.epoch),
19
+ revision: fingerprint(owner.revision),
20
+ runtime: fingerprint(owner.runtimeId),
21
+ story: fingerprint(owner.target.storyId),
22
+ variant: fingerprint(owner.target.variantId),
23
+ truncated: result.truncated,
24
+ };
25
+ return result.inspection === 'variant'
26
+ ? { ...base, inspection: 'variant', stateFields: Object.keys(result.state).length, components: result.components.length, omittedValues: result.omittedValues }
27
+ : { ...base, inspection: 'dom', matched: result.matched, elements: result.nodes.length };
28
+ }
29
+ /** Escape all data interpolated into offline HTML. */
30
+ function escapeHtml(value) {
31
+ return value.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll('\'', '&#39;');
32
+ }
33
+ /** Render static HTML without scripts, network dependencies, or live commands. */
34
+ function renderReport(data) {
35
+ const rows = data.entries.map((entry) => {
36
+ const summary = entry.inspection === 'variant'
37
+ ? `${entry.stateFields} state fields, ${entry.components} prop groups, ${entry.omittedValues} omitted values`
38
+ : entry.matched ? `${entry.elements} elements` : 'No matching element';
39
+ const cells = [entry.observedAt, entry.mode, entry.inspection, entry.source, entry.epoch, entry.revision, entry.runtime, entry.story, entry.variant, summary, entry.truncated ? 'Yes' : 'No'];
40
+ const content = entry.content ? `<pre>${escapeHtml(JSON.stringify(entry.content, null, 2))}</pre>` : '';
41
+ return `<tr>${cells.map(cell => `<td>${escapeHtml(cell)}</td>`).join('')}<td>${content}</td></tr>`;
42
+ }).join('');
43
+ return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; base-uri 'none'; form-action 'none'"><title>Historical inspection</title></head><body><h1>Historical inspection</h1><p>Exported ${escapeHtml(data.generatedAt)}. Identity values are SHA-256 fingerprints. Selected content is caller-sanitized and may contain sensitive information.</p><table><thead><tr><th>Observed</th><th>Mode</th><th>Kind</th><th>Source</th><th>Epoch</th><th>Revision</th><th>Runtime</th><th>Story</th><th>Variant</th><th>Summary</th><th>Truncated</th><th>Selected content</th></tr></thead><tbody>${rows}</tbody></table></body></html>`;
44
+ }
45
+ /**
46
+ * Explicitly export selected validated results as detached metadata and standalone HTML.
47
+ * Rejects oversized or unsupported results; does not read sessions or write files.
48
+ * @param options Report input.
49
+ * @param options.results Results the caller intentionally selected.
50
+ * @param options.content Optional explicitly selected content with caller-provided sanitization.
51
+ */
52
+ export function createInspectionReport(options) {
53
+ const descriptor = options && Object.getOwnPropertyDescriptor(options, 'results');
54
+ const results = descriptor && 'value' in descriptor ? descriptor.value : undefined;
55
+ if (!Array.isArray(results) || results.length > 32)
56
+ throw new Error('Inspection report accepts at most 32 results');
57
+ const resultsValidated = [];
58
+ for (let index = 0; index < results.length; index++) {
59
+ const item = Object.getOwnPropertyDescriptor(results, index);
60
+ if (!item || !('value' in item))
61
+ throw new Error('Inspection report requires detached results');
62
+ resultsValidated.push(validateInspectionResult(item.value));
63
+ }
64
+ const entries = resultsValidated.map(summarize);
65
+ const contentDescriptor = Object.getOwnPropertyDescriptor(options, 'content');
66
+ if (contentDescriptor) {
67
+ if (!('value' in contentDescriptor))
68
+ throw new Error('Report content must be a detached option');
69
+ const content = selectReportContent(contentDescriptor.value, resultsValidated);
70
+ for (const [index, selected] of content)
71
+ entries[index].content = selected;
72
+ }
73
+ const data = { version: 1, generatedAt: new Date().toISOString(), entries };
74
+ const html = renderReport(data);
75
+ if (Buffer.byteLength(JSON.stringify(data), 'utf8') > 64 * 1024 || Buffer.byteLength(html, 'utf8') > 64 * 1024)
76
+ throw new Error('Inspection report exceeds size limit');
77
+ return { data, html };
78
+ }
@@ -0,0 +1,9 @@
1
+ import type { HistoireInspectionOwner } from '@histoire/protocol';
2
+ /** Bounded diagnostic cursor tied to an exact mounted owner. */
3
+ export declare function parseDiagnosticRequest(value: unknown): {
4
+ expectedOwner: HistoireInspectionOwner;
5
+ target: HistoireInspectionOwner['target'];
6
+ afterSequence?: number;
7
+ limit?: number;
8
+ };
9
+ //# sourceMappingURL=diagnostics.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostics.d.ts","sourceRoot":"","sources":["../../src/shared/diagnostics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAGjE,gEAAgE;AAChE,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAWnC;IAAE,aAAa,EAAE,uBAAuB,CAAC;IAAC,MAAM,EAAE,uBAAuB,CAAC,QAAQ,CAAC,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAC9I"}
@@ -0,0 +1,20 @@
1
+ import { assertInspectionJson, measureWireValue, validateInspectionOwner, validateInspectionTarget } from '@histoire/protocol';
2
+ /** Bounded diagnostic cursor tied to an exact mounted owner. */
3
+ export function parseDiagnosticRequest(value) {
4
+ measureWireValue(value, { maxBytes: 16 * 1024, maxDepth: 32 });
5
+ assertInspectionJson(value);
6
+ if (!value || typeof value !== 'object' || Array.isArray(value))
7
+ throw new Error('Invalid diagnostic request');
8
+ const input = value;
9
+ if (Object.keys(input).some(key => !['expectedOwner', 'target', 'afterSequence', 'limit'].includes(key)))
10
+ throw new Error('Invalid diagnostic request');
11
+ const expectedOwner = validateInspectionOwner(input.expectedOwner);
12
+ const target = validateInspectionTarget(input.target);
13
+ if (target.storyId !== expectedOwner.target.storyId || target.variantId !== expectedOwner.target.variantId)
14
+ throw new Error('Invalid diagnostic request');
15
+ if (input.afterSequence !== undefined && (!Number.isSafeInteger(input.afterSequence) || input.afterSequence < 0))
16
+ throw new Error('Invalid diagnostic cursor');
17
+ if (input.limit !== undefined && (!Number.isInteger(input.limit) || input.limit < 1 || input.limit > 100))
18
+ throw new Error('Invalid diagnostic limit');
19
+ return input;
20
+ }
@@ -0,0 +1,27 @@
1
+ import type { HistoireEvent, HistoireInspectionOwner } from '@histoire/protocol';
2
+ /** Finite component event pagination request. */
3
+ export interface EventRequest {
4
+ target: HistoireInspectionOwner['target'];
5
+ expectedOwner: HistoireInspectionOwner;
6
+ afterSequence?: number;
7
+ limit?: number;
8
+ eventLifetime?: string;
9
+ }
10
+ /** Bounded page of current SDK event history; loss counters are session-wide. */
11
+ export interface EventPage {
12
+ mode: 'live';
13
+ owner: HistoireInspectionOwner;
14
+ eventLifetime: string;
15
+ items: HistoireEvent[];
16
+ nextSequence: number;
17
+ oldestSequence: number | null;
18
+ droppedCount: number;
19
+ gap: boolean;
20
+ truncated: boolean;
21
+ omittedCount: number;
22
+ }
23
+ /** Reject extra fields, malformed cursors and unbound target identities. */
24
+ export declare function parseEventRequest(value: unknown): EventRequest;
25
+ /** Validate peer output before it leaves the broker boundary. */
26
+ export declare function validateEventPage(value: unknown, owner: HistoireInspectionOwner, request?: EventRequest): EventPage;
27
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/shared/events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAGhF,iDAAiD;AACjD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,uBAAuB,CAAC,QAAQ,CAAC,CAAA;IACzC,aAAa,EAAE,uBAAuB,CAAA;IACtC,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,iFAAiF;AACjF,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,uBAAuB,CAAA;IAC9B,aAAa,EAAE,MAAM,CAAA;IACrB,KAAK,EAAE,aAAa,EAAE,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,YAAY,EAAE,MAAM,CAAA;IACpB,GAAG,EAAE,OAAO,CAAA;IACZ,SAAS,EAAE,OAAO,CAAA;IAClB,YAAY,EAAE,MAAM,CAAA;CACrB;AAED,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAc9D;AAED,iEAAiE;AACjE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,uBAAuB,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,SAAS,CAiBnH"}
@@ -0,0 +1,49 @@
1
+ import { assertInspectionJson, measureWireValue, sameInspectionOwner, validateInspectionOwner, validateInspectionTarget } from '@histoire/protocol';
2
+ /** Reject extra fields, malformed cursors and unbound target identities. */
3
+ export function parseEventRequest(value) {
4
+ measureWireValue(value, { maxBytes: 16 * 1024, maxDepth: 32 });
5
+ assertInspectionJson(value);
6
+ if (!value || typeof value !== 'object' || Array.isArray(value))
7
+ throw new Error('Invalid event request');
8
+ const input = value;
9
+ if (Object.keys(input).some(key => !['target', 'expectedOwner', 'afterSequence', 'limit', 'eventLifetime'].includes(key)))
10
+ throw new Error('Invalid event request');
11
+ const owner = validateInspectionOwner(input.expectedOwner);
12
+ const target = validateInspectionTarget(input.target);
13
+ if (target.storyId !== owner.target.storyId || target.variantId !== owner.target.variantId)
14
+ throw new Error('Invalid event request');
15
+ if (input.afterSequence !== undefined && (!Number.isSafeInteger(input.afterSequence) || input.afterSequence < 0))
16
+ throw new Error('Invalid event cursor');
17
+ if (input.limit !== undefined && (!Number.isInteger(input.limit) || input.limit < 1 || input.limit > 100))
18
+ throw new Error('Invalid event limit');
19
+ if (input.eventLifetime !== undefined && (typeof input.eventLifetime !== 'string' || !/^[a-f0-9-]{36}$/.test(input.eventLifetime)))
20
+ throw new Error('Invalid event cursor');
21
+ if ((input.afterSequence ?? 0) > 0 && input.eventLifetime === undefined)
22
+ throw new Error('Event lifetime unavailable');
23
+ return input;
24
+ }
25
+ /** Validate peer output before it leaves the broker boundary. */
26
+ export function validateEventPage(value, owner, request) {
27
+ measureWireValue(value, { maxBytes: 60 * 1024, maxDepth: 32 });
28
+ assertInspectionJson(value);
29
+ if (!value || typeof value !== 'object' || Array.isArray(value))
30
+ throw new Error('Invalid event page');
31
+ const page = value;
32
+ if (Object.keys(page).some(key => !['mode', 'owner', 'eventLifetime', 'items', 'nextSequence', 'oldestSequence', 'droppedCount', 'gap', 'truncated', 'omittedCount'].includes(key)))
33
+ throw new Error('Invalid event page');
34
+ if (page.mode !== 'live' || !sameInspectionOwner(validateInspectionOwner(page.owner), owner) || typeof page.eventLifetime !== 'string' || !/^[a-f0-9-]{36}$/.test(page.eventLifetime) || !Array.isArray(page.items) || page.items.length > 100)
35
+ throw new Error('Invalid event page');
36
+ if (!Number.isSafeInteger(page.nextSequence) || page.nextSequence < 0 || (page.oldestSequence !== null && (!Number.isSafeInteger(page.oldestSequence) || page.oldestSequence < 1)) || !Number.isSafeInteger(page.droppedCount) || page.droppedCount < 0 || !Number.isSafeInteger(page.omittedCount) || page.omittedCount < 0 || typeof page.gap !== 'boolean' || typeof page.truncated !== 'boolean')
37
+ throw new Error('Invalid event page');
38
+ const after = request?.afterSequence ?? 0;
39
+ if (page.nextSequence < after || (request?.eventLifetime !== undefined && page.eventLifetime !== request.eventLifetime) || (page.omittedCount > 0 && !page.truncated))
40
+ throw new Error('Invalid event page');
41
+ let previous = after;
42
+ for (const event of page.items) {
43
+ if (!event || typeof event !== 'object' || !Object.hasOwn(event, 'payload') || Object.keys(event).some(key => !['sequence', 'timestamp', 'runtimeId', 'target', 'payload'].includes(key)) || !Number.isSafeInteger(event.sequence) || event.sequence <= previous || event.sequence > page.nextSequence || !Number.isFinite(event.timestamp) || event.runtimeId !== owner.runtimeId || event.target?.storyId !== owner.target.storyId || event.target?.variantId !== owner.target.variantId)
44
+ throw new Error('Invalid event page');
45
+ validateInspectionTarget(event.target);
46
+ previous = event.sequence;
47
+ }
48
+ return page;
49
+ }