@skillre/dsh-plugin-sticky-wall 0.7.0 → 0.8.2
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/README.en.md +34 -11
- package/README.md +35 -14
- package/UNINSTALL.md +22 -7
- package/compatibility.json +78 -1
- package/lib/client.js +1062 -337
- package/lib/config.d.ts +18 -0
- package/lib/config.js +27 -0
- package/lib/frontmatter.d.ts +81 -0
- package/lib/frontmatter.js +440 -0
- package/lib/index.d.ts +9 -4
- package/lib/index.js +236 -36
- package/lib/migrate.d.ts +62 -0
- package/lib/migrate.js +314 -0
- package/lib/store.d.ts +130 -68
- package/lib/store.js +314 -97
- package/lib/transport.d.ts +7 -4
- package/lib/transport.js +14 -3
- package/lib/types.d.ts +87 -6
- package/lib/types.js +38 -5
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
- package/docs/PREVIEW.md +0 -39
package/lib/migrate.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-time migration from the 0.7.x storage domain to real `.md` files.
|
|
3
|
+
*
|
|
4
|
+
* The old build kept a wall in a JSON storage domain. 0.8.0 stores one file per
|
|
5
|
+
* note instead, so an existing user's wall has to be carried across exactly once,
|
|
6
|
+
* without losing a note and without ever leaving a half-migrated directory that
|
|
7
|
+
* the next start would mistake for a finished one.
|
|
8
|
+
*
|
|
9
|
+
* The whole design follows from one requirement: **the notes directory must
|
|
10
|
+
* never be observable in a partial state.** So the notes are written into a
|
|
11
|
+
* sibling staging directory and published with a single `rename`, which is
|
|
12
|
+
* atomic within one parent directory. A crash before the rename leaves the legacy
|
|
13
|
+
* wall untouched and the staging directory removable; a crash after it leaves a
|
|
14
|
+
* complete wall that the marker then declares migrated.
|
|
15
|
+
*
|
|
16
|
+
* Nothing here is on the hot path, and nothing here runs when the directory
|
|
17
|
+
* already holds notes: a user who has been on 0.8.0 for months must not have a
|
|
18
|
+
* stale legacy domain re-imported over edits they have made since.
|
|
19
|
+
*/
|
|
20
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
21
|
+
import { dirname, join, resolve } from 'node:path';
|
|
22
|
+
import { fileNameOf, idOfFile, isNoteId, parseNoteFile, serializeNoteFile } from './frontmatter.js';
|
|
23
|
+
import { COLORS, LEGACY_DOMAIN_NAME, LIMITS, MIGRATION_MARKER, WIDTHS } from './types.js';
|
|
24
|
+
/** The staging directory the migration writes into before publishing. */
|
|
25
|
+
function stagingDirOf(notesDir) {
|
|
26
|
+
return `${notesDir}.migrating`;
|
|
27
|
+
}
|
|
28
|
+
/** True when the directory already holds something this migration must not touch. */
|
|
29
|
+
async function isAlreadyPresent(notesDir) {
|
|
30
|
+
let entries;
|
|
31
|
+
try {
|
|
32
|
+
entries = await readdir(notesDir);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
const code = error.code;
|
|
36
|
+
// No directory, or the path is not a directory at all: nothing to protect.
|
|
37
|
+
// Any other failure is a real I/O problem and is reported as a failure by
|
|
38
|
+
// the caller rather than being read as "empty".
|
|
39
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
40
|
+
return false;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
return entries.some((name) => name === MIGRATION_MARKER || idOfFile(name) !== null);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The marker value written by 0.8.0's BUGGY migration, if this note is it.
|
|
47
|
+
*
|
|
48
|
+
* 0.8.0 read zero legacy notes (its table spec had no `valueSchema`, so the
|
|
49
|
+
* domain quarantined every record) and then seeded the welcome note into an
|
|
50
|
+
* "empty" wall. A user who ran that build therefore has a notes directory that
|
|
51
|
+
* looks already-migrated and holds one seeded welcome note — and the corrected
|
|
52
|
+
* migration would refuse to run, leaving their real notes stranded in `.bak`
|
|
53
|
+
* files forever.
|
|
54
|
+
*
|
|
55
|
+
* The seeded note is identifiable: it is the welcome note, AND the only file in
|
|
56
|
+
* the directory. Both conditions are required. A user who genuinely has one note
|
|
57
|
+
* they wrote themselves must never have it confused with the seed.
|
|
58
|
+
*/
|
|
59
|
+
const SEEDED_WELCOME_BODY = '随手记下一闪而过的灵感,或一件临时的工作。';
|
|
60
|
+
/** True when the directory holds nothing but 0.8.0's auto-seeded welcome note. */
|
|
61
|
+
async function isBuggyEmptyWall(notesDir) {
|
|
62
|
+
let entries;
|
|
63
|
+
try {
|
|
64
|
+
entries = await readdir(notesDir);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const code = error.code;
|
|
68
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
69
|
+
return false;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
const files = entries.filter((name) => idOfFile(name) !== null);
|
|
73
|
+
if (files.length !== 1)
|
|
74
|
+
return false;
|
|
75
|
+
const only = files[0] ?? '';
|
|
76
|
+
if (idOfFile(only) !== 'welcome')
|
|
77
|
+
return false;
|
|
78
|
+
try {
|
|
79
|
+
const text = await readFile(join(notesDir, only), 'utf8');
|
|
80
|
+
return text.includes(SEEDED_WELCOME_BODY);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Recover a wall that 0.8.0 left looking empty.
|
|
88
|
+
*
|
|
89
|
+
* The broken build quarantined every legacy record as `<id>.json.bak.<stamp>`
|
|
90
|
+
* and seeded a welcome note beside them. This reads the quarantined records
|
|
91
|
+
* straight from disk, writes each as a real `.md` next to the seeded note, and
|
|
92
|
+
* removes the seeded note afterwards so the user's own `welcome` note (if the
|
|
93
|
+
* 0.7.x wall had one) is the one that survives.
|
|
94
|
+
*
|
|
95
|
+
* It only ever ADDS notes and removes a note it identified as the seed: an
|
|
96
|
+
* existing `.md` is never overwritten, so a user who wrote something after
|
|
97
|
+
* upgrading keeps it.
|
|
98
|
+
*/
|
|
99
|
+
async function recoverFromBuggyWall(notesDir, options) {
|
|
100
|
+
const legacyDir = options.legacyNotesDir;
|
|
101
|
+
if (legacyDir === undefined)
|
|
102
|
+
return { migrated: false, reason: 'already-present' };
|
|
103
|
+
let entries;
|
|
104
|
+
try {
|
|
105
|
+
entries = await readdir(legacyDir);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return { migrated: false, reason: 'already-present' };
|
|
109
|
+
}
|
|
110
|
+
// Prefer the newest quarantine stamp per note: a second boot of the broken
|
|
111
|
+
// build overwrites the earlier backup, and the newer bytes are the real ones.
|
|
112
|
+
const newest = new Map();
|
|
113
|
+
for (const name of entries) {
|
|
114
|
+
const match = name.match(/^(.*)\.json\.bak\.(\d+)$/);
|
|
115
|
+
if (match === null)
|
|
116
|
+
continue;
|
|
117
|
+
const id = match[1] ?? '';
|
|
118
|
+
if (!isNoteId(id))
|
|
119
|
+
continue;
|
|
120
|
+
const stamp = match[2] ?? '';
|
|
121
|
+
const previous = newest.get(id);
|
|
122
|
+
// The stamp is a zero-padded `YYYYMMDDHHmm`, so a lexical compare orders it.
|
|
123
|
+
if (previous === undefined || stamp >= previous.stamp)
|
|
124
|
+
newest.set(id, { file: name, stamp });
|
|
125
|
+
}
|
|
126
|
+
if (newest.size === 0)
|
|
127
|
+
return { migrated: false, reason: 'already-present' };
|
|
128
|
+
let written = 0;
|
|
129
|
+
for (const [id, { file }] of newest) {
|
|
130
|
+
const target = join(notesDir, fileNameOf(id));
|
|
131
|
+
// Never overwrite a note the user already has on the wall.
|
|
132
|
+
const exists = await stat(target).then(() => true, () => false);
|
|
133
|
+
if (exists)
|
|
134
|
+
continue;
|
|
135
|
+
try {
|
|
136
|
+
const envelope = JSON.parse(await readFile(join(legacyDir, file), 'utf8'));
|
|
137
|
+
const record = envelope.record;
|
|
138
|
+
if (record === null || typeof record !== 'object')
|
|
139
|
+
continue;
|
|
140
|
+
const note = normalizeLegacyRecord(record, id);
|
|
141
|
+
await writeFile(target, serializeNoteFile(note), 'utf8');
|
|
142
|
+
written += 1;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// A record that cannot be read stays quarantined on disk; one bad file
|
|
146
|
+
// must not stop the rest from coming back.
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// The seeded welcome note is only removed when the user's own notes came back,
|
|
150
|
+
// so a failed recovery cannot leave them looking at an empty wall.
|
|
151
|
+
if (written > 0)
|
|
152
|
+
await rm(join(notesDir, fileNameOf('welcome')), { force: true });
|
|
153
|
+
return written === 0
|
|
154
|
+
? { migrated: false, reason: 'already-present' }
|
|
155
|
+
: { migrated: true, reason: 'recovered-from-broken-migration', count: written };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Coerce one quarantined legacy record into a note this build can write.
|
|
159
|
+
*
|
|
160
|
+
* The backup files are raw storage envelopes left by a retired format, so every
|
|
161
|
+
* field is treated as untrusted: a missing or wrongly-typed value takes the same
|
|
162
|
+
* default a fresh note would use rather than aborting the recovery.
|
|
163
|
+
*/
|
|
164
|
+
function normalizeLegacyRecord(record, id) {
|
|
165
|
+
const stamp = new Date().toISOString();
|
|
166
|
+
const text = (value, fallback) => (typeof value === 'string' ? value : fallback);
|
|
167
|
+
const colour = text(record['color'], 'yellow');
|
|
168
|
+
const rawLayout = record['layout'];
|
|
169
|
+
let layout = null;
|
|
170
|
+
if (rawLayout !== null && typeof rawLayout === 'object') {
|
|
171
|
+
const box = rawLayout;
|
|
172
|
+
const width = text(box['width'], 'md');
|
|
173
|
+
layout = {
|
|
174
|
+
x: Number(box['x']) || 0,
|
|
175
|
+
y: Number(box['y']) || 0,
|
|
176
|
+
rotate: Number(box['rotate']) || 0,
|
|
177
|
+
width: WIDTHS.includes(width) ? width : 'md',
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
const tags = Array.isArray(record['tags'])
|
|
181
|
+
? record['tags'].map((tag) => String(tag).trim().toLowerCase()).filter((tag) => tag !== '').slice(0, LIMITS.tags)
|
|
182
|
+
: [];
|
|
183
|
+
return {
|
|
184
|
+
id,
|
|
185
|
+
title: text(record['title'], '').slice(0, LIMITS.title),
|
|
186
|
+
body: text(record['body'], ''),
|
|
187
|
+
tags,
|
|
188
|
+
color: COLORS.includes(colour) ? colour : 'yellow',
|
|
189
|
+
pinned: record['pinned'] === true,
|
|
190
|
+
archived: record['archived'] === true,
|
|
191
|
+
createdAt: text(record['createdAt'], stamp),
|
|
192
|
+
updatedAt: text(record['updatedAt'], stamp),
|
|
193
|
+
layout,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** Two timestamps name the same instant, tolerating format-only differences. */
|
|
197
|
+
function sameInstant(left, right) {
|
|
198
|
+
if (left === right)
|
|
199
|
+
return true;
|
|
200
|
+
const a = new Date(left).getTime();
|
|
201
|
+
const b = new Date(right).getTime();
|
|
202
|
+
return !Number.isNaN(a) && !Number.isNaN(b) && a === b;
|
|
203
|
+
}
|
|
204
|
+
/** One field-by-field read-back comparison between the legacy note and its file. */
|
|
205
|
+
function sameNote(expected, actual) {
|
|
206
|
+
return (expected.id === actual.id &&
|
|
207
|
+
expected.title === actual.title &&
|
|
208
|
+
expected.body === actual.body &&
|
|
209
|
+
expected.color === actual.color &&
|
|
210
|
+
expected.pinned === actual.pinned &&
|
|
211
|
+
expected.archived === actual.archived &&
|
|
212
|
+
expected.tags.length === actual.tags.length &&
|
|
213
|
+
expected.tags.every((tag, index) => tag === actual.tags[index]) &&
|
|
214
|
+
sameLayout(expected.layout, actual.layout) &&
|
|
215
|
+
sameInstant(expected.createdAt, actual.createdAt) &&
|
|
216
|
+
sameInstant(expected.updatedAt, actual.updatedAt));
|
|
217
|
+
}
|
|
218
|
+
/** Compare two placements, treating two `null`s as equal. */
|
|
219
|
+
function sameLayout(left, right) {
|
|
220
|
+
if (left === null || right === null)
|
|
221
|
+
return left === right;
|
|
222
|
+
return left.x === right.x && left.y === right.y && left.rotate === right.rotate && left.width === right.width;
|
|
223
|
+
}
|
|
224
|
+
/** Remove the staging directory, ignoring the case where it is already gone. */
|
|
225
|
+
async function discardStaging(staging) {
|
|
226
|
+
await rm(staging, { recursive: true, force: true });
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Move the legacy notes into `notesDir`, or explain why there was nothing to do.
|
|
230
|
+
*
|
|
231
|
+
* Never throws for an expected outcome: a failed migration is reported as
|
|
232
|
+
* `{ migrated: false, reason: 'failed' }` with the notes directory left absent,
|
|
233
|
+
* so the next start retries with the legacy wall still intact.
|
|
234
|
+
*/
|
|
235
|
+
export async function migrateNotes(options) {
|
|
236
|
+
const notesDir = resolve(options.notesDir);
|
|
237
|
+
const staging = stagingDirOf(notesDir);
|
|
238
|
+
let renamed = false;
|
|
239
|
+
try {
|
|
240
|
+
if (await isAlreadyPresent(notesDir)) {
|
|
241
|
+
// A directory produced by 0.8.0's broken migration also counts as
|
|
242
|
+
// "present", but it is a false empty wall: recover from it instead of
|
|
243
|
+
// leaving the user's real notes quarantined in `.bak` files.
|
|
244
|
+
if (!(await isBuggyEmptyWall(notesDir)))
|
|
245
|
+
return { migrated: false, reason: 'already-present' };
|
|
246
|
+
const recovered = await recoverFromBuggyWall(notesDir, options);
|
|
247
|
+
return recovered;
|
|
248
|
+
}
|
|
249
|
+
const legacy = await options.readLegacy();
|
|
250
|
+
if (legacy.length === 0)
|
|
251
|
+
return { migrated: false, reason: 'no-legacy' };
|
|
252
|
+
// The id becomes a file name, so it has to be one this build would accept
|
|
253
|
+
// from disk. Ids are NOT filtered by shape otherwise: a 0.7.x wall could hold
|
|
254
|
+
// `welcome` (the seeded note does), and rewriting it to a UUID would break
|
|
255
|
+
// every link the user has to that note.
|
|
256
|
+
const invalid = legacy.find((note) => !isNoteId(note.id));
|
|
257
|
+
if (invalid !== undefined)
|
|
258
|
+
throw new Error(`legacy note id ${JSON.stringify(invalid.id)} is not a valid file name`);
|
|
259
|
+
// A leftover staging directory is debris from an interrupted attempt; its
|
|
260
|
+
// contents are untrusted, so it is discarded rather than resumed into.
|
|
261
|
+
await discardStaging(staging);
|
|
262
|
+
await mkdir(dirname(notesDir), { recursive: true });
|
|
263
|
+
await mkdir(staging, { recursive: true });
|
|
264
|
+
for (const note of legacy) {
|
|
265
|
+
await writeFile(join(staging, fileNameOf(note.id)), serializeNoteFile(note), 'utf8');
|
|
266
|
+
}
|
|
267
|
+
// Publish. A directory left by an earlier attempt is empty — a `.md` or a
|
|
268
|
+
// marker in it would have ended this run at `already-present` — and POSIX
|
|
269
|
+
// `rename` replaces an empty destination directory atomically. A destination
|
|
270
|
+
// holding anything else is NOT touched: this plugin never removes files it
|
|
271
|
+
// did not create, so that is a failure the user has to resolve.
|
|
272
|
+
const target = await stat(notesDir).catch(() => null);
|
|
273
|
+
if (target !== null && !target.isDirectory())
|
|
274
|
+
throw new Error(`${notesDir} exists and is not a directory`);
|
|
275
|
+
await rename(staging, notesDir);
|
|
276
|
+
renamed = true;
|
|
277
|
+
// Read the published directory back. The rename guarantees the FILE SET is
|
|
278
|
+
// the one just written, but only a parse proves the bytes survived the
|
|
279
|
+
// round-trip, and a silently mangled note is the one failure a user would
|
|
280
|
+
// never notice until the text was already wrong.
|
|
281
|
+
const files = (await readdir(notesDir)).filter((name) => idOfFile(name) !== null);
|
|
282
|
+
if (files.length !== legacy.length) {
|
|
283
|
+
throw new Error(`migrated ${String(files.length)} files from ${String(legacy.length)} legacy notes`);
|
|
284
|
+
}
|
|
285
|
+
for (const note of legacy) {
|
|
286
|
+
const path = join(notesDir, fileNameOf(note.id));
|
|
287
|
+
const info = await stat(path);
|
|
288
|
+
if (info.size > LIMITS.fileBytes)
|
|
289
|
+
throw new Error(`migrated file for ${note.id} exceeds the size cap`);
|
|
290
|
+
const parsed = parseNoteFile(await readFile(path, 'utf8'), {
|
|
291
|
+
fallbackId: note.id,
|
|
292
|
+
fallbackTime: info.mtime.toISOString(),
|
|
293
|
+
});
|
|
294
|
+
if (!sameNote(note, parsed.note))
|
|
295
|
+
throw new Error(`migrated file for ${note.id} does not round-trip`);
|
|
296
|
+
}
|
|
297
|
+
const at = (options.now ?? (() => new Date()))().toISOString();
|
|
298
|
+
// Best-effort: the marker is documentation of what happened, and the notes
|
|
299
|
+
// themselves already prove the migration. Failing here would delete a good
|
|
300
|
+
// directory over a cosmetic file, so a write error is swallowed — the next
|
|
301
|
+
// start sees the notes and reports `already-present` either way.
|
|
302
|
+
await writeFile(join(notesDir, MIGRATION_MARKER), `${JSON.stringify({ from: LEGACY_DOMAIN_NAME, count: legacy.length, at, sourceCount: legacy.length }, null, 2)}\n`, 'utf8').catch(() => { });
|
|
303
|
+
return { migrated: true, reason: 'migrated', count: legacy.length };
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
// Leave no partial state behind: a published-but-unverified directory is
|
|
307
|
+
// removed so the next start retries instead of treating a bad migration as
|
|
308
|
+
// finished. The staging directory goes too, in every case.
|
|
309
|
+
await discardStaging(staging).catch(() => { });
|
|
310
|
+
if (renamed)
|
|
311
|
+
await rm(notesDir, { recursive: true, force: true }).catch(() => { });
|
|
312
|
+
return { migrated: false, reason: 'failed', error: error instanceof Error ? error.message : String(error) };
|
|
313
|
+
}
|
|
314
|
+
}
|
package/lib/store.d.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Durable note store over the
|
|
2
|
+
* Durable note store over the real `.md` files under the notes directory.
|
|
3
3
|
*
|
|
4
4
|
* The Host owns all data: the Client renders snapshots and submits intents, and
|
|
5
5
|
* every write goes through {@link StickyWallStore} — validate, persist, then
|
|
6
|
-
* hand back the changed records.
|
|
7
|
-
* per-domain write chain, so concurrent writes cannot interleave, and its
|
|
8
|
-
* `per-record` layout keeps one file per note: one damaged record cannot take
|
|
9
|
-
* the whole wall down.
|
|
6
|
+
* hand back the changed records.
|
|
10
7
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
8
|
+
* 0.8.0 moved the notes off the storage domain and onto plain files the user can
|
|
9
|
+
* open in any editor: one `.md` per note, frontmatter for the attributes, the
|
|
10
|
+
* body being the note text itself. That shape is why this store looks the way it
|
|
11
|
+
* does:
|
|
12
|
+
*
|
|
13
|
+
* - **Reads are synchronous.** Every note is loaded once at `open()` into the
|
|
14
|
+
* in-memory map, which stays the AUTHORITATIVE cache. The wall polls with the
|
|
15
|
+
* cheap `revision` call, so a read must never touch the disk.
|
|
16
|
+
* - **Writes go to disk FIRST, then to the cache.** A resolved mutation means
|
|
17
|
+
* the bytes are on disk; the cache can never run ahead of the file, and a
|
|
18
|
+
* failed write leaves the previous state intact.
|
|
19
|
+
* - **Every mutation is serialised** on one chain, so a check-then-write pair —
|
|
20
|
+
* the `maxNotes` count followed by the write — cannot interleave with another
|
|
21
|
+
* request. The filesystem gives us no such guarantee on its own.
|
|
22
|
+
* - **One unreadable file never takes the wall down.** A file that is too large,
|
|
23
|
+
* misnamed or carries a broken frontmatter block is skipped, or degraded to a
|
|
24
|
+
* note with `metaError`, but never thrown away.
|
|
17
25
|
*/
|
|
18
|
-
import { z } from 'zod';
|
|
19
26
|
import { type CentreReserve, type Note, type NoteInput, type NoteLayout, type NotePatch } from './types.js';
|
|
20
27
|
/** Failure carrying a stable code the transport maps onto the RPC envelope. */
|
|
21
28
|
export declare class WallError extends Error {
|
|
@@ -23,46 +30,16 @@ export declare class WallError extends Error {
|
|
|
23
30
|
readonly details: Record<string, unknown>;
|
|
24
31
|
constructor(code: string, message: string, details?: Record<string, unknown>);
|
|
25
32
|
}
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
title: z.ZodDefault<z.ZodString>;
|
|
37
|
-
body: z.ZodString;
|
|
38
|
-
tags: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
39
|
-
color: z.ZodDefault<z.ZodEnum<{
|
|
40
|
-
yellow: "yellow";
|
|
41
|
-
pink: "pink";
|
|
42
|
-
green: "green";
|
|
43
|
-
blue: "blue";
|
|
44
|
-
purple: "purple";
|
|
45
|
-
orange: "orange";
|
|
46
|
-
}>>;
|
|
47
|
-
pinned: z.ZodDefault<z.ZodBoolean>;
|
|
48
|
-
archived: z.ZodDefault<z.ZodBoolean>;
|
|
49
|
-
createdAt: z.ZodString;
|
|
50
|
-
updatedAt: z.ZodString;
|
|
51
|
-
layout: z.ZodDefault<z.ZodNullable<z.ZodObject<{
|
|
52
|
-
x: z.ZodNumber;
|
|
53
|
-
y: z.ZodNumber;
|
|
54
|
-
rotate: z.ZodNumber;
|
|
55
|
-
width: z.ZodEnum<{
|
|
56
|
-
sm: "sm";
|
|
57
|
-
md: "md";
|
|
58
|
-
lg: "lg";
|
|
59
|
-
}>;
|
|
60
|
-
}, z.core.$strip>>>;
|
|
61
|
-
}, z.core.$loose>;
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
/** The minimal domain table face this store uses; the rest is unused here. */
|
|
33
|
+
/**
|
|
34
|
+
* The legacy storage-domain table face, kept only so the one-time migration can
|
|
35
|
+
* read what a 0.7.x install left behind.
|
|
36
|
+
*
|
|
37
|
+
* The store itself no longer touches a domain: the caller opens the legacy
|
|
38
|
+
* domain, drains its `notes` table into `Note[]`, and hands that array to
|
|
39
|
+
* `open()` as `readLegacy`. Mirroring the two-method shape here means the Host
|
|
40
|
+
* entry does not have to re-declare it, and keeps the old file's public surface
|
|
41
|
+
* importable while the migration exists.
|
|
42
|
+
*/
|
|
66
43
|
export interface DomainTable {
|
|
67
44
|
get(key: string): unknown;
|
|
68
45
|
entries(): Iterable<[string, unknown]>;
|
|
@@ -72,40 +49,113 @@ export interface DomainTable {
|
|
|
72
49
|
update(key: string, transform: (value: Note) => Note): Promise<Note>;
|
|
73
50
|
delete(key: string): Promise<boolean>;
|
|
74
51
|
}
|
|
75
|
-
/** The opened-domain handle
|
|
52
|
+
/** The opened-domain handle a legacy migration reads through. */
|
|
76
53
|
export interface DomainHandle {
|
|
77
54
|
table(name: string): DomainTable;
|
|
78
55
|
close(): Promise<void>;
|
|
79
56
|
}
|
|
57
|
+
/** Everything `open()` needs to build a store. */
|
|
58
|
+
export interface StoreOptions {
|
|
59
|
+
/** Directory holding one `.md` per note; created recursively when absent. */
|
|
60
|
+
notesDir: string;
|
|
61
|
+
/** Reject a write that would take the live (non-archived) note count past this. */
|
|
62
|
+
maxNotes: number;
|
|
63
|
+
/** Write the built-in welcome note when the wall is empty. */
|
|
64
|
+
seedWelcomeNote: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Reader for the legacy storage-domain notes.
|
|
67
|
+
*
|
|
68
|
+
* When present, `open()` runs the one-time migration BEFORE scanning the
|
|
69
|
+
* directory. Leave it out and the store opens whatever `.md` files are there
|
|
70
|
+
* and nothing else — which is what the tests and any non-DSH caller want.
|
|
71
|
+
*/
|
|
72
|
+
readLegacy?: (() => Promise<Note[]>) | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Where a broken 0.8.0 run quarantined the legacy records.
|
|
75
|
+
*
|
|
76
|
+
* Passed through to the migration so it can recover a wall that build left
|
|
77
|
+
* looking empty. Omit it when the caller has no legacy medium at all.
|
|
78
|
+
*/
|
|
79
|
+
legacyNotesDir?: string | undefined;
|
|
80
|
+
/** Injectable clock; tests use a stepping clock so timestamps are distinct. */
|
|
81
|
+
now?: (() => Date) | undefined;
|
|
82
|
+
/** Sink for non-fatal problems (an oversized or unreadable note file). */
|
|
83
|
+
log?: ((message: string) => void) | undefined;
|
|
84
|
+
}
|
|
80
85
|
/** The store facade the Host endpoint and the agent tool share. */
|
|
81
86
|
export declare class StickyWallStore {
|
|
82
|
-
|
|
87
|
+
/** Absolute directory the notes live in. */
|
|
88
|
+
readonly notesDir: string;
|
|
89
|
+
private readonly notes;
|
|
83
90
|
private readonly maxNotes;
|
|
84
91
|
private readonly now;
|
|
85
|
-
private readonly
|
|
92
|
+
private readonly log;
|
|
93
|
+
/** Backing counter for {@link revision}. */
|
|
94
|
+
private counter;
|
|
86
95
|
private closed;
|
|
87
96
|
/**
|
|
88
97
|
* Store-level mutation chain.
|
|
89
98
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* write are one indivisible step.
|
|
99
|
+
* A read-then-write pair (the `maxNotes` check followed by the file write) is
|
|
100
|
+
* two steps, and two concurrent creates at the boundary would both read the
|
|
101
|
+
* pre-write count and both succeed. Every mutation therefore runs on this
|
|
102
|
+
* chain, so each check and its write are one indivisible step.
|
|
95
103
|
*/
|
|
96
104
|
private chain;
|
|
97
|
-
constructor(
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
private constructor();
|
|
106
|
+
/**
|
|
107
|
+
* Open the notes directory and load every note.
|
|
108
|
+
*
|
|
109
|
+
* Order matters: the one-time migration runs BEFORE the directory is created,
|
|
110
|
+
* because the migration publishes its result with a single atomic `rename` of
|
|
111
|
+
* a staging directory and would otherwise have to rename over a directory this
|
|
112
|
+
* method had just made. Once the directory and the cache agree, the store is
|
|
113
|
+
* usable; every later read is served from memory.
|
|
114
|
+
*/
|
|
115
|
+
static open(options: StoreOptions): Promise<StickyWallStore>;
|
|
116
|
+
/**
|
|
117
|
+
* Monotonic change counter; bumps once per mutation that actually changed
|
|
118
|
+
* something.
|
|
119
|
+
*
|
|
120
|
+
* The Client cannot be pushed to, so it polls. An unchanged number means the
|
|
121
|
+
* caller must not touch component state at all — that is what keeps a
|
|
122
|
+
* background refresh from interrupting someone mid-composition in the editor.
|
|
123
|
+
*/
|
|
124
|
+
get revision(): number;
|
|
125
|
+
/**
|
|
126
|
+
* Close the store: drain queued writes, then refuse new ones.
|
|
127
|
+
*
|
|
128
|
+
* Draining is the point. A mutation that started before `close()` may still be
|
|
129
|
+
* writing its file; dropping the chain would acknowledge a note to the Client
|
|
130
|
+
* and then lose it on shutdown.
|
|
131
|
+
*/
|
|
105
132
|
close(): Promise<void>;
|
|
106
133
|
private assertOpen;
|
|
107
134
|
/** Queue one mutation so its checks and writes cannot interleave with another. */
|
|
108
135
|
private mutate;
|
|
136
|
+
/** Absolute `.md` path for a note id. */
|
|
137
|
+
pathOf(id: string): string;
|
|
138
|
+
/**
|
|
139
|
+
* Read every note file into the cache.
|
|
140
|
+
*
|
|
141
|
+
* The directory is the source of truth, so this is deliberately forgiving: a
|
|
142
|
+
* subdirectory, a file that is not named after a valid note id, a file too
|
|
143
|
+
* large to be a note, or one that cannot be read is skipped with a log line
|
|
144
|
+
* and the rest of the wall still opens.
|
|
145
|
+
*/
|
|
146
|
+
private load;
|
|
147
|
+
/**
|
|
148
|
+
* Persist one note.
|
|
149
|
+
*
|
|
150
|
+
* The bytes go to a sibling temp file and are then `rename`d over the target:
|
|
151
|
+
* within one directory that is atomic, so a reader — or a crash — sees either
|
|
152
|
+
* the old note or the new one and never a half-written file. Writing the
|
|
153
|
+
* target directly would truncate it first, and a crash in that window loses
|
|
154
|
+
* the note outright.
|
|
155
|
+
*/
|
|
156
|
+
private writeNote;
|
|
157
|
+
/** Remove one note file; already gone is not an error. */
|
|
158
|
+
private dropFile;
|
|
109
159
|
private liveCount;
|
|
110
160
|
/**
|
|
111
161
|
* Reject a write that would take the live (non-archived) note count past
|
|
@@ -118,8 +168,20 @@ export declare class StickyWallStore {
|
|
|
118
168
|
private assertRoom;
|
|
119
169
|
/** Write the one-time welcome note on a brand-new wall. */
|
|
120
170
|
private seedWelcomeNote;
|
|
171
|
+
/** One note by id, or `null` when the wall has no such note. */
|
|
172
|
+
read(id: string): Note | null;
|
|
121
173
|
/** Every stored note, newest-relevant first; archived ones only on request. */
|
|
122
174
|
list(includeArchived?: boolean, sort?: 'pinned' | 'created' | 'updated'): Note[];
|
|
175
|
+
/**
|
|
176
|
+
* The snapshot the store's own mutations work from.
|
|
177
|
+
*
|
|
178
|
+
* Deliberately does NOT check `closed`: {@link close} drains queued writes
|
|
179
|
+
* AFTER flipping the flag, so a mutation that was already acknowledged still
|
|
180
|
+
* has to run to completion — refusing it here would lose a note the caller
|
|
181
|
+
* was told was saved. It is private for exactly that reason: an outside caller
|
|
182
|
+
* always goes through {@link list}, which does check.
|
|
183
|
+
*/
|
|
184
|
+
private gathered;
|
|
123
185
|
/**
|
|
124
186
|
* Create one note from a Client or agent submission.
|
|
125
187
|
*
|