@homelib/xiaomi 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 +22 -0
- package/bld/library/backend/client.d.ts +68 -0
- package/bld/library/backend/client.js +477 -0
- package/bld/library/backend/client.js.map +1 -0
- package/bld/library/backend/constants.d.ts +7 -0
- package/bld/library/backend/constants.js +11 -0
- package/bld/library/backend/constants.js.map +1 -0
- package/bld/library/backend/index.d.ts +3 -0
- package/bld/library/backend/index.js +4 -0
- package/bld/library/backend/index.js.map +1 -0
- package/bld/library/backend/oauth.d.ts +24 -0
- package/bld/library/backend/oauth.js +121 -0
- package/bld/library/backend/oauth.js.map +1 -0
- package/bld/library/binding.d.ts +44 -0
- package/bld/library/binding.js +212 -0
- package/bld/library/binding.js.map +1 -0
- package/bld/library/cloud/client.d.ts +27 -0
- package/bld/library/cloud/client.js +109 -0
- package/bld/library/cloud/client.js.map +1 -0
- package/bld/library/cloud/constants.d.ts +9 -0
- package/bld/library/cloud/constants.js +11 -0
- package/bld/library/cloud/constants.js.map +1 -0
- package/bld/library/cloud/device.d.ts +84 -0
- package/bld/library/cloud/device.js +686 -0
- package/bld/library/cloud/device.js.map +1 -0
- package/bld/library/cloud/index.d.ts +5 -0
- package/bld/library/cloud/index.js +6 -0
- package/bld/library/cloud/index.js.map +1 -0
- package/bld/library/cloud/mqtt.d.ts +69 -0
- package/bld/library/cloud/mqtt.js +517 -0
- package/bld/library/cloud/mqtt.js.map +1 -0
- package/bld/library/cloud/transport.d.ts +8 -0
- package/bld/library/cloud/transport.js +30 -0
- package/bld/library/cloud/transport.js.map +1 -0
- package/bld/library/command.d.ts +6 -0
- package/bld/library/command.js +6 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/configuration.d.ts +62 -0
- package/bld/library/configuration.js +303 -0
- package/bld/library/configuration.js.map +1 -0
- package/bld/library/device.d.ts +68 -0
- package/bld/library/device.js +235 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +48 -0
- package/bld/library/devices/air-conditioner.js +155 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/command-effect.d.ts +35 -0
- package/bld/library/devices/command-effect.js +269 -0
- package/bld/library/devices/command-effect.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +41 -0
- package/bld/library/devices/dehumidifier.js +124 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +37 -0
- package/bld/library/devices/fan.js +135 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +7 -0
- package/bld/library/devices/index.js +8 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +22 -0
- package/bld/library/devices/light.js +112 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/devices/placeholder.d.ts +3 -0
- package/bld/library/devices/placeholder.js +4 -0
- package/bld/library/devices/placeholder.js.map +1 -0
- package/bld/library/devices/value-range.d.ts +2 -0
- package/bld/library/devices/value-range.js +6 -0
- package/bld/library/devices/value-range.js.map +1 -0
- package/bld/library/endpoint-connection.d.ts +154 -0
- package/bld/library/endpoint-connection.js +510 -0
- package/bld/library/endpoint-connection.js.map +1 -0
- package/bld/library/index.d.ts +26 -0
- package/bld/library/index.js +31 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/local/certificate.d.ts +31 -0
- package/bld/library/local/certificate.js +298 -0
- package/bld/library/local/certificate.js.map +1 -0
- package/bld/library/local/controller.d.ts +70 -0
- package/bld/library/local/controller.js +497 -0
- package/bld/library/local/controller.js.map +1 -0
- package/bld/library/local/discovery.d.ts +29 -0
- package/bld/library/local/discovery.js +321 -0
- package/bld/library/local/discovery.js.map +1 -0
- package/bld/library/local/index.d.ts +6 -0
- package/bld/library/local/index.js +7 -0
- package/bld/library/local/index.js.map +1 -0
- package/bld/library/local/message.d.ts +11 -0
- package/bld/library/local/message.js +128 -0
- package/bld/library/local/message.js.map +1 -0
- package/bld/library/local/mqtt.d.ts +113 -0
- package/bld/library/local/mqtt.js +1044 -0
- package/bld/library/local/mqtt.js.map +1 -0
- package/bld/library/local/routed-message-client.d.ts +49 -0
- package/bld/library/local/routed-message-client.js +326 -0
- package/bld/library/local/routed-message-client.js.map +1 -0
- package/bld/library/miot/index.d.ts +4 -0
- package/bld/library/miot/index.js +5 -0
- package/bld/library/miot/index.js.map +1 -0
- package/bld/library/miot/matcher.d.ts +65 -0
- package/bld/library/miot/matcher.js +217 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +16 -0
- package/bld/library/miot/request.js +27 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +4 -0
- package/bld/library/miot/result.js +5 -0
- package/bld/library/miot/result.js.map +1 -0
- package/bld/library/miot/spec.d.ts +74 -0
- package/bld/library/miot/spec.js +78 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +71 -0
- package/bld/library/provider.js +600 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/session-manager.d.ts +34 -0
- package/bld/library/session-manager.js +177 -0
- package/bld/library/session-manager.js.map +1 -0
- package/bld/library/session.d.ts +36 -0
- package/bld/library/session.js +370 -0
- package/bld/library/session.js.map +1 -0
- package/bld/library/storage.d.ts +2 -0
- package/bld/library/storage.js +66 -0
- package/bld/library/storage.js.map +1 -0
- package/bld/library/terminal/bindings.d.ts +3 -0
- package/bld/library/terminal/bindings.js +280 -0
- package/bld/library/terminal/bindings.js.map +1 -0
- package/bld/library/terminal/index.d.ts +2 -0
- package/bld/library/terminal/index.js +3 -0
- package/bld/library/terminal/index.js.map +1 -0
- package/bld/library/terminal/provider-details.d.ts +3 -0
- package/bld/library/terminal/provider-details.js +477 -0
- package/bld/library/terminal/provider-details.js.map +1 -0
- package/package.json +29 -0
- package/src/library/backend/client.ts +713 -0
- package/src/library/backend/constants.ts +17 -0
- package/src/library/backend/index.ts +3 -0
- package/src/library/backend/oauth.ts +170 -0
- package/src/library/binding.ts +387 -0
- package/src/library/cloud/client.ts +155 -0
- package/src/library/cloud/constants.ts +19 -0
- package/src/library/cloud/device.ts +938 -0
- package/src/library/cloud/index.ts +5 -0
- package/src/library/cloud/mqtt.ts +671 -0
- package/src/library/cloud/transport.ts +38 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +548 -0
- package/src/library/devices/air-conditioner.ts +171 -0
- package/src/library/devices/command-effect.ts +425 -0
- package/src/library/devices/dehumidifier.ts +131 -0
- package/src/library/devices/fan.ts +140 -0
- package/src/library/devices/index.ts +7 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +62 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +692 -0
- package/src/library/local/discovery.ts +485 -0
- package/src/library/local/index.ts +6 -0
- package/src/library/local/message.ts +174 -0
- package/src/library/local/mqtt.ts +1493 -0
- package/src/library/local/routed-message-client.ts +377 -0
- package/src/library/miot/index.ts +4 -0
- package/src/library/miot/matcher.ts +461 -0
- package/src/library/miot/request.ts +30 -0
- package/src/library/miot/result.ts +10 -0
- package/src/library/miot/spec.ts +103 -0
- package/src/library/provider.ts +745 -0
- package/src/library/session-manager.ts +224 -0
- package/src/library/session.ts +528 -0
- package/src/library/storage.ts +83 -0
- package/src/library/terminal/bindings.tsx +612 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import type {OAuthSession} from './session.js';
|
|
2
|
+
import {loadValidOAuthSession} from './session.js';
|
|
3
|
+
|
|
4
|
+
const MAX_REFRESH_MARGIN = 60_000;
|
|
5
|
+
const MIN_REFRESH_MARGIN = 1_000;
|
|
6
|
+
const INITIAL_RETRY_DELAY = 1_000;
|
|
7
|
+
const MAX_RETRY_DELAY = 60_000;
|
|
8
|
+
const MAX_TIMER_DELAY = 2_147_483_647;
|
|
9
|
+
|
|
10
|
+
export class OAuthSessionManager {
|
|
11
|
+
private readonly dependencies: OAuthSessionManagerDependencies;
|
|
12
|
+
|
|
13
|
+
private session: OAuthSession | undefined;
|
|
14
|
+
|
|
15
|
+
private loadOperation: OAuthSessionLoadOperation | undefined;
|
|
16
|
+
|
|
17
|
+
private readonly pendingLoadOperationSet =
|
|
18
|
+
new Set<OAuthSessionLoadOperation>();
|
|
19
|
+
|
|
20
|
+
private timer: unknown;
|
|
21
|
+
|
|
22
|
+
private generation = 0;
|
|
23
|
+
|
|
24
|
+
private retryDelay = INITIAL_RETRY_DELAY;
|
|
25
|
+
|
|
26
|
+
constructor(options: OAuthSessionManagerOptions) {
|
|
27
|
+
this.dependencies = {
|
|
28
|
+
loadSession: () => loadValidOAuthSession(options.sessionPath),
|
|
29
|
+
applyAccessToken: options.applyAccessToken,
|
|
30
|
+
now: Date.now,
|
|
31
|
+
setTimer: defaultSetTimer,
|
|
32
|
+
clearTimer: defaultClearTimer,
|
|
33
|
+
reportRefreshFailure: () => {
|
|
34
|
+
console.error('MIoT OAuth token refresh failed; retrying.');
|
|
35
|
+
},
|
|
36
|
+
...options.dependencies,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getSession(): Promise<OAuthSession> {
|
|
41
|
+
const session = this.session;
|
|
42
|
+
|
|
43
|
+
if (
|
|
44
|
+
session !== undefined &&
|
|
45
|
+
getExpirationTime(session) >
|
|
46
|
+
this.dependencies.now() + getRefreshMargin(session)
|
|
47
|
+
) {
|
|
48
|
+
return Promise.resolve(session);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return this.load();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
useSession(session: OAuthSession): void {
|
|
55
|
+
this.generation++;
|
|
56
|
+
this.loadOperation = undefined;
|
|
57
|
+
this.clearRefreshTimer();
|
|
58
|
+
this.acceptSession(session);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async reset(): Promise<void> {
|
|
62
|
+
this.generation++;
|
|
63
|
+
const pendingLoadOperations = [...this.pendingLoadOperationSet];
|
|
64
|
+
this.loadOperation = undefined;
|
|
65
|
+
this.clearRefreshTimer();
|
|
66
|
+
this.session = undefined;
|
|
67
|
+
this.retryDelay = INITIAL_RETRY_DELAY;
|
|
68
|
+
|
|
69
|
+
await Promise.all(
|
|
70
|
+
pendingLoadOperations.map(operation =>
|
|
71
|
+
operation.promise.catch(() => undefined),
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private load(): Promise<OAuthSession> {
|
|
77
|
+
const generation = this.generation;
|
|
78
|
+
const existingOperation = this.loadOperation;
|
|
79
|
+
|
|
80
|
+
if (
|
|
81
|
+
existingOperation !== undefined &&
|
|
82
|
+
existingOperation.generation === generation
|
|
83
|
+
) {
|
|
84
|
+
return existingOperation.promise;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const source = this.dependencies.loadSession();
|
|
88
|
+
const promise = source.then(
|
|
89
|
+
session => {
|
|
90
|
+
if (this.generation !== generation) {
|
|
91
|
+
throw new Error('OAuth session load was invalidated.');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.acceptSession(session);
|
|
95
|
+
return session;
|
|
96
|
+
},
|
|
97
|
+
error => {
|
|
98
|
+
if (this.generation === generation && this.session !== undefined) {
|
|
99
|
+
this.dependencies.reportRefreshFailure();
|
|
100
|
+
this.scheduleRetry(generation);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
throw error;
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
const operation = {generation, promise};
|
|
107
|
+
this.loadOperation = operation;
|
|
108
|
+
this.pendingLoadOperationSet.add(operation);
|
|
109
|
+
|
|
110
|
+
void promise.then(
|
|
111
|
+
() => {
|
|
112
|
+
this.completeLoad(operation);
|
|
113
|
+
},
|
|
114
|
+
() => {
|
|
115
|
+
this.completeLoad(operation);
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
return promise;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private completeLoad(operation: OAuthSessionLoadOperation): void {
|
|
122
|
+
this.pendingLoadOperationSet.delete(operation);
|
|
123
|
+
|
|
124
|
+
if (this.loadOperation === operation) {
|
|
125
|
+
this.loadOperation = undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private acceptSession(session: OAuthSession): void {
|
|
130
|
+
getExpirationTime(session);
|
|
131
|
+
this.session = session;
|
|
132
|
+
this.retryDelay = INITIAL_RETRY_DELAY;
|
|
133
|
+
this.dependencies.applyAccessToken(session.token.accessToken);
|
|
134
|
+
this.scheduleRefresh(session, this.generation);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private scheduleRefresh(session: OAuthSession, generation: number): void {
|
|
138
|
+
const delay = Math.min(
|
|
139
|
+
MAX_TIMER_DELAY,
|
|
140
|
+
Math.max(
|
|
141
|
+
0,
|
|
142
|
+
getExpirationTime(session) -
|
|
143
|
+
this.dependencies.now() -
|
|
144
|
+
getRefreshMargin(session),
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
this.setRefreshTimer(delay, generation);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private scheduleRetry(generation: number): void {
|
|
152
|
+
const delay = this.retryDelay;
|
|
153
|
+
this.retryDelay = Math.min(this.retryDelay * 2, MAX_RETRY_DELAY);
|
|
154
|
+
this.setRefreshTimer(delay, generation);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private setRefreshTimer(delay: number, generation: number): void {
|
|
158
|
+
this.clearRefreshTimer();
|
|
159
|
+
this.timer = this.dependencies.setTimer(() => {
|
|
160
|
+
this.timer = undefined;
|
|
161
|
+
|
|
162
|
+
if (this.generation === generation) {
|
|
163
|
+
void this.load().catch(() => undefined);
|
|
164
|
+
}
|
|
165
|
+
}, delay);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private clearRefreshTimer(): void {
|
|
169
|
+
if (this.timer === undefined) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
this.dependencies.clearTimer(this.timer);
|
|
174
|
+
this.timer = undefined;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type OAuthSessionManagerOptions = {
|
|
179
|
+
readonly sessionPath: string;
|
|
180
|
+
readonly applyAccessToken: (accessToken: string) => void;
|
|
181
|
+
readonly dependencies?: Partial<OAuthSessionManagerDependencies>;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export type OAuthSessionManagerDependencies = {
|
|
185
|
+
readonly loadSession: () => Promise<OAuthSession>;
|
|
186
|
+
readonly applyAccessToken: (accessToken: string) => void;
|
|
187
|
+
readonly now: () => number;
|
|
188
|
+
readonly setTimer: (callback: () => void, delay: number) => unknown;
|
|
189
|
+
readonly clearTimer: (timer: unknown) => void;
|
|
190
|
+
readonly reportRefreshFailure: () => void;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
type OAuthSessionLoadOperation = {
|
|
194
|
+
readonly generation: number;
|
|
195
|
+
readonly promise: Promise<OAuthSession>;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
function getExpirationTime(session: OAuthSession): number {
|
|
199
|
+
const expirationTime = Date.parse(session.expiresAt);
|
|
200
|
+
|
|
201
|
+
if (Number.isNaN(expirationTime)) {
|
|
202
|
+
throw new Error('OAuth session has an invalid expiration time.');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return expirationTime;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function getRefreshMargin(session: OAuthSession): number {
|
|
209
|
+
return Math.min(
|
|
210
|
+
MAX_REFRESH_MARGIN,
|
|
211
|
+
Math.max(MIN_REFRESH_MARGIN, (session.token.expiresIn * 1_000) / 2),
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function defaultSetTimer(callback: () => void, delay: number): unknown {
|
|
216
|
+
const timer = setTimeout(callback, delay);
|
|
217
|
+
|
|
218
|
+
timer.unref();
|
|
219
|
+
return timer;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function defaultClearTimer(timer: unknown): void {
|
|
223
|
+
clearTimeout(timer as ReturnType<typeof setTimeout>);
|
|
224
|
+
}
|