|
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="URL/"></script> |
| 43 | +``` |
| 44 | + |
13 | 45 | # Usage |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +#### Create a file composed by several blocks |
| 50 | + |
| 51 | +```JavaScript |
| 52 | +var data = new UnixFS('file') |
| 53 | +data.addBlockSize(256) // add the size of each block |
| 54 | +data.addBlockSize(256) |
| 55 | +// ... |
| 56 | +``` |
| 57 | + |
| 58 | +#### Create a directory that contains several files |
| 59 | + |
| 60 | +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. |
| 61 | + |
| 62 | +```JavaScript |
| 63 | +var data = new Unixfs('directory') |
| 64 | +``` |
| 65 | + |
| 66 | +## API |
| 67 | + |
| 68 | +#### unixfs Data Structure |
| 69 | + |
| 70 | +```protobuf |
| 71 | +message Data { |
| 72 | + enum DataType { |
| 73 | + Raw = 0; |
| 74 | + Directory = 1; |
| 75 | + File = 2; |
| 76 | + Metadata = 3; |
| 77 | + Symlink = 4; |
| 78 | + } |
| 79 | +
|
| 80 | + required DataType Type = 1; |
| 81 | + optional bytes Data = 2; |
| 82 | + optional uint64 filesize = 3; |
| 83 | + repeated uint64 blocksizes = 4; |
| 84 | +} |
| 85 | +
|
| 86 | +message Metadata { |
| 87 | + required string MimeType = 1; |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +#### create an unixfs Data element |
| 92 | + |
| 93 | +```JavaScript |
| 94 | +var data = new UnixFS(<type>, [<content>]) |
| 95 | +``` |
| 96 | + |
| 97 | +Type can be: `['raw', 'directory', 'file', 'metadata', 'symlink']` |
| 98 | + |
| 99 | +#### add and remove a block size to the block size list |
| 100 | + |
| 101 | +```JavaScript |
| 102 | +data.addBlockSize(<size in bytes>) |
| 103 | +``` |
| 104 | + |
| 105 | +```JavaScript |
| 106 | +data.removeBlockSize(<index>) |
| 107 | +``` |
| 108 | + |
| 109 | +#### get total fileSize |
| 110 | + |
| 111 | +```JavaScript |
| 112 | +data.fileSize() // => size in bytes |
| 113 | +``` |
| 114 | + |
| 115 | +#### marshal and unmarshal |
| 116 | + |
| 117 | +``` |
| 118 | +var marsheled = data.marshal() |
| 119 | +var unmarsheled = Unixfs.unmarshal(marsheled) |
| 120 | +``` |
0 commit comments