astralis-devtools 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/CHANGELOG.md +71 -0
- package/LICENSE +21 -0
- package/README.md +86 -3
- package/package.json +66 -3
- package/src/bridge/bridge.mjs +169 -0
- package/src/bridge/github.mjs +139 -0
- package/src/bridge/http.mjs +194 -0
- package/src/bridge/node-adapter.mjs +68 -0
- package/src/bridge/project.mjs +103 -0
- package/src/bridge/snapshot.mjs +70 -0
- package/src/client/dom.mjs +82 -0
- package/src/client/fonts/OFL.txt +94 -0
- package/src/client/fonts.mjs +22 -0
- package/src/client/icons.mjs +43 -0
- package/src/client/identity.mjs +117 -0
- package/src/client/launcher.mjs +143 -0
- package/src/client/overlay.d.mts +9 -0
- package/src/client/overlay.mjs +529 -0
- package/src/client/page.mjs +92 -0
- package/src/client/picker.mjs +118 -0
- package/src/client/props.mjs +154 -0
- package/src/client/report.mjs +121 -0
- package/src/client/screens/component.mjs +158 -0
- package/src/client/screens/components.mjs +117 -0
- package/src/client/screens/findings.mjs +92 -0
- package/src/client/screens/home.mjs +100 -0
- package/src/client/screens/props.mjs +192 -0
- package/src/client/screens/report.mjs +459 -0
- package/src/client/screens/shared.mjs +59 -0
- package/src/client/styles.mjs +541 -0
- package/src/next/devtools.d.mts +6 -0
- package/src/next/devtools.mjs +32 -0
- package/src/next/route.d.mts +10 -0
- package/src/next/route.mjs +17 -0
- package/src/vite.d.mts +12 -0
- package/src/vite.mjs +59 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { bridgeFor } from "./bridge.mjs";
|
|
2
|
+
import { githubSession } from "./github.mjs";
|
|
3
|
+
import { assembleSnapshot } from "./snapshot.mjs";
|
|
4
|
+
|
|
5
|
+
/* The bridge over HTTP, in web-standard Request/Response so the Next.js
|
|
6
|
+
route handler and the Vite middleware share it. Mounted at BASE_PATH:
|
|
7
|
+
|
|
8
|
+
GET info versions of astralis-ui (with the spec's SHA-256), the framework and React
|
|
9
|
+
GET spec the installed system spec, for the component pages
|
|
10
|
+
GET findings the validator's findings for the project
|
|
11
|
+
GET events the same findings as a server-sent event stream, re-sent on every change
|
|
12
|
+
GET route-files ?path=/about: the files that render that page (Next.js)
|
|
13
|
+
POST snapshot the page's part of an issue snapshot in; the complete snapshot out
|
|
14
|
+
POST submit a snapshot the reporter confirmed, opened as a GitHub issue;
|
|
15
|
+
?addTo=12 adds it to that existing issue instead
|
|
16
|
+
GET github the GitHub sign-in: set up or not, who, and a code to enter
|
|
17
|
+
POST github/sign-in start signing in with a code (the bridge polls GitHub)
|
|
18
|
+
POST github/sign-out forget the saved sign-in
|
|
19
|
+
GET github/issues the issues the signed-in user opened on the Astralis repository
|
|
20
|
+
POST github/similar a snapshot in; the issues that may already report it out
|
|
21
|
+
POST github/link a snapshot in; GitHub's own new-issue form, filled in, out
|
|
22
|
+
|
|
23
|
+
It answers the local machine only, and nothing at all in production. The
|
|
24
|
+
POST routes take JSON only: a page on another site cannot send that type
|
|
25
|
+
without a preflight the bridge never grants. The GitHub token never leaves
|
|
26
|
+
the bridge: the page sees the login, never the token. */
|
|
27
|
+
|
|
28
|
+
export const BASE_PATH = "/api/astralis-devtools";
|
|
29
|
+
|
|
30
|
+
const KEEPALIVE_MS = 15_000;
|
|
31
|
+
/** A snapshot is at most 256 kB; the page's part stays well under this. */
|
|
32
|
+
const MAX_BODY_BYTES = 1024 * 1024;
|
|
33
|
+
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
34
|
+
|
|
35
|
+
const json = (body, status = 200) =>
|
|
36
|
+
new Response(JSON.stringify(body), {
|
|
37
|
+
status,
|
|
38
|
+
headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" },
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A request from this machine: addressed to a loopback host and, when a page
|
|
43
|
+
* sent it, from a loopback page. A dev server opened to the network
|
|
44
|
+
* (`next dev -H 0.0.0.0`) still refuses the bridge to other machines, and a
|
|
45
|
+
* page on another site cannot read it by pointing a DNS name at 127.0.0.1.
|
|
46
|
+
*/
|
|
47
|
+
export function isLocal(request) {
|
|
48
|
+
const hostname = (value) => {
|
|
49
|
+
try {
|
|
50
|
+
return new URL(value).hostname;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const host = request.headers.get("host");
|
|
56
|
+
if (!host || !LOOPBACK.has(hostname(`http://${host}`))) return false;
|
|
57
|
+
const origin = request.headers.get("origin");
|
|
58
|
+
return origin === null || LOOPBACK.has(hostname(origin));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The path after BASE_PATH, e.g. "findings". */
|
|
62
|
+
function route(url) {
|
|
63
|
+
const at = url.pathname.indexOf(BASE_PATH);
|
|
64
|
+
const rest = at === -1 ? url.pathname : url.pathname.slice(at + BASE_PATH.length);
|
|
65
|
+
return rest.replace(/^\/+|\/+$/g, "");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function eventStream(bridge, signal) {
|
|
69
|
+
const encoder = new TextEncoder();
|
|
70
|
+
let stop = () => {};
|
|
71
|
+
const stream = new ReadableStream({
|
|
72
|
+
start(controller) {
|
|
73
|
+
const send = (text) => {
|
|
74
|
+
try {
|
|
75
|
+
controller.enqueue(encoder.encode(text));
|
|
76
|
+
} catch {
|
|
77
|
+
stop();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const publish = (findings) => send(`event: findings\ndata: ${JSON.stringify(findings)}\n\n`);
|
|
81
|
+
const unlisten = bridge.listen(publish);
|
|
82
|
+
const keepalive = setInterval(() => send(": keepalive\n\n"), KEEPALIVE_MS);
|
|
83
|
+
stop = () => {
|
|
84
|
+
unlisten();
|
|
85
|
+
clearInterval(keepalive);
|
|
86
|
+
try {
|
|
87
|
+
controller.close();
|
|
88
|
+
} catch {
|
|
89
|
+
// already closed by the client
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
93
|
+
publish(bridge.findings());
|
|
94
|
+
},
|
|
95
|
+
cancel() {
|
|
96
|
+
stop();
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
return new Response(stream, {
|
|
100
|
+
headers: {
|
|
101
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
102
|
+
"cache-control": "no-store, no-transform",
|
|
103
|
+
connection: "keep-alive",
|
|
104
|
+
"x-accel-buffering": "no",
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Answer one request. `root` is the project directory, `framework` "next" or
|
|
111
|
+
* "vite"; `watch` optionally replaces fs.watch (Vite passes its own watcher).
|
|
112
|
+
*/
|
|
113
|
+
/** The JSON body of a POST, or a Response explaining why it cannot be read. */
|
|
114
|
+
async function readJson(request) {
|
|
115
|
+
if (!/^application\/json\b/i.test(request.headers.get("content-type") ?? "")) {
|
|
116
|
+
return json({ error: "Send JSON (content-type: application/json)." }, 415);
|
|
117
|
+
}
|
|
118
|
+
const declared = Number(request.headers.get("content-length") ?? 0);
|
|
119
|
+
if (declared > MAX_BODY_BYTES) return json({ error: "The request is too large." }, 413);
|
|
120
|
+
const text = await request.text();
|
|
121
|
+
if (text.length > MAX_BODY_BYTES) return json({ error: "The request is too large." }, 413);
|
|
122
|
+
try {
|
|
123
|
+
return { body: JSON.parse(text) };
|
|
124
|
+
} catch {
|
|
125
|
+
return json({ error: "The request is not valid JSON." }, 400);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** A GitHub failure as a response: 401 when the reporter has to sign in (again). */
|
|
130
|
+
const githubFailure = (error) =>
|
|
131
|
+
json({ error: error.message, code: error.code ?? "github" }, error.code === "signed-out" ? 401 : error.code === "invalid" ? 400 : 502);
|
|
132
|
+
|
|
133
|
+
const POSTS = new Set(["snapshot", "submit", "github/sign-in", "github/sign-out", "github/similar", "github/link"]);
|
|
134
|
+
|
|
135
|
+
async function handlePost(bridge, path, request, github, url) {
|
|
136
|
+
if (!POSTS.has(path)) return json({ error: "Not found." }, 404);
|
|
137
|
+
const read = await readJson(request);
|
|
138
|
+
if (read instanceof Response) return read;
|
|
139
|
+
try {
|
|
140
|
+
if (path === "github/sign-in") return json(await github.signIn());
|
|
141
|
+
if (path === "github/sign-out") return json(github.signOut());
|
|
142
|
+
if (path === "github/similar") return json(await github.similar(read.body));
|
|
143
|
+
if (path === "github/link") return json(github.link(read.body));
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return githubFailure(error);
|
|
146
|
+
}
|
|
147
|
+
if (bridge.problem) return json({ error: bridge.problem }, 409);
|
|
148
|
+
if (path === "snapshot") return json(assembleSnapshot(bridge, read.body));
|
|
149
|
+
const addTo = url.searchParams.has("addTo") ? Number(url.searchParams.get("addTo")) : null;
|
|
150
|
+
if (addTo !== null && !(Number.isInteger(addTo) && addTo > 0)) return json({ error: "addTo takes an issue number." }, 400);
|
|
151
|
+
try {
|
|
152
|
+
return json(await github.submit(read.body, { addTo }));
|
|
153
|
+
} catch (error) {
|
|
154
|
+
return githubFailure(error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function handle(request, { root = process.cwd(), framework, watch, github = githubSession() } = {}) {
|
|
159
|
+
if (process.env.NODE_ENV === "production") return new Response(null, { status: 404 });
|
|
160
|
+
if (!isLocal(request)) return json({ error: "The Astralis DevTools bridge answers this machine only." }, 403);
|
|
161
|
+
if (request.method !== "GET" && request.method !== "POST") return json({ error: "Method not allowed." }, 405);
|
|
162
|
+
|
|
163
|
+
const bridge = bridgeFor(root, framework, { watch });
|
|
164
|
+
await bridge.start();
|
|
165
|
+
const url = new URL(request.url);
|
|
166
|
+
if (request.method === "POST") return handlePost(bridge, route(url), request, github, url);
|
|
167
|
+
|
|
168
|
+
switch (route(url)) {
|
|
169
|
+
case "info":
|
|
170
|
+
return bridge.problem ? json({ problem: bridge.problem }) : json(bridge.info);
|
|
171
|
+
case "spec":
|
|
172
|
+
return bridge.problem
|
|
173
|
+
? json({ problem: bridge.problem }, 404)
|
|
174
|
+
: new Response(bridge.specText, {
|
|
175
|
+
headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" },
|
|
176
|
+
});
|
|
177
|
+
case "findings":
|
|
178
|
+
return json(bridge.findings());
|
|
179
|
+
case "events":
|
|
180
|
+
return eventStream(bridge, request.signal);
|
|
181
|
+
case "github":
|
|
182
|
+
return json(github.status());
|
|
183
|
+
case "github/issues":
|
|
184
|
+
try {
|
|
185
|
+
return json({ issues: await github.issues() });
|
|
186
|
+
} catch (error) {
|
|
187
|
+
return githubFailure(error);
|
|
188
|
+
}
|
|
189
|
+
case "route-files":
|
|
190
|
+
return json({ path: url.searchParams.get("path") ?? "/", files: bridge.routeFiles(url.searchParams.get("path") ?? "/") });
|
|
191
|
+
default:
|
|
192
|
+
return json({ error: "Not found." }, 404);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { handle } from "./http.mjs";
|
|
2
|
+
|
|
3
|
+
/* The bridge as Node middleware (Vite's dev server is Connect): turn the
|
|
4
|
+
IncomingMessage into a web Request, answer it, and stream the Response back,
|
|
5
|
+
event stream included. */
|
|
6
|
+
|
|
7
|
+
const LOOPBACK_ADDRESS = /^(127\.|::1$|::ffff:127\.)/;
|
|
8
|
+
/** Past this the bridge refuses the request anyway; stop reading rather than buffer more. */
|
|
9
|
+
const READ_LIMIT = 1024 * 1024 + 1;
|
|
10
|
+
|
|
11
|
+
function readBody(req) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const chunks = [];
|
|
14
|
+
let size = 0;
|
|
15
|
+
req.on("data", (chunk) => {
|
|
16
|
+
size += chunk.length;
|
|
17
|
+
if (size <= READ_LIMIT) chunks.push(chunk);
|
|
18
|
+
});
|
|
19
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
20
|
+
req.on("error", reject);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Middleware for `server.middlewares.use(BASE_PATH, middleware)`. `options`
|
|
26
|
+
* are handle()'s: root, framework and watch. The socket must come from this
|
|
27
|
+
* machine as well; in Node that is known directly, not only from headers.
|
|
28
|
+
*/
|
|
29
|
+
export function bridgeMiddleware(options) {
|
|
30
|
+
return async (req, res, next) => {
|
|
31
|
+
try {
|
|
32
|
+
if (!LOOPBACK_ADDRESS.test(req.socket?.remoteAddress ?? "")) {
|
|
33
|
+
res.statusCode = 403;
|
|
34
|
+
res.end();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
res.on("close", () => controller.abort());
|
|
39
|
+
const headers = new Headers();
|
|
40
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
41
|
+
if (typeof value === "string") headers.set(name, value);
|
|
42
|
+
else if (Array.isArray(value)) headers.set(name, value.join(", "));
|
|
43
|
+
}
|
|
44
|
+
const url = new URL(req.originalUrl ?? req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
|
|
45
|
+
const init = { method: req.method, headers, signal: controller.signal };
|
|
46
|
+
if (req.method === "POST") init.body = await readBody(req);
|
|
47
|
+
const response = await handle(new Request(url, init), options);
|
|
48
|
+
|
|
49
|
+
res.statusCode = response.status;
|
|
50
|
+
response.headers.forEach((value, name) => res.setHeader(name, value));
|
|
51
|
+
if (!response.body) {
|
|
52
|
+
res.end();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
res.flushHeaders?.();
|
|
56
|
+
const reader = response.body.getReader();
|
|
57
|
+
res.on("close", () => reader.cancel().catch(() => {}));
|
|
58
|
+
for (;;) {
|
|
59
|
+
const { done, value } = await reader.read();
|
|
60
|
+
if (done) break;
|
|
61
|
+
res.write(value);
|
|
62
|
+
}
|
|
63
|
+
res.end();
|
|
64
|
+
} catch (error) {
|
|
65
|
+
next(error);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
/* Facts about the project the dev server is running: which astralis-ui,
|
|
6
|
+
framework and React it has installed, and which files make up a route. */
|
|
7
|
+
|
|
8
|
+
/** The nearest node_modules/<name>/package.json above `root`, parsed, or null. */
|
|
9
|
+
export function installedPackage(root, name) {
|
|
10
|
+
for (let dir = resolve(root); ; dir = dirname(dir)) {
|
|
11
|
+
const candidate = join(dir, "node_modules", name, "package.json");
|
|
12
|
+
if (statSync(candidate, { throwIfNoEntry: false })) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(readFileSync(candidate, "utf8"));
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (dirname(dir) === dir) return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The spec file's SHA-256: two reports with the same hash ran against the same contract. */
|
|
24
|
+
export function specHash(specText) {
|
|
25
|
+
return createHash("sha256").update(specText).digest("hex");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Versions the overlay shows and a report will carry. */
|
|
29
|
+
export function projectInfo(root, framework, spec, specText) {
|
|
30
|
+
return {
|
|
31
|
+
library: {
|
|
32
|
+
name: spec.name,
|
|
33
|
+
version: spec.version,
|
|
34
|
+
specVersion: spec.specVersion,
|
|
35
|
+
specSha256: specHash(specText),
|
|
36
|
+
},
|
|
37
|
+
framework: { name: framework, version: installedPackage(root, framework)?.version ?? null },
|
|
38
|
+
react: installedPackage(root, "react")?.version ?? null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* ---- Next.js routes --------------------------------------------------------
|
|
43
|
+
The files that render a URL: the page that matches it and every layout or
|
|
44
|
+
template above that page. Route groups "(marketing)" and parallel slots
|
|
45
|
+
"@modal" add no URL segment; [id] matches one segment, [...slug] one or
|
|
46
|
+
more, [[...slug]] any number. */
|
|
47
|
+
|
|
48
|
+
const APP_DIRS = ["app", "src/app"];
|
|
49
|
+
const ROUTE_FILE = /^(page|layout|template)\.(tsx|jsx)$/;
|
|
50
|
+
|
|
51
|
+
function segmentPattern(segment) {
|
|
52
|
+
if (/^\[\[\.\.\..+\]\]$/.test(segment)) return { kind: "optional-rest" };
|
|
53
|
+
if (/^\[\.\.\..+\]$/.test(segment)) return { kind: "rest" };
|
|
54
|
+
if (/^\[.+\]$/.test(segment)) return { kind: "one" };
|
|
55
|
+
return { kind: "static", value: segment };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function matches(patterns, parts) {
|
|
59
|
+
if (patterns.length === 0) return parts.length === 0;
|
|
60
|
+
const [head, ...tail] = patterns;
|
|
61
|
+
if (head.kind === "static") return parts[0] === head.value && matches(tail, parts.slice(1));
|
|
62
|
+
if (head.kind === "one") return parts.length > 0 && matches(tail, parts.slice(1));
|
|
63
|
+
const least = head.kind === "rest" ? 1 : 0;
|
|
64
|
+
for (let taken = parts.length; taken >= least; taken--) {
|
|
65
|
+
if (matches(tail, parts.slice(taken))) return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The project-relative files (forward slashes) that render `pathname`, from the
|
|
72
|
+
* project's relative file list. Empty when nothing matches or the project has
|
|
73
|
+
* no app directory.
|
|
74
|
+
*/
|
|
75
|
+
export function routeFiles(files, pathname) {
|
|
76
|
+
const parts = decodeURI(pathname.split(/[?#]/)[0]).split("/").filter(Boolean);
|
|
77
|
+
const appDir = APP_DIRS.find((dir) => files.some((file) => file.startsWith(`${dir}/`)));
|
|
78
|
+
if (!appDir) return [];
|
|
79
|
+
|
|
80
|
+
// Like Next.js, a static segment wins over [id], and [id] over a catch-all.
|
|
81
|
+
const cost = (patterns) =>
|
|
82
|
+
patterns.reduce((sum, p) => sum + (p.kind === "static" ? 0 : p.kind === "one" ? 1 : 100), 0);
|
|
83
|
+
const [page] = files
|
|
84
|
+
.filter((file) => file.startsWith(`${appDir}/`) && /\/page\.(tsx|jsx)$/.test(file))
|
|
85
|
+
.map((file) => {
|
|
86
|
+
const segments = file.slice(appDir.length + 1).split("/").slice(0, -1);
|
|
87
|
+
const patterns = segments.filter((s) => !/^\(.+\)$/.test(s) && !s.startsWith("@")).map(segmentPattern);
|
|
88
|
+
return { file, patterns };
|
|
89
|
+
})
|
|
90
|
+
.filter(({ patterns }) => matches(patterns, parts))
|
|
91
|
+
.sort((a, b) => cost(a.patterns) - cost(b.patterns))
|
|
92
|
+
.map(({ file }) => file);
|
|
93
|
+
if (!page) return [];
|
|
94
|
+
|
|
95
|
+
const dirs = page.split("/").slice(0, -1);
|
|
96
|
+
const above = new Set();
|
|
97
|
+
for (let depth = appDir.split("/").length; depth <= dirs.length; depth++) above.add(dirs.slice(0, depth).join("/"));
|
|
98
|
+
const shells = files.filter((file) => {
|
|
99
|
+
const name = file.split("/").pop();
|
|
100
|
+
return ROUTE_FILE.test(name) && !name.startsWith("page.") && above.has(file.split("/").slice(0, -1).join("/"));
|
|
101
|
+
});
|
|
102
|
+
return [page, ...shells.sort((a, b) => b.length - a.length)];
|
|
103
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LIMITS,
|
|
3
|
+
SNAPSHOT_FORMAT,
|
|
4
|
+
checkSnapshot,
|
|
5
|
+
cleanConsoleMessage,
|
|
6
|
+
cleanRoute,
|
|
7
|
+
narrowTree,
|
|
8
|
+
} from "astralis-cli/snapshot";
|
|
9
|
+
|
|
10
|
+
/* The bridge's half of an issue snapshot. The page sends what only the page
|
|
11
|
+
knows (the component tree, the browser, the reporter's words); the bridge
|
|
12
|
+
adds what only the dev server knows (versions, the spec's hash, the
|
|
13
|
+
validator's findings) and checks the whole. Nothing here makes a network
|
|
14
|
+
request; github.mjs sends a snapshot once the reporter confirms. */
|
|
15
|
+
|
|
16
|
+
const shortString = (value, max) => (typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined);
|
|
17
|
+
|
|
18
|
+
/** Findings for the files that render `route` (Next.js), else for the whole project. */
|
|
19
|
+
function findingsFor(bridge, route) {
|
|
20
|
+
const { results = [] } = bridge.findings();
|
|
21
|
+
const files = route ? new Set(bridge.routeFiles(route)) : new Set();
|
|
22
|
+
const chosen = files.size ? results.filter((r) => files.has(r.file)) : results;
|
|
23
|
+
return chosen
|
|
24
|
+
.flatMap((r) => [...r.errors, ...r.warnings])
|
|
25
|
+
.map(({ code, file, line, column, message }) => ({ code, file, line, column, message }))
|
|
26
|
+
.slice(0, LIMITS.findings);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function environmentOf(info, input) {
|
|
30
|
+
const env = input && typeof input === "object" ? input : {};
|
|
31
|
+
const viewport = env.viewport;
|
|
32
|
+
return {
|
|
33
|
+
framework: { name: info.framework?.name ?? null, version: info.framework?.version ?? null },
|
|
34
|
+
react: info.react ?? null,
|
|
35
|
+
browser: shortString(env.browser, 60) ?? null,
|
|
36
|
+
viewport: viewport && Number.isInteger(viewport.width) && Number.isInteger(viewport.height)
|
|
37
|
+
? { width: viewport.width, height: viewport.height }
|
|
38
|
+
: null,
|
|
39
|
+
colorMode: env.colorMode === "light" || env.colorMode === "dark" ? env.colorMode : null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The complete snapshot from the page's part. Returns { snapshot, problems }:
|
|
45
|
+
* the overlay shows `snapshot` as the exact JSON that would be sent.
|
|
46
|
+
*/
|
|
47
|
+
export function assembleSnapshot(bridge, input = {}) {
|
|
48
|
+
const include = input.include ?? {};
|
|
49
|
+
const route = cleanRoute(typeof input.route === "string" ? input.route : null);
|
|
50
|
+
const description = { title: shortString(input.description?.title, LIMITS.title) ?? "" };
|
|
51
|
+
const happened = shortString(input.description?.happened, LIMITS.body);
|
|
52
|
+
const expected = shortString(input.description?.expected, LIMITS.body);
|
|
53
|
+
if (happened) description.happened = happened;
|
|
54
|
+
if (expected) description.expected = expected;
|
|
55
|
+
|
|
56
|
+
const snapshot = {
|
|
57
|
+
format: SNAPSHOT_FORMAT,
|
|
58
|
+
source: "devtools",
|
|
59
|
+
library: { ...bridge.info.library },
|
|
60
|
+
environment: environmentOf(bridge.info, input.environment),
|
|
61
|
+
route,
|
|
62
|
+
tree: narrowTree(bridge.spec, input.tree),
|
|
63
|
+
...(include.findings === false ? {} : { findings: findingsFor(bridge, route) }),
|
|
64
|
+
...(include.console && Array.isArray(input.console)
|
|
65
|
+
? { console: input.console.slice(-LIMITS.console).map((m) => cleanConsoleMessage(m)) }
|
|
66
|
+
: {}),
|
|
67
|
+
description,
|
|
68
|
+
};
|
|
69
|
+
return { snapshot, problems: checkSnapshot(snapshot) };
|
|
70
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/* Small DOM helpers the overlay's screens share. Everything that comes from
|
|
2
|
+
the page or the bridge goes in as text: only the overlay's own icons are
|
|
3
|
+
markup. */
|
|
4
|
+
|
|
5
|
+
/** h("div", { class: "row" }, "text", child): an element, with strings as text nodes. */
|
|
6
|
+
export function h(tag, attributes = {}, ...children) {
|
|
7
|
+
const element = document.createElement(tag);
|
|
8
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
9
|
+
if (value === false || value === null || value === undefined) continue;
|
|
10
|
+
if (name.startsWith("on") && typeof value === "function") element.addEventListener(name.slice(2), value);
|
|
11
|
+
else element.setAttribute(name, value === true ? "" : String(value));
|
|
12
|
+
}
|
|
13
|
+
append(element, children);
|
|
14
|
+
return element;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function append(element, children) {
|
|
18
|
+
for (const child of children.flat(Infinity)) {
|
|
19
|
+
if (child === null || child === undefined || child === false) continue;
|
|
20
|
+
element.append(typeof child === "string" || typeof child === "number" ? String(child) : child);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An icon from icons.mjs: the overlay's own markup, never the page's. */
|
|
25
|
+
export function icon(markup, className = "icon") {
|
|
26
|
+
const span = document.createElement("span");
|
|
27
|
+
span.className = className;
|
|
28
|
+
span.setAttribute("aria-hidden", "true");
|
|
29
|
+
span.innerHTML = markup;
|
|
30
|
+
return span;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const plural = (count, word, many = `${word}s`) => `${count} ${count === 1 ? word : many}`;
|
|
34
|
+
|
|
35
|
+
export const formatValue = (value) => (typeof value === "string" ? `"${value}"` : JSON.stringify(value));
|
|
36
|
+
|
|
37
|
+
export const reducedMotion = () => globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
|
|
38
|
+
|
|
39
|
+
/** Run a Web Animation where the browser has one and motion is welcome. */
|
|
40
|
+
export function animate(element, keyframes, options) {
|
|
41
|
+
if (typeof element.animate !== "function" || reducedMotion()) return null;
|
|
42
|
+
return element.animate(keyframes, options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Count a number up into `element` the first time it is shown. */
|
|
46
|
+
export function countUp(element, to, duration = 520) {
|
|
47
|
+
element.textContent = String(to);
|
|
48
|
+
if (to <= 0 || reducedMotion() || typeof requestAnimationFrame !== "function") return;
|
|
49
|
+
const start = performance.now();
|
|
50
|
+
const step = (now) => {
|
|
51
|
+
const t = Math.min(1, (now - start) / duration);
|
|
52
|
+
element.textContent = String(Math.round(to * (1 - (1 - t) ** 3)));
|
|
53
|
+
if (t < 1 && element.isConnected) requestAnimationFrame(step);
|
|
54
|
+
};
|
|
55
|
+
requestAnimationFrame(step);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A file path split for display: the directory muted, the name bright. */
|
|
59
|
+
export function filePath(path, extra = null) {
|
|
60
|
+
const cut = path.lastIndexOf("/") + 1;
|
|
61
|
+
return h("span", { class: "path", title: path }, h("span", { class: "dir" }, path.slice(0, cut)), h("span", { class: "base" }, path.slice(cut)), extra);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const STORE = "astralis-devtools";
|
|
65
|
+
|
|
66
|
+
/** Per-viewer conveniences (where the launcher sits, whether the panel was open). Never required. */
|
|
67
|
+
export const store = {
|
|
68
|
+
read(storage, key) {
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(globalThis[storage]?.getItem(`${STORE}:${key}`) ?? "null");
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
write(storage, key, value) {
|
|
76
|
+
try {
|
|
77
|
+
globalThis[storage]?.setItem(`${STORE}:${key}`, JSON.stringify(value));
|
|
78
|
+
} catch {
|
|
79
|
+
// private mode or blocked storage: the overlay works without it
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
The DevTools overlay embeds three fonts, each licensed under the SIL Open Font
|
|
2
|
+
License, Version 1.1 (below). They are unmodified latin subsets of the variable
|
|
3
|
+
fonts served by Google Fonts.
|
|
4
|
+
|
|
5
|
+
Inter: Copyright (c) 2016 The Inter Project Authors (https://github.com/rsms/inter)
|
|
6
|
+
Space Grotesk: Copyright 2020 The Space Grotesk Project Authors (https://github.com/floriankarsten/space-grotesk)
|
|
7
|
+
JetBrains Mono: Copyright 2020 The JetBrains Mono Project Authors (https://github.com/JetBrains/JetBrainsMono)
|
|
8
|
+
|
|
9
|
+
-----------------------------------------------------------
|
|
10
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
11
|
+
-----------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
PREAMBLE
|
|
14
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
15
|
+
development of collaborative font projects, to support the font creation
|
|
16
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
17
|
+
open framework in which fonts may be shared and improved in partnership
|
|
18
|
+
with others.
|
|
19
|
+
|
|
20
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
21
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
22
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
23
|
+
redistributed and/or sold with any software provided that any reserved
|
|
24
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
25
|
+
however, cannot be released under any other type of license. The
|
|
26
|
+
requirement for fonts to remain under this license does not apply
|
|
27
|
+
to any document created using the fonts or their derivatives.
|
|
28
|
+
|
|
29
|
+
DEFINITIONS
|
|
30
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
31
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
32
|
+
include source files, build scripts and documentation.
|
|
33
|
+
|
|
34
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
35
|
+
copyright statement(s).
|
|
36
|
+
|
|
37
|
+
"Original Version" refers to the collection of Font Software components as
|
|
38
|
+
distributed by the Copyright Holder(s).
|
|
39
|
+
|
|
40
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
41
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
42
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
43
|
+
new environment.
|
|
44
|
+
|
|
45
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
46
|
+
writer or other person who contributed to the Font Software.
|
|
47
|
+
|
|
48
|
+
PERMISSION AND CONDITIONS
|
|
49
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
50
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
51
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
52
|
+
Software, subject to the following conditions:
|
|
53
|
+
|
|
54
|
+
1) Neither the Font Software nor any of its individual components,
|
|
55
|
+
in Original or Modified Versions, may be sold by itself.
|
|
56
|
+
|
|
57
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
58
|
+
redistributed and/or sold with any software, provided that each copy
|
|
59
|
+
contains the above copyright notice and this license. These can be
|
|
60
|
+
included either as stand-alone text files, human-readable headers or
|
|
61
|
+
in the appropriate machine-readable metadata fields within text or
|
|
62
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
63
|
+
|
|
64
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
65
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
66
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
67
|
+
presented to the users.
|
|
68
|
+
|
|
69
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
70
|
+
Software shall not be used to promote, endorse or advertise any
|
|
71
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
72
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
73
|
+
permission.
|
|
74
|
+
|
|
75
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
76
|
+
must be distributed entirely under this license, and must not be
|
|
77
|
+
distributed under any other license. The requirement for fonts to
|
|
78
|
+
remain under this license does not apply to any document created
|
|
79
|
+
using the Font Software.
|
|
80
|
+
|
|
81
|
+
TERMINATION
|
|
82
|
+
This license becomes null and void if any of the above conditions are
|
|
83
|
+
not met.
|
|
84
|
+
|
|
85
|
+
DISCLAIMER
|
|
86
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
87
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
88
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
89
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
90
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
91
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
92
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
93
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
94
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|