@mandujs/core 0.3.2 → 0.3.4
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.ko.md +200 -200
- package/README.md +200 -200
- package/package.json +4 -2
- package/src/change/history.ts +145 -0
- package/src/change/index.ts +40 -0
- package/src/change/integrity.ts +81 -0
- package/src/change/snapshot.ts +233 -0
- package/src/change/transaction.ts +293 -0
- package/src/change/types.ts +102 -0
- package/src/error/classifier.ts +314 -0
- package/src/error/formatter.ts +237 -0
- package/src/error/index.ts +25 -0
- package/src/error/stack-analyzer.ts +295 -0
- package/src/error/types.ts +140 -0
- package/src/filling/context.ts +228 -219
- package/src/filling/filling.ts +256 -234
- package/src/filling/index.ts +7 -7
- package/src/generator/generate.ts +85 -3
- package/src/generator/index.ts +2 -2
- package/src/guard/auto-correct.ts +257 -203
- package/src/index.ts +3 -0
- package/src/report/index.ts +1 -1
- package/src/runtime/index.ts +3 -3
- package/src/runtime/router.ts +65 -65
- package/src/runtime/server.ts +189 -139
- package/src/runtime/ssr.ts +38 -38
- package/src/slot/corrector.ts +282 -0
- package/src/slot/index.ts +18 -0
- package/src/slot/validator.ts +241 -0
- package/src/spec/index.ts +3 -3
- package/src/spec/load.ts +76 -76
- package/src/spec/lock.ts +56 -56
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot Module
|
|
3
|
+
* 슬롯 파일 검증 및 자동 수정 기능
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
validateSlotContent,
|
|
8
|
+
summarizeValidationIssues,
|
|
9
|
+
type SlotValidationIssue,
|
|
10
|
+
type SlotValidationResult,
|
|
11
|
+
} from "./validator";
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
correctSlotContent,
|
|
15
|
+
runSlotCorrection,
|
|
16
|
+
type CorrectionResult,
|
|
17
|
+
type AppliedFix,
|
|
18
|
+
} from "./corrector";
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot Content Validator
|
|
3
|
+
* 슬롯 파일 내용을 작성 전에 검증하고 문제를 식별합니다.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface SlotValidationIssue {
|
|
7
|
+
code: string;
|
|
8
|
+
severity: "error" | "warning";
|
|
9
|
+
message: string;
|
|
10
|
+
line?: number;
|
|
11
|
+
suggestion: string;
|
|
12
|
+
autoFixable: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SlotValidationResult {
|
|
16
|
+
valid: boolean;
|
|
17
|
+
issues: SlotValidationIssue[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 금지된 import 모듈들
|
|
21
|
+
const FORBIDDEN_IMPORTS = [
|
|
22
|
+
"fs",
|
|
23
|
+
"child_process",
|
|
24
|
+
"cluster",
|
|
25
|
+
"worker_threads",
|
|
26
|
+
"node:fs",
|
|
27
|
+
"node:child_process",
|
|
28
|
+
"node:cluster",
|
|
29
|
+
"node:worker_threads",
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
// 필수 패턴들
|
|
33
|
+
const REQUIRED_PATTERNS = {
|
|
34
|
+
manduImport: /import\s+.*\bMandu\b.*from\s+['"]@mandujs\/core['"]/,
|
|
35
|
+
fillingPattern: /Mandu\s*\.\s*filling\s*\(\s*\)/,
|
|
36
|
+
defaultExport: /export\s+default\b/,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 슬롯 내용을 검증합니다.
|
|
41
|
+
*/
|
|
42
|
+
export function validateSlotContent(content: string): SlotValidationResult {
|
|
43
|
+
const issues: SlotValidationIssue[] = [];
|
|
44
|
+
const lines = content.split("\n");
|
|
45
|
+
|
|
46
|
+
// 1. 금지된 import 검사
|
|
47
|
+
for (let i = 0; i < lines.length; i++) {
|
|
48
|
+
const line = lines[i];
|
|
49
|
+
for (const forbidden of FORBIDDEN_IMPORTS) {
|
|
50
|
+
// import 문에서 금지된 모듈 체크
|
|
51
|
+
const importPattern = new RegExp(
|
|
52
|
+
`import\\s+.*from\\s+['"]${forbidden.replace("/", "\\/")}['"]`
|
|
53
|
+
);
|
|
54
|
+
const requirePattern = new RegExp(
|
|
55
|
+
`require\\s*\\(\\s*['"]${forbidden.replace("/", "\\/")}['"]\\s*\\)`
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (importPattern.test(line) || requirePattern.test(line)) {
|
|
59
|
+
issues.push({
|
|
60
|
+
code: "FORBIDDEN_IMPORT",
|
|
61
|
+
severity: "error",
|
|
62
|
+
message: `금지된 모듈 import: '${forbidden}'`,
|
|
63
|
+
line: i + 1,
|
|
64
|
+
suggestion: `'${forbidden}' 대신 Bun의 안전한 API 또는 adapter를 사용하세요`,
|
|
65
|
+
autoFixable: true,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 2. Mandu import 검사
|
|
72
|
+
if (!REQUIRED_PATTERNS.manduImport.test(content)) {
|
|
73
|
+
issues.push({
|
|
74
|
+
code: "MISSING_MANDU_IMPORT",
|
|
75
|
+
severity: "error",
|
|
76
|
+
message: "Mandu import가 없습니다",
|
|
77
|
+
suggestion: "import { Mandu } from '@mandujs/core' 추가 필요",
|
|
78
|
+
autoFixable: true,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 3. Mandu.filling() 패턴 검사
|
|
83
|
+
if (!REQUIRED_PATTERNS.fillingPattern.test(content)) {
|
|
84
|
+
issues.push({
|
|
85
|
+
code: "MISSING_FILLING_PATTERN",
|
|
86
|
+
severity: "error",
|
|
87
|
+
message: "Mandu.filling() 패턴이 없습니다",
|
|
88
|
+
suggestion: "슬롯은 Mandu.filling()으로 시작해야 합니다",
|
|
89
|
+
autoFixable: false,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 4. default export 검사
|
|
94
|
+
if (!REQUIRED_PATTERNS.defaultExport.test(content)) {
|
|
95
|
+
issues.push({
|
|
96
|
+
code: "MISSING_DEFAULT_EXPORT",
|
|
97
|
+
severity: "error",
|
|
98
|
+
message: "default export가 없습니다",
|
|
99
|
+
suggestion: "export default Mandu.filling()... 형태로 작성하세요",
|
|
100
|
+
autoFixable: true,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 5. 기본 문법 검사 (간단한 체크)
|
|
105
|
+
const syntaxIssues = checkBasicSyntax(content, lines);
|
|
106
|
+
issues.push(...syntaxIssues);
|
|
107
|
+
|
|
108
|
+
// 6. HTTP 메서드 핸들러 검사
|
|
109
|
+
const methodIssues = checkHttpMethods(content);
|
|
110
|
+
issues.push(...methodIssues);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
valid: issues.filter((i) => i.severity === "error").length === 0,
|
|
114
|
+
issues,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 기본 문법 검사
|
|
120
|
+
*/
|
|
121
|
+
function checkBasicSyntax(
|
|
122
|
+
content: string,
|
|
123
|
+
lines: string[]
|
|
124
|
+
): SlotValidationIssue[] {
|
|
125
|
+
const issues: SlotValidationIssue[] = [];
|
|
126
|
+
|
|
127
|
+
// 괄호 균형 체크
|
|
128
|
+
const brackets = { "(": 0, "{": 0, "[": 0 };
|
|
129
|
+
const bracketPairs: Record<string, keyof typeof brackets> = {
|
|
130
|
+
")": "(",
|
|
131
|
+
"}": "{",
|
|
132
|
+
"]": "[",
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < lines.length; i++) {
|
|
136
|
+
const line = lines[i];
|
|
137
|
+
// 문자열 내부는 스킵 (간단한 처리)
|
|
138
|
+
const withoutStrings = line
|
|
139
|
+
.replace(/"[^"]*"/g, "")
|
|
140
|
+
.replace(/'[^']*'/g, "")
|
|
141
|
+
.replace(/`[^`]*`/g, "");
|
|
142
|
+
|
|
143
|
+
for (const char of withoutStrings) {
|
|
144
|
+
if (char in brackets) {
|
|
145
|
+
brackets[char as keyof typeof brackets]++;
|
|
146
|
+
} else if (char in bracketPairs) {
|
|
147
|
+
brackets[bracketPairs[char]]--;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (brackets["("] !== 0) {
|
|
153
|
+
issues.push({
|
|
154
|
+
code: "UNBALANCED_PARENTHESES",
|
|
155
|
+
severity: "error",
|
|
156
|
+
message: `괄호 불균형: ${brackets["("] > 0 ? "닫는" : "여는"} 괄호 부족`,
|
|
157
|
+
suggestion: "괄호 쌍을 확인하세요",
|
|
158
|
+
autoFixable: false,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (brackets["{"] !== 0) {
|
|
163
|
+
issues.push({
|
|
164
|
+
code: "UNBALANCED_BRACES",
|
|
165
|
+
severity: "error",
|
|
166
|
+
message: `중괄호 불균형: ${brackets["{"] > 0 ? "닫는" : "여는"} 중괄호 부족`,
|
|
167
|
+
suggestion: "중괄호 쌍을 확인하세요",
|
|
168
|
+
autoFixable: false,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (brackets["["] !== 0) {
|
|
173
|
+
issues.push({
|
|
174
|
+
code: "UNBALANCED_BRACKETS",
|
|
175
|
+
severity: "error",
|
|
176
|
+
message: `대괄호 불균형: ${brackets["["] > 0 ? "닫는" : "여는"} 대괄호 부족`,
|
|
177
|
+
suggestion: "대괄호 쌍을 확인하세요",
|
|
178
|
+
autoFixable: false,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return issues;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* HTTP 메서드 핸들러 검사
|
|
187
|
+
*/
|
|
188
|
+
function checkHttpMethods(content: string): SlotValidationIssue[] {
|
|
189
|
+
const issues: SlotValidationIssue[] = [];
|
|
190
|
+
|
|
191
|
+
// .get(), .post() 등의 핸들러가 있는지 확인
|
|
192
|
+
const methodPattern = /\.(get|post|put|patch|delete|options|head)\s*\(/gi;
|
|
193
|
+
const hasMethod = methodPattern.test(content);
|
|
194
|
+
|
|
195
|
+
if (!hasMethod) {
|
|
196
|
+
issues.push({
|
|
197
|
+
code: "NO_HTTP_HANDLER",
|
|
198
|
+
severity: "warning",
|
|
199
|
+
message: "HTTP 메서드 핸들러가 없습니다",
|
|
200
|
+
suggestion:
|
|
201
|
+
".get(ctx => ...), .post(ctx => ...) 등의 핸들러를 추가하세요",
|
|
202
|
+
autoFixable: false,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ctx.ok(), ctx.json() 등 응답 패턴 확인
|
|
207
|
+
const responsePattern = /ctx\s*\.\s*(ok|json|created|noContent|error|html)\s*\(/;
|
|
208
|
+
if (hasMethod && !responsePattern.test(content)) {
|
|
209
|
+
issues.push({
|
|
210
|
+
code: "NO_RESPONSE_PATTERN",
|
|
211
|
+
severity: "warning",
|
|
212
|
+
message: "응답 패턴이 없습니다",
|
|
213
|
+
suggestion:
|
|
214
|
+
"핸들러에서 ctx.ok(), ctx.json() 등으로 응답을 반환하세요",
|
|
215
|
+
autoFixable: false,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return issues;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 에러 요약 생성
|
|
224
|
+
*/
|
|
225
|
+
export function summarizeValidationIssues(
|
|
226
|
+
issues: SlotValidationIssue[]
|
|
227
|
+
): string {
|
|
228
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
229
|
+
const warnings = issues.filter((i) => i.severity === "warning");
|
|
230
|
+
|
|
231
|
+
const parts: string[] = [];
|
|
232
|
+
|
|
233
|
+
if (errors.length > 0) {
|
|
234
|
+
parts.push(`${errors.length}개 에러`);
|
|
235
|
+
}
|
|
236
|
+
if (warnings.length > 0) {
|
|
237
|
+
parts.push(`${warnings.length}개 경고`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return parts.join(", ") || "문제 없음";
|
|
241
|
+
}
|
package/src/spec/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./schema";
|
|
2
|
-
export * from "./load";
|
|
3
|
-
export * from "./lock";
|
|
1
|
+
export * from "./schema";
|
|
2
|
+
export * from "./load";
|
|
3
|
+
export * from "./lock";
|
package/src/spec/load.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import { RoutesManifest, type RoutesManifest as RoutesManifestType } from "./schema";
|
|
2
|
-
import { ZodError } from "zod";
|
|
3
|
-
|
|
4
|
-
export interface LoadResult {
|
|
5
|
-
success: boolean;
|
|
6
|
-
data?: RoutesManifestType;
|
|
7
|
-
errors?: string[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function formatZodError(error: ZodError): string[] {
|
|
11
|
-
return error.errors.map((e) => {
|
|
12
|
-
const path = e.path.length > 0 ? `[${e.path.join(".")}] ` : "";
|
|
13
|
-
return `${path}${e.message}`;
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export async function loadManifest(filePath: string): Promise<LoadResult> {
|
|
18
|
-
try {
|
|
19
|
-
const file = Bun.file(filePath);
|
|
20
|
-
const exists = await file.exists();
|
|
21
|
-
|
|
22
|
-
if (!exists) {
|
|
23
|
-
return {
|
|
24
|
-
success: false,
|
|
25
|
-
errors: [`파일을 찾을 수 없습니다: ${filePath}`],
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const content = await file.text();
|
|
30
|
-
let json: unknown;
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
json = JSON.parse(content);
|
|
34
|
-
} catch {
|
|
35
|
-
return {
|
|
36
|
-
success: false,
|
|
37
|
-
errors: ["JSON 파싱 실패: 올바른 JSON 형식이 아닙니다"],
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const result = RoutesManifest.safeParse(json);
|
|
42
|
-
|
|
43
|
-
if (!result.success) {
|
|
44
|
-
return {
|
|
45
|
-
success: false,
|
|
46
|
-
errors: formatZodError(result.error),
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
success: true,
|
|
52
|
-
data: result.data,
|
|
53
|
-
};
|
|
54
|
-
} catch (error) {
|
|
55
|
-
return {
|
|
56
|
-
success: false,
|
|
57
|
-
errors: [`예상치 못한 오류: ${error instanceof Error ? error.message : String(error)}`],
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function validateManifest(data: unknown): LoadResult {
|
|
63
|
-
const result = RoutesManifest.safeParse(data);
|
|
64
|
-
|
|
65
|
-
if (!result.success) {
|
|
66
|
-
return {
|
|
67
|
-
success: false,
|
|
68
|
-
errors: formatZodError(result.error),
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
success: true,
|
|
74
|
-
data: result.data,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
1
|
+
import { RoutesManifest, type RoutesManifest as RoutesManifestType } from "./schema";
|
|
2
|
+
import { ZodError } from "zod";
|
|
3
|
+
|
|
4
|
+
export interface LoadResult {
|
|
5
|
+
success: boolean;
|
|
6
|
+
data?: RoutesManifestType;
|
|
7
|
+
errors?: string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function formatZodError(error: ZodError): string[] {
|
|
11
|
+
return error.errors.map((e) => {
|
|
12
|
+
const path = e.path.length > 0 ? `[${e.path.join(".")}] ` : "";
|
|
13
|
+
return `${path}${e.message}`;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function loadManifest(filePath: string): Promise<LoadResult> {
|
|
18
|
+
try {
|
|
19
|
+
const file = Bun.file(filePath);
|
|
20
|
+
const exists = await file.exists();
|
|
21
|
+
|
|
22
|
+
if (!exists) {
|
|
23
|
+
return {
|
|
24
|
+
success: false,
|
|
25
|
+
errors: [`파일을 찾을 수 없습니다: ${filePath}`],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const content = await file.text();
|
|
30
|
+
let json: unknown;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
json = JSON.parse(content);
|
|
34
|
+
} catch {
|
|
35
|
+
return {
|
|
36
|
+
success: false,
|
|
37
|
+
errors: ["JSON 파싱 실패: 올바른 JSON 형식이 아닙니다"],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const result = RoutesManifest.safeParse(json);
|
|
42
|
+
|
|
43
|
+
if (!result.success) {
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
errors: formatZodError(result.error),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
success: true,
|
|
52
|
+
data: result.data,
|
|
53
|
+
};
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
errors: [`예상치 못한 오류: ${error instanceof Error ? error.message : String(error)}`],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function validateManifest(data: unknown): LoadResult {
|
|
63
|
+
const result = RoutesManifest.safeParse(data);
|
|
64
|
+
|
|
65
|
+
if (!result.success) {
|
|
66
|
+
return {
|
|
67
|
+
success: false,
|
|
68
|
+
errors: formatZodError(result.error),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
success: true,
|
|
74
|
+
data: result.data,
|
|
75
|
+
};
|
|
76
|
+
}
|
package/src/spec/lock.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { createHash } from "crypto";
|
|
2
|
-
import type { RoutesManifest } from "./schema";
|
|
3
|
-
|
|
4
|
-
export interface SpecLock {
|
|
5
|
-
routesHash: string;
|
|
6
|
-
updatedAt: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function computeHash(manifest: RoutesManifest): string {
|
|
10
|
-
const content = JSON.stringify(manifest, null, 2);
|
|
11
|
-
return createHash("sha256").update(content).digest("hex");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export async function readLock(lockPath: string): Promise<SpecLock | null> {
|
|
15
|
-
try {
|
|
16
|
-
const file = Bun.file(lockPath);
|
|
17
|
-
const exists = await file.exists();
|
|
18
|
-
|
|
19
|
-
if (!exists) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const content = await file.text();
|
|
24
|
-
return JSON.parse(content) as SpecLock;
|
|
25
|
-
} catch {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export async function writeLock(lockPath: string, manifest: RoutesManifest): Promise<SpecLock> {
|
|
31
|
-
const lock: SpecLock = {
|
|
32
|
-
routesHash: computeHash(manifest),
|
|
33
|
-
updatedAt: new Date().toISOString(),
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
await Bun.write(lockPath, JSON.stringify(lock, null, 2));
|
|
37
|
-
return lock;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export async function verifyLock(
|
|
41
|
-
lockPath: string,
|
|
42
|
-
manifest: RoutesManifest
|
|
43
|
-
): Promise<{ valid: boolean; currentHash: string; lockHash: string | null }> {
|
|
44
|
-
const currentHash = computeHash(manifest);
|
|
45
|
-
const lock = await readLock(lockPath);
|
|
46
|
-
|
|
47
|
-
if (!lock) {
|
|
48
|
-
return { valid: false, currentHash, lockHash: null };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
valid: lock.routesHash === currentHash,
|
|
53
|
-
currentHash,
|
|
54
|
-
lockHash: lock.routesHash,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
1
|
+
import { createHash } from "crypto";
|
|
2
|
+
import type { RoutesManifest } from "./schema";
|
|
3
|
+
|
|
4
|
+
export interface SpecLock {
|
|
5
|
+
routesHash: string;
|
|
6
|
+
updatedAt: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function computeHash(manifest: RoutesManifest): string {
|
|
10
|
+
const content = JSON.stringify(manifest, null, 2);
|
|
11
|
+
return createHash("sha256").update(content).digest("hex");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function readLock(lockPath: string): Promise<SpecLock | null> {
|
|
15
|
+
try {
|
|
16
|
+
const file = Bun.file(lockPath);
|
|
17
|
+
const exists = await file.exists();
|
|
18
|
+
|
|
19
|
+
if (!exists) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const content = await file.text();
|
|
24
|
+
return JSON.parse(content) as SpecLock;
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function writeLock(lockPath: string, manifest: RoutesManifest): Promise<SpecLock> {
|
|
31
|
+
const lock: SpecLock = {
|
|
32
|
+
routesHash: computeHash(manifest),
|
|
33
|
+
updatedAt: new Date().toISOString(),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
await Bun.write(lockPath, JSON.stringify(lock, null, 2));
|
|
37
|
+
return lock;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function verifyLock(
|
|
41
|
+
lockPath: string,
|
|
42
|
+
manifest: RoutesManifest
|
|
43
|
+
): Promise<{ valid: boolean; currentHash: string; lockHash: string | null }> {
|
|
44
|
+
const currentHash = computeHash(manifest);
|
|
45
|
+
const lock = await readLock(lockPath);
|
|
46
|
+
|
|
47
|
+
if (!lock) {
|
|
48
|
+
return { valid: false, currentHash, lockHash: null };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
valid: lock.routesHash === currentHash,
|
|
53
|
+
currentHash,
|
|
54
|
+
lockHash: lock.routesHash,
|
|
55
|
+
};
|
|
56
|
+
}
|