chatly-sdk 1.0.0 → 2.0.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.
Files changed (63) hide show
  1. package/CONTRIBUTING.md +658 -0
  2. package/IMPROVEMENTS.md +402 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1576 -162
  5. package/dist/index.d.ts +502 -11
  6. package/dist/index.js +1619 -66
  7. package/examples/01-basic-chat/README.md +61 -0
  8. package/examples/01-basic-chat/index.js +58 -0
  9. package/examples/01-basic-chat/package.json +13 -0
  10. package/examples/02-group-chat/README.md +78 -0
  11. package/examples/02-group-chat/index.js +76 -0
  12. package/examples/02-group-chat/package.json +13 -0
  13. package/examples/03-offline-messaging/README.md +73 -0
  14. package/examples/03-offline-messaging/index.js +80 -0
  15. package/examples/03-offline-messaging/package.json +13 -0
  16. package/examples/04-live-chat/README.md +80 -0
  17. package/examples/04-live-chat/index.js +114 -0
  18. package/examples/04-live-chat/package.json +13 -0
  19. package/examples/05-hybrid-messaging/README.md +71 -0
  20. package/examples/05-hybrid-messaging/index.js +106 -0
  21. package/examples/05-hybrid-messaging/package.json +13 -0
  22. package/examples/06-postgresql-integration/README.md +101 -0
  23. package/examples/06-postgresql-integration/adapters/groupStore.js +73 -0
  24. package/examples/06-postgresql-integration/adapters/messageStore.js +47 -0
  25. package/examples/06-postgresql-integration/adapters/userStore.js +40 -0
  26. package/examples/06-postgresql-integration/index.js +92 -0
  27. package/examples/06-postgresql-integration/package.json +14 -0
  28. package/examples/06-postgresql-integration/schema.sql +58 -0
  29. package/examples/08-customer-support/README.md +70 -0
  30. package/examples/08-customer-support/index.js +104 -0
  31. package/examples/08-customer-support/package.json +13 -0
  32. package/examples/README.md +105 -0
  33. package/jest.config.cjs +28 -0
  34. package/package.json +15 -6
  35. package/src/chat/ChatSession.ts +160 -3
  36. package/src/chat/GroupSession.ts +108 -1
  37. package/src/constants.ts +61 -0
  38. package/src/crypto/e2e.ts +9 -20
  39. package/src/crypto/utils.ts +3 -1
  40. package/src/index.ts +530 -63
  41. package/src/models/mediaTypes.ts +62 -0
  42. package/src/models/message.ts +4 -1
  43. package/src/storage/adapters.ts +36 -0
  44. package/src/storage/localStorage.ts +49 -0
  45. package/src/storage/s3Storage.ts +84 -0
  46. package/src/stores/adapters.ts +2 -0
  47. package/src/stores/memory/messageStore.ts +8 -0
  48. package/src/transport/adapters.ts +51 -1
  49. package/src/transport/memoryTransport.ts +75 -13
  50. package/src/transport/websocketClient.ts +269 -21
  51. package/src/transport/websocketServer.ts +26 -26
  52. package/src/utils/errors.ts +97 -0
  53. package/src/utils/logger.ts +96 -0
  54. package/src/utils/mediaUtils.ts +235 -0
  55. package/src/utils/messageQueue.ts +162 -0
  56. package/src/utils/validation.ts +99 -0
  57. package/test/crypto.test.ts +122 -35
  58. package/test/sdk.test.ts +276 -0
  59. package/test/validation.test.ts +64 -0
  60. package/tsconfig.json +11 -10
  61. package/tsconfig.test.json +11 -0
  62. package/src/ChatManager.ts +0 -103
  63. package/src/crypto/keyManager.ts +0 -28
package/src/crypto/e2e.ts CHANGED
@@ -11,26 +11,6 @@
11
11
  const TAG_LENGTH = 16;
12
12
  const PBKDF2_ITERATIONS = 100000;
13
13
 
14
- /**
15
- * Derive a shared secret using ECDH key exchange
16
- */
17
- // export function deriveSharedSecret(local: KeyPair, remotePublicKey: string): Buffer {
18
- // const ecdh = createECDH(SUPPORTED_CURVE);
19
- // ecdh.setPrivateKey(base64ToBuffer(local.privateKey));
20
-
21
- // const remotePublicKeyBuffer = base64ToBuffer(remotePublicKey);
22
- // const sharedSecret = ecdh.computeSecret(remotePublicKeyBuffer);
23
-
24
- // // Derive a symmetric key from the shared secret using PBKDF2
25
- // // Use a deterministic salt based on both public keys for consistency
26
- // const salt = Buffer.concat([
27
- // base64ToBuffer(local.publicKey),
28
- // base64ToBuffer(remotePublicKey)
29
- // ]).slice(0, SALT_LENGTH);
30
- // const derivedKey = pbkdf2Sync(sharedSecret, salt, PBKDF2_ITERATIONS, KEY_LENGTH, "sha256");
31
-
32
- // return derivedKey;
33
- // }
34
14
 
35
15
  export function deriveSharedSecret(local: KeyPair, remotePublicKey: string): Buffer {
36
16
  const ecdh = createECDH(SUPPORTED_CURVE);
@@ -53,6 +33,15 @@
53
33
  return derivedKey;
54
34
  }
55
35
 
36
+ /**
37
+ * Legacy secret derivation without salt (for backward compatibility)
38
+ */
39
+ export function deriveLegacySharedSecret(local: KeyPair, remotePublicKey: string): Buffer {
40
+ const ecdh = createECDH(SUPPORTED_CURVE);
41
+ ecdh.setPrivateKey(base64ToBuffer(local.privateKey));
42
+ return ecdh.computeSecret(base64ToBuffer(remotePublicKey));
43
+ }
44
+
56
45
  /**
57
46
  * Encrypt a message using AES-GCM
58
47
  */
@@ -2,6 +2,8 @@ export function bufferToBase64(buffer: Buffer): string {
2
2
  return buffer.toString("base64");
3
3
  }
4
4
 
5
- export function base64ToBuffer(data: string): Buffer {
5
+ export function base64ToBuffer(data: string | Buffer): Buffer {
6
+ if (Buffer.isBuffer(data)) return data;
7
+ if (!data) return Buffer.alloc(0);
6
8
  return Buffer.from(data, "base64");
7
9
  }