Skip to content

Commit

Permalink
chore: Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Apr 5, 2024
1 parent 5ba3cad commit 1926233
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/audio/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function load({
capsuleKey: Uint8Arr.fromString(storedKey, 'base64'),
})
} else {
const { capsuleKey } = await fs.mountPrivateNode({
const { capsuleKey } = await fs.createPrivateNode({
path: Path.root(),
})

Expand Down
6 changes: 3 additions & 3 deletions examples/audio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function createAudio(
// File
note('Adding file to WNFS')

const path = Path.file('private', fileName)
const path = Path.priv(fileName)

if (!(await fs.exists(path))) {
const { dataRoot } = await fs.write(path, 'bytes', new Uint8Array(fileData))
Expand Down Expand Up @@ -201,7 +201,7 @@ function createMediaSource({
fileSize: number
metadataSize: number
mimeType: string
path: Path.File<Path.PartitionedNonEmpty<Path.Private>>
path: Path.PartitionedNonEmpty<Path.Private>
}): { supported: boolean } {
if (window.MediaSource === undefined) {
return { supported: false }
Expand Down Expand Up @@ -406,7 +406,7 @@ async function createWebAudio({
mediainfo: MediaInfoType
metadataSize: number
mimeType: string
path: Path.File<Path.PartitionedNonEmpty<Path.Private>>
path: Path.PartitionedNonEmpty<Path.Private>
}): Promise<{ supported: boolean }> {
const audioContext = new AudioContext()

Expand Down
25 changes: 13 additions & 12 deletions examples/demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,29 @@ globalThis._fs = fs
globalThis._Path = Path

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

// Read from or write to the file system
const filePath = Path.file('private', 'init')
const filePath = Path.priv('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)
const capsuleKey = await fs.capsuleKey(Path.priv())

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

h1.textContent = 'Time first seen: ' + dateTime
}
31 changes: 14 additions & 17 deletions examples/web3storage/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { type Tracker } from './tracker'

// 🏔️

const TODOS_DIRECTORY = Path.directory('private', 'todos')
const TODOS_DIRECTORY = Path.priv('todos')
const TRANSPORT_HOST = 'radical-party.icidasset.partykit.dev'

let todos: DirectoryItem[] = []
Expand Down Expand Up @@ -82,8 +82,6 @@ async function initAuthed({

/**
*
* @param root0
* @param root0.isAuthenticated
* @param state
*/
function setAuthedState(
Expand Down Expand Up @@ -163,7 +161,7 @@ async function initDelete(fs: FileSystem): Promise<void> {

if (todo === undefined) throw new Error("Couldn't find associated todo")

await fs.remove(Path.combine(TODOS_DIRECTORY, Path.file(todo)))
await fs.remove([...TODOS_DIRECTORY, todo])
await loadAndRender(fs)
}
}
Expand Down Expand Up @@ -233,7 +231,7 @@ async function initInput(fs: FileSystem): Promise<void> {
button.setAttribute('disabled', '')

const todo = input.value
const todoPath = Path.combine(TODOS_DIRECTORY, Path.file(todo))
const todoPath = Path.combine(TODOS_DIRECTORY, [todo])

await fs.write(todoPath, 'json', {})
await loadAndRender(fs)
Expand All @@ -250,9 +248,9 @@ type Payload = Uint8Array

/**
*
* @param fs.client
* @param fs
* @param fs.fs
* @param root0
* @param root0.client
* @param root0.fs
*/
async function initLink({
client,
Expand Down Expand Up @@ -287,11 +285,10 @@ async function initLink({

/**
*
* @param challenge.challenge
* @param challenge
* @param publicKey
* @param challenge.client
* @param challenge.publicKey
* @param root0
* @param root0.challenge
* @param root0.client
* @param root0.publicKey
*/
async function initConsumer({
challenge,
Expand Down Expand Up @@ -364,9 +361,9 @@ async function initConsumer({

/**
*
* @param fs.client
* @param fs
* @param fs.fs
* @param root0
* @param root0.client
* @param root0.fs
*/
async function initProvider({
client,
Expand Down Expand Up @@ -420,7 +417,7 @@ async function initProvider({

await Promise.all(promises)

const key = await fs.capsuleKey(Path.directory('private'))
const key = await fs.capsuleKey(Path.priv())
if (key === undefined) throw new Error("Couldn't resolve access key")
await consumers[did]?.send('key', key)
})
Expand Down
8 changes: 4 additions & 4 deletions examples/web3storage/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function load({

// Create new or load existing private directory at the root
if (storedKey === undefined) {
const { capsuleKey } = await fs.mountPrivateNode({
const { capsuleKey } = await fs.createPrivateNode({
path: Path.root(),
})

Expand All @@ -61,7 +61,7 @@ export async function load({
// 💁 MANAGEMENT

export const Identity = {
PATH: Path.file('public', '.well-known', 'did'),
PATH: Path.pub('.well-known', 'did'),

async assign({ did, fs }: { did: string; fs: FileSystem }): Promise<void> {
await fs.write(this.PATH, 'utf8', did)
Expand All @@ -79,7 +79,7 @@ export const Keys = {
async lookup({
path,
}: {
path: Path.Directory<Path.Segments>
path: Path.Segments
}): Promise<Uint8Array | undefined> {
return await IDB.get(`fs.keys.path:/${Path.toPosix(path)}`)
},
Expand All @@ -89,7 +89,7 @@ export const Keys = {
path,
}: {
key: Uint8Array
path: Path.Directory<Path.Segments>
path: Path.Segments
}): Promise<void> {
await IDB.set(`fs.keys.path:/${Path.toPosix(path)}`, key)
},
Expand Down
3 changes: 1 addition & 2 deletions examples/web3storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,5 @@ async function setup(): Promise<{
const components = await setup()
await App.init(components)

// Debug

// @ts-expect-error Debug
globalThis.fs = components.fs
1 change: 0 additions & 1 deletion examples/web3storage/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"moduleDetection": "force",
"module": "ESNext",
"moduleResolution": "Bundler",
"types": ["vite/client"],
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"outDir": "dist",
Expand Down

0 comments on commit 1926233

Please sign in to comment.