Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gzipstatic option #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 65 additions & 18 deletions lib/angular-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const proxyHandler = require("./proxy");
const useHttps = argv.ssl || argv.https;
const basePath = argv.path ? path.resolve(argv.path) : process.cwd();
const baseHref = argv.baseHref ? argv.baseHref : '';
const rootFile = argv.rootFile ? argv.rootFile: 'index.html';
const rootFile = argv.rootFile ? argv.rootFile : 'index.html';
const port = getPort(argv.p);

const NO_PATH_FILE_ERROR_MESSAGE =
Expand Down Expand Up @@ -74,7 +74,7 @@ if (useHttps) {
}

function start() {
server.listen(port, function() {
server.listen(port, function () {
if (argv.open == true || argv.o) {
opn((useHttps ? "https" : "http") + "://localhost:" + port);
}
Expand All @@ -87,7 +87,7 @@ function start() {
function requestListener(req, res) {
// When we hit the proxy, return
if (argv.proxy && proxyHandler(req, res, argv.proxy)) {
return;
return;
}
// Add CORS header if option chosen
if (argv.cors) {
Expand All @@ -108,22 +108,23 @@ function requestListener(req, res) {

const safeFullFileName = getFilePathFromUrl(req.url, basePath, { baseHref });

fs.stat(safeFullFileName, function(err, stats) {
var fileBuffer;
if (!err && stats.isFile()) {
fileBuffer = fs.readFileSync(safeFullFileName);
let ct = mime.lookup(safeFullFileName);
log(`Sending ${safeFullFileName} with Content-Type ${ct}`);
res.writeHead(200, { "Content-Type": ct });
} else {
log(`Route %s, replacing with rootFile ${rootFile}`, safeFullFileName);
fileBuffer = returnDistFile();
res.writeHead(200, { "Content-Type": "text/html" });
}
// get file content
let fileContent = getFileContent(req, safeFullFileName);
// check file content
if (fileContent && fileContent.content) {
log(`Sending ${safeFullFileName} with Content-Type ${fileContent.type} and Content-Encoding ${fileContent.encoding || 'none'}`);
}
else {
fileContent = getFileContent(req, path.join(basePath, rootFile));
log(`Route %s, replacing with rootFile ${rootFile}`, safeFullFileName);
}
let headers = {};
if (fileContent.type) headers["Content-Type"] = fileContent.type;
if (fileContent.encoding) headers["Content-Encoding"] = fileContent.encoding;
res.writeHead(200, headers);
res.write(fileContent.content);
res.end();

res.write(fileBuffer);
res.end();
});
}

function getPort(portNo) {
Expand Down Expand Up @@ -157,6 +158,52 @@ function returnDistFile(displayFileMessages = false) {
}
}

function getFileContent(req, filePath) {

let fileContent = null;
let contentType = null;
let contentEncoding = null;

if (filePath) {
// check accept-encoding header
const isGZipped = argv.gzipstatic && req && req.headers && (req.headers['accept-encoding'] || '').split(',').findIndex(e => e.trim() === 'gzip') > -1;
const filePathGZ = filePath + (isGZipped ? '.gz' : '');

// if encoding has gzip, check {filePath}.gz file
if (isGZipped) {
try {
let fileStats = fs.statSync(filePathGZ);
if (fileStats && fileStats.isFile()) {
fileContent = fs.readFileSync(filePathGZ);
contentEncoding = "gzip";
}
}
catch (error) { }
}
// if encoding has not gzip or {filePath}.gz file not exists, check {filePath}
if (!isGZipped || !fileContent) {
try {
let fileStats = fs.statSync(filePath);
if (fileStats && fileStats.isFile()) {
fileContent = fs.readFileSync(filePath);
}
}
catch (error) { }
}

// set content type
contentType = mime.lookup(filePath);
}

return {
content: fileContent,
type: contentType,
encoding: contentEncoding
}


}

function log() {
if (!argv.silent) {
console.log.apply(console, arguments);
Expand Down