From e0654ef0410174cd5217d84944cb4c5ae97f0b08 Mon Sep 17 00:00:00 2001 From: "David I. Lehn" Date: Thu, 8 Feb 2024 01:47:46 -0500 Subject: [PATCH] Use vocab term codec for URIs. Encode all URIs using the vocab term codec. This allows arbitrary URIs to be encoded with the same small integers as other terms. The mapping in a context needs to use a redundant form: `{"URI":"URI"}`. This technique assume a use case where the CBOR-LD size has high priority over context size. --- CHANGELOG.md | 5 +++++ lib/codecs/UriDecoder.js | 6 +++++- lib/codecs/UriEncoder.js | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b480fd33..c45383b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ to cryptosuites) to further reduce their encoded size (instead of using the global table). This approach is incompatible with previous encodings that used the global table. +- **BREAKING**: Encode all URIs using the vocab term codec. This allows + arbitrary URIs to be encoded with the same small integers as other terms. + The mapping in a context needs to use a redundant form: `{"URI":"URI"}`. This + technique assume a use case where the CBOR-LD size has high priority over + context size. ## 6.0.3 - 2023-12-19 diff --git a/lib/codecs/UriDecoder.js b/lib/codecs/UriDecoder.js index c7b5a1b5..1e134c55 100644 --- a/lib/codecs/UriDecoder.js +++ b/lib/codecs/UriDecoder.js @@ -5,6 +5,7 @@ import {Base58DidUrlDecoder} from './Base58DidUrlDecoder.js'; import {CborldDecoder} from './CborldDecoder.js'; import {HttpUrlDecoder} from './HttpUrlDecoder.js'; import {UuidUrnDecoder} from './UuidUrnDecoder.js'; +import {VocabTermDecoder} from './VocabTermDecoder.js'; const SCHEME_ID_TO_DECODER = new Map([ [1, HttpUrlDecoder], @@ -15,8 +16,11 @@ const SCHEME_ID_TO_DECODER = new Map([ ]); export class UriDecoder extends CborldDecoder { - static createDecoder({value} = {}) { + static createDecoder({value, transformer} = {}) { if(!(Array.isArray(value) || value.length > 1)) { + if(transformer.idToTerm.has(value)) { + return VocabTermDecoder.createDecoder({value, transformer}); + } return false; } diff --git a/lib/codecs/UriEncoder.js b/lib/codecs/UriEncoder.js index a79d18e8..086ba61a 100644 --- a/lib/codecs/UriEncoder.js +++ b/lib/codecs/UriEncoder.js @@ -5,6 +5,7 @@ import {Base58DidUrlEncoder} from './Base58DidUrlEncoder.js'; import {CborldEncoder} from './CborldEncoder.js'; import {HttpUrlEncoder} from './HttpUrlEncoder.js'; import {UuidUrnEncoder} from './UuidUrnEncoder.js'; +import {VocabTermEncoder} from './VocabTermEncoder.js'; // an encoded URL is an array with the first element being an integer that // signals which encoder was used: @@ -23,11 +24,19 @@ const SCHEME_TO_ENCODER = new Map([ ]); export class UriEncoder extends CborldEncoder { - static createEncoder({value} = {}) { + static createEncoder({value, transformer} = {}) { if(typeof value !== 'string') { return false; } + const {appContextMap} = transformer; + + // check vocab term map + if(transformer.termToId.has(value)) { + return VocabTermEncoder.createEncoder({value, transformer}); + } + + // check URI prefix codecs // get full colon-delimited prefix let scheme; try {