Rammerhead Package
- Easily consume and use Rammerhead server side
CLI
- Fires up a standalone server for Rammerhead
npx @rubynetwork/rammerhead
Flags:
-ho, --host <host>
- Listen on a specific host (default: 0.0.0.0
)-p, --port <port>
- The port to listen on (default: 8080
)-l, --loglevel <loglevel>
- Change the log level (default: debug
) [options: disabled
, debug
, traffic
, info
, warn
, error
]-rp, --reverseproxy
- Whether or not the server is behind a reverse proxy. (default: false
)-h, --help
- Show this same type of info
Usage:
- Basic server (import required items):
import { createRammerhead, shouldRouteRh, routeRhUpgrade, routeRhRequest } from '@rubynetwork/rammerhead';
const rh = createRammerhead({
logLevel: 'debug',
reverseProxy: false,
disableLocalStorageSync: false,
disableHttp2: false
})
const server = http.createServer();
server.on('request', (req, res) => {
if (shouldRouteRh(req)) {
routeRhRequest(rh , req, res)
}
})
server.on('upgrade', (req, socket, head) => {
if (shouldRouteRh(req)) {
routeRhUpgrade(rh , req, socket, head)
}
})
server.listen({host: '0.0.0.0', port: 8080}, () => {
console.log('Server is listening on port 8080!');
})
- Basic server (default export):
import rammerhead from '@rubynetwork/rammerhead';
const rh = rammerhead.createRammerhead({
logLevel: 'debug',
reverseProxy: false,
disableLocalStorageSync: false,
disableHttp2: false
})
const server = http.createServer();
server.on('request', (req, res) => {
if (rammerhead.shouldRouteRh(req)) {
rammerhead.routeRhRequest(rh , req, res)
}
})
server.on('upgrade', (req, socket, head) => {
if (rammerhead.shouldRouteRh(req)) {
rammerhead.routeRhUpgrade(rh , req, socket, head)
}
})
server.listen({host: '0.0.0.0', port: 8080}, () => {
console.log('Server is listening on port 8080!');
})
import express from "express";
import { createServer } from "node:http";
import { createRammerhead, shouldRouteRh, routeRhUpgrade, routeRhRequest } from '@rubynetwork/rammerhead';
const app = express();
const server = createServer();
const rh = rammerhead.createRammerhead({
logLevel: 'debug',
reverseProxy: false,
disableLocalStorageSync: false,
disableHttp2: false
})
server.on("request", (req, res) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
if (shouldRouteRh(req)) {
routeRhRequest(rh , req, res)
}
else {
app(req, res);
}
});
server.on("upgrade", (req, socket, head) => {
if (rammerhead.shouldRouteRh(req)) {
rammerhead.routeRhUpgrade(rh , req, socket, head)
}
else {
socket.end()
}
});
server.on("listening", () => {
console.log("Listening on: http://localhost:8080");
});
server.listen({port: 8080});
import Fastify from 'fastify';
import { createServer } from 'node:http';
import { createRammerhead, shouldRouteRh, routeRhUpgrade, routeRhRequest } from '@rubynetwork/rammerhead';
const rh = rammerhead.createRammerhead({
logLevel: 'debug',
reverseProxy: false,
disableLocalStorageSync: false,
disableHttp2: false
})
const serverFactory = (handler) => {
return createServer()
.on('request', (req, res) => {
if (shouldRouteRh(req)) {
routeRhRequest(rh , req, res)
}
else {
handler(req, res);
}
})
.on('upgrade', (req, socket, head) => {
if (shouldRouteRh(req)) {
routeRhUpgrade(rh , req, socket, head)
}
});
};
const app = Fastify({ logger: false, serverFactory: serverFactory });
app.listen({ port: 8080, host: '0.0.0.0' });
console.log('Server is listening on: http://localhost:8080.');