Skip to content

Installing

Percs edited this page Jul 11, 2024 · 4 revisions

How do I install this?

This will provide built files and the JS API to access the path of dist files.

npm install @titainiumnetwork-dev/ultraviolet

You can use this in your project via:

import express from "express";
import { createServer } from "node:http";
import { publicPath } from "ultraviolet-static";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { join } from "node:path";
import { hostname } from "node:os";
import wisp from "wisp-server-node"

const app = express();
// Load our publicPath first and prioritize it over UV.
app.use(express.static(publicPath));
// Load vendor files last.
// The vendor's uv.config.js won't conflict with our uv.config.js inside the publicPath directory.
app.use("/uv/", express.static(uvPath));
app.use("/epoxy/", express.static(epoxyPath));
app.use("/baremux/", express.static(baremuxPath));

// Error for everything else
app.use((req, res) => {
  res.status(404);
  res.sendFile(join(publicPath, "404.html"));
});

const server = createServer();

server.on("request", (req, res) => {
  res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
  res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
  app(req, res);
});
server.on("upgrade", (req, socket, head) => {
  if (req.url.endsWith("/wisp/"))
    wisp.routeRequest(req, socket, head);
  else
    socket.end();
});

let port = parseInt(process.env.PORT || "");

if (isNaN(port)) port = 8080;

server.on("listening", () => {
  const address = server.address();

  // by default we are listening on 0.0.0.0 (every interface)
  // we just need to list a few
  console.log("Listening on:");
  console.log(`\thttp://localhost:${address.port}`);
  console.log(`\thttp://${hostname()}:${address.port}`);
  console.log(
    `\thttp://${address.family === "IPv6" ? `[${address.address}]` : address.address
    }:${address.port}`
  );
});

// https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);

function shutdown() {
  console.log("SIGTERM signal received: closing HTTP server");
  server.close();
  process.exit(0);
}

server.listen({
  port,
});

This code sets up an Express web server that serves static files from a publicPath directory and the uvPath directory, which contains the Ultraviolet library. The code also serves static files from a baremuxPath directory, which contains the bare-mux library. It also sets up a Wisp server at the /wisp/ URL prefix and routes requests to the appropriate server based on the request URL.

The Express server is created using the express module, and it serves static files using the express.static middleware. The Wisp server routes upgrades to it using the wisp.routeRequest function imported from the wisp-server-node package, and it is used to handle requests to URLs that start with /wisp/. This prefix can be changed to avoid detectability and can also be run on /.

The code then sets up an HTTP server using the createServer function from the node:http module, and it listens for requests and upgrades on that server. When a request or upgrade is received, the code checks whether it should be handled by the Bare server or the Express server, and it routes the request to the appropriate server. Finally, the server is started on port 8080 if the enviroment variable PORT is not set. If the enviroment variable is set to a valid number it will attempt to listen on that port instead.

Clone this wiki locally