Fetching server causes a "maximum call stack size exceeded" error
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
}
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.
How many servers do you have on your panel? Keep in mind that
#fetchAllis 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.
I am also getting this issue also with Express. I haven't got many servers.
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.
.toJSON() is also affected. console.log(server) works but console.log(server.toJSON()); doesn't
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
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.
I've had this same issue. Try deleting the database property from the ApplicationServer object and then calling .toJSON()