javascriptnode.jsarraysbuffer

Convert object to buffer and buffer to object


Demo

const result =    [
  {                                  // This is sample data BUT there can be multiple objects
    "name": "object1",
    "id": "1",
    "timeseriesData": [
      [
        1548979200000,                // epoc timestamp
        325.3                         // value in number
      ],
      [
        1548979260000,
        325.8
      ],
      [
        1548979320000,
        324.2
      ],
      [
        1548979380000,
        323.7
      ],
      [
        1548979440000,
        322.3
      ],
      [
        1548979500000,
        322
      ]
    ]
  },
  {
    "name": "object2",
    "id": "2",
    "timeseriesData": [
      [
        1548989200000,
        625.3
      ],
      [
        1548989260000,
        625.8
      ],
      [
        1548989320000,
        624.2
      ],
      [
        1548989380000,
        623.7
      ],
      [
        1548989440000,
        622.3
      ],
      [
        1548989500000,
        622
      ]
    ]
  }
]

const splitInChunks = (sizeInBytes) =>   // Don't think this is helping to create proper chunks
    (buffer) => {
        console.log(buffer);
        const size = Buffer.byteLength(buffer.buffer);

        let start = 0;
        let end = sizeInBytes;

        const chunks = [];

        do {
            chunks.push(buffer.subarray(start, end));
            start += sizeInBytes;
            end += sizeInBytes;
        } while (start < size);

        return chunks;
    };
    
   
const chunks = splitInChunks(32)(Buffer.from(JSON.stringify(result)));
console.log("chunks length", chunks.length)
console.log('chunks[0]-', chunks[0])
console.log(JSON.parse(Buffer.concat(chunks)))

For some reason, I want to create buffer of each chunk with some additional data as shownbelow and store it in converted variable. But for some reason, I can't get the original object back from converted varaible.

const converted = chunks.map(c=> Buffer.from(JSON.stringify({requestId:"123", buf: c})));
console.log("converted length", converted.length)
const result1 = [];
for (let con of converted) {
  const eachChunkBuf = JSON.parse(con);
  result1.push(eachChunkBuf.buf)
}
console.log('result1', result1)
console.log(JSON.parse(Buffer.concat(result1)))  // I want result1 to print original object but it doesn't work.

Solution

  • The error in the console indicates that it's the Buffer.concat function which is unhappy, expecting an array of Buffers but instead getting Objects.

    I think the error is coming from this bit of your code:

    for (let con of converted) {
      const eachChunkBuf = JSON.parse(con);
      result1.push(eachChunkBuf.buf)
    }
    

    When you parse the JSON it is not preserving the type of the buffer. So you just need to make sure that eachChunkBuf.buf is in fact a buffer before pushing back to results1.

    for (let con of converted) {
      const eachChunkBuf = JSON.parse(con);
      result1.push(Buffer.from(eachChunk.buf));
    }