diff --git a/examples/demo/index.html b/examples/demo/index.html index 0ea846b..82923e4 100644 --- a/examples/demo/index.html +++ b/examples/demo/index.html @@ -1,15 +1,14 @@ - + - - - - - - Demo - - - -

Hello

- - - \ No newline at end of file + + + + + + Demo + + +

Working 🚏

+ + + diff --git a/examples/demo/package.json b/examples/demo/package.json index 680bcb3..fa3a53c 100644 --- a/examples/demo/package.json +++ b/examples/demo/package.json @@ -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" + ] } } diff --git a/examples/demo/src/main.ts b/examples/demo/src/main.ts index d643d76..a5877d7 100644 --- a/examples/demo/src/main.ts +++ b/examples/demo/src/main.ts @@ -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 +} diff --git a/examples/demo/vite.config.js b/examples/demo/vite.config.js new file mode 100644 index 0000000..4a0dad6 --- /dev/null +++ b/examples/demo/vite.config.js @@ -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()], +}) diff --git a/packages/nest/package.json b/packages/nest/package.json index f7a8762..0191641 100644 --- a/packages/nest/package.json +++ b/packages/nest/package.json @@ -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", @@ -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"], @@ -71,7 +107,6 @@ }, "eslintConfig": { "extends": ["@fission-codes"], - "reportUnusedDisableDirectives": false, "env": { "mocha": true }, diff --git a/packages/nest/src/index.ts b/packages/nest/src/index.ts index 72b9f84..710e22f 100644 --- a/packages/nest/src/index.ts +++ b/packages/nest/src/index.ts @@ -1,3 +1,5 @@ +export { CID } from 'multiformats/cid' + export * from './app-info.js' export * from './class.js' export * from './root-tree.js' diff --git a/packages/nest/test/class.test.ts b/packages/nest/test/class.test.ts index c6e705e..01b4d0b 100644 --- a/packages/nest/test/class.test.ts +++ b/packages/nest/test/class.test.ts @@ -22,7 +22,7 @@ import { describe('File System Class', () => { let blockstore: Blockstore let fs: FileSystem - let mounts: Array<{ + let _mounts: Array<{ path: Path.Distinctive capsuleKey: Uint8Array }> @@ -42,7 +42,7 @@ describe('File System Class', () => { ...fsOpts, }) - mounts = await fs.mountPrivateNodes([{ path: Path.root() }]) + _mounts = await fs.mountPrivateNodes([{ path: Path.root() }]) }) // LOADING @@ -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) @@ -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') @@ -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', '🔥') diff --git a/packages/nest/test/helpers/index.ts b/packages/nest/test/helpers/index.ts index b53fcca..6d9d1d5 100644 --- a/packages/nest/test/helpers/index.ts +++ b/packages/nest/test/helpers/index.ts @@ -1,6 +1,8 @@ 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' @@ -8,7 +10,6 @@ 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 @@ -19,7 +20,7 @@ export function arbitraryDirectoryPath

