Skip to content

Commit

Permalink
Use content-type package, instead of purpose-defined parseContentType…
Browse files Browse the repository at this point in the history
… method.
  • Loading branch information
gkellogg committed Feb 17, 2020
1 parent 157c318 commit 84abcd8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
11 changes: 6 additions & 5 deletions lib/documentLoaders/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*/
'use strict';

const contentType = require ('content-type');

const {
parseLinkHeader,
buildHeaders,
parseContentTypeHeader
buildHeaders
} = require('../util');
const {LINK_HEADER_CONTEXT} = require('../constants');
const JsonLdError = require('../JsonLdError');
Expand Down Expand Up @@ -88,14 +89,14 @@ module.exports = ({
}

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

doc = {
contextUrl: null,
documentUrl: url,
document: body || null,
contentType: contentType,
profile: params.profile
contentType: type,
profile: parameters.profile
};

// separate profile from content-type
Expand Down
12 changes: 6 additions & 6 deletions lib/documentLoaders/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*/
'use strict';

const contentType = require ('content-type');

const {
parseLinkHeader,
buildHeaders,
parseContentTypeHeader
buildHeaders
} = require('../util');
const {LINK_HEADER_CONTEXT} = require('../constants');
const JsonLdError = require('../JsonLdError');
Expand Down Expand Up @@ -75,15 +76,14 @@ module.exports = ({
});
}

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

const doc = {
contextUrl: null,
documentUrl: url,
document: req.response,
contentType: contentType,
profile: params.profile
contentType: type,
profile: parameters.profile
};
let alternate = null;

Expand Down
5 changes: 5 additions & 0 deletions lib/jsonld.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
const canonize = require('rdf-canonize');
const contentType = require ('content-type');
const util = require('./util');
const ContextResolver = require('./ContextResolver');
const IdentifierIssuer = util.IdentifierIssuer;
Expand Down Expand Up @@ -900,6 +901,10 @@ jsonld.get = async function(url, options) {
for(let i = 0; i < scripts.length; i++) {
const script = scripts[i];
// only application/ld+json
const {type, parameters} = contentType.parse(script.getAttribute('type'));
if(type !== 'application/ld+json') {
continue;
}
if(!script.getAttribute('type').startsWith('application/ld+json')) {
continue;
}
Expand Down
30 changes: 0 additions & 30 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,36 +143,6 @@ api.parseLinkHeader = header => {
return rval;
};

/**
* Parses a content-type header.
* The results will be key'd by the value of "rel".
*
* Accept: application/ld+json
*
* Parses as: ["application/ld+json", {}]
*
* Accept: application/ld+json;profile=http://www.w3.org/ns/json-ld#context
*
* Parses as: ["application/ld+json",
* {profile: "http://www.w3.org/ns/json-ld#context"}]
*
* If there is more than one
*
* @param header the content-type header to parse.
*/
api.parseContentTypeHeader = header => {
const [type, ...rest] = header.split(';');
const params = {};
const rval = [type.trim(), params];

// assign parameters
for(const paramString of rest) {
const [param, value] = paramString.split('=');
params[param.trim().toLowerCase()] = value.trim();
}
return rval;
};

/**
* Throws an exception if the given value is not a valid @type value.
*
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
],
"dependencies": {
"canonicalize": "^1.0.1",
"content-type": "^1.0.4",
"lru-cache": "^5.1.1",
"rdf-canonize": "^1.0.2",
"request": "^2.88.0",
Expand Down

0 comments on commit 84abcd8

Please sign in to comment.