Skip to content

Commit a79d19d

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Use same patch logic regardless of content type.
1 parent 0b03de7 commit a79d19d

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

lib/handlers/patch.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ var mime = require('mime-types')
44
var debug = require('../debug').handlers
55
var utils = require('../utils.js')
66
var error = require('../http-error')
7-
const sparqlPatch = require('./patch/sparql-patcher.js')
8-
const sparqlUpdatePatch = require('./patch/sparql-update-patcher.js')
97

108
const DEFAULT_CONTENT_TYPE = 'text/turtle'
119

10+
const PATCHERS = {
11+
'application/sparql': require('./patch/sparql-patcher.js'),
12+
'application/sparql-update': require('./patch/sparql-update-patcher.js')
13+
}
14+
1215
function handler (req, res, next) {
1316
req.setEncoding('utf8')
1417
req.text = ''
@@ -37,26 +40,16 @@ function patchHandler (req, res, next) {
3740

3841
debug('PATCH -- Content-type ' + patchContentType + ' patching target ' + targetContentType + ' <' + targetURI + '>')
3942

40-
if (patchContentType === 'application/sparql') {
41-
sparqlPatch(filename, targetURI, req.text, function (err, result) {
42-
if (err) {
43-
return next(err)
44-
}
45-
res.json(result)
46-
return next()
47-
})
48-
} else if (patchContentType === 'application/sparql-update') {
49-
return sparqlUpdatePatch(filename, targetURI, req.text, function (err, patchKB) {
50-
if (err) {
51-
return next(err)
52-
}
53-
54-
// subscription.publishDelta(req, res, patchKB, targetURI)
55-
debug('PATCH -- applied OK (sync)')
56-
res.send('Patch applied OK\n')
57-
return next()
58-
})
59-
} else {
43+
const patch = PATCHERS[patchContentType]
44+
if (!patch) {
6045
return next(error(400, 'Unknown patch content type: ' + patchContentType))
6146
}
62-
} // postOrPatch
47+
patch(filename, targetURI, req.text, function (err, result) {
48+
if (err) {
49+
next(err)
50+
} else {
51+
res.send(result)
52+
next()
53+
}
54+
})
55+
}

lib/handlers/patch/sparql-patcher.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function patch (filename, targetURI, text, callback) {
5656

5757
var onDone = function () {
5858
debug('PATCH -- Query done, no. bindings: ' + bindingsArray.length)
59-
return callback(null, {
59+
const result = {
6060
'head': {
6161
'vars': query.vars.map(function (v) {
6262
return v.toNT()
@@ -65,7 +65,8 @@ function patch (filename, targetURI, text, callback) {
6565
'results': {
6666
'bindings': bindingsArray
6767
}
68-
})
68+
}
69+
callback(null, JSON.stringify(result))
6970
}
7071

7172
var fetcher = new $rdf.Fetcher(targetKB, 10000, true)

lib/handlers/patch/sparql-update-patcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function patch (filename, targetURI, text, callback) {
7171
return cb(error(500, 'Failed to write file back after patch: ' + err))
7272
}
7373
debug('PATCH -- applied OK (sync)')
74-
return cb(null, patchKB)
74+
return cb(null, 'Patch applied OK\n')
7575
})
7676
})
7777
})

0 commit comments

Comments
 (0)