Skip to content

Commit

Permalink
Modernize the remainder of the webserver's code and enable the `no-va…
Browse files Browse the repository at this point in the history
…r` ESLint rule
  • Loading branch information
timvandermeij committed Feb 17, 2024
1 parent 58c5ee4 commit f4d3b18
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions test/webserver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-var */

import fs from "fs";
import fsPromises from "fs/promises";
import http from "http";
import path from "path";

var mimeTypes = {
const MIME_TYPES = {
".css": "text/css",
".html": "text/html",
".js": "application/javascript",
Expand All @@ -36,8 +35,7 @@ var mimeTypes = {
".bcmap": "application/octet-stream",
".ftl": "text/plain",
};

var defaultMimeType = "application/octet-stream";
const DEFAULT_MIME_TYPE = "application/octet-stream";

class WebServer {
constructor() {
Expand All @@ -58,9 +56,7 @@ class WebServer {
this.#ensureNonZeroPort();
this.server = http.createServer(this.#handler.bind(this));
this.server.listen(this.port, this.host, callback);
console.log(
"Server running at http://" + this.host + ":" + this.port + "/"
);
console.log(`Server running at http://${this.host}:${this.port}/`);
}

stop(callback) {
Expand All @@ -71,11 +67,11 @@ class WebServer {
#ensureNonZeroPort() {
if (!this.port) {
// If port is 0, a random port will be chosen instead. Do not set a host
// name to make sure that the port is synchronously set by .listen().
var server = http.createServer().listen(0);
var address = server.address();
// .address().port being available synchronously is merely an
// implementation detail. So we are defensive here and fall back to some
// name to make sure that the port is synchronously set by `.listen()`.
const server = http.createServer().listen(0);
const address = server.address();
// `.address().port` being available synchronously is merely an
// implementation detail, so we are defensive here and fall back to a
// fixed port when the address is not available yet.
this.port = address ? address.port : 8000;
server.close();
Expand Down Expand Up @@ -301,7 +297,7 @@ class WebServer {
});

const extension = path.extname(filePath).toLowerCase();
const contentType = mimeTypes[extension] || defaultMimeType;
const contentType = MIME_TYPES[extension] || DEFAULT_MIME_TYPE;

if (!this.disableRangeRequests) {
response.setHeader("Accept-Ranges", "bytes");
Expand Down Expand Up @@ -329,7 +325,7 @@ class WebServer {
});

const extension = path.extname(filePath).toLowerCase();
const contentType = mimeTypes[extension] || defaultMimeType;
const contentType = MIME_TYPES[extension] || DEFAULT_MIME_TYPE;

response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Type", contentType);
Expand All @@ -347,13 +343,13 @@ class WebServer {
// It is here instead of test.js so that when the test will still complete as
// expected if the user does "gulp server" and then visits
// http://localhost:8888/test/unit/unit_test.html?spec=Cross-origin
function crossOriginHandler(req, res) {
if (req.url === "/test/pdfs/basicapi.pdf?cors=withCredentials") {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
function crossOriginHandler(request, response) {
if (request.url === "/test/pdfs/basicapi.pdf?cors=withCredentials") {
response.setHeader("Access-Control-Allow-Origin", request.headers.origin);
response.setHeader("Access-Control-Allow-Credentials", "true");
}
if (req.url === "/test/pdfs/basicapi.pdf?cors=withoutCredentials") {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
if (request.url === "/test/pdfs/basicapi.pdf?cors=withoutCredentials") {
response.setHeader("Access-Control-Allow-Origin", request.headers.origin);
}
}

Expand Down

0 comments on commit f4d3b18

Please sign in to comment.