-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
recursive_url.ts
211 lines (173 loc) Β· 5.59 KB
/
recursive_url.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { JSDOM } from "jsdom";
import { Document } from "@langchain/core/documents";
import { AsyncCaller } from "@langchain/core/utils/async_caller";
import { BaseDocumentLoader, DocumentLoader } from "../base.js";
export interface RecursiveUrlLoaderOptions {
excludeDirs?: string[];
extractor?: (text: string) => string;
maxDepth?: number;
timeout?: number;
preventOutside?: boolean;
callerOptions?: ConstructorParameters<typeof AsyncCaller>[0];
}
export class RecursiveUrlLoader
extends BaseDocumentLoader
implements DocumentLoader
{
private caller: AsyncCaller;
private url: string;
private excludeDirs: string[];
private extractor: (text: string) => string;
private maxDepth: number;
private timeout: number;
private preventOutside: boolean;
constructor(url: string, options: RecursiveUrlLoaderOptions) {
super();
this.caller = new AsyncCaller({
maxConcurrency: 64,
maxRetries: 0,
...options.callerOptions,
});
this.url = url;
this.excludeDirs = options.excludeDirs ?? [];
this.extractor = options.extractor ?? ((s: string) => s);
this.maxDepth = options.maxDepth ?? 2;
this.timeout = options.timeout ?? 10000;
this.preventOutside = options.preventOutside ?? true;
}
private async fetchWithTimeout(
resource: string,
options: { timeout: number } & RequestInit
): Promise<Response> {
const { timeout, ...rest } = options;
return this.caller.call(() =>
fetch(resource, { ...rest, signal: AbortSignal.timeout(timeout) })
);
}
private getChildLinks(html: string, baseUrl: string): Array<string> {
const allLinks = Array.from(
new JSDOM(html).window.document.querySelectorAll("a")
).map((a) => a.href);
const absolutePaths = [];
// eslint-disable-next-line no-script-url
const invalidPrefixes = ["javascript:", "mailto:", "#"];
const invalidSuffixes = [
".css",
".js",
".ico",
".png",
".jpg",
".jpeg",
".gif",
".svg",
];
for (const link of allLinks) {
if (
invalidPrefixes.some((prefix) => link.startsWith(prefix)) ||
invalidSuffixes.some((suffix) => link.endsWith(suffix))
)
continue;
let standardizedLink: string;
if (link.startsWith("http")) {
standardizedLink = link;
} else if (link.startsWith("//")) {
const base = new URL(baseUrl);
standardizedLink = base.protocol + link;
} else {
standardizedLink = new URL(link, baseUrl).href;
}
if (this.excludeDirs.some((exDir) => standardizedLink.startsWith(exDir)))
continue;
if (link.startsWith("http")) {
const isAllowed = !this.preventOutside || link.startsWith(baseUrl);
if (isAllowed) absolutePaths.push(link);
} else if (link.startsWith("//")) {
const base = new URL(baseUrl);
absolutePaths.push(base.protocol + link);
} else {
const newLink = new URL(link, baseUrl).href;
absolutePaths.push(newLink);
}
}
return Array.from(new Set(absolutePaths));
}
private extractMetadata(rawHtml: string, url: string) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const metadata: Record<string, any> = { source: url };
const { document } = new JSDOM(rawHtml).window;
const title = document.getElementsByTagName("title")[0];
if (title) {
metadata.title = title.textContent;
}
const description = document.querySelector("meta[name=description]");
if (description) {
metadata.description = description.getAttribute("content");
}
const html = document.getElementsByTagName("html")[0];
if (html) {
metadata.language = html.getAttribute("lang");
}
return metadata;
}
private async getUrlAsDoc(url: string): Promise<Document | null> {
let res;
try {
res = await this.fetchWithTimeout(url, { timeout: this.timeout });
res = await res.text();
} catch (e) {
return null;
}
return {
pageContent: this.extractor(res),
metadata: this.extractMetadata(res, url),
};
}
private async getChildUrlsRecursive(
inputUrl: string,
visited: Set<string> = new Set<string>(),
depth = 0
): Promise<Document[]> {
if (depth >= this.maxDepth) return [];
let url = inputUrl;
if (!inputUrl.endsWith("/")) url += "/";
const isExcluded = this.excludeDirs.some((exDir) => url.startsWith(exDir));
if (isExcluded) return [];
let res;
try {
res = await this.fetchWithTimeout(url, { timeout: this.timeout });
res = await res.text();
} catch (e) {
return [];
}
const childUrls: string[] = this.getChildLinks(res, url);
const results = await Promise.all(
childUrls.map((childUrl) =>
(async () => {
if (visited.has(childUrl)) return null;
visited.add(childUrl);
const childDoc = await this.getUrlAsDoc(childUrl);
if (!childDoc) return null;
if (childUrl.endsWith("/")) {
const childUrlResponses = await this.getChildUrlsRecursive(
childUrl,
visited,
depth + 1
);
return [childDoc, ...childUrlResponses];
}
return [childDoc];
})()
)
);
return results.flat().filter((docs) => docs !== null) as Document[];
}
async load(): Promise<Document[]> {
const rootDoc = await this.getUrlAsDoc(this.url);
if (!rootDoc) return [];
const docs = [rootDoc];
docs.push(
...(await this.getChildUrlsRecursive(this.url, new Set([this.url])))
);
return docs;
}
}