sugikit 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 +29 -0
- package/README.md +44 -2
- package/dist/api/browser.d.ts +5 -0
- package/dist/api/browser.js +5 -0
- package/dist/api/rules.d.ts +12 -0
- package/dist/api/rules.js +14 -0
- package/dist/auth.d.ts +156 -0
- package/dist/auth.js +547 -0
- package/dist/browser.d.ts +25 -0
- package/dist/browser.js +120 -0
- package/dist/check.d.ts +21 -0
- package/dist/check.js +89 -0
- package/dist/convert-probe.d.ts +21 -0
- package/dist/convert-probe.js +647 -0
- package/dist/fetch.d.ts +6 -0
- package/dist/fetch.js +22 -0
- package/dist/framework.d.ts +2 -0
- package/dist/framework.js +17 -0
- package/dist/gather.d.ts +48 -0
- package/dist/gather.js +211 -0
- package/dist/html.d.ts +17 -0
- package/dist/html.js +43 -0
- package/dist/http.d.ts +36 -0
- package/dist/http.js +57 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +331 -0
- package/dist/links.d.ts +8 -0
- package/dist/links.js +69 -0
- package/dist/polish-run.d.ts +115 -0
- package/dist/polish-run.js +357 -0
- package/dist/probe.d.ts +14 -0
- package/dist/probe.js +457 -0
- package/dist/psi-scores.d.ts +4 -0
- package/dist/psi-scores.js +7 -0
- package/dist/psi.d.ts +56 -0
- package/dist/psi.js +192 -0
- package/dist/release.d.ts +1 -0
- package/dist/release.js +5 -0
- package/dist/render.d.ts +28 -0
- package/dist/render.js +270 -0
- package/dist/report.d.ts +227 -0
- package/dist/report.js +69 -0
- package/dist/rules/convert-render.d.ts +100 -0
- package/dist/rules/convert-render.js +279 -0
- package/dist/rules/convert.d.ts +15 -0
- package/dist/rules/convert.js +322 -0
- package/dist/rules/fixes.d.ts +16 -0
- package/dist/rules/fixes.js +536 -0
- package/dist/rules/geo.d.ts +15 -0
- package/dist/rules/geo.js +112 -0
- package/dist/rules/polish.d.ts +59 -0
- package/dist/rules/polish.js +223 -0
- package/dist/rules/robots.d.ts +14 -0
- package/dist/rules/robots.js +69 -0
- package/dist/rules/seo.d.ts +31 -0
- package/dist/rules/seo.js +208 -0
- package/dist/rules/types.d.ts +55 -0
- package/dist/rules/types.js +78 -0
- package/dist/score.d.ts +6 -0
- package/dist/score.js +23 -0
- package/dist/skills.d.ts +9 -0
- package/dist/skills.js +22 -0
- package/package.json +63 -3
- package/skills/INSTALL_PROMPT.md +9 -0
- package/skills/sugikit/SKILL.md +147 -0
- package/skills/sugikit/references/convert.md +127 -0
- package/skills/sugikit/references/geo.md +97 -0
- package/skills/sugikit/references/next.md +254 -0
- package/skills/sugikit/references/pagespeed.md +94 -0
- package/skills/sugikit/references/polish.md +123 -0
- package/skills/sugikit/references/seo.md +150 -0
- package/skills/sugikit/references/tanstack-start.md +246 -0
package/dist/auth.js
ADDED
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
// Sugikit account login (RFC 8628 device code) and the license check that paid commands use.
|
|
2
|
+
// The access token lives only in auth.json (mode 0600) and in the Authorization header. It is never printed.
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { chmodSync, closeSync, constants as fsConstants, fchmodSync, fstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
5
|
+
import { lookup as dnsLookup } from "node:dns/promises";
|
|
6
|
+
import { isIP } from "node:net";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { isAbsolute, join } from "node:path";
|
|
9
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
10
|
+
import { Agent, buildConnector, fetch as undiciFetch } from "undici";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { RELEASE_DATE } from "./release.js";
|
|
13
|
+
export const DEFAULT_API_URL = "https://sugikit.com";
|
|
14
|
+
export const CLIENT_ID = "sugikit-cli";
|
|
15
|
+
export const DEVICE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
|
|
16
|
+
export const REQUEST_TIMEOUT_MS = 15_000;
|
|
17
|
+
// A cached license this young is used without a network call.
|
|
18
|
+
export const LICENSE_FRESH_MS = 24 * 60 * 60 * 1000;
|
|
19
|
+
// After this long without a successful check, paid commands stop (no fail-open).
|
|
20
|
+
export const LICENSE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
21
|
+
export const PRICING_URL = "https://sugikit.com/#pricing";
|
|
22
|
+
export const ACCOUNT_URL = "https://sugikit.com/account";
|
|
23
|
+
export class AuthError extends Error {
|
|
24
|
+
name = "AuthError";
|
|
25
|
+
}
|
|
26
|
+
export class NotLoggedInError extends AuthError {
|
|
27
|
+
name = "NotLoggedInError";
|
|
28
|
+
}
|
|
29
|
+
// The server answered 401: the token is invalid, expired or revoked.
|
|
30
|
+
export class SessionExpiredError extends AuthError {
|
|
31
|
+
name = "SessionExpiredError";
|
|
32
|
+
}
|
|
33
|
+
export function defaultContext(overrides = {}) {
|
|
34
|
+
return {
|
|
35
|
+
env: process.env,
|
|
36
|
+
platform: process.platform,
|
|
37
|
+
homedir: homedir(),
|
|
38
|
+
now: () => Date.now(),
|
|
39
|
+
sleep: (ms) => delay(ms),
|
|
40
|
+
open: (url) => openInBrowser(url, process.platform),
|
|
41
|
+
stdout: (text) => process.stdout.write(text),
|
|
42
|
+
stderr: (text) => process.stderr.write(text),
|
|
43
|
+
...overrides,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Characters a URL may hold. Anything else is not passed to a shell opener.
|
|
47
|
+
const SAFE_URL = /^https?:\/\/[A-Za-z0-9\-._~:/?#[\]@!$&'()*+,;=%]+$/;
|
|
48
|
+
export function openInBrowser(url, platform) {
|
|
49
|
+
if (!SAFE_URL.test(url))
|
|
50
|
+
return;
|
|
51
|
+
const [command, args] = platform === "darwin"
|
|
52
|
+
? ["open", [url]]
|
|
53
|
+
: platform === "win32"
|
|
54
|
+
? ["cmd", ["/c", "start", '""', url.replace(/[&^|<>()]/g, "^$&")]]
|
|
55
|
+
: ["xdg-open", [url]];
|
|
56
|
+
try {
|
|
57
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true, windowsVerbatimArguments: platform === "win32" });
|
|
58
|
+
child.on("error", () => { });
|
|
59
|
+
child.unref();
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// The URL is printed too, so a missing opener is not an error.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// ---------- paths and files ----------
|
|
66
|
+
export function configDir(ctx) {
|
|
67
|
+
if (ctx.platform === "win32") {
|
|
68
|
+
return join(ctx.env.APPDATA || join(ctx.homedir, "AppData", "Roaming"), "sugikit");
|
|
69
|
+
}
|
|
70
|
+
const xdg = ctx.env.XDG_CONFIG_HOME;
|
|
71
|
+
return join(xdg && isAbsolute(xdg) ? xdg : join(ctx.homedir, ".config"), "sugikit");
|
|
72
|
+
}
|
|
73
|
+
export const authPath = (ctx) => join(configDir(ctx), "auth.json");
|
|
74
|
+
export const licensePath = (ctx) => join(configDir(ctx), "license.json");
|
|
75
|
+
const authFileSchema = z.object({
|
|
76
|
+
access_token: z.string().min(1),
|
|
77
|
+
api_url: z.string().min(1),
|
|
78
|
+
saved_at: z.string(),
|
|
79
|
+
});
|
|
80
|
+
const planSchema = z.enum(["none", "kit", "pro", "cloud"]);
|
|
81
|
+
const entitlementsSchema = z.object({ kit: z.boolean(), pro: z.boolean(), cloud: z.boolean() });
|
|
82
|
+
const isoDate = z.string().refine((value) => !Number.isNaN(Date.parse(value)), "not a date");
|
|
83
|
+
// v1 bodies have only plan, email, checked_at. v2 adds entitlements, updates_until and cloud_until.
|
|
84
|
+
const licenseResponseSchema = z
|
|
85
|
+
.object({
|
|
86
|
+
plan: planSchema,
|
|
87
|
+
email: z.string(),
|
|
88
|
+
checked_at: z.string(),
|
|
89
|
+
entitlements: entitlementsSchema.optional(),
|
|
90
|
+
updates_until: isoDate.nullable().optional(),
|
|
91
|
+
cloud_until: isoDate.nullable().optional(),
|
|
92
|
+
})
|
|
93
|
+
.transform((body) => ({
|
|
94
|
+
plan: body.plan,
|
|
95
|
+
email: body.email,
|
|
96
|
+
checked_at: body.checked_at,
|
|
97
|
+
entitlements: body.entitlements ?? {
|
|
98
|
+
kit: body.plan === "kit" || body.plan === "pro" || body.plan === "cloud",
|
|
99
|
+
pro: body.plan === "pro" || body.plan === "cloud",
|
|
100
|
+
cloud: body.plan === "cloud",
|
|
101
|
+
},
|
|
102
|
+
updates_until: body.updates_until ?? null,
|
|
103
|
+
cloud_until: body.cloud_until ?? null,
|
|
104
|
+
}));
|
|
105
|
+
const licenseFileSchema = z
|
|
106
|
+
.object({
|
|
107
|
+
api_url: z.string(),
|
|
108
|
+
// Local clock time of the successful check, in ms. The 7-day window counts from here.
|
|
109
|
+
cached_at: z.number(),
|
|
110
|
+
})
|
|
111
|
+
.and(licenseResponseSchema);
|
|
112
|
+
function writeSecretFile(ctx, path, data) {
|
|
113
|
+
const dir = configDir(ctx);
|
|
114
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
115
|
+
const posix = ctx.platform !== "win32";
|
|
116
|
+
if (posix)
|
|
117
|
+
chmodSync(dir, 0o700);
|
|
118
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
119
|
+
try {
|
|
120
|
+
writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
|
|
121
|
+
if (posix)
|
|
122
|
+
chmodSync(tmp, 0o600);
|
|
123
|
+
renameSync(tmp, path);
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
rmSync(tmp, { force: true });
|
|
127
|
+
}
|
|
128
|
+
if (posix) {
|
|
129
|
+
const mode = statSync(path).mode & 0o777;
|
|
130
|
+
if (mode !== 0o600) {
|
|
131
|
+
rmSync(path, { force: true });
|
|
132
|
+
throw new AuthError(`could not set mode 0600 on ${path} (got ${mode.toString(8)}); the file was removed`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Best effort: the config dir goes back to 0700 when it is looser.
|
|
137
|
+
function tightenConfigDir(ctx) {
|
|
138
|
+
try {
|
|
139
|
+
const dir = configDir(ctx);
|
|
140
|
+
if (statSync(dir).mode & 0o077)
|
|
141
|
+
chmodSync(dir, 0o700);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
// The file itself is checked; the dir is best effort.
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Reads a secret file. On posix it refuses a symlink or a file another user owns, and tightens loose modes.
|
|
148
|
+
function readSecretText(ctx, path) {
|
|
149
|
+
if (ctx.platform === "win32") {
|
|
150
|
+
try {
|
|
151
|
+
return readFileSync(path, "utf8");
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let fd;
|
|
158
|
+
try {
|
|
159
|
+
// O_NOFOLLOW: a symlink fails here (ELOOP), so the check and the read see the same file.
|
|
160
|
+
fd = openSync(path, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const stat = fstatSync(fd);
|
|
167
|
+
const uid = ctx.uid ?? process.getuid?.();
|
|
168
|
+
if (!stat.isFile() || (uid !== undefined && stat.uid !== uid))
|
|
169
|
+
return undefined;
|
|
170
|
+
if (stat.mode & 0o077)
|
|
171
|
+
fchmodSync(fd, 0o600);
|
|
172
|
+
tightenConfigDir(ctx);
|
|
173
|
+
return readFileSync(fd, "utf8");
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
closeSync(fd);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function readJson(ctx, path, schema) {
|
|
183
|
+
const raw = readSecretText(ctx, path);
|
|
184
|
+
if (raw === undefined)
|
|
185
|
+
return undefined;
|
|
186
|
+
try {
|
|
187
|
+
const parsed = schema.safeParse(JSON.parse(raw));
|
|
188
|
+
return parsed.success ? parsed.data : undefined;
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export function readAuth(ctx) {
|
|
195
|
+
return readJson(ctx, authPath(ctx), authFileSchema);
|
|
196
|
+
}
|
|
197
|
+
function readLicenseCache(ctx) {
|
|
198
|
+
return readJson(ctx, licensePath(ctx), licenseFileSchema);
|
|
199
|
+
}
|
|
200
|
+
function dropLicenseCache(ctx) {
|
|
201
|
+
rmSync(licensePath(ctx), { force: true });
|
|
202
|
+
}
|
|
203
|
+
// ---------- API URL and requests ----------
|
|
204
|
+
export function apiUrl(env) {
|
|
205
|
+
return (env.SUGIKIT_API_URL?.trim() || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
206
|
+
}
|
|
207
|
+
// Hosts that may get credentials over plain http: localhost, any 127/8 literal and [::1].
|
|
208
|
+
// Other *.localhost names and 0.0.0.0 are refused: a resolver can point them at a LAN address.
|
|
209
|
+
function isHttpLoopbackHost(hostname) {
|
|
210
|
+
if (hostname === "localhost" || hostname === "[::1]")
|
|
211
|
+
return true;
|
|
212
|
+
return isIP(hostname) === 4 && hostname.startsWith("127.");
|
|
213
|
+
}
|
|
214
|
+
// Credentials (device code and token) go only to https, or to http on loopback for a local dev mock.
|
|
215
|
+
export function assertSafeApiUrl(url) {
|
|
216
|
+
let parsed;
|
|
217
|
+
try {
|
|
218
|
+
parsed = new URL(url);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
throw new AuthError(`SUGIKIT_API_URL is not a valid URL: ${url}`);
|
|
222
|
+
}
|
|
223
|
+
if (parsed.protocol === "https:")
|
|
224
|
+
return;
|
|
225
|
+
if (parsed.protocol === "http:" && isHttpLoopbackHost(parsed.hostname))
|
|
226
|
+
return;
|
|
227
|
+
throw new AuthError(`refusing to send credentials to ${parsed.origin}: the Sugikit API URL must use https (http only on localhost, 127.0.0.1 or [::1])`);
|
|
228
|
+
}
|
|
229
|
+
export function redact(text, secrets) {
|
|
230
|
+
let out = text;
|
|
231
|
+
for (const secret of secrets)
|
|
232
|
+
if (secret)
|
|
233
|
+
out = out.split(secret).join("[redacted]");
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
// Lax TLS (a dev proxy's private CA) is allowed only for loopback. A *.localhost name must resolve to loopback.
|
|
237
|
+
function isLoopbackAddress(address) {
|
|
238
|
+
const a = address.toLowerCase().replace(/^\[|\]$/g, "");
|
|
239
|
+
if (a === "::1")
|
|
240
|
+
return true;
|
|
241
|
+
const v4 = a.startsWith("::ffff:") ? a.slice(7) : a;
|
|
242
|
+
return isIP(v4) === 4 && v4.startsWith("127.");
|
|
243
|
+
}
|
|
244
|
+
const defaultLookup = (hostname) => dnsLookup(hostname, { all: true });
|
|
245
|
+
export async function laxTlsAllowed(hostname, lookup = defaultLookup) {
|
|
246
|
+
if (hostname === "localhost" || hostname === "127.0.0.1")
|
|
247
|
+
return true;
|
|
248
|
+
if (!hostname.endsWith(".localhost"))
|
|
249
|
+
return false;
|
|
250
|
+
try {
|
|
251
|
+
const addresses = await lookup(hostname);
|
|
252
|
+
return addresses.length > 0 && addresses.every((entry) => isLoopbackAddress(entry.address));
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// A connect lookup that only hands out loopback addresses, so a name cannot rebind to another host between check and connect.
|
|
259
|
+
const loopbackOnlyLookup = ((hostname, options, callback) => {
|
|
260
|
+
defaultLookup(hostname).then((all) => {
|
|
261
|
+
const safe = all.filter((entry) => isLoopbackAddress(entry.address));
|
|
262
|
+
if (safe.length === 0 || safe.length !== all.length) {
|
|
263
|
+
callback(new Error(`${hostname} does not resolve to loopback`), "", 0);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (options.all)
|
|
267
|
+
callback(null, safe);
|
|
268
|
+
else
|
|
269
|
+
callback(null, safe[0].address, safe[0].family);
|
|
270
|
+
}, (error) => callback(error, "", 0));
|
|
271
|
+
});
|
|
272
|
+
let apiLaxAgent;
|
|
273
|
+
// TLS checks off, and the connection itself may only reach loopback addresses.
|
|
274
|
+
function apiLaxDispatcher() {
|
|
275
|
+
apiLaxAgent ??= new Agent({
|
|
276
|
+
connect: buildConnector({ rejectUnauthorized: false, lookup: loopbackOnlyLookup }),
|
|
277
|
+
});
|
|
278
|
+
return apiLaxAgent;
|
|
279
|
+
}
|
|
280
|
+
let apiLoopbackAgent;
|
|
281
|
+
// Plain http to the name localhost: the connection may only reach loopback addresses.
|
|
282
|
+
function apiLoopbackDispatcher() {
|
|
283
|
+
apiLoopbackAgent ??= new Agent({ connect: buildConnector({ lookup: loopbackOnlyLookup }) });
|
|
284
|
+
return apiLoopbackAgent;
|
|
285
|
+
}
|
|
286
|
+
async function request(base, path, init) {
|
|
287
|
+
assertSafeApiUrl(base);
|
|
288
|
+
const url = `${base}${path}`;
|
|
289
|
+
const headers = { accept: "application/json", "user-agent": "sugikit-cli" };
|
|
290
|
+
if (init.body !== undefined)
|
|
291
|
+
headers["content-type"] = "application/json";
|
|
292
|
+
if (init.token)
|
|
293
|
+
headers.authorization = `Bearer ${init.token}`;
|
|
294
|
+
const { hostname, protocol } = new URL(url);
|
|
295
|
+
const lax = protocol === "https:" && (await laxTlsAllowed(hostname));
|
|
296
|
+
// assertSafeApiUrl lets http through only for localhost and loopback literals. Literals need no lookup;
|
|
297
|
+
// the name localhost must still resolve to loopback.
|
|
298
|
+
const dispatcher = lax ? apiLaxDispatcher() : protocol === "http:" && hostname === "localhost" ? apiLoopbackDispatcher() : undefined;
|
|
299
|
+
let response;
|
|
300
|
+
try {
|
|
301
|
+
response = await undiciFetch(url, {
|
|
302
|
+
method: init.method,
|
|
303
|
+
headers,
|
|
304
|
+
...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}),
|
|
305
|
+
// A redirect could carry the token to another host.
|
|
306
|
+
redirect: "error",
|
|
307
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
308
|
+
...(dispatcher ? { dispatcher } : {}),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
const err = error;
|
|
313
|
+
const reason = err.name === "TimeoutError" || err.name === "AbortError"
|
|
314
|
+
? `no answer within ${REQUEST_TIMEOUT_MS / 1000} seconds`
|
|
315
|
+
: (err.cause?.code ?? err.cause?.message ?? err.message);
|
|
316
|
+
throw new AuthError(redact(`could not reach ${new URL(url).origin}: ${reason}`, [init.token]));
|
|
317
|
+
}
|
|
318
|
+
const text = await response.text().catch(() => "");
|
|
319
|
+
let json;
|
|
320
|
+
try {
|
|
321
|
+
json = text ? JSON.parse(text) : undefined;
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
json = undefined;
|
|
325
|
+
}
|
|
326
|
+
return { status: response.status, json };
|
|
327
|
+
}
|
|
328
|
+
// ---------- login ----------
|
|
329
|
+
const deviceCodeSchema = z.object({
|
|
330
|
+
device_code: z.string().min(1),
|
|
331
|
+
user_code: z.string().min(1),
|
|
332
|
+
verification_uri: z.string().min(1),
|
|
333
|
+
verification_uri_complete: z.string().optional(),
|
|
334
|
+
expires_in: z.number().positive(),
|
|
335
|
+
interval: z.number().nonnegative().optional(),
|
|
336
|
+
});
|
|
337
|
+
const tokenSchema = z.object({ access_token: z.string().min(1) });
|
|
338
|
+
const errorSchema = z.object({ error: z.string(), error_description: z.string().optional() });
|
|
339
|
+
// RFC 8628 default interval, and bounds for a server-sent expires_in.
|
|
340
|
+
const MIN_INTERVAL_S = 5;
|
|
341
|
+
const MIN_EXPIRES_S = 1;
|
|
342
|
+
const MAX_EXPIRES_S = 30 * 60;
|
|
343
|
+
// A server text shown to the user: one line, short, no secrets.
|
|
344
|
+
function serverText(text, secrets) {
|
|
345
|
+
// Very short values (test codes) would mangle ordinary words; real codes and tokens are long.
|
|
346
|
+
const longSecrets = secrets.filter((secret) => secret !== undefined && secret.length >= 8);
|
|
347
|
+
return redact(text, longSecrets).replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ").trim().slice(0, 200);
|
|
348
|
+
}
|
|
349
|
+
const PLAN_LABEL = { none: "no plan", kit: "Kit plan", pro: "Pro plan", cloud: "Cloud plan" };
|
|
350
|
+
export async function login(ctx) {
|
|
351
|
+
const base = apiUrl(ctx.env);
|
|
352
|
+
assertSafeApiUrl(base);
|
|
353
|
+
const codeRes = await request(base, "/api/auth/device/code", {
|
|
354
|
+
method: "POST",
|
|
355
|
+
body: { client_id: CLIENT_ID, scope: "license" },
|
|
356
|
+
});
|
|
357
|
+
const code = deviceCodeSchema.safeParse(codeRes.json);
|
|
358
|
+
if (codeRes.status !== 200 || !code.success) {
|
|
359
|
+
throw new AuthError(`could not start login: ${base}/api/auth/device/code answered ${codeRes.status}`);
|
|
360
|
+
}
|
|
361
|
+
const device = code.data;
|
|
362
|
+
const openUrl = device.verification_uri_complete ?? device.verification_uri;
|
|
363
|
+
ctx.stderr([
|
|
364
|
+
`Your code: ${device.user_code}`,
|
|
365
|
+
`Open ${openUrl} and confirm the code to log in.`,
|
|
366
|
+
"Waiting for confirmation...",
|
|
367
|
+
"",
|
|
368
|
+
].join("\n"));
|
|
369
|
+
if (ctx.env.SUGIKIT_NO_OPEN !== "1") {
|
|
370
|
+
try {
|
|
371
|
+
ctx.open(openUrl);
|
|
372
|
+
}
|
|
373
|
+
catch {
|
|
374
|
+
// The URL is printed above.
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
let intervalMs = Math.max(device.interval ?? MIN_INTERVAL_S, MIN_INTERVAL_S) * 1000;
|
|
378
|
+
const expiresS = Math.min(Math.max(device.expires_in, MIN_EXPIRES_S), MAX_EXPIRES_S);
|
|
379
|
+
const deadline = ctx.now() + expiresS * 1000;
|
|
380
|
+
let token;
|
|
381
|
+
let lastNetworkError;
|
|
382
|
+
while (token === undefined) {
|
|
383
|
+
await ctx.sleep(intervalMs);
|
|
384
|
+
if (ctx.now() > deadline)
|
|
385
|
+
throw lastNetworkError ?? new AuthError("The login code expired. Run sugikit login again.");
|
|
386
|
+
let res;
|
|
387
|
+
try {
|
|
388
|
+
res = await request(base, "/api/auth/device/token", {
|
|
389
|
+
method: "POST",
|
|
390
|
+
body: { grant_type: DEVICE_GRANT_TYPE, device_code: device.device_code, client_id: CLIENT_ID },
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
// A network error or timeout: try again at the next interval until the code expires.
|
|
395
|
+
lastNetworkError = error instanceof AuthError ? error : new AuthError(String(error));
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
lastNetworkError = undefined;
|
|
399
|
+
if (res.status === 200) {
|
|
400
|
+
const ok = tokenSchema.safeParse(res.json);
|
|
401
|
+
if (!ok.success)
|
|
402
|
+
throw new AuthError("the login server sent an answer without an access token");
|
|
403
|
+
token = ok.data.access_token;
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
const err = errorSchema.safeParse(res.json);
|
|
407
|
+
const kind = err.success ? err.data.error : undefined;
|
|
408
|
+
if (kind === "authorization_pending")
|
|
409
|
+
continue;
|
|
410
|
+
if (kind === "slow_down") {
|
|
411
|
+
intervalMs += 5000;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (kind === "expired_token")
|
|
415
|
+
throw new AuthError("The login code expired. Run sugikit login again.");
|
|
416
|
+
if (kind === "access_denied")
|
|
417
|
+
throw new AuthError("Login was denied in the browser. Run sugikit login to try again.");
|
|
418
|
+
const secrets = [device.device_code];
|
|
419
|
+
const detail = err.success
|
|
420
|
+
? ` (${serverText(err.data.error, secrets)}${err.data.error_description ? `: ${serverText(err.data.error_description, secrets)}` : ""})`
|
|
421
|
+
: "";
|
|
422
|
+
throw new AuthError(`login failed: ${base}/api/auth/device/token answered ${res.status}${detail}`);
|
|
423
|
+
}
|
|
424
|
+
writeSecretFile(ctx, authPath(ctx), { access_token: token, api_url: base, saved_at: new Date(ctx.now()).toISOString() });
|
|
425
|
+
dropLicenseCache(ctx);
|
|
426
|
+
try {
|
|
427
|
+
const license = await fetchLicense(ctx, { access_token: token, api_url: base });
|
|
428
|
+
ctx.stdout(`Logged in as ${license.email} (${PLAN_LABEL[license.plan]}).\n`);
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
ctx.stdout("Logged in.\n");
|
|
432
|
+
ctx.stderr(`${redact(error.message, [token])}\n`);
|
|
433
|
+
}
|
|
434
|
+
return 0;
|
|
435
|
+
}
|
|
436
|
+
// ---------- license ----------
|
|
437
|
+
const RELOGIN = "Run sugikit login.";
|
|
438
|
+
async function fetchLicense(ctx, auth) {
|
|
439
|
+
const res = await request(auth.api_url, "/api/license", { method: "GET", token: auth.access_token });
|
|
440
|
+
if (res.status === 401) {
|
|
441
|
+
dropLicenseCache(ctx);
|
|
442
|
+
throw new SessionExpiredError(`Your Sugikit session expired or was signed out. ${RELOGIN}`);
|
|
443
|
+
}
|
|
444
|
+
const parsed = licenseResponseSchema.safeParse(res.json);
|
|
445
|
+
if (res.status !== 200 || !parsed.success) {
|
|
446
|
+
throw new AuthError(`the license check failed: ${auth.api_url}/api/license answered ${res.status}`);
|
|
447
|
+
}
|
|
448
|
+
const cachedAt = ctx.now();
|
|
449
|
+
writeSecretFile(ctx, licensePath(ctx), { ...parsed.data, api_url: auth.api_url, cached_at: cachedAt });
|
|
450
|
+
return { ...parsed.data, cached: false, cachedAt };
|
|
451
|
+
}
|
|
452
|
+
// The current license: a fresh cache without a network call, else the server, else a cache younger than 7 days.
|
|
453
|
+
export async function getLicense(ctx, options = {}) {
|
|
454
|
+
const auth = readAuth(ctx);
|
|
455
|
+
if (!auth)
|
|
456
|
+
throw new NotLoggedInError(`Not logged in. ${RELOGIN}`);
|
|
457
|
+
const cache = readLicenseCache(ctx);
|
|
458
|
+
const age = cache && cache.api_url === auth.api_url ? ctx.now() - cache.cached_at : undefined;
|
|
459
|
+
const usable = age !== undefined && age >= 0 && age < LICENSE_MAX_AGE_MS && cache ? cache : undefined;
|
|
460
|
+
const fromCache = (c) => ({
|
|
461
|
+
plan: c.plan,
|
|
462
|
+
email: c.email,
|
|
463
|
+
checked_at: c.checked_at,
|
|
464
|
+
entitlements: c.entitlements,
|
|
465
|
+
updates_until: c.updates_until,
|
|
466
|
+
cloud_until: c.cloud_until,
|
|
467
|
+
cached: true,
|
|
468
|
+
cachedAt: c.cached_at,
|
|
469
|
+
});
|
|
470
|
+
if (!options.online && usable && age !== undefined && age < LICENSE_FRESH_MS)
|
|
471
|
+
return fromCache(usable);
|
|
472
|
+
try {
|
|
473
|
+
return await fetchLicense(ctx, auth);
|
|
474
|
+
}
|
|
475
|
+
catch (error) {
|
|
476
|
+
if (error instanceof SessionExpiredError)
|
|
477
|
+
throw error;
|
|
478
|
+
const message = redact(error.message, [auth.access_token]);
|
|
479
|
+
if (usable)
|
|
480
|
+
return fromCache(usable);
|
|
481
|
+
throw new AuthError(`${message}. No license check succeeded in the last 7 days. ${RELOGIN}`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
export function missingPlanMessage(plan) {
|
|
485
|
+
return `This needs a Sugikit account with the ${plan === "pro" ? "Pro" : "Kit"} plan. Buy at ${PRICING_URL}, then run sugikit login.`;
|
|
486
|
+
}
|
|
487
|
+
export function updatesEndedMessage(updatesUntil) {
|
|
488
|
+
return `Your updates ended on ${new Date(updatesUntil).toISOString().slice(0, 10)}. Renew at ${ACCOUNT_URL}`;
|
|
489
|
+
}
|
|
490
|
+
// True when this CLI was released after the license's updates window closed. null means no limit.
|
|
491
|
+
export function updatesEnded(updatesUntil, releaseDate) {
|
|
492
|
+
if (updatesUntil === null || releaseDate === undefined)
|
|
493
|
+
return false;
|
|
494
|
+
const release = Date.parse(`${releaseDate}T00:00:00.000Z`);
|
|
495
|
+
return !Number.isNaN(release) && release > Date.parse(updatesUntil);
|
|
496
|
+
}
|
|
497
|
+
// Gate for paid commands. Resolves with the license, or throws an AuthError with the message to print.
|
|
498
|
+
export async function requirePlan(plan, ctx = defaultContext()) {
|
|
499
|
+
let license;
|
|
500
|
+
try {
|
|
501
|
+
license = await getLicense(ctx);
|
|
502
|
+
}
|
|
503
|
+
catch (error) {
|
|
504
|
+
if (error instanceof NotLoggedInError)
|
|
505
|
+
throw new AuthError(missingPlanMessage(plan));
|
|
506
|
+
throw error;
|
|
507
|
+
}
|
|
508
|
+
if (!license.entitlements[plan])
|
|
509
|
+
throw new AuthError(missingPlanMessage(plan));
|
|
510
|
+
const releaseDate = "releaseDate" in ctx ? ctx.releaseDate : RELEASE_DATE;
|
|
511
|
+
if (updatesEnded(license.updates_until, releaseDate))
|
|
512
|
+
throw new AuthError(updatesEndedMessage(license.updates_until));
|
|
513
|
+
return license;
|
|
514
|
+
}
|
|
515
|
+
// ---------- whoami and logout ----------
|
|
516
|
+
export async function whoami(ctx) {
|
|
517
|
+
const license = await getLicense(ctx, { online: true });
|
|
518
|
+
const on = Object.keys(license.entitlements).filter((key) => license.entitlements[key]);
|
|
519
|
+
const updates = license.updates_until === null ? "no limit" : new Date(license.updates_until).toISOString().slice(0, 10);
|
|
520
|
+
ctx.stdout([
|
|
521
|
+
license.email,
|
|
522
|
+
`plan: ${license.plan}`,
|
|
523
|
+
`entitlements: ${on.length ? on.join(", ") : "none"}`,
|
|
524
|
+
`updates until: ${updates}`,
|
|
525
|
+
...(license.cloud_until ? [`cloud until: ${new Date(license.cloud_until).toISOString().slice(0, 10)}`] : []),
|
|
526
|
+
"",
|
|
527
|
+
].join("\n"));
|
|
528
|
+
if (license.cached) {
|
|
529
|
+
ctx.stderr(`offline: showing the license checked at ${new Date(license.cachedAt).toISOString()}\n`);
|
|
530
|
+
}
|
|
531
|
+
return 0;
|
|
532
|
+
}
|
|
533
|
+
export async function logout(ctx) {
|
|
534
|
+
const auth = readAuth(ctx);
|
|
535
|
+
if (auth) {
|
|
536
|
+
try {
|
|
537
|
+
await request(auth.api_url, "/api/auth/revoke", { method: "POST", body: {}, token: auth.access_token });
|
|
538
|
+
}
|
|
539
|
+
catch {
|
|
540
|
+
// Best effort: the local files go away either way.
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
rmSync(authPath(ctx), { force: true });
|
|
544
|
+
dropLicenseCache(ctx);
|
|
545
|
+
ctx.stdout(auth ? "Logged out.\n" : "Not logged in.\n");
|
|
546
|
+
return 0;
|
|
547
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Browser, type BrowserContext } from "playwright";
|
|
2
|
+
import { type FetchResult } from "./fetch.js";
|
|
3
|
+
import { type PolishBudget } from "./polish-run.js";
|
|
4
|
+
import type { Screenshot } from "./report.js";
|
|
5
|
+
import type { ConvertRenderData } from "./rules/convert-render.js";
|
|
6
|
+
import { type RuleResult } from "./rules/types.js";
|
|
7
|
+
export interface ShotsTarget {
|
|
8
|
+
outDir: string;
|
|
9
|
+
dir: string;
|
|
10
|
+
}
|
|
11
|
+
export interface BrowserRun {
|
|
12
|
+
results: RuleResult[];
|
|
13
|
+
states?: string[];
|
|
14
|
+
screenshots?: Screenshot[];
|
|
15
|
+
convert?: ConvertRenderData;
|
|
16
|
+
}
|
|
17
|
+
export declare function blockMedia(context: Pick<BrowserContext, "route">): Promise<void>;
|
|
18
|
+
export type BrowserLaunch = {
|
|
19
|
+
browser: Browser;
|
|
20
|
+
} | {
|
|
21
|
+
error: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function launchBrowser(): Promise<BrowserLaunch>;
|
|
24
|
+
export declare function fetchWithBrowser(browser: Browser, url: string): Promise<FetchResult | undefined>;
|
|
25
|
+
export declare function runBrowserChecks(url: string, shots?: ShotsTarget, shared?: Browser, local?: boolean, budget?: PolishBudget): Promise<BrowserRun>;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { chromium } from "playwright";
|
|
4
|
+
import { BROWSER_USER_AGENT, REQUEST_HEADERS, cliFetch, isChallengePage } from "./fetch.js";
|
|
5
|
+
import { NAVIGATION_MS, POLISH_STATES, firstLine, runPolishChecks, settle, stateContextOptions, } from "./polish-run.js";
|
|
6
|
+
import { result } from "./rules/types.js";
|
|
7
|
+
// Video and audio never change a polish result, and a heavy page can download hundreds of MB of them per state.
|
|
8
|
+
// Images stay: image checks and screenshots need them.
|
|
9
|
+
export async function blockMedia(context) {
|
|
10
|
+
await context.route("**/*", (route) => (route.request().resourceType() === "media" ? route.abort() : route.fallback()));
|
|
11
|
+
}
|
|
12
|
+
// One fresh browser context per state, like a new visitor with that device.
|
|
13
|
+
function contextPerState(browser) {
|
|
14
|
+
return async (state) => {
|
|
15
|
+
const context = await browser.newContext(stateContextOptions(state));
|
|
16
|
+
try {
|
|
17
|
+
await blockMedia(context);
|
|
18
|
+
const page = await context.newPage();
|
|
19
|
+
return { page: page, close: () => context.close() };
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
await context.close().catch(() => undefined);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function browserFailure(message) {
|
|
28
|
+
return { results: [result("polish/browser", "error", false, message)] };
|
|
29
|
+
}
|
|
30
|
+
export async function launchBrowser() {
|
|
31
|
+
try {
|
|
32
|
+
return { browser: await chromium.launch({ headless: true }) };
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
return { error: firstLine(error) };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Loads the page like a visitor when bot protection blocks the plain request. Returns the server HTML of the
|
|
39
|
+
// last main frame navigation, or the rendered DOM when that navigation was a challenge page.
|
|
40
|
+
export async function fetchWithBrowser(browser, url) {
|
|
41
|
+
let context;
|
|
42
|
+
try {
|
|
43
|
+
context = await browser.newContext({
|
|
44
|
+
viewport: POLISH_STATES["desktop-light"].viewport,
|
|
45
|
+
userAgent: BROWSER_USER_AGENT,
|
|
46
|
+
extraHTTPHeaders: { "accept-language": REQUEST_HEADERS["accept-language"] },
|
|
47
|
+
});
|
|
48
|
+
await blockMedia(context);
|
|
49
|
+
const page = await context.newPage();
|
|
50
|
+
const nav = {};
|
|
51
|
+
page.on("response", (response) => {
|
|
52
|
+
if (response.request().isNavigationRequest() && response.frame() === page.mainFrame())
|
|
53
|
+
nav.last = response;
|
|
54
|
+
});
|
|
55
|
+
const first = await page.goto(url, { waitUntil: "load", timeout: NAVIGATION_MS });
|
|
56
|
+
let response = nav.last ?? first ?? undefined;
|
|
57
|
+
let body = response ? await response.text().catch(() => "") : "";
|
|
58
|
+
if (!response || isChallengePage(body)) {
|
|
59
|
+
await settle(page);
|
|
60
|
+
response = nav.last ?? response;
|
|
61
|
+
body = await page.content();
|
|
62
|
+
}
|
|
63
|
+
if (!response)
|
|
64
|
+
return undefined;
|
|
65
|
+
return { status: response.status(), headers: await response.allHeaders(), body, finalUrl: page.url() };
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await context?.close().catch(() => undefined);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Pass a browser to reuse one the caller already launched; the caller closes it then. `local` says the page
|
|
75
|
+
// runs on a dev or preview server (lab timing is not representative); default isLoopbackUrl.
|
|
76
|
+
// `budget` is the soft time budget of the whole check (see PolishOptions.budget).
|
|
77
|
+
export async function runBrowserChecks(url, shots, shared, local, budget) {
|
|
78
|
+
let browser;
|
|
79
|
+
if (shared) {
|
|
80
|
+
browser = shared;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const launched = await launchBrowser();
|
|
84
|
+
if ("error" in launched)
|
|
85
|
+
return browserFailure(`Could not start headless Chromium: ${launched.error}`);
|
|
86
|
+
browser = launched.browser;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
if (shots)
|
|
90
|
+
await mkdir(join(shots.outDir, shots.dir), { recursive: true });
|
|
91
|
+
const run = await runPolishChecks(contextPerState(browser), {
|
|
92
|
+
url,
|
|
93
|
+
screenshots: shots !== undefined,
|
|
94
|
+
fetch: cliFetch,
|
|
95
|
+
...(local !== undefined ? { local } : {}),
|
|
96
|
+
...(budget ? { budget } : {}),
|
|
97
|
+
});
|
|
98
|
+
const screenshots = [];
|
|
99
|
+
if (shots) {
|
|
100
|
+
for (const shot of run.screenshots ?? []) {
|
|
101
|
+
const path = join(shots.dir, `${shot.state}.png`);
|
|
102
|
+
await writeFile(join(shots.outDir, path), shot.png);
|
|
103
|
+
screenshots.push({ state: shot.state, path });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
results: run.results,
|
|
108
|
+
...(run.states ? { states: run.states } : {}),
|
|
109
|
+
...(screenshots.length > 0 ? { screenshots } : {}),
|
|
110
|
+
...(run.convert ? { convert: run.convert } : {}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return browserFailure(`Browser checks could not load ${url}: ${firstLine(error)}`);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
if (!shared)
|
|
118
|
+
await browser.close();
|
|
119
|
+
}
|
|
120
|
+
}
|