-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for compressing arbitrary URLs.
Add support for compressing arbitrary URLs. Any id which is in the `appContextMap` will be compressed as a single number before using other URL codecs.
- Loading branch information
Showing
5 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*! | ||
* Copyright (c) 2021-2023 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import {CborldDecoder} from './CborldDecoder.js'; | ||
import {CborldError} from '../CborldError.js'; | ||
|
||
const ID = 1026; | ||
|
||
export class AppUrlDecoder extends CborldDecoder { | ||
constructor({reverseAppContextMap} = {}) { | ||
super(); | ||
this.reverseAppContextMap = reverseAppContextMap; | ||
} | ||
|
||
decode({value} = {}) { | ||
// handle compressed app URL | ||
const url = this.reverseAppContextMap.get(value[1]); | ||
if(url === undefined) { | ||
throw new CborldError( | ||
'ERR_UNDEFINED_COMPRESSED_APP_URL', | ||
`Undefined compressed context "${value}".`); | ||
} | ||
return url; | ||
} | ||
|
||
static createDecoder({value, transformer} = {}) { | ||
if(!(Array.isArray(value) && value.length === 2)) { | ||
return false; | ||
} | ||
if(!(value[0] === ID)) { | ||
return false; | ||
} | ||
const {reverseAppContextMap} = transformer; | ||
return new AppUrlDecoder({reverseAppContextMap}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/*! | ||
* Copyright (c) 2021-2023 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import {Token, Type} from 'cborg'; | ||
import {CborldEncoder} from './CborldEncoder.js'; | ||
|
||
const ID = 1026; | ||
|
||
export class AppUrlEncoder extends CborldEncoder { | ||
constructor({value, appContextMap} = {}) { | ||
super(); | ||
this.value = value; | ||
this.appContextMap = appContextMap; | ||
} | ||
|
||
encode() { | ||
const {value} = this; | ||
// FIXME: handle errors (shouldn't happen if called correctly) | ||
const id = this.appContextMap.get(value); | ||
const entries = [ | ||
new Token(Type.uint, ID), | ||
new Token(Type.uint, id) | ||
]; | ||
return [new Token(Type.array, entries.length), entries]; | ||
} | ||
|
||
static createEncoder({value, transformer} = {}) { | ||
// FIXME: needed? | ||
if(typeof value !== 'string') { | ||
return false; | ||
} | ||
const {appContextMap} = transformer; | ||
return new AppUrlEncoder({value, appContextMap}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters