@celljs/core 4.3.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -20
- package/cell.schema.json +84 -0
- package/cell.yml +13 -12
- package/esm/browser/application/frontend-application.d.ts +2 -2
- package/esm/browser/application/frontend-application.js +6 -25
- package/esm/browser/application/frontend-application.js.map +1 -1
- package/esm/common/annotation/component.js +1 -2
- package/esm/common/annotation/component.js.map +1 -1
- package/esm/common/annotation/service.js +2 -1
- package/esm/common/annotation/service.js.map +1 -1
- package/esm/common/application/application-entry.js +4 -0
- package/esm/common/application/application-entry.js.map +1 -1
- package/esm/common/application/application-protocol.d.ts +9 -3
- package/esm/common/application/application-protocol.js +19 -7
- package/esm/common/application/application-protocol.js.map +1 -1
- package/esm/common/application/application.js +1 -1
- package/esm/common/application/application.js.map +1 -1
- package/esm/common/config/config-util.d.ts +3 -0
- package/esm/common/config/config-util.js +6 -1
- package/esm/common/config/config-util.js.map +1 -1
- package/esm/common/config/dynamic-config.d.ts +3 -0
- package/esm/common/config/dynamic-config.js +3 -1
- package/esm/common/config/dynamic-config.js.map +1 -1
- package/esm/common/config/env-config-util.d.ts +32 -26
- package/esm/common/config/env-config-util.js +84 -40
- package/esm/common/config/env-config-util.js.map +1 -1
- package/esm/common/constants.d.ts +1 -1
- package/esm/common/constants.js +1 -1
- package/esm/common/constants.js.map +1 -1
- package/esm/common/container/auto-bind.js +2 -2
- package/esm/common/container/auto-bind.js.map +1 -1
- package/esm/common/container/container.js +3 -3
- package/esm/common/container/container.js.map +1 -1
- package/esm/common/el/core-context-initializer.js +3 -1
- package/esm/common/el/core-context-initializer.js.map +1 -1
- package/esm/common/el/expression-engine.d.ts +3 -3
- package/esm/common/el/expression-engine.js +18 -9
- package/esm/common/el/expression-engine.js.map +1 -1
- package/esm/common/el/expression-handler.js +2 -1
- package/esm/common/el/expression-handler.js.map +1 -1
- package/esm/common/el/expression-util.d.ts +17 -4
- package/esm/common/el/expression-util.js +28 -9
- package/esm/common/el/expression-util.js.map +1 -1
- package/esm/common/index.d.ts +1 -0
- package/esm/common/index.js +1 -0
- package/esm/common/index.js.map +1 -1
- package/esm/common/logger/abstract-logger.d.ts +2 -1
- package/esm/common/logger/abstract-logger.js +28 -17
- package/esm/common/logger/abstract-logger.js.map +1 -1
- package/esm/common/logger/logger-protocol.d.ts +2 -2
- package/esm/common/logger/logger-protocol.js +1 -1
- package/esm/common/logger/logger-protocol.js.map +1 -1
- package/esm/common/logger/logger.js +11 -4
- package/esm/common/logger/logger.js.map +1 -1
- package/esm/common/security-check/index.d.ts +2 -0
- package/esm/common/security-check/index.js +3 -0
- package/esm/common/security-check/index.js.map +1 -0
- package/esm/common/security-check/security-check-protocol.d.ts +40 -0
- package/esm/common/security-check/security-check-protocol.js +24 -0
- package/esm/common/security-check/security-check-protocol.js.map +1 -0
- package/esm/common/security-check/security-checker.d.ts +14 -0
- package/esm/common/security-check/security-checker.js +61 -0
- package/esm/common/security-check/security-checker.js.map +1 -0
- package/esm/common/utils/emitter.d.ts +2 -0
- package/esm/common/utils/emitter.js +4 -0
- package/esm/common/utils/emitter.js.map +1 -1
- package/esm/common/utils/error-util.d.ts +8 -0
- package/esm/common/utils/error-util.js +24 -0
- package/esm/common/utils/error-util.js.map +1 -1
- package/esm/common/utils/event.d.ts +6 -1
- package/esm/common/utils/event.js +20 -6
- package/esm/common/utils/event.js.map +1 -1
- package/esm/common/utils/uuid.d.ts +1 -1
- package/esm/common/utils/uuid.js +1 -1
- package/esm/common/utils/uuid.js.map +1 -1
- package/esm/node/application/backend-application.js +1 -1
- package/esm/node/application/backend-application.js.map +1 -1
- package/esm/node/context/context.js +3 -3
- package/esm/node/context/context.js.map +1 -1
- package/lib/browser/application/frontend-application.d.ts +2 -2
- package/lib/browser/application/frontend-application.d.ts.map +1 -1
- package/lib/browser/application/frontend-application.js +6 -25
- package/lib/browser/application/frontend-application.js.map +1 -1
- package/lib/common/annotation/component.js +1 -2
- package/lib/common/annotation/component.js.map +1 -1
- package/lib/common/annotation/service.d.ts.map +1 -1
- package/lib/common/annotation/service.js +2 -1
- package/lib/common/annotation/service.js.map +1 -1
- package/lib/common/annotation/service.spec.d.ts +2 -0
- package/lib/common/annotation/service.spec.d.ts.map +1 -0
- package/lib/common/annotation/service.spec.js +43 -0
- package/lib/common/annotation/service.spec.js.map +1 -0
- package/lib/common/aop/aop.spec.js +4 -4
- package/lib/common/aop/aop.spec.js.map +1 -1
- package/lib/common/application/application-entry.js +4 -0
- package/lib/common/application/application-entry.js.map +1 -1
- package/lib/common/application/application-protocol.d.ts +9 -3
- package/lib/common/application/application-protocol.d.ts.map +1 -1
- package/lib/common/application/application-protocol.js +19 -7
- package/lib/common/application/application-protocol.js.map +1 -1
- package/lib/common/application/application.js +1 -1
- package/lib/common/application/application.js.map +1 -1
- package/lib/common/application/lifecycle.spec.js +54 -7
- package/lib/common/application/lifecycle.spec.js.map +1 -1
- package/lib/common/config/config-util.d.ts +3 -0
- package/lib/common/config/config-util.d.ts.map +1 -1
- package/lib/common/config/config-util.js +5 -0
- package/lib/common/config/config-util.js.map +1 -1
- package/lib/common/config/dynamic-config.d.ts +3 -0
- package/lib/common/config/dynamic-config.d.ts.map +1 -1
- package/lib/common/config/dynamic-config.js +4 -2
- package/lib/common/config/dynamic-config.js.map +1 -1
- package/lib/common/config/env-config-util.d.ts +32 -26
- package/lib/common/config/env-config-util.d.ts.map +1 -1
- package/lib/common/config/env-config-util.js +84 -40
- package/lib/common/config/env-config-util.js.map +1 -1
- package/lib/common/config/env-config-util.spec.js +148 -132
- package/lib/common/config/env-config-util.spec.js.map +1 -1
- package/lib/common/constants.d.ts +1 -1
- package/lib/common/constants.d.ts.map +1 -1
- package/lib/common/constants.js +1 -1
- package/lib/common/constants.js.map +1 -1
- package/lib/common/container/auto-bind.js +2 -2
- package/lib/common/container/auto-bind.js.map +1 -1
- package/lib/common/container/container.js +3 -3
- package/lib/common/container/container.js.map +1 -1
- package/lib/common/el/core-context-initializer.d.ts.map +1 -1
- package/lib/common/el/core-context-initializer.js +3 -1
- package/lib/common/el/core-context-initializer.js.map +1 -1
- package/lib/common/el/expression-engine.d.ts +3 -3
- package/lib/common/el/expression-engine.js +18 -9
- package/lib/common/el/expression-engine.js.map +1 -1
- package/lib/common/el/expression-engine.spec.js +15 -7
- package/lib/common/el/expression-engine.spec.js.map +1 -1
- package/lib/common/el/expression-handler.d.ts.map +1 -1
- package/lib/common/el/expression-handler.js +2 -1
- package/lib/common/el/expression-handler.js.map +1 -1
- package/lib/common/el/expression-util.d.ts +17 -4
- package/lib/common/el/expression-util.d.ts.map +1 -1
- package/lib/common/el/expression-util.js +28 -10
- package/lib/common/el/expression-util.js.map +1 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +1 -0
- package/lib/common/index.js.map +1 -1
- package/lib/common/logger/abstract-logger.d.ts +2 -1
- package/lib/common/logger/abstract-logger.d.ts.map +1 -1
- package/lib/common/logger/abstract-logger.js +28 -17
- package/lib/common/logger/abstract-logger.js.map +1 -1
- package/lib/common/logger/logger-protocol.d.ts +2 -2
- package/lib/common/logger/logger-protocol.d.ts.map +1 -1
- package/lib/common/logger/logger-protocol.js +1 -1
- package/lib/common/logger/logger-protocol.js.map +1 -1
- package/lib/common/logger/logger.d.ts.map +1 -1
- package/lib/common/logger/logger.js +11 -4
- package/lib/common/logger/logger.js.map +1 -1
- package/lib/common/logger/logger.spec.js +35 -0
- package/lib/common/logger/logger.spec.js.map +1 -1
- package/lib/common/security-check/index.d.ts +3 -0
- package/lib/common/security-check/index.d.ts.map +1 -0
- package/lib/common/security-check/index.js +19 -0
- package/lib/common/security-check/index.js.map +1 -0
- package/lib/common/security-check/security-check-protocol.d.ts +41 -0
- package/lib/common/security-check/security-check-protocol.d.ts.map +1 -0
- package/lib/common/security-check/security-check-protocol.js +28 -0
- package/lib/common/security-check/security-check-protocol.js.map +1 -0
- package/lib/common/security-check/security-checker.d.ts +15 -0
- package/lib/common/security-check/security-checker.d.ts.map +1 -0
- package/lib/common/security-check/security-checker.js +64 -0
- package/lib/common/security-check/security-checker.js.map +1 -0
- package/lib/common/security-check/security-checker.spec.d.ts +2 -0
- package/lib/common/security-check/security-checker.spec.d.ts.map +1 -0
- package/lib/common/security-check/security-checker.spec.js +68 -0
- package/lib/common/security-check/security-checker.spec.js.map +1 -0
- package/lib/common/utils/emitter.d.ts +2 -0
- package/lib/common/utils/emitter.d.ts.map +1 -1
- package/lib/common/utils/emitter.js +4 -0
- package/lib/common/utils/emitter.js.map +1 -1
- package/lib/common/utils/error-util.d.ts +8 -0
- package/lib/common/utils/error-util.d.ts.map +1 -1
- package/lib/common/utils/error-util.js +24 -0
- package/lib/common/utils/error-util.js.map +1 -1
- package/lib/common/utils/event.d.ts +6 -1
- package/lib/common/utils/event.d.ts.map +1 -1
- package/lib/common/utils/event.js +20 -6
- package/lib/common/utils/event.js.map +1 -1
- package/lib/common/utils/utils-behavior.spec.js +21 -2
- package/lib/common/utils/utils-behavior.spec.js.map +1 -1
- package/lib/common/utils/uuid.d.ts +1 -1
- package/lib/common/utils/uuid.d.ts.map +1 -1
- package/lib/common/utils/uuid.js +2 -2
- package/lib/common/utils/uuid.js.map +1 -1
- package/lib/node/application/backend-application.js +1 -1
- package/lib/node/application/backend-application.js.map +1 -1
- package/lib/node/context/context.d.ts.map +1 -1
- package/lib/node/context/context.js +3 -3
- package/lib/node/context/context.js.map +1 -1
- package/lib/node/context/context.spec.js +13 -0
- package/lib/node/context/context.spec.js.map +1 -1
- package/package.json +6 -2
- package/src/browser/application/frontend-application.ts +7 -28
- package/src/common/annotation/component.ts +1 -1
- package/src/common/annotation/service.spec.ts +29 -0
- package/src/common/annotation/service.ts +2 -1
- package/src/common/aop/aop.spec.ts +4 -4
- package/src/common/application/application-entry.ts +4 -0
- package/src/common/application/application-protocol.ts +21 -11
- package/src/common/application/application.ts +1 -1
- package/src/common/application/lifecycle.spec.ts +52 -8
- package/src/common/config/config-util.ts +7 -1
- package/src/common/config/dynamic-config.ts +6 -1
- package/src/common/config/env-config-util.spec.ts +152 -149
- package/src/common/config/env-config-util.ts +95 -51
- package/src/common/constants.ts +1 -1
- package/src/common/container/auto-bind.ts +1 -1
- package/src/common/container/container.ts +3 -3
- package/src/common/el/core-context-initializer.ts +4 -2
- package/src/common/el/expression-engine.spec.ts +17 -8
- package/src/common/el/expression-engine.ts +17 -9
- package/src/common/el/expression-handler.ts +2 -1
- package/src/common/el/expression-util.ts +37 -8
- package/src/common/index.ts +1 -0
- package/src/common/logger/abstract-logger.ts +30 -17
- package/src/common/logger/logger-protocol.ts +1 -1
- package/src/common/logger/logger.spec.ts +37 -0
- package/src/common/logger/logger.ts +12 -4
- package/src/common/security-check/index.ts +2 -0
- package/src/common/security-check/security-check-protocol.ts +59 -0
- package/src/common/security-check/security-checker.spec.ts +70 -0
- package/src/common/security-check/security-checker.ts +48 -0
- package/src/common/utils/emitter.ts +5 -0
- package/src/common/utils/error-util.ts +27 -1
- package/src/common/utils/event.ts +20 -9
- package/src/common/utils/utils-behavior.spec.ts +23 -3
- package/src/common/utils/uuid.ts +1 -1
- package/src/node/application/backend-application.ts +1 -1
- package/src/node/context/context.spec.ts +14 -0
- package/src/node/context/context.ts +2 -3
- package/cell.remote.yml +0 -8
|
@@ -58,4 +58,41 @@ describe('LoggerImpl', () => {
|
|
|
58
58
|
listener.dispose();
|
|
59
59
|
expect(seen.map(m => [m.level, m.message])).to.deep.equal([['warn', 'w']]);
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
it('builds no LogMessage while nothing listens; a listener gets every line written', () => {
|
|
63
|
+
const { logger, lines } = capture('info');
|
|
64
|
+
expect(onLogEmitter.hasListeners).to.equal(false);
|
|
65
|
+
logger.info('quiet');
|
|
66
|
+
const messages: LogMessage[] = [];
|
|
67
|
+
const subscription = onLogEmitter.event(message => messages.push(message));
|
|
68
|
+
try {
|
|
69
|
+
logger.info('heard');
|
|
70
|
+
logger.debug('below the level');
|
|
71
|
+
} finally {
|
|
72
|
+
subscription.dispose();
|
|
73
|
+
}
|
|
74
|
+
expect(lines.map(l => l.text.slice(-5))).to.deep.equal(['quiet', 'heard']);
|
|
75
|
+
expect(messages.map(m => [m.level, m.message])).to.deep.equal([['info', 'heard']]);
|
|
76
|
+
expect(onLogEmitter.hasListeners).to.equal(false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('an unknown configured level counts as info; time and timeEnd measure a label once', () => {
|
|
80
|
+
const { logger, lines } = capture('loud' as LogLevel);
|
|
81
|
+
expect(logger.isEnabled('debug')).to.equal(false);
|
|
82
|
+
expect(logger.isEnabled('info')).to.equal(true);
|
|
83
|
+
expect(logger.timeEnd('never')).to.equal(undefined);
|
|
84
|
+
logger.time('task');
|
|
85
|
+
expect(logger.timeEnd('task')).to.be.a('number');
|
|
86
|
+
expect(logger.timeEnd('task')).to.equal(undefined);
|
|
87
|
+
expect(lines.map(l => /No such label|task \[/.exec(l.text)?.[0])).to.deep.equal(['No such label', 'task [', 'No such label']);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('writes an Error with its stack and cause, and separates the prefix from the message', () => {
|
|
91
|
+
const { logger, lines } = capture('info');
|
|
92
|
+
logger.error(Object.assign(new Error('query failed'), { cause: new Error('ECONNRESET') }));
|
|
93
|
+
logger.info('plain', 'Ctx');
|
|
94
|
+
expect(lines[0].text).to.match(/^\S+ \[error\] Error: query failed\n\s+at /);
|
|
95
|
+
expect(lines[0].text).to.contain('Caused by: Error: ECONNRESET');
|
|
96
|
+
expect(lines[1].text).to.match(/^\S+ \[info\] \[Ctx\] plain$/);
|
|
97
|
+
});
|
|
61
98
|
});
|
|
@@ -3,28 +3,36 @@ import { Scope } from '../container';
|
|
|
3
3
|
import { AbstractLogger } from './abstract-logger';
|
|
4
4
|
import { Logger } from './logger-protocol';
|
|
5
5
|
|
|
6
|
+
/** Created once, not per message; they read `console` when called, so a replaced console method is used. */
|
|
7
|
+
const write = {
|
|
8
|
+
error: (...args: any[]) => console.error(...args),
|
|
9
|
+
warn: (...args: any[]) => console.warn(...args),
|
|
10
|
+
info: (...args: any[]) => console.info(...args),
|
|
11
|
+
debug: (...args: any[]) => console.debug(...args)
|
|
12
|
+
};
|
|
13
|
+
|
|
6
14
|
@Component({ id: Logger, scope: Scope.Transient })
|
|
7
15
|
export class LoggerImpl extends AbstractLogger {
|
|
8
16
|
|
|
9
17
|
error(message: any, context?: string): void {
|
|
10
|
-
this.log(message, context,
|
|
18
|
+
this.log(message, context, write.error, 'error');
|
|
11
19
|
}
|
|
12
20
|
|
|
13
21
|
info(message: any, context?: string): void {
|
|
14
22
|
if (this.isEnabled('info')) {
|
|
15
|
-
this.log(message, context,
|
|
23
|
+
this.log(message, context, write.info, 'info');
|
|
16
24
|
}
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
warn(message: any, context?: string): void {
|
|
20
28
|
if (this.isEnabled('warn')) {
|
|
21
|
-
this.log(message, context,
|
|
29
|
+
this.log(message, context, write.warn, 'warn');
|
|
22
30
|
}
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
debug(message: any, context?: string): void {
|
|
26
34
|
if (this.isEnabled('debug')) {
|
|
27
|
-
this.log(message, context,
|
|
35
|
+
this.log(message, context, write.debug, 'debug');
|
|
28
36
|
}
|
|
29
37
|
}
|
|
30
38
|
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { MaybePromise } from '../utils/prioritizeable';
|
|
2
|
+
|
|
3
|
+
export const SecurityCheck = Symbol('SecurityCheck');
|
|
4
|
+
export const SecurityChecker = Symbol('SecurityChecker');
|
|
5
|
+
|
|
6
|
+
/** `securityChecks` in cell.yml: what a production start does about the warnings. */
|
|
7
|
+
export const SECURITY_CHECKS = 'securityChecks';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `warn` (default) logs each warning, `fail` refuses to start with {@link SecurityCheckError},
|
|
11
|
+
* `off` does not check.
|
|
12
|
+
*/
|
|
13
|
+
export type SecurityCheckPolicy = 'warn' | 'fail' | 'off';
|
|
14
|
+
|
|
15
|
+
/** A setting that is unsafe in production. `code` is stable (`CELL_SECURITY_*`), `hint` says how to fix it in one sentence. */
|
|
16
|
+
export interface SecurityWarning {
|
|
17
|
+
code: string;
|
|
18
|
+
message: string;
|
|
19
|
+
hint: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Looks at the configuration of a component for settings that are unsafe in production, such as a
|
|
24
|
+
* signing key everyone knows. Bind implementations to `SecurityCheck`; `cell check` reports their
|
|
25
|
+
* warnings, and a production start (`NODE_ENV=production`) applies `securityChecks`.
|
|
26
|
+
*/
|
|
27
|
+
export interface SecurityCheck {
|
|
28
|
+
check(): MaybePromise<SecurityWarning[]>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SecurityChecker {
|
|
32
|
+
/** The warnings of every check, in any environment: what `cell check` reports. */
|
|
33
|
+
check(): Promise<SecurityWarning[]>;
|
|
34
|
+
/** Called when the application starts: in production, applies `securityChecks` to the warnings. */
|
|
35
|
+
enforce(): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The application refuses to start: `securityChecks: fail` and a check warned in production. */
|
|
39
|
+
export class SecurityCheckError extends Error {
|
|
40
|
+
override readonly name = 'SecurityCheckError';
|
|
41
|
+
readonly code: string;
|
|
42
|
+
readonly hint: string;
|
|
43
|
+
|
|
44
|
+
constructor(readonly warnings: SecurityWarning[]) {
|
|
45
|
+
const failure = {
|
|
46
|
+
code: 'CELL_SECURITY_CHECK_FAILED',
|
|
47
|
+
message: `Unsafe configuration for production:\n${warnings.map(SecurityCheckError.format).join('\n')}`,
|
|
48
|
+
hint: 'Fix each warning listed in the message, or set securityChecks: warn in cell.yml to start anyway.'
|
|
49
|
+
};
|
|
50
|
+
super(failure.message);
|
|
51
|
+
this.code = failure.code;
|
|
52
|
+
this.hint = failure.hint;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `[CELL_SECURITY_X] message Fix: hint`. */
|
|
56
|
+
static format(warning: SecurityWarning): string {
|
|
57
|
+
return `[${warning.code}] ${warning.message} Fix: ${warning.hint}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { afterEach, describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { SecurityCheckerImpl } from './security-checker';
|
|
4
|
+
import { SecurityCheckError, SecurityCheckPolicy, SecurityWarning } from './security-check-protocol';
|
|
5
|
+
import { MockLogger } from '../test/mock-logger';
|
|
6
|
+
import { ErrorUtil } from '../utils/error-util';
|
|
7
|
+
|
|
8
|
+
const WARNING: SecurityWarning = { code: 'CELL_SECURITY_TEST', message: 'The test key is public.', hint: 'Set your own key.' };
|
|
9
|
+
const nodeEnv = process.env.NODE_ENV;
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
process.env.NODE_ENV = nodeEnv;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
class RecordingLogger extends MockLogger {
|
|
15
|
+
readonly warnings: string[] = [];
|
|
16
|
+
override warn(message: any): void {
|
|
17
|
+
this.warnings.push(message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function checker(policy: SecurityCheckPolicy, warnings: SecurityWarning[] = [WARNING]) {
|
|
22
|
+
const logger = new RecordingLogger();
|
|
23
|
+
const instance = Object.assign(new SecurityCheckerImpl(), { policy, logger, checks: [{ check: () => warnings }, { check: async () => [] }] });
|
|
24
|
+
return { checker: instance, logger };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('SecurityCheckerImpl', () => {
|
|
28
|
+
it('check() collects the warnings of every check in any environment', async () => {
|
|
29
|
+
process.env.NODE_ENV = 'development';
|
|
30
|
+
expect(await checker('warn').checker.check()).to.deep.equal([WARNING]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('enforce() does nothing outside production', async () => {
|
|
34
|
+
process.env.NODE_ENV = 'development';
|
|
35
|
+
const { checker: c, logger } = checker('fail');
|
|
36
|
+
await c.enforce();
|
|
37
|
+
expect(logger.warnings).to.deep.equal([]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('in production, warn logs each warning with its code and fix', async () => {
|
|
41
|
+
process.env.NODE_ENV = 'production';
|
|
42
|
+
const { checker: c, logger } = checker('warn');
|
|
43
|
+
await c.enforce();
|
|
44
|
+
expect(logger.warnings).to.deep.equal(['[CELL_SECURITY_TEST] The test key is public. Fix: Set your own key.']);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('in production, fail refuses to start with CELL_SECURITY_CHECK_FAILED; off does not check', async () => {
|
|
48
|
+
process.env.NODE_ENV = 'production';
|
|
49
|
+
const error = await checker('fail').checker.enforce().then(() => undefined, e => e);
|
|
50
|
+
expect(error).to.be.instanceOf(SecurityCheckError);
|
|
51
|
+
expect(error.code).to.equal('CELL_SECURITY_CHECK_FAILED');
|
|
52
|
+
expect(error.warnings).to.deep.equal([WARNING]);
|
|
53
|
+
expect(error.message).to.contain('[CELL_SECURITY_TEST]');
|
|
54
|
+
const off = checker('off');
|
|
55
|
+
await off.checker.enforce();
|
|
56
|
+
expect(off.logger.warnings).to.deep.equal([]);
|
|
57
|
+
await checker('fail', []).checker.enforce();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('ErrorUtil.describe', () => {
|
|
62
|
+
it('writes the stack and each cause', () => {
|
|
63
|
+
const error = Object.assign(new Error('outer'), { cause: Object.assign(new Error('inner'), { cause: 'root' }) });
|
|
64
|
+
const text = ErrorUtil.describe(error);
|
|
65
|
+
expect(text).to.match(/^Error: outer\n\s+at /);
|
|
66
|
+
expect(text).to.contain('Caused by: Error: inner');
|
|
67
|
+
expect(text).to.contain('Caused by: root');
|
|
68
|
+
expect(ErrorUtil.describe('plain')).to.equal('plain');
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Autowired, Component, Optional, Value } from '../annotation';
|
|
2
|
+
import { Logger } from '../logger/logger-protocol';
|
|
3
|
+
import { SECURITY_CHECKS, SecurityCheck, SecurityChecker, SecurityCheckError, SecurityCheckPolicy, SecurityWarning } from './security-check-protocol';
|
|
4
|
+
|
|
5
|
+
@Component(SecurityChecker)
|
|
6
|
+
export class SecurityCheckerImpl implements SecurityChecker {
|
|
7
|
+
|
|
8
|
+
@Autowired(SecurityCheck) @Optional()
|
|
9
|
+
protected readonly checks: SecurityCheck[];
|
|
10
|
+
|
|
11
|
+
@Value(`${SECURITY_CHECKS} ?? 'warn'`)
|
|
12
|
+
protected readonly policy: SecurityCheckPolicy;
|
|
13
|
+
|
|
14
|
+
@Autowired(Logger)
|
|
15
|
+
protected readonly logger: Logger;
|
|
16
|
+
|
|
17
|
+
async check(): Promise<SecurityWarning[]> {
|
|
18
|
+
const warnings: SecurityWarning[] = [];
|
|
19
|
+
for (const check of this.checks) {
|
|
20
|
+
warnings.push(...await check.check());
|
|
21
|
+
}
|
|
22
|
+
return warnings;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async enforce(): Promise<void> {
|
|
26
|
+
if (this.policy === 'off' || this.checks.length === 0 || !this.isProduction()) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const warnings = await this.check();
|
|
30
|
+
if (warnings.length === 0) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (this.policy === 'fail') {
|
|
34
|
+
throw new SecurityCheckError(warnings);
|
|
35
|
+
}
|
|
36
|
+
for (const warning of warnings) {
|
|
37
|
+
this.logger.warn(SecurityCheckError.format(warning));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Development keeps the convenient defaults quietly; `cell build` writes `production` into the
|
|
43
|
+
* server bundle, and `cell preview` and the deployers set it.
|
|
44
|
+
*/
|
|
45
|
+
protected isProduction(): boolean {
|
|
46
|
+
return typeof process !== 'undefined' && process.env.NODE_ENV === 'production';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -120,6 +120,11 @@ export class Emitter<T = any> {
|
|
|
120
120
|
this._leakingStacks.set(stack, count - 1);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
/** Whether anything listens: lets a caller skip building an event nobody receives. */
|
|
124
|
+
get hasListeners(): boolean {
|
|
125
|
+
return !!this._callbacks && !this._callbacks.isEmpty();
|
|
126
|
+
}
|
|
127
|
+
|
|
123
128
|
/**
|
|
124
129
|
* To be kept private to fire an event to
|
|
125
130
|
* subscribers
|
|
@@ -31,4 +31,30 @@ export abstract class ErrorUtil {
|
|
|
31
31
|
captureStackTrace(target, fn);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An error for a log: its stack (or `name: message`), then each `cause` as `Caused by: ...`, at
|
|
37
|
+
* most five levels. Anything else is `String(value)`.
|
|
38
|
+
*/
|
|
39
|
+
static describe(error: unknown): string {
|
|
40
|
+
let text = ErrorUtil.describeOne(error);
|
|
41
|
+
let cause = ErrorUtil.causeOf(error);
|
|
42
|
+
for (let depth = 0; cause !== undefined && depth < 5; depth++) {
|
|
43
|
+
text += `\nCaused by: ${ErrorUtil.describeOne(cause)}`;
|
|
44
|
+
cause = ErrorUtil.causeOf(cause);
|
|
45
|
+
}
|
|
46
|
+
return text;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `cause` of an Error (ES2022; the compiled lib is older). */
|
|
50
|
+
protected static causeOf(error: unknown): unknown {
|
|
51
|
+
return error instanceof Error ? (error as { cause?: unknown }).cause : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected static describeOne(error: unknown): string {
|
|
55
|
+
if (error instanceof Error) {
|
|
56
|
+
return error.stack ?? `${error.name}: ${error.message}`;
|
|
57
|
+
}
|
|
58
|
+
return String(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -92,24 +92,35 @@ export class CallbackList implements Iterable<Callback> {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
public [Symbol.iterator]() {
|
|
95
|
+
/** The callbacks bound to their contexts, as they were when the iteration started. */
|
|
96
|
+
public *[Symbol.iterator](): Iterator<Callback> {
|
|
97
97
|
if (!this._callbacks) {
|
|
98
|
-
return
|
|
98
|
+
return;
|
|
99
99
|
}
|
|
100
100
|
const callbacks = this._callbacks.slice(0);
|
|
101
101
|
const contexts = this._contexts!.slice(0);
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
103
|
+
const callback = callbacks[i];
|
|
104
|
+
const context = contexts[i];
|
|
105
|
+
yield (...args: any[]) => callback.apply(context, args);
|
|
106
|
+
}
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Calls the callbacks that are registered now (a callback added or removed meanwhile does not change
|
|
111
|
+
* the round); one that throws is logged and the others still run.
|
|
112
|
+
*/
|
|
108
113
|
public invoke(...args: any[]): any[] {
|
|
114
|
+
if (!this._callbacks) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
// Called on every event: no bound function per callback, only a copy of the lists.
|
|
118
|
+
const callbacks = this._callbacks.slice(0);
|
|
119
|
+
const contexts = this._contexts!.slice(0);
|
|
109
120
|
const ret: any[] = [];
|
|
110
|
-
for (
|
|
121
|
+
for (let i = 0; i < callbacks.length; i++) {
|
|
111
122
|
try {
|
|
112
|
-
ret.push(
|
|
123
|
+
ret.push(callbacks[i].apply(contexts[i], args));
|
|
113
124
|
} catch (e) {
|
|
114
125
|
console.error(e);
|
|
115
126
|
}
|
|
@@ -2,7 +2,7 @@ import { describe, it } from 'bun:test';
|
|
|
2
2
|
import { expect } from 'chai';
|
|
3
3
|
// 通过 common 入口导入:直接深导入 utils/mime-type-util 会因 error 模块的循环依赖拿到未初始化的 CustomError
|
|
4
4
|
import {
|
|
5
|
-
UrlUtil, ByteUtil, isUUID,
|
|
5
|
+
UrlUtil, ByteUtil, isUUID, generateUUID, Emitter, Event, Disposable, DisposableCollection,
|
|
6
6
|
CancellationTokenSource, CancellationToken, cancelled, isCancelled, checkCancelled,
|
|
7
7
|
MimeTypeUtils, MimeType, WaitUntilEvent, InvalidMimeTypeError, IllegalArgumentError
|
|
8
8
|
} from '../index';
|
|
@@ -49,8 +49,8 @@ describe('core utils:ByteUtil 与 UUID', () => {
|
|
|
49
49
|
expect(ByteUtil.decodeBase64('')).to.have.length(0);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
it('
|
|
53
|
-
const ids = new Set(Array.from({ length: 200 }, () =>
|
|
52
|
+
it('generateUUID 生成 v4 UUID,互不重复;isUUID 校验格式', () => {
|
|
53
|
+
const ids = new Set(Array.from({ length: 200 }, () => generateUUID()));
|
|
54
54
|
expect(ids.size).to.equal(200);
|
|
55
55
|
for (const id of ids) {
|
|
56
56
|
expect(isUUID(id)).to.equal(true);
|
|
@@ -101,6 +101,26 @@ describe('core utils:Emitter / Event / Disposable', () => {
|
|
|
101
101
|
expect(log).to.deep.equal(['first', 'last']);
|
|
102
102
|
});
|
|
103
103
|
|
|
104
|
+
it('hasListeners 反映当前是否有监听器;fire 期间新增或移除的监听器不影响这一轮', () => {
|
|
105
|
+
const emitter = new Emitter<number>();
|
|
106
|
+
expect(emitter.hasListeners).to.equal(false);
|
|
107
|
+
const got: string[] = [];
|
|
108
|
+
let late: Disposable | undefined;
|
|
109
|
+
const first = emitter.event(n => {
|
|
110
|
+
got.push(`a${n}`);
|
|
111
|
+
late ??= emitter.event(m => got.push(`late${m}`));
|
|
112
|
+
second.dispose();
|
|
113
|
+
});
|
|
114
|
+
const second = emitter.event(n => got.push(`b${n}`));
|
|
115
|
+
expect(emitter.hasListeners).to.equal(true);
|
|
116
|
+
emitter.fire(1);
|
|
117
|
+
emitter.fire(2);
|
|
118
|
+
expect(got).to.deep.equal(['a1', 'b1', 'a2', 'late2']);
|
|
119
|
+
first.dispose();
|
|
120
|
+
late!.dispose();
|
|
121
|
+
expect(emitter.hasListeners).to.equal(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
104
124
|
it('emitter.dispose 之后 fire 不再通知', () => {
|
|
105
125
|
const emitter = new Emitter<number>();
|
|
106
126
|
const got: number[] = [];
|
package/src/common/utils/uuid.ts
CHANGED
|
@@ -11,7 +11,7 @@ declare const crypto: undefined | {
|
|
|
11
11
|
randomUUID?(): string;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
export const
|
|
14
|
+
export const generateUUID = (function (): () => string {
|
|
15
15
|
|
|
16
16
|
// use `randomUUID` if possible
|
|
17
17
|
if (typeof crypto === 'object' && typeof crypto.randomUUID === 'function') {
|
|
@@ -216,4 +216,18 @@ describe('Context', () => {
|
|
|
216
216
|
expect(seen).to.deep.equal(['app', 'request']);
|
|
217
217
|
});
|
|
218
218
|
});
|
|
219
|
+
|
|
220
|
+
it('getAttr 不带 scope 时,0、false、空串也算设置过,不会落到下一个作用域', async () => {
|
|
221
|
+
await inRun(async () => {
|
|
222
|
+
Context.setCurrent(named('falsy'));
|
|
223
|
+
Context.setAttr('zero', 0);
|
|
224
|
+
Context.setAttr('no', false);
|
|
225
|
+
Context.setAttr('empty', '');
|
|
226
|
+
Context.setAttr('ctx-spec-zero', 1, AttributeScope.App);
|
|
227
|
+
expect(Context.getAttr('zero')).to.equal(0);
|
|
228
|
+
expect(Context.getAttr('no')).to.equal(false);
|
|
229
|
+
expect(Context.getAttr('empty')).to.equal('');
|
|
230
|
+
expect(Context.getAttr('ctx-spec-zero', AttributeScope.App)).to.equal(1);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
219
233
|
});
|
|
@@ -74,9 +74,8 @@ export class Context {
|
|
|
74
74
|
return appAttrs.get(key);
|
|
75
75
|
}
|
|
76
76
|
} else {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return value ? value : appAttrs.get(key);
|
|
77
|
+
// Falsy values (0, false, '') are values: only a missing key falls through to the next scope.
|
|
78
|
+
return storage.getStore()?.[key] ?? Context.getCurrent()?.[key] ?? appAttrs.get(key);
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
81
|
|