From 8d75746afc10db7df53bac04b7c1e3e432571dba Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 15 Sep 2026 16:17:36 +0200 Subject: [PATCH 1/7] feat!: accept any Uint8Array for encoding Split messages into decoder and encoder types - any `Uint8Array` fields will be accepted for encoding as `Uint8Array` but returned decoded as `Uint8Array` since this is what they are and they can then be passed to onwards APIs without conversion. BREAKING CHANGE: definitions will need to be regenerated --- .../src/implementations/protons/bench.ts | 66 +++-- packages/protons-runtime/src/codec.ts | 10 +- packages/protons-runtime/src/codecs/enum.ts | 4 +- .../protons-runtime/src/codecs/message.ts | 2 +- packages/protons-runtime/src/decode.ts | 2 +- packages/protons-runtime/src/encode.ts | 2 +- packages/protons-runtime/src/index.ts | 2 +- packages/protons-runtime/src/stream.ts | 2 +- packages/protons/src/fields/array-field.ts | 8 +- packages/protons/src/fields/field.ts | 15 +- packages/protons/src/fields/map-field.ts | 17 +- packages/protons/src/fields/message-field.ts | 8 +- packages/protons/src/types/enum.ts | 16 +- packages/protons/src/types/index.ts | 7 +- packages/protons/src/types/message.ts | 72 +++-- packages/protons/src/types/module.ts | 91 ++++-- packages/protons/src/types/primitive.ts | 6 +- packages/protons/test/fixtures/basic.ts | 23 +- packages/protons/test/fixtures/bitswap.ts | 75 +++-- packages/protons/test/fixtures/circuit.ts | 32 +- .../test/fixtures/custom-option-jstype.ts | 64 +++- packages/protons/test/fixtures/daemon.ts | 276 +++++++++++++----- packages/protons/test/fixtures/dht.ts | 51 +++- packages/protons/test/fixtures/maps.ts | 96 ++++-- packages/protons/test/fixtures/noise.ts | 24 +- packages/protons/test/fixtures/oneof.ts | 34 ++- packages/protons/test/fixtures/optional.ts | 43 ++- packages/protons/test/fixtures/peer.ts | 42 ++- packages/protons/test/fixtures/proto2.ts | 12 +- .../protons/test/fixtures/protons-options.ts | 37 ++- packages/protons/test/fixtures/repeated.ts | 44 ++- packages/protons/test/fixtures/singular.ts | 43 ++- packages/protons/test/fixtures/streaming.ts | 132 ++++++--- packages/protons/test/fixtures/test.ts | 43 ++- 34 files changed, 989 insertions(+), 412 deletions(-) diff --git a/packages/protons-benchmark/src/implementations/protons/bench.ts b/packages/protons-benchmark/src/implementations/protons/bench.ts index b07d9ba..35d2166 100644 --- a/packages/protons-benchmark/src/implementations/protons/bench.ts +++ b/packages/protons-benchmark/src/implementations/protons/bench.ts @@ -6,12 +6,16 @@ export interface Foo { baz?: number } +export interface FooEncoder { + baz?: number +} + export namespace Foo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -92,7 +96,7 @@ export namespace Foo { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Foo.codec()) } @@ -109,12 +113,16 @@ export interface Bar { tmp?: Foo } +export interface BarEncoder { + tmp?: FooEncoder +} + export namespace Bar { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -207,7 +215,7 @@ export namespace Bar { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Bar.codec()) } @@ -233,7 +241,7 @@ enum __FOOValues { } export namespace FOO { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__FOOValues) } } @@ -242,12 +250,16 @@ export interface Yo { lol: FOO[] } +export interface YoEncoder { + lol: FOO[] +} + export namespace Yo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -349,7 +361,7 @@ export namespace Yo { value: FOO } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Yo.codec()) } @@ -367,12 +379,17 @@ export interface Lol { b?: Bar } +export interface LolEncoder { + lol?: string + b?: BarEncoder +} + export namespace Lol { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -496,7 +513,7 @@ export namespace Lol { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Lol.codec()) } @@ -516,12 +533,19 @@ export interface Test { payload?: Uint8Array } +export interface TestEncoder { + meh?: LolEncoder + hello?: number + foo?: string + payload?: Uint8Array +} + export namespace Test { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -702,7 +726,7 @@ export namespace Test { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Test.codec()) } diff --git a/packages/protons-runtime/src/codec.ts b/packages/protons-runtime/src/codec.ts index 83fbce3..a2c09a4 100644 --- a/packages/protons-runtime/src/codec.ts +++ b/packages/protons-runtime/src/codec.ts @@ -63,15 +63,15 @@ export interface StreamFunction { (reader: Reader, length: number | undefined, prefix: string, opts?: DecodeOptions): Generator } -export interface Codec { +export interface Codec { name: string type: number - encode: EncodeFunction - decode: DecodeFunction - stream: StreamFunction + encode: EncodeFunction + decode: DecodeFunction + stream: StreamFunction } -export function createCodec (name: string, type: number, encode: EncodeFunction, decode: DecodeFunction, stream: StreamFunction): Codec { +export function createCodec (name: string, type: number, encode: EncodeFunction, decode: DecodeFunction, stream: StreamFunction): Codec { return { name, type, diff --git a/packages/protons-runtime/src/codecs/enum.ts b/packages/protons-runtime/src/codecs/enum.ts index b85b322..4a8cf66 100644 --- a/packages/protons-runtime/src/codecs/enum.ts +++ b/packages/protons-runtime/src/codecs/enum.ts @@ -1,7 +1,7 @@ import { createCodec, CODEC_TYPES } from '../codec.ts' import type { DecodeFunction, EncodeFunction, Codec, StreamFunction } from '../codec.ts' -export function enumeration (v: any): Codec { +export function enumeration (v: any): Codec { function findValue (val: any): number { // Use the reverse mapping to look up the enum key for the stored value // https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings @@ -28,5 +28,5 @@ export function enumeration (v: any): Codec { // enums are simple values that are decoded inline so this is a no-op } - return createCodec('enum', CODEC_TYPES.VARINT, encode, decode, stream) + return createCodec('enum', CODEC_TYPES.VARINT, encode, decode, stream) } diff --git a/packages/protons-runtime/src/codecs/message.ts b/packages/protons-runtime/src/codecs/message.ts index 41018c7..fb44162 100644 --- a/packages/protons-runtime/src/codecs/message.ts +++ b/packages/protons-runtime/src/codecs/message.ts @@ -5,6 +5,6 @@ export interface Factory { new (obj: A): T } -export function message (encode: EncodeFunction, decode: DecodeFunction, stream: StreamFunction): Codec { +export function message (encode: EncodeFunction, decode: DecodeFunction, stream: StreamFunction): Codec { return createCodec('message', CODEC_TYPES.LENGTH_DELIMITED, encode, decode, stream) } diff --git a/packages/protons-runtime/src/decode.ts b/packages/protons-runtime/src/decode.ts index 18ef696..599f078 100644 --- a/packages/protons-runtime/src/decode.ts +++ b/packages/protons-runtime/src/decode.ts @@ -2,7 +2,7 @@ import { createReader } from './utils/reader.ts' import type { Codec, DecodeOptions } from './codec.ts' import type { Uint8ArrayList } from 'uint8arraylist' -export function decodeMessage (buf: Uint8Array | Uint8ArrayList, codec: Pick, 'decode'>, opts?: DecodeOptions): T { +export function decodeMessage (buf: Uint8Array | Uint8ArrayList, codec: Pick, 'decode'>, opts?: DecodeOptions): D { const reader = createReader(buf) return codec.decode(reader, undefined, opts) diff --git a/packages/protons-runtime/src/encode.ts b/packages/protons-runtime/src/encode.ts index 58264a1..ca9db24 100644 --- a/packages/protons-runtime/src/encode.ts +++ b/packages/protons-runtime/src/encode.ts @@ -1,7 +1,7 @@ import { createWriter } from './utils/writer.ts' import type { Codec } from './codec.ts' -export function encodeMessage (message: Partial, codec: Pick, 'encode'>): Uint8Array { +export function encodeMessage (message: Partial, codec: Pick, 'encode'>): Uint8Array { const w = createWriter() codec.encode(message, w, { diff --git a/packages/protons-runtime/src/index.ts b/packages/protons-runtime/src/index.ts index 97233c7..cf954fe 100644 --- a/packages/protons-runtime/src/index.ts +++ b/packages/protons-runtime/src/index.ts @@ -14,7 +14,7 @@ import type { Codec } from './codec.ts' export interface FieldDef { name: string - codec: Codec + codec: Codec optional?: true repeats?: true packed?: true diff --git a/packages/protons-runtime/src/stream.ts b/packages/protons-runtime/src/stream.ts index d01ee38..aae7e0b 100644 --- a/packages/protons-runtime/src/stream.ts +++ b/packages/protons-runtime/src/stream.ts @@ -2,7 +2,7 @@ import { createReader } from './utils/reader.ts' import type { Codec } from './codec.ts' import type { Uint8ArrayList } from 'uint8arraylist' -export function * streamMessage (buf: Uint8Array | Uint8ArrayList, codec: Pick, 'stream'>, opts?: any): Generator { +export function * streamMessage (buf: Uint8Array | Uint8ArrayList, codec: Pick, 'stream'>, opts?: any): Generator { const reader = createReader(buf) yield * codec.stream(reader, undefined, '.', opts) diff --git a/packages/protons/src/fields/array-field.ts b/packages/protons/src/fields/array-field.ts index a79a1ca..ccaecd7 100644 --- a/packages/protons/src/fields/array-field.ts +++ b/packages/protons/src/fields/array-field.ts @@ -21,8 +21,12 @@ export class ArrayField extends Field { this.lengthLimit = def.options?.['(protons.options).limit'] } - getInterfaceField (parent: Parent, indent = ''): string { - return `${super.getInterfaceField(parent, indent)}[]` + getDecoderInterfaceField (parent: Parent, indent = ''): string { + return `${super.getDecoderInterfaceField(parent, indent)}[]` + } + + getEncoderInterfaceField (parent: Parent, indent = ''): string { + return `${super.getEncoderInterfaceField(parent, indent)}[]` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/fields/field.ts b/packages/protons/src/fields/field.ts index 1c55723..160c283 100644 --- a/packages/protons/src/fields/field.ts +++ b/packages/protons/src/fields/field.ts @@ -82,7 +82,12 @@ export interface MessageField { /** * Return a string that can be used in a typescript interface for this field */ - getInterfaceField (parent: Parent, indent?: string): string + getDecoderInterfaceField (parent: Parent, indent?: string): string + + /** + * Return a string that can be used in a typescript interface for this field + */ + getEncoderInterfaceField (parent: Parent, indent?: string): string } export class Field implements MessageField { @@ -127,8 +132,12 @@ export class Field implements MessageField { } } - getInterfaceField (parent: Parent, indent = ''): string { - return `${indent}${this.name}${this.optional ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType}` + getDecoderInterfaceField (parent: Parent, indent = ''): string { + return `${indent}${this.name}${this.optional ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.decode}` + } + + getEncoderInterfaceField (parent: Parent, indent = ''): string { + return `${indent}${this.name}${this.optional ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/fields/map-field.ts b/packages/protons/src/fields/map-field.ts index 5b37827..b78efa4 100644 --- a/packages/protons/src/fields/map-field.ts +++ b/packages/protons/src/fields/map-field.ts @@ -37,16 +37,23 @@ export class MapField extends Field { } } - getInterfaceField (parent: Parent): string { - const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType - const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType + getDecoderInterfaceField (parent: Parent): string { + const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType.decode + const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType.decode + + return `${this.name}: Map<${keyType}, ${valueType}>` + } + + getEncoderInterfaceField (parent: Parent): string { + const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType.encode + const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType.encode return `${this.name}: Map<${keyType}, ${valueType}>` } getDefaultField (parent: Parent): string { - const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType - const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType + const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType.decode + const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType.decode return `${this.name}: new Map<${keyType}, ${valueType}>()` } diff --git a/packages/protons/src/fields/message-field.ts b/packages/protons/src/fields/message-field.ts index f496cc3..04f9e8f 100644 --- a/packages/protons/src/fields/message-field.ts +++ b/packages/protons/src/fields/message-field.ts @@ -3,8 +3,12 @@ import { Field } from './field.ts' import type { Parent, Type } from '../types/index.ts' export class MessageField extends Field { - getInterfaceField (parent: Parent, indent = ''): string { - return `${indent}${this.name}?: ${parent.findType(this.type).jsType}` + getDecoderInterfaceField (parent: Parent, indent = ''): string { + return `${indent}${this.name}?: ${parent.findType(this.type).jsType.decode}` + } + + getEncoderInterfaceField (parent: Parent, indent = ''): string { + return `${indent}${this.name}?: ${parent.findType(this.type).jsType.encode}` } getMessage (parent: Parent): Message { diff --git a/packages/protons/src/types/enum.ts b/packages/protons/src/types/enum.ts index b8e3b1c..3e9c56e 100644 --- a/packages/protons/src/types/enum.ts +++ b/packages/protons/src/types/enum.ts @@ -1,6 +1,6 @@ import { ParseError } from 'protons-runtime' import { ArrayField } from '../fields/array-field.ts' -import type { Parent, Type } from './index.ts' +import type { Parent, Type, TypeCodec } from './index.ts' import type { Field } from '../fields/field.ts' export interface EnumDef { @@ -13,12 +13,12 @@ export function isEnumDef (obj?: any): obj is EnumDef { export class Enum implements Type { public pbType: string - public jsType: string + public jsType: TypeCodec public values: Map public lowestValue: number public lowestValueName: string - constructor (pbType: string, jsType: string, def: EnumDef) { + constructor (pbType: string, jsType: TypeCodec, def: EnumDef) { this.pbType = pbType this.jsType = jsType this.values = new Map(Object.entries(def.values)) @@ -51,7 +51,7 @@ export class Enum implements Type { } getDecoder (field: Field): string { - return `${this.jsType}.codec().decode(reader)` + return `${this.jsType.decode}.codec().decode(reader)` } getStreamingDecoder (field: Field, prefix: string, indent: ''): string { @@ -59,18 +59,18 @@ export class Enum implements Type { return `yield { ${indent} field: ${prefix}, ${indent} index: obj.${field.name}, -${indent} value: ${this.jsType}.codec().decode(reader) +${indent} value: ${this.jsType.decode}.codec().decode(reader) ${indent} }` } return `yield { ${indent} field: ${prefix}, -${indent} value: ${this.jsType}.codec().decode(reader) +${indent} value: ${this.jsType.decode}.codec().decode(reader) ${indent} }` } getEncoder (field: Field, accessor: string): string { - return `${this.jsType}.codec().encode(${accessor}, w)` + return `${this.jsType.decode}.codec().encode(${accessor}, w)` } getValueTest (field: Field, accessor: string): string { @@ -115,7 +115,7 @@ enum __${this.pbType}Values { } export namespace ${this.pbType} { - export const codec = (): Codec<${this.pbType}> => { + export const codec = (): Codec<${this.pbType}, ${this.pbType}> => { return enumeration<${this.pbType}>(__${this.pbType}Values) } } diff --git a/packages/protons/src/types/index.ts b/packages/protons/src/types/index.ts index db02ec2..07e3fcd 100644 --- a/packages/protons/src/types/index.ts +++ b/packages/protons/src/types/index.ts @@ -2,8 +2,13 @@ import type { Field } from '../fields/field.ts' import type { Flags } from '../index.ts' import type { Module } from './module.ts' +export interface TypeCodec { + encode: string + decode: string +} + export interface Type { - jsType: string + jsType: TypeCodec pbType: string init(module: Module): void getDecoder(field: Field, indent?: string): string diff --git a/packages/protons/src/types/message.ts b/packages/protons/src/types/message.ts index d208d2f..ce9169a 100644 --- a/packages/protons/src/types/message.ts +++ b/packages/protons/src/types/message.ts @@ -6,7 +6,7 @@ import { MessageField } from '../fields/message-field.ts' import { Enum } from './enum.ts' import { Primitive } from './primitive.ts' import type { EnumDef } from './enum.ts' -import type { Parent, Type } from './index.ts' +import type { Parent, Type, TypeCodec } from './index.ts' import type { FieldDef } from '../fields/field.ts' import type { Flags } from '../index.ts' @@ -38,14 +38,14 @@ export function isMessageDef (obj?: any): obj is MessageDef { export class Message implements Type { public pbType: string - public jsType: string + public jsType: TypeCodec public fields: Field[] public oneOfs: string[][] public nested: Record private def: MessageDef private parent: Parent - constructor (pbType: string, jsType: string, def: MessageDef, parent: Parent) { + constructor (pbType: string, jsType: TypeCodec, def: MessageDef, parent: Parent) { this.pbType = pbType this.jsType = jsType this.oneOfs = [] @@ -92,12 +92,16 @@ export class Message implements Type { } Object.entries(def.nested ?? {}).forEach(([name, def]) => { - const fullName = `${this.jsType}.${name}` - if (isMessageDef(def)) { - this.nested[name] = new Message(name, fullName, def, this) + this.nested[name] = new Message(name, { + encode: `${this.jsType.decode}.${name}Encoder`, + decode: `${this.jsType.decode}.${name}` + }, def, this) } else { - this.nested[name] = new Enum(name, fullName, def) + this.nested[name] = new Enum(name, { + encode: `${this.jsType.decode}.${name}`, + decode: `${this.jsType.decode}.${name}` + }, def) } }) @@ -184,7 +188,7 @@ ${indent} value: opts.limits?.${field.name}$value ${indent} }` } - return `${this.jsType}.codec().decode(reader, reader.uint32()${opts})` + return `${this.jsType.decode}.codec().decode(reader, reader.uint32()${opts})` } getStreamingDecoder (field: Field, prefix: string, indent: ''): string { @@ -199,7 +203,7 @@ ${indent} }` ${indent} limits: opts.limits?.${field.name}$ ${indent} }` - return `for (const evt of ${this.jsType}.codec().stream(reader, reader.uint32(), ${prefix}${opts})) { + return `for (const evt of ${this.jsType.decode}.codec().stream(reader, reader.uint32(), ${prefix}${opts})) { ${indent} yield { ${indent} ...evt, ${indent} index: obj.${field.name} @@ -213,7 +217,7 @@ ${indent} value: opts.limits?.${field.name}$value ${indent} }` } - return `yield * ${this.jsType}.codec().stream(reader, reader.uint32(), ${prefix}${opts})` + return `yield * ${this.jsType.decode}.codec().stream(reader, reader.uint32(), ${prefix}${opts})` } getEncoder (field: Field, accessor: string): string { @@ -222,10 +226,10 @@ ${indent} }` // is part of a repeated field, and consists of only default values it // won't be written, so write a zero-length buffer if that's the case // writeField = (): string => `w.uint32(${id}) - // ${type.jsType}.codec().encode(${valueVar}, w)` + // ${type.jsType.decode}.codec().encode(${valueVar}, w)` } - return `${this.jsType}.codec().encode(${accessor}, w)` + return `${this.jsType.decode}.codec().encode(${accessor}, w)` } getValueTest (field: Field): string { @@ -254,20 +258,24 @@ ${indent} }` ` } - const interfaceFields = this.fields.map(field => field.getInterfaceField(this)) - .join('\n ') - .trim() - + const decoderFields = this.fields.map(field => field.getDecoderInterfaceField(this)) + const encoderFields = this.fields.map(field => field.getEncoderInterfaceField(this)) let interfaceDef = '' let interfaceCodecDef = '' - if (interfaceFields === '') { + if (decoderFields.length === 0) { interfaceDef = ` +export interface ${this.pbType}Encoder {} + export interface ${this.pbType} {}` } else { interfaceDef = ` export interface ${this.pbType} { - ${interfaceFields} + ${decoderFields.join('\n ').trim()} +} + +export interface ${this.pbType}Encoder { + ${encoderFields.join('\n ').trim()} }` } @@ -286,11 +294,11 @@ export interface ${this.pbType} { } interfaceCodecDef = ` - let _codec: Codec<${this.pbType}> + let _codec: Codec<${this.pbType}, ${this.pbType}Encoder> - export const codec = (): Codec<${this.pbType}> => { + export const codec = (): Codec<${this.pbType}, ${this.pbType}Encoder> => { if (_codec == null) { - _codec = message<${this.pbType}>((obj, w, opts = {}) => { + _codec = message<${this.pbType}, ${this.pbType}Encoder>((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() }${enforceOneOfEncoding}${this.formatFields(encodeFields)} @@ -322,7 +330,7 @@ ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} yield { field: prefix.endsWith('.') ? prefix.substring(0, prefix.length - 1) : prefix, type: 'start', - message: '${this.jsType}' + message: '${this.jsType.decode}' } } @@ -341,7 +349,7 @@ ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} yield { field: prefix.endsWith('.') ? prefix.substring(0, prefix.length - 1) : prefix, type: 'end', - message: '${this.jsType}' + message: '${this.jsType.decode}' } } }) @@ -350,7 +358,7 @@ ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} return _codec }${this.formatStreamEvents(streamEvents)} - export function encode (obj: Partial<${this.pbType}>): Uint8Array { + export function encode (obj: Partial<${this.pbType}Encoder>): Uint8Array { return encodeMessage(obj, ${this.pbType}.codec()) } @@ -546,15 +554,15 @@ ${fields name: `${fieldPrefix === '.' ? this.pbType : ''}${camelize(field.name)}FieldEvent`, fields: [ `field: '${fieldPrefix}${field.name}{}'`, - `key: ${field.jsKeyTypeOverride ?? keyType.jsType}`, - `value: ${field.jsValueTypeOverride ?? valueType.jsType}` + `key: ${field.jsKeyTypeOverride ?? keyType.jsType.decode}`, + `value: ${field.jsValueTypeOverride ?? valueType.jsType.decode}` ], type: 'collection-primitive-member' }) } else if (valueType instanceof Message) { addMessageFields(field, valueType, [ - `key: ${field.jsKeyTypeOverride ?? keyType.jsType}`, - `value: ${field.jsValueTypeOverride ?? valueType.jsType}` + `key: ${field.jsKeyTypeOverride ?? keyType.jsType.decode}`, + `value: ${field.jsValueTypeOverride ?? valueType.jsType.decode}` ]) streamEvents.push({ @@ -562,7 +570,7 @@ ${fields type: 'sub-message', fields: [ `field: '${fieldPrefix}${field.name}{}'`, - `key: ${field.jsKeyTypeOverride ?? keyType.jsType}`, + `key: ${field.jsKeyTypeOverride ?? keyType.jsType.decode}`, "type: 'start'", 'message: string' ] @@ -572,7 +580,7 @@ ${fields type: 'sub-message', fields: [ `field: '${fieldPrefix}${field.name}{}'`, - `key: ${field.jsKeyTypeOverride ?? keyType.jsType}`, + `key: ${field.jsKeyTypeOverride ?? keyType.jsType.decode}`, "type: 'end'", 'message: string' ] @@ -587,7 +595,7 @@ ${fields fields: [ `field: '${fieldPrefix}${field.name}[]'`, 'index: number', - `value: ${field.jsTypeOverride ?? type.jsType}` + `value: ${field.jsTypeOverride ?? type.jsType.decode}` ], type: 'collection-primitive-member' }) @@ -626,7 +634,7 @@ ${fields name: `${fieldPrefix === '.' ? this.pbType : ''}${camelize(field.name)}FieldEvent`, fields: [ `field: '${fieldPrefix}${field.name}'`, - `value: ${field.jsTypeOverride ?? type.jsType}` + `value: ${field.jsTypeOverride ?? type.jsType.decode}` ], type: 'field' }) diff --git a/packages/protons/src/types/module.ts b/packages/protons/src/types/module.ts index f502b8e..f269aa0 100644 --- a/packages/protons/src/types/module.ts +++ b/packages/protons/src/types/module.ts @@ -5,24 +5,69 @@ import { Primitive } from './primitive.ts' import type { EnumDef } from './enum.ts' import type { MessageDef } from './message.ts' import type { Flags } from '../index.ts' -import type { Type } from './index.ts' - -const types: Record = { - bool: 'boolean', - bytes: 'Uint8Array', - double: 'number', - fixed32: 'number', - fixed64: 'bigint', - float: 'number', - int32: 'number', - int64: 'bigint', - sfixed32: 'number', - sfixed64: 'bigint', - sint32: 'number', - sint64: 'bigint', - string: 'string', - uint32: 'number', - uint64: 'bigint' +import type { Type, TypeCodec } from './index.ts' + +const types: Record = { + bool: { + encode: 'boolean', + decode: 'boolean' + }, + bytes: { + encode: 'Uint8Array', + decode: 'Uint8Array' + }, + double: { + encode: 'number', + decode: 'number' + }, + fixed32: { + encode: 'number', + decode: 'number' + }, + fixed64: { + encode: 'bigint', + decode: 'bigint' + }, + float: { + encode: 'number', + decode: 'number' + }, + int32: { + encode: 'number', + decode: 'number' + }, + int64: { + encode: 'bigint', + decode: 'bigint' + }, + sfixed32: { + encode: 'number', + decode: 'number' + }, + sfixed64: { + encode: 'bigint', + decode: 'bigint' + }, + sint32: { + encode: 'number', + decode: 'number' + }, + sint64: { + encode: 'bigint', + decode: 'bigint' + }, + string: { + encode: 'string', + decode: 'string' + }, + uint32: { + encode: 'number', + decode: 'number' + }, + uint64: { + encode: 'bigint', + decode: 'bigint' + } } interface Import { @@ -63,9 +108,15 @@ export class Module { let type: Message | Enum if (isEnumDef(def)) { - type = new Enum(name, name, def) + type = new Enum(name, { + encode: name, + decode: name + }, def) } else { - type = new Message(name, name, def, this) + type = new Message(name, { + encode: `${name}Encoder`, + decode: name + }, def, this) } this.globals.push(type) diff --git a/packages/protons/src/types/primitive.ts b/packages/protons/src/types/primitive.ts index 17ea8a4..e174e21 100644 --- a/packages/protons/src/types/primitive.ts +++ b/packages/protons/src/types/primitive.ts @@ -1,5 +1,5 @@ import { ArrayField } from '../fields/array-field.ts' -import type { Type } from './index.ts' +import type { Type, TypeCodec } from './index.ts' import type { Field } from '../fields/field.ts' const decoderGenerators: Record string> = { @@ -166,10 +166,10 @@ const defaultValueTestGeneratorsJsTypeOverrides: Record + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -116,7 +121,7 @@ export namespace Basic { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Basic.codec()) } @@ -129,14 +134,16 @@ export namespace Basic { } } +export interface EmptyEncoder {} + export interface Empty {} export namespace Empty { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -196,7 +203,7 @@ export namespace Empty { return _codec } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Empty.codec()) } diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index e0452ed..867a74a 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -11,12 +11,25 @@ export interface Message { pendingBytes: number } +export interface MessageEncoder { + wantlist?: Message.WantlistEncoder + blocks: Uint8Array[] + payload: Message.BlockEncoder[] + blockPresences: Message.BlockPresenceEncoder[] + pendingBytes: number +} + export namespace Message { export interface Wantlist { entries: Message.Wantlist.Entry[] full: boolean } + export interface WantlistEncoder { + entries: Message.Wantlist.EntryEncoder[] + full: boolean + } + export namespace Wantlist { export enum WantType { Block = 'Block', @@ -29,7 +42,7 @@ export namespace Message { } export namespace WantType { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__WantTypeValues) } } @@ -42,12 +55,20 @@ export namespace Message { sendDontHave: boolean } + export interface EntryEncoder { + block: Uint8Array + priority: number + cancel?: boolean + wantType: Message.Wantlist.WantType + sendDontHave: boolean + } + export namespace Entry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -217,7 +238,7 @@ export namespace Message { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Entry.codec()) } @@ -230,11 +251,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -401,7 +422,7 @@ export namespace Message { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Wantlist.codec()) } @@ -419,12 +440,17 @@ export namespace Message { data: Uint8Array } + export interface BlockEncoder { + prefix: Uint8Array + data: Uint8Array + } + export namespace Block { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -529,7 +555,7 @@ export namespace Message { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Block.codec()) } @@ -553,7 +579,7 @@ export namespace Message { } export namespace BlockPresenceType { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__BlockPresenceTypeValues) } } @@ -563,12 +589,17 @@ export namespace Message { type: Message.BlockPresenceType } + export interface BlockPresenceEncoder { + cid: Uint8Array + type: Message.BlockPresenceType + } + export namespace BlockPresence { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -673,7 +704,7 @@ export namespace Message { value: Message.BlockPresenceType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, BlockPresence.codec()) } @@ -686,11 +717,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1017,7 +1048,7 @@ export namespace Message { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index 74fffb6..6d8910e 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -10,6 +10,13 @@ export interface CircuitRelay { code?: CircuitRelay.Status } +export interface CircuitRelayEncoder { + type?: CircuitRelay.Type + srcPeer?: CircuitRelay.PeerEncoder + dstPeer?: CircuitRelay.PeerEncoder + code?: CircuitRelay.Status +} + export namespace CircuitRelay { export enum Status { SUCCESS = 'SUCCESS', @@ -50,7 +57,7 @@ export namespace CircuitRelay { } export namespace Status { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__StatusValues) } } @@ -70,7 +77,7 @@ export namespace CircuitRelay { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } @@ -80,12 +87,17 @@ export namespace CircuitRelay { addrs: Uint8Array[] } + export interface PeerEncoder { + id: Uint8Array + addrs: Uint8Array[] + } + export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -209,7 +221,7 @@ export namespace CircuitRelay { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -222,11 +234,11 @@ export namespace CircuitRelay { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -406,7 +418,7 @@ export namespace CircuitRelay { value: CircuitRelay.Status } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, CircuitRelay.codec()) } diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index 9cea0d0..3f3ee59 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -13,18 +13,34 @@ export interface CustomOptionNumber { i64Map: Map } +export interface CustomOptionNumberEncoder { + num: number + i64: number + ui64: number + si64: number + f64: number + sf64: number + i64Array: number[] + i64Map: Map +} + export namespace CustomOptionNumber { export interface CustomOptionNumber$i64MapEntry { key: number value: number } + export interface CustomOptionNumber$i64MapEntryEncoder { + key: number + value: number + } + export namespace CustomOptionNumber$i64MapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -129,7 +145,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, CustomOptionNumber$i64MapEntry.codec()) } @@ -142,11 +158,11 @@ export namespace CustomOptionNumber { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -423,7 +439,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, CustomOptionNumber.codec()) } @@ -447,18 +463,34 @@ export interface CustomOptionString { i64Map: Map } +export interface CustomOptionStringEncoder { + num: number + i64: string + ui64: string + si64: string + f64: string + sf64: string + i64Array: string[] + i64Map: Map +} + export namespace CustomOptionString { export interface CustomOptionString$i64MapEntry { key: string value: string } + export interface CustomOptionString$i64MapEntryEncoder { + key: string + value: string + } + export namespace CustomOptionString$i64MapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -563,7 +595,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, CustomOptionString$i64MapEntry.codec()) } @@ -576,11 +608,11 @@ export namespace CustomOptionString { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -857,7 +889,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, CustomOptionString.codec()) } diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 1a0ec8a..e6653c0 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -15,6 +15,18 @@ export interface Request { peerStore?: PeerstoreRequest } +export interface RequestEncoder { + type: Request.Type + connect?: ConnectRequestEncoder + streamOpen?: StreamOpenRequestEncoder + streamHandler?: StreamHandlerRequestEncoder + dht?: DHTRequestEncoder + connManager?: ConnManagerRequestEncoder + disconnect?: DisconnectRequestEncoder + pubsub?: PSRequestEncoder + peerStore?: PeerstoreRequestEncoder +} + export namespace Request { export enum Type { IDENTIFY = 'IDENTIFY', @@ -43,16 +55,16 @@ export namespace Request { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -493,7 +505,7 @@ export namespace Request { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Request.codec()) } @@ -517,6 +529,17 @@ export interface Response { peerStore?: PeerstoreResponse } +export interface ResponseEncoder { + type: Response.Type + error?: ErrorResponseEncoder + streamInfo?: StreamInfoEncoder + identify?: IdentifyResponseEncoder + dht?: DHTResponseEncoder + peers: PeerInfoEncoder[] + pubsub?: PSResponseEncoder + peerStore?: PeerstoreResponseEncoder +} + export namespace Response { export enum Type { OK = 'OK', @@ -529,16 +552,16 @@ export namespace Response { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -956,7 +979,7 @@ export namespace Response { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Response.codec()) } @@ -974,12 +997,17 @@ export interface IdentifyResponse { addrs: Uint8Array[] } +export interface IdentifyResponseEncoder { + id: Uint8Array + addrs: Uint8Array[] +} + export namespace IdentifyResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1103,7 +1131,7 @@ export namespace IdentifyResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, IdentifyResponse.codec()) } @@ -1122,12 +1150,18 @@ export interface ConnectRequest { timeout?: bigint } +export interface ConnectRequestEncoder { + peer: Uint8Array + addrs: Uint8Array[] + timeout?: bigint +} + export namespace ConnectRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1272,7 +1306,7 @@ export namespace ConnectRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, ConnectRequest.codec()) } @@ -1291,12 +1325,18 @@ export interface StreamOpenRequest { timeout?: bigint } +export interface StreamOpenRequestEncoder { + peer: Uint8Array + proto: string[] + timeout?: bigint +} + export namespace StreamOpenRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1441,7 +1481,7 @@ export namespace StreamOpenRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, StreamOpenRequest.codec()) } @@ -1459,12 +1499,17 @@ export interface StreamHandlerRequest { proto: string[] } +export interface StreamHandlerRequestEncoder { + addr: Uint8Array + proto: string[] +} + export namespace StreamHandlerRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1588,7 +1633,7 @@ export namespace StreamHandlerRequest { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, StreamHandlerRequest.codec()) } @@ -1605,12 +1650,16 @@ export interface ErrorResponse { msg: string } +export interface ErrorResponseEncoder { + msg: string +} + export namespace ErrorResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1693,7 +1742,7 @@ export namespace ErrorResponse { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, ErrorResponse.codec()) } @@ -1712,12 +1761,18 @@ export interface StreamInfo { proto: string } +export interface StreamInfoEncoder { + peer: Uint8Array + addr: Uint8Array + proto: string +} + export namespace StreamInfo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1844,7 +1899,7 @@ export namespace StreamInfo { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, StreamInfo.codec()) } @@ -1867,6 +1922,16 @@ export interface DHTRequest { timeout?: bigint } +export interface DHTRequestEncoder { + type: DHTRequest.Type + peer?: Uint8Array + cid?: Uint8Array + key?: Uint8Array + value?: Uint8Array + count?: number + timeout?: bigint +} + export namespace DHTRequest { export enum Type { FIND_PEER = 'FIND_PEER', @@ -1893,16 +1958,16 @@ export namespace DHTRequest { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2111,7 +2176,7 @@ export namespace DHTRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, DHTRequest.codec()) } @@ -2130,6 +2195,12 @@ export interface DHTResponse { value?: Uint8Array } +export interface DHTResponseEncoder { + type: DHTResponse.Type + peer?: PeerInfoEncoder + value?: Uint8Array +} + export namespace DHTResponse { export enum Type { BEGIN = 'BEGIN', @@ -2144,16 +2215,16 @@ export namespace DHTResponse { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2296,7 +2367,7 @@ export namespace DHTResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, DHTResponse.codec()) } @@ -2314,12 +2385,17 @@ export interface PeerInfo { addrs: Uint8Array[] } +export interface PeerInfoEncoder { + id: Uint8Array + addrs: Uint8Array[] +} + export namespace PeerInfo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2443,7 +2519,7 @@ export namespace PeerInfo { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PeerInfo.codec()) } @@ -2463,6 +2539,13 @@ export interface ConnManagerRequest { weight?: bigint } +export interface ConnManagerRequestEncoder { + type: ConnManagerRequest.Type + peer?: Uint8Array + tag?: string + weight?: bigint +} + export namespace ConnManagerRequest { export enum Type { TAG_PEER = 'TAG_PEER', @@ -2477,16 +2560,16 @@ export namespace ConnManagerRequest { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2632,7 +2715,7 @@ export namespace ConnManagerRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, ConnManagerRequest.codec()) } @@ -2649,12 +2732,16 @@ export interface DisconnectRequest { peer: Uint8Array } +export interface DisconnectRequestEncoder { + peer: Uint8Array +} + export namespace DisconnectRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2737,7 +2824,7 @@ export namespace DisconnectRequest { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, DisconnectRequest.codec()) } @@ -2756,6 +2843,12 @@ export interface PSRequest { data?: Uint8Array } +export interface PSRequestEncoder { + type: PSRequest.Type + topic?: string + data?: Uint8Array +} + export namespace PSRequest { export enum Type { GET_TOPICS = 'GET_TOPICS', @@ -2772,16 +2865,16 @@ export namespace PSRequest { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2906,7 +2999,7 @@ export namespace PSRequest { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PSRequest.codec()) } @@ -2928,12 +3021,21 @@ export interface PSMessage { key?: Uint8Array } +export interface PSMessageEncoder { + from?: Uint8Array + data?: Uint8Array + seqno?: Uint8Array + topicIDs: string[] + signature?: Uint8Array + key?: Uint8Array +} + export namespace PSMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3140,7 +3242,7 @@ export namespace PSMessage { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PSMessage.codec()) } @@ -3158,12 +3260,17 @@ export interface PSResponse { peerIDs: Uint8Array[] } +export interface PSResponseEncoder { + topics: string[] + peerIDs: Uint8Array[] +} + export namespace PSResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3303,7 +3410,7 @@ export namespace PSResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PSResponse.codec()) } @@ -3322,6 +3429,12 @@ export interface PeerstoreRequest { protos: string[] } +export interface PeerstoreRequestEncoder { + type: PeerstoreRequest.Type + id?: Uint8Array + protos: string[] +} + export namespace PeerstoreRequest { export enum Type { INVALID = 'INVALID', @@ -3336,16 +3449,16 @@ export namespace PeerstoreRequest { } export namespace Type { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__TypeValues) } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3490,7 +3603,7 @@ export namespace PeerstoreRequest { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PeerstoreRequest.codec()) } @@ -3508,12 +3621,17 @@ export interface PeerstoreResponse { protos: string[] } +export interface PeerstoreResponseEncoder { + peer?: PeerInfoEncoder + protos: string[] +} + export namespace PeerstoreResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3654,7 +3772,7 @@ export namespace PeerstoreResponse { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, PeerstoreResponse.codec()) } diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index a7626f8..af0ad2b 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -10,12 +10,20 @@ export interface Record { timeReceived?: string } +export interface RecordEncoder { + key?: Uint8Array + value?: Uint8Array + author?: Uint8Array + signature?: Uint8Array + timeReceived?: string +} + export namespace Record { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -180,7 +188,7 @@ export namespace Record { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Record.codec()) } @@ -202,6 +210,15 @@ export interface Message { providerPeers: Message.Peer[] } +export interface MessageEncoder { + type?: Message.MessageType + clusterLevelRaw?: number + key?: Uint8Array + record?: Uint8Array + closerPeers: Message.PeerEncoder[] + providerPeers: Message.PeerEncoder[] +} + export namespace Message { export enum MessageType { PUT_VALUE = 'PUT_VALUE', @@ -222,7 +239,7 @@ export namespace Message { } export namespace MessageType { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__MessageTypeValues) } } @@ -242,7 +259,7 @@ export namespace Message { } export namespace ConnectionType { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__ConnectionTypeValues) } } @@ -253,12 +270,18 @@ export namespace Message { connection?: Message.ConnectionType } + export interface PeerEncoder { + id?: Uint8Array + addrs: Uint8Array[] + connection?: Message.ConnectionType + } + export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -402,7 +425,7 @@ export namespace Message { value: Message.ConnectionType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -415,11 +438,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -705,7 +728,7 @@ export namespace Message { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index f939e48..bf4649e 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -15,7 +15,7 @@ enum __EnumValueValues { } export namespace EnumValue { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__EnumValueValues) } } @@ -25,12 +25,17 @@ export interface SubMessage { bar: number[] } +export interface SubMessageEncoder { + foo: string + bar: number[] +} + export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -154,7 +159,7 @@ export namespace SubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -175,18 +180,31 @@ export interface MapTypes { enumMap: Map } +export interface MapTypesEncoder { + stringMap: Map + intMap: Map + boolMap: Map + messageMap: Map + enumMap: Map +} + export namespace MapTypes { export interface MapTypes$stringMapEntry { key: string value: string } + export interface MapTypes$stringMapEntryEncoder { + key: string + value: string + } + export namespace MapTypes$stringMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -291,7 +309,7 @@ export namespace MapTypes { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes$stringMapEntry.codec()) } @@ -309,12 +327,17 @@ export namespace MapTypes { value: number } + export interface MapTypes$intMapEntryEncoder { + key: number + value: number + } + export namespace MapTypes$intMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -419,7 +442,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes$intMapEntry.codec()) } @@ -437,12 +460,17 @@ export namespace MapTypes { value: boolean } + export interface MapTypes$boolMapEntryEncoder { + key: boolean + value: boolean + } + export namespace MapTypes$boolMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -547,7 +575,7 @@ export namespace MapTypes { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes$boolMapEntry.codec()) } @@ -565,12 +593,17 @@ export namespace MapTypes { value?: SubMessage } + export interface MapTypes$messageMapEntryEncoder { + key: string + value?: SubMessageEncoder + } + export namespace MapTypes$messageMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -692,7 +725,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes$messageMapEntry.codec()) } @@ -710,12 +743,17 @@ export namespace MapTypes { value: EnumValue } + export interface MapTypes$enumMapEntryEncoder { + key: string + value: EnumValue + } + export namespace MapTypes$enumMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -820,7 +858,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes$enumMapEntry.codec()) } @@ -833,11 +871,11 @@ export namespace MapTypes { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1137,7 +1175,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MapTypes.codec()) } diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 9b25a48..de05547 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -3,6 +3,8 @@ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc' import type { Codec, DecodeOptions } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' +export interface pbEncoder {} + export interface pb {} export namespace pb { @@ -12,12 +14,18 @@ export namespace pb { data: Uint8Array } + export interface NoiseHandshakePayloadEncoder { + identityKey: Uint8Array + identitySig: Uint8Array + data: Uint8Array + } + export namespace NoiseHandshakePayload { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -144,7 +152,7 @@ export namespace pb { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, NoiseHandshakePayload.codec()) } @@ -157,11 +165,11 @@ export namespace pb { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -221,7 +229,7 @@ export namespace pb { return _codec } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, pb.codec()) } diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index 0a01cd1..a9f0f47 100644 --- a/packages/protons/test/fixtures/oneof.ts +++ b/packages/protons/test/fixtures/oneof.ts @@ -13,7 +13,7 @@ enum __EnumTypeValues { } export namespace EnumType { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__EnumTypeValues) } } @@ -26,12 +26,20 @@ export interface OneOfMessage { fieldFive: string } +export interface OneOfMessageEncoder { + fieldOne?: string + fieldTwo?: string + fieldThree?: EnumType + fieldFour?: EnumType + fieldFive: string +} + export namespace OneOfMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -232,7 +240,7 @@ export namespace OneOfMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, OneOfMessage.codec()) } @@ -253,12 +261,20 @@ export interface MessageWithoutOneOfs { fieldFive: string } +export interface MessageWithoutOneOfsEncoder { + fieldOne: string + fieldTwo: string + fieldThree: EnumType + fieldFour: EnumType + fieldFive: string +} + export namespace MessageWithoutOneOfs { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -429,7 +445,7 @@ export namespace MessageWithoutOneOfs { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithoutOneOfs.codec()) } diff --git a/packages/protons/test/fixtures/optional.ts b/packages/protons/test/fixtures/optional.ts index 6e407a1..51379a0 100644 --- a/packages/protons/test/fixtures/optional.ts +++ b/packages/protons/test/fixtures/optional.ts @@ -15,7 +15,7 @@ enum __OptionalEnumValues { } export namespace OptionalEnum { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__OptionalEnumValues) } } @@ -25,12 +25,17 @@ export interface OptionalSubMessage { bar?: number } +export interface OptionalSubMessageEncoder { + foo?: string + bar?: number +} + export namespace OptionalSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -132,7 +137,7 @@ export namespace OptionalSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, OptionalSubMessage.codec()) } @@ -165,12 +170,32 @@ export interface Optional { subMessage?: OptionalSubMessage } +export interface OptionalEncoder { + double?: number + float?: number + int32?: number + int64?: bigint + uint32?: number + uint64?: bigint + sint32?: number + sint64?: bigint + fixed32?: number + fixed64?: bigint + sfixed32?: number + sfixed64?: bigint + bool?: boolean + string?: string + bytes?: Uint8Array + enum?: OptionalEnum + subMessage?: OptionalSubMessageEncoder +} + export namespace Optional { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -604,7 +629,7 @@ export namespace Optional { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Optional.codec()) } diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index afcda87..c605034 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -11,12 +11,20 @@ export interface Peer { peerRecordEnvelope?: Uint8Array } +export interface PeerEncoder { + addresses: AddressEncoder[] + protocols: string[] + metadata: MetadataEncoder[] + pubKey?: Uint8Array + peerRecordEnvelope?: Uint8Array +} + export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -286,7 +294,7 @@ export namespace Peer { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -304,12 +312,17 @@ export interface Address { isCertified?: boolean } +export interface AddressEncoder { + multiaddr: Uint8Array + isCertified?: boolean +} + export namespace Address { - let _codec: Codec
+ let _codec: Codec - export const codec = (): Codec
=> { + export const codec = (): Codec => { if (_codec == null) { - _codec = message
((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -413,7 +426,7 @@ export namespace Address { value: boolean } - export function encode (obj: Partial
): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Address.codec()) } @@ -431,12 +444,17 @@ export interface Metadata { value: Uint8Array } +export interface MetadataEncoder { + key: string + value: Uint8Array +} + export namespace Metadata { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -541,7 +559,7 @@ export namespace Metadata { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Metadata.codec()) } diff --git a/packages/protons/test/fixtures/proto2.ts b/packages/protons/test/fixtures/proto2.ts index 78580f2..b370afb 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -6,12 +6,16 @@ export interface MessageWithRequired { scalarField: number } +export interface MessageWithRequiredEncoder { + scalarField: number +} + export namespace MessageWithRequired { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -94,7 +98,7 @@ export namespace MessageWithRequired { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithRequired.codec()) } diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index 462fc4b..b095e72 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -6,12 +6,16 @@ export interface MessageWithSizeLimitedRepeatedField { repeatedField: string[] } +export interface MessageWithSizeLimitedRepeatedFieldEncoder { + repeatedField: string[] +} + export namespace MessageWithSizeLimitedRepeatedField { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -121,7 +125,7 @@ export namespace MessageWithSizeLimitedRepeatedField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedRepeatedField.codec()) } @@ -138,18 +142,27 @@ export interface MessageWithSizeLimitedMap { mapField: Map } +export interface MessageWithSizeLimitedMapEncoder { + mapField: Map +} + export namespace MessageWithSizeLimitedMap { export interface MessageWithSizeLimitedMap$mapFieldEntry { key: string value: string } + export interface MessageWithSizeLimitedMap$mapFieldEntryEncoder { + key: string + value: string + } + export namespace MessageWithSizeLimitedMap$mapFieldEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -254,7 +267,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap$mapFieldEntry.codec()) } @@ -267,11 +280,11 @@ export namespace MessageWithSizeLimitedMap { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -386,7 +399,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap.codec()) } diff --git a/packages/protons/test/fixtures/repeated.ts b/packages/protons/test/fixtures/repeated.ts index 3fca755..c2997d3 100644 --- a/packages/protons/test/fixtures/repeated.ts +++ b/packages/protons/test/fixtures/repeated.ts @@ -7,12 +7,17 @@ export interface SubSubMessage { nonRepeating?: number } +export interface SubSubMessageEncoder { + foo: string[] + nonRepeating?: number +} + export namespace SubSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -135,7 +140,7 @@ export namespace SubSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, SubSubMessage.codec()) } @@ -155,12 +160,19 @@ export interface SubMessage { messages: SubSubMessage[] } +export interface SubMessageEncoder { + foo: string[] + nonRepeating?: number + message?: SubSubMessageEncoder + messages: SubSubMessageEncoder[] +} + export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -385,7 +397,7 @@ export namespace SubMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -406,12 +418,20 @@ export interface RepeatedTypes { nonRepeating?: number } +export interface RepeatedTypesEncoder { + number: number[] + limitedNumber: number[] + messages: SubMessageEncoder[] + message?: SubMessageEncoder + nonRepeating?: number +} + export namespace RepeatedTypes { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -779,7 +799,7 @@ export namespace RepeatedTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, RepeatedTypes.codec()) } diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index d9f5c8c..c303a35 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -16,7 +16,7 @@ enum __SingularEnumValues { } export namespace SingularEnum { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__SingularEnumValues) } } @@ -26,12 +26,17 @@ export interface SingularSubMessage { bar: number } +export interface SingularSubMessageEncoder { + foo: string + bar: number +} + export namespace SingularSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -136,7 +141,7 @@ export namespace SingularSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, SingularSubMessage.codec()) } @@ -169,12 +174,32 @@ export interface Singular { subMessage?: SingularSubMessage } +export interface SingularEncoder { + double: number + float: number + int32: number + int64: bigint + uint32: number + uint64: bigint + sint32: number + sint64: bigint + fixed32: number + fixed64: bigint + sfixed32: number + sfixed64: bigint + bool: boolean + string: string + bytes: Uint8Array + enum: SingularEnum + subMessage?: SingularSubMessageEncoder +} + export namespace Singular { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -625,7 +650,7 @@ export namespace Singular { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, Singular.codec()) } diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index 8e8f960..1051601 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -8,12 +8,18 @@ export interface MessageWithArrayField { arr: string[] } +export interface MessageWithArrayFieldEncoder { + field1?: boolean + field2?: number + arr: string[] +} + export namespace MessageWithArrayField { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -157,7 +163,7 @@ export namespace MessageWithArrayField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithArrayField.codec()) } @@ -174,12 +180,16 @@ export interface NestedMessage { nestedValue: string } +export interface NestedMessageEncoder { + nestedValue: string +} + export namespace NestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -262,7 +272,7 @@ export namespace NestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, NestedMessage.codec()) } @@ -280,12 +290,17 @@ export interface MessageWithNestedMessage { nestedMessage?: NestedMessage } +export interface MessageWithNestedMessageEncoder { + field1: boolean + nestedMessage?: NestedMessageEncoder +} + export namespace MessageWithNestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -401,7 +416,7 @@ export namespace MessageWithNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithNestedMessage.codec()) } @@ -419,12 +434,17 @@ export interface MessageWithDeeplyNestedMessage { nestedMessage?: MessageWithNestedMessage } +export interface MessageWithDeeplyNestedMessageEncoder { + field1: boolean + nestedMessage?: MessageWithNestedMessageEncoder +} + export namespace MessageWithDeeplyNestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -555,7 +575,7 @@ export namespace MessageWithDeeplyNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithDeeplyNestedMessage.codec()) } @@ -573,12 +593,17 @@ export interface MessageWithRepeatedMessage { nestedMessages: NestedMessage[] } +export interface MessageWithRepeatedMessageEncoder { + field1: boolean + nestedMessages: NestedMessageEncoder[] +} + export namespace MessageWithRepeatedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -721,7 +746,7 @@ export namespace MessageWithRepeatedMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithRepeatedMessage.codec()) } @@ -739,18 +764,28 @@ export interface MessageWithMapMessage { nestedMessages: Map } +export interface MessageWithMapMessageEncoder { + field1: boolean + nestedMessages: Map +} + export namespace MessageWithMapMessage { export interface MessageWithMapMessage$nestedMessagesEntry { key: string value?: NestedMessage } + export interface MessageWithMapMessage$nestedMessagesEntryEncoder { + key: string + value?: NestedMessageEncoder + } + export namespace MessageWithMapMessage$nestedMessagesEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -866,7 +901,7 @@ export namespace MessageWithMapMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithMapMessage$nestedMessagesEntry.codec()) } @@ -879,11 +914,11 @@ export namespace MessageWithMapMessage { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1026,7 +1061,7 @@ export namespace MessageWithMapMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithMapMessage.codec()) } @@ -1044,18 +1079,28 @@ export interface MessageWithPrimitiveMap { nestedStrings: Map } +export interface MessageWithPrimitiveMapEncoder { + field1: boolean + nestedStrings: Map +} + export namespace MessageWithPrimitiveMap { export interface MessageWithPrimitiveMap$nestedStringsEntry { key: string value: string } + export interface MessageWithPrimitiveMap$nestedStringsEntryEncoder { + key: string + value: string + } + export namespace MessageWithPrimitiveMap$nestedStringsEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1160,7 +1205,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap$nestedStringsEntry.codec()) } @@ -1173,11 +1218,11 @@ export namespace MessageWithPrimitiveMap { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1306,7 +1351,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap.codec()) } @@ -1332,7 +1377,7 @@ enum __ENUMValues { } export namespace ENUM { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__ENUMValues) } } @@ -1342,12 +1387,17 @@ export interface MessageWithRepeatedEnums { enums: ENUM[] } +export interface MessageWithRepeatedEnumsEncoder { + field1: boolean + enums: ENUM[] +} + export namespace MessageWithRepeatedEnums { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1471,7 +1521,7 @@ export namespace MessageWithRepeatedEnums { value: ENUM } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, MessageWithRepeatedEnums.codec()) } diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index 6f9be94..778a4b9 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -13,7 +13,7 @@ enum __AnEnumValues { } export namespace AnEnum { - export const codec = (): Codec => { + export const codec = (): Codec => { return enumeration(__AnEnumValues) } } @@ -22,12 +22,16 @@ export interface SubMessage { foo: string } +export interface SubMessageEncoder { + foo: string +} + export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -110,7 +114,7 @@ export namespace SubMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -144,12 +148,33 @@ export interface AllTheTypes { field18?: bigint } +export interface AllTheTypesEncoder { + field1?: boolean + field2?: number + field3?: bigint + field4?: number + field5?: bigint + field6?: number + field7?: bigint + field8?: number + field9?: number + field10?: string + field11?: Uint8Array + field12?: AnEnum + field13?: SubMessageEncoder + field14: string[] + field15?: number + field16?: bigint + field17?: number + field18?: bigint +} + export namespace AllTheTypes { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -620,7 +645,7 @@ export namespace AllTheTypes { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: Partial): Uint8Array { return encodeMessage(obj, AllTheTypes.codec()) } From 621e1bcf82716e9c703be336a872591cd164580c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 15 Sep 2026 16:42:28 +0200 Subject: [PATCH 2/7] chore: generate optional types --- .../src/implementations/protons/bench.ts | 12 +-- packages/protons/src/fields/field.ts | 2 +- packages/protons/src/fields/map-field.ts | 2 +- packages/protons/src/types/message.ts | 2 +- packages/protons/test/fixtures/basic.ts | 6 +- packages/protons/test/fixtures/bitswap.ts | 38 ++++---- packages/protons/test/fixtures/circuit.ts | 8 +- .../test/fixtures/custom-option-jstype.ts | 48 +++++----- packages/protons/test/fixtures/daemon.ts | 92 +++++++++---------- packages/protons/test/fixtures/dht.ts | 12 +-- packages/protons/test/fixtures/maps.ts | 46 +++++----- packages/protons/test/fixtures/noise.ts | 10 +- packages/protons/test/fixtures/oneof.ts | 16 ++-- packages/protons/test/fixtures/optional.ts | 4 +- packages/protons/test/fixtures/peer.ts | 18 ++-- packages/protons/test/fixtures/proto2.ts | 4 +- .../protons/test/fixtures/protons-options.ts | 14 +-- packages/protons/test/fixtures/repeated.ts | 18 ++-- packages/protons/test/fixtures/singular.ts | 40 ++++---- packages/protons/test/fixtures/streaming.ts | 50 +++++----- packages/protons/test/fixtures/test.ts | 8 +- 21 files changed, 225 insertions(+), 225 deletions(-) diff --git a/packages/protons-benchmark/src/implementations/protons/bench.ts b/packages/protons-benchmark/src/implementations/protons/bench.ts index 35d2166..af82213 100644 --- a/packages/protons-benchmark/src/implementations/protons/bench.ts +++ b/packages/protons-benchmark/src/implementations/protons/bench.ts @@ -96,7 +96,7 @@ export namespace Foo { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: FooEncoder): Uint8Array { return encodeMessage(obj, Foo.codec()) } @@ -215,7 +215,7 @@ export namespace Bar { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BarEncoder): Uint8Array { return encodeMessage(obj, Bar.codec()) } @@ -251,7 +251,7 @@ export interface Yo { } export interface YoEncoder { - lol: FOO[] + lol?: FOO[] } export namespace Yo { @@ -361,7 +361,7 @@ export namespace Yo { value: FOO } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: YoEncoder): Uint8Array { return encodeMessage(obj, Yo.codec()) } @@ -513,7 +513,7 @@ export namespace Lol { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: LolEncoder): Uint8Array { return encodeMessage(obj, Lol.codec()) } @@ -726,7 +726,7 @@ export namespace Test { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: TestEncoder): Uint8Array { return encodeMessage(obj, Test.codec()) } diff --git a/packages/protons/src/fields/field.ts b/packages/protons/src/fields/field.ts index 160c283..938a14f 100644 --- a/packages/protons/src/fields/field.ts +++ b/packages/protons/src/fields/field.ts @@ -137,7 +137,7 @@ export class Field implements MessageField { } getEncoderInterfaceField (parent: Parent, indent = ''): string { - return `${indent}${this.name}${this.optional ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` + return `${indent}${this.name}?: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/fields/map-field.ts b/packages/protons/src/fields/map-field.ts index b78efa4..93044d2 100644 --- a/packages/protons/src/fields/map-field.ts +++ b/packages/protons/src/fields/map-field.ts @@ -48,7 +48,7 @@ export class MapField extends Field { const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType.encode const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType.encode - return `${this.name}: Map<${keyType}, ${valueType}>` + return `${this.name}?: Map<${keyType}, ${valueType}>` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/types/message.ts b/packages/protons/src/types/message.ts index ce9169a..b5dbeb9 100644 --- a/packages/protons/src/types/message.ts +++ b/packages/protons/src/types/message.ts @@ -358,7 +358,7 @@ ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} return _codec }${this.formatStreamEvents(streamEvents)} - export function encode (obj: Partial<${this.pbType}Encoder>): Uint8Array { + export function encode (obj: ${this.pbType}Encoder): Uint8Array { return encodeMessage(obj, ${this.pbType}.codec()) } diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index edb23cc..0c8236e 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -9,7 +9,7 @@ export interface Basic { export interface BasicEncoder { foo?: string - num: number + num?: number } export namespace Basic { @@ -121,7 +121,7 @@ export namespace Basic { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BasicEncoder): Uint8Array { return encodeMessage(obj, Basic.codec()) } @@ -203,7 +203,7 @@ export namespace Empty { return _codec } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: EmptyEncoder): Uint8Array { return encodeMessage(obj, Empty.codec()) } diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index 867a74a..8536285 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -13,10 +13,10 @@ export interface Message { export interface MessageEncoder { wantlist?: Message.WantlistEncoder - blocks: Uint8Array[] - payload: Message.BlockEncoder[] - blockPresences: Message.BlockPresenceEncoder[] - pendingBytes: number + blocks?: Uint8Array[] + payload?: Message.BlockEncoder[] + blockPresences?: Message.BlockPresenceEncoder[] + pendingBytes?: number } export namespace Message { @@ -26,8 +26,8 @@ export namespace Message { } export interface WantlistEncoder { - entries: Message.Wantlist.EntryEncoder[] - full: boolean + entries?: Message.Wantlist.EntryEncoder[] + full?: boolean } export namespace Wantlist { @@ -56,11 +56,11 @@ export namespace Message { } export interface EntryEncoder { - block: Uint8Array - priority: number + block?: Uint8Array + priority?: number cancel?: boolean - wantType: Message.Wantlist.WantType - sendDontHave: boolean + wantType?: Message.Wantlist.WantType + sendDontHave?: boolean } export namespace Entry { @@ -238,7 +238,7 @@ export namespace Message { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: EntryEncoder): Uint8Array { return encodeMessage(obj, Entry.codec()) } @@ -422,7 +422,7 @@ export namespace Message { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: WantlistEncoder): Uint8Array { return encodeMessage(obj, Wantlist.codec()) } @@ -441,8 +441,8 @@ export namespace Message { } export interface BlockEncoder { - prefix: Uint8Array - data: Uint8Array + prefix?: Uint8Array + data?: Uint8Array } export namespace Block { @@ -555,7 +555,7 @@ export namespace Message { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BlockEncoder): Uint8Array { return encodeMessage(obj, Block.codec()) } @@ -590,8 +590,8 @@ export namespace Message { } export interface BlockPresenceEncoder { - cid: Uint8Array - type: Message.BlockPresenceType + cid?: Uint8Array + type?: Message.BlockPresenceType } export namespace BlockPresence { @@ -704,7 +704,7 @@ export namespace Message { value: Message.BlockPresenceType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BlockPresenceEncoder): Uint8Array { return encodeMessage(obj, BlockPresence.codec()) } @@ -1048,7 +1048,7 @@ export namespace Message { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageEncoder): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index 6d8910e..d45b331 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -88,8 +88,8 @@ export namespace CircuitRelay { } export interface PeerEncoder { - id: Uint8Array - addrs: Uint8Array[] + id?: Uint8Array + addrs?: Uint8Array[] } export namespace Peer { @@ -221,7 +221,7 @@ export namespace CircuitRelay { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerEncoder): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -418,7 +418,7 @@ export namespace CircuitRelay { value: CircuitRelay.Status } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CircuitRelayEncoder): Uint8Array { return encodeMessage(obj, CircuitRelay.codec()) } diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index 3f3ee59..2dde2c4 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -14,14 +14,14 @@ export interface CustomOptionNumber { } export interface CustomOptionNumberEncoder { - num: number - i64: number - ui64: number - si64: number - f64: number - sf64: number - i64Array: number[] - i64Map: Map + num?: number + i64?: number + ui64?: number + si64?: number + f64?: number + sf64?: number + i64Array?: number[] + i64Map?: Map } export namespace CustomOptionNumber { @@ -31,8 +31,8 @@ export namespace CustomOptionNumber { } export interface CustomOptionNumber$i64MapEntryEncoder { - key: number - value: number + key?: number + value?: number } export namespace CustomOptionNumber$i64MapEntry { @@ -145,7 +145,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionNumber$i64MapEntryEncoder): Uint8Array { return encodeMessage(obj, CustomOptionNumber$i64MapEntry.codec()) } @@ -439,7 +439,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionNumberEncoder): Uint8Array { return encodeMessage(obj, CustomOptionNumber.codec()) } @@ -464,14 +464,14 @@ export interface CustomOptionString { } export interface CustomOptionStringEncoder { - num: number - i64: string - ui64: string - si64: string - f64: string - sf64: string - i64Array: string[] - i64Map: Map + num?: number + i64?: string + ui64?: string + si64?: string + f64?: string + sf64?: string + i64Array?: string[] + i64Map?: Map } export namespace CustomOptionString { @@ -481,8 +481,8 @@ export namespace CustomOptionString { } export interface CustomOptionString$i64MapEntryEncoder { - key: string - value: string + key?: string + value?: string } export namespace CustomOptionString$i64MapEntry { @@ -595,7 +595,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionString$i64MapEntryEncoder): Uint8Array { return encodeMessage(obj, CustomOptionString$i64MapEntry.codec()) } @@ -889,7 +889,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionStringEncoder): Uint8Array { return encodeMessage(obj, CustomOptionString.codec()) } diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index e6653c0..2e75396 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -16,7 +16,7 @@ export interface Request { } export interface RequestEncoder { - type: Request.Type + type?: Request.Type connect?: ConnectRequestEncoder streamOpen?: StreamOpenRequestEncoder streamHandler?: StreamHandlerRequestEncoder @@ -505,7 +505,7 @@ export namespace Request { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: RequestEncoder): Uint8Array { return encodeMessage(obj, Request.codec()) } @@ -530,12 +530,12 @@ export interface Response { } export interface ResponseEncoder { - type: Response.Type + type?: Response.Type error?: ErrorResponseEncoder streamInfo?: StreamInfoEncoder identify?: IdentifyResponseEncoder dht?: DHTResponseEncoder - peers: PeerInfoEncoder[] + peers?: PeerInfoEncoder[] pubsub?: PSResponseEncoder peerStore?: PeerstoreResponseEncoder } @@ -979,7 +979,7 @@ export namespace Response { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ResponseEncoder): Uint8Array { return encodeMessage(obj, Response.codec()) } @@ -998,8 +998,8 @@ export interface IdentifyResponse { } export interface IdentifyResponseEncoder { - id: Uint8Array - addrs: Uint8Array[] + id?: Uint8Array + addrs?: Uint8Array[] } export namespace IdentifyResponse { @@ -1131,7 +1131,7 @@ export namespace IdentifyResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: IdentifyResponseEncoder): Uint8Array { return encodeMessage(obj, IdentifyResponse.codec()) } @@ -1151,8 +1151,8 @@ export interface ConnectRequest { } export interface ConnectRequestEncoder { - peer: Uint8Array - addrs: Uint8Array[] + peer?: Uint8Array + addrs?: Uint8Array[] timeout?: bigint } @@ -1306,7 +1306,7 @@ export namespace ConnectRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ConnectRequestEncoder): Uint8Array { return encodeMessage(obj, ConnectRequest.codec()) } @@ -1326,8 +1326,8 @@ export interface StreamOpenRequest { } export interface StreamOpenRequestEncoder { - peer: Uint8Array - proto: string[] + peer?: Uint8Array + proto?: string[] timeout?: bigint } @@ -1481,7 +1481,7 @@ export namespace StreamOpenRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamOpenRequestEncoder): Uint8Array { return encodeMessage(obj, StreamOpenRequest.codec()) } @@ -1500,8 +1500,8 @@ export interface StreamHandlerRequest { } export interface StreamHandlerRequestEncoder { - addr: Uint8Array - proto: string[] + addr?: Uint8Array + proto?: string[] } export namespace StreamHandlerRequest { @@ -1633,7 +1633,7 @@ export namespace StreamHandlerRequest { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamHandlerRequestEncoder): Uint8Array { return encodeMessage(obj, StreamHandlerRequest.codec()) } @@ -1651,7 +1651,7 @@ export interface ErrorResponse { } export interface ErrorResponseEncoder { - msg: string + msg?: string } export namespace ErrorResponse { @@ -1742,7 +1742,7 @@ export namespace ErrorResponse { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ErrorResponseEncoder): Uint8Array { return encodeMessage(obj, ErrorResponse.codec()) } @@ -1762,9 +1762,9 @@ export interface StreamInfo { } export interface StreamInfoEncoder { - peer: Uint8Array - addr: Uint8Array - proto: string + peer?: Uint8Array + addr?: Uint8Array + proto?: string } export namespace StreamInfo { @@ -1899,7 +1899,7 @@ export namespace StreamInfo { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamInfoEncoder): Uint8Array { return encodeMessage(obj, StreamInfo.codec()) } @@ -1923,7 +1923,7 @@ export interface DHTRequest { } export interface DHTRequestEncoder { - type: DHTRequest.Type + type?: DHTRequest.Type peer?: Uint8Array cid?: Uint8Array key?: Uint8Array @@ -2176,7 +2176,7 @@ export namespace DHTRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: DHTRequestEncoder): Uint8Array { return encodeMessage(obj, DHTRequest.codec()) } @@ -2196,7 +2196,7 @@ export interface DHTResponse { } export interface DHTResponseEncoder { - type: DHTResponse.Type + type?: DHTResponse.Type peer?: PeerInfoEncoder value?: Uint8Array } @@ -2367,7 +2367,7 @@ export namespace DHTResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: DHTResponseEncoder): Uint8Array { return encodeMessage(obj, DHTResponse.codec()) } @@ -2386,8 +2386,8 @@ export interface PeerInfo { } export interface PeerInfoEncoder { - id: Uint8Array - addrs: Uint8Array[] + id?: Uint8Array + addrs?: Uint8Array[] } export namespace PeerInfo { @@ -2519,7 +2519,7 @@ export namespace PeerInfo { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerInfoEncoder): Uint8Array { return encodeMessage(obj, PeerInfo.codec()) } @@ -2540,7 +2540,7 @@ export interface ConnManagerRequest { } export interface ConnManagerRequestEncoder { - type: ConnManagerRequest.Type + type?: ConnManagerRequest.Type peer?: Uint8Array tag?: string weight?: bigint @@ -2715,7 +2715,7 @@ export namespace ConnManagerRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ConnManagerRequestEncoder): Uint8Array { return encodeMessage(obj, ConnManagerRequest.codec()) } @@ -2733,7 +2733,7 @@ export interface DisconnectRequest { } export interface DisconnectRequestEncoder { - peer: Uint8Array + peer?: Uint8Array } export namespace DisconnectRequest { @@ -2824,7 +2824,7 @@ export namespace DisconnectRequest { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: DisconnectRequestEncoder): Uint8Array { return encodeMessage(obj, DisconnectRequest.codec()) } @@ -2844,7 +2844,7 @@ export interface PSRequest { } export interface PSRequestEncoder { - type: PSRequest.Type + type?: PSRequest.Type topic?: string data?: Uint8Array } @@ -2999,7 +2999,7 @@ export namespace PSRequest { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PSRequestEncoder): Uint8Array { return encodeMessage(obj, PSRequest.codec()) } @@ -3025,7 +3025,7 @@ export interface PSMessageEncoder { from?: Uint8Array data?: Uint8Array seqno?: Uint8Array - topicIDs: string[] + topicIDs?: string[] signature?: Uint8Array key?: Uint8Array } @@ -3242,7 +3242,7 @@ export namespace PSMessage { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PSMessageEncoder): Uint8Array { return encodeMessage(obj, PSMessage.codec()) } @@ -3261,8 +3261,8 @@ export interface PSResponse { } export interface PSResponseEncoder { - topics: string[] - peerIDs: Uint8Array[] + topics?: string[] + peerIDs?: Uint8Array[] } export namespace PSResponse { @@ -3410,7 +3410,7 @@ export namespace PSResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PSResponseEncoder): Uint8Array { return encodeMessage(obj, PSResponse.codec()) } @@ -3430,9 +3430,9 @@ export interface PeerstoreRequest { } export interface PeerstoreRequestEncoder { - type: PeerstoreRequest.Type + type?: PeerstoreRequest.Type id?: Uint8Array - protos: string[] + protos?: string[] } export namespace PeerstoreRequest { @@ -3603,7 +3603,7 @@ export namespace PeerstoreRequest { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerstoreRequestEncoder): Uint8Array { return encodeMessage(obj, PeerstoreRequest.codec()) } @@ -3623,7 +3623,7 @@ export interface PeerstoreResponse { export interface PeerstoreResponseEncoder { peer?: PeerInfoEncoder - protos: string[] + protos?: string[] } export namespace PeerstoreResponse { @@ -3772,7 +3772,7 @@ export namespace PeerstoreResponse { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerstoreResponseEncoder): Uint8Array { return encodeMessage(obj, PeerstoreResponse.codec()) } diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index af0ad2b..973c3fb 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -188,7 +188,7 @@ export namespace Record { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: RecordEncoder): Uint8Array { return encodeMessage(obj, Record.codec()) } @@ -215,8 +215,8 @@ export interface MessageEncoder { clusterLevelRaw?: number key?: Uint8Array record?: Uint8Array - closerPeers: Message.PeerEncoder[] - providerPeers: Message.PeerEncoder[] + closerPeers?: Message.PeerEncoder[] + providerPeers?: Message.PeerEncoder[] } export namespace Message { @@ -272,7 +272,7 @@ export namespace Message { export interface PeerEncoder { id?: Uint8Array - addrs: Uint8Array[] + addrs?: Uint8Array[] connection?: Message.ConnectionType } @@ -425,7 +425,7 @@ export namespace Message { value: Message.ConnectionType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerEncoder): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -728,7 +728,7 @@ export namespace Message { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageEncoder): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index bf4649e..ce23208 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -26,8 +26,8 @@ export interface SubMessage { } export interface SubMessageEncoder { - foo: string - bar: number[] + foo?: string + bar?: number[] } export namespace SubMessage { @@ -159,7 +159,7 @@ export namespace SubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageEncoder): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -181,11 +181,11 @@ export interface MapTypes { } export interface MapTypesEncoder { - stringMap: Map - intMap: Map - boolMap: Map - messageMap: Map - enumMap: Map + stringMap?: Map + intMap?: Map + boolMap?: Map + messageMap?: Map + enumMap?: Map } export namespace MapTypes { @@ -195,8 +195,8 @@ export namespace MapTypes { } export interface MapTypes$stringMapEntryEncoder { - key: string - value: string + key?: string + value?: string } export namespace MapTypes$stringMapEntry { @@ -309,7 +309,7 @@ export namespace MapTypes { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$stringMapEntryEncoder): Uint8Array { return encodeMessage(obj, MapTypes$stringMapEntry.codec()) } @@ -328,8 +328,8 @@ export namespace MapTypes { } export interface MapTypes$intMapEntryEncoder { - key: number - value: number + key?: number + value?: number } export namespace MapTypes$intMapEntry { @@ -442,7 +442,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$intMapEntryEncoder): Uint8Array { return encodeMessage(obj, MapTypes$intMapEntry.codec()) } @@ -461,8 +461,8 @@ export namespace MapTypes { } export interface MapTypes$boolMapEntryEncoder { - key: boolean - value: boolean + key?: boolean + value?: boolean } export namespace MapTypes$boolMapEntry { @@ -575,7 +575,7 @@ export namespace MapTypes { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$boolMapEntryEncoder): Uint8Array { return encodeMessage(obj, MapTypes$boolMapEntry.codec()) } @@ -594,7 +594,7 @@ export namespace MapTypes { } export interface MapTypes$messageMapEntryEncoder { - key: string + key?: string value?: SubMessageEncoder } @@ -725,7 +725,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$messageMapEntryEncoder): Uint8Array { return encodeMessage(obj, MapTypes$messageMapEntry.codec()) } @@ -744,8 +744,8 @@ export namespace MapTypes { } export interface MapTypes$enumMapEntryEncoder { - key: string - value: EnumValue + key?: string + value?: EnumValue } export namespace MapTypes$enumMapEntry { @@ -858,7 +858,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$enumMapEntryEncoder): Uint8Array { return encodeMessage(obj, MapTypes$enumMapEntry.codec()) } @@ -1175,7 +1175,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypesEncoder): Uint8Array { return encodeMessage(obj, MapTypes.codec()) } diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index de05547..8a03c55 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -15,9 +15,9 @@ export namespace pb { } export interface NoiseHandshakePayloadEncoder { - identityKey: Uint8Array - identitySig: Uint8Array - data: Uint8Array + identityKey?: Uint8Array + identitySig?: Uint8Array + data?: Uint8Array } export namespace NoiseHandshakePayload { @@ -152,7 +152,7 @@ export namespace pb { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: NoiseHandshakePayloadEncoder): Uint8Array { return encodeMessage(obj, NoiseHandshakePayload.codec()) } @@ -229,7 +229,7 @@ export namespace pb { return _codec } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: pbEncoder): Uint8Array { return encodeMessage(obj, pb.codec()) } diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index a9f0f47..2241921 100644 --- a/packages/protons/test/fixtures/oneof.ts +++ b/packages/protons/test/fixtures/oneof.ts @@ -31,7 +31,7 @@ export interface OneOfMessageEncoder { fieldTwo?: string fieldThree?: EnumType fieldFour?: EnumType - fieldFive: string + fieldFive?: string } export namespace OneOfMessage { @@ -240,7 +240,7 @@ export namespace OneOfMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: OneOfMessageEncoder): Uint8Array { return encodeMessage(obj, OneOfMessage.codec()) } @@ -262,11 +262,11 @@ export interface MessageWithoutOneOfs { } export interface MessageWithoutOneOfsEncoder { - fieldOne: string - fieldTwo: string - fieldThree: EnumType - fieldFour: EnumType - fieldFive: string + fieldOne?: string + fieldTwo?: string + fieldThree?: EnumType + fieldFour?: EnumType + fieldFive?: string } export namespace MessageWithoutOneOfs { @@ -445,7 +445,7 @@ export namespace MessageWithoutOneOfs { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithoutOneOfsEncoder): Uint8Array { return encodeMessage(obj, MessageWithoutOneOfs.codec()) } diff --git a/packages/protons/test/fixtures/optional.ts b/packages/protons/test/fixtures/optional.ts index 51379a0..f802320 100644 --- a/packages/protons/test/fixtures/optional.ts +++ b/packages/protons/test/fixtures/optional.ts @@ -137,7 +137,7 @@ export namespace OptionalSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: OptionalSubMessageEncoder): Uint8Array { return encodeMessage(obj, OptionalSubMessage.codec()) } @@ -629,7 +629,7 @@ export namespace Optional { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: OptionalEncoder): Uint8Array { return encodeMessage(obj, Optional.codec()) } diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index c605034..a97cbee 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -12,9 +12,9 @@ export interface Peer { } export interface PeerEncoder { - addresses: AddressEncoder[] - protocols: string[] - metadata: MetadataEncoder[] + addresses?: AddressEncoder[] + protocols?: string[] + metadata?: MetadataEncoder[] pubKey?: Uint8Array peerRecordEnvelope?: Uint8Array } @@ -294,7 +294,7 @@ export namespace Peer { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerEncoder): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -313,7 +313,7 @@ export interface Address { } export interface AddressEncoder { - multiaddr: Uint8Array + multiaddr?: Uint8Array isCertified?: boolean } @@ -426,7 +426,7 @@ export namespace Address { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: AddressEncoder): Uint8Array { return encodeMessage(obj, Address.codec()) } @@ -445,8 +445,8 @@ export interface Metadata { } export interface MetadataEncoder { - key: string - value: Uint8Array + key?: string + value?: Uint8Array } export namespace Metadata { @@ -559,7 +559,7 @@ export namespace Metadata { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MetadataEncoder): Uint8Array { return encodeMessage(obj, Metadata.codec()) } diff --git a/packages/protons/test/fixtures/proto2.ts b/packages/protons/test/fixtures/proto2.ts index b370afb..2efce30 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -7,7 +7,7 @@ export interface MessageWithRequired { } export interface MessageWithRequiredEncoder { - scalarField: number + scalarField?: number } export namespace MessageWithRequired { @@ -98,7 +98,7 @@ export namespace MessageWithRequired { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithRequiredEncoder): Uint8Array { return encodeMessage(obj, MessageWithRequired.codec()) } diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index b095e72..6de89db 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -7,7 +7,7 @@ export interface MessageWithSizeLimitedRepeatedField { } export interface MessageWithSizeLimitedRepeatedFieldEncoder { - repeatedField: string[] + repeatedField?: string[] } export namespace MessageWithSizeLimitedRepeatedField { @@ -125,7 +125,7 @@ export namespace MessageWithSizeLimitedRepeatedField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithSizeLimitedRepeatedFieldEncoder): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedRepeatedField.codec()) } @@ -143,7 +143,7 @@ export interface MessageWithSizeLimitedMap { } export interface MessageWithSizeLimitedMapEncoder { - mapField: Map + mapField?: Map } export namespace MessageWithSizeLimitedMap { @@ -153,8 +153,8 @@ export namespace MessageWithSizeLimitedMap { } export interface MessageWithSizeLimitedMap$mapFieldEntryEncoder { - key: string - value: string + key?: string + value?: string } export namespace MessageWithSizeLimitedMap$mapFieldEntry { @@ -267,7 +267,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithSizeLimitedMap$mapFieldEntryEncoder): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap$mapFieldEntry.codec()) } @@ -399,7 +399,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithSizeLimitedMapEncoder): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap.codec()) } diff --git a/packages/protons/test/fixtures/repeated.ts b/packages/protons/test/fixtures/repeated.ts index c2997d3..2d843a8 100644 --- a/packages/protons/test/fixtures/repeated.ts +++ b/packages/protons/test/fixtures/repeated.ts @@ -8,7 +8,7 @@ export interface SubSubMessage { } export interface SubSubMessageEncoder { - foo: string[] + foo?: string[] nonRepeating?: number } @@ -140,7 +140,7 @@ export namespace SubSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubSubMessageEncoder): Uint8Array { return encodeMessage(obj, SubSubMessage.codec()) } @@ -161,10 +161,10 @@ export interface SubMessage { } export interface SubMessageEncoder { - foo: string[] + foo?: string[] nonRepeating?: number message?: SubSubMessageEncoder - messages: SubSubMessageEncoder[] + messages?: SubSubMessageEncoder[] } export namespace SubMessage { @@ -397,7 +397,7 @@ export namespace SubMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageEncoder): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -419,9 +419,9 @@ export interface RepeatedTypes { } export interface RepeatedTypesEncoder { - number: number[] - limitedNumber: number[] - messages: SubMessageEncoder[] + number?: number[] + limitedNumber?: number[] + messages?: SubMessageEncoder[] message?: SubMessageEncoder nonRepeating?: number } @@ -799,7 +799,7 @@ export namespace RepeatedTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: RepeatedTypesEncoder): Uint8Array { return encodeMessage(obj, RepeatedTypes.codec()) } diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index c303a35..190494c 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -27,8 +27,8 @@ export interface SingularSubMessage { } export interface SingularSubMessageEncoder { - foo: string - bar: number + foo?: string + bar?: number } export namespace SingularSubMessage { @@ -141,7 +141,7 @@ export namespace SingularSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SingularSubMessageEncoder): Uint8Array { return encodeMessage(obj, SingularSubMessage.codec()) } @@ -175,22 +175,22 @@ export interface Singular { } export interface SingularEncoder { - double: number - float: number - int32: number - int64: bigint - uint32: number - uint64: bigint - sint32: number - sint64: bigint - fixed32: number - fixed64: bigint - sfixed32: number - sfixed64: bigint - bool: boolean - string: string - bytes: Uint8Array - enum: SingularEnum + double?: number + float?: number + int32?: number + int64?: bigint + uint32?: number + uint64?: bigint + sint32?: number + sint64?: bigint + fixed32?: number + fixed64?: bigint + sfixed32?: number + sfixed64?: bigint + bool?: boolean + string?: string + bytes?: Uint8Array + enum?: SingularEnum subMessage?: SingularSubMessageEncoder } @@ -650,7 +650,7 @@ export namespace Singular { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SingularEncoder): Uint8Array { return encodeMessage(obj, Singular.codec()) } diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index 1051601..fc9044d 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -11,7 +11,7 @@ export interface MessageWithArrayField { export interface MessageWithArrayFieldEncoder { field1?: boolean field2?: number - arr: string[] + arr?: string[] } export namespace MessageWithArrayField { @@ -163,7 +163,7 @@ export namespace MessageWithArrayField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithArrayFieldEncoder): Uint8Array { return encodeMessage(obj, MessageWithArrayField.codec()) } @@ -181,7 +181,7 @@ export interface NestedMessage { } export interface NestedMessageEncoder { - nestedValue: string + nestedValue?: string } export namespace NestedMessage { @@ -272,7 +272,7 @@ export namespace NestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: NestedMessageEncoder): Uint8Array { return encodeMessage(obj, NestedMessage.codec()) } @@ -291,7 +291,7 @@ export interface MessageWithNestedMessage { } export interface MessageWithNestedMessageEncoder { - field1: boolean + field1?: boolean nestedMessage?: NestedMessageEncoder } @@ -416,7 +416,7 @@ export namespace MessageWithNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithNestedMessageEncoder): Uint8Array { return encodeMessage(obj, MessageWithNestedMessage.codec()) } @@ -435,7 +435,7 @@ export interface MessageWithDeeplyNestedMessage { } export interface MessageWithDeeplyNestedMessageEncoder { - field1: boolean + field1?: boolean nestedMessage?: MessageWithNestedMessageEncoder } @@ -575,7 +575,7 @@ export namespace MessageWithDeeplyNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithDeeplyNestedMessageEncoder): Uint8Array { return encodeMessage(obj, MessageWithDeeplyNestedMessage.codec()) } @@ -594,8 +594,8 @@ export interface MessageWithRepeatedMessage { } export interface MessageWithRepeatedMessageEncoder { - field1: boolean - nestedMessages: NestedMessageEncoder[] + field1?: boolean + nestedMessages?: NestedMessageEncoder[] } export namespace MessageWithRepeatedMessage { @@ -746,7 +746,7 @@ export namespace MessageWithRepeatedMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithRepeatedMessageEncoder): Uint8Array { return encodeMessage(obj, MessageWithRepeatedMessage.codec()) } @@ -765,8 +765,8 @@ export interface MessageWithMapMessage { } export interface MessageWithMapMessageEncoder { - field1: boolean - nestedMessages: Map + field1?: boolean + nestedMessages?: Map } export namespace MessageWithMapMessage { @@ -776,7 +776,7 @@ export namespace MessageWithMapMessage { } export interface MessageWithMapMessage$nestedMessagesEntryEncoder { - key: string + key?: string value?: NestedMessageEncoder } @@ -901,7 +901,7 @@ export namespace MessageWithMapMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithMapMessage$nestedMessagesEntryEncoder): Uint8Array { return encodeMessage(obj, MessageWithMapMessage$nestedMessagesEntry.codec()) } @@ -1061,7 +1061,7 @@ export namespace MessageWithMapMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithMapMessageEncoder): Uint8Array { return encodeMessage(obj, MessageWithMapMessage.codec()) } @@ -1080,8 +1080,8 @@ export interface MessageWithPrimitiveMap { } export interface MessageWithPrimitiveMapEncoder { - field1: boolean - nestedStrings: Map + field1?: boolean + nestedStrings?: Map } export namespace MessageWithPrimitiveMap { @@ -1091,8 +1091,8 @@ export namespace MessageWithPrimitiveMap { } export interface MessageWithPrimitiveMap$nestedStringsEntryEncoder { - key: string - value: string + key?: string + value?: string } export namespace MessageWithPrimitiveMap$nestedStringsEntry { @@ -1205,7 +1205,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithPrimitiveMap$nestedStringsEntryEncoder): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap$nestedStringsEntry.codec()) } @@ -1351,7 +1351,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithPrimitiveMapEncoder): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap.codec()) } @@ -1388,8 +1388,8 @@ export interface MessageWithRepeatedEnums { } export interface MessageWithRepeatedEnumsEncoder { - field1: boolean - enums: ENUM[] + field1?: boolean + enums?: ENUM[] } export namespace MessageWithRepeatedEnums { @@ -1521,7 +1521,7 @@ export namespace MessageWithRepeatedEnums { value: ENUM } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithRepeatedEnumsEncoder): Uint8Array { return encodeMessage(obj, MessageWithRepeatedEnums.codec()) } diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index 778a4b9..e48a80e 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -23,7 +23,7 @@ export interface SubMessage { } export interface SubMessageEncoder { - foo: string + foo?: string } export namespace SubMessage { @@ -114,7 +114,7 @@ export namespace SubMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageEncoder): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -162,7 +162,7 @@ export interface AllTheTypesEncoder { field11?: Uint8Array field12?: AnEnum field13?: SubMessageEncoder - field14: string[] + field14?: string[] field15?: number field16?: bigint field17?: number @@ -645,7 +645,7 @@ export namespace AllTheTypes { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: AllTheTypesEncoder): Uint8Array { return encodeMessage(obj, AllTheTypes.codec()) } From 9f28f3ca825ad0e507533b35d1acb12006fe507e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 15 Sep 2026 16:44:22 +0200 Subject: [PATCH 3/7] chore: remove partial --- packages/protons-runtime/src/codec.ts | 2 +- packages/protons-runtime/src/encode.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/protons-runtime/src/codec.ts b/packages/protons-runtime/src/codec.ts index a2c09a4..6757322 100644 --- a/packages/protons-runtime/src/codec.ts +++ b/packages/protons-runtime/src/codec.ts @@ -16,7 +16,7 @@ export interface EncodeOptions { } export interface EncodeFunction { - (value: Partial, writer: Writer, opts?: EncodeOptions): void + (value: T, writer: Writer, opts?: EncodeOptions): void } // protobuf types that contain multiple values diff --git a/packages/protons-runtime/src/encode.ts b/packages/protons-runtime/src/encode.ts index ca9db24..51c121b 100644 --- a/packages/protons-runtime/src/encode.ts +++ b/packages/protons-runtime/src/encode.ts @@ -1,7 +1,7 @@ import { createWriter } from './utils/writer.ts' import type { Codec } from './codec.ts' -export function encodeMessage (message: Partial, codec: Pick, 'encode'>): Uint8Array { +export function encodeMessage (message: E, codec: Pick, 'encode'>): Uint8Array { const w = createWriter() codec.encode(message, w, { From 79b758496cfe62c6721ddf9417a45704769b5c66 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 08:55:55 +0300 Subject: [PATCH 4/7] chore: rename generated input class --- packages/protons/src/fields/array-field.ts | 2 +- packages/protons/src/fields/field.ts | 2 +- packages/protons/src/types/message.ts | 14 +- packages/protons/src/types/module.ts | 2 +- packages/protons/test/fixtures/basic.ts | 22 +- packages/protons/test/fixtures/bitswap.ts | 78 +++--- packages/protons/test/fixtures/circuit.ts | 26 +- .../test/fixtures/custom-option-jstype.ts | 72 ++--- packages/protons/test/fixtures/daemon.ts | 248 +++++++++--------- packages/protons/test/fixtures/dht.ts | 34 +-- packages/protons/test/fixtures/maps.ts | 94 +++---- packages/protons/test/fixtures/noise.ts | 26 +- packages/protons/test/fixtures/oneof.ts | 32 +-- packages/protons/test/fixtures/optional.ts | 22 +- packages/protons/test/fixtures/peer.ts | 40 +-- packages/protons/test/fixtures/proto2.ts | 12 +- .../protons/test/fixtures/protons-options.ts | 34 +-- packages/protons/test/fixtures/repeated.ts | 38 +-- packages/protons/test/fixtures/singular.ts | 58 ++-- packages/protons/test/fixtures/streaming.ts | 130 ++++----- packages/protons/test/fixtures/test.ts | 24 +- 21 files changed, 505 insertions(+), 505 deletions(-) diff --git a/packages/protons/src/fields/array-field.ts b/packages/protons/src/fields/array-field.ts index ccaecd7..a947dba 100644 --- a/packages/protons/src/fields/array-field.ts +++ b/packages/protons/src/fields/array-field.ts @@ -26,7 +26,7 @@ export class ArrayField extends Field { } getEncoderInterfaceField (parent: Parent, indent = ''): string { - return `${super.getEncoderInterfaceField(parent, indent)}[]` + return `${indent}${this.name}?: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}[]` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/fields/field.ts b/packages/protons/src/fields/field.ts index 938a14f..0ac9202 100644 --- a/packages/protons/src/fields/field.ts +++ b/packages/protons/src/fields/field.ts @@ -137,7 +137,7 @@ export class Field implements MessageField { } getEncoderInterfaceField (parent: Parent, indent = ''): string { - return `${indent}${this.name}?: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` + return `${indent}${this.name}${(this.optional) ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` } getDefaultField (parent: Parent): string { diff --git a/packages/protons/src/types/message.ts b/packages/protons/src/types/message.ts index b5dbeb9..d948f84 100644 --- a/packages/protons/src/types/message.ts +++ b/packages/protons/src/types/message.ts @@ -94,7 +94,7 @@ export class Message implements Type { Object.entries(def.nested ?? {}).forEach(([name, def]) => { if (isMessageDef(def)) { this.nested[name] = new Message(name, { - encode: `${this.jsType.decode}.${name}Encoder`, + encode: `${this.jsType.decode}.${name}Input`, decode: `${this.jsType.decode}.${name}` }, def, this) } else { @@ -265,7 +265,7 @@ ${indent} }` if (decoderFields.length === 0) { interfaceDef = ` -export interface ${this.pbType}Encoder {} +export interface ${this.pbType}Input {} export interface ${this.pbType} {}` } else { @@ -274,7 +274,7 @@ export interface ${this.pbType} { ${decoderFields.join('\n ').trim()} } -export interface ${this.pbType}Encoder { +export interface ${this.pbType}Input { ${encoderFields.join('\n ').trim()} }` } @@ -294,11 +294,11 @@ export interface ${this.pbType}Encoder { } interfaceCodecDef = ` - let _codec: Codec<${this.pbType}, ${this.pbType}Encoder> + let _codec: Codec<${this.pbType}, ${this.pbType}Input> - export const codec = (): Codec<${this.pbType}, ${this.pbType}Encoder> => { + export const codec = (): Codec<${this.pbType}, ${this.pbType}Input> => { if (_codec == null) { - _codec = message<${this.pbType}, ${this.pbType}Encoder>((obj, w, opts = {}) => { + _codec = message<${this.pbType}, ${this.pbType}Input>((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() }${enforceOneOfEncoding}${this.formatFields(encodeFields)} @@ -358,7 +358,7 @@ ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} return _codec }${this.formatStreamEvents(streamEvents)} - export function encode (obj: ${this.pbType}Encoder): Uint8Array { + export function encode (obj: ${this.pbType}Input): Uint8Array { return encodeMessage(obj, ${this.pbType}.codec()) } diff --git a/packages/protons/src/types/module.ts b/packages/protons/src/types/module.ts index f269aa0..23b16de 100644 --- a/packages/protons/src/types/module.ts +++ b/packages/protons/src/types/module.ts @@ -114,7 +114,7 @@ export class Module { }, def) } else { type = new Message(name, { - encode: `${name}Encoder`, + encode: `${name}Input`, decode: name }, def, this) } diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index 0c8236e..ab7871b 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -7,17 +7,17 @@ export interface Basic { num: number } -export interface BasicEncoder { +export interface BasicInput { foo?: string - num?: number + num: number } export namespace Basic { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -121,7 +121,7 @@ export namespace Basic { value: number } - export function encode (obj: BasicEncoder): Uint8Array { + export function encode (obj: BasicInput): Uint8Array { return encodeMessage(obj, Basic.codec()) } @@ -134,16 +134,16 @@ export namespace Basic { } } -export interface EmptyEncoder {} +export interface EmptyInput {} export interface Empty {} export namespace Empty { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -203,7 +203,7 @@ export namespace Empty { return _codec } - export function encode (obj: EmptyEncoder): Uint8Array { + export function encode (obj: EmptyInput): Uint8Array { return encodeMessage(obj, Empty.codec()) } diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index 8536285..7a787f3 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -11,12 +11,12 @@ export interface Message { pendingBytes: number } -export interface MessageEncoder { - wantlist?: Message.WantlistEncoder +export interface MessageInput { + wantlist?: Message.WantlistInput blocks?: Uint8Array[] - payload?: Message.BlockEncoder[] - blockPresences?: Message.BlockPresenceEncoder[] - pendingBytes?: number + payload?: Message.BlockInput[] + blockPresences?: Message.BlockPresenceInput[] + pendingBytes: number } export namespace Message { @@ -25,9 +25,9 @@ export namespace Message { full: boolean } - export interface WantlistEncoder { - entries?: Message.Wantlist.EntryEncoder[] - full?: boolean + export interface WantlistInput { + entries?: Message.Wantlist.EntryInput[] + full: boolean } export namespace Wantlist { @@ -55,20 +55,20 @@ export namespace Message { sendDontHave: boolean } - export interface EntryEncoder { - block?: Uint8Array - priority?: number + export interface EntryInput { + block: Uint8Array + priority: number cancel?: boolean - wantType?: Message.Wantlist.WantType - sendDontHave?: boolean + wantType: Message.Wantlist.WantType + sendDontHave: boolean } export namespace Entry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -238,7 +238,7 @@ export namespace Message { value: boolean } - export function encode (obj: EntryEncoder): Uint8Array { + export function encode (obj: EntryInput): Uint8Array { return encodeMessage(obj, Entry.codec()) } @@ -251,11 +251,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -422,7 +422,7 @@ export namespace Message { value: boolean } - export function encode (obj: WantlistEncoder): Uint8Array { + export function encode (obj: WantlistInput): Uint8Array { return encodeMessage(obj, Wantlist.codec()) } @@ -440,17 +440,17 @@ export namespace Message { data: Uint8Array } - export interface BlockEncoder { - prefix?: Uint8Array - data?: Uint8Array + export interface BlockInput { + prefix: Uint8Array + data: Uint8Array } export namespace Block { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -555,7 +555,7 @@ export namespace Message { value: Uint8Array } - export function encode (obj: BlockEncoder): Uint8Array { + export function encode (obj: BlockInput): Uint8Array { return encodeMessage(obj, Block.codec()) } @@ -589,17 +589,17 @@ export namespace Message { type: Message.BlockPresenceType } - export interface BlockPresenceEncoder { - cid?: Uint8Array - type?: Message.BlockPresenceType + export interface BlockPresenceInput { + cid: Uint8Array + type: Message.BlockPresenceType } export namespace BlockPresence { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -704,7 +704,7 @@ export namespace Message { value: Message.BlockPresenceType } - export function encode (obj: BlockPresenceEncoder): Uint8Array { + export function encode (obj: BlockPresenceInput): Uint8Array { return encodeMessage(obj, BlockPresence.codec()) } @@ -717,11 +717,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1048,7 +1048,7 @@ export namespace Message { value: number } - export function encode (obj: MessageEncoder): Uint8Array { + export function encode (obj: MessageInput): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index d45b331..15b2495 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -10,10 +10,10 @@ export interface CircuitRelay { code?: CircuitRelay.Status } -export interface CircuitRelayEncoder { +export interface CircuitRelayInput { type?: CircuitRelay.Type - srcPeer?: CircuitRelay.PeerEncoder - dstPeer?: CircuitRelay.PeerEncoder + srcPeer?: CircuitRelay.PeerInput + dstPeer?: CircuitRelay.PeerInput code?: CircuitRelay.Status } @@ -87,17 +87,17 @@ export namespace CircuitRelay { addrs: Uint8Array[] } - export interface PeerEncoder { - id?: Uint8Array + export interface PeerInput { + id: Uint8Array addrs?: Uint8Array[] } export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -221,7 +221,7 @@ export namespace CircuitRelay { value: Uint8Array } - export function encode (obj: PeerEncoder): Uint8Array { + export function encode (obj: PeerInput): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -234,11 +234,11 @@ export namespace CircuitRelay { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -418,7 +418,7 @@ export namespace CircuitRelay { value: CircuitRelay.Status } - export function encode (obj: CircuitRelayEncoder): Uint8Array { + export function encode (obj: CircuitRelayInput): Uint8Array { return encodeMessage(obj, CircuitRelay.codec()) } diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index 2dde2c4..8580008 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -13,13 +13,13 @@ export interface CustomOptionNumber { i64Map: Map } -export interface CustomOptionNumberEncoder { - num?: number - i64?: number - ui64?: number - si64?: number - f64?: number - sf64?: number +export interface CustomOptionNumberInput { + num: number + i64: number + ui64: number + si64: number + f64: number + sf64: number i64Array?: number[] i64Map?: Map } @@ -30,17 +30,17 @@ export namespace CustomOptionNumber { value: number } - export interface CustomOptionNumber$i64MapEntryEncoder { - key?: number - value?: number + export interface CustomOptionNumber$i64MapEntryInput { + key: number + value: number } export namespace CustomOptionNumber$i64MapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -145,7 +145,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: CustomOptionNumber$i64MapEntryEncoder): Uint8Array { + export function encode (obj: CustomOptionNumber$i64MapEntryInput): Uint8Array { return encodeMessage(obj, CustomOptionNumber$i64MapEntry.codec()) } @@ -158,11 +158,11 @@ export namespace CustomOptionNumber { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -439,7 +439,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: CustomOptionNumberEncoder): Uint8Array { + export function encode (obj: CustomOptionNumberInput): Uint8Array { return encodeMessage(obj, CustomOptionNumber.codec()) } @@ -463,13 +463,13 @@ export interface CustomOptionString { i64Map: Map } -export interface CustomOptionStringEncoder { - num?: number - i64?: string - ui64?: string - si64?: string - f64?: string - sf64?: string +export interface CustomOptionStringInput { + num: number + i64: string + ui64: string + si64: string + f64: string + sf64: string i64Array?: string[] i64Map?: Map } @@ -480,17 +480,17 @@ export namespace CustomOptionString { value: string } - export interface CustomOptionString$i64MapEntryEncoder { - key?: string - value?: string + export interface CustomOptionString$i64MapEntryInput { + key: string + value: string } export namespace CustomOptionString$i64MapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -595,7 +595,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: CustomOptionString$i64MapEntryEncoder): Uint8Array { + export function encode (obj: CustomOptionString$i64MapEntryInput): Uint8Array { return encodeMessage(obj, CustomOptionString$i64MapEntry.codec()) } @@ -608,11 +608,11 @@ export namespace CustomOptionString { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -889,7 +889,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: CustomOptionStringEncoder): Uint8Array { + export function encode (obj: CustomOptionStringInput): Uint8Array { return encodeMessage(obj, CustomOptionString.codec()) } diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 2e75396..69cbeb6 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -15,16 +15,16 @@ export interface Request { peerStore?: PeerstoreRequest } -export interface RequestEncoder { - type?: Request.Type - connect?: ConnectRequestEncoder - streamOpen?: StreamOpenRequestEncoder - streamHandler?: StreamHandlerRequestEncoder - dht?: DHTRequestEncoder - connManager?: ConnManagerRequestEncoder - disconnect?: DisconnectRequestEncoder - pubsub?: PSRequestEncoder - peerStore?: PeerstoreRequestEncoder +export interface RequestInput { + type: Request.Type + connect?: ConnectRequestInput + streamOpen?: StreamOpenRequestInput + streamHandler?: StreamHandlerRequestInput + dht?: DHTRequestInput + connManager?: ConnManagerRequestInput + disconnect?: DisconnectRequestInput + pubsub?: PSRequestInput + peerStore?: PeerstoreRequestInput } export namespace Request { @@ -60,11 +60,11 @@ export namespace Request { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -505,7 +505,7 @@ export namespace Request { value: string } - export function encode (obj: RequestEncoder): Uint8Array { + export function encode (obj: RequestInput): Uint8Array { return encodeMessage(obj, Request.codec()) } @@ -529,15 +529,15 @@ export interface Response { peerStore?: PeerstoreResponse } -export interface ResponseEncoder { - type?: Response.Type - error?: ErrorResponseEncoder - streamInfo?: StreamInfoEncoder - identify?: IdentifyResponseEncoder - dht?: DHTResponseEncoder - peers?: PeerInfoEncoder[] - pubsub?: PSResponseEncoder - peerStore?: PeerstoreResponseEncoder +export interface ResponseInput { + type: Response.Type + error?: ErrorResponseInput + streamInfo?: StreamInfoInput + identify?: IdentifyResponseInput + dht?: DHTResponseInput + peers?: PeerInfoInput[] + pubsub?: PSResponseInput + peerStore?: PeerstoreResponseInput } export namespace Response { @@ -557,11 +557,11 @@ export namespace Response { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -979,7 +979,7 @@ export namespace Response { value: string } - export function encode (obj: ResponseEncoder): Uint8Array { + export function encode (obj: ResponseInput): Uint8Array { return encodeMessage(obj, Response.codec()) } @@ -997,17 +997,17 @@ export interface IdentifyResponse { addrs: Uint8Array[] } -export interface IdentifyResponseEncoder { - id?: Uint8Array +export interface IdentifyResponseInput { + id: Uint8Array addrs?: Uint8Array[] } export namespace IdentifyResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1131,7 +1131,7 @@ export namespace IdentifyResponse { value: Uint8Array } - export function encode (obj: IdentifyResponseEncoder): Uint8Array { + export function encode (obj: IdentifyResponseInput): Uint8Array { return encodeMessage(obj, IdentifyResponse.codec()) } @@ -1150,18 +1150,18 @@ export interface ConnectRequest { timeout?: bigint } -export interface ConnectRequestEncoder { - peer?: Uint8Array +export interface ConnectRequestInput { + peer: Uint8Array addrs?: Uint8Array[] timeout?: bigint } export namespace ConnectRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1306,7 +1306,7 @@ export namespace ConnectRequest { value: bigint } - export function encode (obj: ConnectRequestEncoder): Uint8Array { + export function encode (obj: ConnectRequestInput): Uint8Array { return encodeMessage(obj, ConnectRequest.codec()) } @@ -1325,18 +1325,18 @@ export interface StreamOpenRequest { timeout?: bigint } -export interface StreamOpenRequestEncoder { - peer?: Uint8Array +export interface StreamOpenRequestInput { + peer: Uint8Array proto?: string[] timeout?: bigint } export namespace StreamOpenRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1481,7 +1481,7 @@ export namespace StreamOpenRequest { value: bigint } - export function encode (obj: StreamOpenRequestEncoder): Uint8Array { + export function encode (obj: StreamOpenRequestInput): Uint8Array { return encodeMessage(obj, StreamOpenRequest.codec()) } @@ -1499,17 +1499,17 @@ export interface StreamHandlerRequest { proto: string[] } -export interface StreamHandlerRequestEncoder { - addr?: Uint8Array +export interface StreamHandlerRequestInput { + addr: Uint8Array proto?: string[] } export namespace StreamHandlerRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1633,7 +1633,7 @@ export namespace StreamHandlerRequest { value: string } - export function encode (obj: StreamHandlerRequestEncoder): Uint8Array { + export function encode (obj: StreamHandlerRequestInput): Uint8Array { return encodeMessage(obj, StreamHandlerRequest.codec()) } @@ -1650,16 +1650,16 @@ export interface ErrorResponse { msg: string } -export interface ErrorResponseEncoder { - msg?: string +export interface ErrorResponseInput { + msg: string } export namespace ErrorResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1742,7 +1742,7 @@ export namespace ErrorResponse { value: string } - export function encode (obj: ErrorResponseEncoder): Uint8Array { + export function encode (obj: ErrorResponseInput): Uint8Array { return encodeMessage(obj, ErrorResponse.codec()) } @@ -1761,18 +1761,18 @@ export interface StreamInfo { proto: string } -export interface StreamInfoEncoder { - peer?: Uint8Array - addr?: Uint8Array - proto?: string +export interface StreamInfoInput { + peer: Uint8Array + addr: Uint8Array + proto: string } export namespace StreamInfo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1899,7 +1899,7 @@ export namespace StreamInfo { value: string } - export function encode (obj: StreamInfoEncoder): Uint8Array { + export function encode (obj: StreamInfoInput): Uint8Array { return encodeMessage(obj, StreamInfo.codec()) } @@ -1922,8 +1922,8 @@ export interface DHTRequest { timeout?: bigint } -export interface DHTRequestEncoder { - type?: DHTRequest.Type +export interface DHTRequestInput { + type: DHTRequest.Type peer?: Uint8Array cid?: Uint8Array key?: Uint8Array @@ -1963,11 +1963,11 @@ export namespace DHTRequest { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2176,7 +2176,7 @@ export namespace DHTRequest { value: bigint } - export function encode (obj: DHTRequestEncoder): Uint8Array { + export function encode (obj: DHTRequestInput): Uint8Array { return encodeMessage(obj, DHTRequest.codec()) } @@ -2195,9 +2195,9 @@ export interface DHTResponse { value?: Uint8Array } -export interface DHTResponseEncoder { - type?: DHTResponse.Type - peer?: PeerInfoEncoder +export interface DHTResponseInput { + type: DHTResponse.Type + peer?: PeerInfoInput value?: Uint8Array } @@ -2220,11 +2220,11 @@ export namespace DHTResponse { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2367,7 +2367,7 @@ export namespace DHTResponse { value: Uint8Array } - export function encode (obj: DHTResponseEncoder): Uint8Array { + export function encode (obj: DHTResponseInput): Uint8Array { return encodeMessage(obj, DHTResponse.codec()) } @@ -2385,17 +2385,17 @@ export interface PeerInfo { addrs: Uint8Array[] } -export interface PeerInfoEncoder { - id?: Uint8Array +export interface PeerInfoInput { + id: Uint8Array addrs?: Uint8Array[] } export namespace PeerInfo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2519,7 +2519,7 @@ export namespace PeerInfo { value: Uint8Array } - export function encode (obj: PeerInfoEncoder): Uint8Array { + export function encode (obj: PeerInfoInput): Uint8Array { return encodeMessage(obj, PeerInfo.codec()) } @@ -2539,8 +2539,8 @@ export interface ConnManagerRequest { weight?: bigint } -export interface ConnManagerRequestEncoder { - type?: ConnManagerRequest.Type +export interface ConnManagerRequestInput { + type: ConnManagerRequest.Type peer?: Uint8Array tag?: string weight?: bigint @@ -2565,11 +2565,11 @@ export namespace ConnManagerRequest { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2715,7 +2715,7 @@ export namespace ConnManagerRequest { value: bigint } - export function encode (obj: ConnManagerRequestEncoder): Uint8Array { + export function encode (obj: ConnManagerRequestInput): Uint8Array { return encodeMessage(obj, ConnManagerRequest.codec()) } @@ -2732,16 +2732,16 @@ export interface DisconnectRequest { peer: Uint8Array } -export interface DisconnectRequestEncoder { - peer?: Uint8Array +export interface DisconnectRequestInput { + peer: Uint8Array } export namespace DisconnectRequest { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2824,7 +2824,7 @@ export namespace DisconnectRequest { value: Uint8Array } - export function encode (obj: DisconnectRequestEncoder): Uint8Array { + export function encode (obj: DisconnectRequestInput): Uint8Array { return encodeMessage(obj, DisconnectRequest.codec()) } @@ -2843,8 +2843,8 @@ export interface PSRequest { data?: Uint8Array } -export interface PSRequestEncoder { - type?: PSRequest.Type +export interface PSRequestInput { + type: PSRequest.Type topic?: string data?: Uint8Array } @@ -2870,11 +2870,11 @@ export namespace PSRequest { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -2999,7 +2999,7 @@ export namespace PSRequest { value: Uint8Array } - export function encode (obj: PSRequestEncoder): Uint8Array { + export function encode (obj: PSRequestInput): Uint8Array { return encodeMessage(obj, PSRequest.codec()) } @@ -3021,7 +3021,7 @@ export interface PSMessage { key?: Uint8Array } -export interface PSMessageEncoder { +export interface PSMessageInput { from?: Uint8Array data?: Uint8Array seqno?: Uint8Array @@ -3031,11 +3031,11 @@ export interface PSMessageEncoder { } export namespace PSMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3242,7 +3242,7 @@ export namespace PSMessage { value: Uint8Array } - export function encode (obj: PSMessageEncoder): Uint8Array { + export function encode (obj: PSMessageInput): Uint8Array { return encodeMessage(obj, PSMessage.codec()) } @@ -3260,17 +3260,17 @@ export interface PSResponse { peerIDs: Uint8Array[] } -export interface PSResponseEncoder { +export interface PSResponseInput { topics?: string[] peerIDs?: Uint8Array[] } export namespace PSResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3410,7 +3410,7 @@ export namespace PSResponse { value: Uint8Array } - export function encode (obj: PSResponseEncoder): Uint8Array { + export function encode (obj: PSResponseInput): Uint8Array { return encodeMessage(obj, PSResponse.codec()) } @@ -3429,8 +3429,8 @@ export interface PeerstoreRequest { protos: string[] } -export interface PeerstoreRequestEncoder { - type?: PeerstoreRequest.Type +export interface PeerstoreRequestInput { + type: PeerstoreRequest.Type id?: Uint8Array protos?: string[] } @@ -3454,11 +3454,11 @@ export namespace PeerstoreRequest { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3603,7 +3603,7 @@ export namespace PeerstoreRequest { value: string } - export function encode (obj: PeerstoreRequestEncoder): Uint8Array { + export function encode (obj: PeerstoreRequestInput): Uint8Array { return encodeMessage(obj, PeerstoreRequest.codec()) } @@ -3621,17 +3621,17 @@ export interface PeerstoreResponse { protos: string[] } -export interface PeerstoreResponseEncoder { - peer?: PeerInfoEncoder +export interface PeerstoreResponseInput { + peer?: PeerInfoInput protos?: string[] } export namespace PeerstoreResponse { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -3772,7 +3772,7 @@ export namespace PeerstoreResponse { value: string } - export function encode (obj: PeerstoreResponseEncoder): Uint8Array { + export function encode (obj: PeerstoreResponseInput): Uint8Array { return encodeMessage(obj, PeerstoreResponse.codec()) } diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index 973c3fb..be998ac 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -10,7 +10,7 @@ export interface Record { timeReceived?: string } -export interface RecordEncoder { +export interface RecordInput { key?: Uint8Array value?: Uint8Array author?: Uint8Array @@ -19,11 +19,11 @@ export interface RecordEncoder { } export namespace Record { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -188,7 +188,7 @@ export namespace Record { value: string } - export function encode (obj: RecordEncoder): Uint8Array { + export function encode (obj: RecordInput): Uint8Array { return encodeMessage(obj, Record.codec()) } @@ -210,13 +210,13 @@ export interface Message { providerPeers: Message.Peer[] } -export interface MessageEncoder { +export interface MessageInput { type?: Message.MessageType clusterLevelRaw?: number key?: Uint8Array record?: Uint8Array - closerPeers?: Message.PeerEncoder[] - providerPeers?: Message.PeerEncoder[] + closerPeers?: Message.PeerInput[] + providerPeers?: Message.PeerInput[] } export namespace Message { @@ -270,18 +270,18 @@ export namespace Message { connection?: Message.ConnectionType } - export interface PeerEncoder { + export interface PeerInput { id?: Uint8Array addrs?: Uint8Array[] connection?: Message.ConnectionType } export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -425,7 +425,7 @@ export namespace Message { value: Message.ConnectionType } - export function encode (obj: PeerEncoder): Uint8Array { + export function encode (obj: PeerInput): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -438,11 +438,11 @@ export namespace Message { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -728,7 +728,7 @@ export namespace Message { message: string } - export function encode (obj: MessageEncoder): Uint8Array { + export function encode (obj: MessageInput): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index ce23208..0d8e096 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -25,17 +25,17 @@ export interface SubMessage { bar: number[] } -export interface SubMessageEncoder { - foo?: string +export interface SubMessageInput { + foo: string bar?: number[] } export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -159,7 +159,7 @@ export namespace SubMessage { value: number } - export function encode (obj: SubMessageEncoder): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -180,11 +180,11 @@ export interface MapTypes { enumMap: Map } -export interface MapTypesEncoder { +export interface MapTypesInput { stringMap?: Map intMap?: Map boolMap?: Map - messageMap?: Map + messageMap?: Map enumMap?: Map } @@ -194,17 +194,17 @@ export namespace MapTypes { value: string } - export interface MapTypes$stringMapEntryEncoder { - key?: string - value?: string + export interface MapTypes$stringMapEntryInput { + key: string + value: string } export namespace MapTypes$stringMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -309,7 +309,7 @@ export namespace MapTypes { value: string } - export function encode (obj: MapTypes$stringMapEntryEncoder): Uint8Array { + export function encode (obj: MapTypes$stringMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$stringMapEntry.codec()) } @@ -327,17 +327,17 @@ export namespace MapTypes { value: number } - export interface MapTypes$intMapEntryEncoder { - key?: number - value?: number + export interface MapTypes$intMapEntryInput { + key: number + value: number } export namespace MapTypes$intMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -442,7 +442,7 @@ export namespace MapTypes { value: number } - export function encode (obj: MapTypes$intMapEntryEncoder): Uint8Array { + export function encode (obj: MapTypes$intMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$intMapEntry.codec()) } @@ -460,17 +460,17 @@ export namespace MapTypes { value: boolean } - export interface MapTypes$boolMapEntryEncoder { - key?: boolean - value?: boolean + export interface MapTypes$boolMapEntryInput { + key: boolean + value: boolean } export namespace MapTypes$boolMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -575,7 +575,7 @@ export namespace MapTypes { value: boolean } - export function encode (obj: MapTypes$boolMapEntryEncoder): Uint8Array { + export function encode (obj: MapTypes$boolMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$boolMapEntry.codec()) } @@ -593,17 +593,17 @@ export namespace MapTypes { value?: SubMessage } - export interface MapTypes$messageMapEntryEncoder { - key?: string - value?: SubMessageEncoder + export interface MapTypes$messageMapEntryInput { + key: string + value?: SubMessageInput } export namespace MapTypes$messageMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -725,7 +725,7 @@ export namespace MapTypes { value: number } - export function encode (obj: MapTypes$messageMapEntryEncoder): Uint8Array { + export function encode (obj: MapTypes$messageMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$messageMapEntry.codec()) } @@ -743,17 +743,17 @@ export namespace MapTypes { value: EnumValue } - export interface MapTypes$enumMapEntryEncoder { - key?: string - value?: EnumValue + export interface MapTypes$enumMapEntryInput { + key: string + value: EnumValue } export namespace MapTypes$enumMapEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -858,7 +858,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: MapTypes$enumMapEntryEncoder): Uint8Array { + export function encode (obj: MapTypes$enumMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$enumMapEntry.codec()) } @@ -871,11 +871,11 @@ export namespace MapTypes { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1175,7 +1175,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: MapTypesEncoder): Uint8Array { + export function encode (obj: MapTypesInput): Uint8Array { return encodeMessage(obj, MapTypes.codec()) } diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 8a03c55..0888d1e 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -3,7 +3,7 @@ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc' import type { Codec, DecodeOptions } from 'protons-runtime' import type { Uint8ArrayList } from 'uint8arraylist' -export interface pbEncoder {} +export interface pbInput {} export interface pb {} @@ -14,18 +14,18 @@ export namespace pb { data: Uint8Array } - export interface NoiseHandshakePayloadEncoder { - identityKey?: Uint8Array - identitySig?: Uint8Array - data?: Uint8Array + export interface NoiseHandshakePayloadInput { + identityKey: Uint8Array + identitySig: Uint8Array + data: Uint8Array } export namespace NoiseHandshakePayload { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -152,7 +152,7 @@ export namespace pb { value: Uint8Array } - export function encode (obj: NoiseHandshakePayloadEncoder): Uint8Array { + export function encode (obj: NoiseHandshakePayloadInput): Uint8Array { return encodeMessage(obj, NoiseHandshakePayload.codec()) } @@ -165,11 +165,11 @@ export namespace pb { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -229,7 +229,7 @@ export namespace pb { return _codec } - export function encode (obj: pbEncoder): Uint8Array { + export function encode (obj: pbInput): Uint8Array { return encodeMessage(obj, pb.codec()) } diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index 2241921..4e9b25b 100644 --- a/packages/protons/test/fixtures/oneof.ts +++ b/packages/protons/test/fixtures/oneof.ts @@ -26,20 +26,20 @@ export interface OneOfMessage { fieldFive: string } -export interface OneOfMessageEncoder { +export interface OneOfMessageInput { fieldOne?: string fieldTwo?: string fieldThree?: EnumType fieldFour?: EnumType - fieldFive?: string + fieldFive: string } export namespace OneOfMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -240,7 +240,7 @@ export namespace OneOfMessage { value: string } - export function encode (obj: OneOfMessageEncoder): Uint8Array { + export function encode (obj: OneOfMessageInput): Uint8Array { return encodeMessage(obj, OneOfMessage.codec()) } @@ -261,20 +261,20 @@ export interface MessageWithoutOneOfs { fieldFive: string } -export interface MessageWithoutOneOfsEncoder { - fieldOne?: string - fieldTwo?: string - fieldThree?: EnumType - fieldFour?: EnumType - fieldFive?: string +export interface MessageWithoutOneOfsInput { + fieldOne: string + fieldTwo: string + fieldThree: EnumType + fieldFour: EnumType + fieldFive: string } export namespace MessageWithoutOneOfs { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -445,7 +445,7 @@ export namespace MessageWithoutOneOfs { value: string } - export function encode (obj: MessageWithoutOneOfsEncoder): Uint8Array { + export function encode (obj: MessageWithoutOneOfsInput): Uint8Array { return encodeMessage(obj, MessageWithoutOneOfs.codec()) } diff --git a/packages/protons/test/fixtures/optional.ts b/packages/protons/test/fixtures/optional.ts index f802320..70bb5b5 100644 --- a/packages/protons/test/fixtures/optional.ts +++ b/packages/protons/test/fixtures/optional.ts @@ -25,17 +25,17 @@ export interface OptionalSubMessage { bar?: number } -export interface OptionalSubMessageEncoder { +export interface OptionalSubMessageInput { foo?: string bar?: number } export namespace OptionalSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -137,7 +137,7 @@ export namespace OptionalSubMessage { value: number } - export function encode (obj: OptionalSubMessageEncoder): Uint8Array { + export function encode (obj: OptionalSubMessageInput): Uint8Array { return encodeMessage(obj, OptionalSubMessage.codec()) } @@ -170,7 +170,7 @@ export interface Optional { subMessage?: OptionalSubMessage } -export interface OptionalEncoder { +export interface OptionalInput { double?: number float?: number int32?: number @@ -187,15 +187,15 @@ export interface OptionalEncoder { string?: string bytes?: Uint8Array enum?: OptionalEnum - subMessage?: OptionalSubMessageEncoder + subMessage?: OptionalSubMessageInput } export namespace Optional { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -629,7 +629,7 @@ export namespace Optional { value: number } - export function encode (obj: OptionalEncoder): Uint8Array { + export function encode (obj: OptionalInput): Uint8Array { return encodeMessage(obj, Optional.codec()) } diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index a97cbee..026be62 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -11,20 +11,20 @@ export interface Peer { peerRecordEnvelope?: Uint8Array } -export interface PeerEncoder { - addresses?: AddressEncoder[] +export interface PeerInput { + addresses?: AddressInput[] protocols?: string[] - metadata?: MetadataEncoder[] + metadata?: MetadataInput[] pubKey?: Uint8Array peerRecordEnvelope?: Uint8Array } export namespace Peer { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -294,7 +294,7 @@ export namespace Peer { value: Uint8Array } - export function encode (obj: PeerEncoder): Uint8Array { + export function encode (obj: PeerInput): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -312,17 +312,17 @@ export interface Address { isCertified?: boolean } -export interface AddressEncoder { - multiaddr?: Uint8Array +export interface AddressInput { + multiaddr: Uint8Array isCertified?: boolean } export namespace Address { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -426,7 +426,7 @@ export namespace Address { value: boolean } - export function encode (obj: AddressEncoder): Uint8Array { + export function encode (obj: AddressInput): Uint8Array { return encodeMessage(obj, Address.codec()) } @@ -444,17 +444,17 @@ export interface Metadata { value: Uint8Array } -export interface MetadataEncoder { - key?: string - value?: Uint8Array +export interface MetadataInput { + key: string + value: Uint8Array } export namespace Metadata { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -559,7 +559,7 @@ export namespace Metadata { value: Uint8Array } - export function encode (obj: MetadataEncoder): Uint8Array { + export function encode (obj: MetadataInput): Uint8Array { return encodeMessage(obj, Metadata.codec()) } diff --git a/packages/protons/test/fixtures/proto2.ts b/packages/protons/test/fixtures/proto2.ts index 2efce30..2bf2c28 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -6,16 +6,16 @@ export interface MessageWithRequired { scalarField: number } -export interface MessageWithRequiredEncoder { - scalarField?: number +export interface MessageWithRequiredInput { + scalarField: number } export namespace MessageWithRequired { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -98,7 +98,7 @@ export namespace MessageWithRequired { value: number } - export function encode (obj: MessageWithRequiredEncoder): Uint8Array { + export function encode (obj: MessageWithRequiredInput): Uint8Array { return encodeMessage(obj, MessageWithRequired.codec()) } diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index 6de89db..0d48c27 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -6,16 +6,16 @@ export interface MessageWithSizeLimitedRepeatedField { repeatedField: string[] } -export interface MessageWithSizeLimitedRepeatedFieldEncoder { +export interface MessageWithSizeLimitedRepeatedFieldInput { repeatedField?: string[] } export namespace MessageWithSizeLimitedRepeatedField { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -125,7 +125,7 @@ export namespace MessageWithSizeLimitedRepeatedField { value: string } - export function encode (obj: MessageWithSizeLimitedRepeatedFieldEncoder): Uint8Array { + export function encode (obj: MessageWithSizeLimitedRepeatedFieldInput): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedRepeatedField.codec()) } @@ -142,7 +142,7 @@ export interface MessageWithSizeLimitedMap { mapField: Map } -export interface MessageWithSizeLimitedMapEncoder { +export interface MessageWithSizeLimitedMapInput { mapField?: Map } @@ -152,17 +152,17 @@ export namespace MessageWithSizeLimitedMap { value: string } - export interface MessageWithSizeLimitedMap$mapFieldEntryEncoder { - key?: string - value?: string + export interface MessageWithSizeLimitedMap$mapFieldEntryInput { + key: string + value: string } export namespace MessageWithSizeLimitedMap$mapFieldEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -267,7 +267,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: MessageWithSizeLimitedMap$mapFieldEntryEncoder): Uint8Array { + export function encode (obj: MessageWithSizeLimitedMap$mapFieldEntryInput): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap$mapFieldEntry.codec()) } @@ -280,11 +280,11 @@ export namespace MessageWithSizeLimitedMap { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -399,7 +399,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: MessageWithSizeLimitedMapEncoder): Uint8Array { + export function encode (obj: MessageWithSizeLimitedMapInput): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap.codec()) } diff --git a/packages/protons/test/fixtures/repeated.ts b/packages/protons/test/fixtures/repeated.ts index 2d843a8..de8b711 100644 --- a/packages/protons/test/fixtures/repeated.ts +++ b/packages/protons/test/fixtures/repeated.ts @@ -7,17 +7,17 @@ export interface SubSubMessage { nonRepeating?: number } -export interface SubSubMessageEncoder { +export interface SubSubMessageInput { foo?: string[] nonRepeating?: number } export namespace SubSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -140,7 +140,7 @@ export namespace SubSubMessage { value: number } - export function encode (obj: SubSubMessageEncoder): Uint8Array { + export function encode (obj: SubSubMessageInput): Uint8Array { return encodeMessage(obj, SubSubMessage.codec()) } @@ -160,19 +160,19 @@ export interface SubMessage { messages: SubSubMessage[] } -export interface SubMessageEncoder { +export interface SubMessageInput { foo?: string[] nonRepeating?: number - message?: SubSubMessageEncoder - messages?: SubSubMessageEncoder[] + message?: SubSubMessageInput + messages?: SubSubMessageInput[] } export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -397,7 +397,7 @@ export namespace SubMessage { message: string } - export function encode (obj: SubMessageEncoder): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -418,20 +418,20 @@ export interface RepeatedTypes { nonRepeating?: number } -export interface RepeatedTypesEncoder { +export interface RepeatedTypesInput { number?: number[] limitedNumber?: number[] - messages?: SubMessageEncoder[] - message?: SubMessageEncoder + messages?: SubMessageInput[] + message?: SubMessageInput nonRepeating?: number } export namespace RepeatedTypes { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -799,7 +799,7 @@ export namespace RepeatedTypes { value: number } - export function encode (obj: RepeatedTypesEncoder): Uint8Array { + export function encode (obj: RepeatedTypesInput): Uint8Array { return encodeMessage(obj, RepeatedTypes.codec()) } diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index 190494c..d2ba96d 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -26,17 +26,17 @@ export interface SingularSubMessage { bar: number } -export interface SingularSubMessageEncoder { - foo?: string - bar?: number +export interface SingularSubMessageInput { + foo: string + bar: number } export namespace SingularSubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -141,7 +141,7 @@ export namespace SingularSubMessage { value: number } - export function encode (obj: SingularSubMessageEncoder): Uint8Array { + export function encode (obj: SingularSubMessageInput): Uint8Array { return encodeMessage(obj, SingularSubMessage.codec()) } @@ -174,32 +174,32 @@ export interface Singular { subMessage?: SingularSubMessage } -export interface SingularEncoder { - double?: number - float?: number - int32?: number - int64?: bigint - uint32?: number - uint64?: bigint - sint32?: number - sint64?: bigint - fixed32?: number - fixed64?: bigint - sfixed32?: number - sfixed64?: bigint - bool?: boolean - string?: string - bytes?: Uint8Array - enum?: SingularEnum - subMessage?: SingularSubMessageEncoder +export interface SingularInput { + double: number + float: number + int32: number + int64: bigint + uint32: number + uint64: bigint + sint32: number + sint64: bigint + fixed32: number + fixed64: bigint + sfixed32: number + sfixed64: bigint + bool: boolean + string: string + bytes: Uint8Array + enum: SingularEnum + subMessage?: SingularSubMessageInput } export namespace Singular { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -650,7 +650,7 @@ export namespace Singular { value: number } - export function encode (obj: SingularEncoder): Uint8Array { + export function encode (obj: SingularInput): Uint8Array { return encodeMessage(obj, Singular.codec()) } diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index fc9044d..79d679e 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -8,18 +8,18 @@ export interface MessageWithArrayField { arr: string[] } -export interface MessageWithArrayFieldEncoder { +export interface MessageWithArrayFieldInput { field1?: boolean field2?: number arr?: string[] } export namespace MessageWithArrayField { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -163,7 +163,7 @@ export namespace MessageWithArrayField { value: string } - export function encode (obj: MessageWithArrayFieldEncoder): Uint8Array { + export function encode (obj: MessageWithArrayFieldInput): Uint8Array { return encodeMessage(obj, MessageWithArrayField.codec()) } @@ -180,16 +180,16 @@ export interface NestedMessage { nestedValue: string } -export interface NestedMessageEncoder { - nestedValue?: string +export interface NestedMessageInput { + nestedValue: string } export namespace NestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -272,7 +272,7 @@ export namespace NestedMessage { value: string } - export function encode (obj: NestedMessageEncoder): Uint8Array { + export function encode (obj: NestedMessageInput): Uint8Array { return encodeMessage(obj, NestedMessage.codec()) } @@ -290,17 +290,17 @@ export interface MessageWithNestedMessage { nestedMessage?: NestedMessage } -export interface MessageWithNestedMessageEncoder { - field1?: boolean - nestedMessage?: NestedMessageEncoder +export interface MessageWithNestedMessageInput { + field1: boolean + nestedMessage?: NestedMessageInput } export namespace MessageWithNestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -416,7 +416,7 @@ export namespace MessageWithNestedMessage { value: string } - export function encode (obj: MessageWithNestedMessageEncoder): Uint8Array { + export function encode (obj: MessageWithNestedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithNestedMessage.codec()) } @@ -434,17 +434,17 @@ export interface MessageWithDeeplyNestedMessage { nestedMessage?: MessageWithNestedMessage } -export interface MessageWithDeeplyNestedMessageEncoder { - field1?: boolean - nestedMessage?: MessageWithNestedMessageEncoder +export interface MessageWithDeeplyNestedMessageInput { + field1: boolean + nestedMessage?: MessageWithNestedMessageInput } export namespace MessageWithDeeplyNestedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -575,7 +575,7 @@ export namespace MessageWithDeeplyNestedMessage { value: string } - export function encode (obj: MessageWithDeeplyNestedMessageEncoder): Uint8Array { + export function encode (obj: MessageWithDeeplyNestedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithDeeplyNestedMessage.codec()) } @@ -593,17 +593,17 @@ export interface MessageWithRepeatedMessage { nestedMessages: NestedMessage[] } -export interface MessageWithRepeatedMessageEncoder { - field1?: boolean - nestedMessages?: NestedMessageEncoder[] +export interface MessageWithRepeatedMessageInput { + field1: boolean + nestedMessages?: NestedMessageInput[] } export namespace MessageWithRepeatedMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -746,7 +746,7 @@ export namespace MessageWithRepeatedMessage { message: string } - export function encode (obj: MessageWithRepeatedMessageEncoder): Uint8Array { + export function encode (obj: MessageWithRepeatedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithRepeatedMessage.codec()) } @@ -764,9 +764,9 @@ export interface MessageWithMapMessage { nestedMessages: Map } -export interface MessageWithMapMessageEncoder { - field1?: boolean - nestedMessages?: Map +export interface MessageWithMapMessageInput { + field1: boolean + nestedMessages?: Map } export namespace MessageWithMapMessage { @@ -775,17 +775,17 @@ export namespace MessageWithMapMessage { value?: NestedMessage } - export interface MessageWithMapMessage$nestedMessagesEntryEncoder { - key?: string - value?: NestedMessageEncoder + export interface MessageWithMapMessage$nestedMessagesEntryInput { + key: string + value?: NestedMessageInput } export namespace MessageWithMapMessage$nestedMessagesEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -901,7 +901,7 @@ export namespace MessageWithMapMessage { value: string } - export function encode (obj: MessageWithMapMessage$nestedMessagesEntryEncoder): Uint8Array { + export function encode (obj: MessageWithMapMessage$nestedMessagesEntryInput): Uint8Array { return encodeMessage(obj, MessageWithMapMessage$nestedMessagesEntry.codec()) } @@ -914,11 +914,11 @@ export namespace MessageWithMapMessage { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1061,7 +1061,7 @@ export namespace MessageWithMapMessage { message: string } - export function encode (obj: MessageWithMapMessageEncoder): Uint8Array { + export function encode (obj: MessageWithMapMessageInput): Uint8Array { return encodeMessage(obj, MessageWithMapMessage.codec()) } @@ -1079,8 +1079,8 @@ export interface MessageWithPrimitiveMap { nestedStrings: Map } -export interface MessageWithPrimitiveMapEncoder { - field1?: boolean +export interface MessageWithPrimitiveMapInput { + field1: boolean nestedStrings?: Map } @@ -1090,17 +1090,17 @@ export namespace MessageWithPrimitiveMap { value: string } - export interface MessageWithPrimitiveMap$nestedStringsEntryEncoder { - key?: string - value?: string + export interface MessageWithPrimitiveMap$nestedStringsEntryInput { + key: string + value: string } export namespace MessageWithPrimitiveMap$nestedStringsEntry { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1205,7 +1205,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: MessageWithPrimitiveMap$nestedStringsEntryEncoder): Uint8Array { + export function encode (obj: MessageWithPrimitiveMap$nestedStringsEntryInput): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap$nestedStringsEntry.codec()) } @@ -1218,11 +1218,11 @@ export namespace MessageWithPrimitiveMap { } } - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1351,7 +1351,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: MessageWithPrimitiveMapEncoder): Uint8Array { + export function encode (obj: MessageWithPrimitiveMapInput): Uint8Array { return encodeMessage(obj, MessageWithPrimitiveMap.codec()) } @@ -1387,17 +1387,17 @@ export interface MessageWithRepeatedEnums { enums: ENUM[] } -export interface MessageWithRepeatedEnumsEncoder { - field1?: boolean +export interface MessageWithRepeatedEnumsInput { + field1: boolean enums?: ENUM[] } export namespace MessageWithRepeatedEnums { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -1521,7 +1521,7 @@ export namespace MessageWithRepeatedEnums { value: ENUM } - export function encode (obj: MessageWithRepeatedEnumsEncoder): Uint8Array { + export function encode (obj: MessageWithRepeatedEnumsInput): Uint8Array { return encodeMessage(obj, MessageWithRepeatedEnums.codec()) } diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index e48a80e..d42d2ee 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -22,16 +22,16 @@ export interface SubMessage { foo: string } -export interface SubMessageEncoder { - foo?: string +export interface SubMessageInput { + foo: string } export namespace SubMessage { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -114,7 +114,7 @@ export namespace SubMessage { value: string } - export function encode (obj: SubMessageEncoder): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -148,7 +148,7 @@ export interface AllTheTypes { field18?: bigint } -export interface AllTheTypesEncoder { +export interface AllTheTypesInput { field1?: boolean field2?: number field3?: bigint @@ -161,7 +161,7 @@ export interface AllTheTypesEncoder { field10?: string field11?: Uint8Array field12?: AnEnum - field13?: SubMessageEncoder + field13?: SubMessageInput field14?: string[] field15?: number field16?: bigint @@ -170,11 +170,11 @@ export interface AllTheTypesEncoder { } export namespace AllTheTypes { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -645,7 +645,7 @@ export namespace AllTheTypes { value: bigint } - export function encode (obj: AllTheTypesEncoder): Uint8Array { + export function encode (obj: AllTheTypesInput): Uint8Array { return encodeMessage(obj, AllTheTypes.codec()) } From 8ad834631a2c7327a45806c37832b5a1c4591239 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 08:57:05 +0300 Subject: [PATCH 5/7] chore: regen benchmark code --- .../src/implementations/protons/bench.ts | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/protons-benchmark/src/implementations/protons/bench.ts b/packages/protons-benchmark/src/implementations/protons/bench.ts index af82213..5fe5130 100644 --- a/packages/protons-benchmark/src/implementations/protons/bench.ts +++ b/packages/protons-benchmark/src/implementations/protons/bench.ts @@ -6,16 +6,16 @@ export interface Foo { baz?: number } -export interface FooEncoder { +export interface FooInput { baz?: number } export namespace Foo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -96,7 +96,7 @@ export namespace Foo { value: number } - export function encode (obj: FooEncoder): Uint8Array { + export function encode (obj: FooInput): Uint8Array { return encodeMessage(obj, Foo.codec()) } @@ -113,16 +113,16 @@ export interface Bar { tmp?: Foo } -export interface BarEncoder { - tmp?: FooEncoder +export interface BarInput { + tmp?: FooInput } export namespace Bar { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -215,7 +215,7 @@ export namespace Bar { value: number } - export function encode (obj: BarEncoder): Uint8Array { + export function encode (obj: BarInput): Uint8Array { return encodeMessage(obj, Bar.codec()) } @@ -250,16 +250,16 @@ export interface Yo { lol: FOO[] } -export interface YoEncoder { +export interface YoInput { lol?: FOO[] } export namespace Yo { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -361,7 +361,7 @@ export namespace Yo { value: FOO } - export function encode (obj: YoEncoder): Uint8Array { + export function encode (obj: YoInput): Uint8Array { return encodeMessage(obj, Yo.codec()) } @@ -379,17 +379,17 @@ export interface Lol { b?: Bar } -export interface LolEncoder { +export interface LolInput { lol?: string - b?: BarEncoder + b?: BarInput } export namespace Lol { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -513,7 +513,7 @@ export namespace Lol { value: number } - export function encode (obj: LolEncoder): Uint8Array { + export function encode (obj: LolInput): Uint8Array { return encodeMessage(obj, Lol.codec()) } @@ -533,19 +533,19 @@ export interface Test { payload?: Uint8Array } -export interface TestEncoder { - meh?: LolEncoder +export interface TestInput { + meh?: LolInput hello?: number foo?: string payload?: Uint8Array } export namespace Test { - let _codec: Codec + let _codec: Codec - export const codec = (): Codec => { + export const codec = (): Codec => { if (_codec == null) { - _codec = message((obj, w, opts = {}) => { + _codec = message((obj, w, opts = {}) => { if (opts.lengthDelimited !== false) { w.fork() } @@ -726,7 +726,7 @@ export namespace Test { value: Uint8Array } - export function encode (obj: TestEncoder): Uint8Array { + export function encode (obj: TestInput): Uint8Array { return encodeMessage(obj, Test.codec()) } From f40ccf59ddb3d451f27755688f4d60a67f4ff108 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 09:18:32 +0300 Subject: [PATCH 6/7] chore: fix build --- packages/protons/test/compatibility.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/protons/test/compatibility.spec.ts b/packages/protons/test/compatibility.spec.ts index f5277fb..16e150f 100644 --- a/packages/protons/test/compatibility.spec.ts +++ b/packages/protons/test/compatibility.spec.ts @@ -429,6 +429,7 @@ describe('encode', () => { }) it('does not write singular field values when defaults are omitted', () => { + // @ts-expect-error fields are required const buf = Singular.encode({}) expect(buf.byteLength).to.equal(0, 'wrote default values for singular fields') From 1459ec888802713b34e4f92e741b6e14349850f7 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 09:29:58 +0300 Subject: [PATCH 7/7] chore: restore optional --- packages/protons/src/fields/field.ts | 8 ++++- packages/protons/test/compatibility.spec.ts | 1 - packages/protons/test/fixtures/basic.ts | 2 +- packages/protons/test/fixtures/bitswap.ts | 20 +++++------ packages/protons/test/fixtures/circuit.ts | 2 +- .../test/fixtures/custom-option-jstype.ts | 32 ++++++++--------- packages/protons/test/fixtures/daemon.ts | 34 +++++++++--------- packages/protons/test/fixtures/maps.ts | 20 +++++------ packages/protons/test/fixtures/noise.ts | 6 ++-- packages/protons/test/fixtures/oneof.ts | 12 +++---- packages/protons/test/fixtures/peer.ts | 6 ++-- packages/protons/test/fixtures/proto2.ts | 6 ++-- .../protons/test/fixtures/protons-options.ts | 4 +-- packages/protons/test/fixtures/singular.ts | 36 +++++++++---------- packages/protons/test/fixtures/streaming.ts | 20 +++++------ packages/protons/test/fixtures/test.ts | 2 +- 16 files changed, 107 insertions(+), 104 deletions(-) diff --git a/packages/protons/src/fields/field.ts b/packages/protons/src/fields/field.ts index 0ac9202..c122f89 100644 --- a/packages/protons/src/fields/field.ts +++ b/packages/protons/src/fields/field.ts @@ -137,7 +137,7 @@ export class Field implements MessageField { } getEncoderInterfaceField (parent: Parent, indent = ''): string { - return `${indent}${this.name}${(this.optional) ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` + return `${indent}${this.name}${this.proto2Required ? '' : '?'}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` } getDefaultField (parent: Parent): string { @@ -197,6 +197,12 @@ export class Field implements MessageField { id = (this.id << 3) | codecTypes.enum } + if (this.proto2Required) { + return ` + w.uint32(${id}) + ${type.getEncoder(this, `obj.${this.name}`)}` + } + return ` if (${type.getValueTest(this, `obj.${this.name}`)}) { w.uint32(${id}) diff --git a/packages/protons/test/compatibility.spec.ts b/packages/protons/test/compatibility.spec.ts index 16e150f..f5277fb 100644 --- a/packages/protons/test/compatibility.spec.ts +++ b/packages/protons/test/compatibility.spec.ts @@ -429,7 +429,6 @@ describe('encode', () => { }) it('does not write singular field values when defaults are omitted', () => { - // @ts-expect-error fields are required const buf = Singular.encode({}) expect(buf.byteLength).to.equal(0, 'wrote default values for singular fields') diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index ab7871b..cfb20d1 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -9,7 +9,7 @@ export interface Basic { export interface BasicInput { foo?: string - num: number + num?: number } export namespace Basic { diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index 7a787f3..4c48eb2 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -16,7 +16,7 @@ export interface MessageInput { blocks?: Uint8Array[] payload?: Message.BlockInput[] blockPresences?: Message.BlockPresenceInput[] - pendingBytes: number + pendingBytes?: number } export namespace Message { @@ -27,7 +27,7 @@ export namespace Message { export interface WantlistInput { entries?: Message.Wantlist.EntryInput[] - full: boolean + full?: boolean } export namespace Wantlist { @@ -56,11 +56,11 @@ export namespace Message { } export interface EntryInput { - block: Uint8Array - priority: number + block?: Uint8Array + priority?: number cancel?: boolean - wantType: Message.Wantlist.WantType - sendDontHave: boolean + wantType?: Message.Wantlist.WantType + sendDontHave?: boolean } export namespace Entry { @@ -441,8 +441,8 @@ export namespace Message { } export interface BlockInput { - prefix: Uint8Array - data: Uint8Array + prefix?: Uint8Array + data?: Uint8Array } export namespace Block { @@ -590,8 +590,8 @@ export namespace Message { } export interface BlockPresenceInput { - cid: Uint8Array - type: Message.BlockPresenceType + cid?: Uint8Array + type?: Message.BlockPresenceType } export namespace BlockPresence { diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index 15b2495..2e17872 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -88,7 +88,7 @@ export namespace CircuitRelay { } export interface PeerInput { - id: Uint8Array + id?: Uint8Array addrs?: Uint8Array[] } diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index 8580008..39e56f1 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -14,12 +14,12 @@ export interface CustomOptionNumber { } export interface CustomOptionNumberInput { - num: number - i64: number - ui64: number - si64: number - f64: number - sf64: number + num?: number + i64?: number + ui64?: number + si64?: number + f64?: number + sf64?: number i64Array?: number[] i64Map?: Map } @@ -31,8 +31,8 @@ export namespace CustomOptionNumber { } export interface CustomOptionNumber$i64MapEntryInput { - key: number - value: number + key?: number + value?: number } export namespace CustomOptionNumber$i64MapEntry { @@ -464,12 +464,12 @@ export interface CustomOptionString { } export interface CustomOptionStringInput { - num: number - i64: string - ui64: string - si64: string - f64: string - sf64: string + num?: number + i64?: string + ui64?: string + si64?: string + f64?: string + sf64?: string i64Array?: string[] i64Map?: Map } @@ -481,8 +481,8 @@ export namespace CustomOptionString { } export interface CustomOptionString$i64MapEntryInput { - key: string - value: string + key?: string + value?: string } export namespace CustomOptionString$i64MapEntry { diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 69cbeb6..fa42eec 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -16,7 +16,7 @@ export interface Request { } export interface RequestInput { - type: Request.Type + type?: Request.Type connect?: ConnectRequestInput streamOpen?: StreamOpenRequestInput streamHandler?: StreamHandlerRequestInput @@ -530,7 +530,7 @@ export interface Response { } export interface ResponseInput { - type: Response.Type + type?: Response.Type error?: ErrorResponseInput streamInfo?: StreamInfoInput identify?: IdentifyResponseInput @@ -998,7 +998,7 @@ export interface IdentifyResponse { } export interface IdentifyResponseInput { - id: Uint8Array + id?: Uint8Array addrs?: Uint8Array[] } @@ -1151,7 +1151,7 @@ export interface ConnectRequest { } export interface ConnectRequestInput { - peer: Uint8Array + peer?: Uint8Array addrs?: Uint8Array[] timeout?: bigint } @@ -1326,7 +1326,7 @@ export interface StreamOpenRequest { } export interface StreamOpenRequestInput { - peer: Uint8Array + peer?: Uint8Array proto?: string[] timeout?: bigint } @@ -1500,7 +1500,7 @@ export interface StreamHandlerRequest { } export interface StreamHandlerRequestInput { - addr: Uint8Array + addr?: Uint8Array proto?: string[] } @@ -1651,7 +1651,7 @@ export interface ErrorResponse { } export interface ErrorResponseInput { - msg: string + msg?: string } export namespace ErrorResponse { @@ -1762,9 +1762,9 @@ export interface StreamInfo { } export interface StreamInfoInput { - peer: Uint8Array - addr: Uint8Array - proto: string + peer?: Uint8Array + addr?: Uint8Array + proto?: string } export namespace StreamInfo { @@ -1923,7 +1923,7 @@ export interface DHTRequest { } export interface DHTRequestInput { - type: DHTRequest.Type + type?: DHTRequest.Type peer?: Uint8Array cid?: Uint8Array key?: Uint8Array @@ -2196,7 +2196,7 @@ export interface DHTResponse { } export interface DHTResponseInput { - type: DHTResponse.Type + type?: DHTResponse.Type peer?: PeerInfoInput value?: Uint8Array } @@ -2386,7 +2386,7 @@ export interface PeerInfo { } export interface PeerInfoInput { - id: Uint8Array + id?: Uint8Array addrs?: Uint8Array[] } @@ -2540,7 +2540,7 @@ export interface ConnManagerRequest { } export interface ConnManagerRequestInput { - type: ConnManagerRequest.Type + type?: ConnManagerRequest.Type peer?: Uint8Array tag?: string weight?: bigint @@ -2733,7 +2733,7 @@ export interface DisconnectRequest { } export interface DisconnectRequestInput { - peer: Uint8Array + peer?: Uint8Array } export namespace DisconnectRequest { @@ -2844,7 +2844,7 @@ export interface PSRequest { } export interface PSRequestInput { - type: PSRequest.Type + type?: PSRequest.Type topic?: string data?: Uint8Array } @@ -3430,7 +3430,7 @@ export interface PeerstoreRequest { } export interface PeerstoreRequestInput { - type: PeerstoreRequest.Type + type?: PeerstoreRequest.Type id?: Uint8Array protos?: string[] } diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index 0d8e096..81506d6 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -26,7 +26,7 @@ export interface SubMessage { } export interface SubMessageInput { - foo: string + foo?: string bar?: number[] } @@ -195,8 +195,8 @@ export namespace MapTypes { } export interface MapTypes$stringMapEntryInput { - key: string - value: string + key?: string + value?: string } export namespace MapTypes$stringMapEntry { @@ -328,8 +328,8 @@ export namespace MapTypes { } export interface MapTypes$intMapEntryInput { - key: number - value: number + key?: number + value?: number } export namespace MapTypes$intMapEntry { @@ -461,8 +461,8 @@ export namespace MapTypes { } export interface MapTypes$boolMapEntryInput { - key: boolean - value: boolean + key?: boolean + value?: boolean } export namespace MapTypes$boolMapEntry { @@ -594,7 +594,7 @@ export namespace MapTypes { } export interface MapTypes$messageMapEntryInput { - key: string + key?: string value?: SubMessageInput } @@ -744,8 +744,8 @@ export namespace MapTypes { } export interface MapTypes$enumMapEntryInput { - key: string - value: EnumValue + key?: string + value?: EnumValue } export namespace MapTypes$enumMapEntry { diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 0888d1e..13a7ed8 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -15,9 +15,9 @@ export namespace pb { } export interface NoiseHandshakePayloadInput { - identityKey: Uint8Array - identitySig: Uint8Array - data: Uint8Array + identityKey?: Uint8Array + identitySig?: Uint8Array + data?: Uint8Array } export namespace NoiseHandshakePayload { diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index 4e9b25b..29f7488 100644 --- a/packages/protons/test/fixtures/oneof.ts +++ b/packages/protons/test/fixtures/oneof.ts @@ -31,7 +31,7 @@ export interface OneOfMessageInput { fieldTwo?: string fieldThree?: EnumType fieldFour?: EnumType - fieldFive: string + fieldFive?: string } export namespace OneOfMessage { @@ -262,11 +262,11 @@ export interface MessageWithoutOneOfs { } export interface MessageWithoutOneOfsInput { - fieldOne: string - fieldTwo: string - fieldThree: EnumType - fieldFour: EnumType - fieldFive: string + fieldOne?: string + fieldTwo?: string + fieldThree?: EnumType + fieldFour?: EnumType + fieldFive?: string } export namespace MessageWithoutOneOfs { diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index 026be62..f2f43e9 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -313,7 +313,7 @@ export interface Address { } export interface AddressInput { - multiaddr: Uint8Array + multiaddr?: Uint8Array isCertified?: boolean } @@ -445,8 +445,8 @@ export interface Metadata { } export interface MetadataInput { - key: string - value: Uint8Array + key?: string + value?: Uint8Array } export namespace Metadata { diff --git a/packages/protons/test/fixtures/proto2.ts b/packages/protons/test/fixtures/proto2.ts index 2bf2c28..014113d 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -20,10 +20,8 @@ export namespace MessageWithRequired { w.fork() } - if (obj.scalarField != null) { - w.uint32(8) - w.int32(obj.scalarField) - } + w.uint32(8) + w.int32(obj.scalarField) if (opts.lengthDelimited !== false) { w.ldelim() diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index 0d48c27..c6c79d8 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -153,8 +153,8 @@ export namespace MessageWithSizeLimitedMap { } export interface MessageWithSizeLimitedMap$mapFieldEntryInput { - key: string - value: string + key?: string + value?: string } export namespace MessageWithSizeLimitedMap$mapFieldEntry { diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index d2ba96d..1f931a3 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -27,8 +27,8 @@ export interface SingularSubMessage { } export interface SingularSubMessageInput { - foo: string - bar: number + foo?: string + bar?: number } export namespace SingularSubMessage { @@ -175,22 +175,22 @@ export interface Singular { } export interface SingularInput { - double: number - float: number - int32: number - int64: bigint - uint32: number - uint64: bigint - sint32: number - sint64: bigint - fixed32: number - fixed64: bigint - sfixed32: number - sfixed64: bigint - bool: boolean - string: string - bytes: Uint8Array - enum: SingularEnum + double?: number + float?: number + int32?: number + int64?: bigint + uint32?: number + uint64?: bigint + sint32?: number + sint64?: bigint + fixed32?: number + fixed64?: bigint + sfixed32?: number + sfixed64?: bigint + bool?: boolean + string?: string + bytes?: Uint8Array + enum?: SingularEnum subMessage?: SingularSubMessageInput } diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index 79d679e..e07c504 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -181,7 +181,7 @@ export interface NestedMessage { } export interface NestedMessageInput { - nestedValue: string + nestedValue?: string } export namespace NestedMessage { @@ -291,7 +291,7 @@ export interface MessageWithNestedMessage { } export interface MessageWithNestedMessageInput { - field1: boolean + field1?: boolean nestedMessage?: NestedMessageInput } @@ -435,7 +435,7 @@ export interface MessageWithDeeplyNestedMessage { } export interface MessageWithDeeplyNestedMessageInput { - field1: boolean + field1?: boolean nestedMessage?: MessageWithNestedMessageInput } @@ -594,7 +594,7 @@ export interface MessageWithRepeatedMessage { } export interface MessageWithRepeatedMessageInput { - field1: boolean + field1?: boolean nestedMessages?: NestedMessageInput[] } @@ -765,7 +765,7 @@ export interface MessageWithMapMessage { } export interface MessageWithMapMessageInput { - field1: boolean + field1?: boolean nestedMessages?: Map } @@ -776,7 +776,7 @@ export namespace MessageWithMapMessage { } export interface MessageWithMapMessage$nestedMessagesEntryInput { - key: string + key?: string value?: NestedMessageInput } @@ -1080,7 +1080,7 @@ export interface MessageWithPrimitiveMap { } export interface MessageWithPrimitiveMapInput { - field1: boolean + field1?: boolean nestedStrings?: Map } @@ -1091,8 +1091,8 @@ export namespace MessageWithPrimitiveMap { } export interface MessageWithPrimitiveMap$nestedStringsEntryInput { - key: string - value: string + key?: string + value?: string } export namespace MessageWithPrimitiveMap$nestedStringsEntry { @@ -1388,7 +1388,7 @@ export interface MessageWithRepeatedEnums { } export interface MessageWithRepeatedEnumsInput { - field1: boolean + field1?: boolean enums?: ENUM[] } diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index d42d2ee..d726b53 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -23,7 +23,7 @@ export interface SubMessage { } export interface SubMessageInput { - foo: string + foo?: string } export namespace SubMessage {