Skip to content

Commit

Permalink
Fix lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 26, 2016
1 parent 69daec3 commit 0c12f38
Showing 1 changed file with 133 additions and 132 deletions.
265 changes: 133 additions & 132 deletions lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,149 @@ module.exports = function (options) {
}
delete req.headers['if-modified-since']; // Prevent false positive conditional GETs after enabling processimage
hijackResponse(res, function (err, res) {
var sourceMetadata;
function makeFilterInfosAndTargetFormat() {
return getFilterInfosAndTargetContentTypeFromQueryString(queryString, _.defaults({
allowOperation: options.allowOperation,
sourceFilePath: options.root && Path.resolve(options.root, req.url.substr(1)),
sourceMetadata: sourceMetadata
}, options));
}

function startProcessing(optionalFirstChunk) {
var hasEnded = false,
cleanedUp = false,
filters;
function cleanUp(doNotDestroyHijacked) {
if (!doNotDestroyHijacked) {
res.destroyHijacked();
}
if (!cleanedUp) {
cleanedUp = true;
// the filters are unpiped after the error is passed to
// next. doing the unpiping before calling next caused
// the tests to fail on node 0.12 (not on 4.0 and 0.10).
if (res._readableState && res._readableState.buffer && res._readableState.buffer.length > 0) {
res._readableState.buffer = [];
}
if (filters) {
filters.forEach(function (filter) {
if (filter.unpipe) {
filter.unpipe();
}
if (filter.kill) {
filter.kill();
} else if (filter.destroy) {
filter.destroy();
} else if (filter.resume) {
filter.resume();
}
if (filter.end) {
filter.end();
}
if (filter._readableState && filter._readableState.buffer && filter._readableState.buffer.length > 0) {
filter._readableState.buffer = [];
}
filter.removeAllListeners();
// Some of the filters seem to emit error more than once sometimes:
filter.on('error', function () {});
});
filters = null;
}
res.removeAllListeners();
}
}

function handleError(err) {
if (!hasEnded) {
hasEnded = true;
if (err) {
if ('commandLine' in this) {
err.message = this.commandLine + ': ' + err.message;
}
if (err.message === 'Input buffer contains unsupported image format') {
err = new httpErrors.UnsupportedMediaType(err.message);
}
if (err.message === 'Input image exceeds pixel limit') {
// ?metadata with an unrecognized image format
err = new httpErrors.RequestEntityTooLarge(err.message);
}

next(err);
}
res.unhijack();
cleanUp(true);
}
}

res.once('error', function () {
res.unhijack();
next(500);
});
res.once('close', cleanUp);
var targetContentType = filterInfosAndTargetFormat.targetContentType;
if (targetContentType) {
res.setHeader('Content-Type', targetContentType);
}
res.removeHeader('Content-Length');
var oldETag = res.getHeader('ETag'),
newETag;
if (oldETag) {
newETag = oldETag.replace(/"$/g, '-processimage"');
res.setHeader('ETag', newETag);

if (ifNoneMatch && ifNoneMatch.indexOf(newETag) !== -1) {
return res.status(304).end();
}
}
filters = [];
try {
filterInfosAndTargetFormat.filterInfos.forEach(function (filterInfo) {
var filter = filterInfo.create();
if (Array.isArray(filter)) {
Array.prototype.push.apply(filters, filter);
} else {
filters.push(filter);
}
});
} catch (e) {
return handleError(new httpErrors.BadRequest(e));
}
if (options.debug) {
// Only used by the test suite to assert that the right engine is used to process gifs:
res.setHeader('X-Express-Processimage', filterInfosAndTargetFormat.filterInfos.map(function (filterInfo) {
return filterInfo.operationName;
}).join(','));
}
if (optionalFirstChunk) {
filters[0].write(optionalFirstChunk);
}
for (var i = 0 ; i < filters.length ; i += 1) {
if (i < filters.length - 1) {
filters[i].pipe(filters[i + 1]);
}
// Some of the filters appear to emit error more than once:
filters[i].once('error', handleError);
}

res.pipe(filters[0]);
filters[filters.length - 1].on('end', function () {
hasEnded = true;
cleanUp();
}).pipe(res);
}

var contentType = res.getHeader('Content-Type');

if (contentType && (contentType.indexOf('image/') === 0 || isMetadataRequest)) {
var contentLengthHeaderValue = res.getHeader('Content-Length');

var sourceMetadata = {
sourceMetadata = {
contentType: contentType,
filesize: contentLengthHeaderValue && parseInt(contentLengthHeaderValue, 10),
etag: res.getHeader('ETag')
};

function makeFilterInfosAndTargetFormat() {
return getFilterInfosAndTargetContentTypeFromQueryString(queryString, _.defaults({
allowOperation: options.allowOperation,
sourceFilePath: options.root && Path.resolve(options.root, req.url.substr(1)),
sourceMetadata: sourceMetadata
}, options));
}

var filterInfosAndTargetFormat = makeFilterInfosAndTargetFormat();

if (filterInfosAndTargetFormat.filterInfos.length === 0) {
Expand Down Expand Up @@ -101,129 +225,6 @@ module.exports = function (options) {
} else {
startProcessing();
}

function startProcessing(optionalFirstChunk) {
var hasEnded = false,
cleanedUp = false,
filters;
function cleanUp(doNotDestroyHijacked) {
if (!doNotDestroyHijacked) {
res.destroyHijacked();
}
if (!cleanedUp) {
cleanedUp = true;
// the filters are unpiped after the error is passed to
// next. doing the unpiping before calling next caused
// the tests to fail on node 0.12 (not on 4.0 and 0.10).
if (res._readableState && res._readableState.buffer && res._readableState.buffer.length > 0) {
res._readableState.buffer = [];
}
if (filters) {
filters.forEach(function (filter) {
if (filter.unpipe) {
filter.unpipe();
}
if (filter.kill) {
filter.kill();
} else if (filter.destroy) {
filter.destroy();
} else if (filter.resume) {
filter.resume();
}
if (filter.end) {
filter.end();
}
if (filter._readableState && filter._readableState.buffer && filter._readableState.buffer.length > 0) {
filter._readableState.buffer = [];
}
filter.removeAllListeners();
// Some of the filters seem to emit error more than once sometimes:
filter.on('error', function () {});
});
filters = null;
}
res.removeAllListeners();
}
}

function handleError(err) {
if (!hasEnded) {
hasEnded = true;
if (err) {
if ('commandLine' in this) {
err.message = this.commandLine + ': ' + err.message;
}
if (err.message === 'Input buffer contains unsupported image format') {
err = new httpErrors.UnsupportedMediaType(err.message);
}
if (err.message === 'Input image exceeds pixel limit') {
// ?metadata with an unrecognized image format
err = new httpErrors.RequestEntityTooLarge(err.message);
}

next(err);
}
res.unhijack();
cleanUp(true);
}
}

res.once('error', function () {
res.unhijack();
next(500);
});
res.once('close', cleanUp);
var targetContentType = filterInfosAndTargetFormat.targetContentType;
if (targetContentType) {
res.setHeader('Content-Type', targetContentType);
}
res.removeHeader('Content-Length');
var oldETag = res.getHeader('ETag'),
newETag;
if (oldETag) {
newETag = oldETag.replace(/"$/g, '-processimage"');
res.setHeader('ETag', newETag);

if (ifNoneMatch && ifNoneMatch.indexOf(newETag) !== -1) {
return res.status(304).end();
}
}
filters = [];
try {
filterInfosAndTargetFormat.filterInfos.forEach(function (filterInfo) {
var filter = filterInfo.create();
if (Array.isArray(filter)) {
Array.prototype.push.apply(filters, filter);
} else {
filters.push(filter);
}
});
} catch (e) {
return handleError(new httpErrors.BadRequest(e));
}
if (options.debug) {
// Only used by the test suite to assert that the right engine is used to process gifs:
res.setHeader('X-Express-Processimage', filterInfosAndTargetFormat.filterInfos.map(function (filterInfo) {
return filterInfo.operationName;
}).join(','));
}
if (optionalFirstChunk) {
filters[0].write(optionalFirstChunk);
}
for (var i = 0 ; i < filters.length ; i += 1) {
if (i < filters.length - 1) {
filters[i].pipe(filters[i + 1]);
}
// Some of the filters appear to emit error more than once:
filters[i].once('error', handleError);
}

res.pipe(filters[0]);
filters[filters.length - 1].on('end', function () {
hasEnded = true;
cleanUp();
}).pipe(res);
}
} else {
res.unhijack();
}
Expand Down

0 comments on commit 0c12f38

Please sign in to comment.