-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from project-kardeshev/fix-logger
feat(kv store): Implement kv store and kv registry
- Loading branch information
Showing
11 changed files
with
565 additions
and
47 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
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
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
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
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,2 @@ | ||
export * from './kv/registry.js'; | ||
export * from './kv/store.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,123 @@ | ||
import { AoSigner, AoWriteOptions } from '../../../../types/ao.js'; | ||
import { findMessageByTag } from '../../../../utils/ao.js'; | ||
import { safeDecode } from '../../../../utils/json.js'; | ||
import { Process, ProcessReadable, ProcessWritable } from '../../process.js'; | ||
|
||
export interface KVRegistryReadable { | ||
getKVStores( | ||
{ | ||
user, | ||
}: { | ||
user: string; | ||
}, | ||
options?: Omit<AoWriteOptions, 'target'>, | ||
): Promise<{ Owned: string[]; Controlled: [] }>; | ||
} | ||
|
||
export interface KVRegistryWritable { | ||
spawnKVStore( | ||
{ name }: { name?: string }, | ||
options?: Omit<AoWriteOptions, 'target'>, | ||
): Promise<string>; | ||
} | ||
|
||
export class KVRegistryProcessReadable implements KVRegistryReadable { | ||
readonly process: ProcessReadable; | ||
|
||
constructor({ | ||
process, | ||
processId, | ||
}: { | ||
process?: ProcessReadable; | ||
processId?: string; | ||
} = {}) { | ||
if (!process && !processId) { | ||
throw new Error('Either process or processId should be provided'); | ||
} | ||
this.process = | ||
process ?? | ||
(Process.createRemoteProcess({ | ||
processId: processId!, | ||
}) as ProcessReadable); | ||
} | ||
|
||
async getKVStores( | ||
{ | ||
user, | ||
}: { | ||
user: string; | ||
}, | ||
options?: Omit<AoWriteOptions, 'target'>, | ||
): Promise<{ Owned: string[]; Controlled: [] }> { | ||
const res = await this.process.read({ | ||
data: options?.data, | ||
tags: [ | ||
{ | ||
name: 'Action', | ||
value: 'KV-Registry.Get-KV-Stores', | ||
}, | ||
{ | ||
name: 'Address', | ||
value: user, | ||
}, | ||
...(options?.tags ?? []), | ||
], | ||
}); | ||
const message = findMessageByTag({ | ||
messages: res.Messages, | ||
name: 'Action', | ||
value: 'KV-Registry.Get-KV-Stores', | ||
}); | ||
return message?.Data !== undefined | ||
? safeDecode(message.Data) | ||
: { Owned: [], Controlled: [] }; | ||
} | ||
} | ||
|
||
export class KVRegistryProcessWritable | ||
extends KVRegistryProcessReadable | ||
implements KVRegistryWritable | ||
{ | ||
readonly process: ProcessWritable; | ||
|
||
constructor({ | ||
signer, | ||
processId, | ||
process = Process.createRemoteProcess({ | ||
signer, | ||
processId, | ||
}) as ProcessWritable, | ||
}: { | ||
signer: AoSigner; | ||
processId: string; | ||
process?: ProcessWritable; | ||
}) { | ||
super({ process }); | ||
this.process = process; | ||
} | ||
|
||
async spawnKVStore( | ||
{ name }: { name?: string | undefined }, | ||
options?: AoWriteOptions | undefined, | ||
): Promise<string> { | ||
const res = await this.process.write({ | ||
data: options?.data, | ||
tags: [ | ||
{ | ||
name: 'Action', | ||
value: 'KV-Registry.Spawn-KV-Store', | ||
}, | ||
...(options?.tags ?? []), | ||
...(name | ||
? [ | ||
{ | ||
name: 'Name', | ||
value: name, | ||
}, | ||
] | ||
: []), | ||
], | ||
}); | ||
return res.id; | ||
} | ||
} |
Oops, something went wrong.