Skip to content

Commit

Permalink
Revert "Remove __non_webpack_require__ workaround and split Node depe…
Browse files Browse the repository at this point in the history
…ndencies correctly (#48154)" (#56766)

This reverts commit 93520b6.
  • Loading branch information
BrennanConroy authored Jul 22, 2024
1 parent 3720ba6 commit 9503913
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 98 deletions.
9 changes: 0 additions & 9 deletions src/SignalR/clients/ts/signalr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,5 @@
"overrides": {
"ansi-regex": "5.0.1",
"tough-cookie": ">=4.1.3"
},
"browser": {
"./src/DynamicImports.ts": "./src/DynamicImports.browser.ts",
"abort-controller": false,
"eventsource": false,
"fetch-cookie": false,
"node-fetch": false,
"ws": false,
"tough-cookie": false
}
}
22 changes: 0 additions & 22 deletions src/SignalR/clients/ts/signalr/src/DynamicImports.browser.ts

This file was deleted.

54 changes: 0 additions & 54 deletions src/SignalR/clients/ts/signalr/src/DynamicImports.ts

This file was deleted.

38 changes: 28 additions & 10 deletions src/SignalR/clients/ts/signalr/src/FetchHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AbortError, HttpError, TimeoutError } from "./Errors";
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
import { ILogger, LogLevel } from "./ILogger";
import { Platform, getGlobalThis, isArrayBuffer } from "./Utils";
import { configureAbortController, configureFetch } from "./DynamicImports";

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

// This is how you do "reference" arguments
const fetchObj = { _fetchType: undefined, _jar: undefined };
if (configureFetch(fetchObj)) {
this._fetchType = fetchObj._fetchType!;
this._jar = fetchObj._jar;
// Node added a fetch implementation to the global scope starting in v18.
// We need to add a cookie jar in node to be able to share cookies with WebSocket
if (typeof fetch === "undefined" || Platform.isNode) {
// In order to ignore the dynamic require in webpack builds we need to do this magic
// @ts-ignore: TS doesn't know about these names
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;

// Cookies aren't automatically handled in Node so we need to add a CookieJar to preserve cookies across requests
this._jar = new (requireFunc("tough-cookie")).CookieJar();

if (typeof fetch === "undefined") {
this._fetchType = requireFunc("node-fetch");
} else {
// Use fetch from Node if available
this._fetchType = fetch;
}

// node-fetch doesn't have a nice API for getting and setting cookies
// fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar);
} else {
this._fetchType = fetch.bind(getGlobalThis());
}
if (typeof AbortController === "undefined") {
// In order to ignore the dynamic require in webpack builds we need to do this magic
// @ts-ignore: TS doesn't know about these names
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;

this._abortControllerType = AbortController;
const abortObj = { _abortControllerType: this._abortControllerType };
if (configureAbortController(abortObj)) {
this._abortControllerType = abortObj._abortControllerType;
// Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
this._abortControllerType = requireFunc("abort-controller");
} else {
this._abortControllerType = AbortController;
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/SignalR/clients/ts/signalr/src/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

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

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

0 comments on commit 9503913

Please sign in to comment.