javascript - Graphics Magick stream() 的 stdout.on('data') 不运行;标准输出流是空的吗?
问题描述
我正在使用节点图形 Magick (gm):https ://github.com/aheckmann/gm
背景:
我在使用时遇到了一些问题,gm.stream()
我想了解如何使用它。我正在尝试使用从“数据”事件中获得的数据缓冲区stdout
上传到 AWSS3。但是,“数据”事件永远不会运行。
我做了一个简单的代码来演示这个问题。在下面的代码中,我尝试注销data
on 'data' 事件。
特定于此代码的问题:
- 我在 GM 中使用了 stream() 方法,如下所示。'事件不会发生
stdout
。data
当我运行脚本时,我的终端没有来自该事件的日志。我做错了什么? stderr
永远不会为空。stderr 总是返回一个 Socket 对象。这是正确的吗?我希望 stderr 为 null 或异常对象。
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 ---------")
})
})
终端输出:
$ 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 ---------
解决方案
推荐阅读
- node.js - Buildroot编译中标记“(”之前缺少二元运算符
- linux - 虚幻制作错误:参数“-fuse-ld=lld”中的链接器名称无效
- java - 使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置?
- pandas - 每天添加到镶木地板数据集
- c# - 开启播放器主摄像头后如何开启
- qt - X11 Inside Qt 如何在顶部窗口单击鼠标
- python - protobuf SerializeToString() 数据的 Python UTF-8 解码错误
- python - 美丽的汤自动将字符串转换为时间格式?
- ios - 如何在 POST 请求的 AFNetworking 4.0 中添加标头?
- python - 基本 Python 函数问题