From 0707bd51df9be8476f4ba48024c08d529468f832 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 21:39:15 +0200 Subject: [PATCH 1/9] Add some more valid options --- prince-api.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/prince-api.js b/prince-api.js index a8fc003..c03aeb9 100644 --- a/prince-api.js +++ b/prince-api.js @@ -87,7 +87,9 @@ var princeOptions = { "disallow-copy": false, "disallow-annotate": false, "disallow-modify": false, - "scanfonts": false + "scanfonts": false, + "structured-log": false, + "debug": false }; /* API constructor */ From 5efc0a926c622b8f6ed66542a1de37ac6ed017a3 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 21:41:14 +0200 Subject: [PATCH 2/9] Spawn process instead of execFile, and facilitate progress tracking and logging on the fly --- prince-api.js | 64 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/prince-api.js b/prince-api.js index c03aeb9..219d868 100644 --- a/prince-api.js +++ b/prince-api.js @@ -110,6 +110,10 @@ function Prince (options) { output: "" }; + + /* array for holding event handlers */ + this.eventHandlers = []; + /* override defaults with more reasonable information about environment */ var install = [ { basedir: "prince/lib/prince", binary: "bin/prince" }, @@ -216,6 +220,19 @@ Prince.prototype.option = function (name, value, forced) { return this; }; +/* register event handlers */ +Prince.prototype.on = function (eventName, fn) { + var eventNames = ['stdout', 'stderr']; + if (eventNames.indexOf(eventName) === -1) { + throw new Error("Uknkown event name. Events must be one of '"+eventNames.join("', '")+"'"); + } + var eventHandler = {eventName: eventName, fn: fn} + if (this.eventHandlers.indexOf(eventHandler) === -1) { + this.eventHandlers.push(eventHandler); + } + return this; +}; + /* execute the CLI binary */ Prince.prototype._execute = function (method, args) { /* determine path to prince(1) binary */ @@ -243,17 +260,41 @@ Prince.prototype._execute = function (method, args) { var options = {}; options.timeout = self.config.timeout; options.cwd = self.config.cwd; - child_process.execFile(prog, args, options, - function (error, stdout, stderr) { - var m; - if (error === null && (m = stderr.match(/prince:\s+error:\s+([^\n]+)/))) - reject({ error: m[1], stdout: stdout, stderr: stderr }); - else if (error !== null) - reject({ error: error, stdout: stdout, stderr: stderr }); - else - resolve({ stdout: stdout, stderr: stderr }); - } - ); + + var _stdout = ""; + var _stderr = ""; + var princeProcess = child_process.spawn(prog, args, options); + var stdoutHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stdout' }) + var stderrHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stderr' }) + + function stdParse(handler) { + (""+this.data).split('\n').map(function(line) { + handler.fn(line) + }) + }; + + princeProcess.stdout.on("data", function(data) { + stdoutHandlers.map(stdParse.bind({data: data})) + _stdout += data; + }); + + princeProcess.stderr.on("data", function(data) { + stderrHandlers.map(stdParse.bind({data: data})) + _stderr += data; + }); + + princeProcess.on("error", function(err) { + reject({ error: err, stdout: _stdout, stderr: _stderr }); + }); + + princeProcess.on("close", function(code) { + if (code !== 0) { + var error = new Error("Prince process exited with status code: "+code); + error.code = code; + return reject({ error: error, stdout: _stdout, stderr: _stderr }); + } + resolve({ stdout: _stdout, stderr: _stderr }); + }); } catch (exception) { reject({ error: exception, stdout: "", stderr: "" }); @@ -289,4 +330,3 @@ Prince.prototype.execute = function () { /* export API constructor */ module.exports = Prince; - From 733b90247073d5d5538e45498ccc2def2903b5f4 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 22:09:39 +0200 Subject: [PATCH 3/9] Just a better function name --- prince-api.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prince-api.js b/prince-api.js index 219d868..3b98fdc 100644 --- a/prince-api.js +++ b/prince-api.js @@ -267,19 +267,19 @@ Prince.prototype._execute = function (method, args) { var stdoutHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stdout' }) var stderrHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stderr' }) - function stdParse(handler) { + function handleData(handler) { (""+this.data).split('\n').map(function(line) { - handler.fn(line) + handler.fn(line); }) }; princeProcess.stdout.on("data", function(data) { - stdoutHandlers.map(stdParse.bind({data: data})) + stdoutHandlers.map(handleData.bind({data: data})); _stdout += data; }); princeProcess.stderr.on("data", function(data) { - stderrHandlers.map(stdParse.bind({data: data})) + stderrHandlers.map(handleData.bind({data: data})); _stderr += data; }); From 305fb3a9077b64959de007d6668b69fbbe8323ad Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 23:37:38 +0200 Subject: [PATCH 4/9] Improve code for registering and executing event handlers --- prince-api.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/prince-api.js b/prince-api.js index 3b98fdc..849b76c 100644 --- a/prince-api.js +++ b/prince-api.js @@ -112,7 +112,7 @@ function Prince (options) { /* array for holding event handlers */ - this.eventHandlers = []; + this.eventHandlers = {stdout: [], stderr: []}; /* override defaults with more reasonable information about environment */ var install = [ @@ -222,13 +222,13 @@ Prince.prototype.option = function (name, value, forced) { /* register event handlers */ Prince.prototype.on = function (eventName, fn) { - var eventNames = ['stdout', 'stderr']; - if (eventNames.indexOf(eventName) === -1) { - throw new Error("Uknkown event name. Events must be one of '"+eventNames.join("', '")+"'"); + if (Object.keys(this.eventHandlers).indexOf(eventName) === -1) { + throw new Error("Unknown event name. Event must be one of '" + + Object.keys(this.eventHandlers).join("', '") + "'"); } - var eventHandler = {eventName: eventName, fn: fn} - if (this.eventHandlers.indexOf(eventHandler) === -1) { - this.eventHandlers.push(eventHandler); + var alreadyAdded = this.eventHandlers[eventName].indexOf(fn) > -1 + if (!alreadyAdded) { + this.eventHandlers[eventName].push(fn); } return this; }; @@ -264,22 +264,26 @@ Prince.prototype._execute = function (method, args) { var _stdout = ""; var _stderr = ""; var princeProcess = child_process.spawn(prog, args, options); - var stdoutHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stdout' }) - var stderrHandlers = self.eventHandlers.filter(function(eventHandler) { return eventHandler.eventName === 'stderr' }) - function handleData(handler) { - (""+this.data).split('\n').map(function(line) { - handler.fn(line); + function callLineByLine(fn, data) { + (""+data).split("\n").map(function(line) { + if (line) { + fn(line); + } }) }; princeProcess.stdout.on("data", function(data) { - stdoutHandlers.map(handleData.bind({data: data})); + self.eventHandlers.stdout.map(function(fn) { + callLineByLine(fn, data); + }) _stdout += data; }); princeProcess.stderr.on("data", function(data) { - stderrHandlers.map(handleData.bind({data: data})); + self.eventHandlers.stderr.map(function(fn) { + callLineByLine(fn, data); + }); _stderr += data; }); From 094adaef9ad0617269514c495fdfbc79d6580508 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 23:46:24 +0200 Subject: [PATCH 5/9] Minor code enhancement --- prince-api.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prince-api.js b/prince-api.js index 849b76c..1bf9032 100644 --- a/prince-api.js +++ b/prince-api.js @@ -222,9 +222,10 @@ Prince.prototype.option = function (name, value, forced) { /* register event handlers */ Prince.prototype.on = function (eventName, fn) { - if (Object.keys(this.eventHandlers).indexOf(eventName) === -1) { + var eventNames = Object.keys(this.eventHandlers); + if (eventNames.indexOf(eventName) === -1) { throw new Error("Unknown event name. Event must be one of '" - + Object.keys(this.eventHandlers).join("', '") + "'"); + + eventNames.join("', '") + "'"); } var alreadyAdded = this.eventHandlers[eventName].indexOf(fn) > -1 if (!alreadyAdded) { From 1e05b537cfa06c75e7cc5343adba1796340f6f0a Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Mon, 2 May 2016 23:52:52 +0200 Subject: [PATCH 6/9] Minor code enhancement --- prince-api.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/prince-api.js b/prince-api.js index 1bf9032..76bb246 100644 --- a/prince-api.js +++ b/prince-api.js @@ -222,10 +222,9 @@ Prince.prototype.option = function (name, value, forced) { /* register event handlers */ Prince.prototype.on = function (eventName, fn) { - var eventNames = Object.keys(this.eventHandlers); - if (eventNames.indexOf(eventName) === -1) { + if (!this.eventHandlers.hasOwnProperty(eventName)) { throw new Error("Unknown event name. Event must be one of '" - + eventNames.join("', '") + "'"); + + Object.keys(this.eventHandlers).join("', '") + "'"); } var alreadyAdded = this.eventHandlers[eventName].indexOf(fn) > -1 if (!alreadyAdded) { From e256801fbb09a5f20383b7c0e9a6324a715e31d2 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Tue, 3 May 2016 00:18:05 +0200 Subject: [PATCH 7/9] Make it possible to unregister event handlers --- prince-api.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/prince-api.js b/prince-api.js index 76bb246..6f7e5e0 100644 --- a/prince-api.js +++ b/prince-api.js @@ -233,6 +233,17 @@ Prince.prototype.on = function (eventName, fn) { return this; }; +/* unregister event handlers */ +Prince.prototype.off = function (eventName, fn) { + if (!this.eventHandlers.hasOwnProperty(eventName)) { + throw new Error("Unknown event name. Event must be one of '" + + Object.keys(this.eventHandlers).join("', '") + "'"); + } + var index = this.eventHandlers[eventName].indexOf(fn) + this.eventHandlers[eventName].splice(index, 1) + return this; +}; + /* execute the CLI binary */ Prince.prototype._execute = function (method, args) { /* determine path to prince(1) binary */ @@ -265,24 +276,25 @@ Prince.prototype._execute = function (method, args) { var _stderr = ""; var princeProcess = child_process.spawn(prog, args, options); - function callLineByLine(fn, data) { + function callLineByLine(eventName, fn, data) { (""+data).split("\n").map(function(line) { - if (line) { - fn(line); + var stillRegistered = self.eventHandlers[eventName].indexOf(fn) > -1; + if (line && stillRegistered) { + fn(line, self); } }) }; princeProcess.stdout.on("data", function(data) { self.eventHandlers.stdout.map(function(fn) { - callLineByLine(fn, data); + callLineByLine('stdout', fn, data); }) _stdout += data; }); princeProcess.stderr.on("data", function(data) { self.eventHandlers.stderr.map(function(fn) { - callLineByLine(fn, data); + callLineByLine('stderr', fn, data); }); _stderr += data; }); From 4c6293093aa8929f6f97adfbbfad19e343a649cf Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Tue, 3 May 2016 00:26:23 +0200 Subject: [PATCH 8/9] Fix tab spaces to conform with the rest of the script --- prince-api.js | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/prince-api.js b/prince-api.js index 6f7e5e0..3885c2f 100644 --- a/prince-api.js +++ b/prince-api.js @@ -224,11 +224,11 @@ Prince.prototype.option = function (name, value, forced) { Prince.prototype.on = function (eventName, fn) { if (!this.eventHandlers.hasOwnProperty(eventName)) { throw new Error("Unknown event name. Event must be one of '" - + Object.keys(this.eventHandlers).join("', '") + "'"); + + Object.keys(this.eventHandlers).join("', '") + "'"); } var alreadyAdded = this.eventHandlers[eventName].indexOf(fn) > -1 if (!alreadyAdded) { - this.eventHandlers[eventName].push(fn); + this.eventHandlers[eventName].push(fn); } return this; }; @@ -237,7 +237,7 @@ Prince.prototype.on = function (eventName, fn) { Prince.prototype.off = function (eventName, fn) { if (!this.eventHandlers.hasOwnProperty(eventName)) { throw new Error("Unknown event name. Event must be one of '" - + Object.keys(this.eventHandlers).join("', '") + "'"); + + Object.keys(this.eventHandlers).join("', '") + "'"); } var index = this.eventHandlers[eventName].indexOf(fn) this.eventHandlers[eventName].splice(index, 1) @@ -277,39 +277,39 @@ Prince.prototype._execute = function (method, args) { var princeProcess = child_process.spawn(prog, args, options); function callLineByLine(eventName, fn, data) { - (""+data).split("\n").map(function(line) { - var stillRegistered = self.eventHandlers[eventName].indexOf(fn) > -1; - if (line && stillRegistered) { - fn(line, self); - } - }) + (""+data).split("\n").map(function(line) { + var stillRegistered = self.eventHandlers[eventName].indexOf(fn) > -1; + if (line && stillRegistered) { + fn(line, self); + } + }) }; princeProcess.stdout.on("data", function(data) { - self.eventHandlers.stdout.map(function(fn) { - callLineByLine('stdout', fn, data); - }) - _stdout += data; + self.eventHandlers.stdout.map(function(fn) { + callLineByLine('stdout', fn, data); + }) + _stdout += data; }); princeProcess.stderr.on("data", function(data) { - self.eventHandlers.stderr.map(function(fn) { - callLineByLine('stderr', fn, data); - }); - _stderr += data; + self.eventHandlers.stderr.map(function(fn) { + callLineByLine('stderr', fn, data); + }); + _stderr += data; }); princeProcess.on("error", function(err) { - reject({ error: err, stdout: _stdout, stderr: _stderr }); + reject({ error: err, stdout: _stdout, stderr: _stderr }); }); princeProcess.on("close", function(code) { - if (code !== 0) { - var error = new Error("Prince process exited with status code: "+code); - error.code = code; - return reject({ error: error, stdout: _stdout, stderr: _stderr }); - } - resolve({ stdout: _stdout, stderr: _stderr }); + if (code !== 0) { + var error = new Error("Prince process exited with status code: "+code); + error.code = code; + return reject({ error: error, stdout: _stdout, stderr: _stderr }); + } + resolve({ stdout: _stdout, stderr: _stderr }); }); } catch (exception) { From 09e3688d390479765ccba2e98f3ac16433d658c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20Knutsen?= Date: Fri, 7 Jun 2019 20:06:53 +0200 Subject: [PATCH 9/9] Add -o flag for output file This is needed in newer versions of Prince --- prince-api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/prince-api.js b/prince-api.js index 3885c2f..c29f8bd 100644 --- a/prince-api.js +++ b/prince-api.js @@ -338,6 +338,7 @@ Prince.prototype.execute = function () { this.config.inputs.forEach(function (input) { args.push(input); }); + args.push('-o'); args.push(this.config.output); /* return promise for executing CLI */