From af61ae4b09a2769ce74e766f2e45a37b307231e6 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Thu, 11 Oct 2018 03:27:15 +1100 Subject: [PATCH] jsdom: add ResourceLoader (#29589) * jsdom: add ResourceLoader * fix: lints --- types/jsdom/index.d.ts | 23 +++++++++++++++++++++-- types/jsdom/jsdom-tests.ts | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/types/jsdom/index.d.ts b/types/jsdom/index.d.ts index 1aa9750fbc..dec74af1ba 100644 --- a/types/jsdom/index.d.ts +++ b/types/jsdom/index.d.ts @@ -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 // Johan 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 | null; + + constructor(obj?: ResourceLoaderConstructorOptions); +} diff --git a/types/jsdom/jsdom-tests.ts b/types/jsdom/jsdom-tests.ts index da82b2db7c..75ff0605bc 100644 --- a/types/jsdom/jsdom-tests.ts +++ b/types/jsdom/jsdom-tests.ts @@ -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() }); +}