@clawchatsai/connector 0.2.6 → 0.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.
- package/dist/index.js +2 -19
- package/dist/shim.d.ts +7 -0
- package/dist/shim.js +27 -0
- package/package.json +1 -1
- package/server/approval-client.js +9 -1
- package/server/approval-client.test.js +16 -0
- package/server/gateway.js +17 -2
- package/server/index.js +38 -2
- package/server/peer/contact-copy.js +164 -0
- package/server/peer/contact-policy.js +250 -0
- package/server/peer/contact-policy.test.js +119 -0
- package/server/peer/contacts.js +175 -0
- package/server/peer/contacts.test.js +135 -0
- package/server/peer/link.js +7 -3
- package/server/peer/notes-sync.js +126 -0
- package/server/peer/sharing.js +522 -61
- package/server/peer/sharing.test.js +302 -41
- package/server/peer/version.js +36 -0
- package/server/store/team-store.js +6 -5
- package/server/team.js +55 -13
- package/server/team.test.js +46 -12
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { Database } from '../bootstrap/native.js';
|
|
7
|
+
import { ContactBook } from './contacts.js';
|
|
8
|
+
import { noteCandidates, checkRepoDoc, resolvePicks, syncNotes, removeNotes, picksSignature } from './notes-sync.js';
|
|
9
|
+
import { olderThan, peerOutdated, outdatedMessage, MIN_PEER_VERSION } from './version.js';
|
|
10
|
+
|
|
11
|
+
const book = () => { const db = new Database(':memory:'); return new ContactBook(() => db); };
|
|
12
|
+
|
|
13
|
+
test('a contact is per person (email, any case); settings save one field at a time, validated', () => {
|
|
14
|
+
const b = book();
|
|
15
|
+
b.create('Kamil@X.dev', { name: 'Kamil' });
|
|
16
|
+
assert.equal(b.get('kamil@x.dev').name, 'Kamil');
|
|
17
|
+
assert.equal(b.create('kamil@x.dev', { access: 'full' }).access, 'trusted', 'create never overwrites');
|
|
18
|
+
assert.throws(() => b.set('kamil@x.dev', 'access', 'root'), /Unknown access/);
|
|
19
|
+
assert.throws(() => b.set('kamil@x.dev', 'autonomy', 'wild'), /Unknown autonomy/);
|
|
20
|
+
assert.throws(() => b.set('kamil@x.dev', 'nope', 1), /Unknown setting/);
|
|
21
|
+
assert.equal(b.set('kamil@x.dev', 'private', 'health').private, 'health');
|
|
22
|
+
assert.deepEqual(b.set('kamil@x.dev', 'mcp', { gmail: 1, x: 0 }).mcp, { gmail: true, x: false });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('Access resets Tools and Commands to that tier; Full needs confirming once', () => {
|
|
26
|
+
const b = book();
|
|
27
|
+
b.create('k@x.dev');
|
|
28
|
+
b.set('k@x.dev', 'tools', ['read']);
|
|
29
|
+
assert.equal(b.set('k@x.dev', 'access', 'restricted').commands, 'basic');
|
|
30
|
+
assert.throws(() => b.set('k@x.dev', 'access', 'full'), e => e.code === 'confirm_full');
|
|
31
|
+
const full = b.set('k@x.dev', 'access', 'full', { confirmFull: true });
|
|
32
|
+
assert.ok(full.fullConfirmedAt);
|
|
33
|
+
assert.deepEqual(full.tools.sort(), ['memory_get', 'memory_search', 'pdf', 'view_image', 'web_fetch', 'web_search']);
|
|
34
|
+
b.set('k@x.dev', 'access', 'trusted');
|
|
35
|
+
assert.equal(b.set('k@x.dev', 'access', 'full').access, 'full', 'confirmed once');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('"Open one folder" is Full agent only and needs ask/auto-review commands; Basic list entries are checked', () => {
|
|
39
|
+
const b = book();
|
|
40
|
+
b.create('k@x.dev');
|
|
41
|
+
assert.throws(() => b.set('k@x.dev', 'openFolder', '/w/plans'), /Full agent/);
|
|
42
|
+
b.set('k@x.dev', 'access', 'full', { confirmFull: true });
|
|
43
|
+
b.set('k@x.dev', 'commands', 'basic');
|
|
44
|
+
assert.throws(() => b.set('k@x.dev', 'openFolder', '/w/plans'), /ask me/);
|
|
45
|
+
b.set('k@x.dev', 'commands', 'ask');
|
|
46
|
+
assert.equal(b.set('k@x.dev', 'openFolder', '/w/plans').openFolder, '/w/plans');
|
|
47
|
+
assert.throws(() => b.set('k@x.dev', 'commands', 'basic'), /ask me/);
|
|
48
|
+
assert.throws(() => b.set('k@x.dev', 'basicList', [{ pattern: 'env' }]), e => e.code === 'bad_entry');
|
|
49
|
+
assert.equal(b.set('k@x.dev', 'basicList', [{ pattern: 'hostname' }]).basicList[0].pattern, 'hostname');
|
|
50
|
+
assert.equal(b.set('k@x.dev', 'basicList', null).basicList, null, 'back to the defaults');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('one counter per person: day and hour windows, per tier; a failed reply gives its count back', () => {
|
|
54
|
+
const b = book();
|
|
55
|
+
b.create('k@x.dev');
|
|
56
|
+
const now = Date.UTC(2026, 10, 20, 10, 30);
|
|
57
|
+
b.count('k@x.dev', 49, now);
|
|
58
|
+
assert.equal(b.overCap('k@x.dev', 'bounded', now), null);
|
|
59
|
+
b.count('k@x.dev', 1, now);
|
|
60
|
+
assert.match(b.overCap('k@x.dev', 'bounded', now), /Daily limit reached \(50/);
|
|
61
|
+
assert.match(b.overCap('k@x.dev', 'extended', now), /Hourly limit reached \(12/);
|
|
62
|
+
assert.equal(b.overCap('k@x.dev', 'extended', now + 3_600_000), null, 'next hour');
|
|
63
|
+
b.count('k@x.dev', -1, now);
|
|
64
|
+
assert.equal(b.usage('k@x.dev', now).day, 49);
|
|
65
|
+
b.forget('k@x.dev');
|
|
66
|
+
assert.equal(b.get('k@x.dev'), null);
|
|
67
|
+
assert.equal(b.usage('k@x.dev', now).day, 0);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('notes: only top-level memory/projects/*.md are offered; infrastructure notes start off', () => {
|
|
71
|
+
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'ws-'));
|
|
72
|
+
fs.mkdirSync(path.join(ws, 'memory/projects/sub'), { recursive: true });
|
|
73
|
+
for (const f of ['clawchats.md', 'homelab.md', 'openclaw-gateway-ops.md', 'vault-notes.md', 'sub/deep.md']) fs.writeFileSync(path.join(ws, 'memory/projects', f), `# ${f}`);
|
|
74
|
+
fs.writeFileSync(path.join(ws, 'memory/2026-11-20.md'), 'daily');
|
|
75
|
+
fs.writeFileSync(path.join(ws, 'MEMORY.md'), 'private');
|
|
76
|
+
const c = noteCandidates([ws]);
|
|
77
|
+
assert.deepEqual(c.map(x => [x.name, x.defaultOn]), [['clawchats.md', true], ['homelab.md', false], ['openclaw-gateway-ops.md', false], ['vault-notes.md', false]]);
|
|
78
|
+
// Picks that aren't candidates or allowed repo docs are dropped (MEMORY.md, a daily note).
|
|
79
|
+
assert.deepEqual(resolvePicks([path.join(ws, 'MEMORY.md'), path.join(ws, 'memory/2026-11-20.md'), c[0].path], c).map(f => f.dest), ['clawchats.md']);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('notes sync: read-only copies, secrets removed, no symlinks followed, re-synced when a note changes, removed on None', () => {
|
|
83
|
+
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'ws-'));
|
|
84
|
+
const copyWs = fs.mkdtempSync(path.join(os.tmpdir(), 'copy-'));
|
|
85
|
+
fs.mkdirSync(path.join(ws, 'memory/projects'), { recursive: true });
|
|
86
|
+
const note = path.join(ws, 'memory/projects/clawchats.md');
|
|
87
|
+
fs.writeFileSync(note, 'Deploy key: ANTHROPIC_API_KEY=sk-ant-api03-AbCdEfGhIjKlMnOpQrStUvWx\nStack: node');
|
|
88
|
+
const files = resolvePicks([note], noteCandidates([ws]));
|
|
89
|
+
const r = syncNotes(copyWs, files);
|
|
90
|
+
assert.deepEqual([r.files, r.secretsRemoved], [1, 1]);
|
|
91
|
+
const out = path.join(copyWs, 'notes/clawchats.md');
|
|
92
|
+
assert.doesNotMatch(fs.readFileSync(out, 'utf8'), /sk-ant/);
|
|
93
|
+
assert.equal(fs.statSync(out).mode & 0o777, 0o444);
|
|
94
|
+
const sig = picksSignature(files);
|
|
95
|
+
fs.writeFileSync(note, 'Stack: node 24', { flag: 'w' });
|
|
96
|
+
fs.utimesSync(note, new Date(), new Date(Date.now() + 5000));
|
|
97
|
+
assert.notEqual(picksSignature(files), sig, 'an edit shows as a new signature');
|
|
98
|
+
syncNotes(copyWs, files);
|
|
99
|
+
assert.equal(fs.readFileSync(out, 'utf8'), 'Stack: node 24');
|
|
100
|
+
// A source that turned into a link is skipped.
|
|
101
|
+
fs.rmSync(note); fs.symlinkSync('/etc/hostname', note);
|
|
102
|
+
assert.equal(syncNotes(copyWs, files).files, 0);
|
|
103
|
+
removeNotes(copyWs);
|
|
104
|
+
assert.equal(fs.existsSync(path.join(copyWs, 'notes')), false);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('repo docs: a .md in a git repo, never under ~/.openclaw, never through a link', () => {
|
|
108
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'home-'));
|
|
109
|
+
const repo = path.join(home, 'repos/app');
|
|
110
|
+
fs.mkdirSync(path.join(repo, '.git'), { recursive: true });
|
|
111
|
+
fs.mkdirSync(path.join(repo, 'specs'));
|
|
112
|
+
fs.writeFileSync(path.join(repo, 'specs/a.md'), '# a');
|
|
113
|
+
fs.writeFileSync(path.join(repo, 'secret.json'), '{}');
|
|
114
|
+
fs.mkdirSync(path.join(home, '.openclaw/workspace/.git'), { recursive: true });
|
|
115
|
+
fs.writeFileSync(path.join(home, '.openclaw/workspace/x.md'), '# x');
|
|
116
|
+
fs.symlinkSync(path.join(repo, 'specs/a.md'), path.join(repo, 'link.md'));
|
|
117
|
+
const ok = checkRepoDoc('~/repos/app/specs/a.md', { home });
|
|
118
|
+
assert.deepEqual([ok.ok, ok.repo, ok.name], [true, repo, 'app/specs/a.md']);
|
|
119
|
+
assert.match(checkRepoDoc(path.join(repo, 'secret.json'), { home }).error, /Markdown/);
|
|
120
|
+
assert.match(checkRepoDoc(path.join(home, '.openclaw/workspace/x.md'), { home }).error, /openclaw/);
|
|
121
|
+
assert.match(checkRepoDoc(path.join(repo, 'link.md'), { home }).error, /link/);
|
|
122
|
+
assert.match(checkRepoDoc(path.join(repo, 'specs/../../app/specs/a.md'), { home }).ok ? 'ok' : 'x', /ok/, 'normalized');
|
|
123
|
+
assert.match(checkRepoDoc('specs/a.md', { home }).error, /full path/);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('peer versions: older than the minimum, or none at all, is too old; the message names the update command', () => {
|
|
127
|
+
assert.equal(olderThan('0.2.4', '0.3.0'), true);
|
|
128
|
+
assert.equal(olderThan('0.3.0', '0.3.0'), false);
|
|
129
|
+
assert.equal(olderThan('0.10.0', '0.3.0'), false);
|
|
130
|
+
assert.equal(peerOutdated(null), true);
|
|
131
|
+
assert.equal(peerOutdated('0.2.4'), true);
|
|
132
|
+
assert.equal(peerOutdated(MIN_PEER_VERSION), false);
|
|
133
|
+
assert.match(outdatedMessage('Kamil', '0.2.4'), /Kamil's ClawChats connector is out of date \(0\.2\.4\)[^]*openclaw plugins install npm:@clawchatsai\/connector --force/);
|
|
134
|
+
assert.match(outdatedMessage(null, null), /^Your ClawChats connector is out of date\./);
|
|
135
|
+
});
|
package/server/peer/link.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// signatures don't verify and the link never opens.
|
|
6
6
|
//
|
|
7
7
|
// Wire format (JSON strings):
|
|
8
|
-
// { t: 'hello', v: 1, role, shareId, nonce }
|
|
8
|
+
// { t: 'hello', v: 1, role, shareId, nonce, cv } cv: the sender's connector version (0.3.0+; version.js)
|
|
9
9
|
// { t: 'auth', sig }
|
|
10
10
|
// { t: 'req', id, method, params } → { t: 'res', id, ok, result | error }
|
|
11
11
|
// { t: 'ev', id, event, data } (progress for a request, e.g. streamed text)
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import crypto from 'node:crypto';
|
|
14
14
|
import { EventEmitter } from 'node:events';
|
|
15
15
|
import { canonicalJson, verifySignature } from './keys.js';
|
|
16
|
+
import { connectorVersion } from './version.js';
|
|
16
17
|
|
|
17
18
|
const HANDSHAKE_MS = 15_000;
|
|
18
19
|
const MAX_FRAME = 256 * 1024;
|
|
@@ -33,12 +34,13 @@ export class PeerLink extends EventEmitter {
|
|
|
33
34
|
* @param {string} o.peerKey the other side's pinned public key (base64 SPKI)
|
|
34
35
|
* @param {{ local: string, remote: string }} o.dtls DTLS fingerprints as this side sees them
|
|
35
36
|
*/
|
|
36
|
-
constructor({ dc, role, shareId, key, peerKey, dtls = { local: '', remote: '' }, handshakeMs = HANDSHAKE_MS, log = console }) {
|
|
37
|
+
constructor({ dc, role, shareId, key, peerKey, dtls = { local: '', remote: '' }, handshakeMs = HANDSHAKE_MS, log = console, version = connectorVersion() }) {
|
|
37
38
|
super();
|
|
38
39
|
Object.assign(this, { dc, role, shareId, key, peerKey, dtls, log });
|
|
39
40
|
this.peerRole = role === 'owner' ? 'requester' : 'owner';
|
|
40
41
|
this.nonce = crypto.randomBytes(24).toString('base64');
|
|
41
42
|
this.peerNonce = null;
|
|
43
|
+
this.peerVersion = null; // the other side's connector version, from its hello (null: too old to send one)
|
|
42
44
|
this.sentAuth = false;
|
|
43
45
|
this.verified = false;
|
|
44
46
|
this.ready = false;
|
|
@@ -53,7 +55,7 @@ export class PeerLink extends EventEmitter {
|
|
|
53
55
|
try { this._onFrame(data); } catch (e) { this.log.warn?.(`[peer] ${this.shareId}: frame handling failed: ${e?.message}`); this._fail('bad frame'); }
|
|
54
56
|
});
|
|
55
57
|
dc.onClosed(() => this._onClosed());
|
|
56
|
-
this._send({ t: 'hello', v: 1, role, shareId, nonce: this.nonce });
|
|
58
|
+
this._send({ t: 'hello', v: 1, role, shareId, nonce: this.nonce, cv: version });
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
/** What `signer` signs: both nonces and both fingerprints, from the signer's point of view. */
|
|
@@ -82,6 +84,8 @@ export class PeerLink extends EventEmitter {
|
|
|
82
84
|
if (m.t === 'hello') {
|
|
83
85
|
if (m.v !== 1 || m.role !== this.peerRole || m.shareId !== this.shareId || typeof m.nonce !== 'string' || this.peerNonce) return this._fail('bad hello');
|
|
84
86
|
this.peerNonce = m.nonce;
|
|
87
|
+
// Rides the DTLS channel the auth below binds, so a relay can't change it.
|
|
88
|
+
if (typeof m.cv === 'string') this.peerVersion = m.cv.slice(0, 40);
|
|
85
89
|
this.sentAuth = true;
|
|
86
90
|
this._send({ t: 'auth', sig: this.key.signJson(JSON.parse(this._statement(this.role, this.nonce, this.peerNonce, this.dtls.local, this.dtls.remote))) });
|
|
87
91
|
} else if (m.t === 'auth') {
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Knowledge "My notes" (contact card): read-only copies of the notes the owner picked, in a `notes/` folder of each
|
|
2
|
+
// agent copy made for that person. The copy reads them with its own file tools (fs.workspaceOnly keeps it in its
|
|
3
|
+
// folder), so the owner's real memory is never reachable. Copies go through redactSecrets, are written 0444 (the
|
|
4
|
+
// folders stay 0755: a read-only folder would stop the owner's own tools from deleting the copy), and symlinks are
|
|
5
|
+
// never followed. Re-synced when the picks change, on "Sync now", and before a turn when a
|
|
6
|
+
// source file changed.
|
|
7
|
+
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import { redactSecrets } from '../util/redact.js';
|
|
13
|
+
|
|
14
|
+
export const NOTES_DIR = 'notes';
|
|
15
|
+
const MAX_FILE = 256 * 1024;
|
|
16
|
+
// Off by default (D10): infrastructure notes, and anything named like it holds credentials.
|
|
17
|
+
const DEFAULT_OFF = /^(homelab|openclaw-gateway-ops)\.md$|secret|credential|password|token|vault/i;
|
|
18
|
+
|
|
19
|
+
/** Notes the card offers from an agent workspace: top-level `memory/projects/*.md` only (never daily notes, MEMORY.md, USER.md, reference/, imports/). */
|
|
20
|
+
export function noteCandidates(workspaces) {
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const ws of new Set((workspaces || []).filter(w => typeof w === 'string' && path.isAbsolute(w)))) {
|
|
23
|
+
const dir = path.join(ws, 'memory', 'projects');
|
|
24
|
+
let names = [];
|
|
25
|
+
try { names = fs.readdirSync(dir, { withFileTypes: true }).filter(d => d.isFile() && d.name.endsWith('.md')).map(d => d.name).sort(); } catch { continue; }
|
|
26
|
+
for (const name of names) {
|
|
27
|
+
const p = path.join(dir, name);
|
|
28
|
+
const st = safeStat(p);
|
|
29
|
+
if (!st) continue;
|
|
30
|
+
out.push({ path: p, name, group: 'projects', root: dir, size: st.size, mtimeMs: st.mtimeMs, defaultOn: !DEFAULT_OFF.test(name) });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The git repo a path is in (the folder holding `.git`), or null. */
|
|
37
|
+
export function repoOf(p) {
|
|
38
|
+
let dir = path.dirname(p);
|
|
39
|
+
for (let i = 0; i < 30 && dir !== path.dirname(dir); i++, dir = path.dirname(dir)) {
|
|
40
|
+
if (fs.existsSync(path.join(dir, '.git'))) return dir;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether a file may be offered as a repo doc: a Markdown file (no symlink anywhere on its path), inside a git repo, not
|
|
47
|
+
* under ~/.openclaw, at most 256 KB. Returns { ok, path, repo, error }.
|
|
48
|
+
*/
|
|
49
|
+
export function checkRepoDoc(input, { home = os.homedir() } = {}) {
|
|
50
|
+
const raw = String(input || '').trim().replace(/^~(?=\/)/, home);
|
|
51
|
+
if (!path.isAbsolute(raw)) return { ok: false, error: 'Use the full path of a .md file in one of your git repos' };
|
|
52
|
+
const p = path.normalize(raw);
|
|
53
|
+
let real;
|
|
54
|
+
try { real = fs.realpathSync(p); } catch { return { ok: false, error: "That file doesn't exist" }; }
|
|
55
|
+
if (real !== p) return { ok: false, error: "That path goes through a link; use the file's real path" };
|
|
56
|
+
if (!/\.md$/i.test(p)) return { ok: false, error: 'Only Markdown (.md) files' };
|
|
57
|
+
if (p.split(path.sep).some(seg => seg === '.openclaw') || p.startsWith(path.join(home, '.openclaw'))) return { ok: false, error: 'Nothing under ~/.openclaw' };
|
|
58
|
+
const st = safeStat(p);
|
|
59
|
+
if (!st?.isFile()) return { ok: false, error: 'Not a file' };
|
|
60
|
+
if (st.size > MAX_FILE) return { ok: false, error: 'Too big (256 KB at most)' };
|
|
61
|
+
const repo = repoOf(p);
|
|
62
|
+
if (!repo) return { ok: false, error: 'Only files inside a git repo' };
|
|
63
|
+
return { ok: true, path: p, repo, size: st.size, mtimeMs: st.mtimeMs, name: path.relative(path.dirname(repo), p) };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The picked files that exist and are allowed: [{ src, dest (relative to notes/) }]. */
|
|
67
|
+
export function resolvePicks(picks, candidates) {
|
|
68
|
+
const byPath = new Map(candidates.map(c => [c.path, c]));
|
|
69
|
+
const out = [];
|
|
70
|
+
for (const p of picks || []) {
|
|
71
|
+
const cand = byPath.get(p);
|
|
72
|
+
if (cand) { out.push({ src: p, dest: cand.name }); continue; }
|
|
73
|
+
const doc = checkRepoDoc(p);
|
|
74
|
+
if (doc.ok) out.push({ src: doc.path, dest: path.join('repos', doc.name) });
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What the picked files look like now (to tell whether a re-sync is due). */
|
|
80
|
+
export function picksSignature(files) {
|
|
81
|
+
const h = crypto.createHash('sha256');
|
|
82
|
+
for (const f of files) { const st = safeStat(f.src); h.update(`${f.src}|${f.dest}|${st?.size ?? -1}|${st?.mtimeMs ?? -1}\n`); }
|
|
83
|
+
return h.digest('hex').slice(0, 16);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Write the picked notes into `<workspace>/notes/`, replacing what was there. Returns { files, bytes, secretsRemoved,
|
|
88
|
+
* at, signature }. Never follows symlinks (a source that became one is skipped).
|
|
89
|
+
*/
|
|
90
|
+
export function syncNotes(workspace, files) {
|
|
91
|
+
const root = path.join(workspace, NOTES_DIR);
|
|
92
|
+
if (fs.existsSync(root) && fs.lstatSync(root).isSymbolicLink()) throw new Error('notes folder is a link: refusing');
|
|
93
|
+
makeWritable(root);
|
|
94
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
95
|
+
fs.mkdirSync(root, { recursive: true });
|
|
96
|
+
let bytes = 0, secretsRemoved = 0, count = 0;
|
|
97
|
+
for (const f of files) {
|
|
98
|
+
const st = fs.lstatSync(f.src, { throwIfNoEntry: false });
|
|
99
|
+
if (!st || !st.isFile() || st.isSymbolicLink() || st.size > MAX_FILE) continue;
|
|
100
|
+
const text = redactSecrets(fs.readFileSync(f.src, 'utf8'), () => { secretsRemoved++; });
|
|
101
|
+
const dest = path.join(root, f.dest);
|
|
102
|
+
if (!dest.startsWith(root + path.sep)) continue;
|
|
103
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
104
|
+
fs.writeFileSync(dest, text, { mode: 0o444 });
|
|
105
|
+
fs.chmodSync(dest, 0o444);
|
|
106
|
+
bytes += Buffer.byteLength(text);
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
return { files: count, bytes, secretsRemoved, at: Date.now(), signature: picksSignature(files) };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Remove a copy's notes (its folders are read-only). */
|
|
113
|
+
export function removeNotes(workspace) {
|
|
114
|
+
const root = path.join(workspace, NOTES_DIR);
|
|
115
|
+
if (!fs.existsSync(root)) return;
|
|
116
|
+
makeWritable(root);
|
|
117
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function makeWritable(dir) {
|
|
121
|
+
if (!fs.existsSync(dir) || fs.lstatSync(dir).isSymbolicLink()) return;
|
|
122
|
+
fs.chmodSync(dir, 0o755);
|
|
123
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) if (e.isDirectory()) makeWritable(path.join(dir, e.name));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function safeStat(p) { try { return fs.statSync(p); } catch { return null; } }
|