starpc 0.2.0 → 0.3.0

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