( .array(arbitraryPathSegment(), { minLength: 1, maxLength: 8 }) .map((array) => { const path: Path.Directory> = { - directory: [partition, ...array], + directory: [partition, ...array] as any, } return path }) @@ -32,7 +33,7 @@ export function arbitraryFilePath

( .array(arbitraryPathSegment(), { minLength: 1, maxLength: 8 }) .map((array) => { const path: Path.File> = { - file: [partition, ...array], + file: [partition, ...array] as any, } return path }) @@ -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') } } diff --git a/packages/nest/test/path.test.ts b/packages/nest/test/path.test.ts index 9969365..5fe2088 100644 --- a/packages/nest/test/path.test.ts +++ b/packages/nest/test/path.test.ts @@ -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'] } ) }) diff --git a/packages/nest/tsconfig.json b/packages/nest/tsconfig.json index dd412b7..cf2c18f 100644 --- a/packages/nest/tsconfig.json +++ b/packages/nest/tsconfig.json @@ -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": "" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 979c725..81ccf0d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,9 +38,15 @@ importers: examples/demo: dependencies: - package1: - specifier: '*' + '@wnfs-wg/nest': + specifier: workspace:../../packages/nest version: link:../../packages/nest + blockstore-idb: + specifier: ^1.1.6 + version: 1.1.6 + uint8arrays: + specifier: ^5.0.0 + version: 5.0.0 devDependencies: '@babel/core': specifier: ^7.23.5 @@ -54,6 +60,9 @@ importers: vite: specifier: ^5.0.7 version: 5.0.7(@types/node@20.10.4) + vite-plugin-wasm: + specifier: ^3.3.0 + version: 3.3.0(vite@5.0.7) packages/nest: dependencies: @@ -372,13 +381,11 @@ packages: /@chainsafe/is-ip@2.0.2: resolution: {integrity: sha512-ndGqEMG1W5WkGagaqOZHpPU172AGdxr+LD15sv3WIUvT5oCFUrG1Y0CW/v2Egwj4JXEvSibaIIIqImsm98y1nA==} - dev: true /@chainsafe/netmask@2.0.0: resolution: {integrity: sha512-I3Z+6SWUoaljh3TBzCnCxjlUyN8tA+NAk5L6m9IxvCf1BENQTePzPMis97CoN/iMW1St3WN+AWCCRp+TTBRiDg==} dependencies: '@chainsafe/is-ip': 2.0.2 - dev: true /@es-joy/jsdoccomment@0.40.1: resolution: {integrity: sha512-YORCdZSusAlBrFpZ77pJjc5r1bQs5caPWtAu+WWmiSo+8XaUzseapVrfAtiRFbQWnrBxxLLEwF6f6ZG/UgCQCg==} @@ -1130,7 +1137,6 @@ packages: uint8arraylist: 2.4.7 transitivePeerDependencies: - supports-color - dev: true /@libp2p/logger@4.0.2: resolution: {integrity: sha512-J9UMtMU9BKXNp+3c5kcI7HyWOPYg2B2E6sn1gEQckiSexTaz0wKJSlgTZ89f9F8bkC3AaC8ybXYuHbFQhwpTIg==} @@ -1142,7 +1148,6 @@ packages: multiformats: 12.1.3 transitivePeerDependencies: - supports-color - dev: true /@multiformats/multiaddr@12.1.11: resolution: {integrity: sha512-CWG9kETEGTTMdr1T+/JEuMwFld3r3fHNP8LkLoUcLvHRy6yr8sWdotVGEDNEdDO/vrKhuD7bQBws3xMSMMyylg==} @@ -1156,7 +1161,6 @@ packages: uint8arrays: 4.0.9 transitivePeerDependencies: - supports-color - dev: true /@multiformats/murmur3@2.1.7: resolution: {integrity: sha512-Yf0UpAaONjed+8PTt5NM/GG4Z4Ai4m1qfT7bqevjnkwRQ12K+0jxtRomirz+VJx4PokpA2St1ZSD1iMkZTqPRQ==} @@ -1881,7 +1885,18 @@ packages: uint8arrays: 5.0.0 transitivePeerDependencies: - supports-color - dev: true + + /blockstore-idb@1.1.6: + resolution: {integrity: sha512-00XBjq7wfLSUvxiszmLHlOm54Dcbnbj6G8YuL0iRqOUdfsdQBerXH5ajIrvlFTjh+pECkQ1RzGsmlEdnVmnEJg==} + dependencies: + blockstore-core: 4.3.8 + idb: 7.1.1 + interface-blockstore: 5.2.7 + interface-store: 5.1.5 + multiformats: 12.1.3 + transitivePeerDependencies: + - supports-color + dev: false /brace-expansion@1.1.11: resolution: {integrity: sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==} @@ -2308,7 +2323,6 @@ packages: receptacle: 1.3.2 transitivePeerDependencies: - supports-color - dev: true /doctrine@2.1.0: resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==} @@ -3470,6 +3484,10 @@ packages: engines: {node: '>=16.17.0'} dev: true + /idb@7.1.1: + resolution: {integrity: sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==} + dev: false + /ieee754@1.2.1: resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==} @@ -3526,7 +3544,6 @@ packages: dependencies: interface-store: 5.1.5 uint8arrays: 5.0.0 - dev: true /interface-store@5.1.5: resolution: {integrity: sha512-X0KnJBk3o+YL13MxZBMwa88/b3Mdrpm0yPzkSTKDDVn9BSPH7UK6W+ZtIPO2bxKOQVmq7zqOwAnYnpfqWjb6/g==} @@ -3884,7 +3901,6 @@ packages: /it-drain@3.0.5: resolution: {integrity: sha512-qYFe4SWdvs9oJGUY5bSjvmiLUMLzFEODNOQUdYdCIkuIgQF+AUB2INhM4yQ09buJ2rhHKDFxvTD/+yUq6qg0XA==} - dev: true /it-filter@3.0.4: resolution: {integrity: sha512-e0sz+st4sudK/zH6GZ/gRTRP8A/ADuJFCYDmRgMbZvR79y5+v4ZXav850bBZk5wL9zXaYZFxS1v/6Qi+Vjwh5g==} @@ -4353,7 +4369,6 @@ packages: /ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} - dev: true /multiformats@12.1.3: resolution: {integrity: sha512-eajQ/ZH7qXZQR2AgtfpmSMizQzmyYVmCql7pdhldPuYQi4atACekbJaQplk6dWyIi10jCaFnd6pqvcEFXjbaJw==} @@ -4915,7 +4930,6 @@ packages: resolution: {integrity: sha512-HrsFvqZZheusncQRiEE7GatOAETrARKV/lnfYicIm8lbvp/JQOdADOfhjBd2DajvoszEyxSM6RlAAIZgEoeu/A==} dependencies: ms: 2.1.3 - dev: true /reflect.getprototypeof@1.0.4: resolution: {integrity: sha512-ECkTw8TmJwW60lOTR+ZkODISW6RQ8+2CL3COqtiJKLd6MmB45hN51HprHFziKLGkAuTGQhBb91V8cy+KHlaCjw==} @@ -5630,7 +5644,6 @@ packages: dependencies: uint8arraylist: 2.4.7 uint8arrays: 4.0.9 - dev: true /uint8arraylist@2.4.7: resolution: {integrity: sha512-ohRElqR6C5dd60vRFLq40MCiSnUe1AzkpHvbCEMCGGP6zMoFYECsjdhL6bR1kTK37ONNRDuHQ3RIpScRYcYYIg==} @@ -5724,6 +5737,14 @@ packages: spdx-expression-parse: 3.0.1 dev: true + /vite-plugin-wasm@3.3.0(vite@5.0.7): + resolution: {integrity: sha512-tVhz6w+W9MVsOCHzxo6SSMSswCeIw4HTrXEi6qL3IRzATl83jl09JVO1djBqPSwfjgnpVHNLYcaMbaDX5WB/pg==} + peerDependencies: + vite: ^2 || ^3 || ^4 || ^5 + dependencies: + vite: 5.0.7(@types/node@20.10.4) + dev: true + /vite@5.0.7(@types/node@20.10.4): resolution: {integrity: sha512-B4T4rJCDPihrQo2B+h1MbeGL/k/GMAHzhQ8S0LjQ142s6/+l3hHTT095ORvsshj4QCkoWu3Xtmob5mazvakaOw==} engines: {node: ^18.0.0 || >=20.0.0}