Skip to content

Commit

Permalink
capricorn86#463@trivial: Continues on XMLHttpRequest implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
daveed07 committed Oct 11, 2022
1 parent 1ff5b9a commit bda8a93
Show file tree
Hide file tree
Showing 16 changed files with 763 additions and 970 deletions.
372 changes: 186 additions & 186 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/happy-dom/package.json
Expand Up @@ -53,7 +53,8 @@
"whatwg-encoding": "^2.0.0",
"whatwg-mimetype": "^3.0.0",
"webidl-conversions": "^7.0.0",
"css.escape": "^1.5.1"
"css.escape": "^1.5.1",
"xmlhttprequest-ssl": "^1.6.3"
},
"devDependencies": {
"@types/he": "^1.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/event/IEventListener.ts
Expand Up @@ -7,7 +7,7 @@ export default interface IEventListener {
/**
* Handles event.
*
* @param event
* @param event Event.
*/
handleEvent(event: Event): void;
}
14 changes: 8 additions & 6 deletions packages/happy-dom/src/fetch/FetchHandler.ts
Expand Up @@ -20,17 +20,19 @@ export default class FetchHandler {
public static fetch(document: IDocument, url: string, init?: IRequestInit): Promise<IResponse> {
// We want to only load NodeFetch when it is needed to improve performance and not have direct dependencies to server side packages.
const taskManager = document.defaultView.happyDOM.asyncTaskManager;
const requestInit = { ...init, headers: { ...init?.headers } };

// We need set referer to solve anti-hotlinking.
// And the browser will set the referer to the origin of the page.
if (init) {
if (!init.headers['referer']) {
init.headers['referer'] = document.defaultView.location.origin;
}
}
requestInit.headers['referer'] = document.defaultView.location.origin;

requestInit.headers['user-agent'] = document.defaultView.navigator.userAgent;
requestInit.headers['cookie'] = document.defaultView.document.cookie;

return new Promise((resolve, reject) => {
const taskID = taskManager.startTask();

NodeFetch(RelativeURL.getAbsoluteURL(document.defaultView.location, url).href, init)
NodeFetch(RelativeURL.getAbsoluteURL(document.defaultView.location, url).href, requestInit)
.then((response) => {
if (taskManager.getTaskCount() === 0) {
reject(new Error('Failed to complete fetch request. Task was canceled.'));
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/location/Location.ts
@@ -1,4 +1,4 @@
import URL from './URL';
import { URL } from 'url';

/**
*
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/location/RelativeURL.ts
@@ -1,5 +1,5 @@
import Location from './Location';
import URL from './URL';
import { URL } from 'url';

/**
* Helper class for getting the URL relative to a Location object.
Expand Down
6 changes: 0 additions & 6 deletions packages/happy-dom/src/location/URL.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/happy-dom/src/location/URLSearchParams.ts

This file was deleted.

9 changes: 7 additions & 2 deletions packages/happy-dom/src/window/IWindow.ts
Expand Up @@ -36,8 +36,7 @@ import AnimationEvent from '../event/events/AnimationEvent';
import KeyboardEvent from '../event/events/KeyboardEvent';
import ProgressEvent from '../event/events/ProgressEvent';
import EventTarget from '../event/EventTarget';
import URL from '../location/URL';
import URLSearchParams from '../location/URLSearchParams';
import { URL, URLSearchParams } from 'url';
import Location from '../location/Location';
import MutationObserver from '../mutation-observer/MutationObserver';
import DOMParser from '../dom-parser/DOMParser';
Expand Down Expand Up @@ -83,6 +82,9 @@ import IRequestInit from '../fetch/IRequestInit';
import IResponse from '../fetch/IResponse';
import Range from '../range/Range';
import MediaQueryList from '../match-media/MediaQueryList';
import XMLHttpRequest from '../xml-http-request/XMLHttpRequest';
import XMLHttpRequestUpload from '../xml-http-request/XMLHttpRequestUpload';
import XMLHttpRequestEventTarget from '../xml-http-request/XMLHttpRequestEventTarget';
import DOMRect from '../nodes/element/DOMRect';
import Window from './Window';
import { Performance } from 'perf_hooks';
Expand Down Expand Up @@ -179,6 +181,9 @@ export default interface IWindow extends IEventTarget, NodeJS.Global {
readonly Response: { new (body?: unknown | null, init?: IResponseInit): IResponse };
readonly Range: typeof Range;
readonly DOMRect: typeof DOMRect;
readonly XMLHttpRequest: typeof XMLHttpRequest;
readonly XMLHttpRequestUpload: typeof XMLHttpRequestUpload;
readonly XMLHttpRequestEventTarget: typeof XMLHttpRequestEventTarget;

// Events
onload: (event: Event) => void;
Expand Down
21 changes: 9 additions & 12 deletions packages/happy-dom/src/window/Window.ts
Expand Up @@ -36,8 +36,7 @@ import AnimationEvent from '../event/events/AnimationEvent';
import KeyboardEvent from '../event/events/KeyboardEvent';
import ProgressEvent from '../event/events/ProgressEvent';
import EventTarget from '../event/EventTarget';
import URL from '../location/URL';
import URLSearchParams from '../location/URLSearchParams';
import { URL, URLSearchParams } from 'url';
import Location from '../location/Location';
import NonImplementedEventTypes from '../event/NonImplementedEventTypes';
import MutationObserver from '../mutation-observer/MutationObserver';
Expand Down Expand Up @@ -94,9 +93,9 @@ import VMGlobalPropertyScript from './VMGlobalPropertyScript';
import * as PerfHooks from 'perf_hooks';
import VM from 'vm';
import { Buffer } from 'buffer';
import XMLHttpRequest from '../xml-http-request/XMLHttpRequest';
import XMLHttpRequestImplementation from '../xml-http-request/XMLHttpRequest';
import XMLHttpRequestUpload from '../xml-http-request/XMLHttpRequestUpload';
import { XMLHttpRequestEventTarget } from '../xml-http-request/XMLHttpRequestEventTarget';
import XMLHttpRequestEventTarget from '../xml-http-request/XMLHttpRequestEventTarget';
import Base64 from '../base64/Base64';
import IDocument from '../nodes/document/IDocument';

Expand Down Expand Up @@ -204,6 +203,9 @@ export default class Window extends EventTarget implements IWindow {
public readonly Response: {
new (body?: NodeJS.ReadableStream | null, init?: IResponseInit): IResponse;
};
public readonly XMLHttpRequestUpload = XMLHttpRequestUpload;
public readonly XMLHttpRequestEventTarget = XMLHttpRequestEventTarget;
public readonly XMLHttpRequest;
public readonly DOMParser;
public readonly Range;
public readonly FileReader;
Expand Down Expand Up @@ -233,10 +235,6 @@ export default class Window extends EventTarget implements IWindow {
public readonly localStorage = new Storage();
public readonly performance = PerfHooks.performance;

public XMLHttpRequest = XMLHttpRequest;
public XMLHttpRequestUpload = XMLHttpRequestUpload;
public XMLHttpRequestEventTarget = XMLHttpRequestEventTarget;

// Node.js Globals
public ArrayBuffer;
public Boolean;
Expand Down Expand Up @@ -345,7 +343,6 @@ export default class Window extends EventTarget implements IWindow {
}
}

XMLHttpRequest._defaultView = this;
HTMLDocument._defaultView = this;

const document = new HTMLDocument();
Expand All @@ -361,6 +358,7 @@ export default class Window extends EventTarget implements IWindow {
FileReaderImplementation._ownerDocument = document;
DOMParserImplementation._ownerDocument = document;
RangeImplementation._ownerDocument = document;
XMLHttpRequestImplementation._ownerDocument = document;

/* eslint-disable jsdoc/require-jsdoc */
class Response extends ResponseImplementation {
Expand All @@ -378,18 +376,17 @@ export default class Window extends EventTarget implements IWindow {
class DOMParser extends DOMParserImplementation {
public static _ownerDocument: IDocument = document;
}
class Range extends RangeImplementation {
class XMLHttpRequest extends XMLHttpRequestImplementation {
public static _ownerDocument: IDocument = document;
}

/* eslint-enable jsdoc/require-jsdoc */

this.Response = Response;
this.Request = Request;
this.Image = Image;
this.FileReader = FileReader;
this.DOMParser = DOMParser;
this.Range = Range;
this.XMLHttpRequest = XMLHttpRequest;

this._setupVMContext();

Expand Down
13 changes: 0 additions & 13 deletions packages/happy-dom/src/xml-http-request/XMLHttpReqeustUtility.ts

This file was deleted.

0 comments on commit bda8a93

Please sign in to comment.