@onyx-p/imlib-web 2.0.8 → 2.1.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/index.esm.js CHANGED
@@ -20278,6 +20278,7 @@ class ConversationManager {
20278
20278
  }
20279
20279
  remove(conOpt) {
20280
20280
  this.store.remove(conOpt);
20281
+ this.localConversationSet.delete(this.getConversationKey(conOpt));
20281
20282
  }
20282
20283
  clearHistoryMessages(conOpt) {
20283
20284
  this.clearUnreadCount(conOpt);
@@ -20940,9 +20941,31 @@ class MessageCache {
20940
20941
  DB_VERSION = 2;
20941
20942
  STORE_NAME = 'messages';
20942
20943
  DIALOG_STATE_STORE = 'dialogStates';
20944
+ encryptKey = {
20945
+ key: ''
20946
+ };
20947
+ SALT_PREFIX = 'im_message_cache_salt_prefix';
20948
+ SALT_SUFFIX = 'im_message_cache_salt_suffix';
20943
20949
  constructor(appKey, userId) {
20950
+ this.generateEncryptKey(appKey, userId);
20944
20951
  this.init(appKey, userId);
20945
20952
  }
20953
+ generateEncryptKey(appKey, userId) {
20954
+ const saltedInput = `${this.SALT_PREFIX}${appKey}_${userId}${this.SALT_SUFFIX}`;
20955
+ const keyMaterial = CryptoJS.SHA256(saltedInput).toString();
20956
+ const iv = keyMaterial.substring(0, 16);
20957
+ const key = keyMaterial.substring(16, 48);
20958
+ const additionalSalt = CryptoJS.SHA256(`${iv}:${this.SALT_PREFIX}:${userId}:${appKey}`).toString().substring(0, 8);
20959
+ const finalKey = CryptoJS.PBKDF2(key, additionalSalt, {
20960
+ keySize: 256 / 32,
20961
+ iterations: 1000,
20962
+ hasher: CryptoJS.algo.SHA256
20963
+ }).toString();
20964
+ this.encryptKey = {
20965
+ key: finalKey.substring(0, 32),
20966
+ iv: iv
20967
+ };
20968
+ }
20946
20969
  async init(appKey, userId) {
20947
20970
  this.currentAppKey = appKey;
20948
20971
  this.currentUserId = userId;
@@ -21080,6 +21103,28 @@ class MessageCache {
21080
21103
  };
21081
21104
  });
21082
21105
  }
21106
+ encryptContent(content) {
21107
+ if (!content) return '';
21108
+ try {
21109
+ const contentStr = JSON.stringify(content);
21110
+ return aes256Encrypt(contentStr, this.encryptKey);
21111
+ } catch (error) {
21112
+ console.error('加密消息内容失败', error);
21113
+ return '';
21114
+ }
21115
+ }
21116
+ decryptContent(encryptedHex) {
21117
+ if (!encryptedHex) {
21118
+ return null;
21119
+ }
21120
+ try {
21121
+ const decryptedStr = aes256Decrypt(encryptedHex, this.encryptKey);
21122
+ return JSON.parse(decryptedStr);
21123
+ } catch (error) {
21124
+ console.error('解密消息内容失败', error);
21125
+ return null;
21126
+ }
21127
+ }
21083
21128
  async addMessages(messages, toConversation, isEnd = undefined) {
21084
21129
  const dialogId = getFullDialogId(toConversation);
21085
21130
  if (isDef(isEnd)) {
@@ -21096,6 +21141,10 @@ class MessageCache {
21096
21141
  ...message,
21097
21142
  dialogId
21098
21143
  };
21144
+ if (messageToStore.content) {
21145
+ const encryptedContent = this.encryptContent(messageToStore.content);
21146
+ messageToStore.content = encryptedContent;
21147
+ }
21099
21148
  store.put(messageToStore);
21100
21149
  });
