@aivin-labs/cli 1.0.57 → 1.0.60

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.
@@ -1,154 +1,155 @@
1
- import { CastConsentMode } from '../localPolicy.mjs';
2
-
3
- /** Kết quả hỏi; khác `ALLOW` / `CANCELLED` thì gửi nguyên giá trị làm lý do từ chối cho người xem. */
4
- export const CastConsentDecision = Object.freeze({
5
- ALLOW: 'allow', ALLOW_ALWAYS: 'allow_always', DISABLED: 'cast_disabled', REJECTED: 'rejected_by_user', TIMEOUT: 'consent_timeout', UNAVAILABLE: 'consent_unavailable', CANCELLED: 'cancelled',
6
- });
7
-
8
- const MAX_REMEMBERED = 100;
9
- /**
10
- * Phiên dừng vì máy rớt mạng (không phải chủ máy hay người xem kết thúc): giữ đồng ý bấy nhiêu lâu. Máy chủ giữ phiên
11
- * chờ máy nối lại rồi mời phát lại đúng phiên đó — hỏi lại thì chủ máy đang điều khiển từ xa bị khoá ngoài (không ai ở máy để bấm).
12
- */
13
- export const CONSENT_RESUME_MS = 10 * 60_000;
14
- /** `viewerKey` do máy chủ tính (tài khoản + IP + trình duyệt); máy chủ đời cũ không gửi thì dùng tên thiết bị. */
15
- const key = (sessionId, viewerKey, viewerDevice) => `${sessionId}\n${viewerKey || viewerDevice || ''}`;
16
-
17
- /**
18
- * Chủ máy đồng ý từng phiên, quyết định ngay trên máy nên máy chủ không trả lời thay được. Đồng ý có hiệu lực cho đúng phiên + đúng người xem đó
19
- * (người xem rớt mạng nối lại không hỏi lại). Phiên kết thúc hẳn (`forget`) là quên; phiên chỉ gián đoạn (`suspend`: máy rớt mạng) thì giữ thêm
20
- * `CONSENT_RESUME_MS`. Chủ máy vừa bấm ngắt thì `isLocked` đúng: luôn hỏi, kể cả chế độ không cần người ở máy.
21
- *
22
- * `trust` (castTrust.mjs): thiết bị người ở máy đã bấm "Luôn cho phép" vào thẳng nếu `prove()` chứng minh đúng khoá của nó; sai thì hỏi như thường.
23
- * Thiết bị đã được cho vào phiên đang chạy cũng vào lại như vậy: `viewerKey` có cả IP, điện thoại đổi Wi-Fi / 4G giữa phiên không phải hỏi lại.
24
- * Chế độ `off` và trạng thái vừa bấm ngắt vẫn thắng quyền tin.
25
- */
26
- export function createCastConsent({ ask, getMode, isLocked = () => false, unlock = () => {}, onWaiting = () => {}, now = Date.now, trust = null }) {
27
- /** khoá → hạn (Infinity khi phiên đang chạy). */
28
- const approved = new Map();
29
- const pending = new Map();
30
- /** Khoá thiết bị đã được cho vào từng phiên — chủ máy bấm ngắt thì các thiết bị đó mất luôn quyền tin. */
31
- const sessionDevices = new Map();
32
- /** Người xem rời tạm trong lúc hộp đang hỏi: mã người xem → mã phiên. Hộp vẫn mở, bấm xong thì nhớ đồng ý nhưng chưa phát cho người đã đi. */
33
- const awayViewers = new Map();
34
-
35
- const approve = (k, sessionId, deviceKey) => {
36
- if (approved.size >= MAX_REMEMBERED) approved.clear();
37
- approved.set(k, Infinity);
38
- if (!deviceKey) return;
39
- if (!sessionDevices.has(sessionId)) sessionDevices.set(sessionId, new Set());
40
- sessionDevices.get(sessionId).add(deviceKey);
41
- };
42
-
43
- const valid = (k) => {
44
- const until = approved.get(k);
45
- if (until === undefined) return false;
46
- if (until >= now()) return true;
47
- approved.delete(k);
48
- return false;
49
- };
50
- const keysOf = (sessionId) => [...approved.keys()].filter((k) => k.startsWith(`${sessionId}\n`));
51
- /** Khoá thiết bị này đã được cho vào phiên và đồng ý của phiên còn hiệu lực (phiên gián đoạn quá `CONSENT_RESUME_MS` thì thôi). */
52
- const allowedInSession = (sessionId, deviceKey) => Boolean(sessionDevices.get(sessionId)?.has(deviceKey)) && keysOf(sessionId).some(valid);
53
-
54
- function request({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, locked }) {
55
- const k = key(sessionId, viewerKey, viewerDevice);
56
- const open = pending.get(k);
57
- if (open) {
58
- // Người xem quay lại khi hộp còn mở: báo lại để họ thấy "đang chờ" thay vì báo lỗi không nhận được hình.
59
- onWaiting({ sessionId, viewerId, viewerDevice });
60
- return open.promise;
61
- }
62
- const controller = new AbortController();
63
- onWaiting({ sessionId, viewerId, viewerDevice });
64
- const canTrust = Boolean(deviceKey && trust?.accepts(deviceKey));
65
- const promise = new Promise((resolve) => resolve(ask({ viewerDevice, locked, canTrust, signal: controller.signal })))
66
- .catch(() => CastConsentDecision.UNAVAILABLE)
67
- .then((answer) => {
68
- pending.delete(k);
69
- if (answer !== CastConsentDecision.ALLOW && answer !== CastConsentDecision.ALLOW_ALWAYS) return answer;
70
- approve(k, sessionId, deviceKey);
71
- if (locked) unlock();
72
- if (answer === CastConsentDecision.ALLOW_ALWAYS && canTrust) {
73
- try { trust.add({ key: deviceKey, name: viewerDevice }); } catch { /* không lưu được: vẫn cho phiên này */ }
74
- }
75
- return CastConsentDecision.ALLOW;
76
- });
77
- pending.set(k, { promise, controller, sessionId, viewerId });
78
- return promise;
79
- }
80
-
81
- /** Đồng ý tới khi người xem đang rời tạm thì chưa phát: họ quay lại là `valid` cho vào thẳng. */
82
- async function requestForViewer(options) {
83
- const answer = await request(options);
84
- return answer === CastConsentDecision.ALLOW && awayViewers.has(String(options.viewerId)) ? CastConsentDecision.CANCELLED : answer;
85
- }
86
-
87
- const clearAway = (sessionId) => {
88
- for (const [viewer, session] of awayViewers) if (session === sessionId) awayViewers.delete(viewer);
89
- };
90
-
91
- return {
92
- async decide({ sessionId, viewerId, viewerDevice = '', viewerKey = '', deviceKey = '', prove = null }) {
93
- awayViewers.delete(String(viewerId));
94
- const mode = getMode();
95
- if (mode === CastConsentMode.OFF) return CastConsentDecision.DISABLED;
96
- const locked = isLocked();
97
- const k = key(sessionId, viewerKey, viewerDevice);
98
- if (!locked && mode === CastConsentMode.UNATTENDED) return CastConsentDecision.ALLOW;
99
- if (!locked && valid(k)) {
100
- // Phiên gián đoạn được mời phát lại: đồng ý cũ có hiệu lực lại cho tới khi phiên kết thúc.
101
- approved.set(k, Infinity);
102
- return CastConsentDecision.ALLOW;
103
- }
104
- if (!locked && deviceKey && (trust?.has(deviceKey) || allowedInSession(sessionId, deviceKey))) {
105
- const proven = await Promise.resolve(prove?.()).catch(() => false);
106
- if (proven === null) return CastConsentDecision.CANCELLED;
107
- // Trong lúc chờ chữ ký chủ máy có thể vừa tắt hẳn hoặc bấm ngắt.
108
- if (getMode() === CastConsentMode.OFF) return CastConsentDecision.DISABLED;
109
- if (proven === true && !isLocked()) {
110
- approve(k, sessionId, deviceKey);
111
- try { trust?.touch(deviceKey); } catch { /* chỉ là gia hạn */ }
112
- return CastConsentDecision.ALLOW;
113
- }
114
- return requestForViewer({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, locked: isLocked() });
115
- }
116
- return requestForViewer({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, locked });
117
- },
118
- /**
119
- * Người xem rời / huỷ trong lúc đang hỏi: đóng hộp, không bắt đầu chụp. `away`: chỉ rời tạm (điện thoại tắt màn hình khi chủ máy quay sang
120
- * bấm trên máy) — giữ hộp, không thì hộp biến mất ngay lúc chủ máy định bấm.
121
- */
122
- cancel({ sessionId, viewerId, final = true, away = false }) {
123
- if (away && !final) {
124
- if ([...pending.values()].some((open) => open.sessionId === sessionId)) awayViewers.set(String(viewerId), sessionId);
125
- return;
126
- }
127
- if (final) clearAway(sessionId);
128
- for (const open of pending.values()) {
129
- if (open.sessionId === sessionId && (final || open.viewerId === viewerId)) open.controller.abort();
130
- }
131
- },
132
- /** Tạm dừng / tắt daemon / chủ máy bấm ngắt: đóng mọi hộp đang hỏi (bấm "Cho phép" muộn cũng không còn tác dụng). */
133
- cancelAll() {
134
- awayViewers.clear();
135
- for (const open of pending.values()) open.controller.abort();
136
- },
137
- /** Phiên kết thúc hẳn: quên đồng ý. */
138
- forget(sessionId) {
139
- for (const k of keysOf(sessionId)) approved.delete(k);
140
- sessionDevices.delete(sessionId);
141
- clearAway(sessionId);
142
- },
143
- /** Chủ máy bấm ngắt: mọi thiết bị đã vào phiên này mất quyền tin (phải gọi trước khi phiên bị quên). */
144
- revokeSessionTrust(sessionId) {
145
- for (const deviceKey of sessionDevices.get(sessionId) || []) {
146
- try { trust?.remove(deviceKey); } catch { /* đã khoá theo cả máy: phiên sau vẫn phải hỏi */ }
147
- }
148
- },
149
- /** Phiên chỉ gián đoạn (máy rớt mạng): giữ đồng ý thêm `ms` để phát lại không phải hỏi lại. */
150
- suspend(sessionId, ms = CONSENT_RESUME_MS) {
151
- for (const k of keysOf(sessionId)) approved.set(k, Math.min(approved.get(k), now() + ms));
152
- },
153
- };
154
- }
1
+ import { CastConsentMode } from '../localPolicy.mjs';
2
+
3
+ /** Kết quả hỏi; khác `ALLOW` / `CANCELLED` thì gửi nguyên giá trị làm lý do từ chối cho người xem. */
4
+ export const CastConsentDecision = Object.freeze({
5
+ ALLOW: 'allow', ALLOW_ALWAYS: 'allow_always', DISABLED: 'cast_disabled', REJECTED: 'rejected_by_user', TIMEOUT: 'consent_timeout', UNAVAILABLE: 'consent_unavailable', CANCELLED: 'cancelled',
6
+ });
7
+
8
+ const MAX_REMEMBERED = 100;
9
+ /**
10
+ * Phiên dừng vì máy rớt mạng (không phải chủ máy hay người xem kết thúc): giữ đồng ý bấy nhiêu lâu. Máy chủ giữ phiên
11
+ * chờ máy nối lại rồi mời phát lại đúng phiên đó — hỏi lại thì chủ máy đang điều khiển từ xa bị khoá ngoài (không ai ở máy để bấm).
12
+ */
13
+ export const CONSENT_RESUME_MS = 10 * 60_000;
14
+ /** `viewerKey` do máy chủ tính (tài khoản + IP + trình duyệt); máy chủ đời cũ không gửi thì dùng tên thiết bị. */
15
+ const key = (sessionId, viewerKey, viewerDevice) => `${sessionId}\n${viewerKey || viewerDevice || ''}`;
16
+
17
+ /**
18
+ * Chủ máy đồng ý từng phiên, quyết định ngay trên máy nên máy chủ không trả lời thay được. Đồng ý có hiệu lực cho đúng phiên + đúng người xem đó
19
+ * (người xem rớt mạng nối lại không hỏi lại). Phiên kết thúc hẳn (`forget`) là quên; phiên chỉ gián đoạn (`suspend`: máy rớt mạng) thì giữ thêm
20
+ * `CONSENT_RESUME_MS`. Chủ máy vừa bấm ngắt thì `isLocked` đúng: luôn hỏi, kể cả chế độ không cần người ở máy.
21
+ *
22
+ * `trust` (castTrust.mjs): thiết bị người ở máy đã bấm "Luôn cho phép" vào thẳng nếu `prove()` chứng minh đúng khoá của nó; sai thì hỏi như thường.
23
+ * Thiết bị đã được cho vào phiên đang chạy cũng vào lại như vậy: `viewerKey` có cả IP, điện thoại đổi Wi-Fi / 4G giữa phiên không phải hỏi lại.
24
+ * Chế độ `off` và trạng thái vừa bấm ngắt vẫn thắng quyền tin.
25
+ */
26
+ export function createCastConsent({ ask, getMode, isLocked = () => false, unlock = () => {}, onWaiting = () => {}, now = Date.now, trust = null }) {
27
+ /** khoá → hạn (Infinity khi phiên đang chạy). */
28
+ const approved = new Map();
29
+ const pending = new Map();
30
+ /** Khoá thiết bị đã được cho vào từng phiên — chủ máy bấm ngắt thì các thiết bị đó mất luôn quyền tin. */
31
+ const sessionDevices = new Map();
32
+ /** Người xem rời tạm trong lúc hộp đang hỏi: mã người xem → mã phiên. Hộp vẫn mở, bấm xong thì nhớ đồng ý nhưng chưa phát cho người đã đi. */
33
+ const awayViewers = new Map();
34
+
35
+ const approve = (k, sessionId, deviceKey) => {
36
+ if (approved.size >= MAX_REMEMBERED) approved.clear();
37
+ approved.set(k, Infinity);
38
+ if (!deviceKey) return;
39
+ if (!sessionDevices.has(sessionId)) sessionDevices.set(sessionId, new Set());
40
+ sessionDevices.get(sessionId).add(deviceKey);
41
+ };
42
+
43
+ const valid = (k) => {
44
+ const until = approved.get(k);
45
+ if (until === undefined) return false;
46
+ if (until >= now()) return true;
47
+ approved.delete(k);
48
+ return false;
49
+ };
50
+ const keysOf = (sessionId) => [...approved.keys()].filter((k) => k.startsWith(`${sessionId}\n`));
51
+ /** Khoá thiết bị này đã được cho vào phiên và đồng ý của phiên còn hiệu lực (phiên gián đoạn quá `CONSENT_RESUME_MS` thì thôi). */
52
+ const allowedInSession = (sessionId, deviceKey) => Boolean(sessionDevices.get(sessionId)?.has(deviceKey)) && keysOf(sessionId).some(valid);
53
+
54
+ function request({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, viewerInfo, locked }) {
55
+ const k = key(sessionId, viewerKey, viewerDevice);
56
+ const open = pending.get(k);
57
+ if (open) {
58
+ // Người xem quay lại khi hộp còn mở: báo lại để họ thấy "đang chờ" thay vì báo lỗi không nhận được hình.
59
+ onWaiting({ sessionId, viewerId, viewerDevice });
60
+ return open.promise;
61
+ }
62
+ const controller = new AbortController();
63
+ onWaiting({ sessionId, viewerId, viewerDevice });
64
+ const canTrust = Boolean(deviceKey && trust?.accepts(deviceKey));
65
+ const promise = new Promise((resolve) => resolve(ask({ viewerDevice, viewerInfo, locked, canTrust, signal: controller.signal })))
66
+ .catch(() => CastConsentDecision.UNAVAILABLE)
67
+ .then((answer) => {
68
+ pending.delete(k);
69
+ if (answer !== CastConsentDecision.ALLOW && answer !== CastConsentDecision.ALLOW_ALWAYS) return answer;
70
+ approve(k, sessionId, deviceKey);
71
+ if (locked) unlock();
72
+ if (answer === CastConsentDecision.ALLOW_ALWAYS && canTrust) {
73
+ try { trust.add({ key: deviceKey, name: viewerDevice, info: viewerInfo }); } catch { /* không lưu được: vẫn cho phiên này */ }
74
+ }
75
+ return CastConsentDecision.ALLOW;
76
+ });
77
+ pending.set(k, { promise, controller, sessionId, viewerId });
78
+ return promise;
79
+ }
80
+
81
+ /** Đồng ý tới khi người xem đang rời tạm thì chưa phát: họ quay lại là `valid` cho vào thẳng. */
82
+ async function requestForViewer(options) {
83
+ const answer = await request(options);
84
+ return answer === CastConsentDecision.ALLOW && awayViewers.has(String(options.viewerId)) ? CastConsentDecision.CANCELLED : answer;
85
+ }
86
+
87
+ const clearAway = (sessionId) => {
88
+ for (const [viewer, session] of awayViewers) if (session === sessionId) awayViewers.delete(viewer);
89
+ };
90
+
91
+ return {
92
+ /** `viewerInfo`: `{ place, ip, account }` chỉ để hiện cho chủ máy (hộp hỏi, danh sách thiết bị tin cậy). */
93
+ async decide({ sessionId, viewerId, viewerDevice = '', viewerKey = '', deviceKey = '', viewerInfo = null, prove = null }) {
94
+ awayViewers.delete(String(viewerId));
95
+ const mode = getMode();
96
+ if (mode === CastConsentMode.OFF) return CastConsentDecision.DISABLED;
97
+ const locked = isLocked();
98
+ const k = key(sessionId, viewerKey, viewerDevice);
99
+ if (!locked && mode === CastConsentMode.UNATTENDED) return CastConsentDecision.ALLOW;
100
+ if (!locked && valid(k)) {
101
+ // Phiên gián đoạn được mời phát lại: đồng ý cũ có hiệu lực lại cho tới khi phiên kết thúc.
102
+ approved.set(k, Infinity);
103
+ return CastConsentDecision.ALLOW;
104
+ }
105
+ if (!locked && deviceKey && (trust?.has(deviceKey) || allowedInSession(sessionId, deviceKey))) {
106
+ const proven = await Promise.resolve(prove?.()).catch(() => false);
107
+ if (proven === null) return CastConsentDecision.CANCELLED;
108
+ // Trong lúc chờ chữ ký chủ máy có thể vừa tắt hẳn hoặc bấm ngắt.
109
+ if (getMode() === CastConsentMode.OFF) return CastConsentDecision.DISABLED;
110
+ if (proven === true && !isLocked()) {
111
+ approve(k, sessionId, deviceKey);
112
+ try { trust?.touch(deviceKey, viewerInfo); } catch { /* chỉ là gia hạn */ }
113
+ return CastConsentDecision.ALLOW;
114
+ }
115
+ return requestForViewer({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, viewerInfo, locked: isLocked() });
116
+ }
117
+ return requestForViewer({ sessionId, viewerId, viewerDevice, viewerKey, deviceKey, viewerInfo, locked });
118
+ },
119
+ /**
120
+ * Người xem rời / huỷ trong lúc đang hỏi: đóng hộp, không bắt đầu chụp. `away`: chỉ rời tạm (điện thoại tắt màn hình khi chủ máy quay sang
121
+ * bấm trên máy) — giữ hộp, không thì hộp biến mất ngay lúc chủ máy định bấm.
122
+ */
123
+ cancel({ sessionId, viewerId, final = true, away = false }) {
124
+ if (away && !final) {
125
+ if ([...pending.values()].some((open) => open.sessionId === sessionId)) awayViewers.set(String(viewerId), sessionId);
126
+ return;
127
+ }
128
+ if (final) clearAway(sessionId);
129
+ for (const open of pending.values()) {
130
+ if (open.sessionId === sessionId && (final || open.viewerId === viewerId)) open.controller.abort();
131
+ }
132
+ },
133
+ /** Tạm dừng / tắt daemon / chủ máy bấm ngắt: đóng mọi hộp đang hỏi (bấm "Cho phép" muộn cũng không còn tác dụng). */
134
+ cancelAll() {
135
+ awayViewers.clear();
136
+ for (const open of pending.values()) open.controller.abort();
137
+ },
138
+ /** Phiên kết thúc hẳn: quên đồng ý. */
139
+ forget(sessionId) {
140
+ for (const k of keysOf(sessionId)) approved.delete(k);
141
+ sessionDevices.delete(sessionId);
142
+ clearAway(sessionId);
143
+ },
144
+ /** Chủ máy bấm ngắt: mọi thiết bị đã vào phiên này mất quyền tin (phải gọi trước khi phiên bị quên). */
145
+ revokeSessionTrust(sessionId) {
146
+ for (const deviceKey of sessionDevices.get(sessionId) || []) {
147
+ try { trust?.remove(deviceKey); } catch { /* đã khoá theo cả máy: phiên sau vẫn phải hỏi */ }
148
+ }
149
+ },
150
+ /** Phiên chỉ gián đoạn (máy rớt mạng): giữ đồng ý thêm `ms` để phát lại không phải hỏi lại. */
151
+ suspend(sessionId, ms = CONSENT_RESUME_MS) {
152
+ for (const k of keysOf(sessionId)) approved.set(k, Math.min(approved.get(k), now() + ms));
153
+ },
154
+ };
155
+ }