@@ -2099,84 +2099,58 @@ const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
20992099const binaryCID = await advanced .putByCID (binaryData );
21002100```
21012101
2102- #### putWithCID(path, data, options?)
2102+ ### Composition Patterns
21032103
2104- Store data at a path and return both the path and CID in a single operation.
2104+ The FS5Advanced API is intentionally minimal with just 4 core methods. For common workflows, compose these with regular FS5 methods:
21052105
2106- ``` typescript
2107- async putWithCID (
2108- path : string ,
2109- data : any ,
2110- options ?: PutOptions
2111- ): Promise < { path : string ; cid : Uint8Array }>
2112- ```
2113-
2114- ** Parameters:**
2115- - ` path: string ` - The path where to store the data
2116- - ` data: any ` - The data to store
2117- - ` options?: PutOptions ` - Optional put options (encryption, media type, etc.)
2118-
2119- ** Returns:**
2120- - ` Promise<{ path: string; cid: Uint8Array }> ` - Object containing both path and CID
2121-
2122- ** Example:**
2106+ #### Store with Path and Get CID
21232107
21242108``` typescript
2125- // Store and get both path and CID
2126- const result = await advanced .putWithCID (' home/file.txt' , ' Content' );
2127- console .log (result .path ); // "home/file.txt"
2128- console .log (formatCID (result .cid )); // "bafybeif..."
2109+ // Instead of putWithCID(path, data) - use composition:
2110+ await s5 .fs .put (' home/file.txt' , ' Content' );
2111+ const cid = await advanced .pathToCID (' home/file.txt' );
2112+
2113+ console .log (` Stored at: home/file.txt ` );
2114+ console .log (` CID: ${formatCID (cid )} ` ); // "bafybeif..."
21292115
21302116// With encryption
2131- const encrypted = await advanced .putWithCID (
2132- ' home/secret.txt' ,
2133- ' Secret data' ,
2134- { encrypt: true }
2135- );
2117+ await s5 .fs .put (' home/secret.txt' , ' Secret data' , {
2118+ encryption: { algorithm: ' xchacha20-poly1305' }
2119+ });
2120+ const secretCid = await advanced .pathToCID (' home/secret.txt' );
21362121
21372122// Can retrieve by either path or CID
21382123const byPath = await s5 .fs .get (' home/secret.txt' );
2139- const byCID = await advanced .getByCID (encrypted . cid );
2124+ const byCID = await advanced .getByCID (secretCid );
21402125console .log (byPath === byCID ); // true
21412126```
21422127
2143- #### getMetadataWithCID(path)
2144-
2145- Get metadata for a file or directory along with its CID.
2146-
2147- ``` typescript
2148- async getMetadataWithCID (path : string ): Promise <{
2149- metadata: any ;
2150- cid: Uint8Array ;
2151- }>
2152- ```
2153-
2154- ** Parameters:**
2155- - ` path: string ` - The file or directory path
2156-
2157- ** Returns:**
2158- - ` Promise<{ metadata: any; cid: Uint8Array }> ` - Object containing metadata and CID
2159-
2160- ** Throws:**
2161- - ` Error ` if path does not exist
2162-
2163- ** Example:**
2128+ #### Get Metadata with CID
21642129
21652130``` typescript
2131+ // Instead of getMetadataWithCID(path) - use composition:
21662132await s5 .fs .put (' home/data.txt' , ' Content' );
21672133
2168- const result = await advanced .getMetadataWithCID (' home/data.txt' );
2169- console .log (result .metadata );
2134+ const metadata = await s5 .fs .getMetadata (' home/data.txt' );
2135+ const cid = await advanced .pathToCID (' home/data.txt' );
2136+
2137+ console .log (metadata );
21702138// {
21712139// type: 'file',
21722140// size: 7,
21732141// created: 1234567890,
21742142// modified: 1234567890
21752143// }
21762144
2177- console .log (formatCID (result . cid )); // "bafybeih..."
2145+ console .log (formatCID (cid )); // "bafybeih..."
21782146```
21792147
2148+ ** Why Composition?**
2149+ - Keeps API minimal and easy to learn (4 methods vs 6)
2150+ - Makes intent explicit (store * then* extract CID)
2151+ - Reduces maintenance burden
2152+ - Still provides all functionality
2153+
21802154### CID Utility Functions
21812155
21822156#### formatCID(cid, encoding?)
@@ -2293,16 +2267,17 @@ import { JSCryptoImplementation } from 's5/core';
22932267const crypto = new JSCryptoImplementation();
22942268const data = new TextEncoder().encode('Hello, World!');
22952269
2296- // Store data
2297- const result = await advanced.putWithCID('home/data.txt', 'Hello, World!');
2270+ // Store data and get CID
2271+ await s5.fs.put('home/data.txt', 'Hello, World!');
2272+ const cid = await advanced.pathToCID('home/data.txt');
22982273
22992274// Verify CID matches
2300- const isValid = await verifyCID(result. cid, data, crypto);
2275+ const isValid = await verifyCID(cid, data, crypto);
23012276console.log(isValid); // true
23022277
23032278// Tampered data fails verification
23042279const tamperedData = new TextEncoder().encode('Goodbye, World!');
2305- const isInvalid = await verifyCID(result. cid, tamperedData, crypto);
2280+ const isInvalid = await verifyCID(cid, tamperedData, crypto);
23062281console.log(isInvalid); // false
23072282` ` `
23082283
@@ -2354,18 +2329,19 @@ await s5.recoverIdentityFromSeedPhrase(seedPhrase);
23542329const advanced = new FS5Advanced(s5.fs);
23552330const crypto = new JSCryptoImplementation();
23562331
2357- // 1. Store data and get CID
2358- const result = await advanced.putWithCID('home/document.txt', 'Important data');
2359- console.log( ` Stored at : $ {result .path }` );
2360- console.log( ` CID : $ {formatCID (result .cid , ' base32' )}` );
2332+ // 1. Store data and get CID (composition pattern)
2333+ await s5.fs.put('home/document.txt', 'Important data');
2334+ const cid = await advanced.pathToCID('home/document.txt');
2335+ console.log( ` Stored at : home / document .txt ` );
2336+ console.log( ` CID : $ {formatCID (cid , ' base32' )}` );
23612337
23622338// 2. Verify the CID
23632339const data = new TextEncoder().encode('Important data');
2364- const isValid = await verifyCID(result. cid, data, crypto);
2340+ const isValid = await verifyCID(cid, data, crypto);
23652341console.log( ` CID valid : $ {isValid }` ); // true
23662342
23672343// 3. Share the CID (as string)
2368- const cidString = formatCID(result. cid, 'base58btc');
2344+ const cidString = formatCID(cid, 'base58btc');
23692345console.log( ` Share this CID : $ {cidString }` );
23702346
23712347// 4. Recipient: parse CID and retrieve data
@@ -2377,13 +2353,12 @@ console.log(`Retrieved: ${retrievedData}`); // "Important data"
23772353const foundPath = await advanced.cidToPath(receivedCID);
23782354console.log( ` Path : $ {foundPath }` ); // "home/document.txt"
23792355
2380- // 6. Get metadata with CID
2381- const metadata = await advanced.getMetadataWithCID(foundPath);
2356+ // 6. Get metadata and CID (composition pattern)
2357+ const metadata = await s5.fs.getMetadata(foundPath);
2358+ const metaCid = await advanced.pathToCID(foundPath);
23822359console.log(metadata);
2383- // {
2384- // metadata: { type: 'file', size: 14, ... },
2385- // cid: Uint8Array(32) [...]
2386- // }
2360+ // { type: 'file', size: 14, ... }
2361+ console.log( ` CID : $ {formatCID (metaCid )}` )
23872362
23882363// 7. CID-only storage (no path)
23892364const tempCID = await advanced.putByCID('Temporary content');
0 commit comments