Skip to content

Commit 1925cfe

Browse files
committed
chore: adding prettier and improving tooling
1 parent 9e96c6e commit 1925cfe

26 files changed

+4077
-2344
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.eslintrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
{
3+
"env": {
4+
"node": true,
5+
"es6": true
6+
},
7+
"plugins": [
8+
"prettier"
9+
],
10+
"extends": [
11+
"standard",
12+
"plugin:prettier/recommended"
13+
]
14+
}

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

package-lock.json

Lines changed: 3249 additions & 1799 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,37 @@
44
"description": "A libp2p compatible pubsub module of the pulsarcast system",
55
"main": "src/index.js",
66
"scripts": {
7+
"fmt": "prettier --write '{,!(coverage)/**/}*.js' && eslint --fix .",
8+
"fmt:ci": "prettier -l '{,!(coverage)/**/}*.js'",
9+
"lint": "eslint .",
710
"test": "nyc --reporter=html --reporter=text mocha --recursive --exit",
8-
"test:ci": "npm run lint && npm test",
9-
"lint": "standard . | snazzy"
11+
"test:ci": "npm run lint && npm run fmt:ci && npm test"
12+
},
13+
"nyc": {
14+
"exclude": [
15+
"test/**",
16+
"coverage/*",
17+
"npm-debug.log",
18+
".nyc_output"
19+
],
20+
"report-dir": "./coverage",
21+
"cache": true,
22+
"all": true,
23+
"extension": [
24+
".js"
25+
]
26+
},
27+
"husky": {
28+
"hooks": {
29+
"pre-commit": "lint-staged && npm run test"
30+
}
31+
},
32+
"lint-staged": {
33+
"*.js": [
34+
"eslint --fix",
35+
"prettier --write",
36+
"git add"
37+
]
1038
},
1139
"pre-commit": [
1240
"lint",
@@ -49,9 +77,17 @@
4977
"libp2p-spdy": "^0.13.1",
5078
"libp2p-tcp": "jgantunes/js-libp2p-tcp",
5179
"mocha": "^5.2.0",
52-
"nyc": "^13.3.0",
53-
"pre-commit": "^1.2.2",
54-
"snazzy": "^7.1.1",
55-
"standard": "^11.0.1"
80+
"nyc": "^14.0.0",
81+
"eslint": "^5.11.1",
82+
"eslint-config-prettier": "^3.3.0",
83+
"eslint-config-standard": "^12.0.0",
84+
"eslint-plugin-import": "^2.14.0",
85+
"eslint-plugin-node": "^8.0.0",
86+
"eslint-plugin-prettier": "^3.0.1",
87+
"eslint-plugin-promise": "^4.0.1",
88+
"eslint-plugin-standard": "^4.0.0",
89+
"husky": "^1.3.0",
90+
"lint-staged": "^8.1.0",
91+
"prettier": "^1.15.3"
5692
}
5793
}

src/dag/event-node.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ const dagCBOR = require('ipld-dag-cbor')
66
const CID = require('cids')
77

88
const config = require('../config')
9-
const {
10-
linkUnmarshalling,
11-
linkMarshalling
12-
} = require('./utils')
9+
const { linkUnmarshalling, linkMarshalling } = require('./utils')
1310

