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

ts-proto 2.x ideas

Open stephenh opened this issue 3 years ago • 9 comments

  • ESM output by default
  • Migrate off long.js
  • Migrate off protobufjs
  • Fix env to be node_browser instead of both
  • Fix useOptionals all to be scalar_messages

stephenh avatar Aug 06 '22 18:08 stephenh

allowed import common from a common ?

generate a common for

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
  ? T
  : T extends Long
  ? string | number | Long
  : T extends Array<infer U>
  ? Array<DeepPartial<U>>
  : T extends ReadonlyArray<infer U>
  ? ReadonlyArray<DeepPartial<U>>
  : T extends {}
  ? { [K in Exclude<keyof T, '$type'>]?: DeepPartial<T[K]> }
  : Partial<T>;

function toTimestamp(date: Date): Timestamp {
  const seconds = numberToLong(date.getTime() / 1_000);
  const nanos = (date.getTime() % 1_000) * 1_000_000;
  return { $type: 'google.protobuf.Timestamp', seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
  let millis = t.seconds.toNumber() * 1_000;
  millis += t.nanos / 1_000_000;
  return new Date(millis);
}

function fromJsonTimestamp(o: any): Date {
  if (o instanceof Date) {
    return o;
  } else if (typeof o === 'string') {
    return new Date(o);
  } else {
    return fromTimestamp(Timestamp.fromJSON(o));
  }
}

function numberToLong(number: number) {
  return Long.fromNumber(number);
}

if (_m0.util.Long !== Long) {
  _m0.util.Long = Long as any;
  _m0.configure();
}

function isSet(value: any): boolean {
  return value !== null && value !== undefined;
}

wenerme avatar Aug 16 '22 04:08 wenerme

I'm using the common import here: https://github.com/aperturerobotics/protobuf-project/blob/main/example/other/other.pb.ts#L2

paralin avatar Aug 16 '22 04:08 paralin

Allowed toJSON to omit empty ?

// sequelize
const model = await ResourceModel.create(User.toJSON(item), {
        returning: true,
});

without toJSON to omit default value, the create may fail, e.g. int64 profile_id, profile_id=0 is not exists.

wenerme avatar Aug 16 '22 04:08 wenerme

Generate with a generated header comment

// Code generated by protoc-gen-ts-proto v1.2.3. DO NOT EDIT.

this can hint some other program, e.g. ignore count code lines

wenerme avatar Aug 16 '22 04:08 wenerme

I'm using the common import here: https://github.com/aperturerobotics/protobuf-project/blob/main/example/other/other.pb.ts#L2

But still has the common functions, only a wkt used an external import.

wenerme avatar Aug 16 '22 04:08 wenerme

Migrate off long.js

Are you planing to migrate to BigInt`s?

Migrate off protobufjs

Do you want your own encoder/decoder?

Great lib, thanks for your work!

Sermelyan avatar Oct 17 '22 10:10 Sermelyan

Hey @Sermelyan ; yeah, I think BigInts would make sense.

For the encoder/decoder, no I think that's probably out-of-scope for ts-proto and I'd poke around at using like the grpc encoders or something like that. Buf ended up writing their own ESM-based encoders, so that would likely be a good candidate.

Full disclaimer I'm not actively planning on working on these personally, as I don't need them for anything I'm working on at the moment, but if others wanted to pick them up, that'd be great.

stephenh avatar Oct 23 '22 17:10 stephenh

Moving from the custom UTF8 encoding/decoding done in protobufjs to the standardised TextEncoder/TextDecoder APIs would be great as well. But this is likely covered by migrating away from protobufjs.

lgrahl avatar Dec 09 '22 21:12 lgrahl

Hopefully 2023 would be the year that bigints make it to protobufjs.

https://github.com/protobufjs/protobuf.js/pull/1557

azizghuloum avatar Dec 28 '22 02:12 azizghuloum