Using the following example (modified from the example on Cloudflare TCP Socket page
import { connect } from "@arrowood.dev/socket";
const decoder = new TextDecoder();
async function fetch(request) {
const gopherAddr = { hostname: "gopher.floodgap.com", port: 70 };
const url = new URL(request.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) {
console.log("error", error);
return new Response("Socket connection failed: " + error, { status: 500 });
}
}
const response = await fetch(new Request("gopher://gopher.floodgap.com/"));
for await (const chunk of response.body) {
// console.log(decoder.decode(chunk));
}
When run against Node.js 22.2, error occurs:
$ $ node --version
v22.2.0
$ node test.js
Warning: Detected unsettled top-level await at file:///Users/sntran/Projects/@sntran/socket/test.js:28
for await (const chunk of response.body) {
^
Works fine on Node.js 20.x and 18.x.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too