1411
class EventNode {
15-
constructor (topicCID, author, payload, options = {}) {
12+
constructor(topicCID, author, payload, options = {}) {
1613
// TODO check it is a CID maybe?
1714
assert(topicCID, 'Need a topicCID object to create an event node')
1815
assert(author, 'Need an author to create an event node')
@@ -27,9 +24,11 @@ class EventNode {
2724
this.metadata = createMetadata(options.metadata)
2825
}
2926

30-
static deserialize (event) {
27+
static deserialize(event) {
3128
const topicCID = linkUnmarshalling(event.topic)
32-
const publisher = event.publisher ? PeerId.createFromBytes(event.publisher) : null
29+
const publisher = event.publisher
30+
? PeerId.createFromBytes(event.publisher)
31+
: null
3332
const author = PeerId.createFromBytes(event.author)
3433
const payload = event.payload
3534
const parent = linkUnmarshalling(event.parent)
@@ -41,18 +40,18 @@ class EventNode {
4140
})
4241
}
4342

44-
static deserializeCBOR (event, cb) {
43+
static deserializeCBOR(event, cb) {
4544
dagCBOR.util.deserialize(event, (err, result) => {
4645
if (err) return cb(err)
4746
cb(null, EventNode.deserialize(result))
4847
})
4948
}
5049

51-
get isPublished () {
50+
get isPublished() {
5251
return Boolean(this.publisher)
5352
}
5453

55-
getReadableFormat () {
54+
getReadableFormat() {
5655
return {
5756
topicCID: this.topicCID.toBaseEncodedString(),
5857
author: this.author.toB58String(),
@@ -64,11 +63,11 @@ class EventNode {
6463
}
6564
}
6665

67-
getCID (cb) {
66+
getCID(cb) {
6867
dagCBOR.util.cid(this.serialize(), cb)
6968
}
7069

71-
serialize () {
70+
serialize() {
7271
return {
7372
topic: linkMarshalling(this.topicCID),
7473
publisher: this.isPublished ? this.publisher.toBytes() : null,
@@ -82,13 +81,13 @@ class EventNode {
8281
}
8382
}
8483

85-
serializeCBOR (cb) {
84+
serializeCBOR(cb) {
8685
const serialized = this.serialize()
8786
dagCBOR.util.serialize(serialized, cb)
8887
}
8988
}
9089

91-
function createMetadata ({
90+
function createMetadata({
9291
created = new Date(),
9392
protocolVersion = config.protocol
9493
} = {}) {

src/dag/event-tree.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const log = require('../utils/logger')
77
// TODO right now memory usage grows indefinitely
88
class EventTree {
99
// TODO probably need access to the DHT?
10-
constructor (topicNode) {
10+
constructor(topicNode) {
1111
// TODO check it is a CID maybe?
1212
assert(topicNode, 'Need a topicNode object to create an event tree')
1313
log.trace(`New event tree for topic ${topicNode.name}`)
@@ -21,24 +21,22 @@ class EventTree {
2121
})
2222
}
2323

24-
addNew (eventNode, options, cb) {
24+
addNew(eventNode, options, cb) {
2525
const done = cb || options
26-
const {parent} = options
26+
const { parent } = options
2727
const eventLinking = this.topicNode.metadata.eventLinking
2828

2929
if (eventLinking === 'custom' && !parent) {
3030
return done(new Error('Event requires custom parent to be provided'))
3131
}
3232

3333
// Set the parent link
34-
eventNode.parent = eventLinking === 'custom'
35-
? parent
36-
: this.mostRecent
34+
eventNode.parent = eventLinking === 'custom' ? parent : this.mostRecent
3735

3836
this.add(eventNode, done)
3937
}
4038

41-
add (eventNode, cb) {
39+
add(eventNode, cb) {
4240
eventNode.getCID((err, eventCID) => {
4341
if (err) return cb(err)
4442

@@ -59,14 +57,13 @@ class EventTree {
5957
})
6058
}
6159

62-
get (eventCID) {
60+
get(eventCID) {
6361
return this.eventTree.get(eventCID.toBaseEncodedString())
6462
}
6563

6664
// TODO to should be optional and could be a CID or a number
6765
// defaults to 1 level of resolution
68-
resolve (fromCID, to) {
69-
}
66+
resolve(fromCID, to) {}
7067
}
7168

7269
module.exports = EventTree

src/dag/topic-node.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ const dagCBOR = require('ipld-dag-cbor')
66
const CID = require('cids')
77

88
const config = require('../config')
9-
const eventLinkTypes = require('../messages/protobuffers').TopicDescriptor.MetaData.EventLinking
10-
const {
11-
linkUnmarshalling,
12-
linkMarshalling
13-
} = require('./utils')
9+
const eventLinkTypes = require('../messages/protobuffers').TopicDescriptor
10+
.MetaData.EventLinking
11+
const { linkUnmarshalling, linkMarshalling } = require('./utils')
1412

1513
class TopicNode {
16-
constructor (name, author, options = {}) {
14+
constructor(name, author, options = {}) {
1715
assert(author, 'Need an author to create a topic node')
1816

1917
this.name = name
@@ -23,19 +21,21 @@ class TopicNode {
2321
this.parent = options.parent ? new CID(options.parent) : null
2422
if (options.subTopics) {
2523
this.subTopics = Object.entries(options.subTopics)
26-
.map(([name, topicB58Str]) => ({[name]: new CID(topicB58Str)}))
27-
.reduce((topics, topic) => ({...topic, ...topics}), {})
24+
.map(([name, topicB58Str]) => ({ [name]: new CID(topicB58Str) }))
25+
.reduce((topics, topic) => ({ ...topic, ...topics }), {})
2826
}
2927

3028
this.metadata = createMetadata(options.metadata)
3129
}
3230

33-
static deserialize (topic) {
31+
static deserialize(topic) {
3432
const author = PeerId.createFromBytes(topic.author)
3533
const parent = linkUnmarshalling(topic.parent)
36-
const subTopics = Object.entries(topic['#']).map(([name, dagLink]) => {
37-
return {[name]: linkUnmarshalling(dagLink)}
38-
}).reduce((topics, topic) => ({...topic, ...topics}), {})
34+
const subTopics = Object.entries(topic['#'])
35+
.map(([name, dagLink]) => {
36+
return { [name]: linkUnmarshalling(dagLink) }
37+
})
38+
.reduce((topics, topic) => ({ ...topic, ...topics }), {})
3939

4040
return new TopicNode(topic.name, author, {
4141
subTopics,
@@ -44,14 +44,14 @@ class TopicNode {
4444
})
4545
}
4646

47-
static deserializeCBOR (topic, cb) {
47+
static deserializeCBOR(topic, cb) {
4848
dagCBOR.util.deserialize(topic, (err, result) => {
4949
if (err) return cb(err)
5050
cb(null, TopicNode.deserialize(result))
5151
})
5252
}
5353

54-
getReadableFormat () {
54+
getReadableFormat() {
5555
const allowedPublishers = Array.isArray(this.metadata.allowedPublishers)
5656
? this.metadata.allowedPublishers.map(p => p.toB58String())
5757
: this.metadata.allowedPublishers
@@ -70,40 +70,44 @@ class TopicNode {
7070
}
7171
}
7272

73-
getCID (cb) {
73+
getCID(cb) {
7474
dagCBOR.util.cid(this.serialize(), cb)
7575
}
7676

77-
serialize () {
77+
serialize() {
7878
return {
7979
name: this.name,
8080
author: this.author.toBytes(),
8181
parent: linkMarshalling(this.parent),
82-
'#': Object.entries(this.subTopics).map(([name, cid]) => {
83-
return {[name]: linkMarshalling(cid)}
84-
}).reduce((topics, topic) => ({...topic, ...topics}), {}),
82+
'#': Object.entries(this.subTopics)
83+
.map(([name, cid]) => {
84+
return { [name]: linkMarshalling(cid) }
85+
})
86+
.reduce((topics, topic) => ({ ...topic, ...topics }), {}),
8587
metadata: serializeMetadata(this.metadata)
8688
}
8789
}
8890

89-
serializeCBOR (cb) {
91+
serializeCBOR(cb) {
9092
const serialized = this.serialize()
9193
dagCBOR.util.serialize(serialized, cb)
9294
}
9395
}
9496

95-
function serializeMetadata (metadata) {
96-
const allowedPublishers = {enabled: false, peers: []}
97+
function serializeMetadata(metadata) {
98+
const allowedPublishers = { enabled: false, peers: [] }
9799
if (metadata.allowedPublishers) {
98100
allowedPublishers.enabled = true
99-
allowedPublishers.peers = metadata.allowedPublishers.map((peer) => peer.toBytes())
101+
allowedPublishers.peers = metadata.allowedPublishers.map(peer =>
102+
peer.toBytes()
103+
)
100104
}
101105

102-
const requestToPublish = {enabled: false, peers: []}
106+
const requestToPublish = { enabled: false, peers: [] }
103107
if (metadata.requestToPublish) {
104108
requestToPublish.enabled = true
105109
requestToPublish.peers = Array.isArray(metadata.requestToPublish)
106-
? metadata.requestToPublish.map((peer) => peer.toBytes())
110+
? metadata.requestToPublish.map(peer => peer.toBytes())
107111
: []
108112
}
109113

@@ -116,15 +120,22 @@ function serializeMetadata (metadata) {
116120
}
117121
}
118122

119-
function deserializeMetadata (metadata) {
123+
function deserializeMetadata(metadata) {
120124
const allowedPublishers = metadata.allowedPublishers.enabled
121-
? metadata.allowedPublishers.peers.map((peer) => PeerId.createFromBytes(peer))
125+
? metadata.allowedPublishers.peers.map(peer => PeerId.createFromBytes(peer))
122126
: false
123127

124128
let requestToPublish
125129
if (!metadata.requestToPublish.enabled) requestToPublish = false
126-
if (metadata.requestToPublish.enabled && !metadata.requestToPublish.peers.length) requestToPublish = true
127-
else requestToPublish = metadata.requestToPublish.peers.map((peer) => PeerId.createFromBytes(peer))
130+
if (
131+
metadata.requestToPublish.enabled &&
132+
!metadata.requestToPublish.peers.length
133+
)
134+
requestToPublish = true
135+
else
136+
requestToPublish = metadata.requestToPublish.peers.map(peer =>
137+
PeerId.createFromBytes(peer)
138+
)
128139

129140
return {
130141
...metadata,
@@ -136,7 +147,7 @@ function deserializeMetadata (metadata) {
136147
}
137148
}
138149

139-
function createMetadata ({
150+
function createMetadata({
140151
allowedPublishers = false,
141152
requestToPublish = true,
142153
eventLinking = 'LAST_SEEN',

0 commit comments

Comments
 (0)