advisers 0.2.2 → 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/LICENSE +0 -0
- package/NOTICE +0 -0
- package/README-zh.md +11 -7
- package/README.md +16 -8
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +6347 -542
- package/index.js +1 -1
- package/lib/agent.js +529 -80
- package/lib/attachments.js +0 -0
- package/lib/audit-anchor.js +683 -0
- package/lib/backup.js +1345 -0
- package/lib/capture.js +0 -0
- package/lib/context-window.js +0 -0
- package/lib/digest.js +0 -0
- package/lib/doc-review.js +0 -0
- package/lib/egress.js +0 -0
- package/lib/error-codes.js +1373 -0
- package/lib/evidence.js +352 -43
- package/lib/fs-safe.js +347 -0
- package/lib/ical.js +0 -0
- package/lib/imap.js +6 -2
- package/lib/importers.js +0 -0
- package/lib/learning.js +0 -0
- package/lib/ledger-read.js +608 -0
- package/lib/mcp-server.js +331 -50
- package/lib/model.js +73 -1
- package/lib/node-warning-filter.js +15 -0
- package/lib/notify.js +13 -3
- package/lib/nudge.js +0 -0
- package/lib/ocr.js +29 -9
- package/lib/pdf.js +0 -0
- package/lib/pet-brief-store.js +0 -0
- package/lib/pet-brief.js +126 -64
- package/lib/receipt-verify.js +213 -15
- package/lib/relative-date.js +0 -0
- package/lib/remind.js +91 -36
- package/lib/research.js +0 -0
- package/lib/review.js +0 -0
- package/lib/routine.js +0 -0
- package/lib/safety/classifier.js +55 -2
- package/lib/safety/explain.js +0 -0
- package/lib/safety/index.js +0 -0
- package/lib/safety/redact.js +0 -0
- package/lib/safety/rules.js +28 -0
- package/lib/scheduler.js +0 -0
- package/lib/source.js +0 -0
- package/lib/sqlite.js +2302 -55
- package/lib/sync.js +398 -71
- package/lib/text-safe.js +229 -0
- package/lib/timestamp.js +29 -9
- package/lib/tos/index.js +1 -1
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +12 -9
- package/lib/tos/safety.zh-CN.md +4 -3
- package/lib/tos/summary.zh-CN.md +3 -3
- package/lib/tos/terms.zh-CN.md +8 -6
- package/lib/tui/color.js +164 -0
- package/lib/tui/index.js +33 -0
- package/lib/tui/json.js +124 -0
- package/lib/tui/labels.js +50 -0
- package/lib/tui/policy.js +108 -0
- package/lib/tui/render.js +371 -0
- package/lib/tui/sanitize.js +142 -0
- package/lib/tui/text-file.js +116 -0
- package/lib/tui/width.js +577 -0
- package/lib/ui.js +0 -0
- package/lib/undo.js +0 -0
- package/lib/untrusted.js +186 -0
- package/lib/vault.js +0 -0
- package/package.json +6 -5
package/lib/fs-safe.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crash-safe filesystem helpers (0.2.3 robustness Phase 0, robustness audit
|
|
3
|
+
* P1-2/P1-3/P1-7).
|
|
4
|
+
*
|
|
5
|
+
* Why this module exists: Node's fs.writeSync (and writeFileSync, which also
|
|
6
|
+
* issues a single write(2)) may write fewer bytes than requested. POSIX allows
|
|
7
|
+
* a short write on ENOSPC/EDQUOT, on signal interruption, and on network/quota
|
|
8
|
+
* filesystems; the 2026-11-17 robustness audit reproduced a 1 MB write into a
|
|
9
|
+
* nearly-full tmpfs returning 204800 bytes with no error, which used to install
|
|
10
|
+
* a truncated state.db.vault. A durable write therefore needs:
|
|
11
|
+
* 1. loop until every byte is written (zero/negative progress is EIO);
|
|
12
|
+
* 2. fsync the file before rename, fsync the directory after rename;
|
|
13
|
+
* 3. never leave a truncated target: write a same-directory temp file, verify
|
|
14
|
+
* its length, then atomically rename it into place; on any failure remove
|
|
15
|
+
* the temp file and rethrow (no 'best effort' success);
|
|
16
|
+
* 4. retry transient Windows/AV/OneDrive failures (EPERM/EBUSY/EACCES) with
|
|
17
|
+
* bounded exponential backoff instead of failing a user command once;
|
|
18
|
+
* 5. flush a path through a writable but non-truncating handle ('r+'): Windows
|
|
19
|
+
* rejects fsync on a read-only handle with EPERM, which used to break
|
|
20
|
+
* backup/import/vault-unseal/migration on every Windows machine.
|
|
21
|
+
*
|
|
22
|
+
* This is a library-only module: it never imports node:sqlite and never writes
|
|
23
|
+
* to the user's home. Every function takes an optional `fs` implementation
|
|
24
|
+
* (default: node:fs), so tests can inject short writes and EPERM without root
|
|
25
|
+
* or a full disk (see test/fs-safe.test.js).
|
|
26
|
+
*
|
|
27
|
+
* bin/ integration contract (Wave 1):
|
|
28
|
+
* - vault seal: const tmp = writeAllToTempSync(out, bytes, { mode: 0o600 })
|
|
29
|
+
* ...verify by decrypting tmp...; commitTempFileSync(tmp, out)
|
|
30
|
+
* - pet-brief --out: atomicWriteFileSync(file, bytes)
|
|
31
|
+
* - backup/import: copyFileWithRetrySync(src, dst); fsyncFileSync(dst)
|
|
32
|
+
* - any rename/rm: renameWithRetrySync / rmWithRetrySync
|
|
33
|
+
* Also exported: tempPathForSync/tempPrefixForSync (so stale *.tmp-* scans can
|
|
34
|
+
* name the same scheme), describeFsError (ENOSPC/EACCES -> actionable text).
|
|
35
|
+
*/
|
|
36
|
+
import * as nodeFs from 'node:fs'
|
|
37
|
+
import { randomBytes } from 'node:crypto'
|
|
38
|
+
import { basename, dirname, join } from 'node:path'
|
|
39
|
+
|
|
40
|
+
/** errno values retried for transient filesystem contention (Windows AV/OneDrive). */
|
|
41
|
+
export const DEFAULT_RETRY_ERRNOS = Object.freeze(['EPERM', 'EBUSY', 'EACCES', 'ENOTEMPTY', 'EAGAIN'])
|
|
42
|
+
/** Marker embedded in every temp file this module creates. */
|
|
43
|
+
export const TEMP_MARKER = '.tmp-'
|
|
44
|
+
const DEFAULT_TEMP_PREFIX = '.'
|
|
45
|
+
const MAX_BACKOFF_MS = 200
|
|
46
|
+
|
|
47
|
+
/** Synchronous sleep; used only between bounded retries. */
|
|
48
|
+
export function sleepSync(ms) {
|
|
49
|
+
if (!ms || ms <= 0) return
|
|
50
|
+
try {
|
|
51
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
|
|
52
|
+
} catch {
|
|
53
|
+
const end = Date.now() + ms
|
|
54
|
+
while (Date.now() < end) {}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Bounded exponential backoff: 10, 20, 40, 80, 160, 200, 200... ms. */
|
|
59
|
+
export function retryBackoffMs(attempt) {
|
|
60
|
+
return Math.min(MAX_BACKOFF_MS, 10 * 2 ** Math.max(0, attempt))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function errnoOf(error) {
|
|
64
|
+
const code = error?.code ?? error?.errno
|
|
65
|
+
return typeof code === 'string' ? code : ''
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** True for transient errno values that a retry can legitimately fix. */
|
|
69
|
+
export function isRetryableFsError(error, errnos = DEFAULT_RETRY_ERRNOS) {
|
|
70
|
+
const allowed = errnos instanceof Set ? errnos : new Set(errnos)
|
|
71
|
+
const code = errnoOf(error)
|
|
72
|
+
if (code && allowed.has(code)) return true
|
|
73
|
+
const message = String(error?.message ?? error)
|
|
74
|
+
// libuv/windows sometimes surface EPERM/EBUSY in the message without a code
|
|
75
|
+
// (e.g. wrapped 'operation not permitted, rename ...'), so keep a narrow
|
|
76
|
+
// message fallback. ENOSPC must NOT be retried here.
|
|
77
|
+
return /operation not permitted|resource busy|permission denied/i.test(message) && /rename|unlink|rmdir|copyfile|fsync/i.test(message)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Retry fn() when it throws a retryable fs error. fn receives the 0-based attempt.
|
|
82
|
+
* Non-retryable errors (ENOSPC, ENOENT, EIO, ...) are rethrown immediately.
|
|
83
|
+
*/
|
|
84
|
+
export function retryFsSync(fn, { retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync, onRetry } = {}) {
|
|
85
|
+
const allowed = errnos instanceof Set ? errnos : new Set(errnos)
|
|
86
|
+
let lastError
|
|
87
|
+
for (let attempt = 0; attempt <= Math.max(0, retries); attempt++) {
|
|
88
|
+
try {
|
|
89
|
+
return fn(attempt)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
lastError = error
|
|
92
|
+
if (attempt >= retries || !isRetryableFsError(error, allowed)) throw error
|
|
93
|
+
try { onRetry?.(attempt, error) } catch {}
|
|
94
|
+
sleep(backoffMs(attempt))
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
throw lastError
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Write the whole buffer through fd, looping over short writes.
|
|
102
|
+
* Returns the number of bytes written (always buffer.length or throws).
|
|
103
|
+
* A 0/undefined/negative return is treated as EIO: reporting success after a
|
|
104
|
+
* short write is exactly the bug this helper exists to prevent.
|
|
105
|
+
*/
|
|
106
|
+
export function writeAllSync(fd, data, { fs = nodeFs } = {}) {
|
|
107
|
+
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data)
|
|
108
|
+
let written = 0
|
|
109
|
+
while (written < buffer.length) {
|
|
110
|
+
const chunk = fs.writeSync(fd, buffer, written, buffer.length - written)
|
|
111
|
+
if (!Number.isFinite(chunk) || chunk <= 0) {
|
|
112
|
+
const error = new Error(`E_SHORT_WRITE: writeSync made no progress after ${written} of ${buffer.length} bytes; refusing to report a truncated write as success`)
|
|
113
|
+
error.code = 'E_SHORT_WRITE'
|
|
114
|
+
error.written = written
|
|
115
|
+
error.expected = buffer.length
|
|
116
|
+
throw error
|
|
117
|
+
}
|
|
118
|
+
written += chunk
|
|
119
|
+
}
|
|
120
|
+
return written
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Errnos a writable open can fail with when the path is read-only for this user.
|
|
125
|
+
* On Windows they mean fsync cannot be performed at all (see openForFsyncSync).
|
|
126
|
+
*/
|
|
127
|
+
const WINDOWS_FSYNC_UNWRITABLE_ERRNOS = new Set(['EACCES', 'EPERM', 'EROFS'])
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Open `file` for the path form of fsyncFileSync.
|
|
131
|
+
*
|
|
132
|
+
* Windows: fsyncSync() is FlushFileBuffers(), which only accepts a handle opened
|
|
133
|
+
* with write access. Opening the file read-only and fsyncing it afterwards fails
|
|
134
|
+
* with EPERM, which used to break every durable-write path on Windows
|
|
135
|
+
* (`advisers backup`, `import --replace`, `vault unseal` and the pre-migration
|
|
136
|
+
* backup; see 0.3 research §4.1/R1). The handle must therefore be writable AND
|
|
137
|
+
* must never truncate, hence `'r+'`: it requires the file to exist and keeps its
|
|
138
|
+
* bytes and length exactly as they are. `createIfMissing` opts into `'a+'`
|
|
139
|
+
* (creates a missing zero-byte file, never truncates an existing one) for a
|
|
140
|
+
* caller that genuinely needs to flush a path that may not exist yet.
|
|
141
|
+
*
|
|
142
|
+
* POSIX: fsync(2) accepts an O_RDONLY descriptor, so a read-only-but-readable
|
|
143
|
+
* file (and a read-only filesystem) keeps the historical `'r'` fallback.
|
|
144
|
+
*
|
|
145
|
+
* Windows read-only files are fail-closed on purpose: returning success without
|
|
146
|
+
* flushing would let a caller report a backup as durable when it is not.
|
|
147
|
+
*/
|
|
148
|
+
function openForFsyncSync(file, { fs = nodeFs, platform = process.platform, createIfMissing = false } = {}) {
|
|
149
|
+
try {
|
|
150
|
+
return fs.openSync(file, 'r+')
|
|
151
|
+
} catch (error) {
|
|
152
|
+
const code = errnoOf(error)
|
|
153
|
+
if (code === 'ENOENT' && createIfMissing) return fs.openSync(file, 'a+')
|
|
154
|
+
if (platform !== 'win32') {
|
|
155
|
+
// POSIX: flush a readable descriptor when the file cannot be opened writable.
|
|
156
|
+
return fs.openSync(file, 'r')
|
|
157
|
+
}
|
|
158
|
+
if (WINDOWS_FSYNC_UNWRITABLE_ERRNOS.has(code)) {
|
|
159
|
+
const denied = new Error(`${code}: cannot fsync ${file} on Windows: FlushFileBuffers requires a writable handle and the file could not be opened read-write; make the file/directory writable, or close the program (antivirus/OneDrive/sync client) that may be holding it`)
|
|
160
|
+
denied.code = code
|
|
161
|
+
if (error?.errno !== undefined) denied.errno = error.errno
|
|
162
|
+
denied.cause = error
|
|
163
|
+
throw denied
|
|
164
|
+
}
|
|
165
|
+
throw error
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* fsync a file descriptor or a path.
|
|
171
|
+
*
|
|
172
|
+
* The numeric form flushes the caller's descriptor (a temp file the caller just
|
|
173
|
+
* wrote is already open for writing). The path form opens a writable,
|
|
174
|
+
* non-truncating handle for the duration of the fsync -- never a read-only one,
|
|
175
|
+
* because Windows rejects fsync on read-only handles with EPERM (see
|
|
176
|
+
* openForFsyncSync). The file's bytes and length are left unchanged.
|
|
177
|
+
*/
|
|
178
|
+
export function fsyncFileSync(fileOrFd, { fs = nodeFs, platform = process.platform, createIfMissing = false, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
|
|
179
|
+
if (typeof fileOrFd === 'number') {
|
|
180
|
+
return retryFsSync(() => fs.fsyncSync(fileOrFd), { retries, errnos, backoffMs, sleep })
|
|
181
|
+
}
|
|
182
|
+
return retryFsSync(() => {
|
|
183
|
+
const fd = openForFsyncSync(fileOrFd, { fs, platform, createIfMissing })
|
|
184
|
+
try { fs.fsyncSync(fd) } finally { try { fs.closeSync(fd) } catch {} }
|
|
185
|
+
}, { retries, errnos, backoffMs, sleep })
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* fsync a directory entry so a completed rename survives a crash. Windows
|
|
190
|
+
* cannot fsync a directory through Node (and cannot open one read-write), while
|
|
191
|
+
* FAT/network shares may not support it either, so this is best-effort by
|
|
192
|
+
* contract: it returns a boolean, never throws, and is a no-op (false) on
|
|
193
|
+
* Windows.
|
|
194
|
+
*/
|
|
195
|
+
export function fsyncDirSync(dir, { fs = nodeFs, platform = process.platform } = {}) {
|
|
196
|
+
// Windows cannot fsync a directory at all (and cannot open one read-write):
|
|
197
|
+
// FlushFileBuffers has no directory-handle semantics there, so honour the
|
|
198
|
+
// documented best-effort contract without attempting a doomed read-only open.
|
|
199
|
+
if (platform === 'win32') return false
|
|
200
|
+
try {
|
|
201
|
+
const fd = fs.openSync(dir, 'r')
|
|
202
|
+
try { fs.fsyncSync(fd) } finally { try { fs.closeSync(fd) } catch {} }
|
|
203
|
+
return true
|
|
204
|
+
} catch {
|
|
205
|
+
return false
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Same-directory temp path: hidden, pid+timestamp+random (unique per process). */
|
|
210
|
+
export function tempPathForSync(file) {
|
|
211
|
+
const unique = `${process.pid}-${Date.now().toString(36)}-${randomBytes(4).toString('hex')}`
|
|
212
|
+
return join(dirname(file), `${DEFAULT_TEMP_PREFIX}${basename(file)}${TEMP_MARKER}${unique}`)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Prefix shared by tempPathForSync paths (for stale-temp scans/cleanup). */
|
|
216
|
+
export function tempPrefixForSync(file) {
|
|
217
|
+
return join(dirname(file), `${DEFAULT_TEMP_PREFIX}${basename(file)}${TEMP_MARKER}`)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** mkdir -p with retry; mode only applies to directories it creates. */
|
|
221
|
+
export function mkdirWithRetrySync(dir, { fs = nodeFs, mode = 0o700, recursive = true, retries = 3 } = {}) {
|
|
222
|
+
retryFsSync(() => fs.mkdirSync(dir, { recursive, mode }), { retries })
|
|
223
|
+
return dir
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** rename() with retry on transient EPERM/EBUSY/EACCES. */
|
|
227
|
+
export function renameWithRetrySync(from, to, { fs = nodeFs, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
|
|
228
|
+
retryFsSync(() => fs.renameSync(from, to), { retries, errnos, backoffMs, sleep })
|
|
229
|
+
return to
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** rm with retry; force:true so a missing target is not an error. */
|
|
233
|
+
export function rmWithRetrySync(target, { fs = nodeFs, retries = 3, force = true, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
|
|
234
|
+
try {
|
|
235
|
+
retryFsSync(() => fs.rmSync(target, { force }), { retries, errnos, backoffMs, sleep })
|
|
236
|
+
return true
|
|
237
|
+
} catch (error) {
|
|
238
|
+
if (force && errnoOf(error) === 'ENOENT') return false
|
|
239
|
+
throw error
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** copyFile with retry, then chmod 0600 unless disabled (backups hold plaintext). */
|
|
244
|
+
export function copyFileWithRetrySync(from, to, { fs = nodeFs, mode = 0o600, chmod = true, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync } = {}) {
|
|
245
|
+
retryFsSync(() => fs.copyFileSync(from, to), { retries, errnos, backoffMs, sleep })
|
|
246
|
+
if (chmod && typeof fs.chmodSync === 'function') {
|
|
247
|
+
try { fs.chmodSync(to, mode) } catch {}
|
|
248
|
+
}
|
|
249
|
+
return to
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function cleanupTemp(fs, tmp) {
|
|
253
|
+
try { fs.rmSync(tmp, { force: true }) } catch {}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Write data to a fresh same-directory temp file, fsync it, verify its length,
|
|
258
|
+
* and return the temp path. The caller commits with commitTempFileSync() (and
|
|
259
|
+
* must call rmWithRetrySync(tmp) if it abandons the file after verification).
|
|
260
|
+
* On any failure the temp file is removed before the error is rethrown.
|
|
261
|
+
*/
|
|
262
|
+
export function writeAllToTempSync(file, data, { fs = nodeFs, mode = 0o600, fsync = true, retries = 5, tempPath, expectedBytes } = {}) {
|
|
263
|
+
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data)
|
|
264
|
+
const expected = Number.isFinite(expectedBytes) ? expectedBytes : buffer.length
|
|
265
|
+
mkdirWithRetrySync(dirname(file), { fs })
|
|
266
|
+
const tmp = tempPath ?? tempPathForSync(file)
|
|
267
|
+
let fd = null
|
|
268
|
+
let created = false
|
|
269
|
+
try {
|
|
270
|
+
fd = fs.openSync(tmp, 'wx', mode)
|
|
271
|
+
created = true
|
|
272
|
+
writeAllSync(fd, buffer, { fs })
|
|
273
|
+
if (fsync) fsyncFileSync(fd, { fs, retries })
|
|
274
|
+
fs.closeSync(fd)
|
|
275
|
+
fd = null
|
|
276
|
+
if (typeof fs.chmodSync === 'function') { try { fs.chmodSync(tmp, mode) } catch {} }
|
|
277
|
+
const size = fs.statSync(tmp).size
|
|
278
|
+
if (size !== expected) {
|
|
279
|
+
const error = new Error(`E_SHORT_WRITE: wrote ${size} of ${expected} bytes to ${tmp}; refusing to commit a truncated file`)
|
|
280
|
+
error.code = 'E_SHORT_WRITE'
|
|
281
|
+
error.written = size
|
|
282
|
+
error.expected = expected
|
|
283
|
+
throw error
|
|
284
|
+
}
|
|
285
|
+
return tmp
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (fd !== null) { try { fs.closeSync(fd) } catch {} }
|
|
288
|
+
// Only remove the temp file when this call created it: a pre-existing file
|
|
289
|
+
// at the same path belongs to someone else and is never deleted.
|
|
290
|
+
if (created) cleanupTemp(fs, tmp)
|
|
291
|
+
throw error
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Atomically install a verified temp file at `file` (same directory, atomic
|
|
297
|
+
* rename). On failure the temp file is removed and the previous target is left
|
|
298
|
+
* untouched (rename is all-or-nothing).
|
|
299
|
+
*/
|
|
300
|
+
export function commitTempFileSync(tmp, file, { fs = nodeFs, mode = 0o600, retries = 5, errnos = DEFAULT_RETRY_ERRNOS, backoffMs = retryBackoffMs, sleep = sleepSync, fsyncDir = true } = {}) {
|
|
301
|
+
try {
|
|
302
|
+
renameWithRetrySync(tmp, file, { fs, retries, errnos, backoffMs, sleep })
|
|
303
|
+
if (typeof fs.chmodSync === 'function') { try { fs.chmodSync(file, mode) } catch {} }
|
|
304
|
+
if (fsyncDir) fsyncDirSync(dirname(file), { fs })
|
|
305
|
+
return file
|
|
306
|
+
} catch (error) {
|
|
307
|
+
cleanupTemp(fs, tmp)
|
|
308
|
+
throw error
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Durable atomic write of a whole file. This is the one-call API for bin/
|
|
314
|
+
* outputs that must never be installed truncated (vault seal, pet-brief --out,
|
|
315
|
+
* exports). Throws on ENOSPC/EIO/permission errors after removing the temp file.
|
|
316
|
+
*/
|
|
317
|
+
export function atomicWriteFileSync(file, data, { fs = nodeFs, mode = 0o600, fsync = true, retries = 5, verifyBytes = true } = {}) {
|
|
318
|
+
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data)
|
|
319
|
+
const tmp = writeAllToTempSync(file, buffer, { fs, mode, fsync, retries, expectedBytes: verifyBytes ? buffer.length : undefined })
|
|
320
|
+
return commitTempFileSync(tmp, file, { fs, mode, retries })
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Human-actionable one-line explanation for a failed durable write. */
|
|
324
|
+
export function describeFsError(error, { action = 'writing', path = '', target = '' } = {}) {
|
|
325
|
+
const code = errnoOf(error)
|
|
326
|
+
const message = String(error?.message ?? error)
|
|
327
|
+
const where = target || path
|
|
328
|
+
if (/ENOSPC|EDQUOT/.test(code) || /no space left|disk quota exceeded/i.test(message)) {
|
|
329
|
+
return `E_NO_SPACE: not enough disk space while ${action}${where ? ` ${where}` : ''} (${code || 'ENOSPC'}); the temp file was removed and no truncated file was installed`
|
|
330
|
+
}
|
|
331
|
+
if (/EACCES|EPERM|EROFS/.test(code)) {
|
|
332
|
+
return `E_FS_PERMISSION: ${action}${where ? ` ${where}` : ''} was denied (${code}); check file/directory permissions, or close the program (antivirus/OneDrive/sync client) that may be holding it`
|
|
333
|
+
}
|
|
334
|
+
if (/EIO/.test(code)) return `E_IO: the filesystem reported an I/O error while ${action}${where ? ` ${where}` : ''}; the write was aborted and the temp file removed`
|
|
335
|
+
return `${action}${where ? ` ${where}` : ''} failed: ${message}`
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Wrap an fs error with the actionable message while preserving code/errno. */
|
|
339
|
+
export function wrapFsError(error, context = {}) {
|
|
340
|
+
const wrapped = new Error(describeFsError(error, context))
|
|
341
|
+
if (error?.code !== undefined) wrapped.code = error.code
|
|
342
|
+
if (error?.errno !== undefined) wrapped.errno = error.errno
|
|
343
|
+
wrapped.cause = error
|
|
344
|
+
return wrapped
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export { nodeFs as defaultFs }
|
package/lib/ical.js
CHANGED
|
File without changes
|
package/lib/imap.js
CHANGED
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import net from 'node:net'
|
|
18
18
|
import tls from 'node:tls'
|
|
19
|
+
// 0.3.0 Wave 3 (docs/TUI.md §4): the server greeting and tagged status lines are
|
|
20
|
+
// remote text. They must never reach stderr with ANSI/OSC/bidi or a second line,
|
|
21
|
+
// so every message that embeds one is sanitised here (bin only prefixes it).
|
|
22
|
+
import { sanitizeConsoleText } from './text-safe.js'
|
|
19
23
|
|
|
20
24
|
const CRLF = '\r\n'
|
|
21
25
|
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
@@ -148,12 +152,12 @@ export async function fetchImapMessages({
|
|
|
148
152
|
const t = tag()
|
|
149
153
|
socket.write(`${t} ${text}${CRLF}`)
|
|
150
154
|
const r = await reader.response(t)
|
|
151
|
-
if (!/^OK/i.test(r.status.split(' ').slice(1).join(' '))) throw new Error(`IMAP ${text.split(' ')[0]} failed: ${r.status}`)
|
|
155
|
+
if (!/^OK/i.test(r.status.split(' ').slice(1).join(' '))) throw new Error(`IMAP ${text.split(' ')[0]} failed: ${sanitizeConsoleText(r.status, { max: 500 })}`)
|
|
152
156
|
return r
|
|
153
157
|
}
|
|
154
158
|
try {
|
|
155
159
|
const greeting = await reader.readLine()
|
|
156
|
-
if (!/^\* (OK|PREAUTH)/i.test(greeting)) throw new Error(`IMAP greeting failed: ${greeting}`)
|
|
160
|
+
if (!/^\* (OK|PREAUTH)/i.test(greeting)) throw new Error(`IMAP greeting failed: ${sanitizeConsoleText(greeting, { max: 500 })}`)
|
|
157
161
|
await command(`LOGIN ${imapQuote(user)} ${imapQuote(pass)}`)
|
|
158
162
|
const selected = await command(`EXAMINE ${imapQuote(mailbox)}`)
|
|
159
163
|
const exists = selected.untagged.find((l) => /^\* \d+ EXISTS/i.test(l))
|
package/lib/importers.js
CHANGED
|
File without changes
|
package/lib/learning.js
CHANGED
|
File without changes
|