Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis server runtime #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/rolodex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'node:fs/promises';
import { access, constants } from 'node:fs/promises';
import morgan from 'morgan';
import * as Sentry from "@sentry/node";
import { createClient } from 'redis';

if (process.env.VITE_SENTRY_DSN) {
Sentry.init({
Expand All @@ -26,6 +27,9 @@ const __dirname = path.dirname(__filename);

const NETWORK = process.env.VITE_NETWORK || "DEVNET"
const RUN_COMMUNITY_AGENT = process.env.VITE_RUN_COMMUNITY_AGENT || true
const redisURL = process.env.REDIS_URL || "redis://localhost:6379"
const redisClient = createClient({ url: redisURL });
await redisClient.connect();

const configDir = process.env.CONFIG_HOME || __dirname
const homeDir = process.env.PROTOCOL_DB_HOME || path.join(__dirname, 'protocol_db')
Expand Down Expand Up @@ -84,7 +88,7 @@ if (RUN_COMMUNITY_AGENT == true) {
let communityAccountDID = config.SHOVEL_ACCOUNT_DID

// create runtime
const communityRuntime = new Runtime(SERVER_RUNTIME, config)
const communityRuntime = new Runtime(SERVER_RUNTIME, config,redisClient)
var communityAgent = new Agent(helia, connection[NETWORK].sync_host, connection[NETWORK].dial_prefix, communityRuntime)
communityAgent = Object.assign(communityAgent, MessageCapability);
communityAgent = Object.assign(communityAgent, StorageCapability);
Expand Down
3 changes: 2 additions & 1 deletion apps/rolodex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"viem": "^2.8.6",
"vite": "^4.4.9",
"vite-plugin-wasm": "^3.3.0",
"buffer": "^6.0.3"
"buffer": "^6.0.3",
"redis": "^4.6.12"
},
"devDependencies": {
"@tauri-apps/cli": "^1.5.11",
Expand Down
69 changes: 52 additions & 17 deletions packages/account-fs/agent/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import { spki } from 'iso-signatures/spki'
import { CID } from 'multiformats/cid'
import * as uint8arrays from 'uint8arrays';
import localforage from "localforage";
import { createClient } from 'redis';

const SHOVEL_FS_ACCESS_KEY = "SHOVEL_FS_ACCESS_KEY"
const SHOVEL_FS_FOREST_CID = "SHOVEL_FS_FOREST_CID"
const SHOVEL_AGENT_WRITE_KEYPAIR = "SHOVEL_AGENT_WRITE_KEYPAIR"
const SHOVEL_AGENT_DID = "SHOVEL_AGENT_DID"

export const BROWSER_RUNTIME=1
export const SERVER_RUNTIME=2
// localforage vs config json, Unknown device-linking/Agent add - assuming config file as given

export class Runtime {
constructor(type, config) {
constructor(type, config, redisClient = null) {
this.type = type
this.config = config
if(this.type === SERVER_RUNTIME) {
this.redisClient = redisClient
}
}

// Read config from file
Expand Down Expand Up @@ -65,15 +70,31 @@ export class Runtime {
case BROWSER_RUNTIME:
return await localforage.getItem(key)
case SERVER_RUNTIME:
if (key == SHOVEL_FS_ACCESS_KEY) {
// convert string to Uint8array
return uint8arrays.fromString(this.config[key], 'base64url')
} else if (key == SHOVEL_FS_FOREST_CID) {
//convert string to Uinst8array
return CID.parse(this.config[key]).bytes
if(this.redisClient) {
const fullKey = `agent:${this.config[SHOVEL_AGENT_DID]}:${key}`
let value = await this.redisClient.get(fullKey);
if (key == SHOVEL_FS_ACCESS_KEY) {
// convert string to Uint8array
return uint8arrays.fromString(value, 'base64url')
} else if (key == SHOVEL_FS_FOREST_CID) {
//convert string to Uinst8array
return CID.parse(value).bytes
}
else {
return value
}
}
else {
return this.config[key]
if (key == SHOVEL_FS_ACCESS_KEY) {
// convert string to Uint8array
return uint8arrays.fromString(this.config[key], 'base64url')
} else if (key == SHOVEL_FS_FOREST_CID) {
//convert string to Uinst8array
return CID.parse(this.config[key]).bytes
}
else {
return this.config[key]
}
}
default:
throw "InvalidRuntime from getItem"
Expand All @@ -85,17 +106,31 @@ export class Runtime {
case BROWSER_RUNTIME:
return await localforage.setItem(key, value)
case SERVER_RUNTIME:
//convert uint8arrays to string before save to file
//TODO: Make the config persist on the config file
if (key == SHOVEL_FS_ACCESS_KEY) {
var valueString = uint8arrays.toString(value, 'base64url')
return this.config[key] = valueString
} else if (key == SHOVEL_FS_FOREST_CID) {
var valueString = CID.decode(value).toString()
return this.config[key] = valueString
if(this.redisClient) {
const fullKey = `agent:${this.config[SHOVEL_AGENT_DID]}:${key}`
//convert uint8arrays to string before save to file
let valueString;
if (key == SHOVEL_FS_ACCESS_KEY) {
valueString = uint8arrays.toString(value, 'base64url')
} else if (key == SHOVEL_FS_FOREST_CID) {
valueString = CID.decode(value).toString()
}
else {
valueString = value
}
return await this.redisClient.set(fullKey, valueString);
}
else {
return this.config[key] = value
if (key == SHOVEL_FS_ACCESS_KEY) {
var valueString = uint8arrays.toString(value, 'base64url')
return this.config[key] = valueString
} else if (key == SHOVEL_FS_FOREST_CID) {
var valueString = CID.decode(value).toString()
return this.config[key] = valueString
}
else {
return this.config[key] = value
}
}
default:
throw "InvalidRuntime from setItem"
Expand Down