broader-vids 0.1.0 → 0.1.4

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.
@@ -1,327 +1,327 @@
1
- "use strict";
2
-
3
- const fs = require("node:fs");
4
- const os = require("node:os");
5
- const path = require("node:path");
6
- const { randomUUID } = require("node:crypto");
7
- const { withSetup } = require("./runtime-lock.cjs");
8
-
9
- const MARKER = ".broader-vids-profile.json";
10
- const FORMAT = "broader-vids.profile";
11
- const KEEP_RESET = new Set([
12
- MARKER,
13
- ".launcher",
14
- "runtimes",
15
- "wheels",
16
- "models",
17
- "access",
18
- "launcher.json",
19
- ]);
20
- const MANAGED = new Set([
21
- ...KEEP_RESET,
22
- ".dashboard",
23
- ".reset-pending",
24
- "onboarding.json",
25
- "backups",
26
- "evidence",
27
- "recommendations",
28
- "discovery",
29
- "feedback",
30
- "runs",
31
- "archives",
32
- "broader-vids.sqlite3",
33
- "broader-vids.sqlite3-wal",
34
- "broader-vids.sqlite3-shm",
35
- "broader-vids.sqlite3-journal",
36
- ]);
37
-
38
- function inside(parent, child) {
39
- const relative = path.relative(parent, child);
40
- return (
41
- relative === "" ||
42
- (!relative.startsWith(`..${path.sep}`) &&
43
- relative !== ".." &&
44
- !path.isAbsolute(relative))
45
- );
46
- }
47
-
48
- function ensureMarker(root) {
49
- const file = path.join(root, MARKER);
50
- if (fs.existsSync(file)) return;
51
- const temporary = path.join(root, `.${randomUUID()}.tmp`);
52
- try {
53
- fs.writeFileSync(
54
- temporary,
55
- JSON.stringify({ format: FORMAT, version: 1 }),
56
- { flag: "wx", mode: 0o600 },
57
- );
58
- fs.renameSync(temporary, file);
59
- } finally {
60
- if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
61
- }
62
- }
63
-
64
- function readSmall(file) {
65
- if (fs.lstatSync(file).isSymbolicLink() || fs.statSync(file).size > 4096)
66
- throw Error("Profile identity metadata is invalid.");
67
- return JSON.parse(fs.readFileSync(file, "utf8"));
68
- }
69
-
70
- function recognized(root) {
71
- const marker = path.join(root, MARKER);
72
- if (fs.existsSync(marker)) {
73
- const value = readSmall(marker);
74
- return value && value.format === FORMAT && value.version === 1;
75
- }
76
- const config = path.join(root, "onboarding.json");
77
- const database = path.join(root, "broader-vids.sqlite3");
78
- if (
79
- !fs.existsSync(config) ||
80
- !fs.existsSync(database) ||
81
- fs.lstatSync(database).isSymbolicLink()
82
- )
83
- return false;
84
- const value = readSmall(config);
85
- const header = Buffer.alloc(16);
86
- const descriptor = fs.openSync(database, "r");
87
- try {
88
- fs.readSync(descriptor, header, 0, header.length, 0);
89
- } finally {
90
- fs.closeSync(descriptor);
91
- }
92
- return (
93
- value &&
94
- value.version === 1 &&
95
- ["youtube", "bilibili", "both"].includes(value.default_platform) &&
96
- header.toString("binary") === "SQLite format 3\0"
97
- );
98
- }
99
-
100
- function validateRoot(root, packageRoot) {
101
- root = path.resolve(root);
102
- const protectedPaths = [
103
- path.parse(root).root,
104
- os.homedir(),
105
- os.tmpdir(),
106
- process.env.USERPROFILE,
107
- process.env.LOCALAPPDATA,
108
- process.env.APPDATA,
109
- process.env.WINDIR,
110
- ]
111
- .filter(Boolean)
112
- .map((item) => path.resolve(item));
113
- if (
114
- protectedPaths.some((item) => inside(root, item)) ||
115
- inside(root, path.resolve(packageRoot))
116
- )
117
- throw Error(
118
- "Reset and Clean cannot target a system directory, user home, or a directory containing the plugin.",
119
- );
120
- for (let current = root; ; current = path.dirname(current)) {
121
- if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink())
122
- throw Error(
123
- "The data directory and its parents cannot be symbolic links or junctions.",
124
- );
125
- if (path.dirname(current) === current) break;
126
- }
127
- for (const name of [".launcher", ".dashboard"]) {
128
- const control = path.join(root, name);
129
- if (fs.existsSync(control) && fs.lstatSync(control).isSymbolicLink())
130
- throw Error(
131
- "Profile control directories cannot be symbolic links or junctions.",
132
- );
133
- }
134
- if (
135
- fs.existsSync(root) &&
136
- (!fs.statSync(root).isDirectory() || !recognized(root))
137
- )
138
- throw Error(
139
- "The target is not a recognized Broader Vids profile. No files were removed.",
140
- );
141
- return root;
142
- }
143
-
144
- function managed(name) {
145
- return (
146
- MANAGED.has(name) ||
147
- /^broader-vids\.before-[A-Za-z0-9-]+\.sqlite3(?:-wal|-shm|-journal)?$/.test(
148
- name,
149
- )
150
- );
151
- }
152
-
153
- function sizeOf(target) {
154
- let files = 0,
155
- bytes = 0;
156
- const pending = [target];
157
- while (pending.length) {
158
- const current = pending.pop();
159
- const stat = fs.lstatSync(current);
160
- if (stat.isSymbolicLink() || stat.isFile()) {
161
- files += 1;
162
- bytes += stat.size;
163
- } else if (stat.isDirectory()) {
164
- for (const name of fs.readdirSync(current))
165
- pending.push(path.join(current, name));
166
- } else
167
- throw Error(
168
- "A managed directory contains an unsupported special file. No files were removed.",
169
- );
170
- }
171
- return { files, bytes };
172
- }
173
-
174
- function preview(root, operation, packageRoot) {
175
- if (!["reset", "clean"].includes(operation))
176
- throw Error("Use reset or clean.");
177
- root = validateRoot(root, packageRoot);
178
- const result = {
179
- operation,
180
- data_directory: root,
181
- status: "preview",
182
- dry_run: true,
183
- files: 0,
184
- bytes: 0,
185
- remove: [],
186
- preserve: [],
187
- unknown: [],
188
- blockers: [],
189
- };
190
- if (!fs.existsSync(root)) return { ...result, status: "absent" };
191
- for (const name of fs.readdirSync(root)) {
192
- if (/^[a-f0-9]{32}\.lock$/.test(name)) {
193
- result.blockers.push(`Active or uncertain engine lock: ${name}`);
194
- continue;
195
- }
196
- if (!managed(name)) {
197
- result.unknown.push(name);
198
- continue;
199
- }
200
- if (operation === "reset" && KEEP_RESET.has(name)) {
201
- result.preserve.push(name);
202
- continue;
203
- }
204
- result.remove.push(name);
205
- const size = sizeOf(path.join(root, name));
206
- result.files += size.files;
207
- result.bytes += size.bytes;
208
- }
209
- const controls = path.join(root, ".launcher");
210
- if (fs.existsSync(controls)) {
211
- for (const name of fs.readdirSync(controls)) {
212
- if (
213
- name === "setup.json" ||
214
- name === "gate.lock" ||
215
- name.endsWith(".use.json")
216
- )
217
- result.blockers.push(`Active or uncertain launcher operation: ${name}`);
218
- }
219
- }
220
- if (fs.existsSync(path.join(root, "access", "bilibili", "browser.lock")))
221
- result.blockers.push("Finish the active Bilibili browser operation.");
222
- if (operation === "clean" && result.unknown.length)
223
- result.blockers.push(
224
- "Move unknown files out of this directory before Clean.",
225
- );
226
- return result;
227
- }
228
-
229
- function removeOwned(root, name) {
230
- const target = path.join(root, name);
231
- if (path.dirname(target) !== root || !managed(name))
232
- throw Error("A removal target is outside the managed profile scope.");
233
- fs.rmSync(target, {
234
- recursive: true,
235
- force: false,
236
- maxRetries: 3,
237
- retryDelay: 100,
238
- });
239
- }
240
-
241
- function resetFiles(root, names) {
242
- const pending = path.join(root, ".reset-pending");
243
- if (fs.existsSync(pending)) removeOwned(root, ".reset-pending");
244
- const selected = names.filter((name) => name !== ".reset-pending");
245
- if (!selected.length) return;
246
- fs.mkdirSync(pending);
247
- const moved = [];
248
- try {
249
- selected.sort(
250
- (a, b) =>
251
- Number(!a.startsWith("broader-vids.sqlite3")) -
252
- Number(!b.startsWith("broader-vids.sqlite3")),
253
- );
254
- for (const name of selected) {
255
- fs.renameSync(path.join(root, name), path.join(pending, name));
256
- moved.push(name);
257
- }
258
- } catch (error) {
259
- for (const name of moved.reverse()) {
260
- try {
261
- fs.renameSync(path.join(pending, name), path.join(root, name));
262
- } catch {
263
- /* Keep staged files for explicit recovery. */
264
- }
265
- }
266
- if (fs.readdirSync(pending).length === 0) fs.rmdirSync(pending);
267
- throw error;
268
- }
269
- removeOwned(root, ".reset-pending");
270
- }
271
-
272
- function apply(root, operation, packageRoot, confirmation) {
273
- if (
274
- !confirmation ||
275
- !path.isAbsolute(confirmation) ||
276
- path.relative(path.resolve(root), path.resolve(confirmation)) !== ""
277
- )
278
- throw Error(
279
- "Supply --confirm-path with the exact absolute data directory shown by the preview.",
280
- );
281
- const initial = preview(root, operation, packageRoot);
282
- if (initial.status === "absent")
283
- return { ...initial, status: "absent", dry_run: false };
284
- if (initial.blockers.length) throw Error(initial.blockers.join(" "));
285
- root = initial.data_directory;
286
- withSetup(root, () => {
287
- validateRoot(root, packageRoot);
288
- ensureMarker(root);
289
- const names = fs.readdirSync(root);
290
- if (operation === "clean" && names.some((name) => !managed(name)))
291
- throw Error("The directory changed. Preview Clean again.");
292
- if (operation === "reset") resetFiles(root, initial.remove);
293
- else {
294
- for (const name of initial.remove.filter(
295
- (name) => name !== ".launcher" && name !== MARKER,
296
- ))
297
- removeOwned(root, name);
298
- if (fs.existsSync(path.join(root, MARKER))) removeOwned(root, MARKER);
299
- }
300
- });
301
- if (operation === "clean") {
302
- try {
303
- fs.rmdirSync(path.join(root, ".launcher"));
304
- fs.rmdirSync(root);
305
- } catch (error) {
306
- return {
307
- ...initial,
308
- status: "partial",
309
- dry_run: false,
310
- error:
311
- "Managed data was removed, but the directory is not empty or is in use. Preview again before another action.",
312
- code: error.code,
313
- };
314
- }
315
- }
316
- return {
317
- ...initial,
318
- status: "completed",
319
- dry_run: false,
320
- next_action:
321
- operation === "reset"
322
- ? "Run the setup skill to choose interests or import preferences. Installed tools and Bilibili sign-in remain."
323
- : "The plugin remains installed. Run the setup skill before using it again.",
324
- };
325
- }
326
-
327
- module.exports = { preview, apply, ensureMarker, MARKER };
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+ const { randomUUID } = require("node:crypto");
7
+ const { withSetup } = require("./runtime-lock.cjs");
8
+
9
+ const MARKER = ".broader-vids-profile.json";
10
+ const FORMAT = "broader-vids.profile";
11
+ const KEEP_RESET = new Set([
12
+ MARKER,
13
+ ".launcher",
14
+ "runtimes",
15
+ "wheels",
16
+ "models",
17
+ "access",
18
+ "launcher.json",
19
+ ]);
20
+ const MANAGED = new Set([
21
+ ...KEEP_RESET,
22
+ ".dashboard",
23
+ ".reset-pending",
24
+ "onboarding.json",
25
+ "backups",
26
+ "evidence",
27
+ "recommendations",
28
+ "discovery",
29
+ "feedback",
30
+ "runs",
31
+ "archives",
32
+ "broader-vids.sqlite3",
33
+ "broader-vids.sqlite3-wal",
34
+ "broader-vids.sqlite3-shm",
35
+ "broader-vids.sqlite3-journal",
36
+ ]);
37
+
38
+ function inside(parent, child) {
39
+ const relative = path.relative(parent, child);
40
+ return (
41
+ relative === "" ||
42
+ (!relative.startsWith(`..${path.sep}`) &&
43
+ relative !== ".." &&
44
+ !path.isAbsolute(relative))
45
+ );
46
+ }
47
+
48
+ function ensureMarker(root) {
49
+ const file = path.join(root, MARKER);
50
+ if (fs.existsSync(file)) return;
51
+ const temporary = path.join(root, `.${randomUUID()}.tmp`);
52
+ try {
53
+ fs.writeFileSync(
54
+ temporary,
55
+ JSON.stringify({ format: FORMAT, version: 1 }),
56
+ { flag: "wx", mode: 0o600 },
57
+ );
58
+ fs.renameSync(temporary, file);
59
+ } finally {
60
+ if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
61
+ }
62
+ }
63
+
64
+ function readSmall(file) {
65
+ if (fs.lstatSync(file).isSymbolicLink() || fs.statSync(file).size > 4096)
66
+ throw Error("Profile identity metadata is invalid.");
67
+ return JSON.parse(fs.readFileSync(file, "utf8"));
68
+ }
69
+
70
+ function recognized(root) {
71
+ const marker = path.join(root, MARKER);
72
+ if (fs.existsSync(marker)) {
73
+ const value = readSmall(marker);
74
+ return value && value.format === FORMAT && value.version === 1;
75
+ }
76
+ const config = path.join(root, "onboarding.json");
77
+ const database = path.join(root, "broader-vids.sqlite3");
78
+ if (
79
+ !fs.existsSync(config) ||
80
+ !fs.existsSync(database) ||
81
+ fs.lstatSync(database).isSymbolicLink()
82
+ )
83
+ return false;
84
+ const value = readSmall(config);
85
+ const header = Buffer.alloc(16);
86
+ const descriptor = fs.openSync(database, "r");
87
+ try {
88
+ fs.readSync(descriptor, header, 0, header.length, 0);
89
+ } finally {
90
+ fs.closeSync(descriptor);
91
+ }
92
+ return (
93
+ value &&
94
+ value.version === 1 &&
95
+ ["youtube", "bilibili", "both"].includes(value.default_platform) &&
96
+ header.toString("binary") === "SQLite format 3\0"
97
+ );
98
+ }
99
+
100
+ function validateRoot(root, packageRoot) {
101
+ root = path.resolve(root);
102
+ const protectedPaths = [
103
+ path.parse(root).root,
104
+ os.homedir(),
105
+ os.tmpdir(),
106
+ process.env.USERPROFILE,
107
+ process.env.LOCALAPPDATA,
108
+ process.env.APPDATA,
109
+ process.env.WINDIR,
110
+ ]
111
+ .filter(Boolean)
112
+ .map((item) => path.resolve(item));
113
+ if (
114
+ protectedPaths.some((item) => inside(root, item)) ||
115
+ inside(root, path.resolve(packageRoot))
116
+ )
117
+ throw Error(
118
+ "Reset and Clean cannot target a system directory, user home, or a directory containing the plugin.",
119
+ );
120
+ for (let current = root; ; current = path.dirname(current)) {
121
+ if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink())
122
+ throw Error(
123
+ "The data directory and its parents cannot be symbolic links or junctions.",
124
+ );
125
+ if (path.dirname(current) === current) break;
126
+ }
127
+ for (const name of [".launcher", ".dashboard"]) {
128
+ const control = path.join(root, name);
129
+ if (fs.existsSync(control) && fs.lstatSync(control).isSymbolicLink())
130
+ throw Error(
131
+ "Profile control directories cannot be symbolic links or junctions.",
132
+ );
133
+ }
134
+ if (
135
+ fs.existsSync(root) &&
136
+ (!fs.statSync(root).isDirectory() || !recognized(root))
137
+ )
138
+ throw Error(
139
+ "The target is not a recognized Broader Vids profile. No files were removed.",
140
+ );
141
+ return root;
142
+ }
143
+
144
+ function managed(name) {
145
+ return (
146
+ MANAGED.has(name) ||
147
+ /^broader-vids\.before-[A-Za-z0-9-]+\.sqlite3(?:-wal|-shm|-journal)?$/.test(
148
+ name,
149
+ )
150
+ );
151
+ }
152
+
153
+ function sizeOf(target) {
154
+ let files = 0,
155
+ bytes = 0;
156
+ const pending = [target];
157
+ while (pending.length) {
158
+ const current = pending.pop();
159
+ const stat = fs.lstatSync(current);
160
+ if (stat.isSymbolicLink() || stat.isFile()) {
161
+ files += 1;
162
+ bytes += stat.size;
163
+ } else if (stat.isDirectory()) {
164
+ for (const name of fs.readdirSync(current))
165
+ pending.push(path.join(current, name));
166
+ } else
167
+ throw Error(
168
+ "A managed directory contains an unsupported special file. No files were removed.",
169
+ );
170
+ }
171
+ return { files, bytes };
172
+ }
173
+
174
+ function preview(root, operation, packageRoot) {
175
+ if (!["reset", "clean"].includes(operation))
176
+ throw Error("Use reset or clean.");
177
+ root = validateRoot(root, packageRoot);
178
+ const result = {
179
+ operation,
180
+ data_directory: root,
181
+ status: "preview",
182
+ dry_run: true,
183
+ files: 0,
184
+ bytes: 0,
185
+ remove: [],
186
+ preserve: [],
187
+ unknown: [],
188
+ blockers: [],
189
+ };
190
+ if (!fs.existsSync(root)) return { ...result, status: "absent" };
191
+ for (const name of fs.readdirSync(root)) {
192
+ if (/^[a-f0-9]{32}\.lock$/.test(name)) {
193
+ result.blockers.push(`Active or uncertain engine lock: ${name}`);
194
+ continue;
195
+ }
196
+ if (!managed(name)) {
197
+ result.unknown.push(name);
198
+ continue;
199
+ }
200
+ if (operation === "reset" && KEEP_RESET.has(name)) {
201
+ result.preserve.push(name);
202
+ continue;
203
+ }
204
+ result.remove.push(name);
205
+ const size = sizeOf(path.join(root, name));
206
+ result.files += size.files;
207
+ result.bytes += size.bytes;
208
+ }
209
+ const controls = path.join(root, ".launcher");
210
+ if (fs.existsSync(controls)) {
211
+ for (const name of fs.readdirSync(controls)) {
212
+ if (
213
+ name === "setup.json" ||
214
+ name === "gate.lock" ||
215
+ name.endsWith(".use.json")
216
+ )
217
+ result.blockers.push(`Active or uncertain launcher operation: ${name}`);
218
+ }
219
+ }
220
+ if (fs.existsSync(path.join(root, "access", "bilibili", "browser.lock")))
221
+ result.blockers.push("Finish the active Bilibili browser operation.");
222
+ if (operation === "clean" && result.unknown.length)
223
+ result.blockers.push(
224
+ "Move unknown files out of this directory before Clean.",
225
+ );
226
+ return result;
227
+ }
228
+
229
+ function removeOwned(root, name) {
230
+ const target = path.join(root, name);
231
+ if (path.dirname(target) !== root || !managed(name))
232
+ throw Error("A removal target is outside the managed profile scope.");
233
+ fs.rmSync(target, {
234
+ recursive: true,
235
+ force: false,
236
+ maxRetries: 3,
237
+ retryDelay: 100,
238
+ });
239
+ }
240
+
241
+ function resetFiles(root, names) {
242
+ const pending = path.join(root, ".reset-pending");
243
+ if (fs.existsSync(pending)) removeOwned(root, ".reset-pending");
244
+ const selected = names.filter((name) => name !== ".reset-pending");
245
+ if (!selected.length) return;
246
+ fs.mkdirSync(pending);
247
+ const moved = [];
248
+ try {
249
+ selected.sort(
250
+ (a, b) =>
251
+ Number(!a.startsWith("broader-vids.sqlite3")) -
252
+ Number(!b.startsWith("broader-vids.sqlite3")),
253
+ );
254
+ for (const name of selected) {
255
+ fs.renameSync(path.join(root, name), path.join(pending, name));
256
+ moved.push(name);
257
+ }
258
+ } catch (error) {
259
+ for (const name of moved.reverse()) {
260
+ try {
261
+ fs.renameSync(path.join(pending, name), path.join(root, name));
262
+ } catch {
263
+ /* Keep staged files for explicit recovery. */
264
+ }
265
+ }
266
+ if (fs.readdirSync(pending).length === 0) fs.rmdirSync(pending);
267
+ throw error;
268
+ }
269
+ removeOwned(root, ".reset-pending");
270
+ }
271
+
272
+ function apply(root, operation, packageRoot, confirmation) {
273
+ if (
274
+ !confirmation ||
275
+ !path.isAbsolute(confirmation) ||
276
+ path.relative(path.resolve(root), path.resolve(confirmation)) !== ""
277
+ )
278
+ throw Error(
279
+ "Supply --confirm-path with the exact absolute data directory shown by the preview.",
280
+ );
281
+ const initial = preview(root, operation, packageRoot);
282
+ if (initial.status === "absent")
283
+ return { ...initial, status: "absent", dry_run: false };
284
+ if (initial.blockers.length) throw Error(initial.blockers.join(" "));
285
+ root = initial.data_directory;
286
+ withSetup(root, () => {
287
+ validateRoot(root, packageRoot);
288
+ ensureMarker(root);
289
+ const names = fs.readdirSync(root);
290
+ if (operation === "clean" && names.some((name) => !managed(name)))
291
+ throw Error("The directory changed. Preview Clean again.");
292
+ if (operation === "reset") resetFiles(root, initial.remove);
293
+ else {
294
+ for (const name of initial.remove.filter(
295
+ (name) => name !== ".launcher" && name !== MARKER,
296
+ ))
297
+ removeOwned(root, name);
298
+ if (fs.existsSync(path.join(root, MARKER))) removeOwned(root, MARKER);
299
+ }
300
+ });
301
+ if (operation === "clean") {
302
+ try {
303
+ fs.rmdirSync(path.join(root, ".launcher"));
304
+ fs.rmdirSync(root);
305
+ } catch (error) {
306
+ return {
307
+ ...initial,
308
+ status: "partial",
309
+ dry_run: false,
310
+ error:
311
+ "Managed data was removed, but the directory is not empty or is in use. Preview again before another action.",
312
+ code: error.code,
313
+ };
314
+ }
315
+ }
316
+ return {
317
+ ...initial,
318
+ status: "completed",
319
+ dry_run: false,
320
+ next_action:
321
+ operation === "reset"
322
+ ? "Run the setup skill to choose interests or import preferences. Installed tools and Bilibili sign-in remain."
323
+ : "The plugin remains installed. Run the setup skill before using it again.",
324
+ };
325
+ }
326
+
327
+ module.exports = { preview, apply, ensureMarker, MARKER };