-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: adhere more closely to the language guide for proto3 default va…
…lues (#66) Only write singular fields when they are non-default values (unless in repeated fields). Always write optional values even when they are non-default values. Updates test suite to compare generated bytes to protobuf.js to ensure compatibility. Also adds a README section on differences between protons and protobuf.js. BREAKING CHANGE: ts definitions will need to be generated from `.proto` files - singular message fields have become optional as message fields are always optional in proto3 fixes #43
- Loading branch information
1 parent
6ad33c5
commit 406d775
Showing
22 changed files
with
1,582 additions
and
727 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,9 @@ | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
import type { Codec } from './codec.js' | ||
import pb from 'protobufjs' | ||
|
||
const Reader = pb.Reader | ||
|
||
// monkey patch the reader to add native bigint support | ||
const methods = [ | ||
'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' | ||
] | ||
methods.forEach(method => { | ||
// @ts-expect-error | ||
const original = Reader.prototype[method] | ||
// @ts-expect-error | ||
Reader.prototype[method] = function (): bigint { | ||
return BigInt(original.call(this).toString()) | ||
} | ||
}) | ||
import { reader } from './reader.js' | ||
|
||
export function decodeMessage <T> (buf: Uint8Array | Uint8ArrayList, codec: Codec<T>): T { | ||
const reader = Reader.create(buf instanceof Uint8Array ? buf : buf.subarray()) | ||
const r = reader(buf instanceof Uint8Array ? buf : buf.subarray()) | ||
|
||
// @ts-expect-error | ||
return codec.decode(reader) | ||
return codec.decode(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pb from 'protobufjs/minimal.js' | ||
import type { Reader } from './index.js' | ||
|
||
const PBReader = pb.Reader | ||
|
||
// monkey patch the reader to add native bigint support | ||
const methods = [ | ||
'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' | ||
] | ||
methods.forEach(method => { | ||
// @ts-expect-error | ||
const original = PBReader.prototype[method] | ||
// @ts-expect-error | ||
PBReader.prototype[method] = function (): bigint { | ||
return BigInt(original.call(this).toString()) | ||
} | ||
}) | ||
|
||
export function reader (buf: Uint8Array): Reader { | ||
// @ts-expect-error class is monkey patched | ||
return PBReader.create(buf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pb from 'protobufjs/minimal.js' | ||
import type { Writer } from './index.js' | ||
|
||
const PBWriter = pb.Writer | ||
|
||
// monkey patch the writer to add native bigint support | ||
const methods = [ | ||
'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64' | ||
] | ||
methods.forEach(method => { | ||
// @ts-expect-error | ||
const original = PBWriter.prototype[method] | ||
// @ts-expect-error | ||
PBWriter.prototype[method] = function (val: bigint): pb.Writer { | ||
return original.call(this, val.toString()) | ||
} | ||
}) | ||
|
||
export function writer (): Writer { | ||
// @ts-expect-error class is monkey patched | ||
return PBWriter.create() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.