Skip to content

Commit 9503913

Browse files
Revert "Remove __non_webpack_require__ workaround and split Node dependencies correctly (#48154)" (#56766)
This reverts commit 93520b6.
1 parent 3720ba6 commit 9503913

File tree

5 files changed

+33
-98
lines changed

5 files changed

+33
-98
lines changed

src/SignalR/clients/ts/signalr/package.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,5 @@
5050
"overrides": {
5151
"ansi-regex": "5.0.1",
5252
"tough-cookie": ">=4.1.3"
53-
},
54-
"browser": {
55-
"./src/DynamicImports.ts": "./src/DynamicImports.browser.ts",
56-
"abort-controller": false,
57-
"eventsource": false,
58-
"fetch-cookie": false,
59-
"node-fetch": false,
60-
"ws": false,
61-
"tough-cookie": false
6253
}
6354
}

src/SignalR/clients/ts/signalr/src/DynamicImports.browser.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/SignalR/clients/ts/signalr/src/DynamicImports.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/SignalR/clients/ts/signalr/src/FetchHttpClient.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { AbortError, HttpError, TimeoutError } from "./Errors";
88
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
99
import { ILogger, LogLevel } from "./ILogger";
1010
import { Platform, getGlobalThis, isArrayBuffer } from "./Utils";
11-
import { configureAbortController, configureFetch } from "./DynamicImports";
1211

1312
export class FetchHttpClient extends HttpClient {
1413
private readonly _abortControllerType: { prototype: AbortController, new(): AbortController };
@@ -21,19 +20,38 @@ export class FetchHttpClient extends HttpClient {
2120
super();
2221
this._logger = logger;
2322

24-
// This is how you do "reference" arguments
25-
const fetchObj = { _fetchType: undefined, _jar: undefined };
26-
if (configureFetch(fetchObj)) {
27-
this._fetchType = fetchObj._fetchType!;
28-
this._jar = fetchObj._jar;
23+
// Node added a fetch implementation to the global scope starting in v18.
24+
// We need to add a cookie jar in node to be able to share cookies with WebSocket
25+
if (typeof fetch === "undefined" || Platform.isNode) {
26+
// In order to ignore the dynamic require in webpack builds we need to do this magic
27+
// @ts-ignore: TS doesn't know about these names
28+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
29+
30+
// Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
31+
this._jar = new (requireFunc("tough-cookie")).CookieJar();
32+
33+
if (typeof fetch === "undefined") {
34+
this._fetchType = requireFunc("node-fetch");
35+
} else {
36+
// Use fetch from Node if available
37+
this._fetchType = fetch;
38+
}
39+
40+
// node-fetch doesn't have a nice API for getting and setting cookies
41+
// fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
42+
this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar);
2943
} else {
3044
this._fetchType = fetch.bind(getGlobalThis());
3145
}
46+
if (typeof AbortController === "undefined") {
47+
// In order to ignore the dynamic require in webpack builds we need to do this magic
48+
// @ts-ignore: TS doesn't know about these names
49+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
3250

33-
this._abortControllerType = AbortController;
34-
const abortObj = { _abortControllerType: this._abortControllerType };
35-
if (configureAbortController(abortObj)) {
36-
this._abortControllerType = abortObj._abortControllerType;
51+
// Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
52+
this._abortControllerType = requireFunc("abort-controller");
53+
} else {
54+
this._abortControllerType = AbortController;
3755
}
3856
}
3957

src/SignalR/clients/ts/signalr/src/HttpConnection.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { AccessTokenHttpClient } from "./AccessTokenHttpClient";
55
import { DefaultHttpClient } from "./DefaultHttpClient";
6-
import { getEventSource, getWS } from "./DynamicImports";
76
import { AggregateErrors, DisabledTransportError, FailedToNegotiateWithServerError, FailedToStartTransportError, HttpError, UnsupportedTransportError, AbortError } from "./Errors";
87
import { IConnection } from "./IConnection";
98
import { IHttpConnectionOptions } from "./IHttpConnectionOptions";
@@ -88,8 +87,11 @@ export class HttpConnection implements IConnection {
8887
let eventSourceModule: any = null;
8988

9089
if (Platform.isNode && typeof require !== "undefined") {
91-
webSocketModule = getWS();
92-
eventSourceModule = getEventSource();
90+
// In order to ignore the dynamic require in webpack builds we need to do this magic
91+
// @ts-ignore: TS doesn't know about these names
92+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
93+
webSocketModule = requireFunc("ws");
94+
eventSourceModule = requireFunc("eventsource");
9395
}
9496

9597
if (!Platform.isNode && typeof WebSocket !== "undefined" && !options.WebSocket) {

0 commit comments

Comments
 (0)