@holz/env-filter 0.7.0-rc.2 → 0.8.2-rc.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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +8 -8
- package/src/__tests__/environment-filter.test.ts +0 -94
- package/src/__tests__/get-env.test.ts +0 -68
- package/src/environment-filter.ts +0 -45
- package/src/get-env.ts +0 -16
- package/src/index.ts +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Jesse Gibson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ import { createLogger } from '@holz/core';
|
|
|
11
11
|
createLogger(
|
|
12
12
|
createEnvironmentFilter({
|
|
13
13
|
processor: plugin,
|
|
14
|
-
})
|
|
14
|
+
}),
|
|
15
15
|
);
|
|
16
16
|
```
|
|
17
17
|
|
|
@@ -55,7 +55,7 @@ const logger = createLogger(
|
|
|
55
55
|
createEnvironmentFilter({
|
|
56
56
|
processor: yourProcessor,
|
|
57
57
|
defaultPattern: '',
|
|
58
|
-
})
|
|
58
|
+
}),
|
|
59
59
|
);
|
|
60
60
|
```
|
|
61
61
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/env-filter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.2-rc.0",
|
|
4
4
|
"description": "Filter logs based on the current environment.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -22,8 +22,7 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"sideEffects": false,
|
|
24
24
|
"files": [
|
|
25
|
-
"dist"
|
|
26
|
-
"src"
|
|
25
|
+
"dist"
|
|
27
26
|
],
|
|
28
27
|
"keywords": [
|
|
29
28
|
"holz",
|
|
@@ -38,19 +37,20 @@
|
|
|
38
37
|
"test:types": "tsc"
|
|
39
38
|
},
|
|
40
39
|
"dependencies": {
|
|
41
|
-
"@holz/pattern-filter": "^0.
|
|
40
|
+
"@holz/pattern-filter": "^0.8.2-rc.0"
|
|
42
41
|
},
|
|
43
42
|
"peerDependencies": {
|
|
44
|
-
"@holz/core": "0.
|
|
43
|
+
"@holz/core": "^0.8.0"
|
|
45
44
|
},
|
|
46
45
|
"devDependencies": {
|
|
47
|
-
"@holz/core": "^0.
|
|
46
|
+
"@holz/core": "^0.8.2-rc.0",
|
|
48
47
|
"@types/node": "^22.0.0",
|
|
49
48
|
"@vitest/coverage-v8": "^3.0.8",
|
|
49
|
+
"typescript": "^5.8.2",
|
|
50
50
|
"vite": "^6.0.0",
|
|
51
51
|
"vite-plugin-dts": "^4.5.3",
|
|
52
52
|
"vite-tsconfig-paths": "^5.1.4",
|
|
53
53
|
"vitest": "^3.0.8"
|
|
54
54
|
},
|
|
55
|
-
"
|
|
56
|
-
}
|
|
55
|
+
"gitHead": "59a13334498d9c3f0952235ed6cb59e493558824"
|
|
56
|
+
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { createLogger } from '@holz/core';
|
|
2
|
-
import { createEnvironmentFilter } from '../environment-filter';
|
|
3
|
-
import { getLocalStorageKey, getEnvironmentVariable } from '../get-env';
|
|
4
|
-
|
|
5
|
-
vi.mock('../get-env');
|
|
6
|
-
|
|
7
|
-
describe('EnvironmentFilter', () => {
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
vi.clearAllMocks();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('uses the given pattern as an override', () => {
|
|
13
|
-
const backend = vi.fn();
|
|
14
|
-
const filter = createEnvironmentFilter({
|
|
15
|
-
pattern: '-ignored, -*:wild, *',
|
|
16
|
-
processor: backend,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const logger = createLogger(filter);
|
|
20
|
-
|
|
21
|
-
logger.namespace('ignored').info('excluded by filter');
|
|
22
|
-
expect(backend).not.toHaveBeenCalled();
|
|
23
|
-
|
|
24
|
-
logger.namespace('something').namespace('wild').info('also excluded');
|
|
25
|
-
expect(backend).not.toHaveBeenCalled();
|
|
26
|
-
|
|
27
|
-
logger.namespace('app').info('not ignored');
|
|
28
|
-
expect(backend).toHaveBeenCalledWith(
|
|
29
|
-
expect.objectContaining({
|
|
30
|
-
message: 'not ignored',
|
|
31
|
-
}),
|
|
32
|
-
);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('uses the fallback pattern if none exist in the environment', () => {
|
|
36
|
-
const backend = vi.fn();
|
|
37
|
-
const filter = createEnvironmentFilter({
|
|
38
|
-
pattern: undefined,
|
|
39
|
-
defaultPattern: '*, -ignored',
|
|
40
|
-
processor: backend,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const logger = createLogger(filter);
|
|
44
|
-
|
|
45
|
-
logger.namespace('ignored').info('excluded by filter');
|
|
46
|
-
expect(backend).not.toHaveBeenCalled();
|
|
47
|
-
|
|
48
|
-
logger.info('not ignored');
|
|
49
|
-
expect(backend).toHaveBeenCalledWith(
|
|
50
|
-
expect.objectContaining({ message: 'not ignored' }),
|
|
51
|
-
);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('prints everything by default', () => {
|
|
55
|
-
const backend = vi.fn();
|
|
56
|
-
const filter = createEnvironmentFilter({
|
|
57
|
-
pattern: undefined,
|
|
58
|
-
processor: backend,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
const logger = createLogger(filter);
|
|
62
|
-
|
|
63
|
-
logger.info('not excluded');
|
|
64
|
-
expect(backend).toHaveBeenCalledWith(
|
|
65
|
-
expect.objectContaining({ message: 'not excluded' }),
|
|
66
|
-
);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('allows you to choose a different localStorage key', () => {
|
|
70
|
-
const backend = vi.fn();
|
|
71
|
-
const filter = createEnvironmentFilter({
|
|
72
|
-
processor: backend,
|
|
73
|
-
localStorageKey: 'override',
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const logger = createLogger(filter);
|
|
77
|
-
logger.info('just print something');
|
|
78
|
-
|
|
79
|
-
expect(getLocalStorageKey).toHaveBeenCalledWith('override');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('allows you to choose a different environment variable', () => {
|
|
83
|
-
const backend = vi.fn();
|
|
84
|
-
const filter = createEnvironmentFilter({
|
|
85
|
-
processor: backend,
|
|
86
|
-
environmentVariable: 'OVERRIDE',
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const logger = createLogger(filter);
|
|
90
|
-
logger.info('just print something');
|
|
91
|
-
|
|
92
|
-
expect(getEnvironmentVariable).toHaveBeenCalledWith('OVERRIDE');
|
|
93
|
-
});
|
|
94
|
-
});
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import * as env from '../get-env';
|
|
2
|
-
|
|
3
|
-
class MockLocalStorage {
|
|
4
|
-
private store = new Map<string, string>();
|
|
5
|
-
|
|
6
|
-
getItem = vi.fn((key: string) => this.store.get(key) ?? null);
|
|
7
|
-
setItem = vi.fn((key: string, value: string) => {
|
|
8
|
-
this.store.set(key, value);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
describe('environment', () => {
|
|
13
|
-
describe('getLocalStorageKey', () => {
|
|
14
|
-
const STORAGE_KEY = 'test-key';
|
|
15
|
-
|
|
16
|
-
function setup() {
|
|
17
|
-
const localStorage = new MockLocalStorage();
|
|
18
|
-
|
|
19
|
-
Object.assign(globalThis, { localStorage });
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
localStorage,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
Object.assign(globalThis, { localStorage: undefined });
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('getLocalStorageKey', () => {
|
|
31
|
-
it('returns the value stored in localStorage, or undefined', () => {
|
|
32
|
-
const { localStorage } = setup();
|
|
33
|
-
|
|
34
|
-
expect(env.getLocalStorageKey(STORAGE_KEY)).toBeUndefined();
|
|
35
|
-
localStorage.setItem(STORAGE_KEY, 'yolo');
|
|
36
|
-
expect(env.getLocalStorageKey(STORAGE_KEY)).toBe('yolo');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('survives if the localStorage lookup fails', () => {
|
|
40
|
-
const { localStorage } = setup();
|
|
41
|
-
|
|
42
|
-
localStorage.getItem.mockImplementation(() => {
|
|
43
|
-
throw new Error('Simulating localStorage DOM exception');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
expect(env.getLocalStorageKey(STORAGE_KEY)).toBeUndefined();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('survives if localStorage is not available', () => {
|
|
50
|
-
expect(env.getLocalStorageKey(STORAGE_KEY)).toBeUndefined();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('getEnvVariable', () => {
|
|
56
|
-
const ENV_KEY = 'HOLZ_DEBUG_TEST_PATTERN';
|
|
57
|
-
|
|
58
|
-
beforeEach(() => {
|
|
59
|
-
delete process.env[ENV_KEY];
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('returns the value stored in process.env', () => {
|
|
63
|
-
expect(env.getEnvironmentVariable(ENV_KEY)).toBeUndefined();
|
|
64
|
-
process.env[ENV_KEY] = '-stuff:*';
|
|
65
|
-
expect(env.getEnvironmentVariable(ENV_KEY)).toBe('-stuff:*');
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { LogProcessor } from '@holz/core';
|
|
2
|
-
import { createPatternFilter } from '@holz/pattern-filter';
|
|
3
|
-
import { getEnvironmentVariable, getLocalStorageKey } from './get-env';
|
|
4
|
-
|
|
5
|
-
export const createEnvironmentFilter = ({
|
|
6
|
-
processor,
|
|
7
|
-
pattern,
|
|
8
|
-
defaultPattern,
|
|
9
|
-
localStorageKey = 'debug',
|
|
10
|
-
environmentVariable = 'DEBUG',
|
|
11
|
-
}: Config): LogProcessor => {
|
|
12
|
-
/**
|
|
13
|
-
* Load from localStorage falling back to `process.env`. We always check
|
|
14
|
-
* both because we might be in Electron, where both are defined and equally
|
|
15
|
-
* valid.
|
|
16
|
-
*/
|
|
17
|
-
const loadPatternFromEnvironment = () => {
|
|
18
|
-
return (
|
|
19
|
-
getLocalStorageKey(localStorageKey) ??
|
|
20
|
-
getEnvironmentVariable(environmentVariable)
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
return createPatternFilter({
|
|
25
|
-
processor,
|
|
26
|
-
pattern: pattern ?? loadPatternFromEnvironment() ?? defaultPattern ?? '*',
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
interface Config {
|
|
31
|
-
/** Replace the detected pattern with something else. */
|
|
32
|
-
pattern?: string;
|
|
33
|
-
|
|
34
|
-
/** The default pattern to use if no pattern is provided. */
|
|
35
|
-
defaultPattern?: string;
|
|
36
|
-
|
|
37
|
-
/** Where to send the logs if they pass the filter. */
|
|
38
|
-
processor: LogProcessor;
|
|
39
|
-
|
|
40
|
-
/** The localStorage key where patterns are saved. Default is `debug`. */
|
|
41
|
-
localStorageKey?: string;
|
|
42
|
-
|
|
43
|
-
/** The environment variable where patterns are saved. Default is `DEBUG`. */
|
|
44
|
-
environmentVariable?: string;
|
|
45
|
-
}
|
package/src/get-env.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export const getEnvironmentVariable = (key: string): undefined | string => {
|
|
2
|
-
if (typeof process !== 'undefined' && 'env' in process) {
|
|
3
|
-
return process.env[key];
|
|
4
|
-
}
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export const getLocalStorageKey = (key: string): undefined | string => {
|
|
8
|
-
try {
|
|
9
|
-
if (typeof localStorage !== 'undefined') {
|
|
10
|
-
return localStorage.getItem(key) ?? undefined;
|
|
11
|
-
}
|
|
12
|
-
} catch {
|
|
13
|
-
// Sometimes localStorage fails (e.g. in Safari private mode). That's
|
|
14
|
-
// okay. We'll just use a fallback pattern.
|
|
15
|
-
}
|
|
16
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createEnvironmentFilter } from './environment-filter';
|