@histoire/sdk 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.
- package/LICENSE +21 -0
- package/dist/adapters/types.d.ts +150 -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 +150 -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 +34 -0
- package/dist/transport/handshake.d.ts.map +1 -0
- package/dist/transport/handshake.js +110 -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 +140 -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,150 @@
|
|
|
1
|
+
import { HistoireSdkError, validateEmbedPresentation } from '@histoire/protocol';
|
|
2
|
+
import { bindHistoireMountChannel } from '../mounts/channel.js';
|
|
3
|
+
import { receiveNotification } from './notifications.js';
|
|
4
|
+
import { publishRuntime } from './runtime.js';
|
|
5
|
+
import { sendCurrentSettings } from './settings.js';
|
|
6
|
+
/** Surfaces owning the one canonical story runtime. */
|
|
7
|
+
const primarySurfaces = new Set(['explorer', 'preview']);
|
|
8
|
+
/** Finite surface validation also applies to untyped JavaScript callers. */
|
|
9
|
+
const surfaces = new Set(['explorer', 'preview', 'tree', 'search', 'toolbar', 'controls', 'docs', 'source', 'events', 'tests']);
|
|
10
|
+
/** Mark inactive before awaits; release primary only after complete cleanup. */
|
|
11
|
+
function unmount(context, owned) {
|
|
12
|
+
if (owned.closing)
|
|
13
|
+
return owned.closing;
|
|
14
|
+
owned.active = false;
|
|
15
|
+
let observerError;
|
|
16
|
+
const id = owned.handle.id;
|
|
17
|
+
context.operations.reject('RUNTIME_CHANGED', 'Surface unmounted.', scope => scope.mountId === id);
|
|
18
|
+
if (context.primaryId === id) {
|
|
19
|
+
context.runtimeVersion++;
|
|
20
|
+
context.publish({ state: null, events: { items: [], droppedCount: 0 }, runtime: { ...context.snapshot.runtime, status: 'absent', runtimeId: null, viewports: [], viewport: null } });
|
|
21
|
+
}
|
|
22
|
+
owned.closing = Promise.resolve().then(async () => {
|
|
23
|
+
try {
|
|
24
|
+
await owned.transport.close();
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (observerError)
|
|
28
|
+
throw new AggregateError([observerError, error], 'Surface teardown failed');
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
context.mounts.delete(id);
|
|
32
|
+
if (context.primaryId === id) {
|
|
33
|
+
context.primaryId = null;
|
|
34
|
+
context.publish({ runtime: { status: 'absent', mountId: null, runtimeId: null, viewports: [], viewport: null } });
|
|
35
|
+
}
|
|
36
|
+
if (observerError)
|
|
37
|
+
throw observerError;
|
|
38
|
+
}).catch((error) => {
|
|
39
|
+
// A failed adapter close gives no proof that its runtime stopped. Keep the
|
|
40
|
+
// slot quarantined so replacement cannot execute alongside that document.
|
|
41
|
+
if (context.primaryId === id && context.snapshot.status !== 'disposed') {
|
|
42
|
+
context.publish({ runtime: { ...context.snapshot.runtime, status: 'failed' } });
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
});
|
|
46
|
+
void owned.closing.catch(() => { });
|
|
47
|
+
// Retire private channels synchronously after cleanup promise exists. Observer
|
|
48
|
+
// failures cannot skip transport teardown or leave primary slot unreleased.
|
|
49
|
+
try {
|
|
50
|
+
owned.unsubscribe();
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
observerError = error;
|
|
54
|
+
}
|
|
55
|
+
return owned.closing;
|
|
56
|
+
}
|
|
57
|
+
/** Reserve primary synchronously before invoking any browser/source adapter. */
|
|
58
|
+
export function mountSurface(context, container, surface, hidden = false, input) {
|
|
59
|
+
const connection = context.assertConnected();
|
|
60
|
+
if (!surfaces.has(surface))
|
|
61
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Unknown Histoire surface.');
|
|
62
|
+
if (input !== undefined && surface !== 'explorer')
|
|
63
|
+
throw new HistoireSdkError('INVALID_ARGUMENT', 'Embed presentation requires explorer surface');
|
|
64
|
+
const presentation = input === undefined ? undefined : validateEmbedPresentation(input);
|
|
65
|
+
if (presentation && !context.snapshot.selection?.variantId)
|
|
66
|
+
throw new HistoireSdkError('VARIANT_NOT_FOUND', 'Select a variant before mounting an embed');
|
|
67
|
+
const primary = primarySurfaces.has(surface);
|
|
68
|
+
if (primary && context.primaryId !== null)
|
|
69
|
+
throw new HistoireSdkError('RUNTIME_IN_USE', 'Session already owns a primary runtime.');
|
|
70
|
+
if (!context.engine?.surfaces[surface].available)
|
|
71
|
+
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', `Source does not support surface ${surface}.`);
|
|
72
|
+
if (!context.adapters.mount)
|
|
73
|
+
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', 'Surface adapter unavailable.');
|
|
74
|
+
const target = context.snapshot.selection;
|
|
75
|
+
const source = context.snapshot.source;
|
|
76
|
+
const selectionVersion = context.selectionVersion;
|
|
77
|
+
const requiresRuntime = primary && target?.variantId != null;
|
|
78
|
+
const mountId = `${context.sessionId}:mount:${++context.mountCounter}`;
|
|
79
|
+
if (primary)
|
|
80
|
+
context.primaryId = mountId;
|
|
81
|
+
let transport;
|
|
82
|
+
try {
|
|
83
|
+
transport = context.adapters.mount({ session: context.session, sessionId: context.sessionId, mountId, surface, presentation, hidden, container, source: connection, target, settings: context.snapshot.settings });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (primary)
|
|
87
|
+
context.primaryId = null;
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
let owned;
|
|
91
|
+
let closeChannel = () => { };
|
|
92
|
+
const handle = { id: mountId, ready: Promise.resolve(), unmount: () => unmount(context, owned) };
|
|
93
|
+
owned = { handle, transport, unsubscribe: () => { }, active: true, hidden, runtimeId: null, retiredRuntimeIds: new Set() };
|
|
94
|
+
context.mounts.set(mountId, owned);
|
|
95
|
+
try {
|
|
96
|
+
closeChannel = bindHistoireMountChannel(handle, context.session, transport.channel);
|
|
97
|
+
owned.unsubscribe = closeChannel;
|
|
98
|
+
const stop = transport.subscribe(event => receiveNotification(context, event, mountId));
|
|
99
|
+
owned.unsubscribe = () => {
|
|
100
|
+
try {
|
|
101
|
+
closeChannel();
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
stop();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
void unmount(context, owned).catch(() => { });
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
if (primary) {
|
|
113
|
+
context.publish({ state: null, runtime: { status: 'mounting', mountId, runtimeId: null, viewports: [], viewport: null } });
|
|
114
|
+
}
|
|
115
|
+
/** Capture before adapter/publication callbacks; late completion cannot adopt a successor. */
|
|
116
|
+
function assertCurrent() {
|
|
117
|
+
context.assertConnected();
|
|
118
|
+
if (context.connection !== connection || context.snapshot.source?.sourceId !== source.sourceId || context.snapshot.source.epoch !== source.epoch || context.snapshot.source.revision !== source.revision)
|
|
119
|
+
throw new HistoireSdkError('STALE_REVISION', 'Source changed before surface readiness.');
|
|
120
|
+
if (!owned.active || context.mounts.get(mountId) !== owned || (primary && context.primaryId !== mountId) || (requiresRuntime && selectionVersion !== context.selectionVersion))
|
|
121
|
+
throw new HistoireSdkError('RUNTIME_CHANGED', 'Surface owner changed before readiness.');
|
|
122
|
+
}
|
|
123
|
+
handle.ready = context.operations.run({ kind: 'mount', mountId }, async (signal) => {
|
|
124
|
+
const runtime = await transport.ready;
|
|
125
|
+
signal.throwIfAborted();
|
|
126
|
+
assertCurrent();
|
|
127
|
+
if (primary && runtime) {
|
|
128
|
+
if (runtime.mountId !== mountId || runtime.status !== 'ready' || !runtime.runtimeId)
|
|
129
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'Surface did not supply an actual ready runtime.');
|
|
130
|
+
if (!publishRuntime(context, owned, runtime))
|
|
131
|
+
throw new HistoireSdkError('RUNTIME_CHANGED', 'Surface readiness belongs to a retired document.');
|
|
132
|
+
// Runtime publication invokes observers synchronously. Recheck ownership
|
|
133
|
+
// before settings dispatch so that callback cannot transfer this completion.
|
|
134
|
+
signal.throwIfAborted();
|
|
135
|
+
assertCurrent();
|
|
136
|
+
await sendCurrentSettings(context);
|
|
137
|
+
signal.throwIfAborted();
|
|
138
|
+
assertCurrent();
|
|
139
|
+
}
|
|
140
|
+
// Initial data-only mount owns view acknowledgement. Later selection is a
|
|
141
|
+
// separate operation and cannot turn that captured acknowledgement into failure.
|
|
142
|
+
if (requiresRuntime && context.snapshot.runtime.status !== 'ready')
|
|
143
|
+
throw new HistoireSdkError('PREVIEW_NOT_READY', 'Story runtime is not ready.');
|
|
144
|
+
}, assertCurrent);
|
|
145
|
+
void handle.ready.catch(() => {
|
|
146
|
+
if (owned.active && context.primaryId === mountId && context.snapshot.status === 'ready' && context.connection === connection && context.snapshot.source?.sourceId === source.sourceId && context.snapshot.source.epoch === source.epoch && context.snapshot.source.revision === source.revision && context.snapshot.runtime.status === 'mounting' && selectionVersion === context.selectionVersion)
|
|
147
|
+
context.publish({ runtime: { ...context.snapshot.runtime, status: 'failed' } });
|
|
148
|
+
});
|
|
149
|
+
return handle;
|
|
150
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { HistoireSessionNotification } from '../adapters/types.js';
|
|
2
|
+
import type { SessionContext } from './context.js';
|
|
3
|
+
/** Gate every validated notification against current source and attachment. */
|
|
4
|
+
export declare function receiveNotification(context: SessionContext, notification: HistoireSessionNotification, mountId?: string): void;
|
|
5
|
+
//# sourceMappingURL=notifications.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notifications.d.ts","sourceRoot":"","sources":["../../src/session/notifications.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAA;AACvE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AASlD,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,2BAA2B,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAyH9H"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { validateHistoireEvent, validateHistoireStateSnapshot, validateHistoireViewports, validateWireValue } from '@histoire/protocol';
|
|
2
|
+
import { isValidatedNotification } from '../transport/validated.js';
|
|
3
|
+
import { appendEvent } from './events.js';
|
|
4
|
+
import { acceptHistoireSourceDescriptor, isAcceptedHistoireSourceDescriptor } from './internal.js';
|
|
5
|
+
import { publishRuntime } from './runtime.js';
|
|
6
|
+
import { reconcileSelection } from './selection.js';
|
|
7
|
+
import { sendCurrentSettings } from './settings.js';
|
|
8
|
+
/** Gate every validated notification against current source and attachment. */
|
|
9
|
+
export function receiveNotification(context, notification, mountId) {
|
|
10
|
+
const mount = mountId ? context.mounts.get(mountId) : undefined;
|
|
11
|
+
const connectionId = mountId ? mount?.transport.id : context.connection?.id;
|
|
12
|
+
if (context.snapshot.status !== 'ready' || notification.connectionId !== connectionId || notification.sourceId !== context.snapshot.source?.sourceId)
|
|
13
|
+
return;
|
|
14
|
+
if (mountId && (!mount?.active || notification.mountId !== mountId || context.primaryId !== mountId))
|
|
15
|
+
return;
|
|
16
|
+
if (notification.type === 'catalog' && !mountId) {
|
|
17
|
+
let descriptor;
|
|
18
|
+
try {
|
|
19
|
+
descriptor = isAcceptedHistoireSourceDescriptor(notification.descriptor)
|
|
20
|
+
? notification.descriptor
|
|
21
|
+
: acceptHistoireSourceDescriptor(notification.descriptor);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
context.publish({ stale: true });
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (descriptor.sourceId !== context.snapshot.source.sourceId || descriptor.epoch !== notification.epoch || descriptor.revision !== notification.revision)
|
|
28
|
+
return;
|
|
29
|
+
// One source descriptor remains canonical for first-party config getters.
|
|
30
|
+
context.descriptor = descriptor;
|
|
31
|
+
const changed = descriptor.epoch !== context.snapshot.source.epoch || descriptor.revision !== context.snapshot.source.revision;
|
|
32
|
+
if (changed)
|
|
33
|
+
context.operations.reject('STALE_REVISION', 'Source publication changed.');
|
|
34
|
+
context.engine = context.copy(descriptor.capabilities);
|
|
35
|
+
const catalog = context.copy(descriptor.catalog);
|
|
36
|
+
// Reconcile against the new catalog before notifying any external observer.
|
|
37
|
+
const previous = context.snapshot;
|
|
38
|
+
const selection = reconcileSelection(context, catalog);
|
|
39
|
+
const targetChanged = selection?.storyId !== previous.selection?.storyId || selection?.variantId !== previous.selection?.variantId;
|
|
40
|
+
if (targetChanged) {
|
|
41
|
+
context.selectionVersion++;
|
|
42
|
+
context.runtimeVersion++;
|
|
43
|
+
}
|
|
44
|
+
context.publish({
|
|
45
|
+
source: context.copy({ ...previous.source, epoch: descriptor.epoch, revision: descriptor.revision, mode: descriptor.mode }),
|
|
46
|
+
catalog,
|
|
47
|
+
inspection: context.copy(descriptor.inspection),
|
|
48
|
+
diagnostics: catalog.diagnostics,
|
|
49
|
+
selection: context.copy(selection),
|
|
50
|
+
stale: false,
|
|
51
|
+
...(targetChanged || descriptor.epoch !== previous.source?.epoch ? { state: null, runtime: { ...previous.runtime, status: context.primaryId ? 'stale' : 'absent', runtimeId: null, viewports: [], viewport: null } } : {}),
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (notification.epoch !== context.snapshot.source?.epoch || notification.revision !== context.snapshot.source.revision)
|
|
56
|
+
return;
|
|
57
|
+
if (notification.type === 'disconnect' && !mountId) {
|
|
58
|
+
context.operations.reject('NOT_CONNECTED', 'Source disconnected.');
|
|
59
|
+
context.runtimeVersion++;
|
|
60
|
+
context.publish({ status: 'disconnected', stale: true, runtime: { ...context.snapshot.runtime, status: context.primaryId ? 'stale' : 'absent', viewports: [], viewport: null } });
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (notification.type === 'dropped') {
|
|
64
|
+
if (!Number.isSafeInteger(notification.count) || notification.count < 0)
|
|
65
|
+
return;
|
|
66
|
+
context.publish({ events: { ...context.snapshot.events, droppedCount: context.snapshot.events.droppedCount + notification.count }, ...(notification.stale ? { stale: true } : {}) });
|
|
67
|
+
if (notification.stale)
|
|
68
|
+
context.operations.reject('STALE_REVISION', 'Required publication was dropped.');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!mountId)
|
|
72
|
+
return;
|
|
73
|
+
if (notification.type === 'runtime') {
|
|
74
|
+
const runtime = notification.runtime;
|
|
75
|
+
if (runtime.mountId !== mountId || runtime.runtimeId !== (notification.runtimeId ?? null))
|
|
76
|
+
return;
|
|
77
|
+
try {
|
|
78
|
+
if (!publishRuntime(context, mount, runtime))
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
context.publish({ stale: true });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (runtime.status === 'ready')
|
|
86
|
+
void sendCurrentSettings(context).catch(() => { });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (notification.runtimeId !== context.snapshot.runtime.runtimeId || context.snapshot.runtime.status !== 'ready')
|
|
90
|
+
return;
|
|
91
|
+
// Bridge ports already sized and schema-checked these exact payload objects (the
|
|
92
|
+
// per-event and viewport checks below are subsets of the envelope checks).
|
|
93
|
+
const validated = isValidatedNotification(notification);
|
|
94
|
+
if (notification.type === 'layout') {
|
|
95
|
+
if (!validated) {
|
|
96
|
+
try {
|
|
97
|
+
validateWireValue({ viewports: notification.viewports }, { kind: 'event', name: 'layout.changed' });
|
|
98
|
+
validateHistoireViewports(notification.viewports);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
context.publish({ stale: true });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const viewports = mount?.hidden ? [] : context.copy(notification.viewports);
|
|
106
|
+
const viewport = viewports.find(viewport => viewport.target.storyId === context.snapshot.selection?.storyId && viewport.target.variantId === context.snapshot.selection.variantId) ?? null;
|
|
107
|
+
context.publish({ runtime: { ...context.snapshot.runtime, viewports, viewport } });
|
|
108
|
+
}
|
|
109
|
+
else if (notification.type === 'state') {
|
|
110
|
+
const state = notification.state;
|
|
111
|
+
if (state.runtimeId !== notification.runtimeId || state.target.storyId !== context.snapshot.selection?.storyId || state.target.variantId !== context.snapshot.selection.variantId)
|
|
112
|
+
return;
|
|
113
|
+
if (!validated) {
|
|
114
|
+
try {
|
|
115
|
+
validateHistoireStateSnapshot(state);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
context.publish({ stale: true });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
context.publish({ state: context.copy(state) });
|
|
123
|
+
}
|
|
124
|
+
else if (notification.type === 'event') {
|
|
125
|
+
if (!validated) {
|
|
126
|
+
try {
|
|
127
|
+
validateWireValue(notification.event, { kind: 'event', name: 'events.appended' });
|
|
128
|
+
validateHistoireEvent(notification.event);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
context.publish({ events: { ...context.snapshot.events, droppedCount: context.snapshot.events.droppedCount + 1 } });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
appendEvent(context, notification.event);
|
|
136
|
+
}
|
|
137
|
+
else if (notification.type === 'channel') {
|
|
138
|
+
context.deliverChannel?.(notification.message);
|
|
139
|
+
}
|
|
140
|
+
else if (notification.type === 'plugin') {
|
|
141
|
+
context.deliverPlugin?.(notification.message);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { HistoireErrorCode } from '@histoire/protocol';
|
|
2
|
+
/** Observe public async wrappers as well as underlying owned transport work. */
|
|
3
|
+
export declare function observeOperation<T>(promise: Promise<T>): Promise<T>;
|
|
4
|
+
/** Request invalidation scope; runtime work does not cancel unrelated docs reads. */
|
|
5
|
+
export interface OperationScope {
|
|
6
|
+
/** Owning operation category. */
|
|
7
|
+
kind: 'connect' | 'source' | 'runtime' | 'selection' | 'mount' | 'inspection';
|
|
8
|
+
/** Exact surface when operation belongs to an attachment. */
|
|
9
|
+
mountId?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Per-session registry; late adapter promises stay observed after invalidation. */
|
|
12
|
+
export declare class OperationOwner {
|
|
13
|
+
/** Currently attributable unsettled work. */
|
|
14
|
+
private readonly pending;
|
|
15
|
+
/** Tracks one operation and observes all late success/failure paths. */
|
|
16
|
+
run<T>(scope: OperationScope, task: (signal: AbortSignal) => Promise<T>, guard: () => void, signal?: AbortSignal): Promise<T>;
|
|
17
|
+
/** Invalidates matching work without waiting for uncooperative user code. */
|
|
18
|
+
reject(code: HistoireErrorCode, message: string, matches?: (scope: OperationScope) => boolean): void;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=ownership.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ownership.d.ts","sourceRoot":"","sources":["../../src/session/ownership.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAG3D,gFAAgF;AAChF,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAGnE;AAED,qFAAqF;AACrF,MAAM,WAAW,cAAc;IAC7B,iCAAiC;IACjC,IAAI,EAAE,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,WAAW,GAAG,OAAO,GAAG,YAAY,CAAA;IAC7E,6DAA6D;IAC7D,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAQD,oFAAoF;AACpF,qBAAa,cAAc;IACzB,6CAA6C;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8B;IAEtD,wEAAwE;IACxE,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CA+C5H;IAED,6EAA6E;IAC7E,MAAM,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,CAAC,KAAK,EAAE,cAAc,KAAK,OAAoB,GAAG,IAAI,CAK/G;CACF"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { HistoireSdkError } from '@histoire/protocol';
|
|
2
|
+
/** Observe public async wrappers as well as underlying owned transport work. */
|
|
3
|
+
export function observeOperation(promise) {
|
|
4
|
+
void promise.catch(() => { });
|
|
5
|
+
return promise;
|
|
6
|
+
}
|
|
7
|
+
/** Per-session registry; late adapter promises stay observed after invalidation. */
|
|
8
|
+
export class OperationOwner {
|
|
9
|
+
/** Currently attributable unsettled work. */
|
|
10
|
+
pending = new Set();
|
|
11
|
+
/** Tracks one operation and observes all late success/failure paths. */
|
|
12
|
+
run(scope, task, guard, signal) {
|
|
13
|
+
const abort = new AbortController();
|
|
14
|
+
let cleanup = () => { };
|
|
15
|
+
const promise = new Promise((resolve, reject) => {
|
|
16
|
+
let settled = false;
|
|
17
|
+
const finish = (complete) => {
|
|
18
|
+
if (settled)
|
|
19
|
+
return;
|
|
20
|
+
settled = true;
|
|
21
|
+
cleanup();
|
|
22
|
+
complete();
|
|
23
|
+
};
|
|
24
|
+
const operation = {
|
|
25
|
+
...scope,
|
|
26
|
+
cancel: error => finish(() => {
|
|
27
|
+
abort.abort(error);
|
|
28
|
+
reject(error);
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
const onAbort = () => operation.cancel(new HistoireSdkError('CANCELLED', 'Operation cancelled.'));
|
|
32
|
+
cleanup = () => {
|
|
33
|
+
this.pending.delete(operation);
|
|
34
|
+
signal?.removeEventListener('abort', onAbort);
|
|
35
|
+
};
|
|
36
|
+
this.pending.add(operation);
|
|
37
|
+
if (signal?.aborted) {
|
|
38
|
+
onAbort();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
42
|
+
Promise.resolve().then(() => {
|
|
43
|
+
guard();
|
|
44
|
+
if (abort.signal.aborted)
|
|
45
|
+
throw abort.signal.reason;
|
|
46
|
+
return task(abort.signal);
|
|
47
|
+
}).then(value => finish(() => {
|
|
48
|
+
try {
|
|
49
|
+
guard();
|
|
50
|
+
resolve(value);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
reject(error);
|
|
54
|
+
}
|
|
55
|
+
}), error => finish(() => reject(error)));
|
|
56
|
+
});
|
|
57
|
+
// Callers may abandon ready/connect/request promises; teardown still owns them.
|
|
58
|
+
void promise.catch(() => { });
|
|
59
|
+
return promise;
|
|
60
|
+
}
|
|
61
|
+
/** Invalidates matching work without waiting for uncooperative user code. */
|
|
62
|
+
reject(code, message, matches = () => true) {
|
|
63
|
+
const error = new HistoireSdkError(code, message);
|
|
64
|
+
for (const operation of [...this.pending]) {
|
|
65
|
+
if (matches(operation))
|
|
66
|
+
operation.cancel(error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { HistoireErrorCode, HistoirePluginChannel, HistoirePluginChannelEvent, HistoirePluginChannelPayload, HistoireTarget } from '@histoire/protocol';
|
|
2
|
+
/** Attached frame plugin actor as one host sees it. */
|
|
3
|
+
export interface HistoirePluginHubActor {
|
|
4
|
+
/** Plugin attached to the actor. */
|
|
5
|
+
readonly pluginId: string;
|
|
6
|
+
/** Rendered mount identity minted by the frame runtime. */
|
|
7
|
+
readonly actorId: string;
|
|
8
|
+
/** Variant rendered by the actor. */
|
|
9
|
+
readonly target: HistoireTarget;
|
|
10
|
+
/** Runtime document hosting the actor. */
|
|
11
|
+
readonly runtimeId: string;
|
|
12
|
+
/** Host frame identity (canvas frame key); SDK sessions use the runtime document id. */
|
|
13
|
+
readonly frameKey: string;
|
|
14
|
+
}
|
|
15
|
+
/** Transport of one host; delivery is the adapter's job, accounting and lifetime the hub's. */
|
|
16
|
+
export interface PluginChannelHubOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Delivers one validated host-to-frame message to the actor's document.
|
|
19
|
+
* @param signal Aborted when the actor retires; implementations cancel pending work.
|
|
20
|
+
*/
|
|
21
|
+
send: (actor: HistoirePluginHubActor, payload: HistoirePluginChannelPayload, signal: AbortSignal) => Promise<unknown> | unknown;
|
|
22
|
+
/** Clock for rate windows. */
|
|
23
|
+
now?: () => number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Host side of plugin channels shared by SDK sessions and the standalone workbench: actor
|
|
27
|
+
* registry from runtime notices, per-plugin accounting per runtime document, delivery by actor id
|
|
28
|
+
* (never by selected variant), retirement without queues or replay. Source, origin and document
|
|
29
|
+
* checks stay with each adapter before `receive`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createPluginChannelHub(options: PluginChannelHubOptions): {
|
|
32
|
+
retire: (predicate: (actor: HistoirePluginHubActor) => boolean, code: HistoireErrorCode) => boolean;
|
|
33
|
+
reject: (message: {
|
|
34
|
+
pluginId?: unknown;
|
|
35
|
+
type?: unknown;
|
|
36
|
+
} | null | undefined) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Admits one frame publication whose source and document the adapter already verified.
|
|
39
|
+
* @param event Attributed message or runtime notice.
|
|
40
|
+
* @param frameKey Host frame the document belongs to.
|
|
41
|
+
*/
|
|
42
|
+
receive(event: HistoirePluginChannelEvent, frameKey: string): void;
|
|
43
|
+
/** Attached actors of one plugin, in attach order. */
|
|
44
|
+
actors(pluginId: string): HistoirePluginHubActor[];
|
|
45
|
+
/**
|
|
46
|
+
* Opens a channel bound to exactly one attached actor; it never retargets.
|
|
47
|
+
* @param pluginId Plugin owning the channel.
|
|
48
|
+
* @param actorId Attached actor.
|
|
49
|
+
* @param owner Aborting it releases this channel's listeners and rejects its later posts.
|
|
50
|
+
*/
|
|
51
|
+
open(pluginId: string, actorId: string, owner?: AbortSignal): HistoirePluginChannel;
|
|
52
|
+
/** Observes attach and detach. */
|
|
53
|
+
subscribe(listener: () => void): () => void;
|
|
54
|
+
/** Observes frame failures attributed to one plugin. */
|
|
55
|
+
onError(pluginId: string, listener: (error: Error) => void): () => void;
|
|
56
|
+
};
|
|
57
|
+
/** Host-side plugin channel registry. */
|
|
58
|
+
export type PluginChannelHub = ReturnType<typeof createPluginChannelHub>;
|
|
59
|
+
//# sourceMappingURL=plugin-hub.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-hub.d.ts","sourceRoot":"","sources":["../../src/session/plugin-hub.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAqB,qBAAqB,EAAE,0BAA0B,EAA+D,4BAA4B,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAI5O,uDAAuD;AACvD,MAAM,WAAW,sBAAsB;IACrC,oCAAoC;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,2DAA2D;IAC3D,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,qCAAqC;IACrC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;IAC/B,0CAA0C;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,wFAAwF;IACxF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAC1B;AAED,+FAA+F;AAC/F,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,EAAE,4BAA4B,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAA;IAC/H,8BAA8B;IAC9B,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;CACnB;AAiCD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB;wBA6B1C,CAAC,KAAK,EAAE,sBAAsB,KAAK,OAAO,QAAQ,iBAAiB,KAAG,OAAO;sBAkB/E;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,GAAG,SAAS,KAAG,IAAI;IAyBvF;;;;OAIG;mBACY,0BAA0B,YAAY,MAAM,GAAG,IAAI;IAwClE,sDAAsD;qBACrC,MAAM,GAAG,sBAAsB,EAAE;IAGlD;;;;;OAKG;mBACY,MAAM,WAAW,MAAM,UAAU,WAAW,GAAG,qBAAqB;IA2CnF,kCAAkC;wBACd,MAAM,IAAI,GAAG,MAAM,IAAI;IAI3C,wDAAwD;sBACtC,MAAM,YAAY,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM,IAAI;EAM1E;AAED,yCAAyC;AACzC,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAA"}
|