deno.land / x / replicache@v10.0.0-beta.0 / dag / key.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import {Hash, parse as parseHash} from '../hash';
export function chunkDataKey(hash: Hash): string { return `c/${hash}/d`;}
export function chunkMetaKey(hash: Hash): string { return `c/${hash}/m`;}
export function chunkRefCountKey(hash: Hash): string { return `c/${hash}/r`;}
export function headKey(name: string): string { return `h/${name}`;}
export const enum KeyType { ChunkData, ChunkMeta, ChunkRefCount, Head,}
export type Key = | { type: KeyType.ChunkData; hash: Hash; } | { type: KeyType.ChunkMeta; hash: Hash; } | { type: KeyType.ChunkRefCount; hash: Hash; } | { type: KeyType.Head; name: string; };
export function parse(key: string): Key { const invalidKey = () => new Error(`Invalid key: '${key}'`); const hash = () => parseHash(key.substring(2, key.length - 2));
// '/' if (key.charCodeAt(1) === 47) { switch (key.charCodeAt(0)) { // c case 99: { if (key.length < 4 || key.charCodeAt(key.length - 2) !== 47) { throw invalidKey(); } switch (key.charCodeAt(key.length - 1)) { case 100: // d return { type: KeyType.ChunkData, hash: hash(), }; case 109: // m return { type: KeyType.ChunkMeta, hash: hash(), }; case 114: // r return { type: KeyType.ChunkRefCount, hash: hash(), }; } break; } case 104: // h return { type: KeyType.Head, name: key.substring(2), }; } } throw invalidKey();}
Version Info