@mandujs/core 0.9.41 → 0.9.42
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/package.json +1 -1
- package/src/bundler/build.ts +91 -73
- package/src/bundler/dev.ts +21 -14
- package/src/client/globals.ts +44 -0
- package/src/client/index.ts +5 -4
- package/src/client/island.ts +8 -13
- package/src/client/router.ts +33 -41
- package/src/client/runtime.ts +23 -51
- package/src/client/window-state.ts +101 -0
- package/src/config/index.ts +1 -0
- package/src/config/mandu.ts +45 -9
- package/src/config/validate.ts +158 -0
- package/src/constants.ts +25 -0
- package/src/contract/client.ts +4 -3
- package/src/contract/define.ts +459 -0
- package/src/devtools/ai/context-builder.ts +375 -0
- package/src/devtools/ai/index.ts +25 -0
- package/src/devtools/ai/mcp-connector.ts +465 -0
- package/src/devtools/client/catchers/error-catcher.ts +327 -0
- package/src/devtools/client/catchers/index.ts +18 -0
- package/src/devtools/client/catchers/network-proxy.ts +363 -0
- package/src/devtools/client/components/index.ts +39 -0
- package/src/devtools/client/components/kitchen-root.tsx +362 -0
- package/src/devtools/client/components/mandu-character.tsx +241 -0
- package/src/devtools/client/components/overlay.tsx +368 -0
- package/src/devtools/client/components/panel/errors-panel.tsx +259 -0
- package/src/devtools/client/components/panel/guard-panel.tsx +244 -0
- package/src/devtools/client/components/panel/index.ts +32 -0
- package/src/devtools/client/components/panel/islands-panel.tsx +304 -0
- package/src/devtools/client/components/panel/network-panel.tsx +292 -0
- package/src/devtools/client/components/panel/panel-container.tsx +259 -0
- package/src/devtools/client/filters/context-filters.ts +282 -0
- package/src/devtools/client/filters/index.ts +16 -0
- package/src/devtools/client/index.ts +63 -0
- package/src/devtools/client/persistence.ts +335 -0
- package/src/devtools/client/state-manager.ts +478 -0
- package/src/devtools/design-tokens.ts +263 -0
- package/src/devtools/hook/create-hook.ts +207 -0
- package/src/devtools/hook/index.ts +13 -0
- package/src/devtools/index.ts +439 -0
- package/src/devtools/init.ts +266 -0
- package/src/devtools/protocol.ts +237 -0
- package/src/devtools/server/index.ts +17 -0
- package/src/devtools/server/source-context.ts +444 -0
- package/src/devtools/types.ts +319 -0
- package/src/devtools/worker/index.ts +25 -0
- package/src/devtools/worker/redaction-worker.ts +222 -0
- package/src/devtools/worker/worker-manager.ts +409 -0
- package/src/error/formatter.ts +28 -24
- package/src/error/index.ts +13 -9
- package/src/error/result.ts +46 -0
- package/src/error/types.ts +6 -4
- package/src/filling/filling.ts +6 -5
- package/src/guard/check.ts +60 -56
- package/src/guard/types.ts +3 -1
- package/src/guard/watcher.ts +10 -1
- package/src/index.ts +81 -0
- package/src/intent/index.ts +310 -0
- package/src/island/index.ts +304 -0
- package/src/router/fs-patterns.ts +7 -0
- package/src/router/fs-routes.ts +20 -8
- package/src/router/fs-scanner.ts +117 -133
- package/src/runtime/server.ts +261 -201
- package/src/runtime/ssr.ts +5 -4
- package/src/runtime/streaming-ssr.ts +5 -4
- package/src/utils/bun.ts +8 -0
- package/src/utils/lru-cache.ts +75 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Kitchen DevTools - Worker Manager
|
|
3
|
+
* @version 1.1.0
|
|
4
|
+
*
|
|
5
|
+
* Worker 생명주기 관리, Fallback 정책, 타임아웃 처리
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { WorkerPolicy, RedactPattern } from '../types';
|
|
9
|
+
import type { WorkerRequest, WorkerResponse } from './redaction-worker';
|
|
10
|
+
import { redactText, truncateText } from './redaction-worker';
|
|
11
|
+
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Types
|
|
14
|
+
// ============================================================================
|
|
15
|
+
|
|
16
|
+
export type WorkerStatus = 'idle' | 'loading' | 'ready' | 'error' | 'disabled';
|
|
17
|
+
|
|
18
|
+
export interface WorkerManagerOptions {
|
|
19
|
+
/** Worker 정책 */
|
|
20
|
+
policy?: Partial<WorkerPolicy>;
|
|
21
|
+
/** Worker 스크립트 URL (기본: 인라인) */
|
|
22
|
+
workerUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Constants
|
|
27
|
+
// ============================================================================
|
|
28
|
+
|
|
29
|
+
const DEFAULT_POLICY: WorkerPolicy = {
|
|
30
|
+
timeout: 3000,
|
|
31
|
+
onTimeout: 'fallback-main',
|
|
32
|
+
onError: 'retry-once',
|
|
33
|
+
maxConsecutiveFailures: 3,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// Worker Manager
|
|
38
|
+
// ============================================================================
|
|
39
|
+
|
|
40
|
+
export class WorkerManager {
|
|
41
|
+
private worker: Worker | null = null;
|
|
42
|
+
private status: WorkerStatus = 'idle';
|
|
43
|
+
private policy: WorkerPolicy;
|
|
44
|
+
private pendingRequests = new Map<string, {
|
|
45
|
+
resolve: (response: WorkerResponse) => void;
|
|
46
|
+
reject: (error: Error) => void;
|
|
47
|
+
timeout: ReturnType<typeof setTimeout>;
|
|
48
|
+
}>();
|
|
49
|
+
private requestIdCounter = 0;
|
|
50
|
+
private consecutiveFailures = 0;
|
|
51
|
+
private workerUrl?: string;
|
|
52
|
+
private listeners: Set<(status: WorkerStatus) => void> = new Set();
|
|
53
|
+
|
|
54
|
+
constructor(options: WorkerManagerOptions = {}) {
|
|
55
|
+
this.policy = { ...DEFAULT_POLICY, ...options.policy };
|
|
56
|
+
this.workerUrl = options.workerUrl;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --------------------------------------------------------------------------
|
|
60
|
+
// Lifecycle
|
|
61
|
+
// --------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Worker 초기화
|
|
65
|
+
*/
|
|
66
|
+
async initialize(): Promise<boolean> {
|
|
67
|
+
if (this.status === 'disabled') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.status === 'ready' && this.worker) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.setStatus('loading');
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
// Worker 생성
|
|
79
|
+
if (this.workerUrl) {
|
|
80
|
+
this.worker = new Worker(this.workerUrl, { type: 'module' });
|
|
81
|
+
} else {
|
|
82
|
+
// 인라인 Worker (번들링된 코드 사용)
|
|
83
|
+
this.worker = this.createInlineWorker();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!this.worker) {
|
|
87
|
+
throw new Error('Worker creation failed');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 메시지 핸들러 설정
|
|
91
|
+
this.worker.onmessage = this.handleWorkerMessage.bind(this);
|
|
92
|
+
this.worker.onerror = this.handleWorkerError.bind(this);
|
|
93
|
+
|
|
94
|
+
// Ping 테스트
|
|
95
|
+
const pingResult = await this.sendRequest({ type: 'ping', data: {} });
|
|
96
|
+
if (!pingResult.success) {
|
|
97
|
+
throw new Error('Worker ping failed');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
this.setStatus('ready');
|
|
101
|
+
this.consecutiveFailures = 0;
|
|
102
|
+
return true;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.warn('[Mandu Kitchen] Worker initialization failed:', error);
|
|
105
|
+
this.handleFailure();
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Worker 종료
|
|
112
|
+
*/
|
|
113
|
+
terminate(): void {
|
|
114
|
+
if (this.worker) {
|
|
115
|
+
this.worker.terminate();
|
|
116
|
+
this.worker = null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 대기 중인 요청 모두 reject
|
|
120
|
+
for (const [id, pending] of this.pendingRequests) {
|
|
121
|
+
clearTimeout(pending.timeout);
|
|
122
|
+
pending.reject(new Error('Worker terminated'));
|
|
123
|
+
}
|
|
124
|
+
this.pendingRequests.clear();
|
|
125
|
+
|
|
126
|
+
this.setStatus('idle');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Worker 비활성화 (복구 불가)
|
|
131
|
+
*/
|
|
132
|
+
disable(): void {
|
|
133
|
+
this.terminate();
|
|
134
|
+
this.setStatus('disabled');
|
|
135
|
+
console.warn('[Mandu Kitchen] Worker disabled due to consecutive failures');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// --------------------------------------------------------------------------
|
|
139
|
+
// Public API
|
|
140
|
+
// --------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 텍스트 리댁션 (Worker 또는 Fallback)
|
|
144
|
+
*/
|
|
145
|
+
async redact(text: string, patterns?: RedactPattern[]): Promise<string> {
|
|
146
|
+
// Worker 사용 불가면 메인 스레드에서 처리
|
|
147
|
+
if (!this.isAvailable()) {
|
|
148
|
+
return this.fallbackRedact(text, patterns);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const response = await this.sendRequest({
|
|
153
|
+
type: 'redact',
|
|
154
|
+
data: { text, patterns },
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (response.success && response.result !== undefined) {
|
|
158
|
+
this.consecutiveFailures = 0;
|
|
159
|
+
return response.result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
throw new Error(response.error ?? 'Redaction failed');
|
|
163
|
+
} catch (error) {
|
|
164
|
+
return this.handleRequestError(error, () => this.fallbackRedact(text, patterns));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 텍스트 Truncation (Worker 또는 Fallback)
|
|
170
|
+
*/
|
|
171
|
+
async truncate(text: string, maxBytes: number): Promise<string> {
|
|
172
|
+
if (!this.isAvailable()) {
|
|
173
|
+
return this.fallbackTruncate(text, maxBytes);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const response = await this.sendRequest({
|
|
178
|
+
type: 'truncate',
|
|
179
|
+
data: { text, maxBytes },
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (response.success && response.result !== undefined) {
|
|
183
|
+
this.consecutiveFailures = 0;
|
|
184
|
+
return response.result;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
throw new Error(response.error ?? 'Truncation failed');
|
|
188
|
+
} catch (error) {
|
|
189
|
+
return this.handleRequestError(error, () => this.fallbackTruncate(text, maxBytes));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Worker 상태 조회
|
|
195
|
+
*/
|
|
196
|
+
getStatus(): WorkerStatus {
|
|
197
|
+
return this.status;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Worker 사용 가능 여부
|
|
202
|
+
*/
|
|
203
|
+
isAvailable(): boolean {
|
|
204
|
+
return this.status === 'ready' && this.worker !== null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 상태 변경 리스너 등록
|
|
209
|
+
*/
|
|
210
|
+
onStatusChange(listener: (status: WorkerStatus) => void): () => void {
|
|
211
|
+
this.listeners.add(listener);
|
|
212
|
+
return () => this.listeners.delete(listener);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// --------------------------------------------------------------------------
|
|
216
|
+
// Internal
|
|
217
|
+
// --------------------------------------------------------------------------
|
|
218
|
+
|
|
219
|
+
private setStatus(status: WorkerStatus): void {
|
|
220
|
+
if (this.status !== status) {
|
|
221
|
+
this.status = status;
|
|
222
|
+
for (const listener of this.listeners) {
|
|
223
|
+
try {
|
|
224
|
+
listener(status);
|
|
225
|
+
} catch {
|
|
226
|
+
// 리스너 에러 무시
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private createInlineWorker(): Worker | null {
|
|
233
|
+
// 인라인 Worker 생성을 위한 코드
|
|
234
|
+
// 실제 프로덕션에서는 번들된 Worker 파일을 사용하는 것이 좋음
|
|
235
|
+
const workerCode = `
|
|
236
|
+
const BUILT_IN_SECRET_PATTERNS = [
|
|
237
|
+
{ source: 'eyJ[A-Za-z0-9_-]{10,}\\\\.[A-Za-z0-9_-]{10,}\\\\.[A-Za-z0-9_-]{10,}', label: 'JWT' },
|
|
238
|
+
{ source: 'AKIA[0-9A-Z]{16}', label: 'AWS_KEY' },
|
|
239
|
+
{ source: '(?:api[_-]?key|apikey)["\\']?\\\\s*[:=]\\\\s*["\\']?[A-Za-z0-9_-]{20,}', flags: 'i', label: 'API_KEY' },
|
|
240
|
+
{ source: 'Bearer\\\\s+[A-Za-z0-9_-]{20,}', label: 'BEARER' },
|
|
241
|
+
{ source: '(?:secret|password|passwd|pwd)["\\']?\\\\s*[:=]\\\\s*["\\']?[^\\\\s"\\'\\']{8,}', flags: 'i', label: 'SECRET' },
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
const PII_PATTERNS = [
|
|
245
|
+
{ source: '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}', label: 'EMAIL' },
|
|
246
|
+
{ source: '\\\\+?[1-9]\\\\d{1,14}', label: 'PHONE' },
|
|
247
|
+
{ source: '\\\\b(?:\\\\d{1,3}\\\\.){3}\\\\d{1,3}\\\\b', label: 'IP' },
|
|
248
|
+
];
|
|
249
|
+
|
|
250
|
+
function applyPattern(text, pattern) {
|
|
251
|
+
try {
|
|
252
|
+
const regex = new RegExp(pattern.source, pattern.flags ?? 'g');
|
|
253
|
+
const replacement = pattern.replacement ?? '[' + (pattern.label ?? 'REDACTED') + ']';
|
|
254
|
+
return text.replace(regex, replacement);
|
|
255
|
+
} catch {
|
|
256
|
+
return text;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function redactText(text, patterns = []) {
|
|
261
|
+
let result = text;
|
|
262
|
+
for (const p of BUILT_IN_SECRET_PATTERNS) result = applyPattern(result, p);
|
|
263
|
+
for (const p of PII_PATTERNS) result = applyPattern(result, p);
|
|
264
|
+
for (const p of patterns) result = applyPattern(result, p);
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function truncateText(text, maxBytes) {
|
|
269
|
+
const encoder = new TextEncoder();
|
|
270
|
+
const bytes = encoder.encode(text);
|
|
271
|
+
if (bytes.length <= maxBytes) return text;
|
|
272
|
+
let truncatedBytes = bytes.slice(0, maxBytes - 3);
|
|
273
|
+
while (truncatedBytes.length > 0 && (truncatedBytes[truncatedBytes.length - 1] & 0xc0) === 0x80) {
|
|
274
|
+
truncatedBytes = truncatedBytes.slice(0, -1);
|
|
275
|
+
}
|
|
276
|
+
return new TextDecoder().decode(truncatedBytes) + '...';
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
self.onmessage = (event) => {
|
|
280
|
+
const { id, type, data } = event.data;
|
|
281
|
+
const start = performance.now();
|
|
282
|
+
try {
|
|
283
|
+
let result;
|
|
284
|
+
if (type === 'ping') result = 'pong';
|
|
285
|
+
else if (type === 'redact') result = redactText(data.text ?? '', data.patterns ?? []);
|
|
286
|
+
else if (type === 'truncate') result = truncateText(data.text ?? '', data.maxBytes ?? 10000);
|
|
287
|
+
else throw new Error('Unknown type: ' + type);
|
|
288
|
+
self.postMessage({ id, success: true, result, timing: performance.now() - start });
|
|
289
|
+
} catch (e) {
|
|
290
|
+
self.postMessage({ id, success: false, error: e.message });
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
`;
|
|
294
|
+
|
|
295
|
+
try {
|
|
296
|
+
const blob = new Blob([workerCode], { type: 'application/javascript' });
|
|
297
|
+
const url = URL.createObjectURL(blob);
|
|
298
|
+
const worker = new Worker(url);
|
|
299
|
+
|
|
300
|
+
// URL 정리
|
|
301
|
+
URL.revokeObjectURL(url);
|
|
302
|
+
|
|
303
|
+
return worker;
|
|
304
|
+
} catch {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private async sendRequest(
|
|
310
|
+
request: Omit<WorkerRequest, 'id'>
|
|
311
|
+
): Promise<WorkerResponse> {
|
|
312
|
+
if (!this.worker) {
|
|
313
|
+
throw new Error('Worker not initialized');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const id = `req-${++this.requestIdCounter}`;
|
|
317
|
+
|
|
318
|
+
return new Promise<WorkerResponse>((resolve, reject) => {
|
|
319
|
+
const timeout = setTimeout(() => {
|
|
320
|
+
this.pendingRequests.delete(id);
|
|
321
|
+
reject(new Error('Worker request timeout'));
|
|
322
|
+
}, this.policy.timeout);
|
|
323
|
+
|
|
324
|
+
this.pendingRequests.set(id, { resolve, reject, timeout });
|
|
325
|
+
|
|
326
|
+
this.worker!.postMessage({ id, ...request });
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private handleWorkerMessage(event: MessageEvent<WorkerResponse>): void {
|
|
331
|
+
const response = event.data;
|
|
332
|
+
const pending = this.pendingRequests.get(response.id);
|
|
333
|
+
|
|
334
|
+
if (pending) {
|
|
335
|
+
clearTimeout(pending.timeout);
|
|
336
|
+
this.pendingRequests.delete(response.id);
|
|
337
|
+
pending.resolve(response);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private handleWorkerError(event: ErrorEvent): void {
|
|
342
|
+
console.error('[Mandu Kitchen] Worker error:', event.message);
|
|
343
|
+
this.handleFailure();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private handleFailure(): void {
|
|
347
|
+
this.consecutiveFailures++;
|
|
348
|
+
|
|
349
|
+
if (this.consecutiveFailures >= this.policy.maxConsecutiveFailures) {
|
|
350
|
+
this.disable();
|
|
351
|
+
} else {
|
|
352
|
+
this.setStatus('error');
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private handleRequestError<T>(error: unknown, fallback: () => T): T {
|
|
357
|
+
this.consecutiveFailures++;
|
|
358
|
+
|
|
359
|
+
if (this.consecutiveFailures >= this.policy.maxConsecutiveFailures) {
|
|
360
|
+
this.disable();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (this.policy.onTimeout === 'fallback-main') {
|
|
364
|
+
return fallback();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
throw error;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// --------------------------------------------------------------------------
|
|
371
|
+
// Fallback (Main Thread)
|
|
372
|
+
// --------------------------------------------------------------------------
|
|
373
|
+
|
|
374
|
+
private fallbackRedact(text: string, patterns?: RedactPattern[]): string {
|
|
375
|
+
return redactText(text, patterns);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private fallbackTruncate(text: string, maxBytes: number): string {
|
|
379
|
+
return truncateText(text, maxBytes);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ============================================================================
|
|
384
|
+
// Singleton Instance
|
|
385
|
+
// ============================================================================
|
|
386
|
+
|
|
387
|
+
let globalWorkerManager: WorkerManager | null = null;
|
|
388
|
+
|
|
389
|
+
export function getWorkerManager(options?: WorkerManagerOptions): WorkerManager {
|
|
390
|
+
if (!globalWorkerManager) {
|
|
391
|
+
globalWorkerManager = new WorkerManager(options);
|
|
392
|
+
}
|
|
393
|
+
return globalWorkerManager;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export async function initializeWorkerManager(
|
|
397
|
+
options?: WorkerManagerOptions
|
|
398
|
+
): Promise<WorkerManager> {
|
|
399
|
+
const manager = getWorkerManager(options);
|
|
400
|
+
await manager.initialize();
|
|
401
|
+
return manager;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function destroyWorkerManager(): void {
|
|
405
|
+
if (globalWorkerManager) {
|
|
406
|
+
globalWorkerManager.terminate();
|
|
407
|
+
globalWorkerManager = null;
|
|
408
|
+
}
|
|
409
|
+
}
|
package/src/error/formatter.ts
CHANGED
|
@@ -105,12 +105,13 @@ export function createNotFoundResponse(
|
|
|
105
105
|
pathname: string,
|
|
106
106
|
routeContext?: RouteContext
|
|
107
107
|
): ManduError {
|
|
108
|
-
return {
|
|
109
|
-
errorType: "SPEC_ERROR",
|
|
110
|
-
code: ErrorCode.SPEC_ROUTE_NOT_FOUND,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
return {
|
|
109
|
+
errorType: "SPEC_ERROR",
|
|
110
|
+
code: ErrorCode.SPEC_ROUTE_NOT_FOUND,
|
|
111
|
+
httpStatus: 404,
|
|
112
|
+
message: `Route not found: ${pathname}`,
|
|
113
|
+
summary: "라우트 없음 - spec 파일에 추가 필요",
|
|
114
|
+
fix: {
|
|
114
115
|
file: "spec/routes.manifest.json",
|
|
115
116
|
suggestion: `'${pathname}' 패턴의 라우트를 추가하세요`,
|
|
116
117
|
},
|
|
@@ -126,12 +127,13 @@ export function createHandlerNotFoundResponse(
|
|
|
126
127
|
routeId: string,
|
|
127
128
|
pattern: string
|
|
128
129
|
): ManduError {
|
|
129
|
-
return {
|
|
130
|
-
errorType: "FRAMEWORK_BUG",
|
|
131
|
-
code: ErrorCode.FRAMEWORK_ROUTER_ERROR,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
130
|
+
return {
|
|
131
|
+
errorType: "FRAMEWORK_BUG",
|
|
132
|
+
code: ErrorCode.FRAMEWORK_ROUTER_ERROR,
|
|
133
|
+
httpStatus: 500,
|
|
134
|
+
message: `Handler not registered for route: ${routeId}`,
|
|
135
|
+
summary: "핸들러 미등록 - generate 재실행 필요",
|
|
136
|
+
fix: {
|
|
135
137
|
file: `apps/server/generated/routes/${routeId}.route.ts`,
|
|
136
138
|
suggestion: "bunx mandu generate를 실행하세요",
|
|
137
139
|
},
|
|
@@ -151,12 +153,13 @@ export function createPageLoadErrorResponse(
|
|
|
151
153
|
pattern: string,
|
|
152
154
|
originalError?: Error
|
|
153
155
|
): ManduError {
|
|
154
|
-
const error: ManduError = {
|
|
155
|
-
errorType: "LOGIC_ERROR",
|
|
156
|
-
code: ErrorCode.SLOT_IMPORT_ERROR,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
const error: ManduError = {
|
|
157
|
+
errorType: "LOGIC_ERROR",
|
|
158
|
+
code: ErrorCode.SLOT_IMPORT_ERROR,
|
|
159
|
+
httpStatus: 500,
|
|
160
|
+
message: originalError?.message || `Failed to load page module for route: ${routeId}`,
|
|
161
|
+
summary: `페이지 모듈 로드 실패 - ${routeId}.route.tsx 확인 필요`,
|
|
162
|
+
fix: {
|
|
160
163
|
file: `apps/web/generated/routes/${routeId}.route.tsx`,
|
|
161
164
|
suggestion: "import 경로와 컴포넌트 export를 확인하세요",
|
|
162
165
|
},
|
|
@@ -186,12 +189,13 @@ export function createSSRErrorResponse(
|
|
|
186
189
|
pattern: string,
|
|
187
190
|
originalError?: Error
|
|
188
191
|
): ManduError {
|
|
189
|
-
const error: ManduError = {
|
|
190
|
-
errorType: "FRAMEWORK_BUG",
|
|
191
|
-
code: ErrorCode.FRAMEWORK_SSR_ERROR,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
192
|
+
const error: ManduError = {
|
|
193
|
+
errorType: "FRAMEWORK_BUG",
|
|
194
|
+
code: ErrorCode.FRAMEWORK_SSR_ERROR,
|
|
195
|
+
httpStatus: 500,
|
|
196
|
+
message: originalError?.message || `SSR rendering failed for route: ${routeId}`,
|
|
197
|
+
summary: `SSR 렌더링 실패 - 컴포넌트 확인 필요`,
|
|
198
|
+
fix: {
|
|
195
199
|
file: `apps/web/generated/routes/${routeId}.route.tsx`,
|
|
196
200
|
suggestion: "React 컴포넌트가 서버에서 렌더링 가능한지 확인하세요 (브라우저 전용 API 사용 금지)",
|
|
197
201
|
},
|
package/src/error/index.ts
CHANGED
|
@@ -14,12 +14,16 @@ export {
|
|
|
14
14
|
} from "./classifier";
|
|
15
15
|
|
|
16
16
|
// Formatter
|
|
17
|
-
export {
|
|
18
|
-
formatErrorResponse,
|
|
19
|
-
formatErrorForConsole,
|
|
20
|
-
createNotFoundResponse,
|
|
21
|
-
createHandlerNotFoundResponse,
|
|
22
|
-
createPageLoadErrorResponse,
|
|
23
|
-
createSSRErrorResponse,
|
|
24
|
-
type FormatOptions,
|
|
25
|
-
} from "./formatter";
|
|
17
|
+
export {
|
|
18
|
+
formatErrorResponse,
|
|
19
|
+
formatErrorForConsole,
|
|
20
|
+
createNotFoundResponse,
|
|
21
|
+
createHandlerNotFoundResponse,
|
|
22
|
+
createPageLoadErrorResponse,
|
|
23
|
+
createSSRErrorResponse,
|
|
24
|
+
type FormatOptions,
|
|
25
|
+
} from "./formatter";
|
|
26
|
+
|
|
27
|
+
// Result helpers
|
|
28
|
+
export type { Result } from "./result";
|
|
29
|
+
export { ok, err, statusFromError, errorToResponse } from "./result";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ManduError } from "./types";
|
|
2
|
+
import { ErrorCode } from "./types";
|
|
3
|
+
import { formatErrorResponse } from "./formatter";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result 타입 - 성공/실패를 명시적으로 표현
|
|
7
|
+
*/
|
|
8
|
+
export type Result<T> =
|
|
9
|
+
| { ok: true; value: T }
|
|
10
|
+
| { ok: false; error: ManduError };
|
|
11
|
+
|
|
12
|
+
export const ok = <T>(value: T): Result<T> => ({ ok: true, value });
|
|
13
|
+
export const err = (error: ManduError): Result<never> => ({ ok: false, error });
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* ManduError -> HTTP status 매핑
|
|
17
|
+
*/
|
|
18
|
+
export function statusFromError(error: ManduError): number {
|
|
19
|
+
if (typeof error.httpStatus === "number") {
|
|
20
|
+
return error.httpStatus;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
switch (error.code) {
|
|
24
|
+
case ErrorCode.SPEC_NOT_FOUND:
|
|
25
|
+
return 404;
|
|
26
|
+
case ErrorCode.SPEC_PARSE_ERROR:
|
|
27
|
+
case ErrorCode.SPEC_VALIDATION_ERROR:
|
|
28
|
+
case ErrorCode.SPEC_ROUTE_DUPLICATE:
|
|
29
|
+
return 400;
|
|
30
|
+
case ErrorCode.SPEC_ROUTE_NOT_FOUND:
|
|
31
|
+
return 404;
|
|
32
|
+
case ErrorCode.SLOT_VALIDATION_ERROR:
|
|
33
|
+
return 400;
|
|
34
|
+
default:
|
|
35
|
+
return 500;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 에러를 Response로 변환
|
|
41
|
+
*/
|
|
42
|
+
export function errorToResponse(error: ManduError, isDev: boolean): Response {
|
|
43
|
+
return Response.json(formatErrorResponse(error, { isDev }), {
|
|
44
|
+
status: statusFromError(error),
|
|
45
|
+
});
|
|
46
|
+
}
|
package/src/error/types.ts
CHANGED
|
@@ -73,10 +73,12 @@ export interface DebugInfo {
|
|
|
73
73
|
export interface ManduError {
|
|
74
74
|
/** 에러 타입 */
|
|
75
75
|
errorType: ErrorType;
|
|
76
|
-
/** 에러 코드 */
|
|
77
|
-
code: ErrorCode | string;
|
|
78
|
-
/**
|
|
79
|
-
|
|
76
|
+
/** 에러 코드 */
|
|
77
|
+
code: ErrorCode | string;
|
|
78
|
+
/** HTTP 상태 코드 (선택) */
|
|
79
|
+
httpStatus?: number;
|
|
80
|
+
/** 에러 메시지 */
|
|
81
|
+
message: string;
|
|
80
82
|
/** 한줄 요약 (에이전트용) */
|
|
81
83
|
summary: string;
|
|
82
84
|
/** 수정 대상 정보 */
|
package/src/filling/filling.ts
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
* 체이닝 API로 비즈니스 로직 정의
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { ManduContext, ValidationError } from "./context";
|
|
7
|
-
import { AuthenticationError, AuthorizationError } from "./auth";
|
|
8
|
-
import { ErrorClassifier, formatErrorResponse, ErrorCode } from "../error";
|
|
9
|
-
import {
|
|
6
|
+
import { ManduContext, ValidationError } from "./context";
|
|
7
|
+
import { AuthenticationError, AuthorizationError } from "./auth";
|
|
8
|
+
import { ErrorClassifier, formatErrorResponse, ErrorCode } from "../error";
|
|
9
|
+
import { TIMEOUTS } from "../constants";
|
|
10
|
+
import { createContract, type ContractDefinition, type ContractInstance } from "../contract";
|
|
10
11
|
import {
|
|
11
12
|
type Middleware as RuntimeMiddleware,
|
|
12
13
|
type MiddlewareEntry,
|
|
@@ -80,7 +81,7 @@ export class ManduFilling<TLoaderData = unknown> {
|
|
|
80
81
|
if (!this.config.loader) {
|
|
81
82
|
return undefined;
|
|
82
83
|
}
|
|
83
|
-
const { timeout =
|
|
84
|
+
const { timeout = TIMEOUTS.LOADER_DEFAULT, fallback } = options;
|
|
84
85
|
try {
|
|
85
86
|
const loaderPromise = Promise.resolve(this.config.loader(ctx));
|
|
86
87
|
const timeoutPromise = new Promise<never>((_, reject) => {
|