@abraca/dabra 1.0.2 → 1.0.3

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.
@@ -0,0 +1,159 @@
1
+ // ── Signaling Wire Protocol ─────────────────────────────────────────────────
2
+ // Mirrors the Rust server's Incoming/Outgoing enums in signaling.rs exactly.
3
+ // All messages are JSON text frames with a "type" discriminator.
4
+
5
+ export type SignalingIncoming =
6
+ | { type: "join" }
7
+ | { type: "leave" }
8
+ | { type: "offer"; to: string; sdp: string }
9
+ | { type: "answer"; to: string; sdp: string }
10
+ | { type: "ice"; to: string; candidate: string }
11
+ | { type: "mute"; muted: boolean }
12
+ | { type: "media-state"; video: boolean; screen: boolean }
13
+ | { type: "profile"; name: string; color: string }
14
+ | { type: "pong" };
15
+
16
+ export type SignalingOutgoing =
17
+ | { type: "welcome"; peer_id: string; peers: PeerInfo[] }
18
+ | {
19
+ type: "joined";
20
+ peer_id: string;
21
+ user_id: string;
22
+ muted: boolean;
23
+ video: boolean;
24
+ screen: boolean;
25
+ name: string | null;
26
+ color: string | null;
27
+ }
28
+ | { type: "left"; peer_id: string }
29
+ | { type: "offer"; from: string; sdp: string }
30
+ | { type: "answer"; from: string; sdp: string }
31
+ | { type: "ice"; from: string; candidate: string }
32
+ | { type: "mute"; peer_id: string; muted: boolean }
33
+ | {
34
+ type: "media-state";
35
+ peer_id: string;
36
+ video: boolean;
37
+ screen: boolean;
38
+ }
39
+ | { type: "profile"; peer_id: string; name: string; color: string }
40
+ | { type: "ping" }
41
+ | { type: "error"; code: string; message: string };
42
+
43
+ // ── Peer State ──────────────────────────────────────────────────────────────
44
+
45
+ export interface PeerInfo {
46
+ peer_id: string;
47
+ user_id: string;
48
+ muted: boolean;
49
+ video: boolean;
50
+ screen: boolean;
51
+ name: string | null;
52
+ color: string | null;
53
+ }
54
+
55
+ export interface PeerState extends PeerInfo {
56
+ connectionState: RTCPeerConnectionState | "new";
57
+ }
58
+
59
+ // ── File Transfer ───────────────────────────────────────────────────────────
60
+
61
+ export interface FileTransferMeta {
62
+ transferId: string;
63
+ filename: string;
64
+ mimeType: string;
65
+ totalSize: number;
66
+ chunkSize: number;
67
+ totalChunks: number;
68
+ }
69
+
70
+ export type FileTransferStatus =
71
+ | "pending"
72
+ | "sending"
73
+ | "receiving"
74
+ | "complete"
75
+ | "cancelled"
76
+ | "error";
77
+
78
+ /** Data channel file transfer message type discriminators (first byte). */
79
+ export const FILE_MSG = {
80
+ START: 0x01,
81
+ CHUNK: 0x02,
82
+ COMPLETE: 0x03,
83
+ CANCEL: 0x04,
84
+ } as const;
85
+
86
+ /** Data channel Y.js message type discriminators (first byte). */
87
+ export const YJS_MSG = {
88
+ SYNC: 0x00,
89
+ UPDATE: 0x01,
90
+ } as const;
91
+
92
+ // ── Data Channel Names ──────────────────────────────────────────────────────
93
+
94
+ export const CHANNEL_NAMES = {
95
+ YJS_SYNC: "yjs-sync",
96
+ AWARENESS: "awareness",
97
+ FILE_TRANSFER: "file-transfer",
98
+ CUSTOM: "custom",
99
+ } as const;
100
+
101
+ // ── Configuration ───────────────────────────────────────────────────────────
102
+
103
+ export interface AbracadabraWebRTCConfiguration {
104
+ /** Document ID for the signaling room. */
105
+ docId: string;
106
+
107
+ /** Server base URL (http/https). Signaling URL derived automatically. */
108
+ url: string;
109
+
110
+ /** JWT token or async token factory. */
111
+ token: string | (() => string) | (() => Promise<string>);
112
+
113
+ /** Optional Y.Doc to sync over data channels (hybrid mode). */
114
+ document?: InstanceType<typeof import("yjs").Doc> | null;
115
+
116
+ /** Optional Awareness instance for presence sync over data channels. */
117
+ awareness?: InstanceType<
118
+ typeof import("y-protocols/awareness").Awareness
119
+ > | null;
120
+
121
+ /** ICE server configuration. Defaults to Google STUN. */
122
+ iceServers?: RTCIceServer[];
123
+
124
+ /** Display name for this peer. */
125
+ displayName?: string;
126
+
127
+ /** Color identifier for this peer. */
128
+ color?: string;
129
+
130
+ /** Enable Y.js document sync over data channels. Default: true when document is provided. */
131
+ enableDocSync?: boolean;
132
+
133
+ /** Enable awareness sync over data channels. Default: true when awareness is provided. */
134
+ enableAwarenessSync?: boolean;
135
+
136
+ /** Enable file transfer channel. Default: false. */
137
+ enableFileTransfer?: boolean;
138
+
139
+ /** Max file chunk size in bytes. Default: 16384 (16KB). */
140
+ fileChunkSize?: number;
141
+
142
+ /** Auto-connect on construction. Default: true. */
143
+ autoConnect?: boolean;
144
+
145
+ /** WebSocket polyfill for signaling (e.g. for Node.js). */
146
+ WebSocketPolyfill?: any;
147
+ }
148
+
149
+ export const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
150
+ { urls: "stun:stun.l.google.com:19302" },
151
+ ];
152
+
153
+ export const DEFAULT_FILE_CHUNK_SIZE = 16384; // 16KB
154
+
155
+ /** UUID v4 transfer ID length when encoded as raw bytes. */
156
+ export const TRANSFER_ID_BYTES = 16;
157
+
158
+ /** SHA-256 hash length in bytes. */
159
+ export const SHA256_BYTES = 32;