@atcute/cid
Version:
lightweight DASL CID codec library for AT Protocol
33 lines • 942 B
JavaScript
import { toBase32 } from '@atcute/multibase';
import { decode, fromString } from './codec.js';
const CID_LINK_SYMBOL = Symbol.for('@atcute/cid-link-wrapper');
export class CidLinkWrapper {
bytes;
/** @internal */
[CID_LINK_SYMBOL] = true;
constructor(bytes) {
this.bytes = bytes;
}
get $link() {
const encoded = toBase32(this.bytes);
return `b${encoded}`;
}
toJSON() {
return { $link: this.$link };
}
}
export const isCidLink = (value) => {
const val = value;
return (val instanceof CidLinkWrapper ||
(val !== null && typeof val === 'object' && typeof val.$link === 'string'));
};
export const toCidLink = (cid) => {
return new CidLinkWrapper(cid.bytes);
};
export const fromCidLink = (link) => {
if (link instanceof CidLinkWrapper) {
return decode(link.bytes);
}
return fromString(link.$link);
};
//# sourceMappingURL=cid-link.js.map