starpc 0.31.6 → 0.31.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/Makefile +4 -4
  2. package/cmd/protoc-gen-es-starpc/typescript.ts +17 -16
  3. package/dist/cmd/protoc-gen-es-starpc/typescript.js +15 -14
  4. package/dist/e2e/mock/mock_pb.d.ts +5 -12
  5. package/dist/e2e/mock/mock_pb.js +9 -33
  6. package/dist/e2e/mock/mock_srpc.pb.d.ts +5 -5
  7. package/dist/e2e/mock/mock_srpc.pb.js +2 -2
  8. package/dist/echo/echo_pb.d.ts +5 -12
  9. package/dist/echo/echo_pb.js +9 -33
  10. package/dist/echo/echo_srpc.pb.d.ts +17 -17
  11. package/dist/echo/echo_srpc.pb.js +4 -4
  12. package/dist/echo/server.d.ts +3 -3
  13. package/dist/rpcstream/rpcstream.d.ts +3 -3
  14. package/dist/rpcstream/rpcstream_pb.d.ts +36 -57
  15. package/dist/rpcstream/rpcstream_pb.js +28 -106
  16. package/dist/srpc/client-rpc.d.ts +1 -1
  17. package/dist/srpc/client-rpc.js +1 -1
  18. package/dist/srpc/common-rpc.d.ts +4 -5
  19. package/dist/srpc/definition.d.ts +2 -1
  20. package/dist/srpc/invoker.d.ts +3 -2
  21. package/dist/srpc/invoker.js +1 -1
  22. package/dist/srpc/message.d.ts +4 -4
  23. package/dist/srpc/message.js +3 -3
  24. package/dist/srpc/pushable.d.ts +2 -2
  25. package/dist/srpc/pushable.js +1 -1
  26. package/dist/srpc/rpcproto_pb.d.ts +55 -76
  27. package/dist/srpc/rpcproto_pb.js +30 -150
  28. package/e2e/mock/mock_pb.ts +18 -50
  29. package/e2e/mock/mock_srpc.pb.ts +7 -7
  30. package/echo/client-test.ts +2 -2
  31. package/echo/echo_pb.ts +18 -50
  32. package/echo/echo_srpc.pb.ts +13 -13
  33. package/echo/server.ts +3 -3
  34. package/go.mod +1 -1
  35. package/go.sum +2 -0
  36. package/package.json +22 -7
  37. package/srpc/client-rpc.ts +4 -4
  38. package/srpc/common-rpc.ts +9 -9
  39. package/srpc/definition.ts +2 -1
  40. package/srpc/invoker.ts +7 -12
  41. package/srpc/message.ts +9 -11
  42. package/srpc/pushable.ts +4 -6
  43. package/srpc/rpcproto_pb.ts +91 -188
@@ -1,6 +1,6 @@
1
1
  import type { Sink, Source } from 'it-stream-types'
2
2
  import { pushable } from 'it-pushable'
3
- import { PartialMessage } from '@bufbuild/protobuf'
3
+ import { CompleteMessage } from '@aptre/protobuf-es-lite'
4
4
 
5
5
  import type { CallData, CallStart } from './rpcproto_pb.js'
6
6
  import { Packet } from './rpcproto_pb.js'
@@ -9,14 +9,14 @@ import { ERR_RPC_ABORT } from './errors.js'
9
9
  // CommonRPC is common logic between server and client RPCs.
