From e4aa5b7bac1ba155767dae0ff622701bb9ce1ab7 Mon Sep 17 00:00:00 2001 From: finn Date: Tue, 5 Sep 2023 15:43:55 -0700 Subject: [PATCH] initial connect implementation with a dumb local disk kvstore --- src/json-rpc-api.ts | 12 ++++++ src/json-rpc-handlers/connect/connect.ts | 52 ++++++++++++++++++++++++ src/json-rpc-handlers/connect/index.ts | 1 + src/json-rpc-handlers/connect/storage.ts | 28 +++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/json-rpc-handlers/connect/connect.ts create mode 100644 src/json-rpc-handlers/connect/index.ts create mode 100644 src/json-rpc-handlers/connect/storage.ts diff --git a/src/json-rpc-api.ts b/src/json-rpc-api.ts index d1f2297..51d55a4 100644 --- a/src/json-rpc-api.ts +++ b/src/json-rpc-api.ts @@ -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); diff --git a/src/json-rpc-handlers/connect/connect.ts b/src/json-rpc-handlers/connect/connect.ts new file mode 100644 index 0000000..88ef323 --- /dev/null +++ b/src/json-rpc-handlers/connect/connect.ts @@ -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 => { + 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 => { + 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 => { + 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 => { + const { id } = req.params; + const message = await store.get('grant-' + id); + + const resp: HandlerResponse = { + jsonRpcResponse: createJsonRpcSuccessResponse(req.id || uuidv4(), message), + }; + + return resp; +}; \ No newline at end of file diff --git a/src/json-rpc-handlers/connect/index.ts b/src/json-rpc-handlers/connect/index.ts new file mode 100644 index 0000000..7fba05e --- /dev/null +++ b/src/json-rpc-handlers/connect/index.ts @@ -0,0 +1 @@ +export * from './connect.js'; \ No newline at end of file diff --git a/src/json-rpc-handlers/connect/storage.ts b/src/json-rpc-handlers/connect/storage.ts new file mode 100644 index 0000000..2623fbe --- /dev/null +++ b/src/json-rpc-handlers/connect/storage.ts @@ -0,0 +1,28 @@ +import fs from 'fs/promises'; +import { existsSync, mkdirSync } from 'fs'; + +export interface KVStore { + set(key: string, value: string): Promise + get(key: string): Promise +} + +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 { + await fs.writeFile(this.path + '/' + key, value); + } + + async get(key: string): Promise { + const value = await fs.readFile(this.path + '/' + key); + return value.toString(); + } +} \ No newline at end of file