starpc 0.2.0 → 0.2.1

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.
@@ -2,16 +2,18 @@ import type { Duplex, Sink } from 'it-stream-types';
2
2
  import { Conn, ConnParams } from './conn';
3
3
  import { Server } from './server';
4
4
  export declare class BroadcastChannelIterable<T> implements Duplex<T> {
5
- readonly channel: BroadcastChannel;
5
+ readonly readChannel: BroadcastChannel;
6
+ readonly writeChannel: BroadcastChannel;
6
7
  sink: Sink<T>;
7
8
  source: AsyncIterable<T>;
8
- constructor(channel: BroadcastChannel);
9
+ constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel);
9
10
  private _createSink;
10
11
  private _createSource;
11
12
  }
12
- export declare function newBroadcastChannelIterable<T>(name: string): BroadcastChannelIterable<T>;
13
+ export declare function newBroadcastChannelIterable<T>(readName: string, writeName: string): BroadcastChannelIterable<T>;
13
14
  export declare class BroadcastChannelConn extends Conn {
14
15
  private channel;
15
- constructor(channel: BroadcastChannel, server?: Server, connParams?: ConnParams);
16
- getBroadcastChannel(): BroadcastChannel;
16
+ constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel, server?: Server, connParams?: ConnParams);
17
+ getReadChannel(): BroadcastChannel;
18
+ getWriteChannel(): BroadcastChannel;
17
19
  }
@@ -3,14 +3,17 @@ import { EventIterator } from 'event-iterator';
3
3
  import { pipe } from 'it-pipe';
4
4
  // BroadcastChannelIterable is a AsyncIterable wrapper for BroadcastChannel.
5
5
  export class BroadcastChannelIterable {
6
- // channel is the broadcast channel
7
- channel;
6
+ // readChannel is the incoming broadcast channel
7
+ readChannel;
8
+ // writeChannel is the outgoing broadcast channel
9
+ writeChannel;
8
10
  // sink is the sink for incoming messages.
9
11
  sink;
10
12
  // source is the source for outgoing messages.
11
13
  source;
12
- constructor(channel) {
13
- this.channel = channel;
14
+ constructor(readChannel, writeChannel) {
15
+ this.readChannel = readChannel;
16
+ this.writeChannel = writeChannel;
14
17
  this.sink = this._createSink();
15
18
  this.source = this._createSource();
16
19
  }
@@ -18,7 +21,7 @@ export class BroadcastChannelIterable {
18
21
  _createSink() {
19
22
  return async (source) => {
20
23
  for await (const msg of source) {
21
- this.channel.postMessage(msg);
24
+ this.writeChannel.postMessage(msg);
22
25
  }
23
26
  };
24
27
  }
@@ -30,17 +33,16 @@ export class BroadcastChannelIterable {
30
33
  queue.push(ev.data);
31
34
  }
32
35
  };
33
- this.channel.addEventListener('message', messageListener);
36
+ this.readChannel.addEventListener('message', messageListener);
34
37
  return () => {
35
- this.channel.removeEventListener('message', messageListener);
38
+ this.readChannel.removeEventListener('message', messageListener);
36
39
  };
37
40
  });
38
41
  }
39
42
  }
40
43
  // newBroadcastChannelIterable constructs a BroadcastChannelIterable with a channel name.
