starpc 0.2.2 → 0.3.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.
@@ -1,19 +1,22 @@
1
1
  import type { Duplex, Sink } from 'it-stream-types';
2
- import { Conn, ConnParams } from './conn';
3
- import { Server } from './server';
2
+ import { ConnParams } from './conn.js';
3
+ import { Server } from './server.js';
4
+ import { DuplexConn } from './conn-duplex.js';
4
5
  export declare class BroadcastChannelIterable<T> implements Duplex<T> {
5
6
  readonly readChannel: BroadcastChannel;
6
7
  readonly writeChannel: BroadcastChannel;
7
8
  sink: Sink<T>;
8
9
  source: AsyncIterable<T>;
9
10
  constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel);
11
+ close(): void;
10
12
  private _createSink;
11
13
  private _createSource;
12
14
  }
13
15
  export declare function newBroadcastChannelIterable<T>(readName: string, writeName: string): BroadcastChannelIterable<T>;
14
- export declare class BroadcastChannelConn extends Conn {
15
- private channel;
16
+ export declare class BroadcastChannelConn extends DuplexConn {
17
+ private broadcastChannel;
16
18
  constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel, server?: Server, connParams?: ConnParams);
17
19
  getReadChannel(): BroadcastChannel;
18
20
  getWriteChannel(): BroadcastChannel;
21
+ close(): void;
19
22
  }
@@ -1,6 +1,5 @@
1
- import { Conn } from './conn';
2
1
  import { EventIterator } from 'event-iterator';
3
- import { pipe } from 'it-pipe';
2
+ import { DuplexConn } from './conn-duplex.js';
4
3
  // BroadcastChannelIterable is a AsyncIterable wrapper for BroadcastChannel.
