Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Dec 13, 2023
1 parent c3bd7a2 commit 1067572
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 288 deletions.
38 changes: 11 additions & 27 deletions packages/nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,22 @@
},
"typesVersions": {
"*": {
"module1": [
"dist/src/crypto"
]
"module1": ["dist/src/crypto"]
}
},
"files": [
"src",
"dist/src/*.d.ts",
"dist/src/*.d.ts.map"
],
"files": ["src", "dist/src/*.d.ts", "dist/src/*.d.ts.map"],
"scripts": {
"lint": "tsc --build && eslint . && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore",
"build": "tsc --build",
"test": "pnpm run test:node && pnpm run test:browser",
"test:node": "mocha 'test/**/!(*.browser).test.ts'",
"test:browser": "playwright-test 'test/**/!(*.node).test.ts'"
"test:node": "mocha 'test/**/!(*.browser).test.ts' --bail --timeout 30000",
"test:browser": "playwright-test 'test/**/!(*.node).test.ts' --timeout 30000"
},
"dependencies": {
"@ipld/dag-cbor": "^9.0.6",
"@ipld/dag-pb": "^4.0.6",
"compare-versions": "^6.1.0",
"debounce-promise": "^3.1.2",
"debounce": "^2.0.0",
"emittery": "^1.0.1",
"interface-blockstore": "^5.2.7",
"ipfs-unixfs": "^11.1.0",
Expand All @@ -68,6 +62,7 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.4",
"assert": "^2.1.0",
"blockstore-core": "^4.3.8",
"fast-check": "^3.14.0",
"mocha": "^10.2.0",
"playwright-test": "^14.0.0",
Expand All @@ -77,16 +72,12 @@
"provenance": true
},
"eslintConfig": {
"extends": [
"@fission-codes"
],
"extends": ["@fission-codes"],
"reportUnusedDisableDirectives": false,
"env": {
"mocha": true
},
"ignorePatterns": [
"dist"
],
"ignorePatterns": ["dist"],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
Expand All @@ -96,18 +87,11 @@
"varsIgnorePattern": "^_"
}
],
"unicorn/no-array-reduce": [
"off"
]
"unicorn/no-array-reduce": ["off"]
}
},
"depcheck": {
"specials": [
"bin"
],
"ignores": [
"@types/*",
"assert"
]
"specials": ["bin"],
"ignores": ["@types/*", "assert"]
}
}
77 changes: 29 additions & 48 deletions packages/nest/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PublicDirectory, PublicFile } from 'wnfs'
import { CID } from 'multiformats/cid'
import { AccessKey, PrivateDirectory, PrivateFile, PrivateNode } from 'wnfs'

