Skip to content

Commit

Permalink
Merge pull request #3 from adobe-rnd/config-service
Browse files Browse the repository at this point in the history
fix: better globs
  • Loading branch information
maxakuru authored Jan 15, 2025
2 parents 351f6ec + 43b2133 commit 5adbd71
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
16 changes: 10 additions & 6 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{
"compilerOptions": {
"target": "ES6",
"module": "NodeNext",
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"checkJs": true,
"allowJs": true,
"baseUrl": ".",
"paths": {
"*": ["types/*"],
"@cloudflare/workers-types/*": ["node_modules/@cloudflare/workers-types/*"]
"*": [
"types/*"
],
"@cloudflare/workers-types/*": [
"node_modules/@cloudflare/workers-types/*"
]
}
},
"include": [
"./**/*.js",
"./src/types.d.ts"
"./src/types.d.ts"
],
"exclude": [
"node_modules",
"dist"
]
}
}
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import { errorWithResponse, ffetch } from './util.js';
import { errorWithResponse, ffetch, globToRegExp } from './util.js';

/** @type {'CONFIG_SERVICE' | 'STORAGE'} */
const SOURCE = 'CONFIG_SERVICE';
Expand All @@ -23,7 +23,7 @@ function findGlobMatch(patterns, path) {
return patterns
.sort((a, b) => b.length - a.length)
.map((pattern) => {
const re = new RegExp(`^${pattern.replace(/\*/g, '([^/]+)').replace('/**/', '(.*)')}$`);
const re = globToRegExp(pattern);
const match = path.match(re);
return match ? pattern : null;
})
Expand Down
12 changes: 12 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ export function errorWithResponse(status, xError, body = '') {
const error = new ResponseError(xError, response);
return error;
}

/**
* @param {string} glob
* @returns {RegExp}
*/
export function globToRegExp(glob) {
const reString = glob
.replaceAll('**', '|')
.replaceAll('*', '[0-9a-z-.]*')
.replaceAll('|', '.*');
return new RegExp(`^${reString}$`);
}
47 changes: 47 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import assert from 'node:assert';
import { globToRegExp } from '../src/util.js';

describe('util tests', () => {
describe('globToRegExp', () => {
it('should convert a glob pattern to a regular expression', () => {
const pattern = 'a*b/**';
const re = globToRegExp(pattern);
assert.ok(re instanceof RegExp);
assert.ok(re.test('a123b/456'));
assert.ok(re.test('a123b/456/789'));
assert.ok(re.test('a123b/456/789/abc'));
assert.ok(!re.test('a123b'));
assert.ok(!re.test('/a123b/456'));
});

it('should convert a glob pattern to a regular expression with a leading slash', () => {
const pattern = '/a*b/**';
const re = globToRegExp(pattern);
assert.ok(re instanceof RegExp);
assert.ok(re.test('/a123b/456'));
assert.ok(re.test('/a123b/456/789'));
assert.ok(re.test('/a123b/456/789/abc'));
assert.ok(!re.test('/a123b'));
});

it('should convert a glob pattern to a regular expression with a single asterisk', () => {
const pattern = 'a/*';
const re = globToRegExp(pattern);
assert.ok(re instanceof RegExp);
assert.ok(re.test('a/123'));
assert.ok(!re.test('a/123/456'));
});
});
});

0 comments on commit 5adbd71

Please sign in to comment.