21101
21150
  transaction.oncomplete = () => {
@@ -21138,7 +21187,18 @@ class MessageCache {
21138
21187
  const cursor = event.target.result;
21139
21188
  if (cursor) {
21140
21189
  if (processedCount < count) {
21141
- messages.push(cursor.value);
21190
+ const message = {
21191
+ ...cursor.value
21192
+ };
21193
+ if (typeof message.content === 'string' && message.content) {
21194
+ try {
21195
+ message.content = this.decryptContent(message.content);
21196
+ } catch (error) {
21197
+ console.error('解密消息内容失败', error);
21198
+ message.content = null;
21199
+ }
21200
+ }
21201
+ messages.push(message);
21142
21202
  processedCount++;
21143
21203
  cursor.continue();
21144
21204
  } else {
@@ -21149,13 +21209,13 @@ class MessageCache {
21149
21209
  finishQuery();
21150
21210
  }
21151
21211
  };
21152
- function finishQuery() {
21212
+ const finishQuery = () => {
21153
21213
  messages.sort((a, b) => Long.fromString(a.sentTime).compare(Long.fromString(b.sentTime)));
21154
21214
  resolve({
21155
21215
  messages,
21156
21216
  hasMore: hasMoreMessages || totalCount > count || !isLoaded
21157
21217
  });
21158
- }
21218
+ };
21159
21219
  };
21160
21220
  transaction.onerror = event => {
21161
21221
  console.error('获取消息事务失败', event);
@@ -21291,7 +21351,6 @@ class MessageCache {
21291
21351
  return new Promise((resolve, reject) => {
21292
21352
  const transaction = this.db.transaction(this.STORE_NAME, 'readwrite');
21293
21353
  const store = transaction.objectStore(this.STORE_NAME);
21294
- new Set(event.messageUIdList);
21295
21354
  let completed = 0;
21296
21355
  let hasError = false;
21297
21356
  event.messageUIdList.forEach(id => {
@@ -21457,6 +21516,7 @@ class MessageCache {
21457
21516
  }
21458
21517
  this.currentAppKey = appKey;
21459
21518
  this.currentUserId = userId;
21519
+ this.generateEncryptKey(appKey, userId);
21460
21520
  await this.checkAndResetDatabase();
21461
21521
  await this.initDatabase();
21462
21522
  }
package/index.umd.js CHANGED
@@ -20284,6 +20284,7 @@
20284
20284
  }
20285
20285
  remove(conOpt) {
20286
20286
  this.store.remove(conOpt);
20287
+ this.localConversationSet.delete(this.getConversationKey(conOpt));
20287
20288
  }
20288
20289
  clearHistoryMessages(conOpt) {
20289
20290
  this.clearUnreadCount(conOpt);
@@ -20946,9 +20947,31 @@
20946
20947
  DB_VERSION = 2;
20947
20948
  STORE_NAME = 'messages';
20948
20949
  DIALOG_STATE_STORE = 'dialogStates';
20950
+ encryptKey = {
20951
+ key: ''
20952
+ };
20953
+ SALT_PREFIX = 'im_message_cache_salt_prefix';
20954
+ SALT_SUFFIX = 'im_message_cache_salt_suffix';
20949
20955
  constructor(appKey, userId) {
20956
+ this.generateEncryptKey(appKey, userId);
20950
20957
  this.init(appKey, userId);
20951
20958
  }
20959
+ generateEncryptKey(appKey, userId) {
20960
+ const saltedInput = `${this.SALT_PREFIX}${appKey}_${userId}${this.SALT_SUFFIX}`;
20961
+ const keyMaterial = CryptoJS.SHA256(saltedInput).toString();
20962
+ const iv = keyMaterial.substring(0, 16);
20963
+ const key = keyMaterial.substring(16, 48);
20964
+ const additionalSalt = CryptoJS.SHA256(`${iv}:${this.SALT_PREFIX}:${userId}:${appKey}`).toString().substring(0, 8);
20965
+ const finalKey = CryptoJS.PBKDF2(key, additionalSalt, {
20966
+ keySize: 256 / 32,
20967
+ iterations: 1000,
20968
+ hasher: CryptoJS.algo.SHA256
20969
+ }).toString();
20970
+ this.encryptKey = {
20971
+ key: finalKey.substring(0, 32),
20972
+ iv: iv
20973
+ };
20974
+ }
20952
20975
  async init(appKey, userId) {
20953
20976
  this.currentAppKey = appKey;
20954
20977
  this.currentUserId = userId;
@@ -21086,6 +21109,28 @@
21086
21109
  };
21087
21110
  });
21088
21111
  }
21112
+ encryptContent(content) {
21113
+ if (!content) return '';
21114
+ try {
21115
+ const contentStr = JSON.stringify(content);
21116
+ return aes256Encrypt(contentStr, this.encryptKey);
21117
+ } catch (error) {
21118
+ console.error('加密消息内容失败', error);
21119
+ return '';
21120
+ }
21121
+ }
21122
+ decryptContent(encryptedHex) {
21123
+ if (!encryptedHex) {
21124
+ return null;
21125
+ }
21126
+ try {
21127
+ const decryptedStr = aes256Decrypt(encryptedHex, this.encryptKey);
21128
+ return JSON.parse(decryptedStr);
21129
+ } catch (error) {
21130
+ console.error('解密消息内容失败', error);
21131
+ return null;
21132
+ }
21133
+ }
21089
21134
  async addMessages(messages, toConversation, isEnd = undefined) {
21090
21135
  const dialogId = getFullDialogId(toConversation);
21091
21136
  if (isDef(isEnd)) {
@@ -21102,6 +21147,10 @@
21102
21147
  ...message,
21103
21148
  dialogId
21104
21149
  };
21150
+ if (messageToStore.content) {
21151
+ const encryptedContent = this.encryptContent(messageToStore.content);
21152
+ messageToStore.content = encryptedContent;
21153
+ }
21105
21154
  store.put(messageToStore);
21106
21155
  });
21107
21156
  transaction.oncomplete = () => {
@@ -21144,7 +21193,18 @@
21144
21193
  const cursor = event.target.result;
21145
21194
  if (cursor) {
21146
21195
  if (processedCount < count) {
21147
- messages.push(cursor.value);
21196
+ const message = {
21197
+ ...cursor.value
21198
+ };
21199
+ if (typeof message.content === 'string' && message.content) {
21200
+ try {
21201
+ message.content = this.decryptContent(message.content);
21202
+ } catch (error) {
21203
+ console.error('解密消息内容失败', error);
21204
+ message.content = null;
21205
+ }
21206
+ }
21207
+ messages.push(message);
21148
21208
  processedCount++;
21149
21209
  cursor.continue();
21150
21210
  } else {
@@ -21155,13 +21215,13 @@
21155
21215
  finishQuery();
21156
21216
  }
21157
21217
  };
21158
- function finishQuery() {
21218
+ const finishQuery = () => {
21159
21219
  messages.sort((a, b) => Long.fromString(a.sentTime).compare(Long.fromString(b.sentTime)));
21160
21220
  resolve({
21161
21221
  messages,
21162
21222
  hasMore: hasMoreMessages || totalCount > count || !isLoaded
21163
21223
  });
21164
- }
21224
+ };
21165
21225
  };
21166
21226
  transaction.onerror = event => {
21167
21227
  console.error('获取消息事务失败', event);
@@ -21297,7 +21357,6 @@
21297
21357
  return new Promise((resolve, reject) => {
21298
21358
  const transaction = this.db.transaction(this.STORE_NAME, 'readwrite');
21299
21359
  const store = transaction.objectStore(this.STORE_NAME);
21300
- new Set(event.messageUIdList);
21301
21360
  let completed = 0;
21302
21361
  let hasError = false;
21303
21362
  event.messageUIdList.forEach(id => {
@@ -21463,6 +21522,7 @@
21463
21522
  }
21464
21523
  this.currentAppKey = appKey;
21465
21524
  this.currentUserId = userId;
21525
+ this.generateEncryptKey(appKey, userId);
21466
21526
  await this.checkAndResetDatabase();
21467
21527
  await this.initDatabase();
21468
21528
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onyx-p/imlib-web",
3
- "version": "2.0.8",
3
+ "version": "2.1.0",
4
4
  "main": "index.umd.js",
5
5
  "module": "index.esm.js",
6
6
  "types": "types/index.d.ts",