jsdom: add ResourceLoader (#29589)

* jsdom: add ResourceLoader

* fix: lints
This commit is contained in:
Joscha Feth
2018-10-10 09:27:15 -07:00
committed by Andy
parent 19f1f4b082
commit af61ae4b09
2 changed files with 35 additions and 3 deletions
+21 -2
View File
@@ -1,4 +1,4 @@
// Type definitions for jsdom 11.12
// Type definitions for jsdom 12.2
// Project: https://github.com/tmpvar/jsdom#readme
// Definitions by: Leonard Thieu <https://github.com/leonard-thieu>
// Johan Palmfjord <https://github.com/palmfjord>
@@ -65,7 +65,7 @@ export interface Options {
*/
includeNodeLocations?: boolean;
runScripts?: 'dangerously' | 'outside-only';
resources?: 'usable';
resources?: 'usable' | ResourceLoader;
virtualConsole?: VirtualConsole;
cookieJar?: CookieJar;
beforeParse?(window: DOMWindow): void;
@@ -287,3 +287,22 @@ export interface ReconfigureSettings {
windowTop?: DOMWindow;
url?: string;
}
export interface FetchOptions {
cookieJar?: CookieJar;
referrer?: string;
accept?: string;
element?: HTMLScriptElement | HTMLLinkElement | HTMLIFrameElement | HTMLImageElement;
}
export interface ResourceLoaderConstructorOptions {
strictSSL?: boolean;
proxy?: string;
userAgent?: string;
}
export class ResourceLoader {
fetch(url: string, options: FetchOptions): Promise<Buffer> | null;
constructor(obj?: ResourceLoaderConstructorOptions);
}
+14 -1
View File
@@ -1,4 +1,4 @@
import { JSDOM, VirtualConsole, CookieJar, FromUrlOptions, FromFileOptions, DOMWindow } from 'jsdom';
import { JSDOM, VirtualConsole, CookieJar, FromUrlOptions, FromFileOptions, DOMWindow, ResourceLoader, FetchOptions } from 'jsdom';
import { CookieJar as ToughCookieJar, MemoryCookieStore } from 'tough-cookie';
import { Script } from 'vm';
@@ -168,3 +168,16 @@ function test_fragment_serialization() {
}
}
}
function test_custom_resource_loader() {
class CustomResourceLoader extends ResourceLoader {
fetch(url: string, options: FetchOptions) {
if (options.element) {
console.log(`Element ${options.element.localName} is requesting the url ${url}`);
}
return super.fetch(url, options);
}
}
new JSDOM('', { resources: new CustomResourceLoader() });
}