Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 01e56ec

Browse files
committed
feat: migrate http-api to use new async DAGNode interface
1 parent 254afdc commit 01e56ec

File tree

2 files changed

+191
-62
lines changed

2 files changed

+191
-62
lines changed

src/http-api/resources/object.js

Lines changed: 146 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ exports.new = (request, reply) => {
4040
}).code(500)
4141
}
4242

43-
return reply(node.toJSON())
43+
node.toJSON((err, nodeJSON) => {
44+
if (err) {
45+
return reply({
46+
Message: 'Failed to get object: ' + err,
47+
Code: 0
48+
}).code(500)
49+
}
50+
return reply(nodeJSON)
51+
})
4452
})
4553
}
4654

@@ -62,37 +70,73 @@ exports.get = {
6270
}).code(500)
6371
}
6472

65-
const res = node.toJSON()
66-
res.Data = res.Data ? res.Data.toString() : ''
67-
return reply(res)
73+
node.toJSON((err, nodeJSON) => {
74+
if (err) {
75+
return reply({
76+
Message: 'Failed to get object: ' + err,
77+
Code: 0
78+
}).code(500)
79+
}
80+
81+
nodeJSON.Data = nodeJSON.Data ? nodeJSON.Data.toString() : ''
82+
return reply(nodeJSON)
83+
})
6884
})
6985
}
7086
}
7187

7288
exports.put = {
73-
// pre request handler that parses the args and returns `node` which is assigned to `request.pre.args`
89+
// pre request handler that parses the args and returns `node`
90+
// which is assigned to `request.pre.args`
7491
parseArgs: (request, reply) => {
7592
if (!request.payload) {
7693
return reply("File argument 'data' is required").code(400).takeover()
7794
}
7895

7996
const enc = request.query.inputenc
80-
8197
const parser = multipart.reqParser(request.payload)
82-
var file
8398

84-
parser.on('file', (fileName, fileStream) => {
85-
fileStream.on('data', (data) => {
99+
let file
100+
let finished = true
101+
102+
parser.on('file', (name, stream) => {
103+
finished = false
104+
// TODO fix: stream is not emitting the 'end' event
105+
stream.on('data', (data) => {
86106
if (enc === 'protobuf') {
87-
const n = new DAGNode().unMarshal(data)
88-
file = new Buffer(JSON.stringify(n.toJSON()))
107+
dagPB.util.deserialize(data, (err, node) => {
108+
if (err) {
109+
return reply({
110+
Message: 'Failed to receive protobuf encoded: ' + err,
111+
Code: 0
112+
}).code(500).takeover()
113+
}
114+
115+
node.toJSON((err, nodeJSON) => {
116+
if (err) {
117+
return reply({
118+
Message: 'Failed to receive protobuf encoded: ' + err,
119+
Code: 0
120+
}).code(500).takeover()
121+
}
122+
file = new Buffer(JSON.stringify(nodeJSON))
123+
finished = true
124+
})
125+
})
89126
} else {
90127
file = data
128+
129+
finished = true
91130
}
92131
})
93132
})
94133

95-
parser.on('end', () => {
134+
parser.on('end', finish)
135+
136+
function finish () {
137+
if (!finished) {
138+
return setTimeout(finish, 10)
139+
}
96140
if (!file) {
97141
return reply("File argument 'data' is required").code(400).takeover()
98142
}
@@ -107,23 +151,33 @@ exports.put = {
107151
Code: 0
108152
}).code(500).takeover()
109153
}
110-
})
154+
}
111155
},
112156

113157
// main route handler which is called after the above `parseArgs`, but only if the args were valid
114158
handler: (request, reply) => {
115-
const node = request.pre.args.node
116-
const dagNode = new DAGNode(new Buffer(node.Data), node.Links)
159+
const nodeJSON = request.pre.args.node
160+
const node = new DAGNode(new Buffer(nodeJSON.Data), nodeJSON.Links)
117161

118-
request.server.app.ipfs.object.put(dagNode, (err, obj) => {
162+
request.server.app.ipfs.object.put(node, (err, obj) => {
119163
if (err) {
120164
log.error(err)
121165
return reply({
122166
Message: 'Failed to put object: ' + err,
123167
Code: 0
124168
}).code(500)
125169
}
126-
return reply(dagNode.toJSON())
170+
171+
node.toJSON((err, nodeJSON) => {
172+
if (err) {
173+
return reply({
174+
Message: 'Failed to put object: ' + err,
175+
Code: 0
176+
}).code(500)
177+
}
178+
179+
return reply(nodeJSON)
180+
})
127181
})
128182
}
129183
}
@@ -189,10 +243,17 @@ exports.links = {
189243
}).code(500)
190244
}
191245

192-
const res = node.toJSON()
193-
return reply({
194-
Hash: res.Hash,
195-
Links: res.Links
246+
node.toJSON((err, nodeJSON) => {
247+
if (err) {
248+
return reply({
249+
Message: 'Failed to get object: ' + err,
250+
Code: 0
251+
}).code(500)
252+
}
253+
return reply({
254+
Hash: nodeJSON.Hash,
255+
Links: nodeJSON.Links
256+
})
196257
})
197258
})
198259
}
@@ -225,7 +286,8 @@ exports.parseKeyAndData = (request, reply) => {
225286
try {
226287
return reply({
227288
data: file,
228-
key: new Buffer(bs58.decode(request.query.arg)) // TODO: support ipfs paths: https://github.com/ipfs/http-api-spec/pull/68/files#diff-2625016b50d68d922257f74801cac29cR3880
289+
key: new Buffer(bs58.decode(request.query.arg))
290+
// TODO: support ipfs paths: https://github.com/ipfs/http-api-spec/pull/68/files#diff-2625016b50d68d922257f74801cac29cR3880
229291
})
230292
} catch (err) {
231293
return reply({
@@ -255,7 +317,15 @@ exports.patchAppendData = {
255317
}).code(500)
256318
}
257319

258-
return reply(node.toJSON())
320+
node.toJSON((err, nodeJSON) => {
321+
if (err) {
322+
return reply({
323+
Message: 'Failed to get object: ' + err,
324+
Code: 0
325+
}).code(500)
326+
}
327+
return reply(nodeJSON)
328+
})
259329
})
260330
}
261331
}
@@ -279,10 +349,17 @@ exports.patchSetData = {
279349
}).code(500)
280350
}
281351

