-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1895 from embroider-build/http-audit
adding new http-audit test support
- Loading branch information
Showing
3 changed files
with
99 additions
and
19 deletions.
There are no files selected for viewing
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,51 @@ | ||
import type { Finding } from './audit'; | ||
import { CodeFrameStorage } from './audit/babel-visitor'; | ||
import { type Module, visitModules, type ContentType } from './module-visitor'; | ||
|
||
export interface HTTPAuditOptions { | ||
appURL: string; | ||
startingFrom: string[]; | ||
fetch?: typeof fetch; | ||
} | ||
|
||
export async function httpAudit( | ||
options: HTTPAuditOptions | ||
): Promise<{ modules: { [file: string]: Module }; findings: Finding[] }> { | ||
let findings: Finding[] = []; | ||
|
||
async function resolveId(specifier: string, fromFile: string): Promise<string | undefined> { | ||
return new URL(specifier, fromFile).href; | ||
} | ||
|
||
async function load(id: string): Promise<{ content: string | Buffer; type: ContentType }> { | ||
let response = await (options.fetch ?? globalThis.fetch)(id); | ||
let content = await response.text(); | ||
let type: ContentType; | ||
switch (response.headers.get('content-type')) { | ||
case 'text/javascript': | ||
type = 'javascript'; | ||
break; | ||
case 'text/html': | ||
type = 'html'; | ||
break; | ||
default: | ||
throw new Error(`oops content type ${response.headers.get('content-type')}`); | ||
} | ||
return { content, type }; | ||
} | ||
|
||
let modules = await visitModules({ | ||
base: options.appURL, | ||
entrypoints: options.startingFrom.map(s => new URL(s, options.appURL).href), | ||
babelConfig: { ast: true }, | ||
frames: new CodeFrameStorage(), | ||
findings, | ||
resolveId, | ||
load, | ||
}); | ||
|
||
return { | ||
modules, | ||
findings, | ||
}; | ||
} |
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