Skip to content

Commit 8696a0b

Browse files
dlongleydavidlehn
authored andcommitted
Use Map for tracking URLs.
1 parent dc01cdc commit 8696a0b

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

lib/context.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,6 @@ async function _retrieveContextUrls(input, options) {
946946
const documentLoader = util.normalizeDocumentLoader(options.documentLoader);
947947

948948
// retrieve all @context URLs in input
949-
const _urls = {};
950949
await retrieve(input, {}, documentLoader);
951950

952951
return input;
@@ -960,17 +959,12 @@ async function _retrieveContextUrls(input, options) {
960959
{code: 'loading remote context failed', max: MAX_CONTEXT_URLS});
961960
}
962961

963-
// find all URLs in the given document, reusing already retrieved URLs
964-
const urls = {};
965-
Object.keys(_urls).forEach(url => {
966-
if(_urls[url] !== false) {
967-
urls[url] = _urls[url];
968-
}
969-
});
962+
// find all URLs in the given document
963+
const urls = new Map();
970964
_findContextUrls(doc, urls, false, options.base);
971965

972966
// queue all unretrieved URLs
973-
const queue = Object.keys(urls).filter(u => urls[u] === false);
967+
const queue = [...urls.keys()].filter(u => urls.get(u) === false);
974968

975969
// retrieve URLs in queue
976970
return Promise.all(queue.map(async url => {
@@ -979,7 +973,7 @@ async function _retrieveContextUrls(input, options) {
979973
throw new JsonLdError(
980974
'Cyclical @context URLs detected.',
981975
'jsonld.ContextUrlError',
982-
{code: 'recursive context inclusion', url: url});
976+
{code: 'recursive context inclusion', url});
983977
}
984978

985979
const _cycles = util.clone(cycles);
@@ -1003,7 +997,7 @@ async function _retrieveContextUrls(input, options) {
1003997
'non-JSON response, or more than one HTTP Link Header was ' +
1004998
'provided for a remote context.',
1005999
'jsonld.InvalidUrl',
1006-
{code: 'loading remote context failed', url: url, cause: e});
1000+
{code: 'loading remote context failed', url, cause: e});
10071001
}
10081002

10091003
// ensure ctx is an object
@@ -1012,7 +1006,7 @@ async function _retrieveContextUrls(input, options) {
10121006
'Dereferencing a URL did not result in a JSON object. The ' +
10131007
'response was valid JSON, but it was not a JSON object.',
10141008
'jsonld.InvalidUrl',
1015-
{code: 'invalid remote context', url: url});
1009+
{code: 'invalid remote context', url});
10161010
}
10171011

10181012
// use empty context if no @context key is present
@@ -1034,7 +1028,7 @@ async function _retrieveContextUrls(input, options) {
10341028
await retrieve(ctx, _cycles, documentLoader);
10351029

10361030
// store retrieved context w/replaced @context URLs
1037-
urls[url] = ctx['@context'];
1031+
urls.set(url, ctx['@context']);
10381032

10391033
// replace all @context URLs in the document
10401034
_findContextUrls(doc, urls, true, options.base);
@@ -1083,7 +1077,7 @@ function _findContextUrls(input, urls, replace, base) {
10831077
const _ctx = ctx[i];
10841078
if(_isString(_ctx)) {
10851079
const prepended = prependBase(base, _ctx);
1086-
const resolved = urls[prepended];
1080+
const resolved = urls.get(prepended);
10871081
// replace w/@context if requested
10881082
if(replace) {
10891083
if(_isArray(resolved)) {
@@ -1096,7 +1090,7 @@ function _findContextUrls(input, urls, replace, base) {
10961090
}
10971091
} else if(resolved === undefined) {
10981092
// @context URL found
1099-
urls[prepended] = false;
1093+
urls.set(prepended, false);
11001094
}
11011095
} else {
11021096
// look for scoped context
@@ -1110,15 +1104,15 @@ function _findContextUrls(input, urls, replace, base) {
11101104
} else if(_isString(ctx)) {
11111105
// string @context
11121106
const prepended = prependBase(base, ctx);
1113-
const resolved = urls[prepended];
1107+
const resolved = urls.get(prepended);
11141108
// replace w/@context if requested
11151109
if(replace) {
11161110
if(resolved !== false) {
11171111
input[key] = resolved;
11181112
}
11191113
} else if(resolved === undefined) {
11201114
// @context URL found
1121-
urls[prepended] = false;
1115+
urls.set(prepended, false);
11221116
}
11231117
} else {
11241118
// look for scoped context

0 commit comments

Comments
 (0)