create-stitchkit 0.6.17 → 0.6.19
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 +42 -0
- package/dist/cli.js +62 -27
- package/examples/repository/packages/frontend/src/app/[locale]/page.tsx +2 -0
- package/examples/repository/packages/frontend/src/app/[locale]/starter-page.tsx +12 -8
- package/examples/repository/project.json +9 -0
- package/package.json +10 -9
- package/template/README.md +7 -0
- package/template/_env.example +5 -0
- package/template/biome.json +4 -1
- package/template/bun.lock +303 -301
- package/template/docs/ADMIN_MOCK.md +38 -0
- package/template/e2e/starter.spec.ts +2 -2
- package/template/package.json +17 -16
- package/template/packages/backend/package.json +6 -6
- package/template/packages/backend/src/cleanup.ts +4 -7
- package/template/packages/backend/src/index.ts +2 -1
- package/template/packages/backend/src/surface-manifest.test.ts +3 -3
- package/template/packages/backend/src/surface.ts +7 -6
- package/template/packages/backend/src/trusted-origins.test.ts +40 -0
- package/template/packages/backend/src/trusted-origins.ts +21 -0
- package/template/packages/config/package.json +3 -3
- package/template/packages/config/src/shutdown.ts +3 -4
- package/template/packages/config/src/variables.ts +21 -9
- package/template/packages/db/package.json +4 -4
- package/template/packages/frontend/next.config.ts +10 -1
- package/template/packages/frontend/package.json +29 -29
- package/template/packages/frontend/scripts/serve.ts +4 -4
- package/template/packages/frontend/src/app/[locale]/admin/[[...section]]/page.dev.tsx +21 -0
- package/template/packages/frontend/src/app/[locale]/page.tsx +2 -1
- package/template/packages/frontend/src/app/[locale]/starter-page.tsx +12 -8
- package/template/packages/frontend/src/app/api/[...path]/route.ts +2 -37
- package/template/packages/frontend/src/app/sitemap.ts +4 -1
- package/template/packages/frontend/src/components/ui/toaster.tsx +2 -2
- package/template/packages/frontend/src/env.ts +12 -3
- package/template/packages/frontend/src/features/admin/admin-data.ts +13 -0
- package/template/packages/frontend/src/features/admin/admin-sections.ts +78 -0
- package/template/packages/frontend/src/features/admin/admin-workspace.test.tsx +59 -0
- package/template/packages/frontend/src/features/admin/admin-workspace.tsx +127 -0
- package/template/packages/frontend/src/features/admin/fixture/admin-fixture.ts +35 -0
- package/template/packages/frontend/src/lib/api-proxy.test.ts +131 -0
- package/template/packages/frontend/src/lib/api-proxy.ts +89 -0
- package/template/packages/frontend/src/lib/query-client.test.ts +1 -1
- package/template/packages/frontend/src/lib/query-client.ts +2 -3
- package/template/packages/frontend/src/lib/seo/cache-by-origin.ts +2 -2
- package/template/packages/frontend/src/lib/seo/request-origin.ts +5 -5
- package/template/packages/frontend/src/proxy.ts +12 -1
- package/template/packages/shared/package.json +2 -2
- package/template/packages/shared/src/contracts/board.ts +1 -3
- package/template/packages/shared/src/contracts/system.ts +1 -3
- package/template/project.json +9 -0
- package/template/scripts/acceptance-database.test.ts +4 -4
- package/template/scripts/acceptance-database.ts +4 -6
- package/template/scripts/acceptance-local.ts +14 -9
- package/template/scripts/build-stamp.test.ts +3 -5
- package/template/scripts/build-stamp.ts +8 -11
- package/template/scripts/check-authored.test.ts +16 -1
- package/template/scripts/check-authored.ts +7 -6
- package/template/scripts/client-boundary.test.ts +26 -8
- package/template/scripts/client-boundary.ts +30 -27
- package/template/scripts/declaration.test.ts +5 -7
- package/template/scripts/declaration.ts +14 -19
- package/template/scripts/deployment-preflight.ts +2 -3
- package/template/scripts/dev.ts +6 -8
- package/template/scripts/fixture-boundary.test.ts +34 -0
- package/template/scripts/fixture-boundary.ts +25 -0
- package/template/scripts/guide-paths.ts +2 -4
- package/template/scripts/readiness.ts +4 -5
- package/template/scripts/release-steps.test.ts +2 -4
- package/template/scripts/release-steps.ts +2 -2
- package/template/scripts/runtime-smoke.test.ts +6 -7
- package/template/scripts/serve-mode.test.ts +2 -2
- package/template/scripts/service-surfaces-closed.ts +13 -0
- package/template/scripts/shutdown-budget.fixture.ts +3 -5
- package/template/scripts/shutdown-budget.test.ts +4 -8
- package/template/scripts/supervision-signal.test.ts +1 -1
- package/template/scripts/web-surface-smoke.ts +38 -8
- package/templates/agent/biome.json +4 -1
- package/templates/agent/package.json +11 -7
- package/templates/telegram-bot/biome.json +4 -1
- package/templates/telegram-bot/package.json +8 -4
|
@@ -2,9 +2,8 @@ import { cache } from 'react';
|
|
|
2
2
|
import { createQueryClientFactory } from 'stitchkit/react';
|
|
3
3
|
|
|
4
4
|
// One request-local server client and one browser singleton, both from the
|
|
5
|
-
// framework. The
|
|
6
|
-
//
|
|
7
|
-
// and it spelled the query retry as a plain `1`, which retries an unauthorized
|
|
5
|
+
// framework. The factory owns the dehydration policy, mutation rule and query
|
|
6
|
+
// retry. A plain retry count of `1` would retry an unauthorized
|
|
8
7
|
// or invalid request exactly as eagerly as a network blip. `apiErrorRetry` —
|
|
9
8
|
// the factory's default — retries what is worth retrying and nothing else.
|
|
10
9
|
// Project-specific cache configuration and mutation toasts belong in these
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* each answer once rather than once per request.
|
|
4
4
|
*
|
|
5
5
|
* This is the price of deriving the public origin from the request instead of
|
|
6
|
-
* the build: `robots.txt`, `sitemap.xml` and page metadata
|
|
7
|
-
*
|
|
6
|
+
* the build: `robots.txt`, `sitemap.xml` and page metadata depend on the served
|
|
7
|
+
* address. They are constants *per address*, and this is
|
|
8
8
|
* where that is spent — one render per address, not one per hit.
|
|
9
9
|
*
|
|
10
10
|
* Bounded and least-recently-used on purpose: part of every key comes from a
|
|
@@ -5,9 +5,9 @@ import { env } from '@/env';
|
|
|
5
5
|
* The public origin this response is being served on.
|
|
6
6
|
*
|
|
7
7
|
* Derived from the REQUEST, never from the build. An absolute address read at
|
|
8
|
-
* build time is frozen into the artifact
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* build time is frozen into the artifact: prerendering `robots.txt` and
|
|
9
|
+
* `sitemap.xml` with one origin would prevent that build from serving a second
|
|
10
|
+
* address correctly. Reading the request makes one artifact
|
|
11
11
|
* correct at every address it is ever routed to.
|
|
12
12
|
*
|
|
13
13
|
* **The request is not trusted on its own.** `x-forwarded-host` is set by
|
|
@@ -68,8 +68,8 @@ function isAllowedHost(host: string): boolean {
|
|
|
68
68
|
*
|
|
69
69
|
* Only two are possible, and anything else is a misconfigured proxy or a
|
|
70
70
|
* forgery — both of which must be seen, not normalised. Mapping every unknown
|
|
71
|
-
* value to `http`
|
|
72
|
-
*
|
|
71
|
+
* value to `http` would turn `ftp`, `javascript` and a truncated header into a
|
|
72
|
+
* plausible origin and put an unverified value into canonical URLs.
|
|
73
73
|
*
|
|
74
74
|
* A missing header is not a failure: a deployment reached directly has no
|
|
75
75
|
* forwarding layer, and `http` is then the truth.
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
import { type NextRequest, NextResponse } from 'next/server';
|
|
1
2
|
import createMiddleware from 'next-intl/middleware';
|
|
3
|
+
import { catalogueIsOpen } from './env';
|
|
2
4
|
import { routing } from './i18n/routing';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
const localized = createMiddleware(routing);
|
|
7
|
+
const CATALOGUE = new RegExp(`^/(?:${routing.locales.join('|')})/ui(?:/|$)`);
|
|
8
|
+
|
|
9
|
+
/** Decided at the door, per request, so one check covers every page of the catalogue. */
|
|
10
|
+
export default function proxy(request: NextRequest): NextResponse {
|
|
11
|
+
if (CATALOGUE.test(request.nextUrl.pathname) && !catalogueIsOpen()) {
|
|
12
|
+
return new NextResponse(null, { status: 404 });
|
|
13
|
+
}
|
|
14
|
+
return localized(request);
|
|
15
|
+
}
|
|
5
16
|
|
|
6
17
|
export const config = { matcher: ['/', '/(en|ru)/:path*'] };
|
|
@@ -2,9 +2,7 @@ import { createContractFactory } from 'stitchkit';
|
|
|
2
2
|
import { defineEvents } from 'stitchkit/live';
|
|
3
3
|
import { BoardChangedSchema, BoardSchema, PostNoteSchema } from '../schemas/board';
|
|
4
4
|
|
|
5
|
-
const { defineContract } = createContractFactory<'public'>(
|
|
6
|
-
toolExposure: 'explicit',
|
|
7
|
-
});
|
|
5
|
+
const { defineContract } = createContractFactory<'public'>();
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* The board: read it, add to it.
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { createContractFactory } from 'stitchkit';
|
|
2
2
|
import { SystemStatusSchema } from '../schemas/system';
|
|
3
3
|
|
|
4
|
-
const { defineContract } = createContractFactory<'public'>(
|
|
5
|
-
toolExposure: 'explicit',
|
|
6
|
-
});
|
|
4
|
+
const { defineContract } = createContractFactory<'public'>();
|
|
7
5
|
|
|
8
6
|
export const systemContract = defineContract(
|
|
9
7
|
{ prefix: 'system', scope: 'public' },
|
package/template/project.json
CHANGED
|
@@ -164,6 +164,15 @@
|
|
|
164
164
|
"shape": "url",
|
|
165
165
|
"required": false
|
|
166
166
|
},
|
|
167
|
+
{
|
|
168
|
+
"name": "SERVICE_SURFACES",
|
|
169
|
+
"shape": "enum",
|
|
170
|
+
"required": false,
|
|
171
|
+
"members": [
|
|
172
|
+
"open",
|
|
173
|
+
"closed"
|
|
174
|
+
]
|
|
175
|
+
},
|
|
167
176
|
{
|
|
168
177
|
"name": "TRUSTED_HOSTS",
|
|
169
178
|
"shape": "string",
|
|
@@ -14,8 +14,8 @@ describe('the acceptance gate writes only to a database of its own', () => {
|
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
test('an unset variable is refused with the line to paste', () => {
|
|
17
|
-
//
|
|
18
|
-
//
|
|
17
|
+
// Falling back to DATABASE_URL would let the gate write to the deployment's
|
|
18
|
+
// database, so an absent acceptance URL must be rejected.
|
|
19
19
|
expect(() => resolveAcceptanceDatabase({ DATABASE_URL: deployment })).toThrow(
|
|
20
20
|
/ACCEPTANCE_DATABASE_URL=postgresql:\/\/app:secret@127\.0\.0\.1:5432\/starter_acceptance/,
|
|
21
21
|
);
|
|
@@ -44,8 +44,8 @@ describe('the acceptance gate writes only to a database of its own', () => {
|
|
|
44
44
|
|
|
45
45
|
test('the same name on another hostname is refused, because a hostname proves nothing', () => {
|
|
46
46
|
// `localhost` and `127.0.0.1` are one server; so are two DNS names for the
|
|
47
|
-
// same PostgreSQL.
|
|
48
|
-
//
|
|
47
|
+
// same PostgreSQL. Comparing `host:port/name` cannot prove the databases
|
|
48
|
+
// are separate and could let a gate migrate the deployment's own.
|
|
49
49
|
expect(() =>
|
|
50
50
|
resolveAcceptanceDatabase({
|
|
51
51
|
DATABASE_URL: 'postgresql://app@localhost:5432/starter',
|
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
* The database the local acceptance gate is allowed to write to.
|
|
3
3
|
*
|
|
4
4
|
* `acceptance:local` creates and destroys its own deployment — its own PM2 home,
|
|
5
|
-
* its own ephemeral ports, its own host allowlist
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* wrote rows into whatever `.env` happened to name — including a production
|
|
10
|
-
* database, if that is what the machine was pointed at.
|
|
5
|
+
* its own ephemeral ports, its own host allowlist and its own database. Runtime
|
|
6
|
+
* gates WRITE: the repository example posts `/api/repository/refresh`, which
|
|
7
|
+
* upserts. Inheriting `DATABASE_URL` would let a verification run write to the
|
|
8
|
+
* deployment named by `.env`, including production.
|
|
11
9
|
*
|
|
12
10
|
* Fail-closed on purpose. An acceptance database that is merely *probably*
|
|
13
11
|
* separate is the same defect one edit later, so an unset variable and one that
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* The runtime gates, against a deployment this script owns.
|
|
3
3
|
*
|
|
4
4
|
* `runtime:smoke` and `e2e` check a RUNNING deployment, so a gate list has to
|
|
5
|
-
* bring one up.
|
|
5
|
+
* bring one up. A gate must not do so through
|
|
6
6
|
* `pm2:prod`: it applies the declared migrations and reloads the developer's
|
|
7
|
-
* own PM2 daemon,
|
|
7
|
+
* own PM2 daemon, turning verification into a deployment.
|
|
8
8
|
* A gate may not mutate a deployment; it may only create and destroy its own.
|
|
9
9
|
*
|
|
10
10
|
* So everything here is this run's and nothing else's: its own `PM2_HOME`, its
|
|
@@ -12,12 +12,10 @@
|
|
|
12
12
|
* stop that names the roles the declaration declares rather than deleting
|
|
13
13
|
* whatever the daemon happened to hold.
|
|
14
14
|
*
|
|
15
|
-
* The
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* deployment's URL is not in the child environment at all, so no role here can
|
|
20
|
-
* reach it even by asking.
|
|
15
|
+
* The gate writes: the repository example's smoke posts a refresh that upserts.
|
|
16
|
+
* It requires `ACCEPTANCE_DATABASE_URL`, distinct from the deployment's own
|
|
17
|
+
* database. The deployment's URL is excluded from the child environment so no
|
|
18
|
+
* role here can reach it.
|
|
21
19
|
*/
|
|
22
20
|
import { mkdtemp, rm } from 'node:fs/promises';
|
|
23
21
|
import { tmpdir } from 'node:os';
|
|
@@ -78,6 +76,9 @@ for (const [name, value] of Object.entries(base)) {
|
|
|
78
76
|
delete environment.ACCEPTANCE_DATABASE_URL;
|
|
79
77
|
Object.assign(environment, {
|
|
80
78
|
NODE_ENV: 'production',
|
|
79
|
+
// The gates read the catalogue and the OpenAPI document; the closed default is
|
|
80
|
+
// proved after them, on the same artifact.
|
|
81
|
+
SERVICE_SURFACES: 'open',
|
|
81
82
|
// The one address every role, every migration and every gate here will see.
|
|
82
83
|
DATABASE_URL: acceptanceDatabaseUrl,
|
|
83
84
|
PM2_HOME: home,
|
|
@@ -106,8 +107,12 @@ try {
|
|
|
106
107
|
await awaitRolesAnswering(declaredRoleReadiness(appDeclaration, environment));
|
|
107
108
|
await run(['bun', 'run', 'runtime:smoke'], environment);
|
|
108
109
|
await run(['bun', 'run', 'e2e'], environment);
|
|
110
|
+
const closed = { ...environment, SERVICE_SURFACES: 'closed' };
|
|
111
|
+
await run(['pm2', 'restart', 'ecosystem.config.cjs', '--update-env'], closed);
|
|
112
|
+
await awaitRolesAnswering(declaredRoleReadiness(appDeclaration, closed));
|
|
113
|
+
await run(['bun', 'run', 'surfaces:closed'], closed);
|
|
109
114
|
console.log(
|
|
110
|
-
`Local acceptance passed: ${appDeclaration.roles.map((role) => role.name).join(' and ')} answered on ephemeral ports,
|
|
115
|
+
`Local acceptance passed: ${appDeclaration.roles.map((role) => role.name).join(' and ')} answered on ephemeral ports, both runtime gates are green, and the service surfaces close`,
|
|
111
116
|
);
|
|
112
117
|
} catch (error) {
|
|
113
118
|
failure = { error };
|
|
@@ -73,11 +73,9 @@ describe('a release starts this source, not a previous one', () => {
|
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
test('a Markdown file counts, because a project may import one', async () => {
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
// called anything. A digest that answers "fresh" about a stale build is the
|
|
80
|
-
// failure the stamp exists to prevent.
|
|
76
|
+
// Markdown, tests and directories named `generated` can all be source:
|
|
77
|
+
// MDX is an import, and a checked-in directory may be called anything.
|
|
78
|
+
// Excluding them could mark a stale build as fresh.
|
|
81
79
|
const root = await tree({
|
|
82
80
|
'src/index.ts': 'export const a = 1;\n',
|
|
83
81
|
'content/post.md': 'before\n',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* What the artifact was built FROM, so a release can tell whether it still is.
|
|
3
3
|
*
|
|
4
|
-
* `assertBuildArtifacts()`
|
|
4
|
+
* `assertBuildArtifacts()` checks that the declared paths exist. Existence is
|
|
5
5
|
* not freshness: `pm2:prod` run without a build applies the migrations this
|
|
6
6
|
* source declares and then starts the `dist` and `.next` of an earlier one. The
|
|
7
7
|
* schema moves ahead of the code, which is the worst direction — rolling the
|
|
@@ -14,15 +14,12 @@
|
|
|
14
14
|
* none.
|
|
15
15
|
*
|
|
16
16
|
* **Everything is source until something says otherwise, and the something is
|
|
17
|
-
* named.**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* it happened to call `generated`, would change its content, keep its digest,
|
|
22
|
-
* and be told its old artifact was current. A digest that answers "fresh" about
|
|
23
|
-
* a stale build is worse than no digest.
|
|
17
|
+
* named.** A Markdown file, a test and a directory named `generated` can affect
|
|
18
|
+
* a build: a project can import MDX or keep authored code in any directory.
|
|
19
|
+
* Excluding source by kind could leave its digest unchanged after an edit and
|
|
20
|
+
* incorrectly mark a stale artifact as current.
|
|
24
21
|
*
|
|
25
|
-
*
|
|
22
|
+
* The exclusions are exactly three kinds, each named rather than guessed:
|
|
26
23
|
*
|
|
27
24
|
* - **The build's own OUTPUTS**, read from `build.artifacts` in the project
|
|
28
25
|
* declaration. One source of truth: a project that adds an artifact is
|
|
@@ -38,7 +35,7 @@
|
|
|
38
35
|
* edited its own environment: the same gate, crying wolf in the other
|
|
39
36
|
* direction.
|
|
40
37
|
*
|
|
41
|
-
* The cost is that editing a README or a test
|
|
38
|
+
* The cost is that editing a README or a test requires a rebuild before a
|
|
42
39
|
* release. That is the safe direction: the gate can be wrong about "stale", and
|
|
43
40
|
* must never be wrong about "fresh".
|
|
44
41
|
*/
|
|
@@ -141,7 +138,7 @@ export function writeBuildStamp(
|
|
|
141
138
|
/**
|
|
142
139
|
* Refuse an artifact that is not this source's.
|
|
143
140
|
*
|
|
144
|
-
* A missing stamp is
|
|
141
|
+
* A missing stamp is rejected: it means the artifact was
|
|
145
142
|
* produced by something other than this build, and "we cannot tell" is not a
|
|
146
143
|
* reason to start it.
|
|
147
144
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import databasePackage from '../packages/db/package.json';
|
|
2
3
|
import { inspect } from './check-authored';
|
|
3
4
|
|
|
4
5
|
describe('check:authored — assertions', () => {
|
|
@@ -25,7 +26,7 @@ describe('check:authored — assertions', () => {
|
|
|
25
26
|
expect(finding).toContain('schema at the boundary');
|
|
26
27
|
});
|
|
27
28
|
|
|
28
|
-
test('
|
|
29
|
+
test('rejects angle-bracket assertions and explicit any separately', () => {
|
|
29
30
|
expect(inspect('scripts/x.ts', 'const a = <Foo>bar;\n')).toHaveLength(1);
|
|
30
31
|
const anyFindings = inspect('scripts/y.ts', 'function f(a: any) { return a; }\n');
|
|
31
32
|
expect(anyFindings).toHaveLength(1);
|
|
@@ -41,3 +42,17 @@ describe('check:authored — assertions', () => {
|
|
|
41
42
|
expect(findings[0]).toContain('scripts/mixed.ts:2');
|
|
42
43
|
});
|
|
43
44
|
});
|
|
45
|
+
|
|
46
|
+
test('the manifest names the database dependency forbidden to client code', () => {
|
|
47
|
+
for (const dependency of [databasePackage.name, `${databasePackage.name}/client`]) {
|
|
48
|
+
expect(
|
|
49
|
+
inspect('packages/shared/src/index.ts', `export * from '${dependency}';\n`),
|
|
50
|
+
).toHaveLength(1);
|
|
51
|
+
}
|
|
52
|
+
expect(
|
|
53
|
+
inspect(
|
|
54
|
+
'packages/shared/src/index.ts',
|
|
55
|
+
`export * from '${databasePackage.name}-external';\n`,
|
|
56
|
+
),
|
|
57
|
+
).toEqual([]);
|
|
58
|
+
});
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { readdir, readFile } from 'node:fs/promises';
|
|
2
2
|
import { extname, join } from 'node:path';
|
|
3
3
|
import { parseSync, Visitor } from 'oxc-parser';
|
|
4
|
+
import databasePackage from '../packages/db/package.json';
|
|
4
5
|
|
|
5
6
|
const roots = ['packages', 'scripts', 'e2e'];
|
|
6
7
|
const rootFiles = ['playwright.config.ts', 'ecosystem.config.cjs', 'ecosystem.dev.config.cjs'];
|
|
7
8
|
const processEnvMarker = ['process', 'env'].join('.');
|
|
8
9
|
const replacedThemePackage = ['next', 'themes'].join('-');
|
|
9
10
|
const generatedDirectories = new Set(['.git', '.next', 'dist', 'node_modules']);
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// are no longer environment boundaries, and this list is smaller by two.
|
|
11
|
+
// Roles read their own validated bindings; supervision passes no environment
|
|
12
|
+
// values in argv. Only runtime and tooling schemas are environment boundaries.
|
|
13
13
|
const processEnvBoundaries = new Set([
|
|
14
14
|
'packages/frontend/src/env.ts',
|
|
15
15
|
'packages/config/src/server.ts',
|
|
@@ -25,8 +25,8 @@ function lineAt(source: string, offset: number): number {
|
|
|
25
25
|
*
|
|
26
26
|
* The gate refuses a cast because a cast can LAUNDER a type — claim something the value has not
|
|
27
27
|
* been shown to be. A const-assertion cannot: it only narrows, it introduces no name, and it
|
|
28
|
-
* cannot widen.
|
|
29
|
-
*
|
|
28
|
+
* cannot widen. Treating it as a type assertion would report a narrowing as an
|
|
29
|
+
* unproven type claim.
|
|
30
30
|
*
|
|
31
31
|
* `const` is a reserved word, so no type can be named `const` — the check has no false positive
|
|
32
32
|
* of its own.
|
|
@@ -72,7 +72,8 @@ export function inspect(path: string, source: string): string[] {
|
|
|
72
72
|
for (const specifier of source.matchAll(/from\s+['"]([^'"]+)['"]/g)) {
|
|
73
73
|
const dependency = specifier[1];
|
|
74
74
|
if (
|
|
75
|
-
dependency ===
|
|
75
|
+
dependency === databasePackage.name ||
|
|
76
|
+
dependency?.startsWith(`${databasePackage.name}/`) ||
|
|
76
77
|
dependency?.startsWith('@prisma/') ||
|
|
77
78
|
(path.startsWith('packages/shared/') && dependency?.startsWith('node:')) ||
|
|
78
79
|
(path.startsWith('packages/shared/') && dependency === 'bun')
|
|
@@ -31,9 +31,8 @@ describe('the declaration stays out of the browser', () => {
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
describe('the scanner fails however the import is written', () => {
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
// two other ways it can come back.
|
|
34
|
+
// Package, barrel and relative imports can reach the same declaration.
|
|
35
|
+
// Every spelling must be checked against the resolved file.
|
|
37
36
|
const created: string[] = [];
|
|
38
37
|
afterAll(() => {
|
|
39
38
|
for (const path of created) rmSync(path, { recursive: true, force: true });
|
|
@@ -50,7 +49,7 @@ describe('the scanner fails however the import is written', () => {
|
|
|
50
49
|
write(
|
|
51
50
|
'packages/config/package.json',
|
|
52
51
|
JSON.stringify({
|
|
53
|
-
name: '@
|
|
52
|
+
name: '@demo-shop/config',
|
|
54
53
|
exports: { './declaration': './src/declaration.ts' },
|
|
55
54
|
}),
|
|
56
55
|
);
|
|
@@ -67,14 +66,13 @@ describe('the scanner fails however the import is written', () => {
|
|
|
67
66
|
const scan = fixture({
|
|
68
67
|
'packages/frontend/src/page.tsx':
|
|
69
68
|
"'use client';\nimport { appDeclaration } from './lib';\nexport const value = appDeclaration;\n",
|
|
70
|
-
'packages/frontend/src/lib/index.ts': "export * from '@
|
|
69
|
+
'packages/frontend/src/lib/index.ts': "export * from '@demo-shop/config/declaration';\n",
|
|
71
70
|
});
|
|
72
71
|
expect(findDeclarationLeaks(scan)).toHaveLength(1);
|
|
73
72
|
});
|
|
74
73
|
|
|
75
74
|
test('a relative path into the config package is a leak', () => {
|
|
76
|
-
//
|
|
77
|
-
// string sees nothing at all.
|
|
75
|
+
// A relative import bypasses any check on the package's spelling.
|
|
78
76
|
const scan = fixture({
|
|
79
77
|
'packages/frontend/src/page.tsx':
|
|
80
78
|
"'use client';\nimport { appDeclaration } from '../../config/src/declaration';\nexport const value = appDeclaration;\n",
|
|
@@ -85,7 +83,7 @@ describe('the scanner fails however the import is written', () => {
|
|
|
85
83
|
test('a double-quoted import is a leak too', () => {
|
|
86
84
|
const scan = fixture({
|
|
87
85
|
'packages/frontend/src/page.tsx':
|
|
88
|
-
'\'use client\';\nimport { appDeclaration } from "@
|
|
86
|
+
'\'use client\';\nimport { appDeclaration } from "@demo-shop/config/declaration";\nexport const value = appDeclaration;\n',
|
|
89
87
|
});
|
|
90
88
|
expect(findDeclarationLeaks(scan)).toHaveLength(1);
|
|
91
89
|
});
|
|
@@ -101,6 +99,26 @@ describe('the scanner fails however the import is written', () => {
|
|
|
101
99
|
expect(findDeclarationLeaks(scan)).toEqual([]);
|
|
102
100
|
});
|
|
103
101
|
|
|
102
|
+
test('a matching subpath under a foreign scope is not a workspace import', () => {
|
|
103
|
+
const scan = fixture({
|
|
104
|
+
'packages/frontend/src/page.tsx':
|
|
105
|
+
"'use client';\nimport { appDeclaration } from '@foreign/config/declaration';\nexport const value = appDeclaration;\n",
|
|
106
|
+
});
|
|
107
|
+
expect(findDeclarationLeaks(scan)).toEqual([]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('the manifest name resolves a package whose directory has a different name', () => {
|
|
111
|
+
const scan = fixture({
|
|
112
|
+
'packages/config/package.json': JSON.stringify({
|
|
113
|
+
name: 'shop-configuration',
|
|
114
|
+
exports: { './declaration': './src/declaration.ts' },
|
|
115
|
+
}),
|
|
116
|
+
'packages/frontend/src/page.tsx':
|
|
117
|
+
"'use client';\nimport { appDeclaration } from 'shop-configuration/declaration';\nexport const value = appDeclaration;\n",
|
|
118
|
+
});
|
|
119
|
+
expect(findDeclarationLeaks(scan)).toHaveLength(1);
|
|
120
|
+
});
|
|
121
|
+
|
|
104
122
|
test('both quote styles and side-effect imports are read', () => {
|
|
105
123
|
expect(
|
|
106
124
|
specifiers(
|
|
@@ -9,15 +9,12 @@ import { dirname, join, resolve } from 'node:path';
|
|
|
9
9
|
* artifact and migration paths, and the name of every environment variable a
|
|
10
10
|
* deployment supplies. One `import` from a module a client component happens to
|
|
11
11
|
* use puts all of it, plus the Zod schema that parses it, into the browser
|
|
12
|
-
* bundle.
|
|
13
|
-
*
|
|
14
|
-
* the graph, only the file that imports directly.
|
|
12
|
+
* bundle. `app-identity.generated.ts` exposes only the public identity; the
|
|
13
|
+
* scanner follows the whole graph to keep the full declaration server-only.
|
|
15
14
|
*
|
|
16
|
-
* The check
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* a relative path into the config package. Both end at the same file, so that
|
|
20
|
-
* is what this follows.
|
|
15
|
+
* The check follows resolved files: a direct package import, a barrel and a
|
|
16
|
+
* relative path can all reach the same declaration. Workspace names come from
|
|
17
|
+
* package manifests so the check also follows a renamed package.
|
|
21
18
|
*/
|
|
22
19
|
export interface ClientBoundaryScan {
|
|
23
20
|
/** Repository root — where `packages/<name>` lives. */
|
|
@@ -77,25 +74,32 @@ function firstExistingFile(base: string): string | undefined {
|
|
|
77
74
|
return undefined;
|
|
78
75
|
}
|
|
79
76
|
|
|
80
|
-
/** `@app/config/declaration` → the file its package's `exports` map names. */
|
|
81
77
|
function resolveWorkspace(scan: ClientBoundaryScan, specifier: string): string | undefined {
|
|
82
|
-
const
|
|
83
|
-
if (
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
typeof manifest
|
|
91
|
-
|
|
78
|
+
const packagesRoot = join(scan.root, 'packages');
|
|
79
|
+
if (!existsSync(packagesRoot)) return undefined;
|
|
80
|
+
for (const entry of readdirSync(packagesRoot, { withFileTypes: true })) {
|
|
81
|
+
if (!entry.isDirectory()) continue;
|
|
82
|
+
const packageRoot = join(packagesRoot, entry.name);
|
|
83
|
+
const manifestPath = join(packageRoot, 'package.json');
|
|
84
|
+
if (!existsSync(manifestPath)) continue;
|
|
85
|
+
const manifest: unknown = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
86
|
+
if (typeof manifest !== 'object' || manifest === null) continue;
|
|
87
|
+
const name: unknown = Reflect.get(manifest, 'name');
|
|
88
|
+
if (typeof name !== 'string') continue;
|
|
89
|
+
if (specifier !== name && !specifier.startsWith(`${name}/`)) continue;
|
|
90
|
+
const subpath = specifier === name ? '.' : `.${specifier.slice(name.length)}`;
|
|
91
|
+
const exportsField: unknown = Reflect.get(manifest, 'exports');
|
|
92
|
+
const target: unknown =
|
|
93
|
+
typeof exportsField === 'string' && subpath === '.'
|
|
94
|
+
? exportsField
|
|
95
|
+
: typeof exportsField === 'object' && exportsField !== null
|
|
96
|
+
? Reflect.get(exportsField, subpath)
|
|
97
|
+
: undefined;
|
|
98
|
+
return typeof target === 'string'
|
|
99
|
+
? firstExistingFile(resolve(packageRoot, target))
|
|
92
100
|
: undefined;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
? Reflect.get(exportsField, subpath)
|
|
96
|
-
: undefined;
|
|
97
|
-
if (typeof target !== 'string') return undefined;
|
|
98
|
-
return firstExistingFile(resolve(packageRoot, target));
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
export function resolveSpecifier(
|
|
@@ -103,13 +107,12 @@ export function resolveSpecifier(
|
|
|
103
107
|
from: string,
|
|
104
108
|
specifier: string,
|
|
105
109
|
): string | undefined {
|
|
106
|
-
if (specifier.startsWith('@app/')) return resolveWorkspace(scan, specifier);
|
|
107
110
|
const base = specifier.startsWith('@/')
|
|
108
111
|
? join(scan.frontendSrc, specifier.slice(2))
|
|
109
112
|
: specifier.startsWith('.')
|
|
110
113
|
? resolve(dirname(from), specifier)
|
|
111
114
|
: undefined;
|
|
112
|
-
return base ? firstExistingFile(base) :
|
|
115
|
+
return base ? firstExistingFile(base) : resolveWorkspace(scan, specifier);
|
|
113
116
|
}
|
|
114
117
|
|
|
115
118
|
/** The import chain from a client entry to the declaration, if one exists. */
|
|
@@ -148,7 +148,7 @@ describe('the drain floor has one home', () => {
|
|
|
148
148
|
test('the API role reads its grace period from the declaration', () => {
|
|
149
149
|
const source = read('packages/backend/src/index.ts');
|
|
150
150
|
// A literal here would be a second number able to disagree with the one a
|
|
151
|
-
// supervisor reads,
|
|
151
|
+
// supervisor reads, potentially allowing a 15s kill timeout for a 30s floor.
|
|
152
152
|
expect(source).toContain('gracePeriodMs: apiRole.drainFloorMs');
|
|
153
153
|
expect(source).not.toMatch(/gracePeriodMs:\s*\d/);
|
|
154
154
|
});
|
|
@@ -157,8 +157,8 @@ describe('the drain floor has one home', () => {
|
|
|
157
157
|
describe('an argument survives the generator intact', () => {
|
|
158
158
|
test('a space and a quote reach the supervision file unmangled', () => {
|
|
159
159
|
// The reason commands are argv and the generator serialises rather than
|
|
160
|
-
// concatenates: `split(' ')`
|
|
161
|
-
// `'${part}'` by hand
|
|
160
|
+
// concatenates: `split(' ')` destroys quoted arguments, and building
|
|
161
|
+
// `'${part}'` by hand emits invalid JavaScript for an argument
|
|
162
162
|
// containing a quote.
|
|
163
163
|
const awkward = {
|
|
164
164
|
...appDeclaration,
|
|
@@ -185,10 +185,8 @@ describe('an argument survives the generator intact', () => {
|
|
|
185
185
|
});
|
|
186
186
|
|
|
187
187
|
describe('the guidance a next agent reads names the keys that exist', () => {
|
|
188
|
-
//
|
|
189
|
-
//
|
|
190
|
-
// after the key became `env.variables`, which is worse than a stale comment:
|
|
191
|
-
// the agent goes looking for something that is not there.
|
|
188
|
+
// Guidance names current declaration keys so an agent can follow it without
|
|
189
|
+
// looking for fields that the declaration does not contain.
|
|
192
190
|
const guidance = ['AGENTS.md', 'README.md'];
|
|
193
191
|
|
|
194
192
|
for (const file of guidance) {
|