-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathglob.mts
More file actions
343 lines (317 loc) · 11.3 KB
/
glob.mts
File metadata and controls
343 lines (317 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import path from 'node:path'
import fastGlob from 'fast-glob'
import ignore from 'ignore'
import micromatch from 'micromatch'
import { parse as yamlParse } from 'yaml'
import { isDirSync, safeReadFile } from '@socketsecurity/lib/fs'
import { defaultIgnore } from '@socketsecurity/lib/globs'
import { readPackageJson } from '@socketsecurity/lib/packages'
import { transform } from '@socketsecurity/lib/streams'
import { isNonEmptyString } from '@socketsecurity/lib/strings'
import { homePath } from '../../constants/paths.mts'
import { NODE_MODULES, PNPM } from '../../constants.mts'
import type { Agent } from '../ecosystem/environment.mts'
import type { SocketYml } from '@socketsecurity/config'
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
import type { Options as GlobOptions } from 'fast-glob'
const DEFAULT_IGNORE_FOR_GIT_IGNORE = defaultIgnore.filter(
(p: string) => !p.endsWith('.gitignore'),
)
const IGNORED_DIRS = [
// Taken from ignore-by-default:
// https://github.com/novemberborn/ignore-by-default/blob/v2.1.0/index.js
'.git', // Git repository files, see <https://git-scm.com/>
'.log', // Log files emitted by tools such as `tsserver`, see <https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29>
'.nyc_output', // Temporary directory where nyc stores coverage data, see <https://github.com/bcoe/nyc>
'.sass-cache', // Cache folder for node-sass, see <https://github.com/sass/node-sass>
'.yarn', // Where node modules are installed when using Yarn, see <https://yarnpkg.com/>
'bower_components', // Where Bower packages are installed, see <http://bower.io/>
'coverage', // Standard output directory for code coverage reports, see <https://github.com/gotwarlost/istanbul>
NODE_MODULES, // Where Node modules are installed, see <https://nodejs.org/>
// Taken from globby:
// https://github.com/sindresorhus/globby/blob/v14.0.2/ignore.js#L11-L16
'flow-typed',
] as const
const IGNORED_DIR_PATTERNS = IGNORED_DIRS.map(i => `**/${i}`)
async function getWorkspaceGlobs(
agent: Agent,
cwd = process.cwd(),
): Promise<string[]> {
let workspacePatterns: unknown
if (agent === PNPM) {
const workspacePath = path.join(cwd, 'pnpm-workspace.yaml')
const yml = await safeReadFile(workspacePath, { encoding: 'utf8' })
if (yml) {
try {
workspacePatterns = yamlParse(yml)?.packages
} catch {}
}
} else {
workspacePatterns = (await readPackageJson(cwd, { throws: false }))?.[
'workspaces'
]
}
return Array.isArray(workspacePatterns)
? workspacePatterns
.filter(isNonEmptyString)
.map(workspacePatternToGlobPattern)
: []
}
function ignoreFileLinesToGlobPatterns(
lines: string[] | readonly string[],
filepath: string,
cwd: string,
): string[] {
const base = path.relative(cwd, path.dirname(filepath)).replace(/\\/g, '/')
const patterns = []
for (let i = 0, { length } = lines; i < length; i += 1) {
const pattern = lines[i]!.trim()
if (pattern.length > 0 && pattern.charCodeAt(0) !== 35 /*'#'*/) {
patterns.push(
ignorePatternToMinimatch(
pattern.length && pattern.charCodeAt(0) === 33 /*'!'*/
? `!${path.posix.join(base, pattern.slice(1))}`
: path.posix.join(base, pattern),
),
)
}
}
return patterns
}
function ignoreFileToGlobPatterns(
content: string,
filepath: string,
cwd: string,
): string[] {
return ignoreFileLinesToGlobPatterns(content.split(/\r?\n/), filepath, cwd)
}
// Based on `@eslint/compat` convertIgnorePatternToMinimatch.
// Apache v2.0 licensed
// Copyright Nicholas C. Zakas
// https://github.com/eslint/rewrite/blob/compat-v1.2.1/packages/compat/src/ignore-file.js#L28
function ignorePatternToMinimatch(pattern: string): string {
const isNegated = pattern.startsWith('!')
const negatedPrefix = isNegated ? '!' : ''
const patternToTest = (isNegated ? pattern.slice(1) : pattern).trimEnd()
// Special cases.
if (
patternToTest === '' ||
patternToTest === '**' ||
patternToTest === '/**' ||
patternToTest === '**'
) {
return `${negatedPrefix}${patternToTest}`
}
const firstIndexOfSlash = patternToTest.indexOf('/')
const matchEverywherePrefix =
firstIndexOfSlash === -1 || firstIndexOfSlash === patternToTest.length - 1
? '**/'
: ''
const patternWithoutLeadingSlash =
firstIndexOfSlash === 0 ? patternToTest.slice(1) : patternToTest
// Escape `{` and `(` because in gitignore patterns they are just
// literal characters without any specific syntactic meaning,
// while in minimatch patterns they can form brace expansion or extglob syntax.
//
// For example, gitignore pattern `src/{a,b}.js` ignores file `src/{a,b}.js`.
// But, the same minimatch pattern `src/{a,b}.js` ignores files `src/a.js` and `src/b.js`.
// Minimatch pattern `src/\{a,b}.js` is equivalent to gitignore pattern `src/{a,b}.js`.
const escapedPatternWithoutLeadingSlash =
patternWithoutLeadingSlash.replaceAll(
/(?=((?:\\.|[^{(])*))\1([{(])/guy,
'$1\\$2',
)
const matchInsideSuffix = patternToTest.endsWith('/**') ? '/*' : ''
return `${negatedPrefix}${matchEverywherePrefix}${escapedPatternWithoutLeadingSlash}${matchInsideSuffix}`
}
function workspacePatternToGlobPattern(workspace: string): string {
const { length } = workspace
if (!length) {
return ''
}
// If the workspace ends with "/"
if (workspace.charCodeAt(length - 1) === 47 /*'/'*/) {
return `${workspace}/*/package.json`
}
// If the workspace ends with "/**"
if (
workspace.charCodeAt(length - 1) === 42 /*'*'*/ &&
workspace.charCodeAt(length - 2) === 42 /*'*'*/ &&
workspace.charCodeAt(length - 3) === 47 /*'/'*/
) {
return `${workspace}/*/**/package.json`
}
// Things like "packages/a" or "packages/*"
return `${workspace}/package.json`
}
export function filterBySupportedScanFiles(
filepaths: string[] | readonly string[],
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
): string[] {
const patterns = getSupportedFilePatterns(supportedFiles)
return filepaths.filter(p => micromatch.some(p, patterns, { dot: true }))
}
export function createSupportedFilesFilter(
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
): (filepath: string) => boolean {
const patterns = getSupportedFilePatterns(supportedFiles)
return (filepath: string) =>
micromatch.some(filepath, patterns, { dot: true })
}
export function getSupportedFilePatterns(
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
): string[] {
const patterns: string[] = []
for (const key of Object.keys(supportedFiles)) {
const supported = supportedFiles[key]
if (supported) {
patterns.push(...Object.values(supported).map(p => `**/${p.pattern}`))
}
}
return patterns
}
type GlobWithGitIgnoreOptions = GlobOptions & {
// Optional filter function to apply during streaming.
// When provided, only files passing this filter are accumulated.
// This is critical for memory efficiency when scanning large monorepos.
filter?: ((filepath: string) => boolean) | undefined
socketConfig?: SocketYml | undefined
}
export async function globWithGitIgnore(
patterns: string[] | readonly string[],
options: GlobWithGitIgnoreOptions,
): Promise<string[]> {
const {
cwd = process.cwd(),
filter,
socketConfig,
...additionalOptions
} = { __proto__: null, ...options } as GlobWithGitIgnoreOptions
const ignores = new Set<string>(IGNORED_DIR_PATTERNS)
const projectIgnorePaths = socketConfig?.projectIgnorePaths
if (Array.isArray(projectIgnorePaths)) {
const ignorePatterns = ignoreFileLinesToGlobPatterns(
projectIgnorePaths,
path.join(cwd, '.gitignore'),
cwd,
)
for (const pattern of ignorePatterns) {
ignores.add(pattern)
}
}
const gitIgnoreStream = fastGlob.globStream(['**/.gitignore'], {
absolute: true,
cwd,
dot: true,
ignore: DEFAULT_IGNORE_FOR_GIT_IGNORE,
}) as AsyncIterable<string>
for await (const ignorePatterns of transform(
gitIgnoreStream,
async (filepath: string) =>
ignoreFileToGlobPatterns(
(await safeReadFile(filepath, { encoding: 'utf8' })) ?? '',
filepath,
cwd,
),
{ concurrency: 8 },
)) {
for (const p of ignorePatterns) {
ignores.add(p)
}
}
let hasNegatedPattern = false
for (const p of ignores) {
if (p.charCodeAt(0) === 33 /*'!'*/) {
hasNegatedPattern = true
break
}
}
const globOptions = {
__proto__: null,
absolute: true,
cwd,
dot: true,
ignore: hasNegatedPattern ? [...defaultIgnore] : [...ignores],
...additionalOptions,
} as GlobOptions
// When no filter is provided and no negated patterns exist, use the fast path.
if (!hasNegatedPattern && !filter) {
return await fastGlob.glob(patterns as string[], globOptions)
}
// Add support for negated "ignore" patterns which many globbing libraries,
// including 'fast-glob', 'globby', and 'tinyglobby', lack support for.
// Use streaming to avoid unbounded memory accumulation.
// This is critical for large monorepos with 100k+ files.
const results: string[] = []
const ig = hasNegatedPattern ? ignore().add([...ignores]) : null
const stream = fastGlob.globStream(
patterns as string[],
globOptions,
) as AsyncIterable<string>
for await (const p of stream) {
// Check gitignore patterns with negation support.
if (ig) {
// Note: the input files must be INSIDE the cwd. If you get strange looking
// relative path errors here, most likely your path is outside the given cwd.
const relPath = globOptions.absolute ? path.relative(cwd, p) : p
if (ig.ignores(relPath)) {
continue
}
}
// Apply the optional filter to reduce memory usage.
// When scanning large monorepos, this filters early (e.g., to manifest files only)
// instead of accumulating all 100k+ files and filtering later.
if (filter && !filter(p)) {
continue
}
results.push(p)
}
return results
}
export async function globWorkspace(
agent: Agent,
cwd = process.cwd(),
): Promise<string[]> {
const workspaceGlobs = await getWorkspaceGlobs(agent, cwd)
return workspaceGlobs.length
? await fastGlob.glob(workspaceGlobs, {
absolute: true,
cwd,
dot: true,
ignore: [...defaultIgnore],
})
: []
}
export function isReportSupportedFile(
filepath: string,
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
) {
const patterns = getSupportedFilePatterns(supportedFiles)
return micromatch.some(filepath, patterns, { dot: true })
}
export function pathsToGlobPatterns(
paths: string[] | readonly string[],
cwd?: string | undefined,
): string[] {
return paths.map(p => {
// Convert current directory references to glob patterns.
if (p === '.' || p === './') {
return '**/*'
}
// Expand tilde to home directory.
let resolvedPath = p
if (p.startsWith('~/')) {
resolvedPath = path.join(homePath, p.slice(2))
} else if (p === '~') {
resolvedPath = homePath
}
const absolutePath = path.isAbsolute(resolvedPath)
? resolvedPath
: path.resolve(cwd ?? process.cwd(), resolvedPath)
// If the path is a directory, scan it recursively for all files.
if (isDirSync(absolutePath)) {
return `${resolvedPath}/**/*`
}
return resolvedPath
})
}