-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
nodes connections
command to list active NodeConnection
s
- Loading branch information
1 parent
ab8d759
commit ff2d436
Showing
13 changed files
with
622 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type PolykeyClient from '../../PolykeyClient'; | ||
import type nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb'; | ||
import CommandPolykey from '../CommandPolykey'; | ||
import * as binUtils from '../utils/utils'; | ||
import * as binProcessors from '../utils/processors'; | ||
|
||
class CommandAdd extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('connections'); | ||
this.description('list all active node connections'); | ||
this.action(async (options) => { | ||
const { default: PolykeyClient } = await import('../../PolykeyClient'); | ||
const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb'); | ||
const clientOptions = await binProcessors.processClientOptions( | ||
options.nodePath, | ||
options.nodeId, | ||
options.clientHost, | ||
options.clientPort, | ||
this.fs, | ||
this.logger.getChild(binProcessors.processClientOptions.name), | ||
); | ||
const meta = await binProcessors.processAuthentication( | ||
options.passwordFile, | ||
this.fs, | ||
); | ||
let pkClient: PolykeyClient; | ||
this.exitHandlers.handlers.push(async () => { | ||
if (pkClient != null) await pkClient.stop(); | ||
}); | ||
try { | ||
pkClient = await PolykeyClient.createPolykeyClient({ | ||
nodePath: options.nodePath, | ||
nodeId: clientOptions.nodeId, | ||
host: clientOptions.clientHost, | ||
port: clientOptions.clientPort, | ||
logger: this.logger.getChild(PolykeyClient.name), | ||
}); | ||
// DO things here... | ||
// Like create the message. | ||
const emptyMessage = new utilsPB.EmptyMessage(); | ||
|
||
const connections = await binUtils.retryAuthentication(async (auth) => { | ||
const connections = pkClient.grpcClient.nodesListConnections( | ||
emptyMessage, | ||
auth, | ||
); | ||
const connectionEntries: Array<nodesPB.NodeConnection.AsObject> = []; | ||
for await (const connection of connections) { | ||
connectionEntries.push(connection.toObject()); | ||
} | ||
return connectionEntries; | ||
}, meta); | ||
if (options.format === 'human') { | ||
const output: Array<string> = []; | ||
for (const connection of connections) { | ||
const hostnameString = | ||
connection.hostname === '' ? '' : `(${connection.hostname})`; | ||
const hostString = `${connection.nodeId}@${connection.host}${hostnameString}:${connection.port}`; | ||
const usageCount = connection.usageCount; | ||
const timeout = | ||
connection.timeout === -1 ? 'NA' : `${connection.timeout}`; | ||
const outputLine = `${hostString}\t${usageCount}\t${timeout}`; | ||
output.push(outputLine); | ||
} | ||
process.stdout.write( | ||
binUtils.outputFormatter({ | ||
type: 'list', | ||
data: output, | ||
}), | ||
); | ||
} else { | ||
process.stdout.write( | ||
binUtils.outputFormatter({ | ||
type: 'json', | ||
data: connections, | ||
}), | ||
); | ||
} | ||
} finally { | ||
if (pkClient! != null) await pkClient.stop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default CommandAdd; |
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
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,52 @@ | ||
import type * as grpc from '@grpc/grpc-js'; | ||
import type { Authenticate } from '../types'; | ||
import type * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; | ||
import type Logger from '@matrixai/logger'; | ||
import type NodeConnectionManager from '../../nodes/NodeConnectionManager'; | ||
import * as grpcUtils from '../../grpc/utils'; | ||
import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb'; | ||
import * as clientUtils from '../utils'; | ||
import * as nodesUtils from '../../nodes/utils'; | ||
|
||
function nodesListConnections({ | ||
authenticate, | ||
nodeConnectionManager, | ||
logger, | ||
}: { | ||
authenticate: Authenticate; | ||
nodeConnectionManager: NodeConnectionManager; | ||
logger: Logger; | ||
}) { | ||
return async ( | ||
call: grpc.ServerWritableStream< | ||
utilsPB.EmptyMessage, | ||
nodesPB.NodeConnection | ||
>, | ||
): Promise<void> => { | ||
const genWritable = grpcUtils.generatorWritable(call, false); | ||
try { | ||
const metadata = await authenticate(call.metadata); | ||
call.sendMetadata(metadata); | ||
const connections = nodeConnectionManager.listConnections(); | ||
for (const connection of connections) { | ||
const connectionMessage = new nodesPB.NodeConnection(); | ||
connectionMessage.setNodeId(nodesUtils.encodeNodeId(connection.nodeId)); | ||
connectionMessage.setHost(connection.address.host); | ||
connectionMessage.setHostname(connection.address.hostname ?? ''); | ||
connectionMessage.setPort(connection.address.port); | ||
connectionMessage.setUsageCount(connection.usageCount); | ||
connectionMessage.setTimeout(connection.timeout ?? -1); | ||
await genWritable.next(connectionMessage); | ||
} | ||
await genWritable.next(null); | ||
return; | ||
} catch (e) { | ||
await genWritable.throw(e); | ||
!clientUtils.isClientClientError(e) && | ||
logger.error(`${nodesListConnections.name}:${e}`); | ||
return; | ||
} | ||
}; | ||
} | ||
|
||
export default nodesListConnections; |
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.