首页 > 解决方案 > Graphics Magick stream() 的 stdout.on('data') 不运行;标准输出流是空的吗?

问题描述

我正在使用节点图形 Magick (gm):https ://github.com/aheckmann/gm

背景:

我在使用时遇到了一些问题,gm.stream()我想了解如何使用它。我正在尝试使用从“数据”事件中获得的数据缓冲区stdout上传到 AWSS3。但是,“数据”事件永远不会运行。

我做了一个简单的代码来演示这个问题。在下面的代码中,我尝试注销dataon 'data' 事件。

特定于此代码的问题:

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 ---------

标签: javascriptnode.jsiogm

解决方案


推荐阅读