memindex 1.1.1 → 1.3.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.
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+ const { lstatSync, realpathSync, rmSync } = require('fs');
3
+ const { rm } = require('fs/promises');
4
+ const os = require('os');
5
+ const path = require('path');
6
+
7
+ class RefusedRemoveError extends Error {}
8
+
9
+ function physical(target) {
10
+ const absolute = path.resolve(target);
11
+ try {
12
+ if (lstatSync(absolute).isSymbolicLink()) return path.join(physical(path.dirname(absolute)), path.basename(absolute));
13
+ return realpathSync(absolute);
14
+ } catch {
15
+ const parent = path.dirname(absolute);
16
+ return parent === absolute ? absolute : path.join(physical(parent), path.basename(absolute));
17
+ }
18
+ }
19
+
20
+ function physicalFolder(folder) {
21
+ try { return realpathSync(path.resolve(folder)); } catch { return physical(folder); }
22
+ }
23
+
24
+ const DATA_DIR_KEYS = ['MEMINDEX_DATA_DIR', 'AGENT_MEM_DATA_DIR'];
25
+
26
+ function settingsDataDirs(home) {
27
+ const found = [];
28
+ for (const folder of ['.memindex', '.agent-mem']) {
29
+ try {
30
+ const raw = JSON.parse(require('fs').readFileSync(path.join(home, folder, 'settings.json'), 'utf8').replace(/^\uFEFF/, ''));
31
+ for (const doc of [raw, raw && raw.env]) {
32
+ if (!doc || typeof doc !== 'object') continue;
33
+ for (const key of DATA_DIR_KEYS) if (typeof doc[key] === 'string' && doc[key]) found.push(doc[key]);
34
+ }
35
+ } catch { }
36
+ }
37
+ return found;
38
+ }
39
+
40
+ const expandHome = (value, home) => (value === '~' ? home : value.startsWith('~/') ? path.join(home, value.slice(2)) : value);
41
+
42
+ function protectedPaths(extra) {
43
+ const home = os.homedir();
44
+ const list = [path.sep, home, path.join(home, '.memindex'), path.join(home, '.agent-mem'), ...extra];
45
+ for (const name of DATA_DIR_KEYS) {
46
+ if (process.env[name]) list.push(process.env[name]);
47
+ }
48
+ list.push(...settingsDataDirs(home).map(value => expandHome(value, home)));
49
+ return [...new Set(list.flatMap(entry => [path.resolve(entry), physical(entry), physicalFolder(entry)]))];
50
+ }
51
+
52
+ const inside = (candidate, root) => candidate.startsWith(root.endsWith(path.sep) ? root : root + path.sep);
53
+
54
+ function assertRemovable(target, within, extraProtected = []) {
55
+ const spelled = path.resolve(target);
56
+ const real = physical(target);
57
+ if (!inside(spelled, path.resolve(within)) || !inside(real, physicalFolder(within))) {
58
+ throw new RefusedRemoveError(`Refused to remove ${spelled}: it is not inside ${path.resolve(within)}.`);
59
+ }
60
+ for (const guarded of protectedPaths(extraProtected)) {
61
+ for (const candidate of [spelled, real]) {
62
+ if (candidate === guarded || inside(guarded, candidate)) {
63
+ throw new RefusedRemoveError(`Refused to remove ${spelled}: it is or holds ${guarded}.`);
64
+ }
65
+ }
66
+ }
67
+ return spelled;
68
+ }
69
+
70
+ function removeTree(target, within) {
71
+ rmSync(assertRemovable(target, within), { recursive: true, force: true });
72
+ }
73
+
74
+ async function removeTreeAsync(target, within) {
75
+ await rm(assertRemovable(target, within), { recursive: true, force: true });
76
+ }
77
+
78
+ module.exports = { RefusedRemoveError, assertRemovable, removeTree, removeTreeAsync, settingsDataDirs };