staticServer(root) {
    var isFile = false;
    try {  // For supporting mounting files instead of just directories
      isFile = fs.statSync(root).isFile();
    } catch (e) {
      if (e.code !== 'ENOENT') throw e;
    }
    return (req, res, next) => {
      if (req.method !== 'GET' && req.method !== 'HEAD') return next();
      var reqpath = isFile ? '' : url.parse(req.url).pathname;
      var hasNoOrigin = !req.headers.origin;
      var injectCandidates =
          [new RegExp('</body>', 'i'), new RegExp('</svg>'), new RegExp('</head>', 'i')];
      var injectTag = null;
      var fileExt = "";

      const directory = () => {
        var pathname = url.parse(req.originalUrl).pathname;
        res.statusCode = 301;
        res.setHeader('Location', pathname + '/');
        res.end('Redirecting');
      };

      const file = (filepath /*, stat*/) => {
        fileExt = path.extname(filepath).toLocaleLowerCase();
        var possibleExtensions = [ "", ".html", ".htm", ".xhtml", ".php", ".svg" ];

        if (hasNoOrigin && (possibleExtensions.indexOf(fileExt) > -1)) {
          // TODO: Sync file read here is not nice, but we need to determine if the html should be
          // injected or not
          var contents = fs.readFileSync(filepath, 'utf8');
          for (var i = 0; i < injectCandidates.length; ++i) {
            let match = injectCandidates[i].exec(contents);
            if (match) {
              injectTag = match[0];
              break;
            }
          }
          if (injectTag === null && this.logLevel >= 3) {
            console.warn(
                'Failed to inject refresh script!'.yellow, 'Couldn\'t find any of the tags ',
                injectCandidates, 'from', filepath);
          }
        }
      };

      const error = (err) => {
        if (err.status === 404) return next();
        next(err);
      };

      const inject = (stream) => {
        if (fileExt == ".wasm") {
          res.setHeader('Content-Type', 'application/wasm');
        }
        if (injectTag) {
          const injectedCode = getInjectedCode(this.generation);
          // We need to modify the length given to browser
          var len = injectedCode.length + res.getHeader('Content-Length');
          res.setHeader('Content-Length', len);
          var originalPipe = stream.pipe;
          stream.pipe = function(resp) {
            originalPipe
                .call(stream, es.replace(new RegExp(injectTag, 'i'), injectedCode + injectTag))
                .pipe(resp);
          };
        }
      };

      send(req, reqpath, {root: root})
          .on('error', error)
          .on('directory', directory)
          .on('file', file)
          .on('stream', inject)
          .pipe(res);
    };
  }
// Based on connect.static(), but streamlined and with added code injecter
Based on connect.static(), but streamlined and with added code injecter
Based on connect . static () but streamlined and with added code injecter
staticServer(root) {
    var isFile = false;
    try {  
      isFile = fs.statSync(root).isFile();
    } catch (e) {
      if (e.code !== 'ENOENT') throw e;
    }
    return (req, res, next) => {
      if (req.method !== 'GET' && req.method !== 'HEAD') return next();
      var reqpath = isFile ? '' : url.parse(req.url).pathname;
      var hasNoOrigin = !req.headers.origin;
      var injectCandidates =
          [new RegExp('</body>', 'i'), new RegExp('</svg>'), new RegExp('</head>', 'i')];
      var injectTag = null;
      var fileExt = "";
      const directory = () => {
        var pathname = url.parse(req.originalUrl).pathname;
        res.statusCode = 301;
        res.setHeader('Location', pathname + '/');
        res.end('Redirecting');
      };
      const file = (filepath ) => {
        fileExt = path.extname(filepath).toLocaleLowerCase();
        var possibleExtensions = [ "", ".html", ".htm", ".xhtml", ".php", ".svg" ];
        if (hasNoOrigin && (possibleExtensions.indexOf(fileExt) > -1)) {
          var contents = fs.readFileSync(filepath, 'utf8');
          for (var i = 0; i < injectCandidates.length; ++i) {
            let match = injectCandidates[i].exec(contents);
            if (match) {
              injectTag = match[0];
              break;
            }
          }
          if (injectTag === null && this.logLevel >= 3) {
            console.warn(
                'Failed to inject refresh script!'.yellow, 'Couldn\'t find any of the tags ',
                injectCandidates, 'from', filepath);
          }
        }
      };
      const error = (err) => {
        if (err.status === 404) return next();
        next(err);
      };
      const inject = (stream) => {
        if (fileExt == ".wasm") {
          res.setHeader('Content-Type', 'application/wasm');
        }
        if (injectTag) {
          const injectedCode = getInjectedCode(this.generation);
          var len = injectedCode.length + res.getHeader('Content-Length');
          res.setHeader('Content-Length', len);
          var originalPipe = stream.pipe;
          stream.pipe = function(resp) {
            originalPipe
                .call(stream, es.replace(new RegExp(injectTag, 'i'), injectedCode + injectTag))
                .pipe(resp);
          };
        }
      };
      send(req, reqpath, {root: root})
          .on('error', error)
          .on('directory', directory)
          .on('file', file)
          .on('stream', inject)
          .pipe(res);
    };
  }
