<commit_before>import * as child_process from "child_process";
import * as crypto from "crypto";
import * as fs from "fs";
import * as http from "http";
import * as path from "path";

import { OPDSFeed } from "@models/opds2/opds2";
import { Publication } from "@models/publication";
import { encodeURIComponent_RFC3986, isHTTP } from "@utils/http/UrlUtils";
import * as css2json from "css2json";
import * as debug_ from "debug";
import * as express from "express";
import * as jsonMarkup from "json-markup";
import { JSON as TAJSON } from "ta-json";
import { tmpNameSync } from "tmp";

import { serverAssets } from "./server-assets";
import { serverManifestJson } from "./server-manifestjson";
import { serverMediaOverlays } from "./server-mediaoverlays";
import { serverOPDS } from "./server-opds";
import { serverOPDS2 } from "./server-opds2";
import { serverPub } from "./server-pub";
import { serverUrl } from "./server-url";

const debug = debug_("r2:server:main");

interface IPathPublicationMap { [key: string]: any; }

// https://github.com/mafintosh/json-markup/blob/master/style.css
const jsonStyle = `
.json-markup {
    line-height: 17px;
    font-size: 13px;
    font-family: monospace;
    white-space: pre;
}
.json-markup-key {
    font-weight: bold;
}
.json-markup-bool {
    color: firebrick;
}
.json-markup-string {
    color: green;
}
.json-markup-null {
    color: gray;
}
.json-markup-number {
    color: blue;
}
`;

export class Server {

    public readonly lcpBeginToken = "*-";
    public readonly lcpEndToken = "-*";

    private readonly publications: string[];
    private publicationsOPDSfeed: OPDSFeed | undefined;
    private readonly pathPublicationMap: IPathPublicationMap;
    private creatingPublicationsOPDS: boolean;
    private readonly opdsJsonFilePath: string;

    private readonly expressApp: express.Application;
    private httpServer: http.Server;

