@ignite-ember/extension-bridge 0.0.0-stage → 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/NOTICE +5 -0
- package/README.md +303 -2
- package/dist/bridge.d.ts +33 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +92 -0
- package/dist/bridge.js.map +1 -0
- package/dist/channel.d.ts +26 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +36 -0
- package/dist/channel.js.map +1 -0
- package/dist/contract.d.ts +110 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +9 -0
- package/dist/contract.js.map +1 -0
- package/dist/correlator.d.ts +37 -0
- package/dist/correlator.d.ts.map +1 -0
- package/dist/correlator.js +67 -0
- package/dist/correlator.js.map +1 -0
- package/dist/guestBridge.d.ts +63 -0
- package/dist/guestBridge.d.ts.map +1 -0
- package/dist/guestBridge.js +156 -0
- package/dist/guestBridge.js.map +1 -0
- package/dist/hostModel.d.ts +155 -0
- package/dist/hostModel.d.ts.map +1 -0
- package/dist/hostModel.generated.d.ts +1870 -0
- package/dist/hostModel.generated.d.ts.map +1 -0
- package/dist/hostModel.generated.js +1241 -0
- package/dist/hostModel.generated.js.map +1 -0
- package/dist/hostModel.js +168 -0
- package/dist/hostModel.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.d.ts +338 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +135 -0
- package/dist/protocol.js.map +1 -0
- package/dist/service.d.ts +143 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +179 -0
- package/dist/service.js.map +1 -0
- package/dist/signals.d.ts +21 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +52 -0
- package/dist/signals.js.map +1 -0
- package/dist/tokens.d.ts +95 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +158 -0
- package/dist/tokens.js.map +1 -0
- package/dist/watchers.d.ts +12 -0
- package/dist/watchers.d.ts.map +1 -0
- package/dist/watchers.js +18 -0
- package/dist/watchers.js.map +1 -0
- package/package.json +53 -4
- package/src/bridge.ts +112 -0
- package/src/channel.ts +48 -0
- package/src/contract.ts +130 -0
- package/src/correlator.ts +81 -0
- package/src/guestBridge.ts +261 -0
- package/src/hostModel.generated.ts +2649 -0
- package/src/hostModel.ts +209 -0
- package/src/index.ts +11 -0
- package/src/protocol.ts +475 -0
- package/src/service.ts +253 -0
- package/src/signals.ts +65 -0
- package/src/tokens.ts +165 -0
- package/src/watchers.ts +18 -0
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What crosses the gap between the host app and an extension's frame.
|
|
3
|
+
*
|
|
4
|
+
* One envelope type, versioned, with the host and the guest reading
|
|
5
|
+
* the same definition. The guest half of this file is the part that
|
|
6
|
+
* becomes a published package when somebody outside this repository
|
|
7
|
+
* writes an extension; until then both sides import it from here, so
|
|
8
|
+
* there is one description of the wire rather than two that agree
|
|
9
|
+
* until they don't.
|
|
10
|
+
*
|
|
11
|
+
* The frame is the untrusted party. Everything the host accepts from
|
|
12
|
+
* it is checked — the origin it came from, the window it came from,
|
|
13
|
+
* the nonce it was issued, and whether the method is one this
|
|
14
|
+
* extension was granted. None of those checks live in the guest.
|
|
15
|
+
*/
|
|
16
|
+
/** Bumped only for a change the other side cannot ignore. */
|
|
17
|
+
export const BRIDGE_VERSION = 1;
|
|
18
|
+
const HOST_OPS = new Set(["get", "set", "call", "subscribe", "unsubscribe"]);
|
|
19
|
+
/**
|
|
20
|
+
* Is this a message from a guest, shaped the way we require?
|
|
21
|
+
*
|
|
22
|
+
* Written as a narrowing guard rather than a schema check because the
|
|
23
|
+
* host has to answer this about `unknown` arriving from a frame, and
|
|
24
|
+
* the answer has to be "no" for everything that is not exactly right.
|
|
25
|
+
* A message that fails here is dropped without a reply: a guest that
|
|
26
|
+
* can tell malformed from unauthorised has learned something.
|
|
27
|
+
*/
|
|
28
|
+
export function isGuestMessage(data) {
|
|
29
|
+
if (typeof data !== "object" || data === null)
|
|
30
|
+
return false;
|
|
31
|
+
const m = data;
|
|
32
|
+
if (m.igni !== BRIDGE_VERSION)
|
|
33
|
+
return false;
|
|
34
|
+
if (typeof m.nonce !== "string" || m.nonce.length === 0)
|
|
35
|
+
return false;
|
|
36
|
+
switch (m.kind) {
|
|
37
|
+
case "call":
|
|
38
|
+
return (typeof m.id === "string" &&
|
|
39
|
+
m.id.length > 0 &&
|
|
40
|
+
typeof m.method === "string" &&
|
|
41
|
+
m.method.length > 0 &&
|
|
42
|
+
typeof m.args === "object" &&
|
|
43
|
+
m.args !== null &&
|
|
44
|
+
!Array.isArray(m.args));
|
|
45
|
+
case "navigate":
|
|
46
|
+
return typeof m.to === "string" && m.to.length > 0;
|
|
47
|
+
case "location":
|
|
48
|
+
// Every value a string, checked rather than assumed: this goes
|
|
49
|
+
// into the host's own URL, and a guest is the untrusted party.
|
|
50
|
+
return (typeof m.state === "object" &&
|
|
51
|
+
m.state !== null &&
|
|
52
|
+
!Array.isArray(m.state) &&
|
|
53
|
+
Object.values(m.state).every((value) => typeof value === "string"));
|
|
54
|
+
case "close":
|
|
55
|
+
return true;
|
|
56
|
+
case "service-token":
|
|
57
|
+
return typeof m.id === "string" && m.id.length > 0;
|
|
58
|
+
case "host":
|
|
59
|
+
return (typeof m.id === "string" &&
|
|
60
|
+
m.id.length > 0 &&
|
|
61
|
+
typeof m.op === "string" &&
|
|
62
|
+
HOST_OPS.has(m.op) &&
|
|
63
|
+
typeof m.path === "string" &&
|
|
64
|
+
(m.window === undefined ||
|
|
65
|
+
m.window === null ||
|
|
66
|
+
typeof m.window === "string") &&
|
|
67
|
+
(m.session === undefined ||
|
|
68
|
+
m.session === null ||
|
|
69
|
+
typeof m.session === "string") &&
|
|
70
|
+
(m.params === undefined ||
|
|
71
|
+
(typeof m.params === "object" &&
|
|
72
|
+
m.params !== null &&
|
|
73
|
+
!Array.isArray(m.params))) &&
|
|
74
|
+
(m.signals === undefined ||
|
|
75
|
+
(Array.isArray(m.signals) &&
|
|
76
|
+
m.signals.every((one) => typeof one === "string"))));
|
|
77
|
+
default:
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Is this a message from the host, shaped the way the guest requires? */
|
|
82
|
+
export function isHostMessage(data) {
|
|
83
|
+
if (typeof data !== "object" || data === null)
|
|
84
|
+
return false;
|
|
85
|
+
const m = data;
|
|
86
|
+
if (m.igni !== BRIDGE_VERSION)
|
|
87
|
+
return false;
|
|
88
|
+
switch (m.kind) {
|
|
89
|
+
case "hello":
|
|
90
|
+
return (typeof m.nonce === "string" &&
|
|
91
|
+
typeof m.extensionId === "string" &&
|
|
92
|
+
typeof m.theme === "string" &&
|
|
93
|
+
typeof m.params === "object" &&
|
|
94
|
+
m.params !== null &&
|
|
95
|
+
(m.settings === undefined || (typeof m.settings === "object" && m.settings !== null)) &&
|
|
96
|
+
(m.serviceUrl === undefined || m.serviceUrl === null || typeof m.serviceUrl === "string") &&
|
|
97
|
+
typeof m.tokens === "object" &&
|
|
98
|
+
m.tokens !== null);
|
|
99
|
+
case "theme":
|
|
100
|
+
return (typeof m.theme === "string" &&
|
|
101
|
+
typeof m.tokens === "object" &&
|
|
102
|
+
m.tokens !== null);
|
|
103
|
+
case "params":
|
|
104
|
+
return (typeof m.params === "object" &&
|
|
105
|
+
m.params !== null &&
|
|
106
|
+
!Array.isArray(m.params));
|
|
107
|
+
case "result":
|
|
108
|
+
return typeof m.id === "string" && typeof m.ok === "boolean";
|
|
109
|
+
case "host-reply":
|
|
110
|
+
return typeof m.id === "string" && typeof m.ok === "boolean";
|
|
111
|
+
case "service-token-reply":
|
|
112
|
+
return (typeof m.id === "string" &&
|
|
113
|
+
typeof m.ok === "boolean" &&
|
|
114
|
+
(m.token === undefined || typeof m.token === "string") &&
|
|
115
|
+
(m.expiresIn === undefined || typeof m.expiresIn === "number"));
|
|
116
|
+
case "signal":
|
|
117
|
+
return typeof m.signal === "string" && m.signal.length > 0;
|
|
118
|
+
default:
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* May this extension call this method?
|
|
124
|
+
*
|
|
125
|
+
* Exact names, or a `*`-suffixed prefix. No regular expressions and no
|
|
126
|
+
* `*` anywhere but the end: a permission list is read by whoever
|
|
127
|
+
* approves an install, and a pattern language it cannot be read in is
|
|
128
|
+
* a permission nobody actually granted.
|
|
129
|
+
*/
|
|
130
|
+
export function methodAllowed(method, allow) {
|
|
131
|
+
return allow.some((pattern) => pattern.endsWith("*")
|
|
132
|
+
? method.startsWith(pattern.slice(0, -1))
|
|
133
|
+
: method === pattern);
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC;AAkShC,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,CAAC,CAAC,CAAC;AAkC7E;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,IAAa;IAC1C,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC5D,MAAM,CAAC,GAAG,IAA+B,CAAC;IAC1C,IAAI,CAAC,CAAC,IAAI,KAAK,cAAc;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM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AAS;gBACzB,CAAC,CAAC,CAAC,KAAK,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,CAAC;gBACtD,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,SAAS,KAAK,QAAQ,CAAC,CAC/D,CAAC;QACJ,KAAK,QAAQ;YACX,OAAO,OAAO,CAAC,CAAC,MAAM,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;QAC7D;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAC3B,MAAc,EACd,KAAwB;IAExB,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAC5B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;QACnB,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC,MAAM,KAAK,OAAO,CACvB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bridge.service`: the extension's own service, from its page, as the
|
|
3
|
+
* signed-in person.
|
|
4
|
+
*
|
|
5
|
+
* const notes = await bridge.service.fetch("/v1/notes");
|
|
6
|
+
* const room = await bridge.service.websocket("/v1/rooms/runbook");
|
|
7
|
+
*
|
|
8
|
+
* The page talks to its service directly — the host is for host things.
|
|
9
|
+
* What the host does is the one thing a page cannot: trade the person's
|
|
10
|
+
* igni session for a ten-minute token whose audience is **this
|
|
11
|
+
* extension's service and nothing else**. The page asks for "my token";
|
|
12
|
+
* igni decides whose service that is, from which extension the frame
|
|
13
|
+
* is. The same exchange the extension's process uses (`ext.service`),
|
|
14
|
+
* and the same cache.
|
|
15
|
+
*
|
|
16
|
+
* The token never leaves for anywhere but the service: `fetch` and
|
|
17
|
+
* `websocket` refuse a path that resolves to another origin, so a page
|
|
18
|
+
* cannot hand it to an egress host by accident. The frame's CSP admits
|
|
19
|
+
* the service's origin (`https`/`wss`) and the hosts the manifest
|
|
20
|
+
* declared in `permissions.egress`, and nothing else.
|
|
21
|
+
*
|
|
22
|
+
* `fetch` is a cross-origin call from igni's window. It sends the token
|
|
23
|
+
* and never credentials (`credentials: "omit"`); the service answers it
|
|
24
|
+
* because its route takes a person's token (igni-service's `BearerCors`,
|
|
25
|
+
* which every service gets from `igni.install`).
|
|
26
|
+
*/
|
|
27
|
+
/** Why `bridge.service` could not do what was asked. */
|
|
28
|
+
export declare class ServiceError extends Error {
|
|
29
|
+
/**
|
|
30
|
+
* `no_service` (none a page may reach), `refused` (igni gave no
|
|
31
|
+
* token — nobody signed in, say), `other_origin`, `closed`, `failed`.
|
|
32
|
+
*/
|
|
33
|
+
readonly code: string;
|
|
34
|
+
constructor(
|
|
35
|
+
/**
|
|
36
|
+
* `no_service` (none a page may reach), `refused` (igni gave no
|
|
37
|
+
* token — nobody signed in, say), `other_origin`, `closed`, `failed`.
|
|
38
|
+
*/
|
|
39
|
+
code: string, message: string);
|
|
40
|
+
}
|
|
41
|
+
/** What the host hands back for one token request. */
|
|
42
|
+
export interface IssuedToken {
|
|
43
|
+
/** The bearer token itself. */
|
|
44
|
+
token: string;
|
|
45
|
+
/** Seconds it has left. */
|
|
46
|
+
expiresIn: number;
|
|
47
|
+
}
|
|
48
|
+
/** How a socket says who it is, once it is open. */
|
|
49
|
+
export interface ServiceSocketOptions {
|
|
50
|
+
/**
|
|
51
|
+
* The first frame, carrying the token. A browser's WebSocket cannot
|
|
52
|
+
* set an `Authorization` header, and a token in the URL ends up in
|
|
53
|
+
* access logs, so the token goes in the first frame. The default is
|
|
54
|
+
* `{"type": "auth", "token": …}` — what igni's services read (the
|
|
55
|
+
* Artifacts document room, for one).
|
|
56
|
+
*/
|
|
57
|
+
authFrame?: (token: string) => string;
|
|
58
|
+
/** Sub-protocols, passed to the WebSocket as they are. */
|
|
59
|
+
protocols?: string | string[];
|
|
60
|
+
}
|
|
61
|
+
/** `bridge.service`, as a page uses it. */
|
|
62
|
+
export interface ServiceBridge {
|
|
63
|
+
/**
|
|
64
|
+
* The service's URL, or `null` when this extension has none a page
|
|
65
|
+
* may reach — then every other member rejects with `no_service`.
|
|
66
|
+
*/
|
|
67
|
+
readonly url: string | null;
|
|
68
|
+
/**
|
|
69
|
+
* A bearer token for the service. Kept until shortly before it
|
|
70
|
+
* expires and fetched again then; concurrent asks share one fetch.
|
|
71
|
+
*/
|
|
72
|
+
token(): Promise<string>;
|
|
73
|
+
/**
|
|
74
|
+
* `fetch` against the service, with the token. `path` is relative
|
|
75
|
+
* to {@link url}. A `401` fetches a new token and is sent once more.
|
|
76
|
+
*/
|
|
77
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
78
|
+
/**
|
|
79
|
+
* A WebSocket to `path` on the service (`https` → `wss`), resolved
|
|
80
|
+
* once it is open and has said who it is (see
|
|
81
|
+
* {@link ServiceSocketOptions.authFrame}).
|
|
82
|
+
*/
|
|
83
|
+
websocket(path: string, options?: ServiceSocketOptions): Promise<WebSocket>;
|
|
84
|
+
}
|
|
85
|
+
/** Fetch a new token when the held one has less than this left. */
|
|
86
|
+
export declare const RENEW_BEFORE_MS = 60000;
|
|
87
|
+
/** The default first frame: `{"type": "auth", "token": …}`. */
|
|
88
|
+
export declare function authMessage(token: string): string;
|
|
89
|
+
/** `bridge.service`'s implementation; the bridge builds one per frame. */
|
|
90
|
+
export declare class ServiceLink implements ServiceBridge {
|
|
91
|
+
readonly url: string | null;
|
|
92
|
+
private readonly ask;
|
|
93
|
+
private readonly env;
|
|
94
|
+
/** The token this link is holding, and when it stops being valid. */
|
|
95
|
+
private held;
|
|
96
|
+
/** The fetch in flight, so two asks share one request to the host. */
|
|
97
|
+
private fetching;
|
|
98
|
+
/**
|
|
99
|
+
* One link, for one frame.
|
|
100
|
+
*
|
|
101
|
+
* @param url — the service's URL, or `null` when there is none.
|
|
102
|
+
* @param ask — how a token is asked of the host.
|
|
103
|
+
* @param env — what the browser is asked for, when it is not this one.
|
|
104
|
+
*/
|
|
105
|
+
constructor(url: string | null, ask: () => Promise<IssuedToken>, env?: {
|
|
106
|
+
now?: () => number;
|
|
107
|
+
fetch?: typeof fetch;
|
|
108
|
+
WebSocket?: typeof WebSocket;
|
|
109
|
+
});
|
|
110
|
+
/**
|
|
111
|
+
* A bearer token for the service.
|
|
112
|
+
*
|
|
113
|
+
* @returns — the token, fetched if it is not held or is nearly spent.
|
|
114
|
+
*/
|
|
115
|
+
token(): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* `fetch` against the service, with the token.
|
|
118
|
+
*
|
|
119
|
+
* @param path — where to go, relative to the service's URL.
|
|
120
|
+
* @param init — what to send, as `fetch` takes it.
|
|
121
|
+
* @returns — the service's answer.
|
|
122
|
+
*/
|
|
123
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
124
|
+
/**
|
|
125
|
+
* A socket to the service, with the token in the first frame.
|
|
126
|
+
*
|
|
127
|
+
* @param path — where to open it, relative to the service's URL.
|
|
128
|
+
* @param options — how the socket says who it is.
|
|
129
|
+
* @returns — the open socket.
|
|
130
|
+
*/
|
|
131
|
+
websocket(path: string, options?: ServiceSocketOptions): Promise<WebSocket>;
|
|
132
|
+
/**
|
|
133
|
+
* A token that is worth handing out: the held one if it has time
|
|
134
|
+
* left, a new one otherwise.
|
|
135
|
+
*
|
|
136
|
+
* @param force — fetch a new one even when the held one is good.
|
|
137
|
+
* @returns — the token.
|
|
138
|
+
*/
|
|
139
|
+
private fresh;
|
|
140
|
+
/** `path` on the service, or a refusal if it is anywhere else. */
|
|
141
|
+
private resolve;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"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OAAO,CAAC,QAAQ,CAAC,GAAG;IAftB,qEAAqE;IACrE,OAAO,CAAC,IAAI,CAAqD;IACjE,sEAAsE;IACtE,OAAO,CAAC,QAAQ,CAAgC;IAEhD;;;;;;OAMG;gBAEQ,GAAG,EAAE,MAAM,GAAG,IAAI,EACV,GAAG,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,EAC/B,GAAG,GAAE;QACpB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;QACnB,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;QACrB,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;KACzB;IAGR;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAIxB;;;;;;OAMG;IACG,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,WAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAgBpE;;;;;;OAMG;IACG,SAAS,CACb,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,SAAS,CAAC;IA0BrB;;;;;;OAMG;YACW,KAAK;IA0BnB,kEAAkE;IAClE,OAAO,CAAC,OAAO;CA6BhB"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bridge.service`: the extension's own service, from its page, as the
|
|
3
|
+
* signed-in person.
|
|
4
|
+
*
|
|
5
|
+
* const notes = await bridge.service.fetch("/v1/notes");
|
|
6
|
+
* const room = await bridge.service.websocket("/v1/rooms/runbook");
|
|
7
|
+
*
|
|
8
|
+
* The page talks to its service directly — the host is for host things.
|
|
9
|
+
* What the host does is the one thing a page cannot: trade the person's
|
|
10
|
+
* igni session for a ten-minute token whose audience is **this
|
|
11
|
+
* extension's service and nothing else**. The page asks for "my token";
|
|
12
|
+
* igni decides whose service that is, from which extension the frame
|
|
13
|
+
* is. The same exchange the extension's process uses (`ext.service`),
|
|
14
|
+
* and the same cache.
|
|
15
|
+
*
|
|
16
|
+
* The token never leaves for anywhere but the service: `fetch` and
|
|
17
|
+
* `websocket` refuse a path that resolves to another origin, so a page
|
|
18
|
+
* cannot hand it to an egress host by accident. The frame's CSP admits
|
|
19
|
+
* the service's origin (`https`/`wss`) and the hosts the manifest
|
|
20
|
+
* declared in `permissions.egress`, and nothing else.
|
|
21
|
+
*
|
|
22
|
+
* `fetch` is a cross-origin call from igni's window. It sends the token
|
|
23
|
+
* and never credentials (`credentials: "omit"`); the service answers it
|
|
24
|
+
* because its route takes a person's token (igni-service's `BearerCors`,
|
|
25
|
+
* which every service gets from `igni.install`).
|
|
26
|
+
*/
|
|
27
|
+
/** Why `bridge.service` could not do what was asked. */
|
|
28
|
+
export class ServiceError extends Error {
|
|
29
|
+
code;
|
|
30
|
+
constructor(
|
|
31
|
+
/**
|
|
32
|
+
* `no_service` (none a page may reach), `refused` (igni gave no
|
|
33
|
+
* token — nobody signed in, say), `other_origin`, `closed`, `failed`.
|
|
34
|
+
*/
|
|
35
|
+
code, message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.name = "ServiceError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Fetch a new token when the held one has less than this left. */
|
|
42
|
+
export const RENEW_BEFORE_MS = 60_000;
|
|
43
|
+
/** The default first frame: `{"type": "auth", "token": …}`. */
|
|
44
|
+
export function authMessage(token) {
|
|
45
|
+
return JSON.stringify({ type: "auth", token });
|
|
46
|
+
}
|
|
47
|
+
/** `bridge.service`'s implementation; the bridge builds one per frame. */
|
|
48
|
+
export class ServiceLink {
|
|
49
|
+
url;
|
|
50
|
+
ask;
|
|
51
|
+
env;
|
|
52
|
+
/** The token this link is holding, and when it stops being valid. */
|
|
53
|
+
held = null;
|
|
54
|
+
/** The fetch in flight, so two asks share one request to the host. */
|
|
55
|
+
fetching = null;
|
|
56
|
+
/**
|
|
57
|
+
* One link, for one frame.
|
|
58
|
+
*
|
|
59
|
+
* @param url — the service's URL, or `null` when there is none.
|
|
60
|
+
* @param ask — how a token is asked of the host.
|
|
61
|
+
* @param env — what the browser is asked for, when it is not this one.
|
|
62
|
+
*/
|
|
63
|
+
constructor(url, ask, env = {}) {
|
|
64
|
+
this.url = url;
|
|
65
|
+
this.ask = ask;
|
|
66
|
+
this.env = env;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A bearer token for the service.
|
|
70
|
+
*
|
|
71
|
+
* @returns — the token, fetched if it is not held or is nearly spent.
|
|
72
|
+
*/
|
|
73
|
+
token() {
|
|
74
|
+
return this.fresh(false);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `fetch` against the service, with the token.
|
|
78
|
+
*
|
|
79
|
+
* @param path — where to go, relative to the service's URL.
|
|
80
|
+
* @param init — what to send, as `fetch` takes it.
|
|
81
|
+
* @returns — the service's answer.
|
|
82
|
+
*/
|
|
83
|
+
async fetch(path, init = {}) {
|
|
84
|
+
const target = this.resolve(path, false);
|
|
85
|
+
const send = async (token) => {
|
|
86
|
+
const headers = new Headers(init.headers);
|
|
87
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
88
|
+
const doFetch = this.env.fetch ?? globalThis.fetch.bind(globalThis);
|
|
89
|
+
// The token and nothing else: no cookies, whatever the caller
|
|
90
|
+
// asked, so the service's own website sessions never ride along.
|
|
91
|
+
return doFetch(target, { ...init, headers, credentials: "omit" });
|
|
92
|
+
};
|
|
93
|
+
const first = await send(await this.fresh(false));
|
|
94
|
+
if (first.status !== 401)
|
|
95
|
+
return first;
|
|
96
|
+
// Revoked or expired early: one new token, one more try.
|
|
97
|
+
return send(await this.fresh(true));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A socket to the service, with the token in the first frame.
|
|
101
|
+
*
|
|
102
|
+
* @param path — where to open it, relative to the service's URL.
|
|
103
|
+
* @param options — how the socket says who it is.
|
|
104
|
+
* @returns — the open socket.
|
|
105
|
+
*/
|
|
106
|
+
async websocket(path, options = {}) {
|
|
107
|
+
const target = this.resolve(path, true);
|
|
108
|
+
const token = await this.fresh(false);
|
|
109
|
+
const Socket = this.env.WebSocket ?? globalThis.WebSocket;
|
|
110
|
+
const frame = (options.authFrame ?? authMessage)(token);
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
const socket = new Socket(target, options.protocols);
|
|
113
|
+
const opened = () => {
|
|
114
|
+
socket.removeEventListener("open", opened);
|
|
115
|
+
socket.removeEventListener("close", failed);
|
|
116
|
+
socket.send(frame);
|
|
117
|
+
resolve(socket);
|
|
118
|
+
};
|
|
119
|
+
const failed = () => {
|
|
120
|
+
socket.removeEventListener("open", opened);
|
|
121
|
+
socket.removeEventListener("close", failed);
|
|
122
|
+
reject(new ServiceError("closed", `the service closed ${path} before it opened`));
|
|
123
|
+
};
|
|
124
|
+
socket.addEventListener("open", opened);
|
|
125
|
+
// `error` is always followed by `close`; one rejection is enough.
|
|
126
|
+
socket.addEventListener("close", failed);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A token that is worth handing out: the held one if it has time
|
|
131
|
+
* left, a new one otherwise.
|
|
132
|
+
*
|
|
133
|
+
* @param force — fetch a new one even when the held one is good.
|
|
134
|
+
* @returns — the token.
|
|
135
|
+
*/
|
|
136
|
+
async fresh(force) {
|
|
137
|
+
if (this.url === null) {
|
|
138
|
+
throw new ServiceError("no_service", "this extension has no service its page may reach");
|
|
139
|
+
}
|
|
140
|
+
const now = (this.env.now ?? Date.now)();
|
|
141
|
+
if (!force && this.held && this.held.expiresAt - now > RENEW_BEFORE_MS) {
|
|
142
|
+
return this.held.token;
|
|
143
|
+
}
|
|
144
|
+
if (this.fetching)
|
|
145
|
+
return this.fetching;
|
|
146
|
+
this.fetching = this.ask()
|
|
147
|
+
.then((issued) => {
|
|
148
|
+
this.held = {
|
|
149
|
+
token: issued.token,
|
|
150
|
+
expiresAt: (this.env.now ?? Date.now)() + issued.expiresIn * 1000,
|
|
151
|
+
};
|
|
152
|
+
return issued.token;
|
|
153
|
+
})
|
|
154
|
+
.finally(() => {
|
|
155
|
+
this.fetching = null;
|
|
156
|
+
});
|
|
157
|
+
return this.fetching;
|
|
158
|
+
}
|
|
159
|
+
/** `path` on the service, or a refusal if it is anywhere else. */
|
|
160
|
+
resolve(path, socket) {
|
|
161
|
+
if (this.url === null) {
|
|
162
|
+
throw new ServiceError("no_service", "this extension has no service its page may reach");
|
|
163
|
+
}
|
|
164
|
+
const base = new URL(this.url);
|
|
165
|
+
const joined = new URL(`${base.pathname.replace(/\/$/, "")}/${path.replace(/^\//, "")}`, base);
|
|
166
|
+
// Something like `//elsewhere.example/x` or a full URL would land
|
|
167
|
+
// on another origin, and the token goes only to the service.
|
|
168
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path) ||
|
|
169
|
+
path.startsWith("//") ||
|
|
170
|
+
joined.origin !== base.origin) {
|
|
171
|
+
throw new ServiceError("other_origin", `'${path}' is not on this extension's service; its token goes nowhere else`);
|
|
172
|
+
}
|
|
173
|
+
if (socket) {
|
|
174
|
+
joined.protocol = joined.protocol === "https:" ? "wss:" : "ws:";
|
|
175
|
+
}
|
|
176
|
+
return joined.toString();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,wDAAwD;AACxD,MAAM,OAAO,YAAa,SAAQ,KAAK;IAM1B;IALX;IACE;;;OAGG;IACM,IAAY,EACrB,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,SAAI,GAAJ,IAAI,CAAQ;QAIrB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAiDD,mEAAmE;AACnE,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC;AAEt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;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,kEAAkE;IAC1D,OAAO,CAAC,IAAY,EAAE,MAAe;QAC3C,IAAI,IAAI,CAAC,GAAG,KAAK,IAAI,EAAE,CAAC;YACtB,MAAM,IAAI,YAAY,CACpB,YAAY,EACZ,kDAAkD,CACnD,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/B,MAAM,MAAM,GAAG,IAAI,GAAG,CACpB,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,EAChE,IAAI,CACL,CAAC;QACF,kEAAkE;QAClE,6DAA6D;QAC7D,IACE,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC;YACjC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;YACrB,MAAM,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAC7B,CAAC;YACD,MAAM,IAAI,YAAY,CACpB,cAAc,EACd,IAAI,IAAI,mEAAmE,CAC5E,CAAC;QACJ,CAAC;QACD,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAClE,CAAC;QACD,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host signals a page handles: `bridge.on(signal, fn)`.
|
|
3
|
+
*
|
|
4
|
+
* The first handler of a signal subscribes to it; removing the last one
|
|
5
|
+
* unsubscribes. A refused subscription is reported, not swallowed: to
|
|
6
|
+
* the handler's `onRefused` when it gave one, to the console otherwise.
|
|
7
|
+
*/
|
|
8
|
+
import type { HostError, Signal } from "./hostModel.js";
|
|
9
|
+
import type { HostSignalMessage } from "./protocol.js";
|
|
10
|
+
/** Asks the host to start or stop sending one signal. */
|
|
11
|
+
export type SignalRequest = (op: "subscribe" | "unsubscribe", signal: string) => Promise<unknown>;
|
|
12
|
+
export declare class SignalSubscriptions {
|
|
13
|
+
private readonly request;
|
|
14
|
+
private readonly handlers;
|
|
15
|
+
constructor(request: SignalRequest);
|
|
16
|
+
on<T>(signal: Signal<T>, fn: (value: T) => void, onRefused?: (error: HostError) => void): () => void;
|
|
17
|
+
/** Hand a signal the host sent to everything handling it. */
|
|
18
|
+
deliver(message: HostSignalMessage): void;
|
|
19
|
+
private off;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=signals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,yDAAyD;AACzD,MAAM,MAAM,aAAa,GAAG,CAC1B,EAAE,EAAE,WAAW,GAAG,aAAa,EAC/B,MAAM,EAAE,MAAM,KACX,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,qBAAa,mBAAmB;IAGlB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoD;gBAEhD,OAAO,EAAE,aAAa;IAEnD,EAAE,CAAC,CAAC,EACF,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EACjB,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EACtB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,GACrC,MAAM,IAAI;IAmBb,6DAA6D;IAC7D,OAAO,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI;IAMzC,OAAO,CAAC,GAAG;CAYZ"}
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host signals a page handles: `bridge.on(signal, fn)`.
|
|
3
|
+
*
|
|
4
|
+
* The first handler of a signal subscribes to it; removing the last one
|
|
5
|
+
* unsubscribes. A refused subscription is reported, not swallowed: to
|
|
6
|
+
* the handler's `onRefused` when it gave one, to the console otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export class SignalSubscriptions {
|
|
9
|
+
request;
|
|
10
|
+
handlers = new Map();
|
|
11
|
+
constructor(request) {
|
|
12
|
+
this.request = request;
|
|
13
|
+
}
|
|
14
|
+
on(signal, fn, onRefused) {
|
|
15
|
+
const wire = signal.name;
|
|
16
|
+
let handlers = this.handlers.get(wire);
|
|
17
|
+
if (handlers === undefined) {
|
|
18
|
+
handlers = new Set();
|
|
19
|
+
this.handlers.set(wire, handlers);
|
|
20
|
+
this.request("subscribe", wire).catch((error) => {
|
|
21
|
+
if (onRefused)
|
|
22
|
+
onRefused(error);
|
|
23
|
+
else
|
|
24
|
+
console.warn(`igni bridge: subscribing to ${wire} failed: ${error.message}`);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const handler = fn;
|
|
28
|
+
handlers.add(handler);
|
|
29
|
+
return () => this.off(wire, handler);
|
|
30
|
+
}
|
|
31
|
+
/** Hand a signal the host sent to everything handling it. */
|
|
32
|
+
deliver(message) {
|
|
33
|
+
const handlers = this.handlers.get(message.signal);
|
|
34
|
+
if (handlers === undefined)
|
|
35
|
+
return;
|
|
36
|
+
for (const fn of handlers)
|
|
37
|
+
fn(message.value);
|
|
38
|
+
}
|
|
39
|
+
off(wire, handler) {
|
|
40
|
+
const current = this.handlers.get(wire);
|
|
41
|
+
if (current === undefined)
|
|
42
|
+
return;
|
|
43
|
+
current.delete(handler);
|
|
44
|
+
if (current.size > 0)
|
|
45
|
+
return;
|
|
46
|
+
this.handlers.delete(wire);
|
|
47
|
+
this.request("unsubscribe", wire).catch((error) => {
|
|
48
|
+
console.warn(`igni bridge: unsubscribing from ${wire} failed: ${error.message}`);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=signals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.js","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAWH,MAAM,OAAO,mBAAmB;IAGD;IAFZ,QAAQ,GAAG,IAAI,GAAG,EAAyC,CAAC;IAE7E,YAA6B,OAAsB;QAAtB,YAAO,GAAP,OAAO,CAAe;IAAG,CAAC;IAEvD,EAAE,CACA,MAAiB,EACjB,EAAsB,EACtB,SAAsC;QAEtC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACzB,IAAI,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;YACrB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YAClC,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,KAAgB,EAAE,EAAE;gBACzD,IAAI,SAAS;oBAAE,SAAS,CAAC,KAAK,CAAC,CAAC;;oBAE9B,OAAO,CAAC,IAAI,CACV,+BAA+B,IAAI,YAAY,KAAK,CAAC,OAAO,EAAE,CAC/D,CAAC;YACN,CAAC,CAAC,CAAC;QACL,CAAC;QACD,MAAM,OAAO,GAAG,EAA8B,CAAC;QAC/C,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACtB,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,6DAA6D;IAC7D,OAAO,CAAC,OAA0B;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACnD,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAO;QACnC,KAAK,MAAM,EAAE,IAAI,QAAQ;YAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;IAEO,GAAG,CAAC,IAAY,EAAE,OAAiC;QACzD,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACxC,IAAI,OAAO,KAAK,SAAS;YAAE,OAAO;QAClC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxB,IAAI,OAAO,CAAC,IAAI,GAAG,CAAC;YAAE,OAAO;QAC7B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,KAAgB,EAAE,EAAE;YAC3D,OAAO,CAAC,IAAI,CACV,mCAAmC,IAAI,YAAY,KAAK,CAAC,OAAO,EAAE,CACnE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/dist/tokens.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The design tokens an extension may rely on, and how they travel.
|
|
3
|
+
*
|
|
4
|
+
* An extension's page is a different origin, so it cannot read this
|
|
5
|
+
* app's stylesheet — and the CSP on its own origin would block
|
|
6
|
+
* fetching one. So the values are **pushed**, over the bridge, at
|
|
7
|
+
* handshake and again whenever they change.
|
|
8
|
+
*
|
|
9
|
+
* Pushed rather than copied into the package on purpose. A static
|
|
10
|
+
* copy is a second description of the theme, and a second description
|
|
11
|
+
* drifts: `theme.css` defines these across forty-five blocks — a
|
|
12
|
+
* ``:root``, a light override, and a ``prefers-color-scheme`` block
|
|
13
|
+
* per feature. Anything duplicating that is wrong within a release.
|
|
14
|
+
* This reads the live computed values instead, so an extension
|
|
15
|
+
* follows dark mode, light mode, and whatever a group's policy pack
|
|
16
|
+
* has branded, without knowing any of those exist.
|
|
17
|
+
*
|
|
18
|
+
* **This list is the floor.** A token named here is one an extension
|
|
19
|
+
* may build against, and removing or repurposing one is a breaking
|
|
20
|
+
* change under the compatibility policy. A token *not* named here is
|
|
21
|
+
* ours to change freely — which is why the list is curated rather
|
|
22
|
+
* than "every custom property in the stylesheet". Layout internals
|
|
23
|
+
* like ``--rail-width`` and ``--crumb-row-h`` are deliberately absent:
|
|
24
|
+
* they describe this app's chrome, and an extension that positioned
|
|
25
|
+
* itself against them would break the first time we moved something.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Published tokens, by what they are for.
|
|
29
|
+
*
|
|
30
|
+
* Grouped because the groups are how an author reads them: pick a
|
|
31
|
+
* surface, pick the text that goes on it, pick a tone for the
|
|
32
|
+
* meaning. Names match the app's own so a reader moving between the
|
|
33
|
+
* two is not translating.
|
|
34
|
+
*/
|
|
35
|
+
export declare const PUBLISHED_TOKENS: {
|
|
36
|
+
/** Surfaces, back to front. */
|
|
37
|
+
readonly surface: readonly ["--bg", "--bg-inset", "--bg-surface", "--bg-raised"];
|
|
38
|
+
/** Text, in descending prominence. */
|
|
39
|
+
readonly text: readonly ["--fg", "--fg-muted", "--fg-faint"];
|
|
40
|
+
/** Lines between things. */
|
|
41
|
+
readonly line: readonly ["--border", "--border-soft"];
|
|
42
|
+
/** Meaning. ``*-text`` variants are the contrast-safe ones for type. */
|
|
43
|
+
readonly tone: readonly ["--success", "--warning", "--danger", "--danger-text", "--accent-blue", "--accent-blue-text", "--accent-green-text", "--accent-orange-text", "--accent-red-text"];
|
|
44
|
+
/** The product's own colour. Use sparingly; it means igni. */
|
|
45
|
+
readonly brand: readonly ["--igni-orange", "--igni-orange-hover", "--igni-red", "--igni-gradient"];
|
|
46
|
+
/** Shape. */
|
|
47
|
+
readonly form: readonly ["--radius-sm", "--radius", "--radius-lg", "--radius-pill", "--radius-circle", "--font-ui", "--font-mono", "--shadow-pop"];
|
|
48
|
+
/**
|
|
49
|
+
* The type scale. Nine steps, by role rather than by ratio.
|
|
50
|
+
*
|
|
51
|
+
* It replaced twenty-two hardcoded sizes that included every
|
|
52
|
+
* half-step from 9px to 15px — 11.5px, 12px and 12.5px between
|
|
53
|
+
* them accounted for 272 declarations, which is one size written
|
|
54
|
+
* three ways. Build against the role, not the pixel.
|
|
55
|
+
*/
|
|
56
|
+
readonly type: readonly ["--text-2xs", "--text-xs", "--text-sm", "--text-md", "--text-lg", "--text-xl", "--text-2xl", "--text-3xl", "--text-4xl"];
|
|
57
|
+
/**
|
|
58
|
+
* The spacing scale. Published so an extension's layout sits on the
|
|
59
|
+
* same rhythm as the app around it rather than next to it.
|
|
60
|
+
*
|
|
61
|
+
* These steps are the ones igni already used — they were written as
|
|
62
|
+
* literals in three hundred and seventy-five places, which is why
|
|
63
|
+
* adopting them changed nothing on screen.
|
|
64
|
+
*/
|
|
65
|
+
readonly space: readonly ["--space-1", "--space-2", "--space-3", "--space-4", "--space-5", "--space-6", "--space-7", "--space-8", "--space-10", "--space-12", "--space-16"];
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Every published token name, flat.
|
|
69
|
+
*
|
|
70
|
+
* Derived rather than listed. A second list would be a second place
|
|
71
|
+
* to forget — and the groups above are an object literal, where a
|
|
72
|
+
* repeated key silently keeps only the last one. That happened: the
|
|
73
|
+
* type scale was added as `text`, which was already the foreground
|
|
74
|
+
* colours, and `--fg` disappeared without a word.
|
|
75
|
+
*/
|
|
76
|
+
export declare const TOKEN_NAMES: readonly string[];
|
|
77
|
+
/** Token name → current value. */
|
|
78
|
+
export type ThemeTokens = Record<string, string>;
|
|
79
|
+
/**
|
|
80
|
+
* Read the published tokens as they are right now.
|
|
81
|
+
*
|
|
82
|
+
* Computed values, not declared ones: a token defined in a media
|
|
83
|
+
* query or overridden by a `data-theme` attribute resolves here to
|
|
84
|
+
* what the user is actually looking at, which is the only answer
|
|
85
|
+
* worth sending.
|
|
86
|
+
*/
|
|
87
|
+
export declare function readThemeTokens(root?: Element): ThemeTokens;
|
|
88
|
+
/**
|
|
89
|
+
* Apply tokens inside the extension's own document.
|
|
90
|
+
*
|
|
91
|
+
* Called by the guest bridge. Separate from reading them so the two
|
|
92
|
+
* halves can be tested without a frame between them.
|
|
93
|
+
*/
|
|
94
|
+
export declare function applyThemeTokens(tokens: ThemeTokens, root: HTMLElement): void;
|
|
95
|
+
//# sourceMappingURL=tokens.d.ts.map
|