Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# bedrock-jsonld-document-loader ChangeLog

## 5.2.0 - 2024-12-xx

### Changed
- Handle parsing of JSON internally when `content-type` does not include `json`.

## 5.1.0 - 2024-07-31

### Changed
Expand Down
17 changes: 16 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {agent} from '@bedrock/https-agent';
import {config} from '@bedrock/core';
import {httpClient} from '@digitalbazaar/http-client';
import {JsonLdDocumentLoader} from 'jsonld-document-loader';
import {logger} from './logger.js';

import './config.js';

Expand All @@ -31,10 +32,24 @@ export const httpClientHandler = {
throw new Error('NotFoundError');
}

return result.data;
return _getJsonData(result);
}
};

async function _getJsonData(result) {
if(result.data) {
return result.data;
}

try {
return await result.json();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should be open ended and always try to parse arbitrary input. One specific use case is when you get application/octet-stream due to the server not knowing what to do (ex, on github pages with no extension). What other content types make sense to try and parse? If it's HTML or an image, better to not bother and let it fail. So maybe should refactor to something like as a special case:

if(result.headers.get('content-type') === 'application/octet-stream') {
  // try the parse
} else {
  // throw an unsupported content type error
}

(And consider using content-type npm package for parsing the header string.)

} catch(e) {
const {url} = result;
logger.error(`DataError: could not parse JSON from: "${url}"`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that should be logged? Would it be good enough to let calling code handle the error? Maybe add the URL to it.

throw new Error('DataError');
}
}

export const jsonLdDocumentLoader = jdl;
export const documentLoader = jdl.documentLoader.bind(jdl);
export {JsonLdDocumentLoader};
7 changes: 7 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import {loggers} from '@bedrock/core';

const pkgName = 'bedrock-jsonld-document-loader';
export const logger = loggers.get('app').child(pkgName);
Loading