Skip to content

Commit

Permalink
initial connect implementation with a dumb local disk kvstore
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block committed Oct 6, 2023
1 parent ac78e39 commit e4aa5b7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/json-rpc-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { handleDwnProcessMessage } from './json-rpc-handlers/dwn/index.js';
import {
handleConnectCreateGrant,
handleConnectCreateRequest,
handleConnectGetGrant,
handleConnectGetRequest,
} from './json-rpc-handlers/connect/index.js';

import { JsonRpcRouter } from './lib/json-rpc-router.js';

export const jsonRpcApi = new JsonRpcRouter();

jsonRpcApi.on('dwn.processMessage', handleDwnProcessMessage);

jsonRpcApi.on('connect.createRequest', handleConnectCreateRequest);
jsonRpcApi.on('connect.getRequest', handleConnectGetRequest);
jsonRpcApi.on('connect.createGrant', handleConnectCreateGrant);
jsonRpcApi.on('connect.getGrant', handleConnectGetGrant);
52 changes: 52 additions & 0 deletions src/json-rpc-handlers/connect/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { JsonRpcRequest } from '../../lib/json-rpc.js';
import type { HandlerResponse, JsonRpcHandler } from '../../lib/json-rpc-router.js';
import { type KVStore, LocalDiskStore } from './storage.js';

import { createJsonRpcSuccessResponse } from '../../lib/json-rpc.js';
import { v4 as uuidv4 } from 'uuid';

const store: KVStore = new LocalDiskStore('./data/connect');

export const handleConnectCreateRequest: JsonRpcHandler = async (req: JsonRpcRequest): Promise<HandlerResponse> => {
const { message, id } = req.params;
await store.set('request-' + id, message);

const resp: HandlerResponse = {
jsonRpcResponse: createJsonRpcSuccessResponse(req.id || uuidv4(), true),
};

return resp;
};

export const handleConnectGetRequest: JsonRpcHandler = async (req: JsonRpcRequest): Promise<HandlerResponse> => {
const { id } = req.params;
const message = await store.get('request-' + id);

const resp: HandlerResponse = {
jsonRpcResponse: createJsonRpcSuccessResponse(req.id || uuidv4(), message),
};

return resp;
};

export const handleConnectCreateGrant: JsonRpcHandler = async (req: JsonRpcRequest): Promise<HandlerResponse> => {
const { message, id } = req.params;
await store.set('grant-' + id, message);

const resp: HandlerResponse = {
jsonRpcResponse: createJsonRpcSuccessResponse(req.id || uuidv4(), true),
};

return resp;
};

export const handleConnectGetGrant: JsonRpcHandler = async (req: JsonRpcRequest): Promise<HandlerResponse> => {
const { id } = req.params;
const message = await store.get('grant-' + id);

const resp: HandlerResponse = {
jsonRpcResponse: createJsonRpcSuccessResponse(req.id || uuidv4(), message),
};

return resp;
};
1 change: 1 addition & 0 deletions src/json-rpc-handlers/connect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './connect.js';
28 changes: 28 additions & 0 deletions src/json-rpc-handlers/connect/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs/promises';
import { existsSync, mkdirSync } from 'fs';

export interface KVStore {
set(key: string, value: string): Promise<void>
get(key: string): Promise<string>
}

export class LocalDiskStore {
private path: string;

constructor(path: string) {
this.path = path;

if (!existsSync(path)) {
mkdirSync(this.path, { recursive: true });
}
}

async set(key: string, value: string): Promise<void> {
await fs.writeFile(this.path + '/' + key, value);
}

async get(key: string): Promise<string> {
const value = await fs.readFile(this.path + '/' + key);
return value.toString();
}
}

0 comments on commit e4aa5b7

Please sign in to comment.