~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
109 lines · 3.5 KB Raw History
/**
 * Reading a registry record into the shape every package reads the same way:
 * what installs it, what it weighs, and every release it holds.
 */

import { beforeEach, describe, expect, it } from 'vitest';
import { account } from '../src/lib/git.js';
import { npmPackage, ociImage, ociPullRef } from '../src/lib/packages.js';

beforeEach(() => {
  account.did = 'did:plc:owner';
  account.pds = 'https://pds.example.com';
  account.handle = 'owner.example.com';
});

/** @param {string} version @param {number} tarball @param {number} manifest */
const version = (version_, tarball, manifest) => ({
  version: version_,
  tarball: { size: tarball },
  manifest: { size: manifest },
  publishedAt: `2026-08-0${version_.slice(-1)}T00:00:00.000Z`,
});

describe('npmPackage', () => {
  const record = {
    name: '@scope/lib',
    distTags: [
      { tag: 'latest', version: '1.1.0' },
      { tag: 'next', version: '2.0.0' },
    ],
    versions: [version('1.0.0', 100, 20), version('1.1.0', 300, 20)],
    updatedAt: '2026-08-02T00:00:00.000Z',
  };

  it('names the command that installs it from this account', () => {
    expect(npmPackage(record, '@scope/lib').install).toBe(
      'npm install @scope/lib --registry https://pds.example.com/npm/',
    );
  });

  it('reads the latest dist-tag as the version that command installs', () => {
    expect(npmPackage(record, '@scope/lib').latest).toBe('1.1.0');
  });

  it('adds up the tarball and manifest blobs of every version', () => {
    expect(npmPackage(record, '@scope/lib').size).toBe(440);
  });

  it('lists the versions newest first', () => {
    expect(
      npmPackage(record, '@scope/lib').releases.map((entry) => entry.name),
    ).toEqual(['1.1.0', '1.0.0']);
  });

  it('reads a record with nothing published', () => {
    const empty = npmPackage({ name: 'lib' }, 'lib');
    expect(empty.releases).toEqual([]);
    expect(empty.size).toBe(0);
    expect(empty.latest).toBe('');
  });
});

describe('ociImage', () => {
  const record = {
    name: 'app/server',
    tags: [
      { tag: 'latest', digest: 'sha256:aa' },
      { tag: '1.2.0', digest: 'sha256:bb' },
    ],
    manifests: [
      { digest: 'sha256:aa', blob: { size: 500 } },
      { digest: 'sha256:bb', blob: { size: 400 } },
    ],
    layers: [{ blob: { size: 9000 } }],
    updatedAt: '2026-08-02T00:00:00.000Z',
  };

  it('names the pull command, which reads the PDS host without a scheme', () => {
    expect(ociImage(record, 'app/server').install).toBe(
      'docker pull pds.example.com/app/server:latest',
    );
    expect(ociPullRef('app/server')).toBe('pds.example.com/app/server');
  });

  it('counts every manifest and layer once, since tags share layers', () => {
    expect(ociImage(record, 'app/server').size).toBe(9900);
  });

  it('reads a tag as a release, sized by its own manifest', () => {
    expect(ociImage(record, 'app/server').releases).toEqual([
      { name: 'latest', size: 500, digest: 'sha256:aa' },
      { name: '1.2.0', size: 400, digest: 'sha256:bb' },
    ]);
  });

  it('pulls the first tag where the image has no latest', () => {
    const tagged = ociImage(
      { ...record, tags: [{ tag: '1.2.0', digest: 'sha256:bb' }] },
      'app/server',
    );
    expect(tagged.install).toBe('docker pull pds.example.com/app/server:1.2.0');
  });

  it('reads an image with no tag at all', () => {
    const untagged = ociImage({ name: 'app/server' }, 'app/server');
    expect(untagged.install).toBe('docker pull pds.example.com/app/server');
    expect(untagged.latest).toBe('');
  });
});