Skip to content

Commit 73526e4

Browse files
authored
Merge pull request #21 from jsonjoy-com/devex
Devex
2 parents 7571120 + 33d8a2f commit 73526e4

File tree

10 files changed

+76
-56
lines changed

10 files changed

+76
-56
lines changed

src/__tests__/e2e/clients.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import WebSocket from 'ws';
2-
import type {RpcCodec} from '../../common/codec/RpcCodec';
3-
import {RpcPersistentClient, WebSocketChannel} from '../../common';
41
import {FetchRpcClient} from '../../common/rpc/client/FetchRpcClient';
52
import {StreamingRpcClient} from '../../common';
3+
import WebSocket from 'ws';
4+
import {RpcPersistentClient, WebSocketChannel} from '../../common';
5+
import type {RpcCodec} from '../../common/codec/RpcCodec';
66

77
export const setupRpcPersistentClient = (codec: RpcCodec) => {
88
const port = +(process.env.PORT || 9999);

src/__tests__/e2e/demo-client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {RpcPersistentClient, StreamingRpcClient, WebSocketChannel} from '../../c
44
import {FetchRpcClient} from '../../common/rpc/client/FetchRpcClient';
55

66
const secure = true;
7-
const host = 'demo-iasd8921ondk0.jsonjoy.com';
8-
// const host = '127.0.0.1:8080';
7+
const host = 'api.jsonjoy.org';
98

109
export const setupDemoServerPersistentClient = (codec: RpcCodec) => {
1110
const url = `ws${secure ? 's' : ''}://${host}/rx`;

src/browser/createBinaryClient.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {CborJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/cbor';
2+
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
3+
import {RpcCodec} from '../common/codec/RpcCodec';
4+
import {BinaryRpcMessageCodec} from '../common/codec/binary';
5+
import {createClient} from './createClient';
6+
7+
/**
8+
* Constructs a JSON Reactive RPC client.
9+
*
10+
* ```typescript
11+
* const client = createBinaryClient('wss://api.host.com', 'token');
12+
* ```
13+
*
14+
* @param url RPC endpoint.
15+
* @param token Authentication token.
16+
* @returns An RPC client.
17+
*/
18+
export const createBinaryClient = (url: string, token?: string) => {
19+
const writer = new Writer(1024 * 4);
20+
const msg = new BinaryRpcMessageCodec();
21+
const req = new CborJsonValueCodec(writer);
22+
const codec = new RpcCodec(msg, req, req);
23+
return createClient(codec, url, token);
24+
};

src/browser/createBinaryWsRpcClient.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/browser/createClient.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {RpcPersistentClient} from '../common/rpc/RpcPersistentClient';
2+
import {WebSocketChannel} from '../common/channel/channel';
3+
import type {RpcCodec} from '../common/codec/RpcCodec';
4+
5+
/**
6+
* Constructs a {@link RpcPersistentClient} with the given codec.
7+
*
8+
* ```typescript
9+
* const client = createRpcPersistentClient(codec, 'wss://api.host.com', 'token');
10+
* ```
11+
*
12+
* @param codec RPC codec.
13+
* @param url RPC endpoint.
14+
* @param token Authentication token.
15+
* @returns An RPC client.
16+
*/
17+
export const createClient = (codec: RpcCodec, url: string, token?: string) => {
18+
const protocols: string[] = [codec.specifier()];
19+
if (token) protocols.push(token);
20+
const client = new RpcPersistentClient({
21+
codec,
22+
channel: {
23+
newChannel: () =>
24+
new WebSocketChannel({
25+
newSocket: () => new WebSocket(url, protocols),
26+
}),
27+
},
28+
});
29+
client.start();
30+
return client;
31+
};
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
import {JsonJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/json';
22
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
3-
import {RpcPersistentClient, WebSocketChannel} from '../common';
43
import {RpcCodec} from '../common/codec/RpcCodec';
54
import {CompactRpcMessageCodec} from '../common/codec/compact';
5+
import {createClient} from './createClient';
66

77
/**
88
* Constructs a JSON Reactive RPC client.
9+
*
910
* @param url RPC endpoint.
1011
* @param token Authentication token.
1112
* @returns An RPC client.
1213
*/
13-
export const createJsonWsRpcClient = (url: string, token: string) => {
14+
export const createJsonClient = (url: string, token?: string) => {
1415
const writer = new Writer(1024 * 4);
1516
const msg = new CompactRpcMessageCodec();
1617
const req = new JsonJsonValueCodec(writer);
1718
const codec = new RpcCodec(msg, req, req);
18-
const client = new RpcPersistentClient({
19-
codec,
20-
channel: {
21-
newChannel: () =>
22-
new WebSocketChannel({
23-
newSocket: () => new WebSocket(url, [codec.specifier(), token]),
24-
}),
25-
},
26-
});
27-
client.start();
28-
return client;
19+
return createClient(codec, url, token);
2920
};

src/browser/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {createClient} from './createClient';
2+
export {createBinaryClient} from './createBinaryClient';

src/server/http1/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type * from './types';
2+
export * from './context';
3+
export * from './Http1Server';
4+
export * from './RpcServer';
5+
export * from './util';

src/server/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from './uws';
1+
export * from './context';
2+
export * from './errors';
3+
export * from './ws';
4+
export * from './http1';

src/server/ws/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './codec';
2+
export {WsServerConnection, WsServerConnectionSocket} from './server/WsServerConnection';

0 commit comments

Comments
 (0)