Comparing version 1.0.1 to 2.0.0
@@ -1,11 +0,11 @@ | ||
var Plugin = require('./Plugin'); | ||
var Preprocessor = require('./Preprocessor'); | ||
const Plugin = require('./Plugin'); | ||
const Preprocessor = require('./Preprocessor'); | ||
Plugin.$inject = ['gzippedFilePaths', 'emitter', 'logger', 'customFileHandlers']; | ||
Plugin.$inject = ['gzippedFilePaths', 'emitter', 'logger', 'customFileHandlers', 'config.basePath']; | ||
Preprocessor.$inject = ['gzippedFilePaths', 'config.gzip', 'logger', 'helper']; | ||
module.exports = { | ||
'gzippedFilePaths': ['value', []], | ||
gzippedFilePaths: ['value', []], | ||
'framework:gzip': ['factory', Plugin], | ||
'preprocessor:gzip': ['factory', Preprocessor] | ||
'preprocessor:gzip': ['factory', Preprocessor], | ||
}; |
@@ -1,12 +0,15 @@ | ||
var path = require('path'); | ||
const _compressedFiles = Object.create(null); | ||
let _log = null; | ||
var log = null; | ||
var compressedFiles = Object.create(null); | ||
function relativePath(basePath, filePath) { | ||
return filePath.replace(basePath, ''); | ||
} | ||
function updateFiles(files, gzippedFiles) { | ||
var webserverFiles = []; | ||
function filterOutGzippedFiles(files, gzippedFilePaths, basePath) { | ||
const webserverFiles = []; | ||
files.included.forEach(function(file) { | ||
if (gzippedFiles.indexOf(file.path) !== -1) { | ||
compressedFiles[path.basename(file.path)] = file; | ||
// filter out gzipped files from .served | ||
files.served.forEach(file => { | ||
if (gzippedFilePaths.includes(file.path)) { | ||
_compressedFiles[relativePath(basePath, file.path)] = file; | ||
} else { | ||
@@ -17,10 +20,8 @@ webserverFiles.push(file); | ||
// eslint-disable-next-line no-param-reassign | ||
files.served = webserverFiles; | ||
} | ||
// Set Gzip headers on the response | ||
function setGzipResponseheaders(response) { | ||
// var type = mime.lookup(file.originalPath); | ||
// var charset = mime.charsets.lookup(type); | ||
// Set gzip headers on the response | ||
function setGzipHeaders(response) { | ||
response.setHeader('Content-Type', 'application/javascript; charset=utf-8'); | ||
@@ -32,3 +33,3 @@ response.setHeader('Content-Encoding', 'gzip'); | ||
// check if the client accepts gzipped responses | ||
function clientAcceptsGzipEncoding(request) { | ||
function acceptsGzip(request) { | ||
// TODO: request.getHeader ??? | ||
@@ -39,10 +40,14 @@ return request.headers['accept-encoding'].indexOf('gzip') !== -1; | ||
// Response handler for gzip encoded files | ||
function responseHandler(request, response) { | ||
var url = request.url.substr(1); | ||
var urlBasename = path.basename(url).split('?')[0]; | ||
var file = compressedFiles[urlBasename]; | ||
function gzipResponseHandler(request, response) { | ||
const urlPath = request.url.split('?')[0]; | ||
const file = _compressedFiles[urlPath.replace('/base', '')]; | ||
if (clientAcceptsGzipEncoding(request)) { | ||
log.debug('serving (gzip): ' + file.path); | ||
setGzipResponseheaders(response); | ||
if (!file) { | ||
_log.error(`Gzipped file not found: ${request.url}`); | ||
throw new Error(`Gzipped file not found: ${request.url}`); | ||
} | ||
if (acceptsGzip(request)) { | ||
_log.debug(`found file, serving (gzip): ${file.path}`); | ||
setGzipHeaders(response); | ||
response.writeHead(200); | ||
@@ -52,3 +57,3 @@ response.end(file.content); | ||
// TODO: fix this — fall back to non-gzipped response | ||
log.error('CLIENT DOES NOT ACCEPT GZIPPED REPSONSES'); | ||
_log.error('Client does not accept gzipped responses'); | ||
throw new Error(); | ||
@@ -58,22 +63,24 @@ } | ||
function createGzipFileHandler(gzippedFiles) { | ||
function createGzipHandler(gzippedFilePaths, basePath) { | ||
const relativePaths = gzippedFilePaths.map(filePath => filePath.replace(basePath, '')); | ||
return { | ||
urlRegex: new RegExp(gzippedFiles.map(path.basename).join('|')), | ||
handler: responseHandler | ||
urlRegex: new RegExp(gzippedFilePaths.map(relativePaths).join('|')), | ||
handler: gzipResponseHandler, | ||
}; | ||
} | ||
function Plugin(gzippedFiles, emitter, logger, customFileHandlers) { | ||
log = logger.create('gzip-plugin'); | ||
function Plugin(gzippedFilePaths, emitter, logger, customFileHandlers, configBasePath) { | ||
_log = logger.create('gzip-plugin'); | ||
emitter.on('file_list_modified', function(filesPromise) { | ||
filesPromise.then(function(files) { | ||
updateFiles(files, gzippedFiles); | ||
customFileHandlers.push(createGzipFileHandler(gzippedFiles)); | ||
}); | ||
emitter.on('file_list_modified', files => { | ||
// create handlers only once, this makes sense only for `watch mode` | ||
// assume that file list doesn't change in between watched runs | ||
if (Object.keys(_compressedFiles).length === 0) { | ||
customFileHandlers.push(createGzipHandler(gzippedFilePaths, configBasePath)); | ||
} | ||
filterOutGzippedFiles(files, gzippedFilePaths, configBasePath); | ||
}); | ||
} | ||
// PUBLIC API | ||
module.exports = Plugin; |
@@ -1,23 +0,18 @@ | ||
var zlib = require('zlib'); | ||
var util = require('./util'); | ||
const zlib = require('zlib'); | ||
const util = require('./util'); | ||
// Proprocess files to provide a gzip compressed alternative version | ||
function Preprocesor(gzippedFilePaths, config, logger, helper) { | ||
var log = logger.create('preprocessor.gzip'); | ||
// Preprocess files to provide a gzip compressed alternative version | ||
function Preprocesor(gzippedFilePaths, config, logger) { | ||
const log = logger.create('preprocessor.gzip'); | ||
var transformPath = function(filepath) { | ||
return filepath.concat('.gz'); | ||
}; | ||
return (content, file, done) => { | ||
const originalSize = content.length; | ||
return function(content, file, done) { | ||
var originalSize = content.length; | ||
const contentBuffer = Buffer.isBuffer(content) ? content : Buffer.from(content); | ||
if(!Buffer.isBuffer(content)) { | ||
content = new Buffer(content, 'utf-8'); | ||
} | ||
zlib.gzip(content, function(err, gzippedContent) { | ||
zlib.gzip(contentBuffer, (err, gzippedContent) => { | ||
if (err) { | ||
log.error(err); | ||
return done(err); | ||
done(err); | ||
return; | ||
} | ||
@@ -27,3 +22,3 @@ | ||
log.info('compressed ' + file.originalPath + ' [' + util.bytesToSize(originalSize) + ' -> ' + util.bytesToSize(gzippedContent.length) + ']'); | ||
log.info(`compressed ${file.originalPath} [${util.bytesToSize(originalSize)} -> ${util.bytesToSize(gzippedContent.length)}]`); | ||
done(null, gzippedContent); | ||
@@ -30,0 +25,0 @@ }); |
@@ -1,8 +0,8 @@ | ||
var bytesToSize = function (bytes) { | ||
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
const bytesToSize = bytes => { | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
if (bytes === 0) { | ||
return '0 Byte'; | ||
} | ||
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10); | ||
return Math.round(bytes / Math.pow(1024, i), 2) + sizes[i]; | ||
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10); | ||
return Math.round(bytes / (1024 ** i), 2) + sizes[i]; | ||
}; | ||
@@ -9,0 +9,0 @@ |
{ | ||
"name": "karma-gzip", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "A gzip preprocessor for Karma", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"lint": "eslint ." | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:Ferocia/karma-gzip.git" | ||
"url": "git@github.com:GreenGremlin/karma-gzip.git" | ||
}, | ||
"author": "Justin Morris <justin.morris@ferocia.com.au> (http://ferocia.com.au)", | ||
"author": { | ||
"name": "Jonathan Felchlin", | ||
"email": "jonathan@xgecko.com" | ||
}, | ||
"contributors": [ | ||
"Jonathan Felchlin <jonathan@xgecko.com>", | ||
"Ilya Furman <ilya.furman@toptal.com>" | ||
], | ||
"license": "MIT", | ||
@@ -21,9 +29,18 @@ "keywords": [ | ||
], | ||
"engines": { | ||
"node": ">=4.8.7" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-auto-release": "~0.0.1", | ||
"grunt-bump": "~0.0.7", | ||
"grunt-contrib-jshint": "~0.6.0", | ||
"grunt-npm": "~0.0.2" | ||
"eslint": "^5.4.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.1", | ||
"eslint-plugin-react": "^7.11.1", | ||
"grunt": "~1.0.1", | ||
"grunt-bump": "^0.8.0", | ||
"grunt-conventional-changelog": "^6.1.0", | ||
"grunt-eslint": "^19.0.0", | ||
"grunt-npm": "0.0.2", | ||
"load-grunt-tasks": "^3.2.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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9489
10
181
11
2