-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathsave.ts
More file actions
126 lines (108 loc) · 3.26 KB
/
save.ts
File metadata and controls
126 lines (108 loc) · 3.26 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
116
117
118
119
120
121
122
123
124
125
126
import { Hono } from "hono";
import { ContentExtractor, SkillGenerator, AutoTagger } from "@skillkit/core";
interface SaveRequest {
url?: string;
text?: string;
name?: string;
global?: boolean;
}
const MAX_TEXT_LENGTH = 500_000;
const BLOCKED_HOSTS = new Set([
"localhost",
"127.0.0.1",
"[::1]",
"::1",
"0.0.0.0",
]);
function isAllowedUrl(url: string): boolean {
try {
const parsed = new URL(url);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
return false;
const hostname = parsed.hostname.toLowerCase();
const bare = hostname.replace(/^\[|\]$/g, "");
if (BLOCKED_HOSTS.has(hostname) || BLOCKED_HOSTS.has(bare)) return false;
if (bare.startsWith("::ffff:"))
return isAllowedUrl(`http://${bare.slice(7)}`);
if (/^127\./.test(bare) || /^0\./.test(bare)) return false;
if (bare.startsWith("10.") || bare.startsWith("192.168.")) return false;
if (/^172\.(1[6-9]|2\d|3[01])\./.test(bare)) return false;
if (bare.startsWith("169.254.")) return false;
if (bare.includes(":")) {
if (
bare.startsWith("fe80:") ||
bare.startsWith("fc") ||
bare.startsWith("fd")
)
return false;
if (bare.startsWith("ff")) return false;
}
if (/^(22[4-9]|23\d|24\d|25[0-5])\./.test(bare)) return false;
return true;
} catch {
return false;
}
}
export function saveRoutes() {
const app = new Hono();
const extractor = new ContentExtractor();
const generator = new SkillGenerator();
const tagger = new AutoTagger();
app.post("/save", async (c) => {
let body: SaveRequest;
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.url && !body.text) {
return c.json({ error: 'Either "url" or "text" is required' }, 400);
}
if (body.url && !isAllowedUrl(body.url)) {
return c.json({ error: "URL must be a public HTTP(S) address" }, 400);
}
if (body.name && !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(body.name)) {
return c.json(
{
error:
"Name must be alphanumeric (hyphens, underscores, dots allowed)",
},
400,
);
}
if (body.text && body.text.length > MAX_TEXT_LENGTH) {
return c.json(
{
error: `Text exceeds maximum length of ${MAX_TEXT_LENGTH} characters`,
},
400,
);
}
try {
const content = body.url
? await extractor.extractFromUrl(body.url)
: extractor.extractFromText(body.text!);
const result = generator.generate(content, {
name: body.name,
global: body.global ?? true,
});
return c.json({
name: result.name,
skillPath: result.skillPath,
skillMd: result.skillMd,
tags: tagger.detectTags(content),
});
} catch (err) {
console.error("Save extraction failed:", err);
const isTimeout =
(err instanceof DOMException && err.name === "TimeoutError") ||
(err instanceof Error &&
(err.name === "TimeoutError" || err.name === "AbortError"));
if (isTimeout) {
return c.json({ error: "Fetch timed out" }, 504);
}
return c.json({ error: "Extraction failed" }, 422);
}
});
return app;
}