Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [1502] Allow resources urls to be transformed #1556

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/happy-dom/src/browser/types/IBrowserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import BrowserNavigationCrossOriginPolicyEnum from '../enums/BrowserNavigationCr
* Browser settings.
*/
export default interface IBrowserSettings {
/**
* Allows resource urls to be transformed.
* Ex: https://www.example.com/assets/index.js -> file:///Users/example-user/Projects/example/dist/assets/index.js
*/
resourceUrlTransformer?: (url: string) => string;

/** Disables JavaScript evaluation. */
disableJavaScriptEvaluation: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import BrowserErrorCaptureEnum from '../enums/BrowserErrorCaptureEnum.js';
import BrowserNavigationCrossOriginPolicyEnum from '../enums/BrowserNavigationCrossOriginPolicyEnum.js';

export default interface IOptionalBrowserSettings {
/**
* Allows resource urls to be transformed.
* Ex: https://www.example.com/assets/index.js -> file:///Users/example-user/Projects/example/dist/assets/index.js
*/
resourceUrlTransformer?: (url: string) => string;

/** Disables JavaScript evaluation. */
disableJavaScriptEvaluation?: boolean;

Expand Down
11 changes: 11 additions & 0 deletions packages/happy-dom/src/fetch/ResourceFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export default class ResourceFetch {
* @returns Response.
*/
public async fetch(url: string): Promise<string> {
if (
typeof this.#browserFrame.page.context.browser.settings.resourceUrlTransformer === 'function'
) {
url = this.#browserFrame.page.context.browser.settings.resourceUrlTransformer(url);
}

const fetch = new Fetch({
browserFrame: this.#browserFrame,
window: this.window,
Expand All @@ -56,6 +62,11 @@ export default class ResourceFetch {
* @returns Response.
*/
public fetchSync(url: string): string {
if (
typeof this.#browserFrame.page.context.browser.settings.resourceUrlTransformer === 'function'
) {
url = this.#browserFrame.page.context.browser.settings.resourceUrlTransformer(url);
}
const fetch = new SyncFetch({
browserFrame: this.#browserFrame,
window: this.window,
Expand Down
Loading