dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.79k stars 9.83k forks source link

SignalR | Electron | CookieJar is creating issue with Electron .Net App + Angular 14 #52656

Open IamAgrawal opened 6 months ago

IamAgrawal commented 6 months ago

Is there an existing issue for this?

Describe the bug

I am using Electron.Net as backend and Angular as frontend to initiate System App. When I am trying to setup connection with HubConnectionBuilder, I am getting issue:

CookieJar is not a constructor

For Further research, I got to realize that this issue arises due to its requirement of fetch-cookie in DefaultHttpClient.ts file in @microsoft/signalR package. For now, I have used downgraded version of it, but will be looking for solution with latest version 8.0.0. My frontend code is like below:

this.hubConnectionBuilder = new HubConnectionBuilder() .configureLogging(LogLevel.Information) .withUrl("http://localhost:4200/ABCD") .build()

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0.414

Anything else?

No response

universal commented 4 months ago

i ran into a similar issue with an electron app when upgrading from signalr 6 to 8.

after adding

  "resolutions": {
    "tough-cookie": "4.1.3"
  }

to my package.json it appears to be working again!

During debugging this I noticed that the call to CookieJar.getCookieString(...) had the callback and the url in a different order, than the code seemed to expect it. Without the additional resolution restriction in my project (lerna monorepo) I end up with multiple different versions of tough-cookie and something along the way goes haywire...

It appears that somehow the wrong (old) version gets required...

Output with the resolution restriction

yarn why tough-cookie
yarn why v1.22.19
[1/4] Why do we have the module "tough-cookie"...?
[2/4] Initialising dependency graph...
warning Resolution field "tough-cookie@4.1.3" is incompatible with requested version "tough-cookie@^3.0.1"
warning Resolution field "tough-cookie@4.1.3" is incompatible with requested version "tough-cookie@~2.5.0"
warning Resolution field "tough-cookie@4.1.3" is incompatible with requested version "tough-cookie@^2.3.3"
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "tough-cookie@4.1.3"
info Reasons this module exists
   - "_project_#jest-environment-jsdom#jsdom" depends on it
   - Hoisted from "_project_#jest-environment-jsdom#jsdom#tough-cookie"
   - Hoisted from "_project_#@app#infrastructure#@microsoft#signalr#fetch-cookie#tough-cookie"
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#tough-cookie"
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#request-promise-native#tough-cookie"
   - Hoisted from "_project_#@app#infrastructure#@jest-runner#electron#jest-runner#jest-config#jest-environment-jsdom#jsdom#tough-cookie"
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#request#tough-cookie"
info Disk size without dependencies: "160KB"
info Disk size with unique dependencies: "784KB"
info Disk size with transitive dependencies: "828KB"
info Number of shared dependencies: 5
Done in 0.62s.

Output without the additional resolution restriction:

yarn why tough-cookie
yarn why v1.22.19
[1/4] Why do we have the module "tough-cookie"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "tough-cookie@2.5.0"
info Has been hoisted to "tough-cookie"
info Reasons this module exists
   - "workspace-aggregator-0efcd8c3-fafa-44cd-9736-47bb72ea7074" depends on it
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#request-promise-native#tough-cookie"
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#request#tough-cookie"
info Disk size without dependencies: "108KB"
info Disk size with unique dependencies: "632KB"
info Disk size with transitive dependencies: "632KB"
info Number of shared dependencies: 2
=> Found "jsdom#tough-cookie@4.1.3"
info This module exists because "_project_#jest-environment-jsdom#jsdom" depends on it.
info Disk size without dependencies: "136KB"
info Disk size with unique dependencies: "760KB"
info Disk size with transitive dependencies: "804KB"
info Number of shared dependencies: 5
=> Found "fetch-cookie#tough-cookie@4.1.3"
info This module exists because "_project_#@app#infrastructure#@microsoft#signalr#fetch-cookie" depends on it.
info Disk size without dependencies: "136KB"
info Disk size with unique dependencies: "760KB"
info Disk size with transitive dependencies: "804KB"
info Number of shared dependencies: 5
=> Found "jest-jasmine2#tough-cookie@3.0.1"
info Reasons this module exists
   - "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom" depends on it
   - Hoisted from "_project_#jest-jasmine2#jest-runtime#jest-config#jest-environment-jsdom#jsdom#tough-cookie"
info Disk size without dependencies: "112KB"
info Disk size with unique dependencies: "652KB"
info Disk size with transitive dependencies: "652KB"
info Number of shared dependencies: 3
=> Found "@jest-runner/electron#tough-cookie@3.0.1"
info Reasons this module exists
   - "_project_#@app#infrastructure#@jest-runner#electron#jest-runner#jest-config#jest-environment-jsdom#jsdom" depends on it
   - Hoisted from "_project_#@app#infrastructure#@jest-runner#electron#jest-runner#jest-config#jest-environment-jsdom#jsdom#tough-cookie"
