Skip to content

Commit 45b0b30

Browse files
committed
fix: address PR comments, refactor to modern class
1 parent 9517501 commit 45b0b30

File tree

5 files changed

+95
-62
lines changed

5 files changed

+95
-62
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
1111
![](https://img.shields.io/badge/Node.js-%3E%3D8.0.0-orange.svg?style=flat-square)
1212

13-
> JavaScript implementation of IPFS' unixfs (a Unix FileSystem files representation on top of a MerkleDAG)
13+
> JavaScript implementation of IPFS' UnixFS (a Unix FileSystem files representation on top of a MerkleDAG)
1414
15-
[The unixfs spec can be found inside the ipfs/specs repository](http://github.com/ipfs/specs)
15+
The UnixFS spec can be found inside the [ipfs/specs repository](http://github.com/ipfs/specs)
1616

1717
## Lead Maintainer <!-- omit in toc -->
1818

@@ -30,11 +30,12 @@
3030
- [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks)
3131
- [Create a directory that contains several files](#create-a-directory-that-contains-several-files)
3232
- [API](#api)
33-
- [unixfs Data Structure](#unixfs-data-structure)
33+
- [UnixFS Data Structure](#unixfs-data-structure)
3434
- [create an unixfs Data element](#create-an-unixfs-data-element)
3535
- [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list)
3636
- [get total fileSize](#get-total-filesize)
3737
- [marshal and unmarshal](#marshal-and-unmarshal)
38+
- [is this UnixFS entry a directory?](#is-this-unixfs-entry-a-directory)
3839
- [Contribute](#contribute)
3940
- [License](#license)
4041

@@ -49,20 +50,20 @@
4950
### Use in Node.js
5051

5152
```JavaScript
52-
var Unixfs = require('ipfs-unixfs')
53+
var UnixFS = require('ipfs-unixfs')
5354
```
5455

5556
### Use in a browser with browserify, webpack or any other bundler
5657

5758
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.
5859

5960
```JavaScript
60-
var Unixfs = require('ipfs-unixfs')
61+
var UnixFS = require('ipfs-unixfs')
6162
```
6263

6364
### Use in a browser Using a script tag
6465

65-
Loading this module through a script tag will make the `Unixfs` obj available in the global namespace.
66+
Loading this module through a script tag will make the `UnixFS` obj available in the global namespace.
6667

6768
```html
6869
<script src="https://npmcdn.com/ipfs-unixfs/dist/index.min.js"></script>
@@ -77,7 +78,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in
7778
#### Create a file composed by several blocks
7879

7980
```JavaScript
80-
const data = new Unixfs({ type: 'file' })
81+
const data = new UnixFS({ type: 'file' })
8182
data.addBlockSize(256) // add the size of each block
8283
data.addBlockSize(256)
8384
// ...
@@ -88,12 +89,12 @@ data.addBlockSize(256)
8889
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.
8990

9091
```JavaScript
91-
const data = new Unixfs({ type: 'directory' })
92+
const data = new UnixFS({ type: 'directory' })
9293
```
9394

9495
## API
9596

96-
#### unixfs Data Structure
97+
#### UnixFS Data Structure
9798

9899
```protobuf
99100
syntax = "proto2";
@@ -119,7 +120,7 @@ message Data {
119120
}
120121
121122
message Metadata {
122-
required string MimeType = 1;
123+
optional string MimeType = 1;
123124
}
124125
```
125126

@@ -131,7 +132,7 @@ const data = new UnixFS([options])
131132

132133
`options` is an optional object argument that might include the following keys:
133134

134-
- type (string, default `file`): The type of UnixFS node. Can be:
135+
- type (string, default `file`): The type of UnixFS entry. Can be:
135136
- `raw`
136137
- `directory`
137138
- `file`
@@ -166,6 +167,16 @@ const marshaled = data.marshal()
166167
const unmarshaled = Unixfs.unmarshal(marshaled)
167168
```
168169

170+
#### is this UnixFS entry a directory?
171+
172+
```JavaScript
173+
const dir = new Data({ type: 'directory' })
174+
dir.isDirectory() // true
175+
176+
const file = new Data({ type: 'file' })
177+
file.isDirectory() // false
178+
```
179+
169180
## Contribute
170181

171182
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs-unixfs/issues)!

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
"devDependencies": {
3939
"aegir": "^20.4.1",
4040
"chai": "^4.2.0",
41-
"dirty-chai": "^2.0.1",
42-
"safe-buffer": "^5.1.2"
41+
"dirty-chai": "^2.0.1"
4342
},
4443
"dependencies": {
4544
"protons": "^1.1.0"

src/index.js

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
const protons = require('protons')
44
const pb = protons(require('./unixfs.proto'))
5-
// encode/decode
65
const unixfsData = pb.Data
7-
// const unixfsMetadata = pb.MetaData // encode/decode
86

97
const types = [
108
'raw',
@@ -23,55 +21,91 @@ const dirTypes = [
2321
const DEFAULT_FILE_MODE = parseInt('0644', 8)
2422
const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8)
2523

26-
function Data (arg1, arg2) {
27-
if (!(this instanceof Data)) {
28-
return new Data(arg1, arg2)
29-
}
30-
31-
if (arg1 == null) {
32-
arg1 = {
24+
function parseArgs (args) {
25+
if (args.length === 0) {
26+
return {
3327
type: 'file'
3428
}
3529
}
3630

37-
let { type, data, blockSizes, mtime, mode } = arg1
38-
39-
if (typeof arg1 === 'string' || arg1 instanceof String) {
31+
if (args.length === 2) {
4032
// support old-style constructor
41-
type = arg1
42-
data = arg2
33+
return {
34+
type: args[0],
35+
data: args[1]
36+
}
4337
}
4438

45-
if (types.indexOf(type) === -1) {
46-
throw new Error('Type: ' + type + ' is not valid')
39+
if (typeof args[0] === 'string' || args[0] instanceof String) {
40+
return {
41+
type: args[0]
42+
}
43+
}
44+
45+
return args[0]
46+
}
47+
48+
class Data {
49+
// decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md
50+
static unmarshal (marshaled) {
51+
const decoded = unixfsData.decode(marshaled)
52+
53+
return new Data({
54+
type: types[decoded.Type],
55+
data: decoded.hasData() ? decoded.Data : undefined,
56+
blockSizes: decoded.blocksizes,
57+
mode: decoded.hasMode() ? decoded.mode : undefined,
58+
mtime: decoded.hasMtime() ? new Date(decoded.mtime * 1000) : undefined
59+
})
4760
}
4861

49-
this.type = type
50-
this.data = data
51-
this.blockSizes = blockSizes || []
62+
constructor (...args) {
63+
const {
64+
type,
65+
data,
66+
blockSizes,
67+
hashType,
68+
fanout,
69+
mtime,
70+
mode
71+
} = parseArgs(args)
72+
73+
if (!types.includes(type)) {
74+
throw new Error('Type: ' + type + ' is not valid')
75+
}
5276

53-
this.mtime = mtime || new Date(0)
54-
this.mode = mode
77+
this.type = type
78+
this.data = data
79+
this.hashType = hashType
80+
this.fanout = fanout
81+
this.blockSizes = blockSizes || []
82+
this.mtime = mtime || new Date(0)
83+
this.mode = mode
5584

56-
if (this.mode === undefined && type === 'file') {
57-
this.mode = DEFAULT_FILE_MODE
85+
if (this.mode === undefined && type === 'file') {
86+
this.mode = DEFAULT_FILE_MODE
87+
}
88+
89+
if (this.mode === undefined && this.isDirectory()) {
90+
this.mode = DEFAULT_DIRECTORY_MODE
91+
}
5892
}
5993

60-
if (this.mode === undefined && type.includes('directory')) {
61-
this.mode = DEFAULT_DIRECTORY_MODE
94+
isDirectory () {
95+
return dirTypes.includes(this.type)
6296
}
6397

64-
this.addBlockSize = (size) => {
98+
addBlockSize (size) {
6599
this.blockSizes.push(size)
66100
}
67101

68-
this.removeBlockSize = (index) => {
102+
removeBlockSize (index) {
69103
this.blockSizes.splice(index, 1)
70104
}
71105

72106
// data.length + blockSizes
73-
this.fileSize = () => {
74-
if (dirTypes.indexOf(this.type) >= 0) {
107+
fileSize () {
108+
if (this.isDirectory()) {
75109
// dirs don't have file size
76110
return undefined
77111
}
@@ -80,14 +114,16 @@ function Data (arg1, arg2) {
80114
this.blockSizes.forEach((size) => {
81115
sum += size
82116
})
117+
83118
if (this.data) {
84119
sum += this.data.length
85120
}
121+
86122
return sum
87123
}
88124

89125
// encode to protobuf
90-
this.marshal = () => {
126+
marshal () {
91127
let type
92128

93129
switch (this.type) {
@@ -115,7 +151,7 @@ function Data (arg1, arg2) {
115151

116152
let mode
117153

118-
if (!isNaN(this.mode)) {
154+
if (!isNaN(parseInt(this.mode))) {
119155
mode = this.mode
120156

121157
if (mode === DEFAULT_FILE_MODE && this.type === 'file') {
@@ -144,23 +180,10 @@ function Data (arg1, arg2) {
144180
blocksizes: blockSizes,
145181
hashType: this.hashType,
146182
fanout: this.fanout,
147-
mode: mode,
148-
mtime: mtime
183+
mode,
184+
mtime
149185
})
150186
}
151187
}
152188

153-
// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
154-
Data.unmarshal = (marshaled) => {
155-
const decoded = unixfsData.decode(marshaled)
156-
157-
return new Data({
158-
type: types[decoded.Type],
159-
data: decoded.hasData() ? decoded.Data : undefined,
160-
blockSizes: decoded.blocksizes,
161-
mode: decoded.hasMode() ? decoded.mode : undefined,
162-
mtime: decoded.hasMtime() ? new Date(decoded.mtime * 1000) : undefined
163-
})
164-
}
165-
166-
exports = module.exports = Data
189+
module.exports = Data

src/unixfs.proto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ message Data {
2424
}
2525
2626
message Metadata {
27-
required string MimeType = 1;
27+
optional string MimeType = 1;
2828
}
2929
`

test/unixfs-format.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const raw = loadFixture('test/fixtures/raw.unixfs')
1212
const directory = loadFixture('test/fixtures/directory.unixfs')
1313
const file = loadFixture('test/fixtures/file.txt.unixfs')
1414
const symlink = loadFixture('test/fixtures/symlink.txt.unixfs')
15-
const Buffer = require('safe-buffer').Buffer
15+
const { Buffer } = require('buffer')
1616

1717
describe('unixfs-format', () => {
1818
it('defaults to file', () => {

0 commit comments

Comments
 (0)