Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ const sourceMatches = (source, requestPath, allowSegments) => {
let results = null;

if (allowSegments) {
const normalized = slashed.replace('*', '(.*)');
const expression = pathToRegExp(normalized, keys);
try {
const normalized = slashed.replace('*', '(.*)');
const expression = pathToRegExp(normalized, keys);

results = expression.exec(resolvedPath);
results = expression.exec(resolvedPath);

if (!results) {
// clear keys so that they are not used
// later with empty results. this may
// happen if minimatch returns true
if (!results) {
// clear keys so that they are not used
// later with empty results. this may
// happen if minimatch returns true
keys.length = 0;
}
} catch (_) {
// If path-to-regexp cannot parse the source pattern (e.g.
// extglob negation like `!(*.css|*.js)`), fall through to
// the minimatch check below.
keys.length = 0;
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@ test('set `rewrites` config property to path segment', async () => {
expect(json).toEqual(content);
});

test('set `rewrites` config property with extglob negation pattern', async () => {
const destination = '.dotfile';
const related = path.join(fixturesFull, destination);
const content = await fs.readFile(related, 'utf8');

const url = await getUrl({
rewrites: [{
source: '**/!(*.js|*.css)',
destination
}]
});

const response = await fetch(`${url}/face/delete`);
const text = await response.text();

expect(response.status).toBe(200);
expect(text).toBe(content);
});

test('set `redirects` config property to wildcard path', async () => {
const destination = 'testing';

Expand Down