info Disk size without dependencies: "112KB"
info Disk size with unique dependencies: "652KB"
info Disk size with transitive dependencies: "652KB"
info Number of shared dependencies: 3
Done in 0.58s.
universal commented 4 months ago

adding an explicit dependency to "tough-cookie": "^4.1.3" in the electron project appears to also be working. (lerna monorepo...)

adding trace log if someone ends up googling for this error...

@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Starting HubConnection.
@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Starting connection with transfer format 'Text'.
@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Sending negotiation request: https://some-domain.non-existent/somehub/negotiate?negotiateVersion=1.
@app/infrastructure: [2024-02-21T16:43:22.246Z] Debug: HubConnection failed to start successfully because of error 'Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')'.
@app/infrastructure: [2024-02-21T16:43:22.246Z] Warning: Error from HTTP request. TypeError: Cannot read properties of undefined (reading 'secure').
@app/infrastructure: [2024-02-21T16:43:22.246Z] Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')
@app/infrastructure: [2024-02-21T16:43:22.246Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')
danluc commented 4 months ago

adding an explicit dependency to "tough-cookie": "^4.1.3" in the electron project appears to also be working. (lerna monorepo...)

adding trace log if someone ends up googling for this error...

@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Starting HubConnection.
@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Starting connection with transfer format 'Text'.
@app/infrastructure: [2024-02-21T16:43:22.245Z] Debug: Sending negotiation request: https://some-domain.non-existent/somehub/negotiate?negotiateVersion=1.
@app/infrastructure: [2024-02-21T16:43:22.246Z] Debug: HubConnection failed to start successfully because of error 'Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')'.
@app/infrastructure: [2024-02-21T16:43:22.246Z] Warning: Error from HTTP request. TypeError: Cannot read properties of undefined (reading 'secure').
@app/infrastructure: [2024-02-21T16:43:22.246Z] Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')
@app/infrastructure: [2024-02-21T16:43:22.246Z] Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: Cannot read properties of undefined (reading 'secure')

where did you put this? Could you show the complete path? I have the same problem

universal commented 4 months ago

where did you put this? Could you show the complete path? I have the same problem

the log level can be set in the connection constructor like this:

this.connection = new signalR.HubConnectionBuilder()
      .withAutomaticReconnect({
        nextRetryDelayInMilliseconds: () =>
          1000 + (Math.random() * 100)
      })
      .configureLogging(signalR.LogLevel.Trace)
      .withUrl(this.options.url.href)
      .build()

and tough-cookie dependency is added to the package.json, for me in packages/infrastructure/package.json

...
  "dependencies": {
    "@app/services": "^1.3.0",
    "@microsoft/signalr": "^8.0.0",
    "electron-unhandled": "^4.0.1",
    "tough-cookie": "^4.1.3"
  },
...
danluc commented 4 months ago

onde você colocou isso? Você poderia mostrar o caminho completo? Eu tenho o mesmo problema

o nível de log pode ser definido no construtor de conexão assim:

this.connection = new signalR.HubConnectionBuilder()
      .withAutomaticReconnect({
        nextRetryDelayInMilliseconds: () =>
          1000 + (Math.random() * 100)
      })
      .configureLogging(signalR.LogLevel.Trace)
      .withUrl(this.options.url.href)
      .build()

e a dependência de cookies resistentes é adicionada ao package.json, para mim empackages/infrastructure/package.json

...
  "dependencies": {
    "@app/services": "^1.3.0",
    "@microsoft/signalr": "^8.0.0",
    "electron-unhandled": "^4.0.1",
    "tough-cookie": "^4.1.3"
  },
...

does not work, but thanks for the help

danluc commented 4 months ago

where did you put this? Could you show the complete path? I have the same problem

the log level can be set in the connection constructor like this:

this.connection = new signalR.HubConnectionBuilder()
      .withAutomaticReconnect({
        nextRetryDelayInMilliseconds: () =>
          1000 + (Math.random() * 100)
      })
      .configureLogging(signalR.LogLevel.Trace)
      .withUrl(this.options.url.href)
      .build()

and tough-cookie dependency is added to the package.json, for me in packages/infrastructure/package.json

...
  "dependencies": {
    "@app/services": "^1.3.0",
    "@microsoft/signalr": "^8.0.0",
    "electron-unhandled": "^4.0.1",
    "tough-cookie": "^4.1.3"
  },
...

I downgraded @microsoft/signalr 8 to 6.0.1 and it worked

reworc commented 1 month ago

I have a similar issue after upgrading to version 8.0.0 (Angular v17 with ASP.NET mono repo) (cf. https://github.com/visualengineers/reflex) The issue only arises when packaging the app with Electron.NET, running angular in dev environment works. For me, only downgrading to Version 6.0.1 solved the issue