junlarsen / league-connect

:electric_plug: Node.js HTTP/1.1, HTTP/2.0 and WebSocket interface to the League of Legends Client APIs
https://www.npmjs.com/package/league-connect
MIT License
156 stars 24 forks source link

SyntaxError: Unexpected end of JSON input #81

Closed EGo14T closed 2 years ago

EGo14T commented 2 years ago

Package Version: 6.0.0-rc3 Node: v16.14 Electron: 18.2.3

When I use this api, without body or body: undefined,

export const acceptGame = async (credentials: Credentials) => {
  try {
    await createHttp1Request({
      method: "POST",
      url: '/lol-matchmaking/v1/ready-check/accept',
    }, credentials)
  } catch (e) {
    console.error(e)
  }
}

This error will appear in the program

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (\node_modules\league-connect\dist\http.js:66:43)
    at IncomingMessage.emit (node:events:402:35)
    at IncomingMessage.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Snipaste_2022-07-14_00-23-25

If response has no content ,this error will happen

const json = JSON.parse("");
junlarsen commented 2 years ago

Oh whoops, I thought I fixed that in #73 but I now see it wasn't fully patched. Will take a look at it tomorrow morning.

Thank you so much for the report! ❤️

junlarsen commented 2 years ago

Looks like I lost a commit somewhere along the way, I'll recreate it and try to fix it as soon as I have time

EGo14T commented 2 years ago

Thanks ~ 😁

junlarsen commented 2 years ago

I currently don't have a windows machine available to test, but I'll be able to get something done start of next week

junlarsen commented 2 years ago

I've published 6.0.0-rc4 (should be available on npm shortly). Please give it another try (and note that the package is esm-only now)

EGo14T commented 2 years ago

I use league-connect/example to test, I thought it be solved; But I can't use v6-rc4 (esm-only) in my electron program very well, looks like #29657

junlarsen commented 2 years ago

I see, I'll see if there's something electron can do, otherwise I'll dual publish esm and commonjs

junlarsen commented 2 years ago

I've published 6.0.0-rc5 which bundles both CommonJS and ESM, could you give this a try?

EGo14T commented 2 years ago

ummmm, when I use v6-rc5 in my program, it threws an error

App threw an error during load
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Demo\node_modules\node-fetch\src\index.js from D:\Demo\node_modules\league-connect\dist\index.cjs not supported.
Instead change the require of index.js in D:\Demo\node_modules\league-connect\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.require.extensions.<computed> [as .js] (D:\Demo\node_modules\ts-node\dist\index.js:785:20)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
    at Function.Module._load (D:\Demo\node_modules\runtime-required\runtime-required.js:34:23)
    at Object.<anonymous> (D:\Demo\node_modules\league-connect\dist\index.cjs:417:33)
    at Object.require.extensions.<computed> [as .js] (D:\Demo\node_modules\ts-node\dist\index.js:785:20)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
    at Function.Module._load (D:\Demo\node_modules\runtime-required\runtime-required.js:34:23)
    at Object.<anonymous> (D:\Demo\src\main\main.ts:17:26)
    at Module.m._compile (D:\Demo\node_modules\ts-node\dist\index.js:791:29)
    at Object.require.extensions.<computed> [as .ts] (D:\Demo\node_modules\ts-node\dist\index.js:793:16)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
    at Function.Module._load (D:\Demo\node_modules\runtime-required\runtime-required.js:34:23)
    at loadApplicationPackage (D:\Demo\node_modules\electron\dist\resources\default_app.asar\main.js:110:16)
    at Object.<anonymous> (D:\Demo\node_modules\electron\dist\resources\default_app.asar\main.js:222:9)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
    at Object.<anonymous> (node:electron/js2c/browser_init:185:3104)
    at Object../lib/browser/init.ts (node:electron/js2c/browser_init:185:3308)
    at __webpack_require__ (node:electron/js2c/browser_init:1:128)
    at node:electron/js2c/browser_init:1:1200
    at node:electron/js2c/browser_init:1:1267
    at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
junlarsen commented 2 years ago

Strange, will investigate

junlarsen commented 2 years ago

Found the problem, I recently upgraded to node-fetch 3.x which is ESM-only but I've reverted it to 2.x which is require() compatible. Can you try to download 6.0.0-rc6?

If the issue still persists, please open a new issue

EGo14T commented 2 years ago

It‘s all right now! Thank you so much!😊