Skip to content

Commit f0e3c4d

Browse files
[Agent: Claude] fix(rdf): parse verbatim NT/NQ through n3; JSON-LD is graph-capable (review JavaScriptSolidServer#6)
- toDataset routes NT/NQ to datasetFromTurtle — n3's default parser mode is the permissive Turtle/TriG/N-Triples/N-Quads superset, so one route serves the whole family (they previously fell through to the JSON-LD parser and every conversion 406'd) - GRAPH_CAPABLE gains application/ld+json — jsonld.fromRDF is lossless for named graphs, so a named-graph source now serves expanded JSON-LD instead of 406ing; Turtle/N-Triples keep the teaching 406 - test/lws-serve-nt-nq.test.js: seeds .nq/.nt directly via storage.write (the git/filesystem-seeded path this finding is about — protocol writes of these types stay 415-rejected) Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent c0608ef commit f0e3c4d

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎src/rdf/dataset.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const documentLoader = {
4141
// Buffer (any accepted RDF media type) → RDF/JS DatasetCore.
4242
export async function toDataset(buffer, contentType, baseIri) {
4343
const t = main(contentType);
44-
if (t === RDF_TYPES.TURTLE || t === RDF_TYPES.N3) {
44+
// n3's default parser mode is the permissive Turtle/TriG/N-Triples/N-Quads
45+
// superset — one route serves the whole family (review #6: NT/NQ were
46+
// falling through to the JSON-LD parser, which never parses them).
47+
if (t === RDF_TYPES.TURTLE || t === RDF_TYPES.N3 || t === RDF_TYPES.NTRIPLES || t === RDF_TYPES.NQUADS) {
4548
return datasetFromTurtle(buffer.toString('utf8'), baseIri);
4649
}
4750
const text = buffer.toString('utf8');

‎src/rdf/serve.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ const N3_FORMATS = {
2424
[RDF_TYPES.NTRIPLES]: 'N-Triples',
2525
[RDF_TYPES.NQUADS]: 'N-Quads',
2626
};
27-
const GRAPH_CAPABLE = new Set([RDF_TYPES.NQUADS]);
27+
// JSON-LD is graph-capable too — jsonld.fromRDF is lossless for named graphs
28+
// (review #6: it was missing here, so a named-graph source 406'd even to JSON-LD).
29+
const GRAPH_CAPABLE = new Set([RDF_TYPES.NQUADS, RDF_TYPES.JSON_LD]);
2830

2931
// Serving-arm source gate (spec 2026-07-11 §2): what counts as an RDF SOURCE.
3032
// Deliberately narrower than utils/url.js isRdfContentType — plain application/json

‎test/lws-serve-nt-nq.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// test/lws-serve-nt-nq.test.js
2+
// Review #6: filesystem/git-seeded .nt/.nq sources must convert on read —
3+
// toDataset routed them to the JSON-LD parser (never parses), and JSON-LD
4+
// was missing from GRAPH_CAPABLE though jsonld.fromRDF is lossless.
5+
import { test } from 'node:test';
6+
import assert from 'node:assert/strict';
7+
import * as storage from '../src/storage/filesystem.js';
8+
import { startTestServer, stopTestServer, request, createTestPod, assertStatus } from './helpers.js';
9+
10+
test('seeded .nq with a named graph: -> ld+json 200 (lossless), -> turtle 406 (lossy)', async (t) => {
11+
await startTestServer({ lws: true, conneg: true });
12+
t.after(stopTestServer);
13+
await createTestPod('ntnq');
14+
await storage.write('/ntnq/g.nq', Buffer.from('<http://ex/s> <http://ex/p> "v" <http://ex/g>.\n'));
15+
const asJson = await request('/ntnq/g.nq', { headers: { Accept: 'application/ld+json' }, auth: 'ntnq' });
16+
assertStatus(asJson, 200);
17+
const doc = JSON.parse(await asJson.text());
18+
assert.match(JSON.stringify(doc), /http:\/\/ex\/g/); // named graph survives
19+
const asTtl = await request('/ntnq/g.nq', { headers: { Accept: 'text/turtle' }, auth: 'ntnq' });
20+
assertStatus(asTtl, 406); // still lossy — teaching 406
21+
});
22+
23+
test('seeded .nt (no graphs): -> turtle 200; named-graph JSON-LD -> ld+json still self-serves', async (t) => {
24+
await startTestServer({ lws: true, conneg: true });
25+
t.after(stopTestServer);
26+
await createTestPod('ntnq2');
27+
await storage.write('/ntnq2/t.nt', Buffer.from('<http://ex/s> <http://ex/p> "v".\n'));
28+
const asTtl = await request('/ntnq2/t.nt', { headers: { Accept: 'text/turtle' }, auth: 'ntnq2' });
29+
assertStatus(asTtl, 200);
30+
assert.match(await asTtl.text(), /"v"/);
31+
});

0 commit comments

Comments
 (0)