-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CommandResponseStream to wrap tcp socket, add parallel call…
… safetiness
- Loading branch information
Showing
15 changed files
with
460 additions
and
74 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
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,35 +1,25 @@ | ||
|
||
import { createConnection, Socket, TcpSocketConnectOpts } from 'node:net'; | ||
import type { CommandResponse, Client } from './client.type.js'; | ||
import { wrapSocket, sendCommandWithResponse } from './client.utils.js'; | ||
import { createConnection, TcpSocketConnectOpts } from 'node:net'; | ||
import type { CommandResponse, RawClient } from './client.type.js'; | ||
import { wrapSocket, CommandResponseStream } from './client.utils.js'; | ||
|
||
|
||
export const createTcpSocket = (options: TcpSocketConnectOpts): Promise<Socket> => { | ||
const socket = createConnection(options); | ||
return wrapSocket(socket); | ||
}; | ||
export const createTcpSocket = | ||
(options: TcpSocketConnectOpts): Promise<CommandResponseStream> => { | ||
const socket = createConnection(options); | ||
return wrapSocket(socket); | ||
}; | ||
|
||
|
||
export type TcpOption = TcpSocketConnectOpts; | ||
|
||
export const TcpClient = ({ host, port, keepAlive = true }: TcpOption): Client => { | ||
let socket: Socket; | ||
export const TcpClient = ({ host, port, keepAlive = true }: TcpOption): RawClient => { | ||
let socket: CommandResponseStream; | ||
return { | ||
sendCommand: async (code: number, payload: Buffer):Promise<CommandResponse> => { | ||
if (!socket) | ||
socket = await createTcpSocket({host, port, keepAlive}); | ||
return sendCommandWithResponse(socket)(code, payload); | ||
return socket.sendCommand(code, payload); | ||
} | ||
} | ||
}; | ||
|
||
// export const createTransport = (options: TcpOption): Client => { | ||
// let socket: Socket; | ||
// return { | ||
// sendCommand: async (code: number, payload: Buffer):Promise<CommandResponse> => { | ||
// if (!socket) | ||
// socket = await createTcpSocket(options); | ||
// return sendCommandWithResponse(socket)(code, payload); | ||
// } | ||
// } | ||
// }; |
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,41 @@ | ||
|
||
import { TcpClient } from './client/tcp.client.js'; | ||
import { login } from './wire/session/login.command.js'; | ||
import { logout } from './wire/session/logout.command.js'; | ||
import { getStreams } from './wire/stream/get-streams.command.js'; | ||
import { getUsers } from './wire/user/get-users.command.js'; | ||
|
||
try { | ||
// create socket | ||
const s = TcpClient({ host: '127.0.0.1', port: 8090 }); | ||
|
||
// LOGIN | ||
const r = await login(s)({ username: 'iggy', password: 'iggy' }); | ||
console.log('RESPONSE_login', r); | ||
|
||
const resp = await Promise.all([ | ||
getUsers(s)(), | ||
getStreams(s)(), | ||
getUsers(s)(), | ||
getStreams(s)(), | ||
getUsers(s)(), | ||
getStreams(s)(), | ||
getUsers(s)(), | ||
getStreams(s)(), | ||
]) | ||
|
||
console.log('RESP', resp); | ||
|
||
console.log('GETUSERS', await getUsers(s)()); | ||
console.log('GETSTREAM', await getStreams(s)()); | ||
|
||
// LOGOUT | ||
const rOut = await logout(s)(); | ||
console.log('RESPONSE LOGOUT', rOut); | ||
|
||
|
||
} catch (err) { | ||
console.error('FAILED!', err); | ||
} | ||
|
||
process.exit(0); |
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,37 @@ | ||
|
||
import { createTcpSocket } from './client/tcp.client.js'; | ||
import { handleResponse } from './client/client.utils.js'; | ||
import { LOGIN } from './wire/session/login.command.js'; | ||
import { logout } from './wire/session/logout.command.js'; | ||
import { GET_STREAMS } from './wire/stream/get-streams.command.js'; | ||
import { GET_USERS } from './wire/user/get-users.command.js'; | ||
|
||
try { | ||
// create socket | ||
const s = await createTcpSocket({ host: '127.0.0.1', port: 8090 }); | ||
|
||
// LOGIN | ||
const log = LOGIN.serialize({ username: 'iggy', password: 'iggy' }); | ||
const logr = await s.sendCommand(LOGIN.code, log); | ||
const r = LOGIN.deserialize(logr); | ||
console.log('RESPONSE_login', r); | ||
|
||
s.on('data', (d) => console.log('=>>DATA!!', d, d.length, handleResponse(d))); | ||
s.on('error', (err) => console.error('=>>SOCKET ERROR!!', err)); | ||
|
||
// TLDR: this is not officialy supported (but somehow works here) | ||
console.log('==> socket write CMD1', | ||
s.writeCommand(GET_USERS.code, GET_USERS.serialize())); | ||
console.log('==> socket write CMD2', | ||
s.writeCommand(GET_STREAMS.code, GET_STREAMS.serialize())); | ||
|
||
// LOGOUT | ||
const rOut = await logout(s)(); | ||
console.log('RESPONSE LOGOUT', rOut); | ||
|
||
|
||
} catch (err) { | ||
console.error('FAILED!', err); | ||
} | ||
|
||
process.exit(0); |
Oops, something went wrong.