victorquinn/memcache-plus

Nested `Buffer` objects don't serialize correctly

Opened this issue · 1 comments

If I cache data that contains a nested Buffer object, such as [Buffer, string], then inside of formatValue() the Buffer.isBuffer(val) check returns false and we fall into the JSON.stringify(val) path:

if (typeof val === 'number') {
value = val.toString();
} else if (Buffer.isBuffer(val)) {
value = val.toString('binary');
} else if (typeof val !== 'string') {
value = JSON.stringify(val);
}

And this results in a JSON string that, when parsed, produces an object that looks like:

[
  {
    type: "Buffer",
    data: "...",
  },
  string
]

which of course is not the same shape as the [Buffer, string] object that was originally registered, which is I believe a bug.

Perhaps something like https://www.npmjs.com/package/msgpackr could work better than JSON.stringify() here?