Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .replit

This file was deleted.

58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
# Buffy ⚡
A simple proxy

[![Deploy On Replit](https://shields.io/badge/Deploy%20on-replit-important?logo=replit&style=for-the-badge)](https://replit.com/github/retronbv/buffy)
## How to Use

**Installing**
```bash
npm install github:retronbv/buffy
```
(You can also use Yarn if you want)

**Importing**
```js
import Buffy from "buffy";
```

**Using**
```js
const opts = {
url: "YOUR URL HERE"
/*
A URL to proxy to. It must be a valid JavaScript URL
https://developer.mozilla.org/docs/Web/API/URL/URL
*/
validateStatus: (status) => status !== 404 // Optional, you should leave this out unless you know what you are doing
/*
`validateStatus` defines whether to call next() or send
the proxied response for a given HTTP response status code.
If `validateStatus` returns `true` (or is set to `null` or `undefined`),
the proxied reponse will be sent; otherwise, next() will be called.
*/
};
const proxy = new Buffy(opts);

app.use((req, res, next) => {
/*
proxy.request requires 3 inputs:
req: Request - The HTTP request
res: Response - The HTTP response
next: Function - A function to be called if validateStatus, well read above
*/
proxy.request(req, res, next);
})
```

**Basic Example**
```js
const proxy = new Buffy({ url: "https://example.com" });

const server = http.createServer();

server.on("request", (req, res) => {
proxy.request(req, res, () => {
res.writeHead(404);
res.end("Error");
});
});

server.listen({ port: 8080 });
```
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions example/.replit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run = ["npm", "run", "example"]
entrypoint = "example.js"

[nix]
channel = "stable-22_11"
3 changes: 3 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Buffy Example

[![Deploy Example On Replit](https://shields.io/badge/Deploy%20Example%20On-replit-important?logo=replit&style=for-the-badge)](https://replit.com/github/retronbv/buffy)
44 changes: 44 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import http from "node:http";
import connect from "connect";
import Buffy from "../index.js"; // Load Buffy
import kuler from "kuler"; // Console colors

console.log(`
██████╗ ██╗ ██╗███████╗███████╗██╗ ██╗
██╔══██╗██║ ██║██╔════╝██╔════╝╚██╗ ██╔╝
██████╔╝██║ ██║█████╗ █████╗ ╚████╔╝
██╔══██╗██║ ██║██╔══╝ ██╔══╝ ╚██╔╝
██████╔╝╚██████╔╝██║ ██║ ██║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
`);

const url = "https://3kh0.github.io";

console.log(kuler(`Proxying the domain: ${url}`, "#00ffdd"));

const proxy = new Buffy({
url: url,
validateStatus: () => true
});
const app = connect();
const server = http.createServer();
const PORT = process.env.PORT || 8080;

app.use((req, res, next) => {
proxy.request(req, res, next);
});

app.use((req, res) => {
res.writeHead(500);
res.end("Error");
});

server.on("request", app);

server.on("listening", () => {
console.log(kuler(`Server has been started! Listening on port ${PORT}`, "#00ff00"));
console.log("Link to view (Replit): " + kuler(`https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`, "#0000ff"));
});

// Here we start the server
server.listen({ port: PORT })
215 changes: 215 additions & 0 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "buffy-example",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js"
},
"contributors": [
{
"name": "retronbv",
"url": "https://retron.dev"
},
{
"name": "mdoryammilwalrus"
}
],
"license": "ISC",
"dependencies": {
"connect": "^3.7.0",
"kuler": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion replit.nix → example/replit.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ pkgs }: {
deps = [
pkgs.nodejs-16_x
pkgs.nodejs
];
}
Loading