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 cb685db
Show file tree
Hide file tree
Showing 4 changed files with 38 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// vitest.config.ts
import { defineConfig } from "file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js";

Check warning on line 2 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `"file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js"` with `'file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js'`

Check warning on line 2 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `"file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js"` with `'file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js'`

Check warning on line 2 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `"file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js"` with `'file:///Users/olsa02/Projects/happy-dom/node_modules/vitest/dist/config.js'`
var vitest_config_default = defineConfig({

Check warning on line 3 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Identifier 'vitest_config_default' is not in camel case

Check warning on line 3 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Identifier 'vitest_config_default' is not in camel case

Check warning on line 3 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Identifier 'vitest_config_default' is not in camel case
test: {

Check warning on line 4 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `··` with `↹`

Check warning on line 4 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 4 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
environment: "node",

Check warning on line 5 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `····environment:·"node"` with `↹↹environment:·'node'`

Check warning on line 5 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····environment:·"node"` with `↹↹environment:·'node'`

Check warning on line 5 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `····environment:·"node"` with `↹↹environment:·'node'`
include: ["./test/**/*.test.ts"],

Check warning on line 6 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `····include:·["./test/**/*.test.ts"` with `↹↹include:·['./test/**/*.test.ts'`

Check warning on line 6 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····include:·["./test/**/*.test.ts"` with `↹↹include:·['./test/**/*.test.ts'`

Check warning on line 6 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `····include:·["./test/**/*.test.ts"` with `↹↹include:·['./test/**/*.test.ts'`
setupFiles: ["./test/setup.ts"],

Check warning on line 7 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `····setupFiles:·["./test/setup.ts"` with `↹↹setupFiles:·['./test/setup.ts'`

Check warning on line 7 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····setupFiles:·["./test/setup.ts"` with `↹↹setupFiles:·['./test/setup.ts'`

Check warning on line 7 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `····setupFiles:·["./test/setup.ts"` with `↹↹setupFiles:·['./test/setup.ts'`
testTimeout: 500,

Check warning on line 8 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `····` with `↹↹`

Check warning on line 8 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····` with `↹↹`

Check warning on line 8 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `····` with `↹↹`
restoreMocks: true

Check warning on line 9 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `····` with `↹↹`

Check warning on line 9 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····` with `↹↹`

Check warning on line 9 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `····` with `↹↹`
}

Check warning on line 10 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `··` with `↹`

Check warning on line 10 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 10 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
});
export {

Check warning on line 12 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (18)

Replace `⏎··vitest_config_default·as·default⏎` with `·vitest_config_default·as·default·`

Check warning on line 12 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (20)

Replace `⏎··vitest_config_default·as·default⏎` with `·vitest_config_default·as·default·`

Check warning on line 12 in packages/happy-dom/vitest.config.ts.timestamp-1727264608254-e40f6185bc157.mjs

View workflow job for this annotation

GitHub Actions / build (22)

Replace `⏎··vitest_config_default·as·default⏎` with `·vitest_config_default·as·default·`
vitest_config_default as default
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsidml0ZXN0LmNvbmZpZy50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiY29uc3QgX192aXRlX2luamVjdGVkX29yaWdpbmFsX2Rpcm5hbWUgPSBcIi9Vc2Vycy9vbHNhMDIvUHJvamVjdHMvaGFwcHktZG9tL3BhY2thZ2VzL2hhcHB5LWRvbVwiO2NvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9maWxlbmFtZSA9IFwiL1VzZXJzL29sc2EwMi9Qcm9qZWN0cy9oYXBweS1kb20vcGFja2FnZXMvaGFwcHktZG9tL3ZpdGVzdC5jb25maWcudHNcIjtjb25zdCBfX3ZpdGVfaW5qZWN0ZWRfb3JpZ2luYWxfaW1wb3J0X21ldGFfdXJsID0gXCJmaWxlOi8vL1VzZXJzL29sc2EwMi9Qcm9qZWN0cy9oYXBweS1kb20vcGFja2FnZXMvaGFwcHktZG9tL3ZpdGVzdC5jb25maWcudHNcIjtpbXBvcnQgeyBkZWZpbmVDb25maWcgfSBmcm9tICd2aXR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