(client, sessionID, directory, options = {})
| 826 | return job |
| 827 | } |
| 828 | |
| 829 | async function maybeCompact(client, sessionID, job) { |
| 830 | const dueRuns = job.compactEveryRuns > 0 && (job.runCount || 0) > 0 && (job.runCount || 0) % job.compactEveryRuns === 0 && job.lastCompactRunCount !== job.runCount |
| 831 | const dueTime = job.compactEveryMs > 0 && (!job.lastCompactAt || now() - job.lastCompactAt >= job.compactEveryMs) |
| 832 | if (!dueRuns && !dueTime) return job |
| 833 | if (await compactSession(client, sessionID)) { |
| 834 | job.lastCompactAt = now() |
| 835 | job.lastCompactRunCount = job.runCount || 0 |
| 836 | } |
| 837 | return job |
| 838 | } |
| 839 | |
| 840 | async function snapshotPaths(directory, files) { |
| 841 | const snapshot = {} |
| 842 | for (const file of files || []) { |
| 843 | try { |
| 844 | const stat = await fs.stat(path.resolve(directory, file)) |
| 845 | snapshot[file] = `${stat.mtimeMs}:${stat.size}` |
| 846 | } catch { snapshot[file] = "missing" } |
| 847 | } |
| 848 | return snapshot |
| 849 | } |
| 850 | |
| 851 | async function watchChanged(directory, job) { |
| 852 | if (!job.watchPaths?.length) return false |
| 853 | const next = await snapshotPaths(directory, job.watchPaths) |
| 854 | const previous = job.watchSnapshot || {} |
| 855 | const changed = job.watchPaths.some((file) => previous[file] !== next[file]) |
| 856 | if (changed) job.watchSnapshot = next |
| 857 | return changed |
| 858 | } |
| 859 | |
| 860 | async function fileContains(filePath, needle) { |
| 861 | try { |
| 862 | const stat = await fs.stat(filePath) |
| 863 | if (!stat.isFile() || stat.size > MAX_SCAN_BYTES) return false |
no test coverage detected