alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
88 lines (86 loc) • 2.54 kB
JavaScript
import "../../chunks/chunk-U5RRZUYZ.js";
// src/cloud/server/CloudDebugHandler.ts
import { Handler, JWTPreviews } from "alinea/backend";
import { GitHistory } from "alinea/cli/serve/GitHistory";
import { createId } from "alinea/core";
var latency = 0;
var lag = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
var DebugCloud = class {
constructor(config, db, rootDir) {
this.config = config;
this.db = db;
this.history = new GitHistory(config, rootDir);
}
drafts = /* @__PURE__ */ new Map();
pending = [];
history;
async mutate(params) {
await lag(latency);
const mutations = params.mutations.flatMap((mutate) => mutate.meta);
for (const mutation of params.mutations) {
console.log(
`> cloud: mutate ${mutation.meta.type} - ${mutation.meta.entryId}`
);
}
const toCommitHash = createId();
await this.db.applyMutations(mutations, toCommitHash);
this.pending.push({ ...params, toCommitHash });
console.log(`> cloud: current ${toCommitHash}`);
return { commitHash: toCommitHash };
}
prepareUpload(file) {
throw new Error(`Not implemented`);
}
async revisions(file) {
await lag(latency);
return this.history.revisions(file, void 0);
}
async revisionData(file, revision) {
await lag(latency);
return this.history.revisionData(file, revision, void 0);
}
async getDraft(entryId) {
await lag(latency);
return this.drafts.get(entryId);
}
async storeDraft(draft) {
await lag(latency);
console.log(`> cloud: store draft ${draft.entryId}`);
this.drafts.set(draft.entryId, draft);
}
async pendingSince(commitHash) {
await lag(latency);
console.log(`> cloud: pending since ${commitHash}`);
let i = this.pending.length;
for (; i >= 0; i--)
if (i > 0 && this.pending[i - 1].toCommitHash === commitHash)
break;
const pending = this.pending.slice(i);
if (pending.length === 0)
return void 0;
return {
toCommitHash: pending[pending.length - 1].toCommitHash,
mutations: pending.flatMap(
(params) => params.mutations.flatMap((mutate) => mutate.meta)
)
};
}
};
function createCloudDebugHandler(config, db, rootDir) {
const api = new DebugCloud(config, db, rootDir);
return new Handler({
db,
config,
target: api,
media: api,
history: api,
drafts: api,
pending: api,
previews: new JWTPreviews("dev"),
previewAuthToken: "dev"
});
}
export {
DebugCloud,
createCloudDebugHandler
};