Guilherme C. Souza
Guilherme C. Souza
DDeno
Created by Guilherme C. Souza on 9/2/2023 in #help
How to make a function whose return type is inferred to be the same as an input function's
This is what I got so far.
type Context = {
abc: string,
def: boolean
}

/* HAVE TO TWEAK THESE */
type Handler = (ctx: Context, ...args: unknown[]) => unknown;

function def<T extends Handler>(fn: T): T{
// TODO
}
/* HAVE TO TWEAK THESE */

const fn = def(async function finalFunc({ abc }, a: string, b: number){
// await new Promise(done => setTimeout(done, 2000));
return 754;
})
type Context = {
abc: string,
def: boolean
}

/* HAVE TO TWEAK THESE */
type Handler = (ctx: Context, ...args: unknown[]) => unknown;

function def<T extends Handler>(fn: T): T{
// TODO
}
/* HAVE TO TWEAK THESE */

const fn = def(async function finalFunc({ abc }, a: string, b: number){
// await new Promise(done => setTimeout(done, 2000));
return 754;
})
I'm not sure if it's possible, but I need the following: - fn type must be the exact same as finalFunc's (parameters and return) plus the first parameter being of type Context - abc parameter must have it's type properly inferred from Context when writing finalFunc Am I missing something?
2 replies
DDeno
Created by Guilherme C. Souza on 5/31/2023 in #help
How to read Chunked request body?
Hello! Is it possible to read a chunked request body using for await? It seems to only read up to the point where the reader is empty and then stop. Since some chunks might only show up after some delay, I need it to read until 'zero-length chunk' is sent, meaning the chunked body is over.
4 replies
DDeno
Created by Guilherme C. Souza on 10/22/2022 in #help
Am I closing this server wrongly? The port is not free for a new listener! (`AddrInUse`)
const server = Deno.listen({ port: 80 });

(async function(){

for await(const conn of server){

(async function(){

const httpConn = Deno.serveHttp(conn);

for await(const reqEvent of httpConn){

reqEvent.respondWith(new Response('res.stream', {
status: 200
}));

}

})();

}

})();

const res = await fetch('http://localhost');
res.body?.cancel()
server.close();

// Port 80 is still in use here??

const newServer = Deno.listen({ port: 80 });
newServer.close();
const server = Deno.listen({ port: 80 });

(async function(){

for await(const conn of server){

(async function(){

const httpConn = Deno.serveHttp(conn);

for await(const reqEvent of httpConn){

reqEvent.respondWith(new Response('res.stream', {
status: 200
}));

}

})();

}

})();

const res = await fetch('http://localhost');
res.body?.cancel()
server.close();

// Port 80 is still in use here??

const newServer = Deno.listen({ port: 80 });
newServer.close();
7 replies