dsh-remote 0.8.2 → 0.8.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.
package/lib/paths.js CHANGED
@@ -1,169 +1,169 @@
1
- // dsh-remote — pure path / shell helpers (no fs, no ssh).
2
- // Split out of lib/index.js so they are unit-testable without loading the
3
- // plugin. Both POSIX (`/a/b`) and Windows (`D:\Code`, `\\server\share`) forms
4
- // are supported everywhere.
5
-
6
- /** Single-quote one shell argument verbatim. */
7
- export function shq(s) {
8
- return "'" + String(s).replace(/'/g, "'\\''") + "'"
9
- }
10
-
11
- /**
12
- * Normalize a remote path into a clean absolute path, supporting BOTH POSIX
13
- * (`/home/user`) and Windows (`D:\Code`, `C:/Users/x`, `\\server\share`) forms.
14
- *
15
- * - POSIX paths keep `/` separators and a leading `/`.
16
- * - Windows drive paths keep their `X:` prefix and are returned with `\`
17
- * separators (the native form on a Windows remote); a leading `/` is NOT
18
- * added, so `D:\Code` stays `D:\Code` instead of becoming `/D:\Code`.
19
- * - UNC paths (`\\server\share\…`) keep the `\\server\share` prefix.
20
- * - `.` / `..` segments are collapsed in both separator styles.
21
- */
22
- export function normalizeRemotePath(p) {
23
- const s = String(p)
24
- // Windows UNC: \\server\share\…
25
- const unc = s.match(/^(\\\\[^\\]+(?:\\[^\\]+)?)(?:\\|$)(.*)$/s)
26
- if (unc) {
27
- const [prefix, rest] = [unc[1], unc[2]]
28
- const parts = []
29
- for (const seg of rest.split(/[\\/]+/)) {
30
- if (seg === '' || seg === '.') continue
31
- if (seg === '..') { parts.pop(); continue }
32
- parts.push(seg)
33
- }
34
- return parts.length ? prefix + '\\' + parts.join('\\') : prefix
35
- }
36
- // Windows drive: X:\… or X:/…
37
- const drive = s.match(/^([a-zA-Z]:)(?:[\\/]|$)(.*)$/s)
38
- if (drive) {
39
- const [prefix, rest] = [drive[1], drive[2]]
40
- const parts = []
41
- for (const seg of rest.split(/[\\/]+/)) {
42
- if (seg === '' || seg === '.') continue
43
- if (seg === '..') { parts.pop(); continue }
44
- parts.push(seg)
45
- }
46
- return prefix + '\\' + parts.join('\\')
47
- }
48
- // POSIX: /a/b/c
49
- const parts = []
50
- for (const seg of s.split('/')) {
51
- if (seg === '' || seg === '.') continue
52
- if (seg === '..') {
53
- parts.pop()
54
- continue
55
- }
56
- parts.push(seg)
57
- }
58
- return '/' + parts.join('/')
59
- }
60
-
61
- /** Join a remote dir + entry name, honoring the dir's own separator style. */
62
- export function joinRemotePath(dir, name) {
63
- const d = String(dir)
64
- if (!d) return String(name)
65
- if (d.endsWith('/') || d.endsWith('\\')) return d + String(name)
66
- return d + (d.includes('\\') ? '\\' : '/') + String(name)
67
- }
68
-
69
- /** Parent dir of a remote absolute path (string-level; POSIX or Windows). */
70
- export function remoteDirname(p) {
71
- const norm = normalizeRemotePath(p)
72
- // Windows drive/UNC root: D:\ or \\server\share
73
- if (norm === '/' || /^[a-zA-Z]:\\?$/.test(norm) || /^\\\\[^\\]+\\[^\\]+$/.test(norm)) {
74
- // Ensure a drive root ends with a backslash: D: → D:\
75
- const m = norm.match(/^([a-zA-Z]:)$/)
76
- return m ? m[1] + '\\' : norm
77
- }
78
- const sep = norm.includes('\\') ? '\\' : '/'
79
- const idx = norm.lastIndexOf(sep)
80
- if (idx <= 0) {
81
- // Windows drive with a child (D:\Code) → D:\
82
- const m = norm.match(/^([a-zA-Z]:)\\/)
83
- return m ? m[1] + '\\' : sep === '/' && norm.startsWith('/') ? '/' : norm
84
- }
85
- // A parent that is a Windows drive root also needs the trailing backslash.
86
- const parent = norm.slice(0, idx)
87
- if (sep === '\\' && /^[a-zA-Z]:$/.test(parent)) return parent + '\\'
88
- return parent
89
- }
90
-
91
- /** Create every level of a remote dir via SFTP (true mkdir -p: parents AND the
92
- * target itself), working for both POSIX (`/a/b`) and Windows (`D:\a\b`)
93
- * paths. Best effort: an existing or permission-denied level is skipped.
94
- * FILE-oriented callers must pass the parent dir (remoteDirname(filePath)). */
95
- export async function mkdirRemoteDirs(sftp, dir) {
96
- const target = normalizeRemotePath(dir)
97
- const isWin = target.includes('\\')
98
- // Split off a Windows drive prefix (D:) so it is not treated as a segment.
99
- const drive = isWin ? (target.match(/^([a-zA-Z]:)[\\/]?(.*)$/s) || null) : null
100
- const segs = drive ? drive[2].split(/[\\/]/).filter(Boolean) : target.split(/[\\/]/).filter(Boolean)
101
- let cur = isWin ? (drive ? drive[1] + '\\' : '') : ''
102
- // Drive root (D:\) itself, best effort (usually exists).
103
- if (isWin && cur) { try { await sftp.mkdir(cur) } catch { /* exists */ } }
104
- for (const s of segs) {
105
- if (cur) {
106
- if (!cur.endsWith('\\') && !cur.endsWith('/')) cur += isWin ? '\\' : '/'
107
- cur += s
108
- } else {
109
- cur = (isWin ? s + ':' : '/' + s)
110
- }
111
- try { await sftp.mkdir(cur) } catch { /* exists or no perms */ }
112
- }
113
- }
114
-
115
- /**
116
- * Convert an internal remote path (POSIX `/a/b` or Windows `D:\Code`) into the
117
- * path format the SFTP server understands.
118
- *
119
- * Windows OpenSSH's sftp-server accepts POSIX-style paths: a drive letter is
120
- * written `/D:/…` (equivalent to `D:\…`). POSIX paths pass through unchanged.
121
- * UNC paths are kept as-is (the server resolves `\\server\share` itself).
122
- */
123
- export function toSftpPath(p) {
124
- const norm = normalizeRemotePath(p)
125
- const m = norm.match(/^([a-zA-Z]):\\(.*)$/s)
126
- if (m) return '/' + m[1] + ':/' + m[2].replace(/\\/g, '/')
127
- return norm
128
- }
129
-
130
- /** Basename of a remote absolute path (POSIX or Windows). */
131
- export function remotePathBase(p) {
132
- const norm = normalizeRemotePath(p).replace(/[\\/]+$/, '')
133
- const base = norm.split(/[\\/]/).pop()
134
- // Windows drive root (D:\) has no meaningful basename
135
- if (!base || /^[a-zA-Z]:$/.test(base)) return 'workspace'
136
- return base
137
- }
138
-
139
- /** Relative path of `p` under `root` (POSIX form, no leading slash), or null
140
- * when `p` is not inside `root`. Both POSIX and Windows drive paths work
141
- * (Windows is compared in its `/D:/…` POSIX-equivalent form). */
142
- export function relPathUnder(root, p) {
143
- const posix = (x) => {
144
- let n = normalizeRemotePath(x).replace(/[\\/]+$/, '')
145
- const m = n.match(/^([a-zA-Z]):(.*)$/s)
146
- if (m) return '/' + m[1].toLowerCase() + ':/' + (m[2] ? m[2].replace(/\\/g, '/').replace(/^\/+/, '') : '')
147
- return n.replace(/\\/g, '/')
148
- }
149
- const r = posix(root)
150
- const n = posix(p)
151
- if (n === r) return ''
152
- const prefix = r === '/' ? '/' : r + '/'
153
- if (n.startsWith(prefix)) return n.slice(prefix.length)
154
- return null
155
- }
156
-
157
- export function truncate(s, max) {
158
- if (s.length <= max) return s
159
- return s.slice(0, max) + `\n…[truncated: ${s.length - max} more chars]`
160
- }
161
-
162
- /** 32-bit string hash rendered in base36 (short, collision-safe enough). */
163
- export function shortHash(s) {
164
- let h = 0
165
- for (let i = 0; i < s.length; i++) {
166
- h = ((h << 5) - h + s.charCodeAt(i)) | 0
167
- }
168
- return (h >>> 0).toString(36)
169
- }
1
+ // dsh-remote — pure path / shell helpers (no fs, no ssh).
2
+ // Split out of lib/index.js so they are unit-testable without loading the
3
+ // plugin. Both POSIX (`/a/b`) and Windows (`D:\Code`, `\\server\share`) forms
4
+ // are supported everywhere.
5
+
6
+ /** Single-quote one shell argument verbatim. */
7
+ export function shq(s) {
8
+ return "'" + String(s).replace(/'/g, "'\\''") + "'"
9
+ }
10
+
11
+ /**
12
+ * Normalize a remote path into a clean absolute path, supporting BOTH POSIX
13
+ * (`/home/user`) and Windows (`D:\Code`, `C:/Users/x`, `\\server\share`) forms.
14
+ *
15
+ * - POSIX paths keep `/` separators and a leading `/`.
16
+ * - Windows drive paths keep their `X:` prefix and are returned with `\`
17
+ * separators (the native form on a Windows remote); a leading `/` is NOT
18
+ * added, so `D:\Code` stays `D:\Code` instead of becoming `/D:\Code`.
19
+ * - UNC paths (`\\server\share\…`) keep the `\\server\share` prefix.
20
+ * - `.` / `..` segments are collapsed in both separator styles.
21
+ */
22
+ export function normalizeRemotePath(p) {
23
+ const s = String(p)
24
+ // Windows UNC: \\server\share\…
25
+ const unc = s.match(/^(\\\\[^\\]+(?:\\[^\\]+)?)(?:\\|$)(.*)$/s)
26
+ if (unc) {
27
+ const [prefix, rest] = [unc[1], unc[2]]
28
+ const parts = []
29
+ for (const seg of rest.split(/[\\/]+/)) {
30
+ if (seg === '' || seg === '.') continue
31
+ if (seg === '..') { parts.pop(); continue }
32
+ parts.push(seg)
33
+ }
34
+ return parts.length ? prefix + '\\' + parts.join('\\') : prefix
35
+ }
36
+ // Windows drive: X:\… or X:/…
37
+ const drive = s.match(/^([a-zA-Z]:)(?:[\\/]|$)(.*)$/s)
38
+ if (drive) {
39
+ const [prefix, rest] = [drive[1], drive[2]]
40
+ const parts = []
41
+ for (const seg of rest.split(/[\\/]+/)) {
42
+ if (seg === '' || seg === '.') continue
43
+ if (seg === '..') { parts.pop(); continue }
44
+ parts.push(seg)
45
+ }
46
+ return prefix + '\\' + parts.join('\\')
47
+ }
48
+ // POSIX: /a/b/c
49
+ const parts = []
50
+ for (const seg of s.split('/')) {
51
+ if (seg === '' || seg === '.') continue
52
+ if (seg === '..') {
53
+ parts.pop()
54
+ continue
55
+ }
56
+ parts.push(seg)
57
+ }
58
+ return '/' + parts.join('/')
59
+ }
60
+
61
+ /** Join a remote dir + entry name, honoring the dir's own separator style. */
62
+ export function joinRemotePath(dir, name) {
63
+ const d = String(dir)
64
+ if (!d) return String(name)
65
+ if (d.endsWith('/') || d.endsWith('\\')) return d + String(name)
66
+ return d + (d.includes('\\') ? '\\' : '/') + String(name)
67
+ }
68
+
69
+ /** Parent dir of a remote absolute path (string-level; POSIX or Windows). */
70
+ export function remoteDirname(p) {
71
+ const norm = normalizeRemotePath(p)
72
+ // Windows drive/UNC root: D:\ or \\server\share
73
+ if (norm === '/' || /^[a-zA-Z]:\\?$/.test(norm) || /^\\\\[^\\]+\\[^\\]+$/.test(norm)) {
74
+ // Ensure a drive root ends with a backslash: D: → D:\
75
+ const m = norm.match(/^([a-zA-Z]:)$/)
76
+ return m ? m[1] + '\\' : norm
77
+ }
78
+ const sep = norm.includes('\\') ? '\\' : '/'
79
+ const idx = norm.lastIndexOf(sep)
80
+ if (idx <= 0) {
81
+ // Windows drive with a child (D:\Code) → D:\
82
+ const m = norm.match(/^([a-zA-Z]:)\\/)
83
+ return m ? m[1] + '\\' : sep === '/' && norm.startsWith('/') ? '/' : norm
84
+ }
85
+ // A parent that is a Windows drive root also needs the trailing backslash.
86
+ const parent = norm.slice(0, idx)
87
+ if (sep === '\\' && /^[a-zA-Z]:$/.test(parent)) return parent + '\\'
88
+ return parent
89
+ }
90
+
91
+ /** Create every level of a remote dir via SFTP (true mkdir -p: parents AND the
92
+ * target itself), working for both POSIX (`/a/b`) and Windows (`D:\a\b`)
93
+ * paths. Best effort: an existing or permission-denied level is skipped.
94
+ * FILE-oriented callers must pass the parent dir (remoteDirname(filePath)). */
95
+ export async function mkdirRemoteDirs(sftp, dir) {
96
+ const target = normalizeRemotePath(dir)
97
+ const isWin = target.includes('\\')
98
+ // Split off a Windows drive prefix (D:) so it is not treated as a segment.
99
+ const drive = isWin ? (target.match(/^([a-zA-Z]:)[\\/]?(.*)$/s) || null) : null
100
+ const segs = drive ? drive[2].split(/[\\/]/).filter(Boolean) : target.split(/[\\/]/).filter(Boolean)
101
+ let cur = isWin ? (drive ? drive[1] + '\\' : '') : ''
102
+ // Drive root (D:\) itself, best effort (usually exists).
103
+ if (isWin && cur) { try { await sftp.mkdir(cur) } catch { /* exists */ } }
104
+ for (const s of segs) {
105
+ if (cur) {
106
+ if (!cur.endsWith('\\') && !cur.endsWith('/')) cur += isWin ? '\\' : '/'
107
+ cur += s
108
+ } else {
109
+ cur = (isWin ? s + ':' : '/' + s)
110
+ }
111
+ try { await sftp.mkdir(cur) } catch { /* exists or no perms */ }
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Convert an internal remote path (POSIX `/a/b` or Windows `D:\Code`) into the
117
+ * path format the SFTP server understands.
118
+ *
119
+ * Windows OpenSSH's sftp-server accepts POSIX-style paths: a drive letter is
120
+ * written `/D:/…` (equivalent to `D:\…`). POSIX paths pass through unchanged.
121
+ * UNC paths are kept as-is (the server resolves `\\server\share` itself).
122
+ */
123
+ export function toSftpPath(p) {
124
+ const norm = normalizeRemotePath(p)
125
+ const m = norm.match(/^([a-zA-Z]):\\(.*)$/s)
126
+ if (m) return '/' + m[1] + ':/' + m[2].replace(/\\/g, '/')
127
+ return norm
128
+ }
129
+
130
+ /** Basename of a remote absolute path (POSIX or Windows). */
131
+ export function remotePathBase(p) {
132
+ const norm = normalizeRemotePath(p).replace(/[\\/]+$/, '')
133
+ const base = norm.split(/[\\/]/).pop()
134
+ // Windows drive root (D:\) has no meaningful basename
135
+ if (!base || /^[a-zA-Z]:$/.test(base)) return 'workspace'
136
+ return base
137
+ }
138
+
139
+ /** Relative path of `p` under `root` (POSIX form, no leading slash), or null
140
+ * when `p` is not inside `root`. Both POSIX and Windows drive paths work
141
+ * (Windows is compared in its `/D:/…` POSIX-equivalent form). */
142
+ export function relPathUnder(root, p) {
143
+ const posix = (x) => {
144
+ let n = normalizeRemotePath(x).replace(/[\\/]+$/, '')
145
+ const m = n.match(/^([a-zA-Z]):(.*)$/s)
146
+ if (m) return '/' + m[1].toLowerCase() + ':/' + (m[2] ? m[2].replace(/\\/g, '/').replace(/^\/+/, '') : '')
147
+ return n.replace(/\\/g, '/')
148
+ }
149
+ const r = posix(root)
150
+ const n = posix(p)
151
+ if (n === r) return ''
152
+ const prefix = r === '/' ? '/' : r + '/'
153
+ if (n.startsWith(prefix)) return n.slice(prefix.length)
154
+ return null
155
+ }
156
+
157
+ export function truncate(s, max) {
158
+ if (s.length <= max) return s
159
+ return s.slice(0, max) + `\n…[truncated: ${s.length - max} more chars]`
160
+ }
161
+
162
+ /** 32-bit string hash rendered in base36 (short, collision-safe enough). */
163
+ export function shortHash(s) {
164
+ let h = 0
165
+ for (let i = 0; i < s.length; i++) {
166
+ h = ((h << 5) - h + s.charCodeAt(i)) | 0
167
+ }
168
+ return (h >>> 0).toString(36)
169
+ }
package/lib/search.js CHANGED
@@ -1,127 +1,127 @@
1
- // dsh-remote — portable recursive search over SFTP.
2
- // Replaces `find … -exec grep` (which needs a POSIX shell and dies on
3
- // Windows remotes). Walks the remote tree over SFTP readdir/stat/readFile with
4
- // the same bounded-parallelism + ignore rules as the mirror sync.
5
- import { joinRemotePath } from './paths.js'
6
-
7
- function mapLimit(items, limit, fn) {
8
- const limitN = Math.max(1, Math.min(Math.floor(limit) || 1, items.length))
9
- let next = 0
10
- const worker = async () => {
11
- while (next < items.length) {
12
- const i = next++
13
- try { await fn(items[i], i) } catch { /* per-item errors are the fn's concern */ }
14
- }
15
- }
16
- const workers = []
17
- for (let w = 0; w < limitN; w++) workers.push(worker())
18
- return Promise.all(workers)
19
- }
20
-
21
- /** fnmatch-lite for the `glob` filter (matches the entry BASENAME). */
22
- export function matchGlob(name, glob) {
23
- let re = ''
24
- let i = 0
25
- const g = String(glob || '')
26
- while (i < g.length) {
27
- const c = g[i]
28
- if (c === '*') {
29
- if (g[i + 1] === '*') { re += '.*'; i += 2; continue }
30
- re += '[^/]*'; i++; continue
31
- }
32
- if (c === '?') { re += '[^/]'; i++; continue }
33
- if ('\\^$+{}[]()|.'.includes(c)) re += '\\' + c
34
- else re += c
35
- i++
36
- }
37
- return new RegExp('^' + re + '$').test(String(name))
38
- }
39
-
40
- const BINARY_SNIFF = 8192
41
- function looksBinary(buf) {
42
- const n = Math.min(buf.length, BINARY_SNIFF)
43
- for (let i = 0; i < n; i++) if (buf[i] === 0) return true
44
- return false
45
- }
46
-
47
- /**
48
- * Recursively search a remote directory.
49
- * @returns {Promise<{matches: Array<{path, line, text}>, scanned: number, truncated: boolean}>}
50
- */
51
- export async function searchTree(sftp, rootDir, opts = {}) {
52
- const {
53
- regex, // RegExp (already compiled with ignoreCase where asked)
54
- glob, // optional basename glob filter
55
- contextLines = 0,
56
- maxMatches = 500,
57
- maxDepth = 12,
58
- maxFiles = 20000,
59
- maxScanBytes = 1024 * 1024,
60
- isIgnored = () => false,
61
- } = opts
62
-
63
- const matches = []
64
- let scanned = 0
65
- let truncated = false
66
-
67
- const walk = async (dir, depth) => {
68
- if (truncated || scanned >= maxFiles) return
69
- let entries = []
70
- try { entries = (await sftp.readdir(dir)) || [] } catch { return }
71
- const dirs = []
72
- for (const e of entries) {
73
- const name = String(e.filename)
74
- if (name === '.' || name === '..') continue
75
- const isDir = !!(e.attrs && e.attrs.isDirectory && e.attrs.isDirectory())
76
- if (isDir) dirs.push(name)
77
- }
78
- // Depth-first, dirs first, but stop as soon as we hit caps.
79
- for (const name of dirs) {
80
- if (truncated || scanned >= maxFiles) return
81
- if (depth <= 0) continue
82
- const sub = joinRemotePath(dir, name)
83
- if (isIgnored(name, true)) continue
84
- await walk(sub, depth - 1)
85
- }
86
- if (truncated || scanned >= maxFiles) return
87
-
88
- const fileNames = entries
89
- .filter((e) => !(e.attrs && e.attrs.isDirectory && e.attrs.isDirectory()))
90
- .map((e) => String(e.filename))
91
- .filter((n) => n !== '.' && n !== '..')
92
- await mapLimit(fileNames, 4, async (name) => {
93
- if (truncated || scanned >= maxFiles || matches.length >= maxMatches) return
94
- if (glob && !matchGlob(name, glob)) return
95
- if (isIgnored(name, false)) return
96
- const fp = joinRemotePath(dir, name)
97
- scanned++
98
- let st
99
- try { st = await sftp.stat(fp) } catch { return }
100
- if (st.size > maxScanBytes) return
101
- let buf
102
- try { buf = await sftp.readFile(fp) } catch { return }
103
- if (looksBinary(buf)) return
104
- const content = buf.toString('utf8').replace(/\r\n/g, '\n')
105
- const lines = content.split('\n')
106
- const hitLines = new Set()
107
- for (let i = 0; i < lines.length; i++) {
108
- if (regex.test(lines[i])) {
109
- regex.lastIndex = 0
110
- for (let c = Math.max(0, i - contextLines); c <= Math.min(lines.length - 1, i + contextLines); c++) {
111
- hitLines.add(c)
112
- }
113
- if (matches.length >= maxMatches) { truncated = true; return }
114
- }
115
- }
116
- if (hitLines.size) {
117
- for (const ln of [...hitLines].sort((a, b) => a - b)) {
118
- if (matches.length >= maxMatches) { truncated = true; return }
119
- matches.push({ path: fp, line: ln + 1, text: lines[ln] })
120
- }
121
- }
122
- })
123
- }
124
-
125
- await walk(rootDir, maxDepth)
126
- return { matches, scanned, truncated }
127
- }
1
+ // dsh-remote — portable recursive search over SFTP.
2
+ // Replaces `find … -exec grep` (which needs a POSIX shell and dies on
3
+ // Windows remotes). Walks the remote tree over SFTP readdir/stat/readFile with
4
+ // the same bounded-parallelism + ignore rules as the mirror sync.
5
+ import { joinRemotePath } from './paths.js'
6
+
7
+ function mapLimit(items, limit, fn) {
8
+ const limitN = Math.max(1, Math.min(Math.floor(limit) || 1, items.length))
9
+ let next = 0
10
+ const worker = async () => {
11
+ while (next < items.length) {
12
+ const i = next++
13
+ try { await fn(items[i], i) } catch { /* per-item errors are the fn's concern */ }
14
+ }
15
+ }
16
+ const workers = []
17
+ for (let w = 0; w < limitN; w++) workers.push(worker())
18
+ return Promise.all(workers)
19
+ }
20
+
21
+ /** fnmatch-lite for the `glob` filter (matches the entry BASENAME). */
22
+ export function matchGlob(name, glob) {
23
+ let re = ''
24
+ let i = 0
25
+ const g = String(glob || '')
26
+ while (i < g.length) {
27
+ const c = g[i]
28
+ if (c === '*') {
29
+ if (g[i + 1] === '*') { re += '.*'; i += 2; continue }
30
+ re += '[^/]*'; i++; continue
31
+ }
32
+ if (c === '?') { re += '[^/]'; i++; continue }
33
+ if ('\\^$+{}[]()|.'.includes(c)) re += '\\' + c
34
+ else re += c
35
+ i++
36
+ }
37
+ return new RegExp('^' + re + '$').test(String(name))
38
+ }
39
+
40
+ const BINARY_SNIFF = 8192
41
+ function looksBinary(buf) {
42
+ const n = Math.min(buf.length, BINARY_SNIFF)
43
+ for (let i = 0; i < n; i++) if (buf[i] === 0) return true
44
+ return false
45
+ }
46
+
47
+ /**
48
+ * Recursively search a remote directory.
49
+ * @returns {Promise<{matches: Array<{path, line, text}>, scanned: number, truncated: boolean}>}
50
+ */
51
+ export async function searchTree(sftp, rootDir, opts = {}) {
52
+ const {
53
+ regex, // RegExp (already compiled with ignoreCase where asked)
54
+ glob, // optional basename glob filter
55
+ contextLines = 0,
56
+ maxMatches = 500,
57
+ maxDepth = 12,
58
+ maxFiles = 20000,
59
+ maxScanBytes = 1024 * 1024,
60
+ isIgnored = () => false,
61
+ } = opts
62
+
63
+ const matches = []
64
+ let scanned = 0
65
+ let truncated = false
66
+
67
+ const walk = async (dir, depth) => {
68
+ if (truncated || scanned >= maxFiles) return
69
+ let entries = []
70
+ try { entries = (await sftp.readdir(dir)) || [] } catch { return }
71
+ const dirs = []
72
+ for (const e of entries) {
73
+ const name = String(e.filename)
74
+ if (name === '.' || name === '..') continue
75
+ const isDir = !!(e.attrs && e.attrs.isDirectory && e.attrs.isDirectory())
76
+ if (isDir) dirs.push(name)
77
+ }
78
+ // Depth-first, dirs first, but stop as soon as we hit caps.
79
+ for (const name of dirs) {
80
+ if (truncated || scanned >= maxFiles) return
81
+ if (depth <= 0) continue
82
+ const sub = joinRemotePath(dir, name)
83
+ if (isIgnored(name, true)) continue
84
+ await walk(sub, depth - 1)
85
+ }
86
+ if (truncated || scanned >= maxFiles) return
87
+
88
+ const fileNames = entries
89
+ .filter((e) => !(e.attrs && e.attrs.isDirectory && e.attrs.isDirectory()))
90
+ .map((e) => String(e.filename))
91
+ .filter((n) => n !== '.' && n !== '..')
92
+ await mapLimit(fileNames, 4, async (name) => {
93
+ if (truncated || scanned >= maxFiles || matches.length >= maxMatches) return
94
+ if (glob && !matchGlob(name, glob)) return
95
+ if (isIgnored(name, false)) return
96
+ const fp = joinRemotePath(dir, name)
97
+ scanned++
98
+ let st
99
+ try { st = await sftp.stat(fp) } catch { return }
100
+ if (st.size > maxScanBytes) return
101
+ let buf
102
+ try { buf = await sftp.readFile(fp) } catch { return }
103
+ if (looksBinary(buf)) return
104
+ const content = buf.toString('utf8').replace(/\r\n/g, '\n')
105
+ const lines = content.split('\n')
106
+ const hitLines = new Set()
107
+ for (let i = 0; i < lines.length; i++) {
108
+ if (regex.test(lines[i])) {
109
+ regex.lastIndex = 0
110
+ for (let c = Math.max(0, i - contextLines); c <= Math.min(lines.length - 1, i + contextLines); c++) {
111
+ hitLines.add(c)
112
+ }
113
+ if (matches.length >= maxMatches) { truncated = true; return }
114
+ }
115
+ }
116
+ if (hitLines.size) {
117
+ for (const ln of [...hitLines].sort((a, b) => a - b)) {
118
+ if (matches.length >= maxMatches) { truncated = true; return }
119
+ matches.push({ path: fp, line: ln + 1, text: lines[ln] })
120
+ }
121
+ }
122
+ })
123
+ }
124
+
125
+ await walk(rootDir, maxDepth)
126
+ return { matches, scanned, truncated }
127
+ }