Skip to content

encoder.encode returns a Map instead of a Buffer when data is large #1

Description

@CherryDT

In case CBOR fails, a Map is unexpectedly returned from encoder.encode. This is because errors in _cborEncode are not handled, but the original object is returned.

Further debugging showed that the swallowed error from CBOR was:

Error: Push Error
    at Encoder._transform (/home/ubuntu/app/project/node_modules/cbor/lib/encoder.js:72:33)
    at Encoder.Transform._read (_stream_transform.js:186:10)
    at Encoder.Transform._write (_stream_transform.js:174:12)
    at doWrite (_stream_writable.js:387:12)
    at writeOrBuffer (_stream_writable.js:373:5)
    at Encoder.Writable.write (_stream_writable.js:290:11)
    at JSONStreamCompressEncoder._cborEncode (/home/ubuntu/app/project/node_modules/json-stream-compression/lib/encoder.js:21:24)
    at JSONStreamCompressEncoder.encode (/home/ubuntu/app/project/node_modules/json-stream-compression/lib/encoder.js:84:42)
    at saveHistory (/home/ubuntu/app/project/lib/history.js:64:27)
    at /home/ubuntu/app/project/lib/history.js:131:4

Example:

const { Encoder } = require('json-stream-compression')
const arr = []
for(let i = 0; i < 20000; i++) arr.push(1)
const result = new Encoder().encode(arr) // throws error

This actually comes from the fact that you are exploiting the stream buffer here instead of processing incoming data in the stream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions