@noisybrood/cli 0.0.0-stage → 0.1.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/README.md +74 -2
- package/dist/brood/api/src/fields.js +335 -0
- package/dist/brood/api/src/index.js +528 -0
- package/dist/brood/api/src/values.js +59 -0
- package/dist/brood/figures/index.js +10 -0
- package/dist/brood/tokens/dark.css +86 -0
- package/dist/brood/tokens/src/contrast.js +109 -0
- package/dist/brood/tokens/src/wren.js +58 -0
- package/dist/brood.js +11 -0
- package/dist/client.js +100 -0
- package/dist/commands.js +417 -0
- package/dist/io.js +50 -0
- package/dist/main.js +193 -0
- package/dist/out.js +53 -0
- package/dist/sign-in.js +129 -0
- package/dist/store.js +83 -0
- package/dist/version.js +16 -0
- package/dist/wren.js +153 -0
- package/package.json +37 -4
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading the colour tokens, and WCAG's contrast arithmetic over them (#608).
|
|
3
|
+
*
|
|
4
|
+
* One copy, for the brandkit, which shows each pair's grade, and for the test
|
|
5
|
+
* beside this file, which holds the pairs used as text to WCAG 2.2 AA. The
|
|
6
|
+
* CSS is passed in: the brandkit inlines it at build time, the test reads the
|
|
7
|
+
* files.
|
|
8
|
+
*/
|
|
9
|
+
export function readColourTokens(css) {
|
|
10
|
+
const out = [];
|
|
11
|
+
let note;
|
|
12
|
+
for (const raw of css.split("\n")) {
|
|
13
|
+
const line = raw.trim();
|
|
14
|
+
const comment = line.match(/^\/\*\s*(.*?)\s*\*\/$/);
|
|
15
|
+
if (comment) {
|
|
16
|
+
note = comment[1];
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
// Both groups are guaranteed by the match, but `noUncheckedIndexedAccess`
|
|
20
|
+
// cannot know that — and reading them through the guard costs nothing.
|
|
21
|
+
const decl = line.match(/^--color-([a-z0-9-]+)\s*:\s*([^;]+);/i);
|
|
22
|
+
if (decl?.[1] && decl[2]) {
|
|
23
|
+
const value = decl[2].trim();
|
|
24
|
+
out.push({
|
|
25
|
+
name: decl[1],
|
|
26
|
+
value,
|
|
27
|
+
alpha: /rgb|hsl|\//.test(value),
|
|
28
|
+
note,
|
|
29
|
+
});
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
// A blank line ends a group, so a note does not leak onto the next one.
|
|
33
|
+
if (line === "")
|
|
34
|
+
note = undefined;
|
|
35
|
+
}
|
|
36
|
+
if (!out.length)
|
|
37
|
+
throw new Error("No --color-* tokens found");
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
/** Relative luminance, per WCAG. */
|
|
41
|
+
function luminance(hex) {
|
|
42
|
+
const m = hex.match(/^#([0-9a-f]{6})$/i);
|
|
43
|
+
const digits = m?.[1];
|
|
44
|
+
if (!digits)
|
|
45
|
+
return null;
|
|
46
|
+
// Read each channel by name rather than destructuring out of `.map`, which
|
|
47
|
+
// hands back `number | undefined` for every element however many there are.
|
|
48
|
+
const channel = (i) => parseInt(digits.slice(i, i + 2), 16) / 255;
|
|
49
|
+
const f = (c) => c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
50
|
+
return (0.2126 * f(channel(0)) + 0.7152 * f(channel(2)) + 0.0722 * f(channel(4)));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The contrast ratio exactly, or null when either colour carries alpha and
|
|
54
|
+
* cannot be judged alone. For grading: WCAG's thresholds are not to be met by
|
|
55
|
+
* rounding up.
|
|
56
|
+
*/
|
|
57
|
+
export function exactContrast(a, b) {
|
|
58
|
+
const la = luminance(a);
|
|
59
|
+
const lb = luminance(b);
|
|
60
|
+
if (la === null || lb === null)
|
|
61
|
+
return null;
|
|
62
|
+
const [hi, lo] = la > lb ? [la, lb] : [lb, la];
|
|
63
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
64
|
+
}
|
|
65
|
+
/** Contrast ratio to two places, for a caption, or null as `exactContrast`. */
|
|
66
|
+
export function contrast(a, b) {
|
|
67
|
+
const r = exactContrast(a, b);
|
|
68
|
+
return r === null ? null : Math.round(r * 100) / 100;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A translucent `rgb(r g b / a)` laid over an opaque ground, as the hex a
|
|
72
|
+
* reader actually sees. A contrast figure for a translucent colour is only true
|
|
73
|
+
* against one backdrop, so this takes the backdrop explicitly rather than
|
|
74
|
+
* guessing it. Hexes pass through unchanged; anything else returns null.
|
|
75
|
+
*/
|
|
76
|
+
export function over(value, ground) {
|
|
77
|
+
if (/^#[0-9a-f]{6}$/i.test(value))
|
|
78
|
+
return value;
|
|
79
|
+
const m = value.match(/^rgb\(\s*(\d+)\s+(\d+)\s+(\d+)\s*\/\s*([\d.]+)\s*\)$/i);
|
|
80
|
+
const g = ground.match(/^#([0-9a-f]{6})$/i)?.[1];
|
|
81
|
+
if (!m || !g)
|
|
82
|
+
return null;
|
|
83
|
+
const alpha = Number(m[4]);
|
|
84
|
+
const mix = (i) => {
|
|
85
|
+
const fg = Number(m[i + 1]);
|
|
86
|
+
const bg = parseInt(g.slice(i * 2, i * 2 + 2), 16);
|
|
87
|
+
return Math.round(fg * alpha + bg * (1 - alpha))
|
|
88
|
+
.toString(16)
|
|
89
|
+
.padStart(2, "0");
|
|
90
|
+
};
|
|
91
|
+
return `#${mix(0)}${mix(1)}${mix(2)}`;
|
|
92
|
+
}
|
|
93
|
+
/** Contrast of a colour, translucent or not, as it lands on `ground`. */
|
|
94
|
+
export function contrastOn(value, ground) {
|
|
95
|
+
const seen = over(value, ground);
|
|
96
|
+
return seen === null ? null : contrast(seen, ground);
|
|
97
|
+
}
|
|
98
|
+
/** WCAG grade for body text at normal size. */
|
|
99
|
+
export function grade(ratio) {
|
|
100
|
+
if (ratio === null)
|
|
101
|
+
return "—";
|
|
102
|
+
if (ratio >= 7)
|
|
103
|
+
return "AAA";
|
|
104
|
+
if (ratio >= 4.5)
|
|
105
|
+
return "AA";
|
|
106
|
+
if (ratio >= 3)
|
|
107
|
+
return "AA large";
|
|
108
|
+
return "fails";
|
|
109
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wren, as coordinates: the mark's one source, beside the colours it is
|
|
3
|
+
* inked in, so the sites' component and the app's draw the same bird (DD13 §6:
|
|
4
|
+
* the app shares tokens, never components).
|
|
5
|
+
*
|
|
6
|
+
* Drawn on a 100×100 field, facing right, tail cocked — the posture is the
|
|
7
|
+
* diagnostic thing about a wren and the silhouette has to carry it at 24px,
|
|
8
|
+
* where nothing else survives.
|
|
9
|
+
*/
|
|
10
|
+
/** The closed outline: beak, crown, back, tail, belly, breast. */
|
|
11
|
+
export const BODY = `M94 43 L76 40
|
|
12
|
+
C71 25 54 20 44 31 C39 36 36 42 34 48
|
|
13
|
+
L12 20 L9 30 L3 26 L27 64
|
|
14
|
+
C29 78 44 85 58 81 C72 77 80 62 78 48 Z`;
|
|
15
|
+
/** The eye, punched through the body as a hole. Never a second colour. */
|
|
16
|
+
export const EYE = { cx: 68, cy: 35, r: 3.2 };
|
|
17
|
+
/** The folded wing, as an open curve punched through the body. */
|
|
18
|
+
export const WING = `M42 52 C52 61 60 67 66 67`;
|
|
19
|
+
/**
|
|
20
|
+
* The weight of the wing and the eye's detail strokes, in user units on the
|
|
21
|
+
* 100-unit field, so it is a fixed share of the mark at any size: the wing
|
|
22
|
+
* holds its weight at 22px in the app's rail and at 72px on a title slide
|
|
23
|
+
* alike. One number for every renderer, so no two draw a different bird.
|
|
24
|
+
*/
|
|
25
|
+
export const DETAIL_WEIGHT = 3.2;
|
|
26
|
+
/** The field everything above is drawn on. */
|
|
27
|
+
export const VIEWBOX = "0 0 100 100";
|
|
28
|
+
/** A path on one line, as an SVG file carries it. */
|
|
29
|
+
const oneLine = (d) => d.replace(/\s+/g, " ").trim();
|
|
30
|
+
/**
|
|
31
|
+
* The mark as a file of its own: the body, with the eye and the wing
|
|
32
|
+
* punched through to whatever is behind, so it needs no ground. `paint`
|
|
33
|
+
* inks the body: a colour, or a stylesheet setting `.wren`'s fill, as the
|
|
34
|
+
* favicon's does to follow the system's scheme. `comment` heads the file.
|
|
35
|
+
* `ground`, when given, sets the bird on a square of that colour with
|
|
36
|
+
* `margin` units of room on each side, for a place that shows an icon on a
|
|
37
|
+
* ground it chooses, such as a dark screen.
|
|
38
|
+
*/
|
|
39
|
+
export function wrenSvg(paint, comment, ground) {
|
|
40
|
+
const style = "style" in paint ? `\n <style>\n${paint.style}\n </style>` : "";
|
|
41
|
+
const ink = "fill" in paint ? `fill="${paint.fill}"` : `class="wren"`;
|
|
42
|
+
const m = ground?.margin ?? 0;
|
|
43
|
+
const field = `x="${-m}" y="${-m}" width="${100 + 2 * m}" height="${100 + 2 * m}"`;
|
|
44
|
+
const viewBox = ground
|
|
45
|
+
? `${-m} ${-m} ${100 + 2 * m} ${100 + 2 * m}`
|
|
46
|
+
: VIEWBOX;
|
|
47
|
+
const square = ground ? `\n <rect ${field} fill="${ground.fill}"/>` : "";
|
|
48
|
+
return `<!-- ${comment} -->
|
|
49
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="${viewBox}">${style}${square}
|
|
50
|
+
<mask id="wren">
|
|
51
|
+
<rect ${ground ? field : 'width="100" height="100"'} fill="white"/>
|
|
52
|
+
<circle cx="${EYE.cx}" cy="${EYE.cy}" r="${EYE.r}" fill="black"/>
|
|
53
|
+
<path d="${oneLine(WING)}" fill="none" stroke="black" stroke-width="${DETAIL_WEIGHT}" stroke-linecap="round"/>
|
|
54
|
+
</mask>
|
|
55
|
+
<path ${ink} d="${oneLine(BODY)}" mask="url(#wren)"/>
|
|
56
|
+
</svg>
|
|
57
|
+
`;
|
|
58
|
+
}
|
package/dist/brood.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { run } from "./main.js";
|
|
3
|
+
process.exitCode = await run({
|
|
4
|
+
fetch: (url, init) => fetch(url, init),
|
|
5
|
+
env: process.env,
|
|
6
|
+
now: () => Date.now(),
|
|
7
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
8
|
+
out: (line) => process.stdout.write(`${line}\n`),
|
|
9
|
+
err: (line) => process.stderr.write(`${line}\n`),
|
|
10
|
+
tty: process.stdout.isTTY === true,
|
|
11
|
+
}, process.argv.slice(2));
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { API_BASE, PROBLEM_CODES, PROBLEMS, } from "./brood/api/src/index.js";
|
|
2
|
+
import { clean, cleaned, isRecord, Refused, Refusal } from "./io.js";
|
|
3
|
+
import { freshCredentials, refresh } from "./sign-in.js";
|
|
4
|
+
import { VERSION } from "./version.js";
|
|
5
|
+
const isProblem = (v) => isRecord(v) &&
|
|
6
|
+
typeof v.status === "number" &&
|
|
7
|
+
typeof v.title === "string" &&
|
|
8
|
+
PROBLEM_CODES.some((c) => c === v.code);
|
|
9
|
+
const isEnvelope = (v) => isRecord(v) &&
|
|
10
|
+
isRecord(v.data) &&
|
|
11
|
+
Array.isArray(v.accounts) &&
|
|
12
|
+
v.accounts.every((a) => isRecord(a) &&
|
|
13
|
+
typeof a.id === "string" &&
|
|
14
|
+
isRecord(a.sync) &&
|
|
15
|
+
typeof a.sync.phase === "string" &&
|
|
16
|
+
typeof a.whole === "boolean") &&
|
|
17
|
+
typeof v.complete === "boolean" &&
|
|
18
|
+
(v.next === undefined || typeof v.next === "string");
|
|
19
|
+
/** A problem as the person reads it: what happened, and what to do. */
|
|
20
|
+
function refusalOf(problem, retryAfter) {
|
|
21
|
+
switch (problem.code) {
|
|
22
|
+
case "credential_revoked":
|
|
23
|
+
return new Refusal("brood was revoked on your account page, or went unused too long. Allow it again there to carry on.");
|
|
24
|
+
case "insufficient_scope":
|
|
25
|
+
return new Refusal("This session may only read. Run brood logout, then brood login without --read-only.");
|
|
26
|
+
case "rate_limited":
|
|
27
|
+
// The day's rule changes have a reason the server gives; the minute's
|
|
28
|
+
// limit has only its wait.
|
|
29
|
+
return new Refusal(problem.detail
|
|
30
|
+
? `${PROBLEMS.rate_limited.title}. ${problem.detail}`
|
|
31
|
+
: retryAfter
|
|
32
|
+
? `${PROBLEMS.rate_limited.title}. Try again in ${clean(retryAfter)} seconds.`
|
|
33
|
+
: `${PROBLEMS.rate_limited.title}. Try again in a minute.`);
|
|
34
|
+
case "invalid_argument":
|
|
35
|
+
return new Refusal(`${PROBLEMS.invalid_argument.title}: ${problem.detail ?? "the request"}.`);
|
|
36
|
+
default:
|
|
37
|
+
return new Refusal(problem.detail
|
|
38
|
+
? `${PROBLEMS[problem.code].title}. ${problem.detail}`
|
|
39
|
+
: `${PROBLEMS[problem.code].title}.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function send(io, operation, args, credentials) {
|
|
43
|
+
let response;
|
|
44
|
+
try {
|
|
45
|
+
response = await io.fetch(`${API_BASE}/${operation}`, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: {
|
|
48
|
+
authorization: `Bearer ${credentials.access_token}`,
|
|
49
|
+
"content-type": "application/json",
|
|
50
|
+
// DD21 §11: so the gate can refuse a version known to be broken.
|
|
51
|
+
"user-agent": `brood-cli/${VERSION}`,
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify(args),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new Refusal("Could not reach Brood. Check the connection and try again.");
|
|
58
|
+
}
|
|
59
|
+
let body;
|
|
60
|
+
try {
|
|
61
|
+
// Cleaned as it arrives: a sender writes its own address, name and
|
|
62
|
+
// domain, so no printer can be the one that forgot (DD21 §8).
|
|
63
|
+
body = cleaned(await response.json());
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
body = null;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
status: response.status,
|
|
70
|
+
body,
|
|
71
|
+
retryAfter: response.headers.get("retry-after"),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Calls one operation as the signed-in person. A token Brood refuses as
|
|
76
|
+
* unauthenticated is refreshed once and the call made again, since an
|
|
77
|
+
* access token can lapse between the check and the call.
|
|
78
|
+
*/
|
|
79
|
+
export async function call(io, operation, args) {
|
|
80
|
+
// Arguments may be read from the session, as logging out's refresh token
|
|
81
|
+
// is: after a refresh, it must be the new one that is ended.
|
|
82
|
+
const of = (c) => (typeof args === "function" ? args(c) : args);
|
|
83
|
+
let held = await freshCredentials(io);
|
|
84
|
+
let answer = await send(io, operation, of(held), held);
|
|
85
|
+
if (answer.status === 401 &&
|
|
86
|
+
isProblem(answer.body) &&
|
|
87
|
+
answer.body.code === "unauthenticated") {
|
|
88
|
+
held = await refresh(io, held);
|
|
89
|
+
answer = await send(io, operation, of(held), held);
|
|
90
|
+
}
|
|
91
|
+
if (isProblem(answer.body))
|
|
92
|
+
throw new Refused(refusalOf(answer.body, answer.retryAfter).message, answer.body.code);
|
|
93
|
+
if (answer.status !== 200 || !isEnvelope(answer.body))
|
|
94
|
+
throw new Refusal("Brood's answer could not be read. Try again shortly.");
|
|
95
|
+
// The envelope's shape is checked above, and its data taken as the
|
|
96
|
+
// operation's answer as packages/api types it: the app is built against the
|
|
97
|
+
// same types, and v1 grows only by addition (DD21 §11), so a field this
|
|
98
|
+
// client reads is one the API still sends.
|
|
99
|
+
return answer.body;
|
|
100
|
+
}
|