Skip to content

Commit

Permalink
Use vocab term codec for URIs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidlehn committed Feb 8, 2024
1 parent eda94b5 commit e0654ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/codecs/UriDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion lib/codecs/UriEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 failure on line 32 in lib/codecs/UriEncoder.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

'appContextMap' is assigned a value but never used

// 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 {
Expand Down

0 comments on commit e0654ef

Please sign in to comment.