@egen-civitas/esm-nextjs-utils 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 +12 -0
- package/README.md +13 -0
- package/dist/client.d.ts +7 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +32 -1
- package/dist/host.d.ts +6 -0
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +131 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/navigation-sync.test.js +931 -0
- package/dist/navigation.d.ts +51 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/navigation.js +153 -0
- package/dist/navigation.test.js +101 -0
- package/dist/nextjs-utils.test.js +5 -1
- package/dist/protocol.d.ts +25 -0
- package/dist/protocol.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +15 -1
- package/src/host.ts +72 -1
- package/src/index.ts +1 -0
- package/src/navigation.ts +163 -0
- package/src/protocol.ts +14 -3
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Navigation partagée Shell ↔ application embarquée (protocole 1.1, additif).
|
|
3
|
+
*
|
|
4
|
+
* L'URL du Core reste la source de vérité, comme pour une app interne : `<base>/<route>/<chemin-de-l'app>?q#h`.
|
|
5
|
+
* Ce module ne contient que des fonctions pures (aucun accès au DOM), testées isolément.
|
|
6
|
+
*/
|
|
7
|
+
import type { NavigationMode } from './protocol.js';
|
|
8
|
+
|
|
9
|
+
/** Paramètres jamais recopiés dans l'URL du Core (jetons, codes OAuth…). Comparaison insensible à la casse. */
|
|
10
|
+
export const DEFAULT_SENSITIVE_PARAMS = [
|
|
11
|
+
'token',
|
|
12
|
+
'access_token',
|
|
13
|
+
'id_token',
|
|
14
|
+
'refresh_token',
|
|
15
|
+
'code',
|
|
16
|
+
'state',
|
|
17
|
+
'password',
|
|
18
|
+
'secret',
|
|
19
|
+
'otp',
|
|
20
|
+
'session',
|
|
21
|
+
'sessionid',
|
|
22
|
+
'auth',
|
|
23
|
+
'key',
|
|
24
|
+
'api_key',
|
|
25
|
+
'apikey',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export const MAX_APP_PATH_LENGTH = 2000;
|
|
29
|
+
// eslint-disable-next-line no-control-regex
|
|
30
|
+
const SAFE_APP_PATH = /^\/(?!\/)[^\s\\\u0000-\u001f]*$/;
|
|
31
|
+
|
|
32
|
+
/** Mappage optionnel entre l'espace de chemins du Core et celui de l'application (flexibilité par app). */
|
|
33
|
+
export interface PathMap {
|
|
34
|
+
/** Chemin affiché dans le Core (`/dashboard/x`) → chemin interne de l'application. */
|
|
35
|
+
toApp?: (corePath: string) => string;
|
|
36
|
+
/** Chemin interne de l'application → chemin affiché dans le Core. */
|
|
37
|
+
toCore?: (appPath: string) => string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface NavigationOptions {
|
|
41
|
+
/** Route de la page dans `routes.json` (`penpot`) : le Core est sur `<base>/<route>[/sous-chemin]`. */
|
|
42
|
+
route: string;
|
|
43
|
+
/** `history` (défaut), `hash`, ou `none` pour désactiver (comportement opaque d'origine). */
|
|
44
|
+
mode?: NavigationMode;
|
|
45
|
+
/** Paramètres supplémentaires à ne jamais recopier (s'ajoutent à `DEFAULT_SENSITIVE_PARAMS`). */
|
|
46
|
+
sensitiveParams?: string[];
|
|
47
|
+
pathMap?: PathMap;
|
|
48
|
+
/** Reçoit le titre annoncé par l'application (fil d'Ariane, titre de page). */
|
|
49
|
+
onTitle?: (title: string) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function trimSlashes(s: string): string {
|
|
53
|
+
return s.replace(/^\/+|\/+$/g, '');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Chemin interne sûr (`/x?y#z`), ou `null` : jamais d'URL absolue, de `//hôte`, d'espaces ni de caractères de contrôle. */
|
|
57
|
+
export function normalizeAppPath(input: unknown): string | null {
|
|
58
|
+
if (typeof input !== 'string' || input.length > MAX_APP_PATH_LENGTH) return null;
|
|
59
|
+
return SAFE_APP_PATH.test(input) ? input : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Retire les paramètres sensibles de la requête (et de la requête portée par le fragment `#/x?token=…`). */
|
|
63
|
+
export function stripSensitive(path: string, extra: string[] = []): string {
|
|
64
|
+
const blocked = new Set([...DEFAULT_SENSITIVE_PARAMS, ...extra].map((p) => p.toLowerCase()));
|
|
65
|
+
const clean = (query: string): string => {
|
|
66
|
+
const kept = query
|
|
67
|
+
.split('&')
|
|
68
|
+
.filter((pair) => pair && !blocked.has(decodeSafe(pair.split('=')[0]).toLowerCase()));
|
|
69
|
+
return kept.join('&');
|
|
70
|
+
};
|
|
71
|
+
const hashAt = path.indexOf('#');
|
|
72
|
+
const beforeHash = hashAt === -1 ? path : path.slice(0, hashAt);
|
|
73
|
+
const hash = hashAt === -1 ? '' : path.slice(hashAt);
|
|
74
|
+
const cleanHash = (() => {
|
|
75
|
+
const q = hash.indexOf('?');
|
|
76
|
+
if (q === -1) return hash;
|
|
77
|
+
const kept = clean(hash.slice(q + 1));
|
|
78
|
+
return hash.slice(0, q) + (kept ? `?${kept}` : '');
|
|
79
|
+
})();
|
|
80
|
+
const qAt = beforeHash.indexOf('?');
|
|
81
|
+
if (qAt === -1) return beforeHash + cleanHash;
|
|
82
|
+
const kept = clean(beforeHash.slice(qAt + 1));
|
|
83
|
+
return beforeHash.slice(0, qAt) + (kept ? `?${kept}` : '') + cleanHash;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function decodeSafe(s: string): string {
|
|
87
|
+
try {
|
|
88
|
+
return decodeURIComponent(s);
|
|
89
|
+
} catch {
|
|
90
|
+
return s;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sous-chemin de l'application déduit de l'URL du Core, ou `null` si le Core n'est pas sur cette page.
|
|
96
|
+
* `pathname=/egen/spa/penpot/dashboard/x`, `spaBase=/egen/spa/`, `route=penpot` → `/dashboard/x` (`/` à la racine).
|
|
97
|
+
*/
|
|
98
|
+
export function coreSubpath(pathname: string, spaBase: string, route: string): string | null {
|
|
99
|
+
const base = '/' + trimSlashes(spaBase);
|
|
100
|
+
const prefix = (base === '/' ? '' : base) + '/' + trimSlashes(route);
|
|
101
|
+
if (pathname !== prefix && !pathname.startsWith(prefix + '/')) return null;
|
|
102
|
+
const rest = pathname.slice(prefix.length);
|
|
103
|
+
return rest === '' ? '/' : rest;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** URL du Core (chemin seul) pour un chemin d'application : `<base>/<route><chemin>`, sans `/` final superflu. */
|
|
107
|
+
export function buildCoreUrl(spaBase: string, route: string, appPath: string): string {
|
|
108
|
+
const base = '/' + trimSlashes(spaBase);
|
|
109
|
+
const prefix = (base === '/' ? '' : base) + '/' + trimSlashes(route);
|
|
110
|
+
if (appPath === '/' || appPath === '') return prefix;
|
|
111
|
+
return prefix + (appPath.startsWith('/') ? appPath : '/' + appPath);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* URL de chargement de l'iframe : l'adresse de base de l'application + le chemin interne.
|
|
116
|
+
* `history` : `<origine><chemin-de-base>/<chemin>?q#h` ; `hash` : `<base>#<chemin>`.
|
|
117
|
+
*/
|
|
118
|
+
export function buildEmbedSrc(baseUrl: string, appPath: string, mode: NavigationMode): string {
|
|
119
|
+
if (mode === 'none' || appPath === '/' || appPath === '') return baseUrl;
|
|
120
|
+
const base = new URL(baseUrl);
|
|
121
|
+
if (mode === 'hash') {
|
|
122
|
+
base.hash = appPath;
|
|
123
|
+
return base.href;
|
|
124
|
+
}
|
|
125
|
+
const hashAt = appPath.indexOf('#');
|
|
126
|
+
const noHash = hashAt === -1 ? appPath : appPath.slice(0, hashAt);
|
|
127
|
+
const hash = hashAt === -1 ? '' : appPath.slice(hashAt);
|
|
128
|
+
const qAt = noHash.indexOf('?');
|
|
129
|
+
const pathname = qAt === -1 ? noHash : noHash.slice(0, qAt);
|
|
130
|
+
const search = qAt === -1 ? '' : noHash.slice(qAt);
|
|
131
|
+
const basePath = base.pathname.replace(/\/+$/, '');
|
|
132
|
+
return `${base.origin}${basePath}${pathname}${search}${hash}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Chemin d'application → chemin affiché par le Core (sans paramètres sensibles ; `pathMap.toCore` en dernier). */
|
|
136
|
+
export function toCorePath(appPath: unknown, opts: Pick<NavigationOptions, 'pathMap' | 'sensitiveParams'>): string | null {
|
|
137
|
+
const safe = normalizeAppPath(appPath);
|
|
138
|
+
if (!safe) return null;
|
|
139
|
+
const clean = stripSensitive(safe, opts.sensitiveParams);
|
|
140
|
+
const mapped = opts.pathMap?.toCore ? opts.pathMap.toCore(clean) : clean;
|
|
141
|
+
return normalizeAppPath(mapped);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Chemin lu dans l'URL du Core → chemin d'application (`pathMap.toApp` ; sans paramètres sensibles). */
|
|
145
|
+
export function toAppPath(corePath: string, opts: Pick<NavigationOptions, 'pathMap' | 'sensitiveParams'>): string | null {
|
|
146
|
+
const safe = normalizeAppPath(corePath);
|
|
147
|
+
if (!safe) return null;
|
|
148
|
+
const clean = stripSensitive(safe, opts.sensitiveParams);
|
|
149
|
+
const mapped = opts.pathMap?.toApp ? opts.pathMap.toApp(clean) : clean;
|
|
150
|
+
return normalizeAppPath(mapped);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Garde-fou de débit : au plus `max` messages par fenêtre `windowMs` (History API limitée par les navigateurs). */
|
|
154
|
+
export function createRateLimiter(max = 40, windowMs = 1000, now: () => number = () => Date.now()) {
|
|
155
|
+
const stamps: number[] = [];
|
|
156
|
+
return (): boolean => {
|
|
157
|
+
const t = now();
|
|
158
|
+
while (stamps.length && t - stamps[0] > windowMs) stamps.shift();
|
|
159
|
+
if (stamps.length >= max) return false;
|
|
160
|
+
stamps.push(t);
|
|
161
|
+
return true;
|
|
162
|
+
};
|
|
163
|
+
}
|
package/src/protocol.ts
CHANGED
|
@@ -13,18 +13,29 @@ export interface EmbedInit {
|
|
|
13
13
|
spaBase: string;
|
|
14
14
|
/** Session filtrée par liste blanche (voir `sessionFields`) ; absente tant que non chargée. */
|
|
15
15
|
session?: Record<string, unknown>;
|
|
16
|
+
/** Protocole 1.1 : navigation partagée active pour cette instance (absente = opaque). */
|
|
17
|
+
navigation?: { mode: NavigationMode; route: string };
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
/** Mode de navigation partagée (voir `navigation.ts`) : `none` = opaque, `history` = chemins, `hash` = `#/chemin`. */
|
|
21
|
+
export type NavigationMode = 'none' | 'history' | 'hash';
|
|
22
|
+
|
|
18
23
|
export type HostToEmbed =
|
|
19
24
|
| { kind: 'init'; payload: EmbedInit }
|
|
20
25
|
| { kind: 'session'; session: Record<string, unknown> }
|
|
21
|
-
| { kind: 'locale'; locale: string }
|
|
26
|
+
| { kind: 'locale'; locale: string }
|
|
27
|
+
/** Protocole 1.1 : poignée de main initiée par le Shell (à chaque chargement de l'iframe). */
|
|
28
|
+
| { kind: 'hello' }
|
|
29
|
+
/** Protocole 1.1 : le Shell demande à l'application d'aller à `path` (retour/avance, lien du Core). */
|
|
30
|
+
| { kind: 'goto'; path: string; seq: number };
|
|
22
31
|
|
|
23
32
|
export type EmbedToHost =
|
|
24
|
-
| { kind: 'ready' }
|
|
33
|
+
| { kind: 'ready'; caps?: string[] }
|
|
25
34
|
| { kind: 'navigate'; to: string }
|
|
26
35
|
| { kind: 'resize'; height: number }
|
|
27
|
-
| { kind: 'title'; title: string }
|
|
36
|
+
| { kind: 'title'; title: string }
|
|
37
|
+
/** Protocole 1.1 : l'application annonce sa page courante (chemin interne, requête et fragment compris). */
|
|
38
|
+
| { kind: 'location'; path: string; replace?: boolean; title?: string };
|
|
28
39
|
|
|
29
40
|
export interface Envelope<T> {
|
|
30
41
|
egen: typeof EGEN_EMBED_PROTOCOL;
|