import debounce from 'debounce-promise'
import debounce from 'debounce'
import Emittery, {
type EmitteryOncePromise,
type OmnipresentEventData,
Expand All @@ -18,11 +18,11 @@ import type {
DataType,
DirectoryItem,
DirectoryItemWithKind,
FileSystemChange,
MutationOptions,
MutationResult,
PrivateMutationResult,
PublicMutationResult,
TransactionResult,
} from './types.js'

import type {
Expand Down Expand Up @@ -50,29 +50,27 @@ import { partition as determinePartition, findPrivateNode } from './mounts.js'
import { TransactionContext } from './transaction.js'
import { BasicRootTree } from './root-tree/basic.js'

/** @internal */
export interface FileSystemOptions {
// OPTIONS

export interface Options {
blockstore: Blockstore
did: string
rootTreeClass?: typeof RootTree
settleTimeBeforePublish?: number
}

export class FileSystem {
readonly #blockstore: Blockstore
readonly #debouncedDataRootUpdate: debounce.DebouncedFunction<any>
readonly #eventEmitter: Emittery<Events>
readonly #rng: Rng.Rng
readonly #settleTimeBeforePublish: number

#privateNodes: MountedPrivateNodes = {}
#rootTree: RootTree

readonly did: string

/** @hidden */
constructor(
blockstore: Blockstore,
did: string,
rootTree: RootTree,
settleTimeBeforePublish: number
) {
Expand All @@ -82,47 +80,41 @@ export class FileSystem {
this.#rootTree = rootTree
this.#settleTimeBeforePublish = settleTimeBeforePublish

this.did = did
this.#debouncedDataRootUpdate = debounce(
async (...dataRoots: CID[]): Promise<void> => {
const dataRoot = dataRoots.at(-1)
if (dataRoot !== undefined) {
await this.#eventEmitter.emit('publish', { dataRoot })
}
},
settleTimeBeforePublish
)
}

// INITIALISATION
// --------------

/**
* Creates a file system with an empty public tree & an empty private tree at the root.
*
* @internal
*/
static async create(opts: FileSystemOptions): Promise<FileSystem> {
const { blockstore, did, rootTreeClass, settleTimeBeforePublish } = opts
static async create(opts: Options): Promise<FileSystem> {
const { blockstore, rootTreeClass, settleTimeBeforePublish } = opts
const rootTree = await (rootTreeClass ?? BasicRootTree).create(blockstore)

return new FileSystem(
blockstore,
did,
rootTree,
settleTimeBeforePublish ?? 2500
)
return new FileSystem(blockstore, rootTree, settleTimeBeforePublish ?? 2500)
}

/**
* Loads an existing file system from a CID.
*
* @internal
*/
static async fromCID(cid: CID, opts: FileSystemOptions): Promise<FileSystem> {
const { blockstore, did, rootTreeClass, settleTimeBeforePublish } = opts
static async fromCID(cid: CID, opts: Options): Promise<FileSystem> {
const { blockstore, rootTreeClass, settleTimeBeforePublish } = opts
const rootTree = await (rootTreeClass ?? BasicRootTree).fromCID(
blockstore,
cid
)

return new FileSystem(
blockstore,
did,
rootTree,
settleTimeBeforePublish ?? 2500
)
return new FileSystem(blockstore, rootTree, settleTimeBeforePublish ?? 2500)
}

// EVENTS
Expand Down Expand Up @@ -606,7 +598,10 @@ export class FileSystem {
async transaction(
handler: (t: TransactionContext) => Promise<void>,
mutationOptions: MutationOptions = {}
): Promise<TransactionResult> {
): Promise<{
changes: FileSystemChange[]
dataRoot: CID
}> {
const context = this.#transactionContext()

// Execute handler
Expand All @@ -631,7 +626,10 @@ export class FileSystem {
}

// Publish
if (mutationOptions.skipPublish === false) {
if (
mutationOptions.skipPublish === false ||
mutationOptions.skipPublish === undefined
) {
await this.#publish(dataRoot)
}

Expand Down Expand Up @@ -755,23 +753,6 @@ export class FileSystem {

// ㊙️ ▒▒ PUBLISHING

readonly #debouncedDataRootUpdate = debounce(
(() => {
return async (args: Array<[dataRoot: CID]>): Promise<void> => {
const arg = args.at(-1)
if (arg !== undefined) {
const [dataRoot] = arg
await this.#eventEmitter.emit('publish', { dataRoot })
}
}
})(),
(() => this.#settleTimeBeforePublish) as any,
{
accumulate: true,
leading: false,
}
)

async #publish(dataRoot: CID): Promise<void> {
await this.#debouncedDataRootUpdate(dataRoot)
}
Expand Down
86 changes: 0 additions & 86 deletions packages/nest/src/codecs.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nest/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const publicListDirectoryWithKind = () => {
return async (params: PublicParams): Promise<DirectoryItemWithKind[]> => {
const dir: PublicDirectory =
params.pathSegments.length === 0
? params.rootTree.publicRoot
? params.rootTree.publicRoot()
: await params.rootTree
.publicRoot()
.getNode(params.pathSegments, Store.wnfs(params.blockstore))
Expand Down
14 changes: 12 additions & 2 deletions packages/nest/src/root-tree/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ export class BasicRootTree implements RootTree {
dir: PublicDirectory,
changes: FileSystemChange[]
): Promise<RootTree> {
const treeWithNewPublicRoot = new BasicRootTree({
blockstore: this.#blockstore,

exchangeRoot: this.#exchangeRoot,
publicRoot: dir,
privateForest: this.#privateForest,
unix: this.#unix,
version: this.#version,
})

const unixTree = await changes.reduce(async (oldRootPromise, change) => {
const oldRoot = await oldRootPromise

Expand All @@ -189,9 +199,9 @@ export class BasicRootTree implements RootTree {
Path.isPartitionedNonEmpty<Path.Public>(change.path)
? await References.contentCID(
this.#blockstore,
this,
treeWithNewPublicRoot,
change.path
).then((a) => a ?? undefined)
)
: undefined

return await Unix.insertNodeIntoTree(
Expand Down
Loading

0 comments on commit 1067572

Please sign in to comment.