@ermis-network/ermis-chat-sdk 1.0.1 → 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.
package/dist/index.cjs CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  CallAction: () => CallAction,
34
34
  CallStatus: () => CallStatus,
35
+ CallType: () => CallType,
35
36
  Channel: () => Channel,
36
37
  ChannelState: () => ChannelState,
37
38
  ClientState: () => ClientState,
@@ -3604,17 +3605,24 @@ var ErmisChat = class _ErmisChat {
3604
3605
  async unpinChannel(channelType, channelId) {
3605
3606
  return await this.post(this.baseURL + `/channels/${channelType}/${channelId}/unpin`);
3606
3607
  }
3607
- channel(channelType, channelID, custom = {}) {
3608
+ channel(channelType, channelIDOrCustom, custom) {
3608
3609
  if (!this.userID) {
3609
3610
  throw Error("Call connectUser before creating a channel");
3610
3611
  }
3611
3612
  if (~channelType.indexOf(":")) {
3612
3613
  throw Error(`Invalid channel group ${channelType}, can't contain the : character`);
3613
3614
  }
3614
- return this.getChannelById(channelType, channelID, custom);
3615
+ let channelID = void 0;
3616
+ let customData = custom || {};
3617
+ if (typeof channelIDOrCustom === "string") {
3618
+ channelID = channelIDOrCustom;
3619
+ } else if (typeof channelIDOrCustom === "object" && channelIDOrCustom !== null) {
3620
+ customData = channelIDOrCustom;
3621
+ }
3622
+ return this.getChannelById(channelType, channelID, customData);
3615
3623
  }
3616
3624
  getChannelById = (channelType, channelID, custom) => {
3617
- const cid = `${channelType}:${channelID}`;
3625
+ const cid = `${channelType}:${channelID || ""}`;
3618
3626
  if (cid in this.activeChannels && !this.activeChannels[cid].disconnected) {
3619
3627
  const channel2 = this.activeChannels[cid];
3620
3628
  if (Object.keys(custom).length > 0) {
@@ -3657,7 +3665,7 @@ var ErmisChat = class _ErmisChat {
3657
3665
  return pinExpires;
3658
3666
  }
3659
3667
  getUserAgent() {
3660
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.1"}`;
3668
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
3661
3669
  }
3662
3670
  setUserAgent(userAgent) {
3663
3671
  this.userAgent = userAgent;
@@ -6367,21 +6375,38 @@ var ErmisCallNode = class {
6367
6375
  const mediaConstraints = await this.getMediaConstraints();
6368
6376
  try {
6369
6377
  const stream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6370
- if (this.callStatus === "ended" /* ENDED */) {
6371
- stream.getTracks().forEach((track) => track.stop());
6372
- this.destroy();
6373
- return;
6378
+ return this.applyLocalStream(stream);
6379
+ } catch (error) {
6380
+ console.warn("Error getting user media:", error?.message);
6381
+ if (this.callType === "video" && mediaConstraints.video) {
6382
+ try {
6383
+ const audioOnlyStream = await navigator.mediaDevices.getUserMedia({
6384
+ audio: mediaConstraints.audio,
6385
+ video: false
6386
+ });
6387
+ this.setConnectionMessage("Camera not available, using audio only");
6388
+ return this.applyLocalStream(audioOnlyStream);
6389
+ } catch {
6390
+ }
6374
6391
  }
6375
- if (this.onLocalStream) {
6376
- this.onLocalStream(stream);
6392
+ if (typeof this.onError === "function") {
6393
+ this.onError("No microphone or camera found. Please check your device.");
6377
6394
  }
6378
- this.localStream = stream;
6379
- return stream;
6380
- } catch (error) {
6381
- console.error("Error getting user media:", error);
6382
6395
  return null;
6383
6396
  }
6384
6397
  }
6398
+ applyLocalStream(stream) {
6399
+ if (this.callStatus === "ended" /* ENDED */) {
6400
+ stream.getTracks().forEach((track) => track.stop());
6401
+ this.destroy();
6402
+ return;
6403
+ }
6404
+ if (this.onLocalStream) {
6405
+ this.onLocalStream(stream);
6406
+ }
6407
+ this.localStream = stream;
6408
+ return stream;
6409
+ }
6385
6410
  setConnectionMessage(message) {
6386
6411
  if (typeof this.onConnectionMessageChange === "function") {
6387
6412
  this.onConnectionMessageChange(message);
@@ -6426,20 +6451,10 @@ var ErmisCallNode = class {
6426
6451
  this.isDestroyed = false;
6427
6452
  this.callStatus = "";
6428
6453
  this.callType = is_video ? "video" : "audio";
6429
- await this.startLocalStream();
6430
- if (this.callStatus === "ended" /* ENDED */) return;
6431
6454
  this.setUserInfo(cid, eventUserId);
6432
6455
  this.setCallStatus("ringing" /* RINGING */);
6433
6456
  this.cid = cid || "";
6434
6457
  this.metadata = metadata || {};
6435
- console.log("----metadata---", metadata);
6436
- if (eventUserId !== this.userID) {
6437
- await this.initialize();
6438
- }
6439
- if (this.localStream && this.mediaSender && this.mediaReceiver) {
6440
- this.mediaSender?.initEncoders(this.localStream);
6441
- this.mediaReceiver?.initDecoders(this.callType);
6442
- }
6443
6458
  if (typeof this.onCallEvent === "function") {
6444
6459
  this.onCallEvent({
6445
6460
  type: eventUserId !== this.userID ? "incoming" : "outgoing",
@@ -6450,6 +6465,15 @@ var ErmisCallNode = class {
6450
6465
  metadata: this.metadata
6451
6466
  });
6452
6467
  }
6468
+ await this.startLocalStream();
6469
+ if (this.callStatus === "ended" /* ENDED */) return;
6470
+ if (eventUserId !== this.userID) {
6471
+ await this.initialize();
6472
+ }
6473
+ if (this.localStream && this.mediaSender && this.mediaReceiver) {
6474
+ this.mediaSender?.initEncoders(this.localStream);
6475
+ this.mediaReceiver?.initDecoders(this.callType);
6476
+ }
6453
6477
  if (eventUserId === this.userID) {
6454
6478
  if (this.missCallTimeout) clearTimeout(this.missCallTimeout);
6455
6479
  this.missCallTimeout = setTimeout(async () => {
@@ -6710,21 +6734,30 @@ var ErmisCallNode = class {
6710
6734
  }
6711
6735
  async stopScreenShare() {
6712
6736
  const mediaConstraints = await this.getMediaConstraints();
6713
- const cameraStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6714
- const cameraTrack = cameraStream.getVideoTracks()[0];
6715
- if (this.localStream) {
6716
- this.localStream.getVideoTracks().forEach((track) => track.stop());
6717
- this.localStream.removeTrack(this.localStream.getVideoTracks()[0]);
6718
- this.localStream.addTrack(cameraTrack);
6719
- } else {
6720
- this.localStream = cameraStream;
6721
- }
6722
- if (this.onLocalStream) {
6723
- this.onLocalStream(this.localStream);
6724
- this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6725
- }
6726
- if (typeof this.onScreenShareChange === "function") {
6727
- this.onScreenShareChange(false);
6737
+ try {
6738
+ const cameraStream = await navigator.mediaDevices.getUserMedia({
6739
+ video: mediaConstraints.video,
6740
+ audio: false
6741
+ });
6742
+ const cameraTrack = cameraStream.getVideoTracks()[0];
6743
+ if (this.localStream) {
6744
+ this.localStream.getVideoTracks().forEach((track) => {
6745
+ track.stop();
6746
+ this.localStream?.removeTrack(track);
6747
+ });
6748
+ this.localStream.addTrack(cameraTrack);
6749
+ } else {
6750
+ this.localStream = cameraStream;
6751
+ }
6752
+ if (this.onLocalStream) {
6753
+ this.onLocalStream(this.localStream);
6754
+ this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6755
+ }
6756
+ if (typeof this.onScreenShareChange === "function") {
6757
+ this.onScreenShareChange(false);
6758
+ }
6759
+ } catch (error) {
6760
+ console.error("Error stopping screen share and reverting to camera:", error);
6728
6761
  }
6729
6762
  }
6730
6763
  async toggleMic(enabled) {
@@ -6992,7 +7025,7 @@ var ErmisAuthProvider = class {
6992
7025
  return data;
6993
7026
  }
6994
7027
  getUserAgent() {
6995
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.1"}`;
7028
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
6996
7029
  }
6997
7030
  setUserAgent(userAgent) {
6998
7031
  this.userAgent = userAgent;
@@ -7225,71 +7258,117 @@ function parseSystemMessage(value, userMap) {
7225
7258
  }
7226
7259
 
7227
7260
  // src/signal_message.ts
7228
- function formatDuration(durationSec) {
7229
- const sec = parseInt(durationSec, 10);
7230
- if (isNaN(sec) || sec < 0) return durationSec;
7231
- const minutes = Math.floor(sec / 60);
7232
- const seconds = sec % 60;
7233
- return `${minutes}:${seconds.toString().padStart(2, "0")}`;
7234
- }
7235
- function resolveUser2(userId, userMap) {
7236
- return userMap[userId] ?? userId;
7261
+ var CallType = {
7262
+ AUDIO: "audio",
7263
+ VIDEO: "video"
7264
+ };
7265
+ function formatDuration(durationMs) {
7266
+ if (!durationMs) return "";
7267
+ const ms = parseInt(durationMs, 10);
7268
+ if (isNaN(ms) || ms <= 0) return "";
7269
+ const totalSeconds = Math.floor(ms / 1e3);
7270
+ const minutes = Math.floor(totalSeconds / 60);
7271
+ const seconds = totalSeconds % 60;
7272
+ return `${minutes} min, ${seconds} sec`;
7237
7273
  }
7238
- function parseSignalMessage(value, userMap) {
7239
- if (!value || typeof value !== "string") return value ?? "";
7274
+ function parseSignalMessage(value, myUserId) {
7275
+ if (!value || typeof value !== "string") return null;
7240
7276
  const trimmed = value.trim();
7241
- if (!trimmed) return "";
7277
+ if (!trimmed) return null;
7242
7278
  const parts = trimmed.split(" ");
7243
- const formatId = parts[0];
7244
- const userId = parts[1] ?? "";
7245
- const userName = userId ? resolveUser2(userId, userMap) : "User";
7246
- switch (formatId) {
7247
- // 1: Audio call started
7248
- case "1":
7249
- return `\u{1F4DE} ${userName} started an audio call.`;
7250
- // 2: Audio call missed
7251
- case "2":
7252
- return `\u{1F4DE} Missed audio call from ${userName}.`;
7253
- // 3: Audio call ended (caller_id ender_id duration)
7254
- case "3": {
7255
- const enderId = parts[2] ?? "";
7256
- const duration = parts[3] ?? "0";
7257
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7258
- return `\u{1F4DE} Audio call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7259
- }
7260
- // 4: Video call started
7261
- case "4":
7262
- return `\u{1F4F9} ${userName} started a video call.`;
7263
- // 5: Video call missed
7264
- case "5":
7265
- return `\u{1F4F9} Missed video call from ${userName}.`;
7266
- // 6: Video call ended (caller_id ender_id duration)
7267
- case "6": {
7268
- const enderId = parts[2] ?? "";
7269
- const duration = parts[3] ?? "0";
7270
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7271
- return `\u{1F4F9} Video call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7272
- }
7273
- // 7: Audio call rejected
7274
- case "7":
7275
- return `\u{1F4DE} Audio call from ${userName} was rejected.`;
7276
- // 8: Video call rejected
7277
- case "8":
7278
- return `\u{1F4F9} Video call from ${userName} was rejected.`;
7279
- // 9: Audio call busy
7280
- case "9":
7281
- return `\u{1F4DE} Audio call from ${userName} \u2014 recipient was busy.`;
7282
- // 10: Video call busy
7283
- case "10":
7284
- return `\u{1F4F9} Video call from ${userName} \u2014 recipient was busy.`;
7279
+ const number = parseInt(parts[0], 10);
7280
+ const callerId = parts[1] ?? "";
7281
+ const isMe = myUserId === callerId;
7282
+ let enderId = "";
7283
+ let duration = "";
7284
+ let callType = "";
7285
+ let color = "";
7286
+ if (number === 3 || number === 6) {
7287
+ enderId = parts[2] ?? "";
7288
+ duration = parts[3] === "0" ? "" : parts[3] ?? "";
7289
+ }
7290
+ let text;
7291
+ switch (number) {
7292
+ case 1:
7293
+ text = isMe ? "Calling..." : "Incoming audio call...";
7294
+ callType = CallType.AUDIO;
7295
+ color = "#54D62C";
7296
+ break;
7297
+ case 2:
7298
+ text = isMe ? "Outgoing audio call" : "You missed audio call";
7299
+ callType = CallType.AUDIO;
7300
+ color = "#FF4842";
7301
+ break;
7302
+ case 3:
7303
+ if (duration) {
7304
+ text = isMe ? "Outgoing audio call" : "Incoming audio call";
7305
+ color = "#54D62C";
7306
+ } else {
7307
+ if (enderId === myUserId) {
7308
+ text = "You cancel audio call";
7309
+ } else {
7310
+ text = "You missed audio call";
7311
+ }
7312
+ color = "#FF4842";
7313
+ }
7314
+ callType = CallType.AUDIO;
7315
+ break;
7316
+ case 4:
7317
+ text = isMe ? "Calling..." : "Incoming video call...";
7318
+ callType = CallType.VIDEO;
7319
+ color = "#54D62C";
7320
+ break;
7321
+ case 5:
7322
+ text = isMe ? "Outgoing video call" : "You missed video call";
7323
+ callType = CallType.VIDEO;
7324
+ color = "#FF4842";
7325
+ break;
7326
+ case 6:
7327
+ if (duration) {
7328
+ text = isMe ? "Outgoing video call" : "Incoming video call";
7329
+ color = "#54D62C";
7330
+ } else {
7331
+ if (enderId === myUserId) {
7332
+ text = "You cancel video call";
7333
+ } else {
7334
+ text = "You missed video call";
7335
+ }
7336
+ color = "#FF4842";
7337
+ }
7338
+ callType = CallType.VIDEO;
7339
+ break;
7340
+ case 7:
7341
+ text = isMe ? "Recipient rejected audio call" : "You rejected audio call";
7342
+ callType = CallType.AUDIO;
7343
+ color = "#FF4842";
7344
+ break;
7345
+ case 8:
7346
+ text = isMe ? "Recipient rejected video call" : "You rejected video call";
7347
+ callType = CallType.VIDEO;
7348
+ color = "#FF4842";
7349
+ break;
7350
+ case 9:
7351
+ text = isMe ? "Recipient was busy" : "You missed audio call";
7352
+ callType = CallType.AUDIO;
7353
+ color = "#FF4842";
7354
+ break;
7355
+ case 10:
7356
+ text = isMe ? "Recipient was busy" : "You missed video call";
7357
+ callType = CallType.VIDEO;
7358
+ color = "#FF4842";
7359
+ break;
7285
7360
  default:
7286
- return trimmed;
7361
+ text = trimmed;
7362
+ callType = "";
7363
+ color = "";
7287
7364
  }
7365
+ return { text, duration: formatDuration(duration), callType, color };
7288
7366
  }
7289
7367
  // Annotate the CommonJS export names for ESM import in node:
7290
7368
  0 && (module.exports = {
7291
7369
  CallAction,
7292
7370
  CallStatus,
7371
+ CallType,
7293
7372
  Channel,
7294
7373
  ChannelState,
7295
7374
  ClientState,