Skip to content

Commit

Permalink
Only access type tables from strategies.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Aug 13, 2024
1 parent c0656a5 commit ea0f470
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/Compressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 7 additions & 18 deletions lib/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

/**
Expand Down Expand Up @@ -204,8 +195,6 @@ export class Converter {
}
}

// FIXME: determine if necessary

/**
* Fetches a resource given a URL and returns it as a string.
*
Expand Down
12 changes: 4 additions & 8 deletions lib/Decompressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/codecs/ContextDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/codecs/ContextEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
6 changes: 3 additions & 3 deletions lib/codecs/ValueDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
6 changes: 3 additions & 3 deletions lib/codecs/ValueEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea0f470

Please sign in to comment.