1
1
'use strict'
2
2
3
3
const promisify = require ( 'promisify-es6' )
4
+ const defaultConfig = require ( '../runtime/config-nodejs.js' ) ( )
4
5
5
6
module . exports = function config ( self ) {
6
7
return {
@@ -17,6 +18,94 @@ module.exports = function config (self) {
17
18
} ) ,
18
19
replace : promisify ( ( config , callback ) => {
19
20
self . _repo . config . set ( config , callback )
20
- } )
21
+ } ) ,
22
+ profile : promisify ( applyProfile )
23
+ }
24
+
25
+ async function applyProfile ( profileName , opts , callback ) {
26
+ if ( typeof opts === 'function' ) {
27
+ callback = opts
28
+ opts = { }
29
+ }
30
+ const { dryRun } = opts
31
+
32
+ const profile = profiles . find ( p => p . name === profileName )
33
+ if ( ! profile ) {
34
+ return callback ( new Error ( `No profile with name '${ profileName } ' exists` ) )
35
+ }
36
+
37
+ try {
38
+ const oldCfg = await self . config . get ( )
39
+ const newCfg = JSON . parse ( JSON . stringify ( oldCfg ) ) // clone
40
+ profile . transform ( newCfg )
41
+ if ( ! dryRun ) {
42
+ await self . config . replace ( newCfg )
43
+ }
44
+
45
+ // Scrub private key from output
46
+ delete oldCfg . Identity . PrivKey
47
+ delete newCfg . Identity . PrivKey
48
+
49
+ callback ( null , { oldCfg, newCfg } )
50
+ } catch ( err ) {
51
+ callback ( new Error ( `Could not apply profile '${ profileName } ' to config: ${ err . message } ` ) )
52
+ }
21
53
}
22
54
}
55
+
56
+ const profiles = [ {
57
+ name : 'server' ,
58
+ description : 'Disables local host discovery - recommended when running IPFS on machines with public IPv4 addresses.' ,
59
+ transform : ( config ) => {
60
+ config . Discovery . MDNS . Enabled = false
61
+ config . Discovery . webRTCStar . Enabled = false
62
+ }
63
+ } , {
64
+ name : 'local-discovery' ,
65
+ description : 'Enables local host discovery - inverse of "server" profile.' ,
66
+ transform : ( config ) => {
67
+ config . Discovery . MDNS . Enabled = true
68
+ config . Discovery . webRTCStar . Enabled = true
69
+ }
70
+ } , {
71
+ name : 'test' ,
72
+ description : 'Reduces external interference of IPFS daemon - for running the daemon in test environments.' ,
73
+ transform : ( config ) => {
74
+ config . Addresses . API = defaultConfig . Addresses . API ? '/ip4/127.0.0.1/tcp/0' : ''
75
+ config . Addresses . Gateway = defaultConfig . Addresses . Gateway ? '/ip4/127.0.0.1/tcp/0' : ''
76
+ config . Addresses . Swarm = defaultConfig . Addresses . Swarm . length ? [ '/ip4/127.0.0.1/tcp/0' ] : [ ]
77
+ config . Bootstrap = [ ]
78
+ config . Discovery . MDNS . Enabled = false
79
+ config . Discovery . webRTCStar . Enabled = false
80
+ }
81
+ } , {
82
+ name : 'default-networking' ,
83
+ description : 'Restores default network settings - inverse of "test" profile.' ,
84
+ transform : ( config ) => {
85
+ console . log ( 'applying default-networking' )
86
+ console . log ( 'setting to' , defaultConfig . Addresses )
87
+ config . Addresses . API = defaultConfig . Addresses . API
88
+ config . Addresses . Gateway = defaultConfig . Addresses . Gateway
89
+ config . Addresses . Swarm = defaultConfig . Addresses . Swarm
90
+ config . Bootstrap = defaultConfig . Bootstrap
91
+ config . Discovery . MDNS . Enabled = defaultConfig . Discovery . MDNS . Enabled
92
+ config . Discovery . webRTCStar . Enabled = defaultConfig . Discovery . webRTCStar . Enabled
93
+ }
94
+ } , {
95
+ name : 'lowpower' ,
96
+ description : 'Reduces daemon overhead on the system - recommended for low power systems.' ,
97
+ transform : ( config ) => {
98
+ config . Swarm = config . Swarm || { }
99
+ config . Swarm . ConnMgr = config . Swarm . ConnMgr || { }
100
+ config . Swarm . ConnMgr . LowWater = 20
101
+ config . Swarm . ConnMgr . HighWater = 40
102
+ }
103
+ } , {
104
+ name : 'default-power' ,
105
+ description : 'Inverse of "lowpower" profile.' ,
106
+ transform : ( config ) => {
107
+ config . Swarm = defaultConfig . Swarm
108
+ }
109
+ } ]
110
+
111
+ module . exports . profiles = profiles
0 commit comments