diff --git a/.gitignore b/.gitignore index d25b2ce..6134bbc 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules # Lock files package-lock.json yarn.lock +.clinic diff --git a/packages/protons-benchmark/src/protons/bench.ts b/packages/protons-benchmark/src/protons/bench.ts index 29f4a32..328e1a5 100644 --- a/packages/protons-benchmark/src/protons/bench.ts +++ b/packages/protons-benchmark/src/protons/bench.ts @@ -10,10 +10,16 @@ export interface Foo { } export namespace Foo { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'baz', codec: uint32 } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'baz', codec: uint32 } + }) + } + + return _codec } export const encode = (obj: Foo): Uint8ArrayList => { @@ -30,10 +36,16 @@ export interface Bar { } export namespace Bar { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'tmp', codec: Foo.codec() } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'tmp', codec: Foo.codec() } + }) + } + + return _codec } export const encode = (obj: Bar): Uint8ArrayList => { @@ -65,10 +77,16 @@ export interface Yo { } export namespace Yo { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'lol', codec: FOO.codec(), repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'lol', codec: FOO.codec(), repeats: true } + }) + } + + return _codec } export const encode = (obj: Yo): Uint8ArrayList => { @@ -86,11 +104,17 @@ export interface Lol { } export namespace Lol { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'lol', codec: string }, - 2: { name: 'b', codec: Bar.codec() } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'lol', codec: string }, + 2: { name: 'b', codec: Bar.codec() } + }) + } + + return _codec } export const encode = (obj: Lol): Uint8ArrayList => { @@ -110,13 +134,19 @@ export interface Test { } export namespace Test { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 6: { name: 'meh', codec: Lol.codec() }, - 3: { name: 'hello', codec: uint32 }, - 1: { name: 'foo', codec: string }, - 7: { name: 'payload', codec: bytes } - }) + if (_codec == null) { + _codec = message({ + 6: { name: 'meh', codec: Lol.codec() }, + 3: { name: 'hello', codec: uint32 }, + 1: { name: 'foo', codec: string }, + 7: { name: 'payload', codec: bytes } + }) + } + + return _codec } export const encode = (obj: Test): Uint8ArrayList => { diff --git a/packages/protons/src/index.ts b/packages/protons/src/index.ts index 77dcda3..0c1ad0d 100644 --- a/packages/protons/src/index.ts +++ b/packages/protons/src/index.ts @@ -191,30 +191,34 @@ export interface ${messageDef.name} { }` interfaceCodecDef = ` let _codec: Codec<${messageDef.name}> + export const codec = (): Codec<${messageDef.name}> => { - if (!_codec) _codec = message<${messageDef.name}>({ - ${Object.entries(fields) - .map(([name, fieldDef]) => { - let codec = encoders[fieldDef.type] - - if (codec == null) { - const def = findDef(fieldDef.type, messageDef, moduleDef) - - if (isEnumDef(def)) { - moduleDef.imports.add('enumeration') - } else { - moduleDef.imports.add('message') - } - - const typeName = findTypeName(fieldDef.type, messageDef, moduleDef) - codec = `${typeName}.codec()` - } else { - moduleDef.imports.add(codec) - } + if (_codec == null) { + _codec = message<${messageDef.name}>({ + ${Object.entries(fields) + .map(([name, fieldDef]) => { + let codec = encoders[fieldDef.type] + + if (codec == null) { + const def = findDef(fieldDef.type, messageDef, moduleDef) + + if (isEnumDef(def)) { + moduleDef.imports.add('enumeration') + } else { + moduleDef.imports.add('message') + } + + const typeName = findTypeName(fieldDef.type, messageDef, moduleDef) + codec = `${typeName}.codec()` + } else { + moduleDef.imports.add(codec) + } return `${fieldDef.id}: { name: '${name}', codec: ${codec}${fieldDef.options?.proto3_optional === true ? ', optional: true' : ''}${fieldDef.rule === 'repeated' ? ', repeats: true' : ''} }` - }).join(',\n ')} - }) + }).join(',\n ')} + }) + } + return _codec } diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index 10046fb..16fcd57 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -11,11 +11,17 @@ export interface Basic { } export namespace Basic { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'foo', codec: string, optional: true }, - 2: { name: 'num', codec: int32 } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'foo', codec: string, optional: true }, + 2: { name: 'num', codec: int32 } + }) + } + + return _codec } export const encode = (obj: Basic): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/circuit.ts b/packages/protons/test/fixtures/circuit.ts index ec47246..e258e02 100644 --- a/packages/protons/test/fixtures/circuit.ts +++ b/packages/protons/test/fixtures/circuit.ts @@ -83,11 +83,17 @@ export namespace CircuitRelay { } export namespace Peer { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'id', codec: bytes }, - 2: { name: 'addrs', codec: bytes, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'id', codec: bytes }, + 2: { name: 'addrs', codec: bytes, repeats: true } + }) + } + + return _codec } export const encode = (obj: Peer): Uint8ArrayList => { @@ -99,13 +105,19 @@ export namespace CircuitRelay { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true }, - 2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true }, - 3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true }, - 4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true }, + 2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true }, + 3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true }, + 4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true } + }) + } + + return _codec } export const encode = (obj: CircuitRelay): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index 3c0a958..09c9553 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -50,18 +50,24 @@ export namespace Request { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: Request.Type.codec() }, - 2: { name: 'connect', codec: ConnectRequest.codec(), optional: true }, - 3: { name: 'streamOpen', codec: StreamOpenRequest.codec(), optional: true }, - 4: { name: 'streamHandler', codec: StreamHandlerRequest.codec(), optional: true }, - 5: { name: 'dht', codec: DHTRequest.codec(), optional: true }, - 6: { name: 'connManager', codec: ConnManagerRequest.codec(), optional: true }, - 7: { name: 'disconnect', codec: DisconnectRequest.codec(), optional: true }, - 8: { name: 'pubsub', codec: PSRequest.codec(), optional: true }, - 9: { name: 'peerStore', codec: PeerstoreRequest.codec(), optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: Request.Type.codec() }, + 2: { name: 'connect', codec: ConnectRequest.codec(), optional: true }, + 3: { name: 'streamOpen', codec: StreamOpenRequest.codec(), optional: true }, + 4: { name: 'streamHandler', codec: StreamHandlerRequest.codec(), optional: true }, + 5: { name: 'dht', codec: DHTRequest.codec(), optional: true }, + 6: { name: 'connManager', codec: ConnManagerRequest.codec(), optional: true }, + 7: { name: 'disconnect', codec: DisconnectRequest.codec(), optional: true }, + 8: { name: 'pubsub', codec: PSRequest.codec(), optional: true }, + 9: { name: 'peerStore', codec: PeerstoreRequest.codec(), optional: true } + }) + } + + return _codec } export const encode = (obj: Request): Uint8ArrayList => { @@ -101,17 +107,23 @@ export namespace Response { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: Response.Type.codec() }, - 2: { name: 'error', codec: ErrorResponse.codec(), optional: true }, - 3: { name: 'streamInfo', codec: StreamInfo.codec(), optional: true }, - 4: { name: 'identify', codec: IdentifyResponse.codec(), optional: true }, - 5: { name: 'dht', codec: DHTResponse.codec(), optional: true }, - 6: { name: 'peers', codec: PeerInfo.codec(), repeats: true }, - 7: { name: 'pubsub', codec: PSResponse.codec(), optional: true }, - 8: { name: 'peerStore', codec: PeerstoreResponse.codec(), optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: Response.Type.codec() }, + 2: { name: 'error', codec: ErrorResponse.codec(), optional: true }, + 3: { name: 'streamInfo', codec: StreamInfo.codec(), optional: true }, + 4: { name: 'identify', codec: IdentifyResponse.codec(), optional: true }, + 5: { name: 'dht', codec: DHTResponse.codec(), optional: true }, + 6: { name: 'peers', codec: PeerInfo.codec(), repeats: true }, + 7: { name: 'pubsub', codec: PSResponse.codec(), optional: true }, + 8: { name: 'peerStore', codec: PeerstoreResponse.codec(), optional: true } + }) + } + + return _codec } export const encode = (obj: Response): Uint8ArrayList => { @@ -129,11 +141,17 @@ export interface IdentifyResponse { } export namespace IdentifyResponse { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'id', codec: bytes }, - 2: { name: 'addrs', codec: bytes, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'id', codec: bytes }, + 2: { name: 'addrs', codec: bytes, repeats: true } + }) + } + + return _codec } export const encode = (obj: IdentifyResponse): Uint8ArrayList => { @@ -152,12 +170,18 @@ export interface ConnectRequest { } export namespace ConnectRequest { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'peer', codec: bytes }, - 2: { name: 'addrs', codec: bytes, repeats: true }, - 3: { name: 'timeout', codec: int64, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'peer', codec: bytes }, + 2: { name: 'addrs', codec: bytes, repeats: true }, + 3: { name: 'timeout', codec: int64, optional: true } + }) + } + + return _codec } export const encode = (obj: ConnectRequest): Uint8ArrayList => { @@ -176,12 +200,18 @@ export interface StreamOpenRequest { } export namespace StreamOpenRequest { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'peer', codec: bytes }, - 2: { name: 'proto', codec: string, repeats: true }, - 3: { name: 'timeout', codec: int64, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'peer', codec: bytes }, + 2: { name: 'proto', codec: string, repeats: true }, + 3: { name: 'timeout', codec: int64, optional: true } + }) + } + + return _codec } export const encode = (obj: StreamOpenRequest): Uint8ArrayList => { @@ -199,11 +229,17 @@ export interface StreamHandlerRequest { } export namespace StreamHandlerRequest { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'addr', codec: bytes }, - 2: { name: 'proto', codec: string, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'addr', codec: bytes }, + 2: { name: 'proto', codec: string, repeats: true } + }) + } + + return _codec } export const encode = (obj: StreamHandlerRequest): Uint8ArrayList => { @@ -220,10 +256,16 @@ export interface ErrorResponse { } export namespace ErrorResponse { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'msg', codec: string } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'msg', codec: string } + }) + } + + return _codec } export const encode = (obj: ErrorResponse): Uint8ArrayList => { @@ -242,12 +284,18 @@ export interface StreamInfo { } export namespace StreamInfo { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'peer', codec: bytes }, - 2: { name: 'addr', codec: bytes }, - 3: { name: 'proto', codec: string } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'peer', codec: bytes }, + 2: { name: 'addr', codec: bytes }, + 3: { name: 'proto', codec: string } + }) + } + + return _codec } export const encode = (obj: StreamInfo): Uint8ArrayList => { @@ -300,16 +348,22 @@ export namespace DHTRequest { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: DHTRequest.Type.codec() }, - 2: { name: 'peer', codec: bytes, optional: true }, - 3: { name: 'cid', codec: bytes, optional: true }, - 4: { name: 'key', codec: bytes, optional: true }, - 5: { name: 'value', codec: bytes, optional: true }, - 6: { name: 'count', codec: int32, optional: true }, - 7: { name: 'timeout', codec: int64, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: DHTRequest.Type.codec() }, + 2: { name: 'peer', codec: bytes, optional: true }, + 3: { name: 'cid', codec: bytes, optional: true }, + 4: { name: 'key', codec: bytes, optional: true }, + 5: { name: 'value', codec: bytes, optional: true }, + 6: { name: 'count', codec: int32, optional: true }, + 7: { name: 'timeout', codec: int64, optional: true } + }) + } + + return _codec } export const encode = (obj: DHTRequest): Uint8ArrayList => { @@ -346,12 +400,18 @@ export namespace DHTResponse { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: DHTResponse.Type.codec() }, - 2: { name: 'peer', codec: PeerInfo.codec(), optional: true }, - 3: { name: 'value', codec: bytes, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: DHTResponse.Type.codec() }, + 2: { name: 'peer', codec: PeerInfo.codec(), optional: true }, + 3: { name: 'value', codec: bytes, optional: true } + }) + } + + return _codec } export const encode = (obj: DHTResponse): Uint8ArrayList => { @@ -369,11 +429,17 @@ export interface PeerInfo { } export namespace PeerInfo { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'id', codec: bytes }, - 2: { name: 'addrs', codec: bytes, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'id', codec: bytes }, + 2: { name: 'addrs', codec: bytes, repeats: true } + }) + } + + return _codec } export const encode = (obj: PeerInfo): Uint8ArrayList => { @@ -411,13 +477,19 @@ export namespace ConnManagerRequest { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: ConnManagerRequest.Type.codec() }, - 2: { name: 'peer', codec: bytes, optional: true }, - 3: { name: 'tag', codec: string, optional: true }, - 4: { name: 'weight', codec: int64, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: ConnManagerRequest.Type.codec() }, + 2: { name: 'peer', codec: bytes, optional: true }, + 3: { name: 'tag', codec: string, optional: true }, + 4: { name: 'weight', codec: int64, optional: true } + }) + } + + return _codec } export const encode = (obj: ConnManagerRequest): Uint8ArrayList => { @@ -434,10 +506,16 @@ export interface DisconnectRequest { } export namespace DisconnectRequest { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'peer', codec: bytes } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'peer', codec: bytes } + }) + } + + return _codec } export const encode = (obj: DisconnectRequest): Uint8ArrayList => { @@ -476,12 +554,18 @@ export namespace PSRequest { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: PSRequest.Type.codec() }, - 2: { name: 'topic', codec: string, optional: true }, - 3: { name: 'data', codec: bytes, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: PSRequest.Type.codec() }, + 2: { name: 'topic', codec: string, optional: true }, + 3: { name: 'data', codec: bytes, optional: true } + }) + } + + return _codec } export const encode = (obj: PSRequest): Uint8ArrayList => { @@ -503,15 +587,21 @@ export interface PSMessage { } export namespace PSMessage { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'from', codec: bytes, optional: true }, - 2: { name: 'data', codec: bytes, optional: true }, - 3: { name: 'seqno', codec: bytes, optional: true }, - 4: { name: 'topicIDs', codec: string, repeats: true }, - 5: { name: 'signature', codec: bytes, optional: true }, - 6: { name: 'key', codec: bytes, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'from', codec: bytes, optional: true }, + 2: { name: 'data', codec: bytes, optional: true }, + 3: { name: 'seqno', codec: bytes, optional: true }, + 4: { name: 'topicIDs', codec: string, repeats: true }, + 5: { name: 'signature', codec: bytes, optional: true }, + 6: { name: 'key', codec: bytes, optional: true } + }) + } + + return _codec } export const encode = (obj: PSMessage): Uint8ArrayList => { @@ -529,11 +619,17 @@ export interface PSResponse { } export namespace PSResponse { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'topics', codec: string, repeats: true }, - 2: { name: 'peerIDs', codec: bytes, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'topics', codec: string, repeats: true }, + 2: { name: 'peerIDs', codec: bytes, repeats: true } + }) + } + + return _codec } export const encode = (obj: PSResponse): Uint8ArrayList => { @@ -568,12 +664,18 @@ export namespace PeerstoreRequest { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: PeerstoreRequest.Type.codec() }, - 2: { name: 'id', codec: bytes, optional: true }, - 3: { name: 'protos', codec: string, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: PeerstoreRequest.Type.codec() }, + 2: { name: 'id', codec: bytes, optional: true }, + 3: { name: 'protos', codec: string, repeats: true } + }) + } + + return _codec } export const encode = (obj: PeerstoreRequest): Uint8ArrayList => { @@ -591,11 +693,17 @@ export interface PeerstoreResponse { } export namespace PeerstoreResponse { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'peer', codec: PeerInfo.codec(), optional: true }, - 2: { name: 'protos', codec: string, repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'peer', codec: PeerInfo.codec(), optional: true }, + 2: { name: 'protos', codec: string, repeats: true } + }) + } + + return _codec } export const encode = (obj: PeerstoreResponse): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index 125655e..929d3ba 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -14,14 +14,20 @@ export interface Record { } export namespace Record { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'key', codec: bytes, optional: true }, - 2: { name: 'value', codec: bytes, optional: true }, - 3: { name: 'author', codec: bytes, optional: true }, - 4: { name: 'signature', codec: bytes, optional: true }, - 5: { name: 'timeReceived', codec: string, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'key', codec: bytes, optional: true }, + 2: { name: 'value', codec: bytes, optional: true }, + 3: { name: 'author', codec: bytes, optional: true }, + 4: { name: 'signature', codec: bytes, optional: true }, + 5: { name: 'timeReceived', codec: string, optional: true } + }) + } + + return _codec } export const encode = (obj: Record): Uint8ArrayList => { @@ -94,12 +100,18 @@ export namespace Message { } export namespace Peer { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'id', codec: bytes, optional: true }, - 2: { name: 'addrs', codec: bytes, repeats: true }, - 3: { name: 'connection', codec: Message.ConnectionType.codec(), optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'id', codec: bytes, optional: true }, + 2: { name: 'addrs', codec: bytes, repeats: true }, + 3: { name: 'connection', codec: Message.ConnectionType.codec(), optional: true } + }) + } + + return _codec } export const encode = (obj: Peer): Uint8ArrayList => { @@ -111,15 +123,21 @@ export namespace Message { } } + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'type', codec: Message.MessageType.codec(), optional: true }, - 10: { name: 'clusterLevelRaw', codec: int32, optional: true }, - 2: { name: 'key', codec: bytes, optional: true }, - 3: { name: 'record', codec: bytes, optional: true }, - 8: { name: 'closerPeers', codec: Message.Peer.codec(), repeats: true }, - 9: { name: 'providerPeers', codec: Message.Peer.codec(), repeats: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'type', codec: Message.MessageType.codec(), optional: true }, + 10: { name: 'clusterLevelRaw', codec: int32, optional: true }, + 2: { name: 'key', codec: bytes, optional: true }, + 3: { name: 'record', codec: bytes, optional: true }, + 8: { name: 'closerPeers', codec: Message.Peer.codec(), repeats: true }, + 9: { name: 'providerPeers', codec: Message.Peer.codec(), repeats: true } + }) + } + + return _codec } export const encode = (obj: Message): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 1c75bc4..85a21b4 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -13,12 +13,18 @@ export namespace pb { } export namespace NoiseHandshakePayload { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'identityKey', codec: bytes }, - 2: { name: 'identitySig', codec: bytes }, - 3: { name: 'data', codec: bytes } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'identityKey', codec: bytes }, + 2: { name: 'identitySig', codec: bytes }, + 3: { name: 'data', codec: bytes } + }) + } + + return _codec } export const encode = (obj: NoiseHandshakePayload): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index f089a4f..33bee0a 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -14,14 +14,20 @@ export interface Peer { } export namespace Peer { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'addresses', codec: Address.codec(), repeats: true }, - 2: { name: 'protocols', codec: string, repeats: true }, - 3: { name: 'metadata', codec: Metadata.codec(), repeats: true }, - 4: { name: 'pubKey', codec: bytes, optional: true }, - 5: { name: 'peerRecordEnvelope', codec: bytes, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'addresses', codec: Address.codec(), repeats: true }, + 2: { name: 'protocols', codec: string, repeats: true }, + 3: { name: 'metadata', codec: Metadata.codec(), repeats: true }, + 4: { name: 'pubKey', codec: bytes, optional: true }, + 5: { name: 'peerRecordEnvelope', codec: bytes, optional: true } + }) + } + + return _codec } export const encode = (obj: Peer): Uint8ArrayList => { @@ -39,11 +45,17 @@ export interface Address { } export namespace Address { + let _codec: Codec
+ export const codec = (): Codec
=> { - return message
({ - 1: { name: 'multiaddr', codec: bytes }, - 2: { name: 'isCertified', codec: bool, optional: true } - }) + if (_codec == null) { + _codec = message
({ + 1: { name: 'multiaddr', codec: bytes }, + 2: { name: 'isCertified', codec: bool, optional: true } + }) + } + + return _codec } export const encode = (obj: Address): Uint8ArrayList => { @@ -61,11 +73,17 @@ export interface Metadata { } export namespace Metadata { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'key', codec: string }, - 2: { name: 'value', codec: bytes } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'key', codec: string }, + 2: { name: 'value', codec: bytes } + }) + } + + return _codec } export const encode = (obj: Metadata): Uint8ArrayList => { diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index 11e6b16..e3c7132 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -25,10 +25,16 @@ export interface SubMessage { } export namespace SubMessage { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'foo', codec: string } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'foo', codec: string } + }) + } + + return _codec } export const encode = (obj: SubMessage): Uint8ArrayList => { @@ -62,27 +68,33 @@ export interface AllTheTypes { } export namespace AllTheTypes { + let _codec: Codec + export const codec = (): Codec => { - return message({ - 1: { name: 'field1', codec: bool, optional: true }, - 2: { name: 'field2', codec: int32, optional: true }, - 3: { name: 'field3', codec: int64, optional: true }, - 4: { name: 'field4', codec: uint32, optional: true }, - 5: { name: 'field5', codec: uint64, optional: true }, - 6: { name: 'field6', codec: sint32, optional: true }, - 7: { name: 'field7', codec: sint64, optional: true }, - 8: { name: 'field8', codec: double, optional: true }, - 9: { name: 'field9', codec: float, optional: true }, - 10: { name: 'field10', codec: string, optional: true }, - 11: { name: 'field11', codec: bytes, optional: true }, - 12: { name: 'field12', codec: AnEnum.codec(), optional: true }, - 13: { name: 'field13', codec: SubMessage.codec(), optional: true }, - 14: { name: 'field14', codec: string, repeats: true }, - 15: { name: 'field15', codec: fixed32, optional: true }, - 16: { name: 'field16', codec: fixed64, optional: true }, - 17: { name: 'field17', codec: sfixed32, optional: true }, - 18: { name: 'field18', codec: sfixed64, optional: true } - }) + if (_codec == null) { + _codec = message({ + 1: { name: 'field1', codec: bool, optional: true }, + 2: { name: 'field2', codec: int32, optional: true }, + 3: { name: 'field3', codec: int64, optional: true }, + 4: { name: 'field4', codec: uint32, optional: true }, + 5: { name: 'field5', codec: uint64, optional: true }, + 6: { name: 'field6', codec: sint32, optional: true }, + 7: { name: 'field7', codec: sint64, optional: true }, + 8: { name: 'field8', codec: double, optional: true }, + 9: { name: 'field9', codec: float, optional: true }, + 10: { name: 'field10', codec: string, optional: true }, + 11: { name: 'field11', codec: bytes, optional: true }, + 12: { name: 'field12', codec: AnEnum.codec(), optional: true }, + 13: { name: 'field13', codec: SubMessage.codec(), optional: true }, + 14: { name: 'field14', codec: string, repeats: true }, + 15: { name: 'field15', codec: fixed32, optional: true }, + 16: { name: 'field16', codec: fixed64, optional: true }, + 17: { name: 'field17', codec: sfixed32, optional: true }, + 18: { name: 'field18', codec: sfixed64, optional: true } + }) + } + + return _codec } export const encode = (obj: AllTheTypes): Uint8ArrayList => {