Skip to content

Commit 536ad22

Browse files
authored
fix: rejectAuthorize broken in VSCode ECL Extension (#4512)
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 4b76aa3 commit 536ad22

3 files changed

Lines changed: 13 additions & 20 deletions

File tree

packages/comms/src/connection.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, promiseTimeout, scopedLogger, utf8ToBase64 } from "@hpcc-js/util";
1+
import { join, promiseTimeout, root, scopedLogger, utf8ToBase64 } from "@hpcc-js/util";
22

33
const logger = scopedLogger("comms/connection.ts");
44

@@ -154,20 +154,12 @@ function doFetch(opts: IOptions, action: string, requestInit: RequestInit, heade
154154
headers: headersInit
155155
};
156156

157-
if (fetch["__setGlobalDispatcher"]) {
158-
fetch["__setGlobalDispatcher"](fetch["__defaultAgent"]);
159-
}
160-
161157
if (opts.baseUrl.indexOf("https:") === 0) {
162158
// NodeJS / node-fetch only ---
163-
if (opts.rejectUnauthorized === false && fetch["__rejectUnauthorizedAgent"]) {
164-
if (fetch["__setGlobalDispatcher"]) {
165-
fetch["__setGlobalDispatcher"](fetch["__rejectUnauthorizedAgent"]);
166-
} else {
167-
requestInit["agent"] = fetch["__rejectUnauthorizedAgent"];
168-
}
169-
} else if (fetch["__trustwaveAgent"]) {
170-
requestInit["agent"] = fetch["__trustwaveAgent"];
159+
if (opts.rejectUnauthorized === false && root.__hpcc_rejectUnauthorizedAgent) {
160+
requestInit["dispatcher"] = root.__hpcc_rejectUnauthorizedAgent;
161+
} else if (root.__hpcc_trustwaveAgent) {
162+
requestInit["agent"] = root.__hpcc_trustwaveAgent;
171163
}
172164
}
173165

@@ -178,12 +170,13 @@ function doFetch(opts: IOptions, action: string, requestInit: RequestInit, heade
178170
throw new Error(response.statusText);
179171
}
180172

181-
return promiseTimeout(opts.timeoutSecs! * 1000, fetch(join(opts.baseUrl, action), requestInit)
173+
const fetchOverride = root.__hpcc_undiciFetch ?? fetch;
174+
return promiseTimeout(opts.timeoutSecs! * 1000, fetchOverride(join(opts.baseUrl, action), requestInit)
182175
.then(handleResponse)
183176
.catch(e => {
184177
// Try again with the opposite credentials mode ---
185178
requestInit.credentials = !_omitMap[opts.baseUrl] ? "omit" : "include";
186-
return fetch(join(opts.baseUrl, action), requestInit)
179+
return fetchOverride(join(opts.baseUrl, action), requestInit)
187180
.then(handleResponse)
188181
.then(responseBody => {
189182
_omitMap[opts.baseUrl] = !_omitMap[opts.baseUrl]; // The "opposite" credentials mode is known to work ---

packages/comms/src/index.node.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ root.DOMParser = DOMParser;
66
// fetch setup for Node.js ---
77
import * as https from "node:https";
88
import { Buffer } from "node:buffer";
9-
import { Agent, setGlobalDispatcher } from "undici";
9+
import { fetch, Agent } from "undici";
1010

1111
// NodeJS >= v18 has native fetch ---
1212
if (root.fetch === undefined) {
1313
throw new Error("@hpcc-js/comms requires Node.js >= 18.0.0 for native fetch support");
1414
}
15-
root.fetch.__defaultAgent = new Agent();
16-
root.fetch.__rejectUnauthorizedAgent = new Agent({
15+
root.__hpcc_undiciFetch = fetch;
16+
root.__hpcc_rejectUnauthorizedAgent = new Agent({
1717
connect: {
1818
rejectUnauthorized: false
1919
}
2020
});
21-
root.fetch.__setGlobalDispatcher = setGlobalDispatcher;
2221

2322
import { trustwave } from "./pem/trustwave.ts";
2423

@@ -38,7 +37,7 @@ if (https.globalAgent.options.ca !== undefined) {
3837
globalCA += "\n";
3938
}
4039

41-
root.fetch.__trustwaveAgent = new https.Agent({
40+
root.__hpcc_trustwaveAgent = new https.Agent({
4241
ca: globalCA + trustwave
4342
});
4443

packages/comms/tests/testLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isBrowser, isCI as utilIsTravis } from "@hpcc-js/util";
22

33
export const isCI = utilIsTravis;
4+
// export const ESP_URL = isCI ? "https://play.hpccsystems.com:18010/" : "https://play.hpccsystems.com:18010/";
45
export const ESP_URL = isCI ? "http://127.0.0.1:8010/" : "http://127.0.0.1:8010/";
56
export const QUERY_URL = isCI ? "http://127.0.0.1:8002/" : "http://127.0.0.1:8002/";
67

0 commit comments

Comments
 (0)