Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nora-soderlund/cloudflare-tcp-fetcher
A Fetcher implementation using the Cloudflare TCP Sockets API.
https://github.com/nora-soderlund/cloudflare-tcp-fetcher
Last synced: 21 days ago
JSON representation
A Fetcher implementation using the Cloudflare TCP Sockets API.
- Host: GitHub
- URL: https://github.com/nora-soderlund/cloudflare-tcp-fetcher
- Owner: nora-soderlund
- Created: 2024-02-18T18:05:14.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-19T07:29:45.000Z (9 months ago)
- Last Synced: 2024-02-19T19:41:57.750Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# cloudflare-tcp-fetcher
A Fetcher implementation using the Cloudflare TCP Sockets API.
## Getting started
### Install the package
```cmd
npm install cloudflare-tcp-fetcher
```### Using the fetchUsingTcp function
You can also input the TcpFetcher in the `init` object to reuse a connection.```ts
import { fetchUsingTcp } from "cloudflare-tcp-fetcher";export default {
async fetch(request: Request, env: never, ctx: ExecutionContext): Promise {
return fetchUsingTcp("https://google.com", {
method: "GET"
});
}
}
```### Using the TcpFetcher class
You can re-use the TcpFetcher class until the worker is closed.> Note: you can not use the `TcpFetcher` instance with `fetch` because the worker will complain that `TcpFetcher` is not an instance of `Fetcher` :/
```ts
import { TcpFetcher } from "cloudflare-tcp-fetcher";export default {
async fetch(request: Request, env: never, ctx: ExecutionContext): Promise {
const fetcher = new TcpFetcher();return fetcher.fetch(new Request("https://google.com", {
method: "GET"
}));
}
}
```## Examples
### Using the response body
```ts
import { fetchUsingTcp } from "cloudflare-tcp-fetcher";export default {
async fetch(request: Request, env: never, ctx: ExecutionContext): Promise {
const response = await fetchUsingTcp("https://httpbin.org/delete", {
method: "DELETE"
});return Response.json({
result: await response.json()
});
}
}
```