Skip to content

Commit 7f92ac8

Browse files
authored
🤖 Merge PR DefinitelyTyped#74506 [jsdom] Update type for new resources options in v28 by @seia-soto
1 parent e32c23c commit 7f92ac8

File tree

5 files changed

+63
-55
lines changed

5 files changed

+63
-55
lines changed

‎types/jsdom/base.d.ts‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { EventEmitter } from "events";
66
import { Token } from "parse5";
77
import * as tough from "tough-cookie";
8+
import { Dispatcher } from "undici-types";
89
import { Context } from "vm";
910

1011
// Needed to allow adding properties to `DOMWindow` that are only supported
@@ -57,12 +58,6 @@ declare module "jsdom" {
5758
reconfigure(settings: ReconfigureSettings): void;
5859
}
5960

60-
class ResourceLoader {
61-
fetch(url: string, options: FetchOptions): AbortablePromise<Buffer> | null;
62-
63-
constructor(obj?: ResourceLoaderConstructorOptions);
64-
}
65-
6661
class VirtualConsole extends EventEmitter {
6762
on<K extends keyof Console>(method: K, callback: Console[K]): this;
6863
on(event: "jsdomError", callback: (e: Error) => void): this;
@@ -78,14 +73,6 @@ declare module "jsdom" {
7873
*/
7974
referrer?: string | undefined;
8075

81-
/**
82-
* userAgent affects the value read from navigator.userAgent, as well as the User-Agent header sent while fetching subresources.
83-
*
84-
* @default
85-
* `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${jsdomVersion}`
86-
*/
87-
userAgent?: string | undefined;
88-
8976
/**
9077
* `includeNodeLocations` preserves the location info produced by the HTML parser,
9178
* allowing you to retrieve it with the nodeLocation() method (described below).
@@ -97,7 +84,7 @@ declare module "jsdom" {
9784
*/
9885
includeNodeLocations?: boolean | undefined;
9986
runScripts?: "dangerously" | "outside-only" | undefined;
100-
resources?: "usable" | ResourceLoader | undefined;
87+
resources?: "usable" | ResourcesOptions | undefined;
10188
virtualConsole?: VirtualConsole | undefined;
10289
cookieJar?: CookieJar | undefined;
10390

@@ -156,6 +143,16 @@ declare module "jsdom" {
156143
storageQuota?: number | undefined;
157144
}
158145

146+
interface RequestInterceptorContext {
147+
element: HTMLElement | null;
148+
}
149+
type RequestInterceptorCallback = (
150+
request: Request,
151+
context: RequestInterceptorContext,
152+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
153+
) => void | undefined | Response | Promise<undefined | Response>;
154+
function requestInterceptor(fn: RequestInterceptorCallback): Dispatcher.DispatchInterceptor;
155+
159156
type SupportedContentTypes =
160157
| "text/html"
161158
| "application/xhtml+xml"
@@ -178,17 +175,16 @@ declare module "jsdom" {
178175
url?: string | undefined;
179176
}
180177

181-
interface FetchOptions {
182-
cookieJar?: CookieJar | undefined;
183-
referrer?: string | undefined;
184-
accept?: string | undefined;
185-
element?: HTMLScriptElement | HTMLLinkElement | HTMLIFrameElement | HTMLImageElement | undefined;
186-
}
187-
188-
interface ResourceLoaderConstructorOptions {
189-
strictSSL?: boolean | undefined;
190-
proxy?: string | undefined;
178+
interface ResourcesOptions {
179+
/**
180+
* userAgent affects the value read from navigator.userAgent, as well as the User-Agent header sent while fetching subresources.
181+
*
182+
* @default
183+
* `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${jsdomVersion}`
184+
*/
191185
userAgent?: string | undefined;
186+
dispatcher?: Dispatcher | undefined;
187+
interceptors?: Dispatcher.DispatcherComposeInterceptor[] | undefined;
192188
}
193189

194190
interface DOMWindow extends Omit<Window, "top" | "self" | "window"> {

‎types/jsdom/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/jsdom",
4-
"version": "27.0.9999",
4+
"version": "28.0.9999",
55
"projects": [
66
"https://github.com/jsdom/jsdom"
77
],
@@ -13,7 +13,8 @@
1313
"dependencies": {
1414
"@types/node": "*",
1515
"@types/tough-cookie": "*",
16-
"parse5": "^7.0.0"
16+
"parse5": "^7.0.0",
17+
"undici-types": "^7.21.0"
1718
},
1819
"devDependencies": {
1920
"@types/jsdom": "workspace:."

‎types/jsdom/test/core.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {
33
ConstructorOptions,
44
CookieJar,
55
DOMWindow,
6-
FetchOptions,
76
FileOptions,
87
JSDOM,
9-
ResourceLoader,
8+
requestInterceptor,
109
VirtualConsole,
1110
} from "jsdom";
1211
import { CookieJar as ToughCookieJar, MemoryCookieStore } from "tough-cookie";
12+
import { ProxyAgent } from "undici-types";
1313
import { Script } from "vm";
1414

1515
function test_basic_usage() {
@@ -189,26 +189,32 @@ function test_fragment_serialization() {
189189
}
190190
}
191191

192-
function test_custom_resource_loader() {
193-
class CustomResourceLoader extends ResourceLoader {
194-
fetch(url: string, options: FetchOptions) {
195-
if (options.element) {
196-
console.log(`Element ${options.element.localName} is requesting the url ${url}`);
197-
198-
if (options.element.localName === "iframe") {
199-
console.log("Ignoring resource requested by iframe element");
200-
return null;
201-
}
202-
}
203-
204-
return super.fetch(url, options);
192+
function test_request_interceptor() {
193+
// $ExpectType DispatchInterceptor
194+
requestInterceptor(function(request, { element }) {
195+
console.log(`${element?.localName || "XHR"} requested ${request.url}`);
196+
if (request.url === "https://example.com/some-specific-script.js") {
197+
return new Response("window.someGlobal = 5;", {
198+
headers: { "Content-Type": "application/javascript" },
199+
});
205200
}
206-
}
207-
new JSDOM("", { resources: new CustomResourceLoader() });
201+
});
208202
}
209203

210-
function test_resource_loader_return_type(resourceLoader: ResourceLoader) {
211-
resourceLoader.fetch("https://example.com", {}); // $ExpectType AbortablePromise<Buffer> | null
204+
function test_resources_options() {
205+
new JSDOM("", {
206+
resources: "usable",
207+
});
208+
new JSDOM("", {
209+
resources: {},
210+
});
211+
new JSDOM("", {
212+
resources: {
213+
userAgent: "",
214+
dispatcher: new ProxyAgent("http://127.0.0.1:9001"),
215+
interceptors: [requestInterceptor(function() {})],
216+
},
217+
});
212218
}
213219

214220
function test_createElementDirectTypes() {

‎types/karma-jsdom-launcher/karma-jsdom-launcher-tests.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import karma = require("karma");
2-
import jsdom = require("jsdom");
1+
import jsdom from "jsdom";
2+
import karma from "karma";
3+
import { ProxyAgent } from "undici-types";
34

4-
const resourceLoader = new jsdom.ResourceLoader({
5-
proxy: "http://127.0.0.1:9001",
6-
strictSSL: false,
7-
userAgent: "Mellblomenator/9000",
8-
});
95
const virtualConsole = new jsdom.VirtualConsole();
106
const cookieJar = new jsdom.CookieJar();
117

@@ -15,7 +11,15 @@ module.exports = (config: karma.Config) => {
1511

1612
jsdomLauncher: {
1713
jsdom: {
18-
resources: resourceLoader,
14+
resources: {
15+
dispatcher: new ProxyAgent({
16+
uri: "http://127.0.0.1:9001",
17+
connect: {
18+
rejectUnauthorized: false,
19+
},
20+
}),
21+
userAgent: "Mellblomenator/9000",
22+
},
1923
virtualConsole,
2024
cookieJar,
2125
},

‎types/karma-jsdom-launcher/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"@types/karma": "*"
1111
},
1212
"devDependencies": {
13-
"@types/karma-jsdom-launcher": "workspace:."
13+
"@types/karma-jsdom-launcher": "workspace:.",
14+
"undici-types": "^7.22.0"
1415
},
1516
"owners": [
1617
{

0 commit comments

Comments
 (0)