Skip to content

Remove regex-based JS/HTML rewriting, switch to AST-based rewriting #7297

Open
@flotwig

Description

AST-based JS/HTML rewriting is available as an experimental feature with #5273

Here are the remaining tasks before it can fully replace the existing regex-based rewriting:

  • Validation with users - is it solving the issues we set out to fix?
  • (2) Rewrite all traffic (currently, only AUT's current origin is intercepted)
    • may require inlining resolvers.ts
  • (3) Do URL scheme not considered when deciding if origin has changed or not #7268 or have it be fixed by inlining resolvers.ts
    • or else rewriting will break if a user visits an http:// url that redirects to https://
  • (4) Inject HTML via parse5 instead of regex.
  • (4) Resolve issue where proxy will no longer work for normal web browsing outside of Cypress frame. Possible solution: inject a stub window.Cypress.resolveWindowReference into every html page here:
    const { oneLine } = require('common-tags')
    module.exports = {
    partial (domain) {
    return oneLine`
    <script type='text/javascript'>
    window.Cypress = {
    // so window.top, etc. still work when browsing outside of Cypress
    resolveWindowReference: (w, o, v) => o[v];
    }
    document.domain = '${domain}';
    </script>
    `
    },
    full (domain) {
    return oneLine`
    <script type='text/javascript'>
    document.domain = '${domain}';
    var Cypress = window.Cypress = parent.Cypress;
    if (!Cypress) {
    throw new Error('Something went terribly wrong and we cannot proceed. We expected to find the global Cypress in the parent window but it is missing!. This should never happen and likely is a bug. Please open an issue!');
    };
    Cypress.action('app:window:before:load', window);
  • (4) Performance measurements:
    • difference between rewriting with sourcemap and without
    • comparison to regex-based rewriting
  • (4) Performance improvements:
    • improve speed: add streaming parsing/rewriting of JS (parse5 handles rewriting HTML as a stream)
    • mem usage: explore caching js in deferred-sourcemap-cache on filesystem
    • understand how sourcemaps are cached in browser and how we can reduce unnecessary regeneration/not store old sourcemaps in RAM
  • (4) Add comments around injected code to help users understand - potentially include original code in a comment
  • (1) Need a real fix for Electron worker_threads SIGABRT when calling process.exit with worker_threads active electron/electron#23366
  • (4) Fix all code TODOs from original PR: Rewrite JS/HTML using AST-based approach #5273

Metadata

Assignees

No one assigned

    Labels

    experiment: source rewritingIssues when using experimentalSourceRewritingpkg/proxyThis is due to an issue in the packages/proxy directorypkg/rewriterThis is due to an issue in the packages/rewriter directorystage: ready for workThe issue is reproducible and in scopetype: enhancementRequested enhancement of existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions