@cancia/astro 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-5AUIPW2I.js +518 -0
- package/dist/chunk-BMCI2F2Y.js +45 -0
- package/dist/{chunk-ST44VULL.js → chunk-L2VKQJPY.js} +9 -4
- package/dist/{chunk-7MPOERVU.js → chunk-U46HCJN3.js} +20 -3
- package/dist/chunk-U7V53JX7.js +83 -0
- package/dist/{chunk-337LJIKX.js → chunk-UR5WC3RA.js} +1 -1
- package/dist/endpoints/publish.js +47 -17
- package/dist/git-backed-DtiH52EI.d.ts +98 -0
- package/dist/{github-client-BAZ1pW24.d.ts → github-client-Db0pIrqE.d.ts} +9 -0
- package/dist/index.d.ts +54 -2
- package/dist/index.js +83 -23
- package/dist/loader/index.js +2 -2
- package/dist/runtime.d.ts +36 -1
- package/dist/runtime.js +4 -3
- package/dist/storage/index.d.ts +13 -84
- package/dist/storage/index.js +14 -4
- package/package.json +1 -1
- package/dist/chunk-5ELSN6LI.js +0 -244
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createGitHubClient
|
|
3
|
+
} from "./chunk-U7V53JX7.js";
|
|
4
|
+
import {
|
|
5
|
+
hashRev
|
|
6
|
+
} from "./chunk-L2VKQJPY.js";
|
|
7
|
+
import {
|
|
8
|
+
RevConflictError
|
|
9
|
+
} from "./chunk-7IA5B5CF.js";
|
|
10
|
+
|
|
11
|
+
// src/storage/sqlite-v2.ts
|
|
12
|
+
import { createRequire } from "module";
|
|
13
|
+
import { randomUUID } from "crypto";
|
|
14
|
+
var require2 = createRequire(import.meta.url);
|
|
15
|
+
function openDB(dbPath) {
|
|
16
|
+
const Database = require2("better-sqlite3");
|
|
17
|
+
const db = new Database(dbPath);
|
|
18
|
+
db.pragma("journal_mode = WAL");
|
|
19
|
+
db.pragma("foreign_keys = ON");
|
|
20
|
+
db.exec(`
|
|
21
|
+
CREATE TABLE IF NOT EXISTS kv (
|
|
22
|
+
site TEXT NOT NULL, key TEXT NOT NULL, lang TEXT NOT NULL, value TEXT NOT NULL,
|
|
23
|
+
PRIMARY KEY (site, key, lang)
|
|
24
|
+
);
|
|
25
|
+
CREATE TABLE IF NOT EXISTS pages (
|
|
26
|
+
site TEXT NOT NULL, route TEXT NOT NULL, meta TEXT NOT NULL,
|
|
27
|
+
PRIMARY KEY (site, route)
|
|
28
|
+
);
|
|
29
|
+
CREATE TABLE IF NOT EXISTS list_entries (
|
|
30
|
+
site TEXT NOT NULL, list TEXT NOT NULL, id TEXT NOT NULL, locale TEXT NOT NULL,
|
|
31
|
+
data TEXT NOT NULL,
|
|
32
|
+
created_at TEXT NOT NULL, updated_at TEXT NOT NULL,
|
|
33
|
+
PRIMARY KEY (site, list, id, locale)
|
|
34
|
+
);
|
|
35
|
+
CREATE TABLE IF NOT EXISTS list_order (
|
|
36
|
+
site TEXT NOT NULL, list TEXT NOT NULL, ids TEXT NOT NULL,
|
|
37
|
+
PRIMARY KEY (site, list)
|
|
38
|
+
);
|
|
39
|
+
`);
|
|
40
|
+
return db;
|
|
41
|
+
}
|
|
42
|
+
function makeKVStore(db) {
|
|
43
|
+
const getStmt = db.prepare("SELECT value FROM kv WHERE site=? AND key=? AND lang=?");
|
|
44
|
+
const setStmt = db.prepare(
|
|
45
|
+
`INSERT INTO kv (site, key, lang, value) VALUES (?, ?, ?, ?)
|
|
46
|
+
ON CONFLICT(site, key, lang) DO UPDATE SET value=excluded.value`
|
|
47
|
+
);
|
|
48
|
+
const getAllStmt = db.prepare("SELECT key, lang, value FROM kv WHERE site=?");
|
|
49
|
+
const delStmt = db.prepare("DELETE FROM kv WHERE site=? AND key=? AND lang=?");
|
|
50
|
+
return {
|
|
51
|
+
async get(site, key, lang) {
|
|
52
|
+
const row = getStmt.get(site, key, lang);
|
|
53
|
+
return row?.value ?? null;
|
|
54
|
+
},
|
|
55
|
+
async set(site, key, lang, value) {
|
|
56
|
+
setStmt.run(site, key, lang, value);
|
|
57
|
+
},
|
|
58
|
+
async getAll(site) {
|
|
59
|
+
const rows = getAllStmt.all(site);
|
|
60
|
+
const out = {};
|
|
61
|
+
for (const r of rows) out[`${r.key}.${r.lang}`] = r.value;
|
|
62
|
+
return out;
|
|
63
|
+
},
|
|
64
|
+
async delete(site, key, lang) {
|
|
65
|
+
delStmt.run(site, key, lang);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function makePageStore(db) {
|
|
70
|
+
const getStmt = db.prepare("SELECT meta FROM pages WHERE site=? AND route=?");
|
|
71
|
+
const listStmt = db.prepare("SELECT route, meta FROM pages WHERE site=?");
|
|
72
|
+
const upsertStmt = db.prepare(
|
|
73
|
+
`INSERT INTO pages (site, route, meta) VALUES (?, ?, ?)
|
|
74
|
+
ON CONFLICT(site, route) DO UPDATE SET meta=excluded.meta`
|
|
75
|
+
);
|
|
76
|
+
const delStmt = db.prepare("DELETE FROM pages WHERE site=? AND route=?");
|
|
77
|
+
function parseMeta(raw) {
|
|
78
|
+
return JSON.parse(raw);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
async get(site, route) {
|
|
82
|
+
const row = getStmt.get(site, route);
|
|
83
|
+
if (!row) return null;
|
|
84
|
+
const meta = parseMeta(row.meta);
|
|
85
|
+
return { route, meta, _rev: hashRev(meta) };
|
|
86
|
+
},
|
|
87
|
+
async list(site) {
|
|
88
|
+
const rows = listStmt.all(site);
|
|
89
|
+
return rows.map((r) => {
|
|
90
|
+
const meta = parseMeta(r.meta);
|
|
91
|
+
return { route: r.route, meta, _rev: hashRev(meta) };
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
async set(site, route, meta, rev) {
|
|
95
|
+
const existing = getStmt.get(site, route);
|
|
96
|
+
if (existing) {
|
|
97
|
+
const currentRev = hashRev(parseMeta(existing.meta));
|
|
98
|
+
if (rev !== currentRev) throw new RevConflictError();
|
|
99
|
+
}
|
|
100
|
+
upsertStmt.run(site, route, JSON.stringify(meta));
|
|
101
|
+
return { route, meta, _rev: hashRev(meta) };
|
|
102
|
+
},
|
|
103
|
+
async delete(site, route) {
|
|
104
|
+
delStmt.run(site, route);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function rowToEntry(row) {
|
|
109
|
+
const data = JSON.parse(row.data);
|
|
110
|
+
return {
|
|
111
|
+
id: row.id,
|
|
112
|
+
locale: row.locale,
|
|
113
|
+
data,
|
|
114
|
+
createdAt: row.created_at,
|
|
115
|
+
updatedAt: row.updated_at,
|
|
116
|
+
_rev: hashRev(data)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function applyOrder(order, ids) {
|
|
120
|
+
const result = [];
|
|
121
|
+
const seen = /* @__PURE__ */ new Set();
|
|
122
|
+
for (const id of order) {
|
|
123
|
+
if (ids.has(id)) {
|
|
124
|
+
result.push(id);
|
|
125
|
+
seen.add(id);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const extras = [...ids].filter((id) => !seen.has(id)).sort();
|
|
129
|
+
return [...result, ...extras];
|
|
130
|
+
}
|
|
131
|
+
function makeListStore(db) {
|
|
132
|
+
const getOrderStmt = db.prepare("SELECT ids FROM list_order WHERE site=? AND list=?");
|
|
133
|
+
const upsertOrderStmt = db.prepare(
|
|
134
|
+
`INSERT INTO list_order (site, list, ids) VALUES (?, ?, ?)
|
|
135
|
+
ON CONFLICT(site, list) DO UPDATE SET ids=excluded.ids`
|
|
136
|
+
);
|
|
137
|
+
const getEntryStmt = db.prepare(
|
|
138
|
+
"SELECT id, locale, data, created_at, updated_at FROM list_entries WHERE site=? AND list=? AND id=? AND locale=?"
|
|
139
|
+
);
|
|
140
|
+
const allEntriesStmt = db.prepare(
|
|
141
|
+
"SELECT id, locale, data, created_at, updated_at FROM list_entries WHERE site=? AND list=? ORDER BY locale"
|
|
142
|
+
);
|
|
143
|
+
const localeEntriesStmt = db.prepare(
|
|
144
|
+
"SELECT id, locale, data, created_at, updated_at FROM list_entries WHERE site=? AND list=? AND locale=?"
|
|
145
|
+
);
|
|
146
|
+
const insertEntryStmt = db.prepare(
|
|
147
|
+
`INSERT INTO list_entries (site, list, id, locale, data, created_at, updated_at)
|
|
148
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
149
|
+
);
|
|
150
|
+
const updateEntryStmt = db.prepare(
|
|
151
|
+
"UPDATE list_entries SET data=?, updated_at=? WHERE site=? AND list=? AND id=? AND locale=?"
|
|
152
|
+
);
|
|
153
|
+
const deleteEntryStmt = db.prepare(
|
|
154
|
+
"DELETE FROM list_entries WHERE site=? AND list=? AND id=? AND locale=?"
|
|
155
|
+
);
|
|
156
|
+
const deleteEntryAllLocalesStmt = db.prepare(
|
|
157
|
+
"DELETE FROM list_entries WHERE site=? AND list=? AND id=?"
|
|
158
|
+
);
|
|
159
|
+
const distinctIdsStmt = db.prepare(
|
|
160
|
+
"SELECT DISTINCT id FROM list_entries WHERE site=? AND list=?"
|
|
161
|
+
);
|
|
162
|
+
function readOrder(site, listName) {
|
|
163
|
+
const row = getOrderStmt.get(site, listName);
|
|
164
|
+
if (!row) return [];
|
|
165
|
+
return JSON.parse(row.ids);
|
|
166
|
+
}
|
|
167
|
+
function writeOrder(site, listName, ids) {
|
|
168
|
+
upsertOrderStmt.run(site, listName, JSON.stringify(ids));
|
|
169
|
+
}
|
|
170
|
+
function distinctIds(site, listName) {
|
|
171
|
+
const rows = distinctIdsStmt.all(site, listName);
|
|
172
|
+
return new Set(rows.map((r) => r.id));
|
|
173
|
+
}
|
|
174
|
+
const createTx = db.transaction(
|
|
175
|
+
(site, listName, id, locale, dataJson, now) => {
|
|
176
|
+
insertEntryStmt.run(site, listName, id, locale, dataJson, now, now);
|
|
177
|
+
const order = readOrder(site, listName);
|
|
178
|
+
if (!order.includes(id)) {
|
|
179
|
+
writeOrder(site, listName, [...order, id]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
const deleteTx = db.transaction((site, listName, id, locale) => {
|
|
184
|
+
deleteEntryStmt.run(site, listName, id, locale);
|
|
185
|
+
const stillExists = distinctIds(site, listName).has(id);
|
|
186
|
+
if (!stillExists) {
|
|
187
|
+
const order = readOrder(site, listName);
|
|
188
|
+
const next = order.filter((existingId) => existingId !== id);
|
|
189
|
+
if (next.length !== order.length) {
|
|
190
|
+
writeOrder(site, listName, next);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
const reorderTx = db.transaction((site, listName, ids) => {
|
|
195
|
+
const all = distinctIds(site, listName);
|
|
196
|
+
const supplied = new Set(ids);
|
|
197
|
+
for (const id of all) {
|
|
198
|
+
if (!supplied.has(id)) {
|
|
199
|
+
deleteEntryAllLocalesStmt.run(site, listName, id);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
writeOrder(site, listName, ids);
|
|
203
|
+
});
|
|
204
|
+
return {
|
|
205
|
+
async list(site, listName, locale) {
|
|
206
|
+
const order = readOrder(site, listName);
|
|
207
|
+
if (locale !== void 0) {
|
|
208
|
+
const rows2 = localeEntriesStmt.all(site, listName, locale);
|
|
209
|
+
const byId2 = /* @__PURE__ */ new Map();
|
|
210
|
+
for (const r of rows2) byId2.set(r.id, r);
|
|
211
|
+
const sorted = applyOrder(order, new Set(byId2.keys()));
|
|
212
|
+
const entries2 = [];
|
|
213
|
+
for (const id of sorted) {
|
|
214
|
+
const r = byId2.get(id);
|
|
215
|
+
if (r) entries2.push(rowToEntry(r));
|
|
216
|
+
}
|
|
217
|
+
return entries2;
|
|
218
|
+
}
|
|
219
|
+
const rows = allEntriesStmt.all(site, listName);
|
|
220
|
+
const byId = /* @__PURE__ */ new Map();
|
|
221
|
+
const allIds = /* @__PURE__ */ new Set();
|
|
222
|
+
for (const r of rows) {
|
|
223
|
+
allIds.add(r.id);
|
|
224
|
+
const list = byId.get(r.id) ?? [];
|
|
225
|
+
list.push(r);
|
|
226
|
+
byId.set(r.id, list);
|
|
227
|
+
}
|
|
228
|
+
const sortedIds = applyOrder(order, allIds);
|
|
229
|
+
const entries = [];
|
|
230
|
+
for (const id of sortedIds) {
|
|
231
|
+
const group = byId.get(id) ?? [];
|
|
232
|
+
for (const r of group) entries.push(rowToEntry(r));
|
|
233
|
+
}
|
|
234
|
+
return entries;
|
|
235
|
+
},
|
|
236
|
+
async get(site, listName, id, locale) {
|
|
237
|
+
const row = getEntryStmt.get(site, listName, id, locale);
|
|
238
|
+
return row ? rowToEntry(row) : null;
|
|
239
|
+
},
|
|
240
|
+
async create(site, listName, data, locale, id) {
|
|
241
|
+
const finalId = id ?? randomUUID();
|
|
242
|
+
const existing = getEntryStmt.get(site, listName, finalId, locale);
|
|
243
|
+
if (existing) {
|
|
244
|
+
throw new Error(`List entry "${finalId}" already exists in "${listName}" (${locale})`);
|
|
245
|
+
}
|
|
246
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
247
|
+
createTx(site, listName, finalId, locale, JSON.stringify(data), now);
|
|
248
|
+
return {
|
|
249
|
+
id: finalId,
|
|
250
|
+
locale,
|
|
251
|
+
data,
|
|
252
|
+
createdAt: now,
|
|
253
|
+
updatedAt: now,
|
|
254
|
+
_rev: hashRev(data)
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
async update(site, listName, id, locale, data, rev) {
|
|
258
|
+
const existing = getEntryStmt.get(site, listName, id, locale);
|
|
259
|
+
if (!existing) {
|
|
260
|
+
throw new Error(`List entry "${id}" not found in "${listName}" (${locale})`);
|
|
261
|
+
}
|
|
262
|
+
const existingData = JSON.parse(existing.data);
|
|
263
|
+
if (hashRev(existingData) !== rev) throw new RevConflictError();
|
|
264
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
265
|
+
updateEntryStmt.run(JSON.stringify(data), now, site, listName, id, locale);
|
|
266
|
+
return {
|
|
267
|
+
id,
|
|
268
|
+
locale,
|
|
269
|
+
data,
|
|
270
|
+
createdAt: existing.created_at,
|
|
271
|
+
updatedAt: now,
|
|
272
|
+
_rev: hashRev(data)
|
|
273
|
+
};
|
|
274
|
+
},
|
|
275
|
+
async delete(site, listName, id, locale) {
|
|
276
|
+
const existing = getEntryStmt.get(site, listName, id, locale);
|
|
277
|
+
if (!existing) return;
|
|
278
|
+
deleteTx(site, listName, id, locale);
|
|
279
|
+
},
|
|
280
|
+
async reorder(site, listName, ids) {
|
|
281
|
+
const all = distinctIds(site, listName);
|
|
282
|
+
for (const id of ids) {
|
|
283
|
+
if (!all.has(id)) {
|
|
284
|
+
throw new Error(`Cannot reorder: entry "${id}" not found in "${listName}"`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
reorderTx(site, listName, ids);
|
|
288
|
+
},
|
|
289
|
+
async translations(site, listName) {
|
|
290
|
+
const rows = allEntriesStmt.all(site, listName);
|
|
291
|
+
const idToLocales = /* @__PURE__ */ new Map();
|
|
292
|
+
for (const r of rows) {
|
|
293
|
+
const list = idToLocales.get(r.id) ?? [];
|
|
294
|
+
list.push(r.locale);
|
|
295
|
+
idToLocales.set(r.id, list);
|
|
296
|
+
}
|
|
297
|
+
const order = readOrder(site, listName);
|
|
298
|
+
const orderedIds = applyOrder(order, new Set(idToLocales.keys()));
|
|
299
|
+
return orderedIds.map((id) => ({
|
|
300
|
+
id,
|
|
301
|
+
locales: idToLocales.get(id) ?? []
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
var _connections = /* @__PURE__ */ new Map();
|
|
307
|
+
function getConnection(dbPath) {
|
|
308
|
+
let db = _connections.get(dbPath);
|
|
309
|
+
if (!db) {
|
|
310
|
+
db = openDB(dbPath);
|
|
311
|
+
_connections.set(dbPath, db);
|
|
312
|
+
}
|
|
313
|
+
return db;
|
|
314
|
+
}
|
|
315
|
+
function createSqliteAdapterV2(opts = {}) {
|
|
316
|
+
const root = opts.projectRoot ?? process.cwd();
|
|
317
|
+
const dbPath = opts.dbPath ?? `${root}/cancia.db`;
|
|
318
|
+
const db = getConnection(dbPath);
|
|
319
|
+
return {
|
|
320
|
+
kv: makeKVStore(db),
|
|
321
|
+
pages: makePageStore(db),
|
|
322
|
+
lists: makeListStore(db)
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function closeSqliteAdapterV2(dbPath) {
|
|
326
|
+
if (dbPath === void 0) {
|
|
327
|
+
for (const db2 of _connections.values()) db2.close();
|
|
328
|
+
_connections.clear();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const db = _connections.get(dbPath);
|
|
332
|
+
if (db) {
|
|
333
|
+
db.close();
|
|
334
|
+
_connections.delete(dbPath);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/storage/git-backed.ts
|
|
339
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "fs";
|
|
340
|
+
import { join, relative } from "path";
|
|
341
|
+
function createGitBackedAdapter(opts) {
|
|
342
|
+
const projectRoot = opts.projectRoot ?? process.cwd();
|
|
343
|
+
const branch = opts.branch ?? "main";
|
|
344
|
+
const debounceMs = opts.debounceMs ?? 3e3;
|
|
345
|
+
const commitMessage = opts.commitMessage ?? "Cancia: content update";
|
|
346
|
+
const warn = opts.warn ?? ((m) => console.warn(m));
|
|
347
|
+
const onError = opts.onError ?? ((m, e) => console.error(m, e));
|
|
348
|
+
const kvPath = opts.contentPaths?.kvPath ?? join(projectRoot, "cancia-content.json");
|
|
349
|
+
const pagesPath = opts.contentPaths?.pagesPath ?? join(projectRoot, ".cancia", "pages.json");
|
|
350
|
+
const listsDir = opts.contentPaths?.listsDir ?? join(projectRoot, ".cancia", "lists");
|
|
351
|
+
const token = opts.token ?? process.env.CANCIA_GITHUB_TOKEN ?? "";
|
|
352
|
+
let client = null;
|
|
353
|
+
if (opts.client) {
|
|
354
|
+
client = opts.client;
|
|
355
|
+
} else if (token) {
|
|
356
|
+
client = createGitHubClient({
|
|
357
|
+
repo: opts.repo,
|
|
358
|
+
branch,
|
|
359
|
+
token,
|
|
360
|
+
committer: opts.committer,
|
|
361
|
+
fetch: opts.fetch,
|
|
362
|
+
apiBase: opts.apiBase
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
const gitEnabled = client !== null;
|
|
366
|
+
if (!gitEnabled) {
|
|
367
|
+
warn(
|
|
368
|
+
"[cancia] Git-backed storage: no GitHub token (CANCIA_GITHUB_TOKEN) \u2014 running local-only. Edits save to disk but are NOT committed/pushed."
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
372
|
+
let timer = null;
|
|
373
|
+
let flushing = null;
|
|
374
|
+
let rerunRequested = false;
|
|
375
|
+
function toRepoPath(absPath) {
|
|
376
|
+
return relative(projectRoot, absPath).split("\\").join("/");
|
|
377
|
+
}
|
|
378
|
+
function markDirty(absPath) {
|
|
379
|
+
dirty.add(absPath);
|
|
380
|
+
}
|
|
381
|
+
function markListDirty(listName, site) {
|
|
382
|
+
const siteDir = join(listsDir, listName, site);
|
|
383
|
+
if (!existsSync(siteDir)) return;
|
|
384
|
+
const walk = (dir) => {
|
|
385
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
386
|
+
const full = join(dir, entry.name);
|
|
387
|
+
if (entry.isDirectory()) walk(full);
|
|
388
|
+
else if (entry.isFile()) markDirty(full);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
walk(siteDir);
|
|
392
|
+
}
|
|
393
|
+
function scheduleFlush() {
|
|
394
|
+
if (!gitEnabled) return;
|
|
395
|
+
if (timer) clearTimeout(timer);
|
|
396
|
+
timer = setTimeout(() => {
|
|
397
|
+
timer = null;
|
|
398
|
+
void runFlush();
|
|
399
|
+
}, debounceMs);
|
|
400
|
+
}
|
|
401
|
+
async function runFlush() {
|
|
402
|
+
if (flushing) {
|
|
403
|
+
rerunRequested = true;
|
|
404
|
+
return flushing;
|
|
405
|
+
}
|
|
406
|
+
flushing = doFlush().finally(() => {
|
|
407
|
+
flushing = null;
|
|
408
|
+
if (rerunRequested) {
|
|
409
|
+
rerunRequested = false;
|
|
410
|
+
void runFlush();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
return flushing;
|
|
414
|
+
}
|
|
415
|
+
async function doFlush() {
|
|
416
|
+
if (!client || dirty.size === 0) return;
|
|
417
|
+
const batch = [...dirty];
|
|
418
|
+
const files = [];
|
|
419
|
+
for (const abs of batch) {
|
|
420
|
+
if (!existsSync(abs) || !statSync(abs).isFile()) continue;
|
|
421
|
+
files.push({ path: toRepoPath(abs), content: readFileSync(abs, "utf-8") });
|
|
422
|
+
}
|
|
423
|
+
if (files.length === 0) {
|
|
424
|
+
for (const abs of batch) dirty.delete(abs);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
try {
|
|
428
|
+
await client.commitFiles(files, commitMessage);
|
|
429
|
+
for (const abs of batch) dirty.delete(abs);
|
|
430
|
+
} catch (err) {
|
|
431
|
+
onError(
|
|
432
|
+
"[cancia] Git-backed storage: commit failed \u2014 data saved locally, will retry on next flush.",
|
|
433
|
+
err
|
|
434
|
+
);
|
|
435
|
+
throw err;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function flush() {
|
|
439
|
+
if (!gitEnabled) return;
|
|
440
|
+
if (timer) {
|
|
441
|
+
clearTimeout(timer);
|
|
442
|
+
timer = null;
|
|
443
|
+
}
|
|
444
|
+
await runFlush();
|
|
445
|
+
}
|
|
446
|
+
const kv = {
|
|
447
|
+
get: (site, key, lang) => opts.local.kv.get(site, key, lang),
|
|
448
|
+
getAll: (site) => opts.local.kv.getAll(site),
|
|
449
|
+
async set(site, key, lang, value) {
|
|
450
|
+
await opts.local.kv.set(site, key, lang, value);
|
|
451
|
+
markDirty(kvPath);
|
|
452
|
+
scheduleFlush();
|
|
453
|
+
},
|
|
454
|
+
async delete(site, key, lang) {
|
|
455
|
+
await opts.local.kv.delete(site, key, lang);
|
|
456
|
+
markDirty(kvPath);
|
|
457
|
+
scheduleFlush();
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
const pages = {
|
|
461
|
+
get: (site, route) => opts.local.pages.get(site, route),
|
|
462
|
+
list: (site) => opts.local.pages.list(site),
|
|
463
|
+
async set(site, route, meta, rev) {
|
|
464
|
+
const result = await opts.local.pages.set(site, route, meta, rev);
|
|
465
|
+
markDirty(pagesPath);
|
|
466
|
+
scheduleFlush();
|
|
467
|
+
return result;
|
|
468
|
+
},
|
|
469
|
+
async delete(site, route) {
|
|
470
|
+
await opts.local.pages.delete(site, route);
|
|
471
|
+
markDirty(pagesPath);
|
|
472
|
+
scheduleFlush();
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
const lists = {
|
|
476
|
+
list: (site, listName, locale) => opts.local.lists.list(site, listName, locale),
|
|
477
|
+
get: (site, listName, id, locale) => opts.local.lists.get(site, listName, id, locale),
|
|
478
|
+
translations: (site, listName) => opts.local.lists.translations(site, listName),
|
|
479
|
+
async create(site, listName, data, locale, id) {
|
|
480
|
+
const entry = await opts.local.lists.create(site, listName, data, locale, id);
|
|
481
|
+
markListDirty(listName, site);
|
|
482
|
+
scheduleFlush();
|
|
483
|
+
return entry;
|
|
484
|
+
},
|
|
485
|
+
async update(site, listName, id, locale, data, rev) {
|
|
486
|
+
const entry = await opts.local.lists.update(site, listName, id, locale, data, rev);
|
|
487
|
+
markListDirty(listName, site);
|
|
488
|
+
scheduleFlush();
|
|
489
|
+
return entry;
|
|
490
|
+
},
|
|
491
|
+
async delete(site, listName, id, locale) {
|
|
492
|
+
await opts.local.lists.delete(site, listName, id, locale);
|
|
493
|
+
markListDirty(listName, site);
|
|
494
|
+
scheduleFlush();
|
|
495
|
+
},
|
|
496
|
+
async reorder(site, listName, ids) {
|
|
497
|
+
await opts.local.lists.reorder(site, listName, ids);
|
|
498
|
+
markListDirty(listName, site);
|
|
499
|
+
scheduleFlush();
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
const git = {
|
|
503
|
+
flush,
|
|
504
|
+
get gitEnabled() {
|
|
505
|
+
return gitEnabled;
|
|
506
|
+
},
|
|
507
|
+
pendingPaths() {
|
|
508
|
+
return [...dirty].map(toRepoPath);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
return { kv, pages, lists, git };
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export {
|
|
515
|
+
createSqliteAdapterV2,
|
|
516
|
+
closeSqliteAdapterV2,
|
|
517
|
+
createGitBackedAdapter
|
|
518
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createGitHubClient
|
|
3
|
+
} from "./chunk-U7V53JX7.js";
|
|
4
|
+
|
|
5
|
+
// src/publish-hook.ts
|
|
6
|
+
async function firePublish(hook, opts = {}) {
|
|
7
|
+
if (!hook) return { ok: false, kind: "no-hook" };
|
|
8
|
+
const headers = { ...opts.headers ?? {} };
|
|
9
|
+
if (opts.token) headers.Authorization = `Bearer ${opts.token}`;
|
|
10
|
+
const init = { method: opts.method ?? "POST" };
|
|
11
|
+
if (Object.keys(headers).length > 0) init.headers = headers;
|
|
12
|
+
try {
|
|
13
|
+
const res = await fetch(hook, init);
|
|
14
|
+
if (!res.ok) return { ok: false, kind: "bad-response", status: res.status };
|
|
15
|
+
return { ok: true };
|
|
16
|
+
} catch {
|
|
17
|
+
return { ok: false, kind: "unreachable" };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/publish-dispatch.ts
|
|
22
|
+
var CANCIA_PUBLISH_EVENT = "cancia-publish";
|
|
23
|
+
async function fireDispatch(opts) {
|
|
24
|
+
if (!opts.repo || !opts.token) return { ok: false, kind: "not-configured" };
|
|
25
|
+
const client = createGitHubClient({
|
|
26
|
+
repo: opts.repo,
|
|
27
|
+
// branch is irrelevant for dispatch (repo-level event) but required by the
|
|
28
|
+
// client's options — a harmless placeholder.
|
|
29
|
+
branch: "main",
|
|
30
|
+
token: opts.token,
|
|
31
|
+
fetch: opts.fetch,
|
|
32
|
+
apiBase: opts.apiBase
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
await client.dispatch(opts.eventType ?? CANCIA_PUBLISH_EVENT, opts.clientPayload);
|
|
36
|
+
return { ok: true };
|
|
37
|
+
} catch {
|
|
38
|
+
return { ok: false, kind: "failed" };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
firePublish,
|
|
44
|
+
fireDispatch
|
|
45
|
+
};
|
|
@@ -48,10 +48,8 @@ function createJsonFileAdapter(filePath) {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
// src/storage/
|
|
52
|
-
import {
|
|
53
|
-
import { dirname as dirname2, join } from "path";
|
|
54
|
-
import { createHash, randomUUID } from "crypto";
|
|
51
|
+
// src/storage/rev.ts
|
|
52
|
+
import { createHash } from "crypto";
|
|
55
53
|
function canonicalize(value) {
|
|
56
54
|
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
57
55
|
if (Array.isArray(value)) {
|
|
@@ -64,6 +62,11 @@ function canonicalize(value) {
|
|
|
64
62
|
function hashRev(value) {
|
|
65
63
|
return createHash("sha256").update(canonicalize(value)).digest("hex").slice(0, 16);
|
|
66
64
|
}
|
|
65
|
+
|
|
66
|
+
// src/storage/json-file-v2.ts
|
|
67
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2, existsSync as existsSync2, mkdirSync as mkdirSync2, readdirSync, rmSync } from "fs";
|
|
68
|
+
import { dirname as dirname2, join } from "path";
|
|
69
|
+
import { randomUUID } from "crypto";
|
|
67
70
|
function readJsonFile(filePath, fallback) {
|
|
68
71
|
if (!existsSync2(filePath)) return fallback;
|
|
69
72
|
try {
|
|
@@ -315,5 +318,7 @@ function createJsonFileAdapterV2(opts = {}) {
|
|
|
315
318
|
|
|
316
319
|
export {
|
|
317
320
|
createJsonFileAdapter,
|
|
321
|
+
canonicalize,
|
|
322
|
+
hashRev,
|
|
318
323
|
createJsonFileAdapterV2
|
|
319
324
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
createGitBackedAdapter
|
|
3
|
-
|
|
2
|
+
createGitBackedAdapter,
|
|
3
|
+
createSqliteAdapterV2
|
|
4
|
+
} from "./chunk-5AUIPW2I.js";
|
|
4
5
|
import {
|
|
5
6
|
createJsonFileAdapter,
|
|
6
7
|
createJsonFileAdapterV2
|
|
7
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-L2VKQJPY.js";
|
|
8
9
|
import {
|
|
9
10
|
detectImageType,
|
|
10
11
|
isValidSite
|
|
@@ -156,6 +157,7 @@ function makeR2UploadHandler(opts) {
|
|
|
156
157
|
}
|
|
157
158
|
|
|
158
159
|
// src/runtime.ts
|
|
160
|
+
import { isAbsolute, join as join2 } from "path";
|
|
159
161
|
var _runtime = null;
|
|
160
162
|
var _bakedConfig = null;
|
|
161
163
|
function setBakedConfig(config) {
|
|
@@ -169,6 +171,9 @@ function buildRuntimeFromBaked(baked) {
|
|
|
169
171
|
const token = process.env.CANCIA_TOKEN?.trim() || "";
|
|
170
172
|
const secret = baked.public ? void 0 : token || void 0;
|
|
171
173
|
const deployHook = process.env.CANCIA_DEPLOY_HOOK || void 0;
|
|
174
|
+
const deployHookToken = process.env.CANCIA_DEPLOY_HOOK_TOKEN || void 0;
|
|
175
|
+
const publishRepo = baked.publishRepo || void 0;
|
|
176
|
+
const publishGithubToken = process.env.CANCIA_GITHUB_TOKEN?.trim() || void 0;
|
|
172
177
|
const storageV2 = buildStorageV2(baked, projectRoot);
|
|
173
178
|
const storage = storageV2 ? storageV2.kv : lazyJsonFileAdapter(projectRoot);
|
|
174
179
|
const uploadHandler = buildUploadHandler(baked, projectRoot);
|
|
@@ -182,12 +187,20 @@ function buildRuntimeFromBaked(baked) {
|
|
|
182
187
|
secret,
|
|
183
188
|
uploadHandler,
|
|
184
189
|
deployHook,
|
|
190
|
+
deployHookToken,
|
|
191
|
+
deployHookMethod: baked.deployHookMethod,
|
|
192
|
+
deployHookHeaders: baked.deployHookHeaders,
|
|
193
|
+
publishRepo,
|
|
194
|
+
publishGithubToken,
|
|
185
195
|
maxUploadMB: baked.maxUploadMB
|
|
186
196
|
};
|
|
187
197
|
}
|
|
188
198
|
function lazyJsonFileAdapter(projectRoot) {
|
|
189
199
|
return createJsonFileAdapter(projectRoot + "/cancia-content.json");
|
|
190
200
|
}
|
|
201
|
+
function resolveDbPath(dbPath, projectRoot) {
|
|
202
|
+
return isAbsolute(dbPath) ? dbPath : join2(projectRoot, dbPath);
|
|
203
|
+
}
|
|
191
204
|
function buildStorageV2(baked, projectRoot) {
|
|
192
205
|
const desc = baked.storage;
|
|
193
206
|
if (!desc) return void 0;
|
|
@@ -204,6 +217,10 @@ function buildStorageV2(baked, projectRoot) {
|
|
|
204
217
|
commitMessage: desc.commitMessage
|
|
205
218
|
});
|
|
206
219
|
}
|
|
220
|
+
if (desc.kind === "sqlite-v2") {
|
|
221
|
+
const dbPath = desc.dbPath ? resolveDbPath(desc.dbPath, projectRoot) : `${projectRoot}/cancia.db`;
|
|
222
|
+
return createSqliteAdapterV2({ dbPath });
|
|
223
|
+
}
|
|
207
224
|
return createJsonFileAdapterV2({ projectRoot });
|
|
208
225
|
}
|
|
209
226
|
function buildUploadHandler(baked, projectRoot) {
|