Github user sgrebnov commented on a diff in the pull request: https://github.com/apache/cordova-paramedic/pull/3#discussion_r59072232 --- Diff: lib/LocalServer.js --- @@ -0,0 +1,141 @@ +/** + 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. +*/ + +var Q = require('q'), + io = require('socket.io'), + logger = require('./utils').logger, + exec = require('./utils').exec, + path = require('path'), + util = require('util'), + portChecker = require('tcp-port-used'), + EventEmitter = require('events').EventEmitter; + +function LocalServer(port, externalServerUrl) { + this.port = port; + this.externalServerUrl = externalServerUrl; +} + +util.inherits(LocalServer, EventEmitter); + +LocalServer.startServer = function(ports, externalServerUrl, useTunnel) { + logger.normal("local-server: scanning ports from " + ports.start + " to " + ports.end); + + return LocalServer.getFirstAvailablePort(ports.start, ports.end) + .then(function(port) { + logger.normal("local-server: port " + port + " is available"); + logger.info("local-server: starting local medic server"); + + var localServer = new LocalServer(port, externalServerUrl); + localServer.createSocketListener(); + + if (useTunnel) { + return Q.promise(function(resolve) { + localServer.createTunnel().then(function(){ + resolve(localServer); + }); + }); + } + + return localServer; + }); +}; + +LocalServer.getFirstAvailablePort = function(startPort, endPort) { + var ports = Array.apply(null, Array(endPort - startPort + 1)).map(function(element, index) { + return startPort + index; + }); + + return Q.promise(function(resolve, reject) { + ports.reduce(function(promise, port){ + return promise.then(function(isPortUsed) { + if (!isPortUsed) { + resolve(port - 1); + return false; + } else { + return portChecker.check(port); + } + }); + }, Q(true)); + }); +}; + +LocalServer.prototype.createTunnel = function() { + logger.info('cordova-paramedic: attempt to create local tunnel'); + var self = this; + + //TODO: use localtunnel module instead of shell + return Q.Promise(function(resolve, reject) { + exec(path.resolve(__dirname, '../node_modules/.bin/lt') + ' --port ' + self.port, null, function(output) { + var tunneledUrl = output.split(' ')[3]; + self.tunneledUrl = tunneledUrl; + logger.info('cordova-paramedic: using tunneled url ' + tunneledUrl); + resolve(); + }); + }); +}; + +LocalServer.prototype.createSocketListener = function() { + var listener = io.listen(this.port, { + pingTimeout: 60000, // how many ms without a pong packet to consider the connection closed + pingInterval: 25000 // how many ms before sending a new ping packet --- End diff -- Updated
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org For additional commands, e-mail: dev-h...@cordova.apache.org