5
4
  export class BroadcastChannelIterable {
6
5
  // readChannel is the incoming broadcast channel
@@ -17,6 +16,11 @@ export class BroadcastChannelIterable {
17
16
  this.sink = this._createSink();
18
17
  this.source = this._createSource();
19
18
  }
19
+ // close closes the broadcast channels.
20
+ close() {
21
+ this.readChannel.close();
22
+ this.writeChannel.close();
23
+ }
20
24
  // _createSink initializes the sink field.
21
25
  _createSink() {
22
26
  return async (source) => {
@@ -47,20 +51,24 @@ export function newBroadcastChannelIterable(readName, writeName) {
47
51
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
48
52
  //
49
53
  // expects Uint8Array objects over the BroadcastChannel.
50
- export class BroadcastChannelConn extends Conn {
51
- // channel is the broadcast channel iterable
52
- channel;
54
+ export class BroadcastChannelConn extends DuplexConn {
55
+ // broadcastChannel is the broadcast channel iterable
56
+ broadcastChannel;
53
57
  constructor(readChannel, writeChannel, server, connParams) {
54
- super(server, connParams);
55
- this.channel = new BroadcastChannelIterable(readChannel, writeChannel);
56
- pipe(this, this.channel, this);
58
+ const broadcastChannel = new BroadcastChannelIterable(readChannel, writeChannel);
59
+ super(broadcastChannel, server, connParams);
60
+ this.broadcastChannel = broadcastChannel;
57
61
  }
58
62
  // getReadChannel returns the read BroadcastChannel.
59
63
  getReadChannel() {
60
- return this.channel.readChannel;
64
+ return this.broadcastChannel.readChannel;
61
65
  }
62
66
  // getWriteChannel returns the write BroadcastChannel.
63
67
  getWriteChannel() {
64
- return this.channel.writeChannel;
68
+ return this.broadcastChannel.writeChannel;
69
+ }
70
+ // close closes the read and write channels.
71
+ close() {
72
+ this.broadcastChannel.close();
65
73
  }
66
74
  }
@@ -4,6 +4,7 @@ import type { OpenStreamFunc } from './stream.js';
4
4
  export declare class Client implements TsProtoRpc {
5
5
  private openConnFn;
6
6
  constructor(openConnFn: OpenStreamFunc);
7
+ setOpenConnFn(openConnFn: OpenStreamFunc): void;
7
8
  request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
8
9
  clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
9
10
  serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
@@ -25,6 +25,10 @@ export class Client {
25
25
  constructor(openConnFn) {
26
26
  this.openConnFn = openConnFn;
27
27
  }
28
+ // setOpenConnFn updates the openConnFn for the Client.
29
+ setOpenConnFn(openConnFn) {
30
+ this.openConnFn = openConnFn;
31
+ }
28
32
  // request starts a non-streaming request.
29
33
  async request(service, method, data) {
30
34
  const call = await this.startRpc(service, method, data);
@@ -0,0 +1,8 @@
1
+ import type { Duplex } from 'it-stream-types';
2
+ import { Conn, ConnParams } from './conn.js';
3
+ import { Server } from './server';
4
+ export declare class DuplexConn extends Conn {
5
+ private channel;
6
+ constructor(duplex: Duplex<Uint8Array>, server?: Server, connParams?: ConnParams);
7
+ getChannelDuplex(): Duplex<Uint8Array>;
8
+ }
@@ -0,0 +1,18 @@
1
+ import { pipe } from 'it-pipe';
2
+ import { Conn } from './conn.js';
3
+ // DuplexConn wraps any Duplex<Uint8Array> into a Conn.
4
+ //
5
+ // expects Uint8Array objects
6
+ export class DuplexConn extends Conn {
7
+ // channel is the iterable
8
+ channel;
9
+ constructor(duplex, server, connParams) {
10
+ super(server, connParams);
11
+ this.channel = duplex;
12
+ pipe(this, this.channel, this);
13
+ }
14
+ // getChannelDuplex returns the Duplex channel.
15
+ getChannelDuplex() {
16
+ return this.channel;
17
+ }
18
+ }
@@ -5,4 +5,5 @@ export { Conn, ConnParams } from './conn.js';
5
5
  export { Handler, InvokeFn, createHandler, createInvokeFn } from './handler.js';
6
6
  export { Mux, createMux } from './mux.js';
7
7
  export { WebSocketConn } from './websocket.js';
8
- export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel';
8
+ export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel.js';
9
+ export { MessagePortIterable, newMessagePortIterable, MessagePortConn, } from './message-port.js';
@@ -4,4 +4,5 @@ export { Conn } from './conn.js';
4
4
  export { createHandler, createInvokeFn } from './handler.js';
5
5
  export { createMux } from './mux.js';
6
6
  export { WebSocketConn } from './websocket.js';
7
- export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel';
7
+ export { BroadcastChannelIterable, newBroadcastChannelIterable, BroadcastChannelConn, } from './broadcast-channel.js';
8
+ export { MessagePortIterable, newMessagePortIterable, MessagePortConn, } from './message-port.js';
@@ -0,0 +1,21 @@
1
+ import type { Duplex, Sink } from 'it-stream-types';
2
+ import { ConnParams } from './conn.js';
3
+ import { DuplexConn } from './conn-duplex.js';
4
+ import { Server } from './server';
5
+ export declare class MessagePortIterable<T> implements Duplex<T> {
6
+ readonly port: MessagePort;
7
+ sink: Sink<T>;
8
+ source: AsyncIterable<T>;
9
+ private _source;
10
+ constructor(port: MessagePort);
11
+ close(): void;
12
+ private _createSink;
13
+ private _createSource;
14
+ }
15
+ export declare function newMessagePortIterable<T>(port: MessagePort): MessagePortIterable<T>;
16
+ export declare class MessagePortConn extends DuplexConn {
17
+ private messagePort;
18
+ constructor(port: MessagePort, server?: Server, connParams?: ConnParams);
19
+ getMessagePort(): MessagePort;
20
+ close(): void;
21
+ }
@@ -0,0 +1,69 @@
1
+ import { EventIterator } from 'event-iterator';
2
+ import { DuplexConn } from './conn-duplex.js';
3
+ // MessagePortIterable is a AsyncIterable wrapper for MessagePort.
4
+ export class MessagePortIterable {
5
+ // port is the message port
6
+ port;
7
+ // sink is the sink for incoming messages.
8
+ sink;
9
+ // source is the source for outgoing messages.
10
+ source;
11
+ // _source is the EventIterator for source.
12
+ _source;
13
+ constructor(port) {
14
+ this.port = port;
15
+ this.sink = this._createSink();
16
+ this._source = this._createSource();
17
+ this.source = this._source;
18
+ }
19
+ // close closes the message port.
20
+ close() {
21
+ this.port.close();
22
+ }
23
+ // _createSink initializes the sink field.
24
+ _createSink() {
25
+ return async (source) => {
26
+ for await (const msg of source) {
27
+ this.port.postMessage(msg);
28
+ }
29
+ };
30
+ }
31
+ // _createSource initializes the source field.
32
+ _createSource() {
33
+ return new EventIterator((queue) => {
34
+ const messageListener = (ev) => {
35
+ if (ev.data) {
36
+ queue.push(ev.data);
37
+ }
38
+ };
39
+ this.port.addEventListener('message', messageListener);
40
+ return () => {
41
+ this.port.removeEventListener('message', messageListener);
42
+ };
43
+ });
44
+ }
45
+ }
46
+ // newMessagePortIterable constructs a MessagePortIterable with a channel name.
47
+ export function newMessagePortIterable(port) {
48
+ return new MessagePortIterable(port);
49
+ }
50
+ // MessagePortConn implements a connection with a MessagePort.
51
+ //
52
+ // expects Uint8Array objects over the MessagePort.
53
+ export class MessagePortConn extends DuplexConn {
54
+ // messagePort is the message port iterable.
55
+ messagePort;
56
+ constructor(port, server, connParams) {
57
+ const messagePort = new MessagePortIterable(port);
58
+ super(messagePort, server, connParams);
59
+ this.messagePort = messagePort;
60
+ }
61
+ // getMessagePort returns the MessagePort.
62
+ getMessagePort() {
63
+ return this.messagePort.port;
64
+ }
65
+ // close closes the message port.
66
+ close() {
67
+ this.messagePort.close();
68
+ }
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.2.2",
3
+ "version": "0.3.2",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -1,8 +1,9 @@
1
1
  import type { Duplex, Sink } from 'it-stream-types'
2
- import { Conn, ConnParams } from './conn'
3
2
  import { EventIterator } from 'event-iterator'
4
- import { pipe } from 'it-pipe'
5
- import { Server } from './server'
3
+
4
+ import { ConnParams } from './conn.js'
5
+ import { Server } from './server.js'
6
+ import { DuplexConn } from './conn-duplex.js'
6
7
 
7
8
  // BroadcastChannelIterable is a AsyncIterable wrapper for BroadcastChannel.
8
9
  export class BroadcastChannelIterable<T> implements Duplex<T> {
@@ -22,6 +23,12 @@ export class BroadcastChannelIterable<T> implements Duplex<T> {
22
23
  this.source = this._createSource()
23
24
  }
24
25
 
26
+ // close closes the broadcast channels.
27
+ public close() {
28
+ this.readChannel.close()
29
+ this.writeChannel.close()
30
+ }
31
+
25
32
  // _createSink initializes the sink field.
26
33
  private _createSink(): Sink<T> {
27
34
  return async (source) => {
@@ -62,9 +69,9 @@ export function newBroadcastChannelIterable<T>(
62
69
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
63
70
  //
64
71
  // expects Uint8Array objects over the BroadcastChannel.
65
- export class BroadcastChannelConn extends Conn {
66
- // channel is the broadcast channel iterable
67
- private channel: BroadcastChannelIterable<Uint8Array>
72
+ export class BroadcastChannelConn extends DuplexConn {
73
+ // broadcastChannel is the broadcast channel iterable
74
+ private broadcastChannel: BroadcastChannelIterable<Uint8Array>
68
75
 
69
76
  constructor(
70
77
  readChannel: BroadcastChannel,
@@ -72,21 +79,26 @@ export class BroadcastChannelConn extends Conn {
72
79
  server?: Server,
73
80
  connParams?: ConnParams
74
81
  ) {
75
- super(server, connParams)
76
- this.channel = new BroadcastChannelIterable<Uint8Array>(
82
+ const broadcastChannel = new BroadcastChannelIterable<Uint8Array>(
77
83
  readChannel,
78
84
  writeChannel
79
85
  )
80
- pipe(this, this.channel, this)
86
+ super(broadcastChannel, server, connParams)
87
+ this.broadcastChannel = broadcastChannel
81
88
  }
82
89
 
83
90
  // getReadChannel returns the read BroadcastChannel.
84
91
  public getReadChannel(): BroadcastChannel {
85
- return this.channel.readChannel
92
+ return this.broadcastChannel.readChannel
86
93
  }
87
94
 
88
95
  // getWriteChannel returns the write BroadcastChannel.
89
96
  public getWriteChannel(): BroadcastChannel {
90
- return this.channel.writeChannel
97
+ return this.broadcastChannel.writeChannel
98
+ }
99
+
100
+ // close closes the read and write channels.
101
+ public close() {
102
+ this.broadcastChannel.close()
91
103
  }
92
104
  }
package/srpc/client.ts CHANGED
@@ -37,6 +37,11 @@ export class Client implements TsProtoRpc {
37
37
  this.openConnFn = openConnFn
38
38
  }
39
39
 
40
+ // setOpenConnFn updates the openConnFn for the Client.
41
+ public setOpenConnFn(openConnFn: OpenStreamFunc) {
42
+ this.openConnFn = openConnFn
43
+ }
44
+
40
45
  // request starts a non-streaming request.
41
46
  public async request(
42
47
  service: string,
@@ -0,0 +1,28 @@
1
+ import { pipe } from 'it-pipe'
2
+ import type { Duplex } from 'it-stream-types'
3
+
4
+ import { Conn, ConnParams } from './conn.js'
5
+ import { Server } from './server'
6
+
7
+ // DuplexConn wraps any Duplex<Uint8Array> into a Conn.
8
+ //
9
+ // expects Uint8Array objects
10
+ export class DuplexConn extends Conn {
11
+ // channel is the iterable
12
+ private channel: Duplex<Uint8Array>
13
+
14
+ constructor(
15
+ duplex: Duplex<Uint8Array>,
16
+ server?: Server,
17
+ connParams?: ConnParams
18
+ ) {
19
+ super(server, connParams)
20
+ this.channel = duplex
21
+ pipe(this, this.channel, this)
22
+ }
23
+
24
+ // getChannelDuplex returns the Duplex channel.
25
+ public getChannelDuplex(): Duplex<Uint8Array> {
26
+ return this.channel
27
+ }
28
+ }
package/srpc/index.ts CHANGED
@@ -9,4 +9,10 @@ export {
9
9
  BroadcastChannelIterable,
10
10
  newBroadcastChannelIterable,
11
11
  BroadcastChannelConn,
12
- } from './broadcast-channel'
12
+ } from './broadcast-channel.js'
13
+
14
+ export {
15
+ MessagePortIterable,
16
+ newMessagePortIterable,
17
+ MessagePortConn,
18
+ } from './message-port.js'
@@ -0,0 +1,86 @@
1
+ import type { Duplex, Sink } from 'it-stream-types'
2
+ import { EventIterator } from 'event-iterator'
3
+
4
+ import { ConnParams } from './conn.js'
5
+ import { DuplexConn } from './conn-duplex.js'
6
+ import { Server } from './server'
7
+
8
+ // MessagePortIterable is a AsyncIterable wrapper for MessagePort.
9
+ export class MessagePortIterable<T> implements Duplex<T> {
10
+ // port is the message port
11
+ public readonly port: MessagePort
12
+ // sink is the sink for incoming messages.
13
+ public sink: Sink<T>
14
+ // source is the source for outgoing messages.
15
+ public source: AsyncIterable<T>
16
+ // _source is the EventIterator for source.
17
+ private _source: EventIterator<T>
18
+
19
+ constructor(port: MessagePort) {
20
+ this.port = port
21
+ this.sink = this._createSink()
22
+ this._source = this._createSource()
23
+ this.source = this._source
24
+ }
25
+
26
+ // close closes the message port.
27
+ public close() {
28
+ this.port.close()
29
+ }
30
+
31
+ // _createSink initializes the sink field.
32
+ private _createSink(): Sink<T> {
33
+ return async (source) => {
34
+ for await (const msg of source) {
35
+ this.port.postMessage(msg)
36
+ }
37
+ }
38
+ }
39
+
40
+ // _createSource initializes the source field.
41
+ private _createSource() {
42
+ return new EventIterator<T>((queue) => {
43
+ const messageListener = (ev: MessageEvent<T>) => {
44
+ if (ev.data) {
45
+ queue.push(ev.data)
46
+ }
47
+ }
48
+
49
+ this.port.addEventListener('message', messageListener)
50
+ return () => {
51
+ this.port.removeEventListener('message', messageListener)
52
+ }
53
+ })
54
+ }
55
+ }
56
+
57
+ // newMessagePortIterable constructs a MessagePortIterable with a channel name.
58
+ export function newMessagePortIterable<T>(
59
+ port: MessagePort
60
+ ): MessagePortIterable<T> {
61
+ return new MessagePortIterable<T>(port)
62
+ }
63
+
64
+ // MessagePortConn implements a connection with a MessagePort.
65
+ //
66
+ // expects Uint8Array objects over the MessagePort.
67
+ export class MessagePortConn extends DuplexConn {
68
+ // messagePort is the message port iterable.
69
+ private messagePort: MessagePortIterable<Uint8Array>
70
+
71
+ constructor(port: MessagePort, server?: Server, connParams?: ConnParams) {
72
+ const messagePort = new MessagePortIterable<Uint8Array>(port)
73
+ super(messagePort, server, connParams)
74
+ this.messagePort = messagePort
75
+ }
76
+
77
+ // getMessagePort returns the MessagePort.
78
+ public getMessagePort(): MessagePort {
79
+ return this.messagePort.port
80
+ }
81
+
82
+ // close closes the message port.
83
+ public close() {
84
+ this.messagePort.close()
85
+ }
86
+ }