-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.ts
More file actions
115 lines (101 loc) · 2.77 KB
/
transform.ts
File metadata and controls
115 lines (101 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
import type {
TransformError,
TransformErrorResponse,
TransformResponse,
} from '@/lib/api';
import {
createInternalError,
createTransformError,
isAbortError,
isTimeoutError,
} from '@/lib/api';
import { callFetchUrl, parseMcpResult, type ProgressCallback } from '@/lib/mcp';
import type { TransformRequest } from '@/lib/validate';
const RETRYABLE_TRANSPORT_ERROR_CODES = new Set<number>([
ErrorCode.RequestTimeout,
ErrorCode.ConnectionClosed,
]);
const MAX_TRANSFORM_ATTEMPTS = 2;
const ABORT_REASON_BY_ERROR_NAME = {
aborted: 'aborted',
timeout: 'timeout',
} as const;
async function executeTransform(
request: TransformRequest,
onProgress?: ProgressCallback,
signal?: AbortSignal
): Promise<TransformResponse> {
try {
const raw = await callFetchUrl(
{ url: request.url },
{ onProgress, signal }
);
return parseMcpResult(raw);
} catch (error) {
return {
ok: false,
error: mapTransportError(error),
};
}
}
export async function transformUrl(
request: TransformRequest,
onProgress?: ProgressCallback,
signal?: AbortSignal
): Promise<TransformResponse> {
for (let attempt = 1; attempt <= MAX_TRANSFORM_ATTEMPTS; attempt += 1) {
const response = await executeTransform(request, onProgress, signal);
if (!shouldRetryResponse(response, attempt, signal)) {
return response;
}
}
return {
ok: false,
error: createInternalError('Transform failed to execute.'),
};
}
function isRetryableErrorResponse(
response: TransformResponse
): response is TransformErrorResponse {
return !response.ok && response.error.retryable;
}
function shouldRetryResponse(
response: TransformResponse,
attempt: number,
signal?: AbortSignal
): boolean {
if (signal?.aborted) return false;
return isRetryableErrorResponse(response) && attempt < MAX_TRANSFORM_ATTEMPTS;
}
function createAbortedTransformError(
message: string,
reason: (typeof ABORT_REASON_BY_ERROR_NAME)[keyof typeof ABORT_REASON_BY_ERROR_NAME]
): TransformError {
return createTransformError('ABORTED', message, {
retryable: true,
details: { reason },
});
}
function mapTransportError(error: unknown): TransformError {
const message = error instanceof Error ? error.message : 'Unknown error';
if (isTimeoutError(error)) {
return createAbortedTransformError(
message,
ABORT_REASON_BY_ERROR_NAME.timeout
);
}
if (isAbortError(error)) {
return createAbortedTransformError(
message,
ABORT_REASON_BY_ERROR_NAME.aborted
);
}
if (error instanceof McpError) {
return createInternalError(
message,
RETRYABLE_TRANSPORT_ERROR_CODES.has(error.code)
);
}
return createInternalError(message, false);
}