@lde/fastify-rdf 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAgC,MAAM,SAAS,CAAC;AAQ7E,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,YAAY,CAAC;AAgIpB;;;;;;;;;GASG;AACH,iBAAe,gBAAgB,CAC7B,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,IAAI,CAAC,CAqFf;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,yBAGrB,CAAC"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAgC,MAAM,SAAS,CAAC;AAQ7E,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,YAAY,CAAC;AAyIpB;;;;;;;;;GASG;AACH,iBAAe,gBAAgB,CAC7B,MAAM,EAAE,eAAe,EACvB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,IAAI,CAAC,CAqFf;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,yBAGrB,CAAC"}
package/dist/plugin.js CHANGED
@@ -58,45 +58,54 @@ async function streamToDataset(stream) {
58
58
  */
59
59
  async function registerRdfParsers(server, parseAll) {
60
60
  const contentTypes = (await rdfParser.getContentTypes()).filter((type) => type !== 'application/json');
61
- server.addContentTypeParser(contentTypes, function (_request, payload, done) {
62
- // Collect the raw body; the preParsing hook is not needed because
63
- // Fastify passes the raw payload stream here.
61
+ const jsonLdType = 'application/ld+json';
62
+ // JSON-LD is JSON.parse'd eagerly so the body is a plain object when
63
+ // Fastify runs preValidation (schema checks happen before preHandler).
64
+ // Other RDF types are stored as a raw Buffer for the preHandler hook.
65
+ server.addContentTypeParser(contentTypes, function (request, payload, done) {
66
+ const isJsonLd = request.headers['content-type']?.split(';')[0].trim() === jsonLdType;
64
67
  const chunks = [];
65
68
  payload.on('data', (chunk) => chunks.push(chunk));
66
- payload.on('end', () => done(null, Buffer.concat(chunks)));
69
+ payload.on('end', () => {
70
+ if (!isJsonLd)
71
+ return done(null, Buffer.concat(chunks));
72
+ try {
73
+ done(null, JSON.parse(Buffer.concat(chunks).toString('utf8')));
74
+ }
75
+ catch (err) {
76
+ err.statusCode = 400;
77
+ done(err);
78
+ }
79
+ });
67
80
  payload.on('error', done);
68
81
  });
69
82
  server.addHook('preHandler', async (request) => {
70
- // Only act on requests that matched an RDF content type parser.
71
- if (!request.body ||
72
- !Buffer.isBuffer(request.body) ||
73
- !request.headers['content-type']) {
83
+ if (!request.body || !request.headers['content-type'])
74
84
  return;
75
- }
76
85
  const contentType = request.headers['content-type'].split(';')[0].trim();
77
- if (!contentTypes.includes(contentType)) {
86
+ if (!contentTypes.includes(contentType))
78
87
  return;
79
- }
80
- if (parseAll || request.routeOptions.config.parseRdf) {
81
- try {
82
- const bodyStream = Readable.from(request.body);
83
- const quadStream = rdfParser.parse(bodyStream, { contentType });
84
- request.body = await streamToDataset(quadStream);
85
- }
86
- catch (cause) {
87
- const error = new Error('Invalid RDF body', {
88
- cause,
89
- });
90
- error.statusCode = 400;
88
+ if (!(parseAll || request.routeOptions.config.parseRdf)) {
89
+ // Not a parseRdf route: JSON-LD is already parsed; reject other RDF.
90
+ if (contentType !== jsonLdType) {
91
+ const error = new Error(`Unsupported Media Type: ${contentType}`);
92
+ error.statusCode = 415;
91
93
  throw error;
92
94
  }
95
+ return;
93
96
  }
94
- else if (contentType === 'application/ld+json') {
95
- request.body = JSON.parse(request.body.toString('utf8'));
97
+ try {
98
+ // JSON-LD body is already an object; re-stringify for rdf-parse.
99
+ const raw = Buffer.isBuffer(request.body)
100
+ ? request.body
101
+ : Buffer.from(JSON.stringify(request.body));
102
+ request.body = await streamToDataset(rdfParser.parse(Readable.from(raw), { contentType }));
96
103
  }
97
- else {
98
- const error = new Error(`Unsupported Media Type: ${contentType}`);
99
- error.statusCode = 415;
104
+ catch (cause) {
105
+ const error = new Error('Invalid RDF body', {
106
+ cause,
107
+ });
108
+ error.statusCode = 400;
100
109
  throw error;
101
110
  }
102
111
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lde/fastify-rdf",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Fastify plugin for serving RDF data with content negotiation",
5
5
  "repository": {
6
6
  "url": "git+https://github.com/ldengine/lde.git",