@sssb/tools 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/bin/sssb.js +102 -0
- package/package.json +89 -5
- package/src/app-port.js +77 -0
- package/src/app.js +142 -0
- package/src/assembly-checks.d.ts +57 -0
- package/src/assembly-checks.js +259 -0
- package/src/commands/check-demo-size.js +80 -0
- package/src/commands/check-doc-paths.js +48 -0
- package/src/commands/check-e2e.js +611 -0
- package/src/commands/check-fences.js +51 -0
- package/src/commands/check-real-env.js +165 -0
- package/src/commands/check-tmpdir-leak.js +78 -0
- package/src/commands/ci.js +350 -0
- package/src/commands/dev-tick.js +25 -0
- package/src/commands/doctor-checks.js +1276 -0
- package/src/commands/doctor-vitest.js +262 -0
- package/src/commands/doctor.js +954 -0
- package/src/commands/e2e-profile.js +94 -0
- package/src/commands/guide.js +108 -0
- package/src/commands/launch-json.js +55 -0
- package/src/commands/port.js +22 -0
- package/src/commands/serve-standalone.js +78 -0
- package/src/commands/tick.js +27 -0
- package/src/doc-paths.d.ts +106 -0
- package/src/doc-paths.js +328 -0
- package/src/framework-guard.js +35 -0
- package/src/kit.d.ts +43 -0
- package/src/kit.js +205 -0
- package/src/next.d.ts +16 -0
- package/src/next.js +55 -0
- package/src/ports.d.ts +72 -0
- package/src/ports.js +306 -0
- package/src/prune-mantine-css.js +171 -0
- package/src/testing/tmp-dir-marker.js +9 -0
- package/src/testing/tmp-dir-setup.d.ts +2 -0
- package/src/testing/tmp-dir-setup.js +9 -0
- package/src/testing/tmp-dir.d.ts +20 -0
- package/src/testing/tmp-dir.js +174 -0
- package/src/tick/dev-tick.js +128 -0
- package/src/tick/tick.js +315 -0
- package/src/tick/ticker-marker.d.ts +17 -0
- package/src/tick/ticker-marker.js +83 -0
- package/src/tick/ticker.d.ts +36 -0
- package/src/tick/ticker.js +142 -0
- package/src/vite.d.ts +38 -0
- package/src/vite.js +64 -0
- package/src/vitest.d.ts +21 -0
- package/src/vitest.js +55 -0
- package/tsconfig.base.json +33 -0
- package/README.md +0 -3
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Checks over a synthesized CloudFormation assembly: pure functions over the template's `Resources` and over
|
|
3
|
+
// the Lambda bundles staged on disk, so an adopter whose infra does not use SSSB's construct can run them on
|
|
4
|
+
// their own assembly. This package never imports `@sssb/kit` and has no dependency on aws-cdk-lib.
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
|
|
8
|
+
/** @typedef {import('./assembly-checks.d.ts').Resources} Resources */
|
|
9
|
+
/** @typedef {import('./assembly-checks.d.ts').Resource} Resource */
|
|
10
|
+
/** @typedef {{ Effect?: unknown, Action?: unknown, NotAction?: unknown, Resource?: unknown }} Statement */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The code staged for each Lambda whose asset has an `index.mjs` or `index.js`, keyed by logical id. For
|
|
14
|
+
* a NodejsFunction that is what esbuild produced during the synth, with the tsconfig the stack passed, not a
|
|
15
|
+
* re-creation of it. CDK's own provider handler ships a ready-made `index.js`, and is read like any other. A
|
|
16
|
+
* placeholder asset with neither is left out.
|
|
17
|
+
* @type {import('./assembly-checks.d.ts').lambdaBundles}
|
|
18
|
+
*/
|
|
19
|
+
export function lambdaBundles(resources, assemblyDir) {
|
|
20
|
+
/** @type {Map<string, string>} */
|
|
21
|
+
const found = new Map()
|
|
22
|
+
for (const [id, r] of Object.entries(resources)) {
|
|
23
|
+
if (r.Type !== 'AWS::Lambda::Function') continue
|
|
24
|
+
const key = /** @type {{ S3Key?: unknown } | undefined} */ (r.Properties?.Code)?.S3Key
|
|
25
|
+
if (typeof key !== 'string') continue
|
|
26
|
+
const staged = path.join(assemblyDir, `asset.${key.replace(/\.zip$/, '')}`)
|
|
27
|
+
const entry = ['index.mjs', 'index.js'].map((file) => path.join(staged, file)).find((file) => existsSync(file))
|
|
28
|
+
if (entry) found.set(id, readFileSync(entry, 'utf8'))
|
|
29
|
+
}
|
|
30
|
+
return found
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @type {import('./assembly-checks.d.ts').lambdaFunction} */
|
|
34
|
+
export function lambdaFunction(resources, namePrefix) {
|
|
35
|
+
const found = Object.entries(resources).find(
|
|
36
|
+
([id, r]) => r.Type === 'AWS::Lambda::Function' && id.startsWith(namePrefix) && !id.includes('Provider'),
|
|
37
|
+
)
|
|
38
|
+
if (!found) throw new Error(`no ${namePrefix} function in the synthesized stack`)
|
|
39
|
+
const props = found[1].Properties ?? {}
|
|
40
|
+
const variables = /** @type {{ Variables?: Record<string, unknown> } | undefined} */ (props.Environment)?.Variables
|
|
41
|
+
return { id: found[0], inVpc: props.VpcConfig !== undefined, env: variables ?? {} }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Requiring the path comments makes the check fail CLOSED: a minified bundle carries no such comments, and
|
|
46
|
+
* so fails here instead of passing every check that reads them.
|
|
47
|
+
* @type {import('./assembly-checks.d.ts').migratorBundleProblems}
|
|
48
|
+
*/
|
|
49
|
+
export function migratorBundleProblems({ code, sources, migrations }) {
|
|
50
|
+
if (code === undefined) return ['the migrator is not bundled by this synth']
|
|
51
|
+
/** @type {string[]} */
|
|
52
|
+
const problems = []
|
|
53
|
+
if (code.includes('test-fixture')) problems.push('the migrator’s bundle holds code from a test-fixture directory')
|
|
54
|
+
for (const source of sources) {
|
|
55
|
+
if (!code.includes(`// ${source}\n`)) {
|
|
56
|
+
problems.push(`the migrator's bundle names ${source} (an unminified bundle, built from the app's seam)`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (const name of migrations) {
|
|
60
|
+
if (!code.includes(`"${name}"`)) problems.push(`the migrator's bundle carries the app's migration ${name}`)
|
|
61
|
+
}
|
|
62
|
+
return problems
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The server must come up in `real` mode and carry no switch that turns on simulated or e2e behaviour. The
|
|
67
|
+
* absence of the other two is judged by presence, not value: `DEMO_MODE: 'false'` is still a switch a later
|
|
68
|
+
* change could flip.
|
|
69
|
+
* @type {import('./assembly-checks.d.ts').failClosedEnvironmentProblems}
|
|
70
|
+
*/
|
|
71
|
+
export function failClosedEnvironmentProblems(env) {
|
|
72
|
+
/** @type {string[]} */
|
|
73
|
+
const problems = []
|
|
74
|
+
if (env.APP_MODE !== 'real') problems.push(`APP_MODE is ${JSON.stringify(env.APP_MODE)}, not 'real'`)
|
|
75
|
+
for (const name of ['DEMO_MODE', 'E2E_BUILD']) {
|
|
76
|
+
if (name in env) problems.push(`${name} is set in the final environment`)
|
|
77
|
+
}
|
|
78
|
+
return problems
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The permission documents a resource carries: a policy's, a managed policy's, or a role's inline ones.
|
|
83
|
+
* @param {Resource} resource
|
|
84
|
+
* @returns {unknown[]}
|
|
85
|
+
*/
|
|
86
|
+
function policyDocuments(resource) {
|
|
87
|
+
if (resource.Type === 'AWS::IAM::Policy' || resource.Type === 'AWS::IAM::ManagedPolicy') {
|
|
88
|
+
return [resource.Properties?.PolicyDocument]
|
|
89
|
+
}
|
|
90
|
+
if (resource.Type === 'AWS::IAM::Role') {
|
|
91
|
+
const inline = /** @type {{ PolicyDocument?: unknown }[] | undefined} */ (resource.Properties?.Policies) ?? []
|
|
92
|
+
return inline.map((policy) => policy.PolicyDocument)
|
|
93
|
+
}
|
|
94
|
+
return []
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Whether an IAM action pattern can name one of `service`'s actions. IAM matches `*` and `?` anywhere in
|
|
99
|
+
* an action, so `*`, `rds-data:*` and `rds*:Execute*` all reach `rds-data:ExecuteStatement`.
|
|
100
|
+
* @param {string} action
|
|
101
|
+
* @param {string} service
|
|
102
|
+
*/
|
|
103
|
+
function reaches(action, service) {
|
|
104
|
+
const prefix = action.includes(':') ? action.slice(0, action.indexOf(':')) : action
|
|
105
|
+
const pattern = prefix
|
|
106
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
107
|
+
.replace(/\*/g, '.*')
|
|
108
|
+
.replace(/\?/g, '.')
|
|
109
|
+
return new RegExp(`^${pattern}$`, 'i').test(service)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Every Allow statement in the stack's IAM policies, managed policies and roles' inline policies that can
|
|
114
|
+
* grant one of `service`'s actions (`rds-data`, `secretsmanager`): by naming one, by a wildcard that
|
|
115
|
+
* reaches one (`*` reaches every service), or by `NotAction`, which grants everything it does not name.
|
|
116
|
+
* Roles' trust policies grant nothing and are not read.
|
|
117
|
+
* @param {Resources} all
|
|
118
|
+
* @param {string} service
|
|
119
|
+
* @returns {Statement[]}
|
|
120
|
+
*/
|
|
121
|
+
function grantsOf(all, service) {
|
|
122
|
+
return Object.values(all)
|
|
123
|
+
.flatMap(policyDocuments)
|
|
124
|
+
.flatMap(
|
|
125
|
+
(document) =>
|
|
126
|
+
/** @type {Statement[]} */ (
|
|
127
|
+
/** @type {{ Statement?: unknown[] } | undefined} */ (document)?.Statement ?? []
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
.filter((s) => s.Effect !== 'Deny')
|
|
131
|
+
.filter((s) => {
|
|
132
|
+
if (s.NotAction !== undefined) return true
|
|
133
|
+
const actions = Array.isArray(s.Action) ? s.Action : [s.Action]
|
|
134
|
+
return actions.some((action) => typeof action === 'string' && reaches(action, service))
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The AWS-managed policies a role may attach: the check cannot read their contents, so it allows only the
|
|
140
|
+
* two Lambda execution policies CDK attaches (logs, and network interfaces on `pg`), which grant neither
|
|
141
|
+
* service. A managed policy defined in the stack is read like any other (grantsOf).
|
|
142
|
+
*/
|
|
143
|
+
const KNOWN_AWS_POLICIES = ['service-role/AWSLambdaBasicExecutionRole', 'service-role/AWSLambdaVPCAccessExecutionRole']
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {Resources} all
|
|
147
|
+
* @returns {string[]}
|
|
148
|
+
*/
|
|
149
|
+
function unreadablePolicies(all) {
|
|
150
|
+
const inStack = new Set(
|
|
151
|
+
Object.entries(all)
|
|
152
|
+
.filter(([, r]) => r.Type === 'AWS::IAM::ManagedPolicy')
|
|
153
|
+
.map(([id]) => id),
|
|
154
|
+
)
|
|
155
|
+
return Object.entries(all)
|
|
156
|
+
.filter(([, r]) => r.Type === 'AWS::IAM::Role')
|
|
157
|
+
.flatMap(([id, r]) =>
|
|
158
|
+
/** @type {unknown[] | undefined} */ (r.Properties?.ManagedPolicyArns ?? [])
|
|
159
|
+
.filter((arn) => {
|
|
160
|
+
const text = JSON.stringify(arn)
|
|
161
|
+
const ref = /** @type {{ Ref?: string }} */ (arn).Ref
|
|
162
|
+
if (ref !== undefined && inStack.has(ref)) return false
|
|
163
|
+
return !KNOWN_AWS_POLICIES.some((name) =>
|
|
164
|
+
new RegExp(`:iam::aws:policy/${name}"[\\]}]*$`).test(text),
|
|
165
|
+
)
|
|
166
|
+
})
|
|
167
|
+
.map((arn) => `${id}: ${JSON.stringify(arn)}`),
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The cluster's own secret: the attachment CDK makes for the cluster, and the secret it attaches.
|
|
173
|
+
* @param {Resources} all
|
|
174
|
+
* @param {string} clusterId
|
|
175
|
+
* @returns {string[]}
|
|
176
|
+
*/
|
|
177
|
+
function databaseSecretIds(all, clusterId) {
|
|
178
|
+
return Object.entries(all)
|
|
179
|
+
.filter(
|
|
180
|
+
([, r]) =>
|
|
181
|
+
r.Type === 'AWS::SecretsManager::SecretTargetAttachment' &&
|
|
182
|
+
JSON.stringify(r.Properties?.TargetId) === JSON.stringify({ Ref: clusterId }),
|
|
183
|
+
)
|
|
184
|
+
.flatMap(([id, r]) => {
|
|
185
|
+
const secret = /** @type {{ Ref?: string } | undefined} */ (r.Properties?.SecretId)?.Ref
|
|
186
|
+
return secret ? [id, secret] : [id]
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Whether every resource `statement` grants refers to one of `ids`, and none is a wildcard.
|
|
192
|
+
* @param {Statement} statement
|
|
193
|
+
* @param {readonly string[]} ids
|
|
194
|
+
*/
|
|
195
|
+
function namesOnly(statement, ids) {
|
|
196
|
+
const granted = Array.isArray(statement.Resource) ? statement.Resource : [statement.Resource ?? '*']
|
|
197
|
+
return granted.every((resource) => {
|
|
198
|
+
const text = JSON.stringify(resource)
|
|
199
|
+
return !text.includes('*') && ids.some((id) => text.includes(`"Ref":"${id}"`))
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The template's database cluster, by logical id. Throws when there is none: every check that reasons about
|
|
205
|
+
* "the cluster's own" anything is meaningless without one.
|
|
206
|
+
* @param {Resources} all
|
|
207
|
+
* @returns {[string, Resource]}
|
|
208
|
+
*/
|
|
209
|
+
function clusterOf(all) {
|
|
210
|
+
const found = Object.entries(all).find(([, r]) => r.Type === 'AWS::RDS::DBCluster')
|
|
211
|
+
if (!found) throw new Error('no AWS::RDS::DBCluster in the synthesized stack')
|
|
212
|
+
return found
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @type {import('./assembly-checks.d.ts').secretGrantProblems} */
|
|
216
|
+
export function secretGrantProblems(resources) {
|
|
217
|
+
const [clusterId] = clusterOf(resources)
|
|
218
|
+
/** @type {string[]} */
|
|
219
|
+
const problems = []
|
|
220
|
+
// The database secret is read by name, on every shape: never a wildcard, never another secret.
|
|
221
|
+
const secretIds = databaseSecretIds(resources, clusterId)
|
|
222
|
+
const secretGrants = grantsOf(resources, 'secretsmanager')
|
|
223
|
+
if (!(secretIds.length > 0 && secretGrants.length > 0 && secretGrants.every((s) => namesOnly(s, secretIds)))) {
|
|
224
|
+
problems.push('every Secrets Manager grant names the cluster’s own secret alone')
|
|
225
|
+
}
|
|
226
|
+
const unreadable = unreadablePolicies(resources)
|
|
227
|
+
if (unreadable.length > 0) {
|
|
228
|
+
problems.push(`no role attaches an AWS-managed policy the check cannot read: ${unreadable.join('; ')}`)
|
|
229
|
+
}
|
|
230
|
+
return problems
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** @type {import('./assembly-checks.d.ts').dataApiShapeProblems} */
|
|
234
|
+
export function dataApiShapeProblems(resources) {
|
|
235
|
+
const [clusterId, cluster] = clusterOf(resources)
|
|
236
|
+
/** @type {string[]} */
|
|
237
|
+
const problems = secretGrantProblems(resources)
|
|
238
|
+
// Every statement that can grant a Data API action (see grantsOf) names this cluster, and nothing
|
|
239
|
+
// wider: the grant plus the secret is the whole way in on this shape.
|
|
240
|
+
const dataApiGrants = grantsOf(resources, 'rds-data')
|
|
241
|
+
if (!dataApiGrants.some((s) => JSON.stringify(s.Action).includes('rds-data:ExecuteStatement'))) {
|
|
242
|
+
problems.push('a role is granted rds-data:ExecuteStatement')
|
|
243
|
+
}
|
|
244
|
+
if (!dataApiGrants.every((s) => namesOnly(s, [clusterId]))) {
|
|
245
|
+
problems.push('every rds-data grant is scoped to this cluster alone')
|
|
246
|
+
}
|
|
247
|
+
// Nothing reaches the database over the network on this shape: its security group admits nobody.
|
|
248
|
+
const dbGroups = JSON.stringify(cluster.Properties?.VpcSecurityGroupIds ?? [])
|
|
249
|
+
const ingress = Object.entries(resources).filter(
|
|
250
|
+
([id, r]) =>
|
|
251
|
+
(r.Type === 'AWS::EC2::SecurityGroupIngress' &&
|
|
252
|
+
dbGroups.includes(JSON.stringify(r.Properties?.GroupId ?? null))) ||
|
|
253
|
+
(r.Type === 'AWS::EC2::SecurityGroup' &&
|
|
254
|
+
dbGroups.includes(`"${id}"`) &&
|
|
255
|
+
/** @type {unknown[] | undefined} */ (r.Properties?.SecurityGroupIngress ?? []).length > 0),
|
|
256
|
+
)
|
|
257
|
+
if (ingress.length > 0) problems.push('the database security group admits no inbound traffic')
|
|
258
|
+
return problems
|
|
259
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `sssb check-demo-size <bytes>`: guards the file:// single-file demo artifact against silent bloat (vendored
|
|
3
|
+
// libraries, base64 blobs, a seam import that drags a server graph in).
|
|
4
|
+
//
|
|
5
|
+
// THE BUDGET IS THE APP'S OWN NUMBER, the argument of the `check:demo-size` script in its package.json; this
|
|
6
|
+
// command holds none. One shared number sized for the largest app is not a gate for the others, since a small app
|
|
7
|
+
// could double and still pass. The artifact is the app's (`<app>/dist-demo/index.html`), found from the app's
|
|
8
|
+
// directory: the working directory, or `--app <dir>`.
|
|
9
|
+
//
|
|
10
|
+
// To change an app's number, re-set it in that app's package.json and record what changed and why beside it (SSSB's
|
|
11
|
+
// starter keeps a DEMO-SIZE.md for this), in its own commit: what grew, by how much, and how much headroom you left
|
|
12
|
+
// on purpose.
|
|
13
|
+
//
|
|
14
|
+
// WHEN IT GOES RED: find what grew before raising anything. The static demo build prints, per app, how much
|
|
15
|
+
// stylesheet the Mantine pruning removed; the JS is where features land. Build the previous commit in a throwaway
|
|
16
|
+
// worktree and compare `dist-demo/index.html`, or bundle with a recorder plugin that prints each module's
|
|
17
|
+
// `renderedLength`: a dependency that arrived, a seam registry imported into the bundle (37% in one commit once, see
|
|
18
|
+
// SSSB's ADR-0012), a base64 asset, a catalog namespace. Fix an incidental cause; raise the budget only for a
|
|
19
|
+
// feature.
|
|
20
|
+
//
|
|
21
|
+
// WHEN IT WARNS: under 2% headroom the command passes and says so (on CI as a workflow annotation too). That is the
|
|
22
|
+
// cue to look at what grew while it is cheap, not a failure; it exists because "one dependency bump from red" used
|
|
23
|
+
// to be something a person had to notice by measuring.
|
|
24
|
+
import { statSync } from 'node:fs'
|
|
25
|
+
import path from 'node:path'
|
|
26
|
+
import { takeAppDir, UsageError } from '../app.js'
|
|
27
|
+
|
|
28
|
+
/** Headroom, as a fraction of the budget, under which the gate still passes but says so. */
|
|
29
|
+
const THIN_HEADROOM = 0.02
|
|
30
|
+
|
|
31
|
+
const WHEN_RED =
|
|
32
|
+
'WHEN IT GOES RED: first find what grew: build the previous commit and compare dist-demo/index.html, or bundle with a recorder ' +
|
|
33
|
+
"plugin that prints each module's renderedLength. Fix an incidental cause (a new dependency, a seam registry " +
|
|
34
|
+
'imported into the bundle, a base64 asset); raise the budget only for a feature.'
|
|
35
|
+
|
|
36
|
+
/** @param {string[]} args */
|
|
37
|
+
export async function run(args) {
|
|
38
|
+
const appDir = takeAppDir(args)
|
|
39
|
+
if (args.length !== 1) {
|
|
40
|
+
throw new UsageError(
|
|
41
|
+
"usage: sssb check-demo-size <bytes>: pass this app's budget, the argument of the `check:demo-size` " +
|
|
42
|
+
'script in its package.json',
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
const budget = Number(args[0])
|
|
46
|
+
if (!Number.isFinite(budget) || budget <= 0) {
|
|
47
|
+
throw new UsageError(
|
|
48
|
+
`sssb check-demo-size: the budget argument must be a positive number of bytes, got ${args[0]}`,
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
const file = path.join(appDir, 'dist-demo', 'index.html')
|
|
52
|
+
let size
|
|
53
|
+
try {
|
|
54
|
+
size = statSync(file).size
|
|
55
|
+
} catch {
|
|
56
|
+
throw new UsageError(`sssb check-demo-size: no ${file}: build the static demo first`)
|
|
57
|
+
}
|
|
58
|
+
if (size > budget) {
|
|
59
|
+
console.error(`${file} is ${size} bytes — ${size - budget} over the ${budget} byte budget.`)
|
|
60
|
+
console.error(
|
|
61
|
+
"That number is this app's own: the argument of `check:demo-size` in its package.json. " +
|
|
62
|
+
`${WHEN_RED} If the growth is intentional, raise the budget in its own commit and say what grew ` +
|
|
63
|
+
'where the app records its budget.',
|
|
64
|
+
)
|
|
65
|
+
return 1
|
|
66
|
+
}
|
|
67
|
+
const headroom = budget - size
|
|
68
|
+
// Rounded DOWN, so a headroom under THIN_HEADROOM never prints as the threshold itself ("2.0%" beside a warning).
|
|
69
|
+
const percent = (Math.floor((headroom * 1000) / budget) / 10).toFixed(1)
|
|
70
|
+
console.log(`${file}: ${size} / ${budget} bytes — OK (${headroom} bytes, ${percent}% headroom)`)
|
|
71
|
+
if (headroom / budget < THIN_HEADROOM) {
|
|
72
|
+
const message =
|
|
73
|
+
`only ${headroom} bytes (${percent}%) of headroom left under the ${budget} byte budget: the next feature ` +
|
|
74
|
+
"may tip it. Look at what grew while that is cheap; raise this app's budget, in its own commit, only for " +
|
|
75
|
+
'a feature.'
|
|
76
|
+
console.warn(`check-demo-size: ${message}`)
|
|
77
|
+
if (process.env['GITHUB_ACTIONS']) console.log(`::warning title=demo size headroom::${message}`)
|
|
78
|
+
}
|
|
79
|
+
return 0
|
|
80
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `sssb check-doc-paths`: fails when a doctrine doc cites a path that does not exist. The doc set and the rules are
|
|
3
|
+
// the workspace's own `sssb.docPaths` (ADR-0014 part 3), so an adopter keeps this gate the day packages/kit leaves
|
|
4
|
+
// the tree.
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import process from 'node:process'
|
|
7
|
+
import { citedPaths, docFiles, missingIncludes, readDocPathsConfig, resolves } from '../doc-paths.js'
|
|
8
|
+
import { takeOption, UsageError, workspaceRootOf } from '../app.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {string[]} args
|
|
12
|
+
* @returns {Promise<number>} 0 when every citation resolves, 1 otherwise
|
|
13
|
+
*/
|
|
14
|
+
export async function run(args) {
|
|
15
|
+
const rest = [...args]
|
|
16
|
+
const other = takeOption(rest, '--root')
|
|
17
|
+
if (rest.length > 0)
|
|
18
|
+
throw new UsageError(
|
|
19
|
+
`sssb check-doc-paths: unknown argument ${rest[0]} (usage: check-doc-paths [--root <dir>])`,
|
|
20
|
+
)
|
|
21
|
+
const root = other === undefined ? workspaceRootOf(process.cwd()) : path.resolve(other)
|
|
22
|
+
const config = readDocPathsConfig(root)
|
|
23
|
+
|
|
24
|
+
const files = docFiles(root, config)
|
|
25
|
+
let checked = 0
|
|
26
|
+
let broken = 0
|
|
27
|
+
// A missing include entry or an empty doc set would make the gate pass by checking nothing.
|
|
28
|
+
for (const rel of missingIncludes(root, config)) {
|
|
29
|
+
broken++
|
|
30
|
+
process.stdout.write(`${rel}: in sssb.docPaths.include, but not in the tree\n`)
|
|
31
|
+
}
|
|
32
|
+
if (files.length === 0) {
|
|
33
|
+
broken++
|
|
34
|
+
process.stdout.write('sssb.docPaths.include names no markdown file: the gate would check nothing\n')
|
|
35
|
+
}
|
|
36
|
+
for (const file of files) {
|
|
37
|
+
for (const { path: cited, line } of citedPaths(file, config, root)) {
|
|
38
|
+
checked++
|
|
39
|
+
if (resolves(cited, root, config)) continue
|
|
40
|
+
broken++
|
|
41
|
+
process.stdout.write(`${file}:${line} cites \`${cited}\`\n`)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
process.stdout.write(
|
|
45
|
+
`check-doc-paths: ${files.length} doc(s), ${checked} citation(s) checked, ${broken} broken` + '\n',
|
|
46
|
+
)
|
|
47
|
+
return broken === 0 ? 0 : 1
|
|
48
|
+
}
|