@hiroosy/baileys-lite 0.3.1 → 0.3.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/README.md +1 -1
- package/package.json +1 -1
- package/src/foundation/ai-rich.js +25 -21
- package/src/foundation/image-thumb.js +65 -0
- package/src/socket/messages-send.js +20 -0
- package/src/utils/media.js +28 -6
- package/src/vendor/jpeg-js/LICENSE +0 -25
- 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
package/README.md
CHANGED
|
@@ -392,7 +392,7 @@ await sock.sendMessage(jid, {
|
|
|
392
392
|
})
|
|
393
393
|
```
|
|
394
394
|
|
|
395
|
-
**Bypass download:** `send()`
|
|
395
|
+
**Bypass download:** enabled by default for both `send()` and `sock.sendMessage(jid, { aiRich })`. The message edits itself right after sending, so the client renders the rich response without having to download it first. Pass `bypassDownload: false` in the options to turn it off. It only runs for messages that carry a rich response. If the edit fails, the error is thrown with `error.relayedKey` set to the key of the message that was already sent.
|
|
396
396
|
|
|
397
397
|
<details> <summary align=center>All AiRich Methods</summary>
|
|
398
398
|
|
package/package.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto'
|
|
2
|
-
import { generateMessageIDV2 } from '../utils/wa-protocol-core.js'
|
|
3
2
|
|
|
4
3
|
const MESSAGE_EDIT_TYPE = 14
|
|
5
4
|
|
|
@@ -485,27 +484,32 @@ export class AIRichBuilder {
|
|
|
485
484
|
}
|
|
486
485
|
}
|
|
487
486
|
|
|
488
|
-
async send(jid, { forwarded, quoted, quotedParticipant, botJid, ...sendOptions } = {}) {
|
|
487
|
+
async send(jid, { forwarded, quoted, quotedParticipant, botJid, bypassDownload = true, ...sendOptions } = {}) {
|
|
489
488
|
if (!this._client) throw new Error('AIRichBuilder.send() needs a client — use sock.aiRich() or new AIRichBuilder(sock)')
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
489
|
+
return this._client.sendMessage(jid, { aiRich: this, forwarded, quoted, quotedParticipant, botJid }, { ...sendOptions, bypassDownload })
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const isPlainObject = v => !!v && typeof v === 'object' && !Array.isArray(v) && !Buffer.isBuffer(v)
|
|
494
|
+
|
|
495
|
+
// spec.html accepts: 'html' | ['html', 'Tab title'] | [['html', 'Tab 1'], ['html', 'Tab 2']]
|
|
496
|
+
// optionally with spec.htmlOptions = { url, trustedSources }, or a trailing/second options object.
|
|
497
|
+
// Before, the options had no way in through the spec form and trusted_sources was always [].
|
|
498
|
+
const addHtmlFromSpec = (builder, html, htmlOptions) => {
|
|
499
|
+
let options = isPlainObject(htmlOptions) ? { ...htmlOptions } : {}
|
|
500
|
+
let items
|
|
501
|
+
if (!Array.isArray(html)) {
|
|
502
|
+
items = [html]
|
|
503
|
+
} else if (Array.isArray(html[0])) {
|
|
504
|
+
items = [...html]
|
|
505
|
+
} else if (html.length === 2 && typeof html[0] === 'string' && isPlainObject(html[1])) {
|
|
506
|
+
items = [html[0]]
|
|
507
|
+
options = { ...html[1], ...options }
|
|
508
|
+
} else {
|
|
509
|
+
items = [html]
|
|
508
510
|
}
|
|
511
|
+
if (isPlainObject(items[items.length - 1])) options = { ...items.pop(), ...options }
|
|
512
|
+
builder.addHtml(...items, options)
|
|
509
513
|
}
|
|
510
514
|
|
|
511
515
|
export const aiRichBuilderFromSpec = spec => {
|
|
@@ -525,6 +529,6 @@ export const aiRichBuilderFromSpec = spec => {
|
|
|
525
529
|
if (spec.tip) b.addTip(spec.tip)
|
|
526
530
|
if (spec.suggest) b.addSuggest(spec.suggest, spec.suggestOptions)
|
|
527
531
|
if (spec.process) b.addProcess(spec.process)
|
|
528
|
-
if (spec.html) b
|
|
532
|
+
if (spec.html) addHtmlFromSpec(b, spec.html, spec.htmlOptions)
|
|
529
533
|
return b
|
|
530
534
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { once } from 'node:events'
|
|
3
|
+
import { Boom } from './boom.js'
|
|
4
|
+
|
|
5
|
+
// Dependency-free image thumbnails. Used when none of sharp / @napi-rs/image / jimp is installed,
|
|
6
|
+
// so a missing optional library never results in a media message without jpegThumbnail
|
|
7
|
+
// (the recipient sees that as a grey box). Uses ffmpeg.
|
|
8
|
+
|
|
9
|
+
const FFMPEG_TIMEOUT_MS = 15_000
|
|
10
|
+
|
|
11
|
+
async function runFfmpeg(command, args, input) {
|
|
12
|
+
const child = spawn(command, args, { stdio: [Buffer.isBuffer(input) ? 'pipe' : 'ignore', 'pipe', 'pipe'] })
|
|
13
|
+
const out = []
|
|
14
|
+
const err = []
|
|
15
|
+
child.stdout.on('data', c => out.push(c))
|
|
16
|
+
child.stderr.on('data', c => err.push(c))
|
|
17
|
+
if (Buffer.isBuffer(input)) {
|
|
18
|
+
child.stdin.on('error', () => {})
|
|
19
|
+
child.stdin.end(input)
|
|
20
|
+
}
|
|
21
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), FFMPEG_TIMEOUT_MS)
|
|
22
|
+
try {
|
|
23
|
+
const [code] = await once(child, 'close')
|
|
24
|
+
if (code !== 0) throw new Boom(`${command} failed (code ${code}): ${Buffer.concat(err).toString('utf8').trim()}`)
|
|
25
|
+
return Buffer.concat(out)
|
|
26
|
+
} finally {
|
|
27
|
+
clearTimeout(timer)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function probeDimensions(input) {
|
|
32
|
+
try {
|
|
33
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
34
|
+
const args = ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=p=0', src]
|
|
35
|
+
const [width, height] = (await runFfmpeg('ffprobe', args, input)).toString('utf8').trim().split(',').map(Number)
|
|
36
|
+
return width > 0 && height > 0 ? { width, height } : {}
|
|
37
|
+
} catch {
|
|
38
|
+
return {}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function ffmpegThumb(input, width) {
|
|
43
|
+
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
44
|
+
const args = [
|
|
45
|
+
'-loglevel', 'error', '-i', src, '-an', '-sn', '-dn', '-map_metadata', '-1',
|
|
46
|
+
'-vf', `scale=w='min(${width},iw)':h=-2`,
|
|
47
|
+
'-frames:v', '1', '-q:v', '12', '-c:v', 'mjpeg', '-f', 'image2pipe', 'pipe:1'
|
|
48
|
+
]
|
|
49
|
+
const buffer = await runFfmpeg('ffmpeg', args, input)
|
|
50
|
+
if (!buffer.length) throw new Boom('ffmpeg produced no output')
|
|
51
|
+
return { buffer, original: await probeDimensions(input) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {Buffer | string} input image bytes or a file path
|
|
56
|
+
* @param {number} width thumbnail width in px
|
|
57
|
+
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
58
|
+
*/
|
|
59
|
+
export async function extractImageThumbFallback(input, width = 32) {
|
|
60
|
+
try {
|
|
61
|
+
return await ffmpegThumb(input, width)
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Boom(`could not generate image thumbnail (install sharp, @napi-rs/image or jimp, or ffmpeg): ${err?.message ?? String(err)}`)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1066,6 +1066,9 @@ export const makeMessagesSocket = (config) => {
|
|
|
1066
1066
|
else if (message.groupInviteMessage) {
|
|
1067
1067
|
return 'url';
|
|
1068
1068
|
}
|
|
1069
|
+
else if (message.albumMessage) {
|
|
1070
|
+
return 'collection';
|
|
1071
|
+
}
|
|
1069
1072
|
return '';
|
|
1070
1073
|
};
|
|
1071
1074
|
const issuePrivacyTokens = async (jids, timestamp) => {
|
|
@@ -1387,6 +1390,23 @@ export const makeMessagesSocket = (config) => {
|
|
|
1387
1390
|
});
|
|
1388
1391
|
}
|
|
1389
1392
|
|
|
1393
|
+
if ('aiRich' in content && content.aiRich && options.bypassDownload !== false && fullMsg.key?.id && fullMsg.message?.botForwardedMessage?.message?.richResponseMessage?.unifiedResponse) {
|
|
1394
|
+
try {
|
|
1395
|
+
const edit = new AIRichBuilder().buildEdit(jid, fullMsg.key, fullMsg.message);
|
|
1396
|
+
await relayMessage(jid, edit, {
|
|
1397
|
+
messageId: generateMessageIDV2(userJid),
|
|
1398
|
+
additionalAttributes: { edit: '1' },
|
|
1399
|
+
additionalNodes: options.additionalNodes,
|
|
1400
|
+
useCachedGroupMetadata: options.useCachedGroupMetadata,
|
|
1401
|
+
statusJidList: options.statusJidList
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
catch (error) {
|
|
1405
|
+
error.relayedKey = fullMsg.key;
|
|
1406
|
+
throw error;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1390
1410
|
if ('album' in content) {
|
|
1391
1411
|
const { delayMs = 1500 } = options;
|
|
1392
1412
|
for (const albumMedia of content.album) {
|
package/src/utils/media.js
CHANGED
|
@@ -10,6 +10,7 @@ import { join } from 'node:path'
|
|
|
10
10
|
import { Readable, Transform } from 'node:stream'
|
|
11
11
|
import { proto } from '../../WAProto/index.js'
|
|
12
12
|
import { Boom } from '../foundation/boom.js'
|
|
13
|
+
import { extractImageThumbFallback } from '../foundation/image-thumb.js'
|
|
13
14
|
import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../binary/wa-binary.js'
|
|
14
15
|
import { aesDecryptGCM, aesEncryptGCM, hkdf } from './wa-protocol-core.js'
|
|
15
16
|
import { generateMessageIDV2 } from './wa-protocol-core.js'
|
|
@@ -75,8 +76,10 @@ const NEWSLETTER_MEDIA_PATH_MAP = {
|
|
|
75
76
|
const getTmpFilesDirectory = () => tmpdir()
|
|
76
77
|
|
|
77
78
|
let imageProcessingLibrary
|
|
79
|
+
let imageProcessingLibraryMissing = false
|
|
78
80
|
export const getImageProcessingLibrary = async () => {
|
|
79
81
|
if (imageProcessingLibrary) return imageProcessingLibrary
|
|
82
|
+
if (imageProcessingLibraryMissing) throw new Boom('No image processing library available')
|
|
80
83
|
const [sharp, image, jimp] = await Promise.all([
|
|
81
84
|
import('sharp').catch(() => {}),
|
|
82
85
|
import('@napi-rs/image').catch(() => {}),
|
|
@@ -85,7 +88,10 @@ export const getImageProcessingLibrary = async () => {
|
|
|
85
88
|
if (sharp) imageProcessingLibrary = { sharp }
|
|
86
89
|
else if (image) imageProcessingLibrary = { image }
|
|
87
90
|
else if (jimp) imageProcessingLibrary = { jimp }
|
|
88
|
-
else
|
|
91
|
+
else {
|
|
92
|
+
imageProcessingLibraryMissing = true
|
|
93
|
+
throw new Boom('No image processing library available')
|
|
94
|
+
}
|
|
89
95
|
return imageProcessingLibrary
|
|
90
96
|
}
|
|
91
97
|
|
|
@@ -172,9 +178,7 @@ export const extractVideoThumb = async (path, time, size) => {
|
|
|
172
178
|
return buffer
|
|
173
179
|
}
|
|
174
180
|
|
|
175
|
-
|
|
176
|
-
if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
|
177
|
-
const lib = await getImageProcessingLibrary()
|
|
181
|
+
const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
178
182
|
if ('sharp' in lib && lib.sharp?.default) {
|
|
179
183
|
const img = lib.sharp.default(bufferOrFilePath)
|
|
180
184
|
const dimensions = await img.metadata()
|
|
@@ -189,13 +193,31 @@ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
189
193
|
} else if ('jimp' in lib && lib.jimp?.Jimp) {
|
|
190
194
|
const jimp = await lib.jimp.Jimp.read(bufferOrFilePath)
|
|
191
195
|
const dimensions = { width: jimp.width, height: jimp.height }
|
|
192
|
-
const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy
|
|
196
|
+
const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy?.BILINEAR }).getBuffer('image/jpeg', { quality: 50 })
|
|
193
197
|
return { buffer, original: dimensions }
|
|
194
198
|
} else {
|
|
195
199
|
throw new Boom('No image processing library available')
|
|
196
200
|
}
|
|
197
201
|
}
|
|
198
202
|
|
|
203
|
+
// Never leave a media message without jpegThumbnail just because sharp / @napi-rs/image / jimp is not installed:
|
|
204
|
+
// fall back to ffmpeg (see foundation/image-thumb.js).
|
|
205
|
+
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
206
|
+
if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
|
207
|
+
let libraryError
|
|
208
|
+
try {
|
|
209
|
+
return await thumbWithLibrary(await getImageProcessingLibrary(), bufferOrFilePath, width)
|
|
210
|
+
} catch (err) {
|
|
211
|
+
libraryError = err
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
return await extractImageThumbFallback(bufferOrFilePath, width)
|
|
215
|
+
} catch (err) {
|
|
216
|
+
if (libraryError && libraryError.message !== 'No image processing library available') err.message += ` (image library error: ${libraryError.message})`
|
|
217
|
+
throw err
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
199
221
|
export const encodeBase64EncodedStringForUpload = b64 => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''))
|
|
200
222
|
|
|
201
223
|
export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
@@ -217,7 +239,7 @@ export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
|
217
239
|
const jimp = await lib.jimp.Jimp.read(buffer)
|
|
218
240
|
const min = Math.min(jimp.width, jimp.height)
|
|
219
241
|
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min })
|
|
220
|
-
img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy
|
|
242
|
+
img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy?.BILINEAR }).getBuffer('image/jpeg', { quality: 80 })
|
|
221
243
|
} else {
|
|
222
244
|
throw new Boom('No image processing library available')
|
|
223
245
|
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
Copyright (c) 2014, Eugene Ware
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without
|
|
5
|
-
modification, are permitted provided that the following conditions are met:
|
|
6
|
-
|
|
7
|
-
1. Redistributions of source code must retain the above copyright
|
|
8
|
-
notice, this list of conditions and the following disclaimer.
|
|
9
|
-
2. Redistributions in binary form must reproduce the above copyright
|
|
10
|
-
notice, this list of conditions and the following disclaimer in the
|
|
11
|
-
documentation and/or other materials provided with the distribution.
|
|
12
|
-
3. Neither the name of Eugene Ware nor the names of its contributors
|
|
13
|
-
may be used to endorse or promote products derived from this software
|
|
14
|
-
without specific prior written permission.
|
|
15
|
-
|
|
16
|
-
THIS SOFTWARE IS PROVIDED BY EUGENE WARE ''AS IS'' AND ANY
|
|
17
|
-
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
-
DISCLAIMED. IN NO EVENT SHALL EUGENE WARE BE LIABLE FOR ANY
|
|
20
|
-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
21
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
22
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
23
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|