From c0656a5d53883f5f3f31f07eadccaef9570647c6 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Tue, 13 Aug 2024 12:30:00 -0400 Subject: [PATCH] Encapsulate `ContextLoader` creation inside `Converter`. --- lib/Converter.js | 16 +++++++++++----- lib/decode.js | 5 +---- lib/encode.js | 3 +-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/Converter.js b/lib/Converter.js index 086a85c0..eeb6634f 100644 --- a/lib/Converter.js +++ b/lib/Converter.js @@ -19,20 +19,20 @@ export class Converter { * `context`, `url`, `none`, and any JSON-LD type, each of which maps to * another map of values of that type to their associated CBOR-LD integer * values. - * @param {ContextLoader} options.contextLoader - The interface used to - * load JSON-LD contexts. + * @param {documentLoaderFunction} options.documentLoader -The document + * loader to use when resolving JSON-LD Context URLs. * @param {boolean} [options.legacy=false] - True if legacy mode is in * effect, false if not. */ constructor({ - strategy, typeTable, contextLoader, legacy = false + strategy, typeTable, documentLoader, legacy = false } = {}) { this.strategy = strategy; + // FIXME: use strategy.typeTable this.typeTable = typeTable; - this.contextLoader = contextLoader; this.legacy = legacy; - this.initialActiveCtx = new ActiveContext({contextLoader}); + // FIXME: expose differently this.typeTableEncodedAsBytes = legacy ? LEGACY_TYPE_TABLE_ENCODED_AS_BYTES : TYPE_TABLE_ENCODED_AS_BYTES; @@ -40,6 +40,12 @@ export class Converter { strategy.converter = this; // FIXME: expose differently this.reverseTypeTable = strategy.reverseTypeTable; + + const contextLoader = new ContextLoader({ + documentLoader, buildReverseMap: !!this.reverseTypeTable + }); + this.contextLoader = contextLoader; + this.initialActiveCtx = new ActiveContext({contextLoader}); } /** diff --git a/lib/decode.js b/lib/decode.js index 004b0cf8..6b7f7339 100644 --- a/lib/decode.js +++ b/lib/decode.js @@ -4,7 +4,6 @@ import * as cborg from 'cborg'; import {createLegacyTypeTable, createTypeTable} from './tables.js'; import {CborldError} from './CborldError.js'; -import {ContextLoader} from './ContextLoader.js'; import {Converter} from './Converter.js'; import {Decompressor} from './Decompressor.js'; import {inspect} from './util.js'; @@ -97,9 +96,7 @@ function _createConverter({ // decompress CBOR-LD => JSON-LD strategy: new Decompressor({typeTable}), typeTable, - // FIXME: consider passing `documentLoader` and have converted determine - // this based on a strategy API - contextLoader: new ContextLoader({documentLoader, buildReverseMap: true}), + documentLoader, // FIXME: try to eliminate need for legacy flag legacy: isLegacy }); diff --git a/lib/encode.js b/lib/encode.js index 834b5b72..0c3693b8 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -7,7 +7,6 @@ import {createLegacyTypeTable, createTypeTable} from './tables.js'; import {CborldEncoder} from './codecs/CborldEncoder.js'; import {CborldError} from './CborldError.js'; import {Compressor} from './Compressor.js'; -import {ContextLoader} from './ContextLoader.js'; import {Converter} from './Converter.js'; import {inspect} from './util.js'; @@ -169,7 +168,7 @@ function _createConverter({ // compress JSON-LD => CBOR-LD strategy: new Compressor({typeTable}), typeTable, - contextLoader: new ContextLoader({documentLoader}), + documentLoader, // FIXME: try to eliminate need for legacy flag legacy: isLegacy });