|
1 |
| -ipfs-unixfs |
2 |
| -=========== |
| 1 | +ipfs-unixfs JavaScript Implementation |
| 2 | +===================================== |
3 | 3 |
|
4 | 4 | [](http://ipn.io)
|
5 |
| -[[](http://webchat.freenode.net/?channels=%23ipfs) |
6 |
| -[](https://travis-ci.org/ipfs/js-ipfs-unixfs) |
| 5 | +[](http://webchat.freenode.net/?channels=%23ipfs) |
| 6 | +[](https://travis-ci.org/ipfs/js-ipfs-unixfs) |
7 | 7 | 
|
8 | 8 | [](https://david-dm.org/ipfs/js-ipfs-unixfs)
|
9 | 9 | [](https://github.com/feross/standard)
|
10 | 10 |
|
11 | 11 | > JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)
|
12 | 12 |
|
| 13 | +[The unixfs spec can be found inside the ipfs/specs repository](http://github.com/ipfs/specs) |
| 14 | + |
| 15 | +# Installation |
| 16 | + |
| 17 | +## npm |
| 18 | + |
| 19 | +```sh |
| 20 | +> npm i ipfs-unixfs |
| 21 | +``` |
| 22 | + |
| 23 | +## Use in Node.js |
| 24 | + |
| 25 | +```JavaScript |
| 26 | +var Unixfs = require('ipfs-unixfs') |
| 27 | +``` |
| 28 | + |
| 29 | +## Use in a browser with browserify, webpack or any other bundler |
| 30 | + |
| 31 | +The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process. |
| 32 | + |
| 33 | +```JavaScript |
| 34 | +var Unixfs = require('ipfs-unixfs') |
| 35 | +``` |
| 36 | + |
| 37 | +## Use in a browser Using a script tag |
| 38 | + |
| 39 | +Loading this module through a script tag will make the `Unixfs` obj available in the global namespace. |
| 40 | + |
| 41 | +```html |
| 42 | +<script src="https://npmcdn.com/ipfs-unixfs/dist/index.min.js"></script> |
| 43 | +<!-- OR --> |
| 44 | +<script src="https://npmcdn.com/ipfs-unixfs/dist/index.js"></script> |
| 45 | +``` |
| 46 | + |
13 | 47 | # Usage
|
| 48 | + |
| 49 | +## Examples |
| 50 | + |
| 51 | +#### Create a file composed by several blocks |
| 52 | + |
| 53 | +```JavaScript |
| 54 | +var data = new Unixfs('file') |
| 55 | +data.addBlockSize(256) // add the size of each block |
| 56 | +data.addBlockSize(256) |
| 57 | +// ... |
| 58 | +``` |
| 59 | + |
| 60 | +#### Create a directory that contains several files |
| 61 | + |
| 62 | +Creating a directory that contains several files is achieve by creating a unixfs element that identifies a MerkleDAG node as a directory. The links of that MerkleDAG node are the files that are contained in this directory. |
| 63 | + |
| 64 | +```JavaScript |
| 65 | +var data = new Unixfs('directory') |
| 66 | +``` |
| 67 | + |
| 68 | +## API |
| 69 | + |
| 70 | +#### unixfs Data Structure |
| 71 | + |
| 72 | +```protobuf |
| 73 | +message Data { |
| 74 | + enum DataType { |
| 75 | + Raw = 0; |
| 76 | + Directory = 1; |
| 77 | + File = 2; |
| 78 | + Metadata = 3; |
| 79 | + Symlink = 4; |
| 80 | + } |
| 81 | +
|
| 82 | + required DataType Type = 1; |
| 83 | + optional bytes Data = 2; |
| 84 | + optional uint64 filesize = 3; |
| 85 | + repeated uint64 blocksizes = 4; |
| 86 | +} |
| 87 | +
|
| 88 | +message Metadata { |
| 89 | + required string MimeType = 1; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +#### create an unixfs Data element |
| 94 | + |
| 95 | +```JavaScript |
| 96 | +var data = new UnixFS(<type>, [<content>]) |
| 97 | +``` |
| 98 | + |
| 99 | +Type can be: `['raw', 'directory', 'file', 'metadata', 'symlink']` |
| 100 | + |
| 101 | +#### add and remove a block size to the block size list |
| 102 | + |
| 103 | +```JavaScript |
| 104 | +data.addBlockSize(<size in bytes>) |
| 105 | +``` |
| 106 | + |
| 107 | +```JavaScript |
| 108 | +data.removeBlockSize(<index>) |
| 109 | +``` |
| 110 | + |
| 111 | +#### get total fileSize |
| 112 | + |
| 113 | +```JavaScript |
| 114 | +data.fileSize() // => size in bytes |
| 115 | +``` |
| 116 | + |
| 117 | +#### marshal and unmarshal |
| 118 | + |
| 119 | +``` |
| 120 | +var marsheled = data.marshal() |
| 121 | +var unmarsheled = Unixfs.unmarshal(marsheled) |
| 122 | +``` |
0 commit comments