Spaces:
Build error
Build error
File size: 15,555 Bytes
57cbae8 66db317 008dcba 57cbae8 4e5aff3 23a3b80 53821d0 66db317 3020d58 57cbae8 94170db 57cbae8 94170db 57cbae8 66db317 57cbae8 94170db 57cbae8 102a168 57cbae8 94170db 3020d58 94170db 102a168 4e5aff3 102a168 ccb4b8a 57cbae8 a082185 57cbae8 22647a0 57cbae8 e4bc29a 57cbae8 c36aa73 57cbae8 c36aa73 57cbae8 c36aa73 57cbae8 c36aa73 57cbae8 102a168 57cbae8 be993c2 57cbae8 be993c2 102a168 57cbae8 94170db 57cbae8 94170db 102a168 57cbae8 102a168 57cbae8 3020d58 57cbae8 008dcba 3bb7315 008dcba 3bb7315 008dcba 57cbae8 008dcba 3bb7315 008dcba 3bb7315 008dcba 57cbae8 1bd3ed7 57cbae8 94170db 008dcba 57cbae8 4e5aff3 57cbae8 94170db 29774ac 4e5aff3 57cbae8 94170db a082185 57cbae8 53821d0 57cbae8 7af2bde 53821d0 57cbae8 53821d0 57cbae8 7af2bde 57cbae8 4e5aff3 57cbae8 53821d0 4e299bf 53821d0 234f61d 008dcba 234f61d 53821d0 b674d26 53821d0 b674d26 53821d0 57cbae8 94170db 57cbae8 102a168 57cbae8 4e5aff3 23a3b80 57cbae8 | 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | import { container, singleton } from 'tsyringe';
import { GlobalLogger } from './logger';
import { ExtendedSnapshot, ImgBrief, PageSnapshot } from './puppeteer';
import { Readability } from '@mozilla/readability';
import TurndownService from 'turndown';
import { Threaded } from '../services/threaded';
import type { ExtraScrappingOptions } from '../api/crawler';
import { tailwindClasses } from '../utils/tailwind-classes';
import { countGPTToken } from '../shared/utils/openai';
import { AsyncService } from 'civkit/async-service';
import { ApplicationError, AssertionFailureError } from 'civkit/civ-rpc';
const pLinkedom = import('linkedom');
@singleton()
export class JSDomControl extends AsyncService {
logger = this.globalLogger.child({ service: this.constructor.name });
linkedom!: Awaited<typeof pLinkedom>;
constructor(
protected globalLogger: GlobalLogger,
) {
super(...arguments);
}
override async init() {
await this.dependencyReady();
this.linkedom = await pLinkedom;
this.emit('ready');
}
async narrowSnapshot(snapshot: PageSnapshot | undefined, options?: ExtraScrappingOptions) {
if (snapshot?.parsed && !options?.targetSelector && !options?.removeSelector && !options?.withIframe && !options?.withShadowDom) {
return snapshot;
}
if (!snapshot?.html) {
return snapshot;
}
try {
// SideLoad contains native objects that cannot go through thread boundaries.
return await this.actualNarrowSnapshot(snapshot, { ...options, sideLoad: undefined });
} catch (err: any) {
this.logger.warn(`Error narrowing snapshot`, { err });
if (err instanceof ApplicationError) {
throw err;
}
throw new AssertionFailureError(`Failed to process the page: ${err?.message}`);
}
}
@Threaded()
async actualNarrowSnapshot(snapshot: PageSnapshot, options?: ExtraScrappingOptions): Promise<PageSnapshot | undefined> {
const t0 = Date.now();
let sourceHTML = snapshot.html;
if (options?.withShadowDom && snapshot.shadowExpanded) {
sourceHTML = snapshot.shadowExpanded;
}
let jsdom = this.linkedom.parseHTML(sourceHTML);
if (!jsdom.window.document.documentElement) {
jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
}
const allNodes: Node[] = [];
jsdom.window.document.querySelectorAll('svg').forEach((x) => x.innerHTML = '');
if (options?.withIframe) {
jsdom.window.document.querySelectorAll('iframe[src],frame[src]').forEach((x) => {
const src = x.getAttribute('src');
const thisSnapshot = snapshot.childFrames?.find((f) => f.href === src);
if (options?.withIframe === 'quoted') {
const blockquoteElem = jsdom.window.document.createElement('blockquote');
const preElem = jsdom.window.document.createElement('pre');
preElem.innerHTML = thisSnapshot?.text || '';
blockquoteElem.appendChild(preElem);
x.replaceWith(blockquoteElem);
} else if (thisSnapshot?.html) {
x.innerHTML = thisSnapshot.html;
x.querySelectorAll('script, style').forEach((s) => s.remove());
if (src) {
x.querySelectorAll('[src]').forEach((el) => {
const imgSrc = el.getAttribute('src')!;
if (URL.canParse(imgSrc, src!)) {
el.setAttribute('src', new URL(imgSrc, src!).toString());
}
});
x.querySelectorAll('[href]').forEach((el) => {
const linkHref = el.getAttribute('href')!;
if (URL.canParse(linkHref, src!)) {
el.setAttribute('href', new URL(linkHref, src!).toString());
}
});
}
}
});
}
if (Array.isArray(options?.removeSelector)) {
for (const rl of options!.removeSelector) {
jsdom.window.document.querySelectorAll(rl).forEach((x) => x.remove());
}
} else if (options?.removeSelector) {
jsdom.window.document.querySelectorAll(options.removeSelector).forEach((x) => x.remove());
}
let bewareTargetContentDoesNotExist = false;
if (Array.isArray(options?.targetSelector)) {
bewareTargetContentDoesNotExist = true;
for (const x of options!.targetSelector.map((x) => jsdom.window.document.querySelectorAll(x))) {
x.forEach((el) => {
if (!allNodes.includes(el)) {
allNodes.push(el);
}
});
}
} else if (options?.targetSelector) {
bewareTargetContentDoesNotExist = true;
jsdom.window.document.querySelectorAll(options.targetSelector).forEach((el) => {
if (!allNodes.includes(el)) {
allNodes.push(el);
}
});
} else {
allNodes.push(jsdom.window.document);
}
if (!allNodes.length) {
if (bewareTargetContentDoesNotExist) {
return undefined;
}
return snapshot;
}
const textNodes: HTMLElement[] = [];
let rootDoc: Document;
if (allNodes.length === 1 && allNodes[0].nodeName === '#document' && (allNodes[0] as any).documentElement) {
rootDoc = allNodes[0] as any;
if (rootDoc.body?.innerText) {
textNodes.push(rootDoc.body);
}
} else {
rootDoc = this.linkedom.parseHTML('<html><body></body></html>').window.document;
for (const n of allNodes) {
rootDoc.body.appendChild(n);
rootDoc.body.appendChild(rootDoc.createTextNode('\n\n'));
if ((n as HTMLElement).innerText) {
textNodes.push(n as HTMLElement);
}
}
}
const textChunks = textNodes.map((x) => {
const clone = x.cloneNode(true) as HTMLElement;
clone.querySelectorAll('script,style,link,svg').forEach((s) => s.remove());
return clone.innerText;
});
let parsed;
try {
parsed = new Readability(rootDoc.cloneNode(true) as any).parse();
} catch (err: any) {
this.logger.warn(`Failed to parse selected element`, { err });
}
const imgSet = new Set<string>();
const rebuiltImgs: ImgBrief[] = [];
Array.from(rootDoc.querySelectorAll('img[src],img[data-src]'))
.map((x: any) => [x.getAttribute('src'), x.getAttribute('data-src'), x.getAttribute('alt')])
.forEach(([u1, u2, alt]) => {
let absUrl: string | undefined;
if (u1) {
try {
const u1Txt = new URL(u1, snapshot.rebase || snapshot.href).toString();
imgSet.add(u1Txt);
absUrl = u1Txt;
} catch (err) {
// void 0;
}
}
if (u2) {
try {
const u2Txt = new URL(u2, snapshot.rebase || snapshot.href).toString();
imgSet.add(u2Txt);
absUrl = u2Txt;
} catch (err) {
// void 0;
}
}
if (absUrl) {
rebuiltImgs.push({
src: absUrl,
alt
});
}
});
const r = {
...snapshot,
title: snapshot.title || jsdom.window.document.title,
description: snapshot.description ||
(jsdom.window.document.head?.querySelector('meta[name="description"]')?.getAttribute('content') ?? ''),
parsed,
html: rootDoc.documentElement.outerHTML,
text: textChunks.join('\n'),
imgs: (snapshot.imgs || rebuiltImgs)?.filter((x) => imgSet.has(x.src)) || [],
} as PageSnapshot;
const dt = Date.now() - t0;
if (dt > 1000) {
this.logger.warn(`Performance issue: Narrowing snapshot took ${dt}ms`, { url: snapshot.href, dt });
}
return r;
}
@Threaded()
async inferSnapshot(snapshot: PageSnapshot) {
const t0 = Date.now();
const extendedSnapshot = { ...snapshot } as ExtendedSnapshot;
try {
const jsdom = this.linkedom.parseHTML(snapshot.html);
jsdom.window.document.querySelectorAll('svg').forEach((x) => x.innerHTML = '');
const links = Array.from(jsdom.window.document.querySelectorAll('a[href]'))
.map((x: any) => [x.textContent.replace(/\s+/g, ' ').trim(), x.getAttribute('href'),])
.map(([text, href]) => {
if (!href) {
return undefined;
}
try {
const parsed = new URL(href, snapshot.rebase || snapshot.href);
return [text, parsed.toString()] as const;
} catch (err) {
return undefined;
}
})
.filter(Boolean) as [string, string][];
extendedSnapshot.links = links;
const imgs = Array.from(jsdom.window.document.querySelectorAll('img[src],img[data-src]'))
.map((x: any) => {
let linkPreferredSrc = x.getAttribute('src') || '';
if (linkPreferredSrc.startsWith('data:')) {
const dataSrc = x.getAttribute('data-src') || '';
if (dataSrc && !dataSrc.startsWith('data:')) {
linkPreferredSrc = dataSrc;
}
}
return {
src: new URL(linkPreferredSrc, snapshot.rebase || snapshot.href).toString(),
width: parseInt(x.getAttribute('width') || '0'),
height: parseInt(x.getAttribute('height') || '0'),
alt: x.getAttribute('alt') || x.getAttribute('title'),
};
});
extendedSnapshot.imgs = imgs as any;
} catch (_err) {
void 0;
}
const dt = Date.now() - t0;
if (dt > 1000) {
this.logger.warn(`Performance issue: Inferring snapshot took ${dt}ms`, { url: snapshot.href, dt });
}
return extendedSnapshot;
}
cleanRedundantEmptyLines(text: string) {
const lines = text.split(/\r?\n/g);
const mappedFlag = lines.map((line) => Boolean(line.trim()));
return lines.filter((_line, i) => mappedFlag[i] || mappedFlag[i - 1]).join('\n');
}
@Threaded()
async cleanHTMLforLMs(sourceHTML: string, ...discardSelectors: string[]): Promise<string> {
const t0 = Date.now();
let jsdom = this.linkedom.parseHTML(sourceHTML);
if (!jsdom.window.document.documentElement) {
jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
}
for (const rl of discardSelectors) {
jsdom.window.document.querySelectorAll(rl).forEach((x) => x.remove());
}
jsdom.window.document.querySelectorAll('img[src],img[data-src]').forEach((x) => {
const src = x.getAttribute('src') || x.getAttribute('data-src');
if (src?.startsWith('data:')) {
x.setAttribute('src', 'blob:opaque');
}
x.removeAttribute('data-src');
x.removeAttribute('srcset');
});
jsdom.window.document.querySelectorAll('[class]').forEach((x) => {
const classes = x.getAttribute('class')?.split(/\s+/g) || [];
const newClasses = classes.filter((c) => !tailwindClasses.has(c));
x.setAttribute('class', newClasses.join(' '));
});
jsdom.window.document.querySelectorAll('[style]').forEach((x) => {
const style = x.getAttribute('style')?.toLocaleLowerCase() || '';
if (style.startsWith('display: none')) {
return;
}
x.removeAttribute('style');
});
const treeWalker = jsdom.window.document.createTreeWalker(
jsdom.window.document, // Start from the root document
0x80 // Only show comment nodes
);
let currentNode;
while ((currentNode = treeWalker.nextNode())) {
currentNode.parentNode?.removeChild(currentNode); // Remove each comment node
}
jsdom.window.document.querySelectorAll('*').forEach((x) => {
const attrs = x.getAttributeNames();
for (const attr of attrs) {
if (attr.startsWith('data-') || attr.startsWith('aria-')) {
x.removeAttribute(attr);
}
}
});
const final = this.cleanRedundantEmptyLines(jsdom.window.document.documentElement.outerHTML);
const dt = Date.now() - t0;
if (dt > 1000) {
this.logger.warn(`Performance issue: Cleaning HTML for LMs took ${dt}ms`, { dt });
}
return final;
}
snippetToElement(snippet?: string, url?: string) {
const parsed = this.linkedom.parseHTML(snippet || '<html><body></body></html>');
// Hack for turndown gfm table plugin.
parsed.window.document.querySelectorAll('table').forEach((x) => {
Object.defineProperty(x, 'rows', { value: Array.from(x.querySelectorAll('tr')), enumerable: true });
});
Object.defineProperty(parsed.window.document.documentElement, 'cloneNode', {
value: function () { return this; },
});
return parsed.window.document.documentElement;
}
runTurndown(turndownService: TurndownService, html: TurndownService.Node | string) {
const t0 = Date.now();
try {
return turndownService.turndown(html);
} finally {
const dt = Date.now() - t0;
if (dt > 1000) {
this.logger.warn(`Performance issue: Turndown took ${dt}ms`, { dt });
}
}
}
@Threaded()
async analyzeHTMLTextLite(sourceHTML: string) {
let jsdom = this.linkedom.parseHTML(sourceHTML);
if (!jsdom.window.document.documentElement) {
jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
}
jsdom.window.document.querySelectorAll('script,style,link,svg').forEach((s) => s.remove());
const text = jsdom.window.document.body.innerText || '';
return {
title: jsdom.window.document.title,
text,
tokens: countGPTToken(text.replaceAll(/[\s\r\n\t]+/g, ' ')),
};
}
}
const jsdomControl = container.resolve(JSDomControl);
export default jsdomControl;
|