@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
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.consoleLogger = void 0;
|
|
4
|
+
const logger_1 = require("./logger");
|
|
5
|
+
const debug_state_1 = require("../debug-state");
|
|
6
|
+
const getLoggingFromLevel = (level) => {
|
|
7
|
+
switch (level) {
|
|
8
|
+
case logger_1.LogLevel.error:
|
|
9
|
+
return 'error';
|
|
10
|
+
case logger_1.LogLevel.debug:
|
|
11
|
+
return 'debug';
|
|
12
|
+
case logger_1.LogLevel.info:
|
|
13
|
+
return 'info';
|
|
14
|
+
case logger_1.LogLevel.warn:
|
|
15
|
+
case logger_1.LogLevel.deprecation:
|
|
16
|
+
return 'warn';
|
|
17
|
+
default:
|
|
18
|
+
return 'debug';
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
// Naive implementation but we can't use bitwise operators :(
|
|
22
|
+
const debugLevels = Object.values(logger_1.LogLevel);
|
|
23
|
+
const errorLevels = debugLevels.filter((level) => level !== logger_1.LogLevel.debug);
|
|
24
|
+
const warnLevels = errorLevels.filter((level) => level !== logger_1.LogLevel.error);
|
|
25
|
+
const deprecationLevels = warnLevels.filter((level) => level !== logger_1.LogLevel.warn);
|
|
26
|
+
const infoLevels = deprecationLevels.filter((level) => level !== logger_1.LogLevel.deprecation);
|
|
27
|
+
const levelsMap = {
|
|
28
|
+
[logger_1.LogLevel.debug]: debugLevels,
|
|
29
|
+
[logger_1.LogLevel.error]: errorLevels,
|
|
30
|
+
[logger_1.LogLevel.warn]: warnLevels,
|
|
31
|
+
[logger_1.LogLevel.deprecation]: deprecationLevels,
|
|
32
|
+
[logger_1.LogLevel.info]: infoLevels,
|
|
33
|
+
};
|
|
34
|
+
function shouldDisplayLog(logLevel, eventLogLevel) {
|
|
35
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
const levels = levelsMap[logLevel];
|
|
39
|
+
return Array.isArray(levels) ? levels.includes(eventLogLevel) : false;
|
|
40
|
+
}
|
|
41
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
42
|
+
exports.consoleLogger = (payload) => {
|
|
43
|
+
const eventLogLevel = payload.level;
|
|
44
|
+
const logLevel = debug_state_1.getLogLevel();
|
|
45
|
+
if (!shouldDisplayLog(logLevel, eventLogLevel)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const consoleKey = getLoggingFromLevel(eventLogLevel);
|
|
49
|
+
const deprecationWarning = payload.level === logger_1.LogLevel.deprecation
|
|
50
|
+
? `⚠️⚠️⚠️ DEPRECATION WARNING ⚠️⚠️⚠️
|
|
51
|
+
`
|
|
52
|
+
: '';
|
|
53
|
+
console[consoleKey](`${deprecationWarning}[Atlassian Client-side Extensions]: ${payload.message}`);
|
|
54
|
+
// Verbose logging
|
|
55
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
56
|
+
console.groupCollapsed(' <click here for more details about above message>');
|
|
57
|
+
console.log({
|
|
58
|
+
components: payload.components,
|
|
59
|
+
meta: payload.meta,
|
|
60
|
+
});
|
|
61
|
+
console.groupEnd();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=console.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console.js","sourceRoot":"","sources":["../../../lib/logger/console.ts"],"names":[],"mappings":";;;AACA,qCAAoC;AACpC,gDAA6C;AAE7C,MAAM,mBAAmB,GAAG,CAAC,KAAe,EAAwB,EAAE;IAClE,QAAQ,KAAK,EAAE;QACX,KAAK,iBAAQ,CAAC,KAAK;YACf,OAAO,OAAO,CAAC;QACnB,KAAK,iBAAQ,CAAC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,CAAC;KACf;IAED,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;IAEnC,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC1E,CAAC;AAED,wDAAwD;AAC3C,QAAA,aAAa,GAAG,CAAC,OAAsB,EAAE,EAAE;IACpD,MAAM,aAAa,GAAG,OAAO,CAAC,KAAK,CAAC;IACpC,MAAM,QAAQ,GAAG,yBAAW,EAAE,CAAC;IAE/B,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,aAAa,CAAC,EAAE;QAC5C,OAAO;KACV;IAED,MAAM,UAAU,GAAG,mBAAmB,CAAC,aAAa,CAAC,CAAC;IACtD,MAAM,kBAAkB,GACpB,OAAO,CAAC,KAAK,KAAK,iBAAQ,CAAC,WAAW;QAClC,CAAC,CAAC;CACb;QACW,CAAC,CAAC,EAAE,CAAC;IAEb,OAAO,CAAC,UAAU,CAAC,CAAC,GAAG,kBAAkB,uCAAuC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IAEnG,kBAAkB;IAClB,IAAI,QAAQ,KAAK,iBAAQ,CAAC,KAAK,EAAE;QAC7B,OAAO,CAAC,cAAc,CAAC,qDAAqD,CAAC,CAAC;QAC9E,OAAO,CAAC,GAAG,CAAC;YACR,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,IAAI,EAAE,OAAO,CAAC,IAAI;SACrB,CAAC,CAAC;QACH,OAAO,CAAC,QAAQ,EAAE,CAAC;KACtB;AACL,CAAC,CAAC","sourcesContent":["import type { LoggerPayload } from './logger';\nimport { LogLevel } from './logger';\nimport { getLogLevel } from '../debug-state';\n\nconst getLoggingFromLevel = (level: LogLevel): keyof typeof console => {\n switch (level) {\n case LogLevel.error:\n return 'error';\n case LogLevel.debug:\n return 'debug';\n case LogLevel.info:\n return 'info';\n case LogLevel.warn:\n case LogLevel.deprecation:\n return 'warn';\n default:\n return 'debug';\n }\n};\n\n// Naive implementation but we can't use bitwise operators :(\nconst debugLevels = Object.values(LogLevel);\nconst errorLevels = debugLevels.filter((level) => level !== LogLevel.debug);\nconst warnLevels = errorLevels.filter((level) => level !== LogLevel.error);\nconst deprecationLevels = warnLevels.filter((level) => level !== LogLevel.warn);\nconst infoLevels = deprecationLevels.filter((level) => level !== LogLevel.deprecation);\n\nconst levelsMap: { [key: string]: string[] } = {\n [LogLevel.debug]: debugLevels,\n [LogLevel.error]: errorLevels,\n [LogLevel.warn]: warnLevels,\n [LogLevel.deprecation]: deprecationLevels,\n [LogLevel.info]: infoLevels,\n};\n\nfunction shouldDisplayLog(logLevel: LogLevel, eventLogLevel: LogLevel) {\n if (logLevel === LogLevel.debug) {\n return true;\n }\n\n const levels = levelsMap[logLevel];\n\n return Array.isArray(levels) ? levels.includes(eventLogLevel) : false;\n}\n\n// eslint-disable-next-line import/prefer-default-export\nexport const consoleLogger = (payload: LoggerPayload) => {\n const eventLogLevel = payload.level;\n const logLevel = getLogLevel();\n\n if (!shouldDisplayLog(logLevel, eventLogLevel)) {\n return;\n }\n\n const consoleKey = getLoggingFromLevel(eventLogLevel);\n const deprecationWarning =\n payload.level === LogLevel.deprecation\n ? `⚠️⚠️⚠️ DEPRECATION WARNING ⚠️⚠️⚠️\n`\n : '';\n\n console[consoleKey](`${deprecationWarning}[Atlassian Client-side Extensions]: ${payload.message}`);\n\n // Verbose logging\n if (logLevel === LogLevel.debug) {\n console.groupCollapsed(' <click here for more details about above message>');\n console.log({\n components: payload.components,\n meta: payload.meta,\n });\n console.groupEnd();\n }\n};\n"]}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("ts-jest/utils");
|
|
4
|
+
const logger_1 = require("./logger");
|
|
5
|
+
const console_1 = require("./console");
|
|
6
|
+
const debug_state_1 = require("../debug-state");
|
|
7
|
+
jest.mock('../debug-state');
|
|
8
|
+
describe('console logger', () => {
|
|
9
|
+
const orgConsole = global.console;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
global.console = {
|
|
12
|
+
...orgConsole,
|
|
13
|
+
log: jest.fn(),
|
|
14
|
+
error: jest.fn(),
|
|
15
|
+
debug: jest.fn(),
|
|
16
|
+
info: jest.fn(),
|
|
17
|
+
warn: jest.fn(),
|
|
18
|
+
groupCollapsed: jest.fn(),
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockClear();
|
|
23
|
+
global.console = orgConsole;
|
|
24
|
+
});
|
|
25
|
+
const getEventPayload = (level) => ({
|
|
26
|
+
level,
|
|
27
|
+
message: 'Hello!',
|
|
28
|
+
});
|
|
29
|
+
describe('"DEBUG" level', () => {
|
|
30
|
+
it.each([
|
|
31
|
+
// prettier-ignore-start
|
|
32
|
+
[logger_1.LogLevel.debug],
|
|
33
|
+
])('should display "DEBUG" log when log level is set to "%s"', (logLevel) => {
|
|
34
|
+
// given
|
|
35
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
36
|
+
const event = getEventPayload(logger_1.LogLevel.debug);
|
|
37
|
+
// when
|
|
38
|
+
console_1.consoleLogger(event);
|
|
39
|
+
// then
|
|
40
|
+
expect(global.console.debug).toHaveBeenCalledWith(expect.stringContaining('Hello!'));
|
|
41
|
+
expect(global.console.groupCollapsed).toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
it.each([
|
|
44
|
+
// prettier-ignore-start
|
|
45
|
+
[logger_1.LogLevel.error],
|
|
46
|
+
[logger_1.LogLevel.warn],
|
|
47
|
+
[logger_1.LogLevel.deprecation],
|
|
48
|
+
[logger_1.LogLevel.info],
|
|
49
|
+
])('should not display "DEBUG" log when log level is set to "%s"', (logLevel) => {
|
|
50
|
+
// given
|
|
51
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
52
|
+
const event = getEventPayload(logger_1.LogLevel.debug);
|
|
53
|
+
// when
|
|
54
|
+
console_1.consoleLogger(event);
|
|
55
|
+
// then
|
|
56
|
+
expect(global.console.debug).not.toHaveBeenCalled();
|
|
57
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('"ERROR" level', () => {
|
|
61
|
+
it.each([
|
|
62
|
+
// prettier-ignore-start
|
|
63
|
+
[logger_1.LogLevel.debug],
|
|
64
|
+
[logger_1.LogLevel.error],
|
|
65
|
+
])('should display "ERROR" log when log level is set to "%s"', (logLevel) => {
|
|
66
|
+
// given
|
|
67
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
68
|
+
const event = getEventPayload(logger_1.LogLevel.error);
|
|
69
|
+
// when
|
|
70
|
+
console_1.consoleLogger(event);
|
|
71
|
+
// then
|
|
72
|
+
expect(global.console.error).toHaveBeenCalledWith(expect.stringContaining('Hello!'));
|
|
73
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
74
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
75
|
+
expect(global.console.groupCollapsed).toHaveBeenCalled();
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
79
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
it.each([
|
|
83
|
+
// prettier-ignore-start
|
|
84
|
+
[logger_1.LogLevel.warn],
|
|
85
|
+
[logger_1.LogLevel.deprecation],
|
|
86
|
+
[logger_1.LogLevel.info],
|
|
87
|
+
])('should not display "ERROR" log when log level is set to "%s"', (logLevel) => {
|
|
88
|
+
// given
|
|
89
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
90
|
+
const event = getEventPayload(logger_1.LogLevel.error);
|
|
91
|
+
// when
|
|
92
|
+
console_1.consoleLogger(event);
|
|
93
|
+
// then
|
|
94
|
+
expect(global.console.error).not.toHaveBeenCalled();
|
|
95
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
describe('"WARN" level', () => {
|
|
99
|
+
it.each([
|
|
100
|
+
// prettier-ignore-start
|
|
101
|
+
[logger_1.LogLevel.debug],
|
|
102
|
+
[logger_1.LogLevel.error],
|
|
103
|
+
[logger_1.LogLevel.warn],
|
|
104
|
+
])('should display "WARN" log when log level is set to "%s"', (logLevel) => {
|
|
105
|
+
// given
|
|
106
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
107
|
+
const event = getEventPayload(logger_1.LogLevel.warn);
|
|
108
|
+
// when
|
|
109
|
+
console_1.consoleLogger(event);
|
|
110
|
+
// then
|
|
111
|
+
expect(global.console.warn).toHaveBeenCalledWith(expect.stringContaining('Hello!'));
|
|
112
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
113
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
114
|
+
expect(global.console.groupCollapsed).toHaveBeenCalled();
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
118
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
it.each([
|
|
122
|
+
// prettier-ignore-start
|
|
123
|
+
[logger_1.LogLevel.deprecation],
|
|
124
|
+
[logger_1.LogLevel.info],
|
|
125
|
+
])('should not display "WARN" log when log level is set to "%s"', (logLevel) => {
|
|
126
|
+
// given
|
|
127
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
128
|
+
const event = getEventPayload(logger_1.LogLevel.warn);
|
|
129
|
+
// when
|
|
130
|
+
console_1.consoleLogger(event);
|
|
131
|
+
// then
|
|
132
|
+
expect(global.console.warn).not.toHaveBeenCalled();
|
|
133
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('"DEPRECATION" level', () => {
|
|
137
|
+
it.each([
|
|
138
|
+
// prettier-ignore-start
|
|
139
|
+
[logger_1.LogLevel.debug],
|
|
140
|
+
[logger_1.LogLevel.error],
|
|
141
|
+
[logger_1.LogLevel.warn],
|
|
142
|
+
])('should display "DEPRECATION" log when log level is set to "%s"', (logLevel) => {
|
|
143
|
+
// given
|
|
144
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
145
|
+
const event = getEventPayload(logger_1.LogLevel.deprecation);
|
|
146
|
+
// when
|
|
147
|
+
console_1.consoleLogger(event);
|
|
148
|
+
// then
|
|
149
|
+
expect(global.console.warn).toHaveBeenCalledWith(expect.stringContaining('Hello!'));
|
|
150
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
151
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
152
|
+
expect(global.console.groupCollapsed).toHaveBeenCalled();
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
156
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
it.each([
|
|
160
|
+
// prettier-ignore-start
|
|
161
|
+
[logger_1.LogLevel.info],
|
|
162
|
+
])('should not display "DEPRECATION" log when log level is set to "%s"', (logLevel) => {
|
|
163
|
+
// given
|
|
164
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
165
|
+
const event = getEventPayload(logger_1.LogLevel.deprecation);
|
|
166
|
+
// when
|
|
167
|
+
console_1.consoleLogger(event);
|
|
168
|
+
// then
|
|
169
|
+
expect(global.console.warn).not.toHaveBeenCalled();
|
|
170
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe('"INFO" level', () => {
|
|
174
|
+
it.each([
|
|
175
|
+
// prettier-ignore-start
|
|
176
|
+
[logger_1.LogLevel.debug],
|
|
177
|
+
[logger_1.LogLevel.error],
|
|
178
|
+
[logger_1.LogLevel.warn],
|
|
179
|
+
[logger_1.LogLevel.deprecation],
|
|
180
|
+
[logger_1.LogLevel.info],
|
|
181
|
+
])('should display "INFO" log when log level is set to "%s"', (logLevel) => {
|
|
182
|
+
// given
|
|
183
|
+
utils_1.mocked(debug_state_1.getLogLevel).mockReturnValue(logLevel);
|
|
184
|
+
const event = getEventPayload(logger_1.LogLevel.info);
|
|
185
|
+
// when
|
|
186
|
+
console_1.consoleLogger(event);
|
|
187
|
+
// then
|
|
188
|
+
expect(global.console.info).toHaveBeenCalledWith(expect.stringContaining('Hello!'));
|
|
189
|
+
if (logLevel === logger_1.LogLevel.debug) {
|
|
190
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
191
|
+
expect(global.console.groupCollapsed).toHaveBeenCalled();
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
// eslint-disable-next-line jest/no-conditional-expect
|
|
195
|
+
expect(global.console.groupCollapsed).not.toHaveBeenCalled();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
//# sourceMappingURL=console.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console.test.js","sourceRoot":"","sources":["../../../lib/logger/console.test.ts"],"names":[],"mappings":";;AAAA,yCAAuC;AAEvC,qCAAoC;AACpC,uCAA0C;AAC1C,gDAA6C;AAE7C,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;AAE5B,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC5B,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC;IAElC,UAAU,CAAC,GAAG,EAAE;QACZ,MAA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E,CAAC;aAChE;QACL,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["import { mocked } from 'ts-jest/utils';\nimport type { LoggerPayload } from './logger';\nimport { LogLevel } from './logger';\nimport { consoleLogger } from './console';\nimport { getLogLevel } from '../debug-state';\n\njest.mock('../debug-state');\n\ndescribe('console logger', () => {\n const orgConsole = global.console;\n\n beforeEach(() => {\n global.console = {\n ...orgConsole,\n log: jest.fn(),\n error: jest.fn(),\n debug: jest.fn(),\n info: jest.fn(),\n warn: jest.fn(),\n groupCollapsed: jest.fn(),\n };\n });\n\n afterEach(() => {\n mocked(getLogLevel).mockClear();\n\n global.console = orgConsole;\n });\n\n const getEventPayload = (level: LogLevel): LoggerPayload => ({\n level,\n message: 'Hello!',\n });\n\n describe('\"DEBUG\" level', () => {\n it.each([\n // prettier-ignore-start\n [LogLevel.debug],\n // prettier-ignore-end\n ])('should display \"DEBUG\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.debug);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.debug).toHaveBeenCalledWith(expect.stringContaining('Hello!'));\n expect(global.console.groupCollapsed).toHaveBeenCalled();\n });\n\n it.each([\n // prettier-ignore-start\n [LogLevel.error],\n [LogLevel.warn],\n [LogLevel.deprecation],\n [LogLevel.info],\n // prettier-ignore-end\n ])('should not display \"DEBUG\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.debug);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.debug).not.toHaveBeenCalled();\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n });\n });\n\n describe('\"ERROR\" level', () => {\n it.each([\n // prettier-ignore-start\n [LogLevel.debug],\n [LogLevel.error],\n // prettier-ignore-end\n ])('should display \"ERROR\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.error);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.error).toHaveBeenCalledWith(expect.stringContaining('Hello!'));\n\n if (logLevel === LogLevel.debug) {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).toHaveBeenCalled();\n } else {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n }\n });\n\n it.each([\n // prettier-ignore-start\n [LogLevel.warn],\n [LogLevel.deprecation],\n [LogLevel.info],\n // prettier-ignore-end\n ])('should not display \"ERROR\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.error);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.error).not.toHaveBeenCalled();\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n });\n });\n\n describe('\"WARN\" level', () => {\n it.each([\n // prettier-ignore-start\n [LogLevel.debug],\n [LogLevel.error],\n [LogLevel.warn],\n // prettier-ignore-end\n ])('should display \"WARN\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.warn);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.warn).toHaveBeenCalledWith(expect.stringContaining('Hello!'));\n\n if (logLevel === LogLevel.debug) {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).toHaveBeenCalled();\n } else {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n }\n });\n\n it.each([\n // prettier-ignore-start\n [LogLevel.deprecation],\n [LogLevel.info],\n // prettier-ignore-end\n ])('should not display \"WARN\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.warn);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.warn).not.toHaveBeenCalled();\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n });\n });\n\n describe('\"DEPRECATION\" level', () => {\n it.each([\n // prettier-ignore-start\n [LogLevel.debug],\n [LogLevel.error],\n [LogLevel.warn],\n // prettier-ignore-end\n ])('should display \"DEPRECATION\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.deprecation);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.warn).toHaveBeenCalledWith(expect.stringContaining('Hello!'));\n\n if (logLevel === LogLevel.debug) {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).toHaveBeenCalled();\n } else {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n }\n });\n\n it.each([\n // prettier-ignore-start\n [LogLevel.info],\n // prettier-ignore-end\n ])('should not display \"DEPRECATION\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.deprecation);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.warn).not.toHaveBeenCalled();\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n });\n });\n\n describe('\"INFO\" level', () => {\n it.each([\n // prettier-ignore-start\n [LogLevel.debug],\n [LogLevel.error],\n [LogLevel.warn],\n [LogLevel.deprecation],\n [LogLevel.info],\n // prettier-ignore-end\n ])('should display \"INFO\" log when log level is set to \"%s\"', (logLevel: LogLevel) => {\n // given\n mocked(getLogLevel).mockReturnValue(logLevel);\n const event = getEventPayload(LogLevel.info);\n\n // when\n consoleLogger(event);\n\n // then\n expect(global.console.info).toHaveBeenCalledWith(expect.stringContaining('Hello!'));\n\n if (logLevel === LogLevel.debug) {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).toHaveBeenCalled();\n } else {\n // eslint-disable-next-line jest/no-conditional-expect\n expect(global.console.groupCollapsed).not.toHaveBeenCalled();\n }\n });\n });\n});\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._deregisterDefaultLogger = exports.setLogger = exports.defaultLogger = exports.observeLogger = void 0;
|
|
4
|
+
const console_1 = require("./console");
|
|
5
|
+
const debug_subjects_1 = require("../debug-subjects");
|
|
6
|
+
const logger_1 = require("./logger");
|
|
7
|
+
/**
|
|
8
|
+
* We need to keep and expose the "observeLogger" function for a legacy reason.
|
|
9
|
+
* It should be removed in CSEv3
|
|
10
|
+
*
|
|
11
|
+
* @deprecated
|
|
12
|
+
*/
|
|
13
|
+
exports.observeLogger = (observer) => {
|
|
14
|
+
return debug_subjects_1.observeDebugSubject("logger" /* Logger */, observer);
|
|
15
|
+
};
|
|
16
|
+
const registerConsoleLogger = () => {
|
|
17
|
+
// Singleton workaround as this module may be executed more than once.
|
|
18
|
+
// We only ever want one global default logger, however.
|
|
19
|
+
if (window.____c_p_d_l_d !== undefined) {
|
|
20
|
+
return window.____c_p_d_l_d;
|
|
21
|
+
}
|
|
22
|
+
window.____c_p_d_l_d = exports.observeLogger(console_1.consoleLogger);
|
|
23
|
+
return window.____c_p_d_l_d;
|
|
24
|
+
};
|
|
25
|
+
const deregisterLogger = () => {
|
|
26
|
+
if (window.____c_p_d_l_d === undefined) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
window.____c_p_d_l_d.unsubscribe();
|
|
30
|
+
};
|
|
31
|
+
exports.defaultLogger = registerConsoleLogger();
|
|
32
|
+
/**
|
|
33
|
+
* Added in CSE 2.1.0
|
|
34
|
+
*
|
|
35
|
+
* @param logger
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
exports.setLogger = (logger) => {
|
|
39
|
+
if (typeof logger !== 'function') {
|
|
40
|
+
throw new Error(`Client-side Extension: The provided logger needs to be a function`);
|
|
41
|
+
}
|
|
42
|
+
deregisterLogger();
|
|
43
|
+
logger_1._loggerSubject.prune();
|
|
44
|
+
debug_subjects_1.observeDebugSubject("logger" /* Logger */, logger);
|
|
45
|
+
};
|
|
46
|
+
const deregisterDefaultLogger = () => exports.defaultLogger.unsubscribe();
|
|
47
|
+
exports._deregisterDefaultLogger = deregisterDefaultLogger;
|
|
48
|
+
//# sourceMappingURL=default-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-logger.js","sourceRoot":"","sources":["../../../lib/logger/default-logger.ts"],"names":[],"mappings":";;;AAEA,uCAA0C;AAC1C,sDAAuE;AAEvE,qCAA0C;AAS1C;;;;;GAKG;AACU,QAAA,aAAa,GAAG,CAAC,QAAiC,EAAgB,EAAE;IAC7E,OAAO,oCAAmB,wBAAsC,QAAQ,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,GAAiB,EAAE;IAC7C,sEAAsE;IACtE,wDAAwD;IACxD,IAAI,MAAM,CAAC,aAAa,KAAK,SAAS,EAAE;QACpC,OAAO,MAAM,CAAC,aAAa,CAAC;KAC/B;IAED,MAAM,CAAC,aAAa,GAAG,qBAAa,CAAC,uBAAa,CAAC,CAAC;IAEpD,OAAO,MAAM,CAAC,aAAa,CAAC;AAChC,CAAC,CAAC;AAEF,MAAM,gBAAgB,GAAG,GAAS,EAAE;IAChC,IAAI,MAAM,CAAC,aAAa,KAAK,SAAS,EAAE;QACpC,OAAO;KACV;IAED,MAAM,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;AACvC,CAAC,CAAC;AAEW,QAAA,aAAa,GAAG,qBAAqB,EAAE,CAAC;AAErD;;;;;GAKG;AACU,QAAA,SAAS,GAAG,CAAC,MAA+B,EAAQ,EAAE;IAC/D,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;KACxF;IAED,gBAAgB,EAAE,CAAC;IACnB,uBAAc,CAAC,KAAK,EAAE,CAAC;IAEvB,oCAAmB,wBAAsC,MAAM,CAAC,CAAC;AACrE,CAAC,CAAC;AAEF,MAAM,uBAAuB,GAAG,GAAG,EAAE,CAAC,qBAAa,CAAC,WAAW,EAAE,CAAC;AAG9B,2DAAwB","sourcesContent":["/* eslint-disable no-underscore-dangle */\nimport type { Observer, Subscription } from '@atlassian/clientside-extensions-base';\nimport { consoleLogger } from './console';\nimport { DebugSubjects, observeDebugSubject } from '../debug-subjects';\nimport type { LoggerPayload } from './logger';\nimport { _loggerSubject } from './logger';\n\ndeclare global {\n interface Window {\n // clientside extension default logging deregister\n ____c_p_d_l_d?: Subscription;\n }\n}\n\n/**\n * We need to keep and expose the \"observeLogger\" function for a legacy reason.\n * It should be removed in CSEv3\n *\n * @deprecated\n */\nexport const observeLogger = (observer: Observer<LoggerPayload>): Subscription => {\n return observeDebugSubject<LoggerPayload>(DebugSubjects.Logger, observer);\n};\n\nconst registerConsoleLogger = (): Subscription => {\n // Singleton workaround as this module may be executed more than once.\n // We only ever want one global default logger, however.\n if (window.____c_p_d_l_d !== undefined) {\n return window.____c_p_d_l_d;\n }\n\n window.____c_p_d_l_d = observeLogger(consoleLogger);\n\n return window.____c_p_d_l_d;\n};\n\nconst deregisterLogger = (): void => {\n if (window.____c_p_d_l_d === undefined) {\n return;\n }\n\n window.____c_p_d_l_d.unsubscribe();\n};\n\nexport const defaultLogger = registerConsoleLogger();\n\n/**\n * Added in CSE 2.1.0\n *\n * @param logger\n * @public\n */\nexport const setLogger = (logger: Observer<LoggerPayload>): void => {\n if (typeof logger !== 'function') {\n throw new Error(`Client-side Extension: The provided logger needs to be a function`);\n }\n\n deregisterLogger();\n _loggerSubject.prune();\n\n observeDebugSubject<LoggerPayload>(DebugSubjects.Logger, logger);\n};\n\nconst deregisterDefaultLogger = () => defaultLogger.unsubscribe();\n\n// @VisibleForTesting\nexport { deregisterDefaultLogger as _deregisterDefaultLogger };\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._loggerSubject = exports.LogLevel = void 0;
|
|
4
|
+
const clientside_extensions_base_1 = require("@atlassian/clientside-extensions-base");
|
|
5
|
+
const debug_subjects_1 = require("../debug-subjects");
|
|
6
|
+
var LogLevel;
|
|
7
|
+
(function (LogLevel) {
|
|
8
|
+
LogLevel["debug"] = "DEBUG";
|
|
9
|
+
LogLevel["error"] = "ERROR";
|
|
10
|
+
LogLevel["warn"] = "WARN";
|
|
11
|
+
LogLevel["deprecation"] = "DEPRECATION";
|
|
12
|
+
LogLevel["info"] = "INFO";
|
|
13
|
+
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
|
|
14
|
+
const loggerSubject = debug_subjects_1.registerDebugSubject("logger" /* Logger */, () => new clientside_extensions_base_1.ReplaySubject(20));
|
|
15
|
+
exports._loggerSubject = loggerSubject;
|
|
16
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../lib/logger/logger.ts"],"names":[],"mappings":";;;AAAA,sFAAsE;AACtE,sDAAwE;AAExE,IAAY,QAMX;AAND,WAAY,QAAQ;IAChB,2BAAe,CAAA;IACf,2BAAe,CAAA;IACf,yBAAa,CAAA;IACb,uCAA2B,CAAA;IAC3B,yBAAa,CAAA;AACjB,CAAC,EANW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAMnB;AAaD,MAAM,aAAa,GAAG,qCAAoB,wBAAuB,GAAG,EAAE,CAAC,IAAI,0CAAa,CAAgB,EAAE,CAAC,CAAC,CAAC;AAGnF,uCAAc","sourcesContent":["import { ReplaySubject } from '@atlassian/clientside-extensions-base';\nimport { DebugSubjects, registerDebugSubject } from '../debug-subjects';\n\nexport enum LogLevel {\n debug = 'DEBUG', // Logs all the things\n error = 'ERROR',\n warn = 'WARN',\n deprecation = 'DEPRECATION',\n info = 'INFO', // The lowest level\n}\n\nexport type LoggerCallback = (levels: typeof LogLevel) => LoggerPayload;\n\nexport type LoggerPayload = {\n level: LogLevel;\n message: string;\n components?: string | string[];\n meta?: {\n [key: string]: unknown;\n };\n};\n\nconst loggerSubject = registerDebugSubject(DebugSubjects.Logger, () => new ReplaySubject<LoggerPayload>(20));\n\n// Exported for internal use only\nexport { loggerSubject as _loggerSubject };\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* eslint-disable no-underscore-dangle */
|
|
2
|
+
import { LogLevel } from './logger/logger';
|
|
3
|
+
/**
|
|
4
|
+
* Using async imports in order to test process.env.NODE_ENV usage in module
|
|
5
|
+
* https://stackoverflow.com/a/48042799
|
|
6
|
+
*/
|
|
7
|
+
describe('debug-state initialization', () => {
|
|
8
|
+
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// reset NODE_ENV to whatever it was.
|
|
11
|
+
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
|
|
12
|
+
// reset cache of loaded modules. This will ensure that the module is re-initialized
|
|
13
|
+
// with the new NODE_ENV value
|
|
14
|
+
delete window.____c_p_d;
|
|
15
|
+
jest.resetModules();
|
|
16
|
+
});
|
|
17
|
+
it('should be set to FALSE if process.env.NODE_ENV is "production"', async () => {
|
|
18
|
+
process.env.NODE_ENV = 'production';
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
20
|
+
const debugStateModule = await import('./debug-state');
|
|
21
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
22
|
+
expect(window.____c_p_d.debug).toBe(false);
|
|
23
|
+
expect(window.____c_p_d.logging).toBe(false);
|
|
24
|
+
expect(window.____c_p_d.validation).toBe(false);
|
|
25
|
+
expect(window.____c_p_d.discovery).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
it('should set the default log level to "ERROR" if process.env.NODE_ENV is "production"', async () => {
|
|
28
|
+
process.env.NODE_ENV = 'production';
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
await import('./debug-state');
|
|
31
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
32
|
+
expect(window.____c_p_d.logLevel).toBe(LogLevel.error);
|
|
33
|
+
});
|
|
34
|
+
it('should be set the default log level to "INFO" if process.env.NODE_ENV is different than "production"', async () => {
|
|
35
|
+
process.env.NODE_ENV = 'development';
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
37
|
+
await import('./debug-state');
|
|
38
|
+
expect(window.____c_p_d.__initialized).toBe(true);
|
|
39
|
+
expect(window.____c_p_d.logLevel).toBe(LogLevel.info);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
//# 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,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;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;QAEp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AAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,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,152 @@
|
|
|
1
|
+
/* eslint-disable no-underscore-dangle */
|
|
2
|
+
var _a;
|
|
3
|
+
import { ReplaySubject } from '@atlassian/clientside-extensions-base';
|
|
4
|
+
import { observeDebugSubject, registerDebugSubject } from './debug-subjects';
|
|
5
|
+
import { LogLevel } from './logger/logger';
|
|
6
|
+
var ClientExtensionDebugTypes;
|
|
7
|
+
(function (ClientExtensionDebugTypes) {
|
|
8
|
+
ClientExtensionDebugTypes["debug"] = "debug";
|
|
9
|
+
ClientExtensionDebugTypes["logging"] = "logging";
|
|
10
|
+
ClientExtensionDebugTypes["validation"] = "validation";
|
|
11
|
+
ClientExtensionDebugTypes["discovery"] = "discovery";
|
|
12
|
+
/** @since 2.1.0 */
|
|
13
|
+
ClientExtensionDebugTypes["logLevel"] = "logLevel";
|
|
14
|
+
})(ClientExtensionDebugTypes || (ClientExtensionDebugTypes = {}));
|
|
15
|
+
const defineDebugGlobal = (previousGlobal) => {
|
|
16
|
+
// initialize as boolean
|
|
17
|
+
const debugGlobal = Object.create(null);
|
|
18
|
+
const debugStates = {
|
|
19
|
+
debug: false,
|
|
20
|
+
logging: false,
|
|
21
|
+
validation: false,
|
|
22
|
+
discovery: false,
|
|
23
|
+
/** @since 2.1.0 */
|
|
24
|
+
// In dev mode we set default loglevel to INFO
|
|
25
|
+
logLevel: process.env.NODE_ENV === 'production' ? LogLevel.error : LogLevel.info,
|
|
26
|
+
};
|
|
27
|
+
const debugStateSubject = registerDebugSubject("state" /* State */, () => new ReplaySubject(1));
|
|
28
|
+
const properties = {
|
|
29
|
+
debug: {
|
|
30
|
+
get() {
|
|
31
|
+
return debugStates.debug;
|
|
32
|
+
},
|
|
33
|
+
set(val) {
|
|
34
|
+
debugStates.debug = Boolean(val);
|
|
35
|
+
debugStateSubject.notify(debugStates);
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
logging: {
|
|
39
|
+
get() {
|
|
40
|
+
return debugStates.logging;
|
|
41
|
+
},
|
|
42
|
+
set(val) {
|
|
43
|
+
debugStates.logging = Boolean(val);
|
|
44
|
+
debugStateSubject.notify(debugStates);
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
validation: {
|
|
48
|
+
get() {
|
|
49
|
+
return debugStates.validation;
|
|
50
|
+
},
|
|
51
|
+
set(val) {
|
|
52
|
+
debugStates.validation = Boolean(val);
|
|
53
|
+
debugStateSubject.notify(debugStates);
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
discovery: {
|
|
57
|
+
get() {
|
|
58
|
+
return debugStates.discovery;
|
|
59
|
+
},
|
|
60
|
+
set(val) {
|
|
61
|
+
debugStates.discovery = Boolean(val);
|
|
62
|
+
debugStateSubject.notify(debugStates);
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
/** @since 2.1.0 */
|
|
66
|
+
logLevel: {
|
|
67
|
+
get() {
|
|
68
|
+
return debugStates.logLevel;
|
|
69
|
+
},
|
|
70
|
+
set(val) {
|
|
71
|
+
let level = String(val).toUpperCase();
|
|
72
|
+
// Handle runtime errors
|
|
73
|
+
if (!Object.values(LogLevel).includes(level)) {
|
|
74
|
+
level = LogLevel.info;
|
|
75
|
+
}
|
|
76
|
+
debugStates.logLevel = level;
|
|
77
|
+
debugStateSubject.notify(debugStates);
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
__initialized: { value: true, writable: false },
|
|
81
|
+
};
|
|
82
|
+
Object.defineProperties(debugGlobal, properties);
|
|
83
|
+
Object.defineProperty(window, '____c_p_d', {
|
|
84
|
+
configurable: true,
|
|
85
|
+
get() {
|
|
86
|
+
return debugGlobal;
|
|
87
|
+
},
|
|
88
|
+
set(val) {
|
|
89
|
+
// its a boolean
|
|
90
|
+
if (val === true || val === false) {
|
|
91
|
+
debugGlobal.debug = val;
|
|
92
|
+
debugGlobal.logging = val;
|
|
93
|
+
debugGlobal.validation = val;
|
|
94
|
+
debugGlobal.discovery = val;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
// we can handle booleans - like above
|
|
98
|
+
// or objects (except null) and nothing else.
|
|
99
|
+
if (typeof val !== 'object' || val === null) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const clientExtensionDebugKeys = Object.keys(ClientExtensionDebugTypes);
|
|
103
|
+
clientExtensionDebugKeys.forEach((key) => {
|
|
104
|
+
if (key in val) {
|
|
105
|
+
debugGlobal[key] = val[key];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
// Now, we can use the previous value e.g. boolean, to set the value for all flags
|
|
111
|
+
window.____c_p_d = previousGlobal;
|
|
112
|
+
};
|
|
113
|
+
// Register global value only once
|
|
114
|
+
if (!('____c_p_d' in window) || !window.____c_p_d.__initialized) {
|
|
115
|
+
defineDebugGlobal((_a = window.____c_p_d) !== null && _a !== void 0 ? _a : process.env.NODE_ENV !== 'production');
|
|
116
|
+
}
|
|
117
|
+
export const observeStateChange = (observer) => observeDebugSubject("state" /* State */, observer);
|
|
118
|
+
export const isDebugEnabled = () => {
|
|
119
|
+
return window.____c_p_d.debug;
|
|
120
|
+
};
|
|
121
|
+
export const isLoggingEnabled = () => {
|
|
122
|
+
return window.____c_p_d.logging;
|
|
123
|
+
};
|
|
124
|
+
export const isValidationEnabled = () => {
|
|
125
|
+
return window.____c_p_d.validation;
|
|
126
|
+
};
|
|
127
|
+
export const isDiscoveryEnabled = () => {
|
|
128
|
+
return window.____c_p_d.discovery;
|
|
129
|
+
};
|
|
130
|
+
/** @since 2.1.0 */
|
|
131
|
+
export const getLogLevel = () => {
|
|
132
|
+
const logLevel = window.____c_p_d.logLevel;
|
|
133
|
+
return logLevel !== null && logLevel !== void 0 ? logLevel : LogLevel.info;
|
|
134
|
+
};
|
|
135
|
+
export const setDebugEnabled = (value) => {
|
|
136
|
+
window.____c_p_d.debug = value;
|
|
137
|
+
};
|
|
138
|
+
export const setLoggingEnabled = (value) => {
|
|
139
|
+
window.____c_p_d.logging = value;
|
|
140
|
+
};
|
|
141
|
+
export const setValidationEnabled = (value) => {
|
|
142
|
+
window.____c_p_d.validation = value;
|
|
143
|
+
};
|
|
144
|
+
export const setDiscoveryEnabled = (value) => {
|
|
145
|
+
window.____c_p_d.discovery = value;
|
|
146
|
+
};
|
|
147
|
+
/** @since 2.1.0 */
|
|
148
|
+
export const setLogLevel = (value) => {
|
|
149
|
+
const level = value.toUpperCase();
|
|
150
|
+
window.____c_p_d.logLevel = level;
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=debug-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-state.js","sourceRoot":"","sources":["../../lib/debug-state.ts"],"names":[],"mappings":"AAAA,yCAAyC;;AAGzC,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAiB,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC5F,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,IAAK,yBAOJ;AAPD,WAAK,yBAAyB;IAC1B,4CAAe,CAAA;IACf,gDAAmB,CAAA;IACnB,sDAAyB,CAAA;IACzB,oDAAuB,CAAA;IACvB,mBAAmB;IACnB,kDAAqB,CAAA;AACzB,CAAC,EAPI,yBAAyB,KAAzB,yBAAyB,QAO7B;AAuBD,MAAM,iBAAiB,GAAG,CAAC,cAAuB,EAAE,EAAE;IAClD,wBAAwB;IACxB,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxC,MAAM,WAAW,GAAyB;QACtC,KAAK,EAAE,KAAK;QACZ,OAAO,EAAE,KAAK;QACd,UAAU,EAAE,KAAK;QACjB,SAAS,EAAE,KAAK;QAChB,mBAAmB;QACnB,8CAA8C;QAC9C,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI;KACnF,CAAC;IAEF,MAAM,iBAAiB,GAAG,oBAAoB,sBAAsB,G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CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAAE,EAAE;IAC/C,MAAM,CAAC,SAAS,CAAC,OAAmB,GAAG,KAAK,CAAC;AAClD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,KAAc,EAAE,EAAE;IAClD,MAAM,CAAC,SAAS,CAAC,UAAsB,GAAG,KAAK,CAAC;AACrD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,KAAc,EAAE,EAAE;IACjD,MAAM,CAAC,SAAS,CAAC,SAAqB,GAAG,KAAK,CAAC;AACpD,CAAC,CAAC;AAEF,mBAAmB;AACnB,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,EAAE;IACzC,MAAM,KAAK,GAAG,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"]}
|