@holz/pattern-filter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/holz-pattern-filter.js +19 -0
- package/dist/holz-pattern-filter.umd.cjs +1 -0
- package/package.json +49 -0
- package/src/__tests__/pattern-filter.test.ts +42 -0
- package/src/__tests__/string-match.test.ts +56 -0
- package/src/index.ts +1 -0
- package/src/pattern-filter.ts +43 -0
- package/src/string-match.ts +42 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const o = (s) => s.split(/[\s,]+/).filter(Boolean).reduce(
|
|
2
|
+
(t, r) => (r[0] === "-" ? t.exclude.push(c(r.slice(1))) : t.include.push(c(r)), t),
|
|
3
|
+
{ include: [], exclude: [] }
|
|
4
|
+
), n = (s, e) => s.exclude.every((t) => !t.test(e)) && s.include.some((t) => t.test(e)), c = (s) => {
|
|
5
|
+
const e = s.replace(/\*/g, ".*?");
|
|
6
|
+
return new RegExp(`^${e}$`);
|
|
7
|
+
};
|
|
8
|
+
class i {
|
|
9
|
+
constructor(e) {
|
|
10
|
+
this.pattern = e.pattern, this.processor = e.processor, this.filters = o(e.pattern);
|
|
11
|
+
}
|
|
12
|
+
processLog(e) {
|
|
13
|
+
n(this.filters, e.origin.join(":")) && this.processor.processLog(e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
i as PatternFilter,
|
|
18
|
+
i as default
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(t,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(t=typeof globalThis<"u"?globalThis:t||self,n(t["holz-pattern-filter"]={}))})(this,function(t){"use strict";const n=s=>s.split(/[\s,]+/).filter(Boolean).reduce((r,i)=>(i[0]==="-"?r.exclude.push(o(i.slice(1))):r.include.push(o(i)),r),{include:[],exclude:[]}),l=(s,e)=>s.exclude.every(r=>!r.test(e))&&s.include.some(r=>r.test(e)),o=s=>{const e=s.replace(/\*/g,".*?");return new RegExp(`^${e}$`)};class c{constructor(e){this.pattern=e.pattern,this.processor=e.processor,this.filters=n(e.pattern)}processLog(e){l(this.filters,e.origin.join(":"))&&this.processor.processLog(e)}}t.PatternFilter=c,t.default=c,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@holz/pattern-filter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Only print log messages that match a pattern",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/holz-pattern-filter.umd.cjs",
|
|
7
|
+
"module": "./dist/holz-pattern-filter.js",
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/PsychoLlama/holz",
|
|
12
|
+
"directory": "packages/holz-pattern-filter"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"require": "./dist/holz-pattern-filter.umd.cjs",
|
|
17
|
+
"import": "./dist/holz-pattern-filter.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"author": "Jesse Gibson",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"src"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"holz",
|
|
32
|
+
"logger",
|
|
33
|
+
"filter",
|
|
34
|
+
"pattern",
|
|
35
|
+
"exclude"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"prepare": "vite build",
|
|
39
|
+
"test:unit": "vitest --color --passWithNoTests",
|
|
40
|
+
"test:types": "tsc"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@holz/core": "0.1.0",
|
|
44
|
+
"@vitest/coverage-c8": "0.28.5",
|
|
45
|
+
"typescript": "4.9.5",
|
|
46
|
+
"vite": "^4.0.0",
|
|
47
|
+
"vitest": "^0.28.5"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { LogProcessor } from '@holz/core';
|
|
2
|
+
import { createLogger } from '@holz/core';
|
|
3
|
+
import PatternFilter from '../pattern-filter';
|
|
4
|
+
|
|
5
|
+
class TestBackend implements LogProcessor {
|
|
6
|
+
processLog = vi.fn();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('PatternFilter', () => {
|
|
10
|
+
it('only passes through logs that have the filter', () => {
|
|
11
|
+
const backend = new TestBackend();
|
|
12
|
+
const filter = new PatternFilter({
|
|
13
|
+
pattern: '-ignored, -*:wild, *',
|
|
14
|
+
processor: backend,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const logger = createLogger(filter);
|
|
18
|
+
|
|
19
|
+
logger.namespace('ignored').info('excluded by filter');
|
|
20
|
+
expect(backend.processLog).not.toHaveBeenCalled();
|
|
21
|
+
|
|
22
|
+
logger.namespace('something').namespace('wild').info('also excluded');
|
|
23
|
+
expect(backend.processLog).not.toHaveBeenCalled();
|
|
24
|
+
|
|
25
|
+
logger.namespace('app').info('not ignored');
|
|
26
|
+
expect(backend.processLog).toHaveBeenCalledWith(
|
|
27
|
+
expect.objectContaining({
|
|
28
|
+
message: 'not ignored',
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('does not send anything by default', () => {
|
|
34
|
+
const backend = new TestBackend();
|
|
35
|
+
const filter = new PatternFilter({ pattern: '', processor: backend });
|
|
36
|
+
const logger = createLogger(filter);
|
|
37
|
+
|
|
38
|
+
logger.info('no include patterns mean this is ignored');
|
|
39
|
+
|
|
40
|
+
expect(backend.processLog).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { parse, matches } from '../string-match';
|
|
2
|
+
|
|
3
|
+
describe('string-match', () => {
|
|
4
|
+
describe('parse', () => {
|
|
5
|
+
it('parses include patterns', () => {
|
|
6
|
+
expect(parse(' foo,bar baz , qux ')).toEqual({
|
|
7
|
+
include: [/^foo$/, /^bar$/, /^baz$/, /^qux$/],
|
|
8
|
+
exclude: [],
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('detects and groups exclusion patterns', () => {
|
|
13
|
+
expect(parse('first,-second,-third,fourth')).toEqual({
|
|
14
|
+
include: [/^first$/, /^fourth$/],
|
|
15
|
+
exclude: [/^second$/, /^third$/],
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('converts star patterns to regex wildcards', () => {
|
|
20
|
+
expect(parse('first,second*,split*part')).toEqual({
|
|
21
|
+
include: [/^first$/, /^second.*?$/, /^split.*?part$/],
|
|
22
|
+
exclude: [],
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('matches', () => {
|
|
28
|
+
it('returns true if the pattern matches', () => {
|
|
29
|
+
expect(matches(parse('hello'), 'hello')).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns true if the pattern matches a wildcard', () => {
|
|
33
|
+
expect(matches(parse('hello*'), 'hello world')).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('returns false if the pattern does not match', () => {
|
|
37
|
+
expect(matches(parse('hello'), 'bar')).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('returns false if the pattern is excluded', () => {
|
|
41
|
+
expect(matches(parse('-hello'), 'hello')).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('returns true if the pattern is included', () => {
|
|
45
|
+
expect(matches(parse('hello'), 'hello')).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('does not match if there is a contradiction', () => {
|
|
49
|
+
expect(matches(parse('maybe,-maybe'), 'maybe')).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('does not match if the filter is empty', () => {
|
|
53
|
+
expect(matches(parse(''), 'content')).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default, default as PatternFilter } from './pattern-filter';
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Log, LogProcessor } from '@holz/core';
|
|
2
|
+
import { parse, matches } from './string-match';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Only forwards logs who's origin matches the given pattern. It works the
|
|
6
|
+
* same as the popular `debug` module.
|
|
7
|
+
*
|
|
8
|
+
* Patterns allow wildcards and negation. For example, the pattern `my-app*`
|
|
9
|
+
* will match all logs with the origin `my-app` or `my-app:foo` or
|
|
10
|
+
* `my-app:foo:bar`. The pattern `*, -my-app` will match all logs except those
|
|
11
|
+
* with the origin `my-app`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* new PatternFilter({
|
|
15
|
+
* pattern: 'my-app*, -spammy:library',
|
|
16
|
+
* processor: backend,
|
|
17
|
+
* })
|
|
18
|
+
*/
|
|
19
|
+
export default class PatternFilter implements LogProcessor {
|
|
20
|
+
private filters: ReturnType<typeof parse>;
|
|
21
|
+
readonly pattern: string;
|
|
22
|
+
private processor: LogProcessor;
|
|
23
|
+
|
|
24
|
+
constructor(config: Config) {
|
|
25
|
+
this.pattern = config.pattern;
|
|
26
|
+
this.processor = config.processor;
|
|
27
|
+
this.filters = parse(config.pattern);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
processLog(log: Log) {
|
|
31
|
+
if (matches(this.filters, log.origin.join(':'))) {
|
|
32
|
+
this.processor.processLog(log);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Config {
|
|
38
|
+
/** A set of patterns to test against `log.origin`. */
|
|
39
|
+
pattern: string;
|
|
40
|
+
|
|
41
|
+
/** Where to send logs if they pass the filter. */
|
|
42
|
+
processor: LogProcessor;
|
|
43
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a string of multiple patterns into a set of regular expressions. We
|
|
3
|
+
* use a pattern string (instead of regexes) because it's easier to persist in
|
|
4
|
+
* storage or specify through a CLI.
|
|
5
|
+
*
|
|
6
|
+
* Special thanks to `debug-js` for the inspiration. I read their code as
|
|
7
|
+
* a guide for this implementation.
|
|
8
|
+
*/
|
|
9
|
+
export const parse = (input: string): PatternFilters => {
|
|
10
|
+
const patterns = input.split(/[\s,]+/).filter(Boolean);
|
|
11
|
+
|
|
12
|
+
return patterns.reduce<PatternFilters>(
|
|
13
|
+
(groups, pattern) => {
|
|
14
|
+
if (pattern[0] === '-') {
|
|
15
|
+
groups.exclude.push(toRegex(pattern.slice(1)));
|
|
16
|
+
} else {
|
|
17
|
+
groups.include.push(toRegex(pattern));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return groups;
|
|
21
|
+
},
|
|
22
|
+
{ include: [], exclude: [] }
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Detect if a string matches a set of patterns. */
|
|
27
|
+
export const matches = (filters: PatternFilters, input: string): boolean => {
|
|
28
|
+
return (
|
|
29
|
+
filters.exclude.every((pattern) => !pattern.test(input)) &&
|
|
30
|
+
filters.include.some((pattern) => pattern.test(input))
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const toRegex = (pattern: string): RegExp => {
|
|
35
|
+
const regex = pattern.replace(/\*/g, '.*?');
|
|
36
|
+
return new RegExp(`^${regex}$`);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
interface PatternFilters {
|
|
40
|
+
include: Array<RegExp>;
|
|
41
|
+
exclude: Array<RegExp>;
|
|
42
|
+
}
|