chuvsu-js 5.4.1 → 5.5.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 +32 -3
- package/dist/common/http.d.ts +3 -1
- package/dist/common/http.js +9 -2
- package/dist/{tt/utils → common}/lesson-type.d.ts +4 -2
- package/dist/{tt/utils → common}/lesson-type.js +17 -2
- package/dist/core.d.ts +13 -5
- package/dist/core.js +7 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lk/client.d.ts +23 -1
- package/dist/lk/client.js +95 -0
- package/dist/lk/parse/grade-value.d.ts +4 -0
- package/dist/lk/parse/grade-value.js +16 -0
- package/dist/lk/parse/portfolio-content.d.ts +10 -0
- package/dist/lk/parse/portfolio-content.js +116 -0
- package/dist/lk/parse/portfolio-grades.d.ts +8 -0
- package/dist/lk/parse/portfolio-grades.js +41 -0
- package/dist/lk/parse/portfolio-performance.d.ts +2 -0
- package/dist/lk/parse/portfolio-performance.js +44 -0
- package/dist/lk/parse/portfolio.d.ts +6 -0
- package/dist/lk/parse/portfolio.js +64 -0
- package/dist/lk/parse/webinar-lesson.d.ts +7 -0
- package/dist/lk/parse/webinar-lesson.js +28 -0
- package/dist/lk/parse/webinars.d.ts +3 -0
- package/dist/lk/parse/webinars.js +67 -0
- package/dist/lk/portfolio-types.d.ts +113 -0
- package/dist/lk/portfolio-types.js +1 -0
- package/dist/lk/types.d.ts +29 -0
- package/dist/{tt → lk}/webinars.d.ts +3 -1
- package/dist/lk/webinars.js +52 -0
- package/dist/parsers.d.ts +3 -1
- package/dist/parsers.js +3 -1
- package/dist/tt/client.d.ts +1 -11
- package/dist/tt/client.js +1 -45
- package/dist/tt/domain/types.d.ts +1 -1
- package/dist/tt/observations.js +2 -1
- package/dist/tt/parse/index.d.ts +0 -1
- package/dist/tt/parse/index.js +0 -1
- package/dist/tt/types.d.ts +0 -21
- package/dist/tt/utils/index.d.ts +0 -1
- package/dist/tt/utils/index.js +0 -1
- package/dist/webinar/client.d.ts +52 -0
- package/dist/webinar/client.js +184 -0
- package/dist/webinar/ddp.d.ts +31 -0
- package/dist/webinar/ddp.js +208 -0
- package/dist/webinar/events.d.ts +6 -0
- package/dist/webinar/events.js +38 -0
- package/dist/webinar/gateway.d.ts +30 -0
- package/dist/webinar/gateway.js +114 -0
- package/dist/webinar/media-protocol.d.ts +9 -0
- package/dist/webinar/media-protocol.js +24 -0
- package/dist/webinar/media-types.d.ts +10 -0
- package/dist/webinar/media-types.js +1 -0
- package/dist/webinar/media.d.ts +21 -0
- package/dist/webinar/media.js +76 -0
- package/dist/webinar/parse.d.ts +14 -0
- package/dist/webinar/parse.js +113 -0
- package/dist/webinar/playback.d.ts +32 -0
- package/dist/webinar/playback.js +188 -0
- package/dist/webinar/socket.d.ts +3 -0
- package/dist/webinar/socket.js +18 -0
- package/dist/webinar/state.d.ts +4 -0
- package/dist/webinar/state.js +98 -0
- package/dist/webinar/types.d.ts +148 -0
- package/dist/webinar/types.js +12 -0
- package/docs/student-portal.md +151 -0
- package/docs/testing.md +13 -0
- package/docs/v5-migration.md +25 -2
- package/docs/webinar.md +145 -0
- package/package.json +2 -2
- package/dist/tt/parse/webinars.d.ts +0 -2
- package/dist/tt/parse/webinars.js +0 -97
- package/dist/tt/webinars.js +0 -39
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { AuthError } from "../common/types.js";
|
|
2
|
+
import { WebinarDdp } from "./ddp.js";
|
|
3
|
+
import { webinarEvents } from "./events.js";
|
|
4
|
+
import { record, string } from "./parse.js";
|
|
5
|
+
import { browserWebinarSocket } from "./socket.js";
|
|
6
|
+
import { buildWebinarSnapshot, normalizeWebinarMessage } from "./state.js";
|
|
7
|
+
import { WebinarPermissionError, WebinarProtocolError, } from "./types.js";
|
|
8
|
+
/** Live participant state and explicit actions; never auto-connects media. */
|
|
9
|
+
export class WebinarClient {
|
|
10
|
+
ddp;
|
|
11
|
+
session;
|
|
12
|
+
history = new Map();
|
|
13
|
+
listeners = new Set();
|
|
14
|
+
ready = false;
|
|
15
|
+
started = false;
|
|
16
|
+
constructor(session, opts = {}) {
|
|
17
|
+
if (session.protocol !== "ddp" || !/^2\.3(?:\.|$)/u.test(session.server.version)) {
|
|
18
|
+
throw new WebinarProtocolError("Unsupported webinar participant protocol");
|
|
19
|
+
}
|
|
20
|
+
this.session = structuredClone(session);
|
|
21
|
+
this.ddp = new WebinarDdp(opts.socketFactory ?? browserWebinarSocket, opts.timeout);
|
|
22
|
+
this.ddp.onChange((collection, id, removed) => {
|
|
23
|
+
this.emit({ type: "change", collection });
|
|
24
|
+
if (collection === "group-chat-msg" && !removed) {
|
|
25
|
+
const doc = this.ddp.documents(collection).find((message) => message._id === id);
|
|
26
|
+
if (doc)
|
|
27
|
+
this.emit({ type: "message", message: normalizeWebinarMessage(doc, [...this.ddp.documents("users"), ...this.ddp.documents("users-persistent-data")]) });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
this.ddp.onDisconnect(() => { this.ready = false; this.emit({ type: "disconnected" }); });
|
|
31
|
+
this.ddp.onError((error) => this.emit({ type: "error", error }));
|
|
32
|
+
}
|
|
33
|
+
async connect() {
|
|
34
|
+
if (this.started)
|
|
35
|
+
throw new WebinarProtocolError("Create a new client to reconnect");
|
|
36
|
+
this.started = true;
|
|
37
|
+
try {
|
|
38
|
+
await this.ddp.connect(this.session.connections.state);
|
|
39
|
+
await this.ddp.call("validateAuthToken", [this.session.meeting.id, this.session.user.id,
|
|
40
|
+
this.session.authToken, this.session.externalUserId]);
|
|
41
|
+
await this.ddp.subscribe("auth-token-validation", [{ meetingId: this.session.meeting.id, userId: this.session.user.id }]);
|
|
42
|
+
const validation = await this.ddp.waitFor("auth-token-validation", (doc) => doc.userId === this.session.user.id && (doc.validationStatus === 3 || doc.validationStatus === 4));
|
|
43
|
+
if (validation.validationStatus !== 3)
|
|
44
|
+
throw new AuthError("Webinar participant authentication rejected");
|
|
45
|
+
await Promise.all([
|
|
46
|
+
"current-user", "users", "users-persistent-data", "meetings", "voiceUsers",
|
|
47
|
+
"presentations", "slides", "slide-positions", "presentation-pods", "video-streams", "screenshare", "note", "group-chat",
|
|
48
|
+
].map((name) => this.ddp.subscribe(name)));
|
|
49
|
+
const chatIds = this.ddp.documents("group-chat").map((chat) => string(chat.chatId));
|
|
50
|
+
await this.ddp.subscribe("group-chat-msg", [chatIds]);
|
|
51
|
+
this.ready = true;
|
|
52
|
+
if (this.session.features.chat)
|
|
53
|
+
await this.loadMessages();
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
this.close();
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
assertConnected() {
|
|
61
|
+
if (!this.ready)
|
|
62
|
+
throw new WebinarProtocolError("Webinar is disconnected");
|
|
63
|
+
}
|
|
64
|
+
getSnapshot() {
|
|
65
|
+
this.assertConnected();
|
|
66
|
+
return buildWebinarSnapshot(this.ddp, this.session, this.history);
|
|
67
|
+
}
|
|
68
|
+
getUsers() { return this.getSnapshot().users; }
|
|
69
|
+
getPresenter() { return this.getUsers().find((user) => user.presenter) ?? null; }
|
|
70
|
+
getModerators() { return this.getUsers().filter((user) => user.moderator); }
|
|
71
|
+
getChats() { return this.getSnapshot().chats; }
|
|
72
|
+
getMessages(opts) {
|
|
73
|
+
const chatId = opts?.chatId ?? this.session.publicChatId;
|
|
74
|
+
return this.getSnapshot().messages.filter((message) => message.chatId === chatId);
|
|
75
|
+
}
|
|
76
|
+
getPresentations() { return this.getSnapshot().presentations; }
|
|
77
|
+
getCurrentPresentation() { return this.getPresentations().find((presentation) => presentation.current) ?? null; }
|
|
78
|
+
getMediaStreams() { return this.getSnapshot().streams; }
|
|
79
|
+
getNotes() { return this.getSnapshot().notes; }
|
|
80
|
+
/** Reads historical pages; live messages arrive through events(). */
|
|
81
|
+
async loadMessages(opts = {}) {
|
|
82
|
+
this.assertConnected();
|
|
83
|
+
const chatId = opts.chatId ?? this.session.publicChatId;
|
|
84
|
+
if (!this.getChats().some((chat) => chat.id === chatId))
|
|
85
|
+
throw new WebinarPermissionError("Chat is not accessible");
|
|
86
|
+
const page = opts.page ?? 1;
|
|
87
|
+
if (!Number.isInteger(page) || page < 1)
|
|
88
|
+
throw new RangeError("Chat page must be a positive integer");
|
|
89
|
+
const result = await this.ddp.call("fetchMessagePerPage", [chatId, page]);
|
|
90
|
+
if (!Array.isArray(result))
|
|
91
|
+
throw new WebinarProtocolError("Invalid webinar chat history");
|
|
92
|
+
const docs = result.map(record);
|
|
93
|
+
for (const doc of docs)
|
|
94
|
+
if (doc.chatId === chatId)
|
|
95
|
+
this.history.set(string(doc.id) || string(doc._id), doc);
|
|
96
|
+
this.emit({ type: "change", collection: "group-chat-msg" });
|
|
97
|
+
return docs.filter((doc) => doc.chatId === chatId).map((doc) => normalizeWebinarMessage(doc, [...this.ddp.documents("users"), ...this.ddp.documents("users-persistent-data")]));
|
|
98
|
+
}
|
|
99
|
+
subscribe(listener) {
|
|
100
|
+
this.listeners.add(listener);
|
|
101
|
+
return () => { this.listeners.delete(listener); };
|
|
102
|
+
}
|
|
103
|
+
events(opts) {
|
|
104
|
+
return webinarEvents((listener) => this.subscribe(listener), opts);
|
|
105
|
+
}
|
|
106
|
+
emit(event) {
|
|
107
|
+
for (const listener of this.listeners) {
|
|
108
|
+
// A UI listener must not corrupt or disconnect the network transport.
|
|
109
|
+
try {
|
|
110
|
+
listener(event);
|
|
111
|
+
}
|
|
112
|
+
catch { /* Listener errors belong to the caller. */ }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
assertPermission(feature, lock) {
|
|
116
|
+
this.assertConnected();
|
|
117
|
+
if (!this.session.features[feature])
|
|
118
|
+
throw new WebinarPermissionError(`Webinar feature disabled: ${feature}`);
|
|
119
|
+
const user = this.getUsers().find((user) => user.id === this.session.user.id);
|
|
120
|
+
const meeting = this.ddp.documents("meetings")[0];
|
|
121
|
+
if (lock && user?.locked && !user.moderator && record(meeting?.lockSettingsProps)[lock] === true) {
|
|
122
|
+
throw new WebinarPermissionError(`Webinar permission locked: ${feature}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async setRaisedHand(raised) {
|
|
126
|
+
if (typeof raised !== "boolean")
|
|
127
|
+
throw new TypeError("Raised hand must be boolean");
|
|
128
|
+
this.assertPermission("raiseHand");
|
|
129
|
+
await this.ddp.call("setEmojiStatus", [this.session.user.id, raised ? "raiseHand" : "none"]);
|
|
130
|
+
}
|
|
131
|
+
async sendMessage(opts) {
|
|
132
|
+
const chatId = opts.chatId ?? this.session.publicChatId;
|
|
133
|
+
const chat = this.getChats().find((chat) => chat.id === chatId);
|
|
134
|
+
if (!chat)
|
|
135
|
+
throw new WebinarPermissionError("Chat is not accessible");
|
|
136
|
+
this.assertPermission("chat", chat.private ? "disablePrivateChat" : "disablePublicChat");
|
|
137
|
+
const text = opts.text.trim();
|
|
138
|
+
if (!text || text.length > this.session.maxMessageLength)
|
|
139
|
+
throw new RangeError("Invalid webinar message length");
|
|
140
|
+
await this.ddp.call("sendGroupChatMsg", [chatId, {
|
|
141
|
+
color: string(this.ddp.documents("users").find((user) => user.userId === this.session.user.id)?.color) || "0",
|
|
142
|
+
correlationId: `${this.session.user.id}-${Date.now()}`,
|
|
143
|
+
sender: { id: this.session.user.id, name: this.session.user.name }, message: text,
|
|
144
|
+
}]);
|
|
145
|
+
}
|
|
146
|
+
async openPrivateChat(opts) {
|
|
147
|
+
this.assertPermission("chat", "disablePrivateChat");
|
|
148
|
+
if (opts.userId === this.session.user.id)
|
|
149
|
+
throw new RangeError("Invalid private chat recipient");
|
|
150
|
+
const existing = this.getChats().find((chat) => chat.private && chat.userIds.includes(opts.userId));
|
|
151
|
+
if (existing)
|
|
152
|
+
return existing;
|
|
153
|
+
const user = this.getUsers().find((user) => user.id === opts.userId);
|
|
154
|
+
if (!user)
|
|
155
|
+
throw new RangeError("Invalid private chat recipient");
|
|
156
|
+
await this.ddp.call("createGroupChat", [{ userId: user.id, name: user.name }]);
|
|
157
|
+
await this.ddp.waitFor("group-chat", (chat) => chat.access === "PRIVATE_ACCESS"
|
|
158
|
+
&& Array.isArray(chat.users) && chat.users.includes(user.id) && chat.users.includes(this.session.user.id));
|
|
159
|
+
const chat = this.getChats().find((chat) => chat.private && chat.userIds.includes(user.id));
|
|
160
|
+
await this.ddp.subscribe("group-chat-msg", [this.getChats().map((chat) => chat.id)]);
|
|
161
|
+
return chat;
|
|
162
|
+
}
|
|
163
|
+
async sendPrivateMessage(opts) {
|
|
164
|
+
if (!opts.text.trim() || opts.text.trim().length > this.session.maxMessageLength)
|
|
165
|
+
throw new RangeError("Invalid webinar message length");
|
|
166
|
+
const chat = await this.openPrivateChat(opts);
|
|
167
|
+
await this.sendMessage({ chatId: chat.id, text: opts.text });
|
|
168
|
+
}
|
|
169
|
+
async setMuted(muted) {
|
|
170
|
+
if (typeof muted !== "boolean")
|
|
171
|
+
throw new TypeError("Muted state must be boolean");
|
|
172
|
+
this.assertConnected();
|
|
173
|
+
const user = this.getUsers().find((user) => user.id === this.session.user.id);
|
|
174
|
+
if (!user?.audio.joined || user.audio.listenOnly)
|
|
175
|
+
throw new WebinarPermissionError("Microphone audio is not connected");
|
|
176
|
+
await this.ddp.call("muteToggle", [this.session.user.id, muted]);
|
|
177
|
+
}
|
|
178
|
+
/** Closes local transport; does not issue userLeftMeeting or alter other users. */
|
|
179
|
+
close() {
|
|
180
|
+
this.ready = false;
|
|
181
|
+
this.ddp.close();
|
|
182
|
+
this.history.clear();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type WebinarSocketFactory } from "./types.js";
|
|
2
|
+
export type WebinarDocument = Record<string, unknown>;
|
|
3
|
+
/** DDP transport: no Meteor runtime and no profile/settings writes. */
|
|
4
|
+
export declare class WebinarDdp {
|
|
5
|
+
private factory;
|
|
6
|
+
private timeout;
|
|
7
|
+
private socket;
|
|
8
|
+
private sequence;
|
|
9
|
+
private pending;
|
|
10
|
+
private collections;
|
|
11
|
+
private listeners;
|
|
12
|
+
private disconnectListeners;
|
|
13
|
+
private errorListeners;
|
|
14
|
+
private subscriptions;
|
|
15
|
+
private connected;
|
|
16
|
+
constructor(factory: WebinarSocketFactory, timeout?: number);
|
|
17
|
+
onChange(listener: (collection: string, id: string, removed: boolean) => void): () => void;
|
|
18
|
+
onDisconnect(listener: () => void): () => void;
|
|
19
|
+
onError(listener: (error: Error) => void): () => void;
|
|
20
|
+
documents(collection: string): WebinarDocument[];
|
|
21
|
+
connect(url: string): Promise<void>;
|
|
22
|
+
private wait;
|
|
23
|
+
private settle;
|
|
24
|
+
private send;
|
|
25
|
+
call(method: string, params?: unknown[]): Promise<unknown>;
|
|
26
|
+
subscribe(name: string, params?: unknown[]): Promise<void>;
|
|
27
|
+
waitFor(collection: string, predicate: (doc: WebinarDocument) => boolean): Promise<WebinarDocument>;
|
|
28
|
+
private receive;
|
|
29
|
+
private disconnected;
|
|
30
|
+
close(): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { openWebinarSocket } from "./socket.js";
|
|
2
|
+
import { WebinarProtocolError } from "./types.js";
|
|
3
|
+
/** DDP transport: no Meteor runtime and no profile/settings writes. */
|
|
4
|
+
export class WebinarDdp {
|
|
5
|
+
factory;
|
|
6
|
+
timeout;
|
|
7
|
+
socket = null;
|
|
8
|
+
sequence = 0;
|
|
9
|
+
pending = new Map();
|
|
10
|
+
collections = new Map();
|
|
11
|
+
listeners = new Set();
|
|
12
|
+
disconnectListeners = new Set();
|
|
13
|
+
errorListeners = new Set();
|
|
14
|
+
subscriptions = new Map();
|
|
15
|
+
connected = false;
|
|
16
|
+
constructor(factory, timeout = 15_000) {
|
|
17
|
+
this.factory = factory;
|
|
18
|
+
this.timeout = timeout;
|
|
19
|
+
if (!Number.isFinite(timeout) || timeout <= 0)
|
|
20
|
+
throw new RangeError("Invalid webinar timeout");
|
|
21
|
+
}
|
|
22
|
+
onChange(listener) {
|
|
23
|
+
this.listeners.add(listener);
|
|
24
|
+
return () => { this.listeners.delete(listener); };
|
|
25
|
+
}
|
|
26
|
+
onDisconnect(listener) {
|
|
27
|
+
this.disconnectListeners.add(listener);
|
|
28
|
+
return () => { this.disconnectListeners.delete(listener); };
|
|
29
|
+
}
|
|
30
|
+
onError(listener) {
|
|
31
|
+
this.errorListeners.add(listener);
|
|
32
|
+
return () => { this.errorListeners.delete(listener); };
|
|
33
|
+
}
|
|
34
|
+
documents(collection) {
|
|
35
|
+
return structuredClone([...this.collections.get(collection)?.values() ?? []]);
|
|
36
|
+
}
|
|
37
|
+
async connect(url) {
|
|
38
|
+
if (this.socket)
|
|
39
|
+
throw new WebinarProtocolError("Webinar transport already opened");
|
|
40
|
+
const socket = this.factory(url);
|
|
41
|
+
this.socket = socket;
|
|
42
|
+
socket.addEventListener("message", (event) => {
|
|
43
|
+
try {
|
|
44
|
+
if (typeof event.data !== "string")
|
|
45
|
+
throw new WebinarProtocolError("Invalid DDP frame");
|
|
46
|
+
this.receive(JSON.parse(event.data));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
const error = new WebinarProtocolError("Invalid webinar DDP response");
|
|
50
|
+
for (const listener of this.errorListeners)
|
|
51
|
+
listener(error);
|
|
52
|
+
this.close();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
socket.addEventListener("close", () => this.disconnected());
|
|
56
|
+
socket.addEventListener("error", () => this.close());
|
|
57
|
+
await openWebinarSocket(socket, this.timeout);
|
|
58
|
+
const ready = this.wait("connect");
|
|
59
|
+
this.send({ msg: "connect", version: "1", support: ["1"] });
|
|
60
|
+
await ready;
|
|
61
|
+
this.connected = true;
|
|
62
|
+
}
|
|
63
|
+
wait(id) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
this.pending.delete(id);
|
|
67
|
+
reject(new WebinarProtocolError("Webinar request timed out"));
|
|
68
|
+
}, this.timeout);
|
|
69
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
settle(id, value, error) {
|
|
73
|
+
const request = this.pending.get(id);
|
|
74
|
+
if (!request)
|
|
75
|
+
return;
|
|
76
|
+
clearTimeout(request.timer);
|
|
77
|
+
this.pending.delete(id);
|
|
78
|
+
if (error)
|
|
79
|
+
request.reject(error);
|
|
80
|
+
else
|
|
81
|
+
request.resolve(value);
|
|
82
|
+
}
|
|
83
|
+
send(message) {
|
|
84
|
+
if (this.socket?.readyState !== 1)
|
|
85
|
+
throw new WebinarProtocolError("Webinar is disconnected");
|
|
86
|
+
this.socket.send(JSON.stringify(message));
|
|
87
|
+
}
|
|
88
|
+
async call(method, params = []) {
|
|
89
|
+
if (!this.connected)
|
|
90
|
+
throw new WebinarProtocolError("Webinar is disconnected");
|
|
91
|
+
const id = `method:${++this.sequence}`;
|
|
92
|
+
const result = this.wait(id);
|
|
93
|
+
this.send({ msg: "method", id, method, params });
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
async subscribe(name, params = []) {
|
|
97
|
+
if (!this.connected)
|
|
98
|
+
throw new WebinarProtocolError("Webinar is disconnected");
|
|
99
|
+
const key = JSON.stringify([name, params]);
|
|
100
|
+
const existing = this.subscriptions.get(key);
|
|
101
|
+
if (existing)
|
|
102
|
+
return existing;
|
|
103
|
+
const id = `sub:${++this.sequence}`;
|
|
104
|
+
const ready = this.wait(id);
|
|
105
|
+
const subscription = ready.then(() => undefined).catch((error) => {
|
|
106
|
+
this.subscriptions.delete(key);
|
|
107
|
+
throw error;
|
|
108
|
+
});
|
|
109
|
+
this.subscriptions.set(key, subscription);
|
|
110
|
+
this.send({ msg: "sub", id, name, params });
|
|
111
|
+
return subscription;
|
|
112
|
+
}
|
|
113
|
+
async waitFor(collection, predicate) {
|
|
114
|
+
const existing = this.documents(collection).find(predicate);
|
|
115
|
+
if (existing)
|
|
116
|
+
return existing;
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
const timer = setTimeout(() => {
|
|
119
|
+
unsubscribe();
|
|
120
|
+
disconnected();
|
|
121
|
+
reject(new WebinarProtocolError("Webinar state timed out"));
|
|
122
|
+
}, this.timeout);
|
|
123
|
+
const finish = () => { clearTimeout(timer); unsubscribe(); disconnected(); };
|
|
124
|
+
const unsubscribe = this.onChange((changed) => {
|
|
125
|
+
if (changed !== collection)
|
|
126
|
+
return;
|
|
127
|
+
const doc = this.documents(collection).find(predicate);
|
|
128
|
+
if (doc) {
|
|
129
|
+
finish();
|
|
130
|
+
resolve(doc);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
const disconnected = this.onDisconnect(() => {
|
|
134
|
+
finish();
|
|
135
|
+
reject(new WebinarProtocolError("Webinar is disconnected"));
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
receive(data) {
|
|
140
|
+
if (!data || typeof data !== "object")
|
|
141
|
+
throw new WebinarProtocolError("Invalid DDP message");
|
|
142
|
+
switch (data.msg) {
|
|
143
|
+
case "connected":
|
|
144
|
+
this.settle("connect", undefined);
|
|
145
|
+
break;
|
|
146
|
+
case "failed":
|
|
147
|
+
this.settle("connect", undefined, new WebinarProtocolError("DDP version rejected"));
|
|
148
|
+
break;
|
|
149
|
+
case "ping":
|
|
150
|
+
this.send({ msg: "pong", ...(data.id === undefined ? {} : { id: data.id }) });
|
|
151
|
+
break;
|
|
152
|
+
case "ready":
|
|
153
|
+
if (Array.isArray(data.subs))
|
|
154
|
+
for (const id of data.subs)
|
|
155
|
+
this.settle(String(id), undefined);
|
|
156
|
+
break;
|
|
157
|
+
case "nosub":
|
|
158
|
+
case "result":
|
|
159
|
+
this.settle(String(data.id), data.result, data.error
|
|
160
|
+
? new WebinarProtocolError(`Webinar ${data.msg === "nosub" ? "subscription" : "method"} rejected`)
|
|
161
|
+
: undefined);
|
|
162
|
+
break;
|
|
163
|
+
case "added":
|
|
164
|
+
case "changed":
|
|
165
|
+
case "removed": {
|
|
166
|
+
if (typeof data.collection !== "string" || typeof data.id !== "string")
|
|
167
|
+
return;
|
|
168
|
+
let collection = this.collections.get(data.collection);
|
|
169
|
+
if (!collection) {
|
|
170
|
+
collection = new Map();
|
|
171
|
+
this.collections.set(data.collection, collection);
|
|
172
|
+
}
|
|
173
|
+
if (data.msg === "removed")
|
|
174
|
+
collection.delete(data.id);
|
|
175
|
+
else {
|
|
176
|
+
const fields = data.fields !== null && typeof data.fields === "object" && !Array.isArray(data.fields)
|
|
177
|
+
? data.fields : {};
|
|
178
|
+
const doc = { ...(data.msg === "added" ? {} : collection.get(data.id)), ...fields, _id: data.id };
|
|
179
|
+
if (Array.isArray(data.cleared))
|
|
180
|
+
for (const field of data.cleared)
|
|
181
|
+
if (field !== "_id")
|
|
182
|
+
delete doc[String(field)];
|
|
183
|
+
collection.set(data.id, doc);
|
|
184
|
+
}
|
|
185
|
+
for (const listener of this.listeners)
|
|
186
|
+
listener(data.collection, data.id, data.msg === "removed");
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
disconnected() {
|
|
192
|
+
if (!this.socket)
|
|
193
|
+
return;
|
|
194
|
+
this.connected = false;
|
|
195
|
+
this.socket = null;
|
|
196
|
+
for (const [id] of this.pending)
|
|
197
|
+
this.settle(id, undefined, new WebinarProtocolError("Webinar is disconnected"));
|
|
198
|
+
for (const listener of this.disconnectListeners)
|
|
199
|
+
listener();
|
|
200
|
+
}
|
|
201
|
+
close() {
|
|
202
|
+
const socket = this.socket;
|
|
203
|
+
this.disconnected();
|
|
204
|
+
socket?.close();
|
|
205
|
+
this.collections.clear();
|
|
206
|
+
this.subscriptions.clear();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { WebinarEvent } from "./types.js";
|
|
2
|
+
/** Bounded stream: slow consumers get an error rather than missing changes silently. */
|
|
3
|
+
export declare function webinarEvents(subscribe: (listener: (event: WebinarEvent) => void) => () => void, opts?: {
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
bufferSize?: number;
|
|
6
|
+
}): AsyncGenerator<WebinarEvent>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Bounded stream: slow consumers get an error rather than missing changes silently. */
|
|
2
|
+
export async function* webinarEvents(subscribe, opts = {}) {
|
|
3
|
+
const limit = opts.bufferSize ?? 512;
|
|
4
|
+
if (!Number.isInteger(limit) || limit < 1)
|
|
5
|
+
throw new RangeError("Invalid webinar event buffer size");
|
|
6
|
+
const queue = [];
|
|
7
|
+
let wake;
|
|
8
|
+
let done = opts.signal?.aborted ?? false;
|
|
9
|
+
let overflow = false;
|
|
10
|
+
const unsubscribe = subscribe((event) => {
|
|
11
|
+
if (queue.length >= limit) {
|
|
12
|
+
overflow = true;
|
|
13
|
+
done = true;
|
|
14
|
+
}
|
|
15
|
+
else
|
|
16
|
+
queue.push(event);
|
|
17
|
+
if (event.type === "disconnected")
|
|
18
|
+
done = true;
|
|
19
|
+
wake?.();
|
|
20
|
+
});
|
|
21
|
+
const abort = () => { done = true; wake?.(); };
|
|
22
|
+
opts.signal?.addEventListener("abort", abort, { once: true });
|
|
23
|
+
try {
|
|
24
|
+
while (queue.length || !done) {
|
|
25
|
+
if (overflow)
|
|
26
|
+
throw new Error("Webinar event buffer overflow; refresh the snapshot");
|
|
27
|
+
const event = queue.shift();
|
|
28
|
+
if (event)
|
|
29
|
+
yield event;
|
|
30
|
+
else
|
|
31
|
+
await new Promise((resolve) => { wake = resolve; });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
unsubscribe();
|
|
36
|
+
opts.signal?.removeEventListener("abort", abort);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type WebinarSession, type WebinarSlide } from "./types.js";
|
|
2
|
+
import { WebinarClient } from "./client.js";
|
|
3
|
+
/** Backend-only LK/BBB bootstrap and same-server presentation HTTP access. */
|
|
4
|
+
export declare class WebinarGateway {
|
|
5
|
+
private http;
|
|
6
|
+
private session;
|
|
7
|
+
private client;
|
|
8
|
+
getSession(): WebinarSession;
|
|
9
|
+
openSession(opts: {
|
|
10
|
+
joinUrl: string;
|
|
11
|
+
}): Promise<WebinarSession>;
|
|
12
|
+
/** Opens state only. Browser media is connected separately by WebinarMediaClient. */
|
|
13
|
+
connect(): Promise<WebinarClient>;
|
|
14
|
+
/** Downloads only when the live participant state exposes this permission. */
|
|
15
|
+
downloadPresentation(opts: {
|
|
16
|
+
presentationId: string;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
body: Buffer;
|
|
19
|
+
contentType: string;
|
|
20
|
+
filename: string;
|
|
21
|
+
}>;
|
|
22
|
+
downloadSlide(opts: {
|
|
23
|
+
slide: WebinarSlide;
|
|
24
|
+
format?: "svg" | "thumbnail" | "text";
|
|
25
|
+
}): Promise<{
|
|
26
|
+
body: Buffer;
|
|
27
|
+
contentType: string;
|
|
28
|
+
filename: string;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { HttpClient } from "../common/http.js";
|
|
2
|
+
import { AuthError, ParseError } from "../common/types.js";
|
|
3
|
+
import { parseWebinarConfig, parseWebinarIceServers, parseWebinarSession } from "./parse.js";
|
|
4
|
+
import { WebinarPermissionError } from "./types.js";
|
|
5
|
+
import { WebinarClient } from "./client.js";
|
|
6
|
+
/** Backend-only LK/BBB bootstrap and same-server presentation HTTP access. */
|
|
7
|
+
export class WebinarGateway {
|
|
8
|
+
http = new HttpClient();
|
|
9
|
+
session = null;
|
|
10
|
+
client = null;
|
|
11
|
+
getSession() {
|
|
12
|
+
if (!this.session)
|
|
13
|
+
throw new AuthError("Open a webinar session first");
|
|
14
|
+
return structuredClone(this.session);
|
|
15
|
+
}
|
|
16
|
+
async openSession(opts) {
|
|
17
|
+
if (this.session)
|
|
18
|
+
throw new Error("Create a new gateway for each webinar session");
|
|
19
|
+
let url = new URL(opts.joinUrl);
|
|
20
|
+
if (url.protocol !== "https:" || url.username || url.password || url.pathname !== "/bigbluebutton/api/join") {
|
|
21
|
+
throw new RangeError("Expected a signed HTTPS BBB join URL");
|
|
22
|
+
}
|
|
23
|
+
const origin = url.origin;
|
|
24
|
+
let html = null;
|
|
25
|
+
for (let redirects = 0; redirects < 6; redirects++) {
|
|
26
|
+
const response = await this.http.get(url.href, false);
|
|
27
|
+
if (response.status >= 300 && response.status < 400 && response.location) {
|
|
28
|
+
const next = new URL(response.location, url);
|
|
29
|
+
if (next.origin !== origin || next.username || next.password)
|
|
30
|
+
throw new AuthError("Webinar redirected outside its server");
|
|
31
|
+
url = next;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (response.status !== 200)
|
|
35
|
+
throw new AuthError(`Webinar join failed with HTTP ${response.status}`);
|
|
36
|
+
html = response.body;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (html === null || !url.searchParams.get("sessionToken"))
|
|
40
|
+
throw new AuthError("Webinar did not create a participant session");
|
|
41
|
+
const config = parseWebinarConfig(html);
|
|
42
|
+
const token = url.searchParams.get("sessionToken");
|
|
43
|
+
const endpoint = (path) => {
|
|
44
|
+
const endpointUrl = new URL(path, origin);
|
|
45
|
+
endpointUrl.searchParams.set("sessionToken", token);
|
|
46
|
+
return endpointUrl.href;
|
|
47
|
+
};
|
|
48
|
+
const entry = await this.http.get(endpoint("/bigbluebutton/api/enter"), false);
|
|
49
|
+
if (entry.status !== 200)
|
|
50
|
+
throw new AuthError("Webinar participant entry failed");
|
|
51
|
+
let data;
|
|
52
|
+
try {
|
|
53
|
+
data = JSON.parse(entry.body);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new ParseError("Invalid webinar entry response");
|
|
57
|
+
}
|
|
58
|
+
const ice = await this.http.get(endpoint("/bigbluebutton/api/stuns"), false);
|
|
59
|
+
if (ice.status !== 200)
|
|
60
|
+
throw new AuthError("Webinar ICE configuration failed");
|
|
61
|
+
this.session = parseWebinarSession({ clientUrl: url.href, config, entry: data, iceServers: parseWebinarIceServers(ice.body) });
|
|
62
|
+
return structuredClone(this.session);
|
|
63
|
+
}
|
|
64
|
+
/** Opens state only. Browser media is connected separately by WebinarMediaClient. */
|
|
65
|
+
async connect() {
|
|
66
|
+
if (!this.session)
|
|
67
|
+
throw new AuthError("Open a webinar session first");
|
|
68
|
+
if (this.client)
|
|
69
|
+
throw new Error("Webinar gateway state client already opened");
|
|
70
|
+
const client = new WebinarClient(this.session, { socketFactory: (url) => this.http.createWebSocket(url) });
|
|
71
|
+
await client.connect();
|
|
72
|
+
this.client = client;
|
|
73
|
+
return client;
|
|
74
|
+
}
|
|
75
|
+
/** Downloads only when the live participant state exposes this permission. */
|
|
76
|
+
async downloadPresentation(opts) {
|
|
77
|
+
const presentation = this.client?.getPresentations().find((item) => item.id === opts.presentationId);
|
|
78
|
+
if (!this.session || !presentation?.downloadable)
|
|
79
|
+
throw new WebinarPermissionError("Original presentation download is disabled or unavailable");
|
|
80
|
+
const extension = presentation.name.split(".").pop() ?? "";
|
|
81
|
+
if (!/^[a-zA-Z0-9]+$/u.test(extension))
|
|
82
|
+
throw new ParseError("Invalid presentation extension");
|
|
83
|
+
const url = new URL(`/bigbluebutton/presentation/download/${encodeURIComponent(this.session.meeting.id)}/${encodeURIComponent(presentation.id)}`, this.session.server.origin);
|
|
84
|
+
url.searchParams.set("presFilename", `${presentation.id}.${extension}`);
|
|
85
|
+
const response = await this.http.getBufferResponse(url.href, false);
|
|
86
|
+
if (response.status !== 200 || !response.contentType || response.contentType.startsWith("text/html")) {
|
|
87
|
+
throw new ParseError("Original presentation download failed");
|
|
88
|
+
}
|
|
89
|
+
return { body: response.body, contentType: response.contentType,
|
|
90
|
+
filename: presentation.name.replace(/[\\/\x00-\x1f]/gu, "_") };
|
|
91
|
+
}
|
|
92
|
+
async downloadSlide(opts) {
|
|
93
|
+
if (!this.session)
|
|
94
|
+
throw new AuthError("Open a webinar session first");
|
|
95
|
+
const format = opts.format ?? "svg";
|
|
96
|
+
const value = format === "svg" ? opts.slide.svgUrl : format === "text" ? opts.slide.textUrl : opts.slide.thumbnailUrl;
|
|
97
|
+
if (!value)
|
|
98
|
+
throw new ParseError("Slide asset is unavailable");
|
|
99
|
+
const url = new URL(value);
|
|
100
|
+
const prefix = `/bigbluebutton/presentation/${encodeURIComponent(this.session.meeting.id)}/`;
|
|
101
|
+
if (url.origin !== this.session.server.origin || !url.pathname.startsWith(prefix)
|
|
102
|
+
|| !/\/(?:svg|thumbnail|textfiles)\/\d+$/u.test(url.pathname) || url.search || url.username || url.password) {
|
|
103
|
+
throw new RangeError("Expected a slide asset from the current webinar");
|
|
104
|
+
}
|
|
105
|
+
const response = await this.http.getBufferResponse(url.href, false);
|
|
106
|
+
if (response.status !== 200)
|
|
107
|
+
throw new Error(`Webinar slide request failed with HTTP ${response.status}`);
|
|
108
|
+
const expected = format === "svg" ? "image/svg+xml" : format === "text" ? "text/" : "image/";
|
|
109
|
+
if (!response.contentType?.startsWith(expected))
|
|
110
|
+
throw new ParseError("Webinar returned an invalid slide asset");
|
|
111
|
+
const extension = format === "svg" ? "svg" : format === "text" ? "txt" : response.contentType.includes("png") ? "png" : "jpg";
|
|
112
|
+
return { body: response.body, contentType: response.contentType, filename: `slide-${opts.slide.number}.${extension}` };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WebinarDocument } from "./ddp.js";
|
|
2
|
+
import type { WebinarPlaybackKind } from "./media-types.js";
|
|
3
|
+
import type { WebinarSession } from "./types.js";
|
|
4
|
+
/** BBB 2.3 SFU messages. One playback per socket; no publish commands. */
|
|
5
|
+
export declare function webinarMediaMessages(session: WebinarSession, kind: WebinarPlaybackKind, streamId?: string, hasAudio?: boolean): {
|
|
6
|
+
start(sdpOffer: string): WebinarDocument;
|
|
7
|
+
candidate(candidate: RTCIceCandidateInit): WebinarDocument;
|
|
8
|
+
stop(): WebinarDocument;
|
|
9
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** BBB 2.3 SFU messages. One playback per socket; no publish commands. */
|
|
2
|
+
export function webinarMediaMessages(session, kind, streamId, hasAudio = false) {
|
|
3
|
+
const type = kind === "camera" ? "video" : kind === "screen" ? "screenshare" : "audio";
|
|
4
|
+
const role = kind === "camera" ? "viewer" : "recv";
|
|
5
|
+
const identity = { type, role, voiceBridge: session.meeting.voiceBridge };
|
|
6
|
+
const specific = kind === "camera" ? { cameraId: streamId } : kind === "screen" ? { callerName: session.user.id } : {};
|
|
7
|
+
return {
|
|
8
|
+
start(sdpOffer) {
|
|
9
|
+
return {
|
|
10
|
+
...identity, ...specific, id: "start", sdpOffer,
|
|
11
|
+
userName: session.user.name,
|
|
12
|
+
...(kind === "camera"
|
|
13
|
+
? { meetingId: session.meeting.id, userId: session.user.id }
|
|
14
|
+
: { internalMeetingId: session.meeting.id }),
|
|
15
|
+
...(kind === "audio" ? { userId: session.user.id, caleeName: `GLOBAL_AUDIO_${session.meeting.voiceBridge}` } : {}),
|
|
16
|
+
...(kind === "screen" ? { hasAudio } : {}),
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
candidate(candidate) {
|
|
20
|
+
return { ...identity, ...specific, id: kind === "camera" ? "onIceCandidate" : "iceCandidate", candidate };
|
|
21
|
+
},
|
|
22
|
+
stop() { return { ...identity, ...specific, id: "stop" }; },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { WebinarSocketFactory } from "./types.js";
|
|
2
|
+
export interface WebinarMediaOptions {
|
|
3
|
+
socketFactory?: WebinarSocketFactory;
|
|
4
|
+
peerFactory?: (config: RTCConfiguration) => RTCPeerConnection;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
}
|
|
7
|
+
export type WebinarPlaybackKind = "audio" | "camera" | "screen";
|
|
8
|
+
export interface WebinarReceiveOptions {
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|