282-
const res = node.toJSON()
283-
return reply({
284-
Hash: res.Hash,
285-
Links: res.Links
352+
node.toJSON((err, nodeJSON) => {
353+
if (err) {
354+
return reply({
355+
Message: 'Failed to get object: ' + err,
356+
Code: 0
357+
}).code(500)
358+
}
359+
return reply({
360+
Hash: nodeJSON.Hash,
361+
Links: nodeJSON.Links
362+
})
286363
})
287364
})
288365
}
@@ -339,19 +416,44 @@ exports.patchAddLink = {
339416
}).code(500)
340417
}
341418

342-
const link = new DAGLink(name, linkedObj.size(), linkedObj.multihash())
343-
344-
request.server.app.ipfs.object.patch.addLink(root, link, (err, node) => {
419+
linkedObj.size((err, size) => {
345420
if (err) {
346-
log.error(err)
347-
348421
return reply({
349-
Message: 'Failed to add link to object: ' + err,
422+
Message: 'Failed to get linked object: ' + err,
350423
Code: 0
351424
}).code(500)
352425
}
353-
354-
return reply(node.toJSON())
426+
linkedObj.multihash((err, multihash) => {
427+
if (err) {
428+
return reply({
429+
Message: 'Failed to get linked object: ' + err,
430+
Code: 0
431+
}).code(500)
432+
}
433+
434+
const link = new DAGLink(name, size, multihash)
435+
436+
request.server.app.ipfs.object.patch.addLink(root, link, (err, node) => {
437+
if (err) {
438+
log.error(err)
439+
440+
return reply({
441+
Message: 'Failed to add link to object: ' + err,
442+
Code: 0
443+
}).code(500)
444+
}
445+
446+
node.toJSON((err, nodeJSON) => {
447+
if (err) {
448+
return reply({
449+
Message: 'Failed to get object: ' + err,
450+
Code: 0
451+
}).code(500)
452+
}
453+
return reply(nodeJSON)
454+
})
455+
})
456+
})
355457
})
356458
})
357459
}
@@ -393,14 +495,21 @@ exports.patchRmLink = {
393495
request.server.app.ipfs.object.patch.rmLink(root, link, (err, node) => {
394496
if (err) {
395497
log.error(err)
396-
397498
return reply({
398499
Message: 'Failed to add link to object: ' + err,
399500
Code: 0
400501
}).code(500)
401502
}
402503

403-
return reply(node.toJSON())
504+
node.toJSON((err, nodeJSON) => {
505+
if (err) {
506+
return reply({
507+
Message: 'Failed to get object: ' + err,
508+
Code: 0
509+
}).code(500)
510+
}
511+
return reply(nodeJSON)
512+
})
404513
})
405514
}
406515
}

0 commit comments

Comments
 (0)