Skip to content

Commit 47cca0d

Browse files
author
nicola
committed
patch is now cleaned and async
1 parent 61737e5 commit 47cca0d

1 file changed

Lines changed: 152 additions & 161 deletions

File tree

‎handlers/patch.js‎

Lines changed: 152 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -10,193 +10,184 @@ var file = require('../fileStore.js');
1010
var subscription = require('../subscription.js');
1111

1212
function handler(req, res) {
13-
var options = req.app.locals.ldp;
13+
var ldp = req.app.locals.ldp;
1414
debug('PATCH -- ' + req.originalUrl);
1515
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'));
1616
res.header('MS-Author-Via' , 'SPARQL' );
17-
var filename = file.uriToFilename(req.path, options.root);
18-
var patchContentType = req.get('content-type').split(';')[0].trim(); // Ignore parameters
17+
18+
var filename = file.uriToFilename(req.path, ldp.root);
1919
var targetContentType = mime.lookup(filename);
20+
var patchContentType = req.get('content-type').split(';')[0].trim(); // Ignore parameters
2021
var targetURI = file.uriAbs(req) + req.originalUrl;
21-
var patchURI = targetURI ; // @@@ beware the triples from the patch ending up in the same place
22+
23+
2224
debug("PATCH -- Content-type " + patchContentType + " patching target " + targetContentType + " <" + targetURI + '>');
23-
var targetKB = $rdf.graph();
24-
var patchKB = $rdf.graph(); // Keep the patch in a sep KB as its URI is the same !
25-
var patchObject;
26-
var dataIn;
27-
28-
var fail = function(status, message) {
29-
debug("FAIL "+status+ " " + message);
30-
return res.status(status)
31-
.send('<html><body>\n'+ message+ '\n</body></html>\n');
32-
};
33-
34-
switch(patchContentType) {
35-
case 'application/sparql':
36-
debug("PATCH -- parsing query ...");
37-
var query = $rdf.SPARQLToQuery(req.text, false, patchKB, patchURI); // last param not used ATM
38-
39-
try {
40-
dataIn = fs.readFileSync(filename, { encoding: 'utf8'});
41-
} catch (err) {
42-
return res.status(404)
43-
.send("Patch: Original file read error:" + err + '\n');
25+
26+
if (patchContentType === 'application/sparql') {
27+
sparql(filename, targetURI, req.text, function(err, result) {
28+
if (err) {
29+
return res
30+
.status(err.status)
31+
.send(err.message);
4432
}
4533

46-
debug("PATCH -- File read OK "+dataIn.length);
34+
return res.json(result);
35+
});
36+
} else
37+
if (patchContentType === 'application/sparql-update') {
38+
return sparqlUpdate(filename, targetURI, req.text, function (err, patchKB) {
39+
if (err) {
40+
return res
41+
.status(err.status)
42+
.send(err.message);
43+
}
4744

48-
try {
49-
debug("PATCH -- parsing target file ...");
50-
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
51-
debug("PATCH -- Target parsed OK ");
52-
} catch(e) {
53-
return res.status(500)
54-
.send("Patch: Target " + targetContentType + " file syntax error:" + e);
45+
if (ldp.live) {
46+
subscription.publishDelta(req, res, patchKB, targetURI);
5547
}
48+
debug("PATCH -- applied OK (sync)");
49+
res.send("Patch applied OK\n");
50+
});
51+
} else {
52+
return res
53+
.status(400)
54+
.send("Sorry unknowm patch content type: " + patchContentType);
55+
}
56+
} // postOrPatch
5657

57-
var bindingsArray = [];
58-
var onBindings = function(bindings) {
59-
var b = {}, v, x; // Map from array to object
60-
for (v in bindings) if (bindings.hasOwnProperty(v)){
61-
x = bindings[v];
62-
b[v] = x.uri ? { 'type': 'uri', 'value': x.uri} :
63-
{ 'type': 'literal', 'value': x.value };
64-
if (x.lang) {
65-
b[v]['xml:lang'] = x.lang;
66-
}
67-
if (x.dt) {
68-
b[v].dt = x.dt.uri; // @@@ Correct? @@ check
69-
}
70-
}
71-
debug("PATCH -- bindings: " + JSON.stringify(b));
72-
bindingsArray.push(b);
73-
};
74-
75-
var onDone = function() {
76-
debug("PATCH -- Query done, no. bindings: " + bindingsArray.length);
77-
res.json({
78-
'head': {
79-
'vars': query.vars.map(function(v){
80-
return v.toNT();
81-
})
82-
},
83-
'results': {
84-
'bindings': bindingsArray
85-
}
86-
});
87-
};
58+
function sparql (filename, targetURI, text, callback) {
59+
debug("PATCH -- parsing query ...");
60+
var patchURI = targetURI; // @@@ beware the triples from the patch ending up in the same place
61+
var patchKB = $rdf.graph();
62+
var targetKB = $rdf.graph();
63+
var targetContentType = mime.lookup(filename);
64+
var query = $rdf.SPARQLToQuery(text, false, patchKB, patchURI); // last param not used ATM
8865

89-
var fetcher = new $rdf.Fetcher(targetKB, 10000, true);
90-
targetKB.query(query, onBindings, fetcher, onDone);
66+
fs.readFile(filename, { encoding: 'utf8'}, function (err, dataIn) {
67+
if (err) {
68+
return callback({
69+
status: 404,
70+
message: "Patch: Original file read error:" + err
71+
});
72+
}
9173

92-
break;
74+
debug("PATCH -- File read OK "+dataIn.length);
75+
debug("PATCH -- parsing target file ...");
9376

94-
case 'application/sparql-update':
9577
try {
96-
// Must parse relative to document's base address but patch doc should get diff URI
97-
debug("PATCH -- parsing patch ...");
98-
patchObject = $rdf.sparqlUpdateParser(req.text, patchKB, patchURI);
78+
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
9979
} catch(e) {
100-
return res.status(400)
101-
.send("Patch format syntax error:\n" + e + '\n');
80+
return callback({
81+
status: 500,
82+
message: "Patch: Target " + targetContentType + " file syntax error:" + e
83+
});
10284
}
103-
debug("PATCH -- reading target file ...");
104-
105-
if (true) { /// USe synchronous style to prevent interruption
106-
try {
107-
dataIn = fs.readFileSync(filename, { encoding: 'utf8'});
108-
} catch (err) {
109-
return res.status(404)
110-
.send("Patch: Original file read error:" + err + '\n');
111-
}
112-
113-
debug("PATCH -- File read OK "+dataIn.length);
114-
115-
try {
116-
debug("PATCH -- parsing target file ...");
117-
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
118-
debug("PATCH -- Target parsed OK ");
119-
} catch(e) {
120-
return res.status(500).send("Patch: Target " + targetContentType + " file syntax error:" + e);
121-
}
122-
123-
// debug("Pre check ");
124-
// targetKB.check(); // @@
125-
var target = patchKB.sym(targetURI);
126-
debug("PATCH -- Target parsed OK, patching... ");
127-
128-
targetKB.applyPatch(patchObject, target, function(err){
129-
if (err) {
130-
return fail(409, err); // HTTP inconsistency error -- very important.
85+
debug("PATCH -- Target parsed OK ");
86+
87+
var bindingsArray = [];
88+
89+
var onBindings = function(bindings) {
90+
var b = {}, v, x; // Map from array to object
91+
for (v in bindings) if (bindings.hasOwnProperty(v)){
92+
x = bindings[v];
93+
b[v] = x.uri ? { 'type': 'uri', 'value': x.uri} :
94+
{ 'type': 'literal', 'value': x.value };
95+
if (x.lang) {
96+
b[v]['xml:lang'] = x.lang;
13197
}
132-
debug("PATCH -- Patched. Writeback URI base " + targetURI);
133-
var data = $rdf.serialize(target, targetKB, targetURI, targetContentType);
134-
// debug("Writeback data: " + data);
135-
try {
136-
fs.writeFileSync(filename, data, { encoding: 'utf8'});
137-
} catch (er) {
138-
return fail(500, "Failed to write file back after patch: "+ err);
98+
if (x.dt) {
99+
b[v].dt = x.dt.uri; // @@@ Correct? @@ check
139100
}
140-
debug("PATCH -- applied OK (sync)");
141-
res.send("Patch applied OK\n");
142-
if (options.live) {
143-
subscription.publishDelta(req, res, patchKB, targetURI);
101+
}
102+
debug("PATCH -- bindings: " + JSON.stringify(b));
103+
bindingsArray.push(b);
104+
};
105+
106+
var onDone = function() {
107+
debug("PATCH -- Query done, no. bindings: " + bindingsArray.length);
108+
return callback(null, {
109+
'head': {
110+
'vars': query.vars.map(function(v){
111+
return v.toNT();
112+
})
113+
},
114+
'results': {
115+
'bindings': bindingsArray
144116
}
145-
return;
146117
});
118+
};
147119

148-
} else { // Assync
120+
var fetcher = new $rdf.Fetcher(targetKB, 10000, true);
121+
targetKB.query(query, onBindings, fetcher, onDone);
122+
});
123+
}
149124

150-
fs.readFile(filename, { encoding: 'utf8'}, function (err,data) {
151-
if (err) {
152-
return res.status(404).send("Patch: Original file read error:" + err + '\n');
153-
}
154-
debug("PATCH -- File read OK "+data.length);
155-
try {
156-
debug("PATCH -- parsing target file ...");
157-
$rdf.parse(data, targetKB, targetURI, targetContentType);
158-
debug("PATCH -- Target parsed OK ");
159-
160-
} catch(e) {
161-
return res.status(500).send("Patch: Target " + targetContentType + " file syntax error:" + e);
162-
}
125+
function sparqlUpdate(filename, targetURI, text, callback) {
126+
var patchURI = targetURI; // @@@ beware the triples from the patch ending up in the same place
127+
var patchKB = $rdf.graph();
128+
var targetKB = $rdf.graph();
129+
var targetContentType = mime.lookup(filename);
163130

164-
var target = patchKB.sym(targetURI);
165-
166-
var writeFileBack = function() {
167-
// debug("Accumulated namespaces:" + targetKB.namespaces)
168-
debug("PATCH -- Writeback URI base " + targetURI);
169-
var data = $rdf.serialize(target, targetKB, targetURI, targetContentType);
170-
// debug("Writeback data: " + data);
171-
172-
fs.writeFile(filename, data, { encoding: 'utf8'}, function(err){
173-
if (err) {
174-
return fail(500, "Failed to write file back after patch: "+ err);
175-
}
176-
debug("PATCH -- applied OK");
177-
res.send("Patch applied OK\n");
178-
if (options.live) {
179-
subscription.publishDelta(req, res, patchKB, targetURI);
180-
}
181-
return;
182-
}); // end write done
183-
};
184-
185-
targetKB.applyPatch(patchObject, target, function(err){
186-
if (err) {
187-
return fail(409, err); // HTTP inconsistency error -- very important.
188-
}
189-
writeFileBack();
190-
});
131+
debug("PATCH -- parsing patch ...");
132+
var patchObject;
133+
try {
134+
// Must parse relative to document's base address but patch doc should get diff URI
135+
patchObject = $rdf.sparqlUpdateParser(text, patchKB, patchURI);
136+
} catch(e) {
137+
return callback({
138+
status: 400,
139+
message: "Patch format syntax error:\n" + e + '\n'
140+
});
141+
}
142+
debug("PATCH -- reading target file ...");
143+
144+
fs.readFile(filename, { encoding: 'utf8'}, function(err, dataIn) {
145+
if (err) {
146+
return callback({
147+
status: 404,
148+
message: "Patch: Original file read error:" + err
149+
});
150+
}
191151

192-
}); // end read file done
152+
debug("PATCH -- File read OK "+dataIn.length);
153+
debug("PATCH -- parsing target file ...");
193154

194-
} // if
195-
break;
155+
try {
156+
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
157+
} catch(e) {
158+
return callback({
159+
status: 500,
160+
message: "Patch: Target " + targetContentType + " file syntax error:" + e
161+
});
162+
}
163+
debug("PATCH -- Target parsed OK ");
196164

197-
default:
198-
return fail(400, "Sorry unknowm patch content type: " + patchContentType);
199-
} // switch content-type
200-
} // postOrPatch
165+
var target = patchKB.sym(targetURI);
166+
debug("PATCH -- Target parsed OK, patching... ");
167+
168+
targetKB.applyPatch(patchObject, target, function(err){
169+
if (err) {
170+
return callback({
171+
status: 409,
172+
message: err
173+
});
174+
}
175+
debug("PATCH -- Patched. Writeback URI base " + targetURI);
176+
var data = $rdf.serialize(target, targetKB, targetURI, targetContentType);
177+
// debug("Writeback data: " + data);
178+
179+
fs.writeFile(filename, data, {encoding: 'utf8'}, function(err, data) {
180+
if (err) {
181+
return callback({
182+
status: 500,
183+
message: "Failed to write file back after patch: "+ err
184+
});
185+
}
186+
debug("PATCH -- applied OK (sync)");
187+
return callback(null, patchKB);
188+
});
189+
});
190+
});
191+
}
201192

202193
exports.handler = handler;

0 commit comments

Comments
 (0)