From e16a6d3f7dce182a40422c12007fa7adf9b3ab27 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Sun, 17 Mar 2024 18:20:19 -0400 Subject: [PATCH] Add fetch options with default size and timeout limits. --- CHANGELOG.md | 6 ++++++ lib/config.js | 15 +++++++++++++++ lib/index.js | 10 ++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 lib/config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d5053d..5249816 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # bedrock-jsonld-document-loader ChangeLog +## 5.0.0 - 2024-mm-dd + +### Changed +- **BREAKING**: Add fetch options with default size and timeout limits + (16 KiB and 5 seconds, respectively) for `httpHandlerClient`. + ## 4.0.0 - 2023-09-18 ### Changed diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..ebd0311 --- /dev/null +++ b/lib/config.js @@ -0,0 +1,15 @@ +/*! + * Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved. + */ +import {config} from '@bedrock/core'; + +const cfg = config['jsonld-document-loader'] = {}; + +cfg.httpClientHandler = { + fetchOptions: { + // max size for fetched documents (in bytes, ~16 KiB) + size: 16384, + // timeout in ms for fetching a document + timeout: 5000 + } +}; diff --git a/lib/index.js b/lib/index.js index 61270b6..917cac4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,10 +1,13 @@ /*! - * Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved. */ import {agent} from '@bedrock/https-agent'; +import {config} from '@bedrock/core'; import {httpClient} from '@digitalbazaar/http-client'; import {JsonLdDocumentLoader} from 'jsonld-document-loader'; +import './config.js'; + const jdl = new JsonLdDocumentLoader(); export const httpClientHandler = { @@ -18,9 +21,12 @@ export const httpClientHandler = { if(!url.startsWith('http')) { throw new Error('NotFoundError'); } + const { + httpClientHandler: {fetchOptions} + } = config['jsonld-document-loader']; let result; try { - result = await httpClient.get(url, {agent}); + result = await httpClient.get(url, {...fetchOptions, agent}); } catch(e) { throw new Error('NotFoundError'); }