    constructor() {
        this.publications = [];
        this.pathPublicationMap = {};
        this.publicationsOPDSfeed = undefined;
        this.creatingPublicationsOPDS = false;

        this.opdsJsonFilePath = tmpNameSync({ prefix: "readium2-OPDS2-", postfix: ".json" });

        this.expressApp = express();
        // this.expressApp.enable('strict routing');

        // https://expressjs.com/en/4x/api.html#express.static
        const staticOptions = {
            etag: false,
        };

        this.expressApp.use("/readerNYPL", express.static("misc/readers/reader-NYPL", staticOptions));
        this.expressApp.use("/readerHADRIEN", express.static("misc/readers/reader-HADRIEN", staticOptions));

        this.expressApp.get("/", (_req: express.Request, res: express.Response) => {

            let html = "<html><body><h1>Publications</h1>";

            this.publications.forEach((pub) => {
                const filePathBase64 = new Buffer(pub).toString("base64");

                html += "<p><strong>"
                    + (isHTTP(pub) ? pub : path.basename(pub))
                    + "</strong><br> => <a href='./pub/" + encodeURIComponent_RFC3986(filePathBase64)
                    + "'>" + "./pub/" + filePathBase64 + "</a></p>";
            });
            html += "<h1>Custom publication URL</h1><p><a href='./url'>CLICK HERE</a></p>";
            html += "<h1>OPDS feed</h1><p><a href='./opds'>CLICK HERE</a></p>";
            html += "<h1>Server version</h1><p><a href='./version/show'>CLICK HERE</a></p>";
            html += "</body></html>";

            res.status(200).send(html);
        });

        this.expressApp.get(["/version", "/version/show/:jsonPath?"],
            (req: express.Request, res: express.Response) => {

                const isShow = req.url.indexOf("/show") >= 0 || req.query.show;
                if (!req.params.jsonPath && req.query.show) {
                    req.params.jsonPath = req.query.show;
                }

                const gitRevJson = "../../../gitrev_.json";
                if (!fs.existsSync(path.resolve(path.join(__dirname, gitRevJson)))) {

                    const err = "Missing Git rev JSON! ";
                    debug(err + gitRevJson);
                    res.status(500).send("<html><body><p>Internal Server Error</p><p>"
                        + err + "</p></body></html>");
                    return;
                }

                const jsonObj = require(gitRevJson);
                // debug(jsonObj);

                if (isShow) {
                    const jsonPretty = jsonMarkup(jsonObj, css2json(jsonStyle));

                    res.status(200).send("<html><body>" +
                        "<h1>R2-STREAMER-JS VERSION INFO</h1>" +
                        "<hr><p><pre>" + jsonPretty + "</pre></p>" +
                        // "<hr><p><pre>" + jsonStr + "</pre></p>" +
                        // "<p><pre>" + dumpStr + "</pre></p>" +
                        "</body></html>");
                } else {
                    this.setResponseCORS(res);
                    res.set("Content-Type", "application/json; charset=utf-8");<commit_msg>forgot to rename value (after testing)<commit_after>import * as child_process from "child_process";
import * as crypto from "crypto";
import * as fs from "fs";
import * as http from "http";
import * as path from "path";

import { OPDSFeed } from "@models/opds2/opds2";
import { Publication } from "@models/publication";
import { encodeURIComponent_RFC3986, isHTTP } from "@utils/http/UrlUtils";
import * as css2json from "css2json";
import * as debug_ from "debug";
import * as express from "express";
import * as jsonMarkup from "json-markup";
import { JSON as TAJSON } from "ta-json";
import { tmpNameSync } from "tmp";

import { serverAssets } from "./server-assets";
import { serverManifestJson } from "./server-manifestjson";
import { serverMediaOverlays } from "./server-mediaoverlays";
import { serverOPDS } from "./server-opds";
import { serverOPDS2 } from "./server-opds2";
import { serverPub } from "./server-pub";
import { serverUrl } from "./server-url";

const debug = debug_("r2:server:main");

interface IPathPublicationMap { [key: string]: any; }

// https://github.com/mafintosh/json-markup/blob/master/style.css
const jsonStyle = `
.json-markup {
    line-height: 17px;
    font-size: 13px;
    font-family: monospace;
    white-space: pre;
}
.json-markup-key {
    font-weight: bold;
}
.json-markup-bool {
    color: firebrick;
}
.json-markup-string {
    color: green;
}
.json-markup-null {
    color: gray;
}
.json-markup-number {
    color: blue;
}
`;

export class Server {

    public readonly lcpBeginToken = "*-";
    public readonly lcpEndToken = "-*";

    private readonly publications: string[];
    private publicationsOPDSfeed: OPDSFeed | undefined;
    private readonly pathPublicationMap: IPathPublicationMap;
    private creatingPublicationsOPDS: boolean;
    private readonly opdsJsonFilePath: string;

    private readonly expressApp: express.Application;
    private httpServer: http.Server;

    constructor() {
        this.publications = [];
        this.pathPublicationMap = {};
        this.publicationsOPDSfeed = undefined;
        this.creatingPublicationsOPDS = false;

        this.opdsJsonFilePath = tmpNameSync({ prefix: "readium2-OPDS2-", postfix: ".json" });

        this.expressApp = express();
        // this.expressApp.enable('strict routing');

        // https://expressjs.com/en/4x/api.html#express.static
        const staticOptions = {
            etag: false,
        };

        this.expressApp.use("/readerNYPL", express.static("misc/readers/reader-NYPL", staticOptions));
        this.expressApp.use("/readerHADRIEN", express.static("misc/readers/reader-HADRIEN", staticOptions));

        this.expressApp.get("/", (_req: express.Request, res: express.Response) => {

            let html = "<html><body><h1>Publications</h1>";

            this.publications.forEach((pub) => {
                const filePathBase64 = new Buffer(pub).toString("base64");

                html += "<p><strong>"
                    + (isHTTP(pub) ? pub : path.basename(pub))
                    + "</strong><br> => <a href='./pub/" + encodeURIComponent_RFC3986(filePathBase64)
                    + "'>" + "./pub/" + filePathBase64 + "</a></p>";
            });
            html += "<h1>Custom publication URL</h1><p><a href='./url'>CLICK HERE</a></p>";
            html += "<h1>OPDS feed</h1><p><a href='./opds'>CLICK HERE</a></p>";
            html += "<h1>Server version</h1><p><a href='./version/show'>CLICK HERE</a></p>";
            html += "</body></html>";

            res.status(200).send(html);
        });

        this.expressApp.get(["/version", "/version/show/:jsonPath?"],
            (req: express.Request, res: express.Response) => {

                const isShow = req.url.indexOf("/show") >= 0 || req.query.show;
                if (!req.params.jsonPath && req.query.show) {
                    req.params.jsonPath = req.query.show;
                }

                const gitRevJson = "../../../gitrev.json";
                if (!fs.existsSync(path.resolve(path.join(__dirname, gitRevJson)))) {

                    const err = "Missing Git rev JSON! ";
                    debug(err + gitRevJson);
                    res.status(500).send("<html><body><p>Internal Server Error</p><p>"
                        + err + "</p></body></html>");
                    return;
                }

                const jsonObj = require(gitRevJson);
                // debug(jsonObj);

                if (isShow) {
                    const jsonPretty = jsonMarkup(jsonObj, css2json(jsonStyle));

                    res.status(200).send("<html><body>" +
                        "<h1>R2-STREAMER-JS VERSION INFO</h1>" +
                        "<hr><p><pre>" + jsonPretty + "</pre></p>" +
                        // "<hr><p><pre>" + jsonStr + "</pre></p>" +
                        // "<p><pre>" + dumpStr + "</pre></p>" +
                        "</body></html>");
                } else {
                    this.setResponseCORS(res);
                    res.set("Content-Type", "application/json; charset=utf-8");
{'fraction_non_alphanumeric': 0.1617298578199052, 'fraction_numerical': 0.01046603475513428, 'mean_word_length': 2.2423175416133163, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 131, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2803872', 'n_tokens_mistral': 3098, 'n_tokens_neox': 2957, 'n_words': 610}