cabal-club / cabal-core

Core database and replication for cabal.
GNU Affero General Public License v3.0
303 stars 43 forks source link

duplexify must be in object mode for mod streams #95

Closed garbados closed 4 years ago

garbados commented 4 years ago

This is a WIP PR because I am unfamiliar with the testing suite, so it only fixes my problem presently.

The issue is that if you treat cabal.moderation.list as a read stream instead of utilizing its callback, you get this:

Uncaught TypeError: The "chunk" argument must be one of type string, Buffer, or Uint8Array. Received type object
      at chunkInvalid (node_modules/readable-stream/lib/_stream_readable.js:313:10)
      at readableAddChunk (node_modules/readable-stream/lib/_stream_readable.js:258:31)
      at Duplexify.Readable.push (node_modules/readable-stream/lib/_stream_readable.js:241:10)
      at Duplexify._forward (node_modules/duplexify/index.js:172:26)
      at Readable.onreadable (node_modules/duplexify/index.js:136:10)
      at emitReadable_ (node_modules/read-only-stream/node_modules/readable-stream/lib/_stream_readable.js:504:10)
      at emitReadable (node_modules/read-only-stream/node_modules/readable-stream/lib/_stream_readable.js:498:62)
      at addChunk (node_modules/read-only-stream/node_modules/readable-stream/lib/_stream_readable.js:298:29)
      at readableAddChunk (node_modules/read-only-stream/node_modules/readable-stream/lib/_stream_readable.js:278:11)
      at Readable.push (node_modules/read-only-stream/node_modules/readable-stream/lib/_stream_readable.js:245:10)
      at Readable.ro._read (node_modules/read-only-stream/index.js:22:16)
      at DestroyableTransform.<anonymous> (node_modules/read-only-stream/index.js:15:16)
      at emitReadable_ (node_modules/readable-stream/lib/_stream_readable.js:538:12)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

Here's an excerpt from the source that triggers the issue:

const modStream = cabal.moderation.list()
modStream.on('data', (action) => {
  console.log(action)
})

The problem is, apparently, that duplexify must be in object mode to handle the objects produced by the stream. This is accomplished by changing duplexify() into duplexify.obj()

garbados commented 4 years ago

PR updated to include a test.

ghost commented 4 years ago

Thanks for the patch! This has been merged into 13.0.2.