-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
60 lines (53 loc) · 1.64 KB
/
index.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
import { isBrowser } from 'browser-or-node';
import puppeteer from 'puppeteer';
import {libraries, LibraryDetector} from './library.js'
type JsFramework = Array<{name: any; url: string; npm: string; version: string}>;
const detect = (libraries: Record<string, LibraryDetector>): JsFramework => {
let frameworkList: JsFramework = [];
for (const key in libraries) {
const newFn = new Function(`return ${libraries[key]!.test}`)();
const version = newFn(window);
if (Boolean(version)) {
frameworkList.push({
name: libraries[key]?.id,
url: libraries[key]?.url!,
npm: libraries[key]?.npm!,
version: version?.version
});
}
}
return frameworkList;
};
const isValidHttpUrl = (url: string): boolean => {
try {
const newUrl = new URL(url);
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:';
} catch {
return false;
}
};
const detectJsFramework = async (url: string): Promise<JsFramework> => {
if (!isValidHttpUrl(url)) {
throw new TypeError(`Error: ${url} is not a a valid HTTP URL`);
}
if (isBrowser) {
// The detection strategy for extension
const frameworkList = detect(libraries);
return frameworkList;
}
// The detection strategy for any node application
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const frameworkList: JsFramework = await page.evaluate(
(list: Record<string, LibraryDetector>, fn: string) => {
const newFn = new Function(`return ${fn}`)();
return newFn(list);
},
libraries,
detect.toString()
);
await browser.close();
return frameworkList;
};
export default detectJsFramework;