starpc 0.11.0 → 0.11.3

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.
@@ -1,6 +1,6 @@
1
- import Long from 'long';
2
- import _m0 from 'protobufjs/minimal.js';
3
- import { RpcStreamPacket } from '../rpcstream/rpcstream.pb.js';
1
+ import Long from "long";
2
+ import _m0 from "protobufjs/minimal.js";
3
+ import { RpcStreamPacket } from "../rpcstream/rpcstream.pb.js";
4
4
  export declare const protobufPackage = "echo";
5
5
  /** EchoMsg is the message body for Echo. */
6
6
  export interface EchoMsg {
@@ -34,7 +34,8 @@ export interface Echoer {
34
34
  }
35
35
  export declare class EchoerClientImpl implements Echoer {
36
36
  private readonly rpc;
37
- constructor(rpc: Rpc);
37
+ private readonly service;
38
+ constructor(rpc: Rpc, service?: string);
38
39
  Echo(request: EchoMsg): Promise<EchoMsg>;
39
40
  EchoServerStream(request: EchoMsg): AsyncIterable<EchoMsg>;
40
41
  EchoClientStream(request: AsyncIterable<EchoMsg>): Promise<EchoMsg>;
@@ -322,9 +323,9 @@ declare type Builtin = Date | Function | Uint8Array | string | number | boolean
322
323
  export declare type DeepPartial<T> = T extends Builtin ? T : T extends Long ? string | number | Long : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
323
324
  $case: string;
324
325
  } ? {
325
- [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]>;
326
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
326
327
  } & {
327
- $case: T['$case'];
328
+ $case: T["$case"];
328
329
  } : T extends {} ? {
329
330
  [K in keyof T]?: DeepPartial<T[K]>;
330
331
  } : Partial<T>;
@@ -1,14 +1,14 @@
1
1
  /* eslint-disable */
2
- import Long from 'long';
3
- import _m0 from 'protobufjs/minimal.js';
4
- import { RpcStreamPacket } from '../rpcstream/rpcstream.pb.js';
5
- export const protobufPackage = 'echo';
2
+ import Long from "long";
3
+ import _m0 from "protobufjs/minimal.js";
4
+ import { RpcStreamPacket } from "../rpcstream/rpcstream.pb.js";
5
+ export const protobufPackage = "echo";
6
6
  function createBaseEchoMsg() {
7
- return { body: '' };
7
+ return { body: "" };
8
8
  }
9
9
  export const EchoMsg = {
10
10
  encode(message, writer = _m0.Writer.create()) {
11
- if (message.body !== '') {
11
+ if (message.body !== "") {
12
12
  writer.uint32(10).string(message.body);
13
13
  }
14
14
  return writer;
@@ -59,7 +59,7 @@ export const EchoMsg = {
59
59
  }
60
60
  },
61
61
  fromJSON(object) {
62
- return { body: isSet(object.body) ? String(object.body) : '' };
62
+ return { body: isSet(object.body) ? String(object.body) : "" };
63
63
  },
64
64
  toJSON(message) {
65
65
  const obj = {};
@@ -68,12 +68,13 @@ export const EchoMsg = {
68
68
  },
69
69
  fromPartial(object) {
70
70
  const message = createBaseEchoMsg();
71
- message.body = object.body ?? '';
71
+ message.body = object.body ?? "";
72
72
  return message;
73
73
  },
74
74
  };
75
75
  export class EchoerClientImpl {
76
- constructor(rpc) {
76
+ constructor(rpc, service) {
77
+ this.service = service || "echo.Echoer";
77
78
  this.rpc = rpc;
78
79
  this.Echo = this.Echo.bind(this);
79
80
  this.EchoServerStream = this.EchoServerStream.bind(this);
@@ -83,37 +84,37 @@ export class EchoerClientImpl {
83
84
  }
84
85
  Echo(request) {
85
86
  const data = EchoMsg.encode(request).finish();
86
- const promise = this.rpc.request('echo.Echoer', 'Echo', data);
87
+ const promise = this.rpc.request(this.service, "Echo", data);
87
88
  return promise.then((data) => EchoMsg.decode(new _m0.Reader(data)));
88
89
  }
89
90
  EchoServerStream(request) {
90
91
  const data = EchoMsg.encode(request).finish();
91
- const result = this.rpc.serverStreamingRequest('echo.Echoer', 'EchoServerStream', data);
92
+ const result = this.rpc.serverStreamingRequest(this.service, "EchoServerStream", data);
92
93
  return EchoMsg.decodeTransform(result);
93
94
  }
94
95
  EchoClientStream(request) {
95
96
  const data = EchoMsg.encodeTransform(request);
96
- const promise = this.rpc.clientStreamingRequest('echo.Echoer', 'EchoClientStream', data);
97
+ const promise = this.rpc.clientStreamingRequest(this.service, "EchoClientStream", data);
97
98
  return promise.then((data) => EchoMsg.decode(new _m0.Reader(data)));
98
99
  }
99
100
  EchoBidiStream(request) {
100
101
  const data = EchoMsg.encodeTransform(request);
101
- const result = this.rpc.bidirectionalStreamingRequest('echo.Echoer', 'EchoBidiStream', data);
102
+ const result = this.rpc.bidirectionalStreamingRequest(this.service, "EchoBidiStream", data);
102
103
  return EchoMsg.decodeTransform(result);
103
104
  }
104
105
  RpcStream(request) {
105
106
  const data = RpcStreamPacket.encodeTransform(request);
106
- const result = this.rpc.bidirectionalStreamingRequest('echo.Echoer', 'RpcStream', data);
107
+ const result = this.rpc.bidirectionalStreamingRequest(this.service, "RpcStream", data);
107
108
  return RpcStreamPacket.decodeTransform(result);
108
109
  }
109
110
  }
110
111
  export const EchoerDefinition = {
111
- name: 'Echoer',
112
- fullName: 'echo.Echoer',
112
+ name: "Echoer",
113
+ fullName: "echo.Echoer",
113
114
  methods: {
114
115
  /** Echo returns the given message. */
115
116
  echo: {
116
- name: 'Echo',
117
+ name: "Echo",
117
118
  requestType: EchoMsg,
118
119
  requestStream: false,
119
120
  responseType: EchoMsg,
@@ -122,7 +123,7 @@ export const EchoerDefinition = {
122
123
  },
123
124
  /** EchoServerStream is an example of a server -> client one-way stream. */
124
125
  echoServerStream: {
125
- name: 'EchoServerStream',
126
+ name: "EchoServerStream",
126
127
  requestType: EchoMsg,
127
128
  requestStream: false,
128
129
  responseType: EchoMsg,
@@ -131,7 +132,7 @@ export const EchoerDefinition = {
131
132
  },
132
133
  /** EchoClientStream is an example of client->server one-way stream. */
133
134
  echoClientStream: {
134
- name: 'EchoClientStream',
135
+ name: "EchoClientStream",
135
136
  requestType: EchoMsg,
136
137
  requestStream: true,
137
138
  responseType: EchoMsg,
@@ -140,7 +141,7 @@ export const EchoerDefinition = {
140
141
  },
141
142
  /** EchoBidiStream is an example of a two-way stream. */
142
143
  echoBidiStream: {
143
- name: 'EchoBidiStream',
144
+ name: "EchoBidiStream",
144
145
  requestType: EchoMsg,
145
146
  requestStream: true,
146
147
  responseType: EchoMsg,
@@ -149,7 +150,7 @@ export const EchoerDefinition = {
149
150
  },
150
151
  /** RpcStream opens a nested rpc call stream. */
151
152
  rpcStream: {
152
- name: 'RpcStream',
153
+ name: "RpcStream",
153
154
  requestType: RpcStreamPacket,
154
155
  requestStream: true,
155
156
  responseType: RpcStreamPacket,
@@ -32,7 +32,7 @@ export class EchoerServer {
32
32
  return result;
33
33
  }
34
34
  RpcStream(request) {
35
- return handleRpcStream(request[Symbol.asyncIterator](), async (_componentId) => {
35
+ return handleRpcStream(request[Symbol.asyncIterator](), async () => {
36
36
  if (!this.proxyServer) {
37
37
  throw new Error('rpc stream proxy server not set');
38
38
  }
@@ -1,16 +1,16 @@
1
- import Long from 'long';
2
- import _m0 from 'protobufjs/minimal.js';
1
+ import Long from "long";
2
+ import _m0 from "protobufjs/minimal.js";
3
3
  export declare const protobufPackage = "rpcstream";
4
4
  /** RpcStreamPacket is a packet encapsulating data for a RPC stream. */
5
5
  export interface RpcStreamPacket {
6
6
  body?: {
7
- $case: 'init';
7
+ $case: "init";
8
8
  init: RpcStreamInit;
9
9
  } | {
10
- $case: 'ack';
10
+ $case: "ack";
11
11
  ack: RpcAck;
12
12
  } | {
13
- $case: 'data';
13
+ $case: "data";
14
14
  data: Uint8Array;
15
15
  };
16
16
  }
@@ -116,9 +116,9 @@ declare type Builtin = Date | Function | Uint8Array | string | number | boolean
116
116
  export declare type DeepPartial<T> = T extends Builtin ? T : T extends Long ? string | number | Long : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
117
117
  $case: string;
118
118
  } ? {
119
- [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]>;
119
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
120
120
  } & {
121
- $case: T['$case'];
121
+ $case: T["$case"];
122
122
  } : T extends {} ? {
123
123
  [K in keyof T]?: DeepPartial<T[K]>;
124
124
  } : Partial<T>;
@@ -1,19 +1,19 @@
1
1
  /* eslint-disable */
2
- import Long from 'long';
3
- import _m0 from 'protobufjs/minimal.js';
4
- export const protobufPackage = 'rpcstream';
2
+ import Long from "long";
3
+ import _m0 from "protobufjs/minimal.js";
4
+ export const protobufPackage = "rpcstream";
5
5
  function createBaseRpcStreamPacket() {
6
6
  return { body: undefined };
7
7
  }
8
8
  export const RpcStreamPacket = {
9
9
  encode(message, writer = _m0.Writer.create()) {
10
- if (message.body?.$case === 'init') {
10
+ if (message.body?.$case === "init") {
11
11
  RpcStreamInit.encode(message.body.init, writer.uint32(10).fork()).ldelim();
12
12
  }
13
- if (message.body?.$case === 'ack') {
13
+ if (message.body?.$case === "ack") {
14
14
  RpcAck.encode(message.body.ack, writer.uint32(18).fork()).ldelim();
15
15
  }
16
- if (message.body?.$case === 'data') {
16
+ if (message.body?.$case === "data") {
17
17
  writer.uint32(26).bytes(message.body.data);
18
18
  }
19
19
  return writer;
@@ -26,19 +26,13 @@ export const RpcStreamPacket = {
26
26
  const tag = reader.uint32();
27
27
  switch (tag >>> 3) {
28
28
  case 1:
29
- message.body = {
30
- $case: 'init',
31
- init: RpcStreamInit.decode(reader, reader.uint32()),
32
- };
29
+ message.body = { $case: "init", init: RpcStreamInit.decode(reader, reader.uint32()) };
33
30
  break;
34
31
  case 2:
35
- message.body = {
36
- $case: 'ack',
37
- ack: RpcAck.decode(reader, reader.uint32()),
38
- };
32
+ message.body = { $case: "ack", ack: RpcAck.decode(reader, reader.uint32()) };
39
33
  break;
40
34
  case 3:
41
- message.body = { $case: 'data', data: reader.bytes() };
35
+ message.body = { $case: "data", data: reader.bytes() };
42
36
  break;
43
37
  default:
44
38
  reader.skipType(tag & 7);
@@ -78,60 +72,43 @@ export const RpcStreamPacket = {
78
72
  fromJSON(object) {
79
73
  return {
80
74
  body: isSet(object.init)
81
- ? { $case: 'init', init: RpcStreamInit.fromJSON(object.init) }
75
+ ? { $case: "init", init: RpcStreamInit.fromJSON(object.init) }
82
76
  : isSet(object.ack)
83
- ? { $case: 'ack', ack: RpcAck.fromJSON(object.ack) }
77
+ ? { $case: "ack", ack: RpcAck.fromJSON(object.ack) }
84
78
  : isSet(object.data)
85
- ? { $case: 'data', data: bytesFromBase64(object.data) }
79
+ ? { $case: "data", data: bytesFromBase64(object.data) }
86
80
  : undefined,
87
81
  };
88
82
  },
89
83
  toJSON(message) {
90
84
  const obj = {};
91
- message.body?.$case === 'init' &&
92
- (obj.init = message.body?.init
93
- ? RpcStreamInit.toJSON(message.body?.init)
94
- : undefined);
95
- message.body?.$case === 'ack' &&
96
- (obj.ack = message.body?.ack
97
- ? RpcAck.toJSON(message.body?.ack)
98
- : undefined);
99
- message.body?.$case === 'data' &&
100
- (obj.data =
101
- message.body?.data !== undefined
102
- ? base64FromBytes(message.body?.data)
103
- : undefined);
85
+ message.body?.$case === "init" &&
86
+ (obj.init = message.body?.init ? RpcStreamInit.toJSON(message.body?.init) : undefined);
87
+ message.body?.$case === "ack" && (obj.ack = message.body?.ack ? RpcAck.toJSON(message.body?.ack) : undefined);
88
+ message.body?.$case === "data" &&
89
+ (obj.data = message.body?.data !== undefined ? base64FromBytes(message.body?.data) : undefined);
104
90
  return obj;
105
91
  },
106
92
  fromPartial(object) {
107
93
  const message = createBaseRpcStreamPacket();
108
- if (object.body?.$case === 'init' &&
109
- object.body?.init !== undefined &&
110
- object.body?.init !== null) {
111
- message.body = {
112
- $case: 'init',
113
- init: RpcStreamInit.fromPartial(object.body.init),
114
- };
94
+ if (object.body?.$case === "init" && object.body?.init !== undefined && object.body?.init !== null) {
95
+ message.body = { $case: "init", init: RpcStreamInit.fromPartial(object.body.init) };
115
96
  }
116
- if (object.body?.$case === 'ack' &&
117
- object.body?.ack !== undefined &&
118
- object.body?.ack !== null) {
119
- message.body = { $case: 'ack', ack: RpcAck.fromPartial(object.body.ack) };
97
+ if (object.body?.$case === "ack" && object.body?.ack !== undefined && object.body?.ack !== null) {
98
+ message.body = { $case: "ack", ack: RpcAck.fromPartial(object.body.ack) };
120
99
  }
121
- if (object.body?.$case === 'data' &&
122
- object.body?.data !== undefined &&
123
- object.body?.data !== null) {
124
- message.body = { $case: 'data', data: object.body.data };
100
+ if (object.body?.$case === "data" && object.body?.data !== undefined && object.body?.data !== null) {
101
+ message.body = { $case: "data", data: object.body.data };
125
102
  }
126
103
  return message;
127
104
  },
128
105
  };
129
106
  function createBaseRpcStreamInit() {
130
- return { componentId: '' };
107
+ return { componentId: "" };
131
108
  }
132
109
  export const RpcStreamInit = {
133
110
  encode(message, writer = _m0.Writer.create()) {
134
- if (message.componentId !== '') {
111
+ if (message.componentId !== "") {
135
112
  writer.uint32(10).string(message.componentId);
136
113
  }
137
114
  return writer;
@@ -182,9 +159,7 @@ export const RpcStreamInit = {
182
159
  }
183
160
  },
184
161
  fromJSON(object) {
185
- return {
186
- componentId: isSet(object.componentId) ? String(object.componentId) : '',
187
- };
162
+ return { componentId: isSet(object.componentId) ? String(object.componentId) : "" };
188
163
  },
189
164
  toJSON(message) {
190
165
  const obj = {};
@@ -193,16 +168,16 @@ export const RpcStreamInit = {
193
168
  },
194
169
  fromPartial(object) {
195
170
  const message = createBaseRpcStreamInit();
196
- message.componentId = object.componentId ?? '';
171
+ message.componentId = object.componentId ?? "";
197
172
  return message;
198
173
  },
199
174
  };
200
175
  function createBaseRpcAck() {
201
- return { error: '' };
176
+ return { error: "" };
202
177
  }
203
178
  export const RpcAck = {
204
179
  encode(message, writer = _m0.Writer.create()) {
205
- if (message.error !== '') {
180
+ if (message.error !== "") {
206
181
  writer.uint32(10).string(message.error);
207
182
  }
208
183
  return writer;
@@ -253,7 +228,7 @@ export const RpcAck = {
253
228
  }
254
229
  },
255
230
  fromJSON(object) {
256
- return { error: isSet(object.error) ? String(object.error) : '' };
231
+ return { error: isSet(object.error) ? String(object.error) : "" };
257
232
  },
258
233
  toJSON(message) {
259
234
  const obj = {};
@@ -262,28 +237,28 @@ export const RpcAck = {
262
237
  },
263
238
  fromPartial(object) {
264
239
  const message = createBaseRpcAck();
265
- message.error = object.error ?? '';
240
+ message.error = object.error ?? "";
266
241
  return message;
267
242
  },
268
243
  };
269
244
  var globalThis = (() => {
270
- if (typeof globalThis !== 'undefined') {
245
+ if (typeof globalThis !== "undefined") {
271
246
  return globalThis;
272
247
  }
273
- if (typeof self !== 'undefined') {
248
+ if (typeof self !== "undefined") {
274
249
  return self;
275
250
  }
276
- if (typeof window !== 'undefined') {
251
+ if (typeof window !== "undefined") {
277
252
  return window;
278
253
  }
279
- if (typeof global !== 'undefined') {
254
+ if (typeof global !== "undefined") {
280
255
  return global;
281
256
  }
282
- throw 'Unable to locate global object';
257
+ throw "Unable to locate global object";
283
258
  })();
284
259
  function bytesFromBase64(b64) {
285
260
  if (globalThis.Buffer) {
286
- return Uint8Array.from(globalThis.Buffer.from(b64, 'base64'));
261
+ return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
287
262
  }
288
263
  else {
289
264
  const bin = globalThis.atob(b64);
@@ -296,14 +271,14 @@ function bytesFromBase64(b64) {
296
271
  }
297
272
  function base64FromBytes(arr) {
298
273
  if (globalThis.Buffer) {
299
- return globalThis.Buffer.from(arr).toString('base64');
274
+ return globalThis.Buffer.from(arr).toString("base64");
300
275
  }
301
276
  else {
302
277
  const bin = [];
303
278
  arr.forEach((byte) => {
304
279
  bin.push(String.fromCharCode(byte));
305
280
  });
306
- return globalThis.btoa(bin.join(''));
281
+ return globalThis.btoa(bin.join(""));
307
282
  }
308
283
  }
309
284
  if (_m0.util.Long !== Long) {
package/dist/srpc/conn.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { pipe } from 'it-pipe';
2
2
  import { Mplex } from '@libp2p/mplex';
3
+ import isPromise from 'is-promise';
3
4
  import { Client } from './client.js';
4
5
  import { combineUint8ArrayListTransform } from './array-list.js';
5
6
  // streamToSRPCStream converts a Stream to a SRPCStream.
@@ -46,6 +47,9 @@ export class Conn {
46
47
  // openStream implements the client open stream function.
47
48
  async openStream() {
48
49
  const stream = this.muxer.newStream();
50
+ if (isPromise(stream)) {
51
+ return streamToSRPCStream(await stream);
52
+ }
49
53
  return streamToSRPCStream(stream);
50
54
  }
51
55
  // buildOpenStreamFunc returns openStream bound to this conn.
@@ -1,13 +1,13 @@
1
- import Long from 'long';
2
- import _m0 from 'protobufjs/minimal.js';
1
+ import Long from "long";
2
+ import _m0 from "protobufjs/minimal.js";
3
3
  export declare const protobufPackage = "srpc";
4
4
  /** Packet is a message sent over a srpc packet connection. */
5
5
  export interface Packet {
6
6
  body?: {
7
- $case: 'callStart';
7
+ $case: "callStart";
8
8
  callStart: CallStart;
9
9
  } | {
10
- $case: 'callData';
10
+ $case: "callData";
11
11
  callData: CallData;
12
12
  };
13
13
  }
@@ -162,9 +162,9 @@ declare type Builtin = Date | Function | Uint8Array | string | number | boolean
162
162
  export declare type DeepPartial<T> = T extends Builtin ? T : T extends Long ? string | number | Long : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
163
163
  $case: string;
164
164
  } ? {
165
- [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]>;
165
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
166
166
  } & {
167
- $case: T['$case'];
167
+ $case: T["$case"];
168
168
  } : T extends {} ? {
169
169
  [K in keyof T]?: DeepPartial<T[K]>;
170
170
  } : Partial<T>;