Skip to content

Commit

Permalink
Consolidate legacy type table creation code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Jul 14, 2024
1 parent 3fb7370 commit 944ceb1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 62 deletions.
36 changes: 11 additions & 25 deletions lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as cborg from 'cborg';
// import * as varint from 'varint';
import {
createLegacyTypeTable,
STRING_TABLE,

Check failure on line 8 in lib/decode.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

'STRING_TABLE' is defined but never used
TYPE_TABLE,

Check failure on line 9 in lib/decode.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

'TYPE_TABLE' is defined but never used
} from './tables.js';
Expand Down Expand Up @@ -33,18 +34,19 @@ const CBORLD_TAG_SECOND_BYTE_LEGACY = 0x05;
* resolving JSON-LD Context URLs.
* @param {diagnosticFunction} options.diagnose - A function that, if
* provided, is called with diagnostic information.
* @param {Map} options.stringTable - A map of string values and
* their associated CBOR-LD integer values.
* @param {Map} options.typeTable - A map of JSON-LD types
* to maps that map values of that type to their CBOR-LD integer values.
* @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 {Map} options.appContextMap - A map of context string values
* to their associated CBOR-LD integer values. For use with legacy cborldBytes.
* to their associated CBOR-LD integer values. For use with legacy
* cborldBytes.
*
* @returns {Promise<object>} - The decoded JSON-LD Document.
*/
export async function decode({
cborldBytes, documentLoader,
stringTable = new Map(),
typeTable = new Map(),
typeTable,
diagnose,
appContextMap = new Map(),
}) {
Expand All @@ -66,7 +68,6 @@ export async function decode({
const decompressor = _createDecompressor({
isLegacy,
documentLoader,
stringTable,
typeTable,
appContextMap
});
Expand All @@ -88,28 +89,13 @@ export async function decode({
function _createDecompressor({
isLegacy,
documentLoader,
stringTable,
typeTable,
appContextMap
}) {
if(isLegacy) {
// FIXME: remove `stringTable`
stringTable = new Map(STRING_TABLE);
for(const [key, value] of appContextMap) {
stringTable.set(key, value);
}
typeTable = new Map(TYPE_TABLE);

// FIXME: remove
typeTable.set('context', stringTable);
typeTable.set('url', stringTable);
typeTable.set('none', stringTable);
typeTable = createLegacyTypeTable({typeTable, appContextMap});
}
return new Decompressor({
documentLoader,
stringTable,
typeTable
});
return new Decompressor({documentLoader, typeTable});
}

function _checkCompressionMode({cborldBytes, isLegacy}) {
Expand Down
50 changes: 13 additions & 37 deletions lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as cborg from 'cborg';
import * as varint from 'varint';
import {
createLegacyTypeTable,
STRING_TABLE,

Check failure on line 8 in lib/encode.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

'STRING_TABLE' is defined but never used
TYPE_TABLE,

Check failure on line 9 in lib/encode.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

'TYPE_TABLE' is defined but never used
} from './tables.js';
Expand All @@ -20,24 +21,23 @@ import {inspect} from './util.js';
* @param {documentLoaderFunction} options.documentLoader -The document loader
* to use when resolving JSON-LD Context URLs.
* @param {number|string} [options.varintValue='legacy] - The varint value for
* the registry entry associated with the resulting CBOR-LD payload.
* For legacy support, use varintTagValue = 'legacy'.
* @param {Map} options.stringTable - A map of string values and
* their associated CBOR-LD integer values.
* @param {Map} options.typeTable - A map of JSON-LD types
* to maps that map values of that type to their associated CBOR-LD
* integer values.
* the registry entry associated with the resulting CBOR-LD payload.
* For legacy support, use varintTagValue = 'legacy'.
* @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 {diagnosticFunction} options.diagnose - A function that, if
* provided, is called with diagnostic information.
* provided, is called with diagnostic information.
* @param {Map} options.appContextMap - For use with the legacy value of
* varintTagValue.
* varintTagValue.
* @param {number} options.compressionMode - For use with the legacy value of
* varintValue.
* varintValue.
*
* @returns {Promise<Uint8Array>} - The encoded CBOR-LD bytes.
*/
export async function encode({
jsonldDocument, documentLoader, varintValue = 'legacy',
stringTable,
typeTable,
diagnose,
appContextMap,
Expand Down Expand Up @@ -74,9 +74,6 @@ export async function encode({
if(!compressionMode) {
compressionMode = 1;
}
if(!appContextMap) {
appContextMap = new Map();
}
}
if(!isLegacy) {
if(compressionMode !== undefined) {
Expand All @@ -90,7 +87,6 @@ export async function encode({
isLegacy,
appContextMap,
documentLoader,
stringTable,
typeTable
});
let suffix;
Expand Down Expand Up @@ -147,32 +143,12 @@ function _createCompressor({
isLegacy,
appContextMap,
documentLoader,
stringTable,
typeTable,
}) {
if(isLegacy) {
if(stringTable || typeTable) {
throw new TypeError(
'Individual tables must not be passed when using "legacy" mode.');
}

// generate legacy mode tables
stringTable = new Map(STRING_TABLE);
for(const [key, value] of appContextMap) {
stringTable.set(key, value);
}
typeTable = new Map(TYPE_TABLE);

// FIXME: remove
typeTable.set('context', stringTable);
typeTable.set('url', stringTable);
typeTable.set('none', stringTable);
typeTable = createLegacyTypeTable({typeTable, appContextMap});
}
return new Compressor({
documentLoader,
stringTable,
typeTable
});
return new Compressor({documentLoader, typeTable});
}

function _getVarintStructure(varintValue) {
Expand Down
23 changes: 23 additions & 0 deletions lib/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ export const TYPE_TABLE = new Map([
]);
export const FIRST_CUSTOM_TERM_ID = 100;

export function createLegacyTypeTable({typeTable, appContextMap} = {}) {
if(typeTable) {
throw new TypeError(
'"typeTable" must not be passed when using "legacy" mode.');
}

// generate legacy type table
typeTable = new Map(TYPE_TABLE);

if(appContextMap) {
// add `appContextMap` to legacy mode combined string table
const stringTable = new Map(STRING_TABLE);
for(const [key, value] of appContextMap) {
stringTable.set(key, value);
}
typeTable.set('context', stringTable);
typeTable.set('url', stringTable);
typeTable.set('none', stringTable);
}

return typeTable;
}

export function reverseMap(m) {
return new Map(Array.from(m, e => e.reverse()));
}

0 comments on commit 944ceb1

Please sign in to comment.