@@ -10,6 +10,7 @@ const $rdf = require('rdflib')
1010const crypto = require ( 'crypto' )
1111const overQuota = require ( '../utils' ) . overQuota
1212const getContentType = require ( '../utils' ) . getContentType
13+ const { lock } = require ( 'proper-lockfile' )
1314
1415// Patch parsers by request body content type
1516const PATCH_PARSERS = {
@@ -23,6 +24,7 @@ const DEFAULT_FOR_NEW_CONTENT_TYPE = 'text/turtle'
2324async function patchHandler ( req , res , next ) {
2425 debug ( `PATCH -- ${ req . originalUrl } ` )
2526 res . header ( 'MS-Author-Via' , 'SPARQL' )
27+ let releaseLock
2628 try {
2729 // Obtain details of the target resource
2830 const ldp = req . app . locals . ldp
@@ -50,22 +52,24 @@ async function patchHandler (req, res, next) {
5052 throw error ( 415 , `Unsupported patch content type: ${ patch . contentType } ` )
5153 }
5254
53- // Parse the target graph and the patch document,
54- // and verify permission for performing this specific patch
55- const [ graph , patchObject ] = await Promise . all ( [
56- readGraph ( resource ) ,
57- parsePatch ( url , patch . uri , patch . text )
58- . then ( patchObject => checkPermission ( req , patchObject ) )
59- ] )
55+ // Parse the patch document and verify permissions
56+ const patchObject = await parsePatch ( url , patch . uri , patch . text )
57+ await checkPermission ( req , patchObject )
6058
6159 // Patch the graph and write it back to the file
60+ releaseLock = await lock ( path , { retries : 10 , realpath : false } )
61+ const graph = await readGraph ( resource )
6262 await applyPatch ( patchObject , graph , url )
6363 const result = await writeGraph ( graph , resource , ldp . resourceMapper . rootPath , ldp . serverUri )
6464
6565 // Send the result to the client
6666 res . send ( result )
6767 } catch ( err ) {
6868 return next ( err )
69+ } finally {
70+ if ( releaseLock ) {
71+ await releaseLock ( )
72+ }
6973 }
7074 return next ( )
7175}
0 commit comments