Skip to content

Commit

Permalink
feat: add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Dec 15, 2023
1 parent a21de2a commit 118f7a2
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 64 deletions.
27 changes: 13 additions & 14 deletions examples/demo/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="theme-color" content="#484A65">
<meta name="description" content="Demo ">
<title>Demo</title>

</head>
<body>
<h1>Hello</h1>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#484A65" />
<meta name="description" content="Demo " />
<title>Demo</title>
</head>
<body>
<h1>Working 🚏</h1>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
15 changes: 11 additions & 4 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
"serve": "vite preview --port 3000"
},
"dependencies": {
"@wnfs-wg/nest": "file:../../packages/nest"
"@wnfs-wg/nest": "workspace:../../packages/nest",
"blockstore-idb": "^1.1.6",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@babel/core": "^7.23.5",
"@types/node": "^20.10.4",
"typescript": "5.2.2",
"vite": "^5.0.7"
"vite": "^5.0.7",
"vite-plugin-wasm": "^3.3.0"
},
"eslintConfig": {
"extends": ["@fission-codes"],
"ignorePatterns": ["dist"]
"extends": [
"@fission-codes"
],
"ignorePatterns": [
"dist"
]
}
}
58 changes: 55 additions & 3 deletions examples/demo/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
import { randomBytes } from '@wnfs-wg/nest'
import { CID, FileSystem, Path } from '@wnfs-wg/nest'
import { IDBBlockstore } from 'blockstore-idb'
import * as uint8arrays from 'uint8arrays'

// eslint-disable-next-line no-console
console.log(randomBytes(10))
declare global {
var fs: FileSystem
var Path: typeof Path
}

// HTML
const h1 = document.querySelector('h1')
if (!h1) throw new Error('Expected to find a h1 element')

// Blockstore
const blockstore = new IDBBlockstore('path/to/store')
await blockstore.open()

// Load existing file system or create a new one
const dataRoot = localStorage.getItem('fs-pointer')
const storedKey = localStorage.getItem('capsule-key')

const fs =
dataRoot === null
? await FileSystem.create({ blockstore })
: await FileSystem.fromCID(CID.parse(dataRoot), { blockstore })

globalThis.fs = fs
globalThis.Path = Path

// Create new private directory at the root
const { capsuleKey } = await fs.mountPrivateNode({
path: Path.directory(),
capsuleKey:
storedKey === null
? undefined
: uint8arrays.fromString(storedKey, 'base64'),
})

// Read from or write to the file system
const filePath = Path.file('private', 'init')

if (await fs.exists(filePath)) {
h1.textContent = 'Time first seen: ' + (await fs.read(filePath, 'utf8'))
} else {
const dateTime = new Date().toString()
const result = await fs.write(filePath, 'utf8', dateTime)

localStorage.setItem('fs-pointer', result.dataRoot.toString())
localStorage.setItem(
'capsule-key',
uint8arrays.toString(capsuleKey, 'base64')
)

// eslint-disable-next-line no-console
h1.textContent = 'Time first seen: ' + dateTime
}
10 changes: 10 additions & 0 deletions examples/demo/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'
import wasm from 'vite-plugin-wasm'

