@histoire/sdk 0.0.0-stage → 1.0.0-beta.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/LICENSE +21 -0
- package/dist/adapters/types.d.ts +152 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/internal.d.ts +16 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +8 -0
- package/dist/mounts/channel.d.ts +20 -0
- package/dist/mounts/channel.d.ts.map +1 -0
- package/dist/mounts/channel.js +76 -0
- package/dist/mounts/events.d.ts +9 -0
- package/dist/mounts/events.d.ts.map +1 -0
- package/dist/mounts/events.js +45 -0
- package/dist/mounts/geometry.d.ts +9 -0
- package/dist/mounts/geometry.d.ts.map +1 -0
- package/dist/mounts/geometry.js +69 -0
- package/dist/mounts/handle.d.ts +5 -0
- package/dist/mounts/handle.d.ts.map +1 -0
- package/dist/mounts/handle.js +44 -0
- package/dist/mounts/iframe.d.ts +4 -0
- package/dist/mounts/iframe.d.ts.map +1 -0
- package/dist/mounts/iframe.js +143 -0
- package/dist/mounts/overlay.d.ts +11 -0
- package/dist/mounts/overlay.d.ts.map +1 -0
- package/dist/mounts/overlay.js +53 -0
- package/dist/mounts/view-snapshot.d.ts +4 -0
- package/dist/mounts/view-snapshot.d.ts.map +1 -0
- package/dist/mounts/view-snapshot.js +24 -0
- package/dist/persistence/key.d.ts +3 -0
- package/dist/persistence/key.d.ts.map +1 -0
- package/dist/persistence/key.js +4 -0
- package/dist/persistence/storage.d.ts +13 -0
- package/dist/persistence/storage.d.ts.map +1 -0
- package/dist/persistence/storage.js +37 -0
- package/dist/session/channels.d.ts +8 -0
- package/dist/session/channels.d.ts.map +1 -0
- package/dist/session/channels.js +117 -0
- package/dist/session/context.d.ts +97 -0
- package/dist/session/context.d.ts.map +1 -0
- package/dist/session/context.js +136 -0
- package/dist/session/controller.d.ts +5 -0
- package/dist/session/controller.d.ts.map +1 -0
- package/dist/session/controller.js +217 -0
- package/dist/session/debug-lease.d.ts +5 -0
- package/dist/session/debug-lease.d.ts.map +1 -0
- package/dist/session/debug-lease.js +64 -0
- package/dist/session/debug.d.ts +14 -0
- package/dist/session/debug.d.ts.map +1 -0
- package/dist/session/debug.js +242 -0
- package/dist/session/events.d.ts +7 -0
- package/dist/session/events.d.ts.map +1 -0
- package/dist/session/events.js +23 -0
- package/dist/session/inspection.d.ts +5 -0
- package/dist/session/inspection.d.ts.map +1 -0
- package/dist/session/inspection.js +88 -0
- package/dist/session/internal.d.ts +87 -0
- package/dist/session/internal.d.ts.map +1 -0
- package/dist/session/internal.js +144 -0
- package/dist/session/mounts.d.ts +6 -0
- package/dist/session/mounts.d.ts.map +1 -0
- package/dist/session/mounts.js +153 -0
- package/dist/session/notifications.d.ts +5 -0
- package/dist/session/notifications.d.ts.map +1 -0
- package/dist/session/notifications.js +143 -0
- package/dist/session/ownership.d.ts +20 -0
- package/dist/session/ownership.d.ts.map +1 -0
- package/dist/session/ownership.js +69 -0
- package/dist/session/plugin-hub.d.ts +59 -0
- package/dist/session/plugin-hub.d.ts.map +1 -0
- package/dist/session/plugin-hub.js +215 -0
- package/dist/session/plugins.d.ts +8 -0
- package/dist/session/plugins.d.ts.map +1 -0
- package/dist/session/plugins.js +78 -0
- package/dist/session/request.d.ts +8 -0
- package/dist/session/request.d.ts.map +1 -0
- package/dist/session/request.js +99 -0
- package/dist/session/runtime.d.ts +5 -0
- package/dist/session/runtime.d.ts.map +1 -0
- package/dist/session/runtime.js +25 -0
- package/dist/session/selection.d.ts +9 -0
- package/dist/session/selection.d.ts.map +1 -0
- package/dist/session/selection.js +111 -0
- package/dist/session/services.d.ts +18 -0
- package/dist/session/services.d.ts.map +1 -0
- package/dist/session/services.js +51 -0
- package/dist/session/settings.d.ts +7 -0
- package/dist/session/settings.d.ts.map +1 -0
- package/dist/session/settings.js +31 -0
- package/dist/session/snapshot.d.ts +12 -0
- package/dist/session/snapshot.d.ts.map +1 -0
- package/dist/session/snapshot.js +72 -0
- package/dist/session/state.d.ts +11 -0
- package/dist/session/state.d.ts.map +1 -0
- package/dist/session/state.js +73 -0
- package/dist/session/variant.d.ts +4 -0
- package/dist/session/variant.d.ts.map +1 -0
- package/dist/session/variant.js +11 -0
- package/dist/transport/authority.d.ts +35 -0
- package/dist/transport/authority.d.ts.map +1 -0
- package/dist/transport/authority.js +110 -0
- package/dist/transport/connection.d.ts +4 -0
- package/dist/transport/connection.d.ts.map +1 -0
- package/dist/transport/connection.js +37 -0
- package/dist/transport/handshake.d.ts +36 -0
- package/dist/transport/handshake.d.ts.map +1 -0
- package/dist/transport/handshake.js +112 -0
- package/dist/transport/inbound.d.ts +15 -0
- package/dist/transport/inbound.d.ts.map +1 -0
- package/dist/transport/inbound.js +51 -0
- package/dist/transport/port.d.ts +51 -0
- package/dist/transport/port.d.ts.map +1 -0
- package/dist/transport/port.js +288 -0
- package/dist/transport/reply.d.ts +6 -0
- package/dist/transport/reply.d.ts.map +1 -0
- package/dist/transport/reply.js +44 -0
- package/dist/transport/requests.d.ts +33 -0
- package/dist/transport/requests.d.ts.map +1 -0
- package/dist/transport/requests.js +62 -0
- package/dist/transport/session-commands.d.ts +5 -0
- package/dist/transport/session-commands.d.ts.map +1 -0
- package/dist/transport/session-commands.js +70 -0
- package/dist/transport/validated.d.ts +13 -0
- package/dist/transport/validated.d.ts.map +1 -0
- package/dist/transport/validated.js +22 -0
- package/dist/transport/validation.d.ts +8 -0
- package/dist/transport/validation.d.ts.map +1 -0
- package/dist/transport/validation.js +20 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +38 -3
- package/README.md +0 -3
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { HistoireSdkError } from '@histoire/protocol';
|
|
2
|
+
import { request } from './request.js';
|
|
3
|
+
import { primaryRuntime } from './state.js';
|
|
4
|
+
import { assertVariant } from './variant.js';
|
|
5
|
+
/** Metadata-only lazy docs; no preview reservation or story execution. */
|
|
6
|
+
export async function getDocs(context, storyId) {
|
|
7
|
+
context.story(storyId);
|
|
8
|
+
context.assertCapability('docs');
|
|
9
|
+
return context.copy(await request(context, context.assertConnected(), 'docs.get', { storyId }));
|
|
10
|
+
}
|
|
11
|
+
/** Raw source uses data bridge; dynamic source requires selected runtime. */
|
|
12
|
+
export async function getSource(context, input) {
|
|
13
|
+
const story = context.story(input?.storyId);
|
|
14
|
+
if (!['raw', 'dynamic'].includes(input.mode))
|
|
15
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Unknown source mode.');
|
|
16
|
+
if (input.variantId !== undefined)
|
|
17
|
+
assertVariant(story, input.variantId);
|
|
18
|
+
context.assertCapability(input.mode === 'raw' ? 'rawSource' : 'dynamicSource');
|
|
19
|
+
const payload = { storyId: input.storyId, mode: input.mode, ...(input.variantId !== undefined ? { variantId: input.variantId } : {}) };
|
|
20
|
+
if (input.mode === 'raw')
|
|
21
|
+
return context.copy(await request(context, context.assertConnected(), 'source.get', payload));
|
|
22
|
+
const selected = context.selected();
|
|
23
|
+
const variantId = input.variantId ?? (selected.storyId === input.storyId ? selected.variantId : null);
|
|
24
|
+
if (selected.storyId !== input.storyId || selected.variantId !== variantId)
|
|
25
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'Dynamic source requires the selected variant.');
|
|
26
|
+
const primary = primaryRuntime(context);
|
|
27
|
+
return context.copy(await request(context, primary.transport, 'source.get', { ...payload, variantId }, { kind: 'runtime', mountId: primary.handle.id }));
|
|
28
|
+
}
|
|
29
|
+
/** Collection belongs to explicit selected ready preview. */
|
|
30
|
+
export async function collectTests(context) {
|
|
31
|
+
const primary = primaryRuntime(context);
|
|
32
|
+
context.assertCapability('previewTests');
|
|
33
|
+
return context.copy(await request(context, primary.transport, 'tests.collect', {}, { kind: 'runtime', mountId: primary.handle.id }));
|
|
34
|
+
}
|
|
35
|
+
/** One requested engine, one attributed run; assertion failure still resolves. */
|
|
36
|
+
export async function runTests(context, options) {
|
|
37
|
+
const target = context.selected();
|
|
38
|
+
if (!target.variantId)
|
|
39
|
+
throw new HistoireSdkError('VARIANT_NOT_FOUND', 'Tests require selected variant.');
|
|
40
|
+
if (!options || !['preview', 'server'].includes(options.mode))
|
|
41
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Test mode must be preview or server.');
|
|
42
|
+
if (options.mode === 'server') {
|
|
43
|
+
context.assertCapability('serverTests');
|
|
44
|
+
if (context.snapshot.source?.mode !== 'dev')
|
|
45
|
+
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', 'Static sources have no server test engine.');
|
|
46
|
+
return context.copy(await request(context, context.assertConnected(), 'tests.run', { mode: options.mode }, { kind: 'selection' }, options.signal));
|
|
47
|
+
}
|
|
48
|
+
const primary = primaryRuntime(context);
|
|
49
|
+
context.assertCapability('previewTests');
|
|
50
|
+
return context.copy(await request(context, primary.transport, 'tests.run', { mode: options.mode }, { kind: 'runtime', mountId: primary.handle.id }, options.signal));
|
|
51
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HistoireSettingsPatch } from '@histoire/protocol';
|
|
2
|
+
import type { SessionContext } from './context.js';
|
|
3
|
+
/** Apply latest preferences once runtime is ready; never restore old mirror. */
|
|
4
|
+
export declare function sendCurrentSettings(context: SessionContext): Promise<void>;
|
|
5
|
+
/** Validate/store preferences even without primary; isolated from host document. */
|
|
6
|
+
export declare function updateSettings(context: SessionContext, patch: HistoireSettingsPatch): Promise<void>;
|
|
7
|
+
//# sourceMappingURL=settings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settings.d.ts","sourceRoot":"","sources":["../../src/session/settings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAIlD,gFAAgF;AAChF,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAc1E;AAED,oFAAoF;AACpF,wBAAsB,cAAc,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAQzG"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { validateSettingsPatch } from '@histoire/protocol';
|
|
2
|
+
import { request } from './request.js';
|
|
3
|
+
/** Apply latest preferences once runtime is ready; never restore old mirror. */
|
|
4
|
+
export function sendCurrentSettings(context) {
|
|
5
|
+
const primary = context.primaryId ? context.mounts.get(context.primaryId) : undefined;
|
|
6
|
+
const runtimeId = context.snapshot.runtime.runtimeId;
|
|
7
|
+
if (context.snapshot.status !== 'ready' || !primary?.active || context.snapshot.runtime.status !== 'ready' || !runtimeId)
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
const settings = context.snapshot.settings;
|
|
10
|
+
const previous = context.settingsSync;
|
|
11
|
+
if (previous?.mountId === primary.handle.id && previous.runtimeId === runtimeId && previous.settings === settings)
|
|
12
|
+
return previous.promise;
|
|
13
|
+
const promise = request(context, primary.transport, 'settings.update', settings, { kind: 'runtime', mountId: primary.handle.id });
|
|
14
|
+
const sync = { mountId: primary.handle.id, runtimeId, settings, promise };
|
|
15
|
+
context.settingsSync = sync;
|
|
16
|
+
void promise.catch(() => {
|
|
17
|
+
if (context.settingsSync === sync)
|
|
18
|
+
context.settingsSync = null;
|
|
19
|
+
});
|
|
20
|
+
return promise;
|
|
21
|
+
}
|
|
22
|
+
/** Validate/store preferences even without primary; isolated from host document. */
|
|
23
|
+
export async function updateSettings(context, patch) {
|
|
24
|
+
context.assertActive();
|
|
25
|
+
const validated = validateSettingsPatch(patch);
|
|
26
|
+
context.explicitSettings = context.copy({ ...context.explicitSettings, ...validated });
|
|
27
|
+
const settings = context.copy({ ...context.snapshot.settings, ...validated });
|
|
28
|
+
context.publish({ settings });
|
|
29
|
+
context.persistence.write(settings);
|
|
30
|
+
await sendCurrentSettings(context);
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HistoireCapabilities, HistoireSnapshot } from '@histoire/protocol';
|
|
2
|
+
/** Copy only validated portable projections; never freeze adapter/live objects. */
|
|
3
|
+
export declare function copyProjection<T>(value: T): T;
|
|
4
|
+
/** Freeze cyclic cleaned projections iteratively while reusing immutable branches. */
|
|
5
|
+
export declare function freezeProjection<T>(value: T): T;
|
|
6
|
+
/** Complete unavailable map before source capability negotiation. */
|
|
7
|
+
export declare function emptyCapabilities(reason?: string): HistoireCapabilities;
|
|
8
|
+
/** Initial snapshot never touches document, storage or source adapters. */
|
|
9
|
+
export declare function initialSnapshot(): HistoireSnapshot;
|
|
10
|
+
/** Readiness filters supported engines without inventing automatic runtimes. */
|
|
11
|
+
export declare function effectiveCapabilities(snapshot: HistoireSnapshot, engine: HistoireCapabilities | null): HistoireCapabilities;
|
|
12
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../../src/session/snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAGhF,mFAAmF;AACnF,wBAAgB,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAE7C;AAED,sFAAsF;AACtF,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAW/C;AAED,qEAAqE;AACrE,wBAAgB,iBAAiB,CAAC,MAAM,SAAkB,GAAG,oBAAoB,CAgBhF;AAED,2EAA2E;AAC3E,wBAAgB,eAAe,IAAI,gBAAgB,CAclD;AAED,gFAAgF;AAChF,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,MAAM,EAAE,oBAAoB,GAAG,IAAI,GAAG,oBAAoB,CAa3H"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createDefaultHistoireSettings } from '@histoire/protocol';
|
|
2
|
+
/** Copy only validated portable projections; never freeze adapter/live objects. */
|
|
3
|
+
export function copyProjection(value) {
|
|
4
|
+
return structuredClone(value);
|
|
5
|
+
}
|
|
6
|
+
/** Freeze cyclic cleaned projections iteratively while reusing immutable branches. */
|
|
7
|
+
export function freezeProjection(value) {
|
|
8
|
+
const stack = [value];
|
|
9
|
+
const seen = new WeakSet();
|
|
10
|
+
while (stack.length) {
|
|
11
|
+
const next = stack.pop();
|
|
12
|
+
if (!next || typeof next !== 'object' || seen.has(next) || Object.isFrozen(next))
|
|
13
|
+
continue;
|
|
14
|
+
seen.add(next);
|
|
15
|
+
for (const value of Object.values(next))
|
|
16
|
+
stack.push(value);
|
|
17
|
+
Object.freeze(next);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
/** Complete unavailable map before source capability negotiation. */
|
|
22
|
+
export function emptyCapabilities(reason = 'NOT_CONNECTED') {
|
|
23
|
+
const no = () => ({ available: false, reason });
|
|
24
|
+
return {
|
|
25
|
+
catalog: no(),
|
|
26
|
+
search: no(),
|
|
27
|
+
docs: no(),
|
|
28
|
+
rawSource: no(),
|
|
29
|
+
dynamicSource: no(),
|
|
30
|
+
state: no(),
|
|
31
|
+
customControls: no(),
|
|
32
|
+
previewTests: no(),
|
|
33
|
+
serverTests: no(),
|
|
34
|
+
openInEditor: no(),
|
|
35
|
+
hostChannels: no(),
|
|
36
|
+
surfaces: { explorer: no(), preview: no(), tree: no(), search: no(), toolbar: no(), controls: no(), docs: no(), source: no(), events: no(), tests: no() },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Initial snapshot never touches document, storage or source adapters. */
|
|
40
|
+
export function initialSnapshot() {
|
|
41
|
+
return freezeProjection({
|
|
42
|
+
status: 'idle',
|
|
43
|
+
stale: false,
|
|
44
|
+
source: null,
|
|
45
|
+
catalog: { stories: [], tree: [], diagnostics: [] },
|
|
46
|
+
diagnostics: [],
|
|
47
|
+
selection: null,
|
|
48
|
+
runtime: { status: 'absent', mountId: null, runtimeId: null, viewports: [], viewport: null },
|
|
49
|
+
state: null,
|
|
50
|
+
settings: createDefaultHistoireSettings(),
|
|
51
|
+
capabilities: emptyCapabilities(),
|
|
52
|
+
events: { items: [], droppedCount: 0 },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** Readiness filters supported engines without inventing automatic runtimes. */
|
|
56
|
+
export function effectiveCapabilities(snapshot, engine) {
|
|
57
|
+
if (!engine || snapshot.status !== 'ready')
|
|
58
|
+
return emptyCapabilities(snapshot.status === 'disposed' ? 'DISPOSED' : 'NOT_CONNECTED');
|
|
59
|
+
const capabilities = copyProjection(engine);
|
|
60
|
+
if (capabilities.hostChannels.available && !capabilities.hostChannels.channels?.length)
|
|
61
|
+
capabilities.hostChannels = { available: false, reason: 'CAPABILITY_UNAVAILABLE' };
|
|
62
|
+
const selected = snapshot.selection?.variantId != null;
|
|
63
|
+
const ready = selected && snapshot.runtime.status === 'ready' && snapshot.runtime.runtimeId !== null;
|
|
64
|
+
for (const name of ['state', 'dynamicSource', 'customControls', 'previewTests', 'hostChannels']) {
|
|
65
|
+
if (capabilities[name].available && !ready)
|
|
66
|
+
capabilities[name] = { ...capabilities[name], available: false, reason: selected ? 'PREVIEW_NOT_READY' : 'SELECTION_REQUIRED' };
|
|
67
|
+
}
|
|
68
|
+
if (capabilities.serverTests.available && (!selected || snapshot.source?.mode !== 'dev')) {
|
|
69
|
+
capabilities.serverTests = { available: false, reason: snapshot.source?.mode !== 'dev' ? 'CAPABILITY_UNAVAILABLE' : 'SELECTION_REQUIRED' };
|
|
70
|
+
}
|
|
71
|
+
return capabilities;
|
|
72
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HistoireStateSnapshot } from '@histoire/protocol';
|
|
2
|
+
import type { SessionContext } from './context.js';
|
|
3
|
+
/** Require explicitly owned selected runtime; no hidden automatic story mount. */
|
|
4
|
+
export declare function primaryRuntime(context: SessionContext): import("./context.js").OwnedMount;
|
|
5
|
+
/** Request canonical runtime state; host mirror is never initialization source. */
|
|
6
|
+
export declare function getState(context: SessionContext): Promise<HistoireStateSnapshot>;
|
|
7
|
+
/** Keep only explicit user patch during reload; captured target may not drift. */
|
|
8
|
+
export declare function patchState(context: SessionContext, patch: Record<string, unknown>): Promise<void>;
|
|
9
|
+
/** Runtime retains its baseline and live callbacks; SDK requests reset only. */
|
|
10
|
+
export declare function resetState(context: SessionContext): Promise<void>;
|
|
11
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/session/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAIlD,kFAAkF;AAClF,wBAAgB,cAAc,CAAC,OAAO,EAAE,cAAc,qCAKrD;AAaD,mFAAmF;AACnF,wBAAsB,QAAQ,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAItF;AAED,kFAAkF;AAClF,wBAAsB,UAAU,CAAC,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BvG;AAED,gFAAgF;AAChF,wBAAsB,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAKvE"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { HistoireSdkError, validateHistoireStateSnapshot, validateWireValue } from '@histoire/protocol';
|
|
2
|
+
import { request, waitForRuntime } from './request.js';
|
|
3
|
+
/** Require explicitly owned selected runtime; no hidden automatic story mount. */
|
|
4
|
+
export function primaryRuntime(context) {
|
|
5
|
+
context.selected();
|
|
6
|
+
const primary = context.primaryId ? context.mounts.get(context.primaryId) : undefined;
|
|
7
|
+
if (!primary?.active || context.snapshot.runtime.status !== 'ready' || !context.snapshot.runtime.runtimeId)
|
|
8
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'A ready variant runtime is required.');
|
|
9
|
+
return primary;
|
|
10
|
+
}
|
|
11
|
+
/** Accept only authoritative state for currently captured document/selection. */
|
|
12
|
+
function publishState(context, state) {
|
|
13
|
+
validateHistoireStateSnapshot(state);
|
|
14
|
+
if (!state || state.runtimeId !== context.snapshot.runtime.runtimeId || state.target.storyId !== context.snapshot.selection?.storyId || state.target.variantId !== context.snapshot.selection.variantId) {
|
|
15
|
+
throw new HistoireSdkError('RUNTIME_CHANGED', 'State reply belongs to another runtime or target.');
|
|
16
|
+
}
|
|
17
|
+
const cleaned = context.copy(state);
|
|
18
|
+
context.publish({ state: cleaned });
|
|
19
|
+
return cleaned;
|
|
20
|
+
}
|
|
21
|
+
/** Request canonical runtime state; host mirror is never initialization source. */
|
|
22
|
+
export async function getState(context) {
|
|
23
|
+
const primary = primaryRuntime(context);
|
|
24
|
+
context.assertCapability('state');
|
|
25
|
+
return publishState(context, await request(context, primary.transport, 'state.get', {}, { kind: 'runtime', mountId: primary.handle.id }));
|
|
26
|
+
}
|
|
27
|
+
/** Keep only explicit user patch during reload; captured target may not drift. */
|
|
28
|
+
export async function patchState(context, patch) {
|
|
29
|
+
context.selected();
|
|
30
|
+
context.assertCapability('state');
|
|
31
|
+
if (!patch || typeof patch !== 'object' || Array.isArray(patch))
|
|
32
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'State patch must be a plain object.');
|
|
33
|
+
if (Object.hasOwn(patch, '_hPropDefs'))
|
|
34
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Derived control definitions belong to runtime.');
|
|
35
|
+
validateWireValue(patch, { kind: 'request', name: 'state.patch' });
|
|
36
|
+
const capturedPatch = context.copy(patch);
|
|
37
|
+
const primary = context.primaryId ? context.mounts.get(context.primaryId) : undefined;
|
|
38
|
+
if (!primary?.active)
|
|
39
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'Select a variant before editing state.');
|
|
40
|
+
if (!['ready', 'mounting'].includes(context.snapshot.runtime.status))
|
|
41
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'Wait for the variant to finish loading before editing state.');
|
|
42
|
+
const connection = context.connection;
|
|
43
|
+
const source = context.snapshot.source;
|
|
44
|
+
const selectionVersion = context.selectionVersion;
|
|
45
|
+
const runtimeVersion = context.runtimeVersion;
|
|
46
|
+
/** Readiness and mutation admission retain original source, target and attachment. */
|
|
47
|
+
function assertCurrent() {
|
|
48
|
+
context.assertConnected();
|
|
49
|
+
if (connection !== context.connection || context.snapshot.source?.sourceId !== source.sourceId || context.snapshot.source.epoch !== source.epoch || context.snapshot.source.revision !== source.revision)
|
|
50
|
+
throw new HistoireSdkError('STALE_REVISION', 'Patch source changed before readiness.');
|
|
51
|
+
if (selectionVersion !== context.selectionVersion || runtimeVersion !== context.runtimeVersion || !primary.active || context.mounts.get(primary.handle.id) !== primary || context.primaryId !== primary.handle.id)
|
|
52
|
+
throw new HistoireSdkError('RUNTIME_CHANGED', 'Patch target changed before readiness.');
|
|
53
|
+
}
|
|
54
|
+
if (context.snapshot.runtime.status !== 'ready') {
|
|
55
|
+
await context.operations.run({ kind: 'selection', mountId: primary.handle.id }, signal => waitForRuntime(context, signal), assertCurrent);
|
|
56
|
+
}
|
|
57
|
+
// OperationOwner settles its waiter before this async service resumes. A source
|
|
58
|
+
// publication in that microtask gap must reject caller edit instead of recapturing it.
|
|
59
|
+
assertCurrent();
|
|
60
|
+
primaryRuntime(context);
|
|
61
|
+
const result = await request(context, primary.transport, 'state.patch', capturedPatch, { kind: 'runtime', mountId: primary.handle.id });
|
|
62
|
+
assertCurrent();
|
|
63
|
+
if (result)
|
|
64
|
+
publishState(context, result);
|
|
65
|
+
}
|
|
66
|
+
/** Runtime retains its baseline and live callbacks; SDK requests reset only. */
|
|
67
|
+
export async function resetState(context) {
|
|
68
|
+
const primary = primaryRuntime(context);
|
|
69
|
+
context.assertCapability('state');
|
|
70
|
+
const result = await request(context, primary.transport, 'state.reset', {}, { kind: 'runtime', mountId: primary.handle.id });
|
|
71
|
+
if (result)
|
|
72
|
+
publishState(context, result);
|
|
73
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { HistoireCatalogStory } from '@histoire/protocol';
|
|
2
|
+
/** Resolve one story-scoped variant before admitting selection or content work. */
|
|
3
|
+
export declare function assertVariant(story: HistoireCatalogStory, variantId: unknown): asserts variantId is string;
|
|
4
|
+
//# sourceMappingURL=variant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variant.d.ts","sourceRoot":"","sources":["../../src/session/variant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAG9D,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,KAAK,EAAE,oBAAoB,EAAE,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAK1G"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HistoireSdkError } from '@histoire/protocol';
|
|
2
|
+
/** Resolve one story-scoped variant before admitting selection or content work. */
|
|
3
|
+
export function assertVariant(story, variantId) {
|
|
4
|
+
if (typeof variantId !== 'string')
|
|
5
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Variant ID must be a string.');
|
|
6
|
+
const matches = story.variants.filter(variant => variant.id === variantId);
|
|
7
|
+
if (matches.length > 1)
|
|
8
|
+
throw new HistoireSdkError('STORY_AMBIGUOUS', 'Variant ID is ambiguous within story.', { storyId: story.id, variantId });
|
|
9
|
+
if (!matches.length || story.docsOnly)
|
|
10
|
+
throw new HistoireSdkError('VARIANT_NOT_FOUND', 'Variant ID not found in story.', { storyId: story.id, variantId });
|
|
11
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { HistoireBridgeIdentity, HistoireBridgePublication, HistoireBridgeRequest, HistoireTarget } from '@histoire/protocol';
|
|
2
|
+
import type { BridgePending, createBridgeRequests } from './requests.js';
|
|
3
|
+
/** Selection publishes synchronously; async lifetime must not capture unrelated host intent. */
|
|
4
|
+
export declare function createBridgeSelectionEcho(): {
|
|
5
|
+
/** Exact incoming intent active during synchronous parent-controller publication. */
|
|
6
|
+
readonly requestId: string | undefined;
|
|
7
|
+
/** Restore nested dispatch scope before any asynchronous work resumes. */
|
|
8
|
+
dispatch<T>(request: HistoireBridgeRequest, child: boolean, operation: () => T): T;
|
|
9
|
+
};
|
|
10
|
+
/** Source lifetime survives story failure, allowing publication-driven replacement. */
|
|
11
|
+
export declare function isBridgeSourcePublication(event: unknown): boolean;
|
|
12
|
+
/** Source events and authoritative parent snapshots survive missed document transitions. */
|
|
13
|
+
export declare function getBridgeIncomingOwner(owner: HistoireBridgeIdentity, input: Record<string, any>, child: boolean, pending?: BridgePending, cancellation?: HistoireBridgeIdentity): HistoireBridgeIdentity;
|
|
14
|
+
/** Finite source/presentation work does not execute a retired story document. */
|
|
15
|
+
export declare function isBridgeSourceRequest(command: unknown, payload: unknown): boolean;
|
|
16
|
+
/** Canonical same-epoch revision retires predecessor requests; duplicate delivery preserves current work. */
|
|
17
|
+
export declare function advanceBridgeSourceRevision(owner: HistoireBridgeIdentity, revision: string, requests: ReturnType<typeof createBridgeRequests>): HistoireBridgeIdentity;
|
|
18
|
+
/** Advance only accepted parent target transitions; presentation updates keep authority. */
|
|
19
|
+
export declare function synchronizeBridgeSelection(owner: HistoireBridgeIdentity, target: HistoireTarget | undefined): HistoireBridgeIdentity;
|
|
20
|
+
/** Target transitions settle superseded intent while exact accepted echo keeps captured ACK ownership. */
|
|
21
|
+
export declare function advanceBridgeSelection(owner: HistoireBridgeIdentity, target: HistoireTarget | undefined, requests: ReturnType<typeof createBridgeRequests>, selectionRequestId?: string): HistoireBridgeIdentity;
|
|
22
|
+
/** Host snapshot owns its predecessor generation; missing snapshots must not double-increment. */
|
|
23
|
+
export declare function acceptBridgeViewSelection(owner: HistoireBridgeIdentity, request: HistoireBridgeRequest, requests: ReturnType<typeof createBridgeRequests>): HistoireBridgeIdentity;
|
|
24
|
+
/** In-process host sync is restricted to the same locally owned session/source/mount. */
|
|
25
|
+
export declare function validateLocalBridgeSelection(owner: HistoireBridgeIdentity, localOwner: HistoireBridgeIdentity, target: HistoireTarget | null): void;
|
|
26
|
+
/** Dedicated port remembers physical document retirement independently of current selection. */
|
|
27
|
+
export declare function createBridgeRuntimeAuthority(): {
|
|
28
|
+
/** Retired documents cannot issue new intent or publish late readiness. */
|
|
29
|
+
isRetired: (runtimeId: unknown) => boolean;
|
|
30
|
+
/** Runtime metadata must agree with envelope and exact attachment before adoption. */
|
|
31
|
+
accepts(event: HistoireBridgePublication, owner: HistoireBridgeIdentity): boolean;
|
|
32
|
+
/** Failure retains attribution but permanently removes that ID's document authority. */
|
|
33
|
+
retire(event: HistoireBridgePublication, owner: HistoireBridgeIdentity): boolean;
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=authority.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authority.d.ts","sourceRoot":"","sources":["../../src/transport/authority.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,qBAAqB,EAA2B,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAC3J,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAA;AAGxE,gGAAgG;AAChG,wBAAgB,yBAAyB;IAGrC,qFAAqF;aACjF,SAAS;IACb,0EAA0E;IAC1E,QAAQ,CAAC,CAAC,WAAW,qBAAqB,SAAS,OAAO,aAAa,MAAM,CAAC,GAAG,CAAC;EAYrF;AAED,uFAAuF;AACvF,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEjE;AAED,4FAA4F;AAC5F,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,sBAAsB,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,aAAa,EAAE,YAAY,CAAC,EAAE,sBAAsB,GAAG,sBAAsB,CAaxM;AAED,iFAAiF;AACjF,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAIjF;AAED,6GAA6G;AAC7G,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,sBAAsB,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,GAAG,sBAAsB,CAGtK;AAED,4FAA4F;AAC5F,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,sBAAsB,EAAE,MAAM,EAAE,cAAc,GAAG,SAAS,GAAG,sBAAsB,CAGpI;AAED,0GAA0G;AAC1G,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,sBAAsB,EAAE,MAAM,EAAE,cAAc,GAAG,SAAS,EAAE,QAAQ,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,EAAE,kBAAkB,CAAC,EAAE,MAAM,GAAG,sBAAsB,CAIhN;AAED,kGAAkG;AAClG,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,GAAG,sBAAsB,CAKlL;AAQD,yFAAyF;AACzF,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,EAAE,UAAU,EAAE,sBAAsB,EAAE,MAAM,EAAE,cAAc,GAAG,IAAI,GAAG,IAAI,CAGnJ;AAED,gGAAgG;AAChG,wBAAgB,4BAA4B;IAGxC,2EAA2E;IAC3E,SAAS,cAAc,OAAO;IAC9B,sFAAsF;IACtF,OAAO,QAAQ,yBAAyB,SAAS,sBAAsB,GAAG,OAAO;IAIjF,wFAAwF;IACxF,MAAM,QAAQ,yBAAyB,SAAS,sBAAsB,GAAG,OAAO;EASnF"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { HistoireSdkError, validateHistoireTarget } from '@histoire/protocol';
|
|
2
|
+
/** Selection publishes synchronously; async lifetime must not capture unrelated host intent. */
|
|
3
|
+
export function createBridgeSelectionEcho() {
|
|
4
|
+
let requestId;
|
|
5
|
+
return {
|
|
6
|
+
/** Exact incoming intent active during synchronous parent-controller publication. */
|
|
7
|
+
get requestId() { return requestId; },
|
|
8
|
+
/** Restore nested dispatch scope before any asynchronous work resumes. */
|
|
9
|
+
dispatch(request, child, operation) {
|
|
10
|
+
if (child || request.command !== 'selection.select')
|
|
11
|
+
return operation();
|
|
12
|
+
const previous = requestId;
|
|
13
|
+
requestId = request.requestId;
|
|
14
|
+
try {
|
|
15
|
+
return operation();
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
requestId = previous;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Source lifetime survives story failure, allowing publication-driven replacement. */
|
|
24
|
+
export function isBridgeSourcePublication(event) {
|
|
25
|
+
return ['catalog.changed', 'content.changed', 'source.disconnected'].includes(event);
|
|
26
|
+
}
|
|
27
|
+
/** Source events and authoritative parent snapshots survive missed document transitions. */
|
|
28
|
+
export function getBridgeIncomingOwner(owner, input, child, pending, cancellation) {
|
|
29
|
+
if (pending || cancellation)
|
|
30
|
+
return pending?.owner ?? cancellation;
|
|
31
|
+
if (input.kind === 'event' && isBridgeSourcePublication(input.event))
|
|
32
|
+
return { ...owner, runtimeId: undefined, target: undefined };
|
|
33
|
+
if (child && input.kind === 'request' && input.command === 'view.sync') {
|
|
34
|
+
// A catalog publication may invalidate an earlier parent snapshot after host
|
|
35
|
+
// already advanced its target/generation. Parent snapshot can catch child up;
|
|
36
|
+
// older generations still fail validation and every source/port field stays exact.
|
|
37
|
+
const generation = typeof input.selectionVersion === 'number' && input.selectionVersion >= (owner.selectionVersion ?? 0) ? input.selectionVersion : owner.selectionVersion;
|
|
38
|
+
return { ...owner, runtimeId: undefined, target: undefined, selectionVersion: generation };
|
|
39
|
+
}
|
|
40
|
+
const readiness = input.kind === 'event' && input.event === 'readiness.changed';
|
|
41
|
+
const selecting = child && input.kind === 'request' && input.command === 'selection.select';
|
|
42
|
+
return readiness || selecting ? { ...owner, runtimeId: undefined, ...(selecting || (input.runtimeId === undefined && input.target === undefined) ? { target: undefined } : {}) } : owner;
|
|
43
|
+
}
|
|
44
|
+
/** Finite source/presentation work does not execute a retired story document. */
|
|
45
|
+
export function isBridgeSourceRequest(command, payload) {
|
|
46
|
+
if (['catalog.list', 'catalog.getStory', 'catalog.search', 'docs.get', 'openInEditor', 'settings.update', 'events.clear', 'subscriptions.add', 'subscriptions.remove', 'controls.configure'].includes(command))
|
|
47
|
+
return true;
|
|
48
|
+
const mode = payload && typeof payload === 'object' ? payload.mode : undefined;
|
|
49
|
+
return (command === 'source.get' && mode === 'raw') || (command === 'tests.run' && mode === 'server');
|
|
50
|
+
}
|
|
51
|
+
/** Canonical same-epoch revision retires predecessor requests; duplicate delivery preserves current work. */
|
|
52
|
+
export function advanceBridgeSourceRevision(owner, revision, requests) {
|
|
53
|
+
requests.reject(new HistoireSdkError('STALE_REVISION', 'Source publication changed'), item => item.owner.revision !== revision);
|
|
54
|
+
return owner.revision === revision ? owner : { ...owner, revision };
|
|
55
|
+
}
|
|
56
|
+
/** Advance only accepted parent target transitions; presentation updates keep authority. */
|
|
57
|
+
export function synchronizeBridgeSelection(owner, target) {
|
|
58
|
+
const changed = owner.target?.storyId !== target?.storyId || owner.target?.variantId !== target?.variantId;
|
|
59
|
+
return { ...owner, target, selectionVersion: (owner.selectionVersion ?? 0) + Number(changed) };
|
|
60
|
+
}
|
|
61
|
+
/** Target transitions settle superseded intent while exact accepted echo keeps captured ACK ownership. */
|
|
62
|
+
export function advanceBridgeSelection(owner, target, requests, selectionRequestId) {
|
|
63
|
+
const next = synchronizeBridgeSelection(owner, target);
|
|
64
|
+
rejectSupersededSelection(owner, next, requests, selectionRequestId);
|
|
65
|
+
return next;
|
|
66
|
+
}
|
|
67
|
+
/** Host snapshot owns its predecessor generation; missing snapshots must not double-increment. */
|
|
68
|
+
export function acceptBridgeViewSelection(owner, request, requests) {
|
|
69
|
+
const predecessor = request.selectionVersion === undefined ? owner : { ...owner, target: request.target, selectionVersion: request.selectionVersion };
|
|
70
|
+
const next = synchronizeBridgeSelection(predecessor, request.payload.selection ?? undefined);
|
|
71
|
+
rejectSupersededSelection(owner, next, requests, request.selectionRequestId);
|
|
72
|
+
return next;
|
|
73
|
+
}
|
|
74
|
+
/** Only exact synchronous parent acceptance can retain predecessor child ACK. */
|
|
75
|
+
function rejectSupersededSelection(owner, next, requests, selectionRequestId) {
|
|
76
|
+
// Same-generation runtime/settings echoes leave captured predecessor ACKs pending.
|
|
77
|
+
if (next.selectionVersion !== owner.selectionVersion)
|
|
78
|
+
requests.reject(new HistoireSdkError('RUNTIME_CHANGED', 'Selection intent superseded'), item => item.command === 'selection.select' && item.owner.selectionVersion !== next.selectionVersion && item.requestId !== selectionRequestId);
|
|
79
|
+
}
|
|
80
|
+
/** In-process host sync is restricted to the same locally owned session/source/mount. */
|
|
81
|
+
export function validateLocalBridgeSelection(owner, localOwner, target) {
|
|
82
|
+
if (['protocolVersion', 'sessionId', 'connectionId', 'mountId', 'sourceId', 'epoch'].some(key => owner[key] !== localOwner[key]))
|
|
83
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Mismatched local surface authority');
|
|
84
|
+
if (target !== null)
|
|
85
|
+
validateHistoireTarget(target);
|
|
86
|
+
}
|
|
87
|
+
/** Dedicated port remembers physical document retirement independently of current selection. */
|
|
88
|
+
export function createBridgeRuntimeAuthority() {
|
|
89
|
+
const retired = new Set();
|
|
90
|
+
return {
|
|
91
|
+
/** Retired documents cannot issue new intent or publish late readiness. */
|
|
92
|
+
isRetired: (runtimeId) => typeof runtimeId === 'string' && retired.has(runtimeId),
|
|
93
|
+
/** Runtime metadata must agree with envelope and exact attachment before adoption. */
|
|
94
|
+
accepts(event, owner) {
|
|
95
|
+
const runtime = event.payload.runtime;
|
|
96
|
+
return runtime.mountId === owner.mountId && (runtime.runtimeId ?? undefined) === event.runtimeId && !(runtime.runtimeId && retired.has(runtime.runtimeId));
|
|
97
|
+
},
|
|
98
|
+
/** Failure retains attribution but permanently removes that ID's document authority. */
|
|
99
|
+
retire(event, owner) {
|
|
100
|
+
const runtime = event.payload.runtime;
|
|
101
|
+
const terminal = ['failed', 'stale', 'absent'].includes(runtime.status);
|
|
102
|
+
const replaced = Boolean(owner.runtimeId && (owner.runtimeId !== runtime.runtimeId || terminal));
|
|
103
|
+
if (replaced)
|
|
104
|
+
retired.add(owner.runtimeId);
|
|
105
|
+
if (terminal && runtime.runtimeId)
|
|
106
|
+
retired.add(runtime.runtimeId);
|
|
107
|
+
return replaced || (terminal && runtime.runtimeId !== null);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { HistoireConnectContext, HistoireSourceConnection } from '../adapters/types.js';
|
|
2
|
+
/** Explicit browser-only metadata connection; bridge imports no story modules. */
|
|
3
|
+
export declare function connectRemoteSource(context: HistoireConnectContext): Promise<HistoireSourceConnection>;
|
|
4
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/transport/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAA;AAI5F,kFAAkF;AAClF,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAgC5G"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { HistoireSdkError } from '@histoire/protocol';
|
|
2
|
+
import { createBridgeFrame } from './handshake.js';
|
|
3
|
+
import { createBridgePort } from './port.js';
|
|
4
|
+
/** Explicit browser-only metadata connection; bridge imports no story modules. */
|
|
5
|
+
export async function connectRemoteSource(context) {
|
|
6
|
+
if (typeof document === 'undefined' || typeof window === 'undefined') {
|
|
7
|
+
throw new HistoireSdkError('BROWSER_REQUIRED', 'Remote connection requires browser');
|
|
8
|
+
}
|
|
9
|
+
if (!document.body) {
|
|
10
|
+
throw new HistoireSdkError('BROWSER_REQUIRED', 'Remote connection requires document body');
|
|
11
|
+
}
|
|
12
|
+
let bridge;
|
|
13
|
+
const frame = createBridgeFrame({ ...context, mountId: `${context.sessionId}:bridge`, role: 'data', container: document.body, disconnected: () => bridge?.disconnect() });
|
|
14
|
+
try {
|
|
15
|
+
const { port, ack } = await frame.ready;
|
|
16
|
+
bridge = createBridgePort({ port, owner: ack.owner, role: 'data', descriptor: ack.descriptor });
|
|
17
|
+
let descriptor = ack.descriptor;
|
|
18
|
+
bridge.subscribe((event) => {
|
|
19
|
+
if (event.type === 'catalog') {
|
|
20
|
+
descriptor = event.descriptor;
|
|
21
|
+
}
|
|
22
|
+
else if (event.type === 'disconnect') {
|
|
23
|
+
frame.close();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return { ...bridge, get descriptor() {
|
|
27
|
+
return descriptor;
|
|
28
|
+
}, initialSettings: { colorScheme: descriptor.config?.theme.defaultColorScheme ?? 'auto', globals: descriptor.config?.globals ?? {}, textDirection: descriptor.config?.textDirection ?? 'ltr' }, close() {
|
|
29
|
+
bridge?.close();
|
|
30
|
+
frame.close();
|
|
31
|
+
} };
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
frame.close();
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { HistoireBridgeAck, HistoireEmbedPresentation, HistoirePortRole } from '@histoire/protocol';
|
|
2
|
+
/** One explicit browser attachment; constructing SDK never evaluates these fields. */
|
|
3
|
+
export interface BridgeFrameOptions {
|
|
4
|
+
/** Absolute normalized book base. */
|
|
5
|
+
url: string;
|
|
6
|
+
/** Parent controller identity. */
|
|
7
|
+
sessionId: string;
|
|
8
|
+
/** Unique bridge or surface identity. */
|
|
9
|
+
mountId: string;
|
|
10
|
+
/** First-party document authority. */
|
|
11
|
+
role: HistoirePortRole;
|
|
12
|
+
/** Explicit requested view, absent for metadata bridge. */
|
|
13
|
+
surface?: string;
|
|
14
|
+
/** Validated explorer-only presentation hints. */
|
|
15
|
+
presentation?: HistoireEmbedPresentation;
|
|
16
|
+
/** Validated variant gutter override in CSS pixels. */
|
|
17
|
+
padding?: number;
|
|
18
|
+
/** Owned attachment container. */
|
|
19
|
+
container: HTMLElement;
|
|
20
|
+
/** Cancellation affects handshake immediately. */
|
|
21
|
+
signal: AbortSignal;
|
|
22
|
+
/** Navigation retires current port; reconnect remains explicit. */
|
|
23
|
+
disconnected?: () => void;
|
|
24
|
+
}
|
|
25
|
+
/** Create one frame and transferred port, with exact-origin port-only acknowledgment. */
|
|
26
|
+
export declare function createBridgeFrame(options: BridgeFrameOptions): {
|
|
27
|
+
iframe: HTMLIFrameElement;
|
|
28
|
+
ready: Promise<{
|
|
29
|
+
port: MessagePort;
|
|
30
|
+
ack: Extract<HistoireBridgeAck, {
|
|
31
|
+
ok: true;
|
|
32
|
+
}>;
|
|
33
|
+
}>;
|
|
34
|
+
close: () => void;
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=handshake.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.d.ts","sourceRoot":"","sources":["../../src/transport/handshake.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAuB,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAG7H,sFAAsF;AACtF,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,kCAAkC;IAClC,SAAS,EAAE,MAAM,CAAA;IACjB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAA;IACf,sCAAsC;IACtC,IAAI,EAAE,gBAAgB,CAAA;IACtB,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,kDAAkD;IAClD,YAAY,CAAC,EAAE,yBAAyB,CAAA;IACxC,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,kCAAkC;IAClC,SAAS,EAAE,WAAW,CAAA;IACtB,kDAAkD;IAClD,MAAM,EAAE,WAAW,CAAA;IACnB,mEAAmE;IACnE,YAAY,CAAC,EAAE,MAAM,IAAI,CAAA;CAC1B;AAED,yFAAyF;AACzF,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB;;;cA0BnD,WAAW;aACZ,OAAO,CAAC,iBAAiB,EAAE;YAC9B,EAAE,EAAE,IAAI,CAAA;SACT,CAAC;;iBA2Bc,IAAI;EAsDvB"}
|