Reputation: 628
I am using node Graphics Magick (gm): https://github.com/aheckmann/gm
Background:
I am having some trouble with using gm.stream()
and I would like to understand how to use it. I am attempting to use the data buffer that I get from the "data" event of stdout
to upload to AWSS3. However, the "data" event never runs.
I made a simple code to demonstrate this problem. In the code below, I attempted to log out data
in the on 'data' event.
Questions specific to this code:
stdout
's data
event does not occur. My terminal has no logs from that event when I run the script. What have I done wrong?stderr
is never null. stderr always returns a Socket object. Is this right? I expected stderr to be null or an exception object.const gm = require('gm').subClass({imageMagick: true});
const quality = 0;
const format = "jpg";
const width = 768;
const height = 512;
const density = 72;
// const savePath = "./";
// const saveFilename = "untitled";
const compression = "jpeg";
gm('./helloworld.pdf')
.background('white')
.flatten() // merges all layers into the backgroud
.density(density, density)
.resize(width, height, "!")
.quality(quality) // dictates the quality of compression in the compress() step
.compress(compression) // compresses the image
.stream(format, (err, stdout, stderr) =>{
if(err) {
console.log("err", err)
throw err;
}
if(stderr) {
console.log("stderr", stderr)
// throw stderr;
}
console.log("stdout", stdout)
stdout.on('data', (data) => {
console.log(">>>> getting data .....") // never happens, see TERMINAL OUTPUT
console.log(data)
})
stdout.on('end', () => {
console.log("--------- END OF FILE ---------")
})
})
TERMINAL OUTPUT:
$ node script5.js
stderr Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
readable: true,
_events: [Object: null prototype] {
end: [Function: onReadableStreamEnd],
close: [Function]
},
_eventsCount: 2,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: {
next: null,
entry: null,
finish: [Function: bound onCorkedFinish]
}
},
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 7,
[Symbol(kHandle)]: Pipe { reading: true, [Symbol(owner)]: [Circular] },
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
}
stdout Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
readable: true,
_events: [Object: null prototype] {
end: [Function: onReadableStreamEnd],
close: [Function]
},
_eventsCount: 2,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: {
next: null,
entry: null,
finish: [Function: bound onCorkedFinish]
}
},
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 6,
[Symbol(kHandle)]: Pipe { reading: true, [Symbol(owner)]: [Circular] },
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
}
--------- END OF FILE ---------
Upvotes: 1
Views: 233