Skip to content

Commit aed5b6a

Browse files
committed
Construct the patch URI through a hash of its contents.
1 parent 71c3101 commit aed5b6a

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

lib/handlers/patch.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const debug = require('../debug').handlers
99
const utils = require('../utils.js')
1010
const error = require('../http-error')
1111
const $rdf = require('rdflib')
12+
const crypto = require('crypto')
1213

1314
const DEFAULT_TARGET_TYPE = 'text/turtle'
1415

@@ -23,31 +24,32 @@ function patchHandler (req, res, next) {
2324
debug('PATCH -- ' + req.originalUrl)
2425
res.header('MS-Author-Via', 'SPARQL')
2526

26-
// Obtain details of the patch document
27-
const patch = {
28-
text: req.body ? req.body.toString() : '',
29-
contentType: (req.get('content-type') || '').match(/^[^;\s]*/)[0]
30-
}
31-
const patchGraph = PATCHERS[patch.contentType]
32-
if (!patchGraph) {
33-
return next(error(415, 'Unknown patch content type: ' + patch.contentType))
34-
}
35-
debug('PATCH -- Received patch (%d bytes, %s)', patch.text.length, patch.contentType)
36-
3727
// Obtain details of the target resource
3828
const ldp = req.app.locals.ldp
3929
const root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'
40-
const target = {
41-
file: utils.uriToFilename(req.path, root),
42-
uri: utils.uriAbs(req) + req.originalUrl
43-
}
30+
const target = {}
31+
target.file = utils.uriToFilename(req.path, root)
32+
target.uri = utils.uriAbs(req) + req.originalUrl
4433
target.contentType = mime.lookup(target.file) || DEFAULT_TARGET_TYPE
4534
debug('PATCH -- Target <%s> (%s)', target.uri, target.contentType)
4635

36+
// Obtain details of the patch document
37+
const patch = {}
38+
patch.text = req.body ? req.body.toString() : ''
39+
patch.uri = `${target.uri}#patch-${hash(patch.text)}`
40+
patch.contentType = (req.get('content-type') || '').match(/^[^;\s]*/)[0]
41+
debug('PATCH -- Received patch (%d bytes, %s)', patch.text.length, patch.contentType)
42+
43+
// Find the appropriate patcher for the given content type
44+
const patchGraph = PATCHERS[patch.contentType]
45+
if (!patchGraph) {
46+
return next(error(415, 'Unknown patch content type: ' + patch.contentType))
47+
}
48+
4749
// Read the RDF graph to be patched from the file
4850
readGraph(target)
4951
// Patch the graph and write it back to the file
50-
.then(graph => patchGraph(graph, target.uri, patch.text))
52+
.then(graph => patchGraph(graph, target.uri, patch.uri, patch.text))
5153
.then(graph => writeGraph(graph, target))
5254
// Send the result to the client
5355
.then(result => { res.send(result) })
@@ -108,3 +110,8 @@ function writeGraph (graph, resource) {
108110
})
109111
})
110112
}
113+
114+
// Creates a hash of the given text
115+
function hash (text) {
116+
return crypto.createHash('md5').update(text).digest('hex')
117+
}

lib/handlers/patch/n3-patcher.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ const PATCH_NS = 'http://example.org/patch#'
1010
const PREFIXES = `PREFIX p: <${PATCH_NS}>\n`
1111

1212
// Patches the given graph
13-
function patch (targetKB, targetURI, patchText) {
13+
function patch (targetKB, targetURI, patchURI, patchText) {
1414
const patchKB = $rdf.graph()
1515
const target = patchKB.sym(targetURI)
1616

17-
// Must parse relative to document's base address but patch doc should get diff URI
18-
// @@@ beware the triples from the patch ending up in the same place
19-
const patchURI = targetURI + '#patch'
20-
2117
return parsePatchDocument(targetURI, patchURI, patchText, patchKB)
2218
.then(patchObject => applyPatch(patchObject, target, targetKB))
2319
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ const debug = require('../../debug').handlers
77
const error = require('../../http-error')
88

99
// Patches the given graph
10-
function patch (targetKB, targetURI, patchText) {
10+
function patch (targetKB, targetURI, patchURI, patchText) {
1111
const patchKB = $rdf.graph()
1212
const target = patchKB.sym(targetURI)
1313

14-
// Must parse relative to document's base address but patch doc should get diff URI
15-
// @@@ beware the triples from the patch ending up in the same place
16-
const patchURI = targetURI
17-
1814
return parsePatchDocument(patchURI, patchText, patchKB)
1915
.then(patchObject => applyPatch(patchObject, target, targetKB))
2016
}
@@ -23,8 +19,9 @@ function patch (targetKB, targetURI, patchText) {
2319
function parsePatchDocument (patchURI, patchText, patchKB) {
2420
debug('PATCH -- Parsing patch...')
2521
return new Promise((resolve, reject) => {
22+
const baseURI = patchURI.replace(/#.*/, '')
2623
try {
27-
resolve($rdf.sparqlUpdateParser(patchText, patchKB, patchURI))
24+
resolve($rdf.sparqlUpdateParser(patchText, patchKB, baseURI))
2825
} catch (err) {
2926
reject(error(400, 'Patch format syntax error:\n' + err + '\n'))
3027
}

0 commit comments

Comments
 (0)