Skip to content

Commit

Permalink
preparations for proxyless roles (#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev committed Nov 21, 2022
1 parent ee44f29 commit a335ee3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "testcafe-hammerhead",
"description": "A powerful web-proxy used as a core for the TestCafe testing framework (https://github.com/DevExpress/testcafe).",
"version": "28.2.0",
"version": "28.2.1",
"homepage": "https://github.com/DevExpress/testcafe-hammerhead",
"bugs": {
"url": "https://github.com/DevExpress/testcafe-hammerhead/issues"
Expand Down
6 changes: 6 additions & 0 deletions src/processing/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ export interface MetaInfo {
}

export interface PageInjectableResources {
storages: string | null;
stylesheets: string[];
scripts: string[];
embeddedScripts: string[];
}

export interface PageRestoreStoragesOptions {
host: string;
sessionId: string;
}
28 changes: 22 additions & 6 deletions src/processing/resources/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import Charset from '../encoding/charset';
import BaseDomAdapter from '../dom/base-dom-adapter';
import SERVICE_ROUTES from '../../proxy/service-routes';
import { stringify as stringifyJSON } from '../../utils/json';
import { MetaInfo, PageInjectableResources } from '../interfaces';

import {
MetaInfo,
PageInjectableResources,
PageRestoreStoragesOptions,
} from '../interfaces';

const PARSED_BODY_CREATED_EVENT_SCRIPT = parse5.parseFragment(SELF_REMOVING_SCRIPTS.onBodyCreated).childNodes![0];
const PARSED_ORIGIN_FIRST_TITLE_ELEMENT_LOADED_SCRIPT = parse5.parseFragment(SELF_REMOVING_SCRIPTS.onOriginFirstTitleLoaded).childNodes![0];
Expand Down Expand Up @@ -67,7 +72,7 @@ class PageProcessor extends ResourceProcessorBase {
return scriptAsContentElement;
}

private _createRestoreStoragesScript (storageKey: string, storages): ASTNode {
private static _createRestoreStoragesScript (storageKey: string, storages): ASTNode {
const parsedDocumentFragment = parse5.parseFragment(util.format(SELF_REMOVING_SCRIPTS.restoreStorages,
storageKey, stringifyJSON(storages.localStorage),
storageKey, stringifyJSON(storages.sessionStorage)));
Expand Down Expand Up @@ -100,9 +105,20 @@ class PageProcessor extends ResourceProcessorBase {
return metas;
}

private static _addPageResources (head: ASTNode, processingOptions: PageInjectableResources | PageProcessingOptions): ASTNode[] {
private static _addPageResources (
head: ASTNode,
processingOptions: PageInjectableResources | PageProcessingOptions,
options? : PageRestoreStoragesOptions): ASTNode[] {

const injectedResources: ASTNode[] = [];

if ((processingOptions as PageInjectableResources).storages && options) {
const storages = (processingOptions as PageInjectableResources).storages;
const script = PageProcessor._createRestoreStoragesScript(getStorageKey(options.sessionId, options.host), storages);

injectedResources.push(script);
}

if (processingOptions.stylesheets) {
processingOptions.stylesheets.forEach(stylesheetUrl => {
injectedResources.unshift(PageProcessor._createShadowUIStyleLinkNode(stylesheetUrl));
Expand Down Expand Up @@ -186,7 +202,7 @@ class PageProcessor extends ResourceProcessorBase {

private _addRestoreStoragesScript (ctx: RequestPipelineContext, head: ASTNode): void {
const storageKey = getStorageKey(ctx.session.id, ctx.dest.host);
const restoreStoragesScript = this._createRestoreStoragesScript(storageKey, ctx.restoringStorages);
const restoreStoragesScript = PageProcessor._createRestoreStoragesScript(storageKey, ctx.restoringStorages);

parse5Utils.insertBeforeFirstScript(restoreStoragesScript, head);
}
Expand Down Expand Up @@ -252,13 +268,13 @@ class PageProcessor extends ResourceProcessorBase {
}

// NOTE: API for new implementation without request pipeline
injectResources (html: string, resources: PageInjectableResources): string {
injectResources (html: string, resources: PageInjectableResources, options?: PageRestoreStoragesOptions): string {
const root = parse5.parse(html);
const elements = parse5Utils.findElementsByTagNames(root, ['head', 'body']);
const head = elements.head[0];
const body = elements.body[0];

PageProcessor._addPageResources(head, resources);
PageProcessor._addPageResources(head, resources, options);
PageProcessor._addBodyCreatedEventScript(body);

return parse5.serialize(root);
Expand Down
15 changes: 13 additions & 2 deletions ts-defs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface SessionOptions {
allowMultipleWindows: boolean;
windowId: string;
requestTimeout: RequestTimeout;
proxyless: boolean;
}

interface RequestEventListenerError {
Expand Down Expand Up @@ -191,6 +192,11 @@ declare module 'testcafe-hammerhead' {
embeddedScripts: string[];
}

export interface PageRestoreStoragesOptions {
host: string;
sessionId: string;
}

export interface ServerInfo {
hostname: string;
port: number;
Expand Down Expand Up @@ -269,7 +275,10 @@ declare module 'testcafe-hammerhead' {
useStateSnapshot (snapshot: StateSnapshot): void;

/** Get the cookie, sessionStorage and localStorage snapshot of current session **/
getStateSnapshot (): StateSnapshot;
getStateSnapshot (): Promise<StateSnapshot> | StateSnapshot;

/** Generates main hammerhead starting script **/
// getTaskScript (options: TaskScriptOpts): Promise<string>;

/** Set RequestMock on the specified ResponseEvent event **/
setMock (responseEventId: string, mock: ResponseMock): Promise<void>;
Expand Down Expand Up @@ -351,6 +360,8 @@ declare module 'testcafe-hammerhead' {

/** The StateSnapshot class is used to create page state snapshot **/
export class StateSnapshot {
constructor (cookies: string | null, storages: StoragesSnapshot | null);

/** Creates a empty page state snapshot **/
static empty (): StateSnapshot;

Expand Down Expand Up @@ -580,7 +591,7 @@ declare module 'testcafe-hammerhead' {
export function sameOriginCheck(location: string, checkedUrl: string): boolean;

/** Inject specified stuff to the page **/
export function injectResources (html: string, resources: PageInjectableResources): string;
export function injectResources (html: string, resources: PageInjectableResources, options?: PageRestoreStoragesOptions): string;

/** Proxy injectable scripts **/
export const INJECTABLE_SCRIPTS: string[];
Expand Down

0 comments on commit a335ee3

Please sign in to comment.