starpc 0.0.1 → 0.1.2

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 (73) hide show
  1. package/LICENSE +19 -0
  2. package/Makefile +140 -0
  3. package/README.md +128 -26
  4. package/dist/echo/echo.d.ts +59 -0
  5. package/dist/echo/echo.js +85 -0
  6. package/dist/srpc/broadcast-channel.d.ts +16 -0
  7. package/dist/srpc/broadcast-channel.js +60 -0
  8. package/dist/srpc/client-rpc.d.ts +31 -0
  9. package/dist/srpc/client-rpc.js +176 -0
  10. package/dist/srpc/client.d.ts +12 -0
  11. package/dist/srpc/client.js +129 -0
  12. package/dist/srpc/conn.d.ts +14 -0
  13. package/dist/srpc/conn.js +38 -0
  14. package/dist/srpc/index.d.ts +3 -0
  15. package/dist/srpc/index.js +2 -0
  16. package/dist/srpc/packet.d.ts +9 -0
  17. package/dist/srpc/packet.js +89 -0
  18. package/dist/srpc/rpcproto.d.ts +194 -0
  19. package/dist/srpc/rpcproto.js +322 -0
  20. package/dist/srpc/stream.d.ts +5 -0
  21. package/dist/srpc/stream.js +1 -0
  22. package/dist/srpc/ts-proto-rpc.d.ts +7 -0
  23. package/dist/srpc/ts-proto-rpc.js +1 -0
  24. package/dist/srpc/websocket.d.ts +7 -0
  25. package/dist/srpc/websocket.js +18 -0
  26. package/e2e/e2e.go +1 -0
  27. package/e2e/e2e_test.go +158 -0
  28. package/echo/echo.go +1 -0
  29. package/echo/echo.pb.go +165 -0
  30. package/echo/echo.proto +19 -0
  31. package/echo/echo.ts +191 -0
  32. package/echo/echo_srpc.pb.go +333 -0
  33. package/echo/echo_vtproto.pb.go +271 -0
  34. package/echo/server.go +73 -0
  35. package/go.mod +50 -0
  36. package/go.sum +210 -0
  37. package/integration/integration.bash +25 -0
  38. package/integration/integration.go +30 -0
  39. package/integration/integration.ts +54 -0
  40. package/integration/tsconfig.json +11 -0
  41. package/package.json +77 -9
  42. package/patches/@libp2p+mplex+1.2.1.patch +22 -0
  43. package/srpc/broadcast-channel.ts +72 -0
  44. package/srpc/client-rpc.go +163 -0
  45. package/srpc/client-rpc.ts +197 -0
  46. package/srpc/client.go +96 -0
  47. package/srpc/client.ts +182 -0
  48. package/srpc/conn.go +7 -0
  49. package/srpc/conn.ts +49 -0
  50. package/srpc/errors.go +20 -0
  51. package/srpc/handler.go +13 -0
  52. package/srpc/index.ts +3 -0
  53. package/srpc/message.go +7 -0
  54. package/srpc/mux.go +76 -0
  55. package/srpc/packet-rw.go +102 -0
  56. package/srpc/packet.go +71 -0
  57. package/srpc/packet.ts +105 -0
  58. package/srpc/rpc-stream.go +76 -0
  59. package/srpc/rpcproto.pb.go +455 -0
  60. package/srpc/rpcproto.proto +46 -0
  61. package/srpc/rpcproto.ts +467 -0
  62. package/srpc/rpcproto_vtproto.pb.go +1094 -0
  63. package/srpc/server-http.go +66 -0
  64. package/srpc/server-pipe.go +26 -0
  65. package/srpc/server-rpc.go +160 -0
  66. package/srpc/server.go +29 -0
  67. package/srpc/stream-pipe.go +86 -0
  68. package/srpc/stream.go +24 -0
  69. package/srpc/stream.ts +11 -0
  70. package/srpc/ts-proto-rpc.ts +29 -0
  71. package/srpc/websocket.go +68 -0
  72. package/srpc/websocket.ts +22 -0
  73. package/srpc/writer.go +9 -0
