Skip to content

Commit

Permalink
feat: Add size method
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Mar 8, 2024
1 parent 5f63ff2 commit a8f6910
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 21 deletions.
36 changes: 15 additions & 21 deletions packages/nest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,18 @@ const blockstore = new IDBBlockstore('path/to/store')
await blockstore.open()

const fs = await FileSystem.create({
blockstore
blockstore,
})

// Create the private node of which we'll keep the encryption key around.
const { capsuleKey } = await fs.mountPrivateNode({
path: Path.root() // ie. root private directory
path: Path.root(), // ie. root private directory
})

// Write & Read
await fs.write(
Path.file('private', 'file'),
'utf8',
'🪺'
)

const contents = await fs.read(
Path.file('private', 'file'),
'utf8'
)
await fs.write(Path.file('private', 'file'), 'utf8', '🪺')

const contents = await fs.read(Path.file('private', 'file'), 'utf8')
```

Scenario 2:<br />
Expand All @@ -73,11 +66,7 @@ of our root tree, the pointer to our file system.
let fsPointer: CID = await fs.calculateDataRoot()

// When we make a modification to the file system a verification is performed.
await fs.write(
Path.file('private', 'file'),
'utf8',
'🪺'
)
await fs.write(Path.file('private', 'file'), 'utf8', '🪺')

// If the commit is approved, the changes are reflected in the file system and
// the `commit` and `publish` events are emitted.
Expand All @@ -103,7 +92,7 @@ const fs = await FileSystem.fromCID(fsPointer, { blockstore })
// `capsuleKey` from scenario 1
await fs.mountPrivateNode({
path: Path.root(),
capsuleKey
capsuleKey,
})
```

Expand All @@ -115,6 +104,7 @@ await fs.mountPrivateNode({
fs.exists
fs.listDirectory // alias: fs.ls
fs.read
fs.size
```

### Mutations
Expand Down Expand Up @@ -153,12 +143,16 @@ import { Modification } from '@wnfs-wg/nest'

const fs = FileSystem.create({
blockstore,
onCommit: (modifications: Modification[]): { commit: boolean } => {
onCommit: async (
modifications: Modification[]
): Promise<{ commit: boolean }> => {
// For example, check if I have access to all paths.
const satisfied = modifications.every(m => ALLOWED_PATHS.includes( Path.toPosix(m.path) ))
const satisfied = modifications.every((m) =>
ALLOWED_PATHS.includes(Path.toPosix(m.path))
)
if (satisfied) return { commit: true }
else return { commit: false }
}
},
})
```

Expand Down
5 changes: 5 additions & 0 deletions packages/nest/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ export class FileSystem {
)
}

/** @group Querying */
async size(path: Path.File<PartitionedNonEmpty<Partition>>): Promise<number> {
return await this.#transactionContext().size(path)
}

// MUTATIONS
// ---------

Expand Down
53 changes: 53 additions & 0 deletions packages/nest/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ export const publicReadFromCID = (
}
}

export const publicSize = () => {
return async (params: PublicParams): Promise<number> => {
const wnfsBlockStore = Store.wnfs(params.blockstore)

const node: PublicNode | null | undefined = await params.rootTree
.publicRoot()
.getNode(params.pathSegments, wnfsBlockStore)

if (node === null || node === undefined) {
throw new Error('Failed to find public node')
} else if (node.isDir()) {
throw new Error('Expected node to be a file')
}

return await node.asFile().getSize(wnfsBlockStore)
}
}

// PRIVATE

export type PrivateParams = {
Expand Down Expand Up @@ -321,3 +339,38 @@ export const privateReadFromAccessKey = (
}
}
}

export const privateSize = () => {
return async (params: PrivateParams): Promise<number> => {
let node

if (params.node.isDir()) {
if (params.remainder.length === 0) {
throw new Error('Expected node to be a file')
}

const tmpNode: PrivateNode | null | undefined = await params.node
.asDir()
.getNode(
params.remainder,
searchLatest(),
params.rootTree.privateForest(),
Store.wnfs(params.blockstore)
)

if (tmpNode === null || tmpNode === undefined) {
throw new Error('Failed to find private node')
} else if (tmpNode.isDir()) {
throw new Error('Expected node to be a file')
}

node = tmpNode
} else {
node = params.node
}

return await node
.asFile()
.getSize(params.rootTree.privateForest(), Store.wnfs(params.blockstore))
}
}
8 changes: 8 additions & 0 deletions packages/nest/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ export class TransactionContext {
return dataFromBytes(dataType, bytes)
}

/** @group Querying */
async size(path: Path.File<PartitionedNonEmpty<Partition>>): Promise<number> {
return await this.#query(path, {
public: Queries.publicSize(),
private: Queries.privateSize(),
})
}

// MUTATIONS

/** @group Mutating */
Expand Down
21 changes: 21 additions & 0 deletions packages/nest/test/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,27 @@ describe('File System Class', () => {
)
})

// SIZE
// ----

it('returns the size of public files', async () => {
const path = Path.file('public', 'file')

await fs.write(path, 'bytes', new Uint8Array([1, 2, 3]))
const size = await fs.size(path)

assert.equal(size, 3)
})

it('returns the size of private files', async () => {
const path = Path.file('private', 'file')

await fs.write(path, 'bytes', new Uint8Array([1, 2, 3, 4]))
const size = await fs.size(path)

assert.equal(size, 4)
})

// REMOVE
// ------

Expand Down

0 comments on commit a8f6910

Please sign in to comment.