-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './cert-storage.ts'; | ||
export * from './types.ts'; |
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,6 @@ | ||
import type { Signer, Verifier } from '@ndn/packet'; | ||
|
||
export interface SecurityAgent { | ||
signer: Signer; | ||
verifier: Verifier; | ||
} |
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 { Storage } from './types.ts'; | ||
|
||
/** | ||
* A storage based on DenoKV. | ||
* Not actually included in the exported package. May need Custom Shims. | ||
*/ | ||
export class DenoKvStorage implements Storage { | ||
constructor( | ||
public readonly kv: Deno.Kv, | ||
) { | ||
} | ||
|
||
public static async create(path?: string) { | ||
return new DenoKvStorage(await Deno.openKv(path)); | ||
} | ||
|
||
async get(key: string): Promise<Uint8Array | undefined> { | ||
const ret = await this.kv.get<Uint8Array>([key]); | ||
return ret.value ?? undefined; | ||
} | ||
|
||
async set(key: string, value: Uint8Array | undefined): Promise<void> { | ||
await this.kv.set([key], value); | ||
} | ||
|
||
async has(key: string): Promise<boolean> { | ||
const ret = await this.kv.get<Uint8Array>([key]); | ||
return !!ret.value; | ||
} | ||
|
||
async delete(key: string): Promise<boolean> { | ||
const ret = await this.kv.get<Uint8Array>([key]); | ||
if (ret.value) { | ||
await this.kv.delete([key]); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
async clear(): Promise<void> { | ||
const entries = this.kv.list({ prefix: [] }); | ||
for await (const entry of entries) { | ||
await this.kv.delete(entry.key); | ||
} | ||
} | ||
|
||
close(): Promise<void> { | ||
this.kv.close(); | ||
return Promise.resolve(); | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from './types.ts'; | ||
export * from './in-memory.ts'; | ||
export * from './file-system.ts'; | ||
// export * from './deno-kv.ts'; |
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 './workspace.ts'; |
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,84 @@ | ||
import { Storage } from '../storage/mod.ts'; | ||
import { Endpoint } from '@ndn/endpoint'; | ||
import type { Name, Signer, Verifier } from '@ndn/packet'; | ||
import { encodeSyncState, parseSyncState, SyncAgent } from '../sync-agent/mod.ts'; | ||
import { NdnSvsAdaptor, YjsStateManager } from '../adaptors/mod.ts'; | ||
import * as Y from 'yjs'; | ||
|
||
export class Workspace { | ||
private constructor( | ||
public readonly nodeId: Name, | ||
public readonly persistStore: Storage, | ||
public readonly endpoint: Endpoint, | ||
public readonly onReset: (() => void) | undefined, | ||
public readonly syncAgent: SyncAgent, | ||
public readonly yjsSnapshotMgr: YjsStateManager, | ||
public readonly yjsAdaptor: NdnSvsAdaptor, | ||
) { | ||
} | ||
|
||
public static async create(opts: { | ||
nodeId: Name; | ||
persistStore: Storage; | ||
endpoint: Endpoint; | ||
rootDoc: Y.Doc; | ||
signer: Signer; | ||
verifier: Verifier; | ||
onReset?: () => void; | ||
createNewDoc?: () => Promise<void>; | ||
}) { | ||
// Sync Agents | ||
const syncAgent = await SyncAgent.create( | ||
opts.nodeId, | ||
opts.persistStore, | ||
opts.endpoint, | ||
opts.signer, | ||
opts.verifier, | ||
opts.onReset, | ||
); | ||
|
||
// Root doc using CRDT and Sync | ||
const yjsAdaptor = new NdnSvsAdaptor( | ||
syncAgent, | ||
opts.rootDoc, | ||
'doc', | ||
); | ||
const yjsSnapshotMgr = new YjsStateManager( | ||
() => encodeSyncState(syncAgent!.getUpdateSyncSV()), | ||
opts.rootDoc, | ||
// No key conflict in this case. If we are worried, use anothe sub-folder. | ||
opts.persistStore, | ||
); | ||
|
||
// Load or create | ||
if (opts.createNewDoc) { | ||
await opts.createNewDoc(); | ||
} else { | ||
const state = await yjsSnapshotMgr.loadLocalSnapshot((update) => { | ||
yjsAdaptor!.handleSyncUpdate(update); | ||
return Promise.resolve(); | ||
}); | ||
await syncAgent.replayUpdates('doc', state ? parseSyncState(state) : undefined); | ||
} | ||
|
||
// Start Sync | ||
syncAgent.ready = true; | ||
|
||
return new Workspace( | ||
opts.nodeId, | ||
opts.persistStore, | ||
opts.endpoint, | ||
opts.onReset, | ||
syncAgent, | ||
yjsSnapshotMgr, | ||
yjsAdaptor, | ||
); | ||
} | ||
|
||
public destroy() { | ||
this.syncAgent.ready = false; | ||
this.yjsSnapshotMgr.destroy(); | ||
this.syncAgent.destroy(); | ||
this.persistStore.close(); | ||
} | ||
} |