Skip to content

Commit

Permalink
feat: [1502] Allow resources urls to be transformed
Browse files Browse the repository at this point in the history
  • Loading branch information
OlaviSau committed Sep 27, 2024
1 parent afd256b commit 1e5fad3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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

0 comments on commit 1e5fad3

Please sign in to comment.