Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#463@minor: Added partial support for XMLHttpRequest. #520

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
18b0bd6
#463@minor: Added partial support for XMLHttpRequest.
Mas0nShi Jun 25, 2022
3e67605
#514@patch: Fixes method getAbsoluteURL.
Mas0nShi Jun 26, 2022
30b4860
#463@minor: Continue added partial support for XMLHttpRequest.
Mas0nShi Jun 26, 2022
9d9b455
#463@minor: Continue added partial support for XMLHttpRequest.
Mas0nShi Jun 26, 2022
e01541e
#463@minor: Continue added partial support for XMLHttpRequest.
Mas0nShi Jun 26, 2022
4d5f54c
#522@patch: Fixes resource load error when websites enabled anti-thef…
Mas0nShi Jun 27, 2022
da34ce5
#463@minor: Continue added partial support for XMLHttpRequest.
Mas0nShi Jun 28, 2022
6fc77aa
Merge branch 'master' into 463-adds-support-for-xml-http-request
Mas0nShi Jul 1, 2022
d1f8256
#521@patch: Replace URL to native module URL.
Mas0nShi Jul 1, 2022
a1c0e81
#521@patch: Fixes test units error.
Mas0nShi Jul 1, 2022
9f9df58
#521@minor: Continue added partial support for XMLHttpRequest.
Mas0nShi Jul 1, 2022
a71819f
#526@minor: Adds support for Document.documentURI and Document.URL.
Mas0nShi Jul 1, 2022
1ff5b9a
#526@minor: Continue Adds support for XMLHttpRequest (Sync) and remov…
Mas0nShi Jul 3, 2022
bda8a93
#463@trivial: Continues on XMLHttpRequest implementation.
daveed07 Oct 11, 2022
fc8a845
#526@minor: Fixes problem.
Mas0nShi Oct 15, 2022
40cd3c1
#526@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
f517c8f
Merge remote-tracking branch 'origin/master' into 463-adds-support-fo…
Mas0nShi Oct 16, 2022
9cce579
#526@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
b72cdd9
#526@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
608b717
#526@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
855ed45
#463@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
aaf8673
#463@minor: Continue Fixes Problem.
Mas0nShi Oct 16, 2022
2ee990e
#463@minor: Found Bugs in CookieJar, add more support for syncRequest.
Mas0nShi Oct 16, 2022
7f55543
#463@minor: Found Bugs in CookieJar, add more support for syncRequest.
Mas0nShi Oct 16, 2022
d588d3b
#463@minor: Found Bugs in CookieJar, add more support for syncRequest.
Mas0nShi Oct 16, 2022
3431208
#463@minor: Continue Fixes.
Mas0nShi Oct 16, 2022
a4b22ea
#463@minor: Continue Fixes.
Mas0nShi Oct 16, 2022
f67f67d
#463@trivial: Disables HTTP request local filesystem by default and i…
daveed07 Oct 18, 2022
861690a
#463@trivial: Removes unused dependency.
capricorn86 Oct 18, 2022
9c23e1e
Merge branch 'master' into 463-adds-support-for-xml-http-request
Mas0nShi Oct 19, 2022
6f8d5dd
#520@minor: Fixes syntax error.
Mas0nShi Oct 20, 2022
3a7f974
#520@minor: Optimize the code.
Mas0nShi Oct 21, 2022
941450c
#520@minor: Optimize the code.
Mas0nShi Oct 21, 2022
1646f8e
#520@minor: Continue to improve XMLHttpRequest.
Mas0nShi Oct 22, 2022
a981b8b
Merge branch 'master' into 463-adds-support-for-xml-http-request
Mas0nShi Oct 25, 2022
677a376
#463@trivial: Continue on XMLHttpRequest implementation.
daveed07 Oct 26, 2022
9b0b6ea
#463@trivial: Continue on XMLHttpRequest implementation.
capricorn86 Nov 14, 2022
929fbd3
Merge branch 'capricorn86:master' into 463-adds-support-for-xml-http-…
Mas0nShi Nov 28, 2022
e7bb2bc
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 3, 2022
ca00cb5
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 5, 2022
b1b7493
Merge branch 'master' into 463-adds-support-for-xml-http-request
Mas0nShi Dec 6, 2022
96682af
#520@patch: Fixes cookie setting.
Mas0nShi Dec 6, 2022
b670ae6
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 6, 2022
2193f38
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 6, 2022
7bf4bd5
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 7, 2022
2e45e3e
#463@trivial: Continues on XMLHttpRequest implementation.
capricorn86 Dec 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 type Event type.
* @param event Event.
*/
handleEvent(event: Event): void;
}
4 changes: 3 additions & 1 deletion packages/happy-dom/src/exception/DOMExceptionNameEnum.ts
Expand Up @@ -7,6 +7,8 @@ enum DOMExceptionNameEnum {
wrongDocumentError = 'WrongDocumentError',
invalidNodeTypeError = 'InvalidNodeTypeError',
invalidCharacterError = 'InvalidCharacterError',
notFoundError = 'NotFoundError'
notFoundError = 'NotFoundError',
securityError = 'SecurityError',
networkError = 'NetworkError'
}
export default DOMExceptionNameEnum;
12 changes: 10 additions & 2 deletions packages/happy-dom/src/fetch/FetchHandler.ts
Expand Up @@ -10,7 +10,7 @@ import NodeFetch from 'node-fetch';
*/
export default class FetchHandler {
/**
* Returns resource data asynchonously.
* Returns resource data asynchronously.
*
* @param document Document.
* @param url URL to resource.
Expand All @@ -20,11 +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.
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), 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.'));
capricorn86 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
14 changes: 10 additions & 4 deletions packages/happy-dom/src/fetch/ResourceFetchHandler.ts
Expand Up @@ -7,7 +7,7 @@ import IDocument from '../nodes/document/IDocument';
*/
export default class ResourceFetchHandler {
/**
* Returns resource data asynchonously.
* Returns resource data asynchronously.
*
* @param document Document.
* @param url URL.
Expand All @@ -24,17 +24,23 @@ export default class ResourceFetchHandler {
}

/**
* Returns resource data synchonously.
* Returns resource data synchronously.
*
* @param document Document.
* @param url URL.
* @returns Response.
*/
public static fetchSync(document: IDocument, url: string): string {
// We want to only load SyncRequest when it is needed to improve performance and not have direct dependencies to server side packages.
const absoluteURL = RelativeURL.getAbsoluteURL(document.defaultView.location, url);
const absoluteURL = RelativeURL.getAbsoluteURL(document.defaultView.location, url).href;
const syncRequest = require('sync-request');
const response = syncRequest('GET', absoluteURL);
const response = syncRequest('GET', absoluteURL, {
headers: {
'user-agent': document.defaultView.navigator.userAgent,
cookie: document.defaultView.document.cookie,
referer: document.defaultView.location.origin
}
});

if (response.isError()) {
throw new DOMException(
Expand Down
6 changes: 3 additions & 3 deletions packages/happy-dom/src/location/Location.ts
@@ -1,4 +1,4 @@
import URL from './URL';
import { URL } from 'url';

/**
*
Expand All @@ -8,7 +8,7 @@ export default class Location extends URL {
* Constructor.
*/
constructor() {
super('');
super('about:blank');
Mas0nShi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -17,7 +17,7 @@ export default class Location extends URL {
* @param url URL.
*/
public replace(url: string): void {
this.parse(url);
this.href = url;
}

/**
Expand Down
17 changes: 3 additions & 14 deletions packages/happy-dom/src/location/RelativeURL.ts
@@ -1,4 +1,5 @@
import Location from './Location';
import { URL } from 'url';

/**
* Helper class for getting the URL relative to a Location object.
Expand All @@ -10,19 +11,7 @@ export default class RelativeURL {
* @param location Location.
* @param url URL.
*/
public static getAbsoluteURL(location: Location, url: string): string {
if (url.startsWith('/')) {
return location.origin + url;
}

if (!url.startsWith('https://') && !url.startsWith('http://')) {
let pathname = location.pathname;
if (pathname.endsWith('/')) {
pathname = pathname.slice(0, -1);
}
return location.origin + pathname + '/' + url;
}

return url;
public static getAbsoluteURL(location: Location, url: string): URL {
return new URL(url, location.href);
}
}
102 changes: 0 additions & 102 deletions packages/happy-dom/src/location/URL.ts

This file was deleted.

20 changes: 18 additions & 2 deletions packages/happy-dom/src/nodes/document/Document.ts
Expand Up @@ -64,7 +64,6 @@ export default class Document extends Node implements IDocument {
/**
* Creates an instance of Document.
*
* @param defaultView Default view.
*/
constructor() {
super();
Expand Down Expand Up @@ -281,6 +280,24 @@ export default class Document extends Node implements IDocument {
return this.defaultView.location.href;
}

/**
* Returns URL.
*
* @returns the URL of the current document.
* */
public get URL(): string {
return this.defaultView.location.href;
}

/**
* Returns document URI.
*
* @returns the URL of the current document.
* */
public get documentURI(): string {
return this.URL;
}

/**
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
Expand Down Expand Up @@ -746,7 +763,6 @@ export default class Document extends Node implements IDocument {
*
* @param node Node to import.
* @param [deep=false] Set to "true" if the clone should be deep.
* @param Imported Node.
*/
public importNode(node: INode, deep = false): INode {
if (!(node instanceof Node)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Expand Up @@ -39,6 +39,8 @@ export default interface IDocument extends IParentNode {
readonly readyState: DocumentReadyStateEnum;
readonly charset: string;
readonly characterSet: string;
readonly URL: string;
readonly documentURI: string;
cookie: string;

/**
Expand Down
15 changes: 10 additions & 5 deletions packages/happy-dom/src/window/IWindow.ts
Expand Up @@ -36,7 +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 { 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 @@ -82,9 +82,11 @@ 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 { URLSearchParams } from 'url';
import { Performance } from 'perf_hooks';

/**
Expand Down Expand Up @@ -150,6 +152,7 @@ export default interface IWindow extends IEventTarget, NodeJS.Global {
readonly DataTransferItem: typeof DataTransferItem;
readonly DataTransferItemList: typeof DataTransferItemList;
readonly URL: typeof URL;
readonly URLSearchParams: typeof URLSearchParams;
readonly Location: typeof Location;
readonly CustomElementRegistry: typeof CustomElementRegistry;
readonly Window: typeof Window;
Expand All @@ -163,7 +166,6 @@ export default interface IWindow extends IEventTarget, NodeJS.Global {
readonly History: typeof History;
readonly Screen: typeof Screen;
readonly Storage: typeof Storage;
readonly URLSearchParams: typeof URLSearchParams;
readonly HTMLCollection: typeof HTMLCollection;
readonly NodeList: typeof NodeList;
readonly CSSUnitValue: typeof CSSUnitValue;
Expand All @@ -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 Expand Up @@ -310,7 +315,7 @@ export default interface IWindow extends IEventTarget, NodeJS.Global {
* Creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/btoa
* @param data Binay data.
* @param data Binary data.
* @returns Base64-encoded string.
*/
btoa(data: unknown): string;
Expand All @@ -321,7 +326,7 @@ export default interface IWindow extends IEventTarget, NodeJS.Global {
* @see https://developer.mozilla.org/en-US/docs/Web/API/atob
* @see https://infra.spec.whatwg.org/#forgiving-base64-encode.
* @see Https://html.spec.whatwg.org/multipage/webappapis.html#btoa.
* @param data Binay string.
* @param data Binary string.
* @returns An ASCII string containing decoded data from encodedData.
*/
atob(data: unknown): string;
Expand Down