-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb4589d
commit a3a59b3
Showing
5 changed files
with
191 additions
and
131 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
audit/apps/audit/src/checkers/async/http-system-checker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { execSync } from 'node:child_process'; | ||
import puppeteer from 'puppeteer'; | ||
|
||
type Checker = ( | ||
url: string, | ||
parsers: { name: string; result: any }[] | ||
) => Promise<{ name: string; result: any } | undefined>; | ||
|
||
const getStatistics = async ( | ||
url: string, | ||
parsers: { name: string; result: any }[] | ||
): Promise<{ name: string; result: any }> => { | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
|
||
let requestsNumber = 0; | ||
let pageSize = 0; | ||
|
||
const urls = new Set<string>(); | ||
|
||
// Écouter les événements de requête pour compter le nombre total de requêtes et calculer le poids total de la page. | ||
page.on('request', (request) => { | ||
urls.add(request.url()); | ||
requestsNumber++; | ||
}); | ||
|
||
page.on('response', async (response) => { | ||
const responseHeaders = response.headers(); | ||
const responseSize = parseInt(responseHeaders['content-length'], 10); | ||
if (!isNaN(responseSize)) { | ||
pageSize += responseSize; | ||
} | ||
}); | ||
|
||
await page.goto(url, { waitUntil: 'networkidle0' }); | ||
|
||
// Calculer le nombre d'éléments HTML sur la page. | ||
const domComplexity: number = await page.evaluate( | ||
() => document.querySelectorAll('*').length | ||
); | ||
|
||
await browser.close(); | ||
return { | ||
name: 'statistics', | ||
result: { | ||
domComplexity, | ||
pageSize, | ||
requestsNumber, | ||
urls, | ||
}, | ||
}; | ||
}; | ||
|
||
export class HttpChecker { | ||
#checkers: Checker[] = [getStatistics]; | ||
constructor(private parsers: { name: string; result: any }[]) {} | ||
check(url: string) { | ||
return Promise.all( | ||
this.#checkers.map((checker) => checker(url, this.parsers)) | ||
).then((result) => result.filter((r) => !!r)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { execSync } from 'node:child_process'; | ||
|
||
type Checker = ( | ||
parsers: { name: string; result: any }[] | ||
) => { name: string; result: any } | undefined; | ||
|
||
const npmAudit = (parsers: { name: string; result: any }[]) => { | ||
const packageManager = parsers.find( | ||
(parser) => parser.name === 'packageManager' | ||
); | ||
if (!packageManager) { | ||
return; | ||
} | ||
|
||
if ( | ||
packageManager.result.includes('npm') || | ||
packageManager.result.includes('yarn') | ||
) { | ||
try { | ||
execSync(`npm --prefix . outdated --json`); | ||
} catch (e) { | ||
return { name: 'npm_outdated', result: JSON.parse(e.stdout.toString()) }; | ||
} | ||
} | ||
return; | ||
}; | ||
|
||
export class FileSystemChecker { | ||
#checkers: Checker[] = [npmAudit]; | ||
constructor(private parsers: { name: string; result: any }[]) {} | ||
check() { | ||
return this.#checkers | ||
.map((checker) => checker(this.parsers)) | ||
.filter((result) => !!result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { fdir } from 'fdir'; | ||
|
||
export type Parser = (responseootPath) => { name: string; result: any }; | ||
|
||
const nodePackageManager = { | ||
'package-lock.json': 'npm', | ||
'yarn.lock': 'yarn', | ||
}; | ||
|
||
const getPackageManagerConfigurationFilePath: Parser = (rootPath) => { | ||
const files = new fdir() | ||
.filter( | ||
(path) => | ||
!path.includes('node_modules') && | ||
!path.includes('.nx') && | ||
!path.includes('tmp') && | ||
!path.includes('dist') | ||
) | ||
.filter((path) => path.endsWith('package.json')) | ||
.withFullPaths() | ||
.crawl(rootPath) | ||
.sync(); | ||
|
||
return { name: 'packageManagerConfigurationFilePath', result: files }; | ||
}; | ||
|
||
const getPackageManager: Parser = (rootPath) => { | ||
const pckManagers = new fdir() | ||
.filter( | ||
(path) => | ||
!path.includes('node_modules') && | ||
!path.includes('.nx') && | ||
!path.includes('tmp') && | ||
!path.includes('dist') | ||
) | ||
.filter( | ||
(path) => | ||
!!Object.keys(nodePackageManager).find((lock) => { | ||
return path.endsWith(lock); | ||
}) | ||
) | ||
.withFullPaths() | ||
.crawl(rootPath) | ||
.sync() | ||
.map((path) => { | ||
return Object.entries(nodePackageManager).find( | ||
([file]: [string, string]) => { | ||
return path.endsWith(file); | ||
} | ||
)[1]; | ||
}) | ||
.filter((packageManager) => !!packageManager); | ||
|
||
return { name: 'packageManager', result: pckManagers }; | ||
}; | ||
|
||
export class FileSystemParser { | ||
#parsers = [getPackageManagerConfigurationFilePath, getPackageManager]; | ||
#crawler: any; | ||
constructor() {} | ||
|
||
parse(rootPath: string) { | ||
return this.#parsers.map((parser) => parser(rootPath)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class HttpParser { | ||
constructor(private readonly crawler: any) {} | ||
} |