@uxfront/site-checks 0.0.0 → 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.
@@ -0,0 +1,188 @@
1
+ // Checks a config's values, so a typo in one fails the run instead of turning a check off.
2
+
3
+ /** @typedef {(value: unknown, path: string) => void} Validator */
4
+
5
+ /** @param {unknown} value */
6
+ const show = (value) => {
7
+ try {
8
+ return JSON.stringify(value) ?? String(value);
9
+ } catch {
10
+ return String(value);
11
+ }
12
+ };
13
+
14
+ /** @param {string} path @param {string} expected @param {unknown} value */
15
+ const fail = (path, expected, value) => {
16
+ throw new TypeError(`${path} must be ${expected}, not ${show(value)}`);
17
+ };
18
+
19
+ /** @param {unknown} value @returns {value is Record<string, unknown>} */
20
+ export const isPlainObject = (value) =>
21
+ Boolean(value) && typeof value === "object" && !Array.isArray(value);
22
+
23
+ /**
24
+ * One of these values.
25
+ * @param {...(string | boolean)} values
26
+ * @returns {Validator}
27
+ */
28
+ export const oneOf =
29
+ (...values) =>
30
+ (value, path) => {
31
+ if (!values.includes(/** @type {string | boolean} */ (value))) {
32
+ fail(path, `one of ${values.map(show).join(", ")}`, value);
33
+ }
34
+ };
35
+
36
+ /** @type {Validator} */
37
+ export const bool = (value, path) => {
38
+ if (typeof value !== "boolean") fail(path, "true or false", value);
39
+ };
40
+
41
+ /** @type {Validator} */
42
+ export const count = (value, path) => {
43
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
44
+ fail(path, "a number of 0 or more", value);
45
+ }
46
+ };
47
+
48
+ /** @type {Validator} */
49
+ export const text = (value, path) => {
50
+ if (typeof value !== "string" || !value) fail(path, "a string that isn't empty", value);
51
+ };
52
+
53
+ /**
54
+ * A list whose every item passes the validator.
55
+ * @param {Validator} item
56
+ * @param {{ nonEmpty?: boolean }} [options]
57
+ * @returns {Validator}
58
+ */
59
+ export const list =
60
+ (item, { nonEmpty = false } = {}) =>
61
+ (value, path) => {
62
+ if (!Array.isArray(value)) return fail(path, "a list", value);
63
+ if (nonEmpty && !value.length) fail(path, "a list that isn't empty", value);
64
+ value.forEach((entry, index) => item(entry, `${path}[${index}]`));
65
+ };
66
+
67
+ /** A site path, like `/docs/`, or a pattern, like `/docs/*` or `*`. @type {Validator} */
68
+ export const pattern = (value, path) => {
69
+ if (typeof value !== "string" || !(value.startsWith("/") || value === "*")) {
70
+ fail(path, 'a path that starts with "/", or "*"', value);
71
+ }
72
+ };
73
+
74
+ /** @type {Validator} */
75
+ export const url = (value, path) => {
76
+ try {
77
+ if (typeof value !== "string") throw new TypeError();
78
+ new URL(value);
79
+ } catch {
80
+ fail(path, "a URL", value);
81
+ }
82
+ };
83
+
84
+ /**
85
+ * An object with these keys, the required ones first.
86
+ * @param {Record<string, Validator>} required
87
+ * @param {Record<string, Validator>} [optional]
88
+ * @returns {Validator}
89
+ */
90
+ export const shape =
91
+ (required, optional = {}) =>
92
+ (value, path) => {
93
+ if (!isPlainObject(value)) return fail(path, "an object", value);
94
+ for (const [key, check] of Object.entries(required)) {
95
+ if (!(key in value)) throw new TypeError(`${path} needs ${key}`);
96
+ check(value[key], `${path}.${key}`);
97
+ }
98
+ for (const [key, entry] of Object.entries(value)) {
99
+ if (key in required) continue;
100
+ const check = optional[key];
101
+ if (!check) {
102
+ const known = [...Object.keys(required), ...Object.keys(optional)];
103
+ throw new TypeError(`${path} has no option "${key}". Its options are: ${known.join(", ")}`);
104
+ }
105
+ check(entry, `${path}.${key}`);
106
+ }
107
+ };
108
+
109
+ const SCOPES = ["head", "head-element", "document", "markup"];
110
+
111
+ /** A source of `sitemap-indexable` or `sitemap-coverage`. @type {Validator} */
112
+ export const source = (value, path) => {
113
+ if (!isPlainObject(value)) return fail(path, "an object with a type", value);
114
+ const types = {
115
+ meta: shape(
116
+ { type: text, names: list(text, { nonEmpty: true }) },
117
+ {
118
+ values: list(text, { nonEmpty: true }),
119
+ word: bool,
120
+ caseSensitive: bool,
121
+ property: bool,
122
+ anyCaseNames: bool,
123
+ scope: oneOf(...SCOPES),
124
+ },
125
+ ),
126
+ refresh: shape({ type: text }),
127
+ files: shape({ type: text, files: list(text) }),
128
+ under: shape({ type: text, paths: list(text) }),
129
+ redirects: shape({ type: text }),
130
+ headers: shape(
131
+ { type: text, hosts: oneOf("ignore", "match", "drop") },
132
+ { values: list(text, { nonEmpty: true }), word: bool },
133
+ ),
134
+ };
135
+ const check = types[/** @type {keyof typeof types} */ (String(value.type))];
136
+ if (!check)
137
+ return fail(`${path}.type`, `one of ${Object.keys(types).map(show).join(", ")}`, value.type);
138
+ check(value, path);
139
+ };
140
+
141
+ /** `json-ld-types`'s map of path patterns to `@type`s. @type {Validator} */
142
+ export const typesByPath = (value, path) => {
143
+ if (!isPlainObject(value))
144
+ return fail(path, 'an object of path patterns, like { "/docs/*": ["TechArticle"] }', value);
145
+ for (const [key, types] of Object.entries(value)) {
146
+ pattern(key, `${path} key`);
147
+ list(text)(types, `${path}["${key}"]`);
148
+ }
149
+ };
150
+
151
+ /** The parsing profile's settings. @type {Record<string, Validator>} */
152
+ export const PARSE = {
153
+ scope: oneOf(...SCOPES),
154
+ titleScope: oneOf(...SCOPES),
155
+ titleTag: oneOf("any", "plain-text", "bare"),
156
+ trimTitle: oneOf("after-decode", "before-decode", "none"),
157
+ quotes: oneOf("any", "double"),
158
+ caseSensitiveTags: bool,
159
+ tagEnd: oneOf("word", "space"),
160
+ metaKey: oneOf("name-or-property", "property-first"),
161
+ trimValues: bool,
162
+ decode: list((value, path) => {
163
+ if (
164
+ typeof value !== "string" ||
165
+ !(
166
+ ["numeric", "amp", "lt", "gt", "quot", "apos"].includes(value) ||
167
+ /^#(x[\da-f]+|\d+)$/i.test(value)
168
+ )
169
+ ) {
170
+ fail(
171
+ path,
172
+ 'an entity: "numeric", "amp", "lt", "gt", "quot", "apos" or a reference like "#x27"',
173
+ value,
174
+ );
175
+ }
176
+ }),
177
+ decodeOrder: oneOf("one-pass", "numeric-first"),
178
+ decodeIgnoreCase: bool,
179
+ count: oneOf("utf-16", "code-points"),
180
+ h1: oneOf("anywhere", "markup", "rendered"),
181
+ nodes: oneOf("graph-or-block", "block-and-graph", "all", "graph-deep"),
182
+ typeArrays: bool,
183
+ jsonLdType: oneOf("exact", "prefix-any-case"),
184
+ blankLines: oneOf("whitespace", "empty"),
185
+ trimSitemap: bool,
186
+ mergeDuplicateLocs: bool,
187
+ headersParser: oneOf("lines", "blocks"),
188
+ };
@@ -0,0 +1,43 @@
1
+ /** A Lighthouse category the preset asserts a score for. */
2
+ export type Category = "performance" | "accessibility" | "best-practices" | "seo";
3
+
4
+ /** How a category's score is asserted: `error` and a `minScore` of 0.95, or 1 for `seo`, unless set. */
5
+ export interface CategoryAssertion {
6
+ level?: "error" | "warn" | "off";
7
+ minScore?: number;
8
+ }
9
+
10
+ export interface LighthouseOptions {
11
+ /** Lighthouse CI's `collect` settings, like `staticDistDir` and `url`. `numberOfRuns` defaults to 3. */
12
+ collect: Record<string, unknown>;
13
+ /** Changes to a category's assertion, like `{ seo: { minScore: 0.95 } }`. */
14
+ categories?: Partial<Record<Category, CategoryAssertion>>;
15
+ /**
16
+ * Asserts each category only on the URLs that match a pattern, as Lighthouse CI's
17
+ * `assertMatrix`, like `{ matchingUrlPattern: "^http://localhost:8799/$", categories: ["performance"] }`.
18
+ */
19
+ matrix?: readonly { matchingUrlPattern: string; categories: readonly Category[] }[];
20
+ }
21
+
22
+ type Assertion = [level: "error" | "warn" | "off", options: { minScore: number }];
23
+
24
+ export interface LighthouseConfig {
25
+ ci: {
26
+ collect: Record<string, unknown>;
27
+ assert:
28
+ | { assertions: Partial<Record<`categories:${Category}`, Assertion>> }
29
+ | {
30
+ assertMatrix: {
31
+ matchingUrlPattern: string;
32
+ assertions: Partial<Record<`categories:${Category}`, Assertion>>;
33
+ }[];
34
+ };
35
+ upload: { target: "filesystem"; outputDir: ".lighthouseci" };
36
+ };
37
+ }
38
+
39
+ /** The four categories, in the order the preset asserts them. */
40
+ export declare const CATEGORIES: readonly Category[];
41
+
42
+ /** A full Lighthouse CI config: the shared thresholds, with the site's `collect` merged in. */
43
+ export declare function lighthouseConfig(options: LighthouseOptions): LighthouseConfig;
package/lighthouse.js ADDED
@@ -0,0 +1,84 @@
1
+ import { isPlainObject, list, oneOf, shape } from "./lib/validate.js";
2
+
3
+ /** @import { Category, CategoryAssertion, LighthouseConfig, LighthouseOptions } from "./lighthouse.js" */
4
+ /** @import { Validator } from "./lib/validate.js" */
5
+
6
+ /** @type {readonly Category[]} */
7
+ export const CATEGORIES = Object.freeze(["performance", "accessibility", "best-practices", "seo"]);
8
+
9
+ /** @type {Validator} */
10
+ const isObject = (value, path) => {
11
+ if (!isPlainObject(value)) throw new TypeError(`${path} must be an object of settings`);
12
+ };
13
+
14
+ /** @type {Validator} */
15
+ const minScore = (value, path) => {
16
+ if (typeof value !== "number" || !(value >= 0 && value <= 1)) {
17
+ throw new TypeError(`${path} must be a score from 0 to 1, not ${JSON.stringify(value)}`);
18
+ }
19
+ };
20
+
21
+ const category = oneOf(...CATEGORIES);
22
+
23
+ /** @type {Validator} */
24
+ const categoryChanges = (value, path) => {
25
+ isObject(value, path);
26
+ for (const [key, change] of Object.entries(/** @type {object} */ (value))) {
27
+ category(key, `${path} key`);
28
+ shape({}, { level: oneOf("error", "warn", "off"), minScore })(change, `${path}.${key}`);
29
+ }
30
+ };
31
+
32
+ const matrixEntries = list(
33
+ shape({
34
+ matchingUrlPattern: (value, path) => {
35
+ if (typeof value !== "string" || !value) throw new TypeError(`${path} must be a pattern`);
36
+ },
37
+ categories: list(category, { nonEmpty: true }),
38
+ }),
39
+ { nonEmpty: true },
40
+ );
41
+
42
+ // Every site errors below 0.95, and below 1 in SEO, unless it says otherwise.
43
+ /** @type {Readonly<Record<Category, Required<CategoryAssertion>>>} */
44
+ const BASE = {
45
+ performance: { level: "error", minScore: 0.95 },
46
+ accessibility: { level: "error", minScore: 0.95 },
47
+ "best-practices": { level: "error", minScore: 0.95 },
48
+ seo: { level: "error", minScore: 1 },
49
+ };
50
+
51
+ /**
52
+ * A full Lighthouse CI config: the shared thresholds, with the site's `collect` merged in.
53
+ * @param {LighthouseOptions} options
54
+ * @returns {LighthouseConfig}
55
+ */
56
+ export function lighthouseConfig(options) {
57
+ shape({ collect: isObject }, { categories: categoryChanges, matrix: matrixEntries })(
58
+ options,
59
+ "lighthouseConfig's options",
60
+ );
61
+ const { collect, categories = {}, matrix } = options;
62
+ /** @param {readonly Category[]} list */
63
+ const assertions = (list) =>
64
+ Object.fromEntries(
65
+ CATEGORIES.filter((category) => list.includes(category)).map((category) => {
66
+ const { level, minScore } = { ...BASE[category], ...categories[category] };
67
+ return [`categories:${category}`, [level, { minScore }]];
68
+ }),
69
+ );
70
+ return {
71
+ ci: {
72
+ collect: { numberOfRuns: 3, ...collect },
73
+ assert: matrix
74
+ ? {
75
+ assertMatrix: matrix.map(({ matchingUrlPattern, categories: list }) => ({
76
+ matchingUrlPattern,
77
+ assertions: assertions(list),
78
+ })),
79
+ }
80
+ : { assertions: assertions(CATEGORIES) },
81
+ upload: { target: "filesystem", outputDir: ".lighthouseci" },
82
+ },
83
+ };
84
+ }
package/package.json CHANGED
@@ -1,12 +1,56 @@
1
1
  {
2
2
  "name": "@uxfront/site-checks",
3
- "version": "0.0.0",
4
- "description": "Placeholder that reserves the name for trusted publishing. Install 0.1.0 or later.",
3
+ "version": "0.1.0",
4
+ "description": "The search and AI search checks and the Lighthouse CI preset shared by the uxfront.com family of sites.",
5
+ "keywords": [
6
+ "lighthouse",
7
+ "llms-txt",
8
+ "seo",
9
+ "sitemap"
10
+ ],
11
+ "homepage": "https://uxfront.com",
12
+ "bugs": "https://github.com/uxfront-com/uxfront/issues",
5
13
  "license": "MIT",
6
14
  "repository": {
7
15
  "type": "git",
8
16
  "url": "git+https://github.com/uxfront-com/uxfront.git",
9
17
  "directory": "packages/site-checks"
10
18
  },
11
- "publishConfig": { "access": "public" }
12
- }
19
+ "bin": {
20
+ "site-checks": "./bin/site-checks.js"
21
+ },
22
+ "files": [
23
+ "bin",
24
+ "lib",
25
+ "*.js",
26
+ "*.d.ts"
27
+ ],
28
+ "type": "module",
29
+ "sideEffects": false,
30
+ "exports": {
31
+ ".": {
32
+ "types": "./index.d.ts",
33
+ "default": "./index.js"
34
+ },
35
+ "./lighthouse": {
36
+ "types": "./lighthouse.d.ts",
37
+ "default": "./lighthouse.js"
38
+ },
39
+ "./package.json": "./package.json"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "devDependencies": {
45
+ "@types/node": "^26.4.1",
46
+ "@uxfront/typescript-config": "0.2.0",
47
+ "vitest": "^5.0.1"
48
+ },
49
+ "engines": {
50
+ "node": ">=24"
51
+ },
52
+ "scripts": {
53
+ "check-types": "tsc --noEmit",
54
+ "test": "vitest run"
55
+ }
56
+ }