Skip to content

Commit d35c703

Browse files
committed
test: add test for PR 644
1 parent 7e8c02a commit d35c703

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/pdf.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,17 @@ PDF.prototype.exec = function PdfExec (callback) {
123123
// Ignore if code has a value of 0 since that means PhantomJS has executed and exited successfully.
124124
// Also, as per your script and standards, having a code value of 1 means one can always assume that
125125
// an error occured.
126-
if (((typeof code !== 'undefined' && code !== null) && code !== 0) || err) {
126+
if (((typeof code !== 'undefined' && code !== null) && code !== 0) || err || (code === 0 && !data)) {
127127
var error = null
128128

129129
if (err) {
130130
// Rudimentary checking if err is an instance of the Error class
131131
error = err instanceof Error ? err : new Error(err)
132+
} else if (code === 0 && !data) {
133+
// This is to catch the edge case of having an exit code value of 0 but not having data (exit can be called before io pipes are closed)
134+
error = new Error('html-pdf: Process exited successfully, but no data received')
132135
} else {
133-
// This is to catch the edge case of having a exit code value of 1 but having no error
136+
// This is to catch the edge case of having an exit code value of 1 but having no error
134137
error = new Error('html-pdf: Unknown Error')
135138
}
136139

@@ -150,7 +153,7 @@ PDF.prototype.exec = function PdfExec (callback) {
150153

151154
// An exit event is most likely an error because we didn't get any data at this point
152155
child.on('close', respond)
153-
child.on('exit', respond)
156+
// child.on('exit', respond)
154157

155158
var config = JSON.stringify({html: this.html, options: this.options})
156159
child.stdin.write(config + '\n', 'utf8')

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,13 @@ test('allows local file access with localUrlAccess=true', function (t) {
258258
t.assert(count === 5, 'Renders a page 5 pages as the content is present')
259259
})
260260
})
261+
262+
test('phantomjs exit without file generated does not cause crash', function (t) {
263+
t.plan(2)
264+
265+
pdf.create(`<body>foo</body>`, { phantomPath: './test/phantomMock.js' })
266+
.toBuffer(function (error, buffer) {
267+
t.true(error instanceof Error)
268+
t.false(buffer)
269+
})
270+
})

test/phantomMock.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
process.exit(0)

0 commit comments

Comments
 (0)