@astrale-os/shell 0.1.0 → 0.2.0
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 +202 -0
- package/dist/application/apps.d.ts +18 -0
- package/dist/application/apps.d.ts.map +1 -0
- package/dist/application/apps.js +35 -0
- package/dist/application/namespaces.d.ts +21 -0
- package/dist/application/namespaces.d.ts.map +1 -0
- package/dist/application/namespaces.js +12 -0
- package/dist/application/session/open.d.ts +19 -0
- package/dist/application/session/open.d.ts.map +1 -0
- package/dist/application/session/open.js +46 -0
- package/dist/application/session/session.d.ts +41 -0
- package/dist/application/session/session.d.ts.map +1 -0
- package/dist/application/session/session.js +9 -0
- package/dist/application/session/token.d.ts +24 -0
- package/dist/application/session/token.d.ts.map +1 -0
- package/dist/application/session/token.js +51 -0
- package/dist/application/spaces.d.ts +17 -0
- package/dist/application/spaces.d.ts.map +1 -0
- package/dist/application/spaces.js +30 -0
- package/dist/application/user.d.ts +20 -0
- package/dist/application/user.d.ts.map +1 -0
- package/dist/application/user.js +26 -0
- package/dist/application/view.d.ts +20 -0
- package/dist/application/view.d.ts.map +1 -0
- package/dist/application/view.js +18 -0
- package/dist/{application/channels/children-channel.d.ts → channel/children.d.ts} +4 -4
- package/dist/channel/children.d.ts.map +1 -0
- package/dist/channel/listeners.d.ts +8 -0
- package/dist/channel/listeners.d.ts.map +1 -0
- package/dist/{adapters/message-port.transport.d.ts → channel/message-port.d.ts} +2 -2
- package/dist/channel/message-port.d.ts.map +1 -0
- package/dist/{application/channels → channel}/observer.d.ts +2 -1
- package/dist/channel/observer.d.ts.map +1 -0
- package/dist/{application/channels/parent-channel.d.ts → channel/parent.d.ts} +4 -4
- package/dist/channel/parent.d.ts.map +1 -0
- package/dist/{application/channels/parent-channel.js → channel/parent.js} +1 -1
- package/dist/channel/transport.d.ts +8 -0
- package/dist/channel/transport.d.ts.map +1 -0
- package/dist/index.d.ts +56 -41
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -20
- package/dist/{domain/intent → intent}/middleware/capabilities.d.ts +2 -2
- package/dist/intent/middleware/capabilities.d.ts.map +1 -0
- package/dist/intent/middleware/logging.d.ts.map +1 -0
- package/dist/{domain/intent → intent}/middleware/rate-limit.d.ts +1 -1
- package/dist/intent/middleware/rate-limit.d.ts.map +1 -0
- package/dist/{application/intent/pipeline-runner.d.ts → intent/pipeline.d.ts} +3 -3
- package/dist/intent/pipeline.d.ts.map +1 -0
- package/dist/intent/policy.d.ts +40 -0
- package/dist/intent/policy.d.ts.map +1 -0
- package/dist/intent/policy.js +15 -0
- package/dist/{application/intent → intent}/request.d.ts +5 -14
- package/dist/intent/request.d.ts.map +1 -0
- package/dist/{application/intent → intent}/request.js +6 -20
- package/dist/intent/router.d.ts +31 -0
- package/dist/intent/router.d.ts.map +1 -0
- package/dist/{application/intent → intent}/router.js +1 -15
- package/dist/kernel/client.d.ts +33 -0
- package/dist/kernel/client.d.ts.map +1 -0
- package/dist/kernel/credential.d.ts +35 -0
- package/dist/kernel/credential.d.ts.map +1 -0
- package/dist/kernel/credential.js +43 -0
- package/dist/kernel/delegated.d.ts +17 -0
- package/dist/kernel/delegated.d.ts.map +1 -0
- package/dist/kernel/delegated.js +29 -0
- package/dist/{application/windowing/token-refresh.d.ts → kernel/delegation/refresh.d.ts} +2 -2
- package/dist/kernel/delegation/refresh.d.ts.map +1 -0
- package/dist/{application/windowing/token-refresh.js → kernel/delegation/refresh.js} +1 -1
- package/dist/{domain/identity/delegation.d.ts → kernel/delegation/token.d.ts} +1 -1
- package/dist/kernel/delegation/token.d.ts.map +1 -0
- package/dist/{adapters/kernel-client.adapter.d.ts → kernel/session.d.ts} +15 -12
- package/dist/kernel/session.d.ts.map +1 -0
- package/dist/{adapters/kernel-client.adapter.js → kernel/session.js} +10 -15
- package/dist/message/ctrl.d.ts +34 -0
- package/dist/message/ctrl.d.ts.map +1 -0
- package/dist/message/error.d.ts +9 -0
- package/dist/message/error.d.ts.map +1 -0
- package/dist/{application/windowing/protocol.d.ts → message/init.d.ts} +1 -1
- package/dist/message/init.d.ts.map +1 -0
- package/dist/{domain/message/registries.d.ts → message/intent.d.ts} +45 -37
- package/dist/message/intent.d.ts.map +1 -0
- package/dist/message/message.d.ts +10 -0
- package/dist/message/message.d.ts.map +1 -0
- package/dist/shell/access.d.ts +10 -0
- package/dist/shell/access.d.ts.map +1 -0
- package/dist/shell/access.js +19 -0
- package/dist/shell/build.d.ts +28 -0
- package/dist/shell/build.d.ts.map +1 -0
- package/dist/shell/build.js +58 -0
- package/dist/shell/config.d.ts +29 -0
- package/dist/shell/config.d.ts.map +1 -0
- package/dist/shell/create.d.ts +4 -0
- package/dist/shell/create.d.ts.map +1 -0
- package/dist/shell/create.js +5 -0
- package/dist/{application/windowing/child-init.d.ts → shell/sandboxed/boot.d.ts} +5 -5
- package/dist/shell/sandboxed/boot.d.ts.map +1 -0
- package/dist/{application/windowing/child-init.js → shell/sandboxed/boot.js} +5 -6
- package/dist/shell/sandboxed/guarded.d.ts +13 -0
- package/dist/shell/sandboxed/guarded.d.ts.map +1 -0
- package/dist/shell/sandboxed/guarded.js +28 -0
- package/dist/shell/sandboxed/pending.d.ts +14 -0
- package/dist/shell/sandboxed/pending.d.ts.map +1 -0
- package/dist/shell/sandboxed/pending.js +30 -0
- package/dist/shell/sandboxed/sandboxed.d.ts +4 -0
- package/dist/shell/sandboxed/sandboxed.d.ts.map +1 -0
- package/dist/shell/sandboxed/sandboxed.js +100 -0
- package/dist/shell/shell.d.ts +36 -0
- package/dist/shell/shell.d.ts.map +1 -0
- package/dist/shell/standalone.d.ts +4 -0
- package/dist/shell/standalone.d.ts.map +1 -0
- package/dist/shell/standalone.js +85 -0
- package/dist/{domain/window/config.d.ts → window/capabilities.d.ts} +3 -3
- package/dist/window/capabilities.d.ts.map +1 -0
- package/dist/{ports/shell-adapter.d.ts → window/embed/adapter.d.ts} +3 -2
- package/dist/window/embed/adapter.d.ts.map +1 -0
- package/dist/{adapters/iframe.shell-adapter.d.ts → window/embed/iframe.d.ts} +2 -2
- package/dist/window/embed/iframe.d.ts.map +1 -0
- package/dist/{application/windowing/lifecycle-controller.d.ts → window/lifecycle/states.d.ts} +4 -3
- package/dist/window/lifecycle/states.d.ts.map +1 -0
- package/dist/{application/windowing/lifecycle-controller.js → window/lifecycle/states.js} +1 -1
- package/dist/{domain/window/lifecycle.d.ts → window/lifecycle/transition.d.ts} +3 -3
- package/dist/window/lifecycle/transition.d.ts.map +1 -0
- package/dist/{domain/window/lifecycle.js → window/lifecycle/transition.js} +1 -1
- package/dist/window/mount/close.d.ts +27 -0
- package/dist/window/mount/close.d.ts.map +1 -0
- package/dist/window/mount/close.js +74 -0
- package/dist/window/mount/credential.d.ts +41 -0
- package/dist/window/mount/credential.d.ts.map +1 -0
- package/dist/window/mount/credential.js +74 -0
- package/dist/window/mount/handshake.d.ts.map +1 -0
- package/dist/{application/windowing → window/mount}/handshake.js +1 -1
- package/dist/{application/windowing/mount-controller.d.ts → window/mount/mount.d.ts} +7 -7
- package/dist/window/mount/mount.d.ts.map +1 -0
- package/dist/window/mount/mount.js +143 -0
- package/dist/window/mount/params.d.ts +73 -0
- package/dist/window/mount/params.d.ts.map +1 -0
- package/dist/window/mount/params.js +1 -0
- package/dist/window/mount/plain.d.ts +20 -0
- package/dist/window/mount/plain.d.ts.map +1 -0
- package/dist/window/mount/plain.js +46 -0
- package/dist/{domain/window → window}/window.d.ts +1 -1
- package/dist/window/window.d.ts.map +1 -0
- package/dist/window/window.js +1 -0
- package/package.json +11 -6
- package/src/application/apps.ts +60 -0
- package/src/application/namespaces.ts +38 -0
- package/src/application/session/open.ts +73 -0
- package/src/application/session/session.ts +46 -0
- package/src/application/session/token.ts +87 -0
- package/src/application/spaces.ts +52 -0
- package/src/application/user.ts +54 -0
- package/src/application/view.ts +42 -0
- package/src/{application/channels/children-channel.ts → channel/children.ts} +3 -3
- package/src/{application/channels/types.ts → channel/listeners.ts} +2 -7
- package/src/{adapters/message-port.transport.ts → channel/message-port.ts} +2 -2
- package/src/{application/channels → channel}/observer.ts +2 -1
- package/src/{application/channels/parent-channel.ts → channel/parent.ts} +4 -4
- package/src/{ports → channel}/transport.ts +2 -1
- package/src/index.ts +80 -88
- package/src/{domain/intent → intent}/middleware/capabilities.ts +2 -3
- package/src/{domain/intent → intent}/middleware/logging.ts +1 -1
- package/src/{domain/intent → intent}/middleware/rate-limit.ts +1 -1
- package/src/{application/intent/pipeline-runner.ts → intent/pipeline.ts} +3 -2
- package/src/intent/policy.ts +46 -0
- package/src/{application/intent → intent}/request.ts +14 -34
- package/src/{application/intent → intent}/router.ts +12 -49
- package/src/kernel/client.ts +33 -0
- package/src/kernel/credential.ts +78 -0
- package/src/kernel/delegated.ts +49 -0
- package/src/{application/windowing/token-refresh.ts → kernel/delegation/refresh.ts} +2 -2
- package/src/{adapters/kernel-client.adapter.ts → kernel/session.ts} +26 -33
- package/src/message/ctrl.ts +25 -0
- package/src/{domain/message/errors.ts → message/error.ts} +8 -0
- package/src/message/intent.ts +98 -0
- package/src/message/message.ts +11 -0
- package/src/shell/access.ts +26 -0
- package/src/shell/build.ts +95 -0
- package/src/shell/config.ts +32 -0
- package/src/shell/create.ts +9 -0
- package/src/{application/windowing/child-init.ts → shell/sandboxed/boot.ts} +10 -11
- package/src/shell/sandboxed/guarded.ts +32 -0
- package/src/shell/sandboxed/pending.ts +52 -0
- package/src/shell/sandboxed/sandboxed.ts +131 -0
- package/src/shell/shell.ts +37 -0
- package/src/shell/standalone.ts +97 -0
- package/src/{domain/window/config.ts → window/capabilities.ts} +2 -2
- package/src/{ports/shell-adapter.ts → window/embed/adapter.ts} +2 -1
- package/src/{adapters/iframe.shell-adapter.ts → window/embed/iframe.ts} +2 -2
- package/src/{application/windowing/lifecycle-controller.ts → window/lifecycle/states.ts} +3 -2
- package/src/{domain/window/lifecycle.ts → window/lifecycle/transition.ts} +2 -2
- package/src/window/mount/close.ts +111 -0
- package/src/window/mount/credential.ts +101 -0
- package/src/{application/windowing → window/mount}/handshake.ts +3 -2
- package/src/window/mount/mount.ts +185 -0
- package/src/window/mount/params.ts +64 -0
- package/src/window/mount/plain.ts +68 -0
- package/src/{domain/window → window}/window.ts +1 -1
- package/dist/adapters/iframe.shell-adapter.d.ts.map +0 -1
- package/dist/adapters/kernel-client.adapter.d.ts.map +0 -1
- package/dist/adapters/message-port.transport.d.ts.map +0 -1
- package/dist/application/channels/children-channel.d.ts.map +0 -1
- package/dist/application/channels/observer.d.ts.map +0 -1
- package/dist/application/channels/parent-channel.d.ts.map +0 -1
- package/dist/application/channels/types.d.ts +0 -12
- package/dist/application/channels/types.d.ts.map +0 -1
- package/dist/application/intent/pipeline-runner.d.ts.map +0 -1
- package/dist/application/intent/request.d.ts.map +0 -1
- package/dist/application/intent/router.d.ts +0 -56
- package/dist/application/intent/router.d.ts.map +0 -1
- package/dist/application/kernel-facade.d.ts +0 -33
- package/dist/application/kernel-facade.d.ts.map +0 -1
- package/dist/application/kernel-facade.js +0 -77
- package/dist/application/shell.d.ts +0 -100
- package/dist/application/shell.d.ts.map +0 -1
- package/dist/application/shell.js +0 -256
- package/dist/application/windowing/child-init.d.ts.map +0 -1
- package/dist/application/windowing/handshake.d.ts.map +0 -1
- package/dist/application/windowing/lifecycle-controller.d.ts.map +0 -1
- package/dist/application/windowing/mount-controller.d.ts.map +0 -1
- package/dist/application/windowing/mount-controller.js +0 -277
- package/dist/application/windowing/protocol.d.ts.map +0 -1
- package/dist/application/windowing/token-refresh.d.ts.map +0 -1
- package/dist/domain/identity/delegation.d.ts.map +0 -1
- package/dist/domain/identity/path.d.ts +0 -11
- package/dist/domain/identity/path.d.ts.map +0 -1
- package/dist/domain/identity/path.js +0 -13
- package/dist/domain/identity/resolve-self.d.ts +0 -21
- package/dist/domain/identity/resolve-self.d.ts.map +0 -1
- package/dist/domain/identity/resolve-self.js +0 -36
- package/dist/domain/intent/middleware/capabilities.d.ts.map +0 -1
- package/dist/domain/intent/middleware/logging.d.ts.map +0 -1
- package/dist/domain/intent/middleware/rate-limit.d.ts.map +0 -1
- package/dist/domain/intent/pipeline.d.ts +0 -3
- package/dist/domain/intent/pipeline.d.ts.map +0 -1
- package/dist/domain/intent/router.d.ts +0 -6
- package/dist/domain/intent/router.d.ts.map +0 -1
- package/dist/domain/message/errors.d.ts +0 -2
- package/dist/domain/message/errors.d.ts.map +0 -1
- package/dist/domain/message/registries.d.ts.map +0 -1
- package/dist/domain/message/types.d.ts +0 -30
- package/dist/domain/message/types.d.ts.map +0 -1
- package/dist/domain/window/config.d.ts.map +0 -1
- package/dist/domain/window/lifecycle.d.ts.map +0 -1
- package/dist/domain/window/window.d.ts.map +0 -1
- package/dist/ports/kernel-client.d.ts +0 -33
- package/dist/ports/kernel-client.d.ts.map +0 -1
- package/dist/ports/shell-adapter.d.ts.map +0 -1
- package/dist/ports/transport.d.ts +0 -7
- package/dist/ports/transport.d.ts.map +0 -1
- package/src/application/kernel-facade.ts +0 -143
- package/src/application/shell.ts +0 -433
- package/src/application/windowing/mount-controller.ts +0 -335
- package/src/domain/identity/path.ts +0 -14
- package/src/domain/identity/resolve-self.ts +0 -38
- package/src/domain/intent/pipeline.ts +0 -3
- package/src/domain/intent/router.ts +0 -6
- package/src/domain/message/registries.ts +0 -76
- package/src/domain/message/types.ts +0 -32
- package/src/ports/kernel-client.ts +0 -32
- /package/dist/{application/channels/children-channel.js → channel/children.js} +0 -0
- /package/dist/{application/channels/types.js → channel/listeners.js} +0 -0
- /package/dist/{adapters/message-port.transport.js → channel/message-port.js} +0 -0
- /package/dist/{application/channels → channel}/observer.js +0 -0
- /package/dist/{ports → channel}/transport.js +0 -0
- /package/dist/{domain/intent → intent}/middleware/capabilities.js +0 -0
- /package/dist/{domain/intent → intent}/middleware/logging.d.ts +0 -0
- /package/dist/{domain/intent → intent}/middleware/logging.js +0 -0
- /package/dist/{domain/intent → intent}/middleware/rate-limit.js +0 -0
- /package/dist/{application/intent/pipeline-runner.js → intent/pipeline.js} +0 -0
- /package/dist/{domain/intent/pipeline.js → kernel/client.js} +0 -0
- /package/dist/{domain/identity/delegation.js → kernel/delegation/token.js} +0 -0
- /package/dist/{domain/intent/router.js → message/ctrl.js} +0 -0
- /package/dist/{domain/message/errors.js → message/error.js} +0 -0
- /package/dist/{application/windowing/protocol.js → message/init.js} +0 -0
- /package/dist/{domain/message/registries.js → message/intent.js} +0 -0
- /package/dist/{domain/message/types.js → message/message.js} +0 -0
- /package/dist/{domain/window → shell}/config.js +0 -0
- /package/dist/{domain/window/window.js → shell/shell.js} +0 -0
- /package/dist/{ports/kernel-client.js → window/capabilities.js} +0 -0
- /package/dist/{ports/shell-adapter.js → window/embed/adapter.js} +0 -0
- /package/dist/{adapters/iframe.shell-adapter.js → window/embed/iframe.js} +0 -0
- /package/dist/{application/windowing → window/mount}/handshake.d.ts +0 -0
- /package/src/{domain/identity/delegation.ts → kernel/delegation/token.ts} +0 -0
- /package/src/{application/windowing/protocol.ts → message/init.ts} +0 -0
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import type { Window } from '../../domain/window/window'
|
|
2
|
-
import type { KernelClient } from '../../ports/kernel-client'
|
|
3
|
-
import type { ShellAdapter, WindowHandle } from '../../ports/shell-adapter'
|
|
4
|
-
import type { ChildrenChannelController } from '../channels/children-channel'
|
|
5
|
-
import type { MountParams, MountedWindow, CloseResult } from '../shell'
|
|
6
|
-
import type { LifecycleController } from './lifecycle-controller'
|
|
7
|
-
|
|
8
|
-
import { createMessagePortTransport } from '../../adapters/message-port.transport'
|
|
9
|
-
import { runParentHandshake } from './handshake'
|
|
10
|
-
import { createTokenRefresher, type TokenRefresher } from './token-refresh'
|
|
11
|
-
|
|
12
|
-
const DEFAULT_HANDSHAKE_TIMEOUT = 5_000
|
|
13
|
-
const DEFAULT_DELEGATION_TTL = 3_600
|
|
14
|
-
const DEFAULT_CLOSE_TIMEOUT = 3_000
|
|
15
|
-
const PARENT_SENDER_ID = '<parent>'
|
|
16
|
-
|
|
17
|
-
export type MountControllerConfig = {
|
|
18
|
-
adapter: ShellAdapter
|
|
19
|
-
kernel: KernelClient
|
|
20
|
-
kernelUrl: string
|
|
21
|
-
mintIdentity?: string
|
|
22
|
-
childrenChannel: ChildrenChannelController
|
|
23
|
-
windows: Map<string, Window>
|
|
24
|
-
lifecycle: LifecycleController
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MountController {
|
|
28
|
-
mount(params: MountParams): Promise<MountedWindow>
|
|
29
|
-
close(windowId: string, opts?: { force?: boolean; timeoutMs?: number }): Promise<CloseResult>
|
|
30
|
-
dispose(): Promise<void>
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
type Entry = {
|
|
34
|
-
readonly handle: WindowHandle
|
|
35
|
-
readonly dispose: () => void
|
|
36
|
-
readonly refresher: TokenRefresher
|
|
37
|
-
/** Plain (no-handshake) mount: no transport, no token, no close ack. */
|
|
38
|
-
readonly plain?: boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** No-op refresher for plain mounts (no token to refresh). */
|
|
42
|
-
const NOOP_REFRESHER: TokenRefresher = {
|
|
43
|
-
start: () => {},
|
|
44
|
-
stop: () => {},
|
|
45
|
-
forceRefreshNow: () => Promise.resolve(),
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function createMountController(config: MountControllerConfig): MountController {
|
|
49
|
-
const { adapter, kernel, kernelUrl, mintIdentity, childrenChannel, windows, lifecycle } = config
|
|
50
|
-
// The child iframe is always a DIFFERENT origin from this parent window, so a
|
|
51
|
-
// relative kernelUrl (e.g. the SPA's same-origin `/api/`) handed across the
|
|
52
|
-
// handshake would resolve against the iframe's OWN origin and POST to the
|
|
53
|
-
// wrong host. Absolutize it against the parent's location once so every child
|
|
54
|
-
// calls THIS origin's kernel. (The parent's own calls keep using the relative
|
|
55
|
-
// form via its kernel client / fetch.)
|
|
56
|
-
const childKernelUrl = new URL(kernelUrl, window.location.href).href
|
|
57
|
-
const entries = new Map<string, Entry>()
|
|
58
|
-
let seq = 0
|
|
59
|
-
let disposed = false
|
|
60
|
-
|
|
61
|
-
function nextWindowId(functionId: string) {
|
|
62
|
-
seq += 1
|
|
63
|
-
const slug = functionId.replace(/[^a-z0-9]/gi, '').slice(-8) || 'fn'
|
|
64
|
-
return `win-${slug}-${seq}`
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function mint(ttlSeconds: number) {
|
|
68
|
-
const credential = await kernel.mintDelegation()
|
|
69
|
-
return { credential, expiresAt: Date.now() + ttlSeconds * 1_000 }
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function originOf(url: string): string {
|
|
73
|
-
try {
|
|
74
|
-
return new URL(url, window.location.href).origin
|
|
75
|
-
} catch {
|
|
76
|
-
throw new Error(`Invalid mount url: ${url}`)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Plain-iframe mount path — for hosted views that don't speak the shell
|
|
81
|
-
// protocol. No handshake, no transport, no delegation token, no refresher;
|
|
82
|
-
// the iframe just renders. `close()` skips the ack request (no channel) and
|
|
83
|
-
// falls straight to dispose.
|
|
84
|
-
async function mountPlain(params: MountParams): Promise<MountedWindow> {
|
|
85
|
-
const windowId = nextWindowId(params.functionId)
|
|
86
|
-
const { handle, dispose: disposeHandle } = adapter.mount({
|
|
87
|
-
functionId: params.functionId,
|
|
88
|
-
isolation: 'sandboxed',
|
|
89
|
-
capabilities: params.capabilities,
|
|
90
|
-
sandbox: params.sandbox,
|
|
91
|
-
})
|
|
92
|
-
if (!(handle.element instanceof HTMLIFrameElement)) {
|
|
93
|
-
disposeHandle()
|
|
94
|
-
throw new Error('Shell adapter did not produce an HTMLIFrameElement')
|
|
95
|
-
}
|
|
96
|
-
params.host.appendChild(handle.element)
|
|
97
|
-
handle.element.src = params.url
|
|
98
|
-
|
|
99
|
-
windows.set(windowId, {
|
|
100
|
-
windowId,
|
|
101
|
-
functionId: params.functionId,
|
|
102
|
-
targetNodeId: params.targetNodeId,
|
|
103
|
-
isolation: 'sandboxed',
|
|
104
|
-
state: 'active',
|
|
105
|
-
delegationToken: null,
|
|
106
|
-
capabilities: params.capabilities,
|
|
107
|
-
})
|
|
108
|
-
lifecycle.register(windowId, 'active')
|
|
109
|
-
entries.set(windowId, {
|
|
110
|
-
handle,
|
|
111
|
-
dispose: disposeHandle,
|
|
112
|
-
refresher: NOOP_REFRESHER,
|
|
113
|
-
plain: true,
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
windowId,
|
|
118
|
-
get window() {
|
|
119
|
-
return windows.get(windowId)!
|
|
120
|
-
},
|
|
121
|
-
handle: { element: handle.element },
|
|
122
|
-
close: (opts) => controller.close(windowId, opts),
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const controller: MountController = {
|
|
127
|
-
async mount(params) {
|
|
128
|
-
if (disposed) throw new Error('MountController is disposed')
|
|
129
|
-
|
|
130
|
-
// Plain (no-handshake) path: hosted views that don't speak the shell
|
|
131
|
-
// protocol. Skip mint/handshake/transport/refresher; the iframe still
|
|
132
|
-
// renders and `close()` falls through to dispose.
|
|
133
|
-
if (params.handshake === 'none') return mountPlain(params)
|
|
134
|
-
|
|
135
|
-
const ttlSeconds = params.delegationTtlSeconds ?? DEFAULT_DELEGATION_TTL
|
|
136
|
-
const handshakeTimeout = params.handshakeTimeoutMs ?? DEFAULT_HANDSHAKE_TIMEOUT
|
|
137
|
-
const expectedOrigin = originOf(params.url)
|
|
138
|
-
const windowId = nextWindowId(params.functionId)
|
|
139
|
-
|
|
140
|
-
// Mint the token and create the iframe in parallel — the iframe
|
|
141
|
-
// doesn't need the credential until the handshake fires.
|
|
142
|
-
const [{ credential, expiresAt }, mounted] = await Promise.all([
|
|
143
|
-
mint(ttlSeconds),
|
|
144
|
-
Promise.resolve(
|
|
145
|
-
adapter.mount({
|
|
146
|
-
functionId: params.functionId,
|
|
147
|
-
isolation: 'sandboxed',
|
|
148
|
-
capabilities: params.capabilities,
|
|
149
|
-
sandbox: params.sandbox,
|
|
150
|
-
}),
|
|
151
|
-
),
|
|
152
|
-
])
|
|
153
|
-
const { handle, dispose: disposeHandle } = mounted
|
|
154
|
-
|
|
155
|
-
if (!(handle.element instanceof HTMLIFrameElement)) {
|
|
156
|
-
disposeHandle()
|
|
157
|
-
throw new Error('Shell adapter did not produce an HTMLIFrameElement')
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
params.host.appendChild(handle.element)
|
|
161
|
-
handle.element.src = params.url
|
|
162
|
-
|
|
163
|
-
let handshakeResult
|
|
164
|
-
try {
|
|
165
|
-
handshakeResult = await runParentHandshake({
|
|
166
|
-
iframe: handle.element,
|
|
167
|
-
expectedOrigin,
|
|
168
|
-
windowId,
|
|
169
|
-
delegationToken: credential,
|
|
170
|
-
tokenExpiresAt: expiresAt,
|
|
171
|
-
kernelUrl: childKernelUrl,
|
|
172
|
-
functionId: params.functionId,
|
|
173
|
-
targetNodeId: params.targetNodeId,
|
|
174
|
-
mintIdentity,
|
|
175
|
-
timeoutMs: handshakeTimeout,
|
|
176
|
-
})
|
|
177
|
-
} catch (err) {
|
|
178
|
-
disposeHandle()
|
|
179
|
-
throw err
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const transport = createMessagePortTransport({
|
|
183
|
-
port: handshakeResult.port,
|
|
184
|
-
onClose: () => {
|
|
185
|
-
if (!entries.has(windowId)) return
|
|
186
|
-
lifecycle.markCrashed(windowId)
|
|
187
|
-
void controller.close(windowId, { force: true }).catch(() => {})
|
|
188
|
-
},
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
childrenChannel.attach(windowId, transport)
|
|
192
|
-
windows.set(windowId, {
|
|
193
|
-
windowId,
|
|
194
|
-
functionId: params.functionId,
|
|
195
|
-
targetNodeId: params.targetNodeId,
|
|
196
|
-
isolation: 'sandboxed',
|
|
197
|
-
state: 'active',
|
|
198
|
-
delegationToken: credential,
|
|
199
|
-
capabilities: params.capabilities,
|
|
200
|
-
})
|
|
201
|
-
lifecycle.register(windowId, 'active')
|
|
202
|
-
|
|
203
|
-
// The live expiry advances on every refresh. The Window store only keeps
|
|
204
|
-
// the credential, so track expiry here — otherwise `token()` keeps
|
|
205
|
-
// returning the original expiry and `schedule()` collapses its lead time
|
|
206
|
-
// to 0, busy-looping `mint()` forever after the first refresh.
|
|
207
|
-
let currentExpiresAt = expiresAt
|
|
208
|
-
const refresher = createTokenRefresher({
|
|
209
|
-
functionId: params.functionId,
|
|
210
|
-
token: () => {
|
|
211
|
-
const stored = windows.get(windowId)
|
|
212
|
-
return {
|
|
213
|
-
credential: stored?.delegationToken ?? credential,
|
|
214
|
-
expiresAt: currentExpiresAt,
|
|
215
|
-
issuedAt: 0,
|
|
216
|
-
scope: { functionId: params.functionId },
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
setToken: (next) => {
|
|
220
|
-
currentExpiresAt = next.expiresAt
|
|
221
|
-
const stored = windows.get(windowId)
|
|
222
|
-
if (stored) {
|
|
223
|
-
windows.set(windowId, { ...stored, delegationToken: next.credential })
|
|
224
|
-
}
|
|
225
|
-
childrenChannel.send(windowId, {
|
|
226
|
-
type: 'ctrl',
|
|
227
|
-
version: 1,
|
|
228
|
-
action: 'tokenRefresh',
|
|
229
|
-
data: { delegationToken: next.credential, tokenExpiresAt: next.expiresAt },
|
|
230
|
-
})
|
|
231
|
-
},
|
|
232
|
-
mint: () => mint(ttlSeconds),
|
|
233
|
-
onFailure: () => {
|
|
234
|
-
void controller.close(windowId, { force: false }).catch(() => {})
|
|
235
|
-
},
|
|
236
|
-
})
|
|
237
|
-
refresher.start()
|
|
238
|
-
|
|
239
|
-
entries.set(windowId, { handle, dispose: disposeHandle, refresher })
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
windowId,
|
|
243
|
-
get window() {
|
|
244
|
-
return windows.get(windowId)!
|
|
245
|
-
},
|
|
246
|
-
handle: { element: handle.element },
|
|
247
|
-
close: (opts) => controller.close(windowId, opts),
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
|
|
251
|
-
async close(windowId, opts) {
|
|
252
|
-
const entry = entries.get(windowId)
|
|
253
|
-
if (!entry) return { kind: 'noop' }
|
|
254
|
-
|
|
255
|
-
const timeoutMs = opts?.timeoutMs ?? DEFAULT_CLOSE_TIMEOUT
|
|
256
|
-
|
|
257
|
-
// Plain mounts have no channel to ack a close; always force.
|
|
258
|
-
if (!opts?.force && !entry.plain) {
|
|
259
|
-
// Move the window into `closing` so the state machine accepts the
|
|
260
|
-
// subsequent `closeRefuse`/`forceClose` transition (both are only valid
|
|
261
|
-
// from `closing`).
|
|
262
|
-
lifecycle.apply(windowId, { kind: 'requestClose' })
|
|
263
|
-
const accepted = await requestCloseAck(childrenChannel, windowId, timeoutMs)
|
|
264
|
-
if (accepted.kind === 'refused') {
|
|
265
|
-
lifecycle.apply(windowId, { kind: 'closeRefuse' })
|
|
266
|
-
return { kind: 'refused', reason: accepted.reason }
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
lifecycle.apply(windowId, { kind: 'forceClose' })
|
|
271
|
-
entry.refresher.stop()
|
|
272
|
-
// Drop the entry before detaching: `detach` synchronously closes the
|
|
273
|
-
// transport, whose `onClose` re-enters `close()`. Removing the entry first
|
|
274
|
-
// makes that re-entrant call's `entries.has(windowId)` guard short-circuit,
|
|
275
|
-
// so `entry.dispose()` runs exactly once (adapters need not be idempotent).
|
|
276
|
-
entries.delete(windowId)
|
|
277
|
-
childrenChannel.detach(windowId)
|
|
278
|
-
windows.delete(windowId)
|
|
279
|
-
entry.dispose()
|
|
280
|
-
lifecycle.remove(windowId)
|
|
281
|
-
return { kind: 'closed' }
|
|
282
|
-
},
|
|
283
|
-
|
|
284
|
-
async dispose() {
|
|
285
|
-
if (disposed) return
|
|
286
|
-
disposed = true
|
|
287
|
-
const ids = [...entries.keys()]
|
|
288
|
-
await Promise.all(ids.map((id) => controller.close(id, { force: true })))
|
|
289
|
-
},
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return controller
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
type CloseAck = { kind: 'ack' } | { kind: 'refused'; reason?: string } | { kind: 'timeout' }
|
|
296
|
-
|
|
297
|
-
async function requestCloseAck(
|
|
298
|
-
children: ChildrenChannelController,
|
|
299
|
-
windowId: string,
|
|
300
|
-
timeoutMs: number,
|
|
301
|
-
): Promise<CloseAck> {
|
|
302
|
-
return new Promise((resolve) => {
|
|
303
|
-
let settled = false
|
|
304
|
-
const off = children.on('intent', (from, message) => {
|
|
305
|
-
if (from !== windowId) return
|
|
306
|
-
if (message.envelope.name === 'closeAck') {
|
|
307
|
-
settled = true
|
|
308
|
-
off()
|
|
309
|
-
clearTimeout(timer)
|
|
310
|
-
resolve({ kind: 'ack' })
|
|
311
|
-
} else if (message.envelope.name === 'closeRefuse') {
|
|
312
|
-
settled = true
|
|
313
|
-
off()
|
|
314
|
-
clearTimeout(timer)
|
|
315
|
-
const reason = (message.envelope.payload as { reason?: string }).reason
|
|
316
|
-
resolve({ kind: 'refused', reason })
|
|
317
|
-
}
|
|
318
|
-
})
|
|
319
|
-
const timer = setTimeout(() => {
|
|
320
|
-
if (settled) return
|
|
321
|
-
off()
|
|
322
|
-
resolve({ kind: 'timeout' })
|
|
323
|
-
}, timeoutMs)
|
|
324
|
-
|
|
325
|
-
children.send(windowId, {
|
|
326
|
-
type: 'intent',
|
|
327
|
-
version: 1,
|
|
328
|
-
envelope: {
|
|
329
|
-
name: 'willClose',
|
|
330
|
-
payload: {},
|
|
331
|
-
sender: { windowId: PARENT_SENDER_ID },
|
|
332
|
-
},
|
|
333
|
-
})
|
|
334
|
-
})
|
|
335
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Built-in kernel root identity, used as the default mint subject when no
|
|
3
|
-
* explicit `mintIdentity` is configured.
|
|
4
|
-
*/
|
|
5
|
-
export const SYSTEM_IDENTITY = '__system__'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Address a method on an identity or path: `@<id>::method` for a bare node id,
|
|
9
|
-
* `<path>::method` for an absolute path (one starting with `/`).
|
|
10
|
-
*/
|
|
11
|
-
export function identityMethod(identity: string, method: string): string {
|
|
12
|
-
const target = identity.startsWith('/') ? identity : `@${identity}`
|
|
13
|
-
return `${target}::${method}`
|
|
14
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { SYSTEM_IDENTITY } from './path'
|
|
2
|
-
|
|
3
|
-
/** Kernel path that returns the AUTHENTICATED caller's own identity node. */
|
|
4
|
-
export const WHOAMI_PATH = '/:kernel.astrale.ai:interface.Identity:whoami'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Resolve the mint SUBJECT — the identity node a delegation/credential should be
|
|
8
|
-
* minted for. It MUST be the principal the kernel actually authenticates this
|
|
9
|
-
* caller as, otherwise the minted token impersonates someone else.
|
|
10
|
-
*
|
|
11
|
-
* Why a kernel round-trip and not the local credential: a cookie-fronted SPA
|
|
12
|
-
* holds an EMPTY credential string (the session token lives in an httpOnly
|
|
13
|
-
* cookie it can't read), so the subject cannot be derived locally. We ask the
|
|
14
|
-
* kernel via `whoami`; that request carries the cookie, so the router unseals it
|
|
15
|
-
* and the kernel answers with the logged-in user's node. Without this the shell
|
|
16
|
-
* defaulted every mint to `__system__`, so a View iframe mounted by a logged-in
|
|
17
|
-
* user acted as the kernel ROOT instead of as that user.
|
|
18
|
-
*
|
|
19
|
-
* `fallback` (the configured `mintIdentity`, default `__system__`) is used when
|
|
20
|
-
* whoami can't be resolved — a genuinely system/dev caller, or an unauthenticated
|
|
21
|
-
* boot. The result is cached: the principal is stable for a session.
|
|
22
|
-
*/
|
|
23
|
-
export function createSelfResolver(
|
|
24
|
-
fallback: string = SYSTEM_IDENTITY,
|
|
25
|
-
): (whoami: () => Promise<unknown>) => Promise<string> {
|
|
26
|
-
let cached: string | undefined
|
|
27
|
-
return async (whoami) => {
|
|
28
|
-
if (cached !== undefined) return cached
|
|
29
|
-
try {
|
|
30
|
-
const me = (await whoami()) as { id?: unknown } | null
|
|
31
|
-
const id = me && typeof me.id === 'string' && me.id.length > 0 ? me.id : undefined
|
|
32
|
-
cached = id ?? fallback
|
|
33
|
-
} catch {
|
|
34
|
-
cached = fallback
|
|
35
|
-
}
|
|
36
|
-
return cached
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
export interface ViewRef {
|
|
2
|
-
readonly viewId: string
|
|
3
|
-
readonly functionId: string
|
|
4
|
-
readonly label?: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Intent registry — extensible via declaration merging.
|
|
9
|
-
*
|
|
10
|
-
* Consumers extend via:
|
|
11
|
-
* declare module '@astrale-os/shell' {
|
|
12
|
-
* interface IntentRegistry { ... }
|
|
13
|
-
* }
|
|
14
|
-
*/
|
|
15
|
-
export interface IntentRegistry {
|
|
16
|
-
open: { payload: { nodeId: string; viewId?: string }; response: { windowId: string } }
|
|
17
|
-
close: { payload: { windowId: string }; response: void }
|
|
18
|
-
focus: { payload: { nodeId: string }; response: void }
|
|
19
|
-
willClose: { payload: Record<string, never>; response: void }
|
|
20
|
-
closeAck: { payload: Record<string, never>; response: void }
|
|
21
|
-
closeRefuse: { payload: { reason: string }; response: void }
|
|
22
|
-
pickView: { payload: { views: ViewRef[] }; response: { viewId: string } }
|
|
23
|
-
receive: { payload: { data: unknown; sourceIntent: string }; response: void }
|
|
24
|
-
/**
|
|
25
|
-
* Hot-swap — parent pushes a new target node to an already-mounted view
|
|
26
|
-
* without remounting. The child reads `targetNodeId` at handshake time
|
|
27
|
-
* for the initial value; `setTarget` replaces it for the lifetime of the
|
|
28
|
-
* window.
|
|
29
|
-
*/
|
|
30
|
-
setTarget: { payload: { nodeId: string }; response: void }
|
|
31
|
-
/**
|
|
32
|
-
* Child → parent: a view asks the user to grant `perms` (a permission
|
|
33
|
-
* bitmask) on `node` to `identity` (the beneficiary subject). Both `node`
|
|
34
|
-
* and `identity` are bare node ids (e.g. the view's `targetNodeId`, or
|
|
35
|
-
* `whoami().id` for "grant to myself") — the root shell addresses them as
|
|
36
|
-
* `@<id>`. The root shell (GUI) surfaces a modal, computes the delta vs.
|
|
37
|
-
* what the subject already holds, and — on approval — grants only the
|
|
38
|
-
* missing bits under the GUI user's own credential. `reason` is shown to
|
|
39
|
-
* the user. `response.perms` is the bitmask actually granted (0 if
|
|
40
|
-
* declined / nothing was missing).
|
|
41
|
-
*
|
|
42
|
-
* Reaches a handler only from a view mounted as a DIRECT child of the root
|
|
43
|
-
* shell — the default intent policy does not bubble past one hop.
|
|
44
|
-
*/
|
|
45
|
-
requestAccess: {
|
|
46
|
-
payload: { node: string; identity: string; perms: number; reason?: string }
|
|
47
|
-
response: { granted: boolean; perms: number }
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Generic correlated reply channel. The default intent transport is
|
|
51
|
-
* fire-and-forget; `requestIntent()` sends an intent carrying a
|
|
52
|
-
* `correlationId` and awaits an `intentReply` whose payload echoes it back
|
|
53
|
-
* with the handler's `result`. Routed to the resolver purely by
|
|
54
|
-
* `correlationId` — it intentionally matches no local handler.
|
|
55
|
-
*/
|
|
56
|
-
intentReply: { payload: { correlationId: string; result: unknown }; response: void }
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Control message registry — extensible via declaration merging.
|
|
61
|
-
*/
|
|
62
|
-
export interface CtrlRegistry {
|
|
63
|
-
handshake: {
|
|
64
|
-
data: {
|
|
65
|
-
delegationToken: string
|
|
66
|
-
tokenExpiresAt: number
|
|
67
|
-
windowId: string
|
|
68
|
-
kernelUrl: string
|
|
69
|
-
functionId: string
|
|
70
|
-
targetNodeId?: string
|
|
71
|
-
mintIdentity?: string
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
handshakeAck: { data: { windowId: string } }
|
|
75
|
-
tokenRefresh: { data: { delegationToken: string; tokenExpiresAt: number } }
|
|
76
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { ErrorCode } from './errors'
|
|
2
|
-
import type { CtrlRegistry, IntentRegistry } from './registries'
|
|
3
|
-
|
|
4
|
-
export type IntentEnvelope<K extends keyof IntentRegistry = keyof IntentRegistry> = {
|
|
5
|
-
readonly name: K
|
|
6
|
-
readonly payload: IntentRegistry[K]['payload']
|
|
7
|
-
readonly sender: { readonly windowId: string }
|
|
8
|
-
readonly correlationId?: string
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type IntentMessage<K extends keyof IntentRegistry = keyof IntentRegistry> = {
|
|
12
|
-
readonly type: 'intent'
|
|
13
|
-
readonly version: 1
|
|
14
|
-
readonly envelope: IntentEnvelope<K>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type CtrlMessage<K extends keyof CtrlRegistry = keyof CtrlRegistry> = {
|
|
18
|
-
readonly type: 'ctrl'
|
|
19
|
-
readonly version: 1
|
|
20
|
-
readonly action: K
|
|
21
|
-
readonly data: CtrlRegistry[K]['data']
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type ErrorMessage = {
|
|
25
|
-
readonly type: 'error'
|
|
26
|
-
readonly version: 1
|
|
27
|
-
readonly code: ErrorCode
|
|
28
|
-
readonly message: string
|
|
29
|
-
readonly context?: unknown
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type ShellMessage = IntentMessage | CtrlMessage | ErrorMessage
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export interface KernelClient {
|
|
2
|
-
call(path: string, params: unknown): Promise<unknown>
|
|
3
|
-
/**
|
|
4
|
-
* Produce a credential usable by a child shell.
|
|
5
|
-
*
|
|
6
|
-
* v1: returns a fresh identity credential scoped to the current principal.
|
|
7
|
-
* Future (SDK v2): will accept `{ functionId }` to scope the delegation
|
|
8
|
-
* to a specific Function — tracked in `.dev/todo/shell-sdk-v2-delegation.md`.
|
|
9
|
-
*/
|
|
10
|
-
mintDelegation(): Promise<string>
|
|
11
|
-
/**
|
|
12
|
-
* Return the JWT this shell currently uses to authenticate `call`. Iframes
|
|
13
|
-
* use it to make their own authenticated HTTP requests to the kernel —
|
|
14
|
-
* e.g. when a third-party SDK like Vercel AI's `useChat` issues a raw
|
|
15
|
-
* `fetch()` that can't run through `call()`. Callers form the Authorization
|
|
16
|
-
* header themselves: `Authorization: \`Bearer ${shell.kernel.authToken()}\``.
|
|
17
|
-
*
|
|
18
|
-
* The value is the *current* token; the parent shell auto-refreshes it
|
|
19
|
-
* before expiry, so always read fresh on each request rather than caching.
|
|
20
|
-
*/
|
|
21
|
-
authToken(): string
|
|
22
|
-
/**
|
|
23
|
-
* URL of the kernel this shell talks to. Pair with `authToken()` when an
|
|
24
|
-
* iframe needs to make a raw `fetch()` from a third-party SDK (e.g.
|
|
25
|
-
* Vercel AI's `useChat`): the kernel-issued JWT's `aud` matches the
|
|
26
|
-
* kernel's own issuer, so requests must target the kernel directly. If
|
|
27
|
-
* a sandboxed iframe POSTs to its own `window.location.origin` (the
|
|
28
|
-
* worker hosting the iframe), the worker's audience check rejects the
|
|
29
|
-
* cred with "Credential audience mismatch".
|
|
30
|
-
*/
|
|
31
|
-
readonly url: string
|
|
32
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|