@celljs/core 3.9.0 → 4.0.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/cell.remote.yml +8 -0
- package/cell.yml +0 -3
- package/esm/common/application/application-entry.js +21 -3
- package/esm/common/application/application-entry.js.map +1 -1
- package/esm/common/application/application-host.d.ts +38 -0
- package/esm/common/application/application-host.js +17 -0
- package/esm/common/application/application-host.js.map +1 -0
- package/esm/common/application/index.d.ts +1 -0
- package/esm/common/application/index.js +1 -0
- package/esm/common/application/index.js.map +1 -1
- package/esm/common/config/env-config-util.d.ts +48 -0
- package/esm/common/config/env-config-util.js +185 -0
- package/esm/common/config/env-config-util.js.map +1 -0
- package/esm/common/config/index.d.ts +1 -0
- package/esm/common/config/index.js +1 -0
- package/esm/common/config/index.js.map +1 -1
- package/lib/common/application/application-entry.d.ts.map +1 -1
- package/lib/common/application/application-entry.js +21 -3
- package/lib/common/application/application-entry.js.map +1 -1
- package/lib/common/application/application-host.d.ts +39 -0
- package/lib/common/application/application-host.d.ts.map +1 -0
- package/lib/common/application/application-host.js +20 -0
- package/lib/common/application/application-host.js.map +1 -0
- package/lib/common/application/index.d.ts +1 -0
- package/lib/common/application/index.d.ts.map +1 -1
- package/lib/common/application/index.js +1 -0
- package/lib/common/application/index.js.map +1 -1
- package/lib/common/config/env-config-util.d.ts +49 -0
- package/lib/common/config/env-config-util.d.ts.map +1 -0
- package/lib/common/config/env-config-util.js +188 -0
- package/lib/common/config/env-config-util.js.map +1 -0
- package/lib/common/config/env-config-util.spec.d.ts +2 -0
- package/lib/common/config/env-config-util.spec.d.ts.map +1 -0
- package/lib/common/config/env-config-util.spec.js +314 -0
- package/lib/common/config/env-config-util.spec.js.map +1 -0
- package/lib/common/config/index.d.ts +1 -0
- package/lib/common/config/index.d.ts.map +1 -1
- package/lib/common/config/index.js +1 -0
- package/lib/common/config/index.js.map +1 -1
- package/package.json +14 -25
- package/src/common/application/application-entry.ts +20 -4
- package/src/common/application/application-host.ts +46 -0
- package/src/common/application/index.ts +1 -0
- package/src/common/config/env-config-util.spec.ts +341 -0
- package/src/common/config/env-config-util.ts +225 -0
- package/src/common/config/index.ts +1 -0
- package/cell-remote.yml +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"esm",
|
|
22
22
|
"src",
|
|
23
23
|
"cell.yml",
|
|
24
|
-
"cell
|
|
24
|
+
"cell.remote.yml"
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
@@ -52,121 +52,110 @@
|
|
|
52
52
|
},
|
|
53
53
|
"exports": {
|
|
54
54
|
".": {
|
|
55
|
-
"webpack": "./lib/common/index.js",
|
|
56
55
|
"import": "./esm/common/index.js",
|
|
57
56
|
"default": "./lib/common/index.js"
|
|
58
57
|
},
|
|
59
58
|
"./package.json": "./package.json",
|
|
60
59
|
"./lib/browser": {
|
|
61
|
-
"webpack": "./lib/browser/index.js",
|
|
62
60
|
"import": "./esm/browser/index.js",
|
|
63
61
|
"default": "./lib/browser/index.js"
|
|
64
62
|
},
|
|
65
63
|
"./lib/browser/application": {
|
|
66
|
-
"webpack": "./lib/browser/application/index.js",
|
|
67
64
|
"import": "./esm/browser/application/index.js",
|
|
68
65
|
"default": "./lib/browser/application/index.js"
|
|
69
66
|
},
|
|
70
67
|
"./lib/browser/shell": {
|
|
71
|
-
"webpack": "./lib/browser/shell/index.js",
|
|
72
68
|
"import": "./esm/browser/shell/index.js",
|
|
73
69
|
"default": "./lib/browser/shell/index.js"
|
|
74
70
|
},
|
|
75
71
|
"./lib/common": {
|
|
76
|
-
"webpack": "./lib/common/index.js",
|
|
77
72
|
"import": "./esm/common/index.js",
|
|
78
73
|
"default": "./lib/common/index.js"
|
|
79
74
|
},
|
|
80
75
|
"./lib/common/annotation": {
|
|
81
|
-
"webpack": "./lib/common/annotation/index.js",
|
|
82
76
|
"import": "./esm/common/annotation/index.js",
|
|
83
77
|
"default": "./lib/common/annotation/index.js"
|
|
84
78
|
},
|
|
85
79
|
"./lib/common/annotation/detached": {
|
|
86
|
-
"webpack": "./lib/common/annotation/detached/index.js",
|
|
87
80
|
"import": "./esm/common/annotation/detached/index.js",
|
|
88
81
|
"default": "./lib/common/annotation/detached/index.js"
|
|
89
82
|
},
|
|
90
83
|
"./lib/common/aop": {
|
|
91
|
-
"webpack": "./lib/common/aop/index.js",
|
|
92
84
|
"import": "./esm/common/aop/index.js",
|
|
93
85
|
"default": "./lib/common/aop/index.js"
|
|
94
86
|
},
|
|
95
87
|
"./lib/common/application": {
|
|
96
|
-
"webpack": "./lib/common/application/index.js",
|
|
97
88
|
"import": "./esm/common/application/index.js",
|
|
98
89
|
"default": "./lib/common/application/index.js"
|
|
99
90
|
},
|
|
100
91
|
"./lib/common/config": {
|
|
101
|
-
"webpack": "./lib/common/config/index.js",
|
|
102
92
|
"import": "./esm/common/config/index.js",
|
|
103
93
|
"default": "./lib/common/config/index.js"
|
|
104
94
|
},
|
|
105
95
|
"./lib/common/container": {
|
|
106
|
-
"webpack": "./lib/common/container/index.js",
|
|
107
96
|
"import": "./esm/common/container/index.js",
|
|
108
97
|
"default": "./lib/common/container/index.js"
|
|
109
98
|
},
|
|
110
99
|
"./lib/common/el": {
|
|
111
|
-
"webpack": "./lib/common/el/index.js",
|
|
112
100
|
"import": "./esm/common/el/index.js",
|
|
113
101
|
"default": "./lib/common/el/index.js"
|
|
114
102
|
},
|
|
115
103
|
"./lib/common/error": {
|
|
116
|
-
"webpack": "./lib/common/error/index.js",
|
|
117
104
|
"import": "./esm/common/error/index.js",
|
|
118
105
|
"default": "./lib/common/error/index.js"
|
|
119
106
|
},
|
|
120
107
|
"./lib/common/logger": {
|
|
121
|
-
"webpack": "./lib/common/logger/index.js",
|
|
122
108
|
"import": "./esm/common/logger/index.js",
|
|
123
109
|
"default": "./lib/common/logger/index.js"
|
|
124
110
|
},
|
|
125
111
|
"./lib/common/provider": {
|
|
126
|
-
"webpack": "./lib/common/provider/index.js",
|
|
127
112
|
"import": "./esm/common/provider/index.js",
|
|
128
113
|
"default": "./lib/common/provider/index.js"
|
|
129
114
|
},
|
|
130
115
|
"./lib/common/test": {
|
|
131
|
-
"webpack": "./lib/common/test/index.js",
|
|
132
116
|
"import": "./esm/common/test/index.js",
|
|
133
117
|
"default": "./lib/common/test/index.js"
|
|
134
118
|
},
|
|
135
119
|
"./lib/common/utils": {
|
|
136
|
-
"webpack": "./lib/common/utils/index.js",
|
|
137
120
|
"import": "./esm/common/utils/index.js",
|
|
138
121
|
"default": "./lib/common/utils/index.js"
|
|
139
122
|
},
|
|
140
123
|
"./lib/node": {
|
|
141
|
-
"webpack": "./lib/node/index.js",
|
|
142
124
|
"import": "./esm/node/index.js",
|
|
143
125
|
"default": "./lib/node/index.js"
|
|
144
126
|
},
|
|
145
127
|
"./lib/node/application": {
|
|
146
|
-
"webpack": "./lib/node/application/index.js",
|
|
147
128
|
"import": "./esm/node/application/index.js",
|
|
148
129
|
"default": "./lib/node/application/index.js"
|
|
149
130
|
},
|
|
150
131
|
"./lib/node/context": {
|
|
151
|
-
"webpack": "./lib/node/context/index.js",
|
|
152
132
|
"import": "./esm/node/context/index.js",
|
|
153
133
|
"default": "./lib/node/context/index.js"
|
|
154
134
|
},
|
|
155
135
|
"./lib/node/utils": {
|
|
156
|
-
"webpack": "./lib/node/utils/index.js",
|
|
157
136
|
"import": "./esm/node/utils/index.js",
|
|
158
137
|
"default": "./lib/node/utils/index.js"
|
|
159
138
|
},
|
|
160
139
|
"./lib/*.js": {
|
|
161
|
-
"webpack": "./lib/*.js",
|
|
162
140
|
"import": "./esm/*.js",
|
|
163
141
|
"default": "./lib/*.js"
|
|
164
142
|
},
|
|
165
143
|
"./lib/*": {
|
|
166
|
-
"webpack": "./lib/*.js",
|
|
167
144
|
"import": "./esm/*.js",
|
|
168
145
|
"default": "./lib/*.js"
|
|
169
146
|
},
|
|
170
147
|
"./*": "./*"
|
|
148
|
+
},
|
|
149
|
+
"cell": {
|
|
150
|
+
"staticModules": {
|
|
151
|
+
"frontend": [
|
|
152
|
+
"lib/common/static-module",
|
|
153
|
+
"lib/browser/static-module"
|
|
154
|
+
],
|
|
155
|
+
"backend": [
|
|
156
|
+
"lib/common/static-module",
|
|
157
|
+
"lib/node/static-module"
|
|
158
|
+
]
|
|
159
|
+
}
|
|
171
160
|
}
|
|
172
161
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { container } from '../container/dynamic-container';
|
|
2
2
|
import { Application } from './application-protocol';
|
|
3
|
+
import { ApplicationHost } from './application-host';
|
|
3
4
|
import { ContainerProvider } from '../container';
|
|
4
5
|
export { container } from '../container/dynamic-container';
|
|
5
6
|
|
|
7
|
+
const host = ApplicationHost.get();
|
|
8
|
+
|
|
6
9
|
(async () => {
|
|
7
10
|
const c = await container;
|
|
8
11
|
try {
|
|
@@ -12,12 +15,25 @@ export { container } from '../container/dynamic-container';
|
|
|
12
15
|
// NoOp
|
|
13
16
|
}
|
|
14
17
|
ContainerProvider.set(c);
|
|
18
|
+
await host?.beforeStart?.();
|
|
15
19
|
const application = c.get<Application>(Application);
|
|
16
|
-
|
|
20
|
+
try {
|
|
21
|
+
await application.start();
|
|
22
|
+
host?.started({ container: c, stop: () => application.stop() });
|
|
23
|
+
} catch (reason) {
|
|
24
|
+
if (host) {
|
|
25
|
+
host.failed(reason);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
17
28
|
console.error(`Failed to start the ${application.constructor.name}.`);
|
|
18
29
|
if (reason) {
|
|
19
30
|
console.error(reason);
|
|
20
31
|
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
}
|
|
33
|
+
})().catch(error => {
|
|
34
|
+
// The container failed to load (a DI module threw): stop, or tell the host.
|
|
35
|
+
if (!host) {
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
host.failed(error);
|
|
39
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an application entry reports to the process that loaded it, instead of listening on a port.
|
|
3
|
+
* `handler` is the HTTP request handler, `server` the server it is attached to (when the host has one).
|
|
4
|
+
*/
|
|
5
|
+
export interface StartedApplication {
|
|
6
|
+
/** The DI container of the application. */
|
|
7
|
+
container: unknown;
|
|
8
|
+
/** Node.js `(req, res)` handler of HTTP entries. */
|
|
9
|
+
handler?: (req: any, res: any, next?: (error?: unknown) => void) => void;
|
|
10
|
+
/** Stops the application: lifecycle `onStop` hooks, timers, connections. */
|
|
11
|
+
stop(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A process that runs the application in-process: `cell dev` (one port with Vite) and tests. When a
|
|
16
|
+
* host is set, entries hand over their request handler with {@link started} instead of listening.
|
|
17
|
+
*/
|
|
18
|
+
export interface ApplicationHost {
|
|
19
|
+
/** The HTTP server requests come from; passed to `ServerAware` components (WebSocket and the like). */
|
|
20
|
+
readonly server?: unknown;
|
|
21
|
+
/**
|
|
22
|
+
* Called once the DI container is loaded, before the application starts; the application starts
|
|
23
|
+
* when the returned promise resolves. Cloudflare Workers allow code generation (`new Function`)
|
|
24
|
+
* only while modules load and I/O only while a request is handled, so the worker loads the
|
|
25
|
+
* modules at startup and starts the application on the first request.
|
|
26
|
+
*/
|
|
27
|
+
beforeStart?(): Promise<void>;
|
|
28
|
+
started(application: StartedApplication): void;
|
|
29
|
+
failed(error: unknown): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export namespace ApplicationHost {
|
|
33
|
+
/**
|
|
34
|
+
* A global symbol: the host is set by the toolchain, which does not share module instances with
|
|
35
|
+
* the application it loads (a Vite module runner, a bundle).
|
|
36
|
+
*/
|
|
37
|
+
export const KEY = Symbol.for('@celljs/core/application-host');
|
|
38
|
+
|
|
39
|
+
export function get(): ApplicationHost | undefined {
|
|
40
|
+
return (globalThis as Record<symbol, ApplicationHost | undefined>)[KEY];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function set(host: ApplicationHost | undefined): void {
|
|
44
|
+
(globalThis as Record<symbol, ApplicationHost | undefined>)[KEY] = host;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import * as chai from 'chai';
|
|
3
|
+
import { EnvConfigUtil } from './env-config-util';
|
|
4
|
+
const expect = chai.expect;
|
|
5
|
+
|
|
6
|
+
describe('EnvConfigUtil', () => {
|
|
7
|
+
const config = () => ({
|
|
8
|
+
cell: {
|
|
9
|
+
server: { path: '/', port: 3000, endpoint: 'http://localhost:${cell.server.port}' },
|
|
10
|
+
'serve-static': { enabled: true },
|
|
11
|
+
typeorm: { ormConfig: [{ type: 'sqlite', database: 'db.sql', logging: false }] },
|
|
12
|
+
cors: null,
|
|
13
|
+
},
|
|
14
|
+
accounts: { title: 'Accounts', providers: ['github'] },
|
|
15
|
+
mode: ['production'],
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('sets keys under cell, matching case, underscores and dashes loosely', () => {
|
|
19
|
+
const target = config();
|
|
20
|
+
const result = EnvConfigUtil.apply(
|
|
21
|
+
target,
|
|
22
|
+
{
|
|
23
|
+
CELL_BACKEND__SERVER__PORT: '8080',
|
|
24
|
+
CELL_BACKEND__SERVE_STATIC__ENABLED: 'FALSE',
|
|
25
|
+
CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE: '/data/db.sql',
|
|
26
|
+
CELL_BACKEND__TYPEORM__ORM_CONFIG__0__LOGGING: 'true',
|
|
27
|
+
PATH: '/usr/bin',
|
|
28
|
+
},
|
|
29
|
+
'backend',
|
|
30
|
+
);
|
|
31
|
+
expect(result.problems).to.deep.equal([]);
|
|
32
|
+
expect(result.applied).to.deep.equal([
|
|
33
|
+
{ variable: 'CELL_BACKEND__SERVER__PORT', path: 'cell.server.port' },
|
|
34
|
+
{ variable: 'CELL_BACKEND__SERVE_STATIC__ENABLED', path: 'cell.serve-static.enabled' },
|
|
35
|
+
{ variable: 'CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE', path: 'cell.typeorm.ormConfig.0.database' },
|
|
36
|
+
{ variable: 'CELL_BACKEND__TYPEORM__ORM_CONFIG__0__LOGGING', path: 'cell.typeorm.ormConfig.0.logging' },
|
|
37
|
+
]);
|
|
38
|
+
expect(target.cell.server.port).to.equal(8080);
|
|
39
|
+
expect(target.cell['serve-static'].enabled).to.equal(false);
|
|
40
|
+
expect(target.cell.typeorm.ormConfig[0]).to.deep.equal({ type: 'sqlite', database: '/data/db.sql', logging: true });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('keeps expressions of other keys for later evaluation', () => {
|
|
44
|
+
const target = config();
|
|
45
|
+
EnvConfigUtil.apply(target, { CELL_BACKEND__SERVER__PORT: '8080' }, 'backend');
|
|
46
|
+
expect(target.cell.server.endpoint).to.equal('http://localhost:${cell.server.port}');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('reaches top-level keys outside cell from the root', () => {
|
|
50
|
+
const target = config();
|
|
51
|
+
const { applied } = EnvConfigUtil.apply(target, { CELL_BACKEND__ACCOUNTS__TITLE: 'Team', CELL_BACKEND__CELL__SERVER__PATH: '/api' }, 'backend');
|
|
52
|
+
expect(applied.map(a => a.path)).to.deep.equal(['accounts.title', 'cell.server.path']);
|
|
53
|
+
expect(target.accounts.title).to.equal('Team');
|
|
54
|
+
expect(target.cell.server.path).to.equal('/api');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('parses objects and arrays as JSON and leaves strings as they are', () => {
|
|
58
|
+
const target = config();
|
|
59
|
+
const result = EnvConfigUtil.apply(
|
|
60
|
+
target,
|
|
61
|
+
{
|
|
62
|
+
CELL_BACKEND__ACCOUNTS__PROVIDERS: '["github","gitlab"]',
|
|
63
|
+
CELL_BACKEND__CORS: '{"origin":"*"}',
|
|
64
|
+
CELL_BACKEND__SERVER__ENDPOINT: '8080',
|
|
65
|
+
},
|
|
66
|
+
'backend',
|
|
67
|
+
);
|
|
68
|
+
expect(result.problems).to.deep.equal([]);
|
|
69
|
+
expect(target.accounts.providers).to.deep.equal(['github', 'gitlab']);
|
|
70
|
+
expect(target.cell.cors).to.deep.equal({ origin: '*' });
|
|
71
|
+
expect(target.cell.server.endpoint).to.equal('8080');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('reports variables that match no key or do not fit its type, and changes nothing for them', () => {
|
|
75
|
+
const target = config();
|
|
76
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
77
|
+
target,
|
|
78
|
+
{
|
|
79
|
+
CELL_BACKEND__SERVER__PROT: '1',
|
|
80
|
+
CELL_BACKEND__SERVR__PORT: '1',
|
|
81
|
+
CELL_BACKEND__SERVER__PORT: 'abc',
|
|
82
|
+
CELL_BACKEND__SERVE_STATIC__ENABLED: 'yes',
|
|
83
|
+
CELL_BACKEND__ACCOUNTS__PROVIDERS: 'github',
|
|
84
|
+
CELL_BACKEND__TYPEORM__ORM_CONFIG__3__TYPE: 'mysql',
|
|
85
|
+
CELL_BACKEND__SERVER__PORT__X: '1',
|
|
86
|
+
CELL_BACKEND__SERVER____PORT: '1',
|
|
87
|
+
},
|
|
88
|
+
'backend',
|
|
89
|
+
);
|
|
90
|
+
expect(applied).to.deep.equal([]);
|
|
91
|
+
expect(target).to.deep.equal(config());
|
|
92
|
+
const messages = Object.fromEntries(problems.map(p => [p.variable, p.message]));
|
|
93
|
+
expect(messages.CELL_BACKEND__SERVER__PROT).to.equal(
|
|
94
|
+
"no key 'PROT' under cell.server (keys: path, port, endpoint). Only keys present in cell.yml can be overridden.",
|
|
95
|
+
);
|
|
96
|
+
expect(messages.CELL_BACKEND__SERVR__PORT).to.equal(
|
|
97
|
+
"no key 'SERVR' at the top level (keys: cell, accounts, mode) or under cell (keys: server, serve-static, typeorm, cors). Only keys present in cell.yml can be overridden.",
|
|
98
|
+
);
|
|
99
|
+
expect(messages.CELL_BACKEND__SERVER__PORT).to.equal("cell.server.port is a number, got 'abc'. Use a decimal number such as 8080, -1 or 0.5.");
|
|
100
|
+
expect(messages.CELL_BACKEND__SERVE_STATIC__ENABLED).to.equal("cell.serve-static.enabled is a boolean, got 'yes'. Use true or false.");
|
|
101
|
+
expect(messages.CELL_BACKEND__ACCOUNTS__PROVIDERS).to.contain('accounts.providers is an array; pass it as JSON');
|
|
102
|
+
expect(messages.CELL_BACKEND__TYPEORM__ORM_CONFIG__3__TYPE).to.contain('(indexes 0-0)');
|
|
103
|
+
expect(messages.CELL_BACKEND__SERVER__PORT__X).to.equal("cell.server.port is not an object, so it has no key 'X'.");
|
|
104
|
+
expect(messages.CELL_BACKEND__SERVER____PORT).to.contain('empty segment');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('applies the backend variables of process.env and warns about the ones that do not apply', () => {
|
|
108
|
+
const variables = { CELL_BACKEND__SERVER__PORT: '8080', CELL_BACKEND__SERVER__PROT: '1', CELL_FRONTEND__SERVER__PATH: '/ui' };
|
|
109
|
+
const warn = console.warn;
|
|
110
|
+
const warnings: string[] = [];
|
|
111
|
+
console.warn = (message: string) => warnings.push(message);
|
|
112
|
+
Object.assign(process.env, variables);
|
|
113
|
+
try {
|
|
114
|
+
const target = config();
|
|
115
|
+
const result = EnvConfigUtil.applyProcessEnv(target);
|
|
116
|
+
expect(result.applied).to.deep.equal([{ variable: 'CELL_BACKEND__SERVER__PORT', path: 'cell.server.port' }]);
|
|
117
|
+
expect(target.cell.server).to.deep.include({ port: 8080, path: '/' });
|
|
118
|
+
expect(warnings).to.deep.equal([
|
|
119
|
+
"[cell] CELL_BACKEND__SERVER__PROT is ignored: no key 'PROT' under cell.server (keys: path, port, endpoint). Only keys present in cell.yml can be overridden.",
|
|
120
|
+
]);
|
|
121
|
+
} finally {
|
|
122
|
+
console.warn = warn;
|
|
123
|
+
for (const name of Object.keys(variables)) {
|
|
124
|
+
delete process.env[name];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('takes finite numbers in JSON syntax only', () => {
|
|
130
|
+
const port = (value: string) => {
|
|
131
|
+
const target = config();
|
|
132
|
+
const { applied, problems } = EnvConfigUtil.apply(target, { CELL_BACKEND__SERVER__PORT: value }, 'backend');
|
|
133
|
+
return applied.length === 1 ? target.cell.server.port : problems[0].message;
|
|
134
|
+
};
|
|
135
|
+
for (const [value, expected] of [['8080', 8080], ['0', 0], ['-1', -1], ['0.5', 0.5], ['1e3', 1000], ['-2.5E-1', -0.25]] as const) {
|
|
136
|
+
expect(port(value), value).to.equal(expected);
|
|
137
|
+
}
|
|
138
|
+
for (const value of ['Infinity', '-Infinity', 'NaN', '1e999', '0x10', '0b1', '1_000', '+1', '08080', '.5', '5.', '', ' 8080', '8080 ']) {
|
|
139
|
+
expect(port(value), JSON.stringify(value)).to.equal(`cell.server.port is a number, got '${value}'. Use a decimal number such as 8080, -1 or 0.5.`);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('follows null in cell.yml with JSON when the value is JSON, else text', () => {
|
|
144
|
+
const cors = (value: string) => {
|
|
145
|
+
const target = config();
|
|
146
|
+
EnvConfigUtil.apply(target, { CELL_BACKEND__CORS: value }, 'backend');
|
|
147
|
+
return target.cell.cors;
|
|
148
|
+
};
|
|
149
|
+
expect(cors('null')).to.equal(null);
|
|
150
|
+
expect(cors('5')).to.equal(5);
|
|
151
|
+
expect(cors('false')).to.equal(false);
|
|
152
|
+
expect(cors('"5"')).to.equal('5');
|
|
153
|
+
expect(cors('[1]')).to.deep.equal([1]);
|
|
154
|
+
expect(cors('*')).to.equal('*');
|
|
155
|
+
expect(cors('')).to.equal('');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('keeps a value that is one ${...} expression for evaluation, whatever the type of the key', () => {
|
|
159
|
+
const target = config();
|
|
160
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
161
|
+
target,
|
|
162
|
+
{
|
|
163
|
+
CELL_BACKEND__SERVER__PORT: '${env.PORT ?: 3000}',
|
|
164
|
+
CELL_BACKEND__SERVE_STATIC__ENABLED: '${env.STATIC == "on"}',
|
|
165
|
+
CELL_BACKEND__ACCOUNTS__PROVIDERS: '${cell.providers}',
|
|
166
|
+
CELL_BACKEND__SERVER__PATH: '/${env.BASE}',
|
|
167
|
+
},
|
|
168
|
+
'backend',
|
|
169
|
+
);
|
|
170
|
+
expect(problems).to.deep.equal([]);
|
|
171
|
+
expect(applied).to.have.length(4);
|
|
172
|
+
expect(target.cell.server.port).to.equal('${env.PORT ?: 3000}');
|
|
173
|
+
expect(target.cell['serve-static'].enabled).to.equal('${env.STATIC == "on"}');
|
|
174
|
+
expect(target.accounts.providers).to.equal('${cell.providers}');
|
|
175
|
+
expect(target.cell.server.path).to.equal('/${env.BASE}');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('rejects text around an expression where the key needs a number, boolean, object or array', () => {
|
|
179
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
180
|
+
config(),
|
|
181
|
+
{ CELL_BACKEND__SERVER__PORT: '80${env.X}', CELL_BACKEND__SERVE_STATIC__ENABLED: '${a} ${b}', CELL_BACKEND__ACCOUNTS__PROVIDERS: 'x${a}' },
|
|
182
|
+
'backend',
|
|
183
|
+
);
|
|
184
|
+
expect(applied).to.deep.equal([]);
|
|
185
|
+
expect(problems.map(p => p.variable)).to.deep.equal([
|
|
186
|
+
'CELL_BACKEND__ACCOUNTS__PROVIDERS',
|
|
187
|
+
'CELL_BACKEND__SERVER__PORT',
|
|
188
|
+
'CELL_BACKEND__SERVE_STATIC__ENABLED',
|
|
189
|
+
]);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('takes true and false in any case for booleans and names the value it rejects', () => {
|
|
193
|
+
const enabled = (value: string) => {
|
|
194
|
+
const target = config();
|
|
195
|
+
const { problems } = EnvConfigUtil.apply(target, { CELL_BACKEND__SERVE_STATIC__ENABLED: value }, 'backend');
|
|
196
|
+
return problems.length > 0 ? problems[0].message : target.cell['serve-static'].enabled;
|
|
197
|
+
};
|
|
198
|
+
expect(enabled('FALSE')).to.equal(false);
|
|
199
|
+
expect(enabled('True')).to.equal(true);
|
|
200
|
+
for (const value of ['yes', '1', '0', '', ' true']) {
|
|
201
|
+
expect(enabled(value)).to.equal(`cell.serve-static.enabled is a boolean, got '${value}'. Use true or false.`);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('rejects integers that a number cannot hold exactly', () => {
|
|
206
|
+
const port = (value: string) => {
|
|
207
|
+
const target = config();
|
|
208
|
+
const { problems } = EnvConfigUtil.apply(target, { CELL_BACKEND__SERVER__PORT: value }, 'backend');
|
|
209
|
+
return problems.length > 0 ? problems[0].message : target.cell.server.port;
|
|
210
|
+
};
|
|
211
|
+
expect(port('9007199254740991')).to.equal(9007199254740991);
|
|
212
|
+
expect(port('-9007199254740991')).to.equal(-9007199254740991);
|
|
213
|
+
expect(port('1.5e300')).to.equal(1.5e300);
|
|
214
|
+
expect(port('9007199254740993')).to.equal(
|
|
215
|
+
"cell.server.port is a number, got '9007199254740993', which is too large to keep exactly. Use a string in cell.yml for such values.",
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('addresses array items by canonical index only', () => {
|
|
220
|
+
const target = config();
|
|
221
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
222
|
+
target,
|
|
223
|
+
{
|
|
224
|
+
CELL_BACKEND__ACCOUNTS__PROVIDERS__00: 'gitlab',
|
|
225
|
+
CELL_BACKEND__TYPEORM__ORM_CONFIG__00__TYPE: 'mysql',
|
|
226
|
+
CELL_BACKEND__TYPEORM__ORM_CONFIG__0__TYPE: 'postgres',
|
|
227
|
+
},
|
|
228
|
+
'backend',
|
|
229
|
+
);
|
|
230
|
+
expect(applied.map(a => a.path)).to.deep.equal(['cell.typeorm.ormConfig.0.type']);
|
|
231
|
+
expect(problems.map(p => p.variable)).to.deep.equal(['CELL_BACKEND__ACCOUNTS__PROVIDERS__00', 'CELL_BACKEND__TYPEORM__ORM_CONFIG__00__TYPE']);
|
|
232
|
+
expect(problems[0].message).to.equal("no key '00' under accounts.providers (indexes 0-0). Only keys present in cell.yml can be overridden.");
|
|
233
|
+
// '00' must not become a property of the array next to item 0.
|
|
234
|
+
expect(Object.keys(target.accounts.providers)).to.deep.equal(['0']);
|
|
235
|
+
expect(target.cell.typeorm.ormConfig[0].type).to.equal('postgres');
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('keys set by more than one variable', () => {
|
|
239
|
+
const conflict = (env: Record<string, string>) => {
|
|
240
|
+
const target = config();
|
|
241
|
+
return { target, ...EnvConfigUtil.apply(target, env, 'backend') };
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
it('applies none of the variables that set the same key, however they spell it', () => {
|
|
245
|
+
const { target, applied, problems } = conflict({
|
|
246
|
+
CELL_BACKEND__SERVER__PORT: '1',
|
|
247
|
+
CELL_BACKEND__CELL__SERVER__PORT: '2',
|
|
248
|
+
CELL_BACKEND__SERVER__P_ORT: '3',
|
|
249
|
+
CELL_BACKEND__SERVER__PATH: '/api',
|
|
250
|
+
});
|
|
251
|
+
expect(applied).to.deep.equal([{ variable: 'CELL_BACKEND__SERVER__PATH', path: 'cell.server.path' }]);
|
|
252
|
+
expect(target.cell.server.port).to.equal(3000);
|
|
253
|
+
expect(problems.map(p => p.variable)).to.deep.equal(['CELL_BACKEND__CELL__SERVER__PORT', 'CELL_BACKEND__SERVER__PORT', 'CELL_BACKEND__SERVER__P_ORT']);
|
|
254
|
+
expect(problems[1].message).to.equal(
|
|
255
|
+
'sets cell.server.port, which CELL_BACKEND__CELL__SERVER__PORT (cell.server.port), CELL_BACKEND__SERVER__P_ORT (cell.server.port) also set. Set each key with one variable; to change items of an object or array you replace, write them into its JSON.',
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('applies neither a key nor a key inside it, in either order of the names', () => {
|
|
260
|
+
const pairs = [
|
|
261
|
+
// The whole array sorts first, then the item.
|
|
262
|
+
['CELL_BACKEND__TYPEORM__ORM_CONFIG', 'CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE'],
|
|
263
|
+
// The item sorts first: applied in order, the array would have wiped it out.
|
|
264
|
+
['CELL_BACKEND__TYPEORM__ORM_CONFIG', 'CELL_BACKEND__CELL__TYPEORM__ORM_CONFIG__0__DATABASE'],
|
|
265
|
+
['CELL_BACKEND__CELL__TYPEORM__ORM_CONFIG', 'CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE'],
|
|
266
|
+
];
|
|
267
|
+
for (const [whole, item] of pairs) {
|
|
268
|
+
const { target, applied, problems } = conflict({ [whole]: '[{"type":"mysql"}]', [item]: 'x.sql' });
|
|
269
|
+
expect(applied, `${whole} + ${item}`).to.deep.equal([]);
|
|
270
|
+
expect(problems.map(p => p.variable).sort()).to.deep.equal([item, whole].sort());
|
|
271
|
+
expect(problems.find(p => p.variable === item)!.message).to.contain(`which ${whole} (cell.typeorm.ormConfig) also sets`);
|
|
272
|
+
expect(target.cell.typeorm.ormConfig).to.deep.equal(config().cell.typeorm.ormConfig);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('lets variables of different keys under the same object apply', () => {
|
|
277
|
+
const { applied, problems } = conflict({ CELL_BACKEND__TYPEORM__ORM_CONFIG__0__TYPE: 'mysql', CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE: 'x' });
|
|
278
|
+
expect(problems).to.deep.equal([]);
|
|
279
|
+
expect(applied).to.have.length(2);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('does not depend on the order of the variables in the environment', () => {
|
|
283
|
+
const names = ['CELL_BACKEND__TYPEORM__ORM_CONFIG__0__DATABASE', 'CELL_BACKEND__CELL__TYPEORM__ORM_CONFIG', 'CELL_BACKEND__SERVER__PORT'];
|
|
284
|
+
const run = (order: string[]) => {
|
|
285
|
+
const target = config();
|
|
286
|
+
const env = Object.fromEntries(order.map(name => [name, name.endsWith('ORM_CONFIG') ? '[]' : '1']));
|
|
287
|
+
return JSON.stringify({ result: EnvConfigUtil.apply(target, env, 'backend'), target });
|
|
288
|
+
};
|
|
289
|
+
expect(run([...names].reverse())).to.equal(run(names));
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe('top-level keys with the same name as keys under cell', () => {
|
|
294
|
+
const shadowed = () => ({ cell: { server: { port: 3000 }, accounts: { title: 'Inner' } }, server: { x: 1 }, accounts: { title: 'Outer', team: 't' } });
|
|
295
|
+
|
|
296
|
+
it('looks up the first key at the top level, then under cell', () => {
|
|
297
|
+
const target = shadowed();
|
|
298
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
299
|
+
target,
|
|
300
|
+
{ CELL_BACKEND__SERVER__X: '5', CELL_BACKEND__ACCOUNTS__TEAM: 'u', CELL_BACKEND__ACCOUNTS__TITLE: 'Top' },
|
|
301
|
+
'backend',
|
|
302
|
+
);
|
|
303
|
+
expect(problems).to.deep.equal([]);
|
|
304
|
+
expect(applied).to.deep.equal([
|
|
305
|
+
{ variable: 'CELL_BACKEND__ACCOUNTS__TEAM', path: 'accounts.team', shadows: 'cell.accounts' },
|
|
306
|
+
{ variable: 'CELL_BACKEND__ACCOUNTS__TITLE', path: 'accounts.title', shadows: 'cell.accounts' },
|
|
307
|
+
{ variable: 'CELL_BACKEND__SERVER__X', path: 'server.x', shadows: 'cell.server' },
|
|
308
|
+
]);
|
|
309
|
+
expect(target).to.deep.equal({ cell: { server: { port: 3000 }, accounts: { title: 'Inner' } }, server: { x: 5 }, accounts: { title: 'Top', team: 'u' } });
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('reaches the keys under cell with CELL__, and reports a key missing from the top-level one', () => {
|
|
313
|
+
const target = shadowed();
|
|
314
|
+
const { applied, problems } = EnvConfigUtil.apply(
|
|
315
|
+
target,
|
|
316
|
+
{ CELL_BACKEND__CELL__ACCOUNTS__TITLE: 'In', CELL_BACKEND__CELL__SERVER__PORT: '8080', CELL_BACKEND__SERVER__PORT: '1' },
|
|
317
|
+
'backend',
|
|
318
|
+
);
|
|
319
|
+
expect(applied).to.deep.equal([
|
|
320
|
+
{ variable: 'CELL_BACKEND__CELL__ACCOUNTS__TITLE', path: 'cell.accounts.title' },
|
|
321
|
+
{ variable: 'CELL_BACKEND__CELL__SERVER__PORT', path: 'cell.server.port' },
|
|
322
|
+
]);
|
|
323
|
+
expect(target.cell).to.deep.equal({ server: { port: 8080 }, accounts: { title: 'In' } });
|
|
324
|
+
expect(problems).to.deep.equal([
|
|
325
|
+
{ variable: 'CELL_BACKEND__SERVER__PORT', message: "no key 'PORT' under server (keys: x). Only keys present in cell.yml can be overridden." },
|
|
326
|
+
]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it('falls back to cell when the top level has no such key', () => {
|
|
330
|
+
const { applied } = EnvConfigUtil.apply(config(), { CELL_BACKEND__SERVER__PORT: '1' }, 'backend');
|
|
331
|
+
expect(applied).to.deep.equal([{ variable: 'CELL_BACKEND__SERVER__PORT', path: 'cell.server.port' }]);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('only reads the variables of the given target', () => {
|
|
336
|
+
const target = config();
|
|
337
|
+
const result = EnvConfigUtil.apply(target, { CELL_FRONTEND__SERVER__PORT: '1', CELL_BACKEND__SERVER__PORT: '2' }, 'frontend');
|
|
338
|
+
expect(result.applied).to.deep.equal([{ variable: 'CELL_FRONTEND__SERVER__PORT', path: 'cell.server.port' }]);
|
|
339
|
+
expect(target.cell.server.port).to.equal(1);
|
|
340
|
+
});
|
|
341
|
+
});
|