Skip to content

Commit

Permalink
fixes for #342
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Feb 28, 2022
1 parent 0f03127 commit 81cffaa
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/agent/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorPolykey } from '../errors';
import { ErrorPolykey, sysexits } from '../errors';

class ErrorAgent extends ErrorPolykey {}

Expand All @@ -8,9 +8,15 @@ class ErrorAgentClientNotStarted extends ErrorAgent {}

class ErrorAgentClientDestroyed extends ErrorAgent {}

class ErrorConnectionInfoMissing extends ErrorAgent {
description = 'Vault already exists';
exitCode = sysexits.UNAVAILABLE;
}

export {
ErrorAgent,
ErrorAgentClientNotStarted,
ErrorAgentRunning,
ErrorAgentClientDestroyed,
ErrorConnectionInfoMissing,
};
10 changes: 3 additions & 7 deletions src/agent/service/echo.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type * as grpc from '@grpc/grpc-js';
import type { ConnectionInfoGetter } from 'agent/types';
import type { ConnectionInfoGet } from 'agent/types';
import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';

function echo({
connectionInfoGetter,
}: {
connectionInfoGetter: ConnectionInfoGetter;
}) {
function echo({ connectionInfoGet }: { connectionInfoGet: ConnectionInfoGet }) {
return async (
call: grpc.ServerUnaryCall<utilsPB.EchoMessage, utilsPB.EchoMessage>,
callback: grpc.sendUnaryData<utilsPB.EchoMessage>,
): Promise<void> => {
connectionInfoGetter(call.getPeer());
connectionInfoGet(call);
const response = new utilsPB.EchoMessage();
response.setChallenge(call.request.getChallenge());
callback(null, response);
Expand Down
6 changes: 2 additions & 4 deletions src/agent/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ function createService(container: {
gestaltGraph: GestaltGraph;
revProxy: ReverseProxy;
}): IAgentServiceServer {
const connectionInfoGetter = agentUtils.connectionInfoGetter(
container.revProxy,
);
const connectionInfoGet = agentUtils.connectionInfoGetter(container.revProxy);
const container_ = {
...container,
connectionInfoGetter,
connectionInfoGet: connectionInfoGet,
};
const service: IAgentServiceServer = {
echo: echo(container_),
Expand Down
7 changes: 5 additions & 2 deletions src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { ConnectionInfo } from 'network/types';
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';

type ConnectionInfoGetter = (peerInfo: string) => ConnectionInfo | undefined;
type ConnectionInfoGet = (
call: ServerSurfaceCall,
) => ConnectionInfo | undefined;

export type { ConnectionInfoGetter };
export type { ConnectionInfoGet };
20 changes: 11 additions & 9 deletions src/agent/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { Host, Port } from 'network/types';
import type ReverseProxy from 'network/ReverseProxy';
import type { ConnectionInfoGetter } from './types';
import type { ConnectionInfoGet } from './types';
import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call';

function connectionInfoGetter(revProxy: ReverseProxy): ConnectionInfoGetter {
return (peerInfo: string) => {
const address = peerInfo.split(':');
const host = address[0] as Host;
const port = parseInt(address[1]) as Port;
// Return undefined in input was invalid
if (host == null || isNaN(port)) return;
return revProxy.getConnectionInfoByProxy(host, port);
function connectionInfoGetter(revProxy: ReverseProxy): ConnectionInfoGet {
return (call: ServerSurfaceCall) => {
let urlString = call.getPeer();
if (!/^.*:\/\//.test(urlString)) urlString = 'pk://' + urlString;
const url = new URL(urlString);
return revProxy.getConnectionInfoByProxy(
url.hostname as Host,
parseInt(url.port) as Port,
);
};
}

Expand Down

0 comments on commit 81cffaa

Please sign in to comment.