@hiroosy/baileys-lite 0.3.2 → 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 +1 -19
- package/src/foundation/image-thumb.js +5 -106
- package/src/socket/messages-send.js +17 -0
- package/src/utils/media.js +3 -3
- 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
|
|
|
@@ -487,24 +486,7 @@ export class AIRichBuilder {
|
|
|
487
486
|
|
|
488
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
|
-
const richResponse = sent?.message?.botForwardedMessage?.message?.richResponseMessage
|
|
492
|
-
if (!bypassDownload || !sent?.key?.id || !richResponse?.unifiedResponse) return sent
|
|
493
|
-
try {
|
|
494
|
-
const userId = this._client.user?.id ?? this._client.authState?.creds?.me?.id
|
|
495
|
-
const edit = this.buildEdit(jid, sent.key, sent.message)
|
|
496
|
-
await this._client.relayMessage(jid, edit, {
|
|
497
|
-
messageId: generateMessageIDV2(userId),
|
|
498
|
-
additionalAttributes: { edit: '1' },
|
|
499
|
-
additionalNodes: sendOptions.additionalNodes,
|
|
500
|
-
useCachedGroupMetadata: sendOptions.useCachedGroupMetadata,
|
|
501
|
-
statusJidList: sendOptions.statusJidList
|
|
502
|
-
})
|
|
503
|
-
} catch (error) {
|
|
504
|
-
error.relayedKey = sent.key
|
|
505
|
-
throw error
|
|
506
|
-
}
|
|
507
|
-
return sent
|
|
489
|
+
return this._client.sendMessage(jid, { aiRich: this, forwarded, quoted, quotedParticipant, botJid }, { ...sendOptions, bypassDownload })
|
|
508
490
|
}
|
|
509
491
|
}
|
|
510
492
|
|
|
@@ -1,90 +1,13 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
2
|
import { once } from 'node:events'
|
|
3
|
-
import { promises as fs } from 'node:fs'
|
|
4
3
|
import { Boom } from './boom.js'
|
|
5
4
|
|
|
6
5
|
// Dependency-free image thumbnails. Used when none of sharp / @napi-rs/image / jimp is installed,
|
|
7
6
|
// so a missing optional library never results in a media message without jpegThumbnail
|
|
8
|
-
// (the recipient sees that as a grey box).
|
|
9
|
-
// 1. built-in decoder (vendored jpeg-js + pngjs) for JPEG / PNG
|
|
10
|
-
// 2. ffmpeg for everything else (webp, gif, bmp, ...) and for big files, so the event loop is not
|
|
11
|
-
// blocked by a pure-JS decode of a multi-megapixel photo
|
|
7
|
+
// (the recipient sees that as a grey box). Uses ffmpeg.
|
|
12
8
|
|
|
13
|
-
const JPEG_QUALITY = 50
|
|
14
|
-
const BUILTIN_PREFERRED_MAX_BYTES = 2 * 1024 * 1024
|
|
15
9
|
const FFMPEG_TIMEOUT_MS = 15_000
|
|
16
10
|
|
|
17
|
-
const isJpeg = b => b.length > 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff
|
|
18
|
-
const isPng = b => b.length > 8 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47
|
|
19
|
-
|
|
20
|
-
async function readHeader(path) {
|
|
21
|
-
const handle = await fs.open(path, 'r')
|
|
22
|
-
try {
|
|
23
|
-
const header = Buffer.alloc(16)
|
|
24
|
-
const { bytesRead } = await handle.read(header, 0, 16, 0)
|
|
25
|
-
return header.subarray(0, bytesRead)
|
|
26
|
-
} finally {
|
|
27
|
-
await handle.close()
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function decodeRaster(buffer) {
|
|
32
|
-
if (isJpeg(buffer)) {
|
|
33
|
-
const { default: decode } = await import('../vendor/jpeg-js/decoder.js')
|
|
34
|
-
return decode(buffer, { useTArray: true, formatAsRGBA: true, tolerantDecoding: true })
|
|
35
|
-
}
|
|
36
|
-
if (isPng(buffer)) {
|
|
37
|
-
const { read } = await import('../vendor/pngjs/png-sync.js')
|
|
38
|
-
return read(buffer)
|
|
39
|
-
}
|
|
40
|
-
throw new Boom('unsupported image format for built-in decoder')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// area-average downscale; transparent pixels are composited over white (JPEG has no alpha)
|
|
44
|
-
function resizeToRgba({ data, width: sw, height: sh }, dw, dh) {
|
|
45
|
-
const out = Buffer.allocUnsafe(dw * dh * 4)
|
|
46
|
-
const xr = sw / dw
|
|
47
|
-
const yr = sh / dh
|
|
48
|
-
for (let dy = 0; dy < dh; dy++) {
|
|
49
|
-
const y0 = Math.floor(dy * yr)
|
|
50
|
-
const y1 = Math.min(sh, Math.max(y0 + 1, Math.ceil((dy + 1) * yr)))
|
|
51
|
-
for (let dx = 0; dx < dw; dx++) {
|
|
52
|
-
const x0 = Math.floor(dx * xr)
|
|
53
|
-
const x1 = Math.min(sw, Math.max(x0 + 1, Math.ceil((dx + 1) * xr)))
|
|
54
|
-
let r = 0
|
|
55
|
-
let g = 0
|
|
56
|
-
let b = 0
|
|
57
|
-
for (let y = y0; y < y1; y++) {
|
|
58
|
-
let i = (y * sw + x0) * 4
|
|
59
|
-
for (let x = x0; x < x1; x++, i += 4) {
|
|
60
|
-
const a = data[i + 3] / 255
|
|
61
|
-
r += data[i] * a + 255 * (1 - a)
|
|
62
|
-
g += data[i + 1] * a + 255 * (1 - a)
|
|
63
|
-
b += data[i + 2] * a + 255 * (1 - a)
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const n = (y1 - y0) * (x1 - x0)
|
|
67
|
-
const o = (dy * dw + dx) * 4
|
|
68
|
-
out[o] = Math.round(r / n)
|
|
69
|
-
out[o + 1] = Math.round(g / n)
|
|
70
|
-
out[o + 2] = Math.round(b / n)
|
|
71
|
-
out[o + 3] = 255
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return out
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async function builtinThumb(buffer, width) {
|
|
78
|
-
const image = await decodeRaster(buffer)
|
|
79
|
-
if (!image?.width || !image?.height || !image.data?.length) throw new Boom('could not decode image')
|
|
80
|
-
const dw = Math.max(1, Math.min(width, image.width)) // never upscale
|
|
81
|
-
const dh = Math.max(1, Math.round((image.height * dw) / image.width))
|
|
82
|
-
const { default: encode } = await import('../vendor/jpeg-js/encoder.js')
|
|
83
|
-
const rgba = resizeToRgba(image, dw, dh)
|
|
84
|
-
const { data } = encode({ data: rgba, width: dw, height: dh }, JPEG_QUALITY)
|
|
85
|
-
return { buffer: Buffer.from(data), original: { width: image.width, height: image.height } }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
11
|
async function runFfmpeg(command, args, input) {
|
|
89
12
|
const child = spawn(command, args, { stdio: [Buffer.isBuffer(input) ? 'pipe' : 'ignore', 'pipe', 'pipe'] })
|
|
90
13
|
const out = []
|
|
@@ -134,33 +57,9 @@ async function ffmpegThumb(input, width) {
|
|
|
134
57
|
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
135
58
|
*/
|
|
136
59
|
export async function extractImageThumbFallback(input, width = 32) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
size = input.length
|
|
142
|
-
} else {
|
|
143
|
-
header = await readHeader(input)
|
|
144
|
-
size = (await fs.stat(input)).size
|
|
145
|
-
}
|
|
146
|
-
const decodable = isJpeg(header) || isPng(header)
|
|
147
|
-
const big = size > BUILTIN_PREFERRED_MAX_BYTES
|
|
148
|
-
const attempts = []
|
|
149
|
-
if (decodable) {
|
|
150
|
-
const builtin = async () => builtinThumb(Buffer.isBuffer(input) ? input : await fs.readFile(input), width)
|
|
151
|
-
attempts.push(builtin)
|
|
152
|
-
if (big) attempts.unshift(() => ffmpegThumb(input, width))
|
|
153
|
-
else attempts.push(() => ffmpegThumb(input, width))
|
|
154
|
-
} else {
|
|
155
|
-
attempts.push(() => ffmpegThumb(input, width))
|
|
156
|
-
}
|
|
157
|
-
const errors = []
|
|
158
|
-
for (const attempt of attempts) {
|
|
159
|
-
try {
|
|
160
|
-
return await attempt()
|
|
161
|
-
} catch (err) {
|
|
162
|
-
errors.push(err?.message ?? String(err))
|
|
163
|
-
}
|
|
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)}`)
|
|
164
64
|
}
|
|
165
|
-
throw new Boom(`could not generate image thumbnail (install sharp, @napi-rs/image or jimp, or ffmpeg): ${errors.join(' | ')}`)
|
|
166
65
|
}
|
|
@@ -1390,6 +1390,23 @@ export const makeMessagesSocket = (config) => {
|
|
|
1390
1390
|
});
|
|
1391
1391
|
}
|
|
1392
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
|
+
|
|
1393
1410
|
if ('album' in content) {
|
|
1394
1411
|
const { delayMs = 1500 } = options;
|
|
1395
1412
|
for (const albumMedia of content.album) {
|
package/src/utils/media.js
CHANGED
|
@@ -193,7 +193,7 @@ const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
|
193
193
|
} else if ('jimp' in lib && lib.jimp?.Jimp) {
|
|
194
194
|
const jimp = await lib.jimp.Jimp.read(bufferOrFilePath)
|
|
195
195
|
const dimensions = { width: jimp.width, height: jimp.height }
|
|
196
|
-
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 })
|
|
197
197
|
return { buffer, original: dimensions }
|
|
198
198
|
} else {
|
|
199
199
|
throw new Boom('No image processing library available')
|
|
@@ -201,7 +201,7 @@ const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
// Never leave a media message without jpegThumbnail just because sharp / @napi-rs/image / jimp is not installed:
|
|
204
|
-
// fall back to
|
|
204
|
+
// fall back to ffmpeg (see foundation/image-thumb.js).
|
|
205
205
|
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
206
206
|
if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
|
207
207
|
let libraryError
|
|
@@ -239,7 +239,7 @@ export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
|
239
239
|
const jimp = await lib.jimp.Jimp.read(buffer)
|
|
240
240
|
const min = Math.min(jimp.width, jimp.height)
|
|
241
241
|
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min })
|
|
242
|
-
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 })
|
|
243
243
|
} else {
|
|
244
244
|
throw new Boom('No image processing library available')
|
|
245
245
|
}
|