starpc 0.2.2 → 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,6 +1,7 @@
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;
@@ -11,8 +12,8 @@ export declare class BroadcastChannelIterable<T> implements Duplex<T> {
11
12
  private _createSource;
12
13
  }
13
14
  export declare function newBroadcastChannelIterable<T>(readName: string, writeName: string): BroadcastChannelIterable<T>;
14
- export declare class BroadcastChannelConn extends Conn {
15
- private channel;
15
+ export declare class BroadcastChannelConn extends DuplexConn {
16
+ private broadcastChannel;
16
17
  constructor(readChannel: BroadcastChannel, writeChannel: BroadcastChannel, server?: Server, connParams?: ConnParams);
17
18
  getReadChannel(): BroadcastChannel;
18
19
  getWriteChannel(): BroadcastChannel;
@@ -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
@@ -47,20 +46,20 @@ export function newBroadcastChannelIterable(readName, writeName) {
47
46
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
48
47
  //
49
48
  // expects Uint8Array objects over the BroadcastChannel.
50
- export class BroadcastChannelConn extends Conn {
51
- // channel is the broadcast channel iterable
52
- channel;
49
+ export class BroadcastChannelConn extends DuplexConn {
50
+ // broadcastChannel is the broadcast channel iterable
51
+ broadcastChannel;
53
52
  constructor(readChannel, writeChannel, server, connParams) {
54
- super(server, connParams);
55
- this.channel = new BroadcastChannelIterable(readChannel, writeChannel);
56
- pipe(this, this.channel, this);
53
+ const broadcastChannel = new BroadcastChannelIterable(readChannel, writeChannel);
54
+ super(broadcastChannel, server, connParams);
55
+ this.broadcastChannel = broadcastChannel;
57
56
  }
58
57
  // getReadChannel returns the read BroadcastChannel.
59
58
  getReadChannel() {
60
- return this.channel.readChannel;
59
+ return this.broadcastChannel.readChannel;
61
60
  }
62
61
  // getWriteChannel returns the write BroadcastChannel.
63
62
  getWriteChannel() {
64
- return this.channel.writeChannel;
63
+ return this.broadcastChannel.writeChannel;
65
64
  }
66
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
+ }
@@ -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,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.2",
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,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> {
@@ -62,9 +63,9 @@ export function newBroadcastChannelIterable<T>(
62
63
  // BroadcastChannelConn implements a connection with a BroadcastChannel.
63
64
  //
64
65
  // expects Uint8Array objects over the BroadcastChannel.
65
- export class BroadcastChannelConn extends Conn {
66
- // channel is the broadcast channel iterable
67
- private channel: BroadcastChannelIterable<Uint8Array>
66
+ export class BroadcastChannelConn extends DuplexConn {
67
+ // broadcastChannel is the broadcast channel iterable
68
+ private broadcastChannel: BroadcastChannelIterable<Uint8Array>
68
69
 
69
70
  constructor(
70
71
  readChannel: BroadcastChannel,
@@ -72,21 +73,21 @@ export class BroadcastChannelConn extends Conn {
72
73
  server?: Server,
73
74
  connParams?: ConnParams
74
75
  ) {
75
- super(server, connParams)
76
- this.channel = new BroadcastChannelIterable<Uint8Array>(
76
+ const broadcastChannel = new BroadcastChannelIterable<Uint8Array>(
77
77
  readChannel,
78
78
  writeChannel
79
79
  )
80
- pipe(this, this.channel, this)
80
+ super(broadcastChannel, server, connParams)
81
+ this.broadcastChannel = broadcastChannel
81
82
  }
82
83
 
83
84
  // getReadChannel returns the read BroadcastChannel.
84
85
  public getReadChannel(): BroadcastChannel {
85
- return this.channel.readChannel
86
+ return this.broadcastChannel.readChannel
86
87
  }
87
88
 
88
89
  // getWriteChannel returns the write BroadcastChannel.
89
90
  public getWriteChannel(): BroadcastChannel {
90
- return this.channel.writeChannel
91
+ return this.broadcastChannel.writeChannel
91
92
  }
92
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
@@ -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,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
+ }