diff options
author | Thomas Schmid <thsmi@users.noreply.github.com> | 2020-08-08 15:44:04 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-08 15:44:04 +0300 |
commit | 96c86b8d8b0ebe021e17f46f22d29a2df99626c6 (patch) | |
tree | 4d5c24a27c7d88389bbe93ba6944fbc01f2297f0 /tools | |
parent | f7e9833a7cdb15a341e4539fa6d554e750b966bf (diff) |
Switch to bootstrap 5 (#324)
* Pin bootstrap version to at least 5.0.0-alpha1
* Migrate from bootstrap 4 to bootstrap 5
* Update Dependencies
* Switch to ES6 Imports
* Remove last jquery pieces
* Fix template path
* Make unit testing ES6 import compatible
* Gulp mjs hack, older Thunderbird version do not load mjs from addon urls
* Fix linter errors
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Server/Server.js | 281 | ||||
-rwxr-xr-x | tools/Tests/Proxy.js | 42 |
2 files changed, 149 insertions, 174 deletions
diff --git a/tools/Server/Server.js b/tools/Server/Server.js index f6bea087..27f580b6 100644 --- a/tools/Server/Server.js +++ b/tools/Server/Server.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ /* * The contents of this file are licensed. You may obtain a copy of * the license at https://github.com/thsmi/sieve/ or request it via @@ -12,181 +13,197 @@ // https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework -(function () { +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const util = require('util'); - "use strict"; - const http = require('http'); - const fs = require('fs'); - const path = require('path'); - const util = require('util'); +const SERVER_PORT = 8125; +const GUI_URL = "gui/"; +const GUI_PATH = "./build/electron/resources/libs"; - const SERVER_PORT = 8125; +const TEST_URL = "test/"; +const TEST_PATH = "./build/test"; - const GUI_URL = "gui/"; - const GUI_PATH = "./build/electron/resources/libs"; +const HTTP_SUCCESS = 200; +const HTTP_FILE_NOT_FOUND = 404; +const HTTP_INTERNAL_ERROR = 500; - const TEST_URL = "test/"; - const TEST_PATH = "./build/test"; +const CONTENT_TYPE_HTML = "text/html"; - const HTTP_SUCCESS = 200; - const HTTP_FILE_NOT_FOUND = 404; - const HTTP_INTERNAL_ERROR = 500; - - const CONTENT_TYPE_HTML = "text/html"; +/** + * Guesses the mime type by the file extensions + * + * @param {string} filePath + * the file name which should be analyzed. + * + * @returns {string} + * the mime type. In case it is unknown application/octet-stream is returned + */ +function getContentType(filePath) { + const extname = path.extname(filePath); + + switch (extname) { + case '.js': + case '.mjs': + case '.cjs': + return 'text/javascript'; + case '.css': + return 'text/css'; + case '.json': + return 'application/json'; + case '.png': + return 'image/png'; + case '.jpg': + return 'image/jpg'; + case ".htm": + case ".html": + return "text/html"; + } - /** - * - * @param {*} filePath - */ - function getContentType(filePath) { - const extname = path.extname(filePath); + console.warn("Unknown mime type for " + filePath); + return 'application/octet-stream'; +} - switch (extname) { - case '.js': - return 'text/javascript'; - case '.css': - return 'text/css'; - case '.json': - return 'application/json'; - case '.png': - return 'image/png'; - case '.jpg': - return 'image/jpg'; - } +/** + * Compares the given path elements. + * + * A directory always wins the comparison. + * Otherwise in case two directories or two + * files are compared alphabetically. + * + * @param {*} a + * the first path + * @param {*} b + * the second path. + * + * @returns {int} + * the comparisons result. + */ +function sortDirectory(a, b) { - return ''; - } + if (a.isDirectory() && b.isDirectory()) + return a.name.localeCompare(b.name); - /** - * Compares the given path elements. - * - * A directory always wins the comparison. - * Otherwise in case two directories or two - * files are compared alphabetically. - * - * @param {*} a - * the first path - * @param {*} b - * the second path. - * - * @returns {int} - * the comparisons result. - */ - function sortDirectory(a, b) { - - if (a.isDirectory() && b.isDirectory()) - return a.name.localeCompare(b.name); - - if (a.isDirectory()) - return -1; - - if (b.isDirectory()) - return 1; + if (a.isDirectory()) + return -1; - return a.name.localeCompare(b.name); - } + if (b.isDirectory()) + return 1; - async function doDirectoryListing(filePath, url, response) { - const items = await(util.promisify(fs.readdir))(filePath, { withFileTypes: true }); + return a.name.localeCompare(b.name); +} - let directory = url.pathname; +/** + * + * @param {*} filePath + * @param {*} url + * @param {*} response + */ +async function doDirectoryListing(filePath, url, response) { + const items = await (util.promisify(fs.readdir))(filePath, { withFileTypes: true }); - if (!directory.endsWith("/")) - directory += "/"; + let directory = url.pathname; - response.writeHead(HTTP_SUCCESS, { 'Content-Type': CONTENT_TYPE_HTML }); + if (!directory.endsWith("/")) + directory += "/"; - let content = ""; - content += `<h1> Index of ${directory}</h1>`; + response.writeHead(HTTP_SUCCESS, { 'Content-Type': CONTENT_TYPE_HTML }); - items.sort(sortDirectory); + let content = ""; + content += `<h1> Index of ${directory}</h1>`; - content += `<div><a href="${directory}../">⮠ ..</a></div>`; + items.sort(sortDirectory); - for (const item of items) { - if (item.isDirectory()) - content += `<div><a href="${directory}${item.name}">🗀 ${item.name}/</a></div>`; - else - content += `<div><a href="${directory}${item.name}">🗎 ${item.name}</a></div>`; - } + content += `<div><a href="${directory}../">⮠ ..</a></div>`; - response.end(content, 'utf-8'); + for (const item of items) { + if (item.isDirectory()) + content += `<div><a href="${directory}${item.name}">🗀 ${item.name}/</a></div>`; + else + content += `<div><a href="${directory}${item.name}">🗎 ${item.name}</a></div>`; } - function doIndex(response) { + response.end(content, 'utf-8'); +} - response.writeHead(HTTP_FILE_NOT_FOUND, { 'Content-Type': CONTENT_TYPE_HTML }); +/** + * + * @param {*} response + */ +function doIndex(response) { - let content = ""; + response.writeHead(HTTP_FILE_NOT_FOUND, { 'Content-Type': CONTENT_TYPE_HTML }); - content += "<h1>Welcome to the debug server</h1>"; - content += "<p>This server is used to bypass cross site scripting problems during development.</p>"; - content += "<ul>"; - content += `<li><a href="http://127.0.0.1:${SERVER_PORT}/${GUI_URL}libSieve/SieveGui.html">🗀 Run GUI Editor</a></li>`; - content += `<li><a href="http://127.0.0.1:${SERVER_PORT}/${TEST_URL}index.html">🗀 Run Unit Tests</a></li>`; - content += "</ul>"; - response.end(content, 'utf-8'); - return; - } + let content = ""; - http.createServer(async function (request, response) { + content += "<h1>Welcome to the debug server</h1>"; + content += "<p>This server is used to bypass cross site scripting problems during development.</p>"; + content += "<ul>"; + content += `<li><a href="http://127.0.0.1:${SERVER_PORT}/${GUI_URL}libSieve/SieveGui.html">🗀 Run GUI Editor</a></li>`; + content += `<li><a href="http://127.0.0.1:${SERVER_PORT}/${TEST_URL}index.html">🗀 Run Unit Tests</a></li>`; + content += "</ul>"; + response.end(content, 'utf-8'); + return; +} - let filePath = ""; +http.createServer(async function (request, response) { - const url = new URL(request.url, `http://${request.headers.host}`); + let filePath = ""; - if (url.pathname.startsWith(`/${GUI_URL}`)) { - filePath = GUI_PATH + url.pathname.substr(GUI_URL.length); - } else if (url.pathname.startsWith(`/${TEST_URL}`)) { - filePath = TEST_PATH + url.pathname.substr(TEST_URL.length); - } else { - doIndex(response); - return; - } - - console.log('Request for ' + filePath); + const url = new URL(request.url, `http://${request.headers.host}`); + if (url.pathname.startsWith(`/${GUI_URL}`)) { + filePath = GUI_PATH + url.pathname.substr(GUI_URL.length); + } else if (url.pathname.startsWith(`/${TEST_URL}`)) { + filePath = TEST_PATH + url.pathname.substr(TEST_URL.length); + } else { + doIndex(response); + return; + } - try { + console.log('Request for ' + filePath); - if (!fs.existsSync(filePath)) { - response.writeHead(HTTP_FILE_NOT_FOUND); - response.end(`File not found (404) ${filePath}\n`); - response.end(); - return; - } + try { - const stat = await (util.promisify(fs.lstat))(filePath); + if (!fs.existsSync(filePath)) { + response.writeHead(HTTP_FILE_NOT_FOUND); + response.end(`File not found (404) ${filePath}\n`); + response.end(); - if (stat.isDirectory()) { - await doDirectoryListing(filePath, url, response); - return; - } + return; + } - if (stat.isFile()) { + const stat = await (util.promisify(fs.lstat))(filePath); - const content = await fs.promises.readFile(filePath); - response.writeHead(HTTP_SUCCESS, { 'Content-Type': getContentType(filePath) }); - response.end(content, 'utf-8'); + if (stat.isDirectory()) { + await doDirectoryListing(filePath, url, response); + return; + } - return; - } + if (stat.isFile()) { - console.log("Unsupported file type for" + filePath); + const content = await fs.promises.readFile(filePath); + response.writeHead(HTTP_SUCCESS, { 'Content-Type': getContentType(filePath) }); + response.end(content, 'utf-8'); - } catch (ex) { - console.log(ex); + return; } - response.writeHead(HTTP_INTERNAL_ERROR); - response.end('Internal server error ' + filePath + ' ...\n'); - response.end(); + console.log("Unsupported file type for" + filePath); + + } catch (ex) { + console.log(ex); + } + + response.writeHead(HTTP_INTERNAL_ERROR); + response.end('Internal server error ' + filePath + ' ...\n'); + response.end(); - }).listen(SERVER_PORT); - console.log(`Server running at http://127.0.0.1:${SERVER_PORT}/${GUI_URL}libSieve/SieveGui.html or http://127.0.0.1:${SERVER_PORT}/${TEST_URL}index.html`); +}).listen(SERVER_PORT); -})(); +console.log(`Server running at http://127.0.0.1:${SERVER_PORT}/${GUI_URL}libSieve/SieveGui.html or http://127.0.0.1:${SERVER_PORT}/${TEST_URL}index.html`); diff --git a/tools/Tests/Proxy.js b/tools/Tests/Proxy.js deleted file mode 100755 index ce1a2b87..00000000 --- a/tools/Tests/Proxy.js +++ /dev/null @@ -1,42 +0,0 @@ -/* global Components */ -/* global print */ - -"use strict"; - -var ios = Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService); -var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"] - .getService(Components.interfaces.nsIProtocolProxyService); - - - - var pi = null; - var uri = null; - - uri = ios.newURI("http://www.mozilla.org/", null, null); - pi = pps.resolve(uri, 0); - print("HTTP Proxy:"+pi); - print(pi.type+" | "+pi.host+" | "+pi.port); - - - uri = ios.newURI("x-sieve://sieve.mozdev.org", null, null); - - pi = pps.resolve(uri, 0); - print("X-Sieve Proxy:"+pi); - print(pi.type+" | "+pi.host+" | "+pi.port); - - uri = ios.newURI("sieve://sieve.mozdev.org", null, null); - pi = pps.resolve(uri, 0); - print("Sieve Proxy:"+pi); - //print(ios.getProtocolHandler("x-sieve")); - //print("X-SIEVE Flags"+ios.getProtocolFlags("x-sieve")); - //print("HTTP Flags"+ios.getProtocolFlags("http")); - - //uri = ios.getProtocolHandler("x-sieve").newURI("x-sieve://mozilla.org",null,null); - //pi = pps.resolve(uri, 0) - //print("Sieve Proxy 2:"+pi); - - print("HTTP Scheme:"+ios.getProtocolHandler("http").scheme); - print("Sieve Scheme:"+ios.getProtocolHandler("x-sieve").scheme); - - //print(Components.classes["@mozilla.org/network/protocol;1?name=x-sieve"].createInstance(Components.interfaces.nsIProtocolHandler));
\ No newline at end of file |