From fb38a6db685ce1487c92d0d5827882476c0a7b99 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:25:44 +0100 Subject: [PATCH 1/3] fix: don't mutate contexts --- lib/ParsingContext.ts | 2 +- lib/entryhandler/keyword/EntryHandlerKeywordType.ts | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/ParsingContext.ts b/lib/ParsingContext.ts index f7b6955..91ff27a 100644 --- a/lib/ParsingContext.ts +++ b/lib/ParsingContext.ts @@ -207,7 +207,7 @@ export class ParsingContext { || scopedContext[key]['@context']['@propagate']; // Propagation is true by default if (propagate !== false || i === keysOriginal.length - 1 - offset) { - contextRaw = scopedContext; + contextRaw = { ...scopedContext }; // Clean up final context delete contextRaw['@propagate']; diff --git a/lib/entryhandler/keyword/EntryHandlerKeywordType.ts b/lib/entryhandler/keyword/EntryHandlerKeywordType.ts index ac292b7..98bf23b 100644 --- a/lib/entryhandler/keyword/EntryHandlerKeywordType.ts +++ b/lib/entryhandler/keyword/EntryHandlerKeywordType.ts @@ -69,18 +69,16 @@ export class EntryHandlerKeywordType extends EntryHandlerKeyword { if (hasTypedScopedContext) { // Do not propagate by default scopedContext = scopedContext.then((c) => { - if (!('@propagate' in c.getContextRaw())) { - c.getContextRaw()['@propagate'] = false; - } + let rawContext = c.getContextRaw(); // Set the original context at this depth as a fallback // This is needed when a context was already defined at the given depth, // and this context needs to remain accessible from child nodes when propagation is disabled. - if (c.getContextRaw()['@propagate'] === false) { - c.getContextRaw()['@__propagateFallback'] = context.getContextRaw(); + if (rawContext['@propagate'] !== true) { + rawContext = { ...rawContext, '@propagate': false, '@__propagateFallback': context.getContextRaw() }; } - return c; + return new JsonLdContextNormalized(rawContext); }); // Set the new context in the context tree From fb15661fce241f636f8c88c1ff6fada907eb2295 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:28:57 +0100 Subject: [PATCH 2/3] perf: don't recreate non-mutated context --- lib/entryhandler/keyword/EntryHandlerKeywordType.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/entryhandler/keyword/EntryHandlerKeywordType.ts b/lib/entryhandler/keyword/EntryHandlerKeywordType.ts index 98bf23b..2283ce3 100644 --- a/lib/entryhandler/keyword/EntryHandlerKeywordType.ts +++ b/lib/entryhandler/keyword/EntryHandlerKeywordType.ts @@ -69,16 +69,14 @@ export class EntryHandlerKeywordType extends EntryHandlerKeyword { if (hasTypedScopedContext) { // Do not propagate by default scopedContext = scopedContext.then((c) => { - let rawContext = c.getContextRaw(); - // Set the original context at this depth as a fallback // This is needed when a context was already defined at the given depth, // and this context needs to remain accessible from child nodes when propagation is disabled. - if (rawContext['@propagate'] !== true) { - rawContext = { ...rawContext, '@propagate': false, '@__propagateFallback': context.getContextRaw() }; + if (c.getContextRaw()['@propagate'] !== true) { + return new JsonLdContextNormalized({ ...c.getContextRaw(), '@propagate': false, '@__propagateFallback': context.getContextRaw() }); } - return new JsonLdContextNormalized(rawContext); + return c; }); // Set the new context in the context tree From fcd592b0f8188d883b2d3806b2c57ec172ed6247 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:02:51 +0000 Subject: [PATCH 3/3] chore: bump jsonld-context-parser version --- package.json | 2 +- test/ParsingContext-test.ts | 10 ++++------ yarn.lock | 9 ++++----- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 1e69542..9553d3c 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "buffer": "^6.0.3", "canonicalize": "^1.0.1", "http-link-header": "^1.0.2", - "jsonld-context-parser": "^2.3.3", + "jsonld-context-parser": "^2.4.0", "rdf-data-factory": "^1.1.0", "readable-stream": "^4.0.0" }, diff --git a/test/ParsingContext-test.ts b/test/ParsingContext-test.ts index 8ac412e..91d7d23 100644 --- a/test/ParsingContext-test.ts +++ b/test/ParsingContext-test.ts @@ -1,4 +1,4 @@ -import {JsonLdContextNormalized} from "jsonld-context-parser"; +import {JsonLdContextNormalized, ContextParser} from "jsonld-context-parser"; import {ParsingContext} from "../lib/ParsingContext"; import {ParsingContextMocked} from "../mocks/ParsingContextMocked"; @@ -607,22 +607,20 @@ describe('ParsingContext', () => { describe('for type-scoped and property-scoped contexts', () => { beforeEach(() => { - parsingContext.contextTree.setContext(['', 'a'], Promise.resolve(new JsonLdContextNormalized({ - '@__propagateFallback': { fallback: true }, - '@propagate': false, + const contextParser = new ContextParser(); + parsingContext.contextTree.setContext(['', 'a'], contextParser.parse({ '@vocab': 'http://bla.org/', 'bar': { '@context': { baz: { '@type': '@vocab' }, }, }, - }))); + })); }); it('should consider the applicable property-scoped context', async () => { return expect(await parsingContext.getContext(['', 'a', 'bar'], 0)) .toEqual(new JsonLdContextNormalized({ - '@__propagateFallback': { fallback: true }, '@vocab': 'http://bla.org/', 'bar': { '@id': 'http://bla.org/bar' }, 'baz': { '@type': '@vocab', '@id': 'http://bla.org/baz' }, diff --git a/yarn.lock b/yarn.lock index 1fa95e5..39fb8f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2518,14 +2518,13 @@ jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3: http-link-header "^1.0.2" relative-to-absolute-iri "^1.0.5" -jsonld-context-parser@^2.3.3: - version "2.3.3" - resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.3.3.tgz#0bdab9eb5cb4b7e68aa7d6c38e58455363caaf9c" - integrity sha512-H+REInOx7XI2ciF8wJV31D20Bh+ofBmEjN2Tkds51vypqDJIiD341E5g+hYyrEInIKRnbW58TN/Ehz+ACT0l0w== +jsonld-context-parser@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.4.0.tgz#fae15a56c5ceabd1c4520ab1a9cc12c9a0a8b67d" + integrity sha512-ZYOfvh525SdPd9ReYY58dxB3E2RUEU4DJ6ZibO8AitcowPeBH4L5rCAitE2om5G1P+HMEgYEYEr4EZKbVN4tpA== dependencies: "@types/http-link-header" "^1.0.1" "@types/node" "^18.0.0" - canonicalize "^1.0.1" cross-fetch "^3.0.6" http-link-header "^1.0.2" relative-to-absolute-iri "^1.0.5"