Zikado
Zikado
Explore posts from servers
SSolidJS
Created by Zikado on 12/18/2024 in #support
Different server context when using "use server"?
Hi, I was trying to create a simple game project where player lobbies would (for simplicity) be saved on the server. Then I wanted to create some RPC using the "use server" so I can for example check whether the specific lobby exists etc. But I encountered that the state of the variable differed in the RPC functions and the rest of the server context, let me show an example on random number variable. src/server/number.ts
export const randomNumber = Math.random();
export const randomNumber = Math.random();
Then I created API endpoints just to be able to easily check whether the numbers match src/routes/api/getRandomNumber.ts
import type { APIEvent } from "@solidjs/start/server";
import { randomNumber } from "~/server/lobby";

export async function GET() {
console.log(randomNumber);
return "ok";
}
import type { APIEvent } from "@solidjs/start/server";
import { randomNumber } from "~/server/lobby";

export async function GET() {
console.log(randomNumber);
return "ok";
}
This is the RPC for getting the random number src/routes/utils/callbacks.ts
"use server";

import { randomNumber } from "~/server/number";

export const getRandomNumber = async () => {
console.log(randomNumber);
};
"use server";

import { randomNumber } from "~/server/number";

export const getRandomNumber = async () => {
console.log(randomNumber);
};
Lastly I have a page where I get the data from both sources so I can check whether they match
import { getRandomNumber } from "~/utils/callbacks";

export default function Page() {
const handleClick = () => {
getRandomNumber();
fetch("/api/getLobbies");
};

return <button on:click={handleClick}>Click</button>;
}
import { getRandomNumber } from "~/utils/callbacks";

export default function Page() {
const handleClick = () => {
getRandomNumber();
fetch("/api/getLobbies");
};

return <button on:click={handleClick}>Click</button>;
}
And this was the result: RPC: 0.7921163507084992 API: 0.4117746060818235 This probably comes from my lack of knowledge but I would really appreciate any explanation why does it behave that way and what is the proper way to handle it/change it to more suitable solution. Thank you in advance.
5 replies
TTCTheo's Typesafe Cult
Created by Zikado on 3/1/2024 in #questions
Planetscale billing
Hi, on account of the Theo's video about Netlify billing I panicked a little. I tried to find a mention about what happens when Planetscale free tier exceeds but only with a little luck in FAQ saying "Databases that exceed the Hobby plan in usage or storage without current billing may experience degraded service." which does not really rule out billing, so my question is what happens when exceeding the free tier? Thanks for any insight.
9 replies
TTCTheo's Typesafe Cult
Created by Zikado on 11/25/2023 in #questions
Compiling user's code - the right way
Hi, so I have been trying to create some kind of a leetcode like app for JS code only in my SvelteKit app. I run into a problem that I have no clue how to actually do that right. I have already been able to create something using 'isolated-vm' but it does not look like the best way to do it.
import { json } from "@sveltejs/kit"
import ivm from "isolated-vm"

export const POST = async ({ request }) => {
const { code } = await request.json()
console.log(code)
if (!code) throw new Error('No code provided');

const isolate = new ivm.Isolate();
const context = await isolate.createContext();

try {
const result = await (await isolate.compileScript(code)).run(context);
console.log(result)
if (result === undefined) return json('undefined');
return json(result.toString());
} catch (error) {
throw new Error(error);
} finally {
await isolate.dispose();
}
}
import { json } from "@sveltejs/kit"
import ivm from "isolated-vm"

export const POST = async ({ request }) => {
const { code } = await request.json()
console.log(code)
if (!code) throw new Error('No code provided');

const isolate = new ivm.Isolate();
const context = await isolate.createContext();

try {
const result = await (await isolate.compileScript(code)).run(context);
console.log(result)
if (result === undefined) return json('undefined');
return json(result.toString());
} catch (error) {
throw new Error(error);
} finally {
await isolate.dispose();
}
}
So that's it I guess, hope someone smart will come by this, thanks for any help :).
4 replies