-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perftool): add beforeTest callback support, add request intercep…
…tion
- Loading branch information
Showing
22 changed files
with
334 additions
and
210 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,3 @@ | ||
import type { InterceptParams } from './intercept'; | ||
|
||
export const intercept = async (params: InterceptParams): Promise<void> => window._perftool_intercept?.(params); |
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,130 @@ | ||
import { Page, HTTPRequest } from 'puppeteer'; | ||
import { minimatch } from 'minimatch'; | ||
import mime from 'mime'; | ||
import fsPromises from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
import { JSONSerializable } from '../utils/types'; | ||
import BaseError from '../utils/baseError'; | ||
import { debug } from '../utils/logger'; | ||
import CWD from '../utils/cwd'; | ||
|
||
type Method = string; | ||
|
||
export const enum FakeResponseType { | ||
JSON = 'json', | ||
File = 'file', | ||
Abort = 'abort', | ||
} | ||
|
||
export type InterceptParams = { | ||
method?: Method; | ||
source: string; | ||
} & ( | ||
| { responseType: FakeResponseType.Abort; response: undefined } | ||
| { responseType: FakeResponseType.File; response: string } | ||
| { responseType?: FakeResponseType.JSON; response: JSONSerializable } | ||
); | ||
|
||
type FakeResponse = { mimeType: string; data: string | Buffer }; | ||
|
||
class BadExtensionError extends BaseError {} | ||
class BadPathError extends BaseError {} | ||
|
||
const ANY_METHOD = 'ANY'; | ||
|
||
export async function useInterceptApi(page: Page): Promise<void> { | ||
const requestReplacementByMethodMap: Map<Method, Map<string, FakeResponse | null>> = new Map(); | ||
|
||
function getRequestReplacement(url: string, method: Method): FakeResponse | null | void { | ||
if (!requestReplacementByMethodMap.has(method)) { | ||
return; | ||
} | ||
|
||
if (requestReplacementByMethodMap.get(method)!.has(url)) { | ||
return requestReplacementByMethodMap.get(method)!.get(url); | ||
} | ||
|
||
for (const [source, response] of requestReplacementByMethodMap.get(method)!) { | ||
if (minimatch(url, source)) { | ||
return response; | ||
} | ||
} | ||
} | ||
|
||
async function setRequestReplacement({ | ||
method = ANY_METHOD, | ||
response, | ||
responseType = FakeResponseType.File, | ||
source, | ||
}: InterceptParams) { | ||
if (!requestReplacementByMethodMap.has(method)) { | ||
requestReplacementByMethodMap.set(method, new Map()); | ||
} | ||
|
||
if (responseType === FakeResponseType.Abort) { | ||
debug(`[Intercept] Set up ABORT ${method} ${source}`); | ||
requestReplacementByMethodMap.get(method)!.set(source, null); | ||
} | ||
|
||
if (responseType === FakeResponseType.JSON) { | ||
debug(`[Intercept] Set up JSON response ${method} ${source}`); | ||
requestReplacementByMethodMap.get(method)!.set(source, { | ||
mimeType: 'application/json', | ||
data: JSON.stringify(response), | ||
}); | ||
} | ||
|
||
if (responseType === FakeResponseType.File) { | ||
if (typeof response !== 'string') { | ||
throw new BadPathError('Request replacement file path is not a string'); | ||
} | ||
|
||
const mimeType = mime.getType(response); | ||
|
||
if (!mimeType) { | ||
throw new BadExtensionError('Could not get a mime type from file extension'); | ||
} | ||
|
||
const data = await fsPromises.readFile(path.resolve(CWD, response)); | ||
|
||
debug(`[Intercept] Set up FILE response ${method} ${source} -> ${response}`); | ||
|
||
requestReplacementByMethodMap.get(method)!.set(source, { | ||
mimeType, | ||
data, | ||
}); | ||
} | ||
} | ||
|
||
async function handleInterceptedRequest(req: HTTPRequest) { | ||
if (req.isInterceptResolutionHandled()) { | ||
return; | ||
} | ||
|
||
const response = getRequestReplacement(req.url(), req.method()) || getRequestReplacement(req.url(), ANY_METHOD); | ||
|
||
if (response == null) { | ||
debug(`[Intercept] ${req.method()} ${req.url()} aborted`); | ||
await req.abort(); | ||
return; | ||
} | ||
|
||
if (!response) { | ||
await req.continue(); | ||
return; | ||
} | ||
|
||
await req.respond({ | ||
contentType: response.mimeType, | ||
body: response.data, | ||
}); | ||
|
||
debug(`[Intercept] ${req.method()} ${req.url()} intercepted, respond ${response.mimeType}`); | ||
} | ||
|
||
await page.exposeFunction('_perftool_intercept', setRequestReplacement); | ||
await page.setRequestInterception(true); | ||
|
||
page.on('request', handleInterceptedRequest); | ||
} |
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
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
Oops, something went wrong.