Skip to content

Commit

Permalink
Fix Fetch + Cookies when using Node 18+ (#48076)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored May 5, 2023
1 parent 801e24f commit a258916
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/SignalR/clients/ts/signalr/src/FetchHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ export class FetchHttpClient extends HttpClient {
super();
this._logger = logger;

if (typeof fetch === "undefined") {
// 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();
this._fetchType = requireFunc("node-fetch");

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
Expand Down

0 comments on commit a258916

Please sign in to comment.