forked from ZeroNetJS/zeronet-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
53 lines (46 loc) · 1.16 KB
/
bundle.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
"use strict"
const merge = require("merge-recursive").recursive
const ZeroNet = require("zeronet-node")
const MEM = require("zeronet-storage-memory")
const clone = require("clone")
const Id = require("peer-id")
module.exports = function ZeroNetBundler(opt) {
const bname = opt.name || "ZeroNetBundle"
let r = {}
const strict = {
modules: opt.modules
}
const defaults = merge(opt.override, {
swarm: {
zero: {
listen: [],
crypto: [
require("zeronet-crypto/secio")
]
},
libp2p: {}
},
modules: {},
storage: MEM
})
r[bname] = function (opt, cb) { //hack to set function name
if (!cb) cb = () => {}
if (!opt) opt = {}
let config = merge(merge(clone(defaults), clone(strict)), merge(opt, clone(strict)))
if (!opt.storage) config.storage = new defaults.storage()
let node
const liftoff = (err, id) => {
if (err) return cb(err)
if (id) config.id = id
node = new ZeroNet(config)
cb(null, node)
return node
}
if (!opt.id) {
Id.create({
bits: 2048
}, liftoff)
} else return liftoff(null, opt.id)
}
return r[bname]
}