UNPKG

@atcute/cid

Version:

lightweight DASL CID codec library for AT Protocol

56 lines (41 loc) 1.12 kB
import { toBase32 } from '@atcute/multibase'; import { decode, fromString, type Cid } from './codec.ts'; const CID_LINK_SYMBOL = Symbol.for('@atcute/cid-link-wrapper'); export interface CidLink { $link: string; } export class CidLinkWrapper implements CidLink { /** @internal */ readonly [CID_LINK_SYMBOL] = true; readonly bytes: Uint8Array; constructor(bytes: Uint8Array) { this.bytes = bytes; } get $link(): string { const link = `b${toBase32(this.bytes)}`; Object.defineProperty(this, '$link', { value: link, enumerable: true, }); return link; } toJSON(): CidLink { return { $link: this.$link }; } } export const isCidLink = (value: unknown): value is CidLink => { const val = value as any; return ( val instanceof CidLinkWrapper || (val !== null && typeof val === 'object' && typeof val.$link === 'string') ); }; export const toCidLink = (cid: Cid): CidLink => { return new CidLinkWrapper(cid.bytes); }; export const fromCidLink = (link: CidLink): Cid => { if (link instanceof CidLinkWrapper) { return decode(link.bytes); } return fromString(link.$link); };