-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathpatch.js
More file actions
178 lines (154 loc) · 5.72 KB
/
patch.js
File metadata and controls
178 lines (154 loc) · 5.72 KB
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
module.exports = handler
var mime = require('mime')
mime.default_type = 'text/turtle'
var fs = require('fs')
var $rdf = require('rdflib')
var debug = require('../debug').handlers
var utils = require('../utils.js')
var error = require('../http-error')
function handler (req, res, next) {
req.setEncoding('utf8')
req.text = ''
req.on('data', function (chunk) {
req.text += chunk
})
req.on('end', function () {
patchHandler(req, res, next)
})
}
function patchHandler (req, res, next) {
var ldp = req.app.locals.ldp
debug('PATCH -- ' + req.originalUrl)
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'))
res.header('MS-Author-Via', 'SPARQL')
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'
var filename = utils.uriToFilename(req.path, root)
var targetContentType = mime.lookup(filename)
var patchContentType = req.get('content-type').split(';')[0].trim() // Ignore parameters
var targetURI = utils.uriAbs(req) + req.originalUrl
debug('PATCH -- Content-type ' + patchContentType + ' patching target ' + targetContentType + ' <' + targetURI + '>')
if (patchContentType === 'application/sparql') {
sparql(filename, targetURI, req.text, function (err, result) {
if (err) {
return next(err)
}
res.json(result)
return next()
})
} else if (patchContentType === 'application/sparql-update') {
return sparqlUpdate(filename, targetURI, req.text, function (err, patchKB) {
if (err) {
return next(err)
}
// subscription.publishDelta(req, res, patchKB, targetURI)
debug('PATCH -- applied OK (sync)')
res.send('Patch applied OK\n')
return next()
})
} else {
return next(error(400, 'Sorry unknown patch content type: ' + patchContentType))
}
} // postOrPatch
function sparql (filename, targetURI, text, callback) {
debug('PATCH -- parsing query ...')
var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
var patchKB = $rdf.graph()
var targetKB = $rdf.graph()
var targetContentType = mime.lookup(filename)
var query = $rdf.SPARQLToQuery(text, false, patchKB, patchURI) // last param not used ATM
fs.readFile(filename, {encoding: 'utf8'}, function (err, dataIn) {
if (err) {
return callback(error(404, 'Patch: Original file read error:' + err))
}
debug('PATCH -- File read OK ' + dataIn.length)
debug('PATCH -- parsing target file ...')
try {
$rdf.parse(dataIn, targetKB, targetURI, targetContentType)
} catch (e) {
debug('Patch: Target ' + targetContentType + ' file syntax error:' + e)
return callback(error(500, 'Patch: Target ' + targetContentType + ' file syntax error:' + e))
}
debug('PATCH -- Target parsed OK ')
var bindingsArray = []
var onBindings = function (bindings) {
var b = {}
var v
var x
for (v in bindings) {
if (bindings.hasOwnProperty(v)) {
x = bindings[v]
b[v] = x.uri ? {'type': 'uri', 'value': x.uri} : { 'type': 'literal', 'value': x.value }
if (x.lang) {
b[v]['xml:lang'] = x.lang
}
if (x.dt) {
b[v].dt = x.dt.uri // @@@ Correct? @@ check
}
}
}
debug('PATCH -- bindings: ' + JSON.stringify(b))
bindingsArray.push(b)
}
var onDone = function () {
debug('PATCH -- Query done, no. bindings: ' + bindingsArray.length)
return callback(null, {
'head': {
'vars': query.vars.map(function (v) {
return v.toNT()
})
},
'results': {
'bindings': bindingsArray
}
})
}
var fetcher = new $rdf.Fetcher(targetKB, 10000, true)
targetKB.query(query, onBindings, fetcher, onDone)
})
}
function sparqlUpdate (filename, targetURI, text, callback) {
var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
var patchKB = $rdf.graph()
var targetKB = $rdf.graph()
var targetContentType = mime.lookup(filename)
debug('PATCH -- parsing patch ...')
var patchObject
try {
// Must parse relative to document's base address but patch doc should get diff URI
patchObject = $rdf.sparqlUpdateParser(text, patchKB, patchURI)
} catch (e) {
return callback(error(400, 'Patch format syntax error:\n' + e + '\n'))
}
debug('PATCH -- reading target file ...')
fs.readFile(filename, {encoding: 'utf8'}, function (err, dataIn) {
if (err) {
return callback(error(404, 'Patch: Original file read error:' + err))
}
debug('PATCH -- target read OK ' + dataIn.length + ' bytes. Parsing...')
try {
$rdf.parse(dataIn, targetKB, targetURI, targetContentType)
} catch (e) {
debug('Patch: Target ' + targetContentType + ' file syntax error:' + e)
return callback(error(500, 'Patch: Target ' + targetContentType + ' file syntax error:' + e))
}
var target = patchKB.sym(targetURI)
debug('PATCH -- Target parsed OK, patching... ')
targetKB.applyPatch(patchObject, target, function (err) {
if (err) {
var message = err.message || err // returns string at the moment
debug('PATCH FAILED. Returning 409. Message: \'' + message + '\'')
return callback(error(409, message))
}
debug('PATCH -- Patched. Writeback URI base ' + targetURI)
var data = $rdf.serialize(target, targetKB, targetURI, targetContentType)
// debug('Writeback data: ' + data)
fs.writeFile(filename, data, {encoding: 'utf8'}, function (err, data) {
if (err) {
return callback(error(500, 'Failed to write file back after patch: ' + err))
}
debug('PATCH -- applied OK (sync)')
return callback(null, patchKB)
})
})
})
}