|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const IPFS = require('ipfs') |
| 4 | +const Repo = require('ipfs-repo') |
| 5 | +const fsLock = require('ipfs-repo/src/lock') |
| 6 | + |
| 7 | +// Create our custom options |
| 8 | +const customRepositoryOptions = { |
| 9 | + |
| 10 | + /** |
| 11 | + * IPFS nodes store different information in separate storageBackends, or datastores. |
| 12 | + * Each storage backend can use the same type of datastore or a different one — you |
| 13 | + * could store your keys in a levelDB database while everything else is in files, |
| 14 | + * for example. (See https://github.com./ipfs/interface-datastore for more about datastores.) |
| 15 | + */ |
| 16 | + storageBackends: { |
| 17 | + root: require('datastore-fs'), // version and config data will be saved here |
| 18 | + blocks: require('datastore-fs'), |
| 19 | + keys: require('datastore-fs'), |
| 20 | + datastore: require('datastore-fs') |
| 21 | + }, |
| 22 | + |
| 23 | + /** |
| 24 | + * Storage Backend Options will get passed into the instantiation of their counterpart |
| 25 | + * in `storageBackends`. If you create a custom datastore, this is where you can pass in |
| 26 | + * custom constructor arguments. You can see an S3 datastore example at: |
| 27 | + * https://github.com./ipfs/js-datastore-s3/tree/master/examples/full-s3-repo |
| 28 | + * |
| 29 | + * NOTE: The following options are being overriden for demonstration purposes only. |
| 30 | + * In most instances you can simply use the default options, by not passing in any |
| 31 | + * overrides, which is recommended if you have no need to override. |
| 32 | + */ |
| 33 | + storageBackendOptions: { |
| 34 | + root: { |
| 35 | + extension: '.ipfsroot', // Defaults to ''. Used by datastore-fs; Appended to all files |
| 36 | + errorIfExists: false, // Used by datastore-fs; If the datastore exists, don't throw an error |
| 37 | + createIfMissing: true // Used by datastore-fs; If the datastore doesn't exist yet, create it |
| 38 | + }, |
| 39 | + blocks: { |
| 40 | + sharding: false, // Used by IPFSRepo Blockstore to determine sharding; Ignored by datastore-fs |
| 41 | + extension: '.ipfsblock', // Defaults to '.data'. |
| 42 | + errorIfExists: false, |
| 43 | + createIfMissing: true |
| 44 | + }, |
| 45 | + keys: { |
| 46 | + extension: '.ipfskey', // No extension by default |
| 47 | + errorIfExists: false, |
| 48 | + createIfMissing: true |
| 49 | + }, |
| 50 | + datastore: { |
| 51 | + extension: '.ipfsds', // No extension by default |
| 52 | + errorIfExists: false, |
| 53 | + createIfMissing: true |
| 54 | + } |
| 55 | + }, |
| 56 | + |
| 57 | + /** |
| 58 | + * A custom lock can be added here. Or the build in Repo `fs` or `memory` locks can be used. |
| 59 | + * See https://github.com./ipfs/js-ipfs-repo for more details on setting the lock. |
| 60 | + */ |
| 61 | + lock: fsLock |
| 62 | +} |
| 63 | + |
| 64 | +// Initialize our IPFS node with the custom repo options |
| 65 | +const node = new IPFS({ |
| 66 | + repo: new Repo('/tmp/custom-repo/.ipfs', customRepositoryOptions) |
| 67 | +}) |
| 68 | + |
| 69 | +// Test the new repo by adding and fetching some data |
| 70 | +node.on('ready', () => { |
| 71 | + console.log('Ready') |
| 72 | + node.version() |
| 73 | + .then((version) => { |
| 74 | + console.log('Version:', version.version) |
| 75 | + }) |
| 76 | + // Once we have the version, let's add a file to IPFS |
| 77 | + .then(() => { |
| 78 | + return node.files.add({ |
| 79 | + path: 'test-data.txt', |
| 80 | + content: Buffer.from('We are using a customized repo!') |
| 81 | + }) |
| 82 | + }) |
| 83 | + // Log out the added files metadata and cat the file from IPFS |
| 84 | + .then((filesAdded) => { |
| 85 | + console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash) |
| 86 | + return node.files.cat(filesAdded[0].hash) |
| 87 | + }) |
| 88 | + // Print out the files contents to console |
| 89 | + .then((data) => { |
| 90 | + console.log('\nFetched file content:') |
| 91 | + process.stdout.write(data) |
| 92 | + }) |
| 93 | + // Log out the error, if there is one |
| 94 | + .catch((err) => { |
| 95 | + console.log('File Processing Error:', err) |
| 96 | + }) |
| 97 | + // After everything is done, shut the node down |
| 98 | + // We don't need to worry about catching errors here |
| 99 | + .then(() => { |
| 100 | + console.log('\n\nStopping the node') |
| 101 | + return node.stop() |
| 102 | + }) |
| 103 | + // Let users know where they can inspect the repo |
| 104 | + .then(() => { |
| 105 | + console.log('Check "/tmp/custom-repo/.ipfs" to see what your customized repository looks like on disk.') |
| 106 | + }) |
| 107 | +}) |
0 commit comments