Skip to main content

A Deno Implementation of `connect()`, the TCP Socket API proposed within WinterCG, and implemented in Cloudflare Workers.

Works with
This package works with Deno
JSR Score
58%
Published
3 weeks ago

deno_socket

A Deno Implementation of the Cloudflare Socket API

Deno already has great support for TCP, but since Cloudflare proposed a WinterCG spec, this provides the same interface for Deno so we could migrate easier later on.

Inspired by @arrowood.dev/socket, but with Deno's runtime APIs.

Usage

import { connect } from "https://deno.land/x/socket/mod.js";

export default {
  async fetch(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 });
    }
  }
};

The module can also be run as a simple CLI to send a message to a TCP server, similar to the example above, but send response to stdout.

For example:

$ deno run --allow-net mod.js gopher.floodgap.com:70 /
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @sntran/socket

Import symbol

import * as mod from "@sntran/socket";