chadtmiller.com/pds.js
An atproto PDS in JavaScript, for Cloudflare Workers and Node
0
0
import { describe, expect, it } from 'vitest';
import {
CID,
cborDecodeJson,
cborEncodeDagCbor,
cidToBytes,
jsonToLex,
lexToJson,
} from '../src/repo.js';
const LINK = 'bafyreidefdycgbfy3oglcb6ism3eqhyp5llsrpzxjsuac2gsy4mtrtx244';
describe('jsonToLex and lexToJson', () => {
const json = {
$type: 'com.example.post',
image: {
$type: 'blob',
ref: { $link: LINK },
mimeType: 'image/png',
size: 3,
},
raw: { $bytes: 'AQID' },
list: [{ $link: LINK }, 1, 'x', null, true],
// Not a link: a second key.
notLink: { $link: LINK, other: 1 },
};
it('stores a link as tag 42 and bytes as a byte string', () => {
const lex = /** @type {any} */ (jsonToLex(json));
expect(lex.image.ref).toBeInstanceOf(CID);
expect(lex.image.ref.bytes).toEqual(cidToBytes(LINK));
expect(lex.raw).toEqual(new Uint8Array([1, 2, 3]));
expect(lex.list[0]).toBeInstanceOf(CID);
expect(lex.notLink).toEqual({ $link: LINK, other: 1 });
const bytes = cborEncodeDagCbor(lex);
// d8 2a: tag 42.
expect(Buffer.from(bytes).includes(Buffer.from([0xd8, 0x2a]))).toBe(true);
});
it('round-trips through storage', () => {
expect(cborDecodeJson(cborEncodeDagCbor(jsonToLex(json)))).toEqual(json);
});
it('leaves plain JSON alone', () => {
const plain = { a: [1, { b: 'c' }], d: null };
expect(jsonToLex(plain)).toEqual(plain);
expect(lexToJson(plain)).toEqual(plain);
});
});