@duct-sdk/sdk 0.1.7 → 0.1.13
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/README.md +3 -3
- package/dist/adapters/next/index.d.ts +1 -1
- package/dist/adapters/next/shell.d.ts +55 -1
- package/dist/adapters/next/shell.js +100 -17
- package/dist/adapters/react/index.d.ts +5 -1
- package/dist/adapters/react/index.js +2 -0
- package/dist/adapters/react/shell.d.ts +61 -1
- package/dist/adapters/react/shell.js +110 -16
- package/dist/adapters/react/use-page-context.d.ts +32 -0
- package/dist/adapters/react/use-page-context.js +99 -0
- package/dist/cli/agents/api-agent.d.ts +9 -3
- package/dist/cli/agents/api-agent.js +163 -69
- package/dist/cli/agents/auth-agent.d.ts +2 -1
- package/dist/cli/agents/auth-agent.js +38 -24
- package/dist/cli/agents/deeplink-agent.js +71 -49
- package/dist/cli/agents/llm.d.ts +1 -1
- package/dist/cli/agents/llm.js +48 -15
- package/dist/cli/agents/pipeline.d.ts +16 -0
- package/dist/cli/agents/pipeline.js +18 -5
- package/dist/cli/agents/reviewer-agent.js +46 -46
- package/dist/cli/agents/route-agent.d.ts +3 -1
- package/dist/cli/agents/route-agent.js +70 -44
- package/dist/cli/detectors/topology.d.ts +26 -0
- package/dist/cli/detectors/topology.js +310 -0
- package/dist/cli/drift.d.ts +11 -0
- package/dist/cli/drift.js +147 -2
- package/dist/cli/fs/sandbox.d.ts +2 -0
- package/dist/cli/fs/sandbox.js +22 -8
- package/dist/cli/generators/config.d.ts +13 -0
- package/dist/cli/generators/config.js +200 -50
- package/dist/cli/generators/receiver.js +728 -601
- package/dist/cli/generators/shell.js +532 -490
- package/dist/cli/index.js +502 -19
- package/dist/cli/init.d.ts +11 -1
- package/dist/cli/init.js +367 -31
- package/dist/cli/processors/postprocess.d.ts +22 -0
- package/dist/cli/processors/postprocess.js +107 -0
- package/dist/cli/processors/prisma-parser.d.ts +35 -0
- package/dist/cli/processors/prisma-parser.js +117 -0
- package/dist/cli/processors/zod-extractor.d.ts +27 -0
- package/dist/cli/processors/zod-extractor.js +153 -0
- package/dist/cli/prompts/review.d.ts +32 -0
- package/dist/cli/prompts/review.js +240 -0
- package/dist/cli/selectors/index.d.ts +22 -0
- package/dist/cli/selectors/index.js +274 -0
- package/dist/core/config.d.ts +18 -18
- package/dist/core/endpoints.d.ts +1 -0
- package/dist/core/endpoints.js +4 -0
- package/dist/core/introspect.d.ts +47 -0
- package/dist/core/introspect.js +94 -0
- package/dist/core/status.d.ts +39 -0
- package/dist/core/status.js +40 -0
- package/dist/core/token.d.ts +36 -1
- package/dist/core/token.js +43 -2
- package/dist/core/types.d.ts +114 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.js +6 -1
- package/package.json +84 -84
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AuthStrategy } from "../../core/types.js";
|
|
2
|
+
import type { FrameworkName } from "./framework.js";
|
|
3
|
+
export interface TopoResult {
|
|
4
|
+
/** Primary frontend/fullstack framework */
|
|
5
|
+
framework: FrameworkName;
|
|
6
|
+
/** Primary language detected at root */
|
|
7
|
+
language: "typescript" | "javascript" | "python" | "ruby" | "go" | "unknown";
|
|
8
|
+
/** Relative path to the route directory (e.g. "app", "pages", "src/routes") */
|
|
9
|
+
routeRoot: string;
|
|
10
|
+
/** Relative path to the API directory (e.g. "app/api", "pages/api", "routes") */
|
|
11
|
+
apiRoot: string;
|
|
12
|
+
/** Auth library detected from deps / config files */
|
|
13
|
+
authLib: AuthStrategy;
|
|
14
|
+
/** Whether this looks like a monorepo (multiple package.json dirs) */
|
|
15
|
+
isMonorepo: boolean;
|
|
16
|
+
/** Relative paths to all subdirectory package roots (monorepo workspaces) */
|
|
17
|
+
packageDirs: string[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Topology Discovery.
|
|
21
|
+
*
|
|
22
|
+
* Reads only package.json / pyproject.toml / go.mod, root config files,
|
|
23
|
+
* and top-level directory names. No LLM, no deep reads.
|
|
24
|
+
* Must run before any agent or selector.
|
|
25
|
+
*/
|
|
26
|
+
export declare function discoverTopology(projectRoot: string): Promise<TopoResult>;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Topology Discovery
|
|
3
|
+
// Reads package.json / pyproject.toml / go.mod, root config files,
|
|
4
|
+
// and top-level directory names only. No LLM, no deep reads.
|
|
5
|
+
// Must run before any agent or file selector.
|
|
6
|
+
// ============================================================
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as fs from "fs/promises";
|
|
9
|
+
// ----------------------------------------------------------------
|
|
10
|
+
// Internal helpers
|
|
11
|
+
// ----------------------------------------------------------------
|
|
12
|
+
async function fileExists(filePath) {
|
|
13
|
+
try {
|
|
14
|
+
await fs.access(filePath);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function dirExists(dirPath) {
|
|
22
|
+
try {
|
|
23
|
+
const stat = await fs.stat(dirPath);
|
|
24
|
+
return stat.isDirectory();
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function readJsonFile(filePath) {
|
|
31
|
+
try {
|
|
32
|
+
const raw = await fs.readFile(filePath, "utf-8");
|
|
33
|
+
return JSON.parse(raw);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** List only first-level entries under a directory. Safe — never recurses. */
|
|
40
|
+
async function listTopLevel(dir) {
|
|
41
|
+
try {
|
|
42
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
43
|
+
return entries.map((e) => ({ name: e.name, isDir: e.isDirectory() }));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const IGNORED_DIRS = new Set([
|
|
50
|
+
"node_modules",
|
|
51
|
+
".git",
|
|
52
|
+
".next",
|
|
53
|
+
"dist",
|
|
54
|
+
"build",
|
|
55
|
+
"out",
|
|
56
|
+
".turbo",
|
|
57
|
+
".vercel",
|
|
58
|
+
".cache",
|
|
59
|
+
"coverage",
|
|
60
|
+
"tmp",
|
|
61
|
+
"temp",
|
|
62
|
+
"__pycache__",
|
|
63
|
+
".venv",
|
|
64
|
+
"venv",
|
|
65
|
+
".mypy_cache",
|
|
66
|
+
]);
|
|
67
|
+
function hasDep(pkg, name) {
|
|
68
|
+
return !!(pkg.dependencies?.[name] ?? pkg.devDependencies?.[name]);
|
|
69
|
+
}
|
|
70
|
+
function detectFrameworkFromPkg(pkg) {
|
|
71
|
+
if (hasDep(pkg, "next")) {
|
|
72
|
+
// App Router vs Pages Router determined later (needs dir check)
|
|
73
|
+
return "next-app"; // default; refined in discoverTopology
|
|
74
|
+
}
|
|
75
|
+
if (hasDep(pkg, "@remix-run/react") || hasDep(pkg, "remix"))
|
|
76
|
+
return "remix";
|
|
77
|
+
if (hasDep(pkg, "@sveltejs/kit"))
|
|
78
|
+
return "sveltekit";
|
|
79
|
+
if (hasDep(pkg, "nuxt") || hasDep(pkg, "@nuxt/kit"))
|
|
80
|
+
return "nuxt";
|
|
81
|
+
if (hasDep(pkg, "vue") && !hasDep(pkg, "nuxt"))
|
|
82
|
+
return "vue3";
|
|
83
|
+
if (hasDep(pkg, "vite") && hasDep(pkg, "react"))
|
|
84
|
+
return "vite-react";
|
|
85
|
+
return "unknown";
|
|
86
|
+
}
|
|
87
|
+
// ----------------------------------------------------------------
|
|
88
|
+
// Auth lib detection from package.json
|
|
89
|
+
// ----------------------------------------------------------------
|
|
90
|
+
function detectAuthFromPkg(pkg) {
|
|
91
|
+
if (hasDep(pkg, "next-auth") || hasDep(pkg, "@auth/core"))
|
|
92
|
+
return "nextauth";
|
|
93
|
+
if (hasDep(pkg, "@clerk/nextjs") ||
|
|
94
|
+
hasDep(pkg, "@clerk/clerk-sdk-node") ||
|
|
95
|
+
hasDep(pkg, "@clerk/backend"))
|
|
96
|
+
return "clerk";
|
|
97
|
+
if (hasDep(pkg, "@auth0/nextjs-auth0") ||
|
|
98
|
+
hasDep(pkg, "auth0") ||
|
|
99
|
+
hasDep(pkg, "@auth0/auth0-react"))
|
|
100
|
+
return "auth0";
|
|
101
|
+
if (hasDep(pkg, "iron-session") ||
|
|
102
|
+
hasDep(pkg, "cookie-session") ||
|
|
103
|
+
hasDep(pkg, "express-session"))
|
|
104
|
+
return "session-cookie";
|
|
105
|
+
if (hasDep(pkg, "jsonwebtoken") || hasDep(pkg, "jose"))
|
|
106
|
+
return "custom-jwt";
|
|
107
|
+
return "unknown";
|
|
108
|
+
}
|
|
109
|
+
// ----------------------------------------------------------------
|
|
110
|
+
// Route root and API root inference
|
|
111
|
+
// ----------------------------------------------------------------
|
|
112
|
+
async function inferRoots(projectRoot, framework, topLevel) {
|
|
113
|
+
const dirs = new Set(topLevel.filter((e) => e.isDir).map((e) => e.name));
|
|
114
|
+
// Next.js — refine app vs pages
|
|
115
|
+
if (framework === "next-app" || framework === "next-pages") {
|
|
116
|
+
const hasApp = dirs.has("app") ||
|
|
117
|
+
(await dirExists(path.join(projectRoot, "src", "app")));
|
|
118
|
+
const hasPages = dirs.has("pages") ||
|
|
119
|
+
(await dirExists(path.join(projectRoot, "src", "pages")));
|
|
120
|
+
if (hasApp && hasPages) {
|
|
121
|
+
// Both exist — prefer App Router
|
|
122
|
+
return {
|
|
123
|
+
routeRoot: dirs.has("app") ? "app" : "src/app",
|
|
124
|
+
apiRoot: dirs.has("app") ? "app/api" : "src/app/api",
|
|
125
|
+
refinedFramework: "next-app",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (hasApp) {
|
|
129
|
+
return {
|
|
130
|
+
routeRoot: dirs.has("app") ? "app" : "src/app",
|
|
131
|
+
apiRoot: dirs.has("app") ? "app/api" : "src/app/api",
|
|
132
|
+
refinedFramework: "next-app",
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (hasPages) {
|
|
136
|
+
return {
|
|
137
|
+
routeRoot: dirs.has("pages") ? "pages" : "src/pages",
|
|
138
|
+
apiRoot: dirs.has("pages") ? "pages/api" : "src/pages/api",
|
|
139
|
+
refinedFramework: "next-pages",
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// Default
|
|
143
|
+
return { routeRoot: "app", apiRoot: "app/api", refinedFramework: "next-app" };
|
|
144
|
+
}
|
|
145
|
+
if (framework === "remix") {
|
|
146
|
+
return { routeRoot: "app/routes", apiRoot: "app/routes", refinedFramework: "remix" };
|
|
147
|
+
}
|
|
148
|
+
if (framework === "sveltekit") {
|
|
149
|
+
return {
|
|
150
|
+
routeRoot: "src/routes",
|
|
151
|
+
apiRoot: "src/routes/api",
|
|
152
|
+
refinedFramework: "sveltekit",
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (framework === "nuxt") {
|
|
156
|
+
return {
|
|
157
|
+
routeRoot: "pages",
|
|
158
|
+
apiRoot: dirs.has("server") ? "server/api" : "api",
|
|
159
|
+
refinedFramework: "nuxt",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// Generic / unknown — best-guess from top-level dirs
|
|
163
|
+
const routeRoot = dirs.has("routes")
|
|
164
|
+
? "routes"
|
|
165
|
+
: dirs.has("pages")
|
|
166
|
+
? "pages"
|
|
167
|
+
: dirs.has("views")
|
|
168
|
+
? "views"
|
|
169
|
+
: "src";
|
|
170
|
+
const apiRoot = dirs.has("api")
|
|
171
|
+
? "api"
|
|
172
|
+
: dirs.has("routes")
|
|
173
|
+
? "routes"
|
|
174
|
+
: dirs.has("controllers")
|
|
175
|
+
? "controllers"
|
|
176
|
+
: dirs.has("handlers")
|
|
177
|
+
? "handlers"
|
|
178
|
+
: "src";
|
|
179
|
+
return { routeRoot, apiRoot, refinedFramework: framework };
|
|
180
|
+
}
|
|
181
|
+
// ----------------------------------------------------------------
|
|
182
|
+
// Language detection
|
|
183
|
+
// ----------------------------------------------------------------
|
|
184
|
+
async function detectLanguage(_projectRoot, topLevel) {
|
|
185
|
+
const files = topLevel.filter((e) => !e.isDir).map((e) => e.name);
|
|
186
|
+
if (files.includes("pyproject.toml") || files.includes("setup.py") || files.includes("requirements.txt")) {
|
|
187
|
+
return "python";
|
|
188
|
+
}
|
|
189
|
+
if (files.includes("go.mod"))
|
|
190
|
+
return "go";
|
|
191
|
+
if (files.includes("Gemfile"))
|
|
192
|
+
return "ruby";
|
|
193
|
+
// TypeScript vs JavaScript — check for tsconfig or .ts files at root
|
|
194
|
+
if (files.includes("tsconfig.json") || files.some((f) => f.endsWith(".ts"))) {
|
|
195
|
+
return "typescript";
|
|
196
|
+
}
|
|
197
|
+
if (files.includes("package.json"))
|
|
198
|
+
return "javascript";
|
|
199
|
+
return "unknown";
|
|
200
|
+
}
|
|
201
|
+
// ----------------------------------------------------------------
|
|
202
|
+
// Monorepo detection
|
|
203
|
+
// ----------------------------------------------------------------
|
|
204
|
+
async function detectMonorepo(projectRoot, pkg, topLevel) {
|
|
205
|
+
// Check for npm/pnpm/yarn workspaces
|
|
206
|
+
const workspaces = pkg?.workspaces;
|
|
207
|
+
if (workspaces) {
|
|
208
|
+
const patterns = Array.isArray(workspaces) ? workspaces : workspaces.packages;
|
|
209
|
+
// For workspace patterns like "packages/*", "apps/*", collect matching dirs
|
|
210
|
+
const packageDirs = [];
|
|
211
|
+
for (const dirEntry of topLevel.filter((e) => e.isDir)) {
|
|
212
|
+
if (IGNORED_DIRS.has(dirEntry.name))
|
|
213
|
+
continue;
|
|
214
|
+
const subPkg = await fileExists(path.join(projectRoot, dirEntry.name, "package.json"));
|
|
215
|
+
if (subPkg && patterns.some((p) => p.startsWith(dirEntry.name) || p.includes("*"))) {
|
|
216
|
+
// List sub-packages
|
|
217
|
+
const subEntries = await listTopLevel(path.join(projectRoot, dirEntry.name));
|
|
218
|
+
for (const sub of subEntries.filter((e) => e.isDir)) {
|
|
219
|
+
const hasPkg = await fileExists(path.join(projectRoot, dirEntry.name, sub.name, "package.json"));
|
|
220
|
+
if (hasPkg)
|
|
221
|
+
packageDirs.push(`${dirEntry.name}/${sub.name}`);
|
|
222
|
+
}
|
|
223
|
+
if (packageDirs.length === 0)
|
|
224
|
+
packageDirs.push(dirEntry.name);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (packageDirs.length > 0)
|
|
228
|
+
return { isMonorepo: true, packageDirs };
|
|
229
|
+
}
|
|
230
|
+
// Heuristic: look for multiple package.json in top-level subdirs
|
|
231
|
+
const subPkgDirs = [];
|
|
232
|
+
for (const entry of topLevel.filter((e) => e.isDir)) {
|
|
233
|
+
if (IGNORED_DIRS.has(entry.name))
|
|
234
|
+
continue;
|
|
235
|
+
const hasPkg = await fileExists(path.join(projectRoot, entry.name, "package.json"));
|
|
236
|
+
if (hasPkg)
|
|
237
|
+
subPkgDirs.push(entry.name);
|
|
238
|
+
}
|
|
239
|
+
if (subPkgDirs.length >= 2) {
|
|
240
|
+
return { isMonorepo: true, packageDirs: subPkgDirs };
|
|
241
|
+
}
|
|
242
|
+
return { isMonorepo: false, packageDirs: [] };
|
|
243
|
+
}
|
|
244
|
+
// ----------------------------------------------------------------
|
|
245
|
+
// discoverTopology — main export
|
|
246
|
+
// ----------------------------------------------------------------
|
|
247
|
+
/**
|
|
248
|
+
* Topology Discovery.
|
|
249
|
+
*
|
|
250
|
+
* Reads only package.json / pyproject.toml / go.mod, root config files,
|
|
251
|
+
* and top-level directory names. No LLM, no deep reads.
|
|
252
|
+
* Must run before any agent or selector.
|
|
253
|
+
*/
|
|
254
|
+
export async function discoverTopology(projectRoot) {
|
|
255
|
+
const resolvedRoot = path.resolve(projectRoot);
|
|
256
|
+
// 1. List top-level entries (names only, no recursion)
|
|
257
|
+
const topLevel = await listTopLevel(resolvedRoot);
|
|
258
|
+
// 2. Read package.json (or pyproject.toml / go.mod for other stacks)
|
|
259
|
+
const pkg = await readJsonFile(path.join(resolvedRoot, "package.json"));
|
|
260
|
+
// 3. Detect language
|
|
261
|
+
const language = await detectLanguage(resolvedRoot, topLevel);
|
|
262
|
+
// 4. Detect framework + auth lib from package.json
|
|
263
|
+
let framework = "unknown";
|
|
264
|
+
let authLib = "unknown";
|
|
265
|
+
if (pkg) {
|
|
266
|
+
framework = detectFrameworkFromPkg(pkg);
|
|
267
|
+
authLib = detectAuthFromPkg(pkg);
|
|
268
|
+
}
|
|
269
|
+
else if (language === "python") {
|
|
270
|
+
// FastAPI / Django detection via pyproject.toml or requirements
|
|
271
|
+
const pyprojectContent = await readJsonFile(path.join(resolvedRoot, "pyproject.toml"));
|
|
272
|
+
// pyproject.toml is TOML, not JSON — just check file presence
|
|
273
|
+
const hasPyproject = await fileExists(path.join(resolvedRoot, "pyproject.toml"));
|
|
274
|
+
const hasManagePy = await fileExists(path.join(resolvedRoot, "manage.py"));
|
|
275
|
+
const hasMainPy = await fileExists(path.join(resolvedRoot, "main.py"));
|
|
276
|
+
if (hasManagePy) {
|
|
277
|
+
framework = "unknown"; // Django — no specific FrameworkName, falls to generic
|
|
278
|
+
}
|
|
279
|
+
else if (hasMainPy || hasPyproject) {
|
|
280
|
+
framework = "unknown"; // FastAPI — generic
|
|
281
|
+
}
|
|
282
|
+
void pyprojectContent; // TOML not parsed
|
|
283
|
+
authLib = "unknown"; // Python auth needs deeper scan
|
|
284
|
+
}
|
|
285
|
+
// 5. Check for auth config files at root to refine authLib
|
|
286
|
+
if (authLib === "unknown") {
|
|
287
|
+
const authConfigCandidates = ["auth.ts", "auth.js", "auth.config.ts"];
|
|
288
|
+
for (const candidate of authConfigCandidates) {
|
|
289
|
+
if (topLevel.some((e) => !e.isDir && e.name === candidate)) {
|
|
290
|
+
// Presence of auth.ts is strong NextAuth signal
|
|
291
|
+
authLib = "nextauth";
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// 6. Infer route root and API root
|
|
297
|
+
const { routeRoot, apiRoot, refinedFramework } = await inferRoots(resolvedRoot, framework, topLevel);
|
|
298
|
+
framework = refinedFramework;
|
|
299
|
+
// 7. Detect monorepo
|
|
300
|
+
const { isMonorepo, packageDirs } = await detectMonorepo(resolvedRoot, pkg, topLevel);
|
|
301
|
+
return {
|
|
302
|
+
framework,
|
|
303
|
+
language,
|
|
304
|
+
routeRoot,
|
|
305
|
+
apiRoot,
|
|
306
|
+
authLib,
|
|
307
|
+
isMonorepo,
|
|
308
|
+
packageDirs,
|
|
309
|
+
};
|
|
310
|
+
}
|
package/dist/cli/drift.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import type { DuctConfig } from "../core/types.js";
|
|
2
2
|
export type DriftStatus = "ok" | "missing" | "auth_changed" | "schema_changed" | "unreachable" | "unknown";
|
|
3
3
|
export type DriftCheckCode = "ROUTE_MISSING" | "AUTH_DRIFT" | "SCHEMA_DRIFT" | "UNREACHABLE";
|
|
4
|
+
export type DataAvailabilityStatus = "ok" | "empty" | "auth_required" | "unreachable" | "no_action";
|
|
5
|
+
export interface DataAvailabilityResult {
|
|
6
|
+
entityId: string;
|
|
7
|
+
discoveryActionId: string;
|
|
8
|
+
status: DataAvailabilityStatus;
|
|
9
|
+
itemCount?: number;
|
|
10
|
+
detail?: string;
|
|
11
|
+
checkedAt: string;
|
|
12
|
+
}
|
|
4
13
|
export interface ActionDriftResult {
|
|
5
14
|
actionId: string;
|
|
6
15
|
endpoint: string;
|
|
@@ -31,6 +40,8 @@ export interface DriftReport {
|
|
|
31
40
|
ok: ActionDriftResult[];
|
|
32
41
|
unreachable: ActionDriftResult[];
|
|
33
42
|
hasDrift: boolean;
|
|
43
|
+
/** Data availability results for dataModel.entities discoveryAction probes. */
|
|
44
|
+
dataAvailability?: DataAvailabilityResult[];
|
|
34
45
|
}
|
|
35
46
|
export declare function runDriftCheck(config: DuctConfig): Promise<DriftReport>;
|
|
36
47
|
export declare function printDriftReport(report: DriftReport): void;
|
package/dist/cli/drift.js
CHANGED
|
@@ -112,6 +112,120 @@ async function checkRoute(route, baseUrl) {
|
|
|
112
112
|
return { routePath: route.path, status: "ok", httpStatus, detail: `Reachable (${httpStatus})`, checkedAt };
|
|
113
113
|
}
|
|
114
114
|
// ----------------------------------------------------------------
|
|
115
|
+
// checkDiscoveryAction — data availability probe
|
|
116
|
+
// ----------------------------------------------------------------
|
|
117
|
+
/**
|
|
118
|
+
* Call a discoveryAction endpoint and verify it returns a non-empty result.
|
|
119
|
+
* Uses a real GET request. If the endpoint requires auth (401/403), reports
|
|
120
|
+
* auth_required (not a failure — availability can't be verified without creds).
|
|
121
|
+
*/
|
|
122
|
+
async function checkDiscoveryAction(entityId, discoveryActionId, actionDef, baseUrl) {
|
|
123
|
+
const checkedAt = new Date().toISOString();
|
|
124
|
+
if (!actionDef) {
|
|
125
|
+
return {
|
|
126
|
+
entityId,
|
|
127
|
+
discoveryActionId,
|
|
128
|
+
status: "no_action",
|
|
129
|
+
detail: `Action '${discoveryActionId}' not found in manifest`,
|
|
130
|
+
checkedAt,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const apiBase = baseUrl.replace(/\/$/, "");
|
|
134
|
+
const path = actionDef.path.startsWith("/") ? actionDef.path : `/${actionDef.path}`;
|
|
135
|
+
const url = `${apiBase}${path}`;
|
|
136
|
+
const controller = new AbortController();
|
|
137
|
+
const timeout = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
|
|
138
|
+
try {
|
|
139
|
+
const res = await fetch(url, {
|
|
140
|
+
method: "GET",
|
|
141
|
+
headers: { Accept: "application/json" },
|
|
142
|
+
signal: controller.signal,
|
|
143
|
+
});
|
|
144
|
+
if (res.status === 401 || res.status === 403) {
|
|
145
|
+
return {
|
|
146
|
+
entityId,
|
|
147
|
+
discoveryActionId,
|
|
148
|
+
status: "auth_required",
|
|
149
|
+
detail: `Endpoint requires auth (${res.status}) — availability unverifiable without credentials`,
|
|
150
|
+
checkedAt,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (res.status >= 500) {
|
|
154
|
+
return {
|
|
155
|
+
entityId,
|
|
156
|
+
discoveryActionId,
|
|
157
|
+
status: "unreachable",
|
|
158
|
+
detail: `Server error (${res.status})`,
|
|
159
|
+
checkedAt,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (!res.ok) {
|
|
163
|
+
return {
|
|
164
|
+
entityId,
|
|
165
|
+
discoveryActionId,
|
|
166
|
+
status: "unreachable",
|
|
167
|
+
detail: `Unexpected status ${res.status}`,
|
|
168
|
+
checkedAt,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
// Parse response and count items
|
|
172
|
+
let body;
|
|
173
|
+
try {
|
|
174
|
+
body = await res.json();
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return {
|
|
178
|
+
entityId,
|
|
179
|
+
discoveryActionId,
|
|
180
|
+
status: "empty",
|
|
181
|
+
detail: "Response is not valid JSON",
|
|
182
|
+
checkedAt,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
// Find the item count: array at top level or first array-valued key
|
|
186
|
+
let itemCount = 0;
|
|
187
|
+
if (Array.isArray(body)) {
|
|
188
|
+
itemCount = body.length;
|
|
189
|
+
}
|
|
190
|
+
else if (typeof body === "object" && body !== null) {
|
|
191
|
+
const arrayEntry = Object.values(body).find((v) => Array.isArray(v));
|
|
192
|
+
if (arrayEntry)
|
|
193
|
+
itemCount = arrayEntry.length;
|
|
194
|
+
}
|
|
195
|
+
if (itemCount === 0) {
|
|
196
|
+
return {
|
|
197
|
+
entityId,
|
|
198
|
+
discoveryActionId,
|
|
199
|
+
status: "empty",
|
|
200
|
+
itemCount: 0,
|
|
201
|
+
detail: "Discovery action returned 0 items — data may not exist yet or filter params are required",
|
|
202
|
+
checkedAt,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
entityId,
|
|
207
|
+
discoveryActionId,
|
|
208
|
+
status: "ok",
|
|
209
|
+
itemCount,
|
|
210
|
+
detail: `${itemCount} item(s) returned`,
|
|
211
|
+
checkedAt,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
216
|
+
return {
|
|
217
|
+
entityId,
|
|
218
|
+
discoveryActionId,
|
|
219
|
+
status: "unreachable",
|
|
220
|
+
detail: msg,
|
|
221
|
+
checkedAt,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
clearTimeout(timeout);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// ----------------------------------------------------------------
|
|
115
229
|
// runDriftCheck
|
|
116
230
|
// ----------------------------------------------------------------
|
|
117
231
|
const PROBE_CONCURRENCY = 10;
|
|
@@ -131,12 +245,14 @@ async function withConcurrency(tasks, limit) {
|
|
|
131
245
|
}
|
|
132
246
|
export async function runDriftCheck(config) {
|
|
133
247
|
const baseUrl = config.product.baseUrl;
|
|
248
|
+
const apiBaseUrl = config.product.apiBaseUrl ?? baseUrl;
|
|
134
249
|
const checkedAt = new Date().toISOString();
|
|
135
250
|
const actions = config.actions ?? [];
|
|
136
251
|
const routes = config.routes ?? [];
|
|
252
|
+
const dataModelEntities = config.dataModel?.entities ?? [];
|
|
137
253
|
// Probe action endpoints in parallel (capped at PROBE_CONCURRENCY)
|
|
138
254
|
const actionTasks = actions.map((action) => async () => {
|
|
139
|
-
const { httpStatus, authScheme, error } = await checkEndpoint(action.path,
|
|
255
|
+
const { httpStatus, authScheme, error } = await checkEndpoint(action.path, apiBaseUrl);
|
|
140
256
|
const { status, code, detail } = classifyResult(httpStatus, action.requiresAuth ?? false, authScheme, error);
|
|
141
257
|
return {
|
|
142
258
|
actionId: action.id,
|
|
@@ -150,9 +266,16 @@ export async function runDriftCheck(config) {
|
|
|
150
266
|
};
|
|
151
267
|
});
|
|
152
268
|
const routeTasks = routes.map((route) => async () => checkRoute(route, baseUrl));
|
|
153
|
-
|
|
269
|
+
// Data availability checks: call each entity's discoveryAction and verify it returns data.
|
|
270
|
+
// Runs in parallel alongside action + route probes.
|
|
271
|
+
const dataTasks = dataModelEntities.map((entity) => async () => {
|
|
272
|
+
const actionDef = actions.find((a) => a.id === entity.discoveryAction);
|
|
273
|
+
return checkDiscoveryAction(entity.id, entity.discoveryAction, actionDef, apiBaseUrl);
|
|
274
|
+
});
|
|
275
|
+
const [actionResults, routeResults, dataResults] = await Promise.all([
|
|
154
276
|
withConcurrency(actionTasks, PROBE_CONCURRENCY),
|
|
155
277
|
withConcurrency(routeTasks, PROBE_CONCURRENCY),
|
|
278
|
+
withConcurrency(dataTasks, PROBE_CONCURRENCY),
|
|
156
279
|
]);
|
|
157
280
|
const drifted = actionResults.filter((r) => r.status !== "ok" && r.status !== "unreachable");
|
|
158
281
|
const routeDrifted = routeResults.filter((r) => r.status !== "ok" && r.status !== "unreachable");
|
|
@@ -169,6 +292,7 @@ export async function runDriftCheck(config) {
|
|
|
169
292
|
ok,
|
|
170
293
|
unreachable,
|
|
171
294
|
hasDrift: drifted.length > 0 || routeDrifted.length > 0,
|
|
295
|
+
dataAvailability: dataResults.length > 0 ? dataResults : undefined,
|
|
172
296
|
};
|
|
173
297
|
}
|
|
174
298
|
// ----------------------------------------------------------------
|
|
@@ -203,6 +327,27 @@ export function printDriftReport(report) {
|
|
|
203
327
|
}
|
|
204
328
|
console.log();
|
|
205
329
|
}
|
|
330
|
+
// Data availability
|
|
331
|
+
if (report.dataAvailability && report.dataAvailability.length > 0) {
|
|
332
|
+
const dataIssues = report.dataAvailability.filter((r) => r.status !== "ok" && r.status !== "auth_required");
|
|
333
|
+
const dataOk = report.dataAvailability.filter((r) => r.status === "ok");
|
|
334
|
+
const dataAuthRequired = report.dataAvailability.filter((r) => r.status === "auth_required");
|
|
335
|
+
console.log(chalk.bold(" Data Model Availability:"));
|
|
336
|
+
for (const r of dataOk) {
|
|
337
|
+
console.log(` ${chalk.green("✓")} ${r.entityId} (${r.discoveryActionId}): ${r.itemCount} item(s)`);
|
|
338
|
+
}
|
|
339
|
+
for (const r of dataAuthRequired) {
|
|
340
|
+
console.log(` ${chalk.gray("–")} ${r.entityId} (${r.discoveryActionId}): ${chalk.gray("auth required — skipped")}`);
|
|
341
|
+
}
|
|
342
|
+
for (const r of dataIssues) {
|
|
343
|
+
const icon = r.status === "empty" ? chalk.yellow("⚠") : chalk.red("✗");
|
|
344
|
+
const label = r.status === "empty" ? chalk.yellow("EMPTY") : chalk.red(r.status.toUpperCase());
|
|
345
|
+
console.log(` ${icon} ${r.entityId} (${r.discoveryActionId}): ${label}`);
|
|
346
|
+
if (r.detail)
|
|
347
|
+
console.log(` ${chalk.gray(r.detail)}`);
|
|
348
|
+
}
|
|
349
|
+
console.log();
|
|
350
|
+
}
|
|
206
351
|
// Summary
|
|
207
352
|
if (report.hasDrift) {
|
|
208
353
|
console.log(chalk.gray(` Run "npx duct init --update" to regenerate config`));
|
package/dist/cli/fs/sandbox.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export declare class SandboxFS {
|
|
|
7
7
|
get projectRoot(): string;
|
|
8
8
|
/**
|
|
9
9
|
* Resolve a relative path within the sandbox.
|
|
10
|
+
* Canonicalizes symlinks via `fs.realpath` before the boundary check so
|
|
11
|
+
* a symlink pointing outside the project root is caught, not followed.
|
|
10
12
|
* Throws if the resolved path escapes the root.
|
|
11
13
|
*/
|
|
12
14
|
private resolveSafe;
|
package/dist/cli/fs/sandbox.js
CHANGED
|
@@ -62,17 +62,31 @@ export class SandboxFS {
|
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* Resolve a relative path within the sandbox.
|
|
65
|
+
* Canonicalizes symlinks via `fs.realpath` before the boundary check so
|
|
66
|
+
* a symlink pointing outside the project root is caught, not followed.
|
|
65
67
|
* Throws if the resolved path escapes the root.
|
|
66
68
|
*/
|
|
67
|
-
resolveSafe(relativePath) {
|
|
68
|
-
// Normalize and resolve
|
|
69
|
+
async resolveSafe(relativePath) {
|
|
69
70
|
const normalized = path.normalize(relativePath);
|
|
70
71
|
const absolute = path.resolve(this.root, normalized);
|
|
71
|
-
//
|
|
72
|
+
// Quick pre-check before the realpath syscall
|
|
72
73
|
if (!absolute.startsWith(this.root + path.sep) && absolute !== this.root) {
|
|
73
74
|
throw new Error(`Path traversal detected: "${relativePath}" resolves outside project root`);
|
|
74
75
|
}
|
|
75
|
-
|
|
76
|
+
// Canonicalize symlinks — prevents a symlink at e.g. src/secrets → /etc
|
|
77
|
+
let real;
|
|
78
|
+
try {
|
|
79
|
+
real = await fs.realpath(absolute);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// File doesn't exist yet (e.g. checking a path before writing); use the
|
|
83
|
+
// lexically-resolved absolute path — still safe for existence checks.
|
|
84
|
+
real = absolute;
|
|
85
|
+
}
|
|
86
|
+
if (!real.startsWith(this.root + path.sep) && real !== this.root) {
|
|
87
|
+
throw new Error(`Path traversal via symlink detected: "${relativePath}" resolves to "${real}" outside project root`);
|
|
88
|
+
}
|
|
89
|
+
return real;
|
|
76
90
|
}
|
|
77
91
|
/**
|
|
78
92
|
* Get the relative path from root, used for block pattern matching.
|
|
@@ -127,7 +141,7 @@ export class SandboxFS {
|
|
|
127
141
|
if (!this.isAllowedExtension(relativePath)) {
|
|
128
142
|
return null;
|
|
129
143
|
}
|
|
130
|
-
const absolute = this.resolveSafe(relativePath);
|
|
144
|
+
const absolute = await this.resolveSafe(relativePath);
|
|
131
145
|
const stat = await fs.stat(absolute);
|
|
132
146
|
if (!stat.isFile()) {
|
|
133
147
|
return null;
|
|
@@ -153,7 +167,7 @@ export class SandboxFS {
|
|
|
153
167
|
if (this.isBlocked(relativePath)) {
|
|
154
168
|
return [];
|
|
155
169
|
}
|
|
156
|
-
const absolute = this.resolveSafe(relativePath);
|
|
170
|
+
const absolute = await this.resolveSafe(relativePath);
|
|
157
171
|
try {
|
|
158
172
|
const entries = await fs.readdir(absolute, { withFileTypes: true });
|
|
159
173
|
const results = [];
|
|
@@ -188,7 +202,7 @@ export class SandboxFS {
|
|
|
188
202
|
return false;
|
|
189
203
|
}
|
|
190
204
|
try {
|
|
191
|
-
const absolute = this.resolveSafe(relativePath);
|
|
205
|
+
const absolute = await this.resolveSafe(relativePath);
|
|
192
206
|
await fs.access(absolute);
|
|
193
207
|
return true;
|
|
194
208
|
}
|
|
@@ -204,7 +218,7 @@ export class SandboxFS {
|
|
|
204
218
|
return false;
|
|
205
219
|
}
|
|
206
220
|
try {
|
|
207
|
-
const absolute = this.resolveSafe(relativePath);
|
|
221
|
+
const absolute = await this.resolveSafe(relativePath);
|
|
208
222
|
const stat = await fs.stat(absolute);
|
|
209
223
|
return stat.isDirectory();
|
|
210
224
|
}
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import type { DuctConfig } from "../../core/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate a heavily-commented `duct.config.ts` template for manual mode.
|
|
4
|
+
* Framework and auth are pre-filled from topology; routes/actions/deeplinks
|
|
5
|
+
* contain commented-out example entries so the developer can fill them in.
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateManualTemplate(opts: {
|
|
8
|
+
shellId: string;
|
|
9
|
+
productName: string;
|
|
10
|
+
baseUrl: string;
|
|
11
|
+
authStrategy: string;
|
|
12
|
+
framework: string;
|
|
13
|
+
logoUrl?: string;
|
|
14
|
+
}): string;
|
|
2
15
|
/**
|
|
3
16
|
* Generate the contents of a `duct.config.ts` file from a DuctConfig object.
|
|
4
17
|
*
|