This repository was archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Allow instantiation with existing ws #49
Open
adrianhopebailie
wants to merge
6
commits into
master
Choose a base branch
from
feat/ws-constructor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae32320
feat: add option to pass raw socket into plugin
97258f3
feat: add option to pass raw socket into plugin
8f9afe9
fix: consolidate handle connection logic
adrianhopebailie 6d36f95
fix: use incomingWs for raw connection
adrianhopebailie 88aa3d7
fix: linting errors
adrianhopebailie 1aaed04
fix: address comment from @sentientwaffle
adrianhopebailie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
'use strict' | ||
|
||
const BtpPacket = require('btp-packet'); | ||
const assert = require('assert') | ||
const btp = require('btp-packet') | ||
const Plugin = require('..') | ||
const mockSocket = require('./helpers/mockSocket') | ||
const WebSocket = require('ws'); | ||
|
||
|
||
describe('BtpPlugin', function () { | ||
beforeEach(async function () { | ||
|
@@ -120,6 +123,58 @@ describe('BtpPlugin', function () { | |
}) | ||
}) | ||
|
||
describe('can pass in websocket connection', function () { | ||
|
||
beforeEach(async function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of this project is 2-space indented. |
||
this.client = new Plugin(this.clientOpts) | ||
}) | ||
|
||
|
||
afterEach(async function () { | ||
await this.client.disconnect() | ||
}) | ||
|
||
it('get incoming socket connection and intantiate plugin', async function () { | ||
return new Promise(resolve => { | ||
const ws = new WebSocket.Server({ port: 9000 }) | ||
let clientConnect = null | ||
|
||
|
||
ws.on('connection', async (connection) => { | ||
|
||
//Manually reply to the auth message | ||
connection.once('message', async (data) => { | ||
const authPacket = BtpPacket.deserialize(data) | ||
connection.send(BtpPacket.serializeResponse(authPacket.requestId, [])) | ||
}) | ||
|
||
this.server = new Plugin({raw: {socket: connection}}) | ||
|
||
await Promise.all([ | ||
clientConnect, | ||
this.server.connect() | ||
]) | ||
|
||
assert.strictEqual(this.server.isConnected(), true) | ||
assert.strictEqual(this.client.isConnected(), true) | ||
|
||
this.server.registerDataHandler((ilp) => { | ||
assert.deepEqual(ilp, Buffer.from('foo')) | ||
return Buffer.from('bar') | ||
}) | ||
|
||
const response = await this.client.sendData(Buffer.from('foo')) | ||
assert.deepEqual(response, Buffer.from('bar')) | ||
await this.server.disconnect() | ||
ws.close() | ||
resolve() | ||
}) | ||
|
||
clientConnect = this.client.connect() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('alternate client account/token config', function () { | ||
beforeEach(async function () { | ||
this.server = new Plugin(this.serverOpts) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for this extra layer of indirection?
Also, from the option alone it isn't clear whether the created plugin will act as a client or server. Maybe it should be
server_socket
(in the top-level options), orlistener.socket
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion. I think
listener.socket
makes the most sense.The plugin is not going to act as a WS server because it is created around an existing ws socket so it's not listening for new connections.
BUT it will handle incoming auth messages (i.e. it's a "BTP server" in as much as the difference between a client and server is that one performs auth with the other even though they may actually be peers)
This is a general problem (I think) with the architecture of the plugin in that it mixes the transport (websockets) with the protocol (BTP).
I tried a more decoupled approach with
ilp-transport
, interested to hear what you think of that and if we could move to something similar with BTP: https://github.com/adrianhopebailie/ilp-transport