Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
{
"name": "George Gershevich",
"email": "george.gershevich@gmail.com"
},
{
"name": "Loren Yu",
"email": "loren@navapbc.com"
}
],
"keywords": [
Expand Down
11 changes: 5 additions & 6 deletions src/ghostscript4js.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ void GhostscriptManager::Init()
code = gsapi_new_instance(&minst, NULL);
if (code < 0)
{
throw "Sorry error happened creating Ghostscript instance. Error code: " + to_string(code);
throw std::runtime_error("Sorry error happened creating Ghostscript instance. Error code: " + to_string(code));
}
gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
code = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_UTF8);
if (code < 0)
{
throw "Sorry error happened in setting the encoding for Ghostscript interpreter. Error code: " + to_string(code);
throw std::runtime_error("Sorry error happened in setting the encoding for Ghostscript interpreter. Error code: " + to_string(code));
}
}

Expand All @@ -99,7 +99,7 @@ void GhostscriptManager::Execute(int gsargc, char *gsargv[])
code = gsapi_init_with_args(minst, gsargc, gsargv);
if (code < 0 && code != gs_error_Quit)
{
throw "Sorry error happened executing Ghostscript command. Error code: " + to_string(code);
throw std::runtime_error("Sorry error happened executing Ghostscript command. Error code: " + to_string(code));
}
DecreaseWorkers();
if (workers == 0)
Expand All @@ -118,7 +118,7 @@ void GhostscriptManager::Exit()
code = gsapi_exit(minst);
if (code < 0 && code != gs_error_Quit)
{
throw "Sorry error happened during the exit from the Ghostscript interpreter. Error code: " + to_string(code);
throw std::runtime_error("Sorry error happened during the exit from the Ghostscript interpreter. Error code: " + to_string(code));
}
}

Expand Down Expand Up @@ -162,12 +162,11 @@ class GhostscriptWorker : public Napi::AsyncWorker
GhostscriptManager *gm = GhostscriptManager::GetInstance();
gm->Execute(gsargc, gsargv);
delete[] gsargv;

}
catch (exception &e)
{
delete[] gsargv;
SetError(Napi::String::New(Env(), e.what()));
SetError(e.what());
}
}

Expand Down
21 changes: 18 additions & 3 deletions test/ghostscript4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const fs = require('fs')
process.chdir(__dirname)

const pdf = 'node-love-ghostscript.pdf'
// Invalid PDF file by taking a text file with Lorem ipsum text and renaming to have .pdf extension
const pdfInvalid = 'invalid.pdf'
const ps = 'node-love-ghostscript.ps'
const pngSync = 'node-love-ghostscript-sync.png'
const pngAsync = 'node-love-ghostscript-async.png'
Expand All @@ -43,6 +45,7 @@ const cmdAsyncPng = `-psconv -q -dNOPAUSE -sDEVICE=pngalpha -o ${pngAsync} -sDEV
const cmdSyncPngArray = ['-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pngalpha', '-o', `${pngSyncArray}`, '-sDEVICE=pngalpha', '-r144', `${pdf}`]
const cmdAsyncPngArray = ['-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pngalpha', '-o', `${pngAsyncArray}`, '-sDEVICE=pngalpha', '-r144', `${pdf}`]

const cmdInvalidPdf = `-q -dNOPAUSE -sDEVICE=jpeg -o test/out-%02d.jpg -r144 ${pdfInvalid}`
const cmdSyncPdf = `-psconv -q -dNOPAUSE -sDEVICE=pdfwrite -o ${pdfSync} -f ${ps}`
const cmdAsyncPdf = `-psconv -q -dNOPAUSE -sDEVICE=pdfwrite -o ${pdfAsync} -f ${ps}`
const cmdSyncPdfArray = ['-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pdfwrite', '-o', `${pdfSyncArray}`, '-f', `${ps}`]
Expand All @@ -65,7 +68,8 @@ describe('Test ghostscript4js', function () {
expect(gs.version).not.toThrow()
const version = gs.version()
expect(version.product).toContain('GPL Ghostscript')
expect(version.copyright).toContain('Copyright (C) 2017 Artifex Software, Inc. All rights reserved.')
expect(version.copyright).toContain('Copyright (C)')
expect(version.copyright).toContain('Artifex Software, Inc. All rights reserved.')
expect(version.product).not.toBeLessThan(gs.MIN_SUPPORTED_REVISION)
expect(version.product).not.toBeLessThan(20160323)
})
Expand Down Expand Up @@ -150,13 +154,25 @@ describe('Test ghostscript4js', function () {
})
})

it('Should execute Ghostscript command asynchronous without parameters and fail', function () {
it('Should throw an error if ghostscript command returns nonzero exit code', function (done) {
gs.execute(cmdInvalidPdf)
.then(() => {
done(new Error ('Promise should not be resolved'));
})
.catch((err) => {
expect(() => { throw err }).toThrowError('Sorry error happened executing Ghostscript command. Error code: -100')
done();
})
})

it('Should execute Ghostscript command asynchronous without parameters and fail', function (done) {
gs.execute()
.then(() => {
done(new Error ('Promise should not be resolved'));
})
.catch((err) => {
expect(() => { throw err }).toThrowError('Sorry method\'s argument should be a string or an array of strings');
done();
})
})

Expand All @@ -178,5 +194,4 @@ describe('Test ghostscript4js', function () {
done()
})
})*/

})
Binary file added test/invalid.pdf
Binary file not shown.