https://github.com/sntran/deno_socket
A Deno Implementation of the Cloudflare Socket API
https://github.com/sntran/deno_socket
cloudflare deno sockets wintercg workers
Last synced: 4 months ago
JSON representation
A Deno Implementation of the Cloudflare Socket API
- Host: GitHub
- URL: https://github.com/sntran/deno_socket
- Owner: sntran
- License: apache-2.0
- Created: 2023-12-22T15:18:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-29T16:20:50.000Z (about 1 year ago)
- Last Synced: 2025-01-23T13:13:58.869Z (5 months ago)
- Topics: cloudflare, deno, sockets, wintercg, workers
- Language: JavaScript
- Homepage: https://deno.land/x/socket
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deno_socket
A Deno Implementation of the Cloudflare Socket API
Deno already has great support for TCP, but since Cloudflare proposed a
[WinterCG spec](https://sockets-api.proposal.wintercg.org/), 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
```js
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:
```shell
$ deno run --allow-net mod.js gopher.floodgap.com:70 /
```