dpogue commented on code in PR #164: URL: https://github.com/apache/cordova-browser/pull/164#discussion_r3593694135
########## lib/CordovaDevServe.js: ########## @@ -0,0 +1,220 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +const http = require('node:http'); +const https = require('node:https'); +const fs = require('node:fs'); +const { styleText } = require('node:util'); +const express = require('express'); +const compression = require('compression'); +const { default: open, apps: supportedBrowsers } = require('open'); + +// TODO: Replace with String.dedent once it's supported by our minimum Node.js version. +const dedent = require('string-dedent'); + +/** + * @typedef CordovaDevServeOptions + * @type {object} + * @property {string} ssl-cert-file - path to the certificate file + * @property {string} ssl-key-file - path to the key file + * @property {number} port + * @property {string} startUrl - URI that is appended to <scheme>://localhost:<port>/ (default = index.html) + * @property {express.Router} router - . + * @property {string[]} argv - browser arguments + */ + +/** + * @class CordovaDevServe + */ +class CordovaDevServe { + /** @type {express.Express} */ + app; + + /** @type {URL} URL to the app */ + appUrl; + + /** @type {http.Server|https.Server} */ + #server; + + /** @type {CordovaDevServeOptions} */ + #serverOpts; + + /** @type {object} */ + #projectLocationPaths; + + /** @type {string} scheme that the app will be served from */ + #scheme; + + /** @type {number} port that the app will be served from */ + #port; + + /** @type {boolean} flag to check if the host machine is darwin */ + static #isDarwin = process.platform === 'darwin'; + + /** + * + * @param {CordovaDevServeOptions} serverOpts + */ + constructor (serverOpts = {}, projectLocationPaths = {}) { + this.#serverOpts = serverOpts || {}; + this.#projectLocationPaths = projectLocationPaths || {}; + + this.#createApp(); + } + + /** + * Creates and stores the Express instance on app. + */ + #createApp () { + if (!this.#projectLocationPaths.www) { + throw new Error('Missing path to the project\'s platform www directory.'); + } + + this.app = express(); + + // Attach this before anything else to provide status output + this.app.use(this.#appStatusHandler); + this.app.use(compression()); + + if (this.#serverOpts.router) { + this.app.use(this.#serverOpts.router); + } + + if (this.#projectLocationPaths.www) { + this.app.use(express.static(this.#projectLocationPaths.www)); + } + } + + /** + * @returns {Promise} resolves to this instance or rejects with an error. + */ + async createAndLaunchServer () { + return new Promise((resolve, reject) => { + let httpsServOptions; + try { + httpsServOptions = { + cert: fs.readFileSync(this.#serverOpts['ssl-cert-file']), + key: fs.readFileSync(this.#serverOpts['ssl-key-file']) + }; + } catch (e) { + // If any one of them fails to read then we dont have what we need for HTTPS + httpsServOptions = undefined; + } + + this.#server = httpsServOptions + ? https.Server(httpsServOptions, this.app) + : http.Server(this.app); + + this.#server.listen(this.#serverOpts.port || undefined); + + this.#scheme = httpsServOptions ? 'https' : 'http'; + this.#port = this.#server?.address()?.port; + + this.#server.once('listening', () => { + this.#serverOnListening(resolve); + }); + this.#server.once('error', (e) => { + reject(e); + }); + }); + } + + // MARK: App Handlers + + #appStatusHandler (req, res, next) { + res.on('finish', function () { + const statusColor = this.statusCode === 404 ? 'red' : 'green'; + const statusCode = styleText(statusColor, this.statusCode.toString()); + + let msg = `${statusCode} ${this.req.originalUrl}`; + + const encoding = this.getHeader('content-encoding'); + if (encoding) { + msg += styleText('gray', ` (${encoding})`); + } + console.log(msg); + }); + next(); + } + + // MARK: HTTP Listeners + + /** + * Server's on listening handler + * + * @param {Promise.resolve} resolve + * @param {Promise.reject} reject + */ + #serverOnListening (resolve, reject) { + // The app url to be opened when target is not none. + this.appUrl = new URL(`${scheme}://localhost:${port}/${this.#serverOpts.startUrl}`); Review Comment: The CI check failures on this line do actually need to be resolved: ```suggestion this.appUrl = new URL(`${this.#scheme}://localhost:${this.#port}/${this.#serverOpts.startUrl}`); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
