You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by bi...@apache.org on 2018/02/15 03:22:53 UTC
[royale-asjs] branch feature/royale-cli updated: Remove unnecessary
code
This is an automated email from the ASF dual-hosted git repository.
bigosmallm pushed a commit to branch feature/royale-cli
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
The following commit(s) were added to refs/heads/feature/royale-cli by this push:
new b99840a Remove unnecessary code
b99840a is described below
commit b99840a510d9b6ef7a09b54a48267ddc7d4ef0aa
Author: Om Muppirala <om...@mz.com>
AuthorDate: Wed Feb 14 19:22:42 2018 -0800
Remove unnecessary code
---
npm/cli/template/reload/reload-client.js | 102 ---------------------
npm/cli/template/reload/reload-server.js | 79 ----------------
npm/cli/template/reload/reload.js | 153 -------------------------------
3 files changed, 334 deletions(-)
diff --git a/npm/cli/template/reload/reload-client.js b/npm/cli/template/reload/reload-client.js
deleted file mode 100644
index 348c8c6..0000000
--- a/npm/cli/template/reload/reload-client.js
+++ /dev/null
@@ -1,102 +0,0 @@
-(function refresh () {
- var verboseLogging = false
- var socketUrl = window.location.origin
- if (!window.location.origin.match(/:[0-9]+/)) {
- socketUrl = window.location.origin + ':80'
- }
- socketUrl = socketUrl.replace() // This is dynamically populated by the reload.js file before it is sent to the browser
- var socket
-
- if (verboseLogging) {
- console.log('Reload Script Loaded')
- }
-
- if (!('WebSocket' in window)) {
- throw new Error('Reload only works with browsers that support WebSockets')
- }
-
- // Explanation of the flags below:
-
- // The first change flag is used to tell reload to wait until the socket closes at least once before we allow the page to open on a socket open event. Otherwise reload will go into a inifite loop, as the page will have a socket on open event once it loads for the first time
- var firstChangeFlag = false
-
- // The navigatedAwayFromPageFlag is set to true in the event handler onbeforeunload because we want to short-circuit reload to prevent it from causing the page to reload before the navigation occurs.
- var navigatedAwayFromPageFlag
-
- // Wait until the page loads for the first time and then call the webSocketWaiter function so that we can connect the socket for the first time
- window.addEventListener('load', function () {
- if (verboseLogging === true) {
- console.log('Page Loaded - Calling webSocketWaiter')
- }
- websocketWaiter()
- })
-
- // If the user navigates away from the page, we want to short-circuit reload to prevent it from causing the page to reload before the navigation occurs.
- window.addEventListener('beforeunload', function () {
- if (verboseLogging === true) {
- console.log('Navigated away from the current URL')
- }
-
- navigatedAwayFromPageFlag = true
- })
-
- // Check to see if the server sent us reload (meaning a manually reload event was fired) and then reloads the page
- var socketOnMessage = function (msg) {
- if (msg.data === 'reload') {
- socket.close()
- }
- }
-
- var socketOnOpen = function (msg) {
- if (verboseLogging) {
- console.log('Socket Opened')
- }
-
- // We only allow the reload on two conditions, one when the socket closed (firstChange === true) and two if we didn't navigate to a new page (navigatedAwayFromPageFlag === false)
- if (firstChangeFlag === true && navigatedAwayFromPageFlag !== true) {
- if (verboseLogging) {
- console.log('Reloaded')
- }
-
- // Reset the firstChangeFlag to false so that when the socket on open events are being fired it won't keep reloading the page
- firstChangeFlag = false
-
- // Now that everything is set up properly we reload the page
- window.location.reload()
- }
- }
-
- // Socket on close event that sets flags and calls the webSocketWaiter function
- var socketOnClose = function (msg) {
- if (verboseLogging) {
- console.log('Socket Closed - Calling webSocketWaiter')
- }
-
- // We encountered a change so we set firstChangeFlag to true so that as soon as the server comes back up and the socket opens we can allow the reload
- firstChangeFlag = true
-
- // Call the webSocketWaiter function so that we can open a new socket and set the event handlers
- websocketWaiter()
- }
-
- var socketOnError = function (msg) {
- if (verboseLogging) {
- console.log(msg)
- }
- }
-
- // Function that opens a new socket and sets the event handlers for the socket
- function websocketWaiter () {
- if (verboseLogging) {
- console.log('Waiting for socket')
- }
- setTimeout(function () {
- socket = new WebSocket(socketUrl) // eslint-disable-line
-
- socket.onopen = socketOnOpen
- socket.onclose = socketOnClose
- socket.onmessage = socketOnMessage
- socket.onerror = socketOnError
- }, 250)
- }
-})()
diff --git a/npm/cli/template/reload/reload-server.js b/npm/cli/template/reload/reload-server.js
deleted file mode 100644
index c855b60..0000000
--- a/npm/cli/template/reload/reload-server.js
+++ /dev/null
@@ -1,79 +0,0 @@
-var http = require('http')
-var reload = require('../lib/reload')
-var fs = require('fs')
-var open = require('open')
-var clc = require('cli-color')
-var argv = require('minimist')(process.argv.slice(2))
-
-var serveStatic = require('serve-static')
-var finalhandler = require('finalhandler')
-var URL = require('url-parse')
-
-var port = argv._[0]
-var dir = argv._[1]
-var openBrowser = (argv._[2] === 'true')
-var hostname = argv._[3]
-var runFile = argv._[4]
-var startPage = argv._[5]
-var verbose = (argv._[6] === 'true')
-
-var reloadOpts = {
- port: port,
- verbose: verbose,
- noExpress: true
-}
-
-var time
-var reloadReturned
-
-var serve = serveStatic(dir, {'index': ['index.html', 'index.htm']})
-
-var server = http.createServer(function (req, res) {
- var url = new URL(req.url)
- var pathname = url.pathname.replace(/(\/)(.*)/, '$2') // Strip leading `/` so we can find files on file system
-
- var fileEnding = pathname.split('.')[1]
-
- if (fileEnding === 'html' || pathname === '/' || pathname === '') { // Server side inject reload code to html files
- if (pathname === '/' || pathname === '') {
- pathname = dir + '/' + startPage
- } else {
- pathname = dir + '/' + pathname
- }
-
- fs.readFile(pathname, 'utf8', function (err, contents) {
- if (err) {
- res.writeHead(404, {'Content-Type': 'text/plain'})
- res.end('File Not Found')
- } else {
- contents += '\n\n<!-- Inserted by Reload -->\n<script src="/reload/reload.js"></script>\n<!-- End Reload -->\n'
-
- res.setHeader('Content-Type', 'text/html')
- res.end(contents)
- }
- })
- } else if (pathname === 'reload/reload.js') { // Server reload-client.js file from injected script tag
- res.setHeader('Content-Type', 'text/javascript')
-
- res.end(reloadReturned.reloadClientCode())
- } else { // Serve any other file using serve-static
- serve(req, res, finalhandler(req, res))
- }
-})
-
-// Reload call and configurations. Stub app as it isn't used here
-reloadReturned = reload(function () {}, reloadOpts, server)
-
-server.listen(port, function () {
- if (!fs.existsSync(runFile)) {
- fs.writeFileSync(runFile)
-
- // If openBrowser, open the browser with the given start page above, at a hostname (localhost default or specified).
- if (openBrowser) {
- open('http://' + hostname + ':' + port)
- }
- } else {
- time = new Date()
- console.log(clc.green('Server restarted at ' + time.toTimeString().slice(0, 8)))
- }
-})
diff --git a/npm/cli/template/reload/reload.js b/npm/cli/template/reload/reload.js
deleted file mode 100644
index cbb43c4..0000000
--- a/npm/cli/template/reload/reload.js
+++ /dev/null
@@ -1,153 +0,0 @@
-module.exports = function reload (app, opts, server) {
- // Requires
- var path = require('path')
- var fs = require('fs')
-
- // Parameters variables
- var httpServerOrPort
- var expressApp
- var verboseLogging
- var port
- var webSocketServerWaitStart
-
- // Application variables
- var RELOAD_FILE = path.join(__dirname, './reload-client.js')
- var reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8')
- var route
-
- // Websocket server variables
- var ws = require('ws')
- var WebSocketServer = ws.Server
- var wss
-
- // General variables
- var socketPortSpecified
- var argumentZero = arguments[0]
- var reloadJsMatch
- var reloadReturn
-
- opts = opts || {}
-
- if (arguments.length > 0 && (typeof (argumentZero) === 'object' || typeof (argumentZero) === 'function')) {
- if (typeof (argumentZero) === 'object') { // If old arguments passed handle old arguments, the old arguments and their order were: httpServerOrPort, expressApp, and verboseLogging
- console.warn('Deprecated Warning: You supplied reload old arguments, please upgrade to the new parameters see: https://github.com/alallier/reload/tree/master#api-for-express')
- if (arguments.length < 2) {
- throw new TypeError('Lack of/invalid arguments provided to reload. It is recommended to update to the new arguments anyways, this would be a good time to do so.', 'reload.js', 7)
- }
- httpServerOrPort = argumentZero
- expressApp = arguments[1]
- verboseLogging = arguments[2]
- route = '/reload/reload.js'
- } else { // Setup options or use defaults
- expressApp = argumentZero
- port = opts.port || 9856
- webSocketServerWaitStart = opts.webSocketServerWaitStart
- route = opts.route
-
- if (route) {
- // If reload.js is found in the route option strip it. We will concat it for user to ensure no case errors or order problems.
- reloadJsMatch = route.match(/reload\.js/i)
- if (reloadJsMatch) {
- route = route.split(reloadJsMatch)[0]
- }
-
- /*
- * Concat their provided path (minus `reload.js` if they specified it) with a `/` if they didn't provide one and `reload.js. This allows for us to ensure case, order, and use of `/` is correct
- * For example these route's are all valid:
- * 1. `newRoutePath` -> Their route + `/` + reload.js
- * 2. `newRoutePath/` -> Their route + reload.js
- * 3. `newRoutePath/reload.js` -> (Strip reload.js above) so now: Their route + reload.js
- * 4. `newRoutePath/rEload.js` -> (Strip reload.js above) so now: Their route + reload.js
- * 5. `newRoutePathreload.js` -> (Strip reload.js above) so now: Their route + `/` + reload.js
- * 6. `newRoutePath/reload.js/rEload.js/... reload.js n number of times -> (Strip above removes all reload.js occurrences at the end of the specified route) so now: Their route + 'reload.js`
- */
- route = route + (route.slice(-1) === '/' ? '' : '/') + 'reload.js'
- } else {
- route = '/reload/reload.js'
- }
-
- verboseLogging = opts.verbose === true || opts.verbose === 'true' || false
-
- if (port) {
- socketPortSpecified = port
- httpServerOrPort = port
- }
-
- if (server) {
- socketPortSpecified = null
- httpServerOrPort = server
- }
- }
- } else {
- throw new TypeError('Lack of/invalid arguments provided to reload', 'reload.js', 7)
- }
-
- // Application setup
- if (verboseLogging) {
- reloadCode = reloadCode.replace('verboseLogging = false', 'verboseLogging = true')
- }
- reloadCode = reloadCode.replace('socketUrl.replace()', 'socketUrl.replace(/(^http(s?):\\/\\/)(.*:)(.*)/,' + (socketPortSpecified ? '\'ws$2://$3' + socketPortSpecified : '\'ws$2://$3$4') + '\')')
-
- if (!server) {
- expressApp.get(route, function (req, res) {
- res.type('text/javascript')
- res.send(reloadCode)
- })
- }
-
- if (!webSocketServerWaitStart) {
- startWebSocketServer()
- }
-
- // Websocket server setup
- function startWebSocketServer () {
- if (verboseLogging) {
- console.log('Starting WebSocket Server')
- }
-
- if (socketPortSpecified) { // Use custom user specified port
- wss = new WebSocketServer({ port: httpServerOrPort })
- } else { // Attach to server, using server's port. Kept here to support legacy arguments.
- wss = new WebSocketServer({ server: httpServerOrPort })
- }
-
- wss.on('connection', (ws) => {
- if (verboseLogging) {
- console.log('Reload client connected to server')
- }
- })
- }
-
- function sendMessage (message) {
- if (verboseLogging) {
- console.log('Sending message to ' + (wss.clients.size) + ' connection(s): ' + message)
- }
-
- wss.clients.forEach(function each (client) {
- if (client.readyState === ws.OPEN) {
- client.send(message)
- }
- })
- }
-
- reloadReturn = {
- 'reload': function () {
- sendMessage('reload')
- },
- 'startWebSocketServer': function () {
- if (webSocketServerWaitStart) {
- startWebSocketServer()
- }
- }
- }
-
- if (server) { // Private return API only used in command line version of reload
- reloadReturn.reloadClientCode = function () {
- if (server) {
- return reloadCode
- }
- }
- }
-
- return reloadReturn
-}
--
To stop receiving notification emails like this one, please contact
bigosmallm@apache.org.