rollup-plugin-serve
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -8,2 +8,3 @@ # Changelog | ||
- Default `contentBase` to current project folder | ||
- Option to serve over HTTPS | ||
@@ -10,0 +11,0 @@ ## [0.1.0] - 2016-09-29 |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var https = require('https'); | ||
var http = require('http'); | ||
@@ -21,11 +22,19 @@ var path = require('path'); | ||
options.port = options.port || 10001; | ||
options.headers = options.headers || {}; | ||
options.https = options.https || false; | ||
mime.default_type = 'text/plain'; | ||
http.createServer(function (request, response) { | ||
var server; | ||
// Fallback to http protocol if https option is false or SSL cert files do not exist | ||
var PROTOCOL = (options.https && 'https://') || 'http://'; | ||
var requestListener = function (request, response) { | ||
// Remove querystring | ||
var urlPath = decodeURI(request.url.split('?')[0]); | ||
Object.keys(options.headers).forEach(function (key) { | ||
response.setHeader(key, options.headers[key]); | ||
}); | ||
readFileFromContentBase(options.contentBase, urlPath, function (error, content, filePath) { | ||
if (!error) { | ||
if (!error) { | ||
return found(response, filePath, content) | ||
@@ -37,3 +46,5 @@ } | ||
'\n\n' + filePath + | ||
'\n\n' + Object.keys(error).map(function (k) { return error[k]; }).join('\n') + | ||
'\n\n' + Object.keys(error).map(function (k) { | ||
return error[k] | ||
}).join('\n') + | ||
'\n\n(rollup-plugin-serve)', 'utf-8'); | ||
@@ -63,4 +74,12 @@ return | ||
}); | ||
}).listen(options.port); | ||
}; | ||
if (options.https) { | ||
server = https.createServer(options.https, requestListener).listen(options.port); | ||
} else { | ||
server = http.createServer(requestListener).listen(options.port); | ||
} | ||
closeServerOnTermination(server); | ||
var running = options.verbose === false; | ||
@@ -75,3 +94,3 @@ | ||
// Log which url to visit | ||
var url = 'http://' + options.host + ':' + options.port; | ||
var url = (PROTOCOL + options.host) + ":" + (options.port); | ||
options.contentBase.forEach(function (base) { | ||
@@ -125,2 +144,12 @@ console.log(green(url) + ' -> ' + path.resolve(base)); | ||
function closeServerOnTermination (server) { | ||
var terminationSignals = ['SIGINT', 'SIGTERM']; | ||
terminationSignals.forEach(function (signal) { | ||
process.on(signal, function () { | ||
server.close(); | ||
process.exit(); | ||
}); | ||
}); | ||
} | ||
module.exports = serve; |
import { readFile } from 'fs'; | ||
import { createServer } from 'http'; | ||
import { createServer } from 'https'; | ||
import { createServer as createServer$1 } from 'http'; | ||
import { resolve } from 'path'; | ||
@@ -16,11 +17,19 @@ import mime from 'mime'; | ||
options.port = options.port || 10001; | ||
options.headers = options.headers || {}; | ||
options.https = options.https || false; | ||
mime.default_type = 'text/plain'; | ||
createServer(function (request, response) { | ||
var server; | ||
// Fallback to http protocol if https option is false or SSL cert files do not exist | ||
var PROTOCOL = (options.https && 'https://') || 'http://'; | ||
var requestListener = function (request, response) { | ||
// Remove querystring | ||
var urlPath = decodeURI(request.url.split('?')[0]); | ||
Object.keys(options.headers).forEach(function (key) { | ||
response.setHeader(key, options.headers[key]); | ||
}); | ||
readFileFromContentBase(options.contentBase, urlPath, function (error, content, filePath) { | ||
if (!error) { | ||
if (!error) { | ||
return found(response, filePath, content) | ||
@@ -32,3 +41,5 @@ } | ||
'\n\n' + filePath + | ||
'\n\n' + Object.keys(error).map(function (k) { return error[k]; }).join('\n') + | ||
'\n\n' + Object.keys(error).map(function (k) { | ||
return error[k] | ||
}).join('\n') + | ||
'\n\n(rollup-plugin-serve)', 'utf-8'); | ||
@@ -58,4 +69,12 @@ return | ||
}); | ||
}).listen(options.port); | ||
}; | ||
if (options.https) { | ||
server = createServer(options.https, requestListener).listen(options.port); | ||
} else { | ||
server = createServer$1(requestListener).listen(options.port); | ||
} | ||
closeServerOnTermination(server); | ||
var running = options.verbose === false; | ||
@@ -70,3 +89,3 @@ | ||
// Log which url to visit | ||
var url = 'http://' + options.host + ':' + options.port; | ||
var url = (PROTOCOL + options.host) + ":" + (options.port); | ||
options.contentBase.forEach(function (base) { | ||
@@ -120,2 +139,12 @@ console.log(green(url) + ' -> ' + resolve(base)); | ||
function closeServerOnTermination (server) { | ||
var terminationSignals = ['SIGINT', 'SIGTERM']; | ||
terminationSignals.forEach(function (signal) { | ||
process.on(signal, function () { | ||
server.close(); | ||
process.exit(); | ||
}); | ||
}); | ||
} | ||
export default serve; |
@@ -0,0 +0,0 @@ The MIT License (MIT) |
{ | ||
"name": "rollup-plugin-serve", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Serve your rolled up bundle", | ||
@@ -12,3 +12,3 @@ "main": "dist/index.cjs.js", | ||
"lint": "standard rollup.config.js src/**", | ||
"prepublish": "npm run build" | ||
"prepare": "npm run build" | ||
}, | ||
@@ -15,0 +15,0 @@ "keywords": [ |
@@ -18,3 +18,3 @@ # Rollup plugin to serve the bundle | ||
</a> | ||
## Installation | ||
@@ -64,3 +64,15 @@ ``` | ||
host: 'localhost', | ||
port: 10001 | ||
port: 10001, | ||
// By default server will be served over HTTP (https: false). It can optionally be served over HTTPS | ||
https: { | ||
key: fs.readFileSync("/path/to/server.key"), | ||
cert: fs.readFileSync("/path/to/server.crt"), | ||
ca: fs.readFileSync("/path/to/ca.pem") | ||
}, | ||
//set headers | ||
headers: { | ||
foo: 'bar' | ||
} | ||
}) | ||
@@ -67,0 +79,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14448
254
105
5