funki
funki
Explore posts from servers
DDeno
Created by funki on 10/11/2024 in #help
Stream a Sharp object / Node Readable with Deno.serve()
This works but isn't quite as fast as piping to the response object of node:http's createServer. Can i somehow pipe the Sharp object directly to the response without writing my own ReadableStream?
import sharp from "npm:sharp";

Deno.serve(() => {
const thing = sharp("img.png").webp(); // 'thing' implements node:streams/Duplex

const body = new ReadableStream({
start(controller) {
thing.on("data", (chunk) => controller.enqueue(chunk));
thing.on("end", () => controller.close());
},
});

return new Response(body);
});
import sharp from "npm:sharp";

Deno.serve(() => {
const thing = sharp("img.png").webp(); // 'thing' implements node:streams/Duplex

const body = new ReadableStream({
start(controller) {
thing.on("data", (chunk) => controller.enqueue(chunk));
thing.on("end", () => controller.close());
},
});

return new Response(body);
});
(Note: cancel() and headers omitted for brevity)
4 replies
DDeno
Created by funki on 10/11/2024 in #help
Deno.serve() + sharp + streams
Hi, i'd like to read an image, convert it, and respond with the result. I'd like to optimise for performance (request time) and low memory usage as much as possible, which is why i tried to use Streams. Unfortunately, i couldn't get my code to work with Streams and i'm very confused between the JavaScript native Streams, Deno's stdlib, and Node's streams module. Here's my tiny example which works but is using toBuffer() which i think could be optimised using Streams:
import sharp from "npm:sharp";

Deno.serve(async () => new Response(
await sharp("img.jpg").webp().toBuffer()
));
import sharp from "npm:sharp";

Deno.serve(async () => new Response(
await sharp("img.jpg").webp().toBuffer()
));
How can i change this code to use Streams and go as fast as possible?
34 replies
TtRPC
Created by funki on 9/23/2024 in #❓-help
Why not create queryClient and trpcClient outside of React?
From the docs: https://trpc.io/docs/client/react/setup#4-add-trpc-providers Relevant excerpt:
export function App() {
const [queryClient] = useState(() => new QueryClient())
const [trpcClient] = useState(() =>
trpc.createClient({ /* ... */ }),
)

// ...
export function App() {
const [queryClient] = useState(() => new QueryClient())
const [trpcClient] = useState(() =>
trpc.createClient({ /* ... */ }),
)

// ...
What are the downsides to moving these two clients outside the component? Does it break hot reloading or something? E.g.
const queryClient = new QueryClient()
const trpcClient = trpc.createClient({ /* ... */ })

export function App() {
// ...
const queryClient = new QueryClient()
const trpcClient = trpc.createClient({ /* ... */ })

export function App() {
// ...
4 replies
TtRPC
Created by funki on 6/21/2024 in #❓-help
Why are `new QueryClient` and `trpc.createClient` run inside a component in the React setup?
From https://trpc.io/docs/client/react/setup:
function App() {
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() =>
trpc.createClient({ ... }),
);
// ...
function App() {
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() =>
trpc.createClient({ ... }),
);
// ...
if these two are "made stable" through useState() and since they should exist only once per SPA (like a singleton), why not just put them outside the component? is this related to HMR?
25 replies
DTDrizzle Team
Created by funki on 5/9/2024 in #help
How do i create an SQLite file?
Hi, i don't need migrations and would like to use drizzle-kit push:sqlite, but i get an error from better-sqlite3: Cannot open database because the directory does not exist. The directory does exist, but the file doesn't.
1 replies
ATApache TinkerPop
Created by funki on 2/12/2024 in #questions
Which database should i use for my DJ set planning software?
Hi, i want to develop a software that lets DJs plan a set (i.e. playlist) and i'm wondering if graph databases are the right way and if yes, which one to pick. The workflow is that the software automatically reads all tracks from the DJ software on the computer and the DJ has to tell the software which tracks mix well together. These links are called "transitions" and contain some data like difficulty grade, quality rating and notes. The software should then assist DJs when selecting tracks (or specifically the next track) for a playlist by considering the "non-repeating transition depth" of a given track. In an RDBMS (SQL) i'd need to use WITH RECURSIVE to accomplish something like this. There should also be query options for considering the rating of the transitions, i.e. sort suggested next tracks by "most tracks to follow up with" (excluding any tracks already in the playlist!) or sort them by highest average rating with at least X number of tracks in a valid transition chain. I hope i could illustrate what i'm looking for. I'm a web developer and want to build this project using Node.js as the runtime and writing the code in TypeScript, but i can live without type helpers (like Prisma generates for example). I'm also not afraid of writing raw queries like i write raw SQL occasionally. A query builder is nice but not required. Also there should be a simple docker container to set up the database for development unless it's a serverless DB like SQLite. Should i be using a graph database for this project or should i stick with RDBMS / SQL? If i should go with a graph database, which one? Thank you for any advice!
5 replies
TtRPC
Created by funki on 12/26/2023 in #❓-help
How can i use inferprocedureoutput?
I'd like to write this code type-safely:
const handleQuery = (query: /* ? */) => {}

const MyComponent = () => {
const query = trpc.foo.useQuery()
handleQuery(query)
// ...
}
const handleQuery = (query: /* ? */) => {}

const MyComponent = () => {
const query = trpc.foo.useQuery()
handleQuery(query)
// ...
}
I'm pretty sure i need to use inferProcedureOutput but i couldn't figure out what to pass. I can never access query.isLoading for example.
4 replies
TtRPC
Created by funki on 11/17/2023 in #❓-help
Globally handle specific Error type on backend
Hi, i have a lot of code that's used for both backend and frontend. That's why i'm not throwing TRPCErrors but custom errors instead. They indicate that the error message should be sent to the client if it arises on the backend (on the frontend this obviously doesn't matter). Can i set up the tRPC server side in a way where it will catch all errors so i can do something like this?
if (error instanceof DispatchableError) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
} else {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Sorry, something went wrong"
})
}
if (error instanceof DispatchableError) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
} else {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Sorry, something went wrong"
})
}
7 replies
TtRPC
Created by funki on 9/12/2023 in #❓-help
404 TRPCError: no query procedure on path
Hi, this is my entire standalone tRPC server:
import { initTRPC } from "@trpc/server";
import { createHTTPServer } from "@trpc/server/adapters/standalone";
import cors from "cors";

