forked from mdn/samples-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (148 loc) · 4.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require("express");
const http = require("http");
const https = require("https");
const app = express();
const fs = require("fs");
const path = require("path");
const escapeHTML = require("escape-html");
let httpPort = 80;
const htmlTop = `<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link href="/css/main.css" rel="stylesheet" media="screen" type="text/css">
<title>MDN Code Samples</title>
</head>
<body>
<div class="page-wrapper">
<div class="mdn-header">
<h1>
MDN Code Samples
</h1>
</div>
<div class="mdn-content">
<p>
This site hosts <a href="https://github.com/mdn/samples-server/"> code samples</a>
for MDN Web Docs that require server assistance to operate, such as examples for WebSocket,
WebRTC, and other APIs.
</p>
</div>
<div class="mdn-filelist">
`;
const htmlBottom = `</div>
<div class="mdn-footer">
All text content on MDN is offered under the
<a href="http://creativecommons.org/licenses/by-sa/2.5/">CC-SA-BY</a> license,
version 2.5 or later. All sample code offered on this site is provided under the
<a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0 (Public
Domain)</a> license and may be reused or repurposed without attribution.
</div>
</div>
</body>
</html>`;
app.get("/", (request, response) => {
let menuHTML = buildMenu("s");
let html = htmlTop + "\r" + menuHTML + "\r" + htmlBottom;
response.send(html);
});
app.use("/s", express.static(path.join(__dirname, "s")));
app.use("/css", express.static(path.join(__dirname, "css")));
// Try to load the key and certificate for HTTPS
let httpsOptions = {};
try {
httpsOptions.key = fs.readFileSync("/etc/pki/tls/private/mdn-samples.mozilla.org.key");
httpsOptions.cert = fs.readFileSync("/etc/pki/tls/certs/mdn-samples.mozilla.org.crt");
} catch(err) {
console.error("Unable to load HTTPS cert and/or key; available on HTTP only: " + err);
httpsOptions = null;
}
let httpServer = http.createServer(app);
httpServer.listen(httpPort);
httpServer.on("error", (err) => {
if (err.code === "EADDRINUSE") {
httpPort = 8888;
httpServer = http.createServer(app);
httpServer.listen(httpPort);
console.log("Listening on port " + httpPort);
} else {
console.error("HTTP startup error: " + err);
}
});
if (httpsOptions) {
let httpServer = https.createServer(httpsOptions, app);
httpServer.listen(443);
console.log("HTTPS listening on port 443");
}
function readJSONFile(pathname) {
const options = {
encoding: "utf8"
};
try {
let data = fs.readFileSync(pathname, options);
const obj = JSON.parse(data);
return obj;
} catch(err) {
console.error(`Error loading JSON data for file ${pathname}: ${err}`);
}
return null;
}
function buildMenuEntry(manifest) {
let {name, docsUrl, description, pathname} = manifest;
let docsLink = `[<a href="${docsUrl}">Documentation</a>]`;
let dt = `<dt><a href="${pathname}">${name}</a></dt>`;
let dd = `<dd>${escapeHTML(description)} ${docsLink}</dd>`;
output = dt+"\n"+dd+"\n";
return output;
}
function buildMenuHTML(manifestList) {
let output = "";
manifestList.forEach(entry => {
output += buildMenuEntry(entry);
});
return output;
}
function getManifestFromDirectory(pathname) {
let manifestPath = `${pathname}${path.sep}manifest.json`;
let manifest = readJSONFile(manifestPath);
return manifest;
}
function compareManifests(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
function loadAllManifests(files, pathname) {
let manifestList = [];
files.forEach(entry => {
if (entry.isDirectory()) {
const entryPath = `${pathname}${path.sep}${entry.name}`;
let manifest = getManifestFromDirectory(entryPath);
if (manifest) {
manifest.pathname = entryPath;
manifestList.push(manifest);
}
}
});
return manifestList.sort(compareManifests);
}
function buildMenu(pathname) {
let output = "";
const readdirOptions = {
encoding: "utf8",
withFileTypes: true
};
try {
let files = fs.readdirSync(pathname, readdirOptions);
let manifestList = loadAllManifests(files, pathname);
output = buildMenuHTML(manifestList);
} catch(err) {
console.error("Error reading directory: " + err);
return null;
}
output = `<dl>\n${output}</dl>\n`;
return output;
}