Skip to content

Commit

Permalink
feat: updating metadata wrapper type name
Browse files Browse the repository at this point in the history
Related #500
Related #501
Related #502

[ci skip]
  • Loading branch information
tegefaulkes committed Feb 14, 2023
1 parent 427adac commit 632281c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/clientRPC/handlers/agentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type KeyRing from '../../keys/KeyRing';
import type CertManager from '../../keys/CertManager';
import type Logger from '@matrixai/logger';
import type { NodeIdEncoded } from '../../ids';
import type { WithMetadata } from '../types';
import type { RPCRequestParams, RPCResponseResult } from '../types';
import * as nodesUtils from '../../nodes/utils';
import * as keysUtils from '../../keys/utils';
import { UnaryHandler } from '../../RPC/handlers';
Expand All @@ -15,8 +15,8 @@ type StatusResult = {
};

const agentStatusCaller = new UnaryCaller<
WithMetadata,
WithMetadata<StatusResult>
RPCRequestParams,
RPCResponseResult<StatusResult>
>();

class AgentStatusHandler extends UnaryHandler<
Expand All @@ -25,10 +25,10 @@ class AgentStatusHandler extends UnaryHandler<
certManager: CertManager;
logger: Logger;
},
WithMetadata,
WithMetadata<StatusResult>
RPCRequestParams,
RPCResponseResult<StatusResult>
> {
public async handle(): Promise<WithMetadata<StatusResult>> {
public async handle(): Promise<RPCResponseResult<StatusResult>> {
return {
pid: process.pid,
nodeId: nodesUtils.encodeNodeId(this.container.keyRing.getNodeId()),
Expand Down
13 changes: 8 additions & 5 deletions src/clientRPC/handlers/agentUnlock.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type Logger from '@matrixai/logger';
import type { WithMetadata } from '../types';
import type { RPCRequestParams, RPCResponseResult } from '../types';
import { UnaryHandler } from '../../RPC/handlers';
import { UnaryCaller } from '../../RPC/callers';

const agentUnlockCaller = new UnaryCaller<WithMetadata, WithMetadata>();
const agentUnlockCaller = new UnaryCaller<
RPCRequestParams,
RPCResponseResult
>();

class AgentUnlockHandler extends UnaryHandler<
{ logger: Logger },
WithMetadata,
WithMetadata
RPCRequestParams,
RPCResponseResult
> {
public async handle(): Promise<WithMetadata> {
public async handle(): Promise<RPCResponseResult> {
// This is a NOP handler,
// authentication and unlocking is handled via middleware.
// Failure to authenticate will be an error from the middleware layer.
Expand Down
10 changes: 8 additions & 2 deletions src/clientRPC/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import type { JSONValue } from '../types';
// eslint-disable-next-line
type NoData = {};

type WithMetadata<T extends Record<string, JSONValue> = NoData> = {
type RPCRequestParams<T extends Record<string, JSONValue> = NoData> = {
metadata?: {
[Key: string]: JSONValue;
} & Partial<{ Authorization: string }>;
} & Omit<T, 'metadata'>;

export type { WithMetadata, NoData };
type RPCResponseResult<T extends Record<string, JSONValue> = NoData> = {
metadata?: {
[Key: string]: JSONValue;
} & Partial<{ Authorization: string }>;
} & Omit<T, 'metadata'>;

export type { RPCRequestParams, RPCResponseResult, NoData };
34 changes: 17 additions & 17 deletions src/clientRPC/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SessionToken } from '../sessions/types';
import type KeyRing from '../keys/KeyRing';
import type SessionManager from '../sessions/SessionManager';
import type { Session } from '../sessions';
import type { WithMetadata } from './types';
import type { RPCResponseResult, RPCRequestParams } from './types';
import type {
JsonRpcRequest,
JsonRpcResponse,
Expand All @@ -25,7 +25,7 @@ import { promise } from '../utils';
async function authenticate(
sessionManager: SessionManager,
keyRing: KeyRing,
message: JsonRpcRequest<WithMetadata>,
message: JsonRpcRequest<RPCRequestParams>,
) {
if (message.params == null) throw new clientErrors.ErrorClientAuthMissing();
if (message.params.metadata == null) {
Expand Down Expand Up @@ -58,7 +58,7 @@ async function authenticate(
return `Bearer ${token}`;
}

function decodeAuth(messageParams: WithMetadata) {
function decodeAuth(messageParams: RPCRequestParams) {
const auth = messageParams.metadata?.Authorization;
if (auth == null || !auth.startsWith('Bearer ')) {
return;
Expand All @@ -75,19 +75,19 @@ function authenticationMiddlewareServer(
sessionManager: SessionManager,
keyRing: KeyRing,
): MiddlewareFactory<
JsonRpcRequest<WithMetadata>,
JsonRpcRequest<WithMetadata>,
JsonRpcResponse<WithMetadata>,
JsonRpcResponse<WithMetadata>
JsonRpcRequest<RPCRequestParams>,
JsonRpcRequest<RPCRequestParams>,
JsonRpcResponse<RPCResponseResult>,
JsonRpcResponse<RPCResponseResult>
> {
return () => {
let forwardFirst = true;
let reverseController;
let outgoingToken: string | null = null;
return {
forward: new TransformStream<
JsonRpcRequest<WithMetadata>,
JsonRpcRequest<WithMetadata>
JsonRpcRequest<RPCRequestParams>,
JsonRpcRequest<RPCRequestParams>
>({
transform: async (chunk, controller) => {
if (forwardFirst) {
Expand Down Expand Up @@ -132,17 +132,17 @@ function authenticationMiddlewareServer(
function authenticationMiddlewareClient(
session: Session,
): MiddlewareFactory<
JsonRpcRequest<WithMetadata>,
JsonRpcRequest<WithMetadata>,
JsonRpcResponse<WithMetadata>,
JsonRpcResponse<WithMetadata>
JsonRpcRequest<RPCRequestParams>,
JsonRpcRequest<RPCRequestParams>,
JsonRpcResponse<RPCResponseResult>,
JsonRpcResponse<RPCResponseResult>
> {
return () => {
let forwardFirst = true;
return {
forward: new TransformStream<
JsonRpcRequest<WithMetadata>,
JsonRpcRequest<WithMetadata>
JsonRpcRequest<RPCRequestParams>,
JsonRpcRequest<RPCRequestParams>
>({
transform: async (chunk, controller) => {
if (forwardFirst) {
Expand All @@ -164,8 +164,8 @@ function authenticationMiddlewareClient(
},
}),
reverse: new TransformStream<
JsonRpcResponse<WithMetadata>,
JsonRpcResponse<WithMetadata>
JsonRpcResponse<RPCResponseResult>,
JsonRpcResponse<RPCResponseResult>
>({
transform: async (chunk, controller) => {
controller.enqueue(chunk);
Expand Down

0 comments on commit 632281c

Please sign in to comment.