Esente
Esente
Explore posts from servers
CDCloudflare Developers
Created by Esente on 4/16/2024 in #workers-help
TCP Socket does not seem to work on Worker
I have created a new worker using the example on https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/#connect (also below), under https://connect.fifo.workers.dev When trying to access https://connect.fifo.workers.dev/gopher/wbgopher, it takes a long time and just returns a blank page. It seems to stuck at responding with the ReadableStream from socket. Code from the example:
import { connect } from 'cloudflare:sockets';

export default {
async fetch(req: Request) {
const gopherAddr = { hostname: "gopher.floodgap.com", port: 70 };
const url = new URL(req.url);

try {
const socket = connect(gopherAddr);

const writer = socket.writable.getWriter()
const encoder = new TextEncoder();
const encoded = encoder.encode(url.pathname + "\r\n");
await writer.write(encoded);

return new Response(socket.readable, { headers: { "Content-Type": "text/plain" } });
} catch (error) {
return new Response("Socket connection failed: " + error, { status: 500 });
}
}
};
import { connect } from 'cloudflare:sockets';

export default {
async fetch(req: Request) {
const gopherAddr = { hostname: "gopher.floodgap.com", port: 70 };
const url = new URL(req.url);

try {
const socket = connect(gopherAddr);

const writer = socket.writable.getWriter()
const encoder = new TextEncoder();
const encoded = encoder.encode(url.pathname + "\r\n");
await writer.write(encoded);

return new Response(socket.readable, { headers: { "Content-Type": "text/plain" } });
} catch (error) {
return new Response("Socket connection failed: " + error, { status: 500 });
}
}
};
5 replies
DDeno
Created by Esente on 3/20/2024 in #help
How to know when a Deno.Conn is closed
A Deno.Conn is created either with Deno.connectTls or Deno.connect. How do we detect when the connection is closed to handle appropriately? I think I can getReader from the conn's readable and use the closed promise there, but my plan is to pass along the readable so I try not to acquire a lock on that reader.
1 replies
DDeno
Created by Esente on 7/31/2023 in #help
Streaming FormData
I have Deno.serve a HTML page with <form enctype="multipart/form-data"> with a file input. I also have a handler for POST that would take that File, split it, and sends the chunks to another endpoint. What I have so far in the POST handler is to await request.formData() to retrieve the File from there and split. It works fine for small files, but for big file, the handler would wait until the whole file is sent before starting to split, which can overflow the memory. Is there a way that I can immediately stream the File from the browser's request, and split it as more data come in?
6 replies
DDeno
Created by Esente on 7/18/2023 in #help
Reuse Deno.serve for other TCP connections
Hello, As a newcommer to Deno, I wrote a client for NNTP protocol (https://github.com/sntran/deno_nntp) following the specs. In the process, I did all the reading the connection, parsing for status code and text, headers and body. As I get more acquainted with Deno, I realize that I basically re-implemented what Deno HTTP API is, of course, in a much less efficient way. I would like to ask for guideline in a way I can reuse some or most of that API to avoid the parsing. For example, we have Deno.serveHttp. If I understand it correctly, it reads the connection for HTTP message (https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages), parses into HTTP request and returns HttpConn, which yields up RequestEvent. I would like to hook on that and return custom Conn instead. NNTP connection is very similar to HTTP conn.
1 replies