示例
minimatch 10.1.1: Escape glob magic characters using backslash prefixing in default mode and character classes under windowsPathsNoEscape mode
已验证示例 — npm minimatch 10.1.1: Escape glob magic characters using backslash prefixing in default mode and character classes under windowsPathsNoEscape mode.…
sha256:b258b376e90297d1f57ecab615e49dc2bdf3f256078365f2bfe44a2c4e8ff87c
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 2
- 构建过它的签名密钥
- 2
声明的环境
node linux x64 node node npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
| node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
案例
HOW- 目标
- Escape glob magic characters using backslash prefixing in default mode and character classes under windowsPathsNoEscape mode
- 符号
-
- minimatch.escape
- 环境
- node
- 创建时间
- 2026-08-18T01:23:57Z
契约
- minimatch.escape wraps wildcard and grouping characters in single-character classes rather than prefixing backslashes when windowsPathsNoEscape is enabled, preserving backslash directory separators while leaving braces unescaped unless magicalBraces is set
- minimatch.escape escapes wildcards, brackets, parentheses, and backslashes with backslash prefixes in default POSIX mode
- minimatch.escape leaves extglob modifiers (+, @, !) unescaped while neutralizing extglobs by escaping their parentheses
- minimatch.escape escapes opening and closing braces only when magicalBraces is explicitly enabled
- minimatch.defaults preserves configured windowsPathsNoEscape and magicalBraces options across escape calls
- minimatch matches literal filenames containing wildcard characters when queried with the escaped pattern
文件
- NOTES.md
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
源代码
# Minimatch Escaping Mechanics
## Known Solutions Cache
`search_known_solution` returned a sample (`sha256:585ab2a8ea43909cb7e47145ba967133e7da688e33baa7e055319539c3675105`) that verified broader pattern matching and filtering alongside basic Windows escaping. This sample focuses directly on the behavior and options of `minimatch.escape`.
## Pinned Version
- `minimatch`: 10.1.1
## Observed Behavior
`minimatch.escape(pattern, options)` transforms glob pattern strings into literal match patterns according to configured options:
1. **Default Mode (`windowsPathsNoEscape: false`, `magicalBraces: false`)**:
- Escapes `?`, `*`, `(`, `)`, `[`, `]`, and `\` using backslash prefixes (`\\$&`).
- Does not escape braces `{}` or extglob prefix characters `+`, `@`, `!`. Escaping parentheses alone (e.g. `@(a|b)` -> `@\\(a|b\\)`) deactivates extglob evaluation without modifying non-extglob occurrences of `+` or `@`.
2. **Windows Path Mode (`windowsPathsNoEscape: true`)**:
- Encloses `?`, `*`, `(`, `)`, `[`, `]` inside single-character classes (`[$&]`, e.g. `*` becomes `[*]`, `[` becomes `[[]`).
- Crucially preserves backslash characters `\\` without escaping them, so they continue to serve as directory separators rather than escape characters.
3. **Magical Braces Mode (`magicalBraces: true`)**:
- Adds `{` and `}` to the escaped character set (`\\{` and `\\}` in default mode; `[{]` and `[}]` under `windowsPathsNoEscape`).
4. **Default Binding**:
- `minimatch.defaults({ windowsPathsNoEscape: true }).escape` binds option defaults into the exported function.
## Failure Mode for Naive Expectations
A common naive expectation is that `minimatch.escape` always uses backslash prefixes for all special characters including path separators. When handling Windows paths under `windowsPathsNoEscape: true`, backslashes are path separators, so backslash-prefixing would turn `dir\\*.txt` into `dir\\\\*.txt`, which fails silently during matching by escaping the path separator or corrupting the directory structure in pattern compilation.
{"case":{"believed":"minimatch.escape prefixes backslashes before all special characters including Windows path separators and braces","caseId":"case:sha256:9a4f821eda7dbb397126513b67047d0ec131569fb971d5af3a191651d225edda","contract":["minimatch.escape wraps wildcard and grouping characters in single-character classes rather than prefixing backslashes when windowsPathsNoEscape is enabled, preserving backslash directory separators while leaving braces unescaped unless magicalBraces is set","minimatch.escape escapes wildcards, brackets, parentheses, and backslashes with backslash prefixes in default POSIX mode","minimatch.escape leaves extglob modifiers (+, @, !) unescaped while neutralizing extglobs by escaping their parentheses","minimatch.escape escapes opening and closing braces only when magicalBraces is explicitly enabled","minimatch.defaults preserves configured windowsPathsNoEscape and magicalBraces options across escape calls","minimatch matches literal filenames containing wildcard characters when queried with the escaped pattern"],"goal":"Escape glob magic characters using backslash prefixing in default mode and character classes under windowsPathsNoEscape mode","kind":"HOW","packages":["pkg:npm/minimatch@10.1.1"],"schemaVersion":1,"symbols":["minimatch.escape"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/minimatch@10.1.1"],"schemaVersion":1,"symbols":["minimatch.escape"],"verifierAdapter":"node-typescript@1"}
{
"name": "minimatch-escape-contract",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "minimatch-escape-contract",
"version": "1.0.0",
"dependencies": {
"minimatch": "10.1.1"
}
},
"node_modules/@isaacs/balanced-match": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@isaacs/balanced-match/-/balanced-match-4.0.1.tgz",
"integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==",
"license": "MIT",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/@isaacs/brace-expansion": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/@isaacs/brace-expansion/-/brace-expansion-5.0.1.tgz",
"integrity": "sha512-WMz71T1JS624nWj2n2fnYAuPovhv7EUhk69R6i9dsVyzxt5eM3bjwvgk9L+APE1TRscGysAVMANkB0jh0LQZrQ==",
"license": "MIT",
"dependencies": {
"@isaacs/balanced-match": "^4.0.1"
},
"engines": {
"node": "20 || >=22"
}
},
"node_modules/minimatch": {
"version": "10.1.1",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz",
"integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==",
"license": "BlueOak-1.0.0",
"dependencies": {
"@isaacs/brace-expansion": "^5.0.0"
},
"engines": {
"node": "20 || >=22"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
}
}
}
{
"name": "minimatch-escape-contract",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"minimatch": "10.1.1"
}
}
import { minimatch, escape } from 'minimatch';
export function escapeGlob(pattern, options = {}) {
return escape(pattern, options);
}
export function escapeGlobWindows(pattern, options = {}) {
return escape(pattern, { windowsPathsNoEscape: true, ...options });
}
export function escapeGlobWithBraces(pattern, options = {}) {
return escape(pattern, { magicalBraces: true, ...options });
}
export function createEscapedMatcher(pattern, options = {}) {
const escapedPattern = escape(pattern, options);
return (filename) => minimatch(filename, escapedPattern, options);
}
import { strict as assert } from 'node:assert';
import { minimatch, escape } from 'minimatch';
import {
escapeGlob,
escapeGlobWindows,
escapeGlobWithBraces,
createEscapedMatcher
} from '../src/index.mjs';
// Symbol export verification
assert.equal(typeof minimatch.escape, 'function');
assert.equal(minimatch.escape, escape);
// Windows path escaping: character class enclosure of wildcards without escaping backslashes
const winPathWithWildcard = 'dir\\*.txt';
const winEscaped = escapeGlobWindows(winPathWithWildcard);
assert.equal(winEscaped, 'dir\\[*].txt');
const winPathWithClass = 'data\\item[1].log';
assert.equal(escapeGlobWindows(winPathWithClass), 'data\\item[[]1[]].log');
const winPathWithExtglob = 'path\\sub\\@(alpha|beta)';
assert.equal(escapeGlobWindows(winPathWithExtglob), 'path\\sub\\@[(]alpha|beta[)]');
const winPathWithBraces = 'path\\{a,b}';
assert.equal(escapeGlobWindows(winPathWithBraces), 'path\\{a,b}');
assert.equal(escapeGlobWindows(winPathWithBraces, { magicalBraces: true }), 'path\\[{]a,b[}]');
// POSIX default escaping: backslash prefixing for wildcards, brackets, parentheses, and backslashes
assert.equal(escapeGlob('file[1]*.js'), 'file\\[1\\]\\*.js');
assert.equal(escapeGlob('path\\to\\file'), 'path\\\\to\\\\file');
assert.equal(escapeGlob('alpha?beta*gamma'), 'alpha\\?beta\\*gamma');
// Extglob modifiers: extglob prefixes are kept as literals while parentheses are escaped to deactivate expansion
assert.equal(escapeGlob('+(a|b)'), '+\\(a|b\\)');
assert.equal(escapeGlob('@(a|b)'), '@\\(a|b\\)');
assert.equal(escapeGlob('!(a|b)'), '!\\(a|b\\)');
assert.equal(escapeGlob('?(a|b)'), '\\?\\(a|b\\)');
assert.equal(escapeGlob('*(a|b)'), '\\*\\(a|b\\)');
// Brace expansion escaping: braces remain unescaped unless magicalBraces is explicitly enabled
assert.equal(escapeGlob('{a,b}'), '{a,b}');
assert.equal(escapeGlobWithBraces('{a,b}'), '\\{a,b\\}');
assert.equal(escapeGlobWithBraces('prefix_{0..9}_suffix'), 'prefix_\\{0..9\\}_suffix');
// minimatch.defaults binds preset options to escape
const configuredWin = minimatch.defaults({ windowsPathsNoEscape: true });
assert.equal(configuredWin.escape('logs\\*.*'), 'logs\\[*].[*]');
const configuredMagical = minimatch.defaults({ magicalBraces: true });
assert.equal(configuredMagical.escape('{lib,dist}/*.mjs'), '\\{lib,dist\\}/\\*.mjs');
// Escaped pattern matching behaves as strict literal matcher
const literalTarget = 'archive-[2024]-(v1)*.tar.gz';
const matcher = createEscapedMatcher(literalTarget);
assert.equal(matcher(literalTarget), true);
assert.equal(matcher('archive-2-(v1)extra.tar.gz'), false);
assert.equal(minimatch('archive-2-(v1)extra.tar.gz', literalTarget), true);
console.log('CONTRACT PASS: minimatch.escape verified');