@homelib/core 0.0.0-wip.ba4eafa051eb
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 +19 -0
- package/README.md +7 -0
- package/bld/exports/mobx.d.ts +1 -0
- package/bld/exports/mobx.js +2 -0
- package/bld/exports/mobx.js.map +1 -0
- package/bld/library/@lifecycle.d.ts +4 -0
- package/bld/library/@lifecycle.js +23 -0
- package/bld/library/@lifecycle.js.map +1 -0
- package/bld/library/atomics/index.d.ts +1 -0
- package/bld/library/atomics/index.js +2 -0
- package/bld/library/atomics/index.js.map +1 -0
- package/bld/library/atomics/temperature.d.ts +13 -0
- package/bld/library/atomics/temperature.js +53 -0
- package/bld/library/atomics/temperature.js.map +1 -0
- package/bld/library/command.d.ts +12 -0
- package/bld/library/command.js +21 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/device/index.d.ts +1 -0
- package/bld/library/device/index.js +2 -0
- package/bld/library/device/index.js.map +1 -0
- package/bld/library/device/sensor.d.ts +8 -0
- package/bld/library/device/sensor.js +2 -0
- package/bld/library/device/sensor.js.map +1 -0
- package/bld/library/device-namespace.d.ts +11 -0
- package/bld/library/device-namespace.js +9 -0
- package/bld/library/device-namespace.js.map +1 -0
- package/bld/library/device.d.ts +23 -0
- package/bld/library/device.js +92 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +72 -0
- package/bld/library/devices/air-conditioner.js +246 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +62 -0
- package/bld/library/devices/dehumidifier.js +211 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +70 -0
- package/bld/library/devices/fan.js +229 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +4 -0
- package/bld/library/devices/index.js +5 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +59 -0
- package/bld/library/devices/light.js +197 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/endpoint.d.ts +100 -0
- package/bld/library/endpoint.js +431 -0
- package/bld/library/endpoint.js.map +1 -0
- package/bld/library/home.d.ts +4 -0
- package/bld/library/home.js +11 -0
- package/bld/library/home.js.map +1 -0
- package/bld/library/index.d.ts +16 -0
- package/bld/library/index.js +17 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/log.d.ts +43 -0
- package/bld/library/log.js +78 -0
- package/bld/library/log.js.map +1 -0
- package/bld/library/provider.d.ts +26 -0
- package/bld/library/provider.js +18 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/registry.d.ts +19 -0
- package/bld/library/registry.js +65 -0
- package/bld/library/registry.js.map +1 -0
- package/bld/library/runtime/@binding-creation.d.ts +4 -0
- package/bld/library/runtime/@binding-creation.js +19 -0
- package/bld/library/runtime/@binding-creation.js.map +1 -0
- package/bld/library/runtime/binding.d.ts +50 -0
- package/bld/library/runtime/binding.js +108 -0
- package/bld/library/runtime/binding.js.map +1 -0
- package/bld/library/runtime/bootstrap-frontend.d.ts +48 -0
- package/bld/library/runtime/bootstrap-frontend.js +96 -0
- package/bld/library/runtime/bootstrap-frontend.js.map +1 -0
- package/bld/library/runtime/bootstrap.d.ts +9 -0
- package/bld/library/runtime/bootstrap.js +165 -0
- package/bld/library/runtime/bootstrap.js.map +1 -0
- package/bld/library/runtime/environment.d.ts +1 -0
- package/bld/library/runtime/environment.js +7 -0
- package/bld/library/runtime/environment.js.map +1 -0
- package/bld/library/runtime/index.d.ts +4 -0
- package/bld/library/runtime/index.js +5 -0
- package/bld/library/runtime/index.js.map +1 -0
- package/bld/library/scope.d.ts +41 -0
- package/bld/library/scope.js +132 -0
- package/bld/library/scope.js.map +1 -0
- package/bld/library/types.d.ts +17 -0
- package/bld/library/types.js +8 -0
- package/bld/library/types.js.map +1 -0
- package/bld/library/utils/backoff.d.ts +10 -0
- package/bld/library/utils/backoff.js +63 -0
- package/bld/library/utils/backoff.js.map +1 -0
- package/bld/library/utils/constructor.d.ts +21 -0
- package/bld/library/utils/constructor.js +30 -0
- package/bld/library/utils/constructor.js.map +1 -0
- package/bld/library/utils/index.d.ts +2 -0
- package/bld/library/utils/index.js +3 -0
- package/bld/library/utils/index.js.map +1 -0
- package/bld/library/x-data-medium.d.ts +12 -0
- package/bld/library/x-data-medium.js +3 -0
- package/bld/library/x-data-medium.js.map +1 -0
- package/package.json +27 -0
- package/src/exports/mobx.ts +1 -0
- package/src/library/@lifecycle.ts +30 -0
- package/src/library/atomics/index.ts +1 -0
- package/src/library/atomics/temperature.ts +60 -0
- package/src/library/command.ts +25 -0
- package/src/library/device/index.ts +1 -0
- package/src/library/device/sensor.ts +10 -0
- package/src/library/device-namespace.ts +21 -0
- package/src/library/device.ts +91 -0
- package/src/library/devices/air-conditioner.ts +234 -0
- package/src/library/devices/dehumidifier.ts +200 -0
- package/src/library/devices/fan.ts +204 -0
- package/src/library/devices/index.ts +4 -0
- package/src/library/devices/light.ts +177 -0
- package/src/library/endpoint.ts +610 -0
- package/src/library/home.ts +11 -0
- package/src/library/index.ts +16 -0
- package/src/library/log.ts +163 -0
- package/src/library/provider.ts +66 -0
- package/src/library/registry.ts +129 -0
- package/src/library/runtime/@binding-creation.ts +48 -0
- package/src/library/runtime/binding.ts +165 -0
- package/src/library/runtime/bootstrap-frontend.ts +243 -0
- package/src/library/runtime/bootstrap.ts +286 -0
- package/src/library/runtime/environment.ts +6 -0
- package/src/library/runtime/index.ts +4 -0
- package/src/library/scope.ts +193 -0
- package/src/library/types/global.d.ts +8 -0
- package/src/library/types.ts +26 -0
- package/src/library/utils/backoff.ts +65 -0
- package/src/library/utils/constructor.ts +89 -0
- package/src/library/utils/index.ts +2 -0
- package/src/library/x-data-medium.ts +16 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import {assertDeclaring} from '../@lifecycle.js';
|
|
2
|
+
import type {Device, DeviceConstructor} from '../device.js';
|
|
3
|
+
import type {EndpointReference} from '../endpoint.js';
|
|
4
|
+
import type {RuntimeProvider} from '../provider.js';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
type BindingFile,
|
|
8
|
+
type EndpointBinding,
|
|
9
|
+
type EndpointPath,
|
|
10
|
+
getEndpointPathKey,
|
|
11
|
+
upsertEndpointBinding,
|
|
12
|
+
} from './binding.js';
|
|
13
|
+
|
|
14
|
+
let bootstrapFrontend: BootstrapFrontend | undefined;
|
|
15
|
+
|
|
16
|
+
export type BootstrapFrontend = (
|
|
17
|
+
context: BootstrapContext,
|
|
18
|
+
) => void | PromiseLike<void>;
|
|
19
|
+
|
|
20
|
+
export type BootstrapContext = {
|
|
21
|
+
readonly providers: readonly BootstrapProvider[];
|
|
22
|
+
readonly bindingScopes: readonly BootstrapBindingScope[];
|
|
23
|
+
readonly initialBindingFile: BindingFile;
|
|
24
|
+
readonly updateBindingFile: (
|
|
25
|
+
createNextBindingFile: (currentBindingFile: BindingFile) => BindingFile,
|
|
26
|
+
) => Promise<BindingFile>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type BootstrapProvider = {
|
|
30
|
+
readonly namespace: string;
|
|
31
|
+
readonly provider: RuntimeProvider;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type BootstrapBindingScope = {
|
|
35
|
+
readonly path: readonly string[];
|
|
36
|
+
readonly scopes: readonly BootstrapBindingScope[];
|
|
37
|
+
readonly devices: readonly BootstrapBindingDevice[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type BootstrapBindingDevice = {
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly deviceConstructors: readonly DeviceConstructor<Device>[];
|
|
43
|
+
readonly endpoints: readonly BootstrapBindingEndpoint[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type BootstrapBindingEndpoint = {
|
|
47
|
+
readonly path: EndpointPath;
|
|
48
|
+
readonly endpoint: EndpointReference;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type ProviderBindingEndpoint = BootstrapBindingEndpoint & {
|
|
52
|
+
readonly binding: EndpointBinding | undefined;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type ProviderBindingDevice = {
|
|
56
|
+
readonly name: string;
|
|
57
|
+
readonly deviceConstructors: readonly DeviceConstructor<Device>[];
|
|
58
|
+
readonly endpoints: readonly ProviderBindingEndpoint[];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type ProviderBindingRecord = {
|
|
62
|
+
readonly endpoint: EndpointPath;
|
|
63
|
+
readonly metadata: unknown;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type ProviderBindingRequest = {
|
|
67
|
+
readonly endpoint: EndpointPath;
|
|
68
|
+
readonly metadata: unknown;
|
|
69
|
+
readonly replaceExisting: boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export function registerBootstrapFrontend(frontend: BootstrapFrontend): void {
|
|
73
|
+
assertDeclaring();
|
|
74
|
+
|
|
75
|
+
if (bootstrapFrontend !== undefined) {
|
|
76
|
+
throw new TypeError('Duplicate bootstrap frontend registration.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
bootstrapFrontend = frontend;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @internal */
|
|
83
|
+
export function getBootstrapFrontend(): BootstrapFrontend | undefined {
|
|
84
|
+
return bootstrapFrontend;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function applyProviderBindingRequests(
|
|
88
|
+
bindingFile: BindingFile,
|
|
89
|
+
requests: readonly ProviderBindingRequest[],
|
|
90
|
+
providerReference: EndpointBinding['provider'],
|
|
91
|
+
provider: RuntimeProvider,
|
|
92
|
+
device: ProviderBindingDevice,
|
|
93
|
+
scopes: readonly BootstrapBindingScope[],
|
|
94
|
+
): BindingFile {
|
|
95
|
+
const endpointPathKeySet = new Set<string>();
|
|
96
|
+
const resolvedRequests = requests.map(request => {
|
|
97
|
+
const endpointPathKey = getEndpointPathKey(request.endpoint);
|
|
98
|
+
|
|
99
|
+
if (endpointPathKeySet.has(endpointPathKey)) {
|
|
100
|
+
throw new TypeError(
|
|
101
|
+
`Duplicate endpoint binding request: ${endpointPathKey}.`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
endpointPathKeySet.add(endpointPathKey);
|
|
106
|
+
|
|
107
|
+
const endpoint = findProviderBindingEndpoint(device, request.endpoint);
|
|
108
|
+
|
|
109
|
+
if (endpoint === undefined) {
|
|
110
|
+
throw new TypeError('Unknown endpoint selected for binding.');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {endpoint, request};
|
|
114
|
+
});
|
|
115
|
+
let nextBindingFile = bindingFile;
|
|
116
|
+
|
|
117
|
+
for (const {endpoint, request} of resolvedRequests) {
|
|
118
|
+
const currentBinding = findEndpointBinding(nextBindingFile, endpoint.path);
|
|
119
|
+
|
|
120
|
+
if (
|
|
121
|
+
currentBinding !== undefined &&
|
|
122
|
+
(currentBinding.provider.namespace !== providerReference.namespace ||
|
|
123
|
+
currentBinding.provider.name !== providerReference.name) &&
|
|
124
|
+
!request.replaceExisting
|
|
125
|
+
) {
|
|
126
|
+
throw new TypeError(
|
|
127
|
+
'Replacing another provider binding requires confirmation.',
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
nextBindingFile = upsertEndpointBinding(nextBindingFile, {
|
|
132
|
+
endpoint: endpoint.path,
|
|
133
|
+
provider: providerReference,
|
|
134
|
+
metadata: request.metadata,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
assertProviderResourceBindingsUnique(
|
|
139
|
+
nextBindingFile,
|
|
140
|
+
providerReference,
|
|
141
|
+
provider,
|
|
142
|
+
scopes,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return nextBindingFile;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function assertProviderResourceBindingsUnique(
|
|
149
|
+
bindingFile: BindingFile,
|
|
150
|
+
providerReference: EndpointBinding['provider'],
|
|
151
|
+
provider: RuntimeProvider,
|
|
152
|
+
scopes: readonly BootstrapBindingScope[],
|
|
153
|
+
): void {
|
|
154
|
+
const resourceKeySet = new Set<string>();
|
|
155
|
+
|
|
156
|
+
for (const binding of bindingFile.bindings) {
|
|
157
|
+
if (
|
|
158
|
+
binding.provider.namespace !== providerReference.namespace ||
|
|
159
|
+
binding.provider.name !== providerReference.name
|
|
160
|
+
) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const target = findBootstrapBindingEndpoint(scopes, binding.endpoint);
|
|
165
|
+
|
|
166
|
+
if (target === undefined) {
|
|
167
|
+
// Stale bindings remain persisted for frontend inspection and cleanup,
|
|
168
|
+
// but do not reserve provider resources in the current topology.
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const plan = provider.createEndpointConnectionBindingPlan(
|
|
173
|
+
target.endpoint.endpoint,
|
|
174
|
+
target.deviceConstructors,
|
|
175
|
+
binding.metadata,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
for (const resourceKey of plan.resourceKeys) {
|
|
179
|
+
if (resourceKeySet.has(resourceKey)) {
|
|
180
|
+
throw new Error(`Provider resource is already bound: ${resourceKey}.`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
resourceKeySet.add(resourceKey);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function findProviderBindingEndpoint(
|
|
189
|
+
device: ProviderBindingDevice,
|
|
190
|
+
path: EndpointPath,
|
|
191
|
+
): ProviderBindingEndpoint | undefined {
|
|
192
|
+
const pathKey = getEndpointPathKey(path);
|
|
193
|
+
|
|
194
|
+
return device.endpoints.find(
|
|
195
|
+
endpoint => getEndpointPathKey(endpoint.path) === pathKey,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function findEndpointBinding(
|
|
200
|
+
bindingFile: BindingFile,
|
|
201
|
+
path: EndpointPath,
|
|
202
|
+
): EndpointBinding | undefined {
|
|
203
|
+
const pathKey = getEndpointPathKey(path);
|
|
204
|
+
|
|
205
|
+
return bindingFile.bindings.find(
|
|
206
|
+
binding => getEndpointPathKey(binding.endpoint) === pathKey,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function findBootstrapBindingEndpoint(
|
|
211
|
+
scopes: readonly BootstrapBindingScope[],
|
|
212
|
+
path: EndpointPath,
|
|
213
|
+
): BootstrapBindingEndpointTarget | undefined {
|
|
214
|
+
const pathKey = getEndpointPathKey(path);
|
|
215
|
+
|
|
216
|
+
for (const scope of scopes) {
|
|
217
|
+
for (const device of scope.devices) {
|
|
218
|
+
const endpoint = device.endpoints.find(
|
|
219
|
+
item => getEndpointPathKey(item.path) === pathKey,
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
if (endpoint !== undefined) {
|
|
223
|
+
return {
|
|
224
|
+
endpoint,
|
|
225
|
+
deviceConstructors: device.deviceConstructors,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const endpoint = findBootstrapBindingEndpoint(scope.scopes, path);
|
|
231
|
+
|
|
232
|
+
if (endpoint !== undefined) {
|
|
233
|
+
return endpoint;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
type BootstrapBindingEndpointTarget = {
|
|
241
|
+
readonly endpoint: BootstrapBindingEndpoint;
|
|
242
|
+
readonly deviceConstructors: readonly DeviceConstructor<Device>[];
|
|
243
|
+
};
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import {
|
|
2
|
+
beginBootstrap,
|
|
3
|
+
completeBootstrap,
|
|
4
|
+
failBootstrap,
|
|
5
|
+
} from '../@lifecycle.js';
|
|
6
|
+
import type {Device, DeviceConstructor} from '../device.js';
|
|
7
|
+
import type {EndpointReference} from '../endpoint.js';
|
|
8
|
+
import {setEndpointLogTarget} from '../log.js';
|
|
9
|
+
import type {EndpointConnectionBindingPlan} from '../provider.js';
|
|
10
|
+
import {getProvider, getProviderEntries, getRootScopes} from '../registry.js';
|
|
11
|
+
import type {Scope} from '../scope.js';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
createEndpointConnectionBindings,
|
|
15
|
+
disposeEndpointConnectionBindings,
|
|
16
|
+
} from './@binding-creation.js';
|
|
17
|
+
import {
|
|
18
|
+
BindingFile,
|
|
19
|
+
type EndpointPath,
|
|
20
|
+
getEndpointPath,
|
|
21
|
+
getEndpointPathKey,
|
|
22
|
+
readBindingFile,
|
|
23
|
+
writeBindingFile,
|
|
24
|
+
} from './binding.js';
|
|
25
|
+
import {
|
|
26
|
+
type BootstrapBindingScope,
|
|
27
|
+
type BootstrapContext,
|
|
28
|
+
getBootstrapFrontend,
|
|
29
|
+
} from './bootstrap-frontend.js';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Finalizes declarations and binds all configured endpoint connections.
|
|
33
|
+
*
|
|
34
|
+
* Resolves after every binding is installed. Individual endpoints may still be
|
|
35
|
+
* waiting to become ready; commands issued after this promise resolves remain
|
|
36
|
+
* queued until their connection is ready. A registered bootstrap frontend may
|
|
37
|
+
* inspect and update bindings before connections are created.
|
|
38
|
+
*/
|
|
39
|
+
export function bootstrap(): Promise<void> {
|
|
40
|
+
beginBootstrap();
|
|
41
|
+
|
|
42
|
+
return bootstrapInternal().then(
|
|
43
|
+
() => {
|
|
44
|
+
completeBootstrap();
|
|
45
|
+
},
|
|
46
|
+
error => {
|
|
47
|
+
failBootstrap();
|
|
48
|
+
throw error;
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function bootstrapInternal(): Promise<void> {
|
|
54
|
+
const initialBindingFile = await readBindingFile();
|
|
55
|
+
const rootScopes = [...getRootScopes()];
|
|
56
|
+
const {scopes, endpointMap} = collectBindingTopology(rootScopes);
|
|
57
|
+
const {context, close} = createBootstrapContext(initialBindingFile, scopes);
|
|
58
|
+
const frontend = getBootstrapFrontend();
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
await frontend?.(context);
|
|
62
|
+
} finally {
|
|
63
|
+
await close();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const bindingFile = await readBindingFile();
|
|
67
|
+
const connectionBindingPlans = createConnectionBindingPlans(
|
|
68
|
+
bindingFile,
|
|
69
|
+
endpointMap,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const bindings = await createEndpointConnectionBindings(
|
|
73
|
+
connectionBindingPlans.map(({plan}) => plan),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
for (const [index, binding] of bindings.entries()) {
|
|
78
|
+
const item = connectionBindingPlans[index];
|
|
79
|
+
|
|
80
|
+
if (item === undefined) {
|
|
81
|
+
throw new Error('Endpoint connection binding plan is missing.');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const {endpoint, path} = item;
|
|
85
|
+
setEndpointLogTarget(endpoint, path);
|
|
86
|
+
binding.bind();
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const disposalErrors = await disposeEndpointConnectionBindings(bindings);
|
|
90
|
+
|
|
91
|
+
if (disposalErrors.length === 0) {
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw new AggregateError(
|
|
96
|
+
[error, ...disposalErrors],
|
|
97
|
+
'Failed to bind endpoint connections.',
|
|
98
|
+
{cause: error},
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function collectBindingTopology(rootScopes: readonly Scope[]): {
|
|
104
|
+
readonly scopes: readonly BootstrapBindingScope[];
|
|
105
|
+
readonly endpointMap: ReadonlyMap<string, BindingTopologyEndpoint>;
|
|
106
|
+
} {
|
|
107
|
+
const endpointMap = new Map<string, BindingTopologyEndpoint>();
|
|
108
|
+
const scopePathSet = new Set<string>();
|
|
109
|
+
const scopes = rootScopes.map(scope =>
|
|
110
|
+
collectBindingScope(scope, endpointMap, scopePathSet),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return {scopes, endpointMap};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function collectBindingScope(
|
|
117
|
+
scope: Scope,
|
|
118
|
+
endpointMap: Map<string, BindingTopologyEndpoint>,
|
|
119
|
+
scopePathSet: Set<string>,
|
|
120
|
+
): BootstrapBindingScope {
|
|
121
|
+
const scopePathKey = getScopePathKey(scope);
|
|
122
|
+
|
|
123
|
+
if (scopePathSet.has(scopePathKey)) {
|
|
124
|
+
throw new Error(`Duplicate logical scope path: ${scopePathKey}.`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
scopePathSet.add(scopePathKey);
|
|
128
|
+
|
|
129
|
+
const devices = Array.from(scope.devices, deviceEntry => {
|
|
130
|
+
const deviceConstructors = Array.from(deviceEntry.constructors);
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
name: deviceEntry.name,
|
|
134
|
+
deviceConstructors,
|
|
135
|
+
endpoints: Array.from(deviceEntry.endpoints, endpoint => {
|
|
136
|
+
const path = getEndpointPath(scope, deviceEntry, endpoint);
|
|
137
|
+
const pathKey = getEndpointPathKey(path);
|
|
138
|
+
|
|
139
|
+
if (endpointMap.has(pathKey)) {
|
|
140
|
+
throw new Error(`Duplicate logical endpoint path: ${pathKey}.`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
endpointMap.set(pathKey, {endpoint, deviceConstructors});
|
|
144
|
+
|
|
145
|
+
return {path, endpoint};
|
|
146
|
+
}),
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
path: scope.path,
|
|
152
|
+
scopes: Array.from(scope.scopes, childScope =>
|
|
153
|
+
collectBindingScope(childScope, endpointMap, scopePathSet),
|
|
154
|
+
),
|
|
155
|
+
devices,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function createConnectionBindingPlans(
|
|
160
|
+
bindingFile: BindingFile,
|
|
161
|
+
endpointMap: ReadonlyMap<string, BindingTopologyEndpoint>,
|
|
162
|
+
): readonly EndpointConnectionBindingPlanEntry[] {
|
|
163
|
+
const bindingPathSet = new Set<string>();
|
|
164
|
+
const providerResourceKeySet = new Set<string>();
|
|
165
|
+
const plans: EndpointConnectionBindingPlanEntry[] = [];
|
|
166
|
+
|
|
167
|
+
for (const binding of bindingFile.bindings) {
|
|
168
|
+
const pathKey = getEndpointPathKey(binding.endpoint);
|
|
169
|
+
const target = endpointMap.get(pathKey);
|
|
170
|
+
|
|
171
|
+
// Keep stale bindings available to bootstrap frontends for inspection and
|
|
172
|
+
// cleanup, but do not let declarations removed from the current script
|
|
173
|
+
// prevent the remaining endpoints from starting.
|
|
174
|
+
if (target === undefined) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (bindingPathSet.has(pathKey)) {
|
|
179
|
+
throw new Error(`Duplicate endpoint binding: ${pathKey}.`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
bindingPathSet.add(pathKey);
|
|
183
|
+
|
|
184
|
+
const provider = getProvider(
|
|
185
|
+
binding.provider.namespace,
|
|
186
|
+
binding.provider.name,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
if (provider === undefined) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
`Binding references an unknown provider: ${binding.provider.namespace}/${binding.provider.name}.`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const plan = provider.createEndpointConnectionBindingPlan(
|
|
196
|
+
target.endpoint,
|
|
197
|
+
target.deviceConstructors,
|
|
198
|
+
binding.metadata,
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
for (const resourceKey of plan.resourceKeys) {
|
|
202
|
+
const providerResourceKey = JSON.stringify([
|
|
203
|
+
binding.provider.namespace,
|
|
204
|
+
binding.provider.name,
|
|
205
|
+
resourceKey,
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
if (providerResourceKeySet.has(providerResourceKey)) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`Duplicate provider resource binding: ${providerResourceKey}.`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
providerResourceKeySet.add(providerResourceKey);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
plans.push({path: binding.endpoint, endpoint: target.endpoint, plan});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return plans;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
type EndpointConnectionBindingPlanEntry = {
|
|
224
|
+
readonly path: EndpointPath;
|
|
225
|
+
readonly endpoint: EndpointReference;
|
|
226
|
+
readonly plan: EndpointConnectionBindingPlan;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
type BindingTopologyEndpoint = {
|
|
230
|
+
readonly endpoint: EndpointReference;
|
|
231
|
+
readonly deviceConstructors: readonly DeviceConstructor<Device>[];
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
function getScopePathKey(scope: Scope): string {
|
|
235
|
+
return JSON.stringify(scope.path);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function createBootstrapContext(
|
|
239
|
+
initialBindingFile: BindingFile,
|
|
240
|
+
bindingScopes: readonly BootstrapBindingScope[],
|
|
241
|
+
): {
|
|
242
|
+
readonly context: BootstrapContext;
|
|
243
|
+
readonly close: () => Promise<void>;
|
|
244
|
+
} {
|
|
245
|
+
let bindingFile = initialBindingFile;
|
|
246
|
+
let bindingFileUpdateQueue = Promise.resolve();
|
|
247
|
+
let active = true;
|
|
248
|
+
|
|
249
|
+
const context: BootstrapContext = {
|
|
250
|
+
providers: Array.from(getProviderEntries(), ([namespace, provider]) => ({
|
|
251
|
+
namespace,
|
|
252
|
+
provider,
|
|
253
|
+
})),
|
|
254
|
+
bindingScopes,
|
|
255
|
+
initialBindingFile,
|
|
256
|
+
updateBindingFile(createNextBindingFile) {
|
|
257
|
+
if (!active) {
|
|
258
|
+
return Promise.reject(new Error('Bootstrap context is closed.'));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const operation = bindingFileUpdateQueue.then(async () => {
|
|
262
|
+
const nextBindingFile = BindingFile.satisfies(
|
|
263
|
+
createNextBindingFile(bindingFile),
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
await writeBindingFile(nextBindingFile);
|
|
267
|
+
bindingFile = nextBindingFile;
|
|
268
|
+
return nextBindingFile;
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
bindingFileUpdateQueue = operation.then(
|
|
272
|
+
() => undefined,
|
|
273
|
+
() => undefined,
|
|
274
|
+
);
|
|
275
|
+
return operation;
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
context,
|
|
281
|
+
async close() {
|
|
282
|
+
active = false;
|
|
283
|
+
await bindingFileUpdateQueue;
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
}
|