package-manager 0.0.1-preview.1

The Zena package manager's libraries: manifests, version selection, lockfiles, archives and the registry client

Readme Files Dependencies Versions

package-manager#

Client libraries for Zena packages published on AT Protocol: package manifests (zena-package.json), lockfiles (zena-lock.json), deterministic CAR source archives, dependency resolution, and registry queries. Versions and ranges are the package zena-lang:semver (packages/semver).

Usage#

import {readManifest} from 'package-manager/manifest';
import {Range} from 'zena-lang:semver';

let manifest = readManifest('{"name": "hello", "version": "1.0.0"}');
let version = manifest.version;
let matches = version != null && Range.parse('^1.0.0').includes(version);

Libraries#

  • package-manager/manifest: reading a package's zena-package.json, and rewriting its dependencies while keeping the rest of the file.
  • package-manager/workspace: a workspace's packages, read from disk: the root's manifest, the packages it lists, and the ones they depend on by path.
  • package-manager/files: which files a package publishes: its manifest's files patterns, or by default every file but tests, and always the manifest, README, and license, never hidden files, node_modules, or the lockfile.
  • package-manager/archive: source archives, the CAR files that hold a release's source. Packing is deterministic, so the same files always have the same CID, and unpacking accepts only what packing writes.
  • package-manager/records: the profile, release, retraction, and maintainers records, an organization's members record, and the private membership entries it commits to, read from the atproto data model with the checks their lexicons describe, and written back. Also the rules for package names.
  • package-manager/registry-methods: the NSIDs of the App View's XRPC methods.
  • package-manager/registry: finding packages: resolving a DID, a handle, or scope:name to a DID, a package's versions and retractions, its profile, and its releases with their proofs.
  • package-manager/resolve: version selection, from the ranges in all of a workspace's manifests to what its lockfile pins, installing as few versions of each package as the ranges allow.
  • package-manager/resolution: the packages a workspace's lockfile, zena-lock.json, pins, writing it, and what writing it changes in the pins already there (diffPins), including releases changed in place.
  • package-manager/cache: the package cache, where each source archive is unpacked into sources/<CID> for the compiler to read.
  • package-manager/fetch: fetching pinned packages into the cache. The release record's proof is checked against the account's signing key, the record and the source archive against the CIDs the lockfile pins, and the release's dependencies against the names it maps.
  • package-manager/validate: checking a package directory and its manifest before publishing.
  • package-manager/testing/fake-pds: an in-process PLC directory, DNS-over-HTTPS resolver, and PDS for package accounts, for testing code that finds and fetches packages.
7.7 KiB Raw
/**
 * A workspace's packages, as `zena pkg add` and `zena pkg update` need them
 * (docs/design/package-manifest.md): the root's manifest, the packages its
 * `workspace.packages` lists, and every package any of them depends on by
 * path. Versions are selected for all of them at once, and pinned in the
 * one `zena-lock.json` beside the root's manifest.
 *
 * Files are read through `WorkspaceFiles`, so that tests can hold a
 * workspace in memory; `DiskFiles` reads them with `zena:fs`.
 */

import {Future} from 'zena:async';
import {HashMap, OrderedHashMap} from 'zena:collections';
import {isDirectory, isFile, listDir, readFile} from 'zena:fs';
import {join2, normalize} from 'zena:path';
import {Manifest, ManifestError, readManifest} from './manifest.zena';
import {Requirement} from './resolve.zena';
import {Resolution, ResolutionError} from './resolution.zena';

/** The manifest's file name. */
export let MANIFEST = 'zena-package.json';

/** The lockfile's file name. */
export let LOCKFILE = 'zena-lock.json';

/** How a workspace's files are read. */
export interface WorkspaceFiles {
  /** The text of the file `path`, or null when there is none. */
  read(path: String): Future<String | null>;
  /** The names in the directory `path`, or null when it is not one. */
  list(path: String): Future<Array<String> | null>;
}

/** A workspace's files on disk. */
export final class DiskFiles implements WorkspaceFiles {
  async read(path: String): Future<String | null> {
    return if (await isFile(path)) await readFile(path) else null;
  }

  async list(path: String): Future<Array<String> | null> {
    if (!(await isDirectory(path))) {
      return null;
    }
    let names = new GrowableArray<String>();
    for (let entry in await listDir(path)) {
      names.push(entry.name);
    }
    return names;
  }
}

/** A package of a workspace, and its manifest. */
export final class WorkspacePackage {
  /** The package's directory. */
  dir: String;
  /** Its manifest's text; null for a root that has no manifest yet. */
  text: String | null;
  manifest: Manifest;

  new(dir: String, text: String | null, manifest: Manifest)
      : dir = dir, text = text, manifest = manifest;

  /** The path of its manifest. */
  manifestPath(): String {
    return join2(this.dir, MANIFEST);
  }
}

/** A workspace: its root and the packages it is made of. */
export final class Workspace {
  /** The root, whose manifest has the workspace's packages and scopes. */
  root: WorkspacePackage;
  /**
   * Every package, the root first, then each in the order it was reached:
   * the ones `workspace.packages` lists, and the ones they depend on by path.
   */
  packages: GrowableArray<WorkspacePackage>;

  new(root: WorkspacePackage, packages: GrowableArray<WorkspacePackage>)
      : root = root, packages = packages;

  /** The path of the lockfile. */
  lockfilePath(): String {
    return join2(this.root.dir, LOCKFILE);
  }

