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 pathconfig.js
217 lines (182 loc) · 5.57 KB
/
config.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict'
const debug = require('debug')
const get = require('dlv')
const set = require('just-safe-set')
const log = debug('ipfs:http-api:config')
log.error = debug('ipfs:http-api:config:error')
const multipart = require('ipfs-multipart')
const Boom = require('@hapi/boom')
const Joi = require('@hapi/joi')
const { profiles } = require('../../../core/components/config')
const all = require('async-iterator-all')
exports.getOrSet = {
// pre request handler that parses the args and returns `key` & `value` which are assigned to `request.pre.args`
parseArgs (request, h) {
const parseValue = (args) => {
if (request.query.bool !== undefined) {
args.value = args.value === 'true'
} else if (request.query.json !== undefined) {
try {
args.value = JSON.parse(args.value)
} catch (err) {
log.error(err)
throw Boom.badRequest('failed to unmarshal json. ' + err)
}
}
return args
}
if (request.query.arg instanceof Array) {
return parseValue({
key: request.query.arg[0],
value: request.query.arg[1]
})
}
if (request.params.key) {
return parseValue({
key: request.params.key,
value: request.query.arg
})
}
if (!request.query.arg) {
throw Boom.badRequest("Argument 'key' is required")
}
return { key: request.query.arg }
},
// main route handler which is called after the above `parseArgs`, but only if the args were valid
async handler (request, h) {
const { ipfs } = request.server.app
const { key } = request.pre.args
let { value } = request.pre.args
// check that value exists - typeof null === 'object'
if (value && (typeof value === 'object' &&
value.type === 'Buffer')) {
throw Boom.badRequest('Invalid value type')
}
let originalConfig
try {
originalConfig = await ipfs.config.get()
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to get config value' })
}
if (value === undefined) {
// Get the value of a given key
value = get(originalConfig, key)
if (value === undefined) {
throw Boom.notFound('Failed to get config value: key has no attributes')
}
} else {
// Set the new value of a given key
const result = set(originalConfig, key, value)
if (!result) {
throw Boom.badRequest('Failed to set config value')
}
try {
await ipfs.config.replace(originalConfig)
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to replace config value' })
}
}
return h.response({
Key: key,
Value: value
})
}
}
exports.get = async (request, h) => {
const { ipfs } = request.server.app
let config
try {
config = await ipfs.config.get()
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to get config value' })
}
return h.response({
Value: config
})
}
exports.show = async (request, h) => {
const { ipfs } = request.server.app
let config
try {
config = await ipfs.config.get()
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to get config value' })
}
return h.response(config)
}
exports.replace = {
// pre request handler that parses the args and returns `config` which is assigned to `request.pre.args`
async parseArgs (request, h) {
if (!request.payload) {
throw Boom.badRequest("Argument 'file' is required")
}
let file
for await (const part of multipart(request)) {
if (part.type !== 'file') {
continue
}
file = Buffer.concat(await all(part.content))
}
if (!file) {
throw Boom.badRequest("Argument 'file' is required")
}
try {
return { config: JSON.parse(file.toString('utf8')) }
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to decode file as config' })
}
},
// main route handler which is called after the above `parseArgs`, but only if the args were valid
async handler (request, h) {
const { ipfs } = request.server.app
const { config } = request.pre.args
try {
await ipfs.config.replace(config)
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to save config' })
}
return h.response()
}
}
exports.profiles = {
apply: {
validate: {
query: Joi.object().keys({
'dry-run': Joi.boolean().default(false)
}).unknown()
},
// pre request handler that parses the args and returns `profile` which is assigned to `request.pre.args`
parseArgs: function (request, h) {
if (!request.query.arg) {
throw Boom.badRequest("Argument 'profile' is required")
}
if (!profiles[request.query.arg]) {
throw Boom.badRequest("Argument 'profile' is not a valid profile name")
}
return { profile: request.query.arg }
},
handler: async function (request, h) {
const { ipfs } = request.server.app
const { profile } = request.pre.args
const dryRun = request.query['dry-run']
try {
const diff = await ipfs.config.profiles.apply(profile, { dryRun })
return h.response({ OldCfg: diff.original, NewCfg: diff.updated })
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to apply profile' })
}
}
},
list: {
handler: async function (request, h) {
const { ipfs } = request.server.app
const list = await ipfs.config.profiles.list()
return h.response(
list.map(profile => ({
Name: profile.name,
Description: profile.description
}))
)
}
}
}