mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

Invalid Avro header does not raise error event #448

Open craxal opened 7 months ago

craxal commented 7 months ago

I discovered some mysterious behavior while using BlockDecoder. If the header is invalid (!tap.isVaid()), the stream does not raise an error event. Instead, it just returns.

This was confusing, because I was expecting an error event that I could process. I can work around this by listening for the "metadata" event and storing the parsed schema in a variable then testing to see if this variable is defined when the stream ends.

I'm not sure if this is intended behavior, but I would prefer if any invalid input would raise an error that can be handled rather than silently completing.

mtth commented 7 months ago

Agreed that it's surprising behavior for the stream to not emit an error if it is ended without a valid header. (Until that happens, the tap.isValid return on failed check is expected: it just means the header is missing data.)

Until this is fixed, your workaround sounds right. To contain complexity on your end (and make migration easier later on) you could isolate the logic in a thin factory function. Something like (untested):

function newBlockDecoder(...args) {
  let truncated = true;
  return new avro.streams.BlockDecoder(...args)
    .on('finish', function () { if (truncated) { this.emit('error', new Error('Invalid header')); }})
    .on('metadata', () => { truncated = false; });
}