New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jpegtran

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jpegtran - npm Package Compare versions

Comparing version 1.0.5 to 1.0.6

.editorconfig

256

lib/JpegTran.js

@@ -1,15 +0,13 @@

var childProcess = require('child_process'),
Stream = require('stream').Stream,
util = require('util'),
which = require('which'),
memoizeAsync = require('memoizeasync');
const childProcess = require('child_process');
const Stream = require('stream').Stream;
const util = require('util');
const which = require('which');
const memoizeAsync = require('memoizeasync');
function JpegTran(jpegTranArgs) {
Stream.call(this);
Stream.call(this);
this.jpegTranArgs = jpegTranArgs;
this.writable = this.readable = true;
this.hasEnded = false;
this.jpegTranArgs = jpegTranArgs;
this.writable = this.readable = true;
this.hasEnded = false;
}

@@ -19,136 +17,156 @@

JpegTran.getBinaryPath = memoizeAsync(function (cb) {
which('jpegtran', function (err, jpegTranBinaryPath) {
if (err) {
jpegTranBinaryPath = require('jpegtran-bin');
}
if (jpegTranBinaryPath) {
cb(null, jpegTranBinaryPath);
} else {
cb(new Error('No jpegtran binary in PATH and jpegtran-bin does not provide a pre-built binary for your architecture'));
}
});
JpegTran.getBinaryPath = memoizeAsync(cb => {
which('jpegtran', (err, jpegTranBinaryPath) => {
if (err) {
jpegTranBinaryPath = require('jpegtran-bin');
}
if (jpegTranBinaryPath) {
cb(null, jpegTranBinaryPath);
} else {
cb(
new Error(
'No jpegtran binary in PATH and jpegtran-bin does not provide a pre-built binary for your architecture'
)
);
}
});
});
JpegTran.prototype._error = function (err) {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.emit('error', err);
}
JpegTran.prototype._error = function(err) {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.emit('error', err);
}
};
JpegTran.prototype.write = function (chunk) {
if (this.hasEnded) {
return;
}
if (this.jpegTranProcess) {
this.jpegTranProcess.stdin.write(chunk);
} else {
if (!this.bufferedChunks) {
this.bufferedChunks = [];
JpegTran.getBinaryPath(function (err, jpegTranBinaryPath) {
if (this.hasEnded) {
return;
}
if (err) {
return this._error(err);
}
this.commandLine = jpegTranBinaryPath + (this.jpegTranArgs ? ' ' + this.jpegTranArgs.join(' ') : ''); // For debugging
this.jpegTranProcess = childProcess.spawn(jpegTranBinaryPath, this.jpegTranArgs);
JpegTran.prototype.write = function(chunk) {
if (this.hasEnded) {
return;
}
if (this.jpegTranProcess) {
this.jpegTranProcess.stdin.write(chunk);
} else {
if (!this.bufferedChunks) {
this.bufferedChunks = [];
JpegTran.getBinaryPath((err, jpegTranBinaryPath) => {
if (this.hasEnded) {
return;
}
if (err) {
return this._error(err);
}
this.commandLine =
jpegTranBinaryPath +
(this.jpegTranArgs ? ' ' + this.jpegTranArgs.join(' ') : ''); // For debugging
this.jpegTranProcess = childProcess.spawn(
jpegTranBinaryPath,
this.jpegTranArgs
);
this.seenDataOnStdout = false;
this.seenDataOnStdout = false;
this.jpegTranProcess.on('error', this._error.bind(this));
this.jpegTranProcess.on('error', this._error.bind(this));
// The child process might close its STDIN prematurely and emit EPIPE when the next chunk is written.
// That's not necessarily an error, so prevent it from causing an exception:
this.jpegTranProcess.stdin.on('error', function () {});
// The child process might close its STDIN prematurely and emit EPIPE when the next chunk is written.
// That's not necessarily an error, so prevent it from causing an exception:
this.jpegTranProcess.stdin.on('error', () => {});
this.jpegTranProcess.on('exit', function (exitCode) {
if (exitCode > 0 && !this.hasEnded) {
this._error(new Error('The jpegtran process exited with a non-zero exit code: ' + exitCode));
this.hasEnded = true;
}
}.bind(this));
this.jpegTranProcess.on('exit', exitCode => {
if (exitCode > 0 && !this.hasEnded) {
this._error(
new Error(
'The jpegtran process exited with a non-zero exit code: ' +
exitCode
)
);
this.hasEnded = true;
}
});
this.jpegTranProcess.stdout.on('data', function (chunk) {
this.seenDataOnStdout = true;
this.emit('data', chunk);
}.bind(this)).on('end', function () {
this.jpegTranProcess = null;
if (!this.hasEnded) {
if (this.seenDataOnStdout) {
this.emit('end');
} else {
this._error(new Error('JpegTran: The stdout stream ended without emitting any data'));
}
this.hasEnded = true;
}
}.bind(this));
this.jpegTranProcess.stdout
.on('data', chunk => {
this.seenDataOnStdout = true;
this.emit('data', chunk);
})
.on('end', () => {
this.jpegTranProcess = null;
if (!this.hasEnded) {
if (this.seenDataOnStdout) {
this.emit('end');
} else {
this._error(
new Error(
'JpegTran: The stdout stream ended without emitting any data'
)
);
}
this.hasEnded = true;
}
});
if (this.isPaused) {
this.jpegTranProcess.stdout.pause();
}
if (this.isPaused) {
this.jpegTranProcess.stdout.pause();
}
this.bufferedChunks.forEach(function (chunk) {
if (chunk === null) {
this.jpegTranProcess.stdin.end();
} else {
this.jpegTranProcess.stdin.write(chunk);
}
}, this);
this.bufferedChunks = null;
}.bind(this));
}
this.bufferedChunks.push(chunk);
this.bufferedChunks.forEach(function(chunk) {
if (chunk === null) {
this.jpegTranProcess.stdin.end();
} else {
this.jpegTranProcess.stdin.write(chunk);
}
}, this);
this.bufferedChunks = null;
});
}
this.bufferedChunks.push(chunk);
}
};
JpegTran.prototype.cleanUp = function () {
if (this.jpegTranProcess) {
this.jpegTranProcess.kill();
this.jpegTranProcess = null;
}
this.bufferedChunks = null;
JpegTran.prototype.cleanUp = function() {
if (this.jpegTranProcess) {
this.jpegTranProcess.kill();
this.jpegTranProcess = null;
}
this.bufferedChunks = null;
};
JpegTran.prototype.destroy = function () {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.bufferedChunks = null;
}
JpegTran.prototype.destroy = function() {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.bufferedChunks = null;
}
};
JpegTran.prototype.end = function (chunk) {
if (chunk) {
this.write(chunk);
}
if (this.jpegTranProcess) {
this.jpegTranProcess.stdin.end();
JpegTran.prototype.end = function(chunk) {
if (chunk) {
this.write(chunk);
}
if (this.jpegTranProcess) {
this.jpegTranProcess.stdin.end();
} else {
if (this.bufferedChunks) {
this.bufferedChunks.push(null);
} else {
if (this.bufferedChunks) {
this.bufferedChunks.push(null);
} else {
// .end called without an argument and with no preceeding .write calls. Make sure that we do create a process in that case:
this.write(new Buffer(0));
}
// .end called without an argument and with no preceeding .write calls. Make sure that we do create a process in that case:
this.write(new Buffer(0));
}
}
};
JpegTran.prototype.pause = function () {
if (this.jpegTranProcess) {
this.jpegTranProcess.stdout.pause();
}
this.isPaused = true;
JpegTran.prototype.pause = function() {
if (this.jpegTranProcess) {
this.jpegTranProcess.stdout.pause();
}
this.isPaused = true;
};
JpegTran.prototype.resume = function () {
if (this.jpegTranProcess) {
this.jpegTranProcess.stdout.resume();
}
this.isPaused = false;
JpegTran.prototype.resume = function() {
if (this.jpegTranProcess) {
this.jpegTranProcess.stdout.resume();
}
this.isPaused = false;
};
module.exports = JpegTran;
{
"name": "jpegtran",
"version": "1.0.5",
"version": "1.0.6",
"description": "The jpegtran utility as a readable/writable stream",

@@ -10,21 +10,24 @@ "main": "lib/JpegTran.js",

"dependencies": {
"jpegtran-bin": "=3.0.4",
"memoizeasync": "0.9.0",
"which": "1.1.1"
"jpegtran-bin": "^4.0.0",
"memoizeasync": "^1.0.0",
"which": "^1.2.14"
},
"devDependencies": {
"istanbul": "0.3.16",
"jshint": "2.8.0",
"mocha": "3.2.0",
"semver": "5.1.0",
"sinon": "1.17.3",
"unexpected": "10.11.1",
"unexpected-sinon": "10.2.0",
"unexpected-stream": "2.0.3"
"eslint": "^5.7.0",
"eslint-config-pretty-standard": "^2.0.0",
"eslint-plugin-prettier": "^3.0.0",
"mocha": "^6.0.0",
"nyc": "^13.1.0",
"prettier": "^1.14.3",
"semver": "^5.3.0",
"sinon": "^7.0.0",
"unexpected": "^11.0.0",
"unexpected-sinon": "^10.7.1",
"unexpected-stream": "^3.0.0"
},
"scripts": {
"lint": "jshint .",
"test": "mocha && npm run lint",
"travis": "npm test && npm run coverage && (<coverage/lcov.info coveralls || true)",
"coverage": "NODE_ENV=development istanbul cover _mocha -- --reporter dot && echo google-chrome coverage/lcov-report/index.html"
"lint": "eslint .",
"test": "mocha",
"ci": "npm test && npm run lint && npm run coverage",
"coverage": "NODE_ENV=development nyc --reporter=lcov --reporter=text --all -- npm test && echo google-chrome coverage/lcov-report/index.html"
},

@@ -46,4 +49,9 @@ "repository": {

],
"author": "Andreas Lind <andreas@one.com>",
"license": "BSD-3-Clause"
"author": "Andreas Lind <andreaslindpetersen@gmail.com>",
"license": "BSD-3-Clause",
"nyc": {
"include": [
"lib/**"
]
}
}
/*global describe, it, __dirname, setTimeout*/
var expect = require('unexpected').clone()
.use(require('unexpected-stream'))
.use(require('unexpected-sinon'));
var sinon = require('sinon');
var JpegTran = require('../lib/JpegTran');
var Path = require('path');
var fs = require('fs');
var semver = require('semver');
const expect = require('unexpected')
.clone()
.use(require('unexpected-stream'))
.use(require('unexpected-sinon'));
const sinon = require('sinon');
const JpegTran = require('../lib/JpegTran');
const Path = require('path');
const fs = require('fs');
const semver = require('semver');
it.skipIf = function (condition) {
(condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1));
it.skipIf = function(condition) {
(condition ? it.skip : it).apply(
it,
Array.prototype.slice.call(arguments, 1)
);
};
describe('JpegTran', function () {
it('should produce a smaller file when run with -grayscale', function () {
return expect(
fs.createReadStream(Path.resolve(__dirname, 'turtle.jpg')),
'when piped through',
new JpegTran(['-grayscale']),
'to yield output satisfying',
function (resultJpegBuffer) {
expect(resultJpegBuffer.length, 'to be within', 0, 105836);
}
);
});
describe('JpegTran', () => {
it('should produce a smaller file when run with -grayscale', () =>
expect(
fs.createReadStream(Path.resolve(__dirname, 'turtle.jpg')),
'when piped through',
new JpegTran(['-grayscale']),
'to yield output satisfying',
expect.it(resultJpegBuffer => {
expect(resultJpegBuffer.length, 'to be within', 0, 105836);
})
));
it.skipIf(semver.satisfies(process.version.replace(/^v/, ''), '>=0.12.0'), 'should not emit data events while paused', function (done) {
var jpegTran = new JpegTran(['-grayscale']);
it.skipIf(
semver.satisfies(process.version.replace(/^v/, ''), '>=0.12.0'),
'should not emit data events while paused',
done => {
const jpegTran = new JpegTran(['-grayscale']);
function fail() {
done(new Error('JpegTran emitted data while it was paused!'));
}
jpegTran.pause();
jpegTran.on('data', fail).on('error', done);
function fail() {
done(new Error('JpegTran emitted data while it was paused!'));
}
jpegTran.pause();
jpegTran.on('data', fail).on('error', done);
fs.createReadStream(Path.resolve(__dirname, 'turtle.jpg')).pipe(jpegTran);
fs.createReadStream(Path.resolve(__dirname, 'turtle.jpg')).pipe(jpegTran);
setTimeout(function () {
jpegTran.removeListener('data', fail);
var chunks = [];
setTimeout(() => {
jpegTran.removeListener('data', fail);
const chunks = [];
jpegTran
.on('data', function (chunk) {
chunks.push(chunk);
})
.on('end', function () {
expect(Buffer.concat(chunks).length, 'to be within', 0, 105836);
done();
});
jpegTran
.on('data', chunk => {
chunks.push(chunk);
})
.on('end', () => {
expect(Buffer.concat(chunks).length, 'to be within', 0, 105836);
done();
});
jpegTran.resume();
}, 1000);
});
jpegTran.resume();
}, 1000);
}
);
it('should emit an error if an invalid image is processed', function (done) {
var jpegTran = new JpegTran();
jpegTran.on('error', function (err) {
done();
}).on('data', function (chunk) {
done(new Error('JpegTran emitted data when an error was expected'));
}).on('end', function (chunk) {
done(new Error('JpegTran emitted end when an error was expected'));
});
it('should emit an error if an invalid image is processed', done => {
const jpegTran = new JpegTran();
jpegTran
.on('error', err => {
done();
})
.on('data', chunk => {
done(new Error('JpegTran emitted data when an error was expected'));
})
.on('end', chunk => {
done(new Error('JpegTran emitted end when an error was expected'));
});
jpegTran.end(new Buffer('qwvopeqwovkqvwiejvq', 'utf-8'));
});
jpegTran.end(Buffer.from('qwvopeqwovkqvwiejvq', 'utf-8'));
});
it('should emit a single error if an invalid command line is specified', function (done) {
var jpegTran = new JpegTran(['-optimize', 'qcwecqweqbar']),
seenError = false;
jpegTran.on('error', function (err) {
if (seenError) {
done(new Error('More than one error event was emitted'));
} else {
seenError = true;
setTimeout(done, 100);
}
}).on('data', function (chunk) {
done(new Error('JpegTran emitted data when an error was expected'));
}).on('end', function (chunk) {
done(new Error('JpegTran emitted end when an error was expected'));
});
it('should emit a single error if an invalid command line is specified', done => {
const jpegTran = new JpegTran(['-optimize', 'qcwecqweqbar']);
jpegTran.end(new Buffer('qwvopeqwovkqvwiejvq', 'utf-8'));
});
let seenError = false;
jpegTran
.on('error', err => {
if (seenError) {
done(new Error('More than one error event was emitted'));
} else {
seenError = true;
setTimeout(done, 100);
}
})
.on('data', chunk => {
done(new Error('JpegTran emitted data when an error was expected'));
})
.on('end', chunk => {
done(new Error('JpegTran emitted end when an error was expected'));
});
describe('#destroy', function () {
it('should kill the underlying child process', function () {
var jpegTran = new JpegTran(['-grayscale']);
jpegTran.end(Buffer.from('qwvopeqwovkqvwiejvq', 'utf-8'));
});
return expect.promise(function (run) {
jpegTran.write('JFIF');
setTimeout(run(function waitForJpegTranProcess() {
var jpegTranProcess = jpegTran.jpegTranProcess;
if (jpegTran.jpegTranProcess) {
sinon.spy(jpegTranProcess, 'kill');
jpegTran.destroy();
sinon.spy(jpegTran, 'emit');
expect(jpegTranProcess.kill, 'to have calls satisfying', function () {
jpegTranProcess.kill();
});
expect(jpegTran.jpegTranProcess, 'to be falsy');
expect(jpegTran.bufferedChunks, 'to be falsy');
setTimeout(run(function () {
expect(jpegTran.emit, 'to have calls satisfying', []);
}), 10);
} else {
setTimeout(run(waitForJpegTranProcess), 0);
}
}), 0);
});
});
describe('#destroy', () => {
it('should kill the underlying child process', () => {
const jpegTran = new JpegTran(['-grayscale']);
return expect.promise(run => {
jpegTran.write('JFIF');
setTimeout(
run(function waitForJpegTranProcess() {
const jpegTranProcess = jpegTran.jpegTranProcess;
if (jpegTran.jpegTranProcess) {
sinon.spy(jpegTranProcess, 'kill');
jpegTran.destroy();
sinon.spy(jpegTran, 'emit');
expect(jpegTranProcess.kill, 'to have calls satisfying', () => {
jpegTranProcess.kill();
});
expect(jpegTran.jpegTranProcess, 'to be falsy');
expect(jpegTran.bufferedChunks, 'to be falsy');
setTimeout(
run(() => {
expect(jpegTran.emit, 'to have calls satisfying', []);
}),
10
);
} else {
setTimeout(run(waitForJpegTranProcess), 0);
}
}),
0
);
});
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc