@duct-sdk/sdk 0.1.6
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 +92 -0
- package/dist/adapters/next/index.d.ts +4 -0
- package/dist/adapters/next/index.js +5 -0
- package/dist/adapters/next/receiver.d.ts +44 -0
- package/dist/adapters/next/receiver.js +162 -0
- package/dist/adapters/next/shell.d.ts +88 -0
- package/dist/adapters/next/shell.js +255 -0
- package/dist/adapters/react/index.d.ts +2 -0
- package/dist/adapters/react/index.js +4 -0
- package/dist/adapters/react/shell.d.ts +88 -0
- package/dist/adapters/react/shell.js +255 -0
- package/dist/adapters/remix/index.d.ts +2 -0
- package/dist/adapters/remix/index.js +4 -0
- package/dist/adapters/remix/receiver.d.ts +44 -0
- package/dist/adapters/remix/receiver.js +153 -0
- package/dist/adapters/sveltekit/index.d.ts +2 -0
- package/dist/adapters/sveltekit/index.js +4 -0
- package/dist/adapters/sveltekit/receiver.d.ts +56 -0
- package/dist/adapters/sveltekit/receiver.js +154 -0
- package/dist/adapters/vite/index.d.ts +2 -0
- package/dist/adapters/vite/index.js +4 -0
- package/dist/adapters/vite/receiver.d.ts +82 -0
- package/dist/adapters/vite/receiver.js +164 -0
- package/dist/assets/logo.d.ts +1 -0
- package/dist/assets/logo.js +1 -0
- package/dist/cli/agents/api-agent.d.ts +14 -0
- package/dist/cli/agents/api-agent.js +656 -0
- package/dist/cli/agents/auth-agent.d.ts +6 -0
- package/dist/cli/agents/auth-agent.js +198 -0
- package/dist/cli/agents/deeplink-agent.d.ts +6 -0
- package/dist/cli/agents/deeplink-agent.js +263 -0
- package/dist/cli/agents/llm.d.ts +33 -0
- package/dist/cli/agents/llm.js +276 -0
- package/dist/cli/agents/pipeline.d.ts +51 -0
- package/dist/cli/agents/pipeline.js +236 -0
- package/dist/cli/agents/reviewer-agent.d.ts +11 -0
- package/dist/cli/agents/reviewer-agent.js +442 -0
- package/dist/cli/agents/route-agent.d.ts +13 -0
- package/dist/cli/agents/route-agent.js +346 -0
- package/dist/cli/detectors/auth.d.ts +18 -0
- package/dist/cli/detectors/auth.js +273 -0
- package/dist/cli/detectors/framework.d.ts +22 -0
- package/dist/cli/detectors/framework.js +242 -0
- package/dist/cli/drift.d.ts +39 -0
- package/dist/cli/drift.js +247 -0
- package/dist/cli/fs/sandbox.d.ts +77 -0
- package/dist/cli/fs/sandbox.js +295 -0
- package/dist/cli/generators/config.d.ts +7 -0
- package/dist/cli/generators/config.js +255 -0
- package/dist/cli/generators/receiver.d.ts +12 -0
- package/dist/cli/generators/receiver.js +683 -0
- package/dist/cli/generators/shell.d.ts +18 -0
- package/dist/cli/generators/shell.js +672 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +603 -0
- package/dist/cli/init.d.ts +26 -0
- package/dist/cli/init.js +835 -0
- package/dist/core/action.d.ts +37 -0
- package/dist/core/action.js +140 -0
- package/dist/core/config.d.ts +514 -0
- package/dist/core/config.js +221 -0
- package/dist/core/endpoints.d.ts +17 -0
- package/dist/core/endpoints.js +128 -0
- package/dist/core/token.d.ts +70 -0
- package/dist/core/token.js +174 -0
- package/dist/core/types.d.ts +450 -0
- package/dist/core/types.js +11 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/package.json +84 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Framework Detector
|
|
3
|
+
// ============================================================
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import * as fs from "fs/promises";
|
|
6
|
+
// ----------------------------------------------------------------
|
|
7
|
+
// Helpers
|
|
8
|
+
// ----------------------------------------------------------------
|
|
9
|
+
async function dirExists(dirPath) {
|
|
10
|
+
try {
|
|
11
|
+
const stat = await fs.stat(dirPath);
|
|
12
|
+
return stat.isDirectory();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function fileExists(filePath) {
|
|
19
|
+
try {
|
|
20
|
+
await fs.access(filePath);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function readPackageJson(projectRoot) {
|
|
28
|
+
try {
|
|
29
|
+
const content = await fs.readFile(path.join(projectRoot, "package.json"), "utf-8");
|
|
30
|
+
return JSON.parse(content);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function getDependencyVersion(pkg, name) {
|
|
37
|
+
const deps = {
|
|
38
|
+
...(pkg.dependencies ?? {}),
|
|
39
|
+
...(pkg.devDependencies ?? {}),
|
|
40
|
+
...(pkg.peerDependencies ?? {}),
|
|
41
|
+
};
|
|
42
|
+
return deps[name] ?? null;
|
|
43
|
+
}
|
|
44
|
+
function cleanVersion(version) {
|
|
45
|
+
if (!version)
|
|
46
|
+
return "unknown";
|
|
47
|
+
// Remove semver range prefix (^, ~, >=, etc.)
|
|
48
|
+
return version.replace(/^[^0-9]*/, "").trim() || "unknown";
|
|
49
|
+
}
|
|
50
|
+
async function detectPackageManager(projectRoot) {
|
|
51
|
+
if (await fileExists(path.join(projectRoot, "bun.lockb")))
|
|
52
|
+
return "bun";
|
|
53
|
+
if (await fileExists(path.join(projectRoot, "pnpm-lock.yaml")))
|
|
54
|
+
return "pnpm";
|
|
55
|
+
if (await fileExists(path.join(projectRoot, "yarn.lock")))
|
|
56
|
+
return "yarn";
|
|
57
|
+
if (await fileExists(path.join(projectRoot, "package-lock.json")))
|
|
58
|
+
return "npm";
|
|
59
|
+
return "unknown";
|
|
60
|
+
}
|
|
61
|
+
// ----------------------------------------------------------------
|
|
62
|
+
// detectFramework
|
|
63
|
+
// ----------------------------------------------------------------
|
|
64
|
+
/**
|
|
65
|
+
* Detect the frontend framework used in a project.
|
|
66
|
+
*
|
|
67
|
+
* @param projectRoot - Absolute path to the project root directory
|
|
68
|
+
* @returns FrameworkInfo object describing the detected framework
|
|
69
|
+
*/
|
|
70
|
+
export async function detectFramework(projectRoot) {
|
|
71
|
+
const pkg = await readPackageJson(projectRoot);
|
|
72
|
+
const packageManager = await detectPackageManager(projectRoot);
|
|
73
|
+
// Default result
|
|
74
|
+
const defaultResult = {
|
|
75
|
+
name: "unknown",
|
|
76
|
+
version: "unknown",
|
|
77
|
+
hasAppRouter: false,
|
|
78
|
+
hasPagesRouter: false,
|
|
79
|
+
packageManager,
|
|
80
|
+
srcDir: projectRoot,
|
|
81
|
+
};
|
|
82
|
+
if (!pkg) {
|
|
83
|
+
return defaultResult;
|
|
84
|
+
}
|
|
85
|
+
// ---- Next.js ----
|
|
86
|
+
const nextVersion = getDependencyVersion(pkg, "next");
|
|
87
|
+
if (nextVersion) {
|
|
88
|
+
// Check for App Router: presence of app/ directory (or src/app/)
|
|
89
|
+
const appDirRoot = await dirExists(path.join(projectRoot, "app"));
|
|
90
|
+
const appDirSrc = await dirExists(path.join(projectRoot, "src", "app"));
|
|
91
|
+
const pagesDirRoot = await dirExists(path.join(projectRoot, "pages"));
|
|
92
|
+
const pagesDirSrc = await dirExists(path.join(projectRoot, "src", "pages"));
|
|
93
|
+
const hasAppRouter = appDirRoot || appDirSrc;
|
|
94
|
+
const hasPagesRouter = pagesDirRoot || pagesDirSrc;
|
|
95
|
+
// Look for next.config file
|
|
96
|
+
const configCandidates = [
|
|
97
|
+
"next.config.js",
|
|
98
|
+
"next.config.mjs",
|
|
99
|
+
"next.config.ts",
|
|
100
|
+
];
|
|
101
|
+
let configFile;
|
|
102
|
+
for (const candidate of configCandidates) {
|
|
103
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
104
|
+
configFile = candidate;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Determine src dir
|
|
109
|
+
const srcDir = appDirSrc || pagesDirSrc
|
|
110
|
+
? path.join(projectRoot, "src")
|
|
111
|
+
: projectRoot;
|
|
112
|
+
// App router takes precedence; if both exist, prefer app router
|
|
113
|
+
const name = hasAppRouter ? "next-app" : "next-pages";
|
|
114
|
+
return {
|
|
115
|
+
name,
|
|
116
|
+
version: cleanVersion(nextVersion),
|
|
117
|
+
hasAppRouter,
|
|
118
|
+
hasPagesRouter,
|
|
119
|
+
packageManager,
|
|
120
|
+
srcDir,
|
|
121
|
+
configFile,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
// ---- Remix ----
|
|
125
|
+
const remixVersion = getDependencyVersion(pkg, "@remix-run/react") ??
|
|
126
|
+
getDependencyVersion(pkg, "@remix-run/node") ??
|
|
127
|
+
getDependencyVersion(pkg, "remix");
|
|
128
|
+
if (remixVersion) {
|
|
129
|
+
const configFile = (await fileExists(path.join(projectRoot, "remix.config.js")))
|
|
130
|
+
? "remix.config.js"
|
|
131
|
+
: (await fileExists(path.join(projectRoot, "remix.config.ts")))
|
|
132
|
+
? "remix.config.ts"
|
|
133
|
+
: undefined;
|
|
134
|
+
const srcDir = (await dirExists(path.join(projectRoot, "app")))
|
|
135
|
+
? path.join(projectRoot, "app")
|
|
136
|
+
: projectRoot;
|
|
137
|
+
return {
|
|
138
|
+
name: "remix",
|
|
139
|
+
version: cleanVersion(remixVersion),
|
|
140
|
+
hasAppRouter: false,
|
|
141
|
+
hasPagesRouter: false,
|
|
142
|
+
packageManager,
|
|
143
|
+
srcDir,
|
|
144
|
+
configFile,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// ---- SvelteKit ----
|
|
148
|
+
const sveltekitVersion = getDependencyVersion(pkg, "@sveltejs/kit") ??
|
|
149
|
+
getDependencyVersion(pkg, "svelte");
|
|
150
|
+
if (sveltekitVersion && getDependencyVersion(pkg, "@sveltejs/kit")) {
|
|
151
|
+
const configFile = (await fileExists(path.join(projectRoot, "svelte.config.js")))
|
|
152
|
+
? "svelte.config.js"
|
|
153
|
+
: undefined;
|
|
154
|
+
return {
|
|
155
|
+
name: "sveltekit",
|
|
156
|
+
version: cleanVersion(sveltekitVersion),
|
|
157
|
+
hasAppRouter: false,
|
|
158
|
+
hasPagesRouter: false,
|
|
159
|
+
packageManager,
|
|
160
|
+
srcDir: projectRoot,
|
|
161
|
+
configFile,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
// ---- Nuxt (check before Vue 3 — Nuxt also has vue as a dep) ----
|
|
165
|
+
const nuxtVersion = getDependencyVersion(pkg, "nuxt") ??
|
|
166
|
+
getDependencyVersion(pkg, "@nuxt/kit");
|
|
167
|
+
if (nuxtVersion) {
|
|
168
|
+
let configFile;
|
|
169
|
+
for (const candidate of ["nuxt.config.ts", "nuxt.config.js", "nuxt.config.mjs"]) {
|
|
170
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
171
|
+
configFile = candidate;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
name: "nuxt",
|
|
177
|
+
version: cleanVersion(nuxtVersion),
|
|
178
|
+
hasAppRouter: false,
|
|
179
|
+
hasPagesRouter: false,
|
|
180
|
+
packageManager,
|
|
181
|
+
srcDir: projectRoot,
|
|
182
|
+
configFile,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
// ---- Vue 3 (standalone, not Nuxt) ----
|
|
186
|
+
const vueVersion = getDependencyVersion(pkg, "vue");
|
|
187
|
+
const vueIsV3 = vueVersion !== null &&
|
|
188
|
+
!vueVersion.startsWith("2") &&
|
|
189
|
+
!vueVersion.startsWith("^2") &&
|
|
190
|
+
!vueVersion.startsWith("~2");
|
|
191
|
+
if (vueIsV3) {
|
|
192
|
+
let configFile;
|
|
193
|
+
for (const candidate of ["vite.config.ts", "vite.config.js", "vue.config.js"]) {
|
|
194
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
195
|
+
configFile = candidate;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const srcDir = (await dirExists(path.join(projectRoot, "src")))
|
|
200
|
+
? path.join(projectRoot, "src")
|
|
201
|
+
: projectRoot;
|
|
202
|
+
return {
|
|
203
|
+
name: "vue3",
|
|
204
|
+
version: cleanVersion(vueVersion),
|
|
205
|
+
hasAppRouter: false,
|
|
206
|
+
hasPagesRouter: false,
|
|
207
|
+
packageManager,
|
|
208
|
+
srcDir,
|
|
209
|
+
configFile,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
// ---- Vite + React ----
|
|
213
|
+
const viteVersion = getDependencyVersion(pkg, "vite");
|
|
214
|
+
const reactVersion = getDependencyVersion(pkg, "react");
|
|
215
|
+
if (viteVersion && reactVersion) {
|
|
216
|
+
const configCandidates = [
|
|
217
|
+
"vite.config.ts",
|
|
218
|
+
"vite.config.js",
|
|
219
|
+
"vite.config.mjs",
|
|
220
|
+
];
|
|
221
|
+
let configFile;
|
|
222
|
+
for (const candidate of configCandidates) {
|
|
223
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
224
|
+
configFile = candidate;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const srcDir = (await dirExists(path.join(projectRoot, "src")))
|
|
229
|
+
? path.join(projectRoot, "src")
|
|
230
|
+
: projectRoot;
|
|
231
|
+
return {
|
|
232
|
+
name: "vite-react",
|
|
233
|
+
version: cleanVersion(viteVersion),
|
|
234
|
+
hasAppRouter: false,
|
|
235
|
+
hasPagesRouter: false,
|
|
236
|
+
packageManager,
|
|
237
|
+
srcDir,
|
|
238
|
+
configFile,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
return defaultResult;
|
|
242
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { DuctConfig } from "../core/types.js";
|
|
2
|
+
export type DriftStatus = "ok" | "missing" | "auth_changed" | "schema_changed" | "unreachable" | "unknown";
|
|
3
|
+
export type DriftCheckCode = "ROUTE_MISSING" | "AUTH_DRIFT" | "SCHEMA_DRIFT" | "UNREACHABLE";
|
|
4
|
+
export interface ActionDriftResult {
|
|
5
|
+
actionId: string;
|
|
6
|
+
endpoint: string;
|
|
7
|
+
method: string;
|
|
8
|
+
status: DriftStatus;
|
|
9
|
+
code?: DriftCheckCode;
|
|
10
|
+
httpStatus?: number;
|
|
11
|
+
detail?: string;
|
|
12
|
+
/** ISO 8601 timestamp of when this check ran */
|
|
13
|
+
checkedAt: string;
|
|
14
|
+
}
|
|
15
|
+
export interface RouteDriftResult {
|
|
16
|
+
routePath: string;
|
|
17
|
+
status: DriftStatus;
|
|
18
|
+
code?: DriftCheckCode;
|
|
19
|
+
httpStatus?: number;
|
|
20
|
+
detail?: string;
|
|
21
|
+
checkedAt: string;
|
|
22
|
+
}
|
|
23
|
+
export interface DriftReport {
|
|
24
|
+
shellId: string;
|
|
25
|
+
checkedAt: string;
|
|
26
|
+
baseUrl: string;
|
|
27
|
+
totalActions: number;
|
|
28
|
+
totalRoutes: number;
|
|
29
|
+
drifted: ActionDriftResult[];
|
|
30
|
+
routeDrifted: RouteDriftResult[];
|
|
31
|
+
ok: ActionDriftResult[];
|
|
32
|
+
unreachable: ActionDriftResult[];
|
|
33
|
+
hasDrift: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare function runDriftCheck(config: DuctConfig): Promise<DriftReport>;
|
|
36
|
+
export declare function printDriftReport(report: DriftReport): void;
|
|
37
|
+
/** Write structured JSON drift report to duct-drift-report.json */
|
|
38
|
+
export declare function writeDriftReportJson(report: DriftReport): void;
|
|
39
|
+
export declare function runScanCommand(config: DuctConfig): Promise<void>;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Drift Detector
|
|
3
|
+
// Compares live API endpoints against duct.config.ts
|
|
4
|
+
// Output: structured JSON report + human-readable CLI output
|
|
5
|
+
// ============================================================
|
|
6
|
+
import { writeFileSync } from "fs";
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
import ora from "ora";
|
|
9
|
+
// ----------------------------------------------------------------
|
|
10
|
+
// checkEndpoint
|
|
11
|
+
// ----------------------------------------------------------------
|
|
12
|
+
const PROBE_TIMEOUT_MS = 8_000;
|
|
13
|
+
/**
|
|
14
|
+
* Probe a single endpoint with a HEAD or OPTIONS request.
|
|
15
|
+
* We intentionally do NOT send a real body — we're checking reachability
|
|
16
|
+
* and auth schema, not invoking the action.
|
|
17
|
+
*/
|
|
18
|
+
async function checkEndpoint(path, baseUrl) {
|
|
19
|
+
const url = `${baseUrl.replace(/\/$/, "")}${path}`;
|
|
20
|
+
const controller = new AbortController();
|
|
21
|
+
const timeout = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
|
|
22
|
+
try {
|
|
23
|
+
// Use OPTIONS first — safest probe (no side effects, returns allowed methods)
|
|
24
|
+
let res = await fetch(url, {
|
|
25
|
+
method: "OPTIONS",
|
|
26
|
+
headers: { Accept: "application/json" },
|
|
27
|
+
signal: controller.signal,
|
|
28
|
+
});
|
|
29
|
+
// If OPTIONS returns 405, fall back to HEAD
|
|
30
|
+
if (res.status === 405) {
|
|
31
|
+
res = await fetch(url, {
|
|
32
|
+
method: "HEAD",
|
|
33
|
+
headers: { Accept: "application/json" },
|
|
34
|
+
signal: controller.signal,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const wwwAuth = res.headers.get("www-authenticate");
|
|
38
|
+
const authScheme = wwwAuth ? wwwAuth.split(" ")[0]?.toLowerCase() ?? null : null;
|
|
39
|
+
return { httpStatus: res.status, authScheme };
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
43
|
+
return { httpStatus: 0, authScheme: null, error: msg };
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
clearTimeout(timeout);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// ----------------------------------------------------------------
|
|
50
|
+
// classifyResult
|
|
51
|
+
// ----------------------------------------------------------------
|
|
52
|
+
function classifyResult(httpStatus, expectedRequiresAuth, authScheme, error) {
|
|
53
|
+
if (error) {
|
|
54
|
+
return { status: "unreachable", code: "UNREACHABLE", detail: error };
|
|
55
|
+
}
|
|
56
|
+
if (httpStatus === 0) {
|
|
57
|
+
return { status: "unreachable", code: "UNREACHABLE", detail: "No response" };
|
|
58
|
+
}
|
|
59
|
+
if (httpStatus === 404) {
|
|
60
|
+
return { status: "missing", code: "ROUTE_MISSING", detail: "Endpoint returned 404 — route may have been removed or renamed" };
|
|
61
|
+
}
|
|
62
|
+
if (httpStatus === 401 || httpStatus === 403) {
|
|
63
|
+
if (!expectedRequiresAuth) {
|
|
64
|
+
return {
|
|
65
|
+
status: "auth_changed",
|
|
66
|
+
code: "AUTH_DRIFT",
|
|
67
|
+
detail: `Endpoint now requires auth (${httpStatus}) but config says requiresAuth: false`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return { status: "ok", detail: `Protected (${httpStatus})` };
|
|
71
|
+
}
|
|
72
|
+
if (httpStatus >= 200 && httpStatus < 300) {
|
|
73
|
+
if (expectedRequiresAuth && authScheme === null) {
|
|
74
|
+
return {
|
|
75
|
+
status: "auth_changed",
|
|
76
|
+
code: "AUTH_DRIFT",
|
|
77
|
+
detail: "Endpoint is open but config says requiresAuth: true",
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return { status: "ok", detail: `Reachable (${httpStatus})` };
|
|
81
|
+
}
|
|
82
|
+
if (httpStatus >= 500) {
|
|
83
|
+
return { status: "unreachable", code: "UNREACHABLE", detail: `Server error (${httpStatus})` };
|
|
84
|
+
}
|
|
85
|
+
return { status: "ok", detail: `HTTP ${httpStatus}` };
|
|
86
|
+
}
|
|
87
|
+
// ----------------------------------------------------------------
|
|
88
|
+
// Route existence check (ROUTE_MISSING for declared routes)
|
|
89
|
+
// ----------------------------------------------------------------
|
|
90
|
+
async function checkRoute(route, baseUrl) {
|
|
91
|
+
const checkedAt = new Date().toISOString();
|
|
92
|
+
// Skip dynamic routes — can't probe without real params
|
|
93
|
+
if (route.params.length > 0) {
|
|
94
|
+
return {
|
|
95
|
+
routePath: route.path,
|
|
96
|
+
status: "ok",
|
|
97
|
+
detail: "Dynamic route — skipped (requires runtime params)",
|
|
98
|
+
checkedAt,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const { httpStatus, error } = await checkEndpoint(route.path, baseUrl);
|
|
102
|
+
if (error || httpStatus === 0) {
|
|
103
|
+
return { routePath: route.path, status: "unreachable", code: "UNREACHABLE", httpStatus, detail: error ?? "No response", checkedAt };
|
|
104
|
+
}
|
|
105
|
+
if (httpStatus === 404) {
|
|
106
|
+
return { routePath: route.path, status: "missing", code: "ROUTE_MISSING", httpStatus, detail: "Route returned 404", checkedAt };
|
|
107
|
+
}
|
|
108
|
+
// 401/403 = gated, which is expected for requiresAuth routes
|
|
109
|
+
if ((httpStatus === 401 || httpStatus === 403) && !route.requiresAuth) {
|
|
110
|
+
return { routePath: route.path, status: "auth_changed", code: "AUTH_DRIFT", httpStatus, detail: "Route now requires auth but config says requiresAuth: false", checkedAt };
|
|
111
|
+
}
|
|
112
|
+
return { routePath: route.path, status: "ok", httpStatus, detail: `Reachable (${httpStatus})`, checkedAt };
|
|
113
|
+
}
|
|
114
|
+
// ----------------------------------------------------------------
|
|
115
|
+
// runDriftCheck
|
|
116
|
+
// ----------------------------------------------------------------
|
|
117
|
+
const PROBE_CONCURRENCY = 10;
|
|
118
|
+
/** Run an array of async tasks with a max concurrency cap. */
|
|
119
|
+
async function withConcurrency(tasks, limit) {
|
|
120
|
+
const results = new Array(tasks.length);
|
|
121
|
+
let index = 0;
|
|
122
|
+
async function worker() {
|
|
123
|
+
while (index < tasks.length) {
|
|
124
|
+
const i = index++;
|
|
125
|
+
results[i] = await tasks[i]();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const workers = Array.from({ length: Math.min(limit, tasks.length) }, worker);
|
|
129
|
+
await Promise.all(workers);
|
|
130
|
+
return results;
|
|
131
|
+
}
|
|
132
|
+
export async function runDriftCheck(config) {
|
|
133
|
+
const baseUrl = config.product.baseUrl;
|
|
134
|
+
const checkedAt = new Date().toISOString();
|
|
135
|
+
const actions = config.actions ?? [];
|
|
136
|
+
const routes = config.routes ?? [];
|
|
137
|
+
// Probe action endpoints in parallel (capped at PROBE_CONCURRENCY)
|
|
138
|
+
const actionTasks = actions.map((action) => async () => {
|
|
139
|
+
const { httpStatus, authScheme, error } = await checkEndpoint(action.path, baseUrl);
|
|
140
|
+
const { status, code, detail } = classifyResult(httpStatus, action.requiresAuth ?? false, authScheme, error);
|
|
141
|
+
return {
|
|
142
|
+
actionId: action.id,
|
|
143
|
+
endpoint: `${action.method} ${action.path}`,
|
|
144
|
+
method: action.method,
|
|
145
|
+
status,
|
|
146
|
+
code,
|
|
147
|
+
httpStatus: httpStatus || undefined,
|
|
148
|
+
detail,
|
|
149
|
+
checkedAt,
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
const routeTasks = routes.map((route) => async () => checkRoute(route, baseUrl));
|
|
153
|
+
const [actionResults, routeResults] = await Promise.all([
|
|
154
|
+
withConcurrency(actionTasks, PROBE_CONCURRENCY),
|
|
155
|
+
withConcurrency(routeTasks, PROBE_CONCURRENCY),
|
|
156
|
+
]);
|
|
157
|
+
const drifted = actionResults.filter((r) => r.status !== "ok" && r.status !== "unreachable");
|
|
158
|
+
const routeDrifted = routeResults.filter((r) => r.status !== "ok" && r.status !== "unreachable");
|
|
159
|
+
const unreachable = actionResults.filter((r) => r.status === "unreachable");
|
|
160
|
+
const ok = actionResults.filter((r) => r.status === "ok");
|
|
161
|
+
return {
|
|
162
|
+
shellId: config.shellId,
|
|
163
|
+
checkedAt,
|
|
164
|
+
baseUrl,
|
|
165
|
+
totalActions: actions.length,
|
|
166
|
+
totalRoutes: routes.length,
|
|
167
|
+
drifted,
|
|
168
|
+
routeDrifted,
|
|
169
|
+
ok,
|
|
170
|
+
unreachable,
|
|
171
|
+
hasDrift: drifted.length > 0 || routeDrifted.length > 0,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// ----------------------------------------------------------------
|
|
175
|
+
// printDriftReport — human-readable CLI output
|
|
176
|
+
// ----------------------------------------------------------------
|
|
177
|
+
export function printDriftReport(report) {
|
|
178
|
+
const { drifted, routeDrifted, ok } = report;
|
|
179
|
+
console.log(`\n ◆ Drift Report\n`);
|
|
180
|
+
if (report.totalActions === 0 && report.totalRoutes === 0) {
|
|
181
|
+
console.log(chalk.gray(" No actions or routes defined in config.\n"));
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// Action drift
|
|
185
|
+
if (drifted.length > 0) {
|
|
186
|
+
console.log(chalk.bold(" API Actions:"));
|
|
187
|
+
for (const r of drifted) {
|
|
188
|
+
const color = r.status === "missing" ? chalk.red : chalk.yellow;
|
|
189
|
+
console.log(` ${color("⚠")} [${r.endpoint}] ${color(r.code ?? r.status)}`);
|
|
190
|
+
if (r.detail)
|
|
191
|
+
console.log(` ${chalk.gray(r.detail)}`);
|
|
192
|
+
}
|
|
193
|
+
console.log();
|
|
194
|
+
}
|
|
195
|
+
// Route drift
|
|
196
|
+
if (routeDrifted.length > 0) {
|
|
197
|
+
console.log(chalk.bold(" Routes:"));
|
|
198
|
+
for (const r of routeDrifted) {
|
|
199
|
+
const color = r.status === "missing" ? chalk.red : chalk.yellow;
|
|
200
|
+
console.log(` ${color("⚠")} [${r.routePath}] ${color(r.code ?? r.status)}`);
|
|
201
|
+
if (r.detail)
|
|
202
|
+
console.log(` ${chalk.gray(r.detail)}`);
|
|
203
|
+
}
|
|
204
|
+
console.log();
|
|
205
|
+
}
|
|
206
|
+
// Summary
|
|
207
|
+
if (report.hasDrift) {
|
|
208
|
+
console.log(chalk.gray(` Run "npx duct init --update" to regenerate config`));
|
|
209
|
+
console.log(chalk.gray(` Run "npx duct push" to sync with Duct cloud\n`));
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const total = ok.length + report.unreachable.length;
|
|
213
|
+
console.log(chalk.gray(` ✓ ${ok.length}/${total} action endpoint${ok.length !== 1 ? "s" : ""} verified\n`));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const DRIFT_REPORT_PATH = "duct-drift-report.json";
|
|
217
|
+
/** Write structured JSON drift report to duct-drift-report.json */
|
|
218
|
+
export function writeDriftReportJson(report) {
|
|
219
|
+
try {
|
|
220
|
+
writeFileSync(DRIFT_REPORT_PATH, JSON.stringify(report, null, 2), "utf-8");
|
|
221
|
+
console.log(chalk.gray(` Report written to ${DRIFT_REPORT_PATH}\n`));
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
console.warn(chalk.yellow(` Warning: could not write ${DRIFT_REPORT_PATH}`));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// ----------------------------------------------------------------
|
|
228
|
+
// runScanCommand — wires into CLI
|
|
229
|
+
// ----------------------------------------------------------------
|
|
230
|
+
export async function runScanCommand(config) {
|
|
231
|
+
const spinner = ora(chalk.gray(`Probing ${config.actions?.length ?? 0} endpoints...`)).start();
|
|
232
|
+
let report;
|
|
233
|
+
try {
|
|
234
|
+
report = await runDriftCheck(config);
|
|
235
|
+
spinner.stop();
|
|
236
|
+
}
|
|
237
|
+
catch (err) {
|
|
238
|
+
spinner.stop();
|
|
239
|
+
console.error(chalk.red(" Drift check failed"));
|
|
240
|
+
throw err;
|
|
241
|
+
}
|
|
242
|
+
printDriftReport(report);
|
|
243
|
+
writeDriftReportJson(report);
|
|
244
|
+
if (report.hasDrift) {
|
|
245
|
+
process.exitCode = 1;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export declare class SandboxFS {
|
|
2
|
+
private readonly root;
|
|
3
|
+
constructor(projectRoot: string);
|
|
4
|
+
/**
|
|
5
|
+
* Get the project root directory.
|
|
6
|
+
*/
|
|
7
|
+
get projectRoot(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Resolve a relative path within the sandbox.
|
|
10
|
+
* Throws if the resolved path escapes the root.
|
|
11
|
+
*/
|
|
12
|
+
private resolveSafe;
|
|
13
|
+
/**
|
|
14
|
+
* Get the relative path from root, used for block pattern matching.
|
|
15
|
+
*/
|
|
16
|
+
private getRelative;
|
|
17
|
+
/**
|
|
18
|
+
* Check whether a path is blocked by security rules.
|
|
19
|
+
*/
|
|
20
|
+
isBlocked(relativePath: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Check whether a file extension is in the allow list.
|
|
23
|
+
*/
|
|
24
|
+
isAllowedExtension(filePath: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Read a file within the sandbox.
|
|
27
|
+
* Returns null if the file doesn't exist or is blocked.
|
|
28
|
+
*/
|
|
29
|
+
readFile(relativePath: string): Promise<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* List directory contents within the sandbox.
|
|
32
|
+
* Returns entries with type information, filtering blocked items.
|
|
33
|
+
*/
|
|
34
|
+
listDir(relativePath?: string): Promise<Array<{
|
|
35
|
+
name: string;
|
|
36
|
+
type: "file" | "dir";
|
|
37
|
+
path: string;
|
|
38
|
+
}>>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a path exists within the sandbox.
|
|
41
|
+
*/
|
|
42
|
+
exists(relativePath: string): Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a path is a directory.
|
|
45
|
+
*/
|
|
46
|
+
isDirectory(relativePath: string): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Find files matching a glob pattern within the sandbox.
|
|
49
|
+
* Blocked files and disallowed extensions are filtered out.
|
|
50
|
+
*/
|
|
51
|
+
findFiles(pattern: string, options?: {
|
|
52
|
+
maxFiles?: number;
|
|
53
|
+
}): Promise<string[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Read multiple files, returning a map of relative path → content.
|
|
56
|
+
* Silently skips blocked or non-existent files.
|
|
57
|
+
*/
|
|
58
|
+
readFiles(relativePaths: string[]): Promise<Record<string, string>>;
|
|
59
|
+
/**
|
|
60
|
+
* Read a JSON file and parse it.
|
|
61
|
+
* Returns null if the file doesn't exist, is blocked, or fails to parse.
|
|
62
|
+
*/
|
|
63
|
+
readJSON<T = unknown>(relativePath: string): Promise<T | null>;
|
|
64
|
+
/**
|
|
65
|
+
* Get the relative path of a file, useful for display.
|
|
66
|
+
*/
|
|
67
|
+
relativize(absolutePath: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* Return a new SandboxFS rooted at a subdirectory of this instance.
|
|
70
|
+
* All paths passed to the returned instance are resolved relative to
|
|
71
|
+
* `path.join(this.root, subdir)` — path-traversal protections still apply.
|
|
72
|
+
*
|
|
73
|
+
* This is the idiomatic way to scope a scanner to a monorepo subdirectory
|
|
74
|
+
* (e.g., `sandboxFs.scoped("frontend")`).
|
|
75
|
+
*/
|
|
76
|
+
scoped(subdir: string): SandboxFS;
|
|
77
|
+
}
|