starpc 0.1.8 → 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
  }
@@ -16,7 +16,7 @@ export class ClientRPC extends CommonRPC {
16
16
  rpcService: this.service,
17
17
  rpcMethod: this.method,
18
18
  data: data || new Uint8Array(0),
19
- dataIsZero: (!!data) && data.length === 0,
19
+ dataIsZero: !!data && data.length === 0,
20
20
  };
21
21
  await this.writePacket({
22
22
  body: {
@@ -29,7 +29,7 @@ export class CommonRPC {
29
29
  async writeCallData(data, complete, error) {
30
30
  const callData = {
31
31
  data: data || new Uint8Array(0),
32
- dataIsZero: (!!data) && data.length === 0,
32
+ dataIsZero: !!data && data.length === 0,
33
33
  complete: complete || false,
34
34
  error: error || '',
35
35
  };
@@ -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';
@@ -1,5 +1,4 @@
1
- import { InvokeFn } from 'srpc';
2
- import { Handler } from './handler';
1
+ import { InvokeFn, Handler } from './handler';
3
2
  export interface Mux {
4
3
  register(handler: Handler): void;
5
4
  lookupMethod(serviceID: string, methodID: string): Promise<InvokeFn | null>;
@@ -23,7 +23,6 @@ export class ServerRPC extends CommonRPC {
23
23
  }
24
24
  this.pushRpcData(packet.data, packet.dataIsZero);
25
25
  this.invokeRPC(methodDef);
26
- return super.handleCallStart(packet);
27
26
  }
28
27
  // handleCallData handles a CallData packet.
29
28
  async handleCallData(packet) {
@@ -35,8 +34,7 @@ export class ServerRPC extends CommonRPC {
35
34
  // invokeRPC starts invoking the RPC handler.
36
35
  invokeRPC(invokeFn) {
37
36
  const dataSink = this._createDataSink();
38
- invokeFn(this.rpcDataSource, dataSink)
39
- .catch((err) => {
37
+ invokeFn(this.rpcDataSource, dataSink).catch((err) => {
40
38
  this.close(err);
41
39
  });
42
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.1.8",
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": {
@@ -36,6 +36,7 @@
36
36
  "scripts": {
37
37
  "build": "rimraf ./dist && tsc --project tsconfig.build.json --outDir ./dist/",
38
38
  "check": "tsc",
39
+ "deps": "depcheck",
39
40
  "codegen": "npm run gen",
40
41
  "ci": "npm run build && npm run lint:js && npm run lint:go",
41
42
  "format": "prettier --write './{srpc,echo,e2e,integration}/**/(*.ts|*.tsx|*.html|*.css)'",
@@ -56,29 +57,21 @@
56
57
  "singleQuote": true
57
58
  },
58
59
  "devDependencies": {
59
- "@babel/core": "^7.18.5",
60
- "@babel/preset-env": "^7.18.2",
61
- "@babel/preset-typescript": "^7.17.12",
62
- "@types/varint": "^6.0.0",
63
60
  "@typescript-eslint/eslint-plugin": "^5.27.1",
64
61
  "@typescript-eslint/parser": "^5.27.1",
65
- "esbuild": "^0.14.44",
66
- "eslint": "^8.17.0",
62
+ "depcheck": "^1.4.3",
63
+ "esbuild": "^0.14.46",
64
+ "eslint": "^8.18.0",
67
65
  "eslint-config-prettier": "^8.5.0",
68
- "eslint-config-standard-with-typescript": "^21.0.1",
69
- "eslint-plugin-react-hooks": "^4.6.0",
70
- "husky": "^8.0.1",
71
- "prettier": "^2.7.0",
66
+ "prettier": "^2.7.1",
72
67
  "rimraf": "^3.0.2",
73
- "ts-eager": "^2.0.2",
74
- "ts-node": "^10.8.1",
75
68
  "ts-proto": "^1.115.4",
76
- "typescript": "^4.7.3"
69
+ "typescript": "^4.7.4"
77
70
  },
78
71
  "dependencies": {
79
- "@libp2p/components": "^1.0.0",
80
- "@libp2p/interface-connection": "^1.0.1",
81
- "@libp2p/mplex": "^2.0.0",
72
+ "@libp2p/interface-connection": "^2.0.0",
73
+ "@libp2p/interface-stream-muxer": "^1.0.2",
74
+ "@libp2p/mplex": "^3.0.0",
82
75
  "event-iterator": "^2.0.0",
83
76
  "isomorphic-ws": "^4.0.1",
84
77
  "it-length-prefixed": "^7.0.1",
@@ -86,10 +79,10 @@
86
79
  "it-pushable": "^3.0.0",
87
80
  "it-stream-types": "^1.0.4",
88
81
  "it-ws": "^5.0.2",
82
+ "long": "^5.2.0",
89
83
  "patch-package": "^6.4.7",
90
84
  "protobufjs": "^6.11.3",
91
85
  "rxjs": "^7.5.5",
92
- "ts-poet": "^4.13.0",
93
86
  "ws": "^8.8.0"
94
87
  }
95
88
  }
@@ -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
  }
@@ -19,7 +19,7 @@ export class ClientRPC extends CommonRPC {
19
19
  rpcService: this.service,
20
20
  rpcMethod: this.method,
21
21
  data: data || new Uint8Array(0),
22
- dataIsZero: (!!data) && data.length === 0,
22
+ dataIsZero: !!data && data.length === 0,
23
23
  }
24
24
  await this.writePacket({
25
25
  body: {
package/srpc/client.ts CHANGED
@@ -133,7 +133,7 @@ export class Client implements TsProtoRpc {
133
133
  private async startRpc(
134
134
  rpcService: string,
135
135
  rpcMethod: string,
136
- data: Uint8Array | null,
136
+ data: Uint8Array | null
137
137
  ): Promise<ClientRPC> {
138
138
  const conn = await this.openConnFn()
139
139
  const call = new ClientRPC(rpcService, rpcMethod)
@@ -48,7 +48,7 @@ export class CommonRPC {
48
48
  ) {
49
49
  const callData: CallData = {
50
50
  data: data || new Uint8Array(0),
51
- dataIsZero: (!!data) && data.length === 0,
51
+ dataIsZero: !!data && data.length === 0,
52
52
  complete: complete || false,
53
53
  error: error || '',
54
54
  }
@@ -103,8 +103,11 @@ export class CommonRPC {
103
103
  )
104
104
  }
105
105
 
106
- // pushRpcData pushes incoming rpc data to the rpc data source.
107
- protected pushRpcData(data: Uint8Array | undefined, dataIsZero: boolean | undefined) {
106
+ // pushRpcData pushes incoming rpc data to the rpc data source.
107
+ protected pushRpcData(
108
+ data: Uint8Array | undefined,
109
+ dataIsZero: boolean | undefined
110
+ ) {
108
111
  if (dataIsZero) {
109
112
  if (!data || data.length !== 0) {
110
113
  data = new Uint8Array(0)
package/srpc/handler.ts CHANGED
@@ -66,11 +66,10 @@ export class StaticHandler implements Handler {
66
66
 
67
67
  // MethodProto is a function which matches one of the RPC signatures.
68
68
  type MethodProto =
69
- ((request: unknown) => Promise<unknown>) |
70
- ((request: unknown) => Observable<unknown>) |
71
- ((request: Observable<unknown>) => Promise<unknown>) |
72
- ((request: Observable<unknown>) => Observable<unknown>)
73
-
69
+ | ((request: unknown) => Promise<unknown>)
70
+ | ((request: unknown) => Observable<unknown>)
71
+ | ((request: Observable<unknown>) => Promise<unknown>)
72
+ | ((request: Observable<unknown>) => Observable<unknown>)
74
73
 
75
74
  // createInvokeFn builds an InvokeFn from a method definition and a function prototype.
76
75
  export function createInvokeFn(
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'
package/srpc/mux.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { InvokeFn } from 'srpc'
2
- import { Handler } from './handler'
1
+ import { InvokeFn, Handler } from './handler'
3
2
 
4
3
  // Mux contains a set of <service, method> handlers.
5
4
  export interface Mux {
@@ -31,7 +31,6 @@ export class ServerRPC extends CommonRPC {
31
31
  }
32
32
  this.pushRpcData(packet.data, packet.dataIsZero)
33
33
  this.invokeRPC(methodDef)
34
- return super.handleCallStart(packet)
35
34
  }
36
35
 
37
36
  // handleCallData handles a CallData packet.
@@ -45,10 +44,9 @@ export class ServerRPC extends CommonRPC {
45
44
  // invokeRPC starts invoking the RPC handler.
46
45
  private invokeRPC(invokeFn: InvokeFn) {
47
46
  const dataSink = this._createDataSink()
48
- invokeFn(this.rpcDataSource, dataSink)
49
- .catch((err) => {
50
- this.close(err)
51
- })
47
+ invokeFn(this.rpcDataSource, dataSink).catch((err) => {
48
+ this.close(err)
49
+ })
52
50
  }
53
51
 
54
52
  // _createDataSink creates a sink for outgoing data packets.