41
- export function newBroadcastChannelIterable(name) {
42
- const channel = new BroadcastChannel(name);
43
- return new BroadcastChannelIterable(channel);
44
+ export function newBroadcastChannelIterable(readName, writeName) {
45
+ return new BroadcastChannelIterable(new BroadcastChannel(readName), new BroadcastChannel(writeName));
44
46
  }
45
47
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
46
48
  //
@@ -48,13 +50,17 @@ export function newBroadcastChannelIterable(name) {
48
50
  export class BroadcastChannelConn extends Conn {
49
51
  // channel is the broadcast channel iterable
50
52
  channel;
51
- constructor(channel, server, connParams) {
53
+ constructor(readChannel, writeChannel, server, connParams) {
52
54
  super(server, connParams);
53
- this.channel = new BroadcastChannelIterable(channel);
55
+ this.channel = new BroadcastChannelIterable(readChannel, writeChannel);
54
56
  pipe(this, this.channel, this);
55
57
  }
56
- // getBroadcastChannel returns the BroadcastChannel.
57
- getBroadcastChannel() {
58
- return this.channel.channel;
58
+ // getReadChannel returns the read BroadcastChannel.
59
+ getReadChannel() {
60
+ return this.channel.readChannel;
61
+ }
62
+ // getWriteChannel returns the write BroadcastChannel.
63
+ getWriteChannel() {
64
+ return this.channel.writeChannel;
59
65
  }
60
66
  }
@@ -5,3 +5,4 @@ export { Conn } 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';
@@ -4,3 +4,4 @@ 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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -6,15 +6,18 @@ import { Server } from './server'
6
6
 
7
7
  // BroadcastChannelIterable is a AsyncIterable wrapper for BroadcastChannel.
8
8
  export class BroadcastChannelIterable<T> implements Duplex<T> {
9
- // channel is the broadcast channel
10
- public readonly channel: BroadcastChannel
9
+ // readChannel is the incoming broadcast channel
10
+ public readonly readChannel: BroadcastChannel
11
+ // writeChannel is the outgoing broadcast channel
12
+ public readonly writeChannel: BroadcastChannel
11
13
  // sink is the sink for incoming messages.
12
14
  public sink: Sink<T>
13
15
  // source is the source for outgoing messages.
14
16
  public source: AsyncIterable<T>
15
17
 
16
- constructor(channel: BroadcastChannel) {
17
- this.channel = channel
18
+ constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel) {
19
+ this.readChannel = readChannel
20
+ this.writeChannel = writeChannel
18
21
  this.sink = this._createSink()
19
22
  this.source = this._createSource()
20
23
  }
@@ -23,7 +26,7 @@ export class BroadcastChannelIterable<T> implements Duplex<T> {
23
26
  private _createSink(): Sink<T> {
24
27
  return async (source) => {
25
28
  for await (const msg of source) {
26
- this.channel.postMessage(msg)
29
+ this.writeChannel.postMessage(msg)
27
30
  }
28
31
  }
29
32
  }
@@ -36,10 +39,10 @@ export class BroadcastChannelIterable<T> implements Duplex<T> {
36
39
  queue.push(ev.data)
37
40
  }
38
41
  }
39
- this.channel.addEventListener('message', messageListener)
42
+ this.readChannel.addEventListener('message', messageListener)
40
43
 
41
44
  return () => {
42
- this.channel.removeEventListener('message', messageListener)
45
+ this.readChannel.removeEventListener('message', messageListener)
43
46
  }
44
47
  })
45
48
  }
@@ -47,10 +50,13 @@ export class BroadcastChannelIterable<T> implements Duplex<T> {
47
50
 
48
51
  // newBroadcastChannelIterable constructs a BroadcastChannelIterable with a channel name.
49
52
  export function newBroadcastChannelIterable<T>(
50
- name: string
53
+ readName: string,
54
+ writeName: string
51
55
  ): BroadcastChannelIterable<T> {
52
- const channel = new BroadcastChannel(name)
53
- return new BroadcastChannelIterable<T>(channel)
56
+ return new BroadcastChannelIterable<T>(
57
+ new BroadcastChannel(readName),
58
+ new BroadcastChannel(writeName)
59
+ )
54
60
  }
55
61
 
56
62
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
@@ -61,17 +67,26 @@ export class BroadcastChannelConn extends Conn {
61
67
  private channel: BroadcastChannelIterable<Uint8Array>
62
68
 
63
69
  constructor(
64
- channel: BroadcastChannel,
70
+ readChannel: BroadcastChannel,
71
+ writeChannel: BroadcastChannel,
65
72
  server?: Server,
66
73
  connParams?: ConnParams
67
74
  ) {
68
75
  super(server, connParams)
69
- this.channel = new BroadcastChannelIterable<Uint8Array>(channel)
76
+ this.channel = new BroadcastChannelIterable<Uint8Array>(
77
+ readChannel,
78
+ writeChannel
79
+ )
70
80
  pipe(this, this.channel, this)
71
81
  }
72
82
 
73
- // getBroadcastChannel returns the BroadcastChannel.
74
- public getBroadcastChannel(): BroadcastChannel {
75
- return this.channel.channel
83
+ // getReadChannel returns the read BroadcastChannel.
84
+ public getReadChannel(): BroadcastChannel {
85
+ return this.channel.readChannel
86
+ }
87
+
88
+ // getWriteChannel returns the write BroadcastChannel.
89
+ public getWriteChannel(): BroadcastChannel {
90
+ return this.channel.writeChannel
76
91
  }
77
92
  }
package/srpc/index.ts CHANGED
@@ -5,3 +5,8 @@ export { Conn } 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 {
9
+ BroadcastChannelIterable,
10
+ newBroadcastChannelIterable,
11
+ BroadcastChannelConn,
12
+ } from './broadcast-channel'