ts-proto
ts-proto copied to clipboard
ts-proto 2.x ideas
- ESM output by default
- Migrate off long.js
- Migrate off protobufjs
- Fix
envto benode_browserinstead ofboth - Fix
useOptionalsallto bescalar_messages
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;
}
I'm using the common import here: https://github.com/aperturerobotics/protobuf-project/blob/main/example/other/other.pb.ts#L2
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.
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
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.
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!
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.
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.
Hopefully 2023 would be the year that bigints make it to protobufjs.
https://github.com/protobufjs/protobuf.js/pull/1557