-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A channel augmenter that encodes and decodes messages as JSON
- Loading branch information
1 parent
d32e6d6
commit 3007e83
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/rpc-subscriptions/src/__tests__/rpc-subscriptions-json-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { RpcSubscriptionsChannel } from '@solana/rpc-subscriptions-spec'; | ||
|
||
import { getRpcSubscriptionsChannelWithJSONSerialization } from '../rpc-subscriptions-json'; | ||
|
||
describe('getRpcSubscriptionsChannelWithJSONSerialization', () => { | ||
let mockOn: jest.Mock; | ||
let channel: RpcSubscriptionsChannel<string, string>; | ||
function receiveMessage(message: unknown) { | ||
mockOn.mock.calls.filter(([type]) => type === 'message').forEach(([_, listener]) => listener(message)); | ||
} | ||
beforeEach(() => { | ||
mockOn = jest.fn(); | ||
channel = { | ||
on: mockOn, | ||
send: jest.fn(), | ||
}; | ||
}); | ||
it('forwards JSON-serialized messages to the underlying channel', () => { | ||
const channelWithJSONSerialization = getRpcSubscriptionsChannelWithJSONSerialization(channel); | ||
channelWithJSONSerialization.send('hello'); | ||
expect(channel.send).toHaveBeenCalledWith(JSON.stringify('hello')); | ||
}); | ||
it('deserializes messages received from the underlying channel as JSON', () => { | ||
const channelWithJSONSerialization = getRpcSubscriptionsChannelWithJSONSerialization(channel); | ||
const messageListener = jest.fn(); | ||
channelWithJSONSerialization.on('message', messageListener); | ||
receiveMessage(JSON.stringify('hello')); | ||
expect(messageListener).toHaveBeenCalledWith('hello'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RpcSubscriptionsChannel } from '@solana/rpc-subscriptions-spec'; | ||
|
||
export function getRpcSubscriptionsChannelWithJSONSerialization( | ||
channel: RpcSubscriptionsChannel<string, string>, | ||
): RpcSubscriptionsChannel<unknown, unknown> { | ||
return Object.freeze({ | ||
...channel, | ||
on(type, listener, options) { | ||
if (type !== 'message') { | ||
return channel.on(type, listener, options); | ||
} | ||
return channel.on( | ||
'message', | ||
function deserializingListener(message: string) { | ||
const deserializedMessage = JSON.parse(message); | ||
listener(deserializedMessage); | ||
}, | ||
options, | ||
); | ||
}, | ||
send(message) { | ||
const serializedMessage = JSON.stringify(message); | ||
return channel.send(serializedMessage); | ||
}, | ||
} as RpcSubscriptionsChannel<unknown, unknown>); | ||
} |