This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
185 lines (164 loc) · 5.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict'
const BlockService = require('ipfs-block-service')
const Ipld = require('ipld')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const crypto = require('libp2p-crypto')
const isIPFS = require('is-ipfs')
const multiaddr = require('multiaddr')
const multihash = require('multihashes')
const PeerBook = require('peer-book')
const multibase = require('multibase')
const multicodec = require('multicodec')
const CID = require('cids')
const debug = require('debug')
const mergeOptions = require('merge-options')
const EventEmitter = require('events')
const config = require('./config')
const boot = require('./boot')
const components = require('./components')
// replaced by repo-browser when running in the browser
const defaultRepo = require('./runtime/repo-nodejs')
const preload = require('./preload')
const mfsPreload = require('./mfs-preload')
// All known (non-default) IPLD formats
const IpldFormats = {
get 'bitcoin-block' () {
return require('ipld-bitcoin')
},
get 'eth-account-snapshot' () {
return require('ipld-ethereum').ethAccountSnapshot
},
get 'eth-block' () {
return require('ipld-ethereum').ethBlock
},
get 'eth-block-list' () {
return require('ipld-ethereum').ethBlockList
},
get 'eth-state-trie' () {
return require('ipld-ethereum').ethStateTrie
},
get 'eth-storage-trie' () {
return require('ipld-ethereum').ethStorageTrie
},
get 'eth-tx' () {
return require('ipld-ethereum').ethTx
},
get 'eth-tx-trie' () {
return require('ipld-ethereum').ethTxTrie
},
get 'git-raw' () {
return require('ipld-git')
},
get 'zcash-block' () {
return require('ipld-zcash')
}
}
class IPFS extends EventEmitter {
constructor (options) {
super()
const defaults = {
init: true,
start: true,
EXPERIMENTAL: {},
preload: {
enabled: true,
addresses: [
'/dnsaddr/node0.preload.ipfs.io/https',
'/dnsaddr/node1.preload.ipfs.io/https'
]
}
}
options = config.validate(options || {})
this._options = mergeOptions(defaults, options)
if (options.init === false) {
this._options.init = false
}
if (!(options.start === false)) {
this._options.start = true
}
if (typeof options.repo === 'string' ||
options.repo === undefined) {
this._repo = defaultRepo(options.repo)
} else {
this._repo = options.repo
}
// IPFS utils
this.log = debug('ipfs')
this.log.err = debug('ipfs:err')
// IPFS Core Internals
// this._repo - assigned above
this._peerInfoBook = new PeerBook()
this._peerInfo = undefined
this._bitswap = undefined
this._blockService = new BlockService(this._repo)
this._ipld = new Ipld({
blockService: this._blockService,
loadFormat: (codec, callback) => {
this.log('Loading IPLD format', codec)
if (IpldFormats[codec]) return callback(null, IpldFormats[codec])
callback(new Error(`Missing IPLD format "${codec}"`))
}
})
this._preload = preload(this)
this._mfsPreload = mfsPreload(this)
this._ipns = undefined
// eslint-disable-next-line no-console
this._print = this._options.silent ? this.log : console.log
// IPFS Core exposed components
// - for booting up a node
this.init = components.init(this)
this.preStart = components.preStart(this)
this.start = components.start(this)
this.stop = components.stop(this)
this.shutdown = this.stop
this.isOnline = components.isOnline(this)
// - interface-ipfs-core defined API
Object.assign(this, components.filesRegular(this))
this.version = components.version(this)
this.id = components.id(this)
this.repo = components.repo(this)
this.bootstrap = components.bootstrap(this)
this.config = components.config(this)
this.block = components.block(this)
this.object = components.object(this)
this.dag = components.dag(this)
this.files = components.filesMFS(this)
this.libp2p = null // assigned on start
this.swarm = components.swarm(this)
this.name = components.name(this)
this.bitswap = components.bitswap(this)
this.pin = components.pin(this)
this.ping = components.ping(this)
this.pingPullStream = components.pingPullStream(this)
this.pingReadableStream = components.pingReadableStream(this)
this.pubsub = components.pubsub(this)
this.dht = components.dht(this)
this.dns = components.dns(this)
this.key = components.key(this)
this.stats = components.stats(this)
this.resolve = components.resolve(this)
if (this._options.EXPERIMENTAL.pubsub) {
this.log('EXPERIMENTAL pubsub is enabled')
}
if (this._options.EXPERIMENTAL.ipnsPubsub) {
if (!this._options.EXPERIMENTAL.pubsub) {
this.log('EXPERIMENTAL pubsub is enabled to use IPNS pubsub')
this._options.EXPERIMENTAL.pubsub = true
}
this.log('EXPERIMENTAL IPNS pubsub is enabled')
}
if (this._options.EXPERIMENTAL.sharding) {
this.log('EXPERIMENTAL sharding is enabled')
}
this.state = require('./state')(this)
boot(this)
}
}
module.exports = IPFS
// Note: We need to do this to force browserify to load the Buffer module
const BufferImpl = Buffer
Object.assign(module.exports, { crypto, isIPFS, Buffer: BufferImpl, CID, multiaddr, multibase, multihash, multicodec, PeerId, PeerInfo })
module.exports.createNode = (options) => {
return new IPFS(options)
}