10
10
  export class CommonRPC {
11
11
  // sink is the data sink for incoming messages.
12
- public readonly sink: Sink<Source<PartialMessage<Packet>>>
12
+ public readonly sink: Sink<Source<Packet>>
13
13
  // source is the packet source for outgoing Packets.
14
- public readonly source: AsyncIterable<PartialMessage<Packet>>
14
+ public readonly source: AsyncIterable<Packet>
15
15
  // rpcDataSource is the source for rpc packets.
16
16
  public readonly rpcDataSource: AsyncIterable<Uint8Array>
17
17
 
18
18
  // _source is used to write to the source.
19
- private readonly _source = pushable<PartialMessage<Packet>>({
19
+ private readonly _source = pushable<Packet>({
20
20
  objectMode: true,
21
21
  })
22
22
 
@@ -50,7 +50,7 @@ export class CommonRPC {
50
50
  complete?: boolean,
51
51
  error?: string,
52
52
  ) {
53
- const callData: PartialMessage<CallData> = {
53
+ const callData: CompleteMessage<CallData> = {
54
54
  data: data || new Uint8Array(0),
55
55
  dataIsZero: !!data && data.length === 0,
56
56
  complete: complete || false,
@@ -87,7 +87,7 @@ export class CommonRPC {
87
87
  }
88
88
 
89
89
  // writePacket writes a packet to the stream.
90
- protected async writePacket(packet: PartialMessage<Packet>) {
90
+ protected async writePacket(packet: Packet) {
91
91
  this._source.push(packet)
92
92
  }
93
93
 
@@ -101,7 +101,7 @@ export class CommonRPC {
101
101
  // handlePacket handles an incoming packet.
102
102
  //
103
103
  // note: closes the stream if any error is thrown.
104
- public async handlePacket(packet: PartialMessage<Packet>) {
104
+ public async handlePacket(packet: Packet) {
105
105
  // console.log('handlePacket', packet)
106
106
  try {
107
107
  switch (packet?.body?.case) {
@@ -182,8 +182,8 @@ export class CommonRPC {
182
182
  }
183
183
 
184
184
  // _createSink returns a value for the sink field.
185
- private _createSink(): Sink<Source<PartialMessage<Packet>>> {
186
- return async (source: Source<PartialMessage<Packet>>) => {
185
+ private _createSink(): Sink<Source<Packet>> {
186
+ return async (source: Source<Packet>) => {
187
187
  try {
188
188
  if (Symbol.asyncIterator in source) {
189
189
  // Handle async source
@@ -1,4 +1,5 @@
1
- import { MessageType, MethodKind, MethodIdempotency } from '@bufbuild/protobuf'
1
+ import { MethodIdempotency, MethodKind } from '@bufbuild/protobuf'
2
+ import { MessageType } from '@aptre/protobuf-es-lite'
2
3
 
3
4
  // ServiceMethodDefinitions is the type of the methods map on the service definition.
4
5
  export type ServiceMethodDefinitions = {
package/srpc/invoker.ts CHANGED
@@ -8,20 +8,15 @@ import {
8
8
  buildEncodeMessageTransform,
9
9
  } from './message.js'
10
10
  import { writeToPushable } from './pushable.js'
11
- import {
12
- Message,
13
- MessageType,
14
- MethodIdempotency,
15
- MethodKind,
16
- PartialMessage,
17
- } from '@bufbuild/protobuf'
11
+ import type { MessageType, Message } from '@aptre/protobuf-es-lite'
12
+ import { MethodIdempotency, MethodKind } from '@bufbuild/protobuf'
18
13
 
19
14
  // MethodProto is a function which matches one of the RPC signatures.
20
15
  export type MethodProto<R extends Message<R>, O extends Message<O>> =
21
16
  | ((request: R) => Promise<O>)
22
17
  | ((request: R) => AsyncIterable<O>)
23
- | ((request: AsyncIterable<PartialMessage<R>>) => Promise<O>)
24
- | ((request: AsyncIterable<PartialMessage<R>>) => AsyncIterable<O>)
18
+ | ((request: AsyncIterable<Message<R>>) => Promise<O>)
19
+ | ((request: AsyncIterable<Message<R>>) => AsyncIterable<O>)
25
20
 
26
21
  // createInvokeFn builds an InvokeFn from a method definition and a function prototype.
27
22
  export function createInvokeFn<R extends Message<R>, O extends Message<O>>(
@@ -39,7 +34,7 @@ export function createInvokeFn<R extends Message<R>, O extends Message<O>>(
39
34
  dataSink: Sink<Source<Uint8Array>>,
40
35
  ) => {
41
36
  // responseSink is a Sink for response messages.
42
- const responseSink = pushable<PartialMessage<O>>({
37
+ const responseSink = pushable<Message<O>>({
43
38
  objectMode: true,
44
39
  })
45
40
 
@@ -81,11 +76,11 @@ export function createInvokeFn<R extends Message<R>, O extends Message<O>>(
81
76
  ) {
82
77
  const response = responseObj as AsyncIterable<O>
83
78
  return writeToPushable(
84
- response as AsyncIterable<PartialMessage<O>>,
79
+ response as AsyncIterable<Message<O>>,
85
80
  responseSink,
86
81
  )
87
82
  } else {
88
- const responsePromise = responseObj as Promise<PartialMessage<O>>
83
+ const responsePromise = responseObj as Promise<Message<O>>
89
84
  if (!responsePromise.then) {
90
85
  throw new Error('expected return value to be a Promise')
91
86
  }
package/srpc/message.ts CHANGED
@@ -1,18 +1,16 @@
1
+ import { MessageType, Message } from '@aptre/protobuf-es-lite'
1
2
  import type { Source } from 'it-stream-types'
2
- import { Message, PartialMessage, MessageType } from '@bufbuild/protobuf'
3
3
  import memoize from 'memoize-one'
4
4
 
5
5
  // MessageStream is an async iterable of partial messages.
6
- export type MessageStream<T extends Message<T>> = AsyncIterable<
7
- PartialMessage<T>
8
- >
6
+ export type MessageStream<T extends Message<T>> = AsyncIterable<Message<T>>
9
7
 
10
8
  // memoProto returns a function that encodes the message and caches the result.
11
9
  export function memoProto<T extends Message<T>>(
12
10
  def: MessageType<T>,
13
- ): (msg: PartialMessage<T>) => Uint8Array {
14
- return memoize((msg: PartialMessage<T>): Uint8Array => {
15
- return new def(msg).toBinary()
11
+ ): (msg: Message<T>) => Uint8Array {
12
+ return memoize((msg: Message<T>): Uint8Array => {
13
+ return def.toBinary(def.create(msg))
16
14
  })
17
15
  }
18
16
 
@@ -56,7 +54,7 @@ export function buildDecodeMessageTransform<T extends Message<T>>(
56
54
 
57
55
  // EncodeMessageTransform is a transformer that encodes messages.
58
56
  export type EncodeMessageTransform<T extends Message<T>> = (
59
- source: Source<PartialMessage<T> | Array<PartialMessage<T>>>,
57
+ source: Source<Message<T> | Array<Message<T>>>,
60
58
  ) => AsyncIterable<Uint8Array>
61
59
 
62
60
  // buildEncodeMessageTransform builds a transformer that encodes messages.
@@ -65,15 +63,15 @@ export function buildEncodeMessageTransform<T extends Message<T>>(
65
63
  ): EncodeMessageTransform<T> {
66
64
  // encodeMessageSource marshals and async yields Messages.
67
65
  return async function* encodeMessageSource(
68
- source: Source<PartialMessage<T> | Array<PartialMessage<T>>>,
66
+ source: Source<Message<T> | Array<Message<T>>>,
69
67
  ): AsyncIterable<Uint8Array> {
70
68
  for await (const pkt of source) {
71
69
  if (Array.isArray(pkt)) {
72
70
  for (const p of pkt) {
73
- yield new def(p).toBinary()
71
+ yield def.toBinary(def.create(p))
74
72
  }
75
73
  } else {
76
- yield new def(pkt).toBinary()
74
+ yield def.toBinary(def.create(pkt))
77
75
  }
78
76
  }
79
77
  }
package/srpc/pushable.ts CHANGED
@@ -1,12 +1,10 @@
1
+ import { Message } from '@aptre/protobuf-es-lite'
1
2
  import { Pushable, pushable } from 'it-pushable'
2
3
  import { Sink, Source } from 'it-stream-types'
3
- import { Message, PartialMessage } from '@bufbuild/protobuf'
4
4
 
5
- // messagePushable is a shortcut to build a pushable for partial messages.
6
- export function messagePushable<T extends Message<T>>(): Pushable<
7
- PartialMessage<T>
8
- > {
9
- return pushable<PartialMessage<T>>({ objectMode: true })
5
+ // messagePushable is a shortcut to build a pushable for messages.
6
+ export function messagePushable<T extends Message<T>>(): Pushable<T> {
7
+ return pushable<T>({ objectMode: true })
10
8
  }
11
9
 
12
10
  // writeToPushable writes the incoming server data to the pushable.
@@ -1,151 +1,54 @@
1
- // @generated by protoc-gen-es v1.9.0 with parameter "target=ts,ts_nocheck=false"
1
+ // @generated by protoc-gen-es-lite unknown with parameter "target=ts,ts_nocheck=false"
2
2
  // @generated from file github.com/aperturerobotics/starpc/srpc/rpcproto.proto (package srpc, syntax proto3)
3
3
  /* eslint-disable */
4
4
 
5
- import type {
6
- BinaryReadOptions,
7
- FieldList,
8
- JsonReadOptions,
9
- JsonValue,
10
- PartialMessage,
11
- PlainMessage,
12
- } from '@bufbuild/protobuf'
13
- import { Message, proto3 } from '@bufbuild/protobuf'
5
+ import {
6
+ createMessageType,
7
+ Message,
8
+ MessageType,
9
+ PartialFieldInfo,
10
+ } from '@aptre/protobuf-es-lite'
14
11
 
15
- /**
16
- * Packet is a message sent over a srpc packet connection.
17
- *
18
- * @generated from message srpc.Packet
19
- */
20
- export class Packet extends Message<Packet> {
21
- /**
22
- * Body is the packet body.
23
- *
24
- * @generated from oneof srpc.Packet.body
25
- */
26
- body:
27
- | {
28
- /**
29
- * CallStart initiates a new call.
30
- *
31
- * @generated from field: srpc.CallStart call_start = 1;
32
- */
33
- value: CallStart
34
- case: 'callStart'
35
- }
36
- | {
37
- /**
38
- * CallData is a message in a streaming RPC sequence.
39
- *
40
- * @generated from field: srpc.CallData call_data = 2;
41
- */
42
- value: CallData
43
- case: 'callData'
44
- }
45
- | {
46
- /**
47
- * CallCancel cancels the call.
48
- *
49
- * @generated from field: bool call_cancel = 3;
50
- */
51
- value: boolean
52
- case: 'callCancel'
53
- }
54
- | { case: undefined; value?: undefined } = { case: undefined }
55
-
56
- constructor(data?: PartialMessage<Packet>) {
57
- super()
58
- proto3.util.initPartial(data, this)
59
- }
60
-
61
- static readonly runtime: typeof proto3 = proto3
62
- static readonly typeName = 'srpc.Packet'
63
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
64
- { no: 1, name: 'call_start', kind: 'message', T: CallStart, oneof: 'body' },
65
- { no: 2, name: 'call_data', kind: 'message', T: CallData, oneof: 'body' },
66
- {
67
- no: 3,
68
- name: 'call_cancel',
69
- kind: 'scalar',
70
- T: 8 /* ScalarType.BOOL */,
71
- oneof: 'body',
72
- },
73
- ])
74
-
75
- static fromBinary(
76
- bytes: Uint8Array,
77
- options?: Partial<BinaryReadOptions>,
78
- ): Packet {
79
- return new Packet().fromBinary(bytes, options)
80
- }
81
-
82
- static fromJson(
83
- jsonValue: JsonValue,
84
- options?: Partial<JsonReadOptions>,
85
- ): Packet {
86
- return new Packet().fromJson(jsonValue, options)
87
- }
88
-
89
- static fromJsonString(
90
- jsonString: string,
91
- options?: Partial<JsonReadOptions>,
92
- ): Packet {
93
- return new Packet().fromJsonString(jsonString, options)
94
- }
95
-
96
- static equals(
97
- a: Packet | PlainMessage<Packet> | undefined,
98
- b: Packet | PlainMessage<Packet> | undefined,
99
- ): boolean {
100
- return proto3.util.equals(Packet, a, b)
101
- }
102
- }
12
+ export const protobufPackage = 'srpc'
103
13
 
104
14
  /**
105
15
  * CallStart requests starting a new RPC call.
106
16
  *
107
17
  * @generated from message srpc.CallStart
108
18
  */
109
- export class CallStart extends Message<CallStart> {
19
+ export interface CallStart extends Message<CallStart> {
110
20
  /**
111
21
  * RpcService is the service to contact.
112
22
  * Must be set.
113
23
  *
114
24
  * @generated from field: string rpc_service = 1;
115
25
  */
116
- rpcService = ''
117
-
26
+ rpcService?: string
118
27
  /**
119
28
  * RpcMethod is the RPC method to call.
120
29
  * Must be set.
121
30
  *
122
31
  * @generated from field: string rpc_method = 2;
123
32
  */
124
- rpcMethod = ''
125
-
33
+ rpcMethod?: string
126
34
  /**
127
35
  * Data contains the request or the first message in the stream.
128
36
  * Optional if streaming.
129
37
  *
130
38
  * @generated from field: bytes data = 3;
131
39
  */
132
- data = new Uint8Array(0)
133
-
40
+ data?: Uint8Array
134
41
  /**
135
42
  * DataIsZero indicates Data is set with an empty message.
136
43
  *
137
44
  * @generated from field: bool data_is_zero = 4;
138
45
  */
139
- dataIsZero = false
140
-
141
- constructor(data?: PartialMessage<CallStart>) {
142
- super()
143
- proto3.util.initPartial(data, this)
144
- }
46
+ dataIsZero?: boolean
47
+ }
145
48
 
146
- static readonly runtime: typeof proto3 = proto3
147
- static readonly typeName = 'srpc.CallStart'
148
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
49
+ export const CallStart: MessageType<CallStart> = createMessageType({
50
+ typeName: 'srpc.CallStart',
51
+ fields: [
149
52
  {
150
53
  no: 1,
151
54
  name: 'rpc_service',
@@ -155,111 +58,111 @@ export class CallStart extends Message<CallStart> {
155
58
  { no: 2, name: 'rpc_method', kind: 'scalar', T: 9 /* ScalarType.STRING */ },
156
59
  { no: 3, name: 'data', kind: 'scalar', T: 12 /* ScalarType.BYTES */ },
157
60
  { no: 4, name: 'data_is_zero', kind: 'scalar', T: 8 /* ScalarType.BOOL */ },
158
- ])
159
-
160
- static fromBinary(
161
- bytes: Uint8Array,
162
- options?: Partial<BinaryReadOptions>,
163
- ): CallStart {
164
- return new CallStart().fromBinary(bytes, options)
165
- }
166
-
167
- static fromJson(
168
- jsonValue: JsonValue,
169
- options?: Partial<JsonReadOptions>,
170
- ): CallStart {
171
- return new CallStart().fromJson(jsonValue, options)
172
- }
173
-
174
- static fromJsonString(
175
- jsonString: string,
176
- options?: Partial<JsonReadOptions>,
177
- ): CallStart {
178
- return new CallStart().fromJsonString(jsonString, options)
179
- }
180
-
181
- static equals(
182
- a: CallStart | PlainMessage<CallStart> | undefined,
183
- b: CallStart | PlainMessage<CallStart> | undefined,
184
- ): boolean {
185
- return proto3.util.equals(CallStart, a, b)
186
- }
187
- }
61
+ ] as readonly PartialFieldInfo[],
62
+ packedByDefault: true,
63
+ })
188
64
 
189
65
  /**
190
66
  * CallData contains a message in a streaming RPC sequence.
191
67
  *
192
68
  * @generated from message srpc.CallData
193
69
  */
194
- export class CallData extends Message<CallData> {
70
+ export interface CallData extends Message<CallData> {
195
71
  /**
196
72
  * Data contains the packet in the sequence.
197
73
  *
198
74
  * @generated from field: bytes data = 1;
199
75
  */
200
- data = new Uint8Array(0)
201
-
76
+ data?: Uint8Array
202
77
  /**
203
78
  * DataIsZero indicates Data is set with an empty message.
204
79
  *
205
80
  * @generated from field: bool data_is_zero = 2;
206
81
  */
207
- dataIsZero = false
208
-
82
+ dataIsZero?: boolean
209
83
  /**
210
84
  * Complete indicates the RPC call is completed.
211
85
  *
212
86
  * @generated from field: bool complete = 3;
213
87
  */
214
- complete = false
215
-
88
+ complete?: boolean
216
89
  /**
217
90
  * Error contains any error that caused the RPC to fail.
218
91
  * If set, implies complete=true.
219
92
  *
220
93
  * @generated from field: string error = 4;
221
94
  */
222
- error = ''
223
-
224
- constructor(data?: PartialMessage<CallData>) {
225
- super()
226
- proto3.util.initPartial(data, this)
227
- }
95
+ error?: string
96
+ }
228
97
 
229
- static readonly runtime: typeof proto3 = proto3
230
- static readonly typeName = 'srpc.CallData'
231
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
98
+ export const CallData: MessageType<CallData> = createMessageType({
99
+ typeName: 'srpc.CallData',
100
+ fields: [
232
101
  { no: 1, name: 'data', kind: 'scalar', T: 12 /* ScalarType.BYTES */ },
233
102
  { no: 2, name: 'data_is_zero', kind: 'scalar', T: 8 /* ScalarType.BOOL */ },
234
103
  { no: 3, name: 'complete', kind: 'scalar', T: 8 /* ScalarType.BOOL */ },
235
104
  { no: 4, name: 'error', kind: 'scalar', T: 9 /* ScalarType.STRING */ },
236
- ])
105
+ ] as readonly PartialFieldInfo[],
106
+ packedByDefault: true,
107
+ })
237
108
 
238
- static fromBinary(
239
- bytes: Uint8Array,
240
- options?: Partial<BinaryReadOptions>,
241
- ): CallData {
242
- return new CallData().fromBinary(bytes, options)
243
- }
244
-
245
- static fromJson(
246
- jsonValue: JsonValue,
247
- options?: Partial<JsonReadOptions>,
248
- ): CallData {
249
- return new CallData().fromJson(jsonValue, options)
250
- }
251
-
252
- static fromJsonString(
253
- jsonString: string,
254
- options?: Partial<JsonReadOptions>,
255
- ): CallData {
256
- return new CallData().fromJsonString(jsonString, options)
257
- }
258
-
259
- static equals(
260
- a: CallData | PlainMessage<CallData> | undefined,
261
- b: CallData | PlainMessage<CallData> | undefined,
262
- ): boolean {
263
- return proto3.util.equals(CallData, a, b)
264
- }
109
+ /**
110
+ * Packet is a message sent over a srpc packet connection.
111
+ *
112
+ * @generated from message srpc.Packet
113
+ */
114
+ export interface Packet extends Message<Packet> {
115
+ /**
116
+ * Body is the packet body.
117
+ *
118
+ * @generated from oneof srpc.Packet.body
119
+ */
120
+ body?:
121
+ | {
122
+ value?: undefined
123
+ case: undefined
124
+ }
125
+ | {
126
+ /**
127
+ * CallStart initiates a new call.
128
+ *
129
+ * @generated from field: srpc.CallStart call_start = 1;
130
+ */
131
+ value: CallStart
132
+ case: 'callStart'
133
+ }
134
+ | {
135
+ /**
136
+ * CallData is a message in a streaming RPC sequence.
137
+ *
138
+ * @generated from field: srpc.CallData call_data = 2;
139
+ */
140
+ value: CallData
141
+ case: 'callData'
142
+ }
143
+ | {
144
+ /**
145
+ * CallCancel cancels the call.
146
+ *
147
+ * @generated from field: bool call_cancel = 3;
148
+ */
149
+ value: boolean
150
+ case: 'callCancel'
151
+ }
265
152
  }
153
+
154
+ export const Packet: MessageType<Packet> = createMessageType({
155
+ typeName: 'srpc.Packet',
156
+ fields: [
157
+ { no: 1, name: 'call_start', kind: 'message', T: CallStart, oneof: 'body' },
158
+ { no: 2, name: 'call_data', kind: 'message', T: CallData, oneof: 'body' },
159
+ {
160
+ no: 3,
161
+ name: 'call_cancel',
162
+ kind: 'scalar',
163
+ T: 8 /* ScalarType.BOOL */,
164
+ oneof: 'body',
165
+ },
166
+ ] as readonly PartialFieldInfo[],
167
+ packedByDefault: true,
168
+ })