  /** What every package requires of published packages. */
  requirements(): GrowableArray<Requirement> {
    let all = new GrowableArray<Requirement>();
    for (let pkg in this.packages) {
      pkg.manifest.requirements.forEach((name, requirement) => {
        all.push(requirement);
      });
    }
    return all;
  }

  /**
   * The lockfile's `workspace` section for `resolution`: each package's
   * published dependencies, by the package's directory relative to the
   * root (`.` for the root), each name mapped to the key of the version
   * selected for it. Throws `ResolutionError` for a dependency the
   * resolution has no version for.
   */
  pinsByPackage(resolution: Resolution): OrderedHashMap<String, OrderedHashMap<String, String>> {
    let out = new OrderedHashMap<String, OrderedHashMap<String, String>>();
    for (let pkg in this.packages) {
      let pins = new OrderedHashMap<String, String>();
      pkg.manifest.requirements.forEach((name, requirement) => {
        let key = resolution.keyOf(requirement.did, requirement.range);
        if (key == null) {
          throw new ResolutionError(`${pkg.manifestPath()}: no version of '${name}' was selected`);
        }
        pins[name] = key;
      });
      if (pins.size > 0) {
        out[relativeDir(this.root.dir, pkg.dir)] = pins;
      }
    }
    return out;
  }

  /**
   * What the workspace, and `pkg`'s own tests, call `pkg`: its `name`,
   * with its scope when it has one, `<scope>:<name>`, and null when it has
   * no name. The compiler names it the same way
   * (docs/design/import-names.md, "Workspace packages").
   */
  localNameOf(pkg: WorkspacePackage): String | null {
    let name = pkg.manifest.name;
    if (name == null) {
      return null;
    }
    let scope = pkg.manifest.scope;
    return if (scope != null) `${scope}:${name}` else name;
  }

  /**
   * The package whose directory holds `dir`, the innermost one if several
   * do, and the root if no other does.
   */
  packageAt(dir: String): WorkspacePackage {
    let path = normalize(dir);
    var found = this.root;
    for (let pkg in this.packages) {
      if ((path == pkg.dir || path.startsWith(`${pkg.dir}/`)) &&
          pkg.dir.length > found.dir.length) {
        found = pkg;
      }
    }
    return found;
  }
}

/**
 * Reads the workspace whose root is `root`. The root need not have a
 * manifest yet. Throws `ManifestError`, its message naming the manifest,
 * for one that cannot be read, and for a path dependency on a directory
 * with no manifest.
 */
export async function readWorkspace(root: String, files: WorkspaceFiles): Future<Workspace> {
  let reader = new WorkspaceReader(files);
  let rootPackage = await reader.read(normalize(root), true);
  for (let entry in rootPackage.manifest.workspacePackages) {
    if (entry.endsWith('/*')) {
      let parent = join2(rootPackage.dir, entry.sliceBytes(0, entry.length - 2));
      let names = await files.list(parent);
      if (names == null) {
        throw new ManifestError(
            `${rootPackage.manifestPath()}: workspace.packages names ${parent}, which is not a directory`);
      }
      for (let name in names) {
        let dir = join2(parent, name);
        if ((await files.read(join2(dir, MANIFEST))) != null) {
          await reader.read(dir, false);
        }
      }
    } else {
      await reader.read(join2(rootPackage.dir, entry), false);
    }
  }
  return new Workspace(rootPackage, reader.packages);
}

/** Reads packages once each, following their path dependencies. */
final class WorkspaceReader {
  #files: WorkspaceFiles;
  #byDir = new HashMap<String, WorkspacePackage>();
  packages = new GrowableArray<WorkspacePackage>();

  new(files: WorkspaceFiles) : #files = files;

  async read(dir: String, isRoot: boolean): Future<WorkspacePackage> {
    if (let (true, pkg) = this.#byDir.get(dir)) {
      return pkg;
    }
    let path = join2(dir, MANIFEST);
    let text = await this.#files.read(path);
    if (text == null && !isRoot) {
      throw new ManifestError(`${dir} has no ${MANIFEST}`);
    }
    var manifest: Manifest | null = null;
    try {
      manifest = readManifest(text);
    } catch (e) {
      throw new ManifestError(`${dir}/${e.message}`);
    }
    let pkg = new WorkspacePackage(dir, text, manifest);
    this.#byDir[dir] = pkg;
    this.packages.push(pkg);
    for (let name in pkg.manifest.paths.keys()) {
      await this.read(join2(dir, pkg.manifest.paths[name]), false);
    }
    return pkg;
  }
}

/**
 * `dir` relative to the workspace's root `root`, as the lockfile's
 * `workspace` section and the compiler name packages: `.` for the root,
 * and a directory outside it as it is.
 */
export function relativeDir(root: String, dir: String): String {
  if (dir == root) {
    return '.';
  }
  if (root == '.') {
    return if (dir.startsWith('./')) dir.sliceBytes(2, dir.length) else dir;
  }
  let prefix = `${root}/`;
  return if (dir.startsWith(prefix)) dir.sliceBytes(prefix.length, dir.length) else dir;
}
  • atproto atproto.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1
  • zena-lang:semver semver.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1
VersionPublishedDependenciesApproved by
0.0.1-preview.1 2026-11-19
  • atproto atproto.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1
  • zena-lang:semver semver.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1