@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.
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +36 -27
- package/package.json +1 -1
package/dist/plugin.d.ts.map
CHANGED
|
@@ -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;
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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', () =>
|
|
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
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
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
|
-
|
|
95
|
-
|
|
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
|
-
|
|
98
|
-
const error = new Error(
|
|
99
|
-
|
|
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
|
});
|