Mihai Andrei
Mihai Andreiโ€ข2mo ago

Stop File stream if file is too large

Hey guys! I'm trying to do some file upload with trpc and I took the example from here: https://github.com/trpc/trpc/blob/main/examples/minimal-content-types/server/index.ts
file: publicProcedure.input(octetInputParser).mutation(async ({ input }) => {
const chunks = [];
const FILE_SIZE_LIMIT = 10 * 1024 * 1024; // 10 MB
const reader = input.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const size = Buffer.byteLength(value);

chunks.push(value);
fileSize += size;
if(fileSize > FILE_SIZE_LIMIT){
await reader.cancel('too large');
throw new TRPCError({
code: 'PAYLOAD_TOO_LARGE'
});
}
}

const content = Buffer.concat(chunks).toString('utf-8');

console.log('File: ', content);

return {
text: 'ACK',
data: content,
};
}),
file: publicProcedure.input(octetInputParser).mutation(async ({ input }) => {
const chunks = [];
const FILE_SIZE_LIMIT = 10 * 1024 * 1024; // 10 MB
const reader = input.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const size = Buffer.byteLength(value);

chunks.push(value);
fileSize += size;
if(fileSize > FILE_SIZE_LIMIT){
await reader.cancel('too large');
throw new TRPCError({
code: 'PAYLOAD_TOO_LARGE'
});
}
}

const content = Buffer.concat(chunks).toString('utf-8');

console.log('File: ', content);

return {
text: 'ACK',
data: content,
};
}),
In the console i can see if the file is too big: POST /api/trpc/upload.file 413 in 426ms So i see the 413 status. But in the browser, the request stays in pending for a while then goes into (failed)net::ERR_CONNECTION_RESET Any tips what i could look into to debug the issue?
GitHub
trpc/examples/minimal-content-types/server/index.ts at main ยท trpc...
๐Ÿง™โ€โ™€๏ธ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?