Skip to content

Commit

Permalink
Merge pull request #21 from jsonjoy-com/devex
Browse files Browse the repository at this point in the history
Devex
  • Loading branch information
streamich authored Oct 8, 2024
2 parents 7571120 + 33d8a2f commit 73526e4
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/e2e/clients.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import WebSocket from 'ws';
import type {RpcCodec} from '../../common/codec/RpcCodec';
import {RpcPersistentClient, WebSocketChannel} from '../../common';
import {FetchRpcClient} from '../../common/rpc/client/FetchRpcClient';
import {StreamingRpcClient} from '../../common';
import WebSocket from 'ws';
import {RpcPersistentClient, WebSocketChannel} from '../../common';
import type {RpcCodec} from '../../common/codec/RpcCodec';

export const setupRpcPersistentClient = (codec: RpcCodec) => {
const port = +(process.env.PORT || 9999);
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/e2e/demo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {RpcPersistentClient, StreamingRpcClient, WebSocketChannel} from '../../c
import {FetchRpcClient} from '../../common/rpc/client/FetchRpcClient';

const secure = true;
const host = 'demo-iasd8921ondk0.jsonjoy.com';
// const host = '127.0.0.1:8080';
const host = 'api.jsonjoy.org';

export const setupDemoServerPersistentClient = (codec: RpcCodec) => {
const url = `ws${secure ? 's' : ''}://${host}/rx`;
Expand Down
24 changes: 24 additions & 0 deletions src/browser/createBinaryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {CborJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/cbor';
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
import {RpcCodec} from '../common/codec/RpcCodec';
import {BinaryRpcMessageCodec} from '../common/codec/binary';
import {createClient} from './createClient';

/**
* Constructs a JSON Reactive RPC client.
*
* ```typescript
* const client = createBinaryClient('wss://api.host.com', 'token');
* ```
*
* @param url RPC endpoint.
* @param token Authentication token.
* @returns An RPC client.
*/
export const createBinaryClient = (url: string, token?: string) => {
const writer = new Writer(1024 * 4);
const msg = new BinaryRpcMessageCodec();
const req = new CborJsonValueCodec(writer);
const codec = new RpcCodec(msg, req, req);
return createClient(codec, url, token);
};
37 changes: 0 additions & 37 deletions src/browser/createBinaryWsRpcClient.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/browser/createClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {RpcPersistentClient} from '../common/rpc/RpcPersistentClient';
import {WebSocketChannel} from '../common/channel/channel';
import type {RpcCodec} from '../common/codec/RpcCodec';

/**
* Constructs a {@link RpcPersistentClient} with the given codec.
*
* ```typescript
* const client = createRpcPersistentClient(codec, 'wss://api.host.com', 'token');
* ```
*
* @param codec RPC codec.
* @param url RPC endpoint.
* @param token Authentication token.
* @returns An RPC client.
*/
export const createClient = (codec: RpcCodec, url: string, token?: string) => {
const protocols: string[] = [codec.specifier()];
if (token) protocols.push(token);
const client = new RpcPersistentClient({
codec,
channel: {
newChannel: () =>
new WebSocketChannel({
newSocket: () => new WebSocket(url, protocols),
}),
},
});
client.start();
return client;
};
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import {JsonJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/json';
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
import {RpcPersistentClient, WebSocketChannel} from '../common';
import {RpcCodec} from '../common/codec/RpcCodec';
import {CompactRpcMessageCodec} from '../common/codec/compact';
import {createClient} from './createClient';

/**
* Constructs a JSON Reactive RPC client.
*
* @param url RPC endpoint.
* @param token Authentication token.
* @returns An RPC client.
*/
export const createJsonWsRpcClient = (url: string, token: string) => {
export const createJsonClient = (url: string, token?: string) => {
const writer = new Writer(1024 * 4);
const msg = new CompactRpcMessageCodec();
const req = new JsonJsonValueCodec(writer);
const codec = new RpcCodec(msg, req, req);
const client = new RpcPersistentClient({
codec,
channel: {
newChannel: () =>
new WebSocketChannel({
newSocket: () => new WebSocket(url, [codec.specifier(), token]),
}),
},
});
client.start();
return client;
return createClient(codec, url, token);
};
2 changes: 2 additions & 0 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {createClient} from './createClient';
export {createBinaryClient} from './createBinaryClient';
5 changes: 5 additions & 0 deletions src/server/http1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type * from './types';
export * from './context';
export * from './Http1Server';
export * from './RpcServer';
export * from './util';
5 changes: 4 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './uws';
export * from './context';
export * from './errors';
export * from './ws';
export * from './http1';
2 changes: 2 additions & 0 deletions src/server/ws/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './codec';
export {WsServerConnection, WsServerConnectionSocket} from './server/WsServerConnection';

0 comments on commit 73526e4

Please sign in to comment.