Skip to content

Commit

Permalink
Merge pull request #4 from project-kardeshev/fix-logger
Browse files Browse the repository at this point in the history
feat(kv store): Implement kv store and kv registry
  • Loading branch information
atticusofsparta authored Nov 5, 2024
2 parents c4c7a56 + 054d99e commit c2fbe5e
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
"allowNullableObject": true,
"allowNullableBoolean": true,
"allowAny": true,
"allowNullableString": true,
},
],
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"format": "prettier --write .",
"test": "yarn test:unit && yarn test:e2e",
"test": "echo \"Error: no test specified\" && exit 0",
"test:cjs": "cd ./tests/e2e/cjs && yarn && yarn test",
"test:esm": "cd ./tests/e2e/esm && yarn && yarn test",
"test:web": "cd ./tests/e2e/web && yarn && yarn test",
Expand Down
17 changes: 9 additions & 8 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Logger } from '../utils/logger.js';
import {
CompositeTransport,
ConsoleTransport,
} from './events/event-transport.js';
import { EventVacuum } from './events/event-vacuum.js';

// import {
// CompositeTransport,
// ConsoleTransport,
// } from './events/event-transport.js';
// import { EventVacuum } from './events/event-vacuum.js';

export const defaultLogger = Logger.default;

export const defaultAoEventVacuum = new EventVacuum(
new CompositeTransport([new ConsoleTransport()]),
);
// export const defaultAoEventVacuum = new EventVacuum(
// new CompositeTransport([new ConsoleTransport()]),
// );

export const ARWEAVE_TX_REGEX = new RegExp('^[a-zA-Z0-9_-]{43}$');

Expand Down
2 changes: 1 addition & 1 deletion src/common/events/event-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class CompositeTransport {
export class ConsoleTransport implements IEventTransport {
logger: ILogger;
constructor() {
this.logger = defaultLogger.child('console-transport');
this.logger = defaultLogger.child('console-transport') as ILogger;
}

sendEvents(events: AoEvent[], processId: string, nonce: number) {
Expand Down
2 changes: 2 additions & 0 deletions src/common/process/impl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './kv/registry.js';
export * from './kv/store.js';
123 changes: 123 additions & 0 deletions src/common/process/impl/kv/registry.ts
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;
}
}
Loading

0 comments on commit c2fbe5e

Please sign in to comment.