Skip to content

Commit 9517501

Browse files
committed
fix: remove boolean trap constructor and update readme
1 parent e232acf commit 9517501

File tree

3 files changed

+151
-90
lines changed

3 files changed

+151
-90
lines changed

README.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ipfs-unixfs JavaScript Implementation
1+
# ipfs-unixfs JavaScript Implementation <!-- omit in toc -->
22

33
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
44
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
@@ -14,32 +14,29 @@
1414
1515
[The unixfs spec can be found inside the ipfs/specs repository](http://github.com/ipfs/specs)
1616

17-
## Lead Maintainer
17+
## Lead Maintainer <!-- omit in toc -->
1818

1919
[Alex Potsides](https://github.com/achingbrain)
2020

21-
## Table of Contents
22-
23-
- [ipfs-unixfs JavaScript Implementation](#ipfs-unixfs-javascript-implementation)
24-
- [Lead Maintainer](#lead-maintainer)
25-
- [Table of Contents](#table-of-contents)
26-
- [Install](#install)
27-
- [npm](#npm)
28-
- [Use in Node.js](#use-in-nodejs)
29-
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify--webpack-or-any-other-bundler)
30-
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
31-
- [Usage](#usage)
32-
- [Examples](#examples)
33-
- [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks)
34-
- [Create a directory that contains several files](#create-a-directory-that-contains-several-files)
35-
- [API](#api)
36-
- [unixfs Data Structure](#unixfs-data-structure)
37-
- [create an unixfs Data element](#create-an-unixfs-data-element)
38-
- [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list)
39-
- [get total fileSize](#get-total-filesize)
40-
- [marshal and unmarshal](#marshal-and-unmarshal)
41-
- [Contribute](#contribute)
42-
- [License](#license)
21+
## Table of Contents <!-- omit in toc -->
22+
23+
- [Install](#install)
24+
- [npm](#npm)
25+
- [Use in Node.js](#use-in-nodejs)
26+
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
27+
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
28+
- [Usage](#usage)
29+
- [Examples](#examples)
30+
- [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks)
31+
- [Create a directory that contains several files](#create-a-directory-that-contains-several-files)
32+
- [API](#api)
33+
- [unixfs Data Structure](#unixfs-data-structure)
34+
- [create an unixfs Data element](#create-an-unixfs-data-element)
35+
- [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list)
36+
- [get total fileSize](#get-total-filesize)
37+
- [marshal and unmarshal](#marshal-and-unmarshal)
38+
- [Contribute](#contribute)
39+
- [License](#license)
4340

4441
## Install
4542

@@ -80,7 +77,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in
8077
#### Create a file composed by several blocks
8178

8279
```JavaScript
83-
var data = new Unixfs('file')
80+
const data = new Unixfs({ type: 'file' })
8481
data.addBlockSize(256) // add the size of each block
8582
data.addBlockSize(256)
8683
// ...
@@ -91,14 +88,16 @@ data.addBlockSize(256)
9188
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.
9289

9390
```JavaScript
94-
var data = new Unixfs('directory')
91+
const data = new Unixfs({ type: 'directory' })
9592
```
9693

9794
## API
9895

9996
#### unixfs Data Structure
10097

10198
```protobuf
99+
syntax = "proto2";
100+
102101
message Data {
103102
enum DataType {
104103
Raw = 0;
@@ -113,23 +112,36 @@ message Data {
113112
optional bytes Data = 2;
114113
optional uint64 filesize = 3;
115114
repeated uint64 blocksizes = 4;
116-
117115
optional uint64 hashType = 5;
118116
optional uint64 fanout = 6;
117+
optional uint32 mode = 7;
118+
optional int64 mtime = 8;
119119
}
120120
121121
message Metadata {
122-
optional string MimeType = 1;
122+
required string MimeType = 1;
123123
}
124124
```
125125

126126
#### create an unixfs Data element
127127

128128
```JavaScript
129-
var data = new UnixFS(<type>, [<content>])
129+
const data = new UnixFS([options])
130130
```
131131

132-
Type can be: `['raw', 'directory', 'file', 'metadata', 'symlink', 'hamt-sharded-directory']`
132+
`options` is an optional object argument that might include the following keys:
133+
134+
- type (string, default `file`): The type of UnixFS node. Can be:
135+
- `raw`
136+
- `directory`
137+
- `file`
138+
- `metadata`
139+
- `symlink`
140+
- `hamt-sharded-directory`
141+
- data (Buffer): The optional data field for this node
142+
- blockSizes (Array, default: `[]`): If this is a `file` node that is made up of multiple blocks, `blockSizes` is a list numbers that represent the size of the file chunks stored in each child node. It is used to calculate the total file size.
143+
- mode (Number, default `0644` for files, `0755` for directories/hamt-sharded-directories) file mode
144+
- mtime (Date, default `0`): The modification time of this node
133145

134146
#### add and remove a block size to the block size list
135147

@@ -149,9 +161,9 @@ data.fileSize() // => size in bytes
149161

150162
#### marshal and unmarshal
151163

152-
```
153-
var marshaled = data.marshal()
154-
var unmarshaled = Unixfs.unmarshal(marshaled)
164+
```javascript
165+
const marshaled = data.marshal()
166+
const unmarshaled = Unixfs.unmarshal(marshaled)
155167
```
156168

157169
## Contribute

src/index.js

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,46 @@ const dirTypes = [
2020
'hamt-sharded-directory'
2121
]
2222

23-
function Data (type, data) {
23+
const DEFAULT_FILE_MODE = parseInt('0644', 8)
24+
const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8)
25+
26+
function Data (arg1, arg2) {
2427
if (!(this instanceof Data)) {
25-
return new Data(type, data)
28+
return new Data(arg1, arg2)
29+
}
30+
31+
if (arg1 == null) {
32+
arg1 = {
33+
type: 'file'
34+
}
35+
}
36+
37+
let { type, data, blockSizes, mtime, mode } = arg1
38+
39+
if (typeof arg1 === 'string' || arg1 instanceof String) {
40+
// support old-style constructor
41+
type = arg1
42+
data = arg2
2643
}
44+
2745
if (types.indexOf(type) === -1) {
2846
throw new Error('Type: ' + type + ' is not valid')
2947
}
3048

3149
this.type = type
3250
this.data = data
33-
this.blockSizes = []
51+
this.blockSizes = blockSizes || []
52+
53+
this.mtime = mtime || new Date(0)
54+
this.mode = mode
55+
56+
if (this.mode === undefined && type === 'file') {
57+
this.mode = DEFAULT_FILE_MODE
58+
}
59+
60+
if (this.mode === undefined && type.includes('directory')) {
61+
this.mode = DEFAULT_DIRECTORY_MODE
62+
}
3463

3564
this.addBlockSize = (size) => {
3665
this.blockSizes.push(size)
@@ -88,12 +117,24 @@ function Data (type, data) {
88117

89118
if (!isNaN(this.mode)) {
90119
mode = this.mode
120+
121+
if (mode === DEFAULT_FILE_MODE && this.type === 'file') {
122+
mode = undefined
123+
}
124+
125+
if (mode === DEFAULT_DIRECTORY_MODE && this.type.includes('directory')) {
126+
mode = undefined
127+
}
91128
}
92129

93130
let mtime
94131

95132
if (this.mtime) {
96133
mtime = Math.round(this.mtime.getTime() / 1000)
134+
135+
if (mtime === 0) {
136+
mtime = undefined
137+
}
97138
}
98139

99140
return unixfsData.encode({
@@ -112,19 +153,14 @@ function Data (type, data) {
112153
// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
113154
Data.unmarshal = (marshaled) => {
114155
const decoded = unixfsData.decode(marshaled)
115-
const data = decoded.hasData() ? decoded.Data : undefined
116-
const obj = new Data(types[decoded.Type], data)
117-
obj.blockSizes = decoded.blocksizes
118-
119-
if (decoded.hasMode()) {
120-
obj.mode = decoded.mode
121-
}
122-
123-
if (decoded.hasMtime()) {
124-
obj.mtime = new Date(decoded.mtime * 1000)
125-
}
126156

127-
return obj
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+
})
128164
}
129165

130166
exports = module.exports = Data

0 commit comments

Comments
 (0)