From fbb2c4ca51389d4aa5a81ecae5f644ab5283cb31 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Fri, 15 Dec 2023 15:43:42 +0100 Subject: [PATCH] refactor: change -> modification --- packages/nest/readme.md | 18 +++++++++++++++++ packages/nest/src/class.ts | 8 ++++---- packages/nest/src/root-tree.ts | 4 ++-- packages/nest/src/root-tree/basic.ts | 18 ++++++++--------- packages/nest/src/transaction.ts | 30 ++++++++++++++-------------- packages/nest/src/types.ts | 2 +- packages/nest/test/class.test.ts | 2 +- 7 files changed, 50 insertions(+), 32 deletions(-) diff --git a/packages/nest/readme.md b/packages/nest/readme.md index e667fc9..fb94e27 100644 --- a/packages/nest/readme.md +++ b/packages/nest/readme.md @@ -92,6 +92,24 @@ Scenario 3:
const fs = await FileSystem.fromCID(fsPointer, { blockstore }) ``` +## Commit verification + +This exists so you can approve modifications to the file system. + +```ts +import { Modification } from '@wnfs-wg/nest' + +const fs = FileSystem.create({ + blockstore, + onCommit: (modifications: Modification[]): { commit: boolean } => { + // For example, check if I have access to all paths. + const satisfied = modifications.every(m => ALLOWED_PATHS.includes( Path.toPosix(m.path) )) + if (satisfied) return { commit: true } + else return { commit: false } + } +}) +``` + ## Docs Check diff --git a/packages/nest/src/class.ts b/packages/nest/src/class.ts index da8718f..ce2cfef 100644 --- a/packages/nest/src/class.ts +++ b/packages/nest/src/class.ts @@ -625,7 +625,7 @@ export class FileSystem { mutationOptions: MutationOptions = {} ): Promise< | { - changes: Modification[] + modifications: Modification[] dataRoot: CID } | NOOP @@ -639,7 +639,7 @@ export class FileSystem { const commitResult = await TransactionContext.commit(context) if (commitResult === 'no-op') return 'no-op' - const { changes, privateNodes, rootTree } = commitResult + const { modifications, privateNodes, rootTree } = commitResult this.#privateNodes = privateNodes this.#rootTree = rootTree @@ -650,7 +650,7 @@ export class FileSystem { // Emit events await this.#eventEmitter.emit('commit', { dataRoot, - modifications: [...changes], + modifications: [...modifications], }) // Publish @@ -663,7 +663,7 @@ export class FileSystem { // Fin return { - changes, + modifications, dataRoot, } } diff --git a/packages/nest/src/root-tree.ts b/packages/nest/src/root-tree.ts index c5b4e96..84def0e 100644 --- a/packages/nest/src/root-tree.ts +++ b/packages/nest/src/root-tree.ts @@ -11,12 +11,12 @@ export abstract class RootTree { abstract privateForest(): PrivateForest abstract replacePrivateForest( forest: PrivateForest, - changes: Modification[] + modifications: Modification[] ): Promise abstract publicRoot(): PublicDirectory abstract replacePublicRoot( dir: PublicDirectory, - changes: Modification[] + modifications: Modification[] ): Promise abstract clone(): RootTree diff --git a/packages/nest/src/root-tree/basic.ts b/packages/nest/src/root-tree/basic.ts index c0352c3..e44a81b 100644 --- a/packages/nest/src/root-tree/basic.ts +++ b/packages/nest/src/root-tree/basic.ts @@ -150,7 +150,7 @@ export class BasicRootTree implements RootTree { async replacePrivateForest( forest: PrivateForest, - _changes: Modification[] + _modifications: Modification[] ): Promise { return new BasicRootTree({ blockstore: this.#blockstore, @@ -169,7 +169,7 @@ export class BasicRootTree implements RootTree { async replacePublicRoot( dir: PublicDirectory, - changes: Modification[] + modifications: Modification[] ): Promise { const treeWithNewPublicRoot = new BasicRootTree({ blockstore: this.#blockstore, @@ -181,26 +181,26 @@ export class BasicRootTree implements RootTree { version: this.#version, }) - const unixTree = await changes.reduce(async (oldRootPromise, change) => { + const unixTree = await modifications.reduce(async (oldRootPromise, mod) => { const oldRoot = await oldRootPromise - if (!Path.isPartition('public', change.path)) { + if (!Path.isPartition('public', mod.path)) { return oldRoot } - const path = Path.removePartition(change.path) + const path = Path.removePartition(mod.path) - if (change.type === 'removed') { + if (mod.type === 'removed') { return await Unix.removeNodeFromTree(oldRoot, path, this.#blockstore) } const contentCID = - Path.isFile(change.path) && - Path.isPartitionedNonEmpty(change.path) + Path.isFile(mod.path) && + Path.isPartitionedNonEmpty(mod.path) ? await References.contentCID( this.#blockstore, treeWithNewPublicRoot, - change.path + mod.path ) : undefined diff --git a/packages/nest/src/transaction.ts b/packages/nest/src/transaction.ts index a436503..9d12c58 100644 --- a/packages/nest/src/transaction.ts +++ b/packages/nest/src/transaction.ts @@ -52,7 +52,7 @@ export class TransactionContext { #privateNodes: MountedPrivateNodes #rootTree: RootTree - readonly #changes: Set<{ + readonly #modifications: Set<{ type: MutationType path: Path.Distinctive> }> @@ -71,13 +71,13 @@ export class TransactionContext { this.#rng = rng this.#rootTree = rootTree - this.#changes = new Set() + this.#modifications = new Set() } /** @internal */ static async commit(context: TransactionContext): Promise< | { - changes: Array<{ + modifications: Array<{ path: Path.Distinctive> type: MutationType }> @@ -86,23 +86,23 @@ export class TransactionContext { } | NOOP > { - const changes = [...context.#changes] + const modifications = [...context.#modifications] // Verify - const { commit } = await context.#onCommit([...changes]) + const { commit } = await context.#onCommit([...modifications]) if (!commit) return 'no-op' // Private forest - const newForest = await changes.reduce( - async (oldForestPromise, change): Promise => { + const newForest = await modifications.reduce( + async (oldForestPromise, mod): Promise => { const oldForest = await oldForestPromise - if (!Path.isPartition('private', change.path)) { + if (!Path.isPartition('private', mod.path)) { return oldForest } const maybeNode = findPrivateNode( - change.path as Path.Distinctive>, + mod.path as Path.Distinctive>, context.#privateNodes ) @@ -119,12 +119,12 @@ export class TransactionContext { // Replace forest const rootTree = await context.#rootTree.replacePrivateForest( newForest, - changes + modifications ) // Fin return { - changes: changes, + modifications: modifications, privateNodes: context.#privateNodes, rootTree: rootTree, } @@ -568,7 +568,7 @@ export class TransactionContext { mut: Mutations.Public, mutType: MutationType ): Promise { - const change = { + const mod = { type: mutType, path: path, } @@ -581,11 +581,11 @@ export class TransactionContext { // Replace public root this.#rootTree = await this.#rootTree.replacePublicRoot(result.rootDir, [ - change, + mod, ]) // Mark node as changed - this.#changes.add(change) + this.#modifications.add(mod) } async #privateMutation( @@ -609,7 +609,7 @@ export class TransactionContext { }) // Mark node as changed - this.#changes.add(change) + this.#modifications.add(change) // Replace forest this.#rootTree = await this.#rootTree.replacePrivateForest(result.forest, [ diff --git a/packages/nest/src/types.ts b/packages/nest/src/types.ts index a604f4f..18490cc 100644 --- a/packages/nest/src/types.ts +++ b/packages/nest/src/types.ts @@ -7,7 +7,7 @@ export type AnySupportedDataType = | string export type CommitVerifier = ( - changes: Modification[] + modifications: Modification[] ) => Promise<{ commit: boolean }> export type DataType = 'bytes' | 'json' | 'utf8' diff --git a/packages/nest/test/class.test.ts b/packages/nest/test/class.test.ts index d95b889..c6e705e 100644 --- a/packages/nest/test/class.test.ts +++ b/packages/nest/test/class.test.ts @@ -1035,7 +1035,7 @@ describe('File System Class', () => { fs = await FileSystem.create({ blockstore, ...fsOpts, - onCommit: async (_changes: Modification[]) => ({ commit: false }), + onCommit: async (_modifications: Modification[]) => ({ commit: false }), }) mounts = await fs.mountPrivateNodes([{ path: Path.root() }])