FoxxMD / multi-scrobbler

Scrobble plays from multiple sources to multiple clients
https://foxxmd.github.io/multi-scrobbler
MIT License
421 stars 17 forks source link

PlexSource crashing (Cannot read properties of undefined (reading 'includes')) #191

Closed jackwilsdon closed 2 months ago

jackwilsdon commented 2 months ago

Please check the FAQ before submitting a bug report.

Describe the bug The Plex source causes multi-scrobbler to crash.

To Reproduce Steps to reproduce the behavior:

  1. Set up the Plex source (using the webhook)
  2. When Plex calls the webhook, multi-scrobbler crashes.

Expected behavior multi-scrobbler correctly scrobbles from Plex.

Logs If possible reproduce the issue with debug logging ON

[2024-09-13 13:33:01.350 +0000] VERBOSE: [Init] Config Dir ENV: /nix/store/h181nga2lmr3rba4s8ldr24bdia15665-config.json -> Resolved: /nix/store/h181nga2lmr3rba4s8ldr24bdia15665-config.json
[2024-09-13 13:33:01.379 +0000] INFO   : [Init] Version: 0.8.3
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
1:33:01 PM [vite-express] Running in production mode
1:33:01 PM [vite-express] Inline config detected, ignoring Vite config file
1:33:01 PM [vite-express] Serving static files from /nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/dist
[2024-09-13 13:33:01.407 +0000] INFO   : [App] [API] Server started:
---> Local          http://localhost:9078
[2024-09-13 13:33:01.505 +0000] DEBUG  : [App] [Scrobblers] Constructing maloja (Maloja) client...
[2024-09-13 13:33:01.507 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Client Added
[2024-09-13 13:33:01.508 +0000] INFO   : [App] Starting scrobble clients...
[2024-09-13 13:33:01.509 +0000] DEBUG  : [App] [Scrobblers] [Maloja - Maloja] Attempting to initialize...
[2024-09-13 13:33:01.511 +0000] VERBOSE: [App] [Scrobblers] [Maloja - Maloja] Building required data init succeeded
[2024-09-13 13:33:01.527 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Communication test succeeded.
[2024-09-13 13:33:01.528 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Maloja Server Version: 3.2.2
[2024-09-13 13:33:01.533 +0000] VERBOSE: [App] [Scrobblers] [Maloja - Maloja] Connection check succeeded
[2024-09-13 13:33:01.537 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Auth test passed!
[2024-09-13 13:33:01.538 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Fully Initialized!
[2024-09-13 13:33:01.539 +0000] VERBOSE: [App] [Scrobblers] [Maloja - Maloja] Fetching up to 100 initial scrobbles...
[2024-09-13 13:33:01.539 +0000] DEBUG  : [App] [Scrobblers] [Maloja - Maloja] Refreshing recent scrobbles
[2024-09-13 13:33:01.551 +0000] DEBUG  : [App] [Scrobblers] [Maloja - Maloja] Found 100 recent scrobbles
[2024-09-13 13:33:01.554 +0000] INFO   : [App] [Scrobblers] [Maloja - Maloja] Scrobble processing started
[2024-09-13 13:33:01.675 +0000] DEBUG  : [App] [Sources] (Plex) Constructing plex source
[2024-09-13 13:33:01.676 +0000] INFO   : [App] [Sources] [Plex - Plex] Initializing with the following filters => Users: N/A | Libraries: music | Servers: nidhogg
[2024-09-13 13:33:01.677 +0000] DEBUG  : [App] [Sources] [Plex - Plex] Attempting to initialize...
[2024-09-13 13:33:01.677 +0000] DEBUG  : [App] [Sources] [Plex - Plex] No required data to build.
[2024-09-13 13:33:01.677 +0000] DEBUG  : [App] [Sources] [Plex - Plex] Connection check was not required.
[2024-09-13 13:33:01.678 +0000] INFO   : [App] [Sources] [Plex - Plex] Fully Initialized!
[2024-09-13 13:33:01.679 +0000] INFO   : [App] Scheduler started.
[2024-09-13 13:33:10.774 +0000] INFO   : [App] [API] [Ingress] [Plex] Received request from a new remote address: 10.99.99.4 (UA: PlexMediaServer/1.40.5.8921-836b34c27)
[2024-09-13 13:33:10.776 +0000] DEBUG  : [App] [API] [Plex Request] Receiving request from Plex...
[2024-09-13 13:33:10.778 +0000] DEBUG  : [App] [API] [Plex Request] Received 0 bytes of expected 8921
[2024-09-13 13:33:10.782 +0000] DEBUG  : [App] [API] [Plex Request] Received 8921 bytes of expected 8921
[2024-09-13 13:33:10.788 +0000] DEBUG  : [App] [API] [Plex Request] File Begin: payload
[2024-09-13 13:33:10.795 +0000] DEBUG  : [App] [API] [Plex Request] File Received: payload
[2024-09-13 13:33:10.796 +0000] DEBUG  : [App] [API] [Plex Request] File Begin: thumb
[2024-09-13 13:33:10.798 +0000] DEBUG  : [App] [API] [Plex Request] File Received: thumb
[2024-09-13 13:33:10.798 +0000] DEBUG  : [App] [API] [Plex Request] Received end of form data from Plex
[2024-09-13 13:33:10.800 +0000] ERROR  : [App] Uncaught exception is crashing the app! :( Type: uncaughtException
Error: Uncaught exception is crashing the app! :( Type: uncaughtException
    at process.<anonymous> (CWD/src/backend/index.ts:42:22)
    at process.emit (node:events:519:28)
    at process.emit (CWD/node_modules/tsx/dist/suppress-warnings.cjs:1:472)
    at process._fatalException (node:internal/process/execution:185:13)
caused by: TypeError: Cannot read properties of undefined (reading 'includes')
    at <anonymous> (CWD/src/backend/sources/PlexSource.ts:280:44)
    at zalgoSafe (CWD/node_modules/dezalgo/dezalgo.js:20:10)
    at f (CWD/node_modules/once/once.js:25:25)
    at IncomingForm.<anonymous> (file://CWD/node_modules/formidable/src/Formidable.js:228:7)
    at IncomingForm.emit (node:events:531:35)
    at IncomingForm._maybeEnd (file://CWD/node_modules/formidable/src/Formidable.js:679:10)
    at file://CWD/node_modules/formidable/src/Formidable.js:438:14
    at ConcatStream.<anonymous> (file://CWD/node_modules/formidable/src/VolatileFile.js:75:7)
    at Object.onceWrapper (node:events:633:28)
    at ConcatStream.emit (node:events:531:35)
/nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/src/backend/sources/PlexSource.ts:280
                    if (namedFile.mimetype.includes('json')) {
                                           ^
TypeError: Cannot read properties of undefined (reading 'includes')
    at <anonymous> (/nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/src/backend/sources/PlexSource.ts:280:44)
    at zalgoSafe (/nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/dezalgo/dezalgo.js:20:10)
    at f (/nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/once/once.js:25:25)
    at IncomingForm.<anonymous> (file:///nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/formidable/src/Formidable.js:228:7)
    at IncomingForm.emit (node:events:531:35)
    at IncomingForm._maybeEnd (file:///nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/formidable/src/Formidable.js:679:10)
    at file:///nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/formidable/src/Formidable.js:438:14
    at ConcatStream.<anonymous> (file:///nix/store/h2v2qhcs5z3czn95qzm0cmwz7px43zyl-multi-scrobbler-2024-09-11/lib/node_modules/multi-scrobbler/node_modules/formidable/src/VolatileFile.js:75:7)
    at Object.onceWrapper (node:events:633:28)
    at ConcatStream.emit (node:events:531:35)
Node.js v20.17.0

Versions (please complete the following information): Provide version information for any related sources/clients.

Additional context Here's the structure of the webhook request triggering the issue:

--------------------------G7Eg6j8dMUI03PAbXJmOc2
Content-Disposition: form-data; name="payload"
Content-Type: application/json

- bunch of json here -
--------------------------G7Eg6j8dMUI03PAbXJmOc2
Content-Disposition: form-data; name="thumb"; filename="thumb.jpg"
Content-Type: image/jpeg

- bunch of binary data here -
--------------------------G7Eg6j8dMUI03PAbXJmOc2--

I'm not sure how mimetype is ending up undefined here: https://github.com/FoxxMD/multi-scrobbler/blob/a1443594f7a508a8b580c8234a11e35719f6e8c3/src/backend/sources/PlexSource.ts#L280

The docs for formidable seem to suggest it can be null: https://www.npmjs.com/package/formidable#file

I'm also seeing this under Node.js v18.20.4. Weirdly I didn't have this issue when I first set up multi-scrobbler a few days ago, so I'm not sure what has changed (I may have updated Plex?).

FoxxMD commented 2 months ago

I do think it's due to a Plex upgrade. I'm able to reproduce as well but haven't had issues in the past. Though it could also be something internal about how Plex decides to send the payload because this was accounted for in the past but I changed it. Plex seems to be waffling between sending the json as a single file or as a nested file within an array. In the past it was an array, then it was a regular file, now it's back to an array :shrug:

I've added code to defensively check for both types of structures it can send which should fix this issue.

You should be able to pull foxxmd/multi-scrobbler:pr-192 soon or use the https://github.com/FoxxMD/multi-scrobbler/tree/GH-191/plex-formdata-fix branch now

jackwilsdon commented 2 months ago

Yep that has fixed it, thanks!