@twinsearth/timemachine-dsh-plugin 0.0.0-stage → 0.1.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/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/README.md +225 -2
- package/cordis.patch.yml +28 -0
- package/docs/ARCHITECTURE.md +172 -0
- package/docs/REQUIREMENTS.md +81 -0
- package/docs/TESTPLAN.md +155 -0
- package/lib/client.js +818 -0
- package/lib/index.js +5 -0
- package/package.json +48 -4
- package/plugin.json +9 -0
- package/scripts/host-menu-patch.mjs +274 -0
- package/src/archive/tar.mjs +138 -0
- package/src/archive/zstd.mjs +206 -0
- package/src/plugin.mjs +213 -0
- package/src/plugins.mjs +355 -0
- package/src/point.mjs +671 -0
- package/src/registry.mjs +120 -0
- package/src/restore.mjs +777 -0
- package/src/scan.mjs +244 -0
- package/src/service.mjs +448 -0
- package/src/session-log.mjs +123 -0
- package/src/util/fs.mjs +92 -0
- package/src/util/paths.mjs +82 -0
package/src/registry.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace-registry merge.
|
|
3
|
+
*
|
|
4
|
+
* A restore must not replace `storages/workspace.json`: that file belongs to the
|
|
5
|
+
* live client and may already contain workspaces created after the point. The
|
|
6
|
+
* merge adds the restored workspaces and session ids, preserves everything
|
|
7
|
+
* else, and is idempotent.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { normalizeRegistry } from './scan.mjs';
|
|
11
|
+
|
|
12
|
+
function isObject(value) {
|
|
13
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A mutable registry document with the shape DSH 0.2.x writes. */
|
|
17
|
+
export function registryDocument(raw) {
|
|
18
|
+
const base = isObject(raw) ? JSON.parse(JSON.stringify(raw)) : {};
|
|
19
|
+
if (!isObject(base.unit)) base.unit = { name: 'workspace', version: 2 };
|
|
20
|
+
if (!isObject(base.global)) base.global = {};
|
|
21
|
+
if (!isObject(base.tables)) base.tables = {};
|
|
22
|
+
if (Array.isArray(base.tables.workspaces)) {
|
|
23
|
+
const map = {};
|
|
24
|
+
for (const row of base.tables.workspaces) {
|
|
25
|
+
if (isObject(row) && typeof row.id === 'string') map[row.id] = row;
|
|
26
|
+
}
|
|
27
|
+
base.tables.workspaces = map;
|
|
28
|
+
}
|
|
29
|
+
if (!isObject(base.tables.workspaces)) base.tables.workspaces = {};
|
|
30
|
+
const global = base.global;
|
|
31
|
+
if (!Array.isArray(global.workspaceIds)) global.workspaceIds = Object.keys(base.tables.workspaces);
|
|
32
|
+
if (!Array.isArray(global.archivedSessionIds)) global.archivedSessionIds = [];
|
|
33
|
+
if (!Array.isArray(global.pinnedSessionIds)) global.pinnedSessionIds = [];
|
|
34
|
+
if (global.initialized !== true) global.initialized = true;
|
|
35
|
+
if (typeof global.defaultWorkspaceId !== 'string') global.defaultWorkspaceId = null;
|
|
36
|
+
return base;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function unique(values) {
|
|
40
|
+
return [...new Set(values.filter((value) => typeof value === 'string' && value !== ''))];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Merge restored workspaces and session flags into a target registry.
|
|
45
|
+
*
|
|
46
|
+
* @param {{
|
|
47
|
+
* target: object|null,
|
|
48
|
+
* workspaces: Array<{target_id: string, target_path: string, title: string, session_ids: string[], created_at?: string|null}>,
|
|
49
|
+
* sessions: Array<{session_id: string, archived: boolean, pinned: boolean}>,
|
|
50
|
+
* now?: string,
|
|
51
|
+
* }} input
|
|
52
|
+
* @returns {{document: object, added_workspaces: string[], added_sessions: string[], changed: boolean}}
|
|
53
|
+
*/
|
|
54
|
+
export function mergeRegistry(input) {
|
|
55
|
+
const document = registryDocument(input.target);
|
|
56
|
+
const global = document.global;
|
|
57
|
+
const table = document.tables.workspaces;
|
|
58
|
+
const now = input.now ?? new Date().toISOString();
|
|
59
|
+
const addedWorkspaces = [];
|
|
60
|
+
const addedSessions = [];
|
|
61
|
+
|
|
62
|
+
for (const workspace of input.workspaces ?? []) {
|
|
63
|
+
if (typeof workspace.target_id !== 'string' || workspace.target_id === '') continue;
|
|
64
|
+
if (typeof workspace.target_path !== 'string' || workspace.target_path === '') continue;
|
|
65
|
+
const existing = isObject(table[workspace.target_id]) ? table[workspace.target_id] : null;
|
|
66
|
+
const sessionIds = unique([
|
|
67
|
+
...(Array.isArray(existing?.sessionIds) ? existing.sessionIds : []),
|
|
68
|
+
...(workspace.session_ids ?? []),
|
|
69
|
+
]);
|
|
70
|
+
const row = {
|
|
71
|
+
...(existing ?? {}),
|
|
72
|
+
path: workspace.target_path,
|
|
73
|
+
title: typeof workspace.title === 'string' && workspace.title !== ''
|
|
74
|
+
? workspace.title
|
|
75
|
+
: (existing?.title ?? workspace.target_path),
|
|
76
|
+
sessionIds,
|
|
77
|
+
createdAt: existing?.createdAt ?? workspace.created_at ?? now,
|
|
78
|
+
updatedAt: now,
|
|
79
|
+
};
|
|
80
|
+
if (existing === null) addedWorkspaces.push(workspace.target_id);
|
|
81
|
+
for (const id of workspace.session_ids ?? []) {
|
|
82
|
+
if (!(Array.isArray(existing?.sessionIds) && existing.sessionIds.includes(id))) addedSessions.push(id);
|
|
83
|
+
}
|
|
84
|
+
table[workspace.target_id] = row;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
global.workspaceIds = unique([...global.workspaceIds, ...Object.keys(table)]);
|
|
88
|
+
const archived = new Set(global.archivedSessionIds);
|
|
89
|
+
const pinned = new Set(global.pinnedSessionIds);
|
|
90
|
+
for (const session of input.sessions ?? []) {
|
|
91
|
+
if (typeof session.session_id !== 'string' || session.session_id === '') continue;
|
|
92
|
+
if (session.archived === true) archived.add(session.session_id);
|
|
93
|
+
else archived.delete(session.session_id);
|
|
94
|
+
if (session.pinned === true) pinned.add(session.session_id);
|
|
95
|
+
else pinned.delete(session.session_id);
|
|
96
|
+
}
|
|
97
|
+
global.archivedSessionIds = [...archived];
|
|
98
|
+
global.pinnedSessionIds = [...pinned];
|
|
99
|
+
if (typeof global.defaultWorkspaceId !== 'string' || global.defaultWorkspaceId === '' || !table[global.defaultWorkspaceId]) {
|
|
100
|
+
global.defaultWorkspaceId = Object.keys(table)[0] ?? null;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
document,
|
|
104
|
+
added_workspaces: addedWorkspaces,
|
|
105
|
+
added_sessions: unique(addedSessions),
|
|
106
|
+
changed: addedWorkspaces.length > 0 || addedSessions.length > 0 || (input.sessions ?? []).length > 0,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A compact view for the plan UI. */
|
|
111
|
+
export function registrySummary(raw) {
|
|
112
|
+
const normalized = normalizeRegistry(raw);
|
|
113
|
+
return {
|
|
114
|
+
ok: raw !== null,
|
|
115
|
+
workspaces: normalized.workspaces,
|
|
116
|
+
default_workspace_id: normalized.defaultWorkspaceId,
|
|
117
|
+
archived_session_ids: normalized.archivedSessionIds,
|
|
118
|
+
pinned_session_ids: normalized.pinnedSessionIds,
|
|
119
|
+
};
|
|
120
|
+
}
|