-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathglob.mts
More file actions
320 lines (294 loc) · 10.3 KB
/
glob.mts
File metadata and controls
320 lines (294 loc) · 10.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
import fastGlob from 'fast-glob'
import ignore from 'ignore'
import micromatch from 'micromatch'
import os from 'node:os'
import path from 'node:path'
import { parse as yamlParse } from 'yaml'
import { NODE_MODULES } from '@socketsecurity/lib/constants/paths'
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 { PNPM } from '../../constants/agents.mjs'
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 => !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
if (agent === PNPM) {
const workspacePath = path.join(cwd, 'pnpm-workspace.yaml')
const yml = await safeReadFile(workspacePath)
if (yml) {
try {
const ymlStr = typeof yml === 'string' ? yml : yml.toString('utf8')
workspacePatterns = yamlParse(ymlStr)?.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 && 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))
}
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 & {
socketConfig?: SocketYml | undefined
}
export async function globWithGitIgnore(
patterns: string[] | readonly string[],
options: GlobWithGitIgnoreOptions,
): Promise<string[]> {
const {
cwd = process.cwd(),
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,
ignore: DEFAULT_IGNORE_FOR_GIT_IGNORE,
})
for await (const ignorePatterns of transform(
gitIgnoreStream as AsyncIterable<string>,
async (filepath: string) => {
const content = await safeReadFile(filepath)
const contentStr = content
? typeof content === 'string'
? content
: content.toString('utf8')
: ''
return ignoreFileToGlobPatterns(contentStr, 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: GlobOptions = {
absolute: true,
cwd,
dot: true,
ignore: hasNegatedPattern ? [...defaultIgnore] : [...ignores],
...additionalOptions,
}
if (!hasNegatedPattern) {
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.
const filtered: string[] = []
const ig = ignore().add([...ignores])
const stream = fastGlob.globStream(
patterns as string[],
globOptions,
) as AsyncIterable<string>
for await (const p of stream) {
// 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)) {
filtered.push(p)
}
}
return filtered
}
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,
ignore: [...defaultIgnore],
})
: []
}
export function isReportSupportedFile(
filepath: string,
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
) {
const patterns = getSupportedFilePatterns(supportedFiles)
return micromatch.some(filepath, patterns)
}
/**
* Expand tilde (~) to home directory.
*/
function expandTildePath(p: string): string {
if (p === '~' || p.startsWith('~/') || p.startsWith('~\\')) {
const homeDir = os.homedir()
return p === '~' ? homeDir : path.join(homeDir, p.slice(2))
}
return p
}
export function pathsToGlobPatterns(
paths: string[] | readonly string[],
): string[] {
return paths.map(p => {
// Expand tilde paths.
const expanded = expandTildePath(p)
// Convert current directory references to glob patterns.
if (expanded === '.' || expanded === './') {
return '**/*'
}
// If the path is a directory, scan it recursively for all files.
if (isDirSync(expanded)) {
return `${expanded}/**/*`
}
return expanded
})
}