staticServer ( root ) { var isFile = false ; try { isFile = fs . statSync ( root ) . isFile ( ) ; } catch ( e ) { if ( e . code !== 'ENOENT' ) throw e ; } return ( req , res , next ) => { if ( req . method !== 'GET' && req . method !== 'HEAD' ) return next ( ) ; var reqpath = isFile ? '' : url . parse ( req . url ) . pathname ; var hasNoOrigin = ! req . headers . origin ; var injectCandidates = [ new RegExp ( '</body>' , 'i' ) , new RegExp ( '</svg>' ) , new RegExp ( '</head>' , 'i' ) ] ; var injectTag = null ; var fileExt = "" ; const directory = ( ) => { var pathname = url . parse ( req . originalUrl ) . pathname ; res . statusCode = 301 ; res . setHeader ( 'Location' , pathname + '/' ) ; res . end ( 'Redirecting' ) ; } ; const file = ( filepath ) => { fileExt = path . extname ( filepath ) . toLocaleLowerCase ( ) ; var possibleExtensions = [ "" , ".html" , ".htm" , ".xhtml" , ".php" , ".svg" ] ; if ( hasNoOrigin && ( possibleExtensions . indexOf ( fileExt ) > - 1 ) ) { var contents = fs . readFileSync ( filepath , 'utf8' ) ; for ( var i = 0 ; i < injectCandidates . length ; ++ i ) { let match = injectCandidates [ i ] . exec ( contents ) ; if ( match ) { injectTag = match [ 0 ] ; break ; } } if ( injectTag === null && this . logLevel >= 3 ) { console . warn ( 'Failed to inject refresh script!' . yellow , 'Couldn\'t find any of the tags ' , injectCandidates , 'from' , filepath ) ; } } } ; const error = ( err ) => { if ( err . status === 404 ) return next ( ) ; next ( err ) ; } ; const inject = ( stream ) => { if ( fileExt == ".wasm" ) { res . setHeader ( 'Content-Type' , 'application/wasm' ) ; } if ( injectTag ) { const injectedCode = getInjectedCode ( this . generation ) ; var len = injectedCode . length + res . getHeader ( 'Content-Length' ) ; res . setHeader ( 'Content-Length' , len ) ; var originalPipe = stream . pipe ; stream . pipe = function ( resp ) { originalPipe . call ( stream , es . replace ( new RegExp ( injectTag , 'i' ) , injectedCode + injectTag ) ) . pipe ( resp ) ; } ; } } ; send ( req , reqpath , { root : root } ) . on ( 'error' , error ) . on ( 'directory' , directory ) . on ( 'file' , file ) . on ( 'stream' , inject ) . pipe ( res ) ; } ; }
Based on connect.static(), but streamlined and with added code injecter
Based on connect . static () but streamlined and with added code injecter
// For supporting mounting files instead of just directories /*, stat*/ // TODO: Sync file read here is not nice, but we need to determine if the html should be // injected or not // We need to modify the length given to browser