PteroJS icon indicating copy to clipboard operation
PteroJS copied to clipboard

Fetching server causes a "maximum call stack size exceeded" error

Open kivox opened this issue 2 years ago • 8 comments

When used in NextJS's API Handlers

import { type NextApiRequest, type NextApiResponse } from "next";
import { pteroAdmin } from "~/server/pterodactyl";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  return res.status(200).json(await pteroAdmin.servers.fetch("test"));
};

export default handler;

Issue is specific to NextJS, same does not occur when Node alone is used.

const { PteroApp } = require("@devnote-dev/pterojs");

const pteroAdmin = new PteroApp(
  "https://panel.",
  ""
);

pteroAdmin.servers
  .fetch("test")
  .then((servers) => {
    console.log(servers);
  })
  .catch((error) => {
    console.error(error);
  });

The issue seems to stem from the function "toSnakeCase"

error - Error [RangeError]: Maximum call stack size exceeded
    at toSnakeCase (file:///home/.../node_modules/@devnote-dev/pterojs/dist/index.mjs:300:21)
    at toSnakeCase (file:///home/.../node_modules/@devnote-dev/pterojs/dist/index.mjs:323:11)
    at toSnakeCase (file:///home/.../node_modules/@devnote-dev/pterojs/dist/index.mjs:323:11)
    ...
    digest: undefined
}

kivox avatar May 12 '23 16:05 kivox

How many servers do you have on your panel? Keep in mind that #fetchAll is recursive and will attempt to cycle through every page of servers available.

devnote-dev avatar May 12 '23 21:05 devnote-dev

How many servers do you have on your panel? Keep in mind that #fetchAll is recursive and will attempt to cycle through every page of servers available.

I've only got about 20 servers on the panel, so it shouldn't really be an issue. I have a feeling webpack or some of the underlying NextJS code might be at fault, as normal js works perfectly fine.

kivox avatar May 13 '23 00:05 kivox

I am also getting this issue also with Express. I haven't got many servers.

WavingCatDevs avatar May 13 '23 15:05 WavingCatDevs

Some additional info

Attempted to fix it, but it's not the correct fix, it's an issue with the conversion of the object to JSON.

import { type NextApiRequest, type NextApiResponse } from "next";
import { pteroAdmin } from "~/server/pterodactyl";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  const server = await pteroAdmin.servers.fetch("test");

  return res.status(200).json(server);
};

export default handler;

Same error occurs with res.status(200).json(server.toJSON());, but when I use server.id instead of just server it works fine.

kivox avatar May 14 '23 20:05 kivox

.toJSON() is also affected. console.log(server) works but console.log(server.toJSON()); doesn't

kivox avatar May 14 '23 20:05 kivox

const pteroSrv = await ctx.pteroAdmin.servers.fetch(srv.id);

return {
   ...server
}

Causes the following error

Converting circular structure to JSON
    --> starting at object with constructor 'PteroApp'
    |     property 'allocations' -> object with constructor 'NodeAllocationManager'
    --- property 'client' closes the circle

Using the library built-in toJSON causes Maximum call stack size exceeded

kivox avatar May 14 '23 21:05 kivox

It looks like there's still objects in the ApplicationServer class that the case conversion functions are trying to handle which are evidently circularly-referenced, hence the recursion issues. I'll need to do more debugging into it to see what fields cause it though.

devnote-dev avatar May 14 '23 22:05 devnote-dev

I've had this same issue. Try deleting the database property from the ApplicationServer object and then calling .toJSON()

FC5570 avatar May 18 '23 19:05 FC5570