This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from udevbe/split_connection
make connection setup generic so we can call it in a browser
- Loading branch information
Showing
6 changed files
with
1,354 additions
and
1,521 deletions.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
connectGeneric, | ||
SetupConnection, | ||
XConnection, | ||
XConnectionOptions, | ||
XConnectionSocket | ||
} from './connection' | ||
import { Setup } from './xproto' | ||
|
||
function isSetup(setup: any): setup is Setup { | ||
// TODO we could check all Setup attributes but it would be a long list... | ||
return setup.rootsLen !== undefined | ||
&& setup.protocolMinorVersion !== undefined | ||
&& setup.protocolMajorVersion !== undefined | ||
&& setup.length !== undefined | ||
} | ||
|
||
function createXConnectionSocket(webSocket: WebSocket): XConnectionSocket { | ||
webSocket.binaryType = 'arraybuffer' | ||
const xConnectionSocket = { | ||
close() { | ||
webSocket.close() | ||
}, | ||
write(data: Uint8Array) { | ||
webSocket.send(data) | ||
} | ||
} | ||
webSocket.onmessage = ev => xConnectionSocket?.write(ev.data) | ||
webSocket.onerror = ev => { | ||
xConnectionSocket.close() | ||
console.error('XConnection is in error: ' + ev) | ||
} | ||
return xConnectionSocket | ||
} | ||
|
||
async function auth(webSocket: WebSocket, options?: XConnectionOptions) { | ||
return new Promise<Setup>((resolve, reject) => { | ||
webSocket.onmessage = ev => { | ||
const message = JSON.parse(ev.data) | ||
if (message?.reply === 'connectAck' && isSetup(message?.replyArgs)) { | ||
resolve(message.replyArgs) | ||
} else { | ||
reject('Expected connectAck, got: ' + ev) | ||
} | ||
} | ||
webSocket.onerror = ev => { | ||
reject('WebSocket is in error: ' + ev) | ||
} | ||
webSocket.send(JSON.stringify({ | ||
request: 'connect', | ||
requestArgs: [options] | ||
})) | ||
}) | ||
} | ||
|
||
export async function connect(webSocket: WebSocket, options?: XConnectionOptions): Promise<XConnection> { | ||
const connectionSetup: SetupConnection = async () => { | ||
const setup = await auth(webSocket, options) | ||
const xConnectionSocket = createXConnectionSocket(webSocket) | ||
return { setup, xConnectionSocket } | ||
} | ||
|
||
return connectGeneric(connectionSetup) | ||
} |
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
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
Oops, something went wrong.