ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

Use BufferWriter type in env=node

Open lgrahl opened this issue 2 years ago • 3 comments

Considering that --ts_proto_opt=env=node enables the Buffer type for any bytes, it should also output Buffer when encoding a protobuf message to bytes (i.e. calling Writer.finish). protobufjs should already use a BufferWriter underneath anyways. It's just the type that needs fixing.

This should be easily fixable by explicitly using a BufferWriter instead of a Writer when generating the code.

lgrahl avatar Dec 09 '22 16:12 lgrahl

I'm a little worried about the assumption that protobufjs would always use a BufferWriter, but if you're 100% sure / that is what their return types / docs say anyway, then sgtm.

Feel free to submit a PR if you'd like to see this change; if you think its 100% safe then we don't need a new flag/env setting, so it should be pretty straight forward. Thanks!

stephenh avatar Dec 09 '22 17:12 stephenh

Not exactly 100% because you can technically configure it at runtime by overriding the Writer global which means you can also override the BufferWriter global. But those runtime overrides are impossible to match with typing. If the env=node configuration explicitly uses BufferWriter, I'd say that it's as close to the type as it can get.

I'll see if I can find some time do it. But in case anyone else runs into this (for me it was another API requiring a Buffer and you want to spare yourself a copy operation), feel free to pick this up. :slightly_smiling_face:

lgrahl avatar Dec 09 '22 20:12 lgrahl

You can always use Buffer.from(uint8Array.buffer) docs at nodejs.org

davidzeng0 avatar Mar 26 '23 11:03 davidzeng0