Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import connect from 'connect';
import path from 'path';
import portscanner from 'portscanner';
import serveStatic from 'serve-static';
// Configuration for the server.
const PORT = 9999;
const MAX_PORT = PORT + 100;
const HOST = '127.0.0.1'
const app = connect();
// update mimetype
serveStatic.mime.define({'application/wasm': ['wasm']});
app.use(serveStatic(path.join(__dirname, '..')));
portscanner.findAPortNotInUse(PORT, MAX_PORT, HOST, (error, port) => {
if (error) {
throw error;
}
process.stdout.write(`Serving on http://${HOST}:${port}` + '\n\n');
app.listen(port);
});
var logger = require('./simple-http-logger')
var urlLib = require('url')
var xtend = require('xtend')
var pushState = require('connect-pushstate')
var liveReload = require('inject-lr-script')
var urlTrim = require('url-trim')
var escapeHtml = require('escape-html')
var fs = require('fs')
var browserify = require('browserify')
var path = require('path')
var liveReloadClientFile = path.resolve(__dirname, 'reload', 'client.js')
var bundledReloadClientFile = path.resolve(__dirname, '..', 'build', 'bundled-livereload-client.js')
// Patch 'wasm' since send has not yet been updated to latest 'mime' module
serveStatic.mime.types['wasm'] = 'application/wasm'
module.exports = budoMiddleware
function budoMiddleware (entryMiddleware, opts) {
opts = opts || {}
var staticPaths = [].concat(opts.dir).filter(Boolean)
if (staticPaths.length === 0) {
staticPaths = [ process.cwd() ]
}
var entrySrc = opts.serve
var live = opts.live
var cors = opts.cors
var handler = stacked()
var middlewares = [].concat(opts.middleware).filter(Boolean)
// Everything is logged except favicon.ico
(req, res, next) => {
const type = serveStatic.mime.lookup(req.url)
// build paths:
if (req.url.indexOf(utils.BUILD_FOLDER) > -1) {
return serveStatic(global.servePath, {
// let folders with index.html pass through
index: false,
// render html files as plain text
setHeaders: (res, path) => {
if (type === 'text/html') {
res.setHeader('Content-Type', 'text/plain')
}
}
})(req, res, next)
} else {
// all other assets serve static
return serveStatic(global.servePath)(req, res, next)
}
app.use((req, res, next) => {
const path = req.url;
req.url = asset.decode(path);
let mime = serveStatic.mime.lookup(req.url);
if (mime.match('image*') || req.url.includes('manifest.json')) {
res.setHeader('Cache-Control', 'public, max-age=' + CACHE_CONTROL_EXPIRES);
} else if (req.url === path) {
res.setHeader('Cache-Control', 'public, max-age=' + CACHE_CONTROL_SHORT_EXPIRES);
} else {
// versioned assets don't expire
res.setHeader('Cache-Control', 'public, max-age=' + CACHE_CONTROL_NEVER_EXPIRE);
}
staticFilesMiddleware(req, res, next);
});
let fs = require("fs");
let serveStatic = require('serve-static');
let sanitizeOptions = require('./util/options').sanitizeOptions;
let findEncoding = require('./util/encoding-selection').findEncoding;
let mime = serveStatic.mime;
module.exports = expressStaticGzipMiddleware;
/**
* Generates a middleware function to serve pre-compressed files. It is build on top of serveStatic.
* The pre-compressed files need to be placed next to the original files, in the provided `root` directory.
* @param { string } root: directory to staticly serve files from
* @param { expressStaticGzip.ExpressStaticGzipOptions } options: options to change module behaviour
* @returns express middleware function
*/
function expressStaticGzipMiddleware(root, options) {
let opts = sanitizeOptions(options);
let serveStaticMiddleware = serveStatic(root, opts.serveStatic || null);
let compressions = [];
let files = {};
function setCustomCacheControl (res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=10')
}
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
}
function setCustomCacheControl (res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=10')
}
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
}
const http = require('http');
const https = require('https');
const serveStatic = require('serve-static');
const getPort = require('get-port');
const serverErrors = require('./utils/customErrors').serverErrors;
const generateCertificate = require('./utils/generateCertificate');
const getCertificate = require('./utils/getCertificate');
const AnsiToHtml = require('ansi-to-html');
const logger = require('@parcel/logger');
const path = require('path');
const url = require('url');
const ansiToHtml = new AnsiToHtml({newline: true});
serveStatic.mime.define({
'application/wasm': ['wasm'],
});
function setHeaders(res) {
enableCors(res);
}
function enableCors(res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Methods',
'GET, HEAD, PUT, PATCH, POST, DELETE',
);
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Content-Type',
function setCustomCacheControl (res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=10')
}
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
}
const setCustomCacheControl = (res, path) => {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'build, max-age=0')
}
}