@atlassian/clientside-extensions-debug 2.4.0-69e0f9e-kugbhrvw
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.txt +30 -0
- package/README.md +7 -0
- package/dist/cjs/debug-state-env.test.js +63 -0
- package/dist/cjs/debug-state-env.test.js.map +1 -0
- package/dist/cjs/debug-state.js +155 -0
- package/dist/cjs/debug-state.js.map +1 -0
- package/dist/cjs/debug-state.test.js +66 -0
- package/dist/cjs/debug-state.test.js.map +1 -0
- package/dist/cjs/debug-subjects.js +42 -0
- package/dist/cjs/debug-subjects.js.map +1 -0
- package/dist/cjs/debug-subjects.test.js +35 -0
- package/dist/cjs/debug-subjects.test.js.map +1 -0
- package/dist/cjs/debug.js +23 -0
- package/dist/cjs/debug.js.map +1 -0
- package/dist/cjs/debug.test.js +75 -0
- package/dist/cjs/debug.test.js.map +1 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/logger/console.js +64 -0
- package/dist/cjs/logger/console.js.map +1 -0
- package/dist/cjs/logger/console.test.js +200 -0
- package/dist/cjs/logger/console.test.js.map +1 -0
- package/dist/cjs/logger/default-logger.js +48 -0
- package/dist/cjs/logger/default-logger.js.map +1 -0
- package/dist/cjs/logger/logger.js +16 -0
- package/dist/cjs/logger/logger.js.map +1 -0
- package/dist/esm/debug-state-env.test.js +42 -0
- package/dist/esm/debug-state-env.test.js.map +1 -0
- package/dist/esm/debug-state.js +152 -0
- package/dist/esm/debug-state.js.map +1 -0
- package/dist/esm/debug-state.test.js +63 -0
- package/dist/esm/debug-state.test.js.map +1 -0
- package/dist/esm/debug-subjects.js +39 -0
- package/dist/esm/debug-subjects.js.map +1 -0
- package/dist/esm/debug-subjects.test.js +33 -0
- package/dist/esm/debug-subjects.test.js.map +1 -0
- package/dist/esm/debug.js +20 -0
- package/dist/esm/debug.js.map +1 -0
- package/dist/esm/debug.test.js +73 -0
- package/dist/esm/debug.test.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logger/console.js +61 -0
- package/dist/esm/logger/console.js.map +1 -0
- package/dist/esm/logger/console.test.js +198 -0
- package/dist/esm/logger/console.test.js.map +1 -0
- package/dist/esm/logger/default-logger.js +46 -0
- package/dist/esm/logger/default-logger.js.map +1 -0
- package/dist/esm/logger/logger.js +14 -0
- package/dist/esm/logger/logger.js.map +1 -0
- package/dist/types/debug-state-env.test.d.ts +2 -0
- package/dist/types/debug-state-env.test.d.ts.map +1 -0
- package/dist/types/debug-state.d.ts +41 -0
- package/dist/types/debug-state.d.ts.map +1 -0
- package/dist/types/debug-state.test.d.ts +2 -0
- package/dist/types/debug-state.test.d.ts.map +1 -0
- package/dist/types/debug-subjects.d.ts +25 -0
- package/dist/types/debug-subjects.d.ts.map +1 -0
- package/dist/types/debug-subjects.test.d.ts +2 -0
- package/dist/types/debug-subjects.test.d.ts.map +1 -0
- package/dist/types/debug.d.ts +3 -0
- package/dist/types/debug.d.ts.map +1 -0
- package/dist/types/debug.test.d.ts +2 -0
- package/dist/types/debug.test.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logger/console.d.ts +3 -0
- package/dist/types/logger/console.d.ts.map +1 -0
- package/dist/types/logger/console.test.d.ts +2 -0
- package/dist/types/logger/console.test.d.ts.map +1 -0
- package/dist/types/logger/default-logger.d.ts +25 -0
- package/dist/types/logger/default-logger.d.ts.map +1 -0
- package/dist/types/logger/logger.d.ts +20 -0
- package/dist/types/logger/logger.d.ts.map +1 -0
- package/lib/__mocks__/debug-state.mock.ts +7 -0
- package/lib/__mocks__/debug-subjects.mock.ts +4 -0
- package/lib/debug-state-env.test.ts +53 -0
- package/lib/debug-state.test.ts +87 -0
- package/lib/debug-state.ts +198 -0
- package/lib/debug-subjects.test.ts +40 -0
- package/lib/debug-subjects.ts +78 -0
- package/lib/debug.test.ts +95 -0
- package/lib/debug.ts +24 -0
- package/lib/index.ts +23 -0
- package/lib/logger/console.test.ts +240 -0
- package/lib/logger/console.ts +73 -0
- package/lib/logger/default-logger.ts +67 -0
- package/lib/logger/logger.ts +26 -0
- package/package.json +80 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Copyright (c) 2019 - Atlassian Software Systems Pty Ltd
|
|
2
|
+
|
|
3
|
+
All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions
|
|
7
|
+
are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
10
|
+
this list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of Atlassian Software Systems Pty Ltd nor the names of
|
|
17
|
+
its contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
23
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
24
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
25
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
26
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
27
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
28
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
29
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
30
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
/* eslint-disable no-underscore-dangle */
|
|
23
|
+
const logger_1 = require("./logger/logger");
|
|
24
|
+
/**
|
|
25
|
+
* Using async imports in order to test process.env.NODE_ENV usage in module
|
|
26
|
+
* https://stackoverflow.com/a/48042799
|
|
27
|
+
*/
|
|
28
|
+
describe('debug-state initialization', () => {
|
|
29
|
+
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
// reset NODE_ENV to whatever it was.
|
|
32
|
+
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
|
|
33
|
+
// reset cache of loaded modules. This will ensure that the module is re-initialized
|
|
34
|
+
// with the new NODE_ENV value
|
|
35
|
+
delete window.____c_p_d;
|
|
36
|
+
jest.resetModules();
|
|
37
|
+
});
|
|
38
|
+
it('should be set to FALSE if process.env.NODE_ENV is "production"', async () => {
|
|
39
|
+
process.env.NODE_ENV = 'production';
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
41
|
+
const debugStateModule = await Promise.resolve().then(() => __importStar(require('./debug-state')));
|
|
42
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
43
|
+
expect(window.____c_p_d.debug).toBe(false);
|
|
44
|
+
expect(window.____c_p_d.logging).toBe(false);
|
|
45
|
+
expect(window.____c_p_d.validation).toBe(false);
|
|
46
|
+
expect(window.____c_p_d.discovery).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
it('should set the default log level to "ERROR" if process.env.NODE_ENV is "production"', async () => {
|
|
49
|
+
process.env.NODE_ENV = 'production';
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
+
await Promise.resolve().then(() => __importStar(require('./debug-state')));
|
|
52
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
53
|
+
expect(window.____c_p_d.logLevel).toBe(logger_1.LogLevel.error);
|
|
54
|
+
});
|
|
55
|
+
it('should be set the default log level to "INFO" if process.env.NODE_ENV is different than "production"', async () => {
|
|
56
|
+
process.env.NODE_ENV = 'development';
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
58
|
+
await Promise.resolve().then(() => __importStar(require('./debug-state')));
|
|
59
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
60
|
+
expect(window.____c_p_d.logLevel).toBe(logger_1.LogLevel.info);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
//# sourceMappingURL=debug-state-env.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-state-env.test.js","sourceRoot":"","sources":["../../lib/debug-state-env.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,0CAA0C;AAC1C,4CAA2C;AAE3C;;;GAGG;AACH,QAAQ,CAAC,4BAA4B,EAAE,GAAG,EAAE;IACxC,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IAE/C,UAAU,CAAC,GAAG,EAAE;QACZ,qCAAqC;QACrC,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,iBAAiB,CAAC;QACzC,oFAAoF;QACpF,8BAA8B;QAC9B,OAAO,MAAM,CAAC,SAAS,CAAC;QACxB,IAAI,CAAC,YAAY,EAAE,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gEAAgE,EAAE,KAAK,IAAI,EAAE;QAC5E,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,YAAY,CAAC;QAEpC,6DAA6D;QAC7D,MAAM,gBAAgB,GAAG,wDAAa,eAAe,GAAC,CAAC;QAEvD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAElD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC7C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qFAAqF,EAAE,KAAK,IAAI,EAAE;QACjG,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,YAAY,CAAC;QAEpC,6DAA6D;QAC7D,wDAAa,eAAe,GAAC,CAAC;QAE9B,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,iBAAQ,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sGAAsG,EAAE,KAAK,IAAI,EAAE;QAClH,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,aAAa,CAAC;QAErC,6DAA6D;QAC7D,wDAAa,eAAe,GAAC,CAAC;QAE9B,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,iBAAQ,CAAC,IAAI,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["/* eslint-disable no-underscore-dangle */\nimport { LogLevel } from './logger/logger';\n\n/**\n * Using async imports in order to test process.env.NODE_ENV usage in module\n * https://stackoverflow.com/a/48042799\n */\ndescribe('debug-state initialization', () => {\n const ORIGINAL_NODE_ENV = process.env.NODE_ENV;\n\n beforeEach(() => {\n // reset NODE_ENV to whatever it was.\n process.env.NODE_ENV = ORIGINAL_NODE_ENV;\n // reset cache of loaded modules. This will ensure that the module is re-initialized\n // with the new NODE_ENV value\n delete window.____c_p_d;\n jest.resetModules();\n });\n\n it('should be set to FALSE if process.env.NODE_ENV is \"production\"', async () => {\n process.env.NODE_ENV = 'production';\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const debugStateModule = await import('./debug-state');\n\n expect(window.____c_p_d.__initialized).toBe(true);\n\n expect(window.____c_p_d.debug).toBe(false);\n expect(window.____c_p_d.logging).toBe(false);\n expect(window.____c_p_d.validation).toBe(false);\n expect(window.____c_p_d.discovery).toBe(false);\n });\n\n it('should set the default log level to \"ERROR\" if process.env.NODE_ENV is \"production\"', async () => {\n process.env.NODE_ENV = 'production';\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n await import('./debug-state');\n\n expect(window.____c_p_d.__initialized).toBe(true);\n expect(window.____c_p_d.logLevel).toBe(LogLevel.error);\n });\n\n it('should be set the default log level to \"INFO\" if process.env.NODE_ENV is different than \"production\"', async () => {\n process.env.NODE_ENV = 'development';\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n await import('./debug-state');\n\n expect(window.____c_p_d.__initialized).toBe(true);\n expect(window.____c_p_d.logLevel).toBe(LogLevel.info);\n });\n});\n"]}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-underscore-dangle */
|
|
3
|
+
var _a;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.setLogLevel = exports.setDiscoveryEnabled = exports.setValidationEnabled = exports.setLoggingEnabled = exports.setDebugEnabled = exports.getLogLevel = exports.isDiscoveryEnabled = exports.isValidationEnabled = exports.isLoggingEnabled = exports.isDebugEnabled = exports.observeStateChange = void 0;
|
|
6
|
+
const clientside_extensions_base_1 = require("@atlassian/clientside-extensions-base");
|
|
7
|
+
const debug_subjects_1 = require("./debug-subjects");
|
|
8
|
+
const logger_1 = require("./logger/logger");
|
|
9
|
+
var ClientExtensionDebugTypes;
|
|
10
|
+
(function (ClientExtensionDebugTypes) {
|
|
11
|
+
ClientExtensionDebugTypes["debug"] = "debug";
|
|
12
|
+
ClientExtensionDebugTypes["logging"] = "logging";
|
|
13
|
+
ClientExtensionDebugTypes["validation"] = "validation";
|
|
14
|
+
ClientExtensionDebugTypes["discovery"] = "discovery";
|
|
15
|
+
/** @since 2.1.0 */
|
|
16
|
+
ClientExtensionDebugTypes["logLevel"] = "logLevel";
|
|
17
|
+
})(ClientExtensionDebugTypes || (ClientExtensionDebugTypes = {}));
|
|
18
|
+
const defineDebugGlobal = (previousGlobal) => {
|
|
19
|
+
// initialize as boolean
|
|
20
|
+
const debugGlobal = Object.create(null);
|
|
21
|
+
const debugStates = {
|
|
22
|
+
debug: false,
|
|
23
|
+
logging: false,
|
|
24
|
+
validation: false,
|
|
25
|
+
discovery: false,
|
|
26
|
+
/** @since 2.1.0 */
|
|
27
|
+
// In dev mode we set default loglevel to INFO
|
|
28
|
+
logLevel: process.env.NODE_ENV === 'production' ? logger_1.LogLevel.error : logger_1.LogLevel.info,
|
|
29
|
+
};
|
|
30
|
+
const debugStateSubject = debug_subjects_1.registerDebugSubject("state" /* State */, () => new clientside_extensions_base_1.ReplaySubject(1));
|
|
31
|
+
const properties = {
|
|
32
|
+
debug: {
|
|
33
|
+
get() {
|
|
34
|
+
return debugStates.debug;
|
|
35
|
+
},
|
|
36
|
+
set(val) {
|
|
37
|
+
debugStates.debug = Boolean(val);
|
|
38
|
+
debugStateSubject.notify(debugStates);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
logging: {
|
|
42
|
+
get() {
|
|
43
|
+
return debugStates.logging;
|
|
44
|
+
},
|
|
45
|
+
set(val) {
|
|
46
|
+
debugStates.logging = Boolean(val);
|
|
47
|
+
debugStateSubject.notify(debugStates);
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
validation: {
|
|
51
|
+
get() {
|
|
52
|
+
return debugStates.validation;
|
|
53
|
+
},
|
|
54
|
+
set(val) {
|
|
55
|
+
debugStates.validation = Boolean(val);
|
|
56
|
+
debugStateSubject.notify(debugStates);
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
discovery: {
|
|
60
|
+
get() {
|
|
61
|
+
return debugStates.discovery;
|
|
62
|
+
},
|
|
63
|
+
set(val) {
|
|
64
|
+
debugStates.discovery = Boolean(val);
|
|
65
|
+
debugStateSubject.notify(debugStates);
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
/** @since 2.1.0 */
|
|
69
|
+
logLevel: {
|
|
70
|
+
get() {
|
|
71
|
+
return debugStates.logLevel;
|
|
72
|
+
},
|
|
73
|
+
set(val) {
|
|
74
|
+
let level = String(val).toUpperCase();
|
|
75
|
+
// Handle runtime errors
|
|
76
|
+
if (!Object.values(logger_1.LogLevel).includes(level)) {
|
|
77
|
+
level = logger_1.LogLevel.info;
|
|
78
|
+
}
|
|
79
|
+
debugStates.logLevel = level;
|
|
80
|
+
debugStateSubject.notify(debugStates);
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
__initialized: { value: true, writable: false },
|
|
84
|
+
};
|
|
85
|
+
Object.defineProperties(debugGlobal, properties);
|
|
86
|
+
Object.defineProperty(window, '____c_p_d', {
|
|
87
|
+
configurable: true,
|
|
88
|
+
get() {
|
|
89
|
+
return debugGlobal;
|
|
90
|
+
},
|
|
91
|
+
set(val) {
|
|
92
|
+
// its a boolean
|
|
93
|
+
if (val === true || val === false) {
|
|
94
|
+
debugGlobal.debug = val;
|
|
95
|
+
debugGlobal.logging = val;
|
|
96
|
+
debugGlobal.validation = val;
|
|
97
|
+
debugGlobal.discovery = val;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// we can handle booleans - like above
|
|
101
|
+
// or objects (except null) and nothing else.
|
|
102
|
+
if (typeof val !== 'object' || val === null) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const clientExtensionDebugKeys = Object.keys(ClientExtensionDebugTypes);
|
|
106
|
+
clientExtensionDebugKeys.forEach((key) => {
|
|
107
|
+
if (key in val) {
|
|
108
|
+
debugGlobal[key] = val[key];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
// Now, we can use the previous value e.g. boolean, to set the value for all flags
|
|
114
|
+
window.____c_p_d = previousGlobal;
|
|
115
|
+
};
|
|
116
|
+
// Register global value only once
|
|
117
|
+
if (!('____c_p_d' in window) || !window.____c_p_d.__initialized) {
|
|
118
|
+
defineDebugGlobal((_a = window.____c_p_d) !== null && _a !== void 0 ? _a : process.env.NODE_ENV !== 'production');
|
|
119
|
+
}
|
|
120
|
+
exports.observeStateChange = (observer) => debug_subjects_1.observeDebugSubject("state" /* State */, observer);
|
|
121
|
+
exports.isDebugEnabled = () => {
|
|
122
|
+
return window.____c_p_d.debug;
|
|
123
|
+
};
|
|
124
|
+
exports.isLoggingEnabled = () => {
|
|
125
|
+
return window.____c_p_d.logging;
|
|
126
|
+
};
|
|
127
|
+
exports.isValidationEnabled = () => {
|
|
128
|
+
return window.____c_p_d.validation;
|
|
129
|
+
};
|
|
130
|
+
exports.isDiscoveryEnabled = () => {
|
|
131
|
+
return window.____c_p_d.discovery;
|
|
132
|
+
};
|
|
133
|
+
/** @since 2.1.0 */
|
|
134
|
+
exports.getLogLevel = () => {
|
|
135
|
+
const logLevel = window.____c_p_d.logLevel;
|
|
136
|
+
return logLevel !== null && logLevel !== void 0 ? logLevel : logger_1.LogLevel.info;
|
|
137
|
+
};
|
|
138
|
+
exports.setDebugEnabled = (value) => {
|
|
139
|
+
window.____c_p_d.debug = value;
|
|
140
|
+
};
|
|
141
|
+
exports.setLoggingEnabled = (value) => {
|
|
142
|
+
window.____c_p_d.logging = value;
|
|
143
|
+
};
|
|
144
|
+
exports.setValidationEnabled = (value) => {
|
|
145
|
+
window.____c_p_d.validation = value;
|
|
146
|
+
};
|
|
147
|
+
exports.setDiscoveryEnabled = (value) => {
|
|
148
|
+
window.____c_p_d.discovery = value;
|
|
149
|
+
};
|
|
150
|
+
/** @since 2.1.0 */
|
|
151
|
+
exports.setLogLevel = (value) => {
|
|
152
|
+
const level = value.toUpperCase();
|
|
153
|
+
window.____c_p_d.logLevel = level;
|
|
154
|
+
};
|
|
155
|
+
//# sourceMapp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AG,KAAK,CAAC,WAAW,EAAc,CAAC;IAE7C,MAAM,CAAC,SAAS,CAAC,QAAqB,GAAG,KAAK,CAAC;AACpD,CAAC,CAAC","sourcesContent":["/* eslint-disable no-underscore-dangle */\n\nimport type { Observer } from '@atlassian/clientside-extensions-base';\nimport { ReplaySubject } from '@atlassian/clientside-extensions-base';\nimport { DebugSubjects, observeDebugSubject, registerDebugSubject } from './debug-subjects';\nimport { LogLevel } from './logger/logger';\n\nenum ClientExtensionDebugTypes {\n debug = 'debug',\n logging = 'logging',\n validation = 'validation',\n discovery = 'discovery',\n /** @since 2.1.0 */\n logLevel = 'logLevel',\n}\n\ntype DebugTypeKeys = Exclude<keyof typeof ClientExtensionDebugTypes, 'logLevel'>;\n\nexport type ClientExtensionDebug = {\n [key in DebugTypeKeys]: boolean;\n} & {\n [ClientExtensionDebugTypes.logLevel]: LogLevel;\n};\n\nexport type ExposedClientExtensionDebug = { [key in keyof ClientExtensionDebug]: PropertyDescriptor } & {\n __initialized: PropertyDescriptor;\n};\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Window {\n // The global ____c_p_d is a primitive boolean value that holds additional properties\n // Due to historical name changes the \"p\" still represents the old \"plugin\" nomenclature\n ____c_p_d: ExposedClientExtensionDebug;\n }\n}\n\nconst defineDebugGlobal = (previousGlobal: unknown) => {\n // initialize as boolean\n const debugGlobal = Object.create(null);\n const debugStates: ClientExtensionDebug = {\n debug: false,\n logging: false,\n validation: false,\n discovery: false,\n /** @since 2.1.0 */\n // In dev mode we set default loglevel to INFO\n logLevel: process.env.NODE_ENV === 'production' ? LogLevel.error : LogLevel.info,\n };\n\n const debugStateSubject = registerDebugSubject(DebugSubjects.State, () => new ReplaySubject(1));\n\n const properties: ExposedClientExtensionDebug = {\n debug: {\n get() {\n return debugStates.debug;\n },\n set(val: unknown) {\n debugStates.debug = Boolean(val);\n debugStateSubject.notify(debugStates);\n },\n },\n logging: {\n get() {\n return debugStates.logging;\n },\n set(val: unknown) {\n debugStates.logging = Boolean(val);\n debugStateSubject.notify(debugStates);\n },\n },\n validation: {\n get() {\n return debugStates.validation;\n },\n set(val: unknown) {\n debugStates.validation = Boolean(val);\n debugStateSubject.notify(debugStates);\n },\n },\n discovery: {\n get() {\n return debugStates.discovery;\n },\n set(val: unknown) {\n debugStates.discovery = Boolean(val);\n debugStateSubject.notify(debugStates);\n },\n },\n /** @since 2.1.0 */\n logLevel: {\n get() {\n return debugStates.logLevel;\n },\n set(val: unknown) {\n let level = String(val).toUpperCase() as LogLevel;\n\n // Handle runtime errors\n if (!Object.values(LogLevel).includes(level)) {\n level = LogLevel.info;\n }\n\n debugStates.logLevel = level;\n debugStateSubject.notify(debugStates);\n },\n },\n __initialized: { value: true, writable: false },\n };\n Object.defineProperties(debugGlobal, properties);\n\n Object.defineProperty(window, '____c_p_d', {\n configurable: true, // needed for testing :(\n get() {\n return debugGlobal;\n },\n set(val) {\n // its a boolean\n if (val === true || val === false) {\n debugGlobal.debug = val;\n debugGlobal.logging = val;\n debugGlobal.validation = val;\n debugGlobal.discovery = val;\n return;\n }\n\n // we can handle booleans - like above\n // or objects (except null) and nothing else.\n if (typeof val !== 'object' || val === null) {\n return;\n }\n\n const clientExtensionDebugKeys = Object.keys(ClientExtensionDebugTypes) as (keyof ClientExtensionDebug)[];\n\n clientExtensionDebugKeys.forEach((key) => {\n if (key in val) {\n debugGlobal[key] = val[key];\n }\n });\n },\n });\n\n // Now, we can use the previous value e.g. boolean, to set the value for all flags\n window.____c_p_d = previousGlobal as ExposedClientExtensionDebug;\n};\n\n// Register global value only once\nif (!('____c_p_d' in window) || !window.____c_p_d.__initialized) {\n defineDebugGlobal(window.____c_p_d ?? process.env.NODE_ENV !== 'production');\n}\n\nexport const observeStateChange = (observer: Observer<ClientExtensionDebug>) =>\n observeDebugSubject<ClientExtensionDebug>(DebugSubjects.State, observer);\n\nexport const isDebugEnabled = () => {\n return window.____c_p_d.debug as boolean;\n};\n\nexport const isLoggingEnabled = () => {\n return window.____c_p_d.logging as boolean;\n};\n\nexport const isValidationEnabled = () => {\n return window.____c_p_d.validation as boolean;\n};\n\nexport const isDiscoveryEnabled = () => {\n return window.____c_p_d.discovery as boolean;\n};\n\n/** @since 2.1.0 */\nexport const getLogLevel = (): LogLevel => {\n const logLevel = window.____c_p_d.logLevel as LogLevel;\n\n return logLevel ?? LogLevel.info;\n};\n\nexport const setDebugEnabled = (value: boolean) => {\n (window.____c_p_d.debug as boolean) = value;\n};\n\nexport const setLoggingEnabled = (value: boolean) => {\n (window.____c_p_d.logging as boolean) = value;\n};\n\nexport const setValidationEnabled = (value: boolean) => {\n (window.____c_p_d.validation as boolean) = value;\n};\n\nexport const setDiscoveryEnabled = (value: boolean) => {\n (window.____c_p_d.discovery as boolean) = value;\n};\n\n/** @since 2.1.0 */\nexport const setLogLevel = (value: string) => {\n const level = value.toUpperCase() as LogLevel;\n\n (window.____c_p_d.logLevel as LogLevel) = level;\n};\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
/* eslint-disable no-underscore-dangle, jest/no-standalone-expect */
|
|
5
|
+
const jest_in_case_1 = tslib_1.__importDefault(require("jest-in-case"));
|
|
6
|
+
const debug_state_1 = require("./debug-state");
|
|
7
|
+
const logger_1 = require("./logger/logger");
|
|
8
|
+
describe('debug utils', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
window.____c_p_d = false;
|
|
11
|
+
});
|
|
12
|
+
jest_in_case_1.default('should be active if respective flag is set to TRUE', (opts) => {
|
|
13
|
+
expect(opts.isUtil()).toBe(false);
|
|
14
|
+
// switch flag to TRUE
|
|
15
|
+
window.____c_p_d = { [opts.key]: true };
|
|
16
|
+
expect(opts.isUtil()).toBe(true);
|
|
17
|
+
// switch ALL false
|
|
18
|
+
window.____c_p_d = false;
|
|
19
|
+
expect(opts.isUtil()).toBe(false);
|
|
20
|
+
// switch ALL true
|
|
21
|
+
window.____c_p_d = true;
|
|
22
|
+
expect(opts.isUtil()).toBe(true);
|
|
23
|
+
}, {
|
|
24
|
+
isDebugEnabled: { isUtil: debug_state_1.isDebugEnabled, key: 'debug' },
|
|
25
|
+
isLoggingEnabled: { isUtil: debug_state_1.isLoggingEnabled, key: 'logging' },
|
|
26
|
+
isValidationEnabled: { isUtil: debug_state_1.isValidationEnabled, key: 'validation' },
|
|
27
|
+
isDiscoveryEnabled: { isUtil: debug_state_1.isDiscoveryEnabled, key: 'discovery' },
|
|
28
|
+
});
|
|
29
|
+
describe('log levels', () => {
|
|
30
|
+
let prevDebugState;
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
prevDebugState = window.____c_p_d;
|
|
33
|
+
});
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
window.____c_p_d = prevDebugState;
|
|
36
|
+
});
|
|
37
|
+
it('should allow to change the default log level using utils', () => {
|
|
38
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.info);
|
|
39
|
+
debug_state_1.setLogLevel(logger_1.LogLevel.debug);
|
|
40
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.debug);
|
|
41
|
+
});
|
|
42
|
+
it('should allow to change log level using global variable', () => {
|
|
43
|
+
// Change with enum
|
|
44
|
+
window.____c_p_d.logLevel = logger_1.LogLevel.error;
|
|
45
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.error);
|
|
46
|
+
// Change with string value
|
|
47
|
+
window.____c_p_d.logLevel = 'warn';
|
|
48
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.warn);
|
|
49
|
+
});
|
|
50
|
+
it('should fallback to a default "INFO" level when using unsupported runtime value', () => {
|
|
51
|
+
window.____c_p_d.logLevel = 'show-me-all-the-things';
|
|
52
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.info);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe('compatibility mode', () => {
|
|
56
|
+
it('should fallback to a default "INFO" level when "logLevel" property is missing e.g. CSE < 2.1.0', () => {
|
|
57
|
+
Object.defineProperty(window, '____c_p_d', {
|
|
58
|
+
value: {},
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
});
|
|
62
|
+
expect(debug_state_1.getLogLevel()).toBe(logger_1.LogLevel.info);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
//# sourceMappingURL=debug-state.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-state.test.js","sourceRoot":"","sources":["../../lib/debug-state.test.ts"],"names":[],"mappings":";;;AAAA,oEAAoE;AACpE,wEAAiC;AAEjC,+CAAoI;AACpI,4CAA2C;AAE3C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IACzB,UAAU,CAAC,GAAG,EAAE;QACX,MAAM,CAAC,SAAqB,GAAG,KAAK,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,sBAAK,CACD,oDAAoD,EACpD,CAAC,IAAI,EAAE,EAAE;QACL,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAElC,sBAAsB;QACrB,MAAM,CAAC,SAAqB,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;QAErD,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEjC,mBAAmB;QAClB,MAAM,CAAC,SAAqB,GAAG,KAAK,CAAC;QAEtC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAElC,kBAAkB;QACjB,MAAM,CAAC,SAAqB,GAAG,IAAI,CAAC;QAErC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAA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AM,CAAC,yBAAW,EAAE,CAAC,CAAC,IAAI,CAAC,iBAAQ,CAAC,IAAI,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,gFAAgF,EAAE,GAAG,EAAE;YACrF,MAAM,CAAC,SAAS,CAAC,QAAmB,GAAG,wBAAwB,CAAC;YAEjE,MAAM,CAAC,yBAAW,EAAE,CAAC,CAAC,IAAI,CAAC,iBAAQ,CAAC,IAAI,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAChC,EAAE,CAAC,gGAAgG,EAAE,GAAG,EAAE;YACtG,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;gBACvC,KAAK,EAAE,EAAE;gBACT,YAAY,EAAE,IAAI;gBAClB,QAAQ,EAAE,IAAI;aACjB,CAAC,CAAC;YAEH,MAAM,CAAC,yBAAW,EAAE,CAAC,CAAC,IAAI,CAAC,iBAAQ,CAAC,IAAI,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["/* eslint-disable no-underscore-dangle, jest/no-standalone-expect */\nimport cases from 'jest-in-case';\nimport type { ExposedClientExtensionDebug } from './debug-state';\nimport { isDebugEnabled, isDiscoveryEnabled, isLoggingEnabled, isValidationEnabled, getLogLevel, setLogLevel } from './debug-state';\nimport { LogLevel } from './logger/logger';\n\ndescribe('debug utils', () => {\n beforeEach(() => {\n (window.____c_p_d as unknown) = false;\n });\n\n cases(\n 'should be active if respective flag is set to TRUE',\n (opts) => {\n expect(opts.isUtil()).toBe(false);\n\n // switch flag to TRUE\n (window.____c_p_d as unknown) = { [opts.key]: true };\n\n expect(opts.isUtil()).toBe(true);\n\n // switch ALL false\n (window.____c_p_d as unknown) = false;\n\n expect(opts.isUtil()).toBe(false);\n\n // switch ALL true\n (window.____c_p_d as unknown) = true;\n\n expect(opts.isUtil()).toBe(true);\n },\n {\n isDebugEnabled: { isUtil: isDebugEnabled, key: 'debug' },\n isLoggingEnabled: { isUtil: isLoggingEnabled, key: 'logging' },\n isValidationEnabled: { isUtil: isValidationEnabled, key: 'validation' },\n isDiscoveryEnabled: { isUtil: isDiscoveryEnabled, key: 'discovery' },\n },\n );\n\n describe('log levels', () => {\n let prevDebugState: ExposedClientExtensionDebug;\n\n beforeEach(() => {\n prevDebugState = window.____c_p_d;\n });\n\n afterEach(() => {\n window.____c_p_d = prevDebugState;\n });\n\n it('should allow to change the default log level using utils', () => {\n expect(getLogLevel()).toBe(LogLevel.info);\n\n setLogLevel(LogLevel.debug);\n\n expect(getLogLevel()).toBe(LogLevel.debug);\n });\n\n it('should allow to change log level using global variable', () => {\n // Change with enum\n (window.____c_p_d.logLevel as LogLevel) = LogLevel.error;\n expect(getLogLevel()).toBe(LogLevel.error);\n\n // Change with string value\n (window.____c_p_d.logLevel as string) = 'warn';\n expect(getLogLevel()).toBe(LogLevel.warn);\n });\n\n it('should fallback to a default \"INFO\" level when using unsupported runtime value', () => {\n (window.____c_p_d.logLevel as string) = 'show-me-all-the-things';\n\n expect(getLogLevel()).toBe(LogLevel.info);\n });\n });\n\n describe('compatibility mode', () => {\n it('should fallback to a default \"INFO\" level when \"logLevel\" property is missing e.g. CSE < 2.1.0', () => {\n Object.defineProperty(window, '____c_p_d', {\n value: {},\n configurable: true,\n writable: true,\n });\n\n expect(getLogLevel()).toBe(LogLevel.info);\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.observeDebugSubject = exports.registerDebugSubject = void 0;
|
|
4
|
+
const defineSubjectGlobal = () => {
|
|
5
|
+
// initialize as boolean
|
|
6
|
+
const subjectGlobal = Object.create(null);
|
|
7
|
+
const properties = {
|
|
8
|
+
__initialized: { value: true, writable: false },
|
|
9
|
+
subjects: {
|
|
10
|
+
value: subjectGlobal,
|
|
11
|
+
writable: false,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperties(subjectGlobal, properties);
|
|
15
|
+
window.____c_p_s = subjectGlobal;
|
|
16
|
+
};
|
|
17
|
+
if (!('____c_p_s' in window) || !window.____c_p_s.__initialized) {
|
|
18
|
+
defineSubjectGlobal();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Registers a debug subject as singleton
|
|
22
|
+
*
|
|
23
|
+
* @param key
|
|
24
|
+
* @param subjectFactory
|
|
25
|
+
*/
|
|
26
|
+
exports.registerDebugSubject = (key, subjectFactory) => {
|
|
27
|
+
const debugSubjects = window.____c_p_s.subjects;
|
|
28
|
+
// good old singletons - ensure we do not register the same subject twice.
|
|
29
|
+
// we also can't fail as multiple instances might try to access the same, so instead we return the previously registered one.
|
|
30
|
+
if (!(key in debugSubjects)) {
|
|
31
|
+
debugSubjects[key] = subjectFactory();
|
|
32
|
+
}
|
|
33
|
+
return debugSubjects[key];
|
|
34
|
+
};
|
|
35
|
+
exports.observeDebugSubject = (key, observer) => {
|
|
36
|
+
const debugSubjects = window.____c_p_s.subjects;
|
|
37
|
+
if (!(key in debugSubjects)) {
|
|
38
|
+
throw new Error(`No subject registered for key: "${key}"`);
|
|
39
|
+
}
|
|
40
|
+
return debugSubjects[key].subscribe(observer);
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=debug-subjects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-subjects.js","sourceRoot":"","sources":["../../lib/debug-subjects.ts"],"names":[],"mappings":";;;AAyBA,MAAM,mBAAmB,GAAG,GAAG,EAAE;IAC7B,wBAAwB;IACxB,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAE1C,MAAM,UAAU,GAAmC;QAC/C,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE;QAC/C,QAAQ,EAAE;YACN,KAAK,EAAE,aAAa;YACpB,QAAQ,EAAE,KAAK;SAClB;KACJ,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC;IAEnD,MAAM,CAAC,SAAS,GAAG,aAAa,CAAC;AACrC,CAAC,CAAC;AAEF,IAAI,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,EAAE;IAC7D,mBAAmB,EAAE,CAAC;CACzB;AAID;;;;;GAKG;AACU,QAAA,oBAAoB,GAAG,CAChC,GAAkB,EAClB,cAAiC,EACtB,EAAE;IACb,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,QAAyC,CAAC;IAEjF,0EAA0E;IAC1E,6HAA6H;IAC7H,IAAI,CAAC,CAAC,GAAG,IAAI,aAAa,CAAC,EAAE;QACzB,aAAa,CAAC,GAAG,CAAC,GAAG,cAAc,EAAE,CAAC;KACzC;IAED,OAAO,aAAa,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC,CAAC;AAEW,QAAA,mBAAmB,GAAG,CAAc,GAAkB,EAAE,QAA+B,EAAE,EAAE;IACpG,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,QAAkD,CAAC;IAE1F,IAAI,CAAC,CAAC,GAAG,IAAI,aAAa,CAAC,EAAE;QACzB,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,GAAG,CAAC,CAAC;KAC9D;IAED,OAAO,aAAa,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AAClD,CAAC,CAAC","sourcesContent":["/* eslint-disable no-underscore-dangle */\nimport type { Observer, Subject } from '@atlassian/clientside-extensions-base';\n\ntype ExposedClientExtensionSubjects = {\n __initialized: PropertyDescriptor;\n subjects: PropertyDescriptor;\n};\n\nexport const enum DebugSubjects {\n Logger = 'logger',\n State = 'state',\n}\n\ntype DebugSubjectsMap<Subject> = {\n [key in DebugSubjects]: Subject;\n};\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n interface Window {\n // Due to historical name changes the \"p\" still represents the old \"plugin\" nomenclature\n ____c_p_s: ExposedClientExtensionSubjects;\n }\n}\n\nconst defineSubjectGlobal = () => {\n // initialize as boolean\n const subjectGlobal = Object.create(null);\n\n const properties: ExposedClientExtensionSubjects = {\n __initialized: { value: true, writable: false },\n subjects: {\n value: subjectGlobal,\n writable: false,\n },\n };\n\n Object.defineProperties(subjectGlobal, properties);\n\n window.____c_p_s = subjectGlobal;\n};\n\nif (!('____c_p_s' in window) || !window.____c_p_s.__initialized) {\n defineSubjectGlobal();\n}\n\ntype UnpackPayloadType<SubjectType> = SubjectType extends Subject<infer U> ? U : never;\n\n/**\n * Registers a debug subject as singleton\n *\n * @param key\n * @param subjectFactory\n */\nexport const registerDebugSubject = <SubjectType extends Subject<PayloadType>, PayloadType = UnpackPayloadType<SubjectType>>(\n key: DebugSubjects,\n subjectFactory: () => SubjectType,\n): SubjectType => {\n const debugSubjects = window.____c_p_s.subjects as DebugSubjectsMap<SubjectType>;\n\n // good old singletons - ensure we do not register the same subject twice.\n // we also can't fail as multiple instances might try to access the same, so instead we return the previously registered one.\n if (!(key in debugSubjects)) {\n debugSubjects[key] = subjectFactory();\n }\n\n return debugSubjects[key];\n};\n\nexport const observeDebugSubject = <PayloadType>(key: DebugSubjects, observer: Observer<PayloadType>) => {\n const debugSubjects = window.____c_p_s.subjects as DebugSubjectsMap<Subject<PayloadType>>;\n\n if (!(key in debugSubjects)) {\n throw new Error(`No subject registered for key: \"${key}\"`);\n }\n\n return debugSubjects[key].subscribe(observer);\n};\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const clientside_extensions_base_1 = require("@atlassian/clientside-extensions-base");
|
|
4
|
+
const debug_subjects_1 = require("./debug-subjects");
|
|
5
|
+
describe('debug-subject', () => {
|
|
6
|
+
const debugFactory = () => new clientside_extensions_base_1.Subject();
|
|
7
|
+
describe('registerDebugSubject', () => {
|
|
8
|
+
it('should create an register a subject under a given namespace', () => {
|
|
9
|
+
const subject = debug_subjects_1.registerDebugSubject("logger" /* Logger */, debugFactory);
|
|
10
|
+
expect(subject).toBeInstanceOf(clientside_extensions_base_1.Subject);
|
|
11
|
+
});
|
|
12
|
+
it('should not create a new instance for a previously registered key', () => {
|
|
13
|
+
const subject = debug_subjects_1.registerDebugSubject("state" /* State */, debugFactory);
|
|
14
|
+
const sameSubject = debug_subjects_1.registerDebugSubject("state" /* State */, debugFactory);
|
|
15
|
+
expect(subject).toBe(sameSubject);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe('observeDebugSubject', () => {
|
|
19
|
+
it('should throw if the subject does not exist yet', () => {
|
|
20
|
+
// @ts-expect-error We are using a wrong subject key here
|
|
21
|
+
const unregisteredSubject = () => debug_subjects_1.observeDebugSubject('Foo', () => { });
|
|
22
|
+
expect(unregisteredSubject).toThrow(`No subject registered for key: "Foo"`);
|
|
23
|
+
});
|
|
24
|
+
it('should subscribe to a previously registered subject', () => {
|
|
25
|
+
const SUBJECT = "state" /* State */;
|
|
26
|
+
const TEST_PAYLOAD = 'some payload';
|
|
27
|
+
const observer = jest.fn();
|
|
28
|
+
const subject = debug_subjects_1.registerDebugSubject(SUBJECT, debugFactory);
|
|
29
|
+
debug_subjects_1.observeDebugSubject(SUBJECT, observer);
|
|
30
|
+
subject.notify(TEST_PAYLOAD);
|
|
31
|
+
expect(observer).toBeCalledWith(TEST_PAYLOAD);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=debug-subjects.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-subjects.test.js","sourceRoot":"","sources":["../../lib/debug-subjects.test.ts"],"names":[],"m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CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;YAEvC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAC7B,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["import { Subject } from '@atlassian/clientside-extensions-base';\nimport { DebugSubjects, observeDebugSubject, registerDebugSubject } from './debug-subjects';\n\ndescribe('debug-subject', () => {\n const debugFactory = (): Subject<string> => new Subject<string>();\n\n describe('registerDebugSubject', () => {\n it('should create an register a subject under a given namespace', () => {\n const subject = registerDebugSubject(DebugSubjects.Logger, debugFactory);\n expect(subject).toBeInstanceOf(Subject);\n });\n\n it('should not create a new instance for a previously registered key', () => {\n const subject = registerDebugSubject(DebugSubjects.State, debugFactory);\n const sameSubject = registerDebugSubject(DebugSubjects.State, debugFactory);\n expect(subject).toBe(sameSubject);\n });\n });\n\n describe('observeDebugSubject', () => {\n it('should throw if the subject does not exist yet', () => {\n // @ts-expect-error We are using a wrong subject key here\n const unregisteredSubject = () => observeDebugSubject('Foo', () => {});\n\n expect(unregisteredSubject).toThrow(`No subject registered for key: \"Foo\"`);\n });\n\n it('should subscribe to a previously registered subject', () => {\n const SUBJECT = DebugSubjects.State;\n const TEST_PAYLOAD = 'some payload';\n\n const observer = jest.fn();\n const subject = registerDebugSubject(SUBJECT, debugFactory);\n observeDebugSubject(SUBJECT, observer);\n\n subject.notify(TEST_PAYLOAD);\n expect(observer).toBeCalledWith(TEST_PAYLOAD);\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.onDebug = void 0;
|
|
4
|
+
const debug_state_1 = require("./debug-state");
|
|
5
|
+
const logger_1 = require("./logger/logger");
|
|
6
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
7
|
+
exports.onDebug = (callback) => {
|
|
8
|
+
// Return early if both debug and logging is disabled
|
|
9
|
+
if (!debug_state_1.isDebugEnabled() || !debug_state_1.isLoggingEnabled()) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
let payload;
|
|
13
|
+
try {
|
|
14
|
+
payload = callback(logger_1.LogLevel);
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
// eslint-disable-next-line no-empty
|
|
18
|
+
}
|
|
19
|
+
if (payload) {
|
|
20
|
+
logger_1._loggerSubject.notify(payload);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=debug.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.js","sourceRoot":"","sources":["../../lib/debug.ts"],"names":[],"mappings":";;;AAAA,+CAAiE;AAGjE,4CAA2D;AAE3D,wDAAwD;AAC3C,QAAA,OAAO,GAAG,CAAC,QAAwB,EAAE,EAAE;IAChD,qDAAqD;IACrD,IAAI,CAAC,4BAAc,EAAE,IAAI,CAAC,8BAAgB,EAAE,EAAE;QAC1C,OAAO;KACV;IAED,IAAI,OAAkC,CAAC;IAEvC,IAAI;QACA,OAAO,GAAG,QAAQ,CAAC,iBAAQ,CAAC,CAAC;KAChC;IAAC,OAAO,CAAC,EAAE;QACR,oCAAoC;KACvC;IAED,IAAI,OAAO,EAAE;QACT,uBAAc,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;KAClC;AACL,CAAC,CAAC","sourcesContent":["import { isDebugEnabled, isLoggingEnabled } from './debug-state';\n\nimport type { LoggerCallback, LoggerPayload } from './logger/logger';\nimport { _loggerSubject, LogLevel } from './logger/logger';\n\n// eslint-disable-next-line import/prefer-default-export\nexport const onDebug = (callback: LoggerCallback) => {\n // Return early if both debug and logging is disabled\n if (!isDebugEnabled() || !isLoggingEnabled()) {\n return;\n }\n\n let payload: LoggerPayload | undefined;\n\n try {\n payload = callback(LogLevel);\n } catch (e) {\n // eslint-disable-next-line no-empty\n }\n\n if (payload) {\n _loggerSubject.notify(payload);\n }\n};\n"]}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("ts-jest/utils");
|
|
4
|
+
const debug_state_1 = require("./debug-state");
|
|
5
|
+
const debug_1 = require("./debug");
|
|
6
|
+
const logger_1 = require("./logger/logger");
|
|
7
|
+
const default_logger_1 = require("./logger/default-logger");
|
|
8
|
+
jest.mock('./debug-state');
|
|
9
|
+
describe('onDebug util', () => {
|
|
10
|
+
const orgConsole = global.console;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockReturnValue(false);
|
|
13
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(false);
|
|
14
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logger_1.LogLevel.debug);
|
|
15
|
+
// Silent down console
|
|
16
|
+
global.console = {
|
|
17
|
+
...orgConsole,
|
|
18
|
+
error: jest.fn(),
|
|
19
|
+
log: jest.fn(),
|
|
20
|
+
groupCollapsed: jest.fn(),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockClear();
|
|
25
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockClear();
|
|
26
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockClear();
|
|
27
|
+
global.console = orgConsole;
|
|
28
|
+
});
|
|
29
|
+
it('should only execute the given callback when debug and logging is enabled', () => {
|
|
30
|
+
const debugCallbackSpy = jest.fn();
|
|
31
|
+
// disable
|
|
32
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockReturnValue(false);
|
|
33
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(false);
|
|
34
|
+
debug_1.onDebug(debugCallbackSpy);
|
|
35
|
+
expect(debugCallbackSpy).not.toHaveBeenCalled();
|
|
36
|
+
// enable
|
|
37
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockReturnValue(true);
|
|
38
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(true);
|
|
39
|
+
debug_1.onDebug(debugCallbackSpy);
|
|
40
|
+
expect(debugCallbackSpy).toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
it('should not notify the observer when logging is disabled', () => {
|
|
43
|
+
const payload = {
|
|
44
|
+
message: 'foo',
|
|
45
|
+
level: logger_1.LogLevel.error,
|
|
46
|
+
};
|
|
47
|
+
const loggerCallback = jest.fn(() => payload);
|
|
48
|
+
const loggerObserver = jest.fn();
|
|
49
|
+
// need to enable debug in order to run the debugCallback
|
|
50
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockReturnValue(true);
|
|
51
|
+
// disable
|
|
52
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(false);
|
|
53
|
+
default_logger_1.observeLogger(loggerObserver);
|
|
54
|
+
debug_1.onDebug(loggerCallback);
|
|
55
|
+
expect(loggerCallback).not.toHaveBeenCalled();
|
|
56
|
+
expect(loggerObserver).not.toHaveBeenCalled();
|
|
57
|
+
// enable
|
|
58
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(true);
|
|
59
|
+
debug_1.onDebug(loggerCallback);
|
|
60
|
+
expect(loggerCallback).toHaveBeenCalledTimes(1);
|
|
61
|
+
expect(loggerObserver).toHaveBeenCalledWith(payload);
|
|
62
|
+
});
|
|
63
|
+
it('should not throw an error when logger callback fails', () => {
|
|
64
|
+
utils_1.mocked(debug_state_1.isDebugEnabled).mockReturnValue(true);
|
|
65
|
+
utils_1.mocked(debug_state_1.isLoggingEnabled).mockReturnValue(true);
|
|
66
|
+
const callOnDebug = () => {
|
|
67
|
+
const loggerCallback = () => {
|
|
68
|
+
throw new Error('ups');
|
|
69
|
+
};
|
|
70
|
+
debug_1.onDebug(loggerCallback);
|
|
71
|
+
};
|
|
72
|
+
expect(callOnDebug).not.toThrow();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
//# sourceMappingURL=debug.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.test.js","sourceRoot":"","sources":["../../lib/debug.test.ts"],"names":[],"mappings":";;AAAA,yCAAuC;AACvC,+CAA8E;AAC9E,mCAAkC;AAClC,4CAA2C;AAC3C,4DAAwD;AAExD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AAE3B,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC;IAElC,UAAU,CAAC,GAAG,EAAE;QACZ,cAAM,CAAC,4BAAc,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAC9C,cAAM,CAAC,8BAAgB,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAChD,cAAM,CAAC,yBAAW,CAAC,CAAC,eAAe,CAAC,iBAAQ,CAAC,KAAK,CAAC,CAAC;QAEpD,sBAAsB;QACtB,MAAM,CAAC,OAAO,GAAG;YACb,GAAG,UAAU;YACb,KAAK,EAAE,IAAI,CAAC,EAAE,EAAE;YAChB,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE;YACd,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;SAC5B,CAAC;IACN,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACX,cAAM,CAAC,4BAAc,CAAC,CAAC,SAAS,EAAE,CAAC;QACnC,cAAM,CAAC,8BAAgB,CAAC,CAAC,SAAS,EAAE,CAAC;QACrC,cAAM,CAAC,yBAAW,CAAC,CAAC,SAAS,EAAE,CAAC;QAEhC,MAAM,CAAC,OAAO,GAAG,UAAU,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0EAA0E,EAAE,GAAG,E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C,gBAAgB,EAAE,CAAC;QAE9C,SAAS;QACT,cAAM,CAAC,8BAAgB,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAC/C,eAAO,CAAC,cAAc,CAAC,CAAC;QAExB,MAAM,CAAC,cAAc,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;QAChD,MAAM,CAAC,cAAc,CAAC,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAC5D,cAAM,CAAC,4BAAc,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAC7C,cAAM,CAAC,8BAAgB,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAE/C,MAAM,WAAW,GAAG,GAAG,EAAE;YACrB,MAAM,cAAc,GAAG,GAAG,EAAE;gBACxB,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;YAC3B,CAAC,CAAC;YAEF,eAAO,CAAC,cAAc,CAAC,CAAC;QAC5B,CAAC,CAAC;QAEF,MAAM,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IACtC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["import { mocked } from 'ts-jest/utils';\nimport { getLogLevel, isDebugEnabled, isLoggingEnabled } from './debug-state';\nimport { onDebug } from './debug';\nimport { LogLevel } from './logger/logger';\nimport { observeLogger } from './logger/default-logger';\n\njest.mock('./debug-state');\n\ndescribe('onDebug util', () => {\n const orgConsole = global.console;\n\n beforeEach(() => {\n mocked(isDebugEnabled).mockReturnValue(false);\n mocked(isLoggingEnabled).mockReturnValue(false);\n mocked(getLogLevel).mockReturnValue(LogLevel.debug);\n\n // Silent down console\n global.console = {\n ...orgConsole,\n error: jest.fn(),\n log: jest.fn(),\n groupCollapsed: jest.fn(),\n };\n });\n\n afterEach(() => {\n mocked(isDebugEnabled).mockClear();\n mocked(isLoggingEnabled).mockClear();\n mocked(getLogLevel).mockClear();\n\n global.console = orgConsole;\n });\n\n it('should only execute the given callback when debug and logging is enabled', () => {\n const debugCallbackSpy = jest.fn();\n\n // disable\n mocked(isDebugEnabled).mockReturnValue(false);\n mocked(isLoggingEnabled).mockReturnValue(false);\n\n onDebug(debugCallbackSpy);\n\n expect(debugCallbackSpy).not.toHaveBeenCalled();\n\n // enable\n mocked(isDebugEnabled).mockReturnValue(true);\n mocked(isLoggingEnabled).mockReturnValue(true);\n\n onDebug(debugCallbackSpy);\n\n expect(debugCallbackSpy).toHaveBeenCalled();\n });\n\n it('should not notify the observer when logging is disabled', () => {\n const payload = {\n message: 'foo',\n level: LogLevel.error,\n };\n const loggerCallback = jest.fn(() => payload);\n const loggerObserver = jest.fn();\n\n // need to enable debug in order to run the debugCallback\n mocked(isDebugEnabled).mockReturnValue(true);\n // disable\n mocked(isLoggingEnabled).mockReturnValue(false);\n\n observeLogger(loggerObserver);\n onDebug(loggerCallback);\n\n expect(loggerCallback).not.toHaveBeenCalled();\n expect(loggerObserver).not.toHaveBeenCalled();\n\n // enable\n mocked(isLoggingEnabled).mockReturnValue(true);\n onDebug(loggerCallback);\n\n expect(loggerCallback).toHaveBeenCalledTimes(1);\n expect(loggerObserver).toHaveBeenCalledWith(payload);\n });\n\n it('should not throw an error when logger callback fails', () => {\n mocked(isDebugEnabled).mockReturnValue(true);\n mocked(isLoggingEnabled).mockReturnValue(true);\n\n const callOnDebug = () => {\n const loggerCallback = () => {\n throw new Error('ups');\n };\n\n onDebug(loggerCallback);\n };\n\n expect(callOnDebug).not.toThrow();\n });\n});\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var debug_1 = require("./debug");
|
|
4
|
+
Object.defineProperty(exports, "onDebug", { enumerable: true, get: function () { return debug_1.onDebug; } });
|
|
5
|
+
var logger_1 = require("./logger/logger");
|
|
6
|
+
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_1.LogLevel; } });
|
|
7
|
+
var default_logger_1 = require("./logger/default-logger");
|
|
8
|
+
/** @deprecated in 2.1.0 */ Object.defineProperty(exports, "observeLogger", { enumerable: true, get: function () { return default_logger_1.observeLogger; } });
|
|
9
|
+
/** @since in 2.1.0 */ Object.defineProperty(exports, "setLogger", { enumerable: true, get: function () { return default_logger_1.setLogger; } });
|
|
10
|
+
Object.defineProperty(exports, "_deregisterDefaultLogger", { enumerable: true, get: function () { return default_logger_1._deregisterDefaultLogger; } });
|
|
11
|
+
var console_1 = require("./logger/console");
|
|
12
|
+
Object.defineProperty(exports, "consoleLogger", { enumerable: true, get: function () { return console_1.consoleLogger; } });
|
|
13
|
+
var debug_state_1 = require("./debug-state");
|
|
14
|
+
Object.defineProperty(exports, "isDebugEnabled", { enumerable: true, get: function () { return debug_state_1.isDebugEnabled; } });
|
|
15
|
+
Object.defineProperty(exports, "isLoggingEnabled", { enumerable: true, get: function () { return debug_state_1.isLoggingEnabled; } });
|
|
16
|
+
Object.defineProperty(exports, "isDiscoveryEnabled", { enumerable: true, get: function () { return debug_state_1.isDiscoveryEnabled; } });
|
|
17
|
+
Object.defineProperty(exports, "isValidationEnabled", { enumerable: true, get: function () { return debug_state_1.isValidationEnabled; } });
|
|
18
|
+
Object.defineProperty(exports, "setDebugEnabled", { enumerable: true, get: function () { return debug_state_1.setDebugEnabled; } });
|
|
19
|
+
Object.defineProperty(exports, "setLoggingEnabled", { enumerable: true, get: function () { return debug_state_1.setLoggingEnabled; } });
|
|
20
|
+
Object.defineProperty(exports, "setDiscoveryEnabled", { enumerable: true, get: function () { return debug_state_1.setDiscoveryEnabled; } });
|
|
21
|
+
Object.defineProperty(exports, "setValidationEnabled", { enumerable: true, get: function () { return debug_state_1.setValidationEnabled; } });
|
|
22
|
+
Object.defineProperty(exports, "observeStateChange", { enumerable: true, get: function () { return debug_state_1.observeStateChange; } });
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":";;AAAA,iCAAkC;AAAzB,gGAAA,OAAO,OAAA;AAEhB,0CAA2C;AAAlC,kGAAA,QAAQ,OAAA;AAEjB,0DAIiC;AAH7B,2BAA2B,CAAC,+GAAA,aAAa,OAAA;AACzC,sBAAsB,CAAC,2GAAA,SAAS,OAAA;AAChC,0HAAA,wBAAwB,OAAA;AAG5B,4CAAwE;AAAxC,wGAAA,aAAa,OAAA;AAE7C,6CAUuB;AATnB,6GAAA,cAAc,OAAA;AACd,+GAAA,gBAAgB,OAAA;AAChB,iHAAA,kBAAkB,OAAA;AAClB,kHAAA,mBAAmB,OAAA;AACnB,8GAAA,eAAe,OAAA;AACf,gHAAA,iBAAiB,OAAA;AACjB,kHAAA,mBAAmB,OAAA;AACnB,mHAAA,oBAAoB,OAAA;AACpB,iHAAA,kBAAkB,OAAA","sourcesContent":["export { onDebug } from './debug';\nexport type { LoggerPayload } from './logger/logger';\nexport { LogLevel } from './logger/logger';\n\nexport {\n /** @deprecated in 2.1.0 */ observeLogger,\n /** @since in 2.1.0 */ setLogger,\n _deregisterDefaultLogger,\n} from './logger/default-logger';\n\nexport { /** @since in 2.1.0 */ consoleLogger } from './logger/console';\n\nexport {\n isDebugEnabled,\n isLoggingEnabled,\n isDiscoveryEnabled,\n isValidationEnabled,\n setDebugEnabled,\n setLoggingEnabled,\n setDiscoveryEnabled,\n setValidationEnabled,\n observeStateChange,\n} from './debug-state';\n"]}
|