@duct-sdk/sdk 0.4.5 → 0.5.2
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 +50 -0
- package/README.md +36 -1
- package/TOKEN_SPEC.md +237 -0
- package/dist/adapters/react/page-context-guard.d.ts +7 -0
- package/dist/adapters/react/page-context-guard.js +26 -0
- package/dist/adapters/react/page-context-guard.test.d.ts +1 -0
- package/dist/adapters/react/page-context-guard.test.js +15 -0
- package/dist/adapters/react/shell.js +18 -26
- package/dist/adapters/react/use-page-context.d.ts +1 -1
- package/dist/adapters/react/use-page-context.js +6 -16
- package/dist/cli/agents/reviewer-agent.d.ts +3 -1
- package/dist/cli/agents/reviewer-agent.js +29 -21
- package/dist/cli/config-loader.d.ts +15 -0
- package/dist/cli/config-loader.js +187 -0
- package/dist/cli/config-loader.test.d.ts +1 -0
- package/dist/cli/config-loader.test.js +63 -0
- package/dist/cli/detectors/framework.d.ts +1 -1
- package/dist/cli/detectors/framework.js +40 -0
- package/dist/cli/generators/receiver.js +206 -76
- package/dist/cli/generators/shell.js +50 -4
- package/dist/cli/generators/shell.test.d.ts +1 -0
- package/dist/cli/generators/shell.test.js +73 -0
- package/dist/cli/generators/token-route.d.ts +1 -0
- package/dist/cli/generators/token-route.js +108 -5
- package/dist/cli/generators/wiring-snippets.js +59 -19
- package/dist/cli/generators/wiring-snippets.test.js +15 -1
- package/dist/cli/index.js +108 -151
- package/dist/cli/init.js +110 -17
- package/dist/cli/manifest-diff.d.ts +21 -0
- package/dist/cli/manifest-diff.js +158 -0
- package/dist/cli/manifest-diff.test.d.ts +1 -0
- package/dist/cli/manifest-diff.test.js +104 -0
- package/dist/cli/output-paths.d.ts +2 -0
- package/dist/cli/output-paths.js +22 -4
- package/dist/core/config-policy.test.d.ts +1 -0
- package/dist/core/config-policy.test.js +101 -0
- package/dist/core/config.d.ts +86 -1
- package/dist/core/config.js +120 -2
- package/dist/core/invariants.test.d.ts +1 -0
- package/dist/core/invariants.test.js +217 -0
- package/dist/core/jcs.d.ts +5 -0
- package/dist/core/jcs.js +21 -0
- package/dist/core/receipt.d.ts +70 -0
- package/dist/core/receipt.js +86 -0
- package/dist/core/receipt.test.d.ts +1 -0
- package/dist/core/receipt.test.js +75 -0
- package/dist/core/replay-store.d.ts +51 -0
- package/dist/core/replay-store.js +65 -0
- package/dist/core/token.d.ts +14 -0
- package/dist/core/token.js +50 -18
- package/dist/core/token.test.d.ts +1 -0
- package/dist/core/token.test.js +66 -0
- package/dist/core/types.d.ts +72 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -2
- package/dist/server.d.ts +6 -4
- package/dist/server.js +4 -2
- package/package.json +91 -90
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — duct.config.ts static loader (no arbitrary code exec)
|
|
3
|
+
// ============================================================
|
|
4
|
+
import * as fs from "fs/promises";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import * as vm from "node:vm";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
export class ConfigRuntimeReferenceError extends Error {
|
|
9
|
+
constructor() {
|
|
10
|
+
super("duct.config.ts references runtime values (process, require, fetch, import). " +
|
|
11
|
+
"Use static literals only — push/scan must not execute project code.");
|
|
12
|
+
this.name = "ConfigRuntimeReferenceError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function configSourceHasRuntimeReferences(source) {
|
|
16
|
+
const stripped = source
|
|
17
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
18
|
+
.replace(/\/\/.*$/gm, "");
|
|
19
|
+
return (/\bprocess\b/.test(stripped) ||
|
|
20
|
+
/\brequire\s*\(/.test(stripped) ||
|
|
21
|
+
/\bfetch\s*\(/.test(stripped) ||
|
|
22
|
+
/\bimport\s*\(/.test(stripped));
|
|
23
|
+
}
|
|
24
|
+
export function extractConfigObjectLiteral(source) {
|
|
25
|
+
const marker = "defineDuctConfig(";
|
|
26
|
+
const markerIndex = source.indexOf(marker);
|
|
27
|
+
if (markerIndex === -1)
|
|
28
|
+
return null;
|
|
29
|
+
const startSearch = markerIndex + marker.length;
|
|
30
|
+
let objectStart = -1;
|
|
31
|
+
for (let i = startSearch; i < source.length; i++) {
|
|
32
|
+
const ch = source[i];
|
|
33
|
+
if (!/\s/.test(ch)) {
|
|
34
|
+
if (ch !== "{")
|
|
35
|
+
return null;
|
|
36
|
+
objectStart = i;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (objectStart === -1)
|
|
41
|
+
return null;
|
|
42
|
+
let depth = 0;
|
|
43
|
+
let inSingle = false;
|
|
44
|
+
let inDouble = false;
|
|
45
|
+
let inTemplate = false;
|
|
46
|
+
let inLineComment = false;
|
|
47
|
+
let inBlockComment = false;
|
|
48
|
+
for (let i = objectStart; i < source.length; i++) {
|
|
49
|
+
const ch = source[i];
|
|
50
|
+
const next = source[i + 1] ?? "";
|
|
51
|
+
const prev = source[i - 1] ?? "";
|
|
52
|
+
if (inLineComment) {
|
|
53
|
+
if (ch === "\n")
|
|
54
|
+
inLineComment = false;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (inBlockComment) {
|
|
58
|
+
if (prev === "*" && ch === "/")
|
|
59
|
+
inBlockComment = false;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!inSingle && !inDouble && !inTemplate) {
|
|
63
|
+
if (ch === "/" && next === "/") {
|
|
64
|
+
inLineComment = true;
|
|
65
|
+
i++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === "/" && next === "*") {
|
|
69
|
+
inBlockComment = true;
|
|
70
|
+
i++;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (!inDouble && !inTemplate && ch === "'" && prev !== "\\") {
|
|
75
|
+
inSingle = !inSingle;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!inSingle && !inTemplate && ch === '"' && prev !== "\\") {
|
|
79
|
+
inDouble = !inDouble;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!inSingle && !inDouble && ch === "`" && prev !== "\\") {
|
|
83
|
+
inTemplate = !inTemplate;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (inSingle || inDouble || inTemplate) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (ch === "{")
|
|
90
|
+
depth++;
|
|
91
|
+
if (ch === "}") {
|
|
92
|
+
depth--;
|
|
93
|
+
if (depth === 0) {
|
|
94
|
+
return source.slice(objectStart, i + 1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
function toCompiledCandidates(configPath) {
|
|
101
|
+
const ext = path.extname(configPath).toLowerCase();
|
|
102
|
+
if (ext !== ".ts" && ext !== ".tsx") {
|
|
103
|
+
return [configPath];
|
|
104
|
+
}
|
|
105
|
+
const jsSibling = configPath.replace(/\.(ts|tsx)$/i, ".js");
|
|
106
|
+
const mjsSibling = configPath.replace(/\.(ts|tsx)$/i, ".mjs");
|
|
107
|
+
const cjsSibling = configPath.replace(/\.(ts|tsx)$/i, ".cjs");
|
|
108
|
+
const distStyle = configPath
|
|
109
|
+
.replace(/([\\/])src([\\/])/g, "$1dist$2")
|
|
110
|
+
.replace(/\.(ts|tsx)$/i, ".js");
|
|
111
|
+
return [...new Set([jsSibling, mjsSibling, cjsSibling, distStyle])];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Parse `defineDuctConfig({...})` as a JSON-safe object literal.
|
|
115
|
+
* Rejects configs that reference runtime values; does not expose `process`.
|
|
116
|
+
*/
|
|
117
|
+
export async function loadConfigFromSource(configPath) {
|
|
118
|
+
const source = await fs.readFile(configPath, "utf-8");
|
|
119
|
+
if (configSourceHasRuntimeReferences(source)) {
|
|
120
|
+
throw new ConfigRuntimeReferenceError();
|
|
121
|
+
}
|
|
122
|
+
const objectLiteral = extractConfigObjectLiteral(source);
|
|
123
|
+
if (!objectLiteral) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
if (configSourceHasRuntimeReferences(objectLiteral)) {
|
|
127
|
+
throw new ConfigRuntimeReferenceError();
|
|
128
|
+
}
|
|
129
|
+
const script = new vm.Script(`(${objectLiteral})`, { filename: configPath });
|
|
130
|
+
const evaluated = script.runInNewContext(Object.freeze({ Math, Date, JSON }), {
|
|
131
|
+
timeout: 1000,
|
|
132
|
+
codeGeneration: { strings: false },
|
|
133
|
+
});
|
|
134
|
+
if (!evaluated || typeof evaluated !== "object") {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return evaluated;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Load duct.config.ts / .js without executing untrusted TypeScript source.
|
|
141
|
+
* Prefer AST extraction for `.ts`; fall back to compiled JS siblings only.
|
|
142
|
+
*/
|
|
143
|
+
export async function loadDuctConfig(configPath) {
|
|
144
|
+
const ext = path.extname(configPath).toLowerCase();
|
|
145
|
+
if (ext === ".ts" || ext === ".tsx") {
|
|
146
|
+
try {
|
|
147
|
+
const sourceLoaded = await loadConfigFromSource(configPath);
|
|
148
|
+
if (sourceLoaded) {
|
|
149
|
+
return sourceLoaded;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
if (err instanceof ConfigRuntimeReferenceError) {
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const candidates = toCompiledCandidates(configPath);
|
|
159
|
+
for (const candidate of candidates) {
|
|
160
|
+
if (/\.tsx?$/i.test(candidate)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
await fs.access(candidate);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const module = (await import(pathToFileURL(candidate).href));
|
|
171
|
+
const loaded = module.default ?? module;
|
|
172
|
+
if (loaded && typeof loaded === "object") {
|
|
173
|
+
return loaded;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
// Try next candidate.
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (ext !== ".ts" && ext !== ".tsx") {
|
|
181
|
+
const sourceLoaded = await loadConfigFromSource(configPath);
|
|
182
|
+
if (sourceLoaded) {
|
|
183
|
+
return sourceLoaded;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
throw new Error("CONFIG_LOAD_FAILED");
|
|
187
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import * as fs from "fs/promises";
|
|
4
|
+
import * as os from "os";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import { ConfigRuntimeReferenceError, configSourceHasRuntimeReferences, loadConfigFromSource, } from "./config-loader.js";
|
|
7
|
+
describe("configSourceHasRuntimeReferences", () => {
|
|
8
|
+
it("detects process.exit at top level", () => {
|
|
9
|
+
const source = `process.exit(1);\nexport default defineDuctConfig({ shellId: "x" });`;
|
|
10
|
+
assert.equal(configSourceHasRuntimeReferences(source), true);
|
|
11
|
+
});
|
|
12
|
+
it("allows static defineDuctConfig literals", () => {
|
|
13
|
+
const source = `
|
|
14
|
+
import { defineDuctConfig } from "@duct-sdk/sdk";
|
|
15
|
+
export default defineDuctConfig({
|
|
16
|
+
shellId: "shell_acme",
|
|
17
|
+
product: { name: "Acme", description: "Test app", baseUrl: "https://app.acme.com" },
|
|
18
|
+
});
|
|
19
|
+
`;
|
|
20
|
+
assert.equal(configSourceHasRuntimeReferences(source), false);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe("loadConfigFromSource", () => {
|
|
24
|
+
it("does not execute process.exit during load", async () => {
|
|
25
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "duct-config-"));
|
|
26
|
+
const configPath = path.join(dir, "duct.config.ts");
|
|
27
|
+
const source = `
|
|
28
|
+
process.exit(42);
|
|
29
|
+
import { defineDuctConfig } from "@duct-sdk/sdk";
|
|
30
|
+
export default defineDuctConfig({
|
|
31
|
+
shellId: "shell_bad",
|
|
32
|
+
product: { name: "Bad", description: "Should not run", baseUrl: "https://bad.example" },
|
|
33
|
+
auth: { strategy: "none", sessionFields: ["id"], guestAllowed: true, tokenExpirySeconds: 60 },
|
|
34
|
+
routes: [],
|
|
35
|
+
actions: [],
|
|
36
|
+
deeplinks: [],
|
|
37
|
+
agentPermissions: { defaultLevel: "none", requireUserConsent: true },
|
|
38
|
+
});
|
|
39
|
+
`;
|
|
40
|
+
await fs.writeFile(configPath, source, "utf-8");
|
|
41
|
+
await assert.rejects(() => loadConfigFromSource(configPath), (err) => err instanceof ConfigRuntimeReferenceError);
|
|
42
|
+
});
|
|
43
|
+
it("parses static defineDuctConfig object literal", async () => {
|
|
44
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "duct-config-"));
|
|
45
|
+
const configPath = path.join(dir, "duct.config.ts");
|
|
46
|
+
const source = `
|
|
47
|
+
import { defineDuctConfig } from "@duct-sdk/sdk";
|
|
48
|
+
export default defineDuctConfig({
|
|
49
|
+
shellId: "shell_ok",
|
|
50
|
+
product: { name: "Ok", description: "Static config", baseUrl: "https://ok.example" },
|
|
51
|
+
auth: { strategy: "none", sessionFields: ["id"], guestAllowed: true, tokenExpirySeconds: 60 },
|
|
52
|
+
routes: [],
|
|
53
|
+
actions: [],
|
|
54
|
+
deeplinks: [],
|
|
55
|
+
agentPermissions: { defaultLevel: "none", requireUserConsent: true },
|
|
56
|
+
});
|
|
57
|
+
`;
|
|
58
|
+
await fs.writeFile(configPath, source, "utf-8");
|
|
59
|
+
const loaded = await loadConfigFromSource(configPath);
|
|
60
|
+
assert.ok(loaded);
|
|
61
|
+
assert.equal(loaded.shellId, "shell_ok");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type FrameworkName = "next-app" | "next-pages" | "remix" | "vite-react" | "sveltekit" | "vue3" | "nuxt" | "unknown";
|
|
1
|
+
export type FrameworkName = "next-app" | "next-pages" | "remix" | "vite-react" | "sveltekit" | "vue3" | "nuxt" | "express" | "fastapi" | "unknown";
|
|
2
2
|
export interface FrameworkInfo {
|
|
3
3
|
name: FrameworkName;
|
|
4
4
|
version: string;
|
|
@@ -58,6 +58,27 @@ async function detectPackageManager(projectRoot) {
|
|
|
58
58
|
return "npm";
|
|
59
59
|
return "unknown";
|
|
60
60
|
}
|
|
61
|
+
/** Detect a Python FastAPI backend via requirements.txt / pyproject.toml. */
|
|
62
|
+
async function detectFastApiVersion(projectRoot) {
|
|
63
|
+
try {
|
|
64
|
+
const req = await fs.readFile(path.join(projectRoot, "requirements.txt"), "utf-8");
|
|
65
|
+
const m = req.match(/^\s*fastapi\b\s*([=<>!~]=?\s*[\d.]+)?/im);
|
|
66
|
+
if (m)
|
|
67
|
+
return cleanVersion(m[1] ?? null);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
/* no requirements.txt */
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const toml = await fs.readFile(path.join(projectRoot, "pyproject.toml"), "utf-8");
|
|
74
|
+
if (/(^|\n)\s*fastapi\b/i.test(toml) || /["']fastapi["']/i.test(toml))
|
|
75
|
+
return "unknown";
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
/* no pyproject.toml */
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
61
82
|
// ----------------------------------------------------------------
|
|
62
83
|
// detectFramework
|
|
63
84
|
// ----------------------------------------------------------------
|
|
@@ -80,6 +101,11 @@ export async function detectFramework(projectRoot) {
|
|
|
80
101
|
srcDir: projectRoot,
|
|
81
102
|
};
|
|
82
103
|
if (!pkg) {
|
|
104
|
+
// Pure Python project (no package.json) — check for FastAPI.
|
|
105
|
+
const fastapiVersion = await detectFastApiVersion(projectRoot);
|
|
106
|
+
if (fastapiVersion) {
|
|
107
|
+
return { ...defaultResult, name: "fastapi", version: fastapiVersion };
|
|
108
|
+
}
|
|
83
109
|
return defaultResult;
|
|
84
110
|
}
|
|
85
111
|
// ---- Next.js ----
|
|
@@ -238,5 +264,19 @@ export async function detectFramework(projectRoot) {
|
|
|
238
264
|
configFile,
|
|
239
265
|
};
|
|
240
266
|
}
|
|
267
|
+
// ---- Standalone Express backend (no frontend framework matched above) ----
|
|
268
|
+
const expressVersion = getDependencyVersion(pkg, "express");
|
|
269
|
+
if (expressVersion) {
|
|
270
|
+
return {
|
|
271
|
+
...defaultResult,
|
|
272
|
+
name: "express",
|
|
273
|
+
version: cleanVersion(expressVersion),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
// ---- FastAPI backend alongside a package.json (e.g. tooling/monorepo root) ----
|
|
277
|
+
const fastapiVersion = await detectFastApiVersion(projectRoot);
|
|
278
|
+
if (fastapiVersion) {
|
|
279
|
+
return { ...defaultResult, name: "fastapi", version: fastapiVersion };
|
|
280
|
+
}
|
|
241
281
|
return defaultResult;
|
|
242
282
|
}
|