const t = initTRPC.create();
const appRouter = t.router({
foo: t.procedure.query(() => "hi"),
});

const server = createHTTPServer({
middleware: cors(),
router: appRouter,
});

export type AppRouter = typeof appRouter;

server.listen(3000);
import { initTRPC } from "@trpc/server";
import { createHTTPServer } from "@trpc/server/adapters/standalone";
import cors from "cors";

const t = initTRPC.create();
const appRouter = t.router({
foo: t.procedure.query(() => "hi"),
});

const server = createHTTPServer({
middleware: cors(),
router: appRouter,
});

export type AppRouter = typeof appRouter;

server.listen(3000);
and here is my tRPC client:
const trpcClient = trpc.createClient({
links: [
httpBatchLink({
url: "http://localhost:3000/trpc"
}),
],
});
const trpcClient = trpc.createClient({
links: [
httpBatchLink({
url: "http://localhost:3000/trpc"
}),
],
});
but when i use it with trpc.foo.useQuery(), this the response:
{
"error": {
"message": "No \"query\"-procedure on path \"trpc/foo\"",
"code": -32004,
"data": {
"code": "NOT_FOUND",
"httpStatus": 404,
"stack": "TRPCError: No \"query\"-procedure on path \"trpc/foo\"\n at new TRPCError (/somepath/node_modules/@trpc/server/dist/TRPCError-6a1653a4.mjs:52:12)\n at callProcedure
[...]
}
}
}
{
"error": {
"message": "No \"query\"-procedure on path \"trpc/foo\"",
"code": -32004,
"data": {
"code": "NOT_FOUND",
"httpStatus": 404,
"stack": "TRPCError: No \"query\"-procedure on path \"trpc/foo\"\n at new TRPCError (/somepath/node_modules/@trpc/server/dist/TRPCError-6a1653a4.mjs:52:12)\n at callProcedure
[...]
}
}
}
What am i doing wrong? I already tried removing /trpc from the url that the client uses which also resulted in 404.
18 replies
TtRPC
Created by funki on 5/14/2023 in #❓-help
Why does this starter with Prisma have it's own postinstall script?
4 replies
TtRPC
Created by funki on 4/28/2023 in #❓-help
next-prisma-websockets-starter seeds twice on 'pnpm dx'
Hi, i'm using this starter template for my app. The dx script from package.json runs both prisma migrate as well as prisma seed and the former seems to run the seeder as well, causing data to exist twice in the database after running dx. Link to line in package.json: https://github.com/trpc/examples-next-prisma-websockets-starter/blob/db3a7794caa8d024f7115ce1f767d84c0172dd93/package.json#L21
2 replies