Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove __non_webpack_require__ workaround and split Node dependencies correctly (#48154)" #55229

Merged
merged 1 commit into from May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/SignalR/clients/ts/signalr/package.json
Expand Up @@ -59,14 +59,5 @@
},
"resolutions": {
"ansi-regex": "5.0.1"
},
"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
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
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