peers/peerjs-server

View on GitHub
src/api/v1/public/index.ts

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import express from "express";
import type { IConfig } from "../../../config/index.ts";
import type { IRealm } from "../../../models/realm.ts";

export default ({
    config,
    realm,
}: {
    config: IConfig;
    realm: IRealm;
}): express.Router => {
    const app = express.Router();

    // Retrieve guaranteed random ID.
    app.get("/id", (_, res: express.Response) => {
        res.contentType("html");
        res.send(realm.generateClientId(config.generateClientId));
    });

    // Get a list of all peers for a key, enabled by the `allowDiscovery` flag.
    app.get("/peers", (_, res: express.Response) => {
        if (config.allow_discovery) {
            const clientsIds = realm.getClientsIds();

            return res.send(clientsIds);
        }

        return res.sendStatus(401);
    });

    return app;
};