Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket Subscriptions via JRPC #107

Merged
merged 40 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
dec147b
initial pass at server supporting long running subscriptions via sockets
LiranCohen Feb 9, 2024
aaa6aa2
json rpc cleanup
LiranCohen Feb 10, 2024
6f46c01
brought back requestCounter functionality for jsonrpc response/error
LiranCohen Feb 11, 2024
523558d
add JSONRPCSocket class to handle requests and long-running subscript…
LiranCohen Feb 12, 2024
5b79176
handle connection close not found error explicitly, general clean up
LiranCohen Feb 12, 2024
81f3ccd
replace crypto.randomUUID()
LiranCohen Feb 12, 2024
1dd2943
clean up JSON RPC Socket client
LiranCohen Feb 12, 2024
919df3b
optional tenant gate and event stream in options
LiranCohen Feb 12, 2024
eac8d3b
refactor into separate files, account for null errors, use loglevel
LiranCohen Feb 12, 2024
0669f04
uncesseary chagnge
LiranCohen Feb 12, 2024
f2b71d3
update comments
LiranCohen Feb 13, 2024
47279cd
refactor SocketConnection
LiranCohen Feb 13, 2024
d9b731a
clearer comments related to connetion reference handling and close
LiranCohen Feb 13, 2024
c14ab4f
increase coverage in ws-api and dwn-server
LiranCohen Feb 13, 2024
9451a26
clean up listener on reject
LiranCohen Feb 14, 2024
5ed80e0
additional JSON RPC Socket client coverage
LiranCohen Feb 14, 2024
0f0a223
remove only test decorator
LiranCohen Feb 14, 2024
be4a981
addressed PR suggestions
LiranCohen Feb 14, 2024
50dbb89
remove close connection
LiranCohen Feb 14, 2024
18e4a5c
optional socket send logger for SocketConnection.send()
LiranCohen Feb 14, 2024
7bb207a
rename onError to callback
LiranCohen Feb 14, 2024
c837cd7
rename some classes for consistency, update coments as per PR review
LiranCohen Feb 15, 2024
b2eacec
update chai, added types for chai and sinon
LiranCohen Feb 15, 2024
e12028f
address review suggestions
LiranCohen Feb 15, 2024
97a1ef9
review comments
LiranCohen Feb 15, 2024
1c9adee
match class name in test
LiranCohen Feb 15, 2024
d699035
fix tests
LiranCohen Feb 16, 2024
16694f2
update jrpc subscription support - more error proof and concise
LiranCohen Feb 21, 2024
fcdf960
added `rpc.subscribe.close` method and handling
LiranCohen Feb 22, 2024
7de0ad1
increase testing across the board
LiranCohen Feb 23, 2024
f54fac2
remove utils that were not very useful
LiranCohen Feb 23, 2024
a1caaae
increase test covrage for connection manager and add comments
LiranCohen Feb 27, 2024
7c9983f
review comments
LiranCohen Feb 27, 2024
6bb0ede
review suggestions
LiranCohen Feb 27, 2024
db403d6
add coverage to json-rpc-socet
LiranCohen Feb 27, 2024
72c8a1d
removed test code
LiranCohen Feb 27, 2024
fdc0ee5
unecessary socket injection
LiranCohen Feb 27, 2024
077db21
remove unecessary JsonRpcParams = any export
LiranCohen Feb 27, 2024
ad90dd2
update comment
LiranCohen Feb 27, 2024
95bb520
remove unused test internals
LiranCohen Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/json-rpc-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class JsonRpcSocket {
static async connect(url: string, options: JsonRpcSocketOptions = {}): Promise<JsonRpcSocket> {
const { connectTimeout = CONNECT_TIMEOUT, responseTimeout = RESPONSE_TIMEOUT, onclose, onerror } = options;

const socket = new WebSocket(url);
const socket = new WebSocket(url, { timeout: connectTimeout });

socket.onclose = onclose;
socket.onerror = onerror;
Expand Down
9 changes: 4 additions & 5 deletions src/lib/json-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export type JsonRpcId = string | number | null;
export type JsonRpcParams = any;
export type JsonRpcVersion = '2.0';

export interface JsonRpcRequest {
jsonrpc: JsonRpcVersion;
id?: JsonRpcId;
method: string;
params?: JsonRpcParams;
params?: any;
/** JSON RPC Subscription Extension Parameters */
subscription?: {
id: JsonRpcId
Expand Down Expand Up @@ -76,7 +75,7 @@ export const createJsonRpcErrorResponse = (

export const createJsonRpcNotification = (
method: string,
params?: JsonRpcParams,
params?: any,
): JsonRpcRequest => {
return {
jsonrpc: '2.0',
Expand All @@ -88,7 +87,7 @@ export const createJsonRpcNotification = (
export const createJsonRpcSubscriptionRequest = (
id: JsonRpcId,
method: string,
params?: JsonRpcParams,
params?: any,
subscriptionId?: JsonRpcId
): JsonRpcRequest => {
return {
Expand All @@ -105,7 +104,7 @@ export const createJsonRpcSubscriptionRequest = (
export const createJsonRpcRequest = (
id: JsonRpcId,
method: string,
params?: JsonRpcParams,
params?: any,
): JsonRpcRequest => {
return {
jsonrpc: '2.0',
Expand Down
4 changes: 3 additions & 1 deletion tests/json-rpc-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ describe('JsonRpcSocket', () => {
const onErrorHandler = { onerror: ():void => {} };
const onErrorSpy = sinon.spy(onErrorHandler, 'onerror');

await JsonRpcSocket.connect('ws://127.0.0.1:9003', { onerror: onErrorHandler.onerror });
await JsonRpcSocket.connect('ws://127.0.0.1:9003', { onerror: onErrorHandler.onerror, connectTimeout: 1 });
// const serverSocket = [...wsServer.clients][0];
// serverSocket.emit('error', { type: 'error', target: null, message: 'unknown error' });
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved

await new Promise((resolve) => setTimeout(resolve, 5)); // wait for close event to arrive
expect(onErrorSpy.callCount).to.equal(1, 'error');
Expand Down
Loading