@@ -0,0 +1,467 @@
1
+ /* eslint-disable */
2
+ import Long from 'long'
3
+ import * as _m0 from 'protobufjs/minimal'
4
+
5
+ export const protobufPackage = 'srpc'
6
+
7
+ /** Packet is a message sent over a srpc packet connection. */
8
+ export interface Packet {
9
+ body?:
10
+ | { $case: 'callStart'; callStart: CallStart }
11
+ | { $case: 'callStartResp'; callStartResp: CallStartResp }
12
+ | { $case: 'callData'; callData: CallData }
13
+ }
14
+
15
+ /** CallStart requests starting a new RPC call. */
16
+ export interface CallStart {
17
+ /**
18
+ * RpcService is the service to contact.
19
+ * Must be set.
20
+ */
21
+ rpcService: string
22
+ /**
23
+ * RpcMethod is the RPC method to call.
24
+ * Must be set.
25
+ */
26
+ rpcMethod: string
27
+ /**
28
+ * Data contains the request or the first message in the stream.
29
+ * Optional if streaming.
30
+ */
31
+ data: Uint8Array
32
+ }
33
+
34
+ /** CallStartResp is the response to CallStart. */
35
+ export interface CallStartResp {
36
+ /**
37
+ * Error contains any error starting the RPC call.
38
+ * Empty if successful.
39
+ */
40
+ error: string
41
+ }
42
+
43
+ /** CallData contains a message in a streaming RPC sequence. */
44
+ export interface CallData {
45
+ /** Data contains the packet in the sequence. */
46
+ data: Uint8Array
47
+ /** Complete indicates the RPC call is completed. */
48
+ complete: boolean
49
+ /**
50
+ * Error contains any error that caused the RPC to fail.
51
+ * If set, implies complete=true.
52
+ */
53
+ error: string
54
+ }
55
+
56
+ function createBasePacket(): Packet {
57
+ return { body: undefined }
58
+ }
59
+
60
+ export const Packet = {
61
+ encode(
62
+ message: Packet,
63
+ writer: _m0.Writer = _m0.Writer.create()
64
+ ): _m0.Writer {
65
+ if (message.body?.$case === 'callStart') {
66
+ CallStart.encode(
67
+ message.body.callStart,
68
+ writer.uint32(10).fork()
69
+ ).ldelim()
70
+ }
71
+ if (message.body?.$case === 'callStartResp') {
72
+ CallStartResp.encode(
73
+ message.body.callStartResp,
74
+ writer.uint32(18).fork()
75
+ ).ldelim()
76
+ }
77
+ if (message.body?.$case === 'callData') {
78
+ CallData.encode(message.body.callData, writer.uint32(26).fork()).ldelim()
79
+ }
80
+ return writer
81
+ },
82
+
83
+ decode(input: _m0.Reader | Uint8Array, length?: number): Packet {
84
+ const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input)
85
+ let end = length === undefined ? reader.len : reader.pos + length
86
+ const message = createBasePacket()
87
+ while (reader.pos < end) {
88
+ const tag = reader.uint32()
89
+ switch (tag >>> 3) {
90
+ case 1:
91
+ message.body = {
92
+ $case: 'callStart',
93
+ callStart: CallStart.decode(reader, reader.uint32()),
94
+ }
95
+ break
96
+ case 2:
97
+ message.body = {
98
+ $case: 'callStartResp',
99
+ callStartResp: CallStartResp.decode(reader, reader.uint32()),
100
+ }
101
+ break
102
+ case 3:
103
+ message.body = {
104
+ $case: 'callData',
105
+ callData: CallData.decode(reader, reader.uint32()),
106
+ }
107
+ break
108
+ default:
109
+ reader.skipType(tag & 7)
110
+ break
111
+ }
112
+ }
113
+ return message
114
+ },
115
+
116
+ fromJSON(object: any): Packet {
117
+ return {
118
+ body: isSet(object.callStart)
119
+ ? {
120
+ $case: 'callStart',
121
+ callStart: CallStart.fromJSON(object.callStart),
122
+ }
123
+ : isSet(object.callStartResp)
124
+ ? {
125
+ $case: 'callStartResp',
126
+ callStartResp: CallStartResp.fromJSON(object.callStartResp),
127
+ }
128
+ : isSet(object.callData)
129
+ ? { $case: 'callData', callData: CallData.fromJSON(object.callData) }
130
+ : undefined,
131
+ }
132
+ },
133
+
134
+ toJSON(message: Packet): unknown {
135
+ const obj: any = {}
136
+ message.body?.$case === 'callStart' &&
137
+ (obj.callStart = message.body?.callStart
138
+ ? CallStart.toJSON(message.body?.callStart)
139
+ : undefined)
140
+ message.body?.$case === 'callStartResp' &&
141
+ (obj.callStartResp = message.body?.callStartResp
142
+ ? CallStartResp.toJSON(message.body?.callStartResp)
143
+ : undefined)
144
+ message.body?.$case === 'callData' &&
145
+ (obj.callData = message.body?.callData
146
+ ? CallData.toJSON(message.body?.callData)
147
+ : undefined)
148
+ return obj
149
+ },
150
+
151
+ fromPartial<I extends Exact<DeepPartial<Packet>, I>>(object: I): Packet {
152
+ const message = createBasePacket()
153
+ if (
154
+ object.body?.$case === 'callStart' &&
155
+ object.body?.callStart !== undefined &&
156
+ object.body?.callStart !== null
157
+ ) {
158
+ message.body = {
159
+ $case: 'callStart',
160
+ callStart: CallStart.fromPartial(object.body.callStart),
161
+ }
162
+ }
163
+ if (
164
+ object.body?.$case === 'callStartResp' &&
165
+ object.body?.callStartResp !== undefined &&
166
+ object.body?.callStartResp !== null
167
+ ) {
168
+ message.body = {
169
+ $case: 'callStartResp',
170
+ callStartResp: CallStartResp.fromPartial(object.body.callStartResp),
171
+ }
172
+ }
173
+ if (
174
+ object.body?.$case === 'callData' &&
175
+ object.body?.callData !== undefined &&
176
+ object.body?.callData !== null
177
+ ) {
178
+ message.body = {
179
+ $case: 'callData',
180
+ callData: CallData.fromPartial(object.body.callData),
181
+ }
182
+ }
183
+ return message
184
+ },
185
+ }
186
+
187
+ function createBaseCallStart(): CallStart {
188
+ return { rpcService: '', rpcMethod: '', data: new Uint8Array() }
189
+ }
190
+
191
+ export const CallStart = {
192
+ encode(
193
+ message: CallStart,
194
+ writer: _m0.Writer = _m0.Writer.create()
195
+ ): _m0.Writer {
196
+ if (message.rpcService !== '') {
197
+ writer.uint32(10).string(message.rpcService)
198
+ }
199
+ if (message.rpcMethod !== '') {
200
+ writer.uint32(18).string(message.rpcMethod)
201
+ }
202
+ if (message.data.length !== 0) {
203
+ writer.uint32(26).bytes(message.data)
204
+ }
205
+ return writer
206
+ },
207
+
208
+ decode(input: _m0.Reader | Uint8Array, length?: number): CallStart {
209
+ const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input)
210
+ let end = length === undefined ? reader.len : reader.pos + length
211
+ const message = createBaseCallStart()
212
+ while (reader.pos < end) {
213
+ const tag = reader.uint32()
214
+ switch (tag >>> 3) {
215
+ case 1:
216
+ message.rpcService = reader.string()
217
+ break
218
+ case 2:
219
+ message.rpcMethod = reader.string()
220
+ break
221
+ case 3:
222
+ message.data = reader.bytes()
223
+ break
224
+ default:
225
+ reader.skipType(tag & 7)
226
+ break
227
+ }
228
+ }
229
+ return message
230
+ },
231
+
232
+ fromJSON(object: any): CallStart {
233
+ return {
234
+ rpcService: isSet(object.rpcService) ? String(object.rpcService) : '',
235
+ rpcMethod: isSet(object.rpcMethod) ? String(object.rpcMethod) : '',
236
+ data: isSet(object.data)
237
+ ? bytesFromBase64(object.data)
238
+ : new Uint8Array(),
239
+ }
240
+ },
241
+
242
+ toJSON(message: CallStart): unknown {
243
+ const obj: any = {}
244
+ message.rpcService !== undefined && (obj.rpcService = message.rpcService)
245
+ message.rpcMethod !== undefined && (obj.rpcMethod = message.rpcMethod)
246
+ message.data !== undefined &&
247
+ (obj.data = base64FromBytes(
248
+ message.data !== undefined ? message.data : new Uint8Array()
249
+ ))
250
+ return obj
251
+ },
252
+
253
+ fromPartial<I extends Exact<DeepPartial<CallStart>, I>>(
254
+ object: I
255
+ ): CallStart {
256
+ const message = createBaseCallStart()
257
+ message.rpcService = object.rpcService ?? ''
258
+ message.rpcMethod = object.rpcMethod ?? ''
259
+ message.data = object.data ?? new Uint8Array()
260
+ return message
261
+ },
262
+ }
263
+
264
+ function createBaseCallStartResp(): CallStartResp {
265
+ return { error: '' }
266
+ }
267
+
268
+ export const CallStartResp = {
269
+ encode(
270
+ message: CallStartResp,
271
+ writer: _m0.Writer = _m0.Writer.create()
272
+ ): _m0.Writer {
273
+ if (message.error !== '') {
274
+ writer.uint32(10).string(message.error)
275
+ }
276
+ return writer
277
+ },
278
+
279
+ decode(input: _m0.Reader | Uint8Array, length?: number): CallStartResp {
280
+ const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input)
281
+ let end = length === undefined ? reader.len : reader.pos + length
282
+ const message = createBaseCallStartResp()
283
+ while (reader.pos < end) {
284
+ const tag = reader.uint32()
285
+ switch (tag >>> 3) {
286
+ case 1:
287
+ message.error = reader.string()
288
+ break
289
+ default:
290
+ reader.skipType(tag & 7)
291
+ break
292
+ }
293
+ }
294
+ return message
295
+ },
296
+
297
+ fromJSON(object: any): CallStartResp {
298
+ return {
299
+ error: isSet(object.error) ? String(object.error) : '',
300
+ }
301
+ },
302
+
303
+ toJSON(message: CallStartResp): unknown {
304
+ const obj: any = {}
305
+ message.error !== undefined && (obj.error = message.error)
306
+ return obj
307
+ },
308
+
309
+ fromPartial<I extends Exact<DeepPartial<CallStartResp>, I>>(
310
+ object: I
311
+ ): CallStartResp {
312
+ const message = createBaseCallStartResp()
313
+ message.error = object.error ?? ''
314
+ return message
315
+ },
316
+ }
317
+
318
+ function createBaseCallData(): CallData {
319
+ return { data: new Uint8Array(), complete: false, error: '' }
320
+ }
321
+
322
+ export const CallData = {
323
+ encode(
324
+ message: CallData,
325
+ writer: _m0.Writer = _m0.Writer.create()
326
+ ): _m0.Writer {
327
+ if (message.data.length !== 0) {
328
+ writer.uint32(10).bytes(message.data)
329
+ }
330
+ if (message.complete === true) {
331
+ writer.uint32(16).bool(message.complete)
332
+ }
333
+ if (message.error !== '') {
334
+ writer.uint32(26).string(message.error)
335
+ }
336
+ return writer
337
+ },
338
+
339
+ decode(input: _m0.Reader | Uint8Array, length?: number): CallData {
340
+ const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input)
341
+ let end = length === undefined ? reader.len : reader.pos + length
342
+ const message = createBaseCallData()
343
+ while (reader.pos < end) {
344
+ const tag = reader.uint32()
345
+ switch (tag >>> 3) {
346
+ case 1:
347
+ message.data = reader.bytes()
348
+ break
349
+ case 2:
350
+ message.complete = reader.bool()
351
+ break
352
+ case 3:
353
+ message.error = reader.string()
354
+ break
355
+ default:
356
+ reader.skipType(tag & 7)
357
+ break
358
+ }
359
+ }
360
+ return message
361
+ },
362
+
363
+ fromJSON(object: any): CallData {
364
+ return {
365
+ data: isSet(object.data)
366
+ ? bytesFromBase64(object.data)
367
+ : new Uint8Array(),
368
+ complete: isSet(object.complete) ? Boolean(object.complete) : false,
369
+ error: isSet(object.error) ? String(object.error) : '',
370
+ }
371
+ },
372
+
373
+ toJSON(message: CallData): unknown {
374
+ const obj: any = {}
375
+ message.data !== undefined &&
376
+ (obj.data = base64FromBytes(
377
+ message.data !== undefined ? message.data : new Uint8Array()
378
+ ))
379
+ message.complete !== undefined && (obj.complete = message.complete)
380
+ message.error !== undefined && (obj.error = message.error)
381
+ return obj
382
+ },
383
+
384
+ fromPartial<I extends Exact<DeepPartial<CallData>, I>>(object: I): CallData {
385
+ const message = createBaseCallData()
386
+ message.data = object.data ?? new Uint8Array()
387
+ message.complete = object.complete ?? false
388
+ message.error = object.error ?? ''
389
+ return message
390
+ },
391
+ }
392
+
393
+ declare var self: any | undefined
394
+ declare var window: any | undefined
395
+ declare var global: any | undefined
396
+ var globalThis: any = (() => {
397
+ if (typeof globalThis !== 'undefined') return globalThis
398
+ if (typeof self !== 'undefined') return self
399
+ if (typeof window !== 'undefined') return window
400
+ if (typeof global !== 'undefined') return global
401
+ throw 'Unable to locate global object'
402
+ })()
403
+
404
+ const atob: (b64: string) => string =
405
+ globalThis.atob ||
406
+ ((b64) => globalThis.Buffer.from(b64, 'base64').toString('binary'))
407
+ function bytesFromBase64(b64: string): Uint8Array {
408
+ const bin = atob(b64)
409
+ const arr = new Uint8Array(bin.length)
410
+ for (let i = 0; i < bin.length; ++i) {
411
+ arr[i] = bin.charCodeAt(i)
412
+ }
413
+ return arr
414
+ }
415
+
416
+ const btoa: (bin: string) => string =
417
+ globalThis.btoa ||
418
+ ((bin) => globalThis.Buffer.from(bin, 'binary').toString('base64'))
419
+ function base64FromBytes(arr: Uint8Array): string {
420
+ const bin: string[] = []
421
+ arr.forEach((byte) => {
422
+ bin.push(String.fromCharCode(byte))
423
+ })
424
+ return btoa(bin.join(''))
425
+ }
426
+
427
+ type Builtin =
428
+ | Date
429
+ | Function
430
+ | Uint8Array
431
+ | string
432
+ | number
433
+ | boolean
434
+ | undefined
435
+
436
+ export type DeepPartial<T> = T extends Builtin
437
+ ? T
438
+ : T extends Long
439
+ ? string | number | Long
440
+ : T extends Array<infer U>
441
+ ? Array<DeepPartial<U>>
442
+ : T extends ReadonlyArray<infer U>
443
+ ? ReadonlyArray<DeepPartial<U>>
444
+ : T extends { $case: string }
445
+ ? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & {
446
+ $case: T['$case']
447
+ }
448
+ : T extends {}
449
+ ? { [K in keyof T]?: DeepPartial<T[K]> }
450
+ : Partial<T>
451
+
452
+ type KeysOfUnion<T> = T extends T ? keyof T : never
453
+ export type Exact<P, I extends P> = P extends Builtin
454
+ ? P
455
+ : P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<
456
+ Exclude<keyof I, KeysOfUnion<P>>,
457
+ never
458
+ >
459
+
460
+ if (_m0.util.Long !== Long) {
461
+ _m0.util.Long = Long as any
462
+ _m0.configure()
463
+ }
464
+
465
+ function isSet(value: any): boolean {
466
+ return value !== null && value !== undefined
467
+ }