@fedify/fedify
Version:
An ActivityPub server framework
31 lines (30 loc) • 778 B
JavaScript
import { encodeText } from "./util.js";
/**
* Class to encode/decode in the supported Bases
*/
export class Base {
name;
code;
alphabet;
codeBuf;
codec;
constructor(name, code, factory, alphabet) {
this.name = name;
this.code = code;
this.alphabet = alphabet;
this.codeBuf = encodeText(this.code);
this.alphabet = alphabet;
this.codec = factory(alphabet);
}
encode(buf) {
return this.codec.encode(buf);
}
decode(string) {
for (const char of string) {
if (this.alphabet && this.alphabet.indexOf(char) < 0) {
throw new Error(`invalid character '${char}' in '${string}'`);
}
}
return this.codec.decode(string);
}
}