@hiroosy/baileys-lite 0.3.2 → 0.3.4
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 +1 -1
- package/WAProto/index.js +1530 -1514
- package/package.json +1 -1
- package/src/binary/wa-binary.js +4 -2
- package/src/defaults.js +2 -1
- package/src/foundation/ai-rich.js +1 -19
- package/src/foundation/cache.js +99 -24
- package/src/foundation/image-thumb.js +5 -106
- package/src/foundation/memory.js +103 -0
- package/src/signal/libsignal.js +1 -8
- package/src/signal/signal-repository.js +6 -4
- package/src/socket/chats.js +2 -0
- package/src/socket/messages-recv.js +5 -3
- package/src/socket/messages-send.js +19 -2
- package/src/socket/socket-core.js +3 -0
- package/src/utils/auth-state-core.js +2 -1
- package/src/utils/chat-sync.js +4 -0
- package/src/utils/media.js +16 -11
- package/src/utils/message-processing.js +22 -6
- package/src/utils/wa-protocol-core.js +6 -5
- package/src/wam/wam-constants.js +22 -22851
- package/src/wam/wam-data.json +1 -0
- package/src/vendor/jpeg-js/decoder.js +0 -1091
- package/src/vendor/jpeg-js/encoder.js +0 -739
- package/src/vendor/pngjs/LICENSE +0 -20
- package/src/vendor/pngjs/bitmapper.js +0 -251
- package/src/vendor/pngjs/bitpacker.js +0 -159
- package/src/vendor/pngjs/constants.js +0 -30
- package/src/vendor/pngjs/crc.js +0 -41
- package/src/vendor/pngjs/filter-pack.js +0 -173
- package/src/vendor/pngjs/filter-parse-sync.js +0 -21
- package/src/vendor/pngjs/filter-parse.js +0 -175
- package/src/vendor/pngjs/format-normaliser.js +0 -95
- package/src/vendor/pngjs/interlace.js +0 -84
- package/src/vendor/pngjs/packer-sync.js +0 -54
- package/src/vendor/pngjs/packer.js +0 -130
- package/src/vendor/pngjs/paeth-predictor.js +0 -16
- package/src/vendor/pngjs/parser-sync.js +0 -113
- package/src/vendor/pngjs/parser.js +0 -275
- package/src/vendor/pngjs/png-sync.js +0 -12
- package/src/vendor/pngjs/sync-inflate.js +0 -165
- package/src/vendor/pngjs/sync-reader.js +0 -47
|
@@ -100,10 +100,11 @@ export class PreKeyManager {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
const txStorage = new AsyncLocalStorage()
|
|
103
|
+
const SIGNAL_CACHE_BYTES = 6 * 1024 * 1024
|
|
103
104
|
|
|
104
105
|
export function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
105
106
|
|
|
106
|
-
const cache = _cache || new Cache({ max:
|
|
107
|
+
const cache = _cache || new Cache({ max: 2048, maxBytes: SIGNAL_CACHE_BYTES, ttl: DEFAULT_CACHE_TTLS.SIGNAL_STORE * 1000 })
|
|
107
108
|
const cacheMutex = makeMutex()
|
|
108
109
|
|
|
109
110
|
function getUniqueId(type, id) {
|
package/src/utils/chat-sync.js
CHANGED
|
@@ -8,6 +8,7 @@ import { aesDecrypt, aesEncrypt, hmacSign, toNumber, trimUndefined, LT_HASH_ANTI
|
|
|
8
8
|
import { downloadContentFromMessage } from './media.js'
|
|
9
9
|
import { updateMessageWithReaction, updateMessageWithReceipt } from './message-compose.js'
|
|
10
10
|
import { isRealMessage, shouldIncrementChatUnread } from './message-processing.js'
|
|
11
|
+
import { requestTrim } from '../foundation/memory.js'
|
|
11
12
|
|
|
12
13
|
const mutationKeys = keydata => {
|
|
13
14
|
const keys = expandAppStateKeys(keydata)
|
|
@@ -897,6 +898,7 @@ export const makeEventBuffer = logger => {
|
|
|
897
898
|
let flushPendingTimeout = null
|
|
898
899
|
let bufferCount = 0
|
|
899
900
|
const MAX_HISTORY_CACHE_SIZE = 10000
|
|
901
|
+
const HEAVY_FLUSH_MESSAGES = 200
|
|
900
902
|
const BUFFER_TIMEOUT_MS = 30000
|
|
901
903
|
|
|
902
904
|
ev.on('event', map => {
|
|
@@ -947,7 +949,9 @@ export const makeEventBuffer = logger => {
|
|
|
947
949
|
}
|
|
948
950
|
}
|
|
949
951
|
const consolidatedData = consolidateEvents(data)
|
|
952
|
+
const heavy = !!consolidatedData['messaging-history.set'] || (consolidatedData['messages.upsert']?.messages.length ?? 0) > HEAVY_FLUSH_MESSAGES
|
|
950
953
|
if (Object.keys(consolidatedData).length) ev.emit('event', consolidatedData)
|
|
954
|
+
if (heavy) requestTrim(3000)
|
|
951
955
|
data = newData
|
|
952
956
|
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
953
957
|
return true
|
package/src/utils/media.js
CHANGED
|
@@ -85,7 +85,13 @@ export const getImageProcessingLibrary = async () => {
|
|
|
85
85
|
import('@napi-rs/image').catch(() => {}),
|
|
86
86
|
import('jimp').catch(() => {})
|
|
87
87
|
])
|
|
88
|
-
if (sharp)
|
|
88
|
+
if (sharp) {
|
|
89
|
+
try {
|
|
90
|
+
sharp.default.cache(false)
|
|
91
|
+
sharp.default.concurrency(1)
|
|
92
|
+
} catch {}
|
|
93
|
+
imageProcessingLibrary = { sharp }
|
|
94
|
+
}
|
|
89
95
|
else if (image) imageProcessingLibrary = { image }
|
|
90
96
|
else if (jimp) imageProcessingLibrary = { jimp }
|
|
91
97
|
else {
|
|
@@ -167,15 +173,13 @@ export const extractVideoThumb = async (path, time, size) => {
|
|
|
167
173
|
],
|
|
168
174
|
{ stdio: ['ignore', 'pipe', 'pipe'] }
|
|
169
175
|
)
|
|
170
|
-
|
|
176
|
+
const stdoutChunks = []
|
|
171
177
|
const stderrChunks = []
|
|
172
|
-
ffmpeg.stdout.on('data', chunk =>
|
|
173
|
-
buffer = Buffer.concat([buffer, chunk])
|
|
174
|
-
})
|
|
178
|
+
ffmpeg.stdout.on('data', chunk => stdoutChunks.push(chunk))
|
|
175
179
|
ffmpeg.stderr.on('data', chunk => stderrChunks.push(chunk))
|
|
176
180
|
const [code] = await once(ffmpeg, 'close')
|
|
177
181
|
if (code !== 0) throw new Boom(`FFmpeg failed (code ${code}):\n` + Buffer.concat(stderrChunks).toString('utf8'))
|
|
178
|
-
return
|
|
182
|
+
return Buffer.concat(stdoutChunks)
|
|
179
183
|
}
|
|
180
184
|
|
|
181
185
|
const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
@@ -193,7 +197,7 @@ const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
|
193
197
|
} else if ('jimp' in lib && lib.jimp?.Jimp) {
|
|
194
198
|
const jimp = await lib.jimp.Jimp.read(bufferOrFilePath)
|
|
195
199
|
const dimensions = { width: jimp.width, height: jimp.height }
|
|
196
|
-
const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy
|
|
200
|
+
const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy?.BILINEAR }).getBuffer('image/jpeg', { quality: 50 })
|
|
197
201
|
return { buffer, original: dimensions }
|
|
198
202
|
} else {
|
|
199
203
|
throw new Boom('No image processing library available')
|
|
@@ -201,7 +205,7 @@ const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
// Never leave a media message without jpegThumbnail just because sharp / @napi-rs/image / jimp is not installed:
|
|
204
|
-
// fall back to
|
|
208
|
+
// fall back to ffmpeg (see foundation/image-thumb.js).
|
|
205
209
|
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
206
210
|
if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
|
207
211
|
let libraryError
|
|
@@ -239,7 +243,7 @@ export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
|
239
243
|
const jimp = await lib.jimp.Jimp.read(buffer)
|
|
240
244
|
const min = Math.min(jimp.width, jimp.height)
|
|
241
245
|
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min })
|
|
242
|
-
img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy
|
|
246
|
+
img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy?.BILINEAR }).getBuffer('image/jpeg', { quality: 80 })
|
|
243
247
|
} else {
|
|
244
248
|
throw new Boom('No image processing library available')
|
|
245
249
|
}
|
|
@@ -574,9 +578,10 @@ export const uploadWithNodeHttp = async ({ url, filePath, headers, timeoutMs, ag
|
|
|
574
578
|
resolve(uploadWithNodeHttp({ url: newUrl, filePath, headers, timeoutMs, agent }, redirectCount + 1))
|
|
575
579
|
return
|
|
576
580
|
}
|
|
577
|
-
|
|
578
|
-
res.on('data', chunk => (
|
|
581
|
+
const bodyChunks = []
|
|
582
|
+
res.on('data', chunk => bodyChunks.push(chunk))
|
|
579
583
|
res.on('end', () => {
|
|
584
|
+
const body = Buffer.concat(bodyChunks).toString('utf8')
|
|
580
585
|
try {
|
|
581
586
|
resolve(JSON.parse(body))
|
|
582
587
|
} catch {
|
|
@@ -28,8 +28,10 @@ import { aesDecryptGCM, hmacSign, getKeyAuthor, toNumber, generateMessageIDV2 }
|
|
|
28
28
|
import { getContentType, normalizeMessageContent } from './message-compose.js'
|
|
29
29
|
import { downloadContentFromMessage, getStream, getUrlFromDirectPath } from './media.js'
|
|
30
30
|
import { buildMergedTcTokenIndexWrite, resolveTcTokenJid } from './auth-state-core.js'
|
|
31
|
+
import { requestTrim } from '../foundation/memory.js'
|
|
31
32
|
|
|
32
33
|
const inflatePromise = promisify(inflate)
|
|
34
|
+
const MAX_PROCESSED_HISTORY_MESSAGES = 64
|
|
33
35
|
|
|
34
36
|
const REAL_MSG_STUB_TYPES = new Set([
|
|
35
37
|
WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
@@ -207,7 +209,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
|
|
|
207
209
|
if (process) {
|
|
208
210
|
if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
209
211
|
ev.emit('creds.update', {
|
|
210
|
-
processedHistoryMessages: [...(creds.processedHistoryMessages || []), { key: message.key, messageTimestamp: message.messageTimestamp }]
|
|
212
|
+
processedHistoryMessages: [...(creds.processedHistoryMessages || []), { key: message.key, messageTimestamp: message.messageTimestamp }].slice(-MAX_PROCESSED_HISTORY_MESSAGES)
|
|
211
213
|
})
|
|
212
214
|
}
|
|
213
215
|
const data = await downloadAndProcessHistorySyncNotification(histNotification, options, logger)
|
|
@@ -222,6 +224,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
|
|
|
222
224
|
chunkOrder: histNotification.chunkOrder,
|
|
223
225
|
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
|
224
226
|
})
|
|
227
|
+
requestTrim(4000)
|
|
225
228
|
}
|
|
226
229
|
break
|
|
227
230
|
}
|
|
@@ -480,7 +483,8 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
|
|
|
480
483
|
|
|
481
484
|
export default processMessage
|
|
482
485
|
|
|
483
|
-
const RECENT_MESSAGES_SIZE =
|
|
486
|
+
const RECENT_MESSAGES_SIZE = 512
|
|
487
|
+
const RECENT_MESSAGES_BYTES = 4 * 1024 * 1024
|
|
484
488
|
const MESSAGE_KEY_SEPARATOR = '\u0000'
|
|
485
489
|
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000
|
|
486
490
|
const PHONE_REQUEST_DELAY = 3000
|
|
@@ -511,6 +515,7 @@ export class MessageRetryManager {
|
|
|
511
515
|
this.logger = logger
|
|
512
516
|
this.recentMessagesMap = new Cache({
|
|
513
517
|
max: RECENT_MESSAGES_SIZE,
|
|
518
|
+
maxBytes: RECENT_MESSAGES_BYTES,
|
|
514
519
|
ttl: 5 * 60 * 1000,
|
|
515
520
|
dispose: (_value, key) => {
|
|
516
521
|
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR)
|
|
@@ -519,9 +524,9 @@ export class MessageRetryManager {
|
|
|
519
524
|
})
|
|
520
525
|
this.messageKeyIndex = new Map()
|
|
521
526
|
|
|
522
|
-
this.sessionRecreateHistory = new Cache({ max:
|
|
523
|
-
this.retryCounters = new Cache({ max:
|
|
524
|
-
this.baseKeys = new Cache({ max:
|
|
527
|
+
this.sessionRecreateHistory = new Cache({ max: 2048, ttl: RECREATE_SESSION_TIMEOUT * 2 })
|
|
528
|
+
this.retryCounters = new Cache({ max: 2048, ttl: 15 * 60 * 1000, updateAgeOnGet: true })
|
|
529
|
+
this.baseKeys = new Cache({ max: 512, ttl: 15 * 60 * 1000 })
|
|
525
530
|
this.pendingPhoneRequests = {}
|
|
526
531
|
this.maxMsgRetryCount = maxMsgRetryCount
|
|
527
532
|
this.statistics = { totalRetries: 0, successfulRetries: 0, failedRetries: 0, mediaRetries: 0, sessionRecreations: 0, phoneRequests: 0 }
|
|
@@ -646,6 +651,15 @@ export class MessageRetryManager {
|
|
|
646
651
|
this.statistics = { totalRetries: 0, successfulRetries: 0, failedRetries: 0, mediaRetries: 0, sessionRecreations: 0, phoneRequests: 0 }
|
|
647
652
|
}
|
|
648
653
|
|
|
654
|
+
close() {
|
|
655
|
+
this.clear()
|
|
656
|
+
this.recentMessagesMap.close()
|
|
657
|
+
this.sessionRecreateHistory.close()
|
|
658
|
+
this.retryCounters.close()
|
|
659
|
+
this.baseKeys.close()
|
|
660
|
+
this.invalidatedMessages.close()
|
|
661
|
+
}
|
|
662
|
+
|
|
649
663
|
saveBaseKey(addr, msgId, baseKey) {
|
|
650
664
|
this.baseKeys.set(`${addr}:${msgId}`, baseKey)
|
|
651
665
|
}
|
|
@@ -719,7 +733,9 @@ export const downloadHistory = async (msg, options) => {
|
|
|
719
733
|
const chunks = []
|
|
720
734
|
inflater.on('data', chunk => chunks.push(chunk))
|
|
721
735
|
await pipeline(stream, inflater)
|
|
722
|
-
|
|
736
|
+
const inflated = Buffer.concat(chunks)
|
|
737
|
+
chunks.length = 0
|
|
738
|
+
return proto.HistorySync.decode(inflated)
|
|
723
739
|
}
|
|
724
740
|
|
|
725
741
|
export const processHistoryMessage = (item, logger) => {
|
|
@@ -717,7 +717,8 @@ export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publi
|
|
|
717
717
|
let decKey = hash
|
|
718
718
|
let counter = 0
|
|
719
719
|
let sentIntro = false
|
|
720
|
-
|
|
720
|
+
const EMPTY_BYTES = Buffer.alloc(0)
|
|
721
|
+
let inBytes = EMPTY_BYTES
|
|
721
722
|
let transport = null
|
|
722
723
|
let isWaitingForTransport = false
|
|
723
724
|
let pendingOnFrame = null
|
|
@@ -785,7 +786,7 @@ export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publi
|
|
|
785
786
|
size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2]
|
|
786
787
|
if (inBytes.length < size + 3) return
|
|
787
788
|
let frame = inBytes.subarray(3, size + 3)
|
|
788
|
-
inBytes = inBytes.subarray(size + 3)
|
|
789
|
+
inBytes = inBytes.length === size + 3 ? EMPTY_BYTES : inBytes.subarray(size + 3)
|
|
789
790
|
if (transport) {
|
|
790
791
|
const result = transport.decrypt(frame) // gagal decrypt = counter tidak sinkron lagi -> biarkan melempar (fatal)
|
|
791
792
|
try {
|
|
@@ -863,11 +864,11 @@ export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publi
|
|
|
863
864
|
inBytes = Buffer.concat([inBytes, newData])
|
|
864
865
|
pendingOnFrame = onFrame
|
|
865
866
|
} else {
|
|
866
|
-
inBytes = inBytes.length === 0 ?
|
|
867
|
+
inBytes = inBytes.length === 0 ? newData : Buffer.concat([inBytes, newData])
|
|
867
868
|
}
|
|
868
869
|
if (inBytes.length > MAX_IN_BYTES) {
|
|
869
870
|
logger.error({ bufferedBytes: inBytes.length, max: MAX_IN_BYTES }, 'noise handler inBytes buffer exceeded cap, clearing')
|
|
870
|
-
inBytes =
|
|
871
|
+
inBytes = EMPTY_BYTES
|
|
871
872
|
pendingOnFrame = null
|
|
872
873
|
return
|
|
873
874
|
}
|
|
@@ -882,7 +883,7 @@ export const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publi
|
|
|
882
883
|
|
|
883
884
|
destroy: () => {
|
|
884
885
|
destroyed = true
|
|
885
|
-
inBytes =
|
|
886
|
+
inBytes = EMPTY_BYTES
|
|
886
887
|
pendingOnFrame = null
|
|
887
888
|
transport = null
|
|
888
889
|
}
|