Skip to content

Commit 7411cbc

Browse files
committed
Use content-type package, instead of purpose-defined parseContentType method.
1 parent a89a964 commit 7411cbc

File tree

5 files changed

+18
-41
lines changed

5 files changed

+18
-41
lines changed

lib/documentLoaders/node.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
*/
44
'use strict';
55

6+
const contentType = require ('content-type');
7+
68
const {
79
parseLinkHeader,
8-
buildHeaders,
9-
parseContentTypeHeader
10+
buildHeaders
1011
} = require('../util');
1112
const {LINK_HEADER_CONTEXT} = require('../constants');
1213
const JsonLdError = require('../JsonLdError');
@@ -88,14 +89,14 @@ module.exports = ({
8889
}
8990

9091
const {res, body} = result;
91-
const {contentType, params} = parseContentTypeHeader(res.headers['content-type']);
92+
const {type, parameters} = contentType.parse(res);
9293

9394
doc = {
9495
contextUrl: null,
9596
documentUrl: url,
9697
document: body || null,
97-
contentType: contentType,
98-
profile: params.profile
98+
contentType: type,
99+
profile: parameters.profile
99100
};
100101

101102
// separate profile from content-type

lib/documentLoaders/xhr.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
*/
44
'use strict';
55

6+
const contentType = require ('content-type');
7+
68
const {
79
parseLinkHeader,
8-
buildHeaders,
9-
parseContentTypeHeader
10+
buildHeaders
1011
} = require('../util');
1112
const {LINK_HEADER_CONTEXT} = require('../constants');
1213
const JsonLdError = require('../JsonLdError');
@@ -75,15 +76,14 @@ module.exports = ({
7576
});
7677
}
7778

78-
const {contentType, params} =
79-
parseContentTypeHeader(req.getResponseHeader('Content-Type'));
79+
const {type, parameters} = contentType.parse(req);
8080

8181
let doc = {
8282
contextUrl: null,
8383
documentUrl: url,
8484
document: req.response,
85-
contentType: contentType,
86-
profile: params.profile
85+
contentType: type,
86+
profile: parameters.profile
8787
};
8888
let alternate = null;
8989

lib/jsonld.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535
*/
3636
const canonize = require('rdf-canonize');
37+
const contentType = require ('content-type');
3738
const util = require('./util');
3839
const ContextResolver = require('./ContextResolver');
3940
const IdentifierIssuer = util.IdentifierIssuer;
@@ -908,6 +909,10 @@ jsonld.get = async function(url, options) {
908909
for(let i = 0; i < scripts.length; i++) {
909910
const script = scripts[i];
910911
// only application/ld+json
912+
const {type, parameters} = contentType.parse(script.getAttribute('type'));
913+
if(type !== 'application/ld+json') {
914+
continue;
915+
}
911916
if(!script.getAttribute('type').startsWith('application/ld+json')) {
912917
continue;
913918
}

lib/util.js

-30
Original file line numberDiff line numberDiff line change
@@ -143,36 +143,6 @@ api.parseLinkHeader = header => {
143143
return rval;
144144
};
145145

146-
/**
147-
* Parses a content-type header.
148-
* The results will be key'd by the value of "rel".
149-
*
150-
* Accept: application/ld+json
151-
*
152-
* Parses as: ["application/ld+json", {}]
153-
*
154-
* Accept: application/ld+json;profile=http://www.w3.org/ns/json-ld#context
155-
*
156-
* Parses as: ["application/ld+json",
157-
* {profile: "http://www.w3.org/ns/json-ld#context"}]
158-
*
159-
* If there is more than one
160-
*
161-
* @param header the content-type header to parse.
162-
*/
163-
api.parseContentTypeHeader = header => {
164-
const [type, ...rest] = header.split(';');
165-
const params = {};
166-
const rval = [type.trim(), params];
167-
168-
// assign parameters
169-
for(const paramString of rest) {
170-
const [param, value] = paramString.split('=');
171-
params[param.trim().toLowerCase()] = value.trim();
172-
}
173-
return rval;
174-
};
175-
176146
/**
177147
* Throws an exception if the given value is not a valid @type value.
178148
*

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
],
3232
"dependencies": {
3333
"canonicalize": "^1.0.1",
34+
"content-type": "^1.0.4",
3435
"lru-cache": "^5.1.1",
3536
"object.fromentries": "^2.0.2",
3637
"rdf-canonize": "^1.0.2",

0 commit comments

Comments
 (0)