diff --git a/packages/protons-benchmark/src/implementations/protons/bench.ts b/packages/protons-benchmark/src/implementations/protons/bench.ts index b07d9ba..5fe5130 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 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() } @@ -92,7 +96,7 @@ export namespace Foo { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: FooInput): Uint8Array { return encodeMessage(obj, Foo.codec()) } @@ -109,12 +113,16 @@ export interface Bar { tmp?: Foo } +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() } @@ -207,7 +215,7 @@ export namespace Bar { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BarInput): 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 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() } @@ -349,7 +361,7 @@ export namespace Yo { value: FOO } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: YoInput): Uint8Array { return encodeMessage(obj, Yo.codec()) } @@ -367,12 +379,17 @@ export interface Lol { b?: Bar } +export interface LolInput { + lol?: string + 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() } @@ -496,7 +513,7 @@ export namespace Lol { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: LolInput): Uint8Array { return encodeMessage(obj, Lol.codec()) } @@ -516,12 +533,19 @@ export interface Test { payload?: Uint8Array } +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() } @@ -702,7 +726,7 @@ export namespace Test { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: TestInput): Uint8Array { return encodeMessage(obj, Test.codec()) } diff --git a/packages/protons-runtime/src/codec.ts b/packages/protons-runtime/src/codec.ts index 83fbce3..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 @@ -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..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, { 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..a947dba 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 `${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 1c55723..c122f89 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.proto2Required ? '' : '?'}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType.encode}` } getDefaultField (parent: Parent): string { @@ -188,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/src/fields/map-field.ts b/packages/protons/src/fields/map-field.ts index 5b37827..93044d2 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..d948f84 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}Input`, + 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}Input {} + export interface ${this.pbType} {}` } else { interfaceDef = ` export interface ${this.pbType} { - ${interfaceFields} + ${decoderFields.join('\n ').trim()} +} + +export interface ${this.pbType}Input { + ${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}Input> - export const codec = (): Codec<${this.pbType}> => { + export const codec = (): Codec<${this.pbType}, ${this.pbType}Input> => { if (_codec == null) { - _codec = message<${this.pbType}>((obj, w, opts = {}) => { + _codec = message<${this.pbType}, ${this.pbType}Input>((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: ${this.pbType}Input): 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..23b16de 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}Input`, + 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: BasicInput): Uint8Array { return encodeMessage(obj, Basic.codec()) } @@ -129,14 +134,16 @@ export namespace Basic { } } +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() } @@ -196,7 +203,7 @@ export namespace Empty { return _codec } - export function encode (obj: Partial): 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 e0452ed..4c48eb2 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 MessageInput { + wantlist?: Message.WantlistInput + blocks?: Uint8Array[] + payload?: Message.BlockInput[] + blockPresences?: Message.BlockPresenceInput[] + pendingBytes?: number +} + export namespace Message { export interface Wantlist { entries: Message.Wantlist.Entry[] full: boolean } + export interface WantlistInput { + entries?: Message.Wantlist.EntryInput[] + 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 EntryInput { + 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: EntryInput): 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: WantlistInput): Uint8Array { return encodeMessage(obj, Wantlist.codec()) } @@ -419,12 +440,17 @@ export namespace Message { 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() } @@ -529,7 +555,7 @@ export namespace Message { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BlockInput): 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 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() } @@ -673,7 +704,7 @@ export namespace Message { value: Message.BlockPresenceType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: BlockPresenceInput): 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: MessageInput): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index 74fffb6..2e17872 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 CircuitRelayInput { + type?: CircuitRelay.Type + srcPeer?: CircuitRelay.PeerInput + dstPeer?: CircuitRelay.PeerInput + 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 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() } @@ -209,7 +221,7 @@ export namespace CircuitRelay { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerInput): 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: 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 9cea0d0..39e56f1 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 CustomOptionNumberInput { + 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$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() } @@ -129,7 +145,7 @@ export namespace CustomOptionNumber { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionNumber$i64MapEntryInput): 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: CustomOptionNumberInput): Uint8Array { return encodeMessage(obj, CustomOptionNumber.codec()) } @@ -447,18 +463,34 @@ export interface CustomOptionString { i64Map: Map } +export interface CustomOptionStringInput { + 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$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() } @@ -563,7 +595,7 @@ export namespace CustomOptionString { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: CustomOptionString$i64MapEntryInput): 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: CustomOptionStringInput): Uint8Array { return encodeMessage(obj, CustomOptionString.codec()) } diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 1a0ec8a..fa42eec 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 RequestInput { + type?: Request.Type + connect?: ConnectRequestInput + streamOpen?: StreamOpenRequestInput + streamHandler?: StreamHandlerRequestInput + dht?: DHTRequestInput + connManager?: ConnManagerRequestInput + disconnect?: DisconnectRequestInput + pubsub?: PSRequestInput + peerStore?: PeerstoreRequestInput +} + 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: RequestInput): Uint8Array { return encodeMessage(obj, Request.codec()) } @@ -517,6 +529,17 @@ export interface Response { peerStore?: PeerstoreResponse } +export interface ResponseInput { + type?: Response.Type + error?: ErrorResponseInput + streamInfo?: StreamInfoInput + identify?: IdentifyResponseInput + dht?: DHTResponseInput + peers?: PeerInfoInput[] + pubsub?: PSResponseInput + peerStore?: PeerstoreResponseInput +} + 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: ResponseInput): Uint8Array { return encodeMessage(obj, Response.codec()) } @@ -974,12 +997,17 @@ export interface IdentifyResponse { addrs: 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() } @@ -1103,7 +1131,7 @@ export namespace IdentifyResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: IdentifyResponseInput): Uint8Array { return encodeMessage(obj, IdentifyResponse.codec()) } @@ -1122,12 +1150,18 @@ export interface ConnectRequest { timeout?: bigint } +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() } @@ -1272,7 +1306,7 @@ export namespace ConnectRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ConnectRequestInput): Uint8Array { return encodeMessage(obj, ConnectRequest.codec()) } @@ -1291,12 +1325,18 @@ export interface StreamOpenRequest { timeout?: bigint } +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() } @@ -1441,7 +1481,7 @@ export namespace StreamOpenRequest { value: bigint } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamOpenRequestInput): Uint8Array { return encodeMessage(obj, StreamOpenRequest.codec()) } @@ -1459,12 +1499,17 @@ export interface StreamHandlerRequest { proto: string[] } +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() } @@ -1588,7 +1633,7 @@ export namespace StreamHandlerRequest { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamHandlerRequestInput): Uint8Array { return encodeMessage(obj, StreamHandlerRequest.codec()) } @@ -1605,12 +1650,16 @@ export interface ErrorResponse { 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() } @@ -1693,7 +1742,7 @@ export namespace ErrorResponse { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: ErrorResponseInput): Uint8Array { return encodeMessage(obj, ErrorResponse.codec()) } @@ -1712,12 +1761,18 @@ export interface StreamInfo { 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() } @@ -1844,7 +1899,7 @@ export namespace StreamInfo { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: StreamInfoInput): Uint8Array { return encodeMessage(obj, StreamInfo.codec()) } @@ -1867,6 +1922,16 @@ export interface DHTRequest { timeout?: bigint } +export interface DHTRequestInput { + 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: DHTRequestInput): Uint8Array { return encodeMessage(obj, DHTRequest.codec()) } @@ -2130,6 +2195,12 @@ export interface DHTResponse { value?: Uint8Array } +export interface DHTResponseInput { + type?: DHTResponse.Type + peer?: PeerInfoInput + 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: DHTResponseInput): Uint8Array { return encodeMessage(obj, DHTResponse.codec()) } @@ -2314,12 +2385,17 @@ export interface PeerInfo { addrs: 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() } @@ -2443,7 +2519,7 @@ export namespace PeerInfo { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerInfoInput): Uint8Array { return encodeMessage(obj, PeerInfo.codec()) } @@ -2463,6 +2539,13 @@ export interface ConnManagerRequest { weight?: bigint } +export interface ConnManagerRequestInput { + 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: ConnManagerRequestInput): Uint8Array { return encodeMessage(obj, ConnManagerRequest.codec()) } @@ -2649,12 +2732,16 @@ export interface DisconnectRequest { 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() } @@ -2737,7 +2824,7 @@ export namespace DisconnectRequest { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: DisconnectRequestInput): Uint8Array { return encodeMessage(obj, DisconnectRequest.codec()) } @@ -2756,6 +2843,12 @@ export interface PSRequest { data?: Uint8Array } +export interface PSRequestInput { + 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: PSRequestInput): Uint8Array { return encodeMessage(obj, PSRequest.codec()) } @@ -2928,12 +3021,21 @@ export interface PSMessage { key?: Uint8Array } +export interface PSMessageInput { + 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: PSMessageInput): Uint8Array { return encodeMessage(obj, PSMessage.codec()) } @@ -3158,12 +3260,17 @@ export interface PSResponse { peerIDs: Uint8Array[] } +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() } @@ -3303,7 +3410,7 @@ export namespace PSResponse { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PSResponseInput): Uint8Array { return encodeMessage(obj, PSResponse.codec()) } @@ -3322,6 +3429,12 @@ export interface PeerstoreRequest { protos: string[] } +export interface PeerstoreRequestInput { + 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: PeerstoreRequestInput): Uint8Array { return encodeMessage(obj, PeerstoreRequest.codec()) } @@ -3508,12 +3621,17 @@ export interface PeerstoreResponse { protos: string[] } +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() } @@ -3654,7 +3772,7 @@ export namespace PeerstoreResponse { value: string } - export function encode (obj: Partial): 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 a7626f8..be998ac 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 RecordInput { + 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: RecordInput): Uint8Array { return encodeMessage(obj, Record.codec()) } @@ -202,6 +210,15 @@ export interface Message { providerPeers: Message.Peer[] } +export interface MessageInput { + type?: Message.MessageType + clusterLevelRaw?: number + key?: Uint8Array + record?: Uint8Array + closerPeers?: Message.PeerInput[] + providerPeers?: Message.PeerInput[] +} + 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 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() } @@ -402,7 +425,7 @@ export namespace Message { value: Message.ConnectionType } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerInput): 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: MessageInput): Uint8Array { return encodeMessage(obj, Message.codec()) } diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index f939e48..81506d6 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 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() } @@ -154,7 +159,7 @@ export namespace SubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -175,18 +180,31 @@ export interface MapTypes { enumMap: Map } +export interface MapTypesInput { + stringMap?: Map + intMap?: Map + boolMap?: Map + messageMap?: Map + enumMap?: Map +} + export namespace MapTypes { export interface MapTypes$stringMapEntry { 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() } @@ -291,7 +309,7 @@ export namespace MapTypes { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$stringMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$stringMapEntry.codec()) } @@ -309,12 +327,17 @@ export namespace MapTypes { 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() } @@ -419,7 +442,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$intMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$intMapEntry.codec()) } @@ -437,12 +460,17 @@ export namespace MapTypes { 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() } @@ -547,7 +575,7 @@ export namespace MapTypes { value: boolean } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$boolMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$boolMapEntry.codec()) } @@ -565,12 +593,17 @@ export namespace MapTypes { value?: SubMessage } + 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() } @@ -692,7 +725,7 @@ export namespace MapTypes { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$messageMapEntryInput): Uint8Array { return encodeMessage(obj, MapTypes$messageMapEntry.codec()) } @@ -710,12 +743,17 @@ export namespace MapTypes { 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() } @@ -820,7 +858,7 @@ export namespace MapTypes { value: EnumValue } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MapTypes$enumMapEntryInput): 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: MapTypesInput): Uint8Array { return encodeMessage(obj, MapTypes.codec()) } diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 9b25a48..13a7ed8 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 pbInput {} + export interface pb {} export namespace pb { @@ -12,12 +14,18 @@ export namespace pb { 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() } @@ -144,7 +152,7 @@ export namespace pb { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: NoiseHandshakePayloadInput): 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: pbInput): Uint8Array { return encodeMessage(obj, pb.codec()) } diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index 0a01cd1..29f7488 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 OneOfMessageInput { + 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: OneOfMessageInput): Uint8Array { return encodeMessage(obj, OneOfMessage.codec()) } @@ -253,12 +261,20 @@ export interface MessageWithoutOneOfs { 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() } @@ -429,7 +445,7 @@ export namespace MessageWithoutOneOfs { value: string } - export function encode (obj: Partial): 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 6e407a1..70bb5b5 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 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() } @@ -132,7 +137,7 @@ export namespace OptionalSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: OptionalSubMessageInput): Uint8Array { return encodeMessage(obj, OptionalSubMessage.codec()) } @@ -165,12 +170,32 @@ export interface Optional { subMessage?: OptionalSubMessage } +export interface OptionalInput { + 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?: 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() } @@ -604,7 +629,7 @@ export namespace Optional { value: number } - export function encode (obj: Partial): 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 afcda87..f2f43e9 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 PeerInput { + addresses?: AddressInput[] + protocols?: string[] + 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() } @@ -286,7 +294,7 @@ export namespace Peer { value: Uint8Array } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: PeerInput): Uint8Array { return encodeMessage(obj, Peer.codec()) } @@ -304,12 +312,17 @@ export interface Address { isCertified?: boolean } +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() } @@ -413,7 +426,7 @@ export namespace Address { value: boolean } - export function encode (obj: Partial
): Uint8Array { + export function encode (obj: AddressInput): Uint8Array { return encodeMessage(obj, Address.codec()) } @@ -431,12 +444,17 @@ export interface Metadata { 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() } @@ -541,7 +559,7 @@ export namespace Metadata { value: Uint8Array } - export function encode (obj: Partial): 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 78580f2..014113d 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -6,20 +6,22 @@ export interface MessageWithRequired { 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() } - if (obj.scalarField != null) { - w.uint32(8) - w.int32(obj.scalarField) - } + w.uint32(8) + w.int32(obj.scalarField) if (opts.lengthDelimited !== false) { w.ldelim() @@ -94,7 +96,7 @@ export namespace MessageWithRequired { value: number } - export function encode (obj: Partial): 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 462fc4b..c6c79d8 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 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() } @@ -121,7 +125,7 @@ export namespace MessageWithSizeLimitedRepeatedField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithSizeLimitedRepeatedFieldInput): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedRepeatedField.codec()) } @@ -138,18 +142,27 @@ export interface MessageWithSizeLimitedMap { mapField: Map } +export interface MessageWithSizeLimitedMapInput { + mapField?: Map +} + export namespace MessageWithSizeLimitedMap { export interface MessageWithSizeLimitedMap$mapFieldEntry { 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() } @@ -254,7 +267,7 @@ export namespace MessageWithSizeLimitedMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithSizeLimitedMap$mapFieldEntryInput): 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: MessageWithSizeLimitedMapInput): Uint8Array { return encodeMessage(obj, MessageWithSizeLimitedMap.codec()) } diff --git a/packages/protons/test/fixtures/repeated.ts b/packages/protons/test/fixtures/repeated.ts index 3fca755..de8b711 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 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() } @@ -135,7 +140,7 @@ export namespace SubSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubSubMessageInput): Uint8Array { return encodeMessage(obj, SubSubMessage.codec()) } @@ -155,12 +160,19 @@ export interface SubMessage { messages: SubSubMessage[] } +export interface SubMessageInput { + foo?: string[] + nonRepeating?: number + 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() } @@ -385,7 +397,7 @@ export namespace SubMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -406,12 +418,20 @@ export interface RepeatedTypes { nonRepeating?: number } +export interface RepeatedTypesInput { + number?: number[] + limitedNumber?: number[] + 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() } @@ -779,7 +799,7 @@ export namespace RepeatedTypes { value: number } - export function encode (obj: Partial): 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 d9f5c8c..1f931a3 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 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() } @@ -136,7 +141,7 @@ export namespace SingularSubMessage { value: number } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SingularSubMessageInput): Uint8Array { return encodeMessage(obj, SingularSubMessage.codec()) } @@ -169,12 +174,32 @@ export interface Singular { subMessage?: SingularSubMessage } +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() } @@ -625,7 +650,7 @@ export namespace Singular { value: number } - export function encode (obj: Partial): 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 8e8f960..e07c504 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 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() } @@ -157,7 +163,7 @@ export namespace MessageWithArrayField { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithArrayFieldInput): Uint8Array { return encodeMessage(obj, MessageWithArrayField.codec()) } @@ -174,12 +180,16 @@ export interface NestedMessage { 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() } @@ -262,7 +272,7 @@ export namespace NestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: NestedMessageInput): Uint8Array { return encodeMessage(obj, NestedMessage.codec()) } @@ -280,12 +290,17 @@ export interface MessageWithNestedMessage { nestedMessage?: NestedMessage } +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() } @@ -401,7 +416,7 @@ export namespace MessageWithNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithNestedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithNestedMessage.codec()) } @@ -419,12 +434,17 @@ export interface MessageWithDeeplyNestedMessage { nestedMessage?: MessageWithNestedMessage } +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() } @@ -555,7 +575,7 @@ export namespace MessageWithDeeplyNestedMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithDeeplyNestedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithDeeplyNestedMessage.codec()) } @@ -573,12 +593,17 @@ export interface MessageWithRepeatedMessage { nestedMessages: NestedMessage[] } +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() } @@ -721,7 +746,7 @@ export namespace MessageWithRepeatedMessage { message: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithRepeatedMessageInput): Uint8Array { return encodeMessage(obj, MessageWithRepeatedMessage.codec()) } @@ -739,18 +764,28 @@ export interface MessageWithMapMessage { nestedMessages: Map } +export interface MessageWithMapMessageInput { + field1?: boolean + nestedMessages?: Map +} + export namespace MessageWithMapMessage { export interface MessageWithMapMessage$nestedMessagesEntry { key: string value?: NestedMessage } + 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() } @@ -866,7 +901,7 @@ export namespace MessageWithMapMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithMapMessage$nestedMessagesEntryInput): 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: MessageWithMapMessageInput): Uint8Array { return encodeMessage(obj, MessageWithMapMessage.codec()) } @@ -1044,18 +1079,28 @@ export interface MessageWithPrimitiveMap { nestedStrings: Map } +export interface MessageWithPrimitiveMapInput { + field1?: boolean + nestedStrings?: Map +} + export namespace MessageWithPrimitiveMap { export interface MessageWithPrimitiveMap$nestedStringsEntry { 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() } @@ -1160,7 +1205,7 @@ export namespace MessageWithPrimitiveMap { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: MessageWithPrimitiveMap$nestedStringsEntryInput): 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: MessageWithPrimitiveMapInput): 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 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() } @@ -1471,7 +1521,7 @@ export namespace MessageWithRepeatedEnums { value: ENUM } - export function encode (obj: Partial): 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 6f9be94..d726b53 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 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() } @@ -110,7 +114,7 @@ export namespace SubMessage { value: string } - export function encode (obj: Partial): Uint8Array { + export function encode (obj: SubMessageInput): Uint8Array { return encodeMessage(obj, SubMessage.codec()) } @@ -144,12 +148,33 @@ export interface AllTheTypes { field18?: bigint } +export interface AllTheTypesInput { + 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?: SubMessageInput + 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: AllTheTypesInput): Uint8Array { return encodeMessage(obj, AllTheTypes.codec()) }