Skip to content

Commit 24dcc0d

Browse files
committed
JSON.parse .jsonld and extend PATCH to .n3 and .rdf
1 parent 1859144 commit 24dcc0d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/handlers/patch.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ const PATCH_PARSERS = {
1818
'text/n3': require('./patch/n3-patch-parser.js')
1919
}
2020

21+
// use mime-type as contentType for new rdf resource
2122
const DEFAULT_FOR_NEW_CONTENT_TYPE = 'text/turtle'
2223

24+
function contentTypeForNew (req) {
25+
let contentTypeForNew = DEFAULT_FOR_NEW_CONTENT_TYPE
26+
if (req.path.endsWith('.jsonld')) contentTypeForNew = 'application/ld+json'
27+
else if (req.path.endsWith('.n3')) contentTypeForNew = 'text/n3'
28+
else if (req.path.endsWith('.rdf')) contentTypeForNew = 'application/rdf+xml'
29+
return contentTypeForNew
30+
}
31+
2332
// Handles a PATCH request
2433
async function patchHandler (req, res, next) {
2534
debug(`PATCH -- ${req.originalUrl}`)
@@ -33,9 +42,9 @@ async function patchHandler (req, res, next) {
3342
// First check if the file already exists
3443
({ path, contentType } = await ldp.resourceMapper.mapUrlToFile({ url: req }))
3544
} catch (err) {
36-
// If the file doesn't exist, request one to be created with the default content type
45+
// If the file doesn't exist, request one to be created with the file mime type as contentType
3746
({ path, contentType } = await ldp.resourceMapper.mapUrlToFile(
38-
{ url: req, createIfNotExists: true, contentType: DEFAULT_FOR_NEW_CONTENT_TYPE }))
47+
{ url: req, createIfNotExists: true, contentType: contentTypeForNew(req) }))
3948
// check if a folder with same name exists
4049
await ldp.checkItemName(req)
4150
resourceExists = false
@@ -93,13 +102,14 @@ function readGraph (resource) {
93102
// If the file does not exist, assume empty contents
94103
// (it will be created after a successful patch)
95104
if (err.code === 'ENOENT') {
96-
fileContents = ''
105+
fileContents = resource.contentType.includes('json') ? JSON.stringify('{}') : ''
97106
// Fail on all other errors
98107
} else {
99108
return reject(error(500, `Original file read error: ${err}`))
100109
}
101110
}
102111
debug('PATCH -- Read target file (%d bytes)', fileContents.length)
112+
fileContents = resource.contentType.includes('json') ? JSON.parse(fileContents) : fileContents
103113
resolve(fileContents)
104114
})
105115
)

test/integration/patch-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ describe('PATCH', () => {
8484
result: '@prefix : </new.ttl#>.\n@prefix tim: </>.\n\ntim:x tim:y tim:z.\n\n'
8585
}))
8686

87+
describe('on a non-existing jsonld file', describePatch({
88+
path: '/new.jsonld',
89+
exists: false,
90+
patch: `<> a solid:InsertDeletePatch;
91+
solid:inserts { <x> <y> <z>. }.`
92+
}, { // expected:
93+
status: 200,
94+
text: 'Patch applied successfully',
95+
result: '[{"@id":"https://tim.localhost:7777/x","https://tim.localhost:7777/y":[{"@id":"https://tim.localhost:7777/z"}]}]'
96+
}))
97+
8798
describe('on a resource with read-only access', describePatch({
8899
path: '/read-only.ttl',
89100
patch: `<> a solid:InsertDeletePatch;

0 commit comments

Comments
 (0)