-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial connect implementation with a dumb local disk kvstore
- Loading branch information
1 parent
ac78e39
commit e4aa5b7
Showing
4 changed files
with
93 additions
and
0 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 |
---|---|---|
@@ -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); |
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 { 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; | ||
}; |
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 @@ | ||
export * from './connect.js'; |
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,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(); | ||
} | ||
} |