// https://vitejs.dev/config/
export default defineConfig({
build: {
target: 'esnext',
},
plugins: [wasm()],
})
39 changes: 37 additions & 2 deletions packages/nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,38 @@
"types": "./dist/src/index.d.ts",
"default": "./src/index.js"
},
"./app-info": {
"types": "./dist/src/app-info.d.ts",
"default": "./src/app-info.js"
},
"./class": {
"types": "./dist/src/class.d.ts",
"default": "./src/class.js"
},
"./crypto": {
"types": "./dist/src/crypto.d.ts",
"browser": "./src/crypto-browser.js",
"default": "./src/crypto.js"
},
"./errors": {
"types": "./dist/src/errors.d.ts",
"default": "./src/errors.js"
},
"./events": {
"types": "./dist/src/events.d.ts",
"default": "./src/events.js"
},
"./path": {
"types": "./dist/src/path.d.ts",
"default": "./src/path.js"
},
"./types": {
"types": "./dist/src/types.d.ts",
"default": "./src/types.js"
},
"./version": {
"types": "./dist/src/version.d.ts",
"default": "./src/version.js"
}
},
"main": "src/index.js",
Expand All @@ -29,7 +57,15 @@
},
"typesVersions": {
"*": {
"module1": ["dist/src/crypto"]
".": ["dist/src/index"],
"app-info": ["dist/src/app-info"],
"class": ["dist/src/class"],
"crypto": ["dist/src/crypto"],
"errors": ["dist/src/errors"],
"events": ["dist/src/events"],
"path": ["dist/src/path"],
"types": ["dist/src/types"],
"version": ["dist/src/version"]
}
},
"files": ["src", "dist/src/*.d.ts", "dist/src/*.d.ts.map"],
Expand Down Expand Up @@ -71,7 +107,6 @@
},
"eslintConfig": {
"extends": ["@fission-codes"],
"reportUnusedDisableDirectives": false,
"env": {
"mocha": true
},
Expand Down
2 changes: 2 additions & 0 deletions packages/nest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { CID } from 'multiformats/cid'

export * from './app-info.js'
export * from './class.js'
export * from './root-tree.js'
Expand Down
17 changes: 10 additions & 7 deletions packages/nest/test/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
describe('File System Class', () => {
let blockstore: Blockstore
let fs: FileSystem
let mounts: Array<{
let _mounts: Array<{
path: Path.Distinctive<Path.Segments>
capsuleKey: Uint8Array
}>
Expand All @@ -42,7 +42,7 @@ describe('File System Class', () => {
...fsOpts,
})

mounts = await fs.mountPrivateNodes([{ path: Path.root() }])
_mounts = await fs.mountPrivateNodes([{ path: Path.root() }])
})

// LOADING
Expand All @@ -53,7 +53,11 @@ describe('File System Class', () => {
const privatePath = Path.file('private', 'nested-private', 'private.txt')

const { contentCID } = await fs.write(publicPath, 'utf8', 'public')
const { dataRoot } = await fs.write(privatePath, 'utf8', 'private')
const { capsuleKey, dataRoot } = await fs.write(
privatePath,
'utf8',
'private'
)

const contentBytes = await Unix.exportFile(contentCID, blockstore)

Expand All @@ -63,10 +67,9 @@ describe('File System Class', () => {
blockstore,
...fsOpts,
})

await loadedFs.mountPrivateNodes([
// TODO: Needs to be fixed in rs-wnfs
// { path: Path.removePartition(privatePath), capsuleKey },
{ path: Path.root(), capsuleKey: mounts[0].capsuleKey },
{ path: Path.removePartition(privatePath), capsuleKey },
])

assert.equal(await loadedFs.read(publicPath, 'utf8'), 'public')
Expand Down Expand Up @@ -1038,7 +1041,7 @@ describe('File System Class', () => {
onCommit: async (_modifications: Modification[]) => ({ commit: false }),
})

mounts = await fs.mountPrivateNodes([{ path: Path.root() }])
_mounts = await fs.mountPrivateNodes([{ path: Path.root() }])

// TODO:
// await fs.write(Path.file('private', 'test', 'file'), 'utf8', '🔥')
Expand Down
9 changes: 5 additions & 4 deletions packages/nest/test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import assert from 'assert'
import all from 'it-all'

import type { Blockstore } from 'interface-blockstore'

import * as fc from 'fast-check'
import * as UnixExporter from 'ipfs-unixfs-exporter'
import * as Uint8Arrays from 'uint8arrays'

import type { FileSystem } from '../../src/class.js'
import { linksFromCID } from '../../src/root-tree/basic.js'
import * as Path from '../../src/path.js'
import type { Blockstore } from 'interface-blockstore'

// PATHS

Expand All @@ -19,7 +20,7 @@ export function arbitraryDirectoryPath<P extends Path.Partition>(
.array(arbitraryPathSegment(), { minLength: 1, maxLength: 8 })
.map((array) => {
const path: Path.Directory<Path.PartitionedNonEmpty<P>> = {
directory: [partition, ...array],
directory: [partition, ...array] as any,
}
return path
})
Expand All @@ -32,7 +33,7 @@ export function arbitraryFilePath<P extends Path.Partition>(
.array(arbitraryPathSegment(), { minLength: 1, maxLength: 8 })
.map((array) => {
const path: Path.File<Path.PartitionedNonEmpty<P>> = {
file: [partition, ...array],
file: [partition, ...array] as any,
}
return path
})
Expand Down Expand Up @@ -110,7 +111,7 @@ export async function assertUnixNodeRemoval(
`${unixRoot.toString()}${pathString}`,
opts.blockstore
)
} catch (error) {
} catch (error: any) {
assert(error.toString(), 'File does not exist')
}
}
4 changes: 2 additions & 2 deletions packages/nest/test/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ describe('Path functions', () => {

it('supports map', () => {
assert.deepEqual(
Path.map((p) => [...p, 'bar'], Path.directory('foo')),
Path.map((p) => [...p, 'bar'], Path.directory('foo')), // eslint-disable-line unicorn/no-array-method-this-argument
{ directory: ['foo', 'bar'] }
)

assert.deepEqual(
Path.map((p) => [...p, 'bar'], Path.file('foo')),
Path.map((p) => [...p, 'bar'], Path.file('foo')), // eslint-disable-line unicorn/no-array-method-this-argument
{ file: ['foo', 'bar'] }
)
})
Expand Down
19 changes: 5 additions & 14 deletions packages/nest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
"compilerOptions": {
"outDir": "dist",
"module": "NodeNext",
"moduleResolution": "NodeNext"
"moduleResolution": "NodeNext",
"noUnusedLocals": false
},
"include": [
"src",
"test"
],
"exclude": [
"node_modules",
"dist",
"out"
],
"include": ["src", "test"],
"exclude": ["node_modules", "dist", "out"],
"typedocOptions": {
"entryPoints": [
"src/index.js",
"src/module1.js"
],
"entryPoints": ["src/index.js", "src/module1.js"],
"includeVersion": true,
"excludeExternals": true,
"internalModule": "<internal>"
Expand Down
Loading

0 comments on commit 118f7a2

Please sign in to comment.