Closed noway closed 2 years ago
This is likely because the Buffer
implementation you're using outside of the WebPack'd code is different from the one inside. The one inside got mangled, and the one outside didn't, so when node-cbor is trying to find out "is this a Buffer?" it doesn't get a match. This leads to the buffer being encoded as a plain object, which, if I recall correctly, serializes it as a string with comma-delimited decimal numbers, one per byte -- which will be much larger than the correct serialization.
To fix this, either pack all of your code together, or export the Buffer implementation from inside the packed code, then use that in your unpacked code. There are likely other ways to fix it, but let's see if this hypothesis proves out first.
You're right, Buffer.name
gets mangled and the encoding can't find a proper converter. I'll make a PR.
Can you try passing in a genTypes
option into the Encoder instance?
Encoder.encodeOne(Buffer.alloc(2), { genTypes: [Buffer, Encoder._pushBuffer] });
and if that doesn't work, try:
Encoder.encodeOne(Buffer.alloc(2), { genTypes: [Buffer.name, Encoder._pushBuffer] });
That worked! Passing in my implementation of Buffer from import { Buffer } from "buffer"
package using genTypes worked for me. Now I don't need the patch in https://github.com/hildjj/node-cbor/pull/166
I gonna close that pull request since this workaround solves it.
Here's how i've done it: https://github.com/vaxxnz/nzcp-js/pull/60
Webpack minimize (https://webpack.js.org/configuration/optimization/#optimizationminimize, uses Terser) causes cbor.encode to produce a different value from when no minimize is applied.
I'm working on a minimum reproducable example for this, but essentially production build generates Uint8Array of 1661 length, and development build generates Uint8Array of 314 length.
I've pinpointed the bug to switching between
optimization.minimize: false
andoptimization.minimize: true
in my webpack config.I'm working on a minimum reproducable example, but does this issue ring a bell? I searched github issues a bit, but there's nothing that precisely fits the bill.
I've put together the test case that fails for me on production/development builds.