|
| 1 | +# Refs API |
| 2 | + |
| 3 | +* [refs](#refs) |
| 4 | +* [refsReadableStream](#refsreadablestream) |
| 5 | +* [refsPullStream](#refspullstream) |
| 6 | +* [refs.local](#refslocal) |
| 7 | +* [refs.localReadableStream](#refslocalreadablestream) |
| 8 | +* [refs.localPullStream](#refslocalpullstream) |
| 9 | + |
| 10 | +#### `refs` |
| 11 | + |
| 12 | +> Get links (references) from an object. |
| 13 | +
|
| 14 | +##### `ipfs.refs(ipfsPath, [options], [callback])` |
| 15 | + |
| 16 | +`ipfsPath` can be of type: |
| 17 | + |
| 18 | +- [`cid`][cid] of type: |
| 19 | + - a [CID](https://github.com/ipfs/js-cid) instance |
| 20 | + - [Buffer][b], the raw Buffer of the cid |
| 21 | + - String, the base58 encoded version of the cid |
| 22 | +- String, including the ipfs handler, a cid and a path to traverse to, ie: |
| 23 | + - '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66' |
| 24 | + - '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt' |
| 25 | + - 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt' |
| 26 | + |
| 27 | +`options` is an optional object that may contain the following keys: |
| 28 | + - `recursive (false)`: recursively list references of child nodes |
| 29 | + - `unique (false)`: omit duplicate references from output |
| 30 | + - `format ("<dst>")`: output edges with given format. Available tokens: `<src>`, `<dst>`, `<linkname>` |
| 31 | + - `edges (false)`: output references in edge format: `"<src> -> <dst>"` |
| 32 | + - `maxDepth (1)`: only for recursive refs, limits fetch and listing to the given depth |
| 33 | + |
| 34 | +`callback` must follow `function (err, refs) {}` signature, where `err` is an error if the operation was not successful and `refs` is an array of `{ ref: "myref", err: "error msg" }` |
| 35 | + |
| 36 | +If no `callback` is passed, a promise is returned. |
| 37 | + |
| 38 | +**Example:** |
| 39 | + |
| 40 | +```JavaScript |
| 41 | +ipfs.refs(ipfsPath, { recursive: true }, function (err, refs) { |
| 42 | + if (err) { |
| 43 | + throw err |
| 44 | + } |
| 45 | + |
| 46 | + for (const ref of refs) { |
| 47 | + if (ref.err) { |
| 48 | + console.error(ref.err) |
| 49 | + } else { |
| 50 | + console.log(ref.ref) |
| 51 | + // output: "QmHash" |
| 52 | + } |
| 53 | + } |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +#### `refsReadableStream` |
| 58 | + |
| 59 | +> Output references using a [Readable Stream][rs] |
| 60 | +
|
| 61 | +##### `ipfs.refsReadableStream(ipfsPath, [options])` -> [Readable Stream][rs] |
| 62 | + |
| 63 | +`options` is an optional object argument identical to the options for [ipfs.refs](#refs) |
| 64 | + |
| 65 | +**Example:** |
| 66 | + |
| 67 | +```JavaScript |
| 68 | +const stream = ipfs.refsReadableStream(ipfsPath, { recursive: true }) |
| 69 | +stream.on('data', function (ref) { |
| 70 | + // 'ref' will be of the form |
| 71 | + // { |
| 72 | + // ref: 'QmHash', |
| 73 | + // err: 'err message' |
| 74 | + // } |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +#### `refsPullStream` |
| 79 | + |
| 80 | +> Output references using a [Pull Stream][ps]. |
| 81 | +
|
| 82 | +##### `ipfs.refsReadableStream(ipfsPath, [options])` -> [Pull Stream][ps] |
| 83 | + |
| 84 | +`options` is an optional object argument identical to the options for [ipfs.refs](#refs) |
| 85 | + |
| 86 | +**Example:** |
| 87 | + |
| 88 | +```JavaScript |
| 89 | +const stream = ipfs.refsPullStream(ipfsPath, { recursive: true }) |
| 90 | + |
| 91 | +pull( |
| 92 | + stream, |
| 93 | + pull.collect((err, values) => { |
| 94 | + // values will be an array of objects, each one of the form |
| 95 | + // { |
| 96 | + // ref: 'QmHash', |
| 97 | + // err: 'err message' |
| 98 | + // } |
| 99 | + }) |
| 100 | +) |
| 101 | +``` |
| 102 | + |
| 103 | +#### `refs.local` |
| 104 | + |
| 105 | +> Output all local references (CIDs of all blocks in the blockstore) |
| 106 | +
|
| 107 | +##### `ipfs.refs.local([callback])` |
| 108 | + |
| 109 | +`callback` must follow `function (err, refs) {}` signature, where `err` is an error if the operation was not successful and `refs` is an array of `{ ref: "myref", err: "error msg" }` |
| 110 | + |
| 111 | +If no `callback` is passed, a promise is returned. |
| 112 | + |
| 113 | +**Example:** |
| 114 | + |
| 115 | +```JavaScript |
| 116 | +ipfs.refs.local(function (err, refs) { |
| 117 | + if (err) { |
| 118 | + throw err |
| 119 | + } |
| 120 | + |
| 121 | + for (const ref of refs) { |
| 122 | + if (ref.err) { |
| 123 | + console.error(ref.err) |
| 124 | + } else { |
| 125 | + console.log(ref.ref) |
| 126 | + // output: "QmHash" |
| 127 | + } |
| 128 | + } |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | +#### `refs.localReadableStream` |
| 133 | + |
| 134 | +> Output all local references using a [Readable Stream][rs] |
| 135 | +
|
| 136 | +##### `ipfs.localReadableStream()` -> [Readable Stream][rs] |
| 137 | + |
| 138 | +**Example:** |
| 139 | + |
| 140 | +```JavaScript |
| 141 | +const stream = ipfs.refs.localReadableStream() |
| 142 | +stream.on('data', function (ref) { |
| 143 | + // 'ref' will be of the form |
| 144 | + // { |
| 145 | + // ref: 'QmHash', |
| 146 | + // err: 'err message' |
| 147 | + // } |
| 148 | +}) |
| 149 | +``` |
| 150 | + |
| 151 | +#### `refs.localPullStream` |
| 152 | + |
| 153 | +> Output all local references using a [Pull Stream][ps]. |
| 154 | +
|
| 155 | +##### `ipfs.refs.localReadableStream()` -> [Pull Stream][ps] |
| 156 | + |
| 157 | +**Example:** |
| 158 | + |
| 159 | +```JavaScript |
| 160 | +const stream = ipfs.refs.localPullStream() |
| 161 | + |
| 162 | +pull( |
| 163 | + stream, |
| 164 | + pull.collect((err, values) => { |
| 165 | + // values will be an array of objects, each one of the form |
| 166 | + // { |
| 167 | + // ref: 'QmHash', |
| 168 | + // err: 'err message' |
| 169 | + // } |
| 170 | + }) |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +A great source of [examples][] can be found in the tests for this API. |
| 175 | + |
| 176 | +[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files-regular |
| 177 | +[b]: https://www.npmjs.com/package/buffer |
| 178 | +[rs]: https://www.npmjs.com/package/readable-stream |
| 179 | +[ps]: https://www.npmjs.com/package/pull-stream |
| 180 | +[cid]: https://www.npmjs.com/package/cids |
| 181 | +[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob |
0 commit comments