alinea
Version:
Headless git-based CMS
56 lines (54 loc) • 1.54 kB
JavaScript
import "../../chunks/chunk-NZLE2WMY.js";
// src/core/source/MemorySource.ts
import { assert } from "../util/Assert.js";
import { hashBlob } from "./GitUtils.js";
import { ShaMismatchError } from "./ShaMismatchError.js";
import { ReadonlyTree } from "./Tree.js";
var MemorySource = class {
#tree;
#blobs = /* @__PURE__ */ new Map();
constructor(tree = ReadonlyTree.EMPTY, blobs = /* @__PURE__ */ new Map()) {
this.#tree = tree;
this.#blobs = blobs;
}
async getTree() {
return this.#tree;
}
async getTreeIfDifferent(sha) {
return this.#tree.sha === sha ? void 0 : this.#tree;
}
async *getBlobs(shas) {
for (const sha of shas) {
const blob = this.#blobs.get(sha);
assert(blob, `Blob not found: ${sha}`);
yield [sha, blob];
}
}
async addBlob(contents) {
const sha = await hashBlob(contents);
this.#blobs.set(sha, contents);
return sha;
}
async applyChanges(batch) {
const { fromSha, changes } = batch;
if (this.#tree.sha !== fromSha)
throw new ShaMismatchError(fromSha, this.#tree.sha);
for (const change of changes) {
switch (change.op) {
case "add": {
assert(change.contents, "Missing contents");
this.#blobs.set(change.sha, change.contents);
continue;
}
}
}
const compiled = await this.#tree.withChanges(batch);
for (const sha of this.#blobs.keys()) {
if (!compiled.hasSha(sha)) this.#blobs.delete(sha);
}
this.#tree = compiled;
}
};
export {
MemorySource
};