@egen-civitas/esm-embed-host 0.2.0 → 0.3.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/CHANGELOG.md +13 -0
- package/README.md +6 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +27 -84
- package/dist/providers.d.ts +12 -40
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +6 -300
- package/dist/table.d.ts +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +11 -135
- package/package.json +3 -3
- package/src/host.ts +7 -11
- package/src/providers.ts +10 -78
- package/src/table.ts +11 -51
package/src/providers.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { EmbedError } from '@egen-civitas/esm-embed-protocol';
|
|
2
|
-
|
|
3
1
|
export type Unsub = () => void;
|
|
4
2
|
|
|
5
3
|
/** Valeur lisible et observable fournie par le Core. */
|
|
@@ -13,11 +11,6 @@ export interface CallerContext {
|
|
|
13
11
|
moduleName: string;
|
|
14
12
|
}
|
|
15
13
|
|
|
16
|
-
export interface ThemeState {
|
|
17
|
-
mode?: string;
|
|
18
|
-
tokens: Record<string, string>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
14
|
export interface NotificationInput {
|
|
22
15
|
kind: 'info' | 'success' | 'warning' | 'error';
|
|
23
16
|
title: string;
|
|
@@ -25,18 +18,11 @@ export interface NotificationInput {
|
|
|
25
18
|
timeoutMs?: number;
|
|
26
19
|
}
|
|
27
20
|
|
|
28
|
-
export interface
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
body?: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface ApiResponse {
|
|
37
|
-
status: number;
|
|
38
|
-
headers: Record<string, string>;
|
|
39
|
-
body: string;
|
|
21
|
+
export interface ExtensionInfo {
|
|
22
|
+
name: string;
|
|
23
|
+
moduleName: string;
|
|
24
|
+
order?: number;
|
|
25
|
+
meta: Record<string, unknown>;
|
|
40
26
|
}
|
|
41
27
|
|
|
42
28
|
export interface AiToolDescriptor {
|
|
@@ -48,19 +34,20 @@ export interface AiToolDescriptor {
|
|
|
48
34
|
/**
|
|
49
35
|
* Services du Core mis à disposition des apps embarquées. Toute capacité sans fournisseur est
|
|
50
36
|
* automatiquement refusée à la négociation (`UNAVAILABLE`) : on ne promet que ce qu'on sait servir.
|
|
37
|
+
*
|
|
38
|
+
* Règle d'or (doc 17 §2) : seulement ce dont le Core est lui-même la source. Pas d'identité/session, de droits, de thème ni d'appels
|
|
39
|
+
* API relayés : l'app les obtient directement auprès de leurs sources (Keycloak, backends, service de thèmes).
|
|
51
40
|
*/
|
|
52
41
|
export interface HostProviders {
|
|
53
|
-
session?: Watchable<Record<string, unknown> | undefined>;
|
|
54
42
|
tenant?: Watchable<{ id: string | null }>;
|
|
55
43
|
locale?: Watchable<string>;
|
|
56
|
-
theme?: Watchable<ThemeState>;
|
|
57
44
|
config?: Watchable<Record<string, unknown>>;
|
|
58
45
|
flags?: { isEnabled(name: string): boolean | Promise<boolean>; subscribe?(cb: () => void): Unsub };
|
|
59
|
-
permissions?: { can(permission: string): boolean | Promise<boolean>; subscribe?(cb: () => void): Unsub };
|
|
60
46
|
events?: { publish(topic: string, payload: unknown, ctx: CallerContext): void; subscribe(topic: string, cb: (payload: unknown) => void): Unsub };
|
|
61
47
|
notifications?: { show(n: NotificationInput, ctx: CallerContext): void | Promise<void> };
|
|
62
48
|
dialogs?: { confirm(d: { title: string; message: string; danger?: boolean }, ctx: CallerContext): boolean | Promise<boolean> };
|
|
63
|
-
|
|
49
|
+
/** Extensions attachées à un slot du Core (apps internes, framework, autres apps) : métadonnées seulement, jamais le rendu. */
|
|
50
|
+
extensions?: { list(slot: string): ExtensionInfo[] | Promise<ExtensionInfo[]>; subscribe?(slot: string, cb: (list: ExtensionInfo[]) => void): Unsub };
|
|
64
51
|
telemetry?: {
|
|
65
52
|
log(level: 'debug' | 'info' | 'warn' | 'error', message: string, ctx: Record<string, unknown> | undefined, from: CallerContext): void;
|
|
66
53
|
metric(name: string, value: number, tags: Record<string, unknown> | undefined, from: CallerContext): void;
|
|
@@ -70,58 +57,3 @@ export interface HostProviders {
|
|
|
70
57
|
unregister(moduleName: string, names: string[]): void;
|
|
71
58
|
};
|
|
72
59
|
}
|
|
73
|
-
|
|
74
|
-
export interface FetchProviderOptions {
|
|
75
|
-
/** Base de l'API du Core (ex. `https://api.exemple.org/v1`) : résolution des `path`. */
|
|
76
|
-
baseUrl: string;
|
|
77
|
-
/** En-têtes d'authentification/tenant ajoutés aux appels `path` UNIQUEMENT (jamais aux `url` externes). */
|
|
78
|
-
getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;
|
|
79
|
-
fetchImpl?: typeof fetch;
|
|
80
|
-
timeoutMs?: number;
|
|
81
|
-
/** Taille maximale du corps de réponse (octets approximatifs). Défaut 200 000. */
|
|
82
|
-
maxResponseBytes?: number;
|
|
83
|
-
/** En-têtes de réponse exposés à l'app. Défaut : content-type, etag, cache-control, content-language. */
|
|
84
|
-
exposeResponseHeaders?: string[];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Fournisseur `api.fetch` : la requête s'exécute côté Core. Le jeton ne quitte jamais le Core ;
|
|
89
|
-
* `path` → API du Core (cookies + en-têtes d'auth) ; `url` → hôte externe autorisé par le grant, sans cookies ni auth, sans redirection.
|
|
90
|
-
*/
|
|
91
|
-
export function createFetchApiProvider(o: FetchProviderOptions): NonNullable<HostProviders['api']> {
|
|
92
|
-
const doFetch = o.fetchImpl ?? ((...a: Parameters<typeof fetch>) => fetch(...a));
|
|
93
|
-
const maxBytes = o.maxResponseBytes ?? 200_000;
|
|
94
|
-
const expose = new Set((o.exposeResponseHeaders ?? ['content-type', 'etag', 'cache-control', 'content-language']).map((h) => h.toLowerCase()));
|
|
95
|
-
return {
|
|
96
|
-
async fetch(req) {
|
|
97
|
-
const internal = req.path !== undefined;
|
|
98
|
-
const target = internal ? o.baseUrl.replace(/\/+$/, '') + req.path : (req.url as string);
|
|
99
|
-
const headers: Record<string, string> = { ...(req.headers ?? {}) };
|
|
100
|
-
if (internal && o.getHeaders) Object.assign(headers, await o.getHeaders());
|
|
101
|
-
const ctrl = new AbortController();
|
|
102
|
-
const timer = setTimeout(() => ctrl.abort(), o.timeoutMs ?? 10_000);
|
|
103
|
-
try {
|
|
104
|
-
const res = await doFetch(target, {
|
|
105
|
-
method: req.method ?? 'GET',
|
|
106
|
-
headers,
|
|
107
|
-
body: req.body,
|
|
108
|
-
credentials: internal ? 'include' : 'omit',
|
|
109
|
-
redirect: internal ? 'follow' : 'error',
|
|
110
|
-
signal: ctrl.signal,
|
|
111
|
-
});
|
|
112
|
-
const body = await res.text();
|
|
113
|
-
if (body.length > maxBytes) throw new EmbedError('TOO_LARGE', 'Réponse trop volumineuse');
|
|
114
|
-
const outHeaders: Record<string, string> = {};
|
|
115
|
-
res.headers.forEach((v, k) => {
|
|
116
|
-
if (expose.has(k.toLowerCase())) outHeaders[k.toLowerCase()] = v;
|
|
117
|
-
});
|
|
118
|
-
return { status: res.status, headers: outHeaders, body };
|
|
119
|
-
} catch (e) {
|
|
120
|
-
if (e instanceof EmbedError) throw e;
|
|
121
|
-
throw new EmbedError('INTERNAL', ctrl.signal.aborted ? 'Délai dépassé' : 'Requête impossible');
|
|
122
|
-
} finally {
|
|
123
|
-
clearTimeout(timer);
|
|
124
|
-
}
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
}
|
package/src/table.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CAPABILITIES,
|
|
3
|
-
DEFAULT_SESSION_FIELDS,
|
|
4
3
|
EmbedError,
|
|
5
|
-
|
|
6
|
-
pickFields,
|
|
4
|
+
slotAllowed,
|
|
7
5
|
topicAllowed,
|
|
8
6
|
type CapabilityGranted,
|
|
9
7
|
type CapabilityId,
|
|
@@ -12,7 +10,7 @@ import {
|
|
|
12
10
|
type SubscribeHandler,
|
|
13
11
|
} from '@egen-civitas/esm-embed-protocol';
|
|
14
12
|
import type { Bus } from './bus.js';
|
|
15
|
-
import type {
|
|
13
|
+
import type { CallerContext, HostProviders } from './providers.js';
|
|
16
14
|
|
|
17
15
|
/** Actions du Shell pilotées par les capacités `navigation` et `lifecycle`. */
|
|
18
16
|
export interface HostControls {
|
|
@@ -29,17 +27,14 @@ export interface HostControls {
|
|
|
29
27
|
export function availableCapabilities(p: HostProviders): Set<CapabilityId> {
|
|
30
28
|
const s = new Set<CapabilityId>(['navigation', 'lifecycle', 'telemetry']);
|
|
31
29
|
const map: Array<[CapabilityId, unknown]> = [
|
|
32
|
-
['session', p.session],
|
|
33
30
|
['tenant', p.tenant],
|
|
34
31
|
['locale', p.locale],
|
|
35
|
-
['theme', p.theme],
|
|
36
32
|
['config', p.config],
|
|
37
33
|
['flags', p.flags],
|
|
38
|
-
['permissions', p.permissions],
|
|
39
34
|
['events', p.events],
|
|
40
35
|
['notifications', p.notifications],
|
|
41
36
|
['dialogs', p.dialogs],
|
|
42
|
-
['
|
|
37
|
+
['extensions', p.extensions],
|
|
43
38
|
['ai-tools', p.aiTools],
|
|
44
39
|
];
|
|
45
40
|
for (const [id, prov] of map) if (prov) s.add(id);
|
|
@@ -48,14 +43,6 @@ export function availableCapabilities(p: HostProviders): Set<CapabilityId> {
|
|
|
48
43
|
|
|
49
44
|
const list = (cfg: GrantConfig | undefined, key: string): string[] => (Array.isArray(cfg?.[key]) ? (cfg![key] as string[]) : []);
|
|
50
45
|
|
|
51
|
-
function normOrigin(u: string): string {
|
|
52
|
-
try {
|
|
53
|
-
return new URL(u).origin;
|
|
54
|
-
} catch {
|
|
55
|
-
return u;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
46
|
export interface TableOptions {
|
|
60
47
|
moduleName: string;
|
|
61
48
|
granted: CapabilityGranted[];
|
|
@@ -67,7 +54,7 @@ export interface TableOptions {
|
|
|
67
54
|
|
|
68
55
|
/**
|
|
69
56
|
* Construit la table de dispatch FERMÉE d'une connexion : seules les capacités accordées (et servies) y figurent,
|
|
70
|
-
* chaque méthode applique son grant (
|
|
57
|
+
* chaque méthode applique son grant (sujets d'événements, slots découvrables, drapeaux, outils IA).
|
|
71
58
|
*/
|
|
72
59
|
export function createCapabilityTable(o: TableOptions): { table: HandlerTable; subscribe: SubscribeHandler; cleanup: () => void } {
|
|
73
60
|
const { providers: p, controls: c } = o;
|
|
@@ -76,28 +63,12 @@ export function createCapabilityTable(o: TableOptions): { table: HandlerTable; s
|
|
|
76
63
|
const table: HandlerTable = {};
|
|
77
64
|
const registeredTools = new Set<string>();
|
|
78
65
|
|
|
79
|
-
const sessionFields = () => {
|
|
80
|
-
const f = list(cfgOf.get('session'), 'fields');
|
|
81
|
-
return f.length ? f : [...DEFAULT_SESSION_FIELDS];
|
|
82
|
-
};
|
|
83
|
-
const pick = (s: unknown) => pickFields(s, sessionFields());
|
|
84
66
|
const denied = (msg: string) => new EmbedError('DENIED', msg);
|
|
85
67
|
|
|
86
|
-
if (cfgOf.has('session') && p.session) table.session = { get: async () => pick(await p.session!.get()) };
|
|
87
68
|
if (cfgOf.has('tenant') && p.tenant) table.tenant = { get: async () => p.tenant!.get() };
|
|
88
69
|
if (cfgOf.has('locale') && p.locale) table.locale = { get: async () => p.locale!.get() };
|
|
89
|
-
if (cfgOf.has('theme') && p.theme) table.theme = { get: async () => p.theme!.get() };
|
|
90
70
|
if (cfgOf.has('config') && p.config) table.config = { get: async () => p.config!.get() };
|
|
91
71
|
|
|
92
|
-
if (cfgOf.has('permissions') && p.permissions) {
|
|
93
|
-
const probe = list(cfgOf.get('permissions'), 'probe');
|
|
94
|
-
table.permissions = {
|
|
95
|
-
can: async (perm: string) => {
|
|
96
|
-
if (!probe.includes(perm)) throw denied(`Permission non sondable : ${perm}`);
|
|
97
|
-
return !!(await p.permissions!.can(perm));
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
72
|
if (cfgOf.has('flags') && p.flags) {
|
|
102
73
|
const names = list(cfgOf.get('flags'), 'names');
|
|
103
74
|
table.flags = {
|
|
@@ -118,18 +89,12 @@ export function createCapabilityTable(o: TableOptions): { table: HandlerTable; s
|
|
|
118
89
|
}
|
|
119
90
|
if (cfgOf.has('notifications') && p.notifications) table.notifications = { show: async (n: any) => void (await p.notifications!.show(n, ctx)) };
|
|
120
91
|
if (cfgOf.has('dialogs') && p.dialogs) table.dialogs = { confirm: async (d: any) => !!(await p.dialogs!.confirm(d, ctx)) };
|
|
121
|
-
if (cfgOf.has('
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
fetch: async (req: ApiRequest) => {
|
|
128
|
-
if (req.url !== undefined && !hosts.includes(normOrigin(req.url))) throw denied("Hôte non autorisé par le grant de l'app");
|
|
129
|
-
// Sans liste de chemins (deny-by-default) une app ne peut PAS appeler l'API du Core avec la session de l'utilisateur.
|
|
130
|
-
if (req.path !== undefined && !pathAllowed(paths, req.path)) throw denied(`Chemin d'API non autorisé par le grant de l'app : ${req.path}`);
|
|
131
|
-
for (const h of Object.keys(req.headers ?? {})) if (!headers.includes(h.toLowerCase())) throw denied(`En-tête non autorisé : ${h}`);
|
|
132
|
-
return p.api!.fetch(req, ctx);
|
|
92
|
+
if (cfgOf.has('extensions') && p.extensions) {
|
|
93
|
+
const discover = list(cfgOf.get('extensions'), 'discover');
|
|
94
|
+
table.extensions = {
|
|
95
|
+
list: async (slot: string) => {
|
|
96
|
+
if (!slotAllowed(discover, slot)) throw denied(`Slot non découvrable par le grant de l'app : ${slot}`);
|
|
97
|
+
return p.extensions!.list(slot);
|
|
133
98
|
},
|
|
134
99
|
};
|
|
135
100
|
}
|
|
@@ -168,22 +133,17 @@ export function createCapabilityTable(o: TableOptions): { table: HandlerTable; s
|
|
|
168
133
|
const desc = CAPABILITIES[cap as CapabilityId];
|
|
169
134
|
if (!cfg || !desc || !table[cap]) return undefined;
|
|
170
135
|
if (cap === 'events') return topicAllowed(list(cfg, 'subscribe'), ev) ? p.events!.subscribe(ev, emit) : undefined;
|
|
136
|
+
if (cap === 'extensions') return slotAllowed(list(cfg, 'discover'), ev) && p.extensions!.subscribe ? p.extensions!.subscribe(ev, emit as (l: any) => void) : undefined;
|
|
171
137
|
if (!desc.events.includes(ev)) return undefined;
|
|
172
138
|
switch (cap) {
|
|
173
|
-
case 'session':
|
|
174
|
-
return p.session!.subscribe?.((v) => emit(pick(v)));
|
|
175
139
|
case 'tenant':
|
|
176
140
|
return p.tenant!.subscribe?.(emit);
|
|
177
141
|
case 'locale':
|
|
178
142
|
return p.locale!.subscribe?.(emit);
|
|
179
|
-
case 'theme':
|
|
180
|
-
return p.theme!.subscribe?.(emit);
|
|
181
143
|
case 'config':
|
|
182
144
|
return p.config!.subscribe?.(emit);
|
|
183
145
|
case 'flags':
|
|
184
146
|
return p.flags!.subscribe?.(() => emit());
|
|
185
|
-
case 'permissions':
|
|
186
|
-
return p.permissions!.subscribe?.(() => emit());
|
|
187
147
|
case 'navigation':
|
|
188
148
|
case 'lifecycle':
|
|
189
149
|
return c.channel.on(ev, emit);
|