diff --git a/lib/Compressor.js b/lib/Compressor.js index 20ddca84..01210b1b 100644 --- a/lib/Compressor.js +++ b/lib/Compressor.js @@ -38,12 +38,12 @@ export class Compressor { } // encode `@context`... - const {converter} = this; + const {typeTable} = this; const encodedContexts = []; const isArray = Array.isArray(context); const contexts = isArray ? context : [context]; for(const value of contexts) { - const encoder = ContextEncoder.createEncoder({value, converter}); + const encoder = ContextEncoder.createEncoder({value, typeTable}); encodedContexts.push(encoder || value); } const id = isArray ? CONTEXT_TERM_ID_PLURAL : CONTEXT_TERM_ID; diff --git a/lib/Converter.js b/lib/Converter.js index eeb6634f..3f5be394 100644 --- a/lib/Converter.js +++ b/lib/Converter.js @@ -15,37 +15,28 @@ export class Converter { * @param {object} options - The options to use. * @param {object} options.strategy - The conversion strategy to use, * e.g., a compressor or decompressor. - * @param {Map} options.typeTable - A map of possible value types, including - * `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 {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, documentLoader, legacy = false + strategy, documentLoader, legacy = false } = {}) { this.strategy = strategy; - // FIXME: use strategy.typeTable - this.typeTable = typeTable; this.legacy = legacy; + const contextLoader = new ContextLoader({ + documentLoader, buildReverseMap: !!strategy.reverseTypeTable + }); + this.contextLoader = contextLoader; + this.initialActiveCtx = new ActiveContext({contextLoader}); // FIXME: expose differently - this.typeTableEncodedAsBytes = legacy ? + this.typeTableEncodedAsBytesSet = legacy ? LEGACY_TYPE_TABLE_ENCODED_AS_BYTES : TYPE_TABLE_ENCODED_AS_BYTES; // FIXME: eliminate cyclical reference 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}); } /** @@ -204,8 +195,6 @@ export class Converter { } } -// FIXME: determine if necessary - /** * Fetches a resource given a URL and returns it as a string. * diff --git a/lib/Decompressor.js b/lib/Decompressor.js index 70e158c9..ce473077 100644 --- a/lib/Decompressor.js +++ b/lib/Decompressor.js @@ -40,16 +40,13 @@ export class Decompressor { } async convertContexts({activeCtx, input, output}) { - const {converter} = this; + const {reverseTypeTable} = this; + const decoder = ContextDecoder.createDecoder({reverseTypeTable}); // decode `@context` in `input`, if any const encodedContext = input.get(CONTEXT_TERM_ID); if(encodedContext) { - const decoder = ContextDecoder.createDecoder({ - value: encodedContext, converter - }); - output['@context'] = decoder?.decode({value: encodedContext}) ?? - encodedContext; + output['@context'] = decoder.decode({value: encodedContext}); } const encodedContexts = input.get(CONTEXT_TERM_ID_PLURAL); if(encodedContexts) { @@ -68,8 +65,7 @@ export class Decompressor { } const contexts = []; for(const value of encodedContexts) { - const decoder = ContextDecoder.createDecoder({value, converter}); - contexts.push(decoder ? decoder.decode({value}) : value); + contexts.push(decoder.decode({value})); } output['@context'] = contexts; } diff --git a/lib/codecs/ContextDecoder.js b/lib/codecs/ContextDecoder.js index cc543605..dbd7da8f 100644 --- a/lib/codecs/ContextDecoder.js +++ b/lib/codecs/ContextDecoder.js @@ -26,8 +26,8 @@ export class ContextDecoder extends CborldDecoder { return url; } - static createDecoder({converter} = {}) { - const reverseContextTable = converter.reverseTypeTable.get('context'); + static createDecoder({reverseTypeTable} = {}) { + const reverseContextTable = reverseTypeTable.get('context'); return new ContextDecoder({reverseContextTable}); } } diff --git a/lib/codecs/ContextEncoder.js b/lib/codecs/ContextEncoder.js index f2b7b290..12d14056 100644 --- a/lib/codecs/ContextEncoder.js +++ b/lib/codecs/ContextEncoder.js @@ -20,11 +20,11 @@ export class ContextEncoder extends CborldEncoder { return new Token(Type.uint, id); } - static createEncoder({value, converter} = {}) { + static createEncoder({value, typeTable} = {}) { if(typeof value !== 'string') { return; } - const contextTable = converter.typeTable.get('context'); + const contextTable = typeTable.get('context'); return new ContextEncoder({context: value, contextTable}); } } diff --git a/lib/codecs/ValueDecoder.js b/lib/codecs/ValueDecoder.js index ec9e93b5..6fbf8d6e 100644 --- a/lib/codecs/ValueDecoder.js +++ b/lib/codecs/ValueDecoder.js @@ -33,15 +33,15 @@ export class ValueDecoder extends CborldDecoder { static createDecoder({value, converter, termType, termInfo} = {}) { const tableType = getTableType({termInfo, termType}); - const subTable = converter.reverseTypeTable.get(tableType); + const subTable = converter.strategy.reverseTypeTable.get(tableType); // handle decoding value for term with a subtable if(subTable) { let intValue; let useTable = false; const isBytes = value instanceof Uint8Array; - const {typeTableEncodedAsBytes} = converter; - const tableEncodingUsesBytes = typeTableEncodedAsBytes.has(tableType); + const {typeTableEncodedAsBytesSet} = converter; + const tableEncodingUsesBytes = typeTableEncodedAsBytesSet.has(tableType); if(isBytes && tableEncodingUsesBytes) { useTable = true; intValue = uintFromBytes({bytes: value}); diff --git a/lib/codecs/ValueEncoder.js b/lib/codecs/ValueEncoder.js index 7445a06d..642ca83e 100644 --- a/lib/codecs/ValueEncoder.js +++ b/lib/codecs/ValueEncoder.js @@ -49,15 +49,15 @@ export class ValueEncoder extends CborldEncoder { } // if a subtable exists for `tableType`... - const subTable = converter.typeTable.get(tableType); + const subTable = converter.strategy.typeTable.get(tableType); if(subTable) { let intValue = subTable.get(value); let convertToBytes; let includeSign; if(intValue !== undefined) { // determine if ID from table must be expressed as bytes for `tableType` - const {typeTableEncodedAsBytes} = converter; - convertToBytes = typeTableEncodedAsBytes.has(tableType); + const {typeTableEncodedAsBytesSet} = converter; + convertToBytes = typeTableEncodedAsBytesSet.has(tableType); includeSign = false; } else if(tableType !== 'none' && Number.isInteger(value)) { /* Note: Here is an unusual case that a type subtable has been defined diff --git a/lib/decode.js b/lib/decode.js index 6b7f7339..1c115996 100644 --- a/lib/decode.js +++ b/lib/decode.js @@ -95,7 +95,6 @@ function _createConverter({ return new Converter({ // decompress CBOR-LD => JSON-LD strategy: new Decompressor({typeTable}), - typeTable, documentLoader, // FIXME: try to eliminate need for legacy flag legacy: isLegacy diff --git a/lib/encode.js b/lib/encode.js index 0c3693b8..5df2755f 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -167,7 +167,6 @@ function _createConverter({ return new Converter({ // compress JSON-LD => CBOR-LD strategy: new Compressor({typeTable}), - typeTable, documentLoader, // FIXME: try to eliminate need for legacy flag legacy: isLegacy