@holz/env-filter 0.2.0 → 0.6.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/README.md +62 -0
- package/dist/holz-env-filter.cjs +1 -1
- package/dist/holz-env-filter.js +16 -50
- package/package.json +4 -4
- package/src/__tests__/environment-filter.test.ts +35 -37
- package/src/__tests__/get-env.test.ts +68 -0
- package/src/environment-filter.ts +28 -52
- package/src/get-env.ts +16 -0
- package/src/index.ts +1 -1
- package/src/__tests__/browser-env.test.ts +0 -61
- package/src/__tests__/environment.test.ts +0 -11
- package/src/browser-env.ts +0 -22
- package/src/environment.ts +0 -19
- package/src/server-env.ts +0 -9
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# `@holz/env-filter`
|
|
2
|
+
|
|
3
|
+
Builds on [`@holz/pattern-filter`](https://github.com/PsychoLlama/holz/tree/main/packages/holz-pattern-filter) to filter logs based on your current environment, loading the pattern from `process.env.DEBUG` or `localStorage.debug`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { createEnvironmentFilter } from '@holz/env-filter';
|
|
9
|
+
import { createLogger } from '@holz/core';
|
|
10
|
+
|
|
11
|
+
createLogger(
|
|
12
|
+
createEnvironmentFilter({
|
|
13
|
+
processor: plugin,
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Configuration Options
|
|
19
|
+
|
|
20
|
+
`createEnvironmentFilter()` accepts the following configuration options:
|
|
21
|
+
|
|
22
|
+
- `processor`: The log processor to use. Required.
|
|
23
|
+
- `pattern`: The pattern to use for filtering logs. If not provided, the pattern will be loaded from the environment.
|
|
24
|
+
- `defaultPattern`: The default pattern to use if no pattern is provided or detected in the environment. Default is `'*'` (all logs).
|
|
25
|
+
- `localStorageKey`: The name of the key used to store the pattern in localStorage. Default is `'debug'`.
|
|
26
|
+
- `environmentVariable`: The name of the environment variable used to store the pattern. Default is `'DEBUG'`.
|
|
27
|
+
|
|
28
|
+
Note that the `pattern` option will override any pattern detected in the environment.
|
|
29
|
+
|
|
30
|
+
## How it Works
|
|
31
|
+
|
|
32
|
+
`createEnvironmentFilter()` loads the pattern from the environment using the following process:
|
|
33
|
+
|
|
34
|
+
- It looks for a pattern stored in `localStorage` with the key specified in `localStorageKey`. If found, it uses that pattern.
|
|
35
|
+
- If `localStorage` is not available or doesn't contain a pattern with the specified key, it looks for a pattern stored in `process.env` specified in `environmentVariable`. If found, it uses that pattern.
|
|
36
|
+
- If no pattern is found in either `localStorage` or the environment variable, it uses the default pattern specified in `defaultPattern`.
|
|
37
|
+
|
|
38
|
+
Once the pattern is loaded, `createEnvironmentFilter()` uses `createPatternFilter()` from [`@holz/pattern-filter`](https://github.com/PsychoLlama/holz/tree/main/packages/holz-pattern-filter) to create a log processor that filters logs based on the loaded pattern.
|
|
39
|
+
|
|
40
|
+
## Example
|
|
41
|
+
|
|
42
|
+
Suppose you want to filter logs in development that come from your app, but disable all logs in production. You can use `@holz/env-filter` to accomplish this by setting the `DEBUG` environment variable in development:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
DEBUG=your-app*
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In production, you may want to disable all logging by default. This can be done by setting the default pattern to an empty string:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { createEnvironmentFilter } from '@holz/env-filter';
|
|
52
|
+
import { createLogger } from '@holz/core';
|
|
53
|
+
|
|
54
|
+
const logger = createLogger(
|
|
55
|
+
createEnvironmentFilter({
|
|
56
|
+
processor: yourProcessor,
|
|
57
|
+
defaultPattern: '',
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
With this configuration, logs will be disabled by default in production, and only logs originating from the `your-app` namespace will be shown in development.
|
package/dist/holz-env-filter.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@holz/pattern-filter"),c=e=>{if(typeof process<"u"&&"env"in process)return process.env[e]},l=e=>{try{if(typeof localStorage<"u")return localStorage.getItem(e)??void 0}catch{}},s=({processor:e,pattern:t,defaultPattern:r,localStorageKey:n="debug",environmentVariable:o="DEBUG"})=>{const a=()=>l(n)??c(o);return i.createPatternFilter({processor:e,pattern:t??a()??r??"*"})};exports.createEnvironmentFilter=s;
|
package/dist/holz-env-filter.js
CHANGED
|
@@ -1,57 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const
|
|
1
|
+
import { createPatternFilter as a } from "@holz/pattern-filter";
|
|
2
|
+
const c = (e) => {
|
|
3
|
+
if (typeof process < "u" && "env" in process)
|
|
4
|
+
return process.env[e];
|
|
5
|
+
}, i = (e) => {
|
|
3
6
|
try {
|
|
4
7
|
if (typeof localStorage < "u")
|
|
5
8
|
return localStorage.getItem(e) ?? void 0;
|
|
6
9
|
} catch {
|
|
7
10
|
}
|
|
8
|
-
},
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
class f {
|
|
20
|
-
constructor(r) {
|
|
21
|
-
this.env = c(globalThis.process), this.processor = r.processor, this.replaceFilter(
|
|
22
|
-
r.pattern ?? this.loadPattern() ?? r.defaultPattern ?? "*"
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
processLog(r) {
|
|
26
|
-
this.filter.processLog(r);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Change the pattern used to filter logs. If the environment supports it,
|
|
30
|
-
* it will attempt to persist your preference.
|
|
31
|
-
*/
|
|
32
|
-
setPattern(r) {
|
|
33
|
-
this.replaceFilter(r), this.env === t.Browser && a(o, r);
|
|
34
|
-
}
|
|
35
|
-
/** Return the current filter pattern. */
|
|
36
|
-
getPattern() {
|
|
37
|
-
return this.filter.pattern;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Load from localStorage falling back to `process.env`. We always check
|
|
41
|
-
* both because we might be in Electron, where both are defined and equally
|
|
42
|
-
* valid.
|
|
43
|
-
*/
|
|
44
|
-
loadPattern() {
|
|
45
|
-
return n(o) ?? i("DEBUG");
|
|
46
|
-
}
|
|
47
|
-
replaceFilter(r) {
|
|
48
|
-
this.filter = new s({
|
|
49
|
-
processor: this.processor,
|
|
50
|
-
pattern: r
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
11
|
+
}, m = ({
|
|
12
|
+
processor: e,
|
|
13
|
+
pattern: t,
|
|
14
|
+
defaultPattern: r,
|
|
15
|
+
localStorageKey: n = "debug",
|
|
16
|
+
environmentVariable: o = "DEBUG"
|
|
17
|
+
}) => a({
|
|
18
|
+
processor: e,
|
|
19
|
+
pattern: t ?? (() => i(n) ?? c(o))() ?? r ?? "*"
|
|
20
|
+
});
|
|
54
21
|
export {
|
|
55
|
-
|
|
56
|
-
f as default
|
|
22
|
+
m as createEnvironmentFilter
|
|
57
23
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/env-filter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Filter logs based on the current environment.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/holz-env-filter.cjs",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
"test:types": "tsc"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@holz/pattern-filter": "0.
|
|
43
|
+
"@holz/pattern-filter": "^0.6.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@holz/core": "0.
|
|
46
|
+
"@holz/core": "^0.6.0",
|
|
47
47
|
"@types/node": "^18.14.0",
|
|
48
|
-
"@vitest/coverage-c8": "0.28.5",
|
|
48
|
+
"@vitest/coverage-c8": "^0.28.5",
|
|
49
49
|
"vite": "^4.0.0",
|
|
50
50
|
"vitest": "^0.28.5"
|
|
51
51
|
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import type { LogProcessor } from '@holz/core';
|
|
2
1
|
import { createLogger } from '@holz/core';
|
|
3
|
-
import
|
|
2
|
+
import { createEnvironmentFilter } from '../environment-filter';
|
|
3
|
+
import { getLocalStorageKey, getEnvironmentVariable } from '../get-env';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
processLog = vi.fn();
|
|
7
|
-
}
|
|
5
|
+
vi.mock('../get-env');
|
|
8
6
|
|
|
9
7
|
describe('EnvironmentFilter', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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({
|
|
13
15
|
pattern: '-ignored, -*:wild, *',
|
|
14
16
|
processor: backend,
|
|
15
17
|
});
|
|
@@ -17,22 +19,22 @@ describe('EnvironmentFilter', () => {
|
|
|
17
19
|
const logger = createLogger(filter);
|
|
18
20
|
|
|
19
21
|
logger.namespace('ignored').info('excluded by filter');
|
|
20
|
-
expect(backend
|
|
22
|
+
expect(backend).not.toHaveBeenCalled();
|
|
21
23
|
|
|
22
24
|
logger.namespace('something').namespace('wild').info('also excluded');
|
|
23
|
-
expect(backend
|
|
25
|
+
expect(backend).not.toHaveBeenCalled();
|
|
24
26
|
|
|
25
27
|
logger.namespace('app').info('not ignored');
|
|
26
|
-
expect(backend
|
|
28
|
+
expect(backend).toHaveBeenCalledWith(
|
|
27
29
|
expect.objectContaining({
|
|
28
30
|
message: 'not ignored',
|
|
29
31
|
})
|
|
30
32
|
);
|
|
31
33
|
});
|
|
32
34
|
|
|
33
|
-
it('uses the fallback pattern if
|
|
34
|
-
const backend =
|
|
35
|
-
const filter =
|
|
35
|
+
it('uses the fallback pattern if none exist in the environment', () => {
|
|
36
|
+
const backend = vi.fn();
|
|
37
|
+
const filter = createEnvironmentFilter({
|
|
36
38
|
pattern: undefined,
|
|
37
39
|
defaultPattern: '*, -ignored',
|
|
38
40
|
processor: backend,
|
|
@@ -41,17 +43,17 @@ describe('EnvironmentFilter', () => {
|
|
|
41
43
|
const logger = createLogger(filter);
|
|
42
44
|
|
|
43
45
|
logger.namespace('ignored').info('excluded by filter');
|
|
44
|
-
expect(backend
|
|
46
|
+
expect(backend).not.toHaveBeenCalled();
|
|
45
47
|
|
|
46
48
|
logger.info('not ignored');
|
|
47
|
-
expect(backend
|
|
49
|
+
expect(backend).toHaveBeenCalledWith(
|
|
48
50
|
expect.objectContaining({ message: 'not ignored' })
|
|
49
51
|
);
|
|
50
52
|
});
|
|
51
53
|
|
|
52
54
|
it('prints everything by default', () => {
|
|
53
|
-
const backend =
|
|
54
|
-
const filter =
|
|
55
|
+
const backend = vi.fn();
|
|
56
|
+
const filter = createEnvironmentFilter({
|
|
55
57
|
pattern: undefined,
|
|
56
58
|
processor: backend,
|
|
57
59
|
});
|
|
@@ -59,38 +61,34 @@ describe('EnvironmentFilter', () => {
|
|
|
59
61
|
const logger = createLogger(filter);
|
|
60
62
|
|
|
61
63
|
logger.info('not excluded');
|
|
62
|
-
expect(backend
|
|
64
|
+
expect(backend).toHaveBeenCalledWith(
|
|
63
65
|
expect.objectContaining({ message: 'not excluded' })
|
|
64
66
|
);
|
|
65
67
|
});
|
|
66
68
|
|
|
67
|
-
it('
|
|
68
|
-
const backend =
|
|
69
|
-
const filter =
|
|
70
|
-
pattern: '',
|
|
69
|
+
it('allows you to choose a different localStorage key', () => {
|
|
70
|
+
const backend = vi.fn();
|
|
71
|
+
const filter = createEnvironmentFilter({
|
|
71
72
|
processor: backend,
|
|
73
|
+
localStorageKey: 'override',
|
|
72
74
|
});
|
|
73
75
|
|
|
74
76
|
const logger = createLogger(filter);
|
|
77
|
+
logger.info('just print something');
|
|
75
78
|
|
|
76
|
-
|
|
77
|
-
expect(backend.processLog).not.toHaveBeenCalled();
|
|
78
|
-
|
|
79
|
-
filter.setPattern('*');
|
|
80
|
-
logger.info('all logs printed');
|
|
81
|
-
expect(backend.processLog).toHaveBeenCalledWith(
|
|
82
|
-
expect.objectContaining({ message: 'all logs printed' })
|
|
83
|
-
);
|
|
79
|
+
expect(getLocalStorageKey).toHaveBeenCalledWith('override');
|
|
84
80
|
});
|
|
85
81
|
|
|
86
|
-
it('
|
|
87
|
-
const backend =
|
|
88
|
-
const filter =
|
|
89
|
-
pattern: undefined,
|
|
90
|
-
defaultPattern: '*, -ignored',
|
|
82
|
+
it('allows you to choose a different environment variable', () => {
|
|
83
|
+
const backend = vi.fn();
|
|
84
|
+
const filter = createEnvironmentFilter({
|
|
91
85
|
processor: backend,
|
|
86
|
+
environmentVariable: 'OVERRIDE',
|
|
92
87
|
});
|
|
93
88
|
|
|
94
|
-
|
|
89
|
+
const logger = createLogger(filter);
|
|
90
|
+
logger.info('just print something');
|
|
91
|
+
|
|
92
|
+
expect(getEnvironmentVariable).toHaveBeenCalledWith('OVERRIDE');
|
|
95
93
|
});
|
|
96
94
|
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as env from '../get-env';
|
|
2
|
+
|
|
3
|
+
class MockLocalStorage {
|
|
4
|
+
private store = new Map();
|
|
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,61 +1,31 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
private filter!: PatternFilter;
|
|
13
|
-
private env = detectEnvironment(globalThis.process);
|
|
14
|
-
|
|
15
|
-
constructor(options: Config) {
|
|
16
|
-
this.processor = options.processor;
|
|
17
|
-
this.replaceFilter(
|
|
18
|
-
options.pattern ?? this.loadPattern() ?? options.defaultPattern ?? '*'
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
processLog(log: Log) {
|
|
23
|
-
this.filter.processLog(log);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Change the pattern used to filter logs. If the environment supports it,
|
|
28
|
-
* it will attempt to persist your preference.
|
|
29
|
-
*/
|
|
30
|
-
setPattern(pattern: string) {
|
|
31
|
-
this.replaceFilter(pattern);
|
|
32
|
-
|
|
33
|
-
if (this.env === Environment.Browser) {
|
|
34
|
-
browserEnv.save(browserEnv.STORAGE_KEY, pattern);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Return the current filter pattern. */
|
|
39
|
-
getPattern() {
|
|
40
|
-
return this.filter.pattern;
|
|
41
|
-
}
|
|
42
|
-
|
|
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 => {
|
|
43
12
|
/**
|
|
44
13
|
* Load from localStorage falling back to `process.env`. We always check
|
|
45
14
|
* both because we might be in Electron, where both are defined and equally
|
|
46
15
|
* valid.
|
|
47
16
|
*/
|
|
48
|
-
|
|
49
|
-
return
|
|
50
|
-
|
|
17
|
+
const loadPatternFromEnvironment = () => {
|
|
18
|
+
return (
|
|
19
|
+
getLocalStorageKey(localStorageKey) ??
|
|
20
|
+
getEnvironmentVariable(environmentVariable)
|
|
21
|
+
);
|
|
22
|
+
};
|
|
51
23
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
}
|
|
24
|
+
return createPatternFilter({
|
|
25
|
+
processor,
|
|
26
|
+
pattern: pattern ?? loadPatternFromEnvironment() ?? defaultPattern ?? '*',
|
|
27
|
+
});
|
|
28
|
+
};
|
|
59
29
|
|
|
60
30
|
interface Config {
|
|
61
31
|
/** Replace the detected pattern with something else. */
|
|
@@ -66,4 +36,10 @@ interface Config {
|
|
|
66
36
|
|
|
67
37
|
/** Where to send the logs if they pass the filter. */
|
|
68
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;
|
|
69
45
|
}
|
package/src/get-env.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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 (error) {
|
|
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createEnvironmentFilter } from './environment-filter';
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import * as browser from '../browser-env';
|
|
2
|
-
|
|
3
|
-
class MockLocalStorage {
|
|
4
|
-
getItem = vi.fn((): null | string => null);
|
|
5
|
-
setItem = vi.fn();
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
describe('Browser environment', () => {
|
|
9
|
-
function setup() {
|
|
10
|
-
const localStorage = new MockLocalStorage();
|
|
11
|
-
|
|
12
|
-
Object.assign(globalThis, { localStorage });
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
localStorage,
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
Object.assign(globalThis, { localStorage: undefined });
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe('load', () => {
|
|
24
|
-
it('returns the value stored in localStorage, or undefined', () => {
|
|
25
|
-
const { localStorage } = setup();
|
|
26
|
-
|
|
27
|
-
expect(browser.load(browser.STORAGE_KEY)).toBeUndefined();
|
|
28
|
-
localStorage.getItem.mockReturnValue('yolo');
|
|
29
|
-
expect(browser.load(browser.STORAGE_KEY)).toBe('yolo');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('survives if the localStorage lookup fails', () => {
|
|
33
|
-
const { localStorage } = setup();
|
|
34
|
-
|
|
35
|
-
localStorage.getItem.mockImplementation(() => {
|
|
36
|
-
throw new Error('Simulating localStorage DOM exception');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
expect(browser.load(browser.STORAGE_KEY)).toBeUndefined();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('survives if localStorage is not available', () => {
|
|
43
|
-
expect(browser.load(browser.STORAGE_KEY)).toBeUndefined();
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
describe('save', () => {
|
|
48
|
-
it('saves the value in localStorage', () => {
|
|
49
|
-
const { localStorage } = setup();
|
|
50
|
-
|
|
51
|
-
browser.save(browser.STORAGE_KEY, 'yolo');
|
|
52
|
-
expect(localStorage.setItem).toHaveBeenCalledWith('debug', 'yolo');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('survives if localStorage is not available', () => {
|
|
56
|
-
const pass = () => browser.save(browser.STORAGE_KEY, 'no storage');
|
|
57
|
-
|
|
58
|
-
expect(pass).not.toThrow();
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { detectEnvironment, Environment } from '../environment';
|
|
2
|
-
|
|
3
|
-
describe('detectEnvironment', () => {
|
|
4
|
-
it('detects the correct environment', () => {
|
|
5
|
-
expect(detectEnvironment(undefined)).toBe(Environment.Browser);
|
|
6
|
-
expect(detectEnvironment({ browser: true })).toBe(Environment.Browser);
|
|
7
|
-
expect(detectEnvironment({ type: 'renderer' })).toBe(Environment.Browser);
|
|
8
|
-
expect(detectEnvironment({ __nwjs: true })).toBe(Environment.Browser);
|
|
9
|
-
expect(detectEnvironment(process)).toBe(Environment.Server);
|
|
10
|
-
});
|
|
11
|
-
});
|
package/src/browser-env.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export const STORAGE_KEY = 'debug';
|
|
2
|
-
|
|
3
|
-
export const load = (key: string): undefined | string => {
|
|
4
|
-
try {
|
|
5
|
-
if (typeof localStorage !== 'undefined') {
|
|
6
|
-
return localStorage.getItem(key) ?? undefined;
|
|
7
|
-
}
|
|
8
|
-
} catch (error) {
|
|
9
|
-
// Sometimes localStorage fails (e.g. in Safari private mode). That's
|
|
10
|
-
// okay.
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const save = (key: string, pattern: string) => {
|
|
15
|
-
try {
|
|
16
|
-
if (typeof localStorage !== 'undefined') {
|
|
17
|
-
localStorage.setItem(key, pattern);
|
|
18
|
-
}
|
|
19
|
-
} catch (error) {
|
|
20
|
-
// Throwing might break a startup script. Don't do that.
|
|
21
|
-
}
|
|
22
|
-
};
|
package/src/environment.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Detect Electron renderer / nwjs processes, which are node, but we should
|
|
3
|
-
* treat them as a browser.
|
|
4
|
-
*
|
|
5
|
-
* Credit: debug-js
|
|
6
|
-
*/
|
|
7
|
-
export const detectEnvironment = (process: unknown) => {
|
|
8
|
-
return typeof process === 'undefined' ||
|
|
9
|
-
Object(process).type === 'renderer' ||
|
|
10
|
-
Object(process).browser === true ||
|
|
11
|
-
Object(process).__nwjs
|
|
12
|
-
? Environment.Browser
|
|
13
|
-
: Environment.Server;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export enum Environment {
|
|
17
|
-
Browser = 'browser',
|
|
18
|
-
Server = 'server',
|
|
19
|
-
}
|
package/src/server-env.ts
DELETED