@oxidezap/baileyrs 0.3.7 → 0.3.8
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 +48 -8
- package/lib/Bridge/history-sync-wire.d.ts +2 -2
- package/lib/Bridge/history-sync-wire.js +4 -4
- package/lib/Bridge/schema.js +7 -1
- package/lib/Bridge/types.d.ts +7 -1
- package/lib/Compatibility/auth-state.d.ts +30 -0
- package/lib/Compatibility/auth-state.js +96 -0
- package/lib/Compatibility/encode-proto-core.d.ts +31 -0
- package/lib/Compatibility/encode-proto-core.js +51 -0
- package/lib/Compatibility/encode-proto.d.ts +0 -29
- package/lib/Compatibility/encode-proto.js +3 -50
- package/lib/Compatibility/host-auth-state.d.ts +6 -0
- package/lib/Compatibility/host-auth-state.js +162 -0
- package/lib/Compatibility/internal/auth-state.d.ts +3 -7
- package/lib/Compatibility/internal/auth-state.js +34 -1
- package/lib/Compatibility/internal/host-auth-state.d.ts +4 -0
- package/lib/Compatibility/internal/host-auth-state.js +14 -0
- package/lib/Compatibility/legacy-store/device.js +5 -5
- package/lib/Compatibility/message-ids.d.ts +17 -0
- package/lib/Compatibility/message-ids.js +47 -0
- package/lib/Compatibility/message-relay.d.ts +2 -2
- package/lib/Compatibility/message-relay.js +4 -4
- package/lib/Compatibility/proto-runtime.js +33 -11
- package/lib/Compatibility/socket-results.d.ts +1 -1
- package/lib/Compatibility/tagged-message-waiter.d.ts +5 -2
- package/lib/Compatibility/tagged-message-waiter.js +5 -5
- package/lib/Compatibility/usync/adapter.d.ts +1 -1
- package/lib/Compatibility/websocket-client.d.ts +11 -3
- package/lib/Compatibility/websocket-client.js +84 -11
- package/lib/Defaults/index.d.ts +8 -8
- package/lib/Defaults/index.js +8 -8
- package/lib/Media/content.d.ts +28 -0
- package/lib/Media/content.js +129 -0
- package/lib/Media/core.d.ts +34 -0
- package/lib/Media/core.js +120 -0
- package/lib/Media/mutations.d.ts +24 -0
- package/lib/Media/mutations.js +38 -0
- package/lib/Runtime/aes-gcm.d.ts +4 -0
- package/lib/Runtime/aes-gcm.js +11 -0
- package/lib/Runtime/bridge.d.ts +44 -0
- package/lib/Runtime/bridge.js +76 -0
- package/lib/Runtime/buffer.d.ts +9 -0
- package/lib/Runtime/buffer.js +22 -0
- package/lib/Runtime/bytes.d.ts +61 -0
- package/lib/Runtime/bytes.js +270 -0
- package/lib/Runtime/host.d.ts +22 -0
- package/lib/Runtime/host.js +43 -0
- package/lib/Runtime/long.d.ts +33 -0
- package/lib/Runtime/long.js +64 -0
- package/lib/Runtime/node-media-processors.d.ts +4 -0
- package/lib/Runtime/node-media-processors.js +29 -0
- package/lib/Runtime/node-media.d.ts +28 -0
- package/lib/Runtime/node-media.js +115 -0
- package/lib/Runtime/node.d.ts +14 -0
- package/lib/Runtime/node.js +81 -0
- package/lib/Runtime/paths.d.ts +4 -0
- package/lib/Runtime/paths.js +7 -0
- package/lib/Runtime/stream.d.ts +11 -0
- package/lib/Runtime/stream.js +25 -0
- package/lib/Runtime/types.d.ts +98 -0
- package/lib/Runtime/types.js +13 -0
- package/lib/Socket/core.d.ts +312 -0
- package/lib/Socket/core.js +1122 -0
- package/lib/Socket/events-core.d.ts +75 -0
- package/lib/Socket/events-core.js +1058 -0
- package/lib/Socket/events.d.ts +1 -63
- package/lib/Socket/events.js +4 -1047
- package/lib/Socket/groups.js +4 -3
- package/lib/Socket/index.d.ts +521 -103
- package/lib/Socket/index.js +10 -1007
- package/lib/Socket/messages-core.d.ts +41 -0
- package/lib/Socket/messages-core.js +270 -0
- package/lib/Socket/messages.d.ts +9 -36
- package/lib/Socket/messages.js +3 -260
- package/lib/Socket/newsletter.js +3 -3
- package/lib/Socket/profile.js +1 -1
- package/lib/Socket/terminal-close-reporter.d.ts +2 -0
- package/lib/Socket/terminal-close-reporter.js +3 -3
- package/lib/Socket/transport.d.ts +2 -0
- package/lib/Socket/transport.js +16 -1
- package/lib/Socket/types.d.ts +2 -0
- package/lib/Types/Auth.d.ts +11 -0
- package/lib/Types/Message.d.ts +1 -1
- package/lib/Types/Socket.d.ts +2 -2
- package/lib/Types/index.d.ts +6 -1
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.js +53 -2
- package/lib/Utils/crypto.d.ts +1 -0
- package/lib/Utils/crypto.js +1 -0
- package/lib/Utils/event-buffer.d.ts +6 -1
- package/lib/Utils/event-buffer.js +28 -27
- package/lib/Utils/link-preview.d.ts +4 -3
- package/lib/Utils/logger.d.ts +3 -0
- package/lib/Utils/logger.js +35 -6
- package/lib/Utils/messages-core.d.ts +174 -0
- package/lib/Utils/messages-core.js +1042 -0
- package/lib/Utils/messages-media-core.d.ts +73 -0
- package/lib/Utils/messages-media-core.js +319 -0
- package/lib/Utils/messages-media.d.ts +1 -72
- package/lib/Utils/messages-media.js +9 -358
- package/lib/Utils/messages.d.ts +1 -166
- package/lib/Utils/messages.js +7 -1040
- package/lib/Utils/optional-peer.js +14 -5
- package/lib/Utils/process-history-message-core.d.ts +36 -0
- package/lib/Utils/process-history-message-core.js +187 -0
- package/lib/Utils/process-history-message.d.ts +5 -43
- package/lib/Utils/process-history-message.js +11 -197
- package/lib/Utils/process-message-core.d.ts +29 -0
- package/lib/Utils/process-message-core.js +112 -0
- package/lib/Utils/process-message.d.ts +4 -24
- package/lib/Utils/process-message.js +6 -111
- package/lib/WABinary/generic-utils.js +7 -5
- package/lib/host-shared.d.ts +74 -0
- package/lib/host-shared.js +15 -0
- package/lib/host-socket-operations.d.ts +3 -0
- package/lib/host-socket-operations.js +2 -0
- package/lib/host-surface.d.ts +21 -0
- package/lib/host-surface.js +35 -0
- package/lib/host-types.d.ts +356 -0
- package/lib/host-types.js +2 -0
- package/lib/host.d.ts +33 -0
- package/lib/host.js +31 -0
- package/lib/node-surface.d.ts +9 -0
- package/lib/node-surface.js +15 -0
- package/lib/surface.d.ts +5 -0
- package/lib/surface.js +6 -0
- package/lib/wasm.d.ts +27 -0
- package/lib/wasm.js +27 -0
- package/package.json +18 -6
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Buffer } from 'node:buffer';
|
|
2
|
+
import type { Readable } from 'node:stream';
|
|
3
|
+
import { type MediaType } from '../Defaults/index.js';
|
|
4
|
+
import type { BaileysEventMap, BinaryNode, MediaDecryptionKeyInfo, WAMediaUpload, WAMessageContent, WAMessageKey } from '../Types/index.js';
|
|
5
|
+
import { proto } from '../WAProto/runtime.js';
|
|
6
|
+
import type { ILogger } from './logger.js';
|
|
7
|
+
export declare const hkdfInfoKey: (type: MediaType) => string;
|
|
8
|
+
export declare function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): Promise<MediaDecryptionKeyInfo>;
|
|
9
|
+
export declare const extractImageThumb: (bufferOrFilePath: Readable | Buffer | string, width?: number) => Promise<{
|
|
10
|
+
buffer: Buffer<ArrayBufferLike>;
|
|
11
|
+
original: {
|
|
12
|
+
width: number | undefined;
|
|
13
|
+
height: number | undefined;
|
|
14
|
+
};
|
|
15
|
+
}>;
|
|
16
|
+
export declare const encodeBase64EncodedStringForUpload: (b64: string) => string;
|
|
17
|
+
export declare const generateProfilePicture: (mediaUpload: WAMediaUpload, dimensions?: {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}) => Promise<{
|
|
21
|
+
img: Buffer;
|
|
22
|
+
}>;
|
|
23
|
+
export declare const mediaMessageSHA256B64: (message: WAMessageContent) => string | null | undefined;
|
|
24
|
+
/** Returns audio duration when the selected runtime provides a metadata processor. */
|
|
25
|
+
export declare const getAudioDuration: (buffer: Buffer | string | Readable) => Promise<number | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
28
|
+
*/
|
|
29
|
+
export declare function getAudioWaveform(buffer: Buffer | string | Readable, logger?: ILogger): Promise<Uint8Array<ArrayBuffer> | undefined>;
|
|
30
|
+
export declare const toReadable: (buffer: Buffer) => Readable;
|
|
31
|
+
export declare const toBuffer: (stream: Readable | Uint8Array) => Promise<Buffer<ArrayBufferLike>>;
|
|
32
|
+
export declare const getStream: (item: WAMediaUpload, opts?: RequestInit & {
|
|
33
|
+
maxContentLength?: number;
|
|
34
|
+
}) => Promise<{
|
|
35
|
+
readonly stream: Buffer<ArrayBufferLike> | Readable;
|
|
36
|
+
readonly type: 'buffer';
|
|
37
|
+
} | {
|
|
38
|
+
readonly stream: Readable;
|
|
39
|
+
readonly type: 'readable';
|
|
40
|
+
} | {
|
|
41
|
+
readonly stream: Readable;
|
|
42
|
+
readonly type: 'remote';
|
|
43
|
+
} | {
|
|
44
|
+
readonly stream: Readable;
|
|
45
|
+
readonly type: 'file';
|
|
46
|
+
}>;
|
|
47
|
+
/** generates a thumbnail for a given media, if required */
|
|
48
|
+
export declare function generateThumbnail(bufferOrPath: Buffer | string, mediaType: 'video' | 'image', options: {
|
|
49
|
+
logger?: ILogger;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
thumbnail: string | undefined;
|
|
52
|
+
originalImageDimensions: {
|
|
53
|
+
width: number;
|
|
54
|
+
height: number;
|
|
55
|
+
} | undefined;
|
|
56
|
+
}>;
|
|
57
|
+
export declare const getHttpStream: (url: string | URL, options?: RequestInit & {
|
|
58
|
+
isStream?: true;
|
|
59
|
+
}) => Promise<Readable>;
|
|
60
|
+
export type MediaDownloadOptions = {
|
|
61
|
+
options?: RequestInit;
|
|
62
|
+
};
|
|
63
|
+
export declare const DEF_MEDIA_HOST = "mmg.whatsapp.net";
|
|
64
|
+
export declare const getUrlFromDirectPath: (directPath: string, host?: string) => string;
|
|
65
|
+
export declare function extensionForMediaMessage(message: WAMessageContent): string;
|
|
66
|
+
export declare const encryptMediaRetryRequest: (key: WAMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => BinaryNode;
|
|
67
|
+
export declare const getStatusCodeForMediaRetry: (code: number) => 200 | 412 | 404 | 418;
|
|
68
|
+
export declare const decodeMediaRetryNode: (node: BinaryNode) => BaileysEventMap['messages.media-update'][number];
|
|
69
|
+
export declare const decryptMediaRetryData: ({ ciphertext, iv }: {
|
|
70
|
+
ciphertext: Uint8Array;
|
|
71
|
+
iv: Uint8Array;
|
|
72
|
+
}, mediaKey: Uint8Array, msgId: string) => proto.MediaRetryNotification;
|
|
73
|
+
//# sourceMappingURL=messages-media-core.d.ts.map
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { BufferRuntime } from '../Runtime/buffer.js';
|
|
2
|
+
import { MEDIA_HKDF_KEY_MAPPING } from '../Defaults/index.js';
|
|
3
|
+
import { proto } from '../WAProto/runtime.js';
|
|
4
|
+
import { getBinaryNodeChild, getBinaryNodeChildBuffer } from '../WABinary/generic-utils.js';
|
|
5
|
+
import { jidNormalizedUser } from '../WABinary/jid-utils.js';
|
|
6
|
+
import { Boom } from './boom.js';
|
|
7
|
+
import { base64Encode, isBytes, randomBytes } from '../Runtime/bytes.js';
|
|
8
|
+
import { runtimeJoinPath } from '../Runtime/paths.js';
|
|
9
|
+
import { createReadable, isReadable, readableFromWeb } from '../Runtime/stream.js';
|
|
10
|
+
import { nodeMedia } from '../Runtime/node-media.js';
|
|
11
|
+
const randomId = () => globalThis.crypto.randomUUID();
|
|
12
|
+
const isNodeReadable = (value) => isReadable(value);
|
|
13
|
+
const getTmpFilesDirectory = () => nodeMedia.tempDir();
|
|
14
|
+
const getImageProcessingLibrary = async () => (await nodeMedia.getImageProcessingLibrary());
|
|
15
|
+
export const hkdfInfoKey = (type) => `WhatsApp ${MEDIA_HKDF_KEY_MAPPING[type]} Keys`;
|
|
16
|
+
export async function getMediaKeys(buffer, mediaType) {
|
|
17
|
+
if (!buffer)
|
|
18
|
+
throw new Boom('Cannot derive from empty media key');
|
|
19
|
+
if (typeof buffer === 'string')
|
|
20
|
+
buffer = BufferRuntime.from(buffer.replace('data:;base64,', ''), 'base64');
|
|
21
|
+
const expandedMediaKey = nodeMedia.hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
|
|
22
|
+
return {
|
|
23
|
+
iv: expandedMediaKey.slice(0, 16),
|
|
24
|
+
cipherKey: expandedMediaKey.slice(16, 48),
|
|
25
|
+
macKey: expandedMediaKey.slice(48, 80)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Extracts video thumb using FFMPEG */
|
|
29
|
+
const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
|
|
30
|
+
nodeMedia.execFile('ffmpeg', ['-ss', time, '-i', path, '-y', '-vf', `scale=${size.width}:-1`, '-vframes', '1', '-f', 'image2', destPath], (err) => {
|
|
31
|
+
if (err) {
|
|
32
|
+
reject(err);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
resolve();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
40
|
+
// TODO: Move entirely to sharp, removing jimp as it supports readable streams
|
|
41
|
+
// This will have positive speed and performance impacts as well as minimizing RAM usage.
|
|
42
|
+
if (isNodeReadable(bufferOrFilePath)) {
|
|
43
|
+
bufferOrFilePath = await toBuffer(bufferOrFilePath);
|
|
44
|
+
}
|
|
45
|
+
const lib = await getImageProcessingLibrary();
|
|
46
|
+
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
|
|
47
|
+
const img = lib.sharp.default(bufferOrFilePath);
|
|
48
|
+
const dimensions = await img.metadata();
|
|
49
|
+
const buffer = await img.resize(width).jpeg({ quality: 50 }).toBuffer();
|
|
50
|
+
return {
|
|
51
|
+
buffer,
|
|
52
|
+
original: {
|
|
53
|
+
width: dimensions.width,
|
|
54
|
+
height: dimensions.height
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
|
|
59
|
+
const JimpClass = lib.jimp.Jimp;
|
|
60
|
+
const jimp = await JimpClass.read(bufferOrFilePath);
|
|
61
|
+
const dimensions = {
|
|
62
|
+
width: jimp.width,
|
|
63
|
+
height: jimp.height
|
|
64
|
+
};
|
|
65
|
+
const buffer = await jimp
|
|
66
|
+
.resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR })
|
|
67
|
+
.getBuffer('image/jpeg', { quality: 50 });
|
|
68
|
+
return {
|
|
69
|
+
buffer,
|
|
70
|
+
original: dimensions
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new Boom('No image processing library available');
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''));
|
|
78
|
+
export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
79
|
+
const buffer = isBytes(mediaUpload) ? mediaUpload : await toBuffer((await getStream(mediaUpload)).stream);
|
|
80
|
+
const { width = 640, height = 640 } = dimensions || {};
|
|
81
|
+
const lib = await getImageProcessingLibrary();
|
|
82
|
+
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
|
|
83
|
+
return { img: await lib.sharp.default(buffer).resize(width, height).jpeg({ quality: 50 }).toBuffer() };
|
|
84
|
+
}
|
|
85
|
+
if ('jimp' in lib && typeof lib.jimp?.Jimp === 'function') {
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
const jimp = await lib.jimp.Jimp.read(buffer);
|
|
88
|
+
const min = Math.min(jimp.width, jimp.height);
|
|
89
|
+
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min });
|
|
90
|
+
return {
|
|
91
|
+
img: await cropped
|
|
92
|
+
.resize({ w: width, h: height, mode: lib.jimp.ResizeStrategy.BILINEAR })
|
|
93
|
+
.getBuffer('image/jpeg', { quality: 50 })
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
throw new Boom('No image processing library available');
|
|
97
|
+
};
|
|
98
|
+
export const mediaMessageSHA256B64 = (message) => {
|
|
99
|
+
const media = Object.values(message)[0];
|
|
100
|
+
return media?.fileSha256 && base64Encode(media.fileSha256);
|
|
101
|
+
};
|
|
102
|
+
/** Returns audio duration when the selected runtime provides a metadata processor. */
|
|
103
|
+
export const getAudioDuration = (buffer) => nodeMedia.getAudioDuration(buffer);
|
|
104
|
+
/**
|
|
105
|
+
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
106
|
+
*/
|
|
107
|
+
export async function getAudioWaveform(buffer, logger) {
|
|
108
|
+
try {
|
|
109
|
+
let audioData;
|
|
110
|
+
if (isBytes(buffer)) {
|
|
111
|
+
audioData = buffer;
|
|
112
|
+
}
|
|
113
|
+
else if (typeof buffer === 'string') {
|
|
114
|
+
const rStream = nodeMedia.createReadStream(buffer);
|
|
115
|
+
audioData = (await toBuffer(rStream));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
audioData = (await toBuffer(buffer));
|
|
119
|
+
}
|
|
120
|
+
const audioBuffer = await nodeMedia.decodeAudio(audioData);
|
|
121
|
+
const rawData = audioBuffer.getChannelData(0); // We only need to work with one channel of data
|
|
122
|
+
const samples = 64; // Number of samples we want to have in our final data set
|
|
123
|
+
const blockSize = Math.floor(rawData.length / samples); // the number of samples in each subdivision
|
|
124
|
+
const filteredData = [];
|
|
125
|
+
for (let i = 0; i < samples; i++) {
|
|
126
|
+
const blockStart = blockSize * i; // the location of the first sample in the block
|
|
127
|
+
let sum = 0;
|
|
128
|
+
for (let j = 0; j < blockSize; j++) {
|
|
129
|
+
sum = sum + Math.abs(rawData[blockStart + j]); // find the sum of all the samples in the block
|
|
130
|
+
}
|
|
131
|
+
filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
|
|
132
|
+
}
|
|
133
|
+
// This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
|
|
134
|
+
const multiplier = Math.pow(Math.max(...filteredData), -1);
|
|
135
|
+
const normalizedData = filteredData.map(n => n * multiplier);
|
|
136
|
+
// Generate waveform like WhatsApp
|
|
137
|
+
const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
|
|
138
|
+
return waveform;
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
logger?.debug('Failed to generate waveform: ' + e);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export const toReadable = (buffer) => {
|
|
145
|
+
const readable = createReadable();
|
|
146
|
+
readable.push(buffer);
|
|
147
|
+
readable.push(null);
|
|
148
|
+
return readable;
|
|
149
|
+
};
|
|
150
|
+
export const toBuffer = async (stream) => {
|
|
151
|
+
if (isBytes(stream))
|
|
152
|
+
return stream;
|
|
153
|
+
const chunks = [];
|
|
154
|
+
for await (const chunk of stream) {
|
|
155
|
+
chunks.push(chunk);
|
|
156
|
+
}
|
|
157
|
+
const destroy = stream.destroy;
|
|
158
|
+
if (typeof destroy === 'function')
|
|
159
|
+
destroy.call(stream);
|
|
160
|
+
return BufferRuntime.concat(chunks);
|
|
161
|
+
};
|
|
162
|
+
export const getStream = async (item, opts) => {
|
|
163
|
+
if (isBytes(item)) {
|
|
164
|
+
return { stream: isReadable(createReadable()) ? toReadable(item) : item, type: 'buffer' };
|
|
165
|
+
}
|
|
166
|
+
if ('stream' in item) {
|
|
167
|
+
return { stream: item.stream, type: 'readable' };
|
|
168
|
+
}
|
|
169
|
+
const urlStr = item.url.toString();
|
|
170
|
+
if (urlStr.startsWith('data:')) {
|
|
171
|
+
const buffer = BufferRuntime.from(urlStr.split(',')[1], 'base64');
|
|
172
|
+
return { stream: isReadable(createReadable()) ? toReadable(buffer) : buffer, type: 'buffer' };
|
|
173
|
+
}
|
|
174
|
+
if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
|
|
175
|
+
return { stream: await getHttpStream(item.url, opts), type: 'remote' };
|
|
176
|
+
}
|
|
177
|
+
return { stream: nodeMedia.createReadStream(item.url), type: 'file' };
|
|
178
|
+
};
|
|
179
|
+
/** generates a thumbnail for a given media, if required */
|
|
180
|
+
export async function generateThumbnail(bufferOrPath, mediaType, options) {
|
|
181
|
+
let thumbnail;
|
|
182
|
+
let originalImageDimensions;
|
|
183
|
+
if (mediaType === 'image') {
|
|
184
|
+
const { buffer, original } = await extractImageThumb(bufferOrPath);
|
|
185
|
+
thumbnail = buffer.toString('base64');
|
|
186
|
+
if (original.width && original.height) {
|
|
187
|
+
originalImageDimensions = {
|
|
188
|
+
width: original.width,
|
|
189
|
+
height: original.height
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else if (mediaType === 'video') {
|
|
194
|
+
// Video thumbnails need ffmpeg + file on disk
|
|
195
|
+
let filePath;
|
|
196
|
+
let needsCleanup = false;
|
|
197
|
+
if (isBytes(bufferOrPath)) {
|
|
198
|
+
filePath = runtimeJoinPath(getTmpFilesDirectory(), 'vid-' + randomId());
|
|
199
|
+
await nodeMedia.fs.writeFile(filePath, bufferOrPath);
|
|
200
|
+
needsCleanup = true;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
filePath = bufferOrPath;
|
|
204
|
+
}
|
|
205
|
+
const imgFilename = runtimeJoinPath(getTmpFilesDirectory(), randomId() + '.jpg');
|
|
206
|
+
try {
|
|
207
|
+
await extractVideoThumb(filePath, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
208
|
+
const buff = await nodeMedia.fs.readFile(imgFilename);
|
|
209
|
+
thumbnail = buff.toString('base64');
|
|
210
|
+
await nodeMedia.fs.unlink(imgFilename);
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
options.logger?.debug('could not generate video thumb: ' + err);
|
|
214
|
+
}
|
|
215
|
+
if (needsCleanup) {
|
|
216
|
+
try {
|
|
217
|
+
await nodeMedia.fs.unlink(filePath);
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
/* ignore */
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
thumbnail,
|
|
226
|
+
originalImageDimensions
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
export const getHttpStream = async (url, options = {}) => {
|
|
230
|
+
const response = await fetch(url.toString(), {
|
|
231
|
+
dispatcher: options.dispatcher,
|
|
232
|
+
method: 'GET',
|
|
233
|
+
headers: options.headers
|
|
234
|
+
});
|
|
235
|
+
if (!response.ok) {
|
|
236
|
+
throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
|
|
237
|
+
}
|
|
238
|
+
// @ts-ignore Node18+ Readable.fromWeb exists
|
|
239
|
+
return isNodeReadable(response.body)
|
|
240
|
+
? response.body
|
|
241
|
+
: readableFromWeb(response.body);
|
|
242
|
+
};
|
|
243
|
+
export const DEF_MEDIA_HOST = 'mmg.whatsapp.net';
|
|
244
|
+
export const getUrlFromDirectPath = (directPath, host = DEF_MEDIA_HOST) => `https://${host}${directPath}`;
|
|
245
|
+
export function extensionForMediaMessage(message) {
|
|
246
|
+
const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
|
|
247
|
+
const type = Object.keys(message)[0];
|
|
248
|
+
if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage')
|
|
249
|
+
return '.jpeg';
|
|
250
|
+
return getExtension(message[type].mimetype);
|
|
251
|
+
}
|
|
252
|
+
const getMediaRetryKey = (mediaKey) => nodeMedia.hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
|
|
253
|
+
export const encryptMediaRetryRequest = (key, mediaKey, meId) => {
|
|
254
|
+
const receiptBuffer = proto.ServerErrorReceipt.encode({ stanzaId: key.id }).finish();
|
|
255
|
+
const iv = randomBytes(12);
|
|
256
|
+
const ciphertext = nodeMedia.aesGcm256Encrypt(getMediaRetryKey(mediaKey), iv, new TextEncoder().encode(key.id), receiptBuffer);
|
|
257
|
+
const nodeCompatibleIv = BufferRuntime.from(iv);
|
|
258
|
+
const nodeCompatibleCiphertext = BufferRuntime.from(ciphertext);
|
|
259
|
+
return {
|
|
260
|
+
tag: 'receipt',
|
|
261
|
+
attrs: { id: key.id, to: jidNormalizedUser(meId), type: 'server-error' },
|
|
262
|
+
content: [
|
|
263
|
+
{
|
|
264
|
+
tag: 'encrypt',
|
|
265
|
+
attrs: {},
|
|
266
|
+
content: [
|
|
267
|
+
{ tag: 'enc_p', attrs: {}, content: nodeCompatibleCiphertext },
|
|
268
|
+
{ tag: 'enc_iv', attrs: {}, content: nodeCompatibleIv }
|
|
269
|
+
]
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
tag: 'rmr',
|
|
273
|
+
attrs: {
|
|
274
|
+
jid: key.remoteJid,
|
|
275
|
+
from_me: (!!key.fromMe).toString(),
|
|
276
|
+
...(key.participant ? { participant: key.participant } : {})
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
]
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
const MEDIA_RETRY_STATUS_MAP = {
|
|
283
|
+
[proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
284
|
+
[proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
285
|
+
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
286
|
+
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
|
|
287
|
+
};
|
|
288
|
+
export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
289
|
+
export const decodeMediaRetryNode = (node) => {
|
|
290
|
+
const retryNode = getBinaryNodeChild(node, 'rmr');
|
|
291
|
+
const event = {
|
|
292
|
+
key: {
|
|
293
|
+
id: node.attrs.id,
|
|
294
|
+
remoteJid: retryNode.attrs.jid,
|
|
295
|
+
fromMe: retryNode.attrs.from_me === 'true',
|
|
296
|
+
participant: retryNode.attrs.participant
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
const errorNode = getBinaryNodeChild(node, 'error');
|
|
300
|
+
if (errorNode) {
|
|
301
|
+
const errorCode = +errorNode.attrs.code;
|
|
302
|
+
event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
|
|
303
|
+
data: errorNode.attrs,
|
|
304
|
+
statusCode: getStatusCodeForMediaRetry(errorCode)
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
|
|
309
|
+
const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
|
|
310
|
+
const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
|
|
311
|
+
if (ciphertext && iv)
|
|
312
|
+
event.media = { ciphertext, iv };
|
|
313
|
+
else
|
|
314
|
+
event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
|
|
315
|
+
}
|
|
316
|
+
return event;
|
|
317
|
+
};
|
|
318
|
+
export const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => proto.MediaRetryNotification.decode(nodeMedia.aesGcm256Decrypt(getMediaRetryKey(mediaKey), iv, new TextEncoder().encode(msgId), ciphertext));
|
|
319
|
+
//# sourceMappingURL=messages-media-core.js.map
|
|
@@ -1,73 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { Readable } from 'node:stream';
|
|
3
|
-
import { type MediaType } from '../Defaults/index.js';
|
|
4
|
-
import type { BaileysEventMap, BinaryNode, MediaDecryptionKeyInfo, WAMediaUpload, WAMessageContent, WAMessageKey } from '../Types/index.js';
|
|
5
|
-
import { proto } from '../WAProto/runtime.js';
|
|
6
|
-
import type { ILogger } from './logger.js';
|
|
7
|
-
export declare const hkdfInfoKey: (type: MediaType) => string;
|
|
8
|
-
export declare function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): Promise<MediaDecryptionKeyInfo>;
|
|
9
|
-
export declare const extractImageThumb: (bufferOrFilePath: Readable | Buffer | string, width?: number) => Promise<{
|
|
10
|
-
buffer: any;
|
|
11
|
-
original: {
|
|
12
|
-
width: any;
|
|
13
|
-
height: any;
|
|
14
|
-
};
|
|
15
|
-
}>;
|
|
16
|
-
export declare const encodeBase64EncodedStringForUpload: (b64: string) => string;
|
|
17
|
-
export declare const generateProfilePicture: (mediaUpload: WAMediaUpload, dimensions?: {
|
|
18
|
-
width: number;
|
|
19
|
-
height: number;
|
|
20
|
-
}) => Promise<{
|
|
21
|
-
img: Buffer;
|
|
22
|
-
}>;
|
|
23
|
-
export declare const mediaMessageSHA256B64: (message: WAMessageContent) => string | null | undefined;
|
|
24
|
-
/** Returns audio duration in seconds, parsed via `music-metadata`. */
|
|
25
|
-
export declare function getAudioDuration(buffer: Buffer | string | Readable): Promise<number | undefined>;
|
|
26
|
-
/**
|
|
27
|
-
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
28
|
-
*/
|
|
29
|
-
export declare function getAudioWaveform(buffer: Buffer | string | Readable, logger?: ILogger): Promise<Uint8Array<ArrayBuffer> | undefined>;
|
|
30
|
-
export declare const toReadable: (buffer: Buffer) => Readable;
|
|
31
|
-
export declare const toBuffer: (stream: Readable) => Promise<Buffer<ArrayBuffer>>;
|
|
32
|
-
export declare const getStream: (item: WAMediaUpload, opts?: RequestInit & {
|
|
33
|
-
maxContentLength?: number;
|
|
34
|
-
}) => Promise<{
|
|
35
|
-
readonly stream: Readable;
|
|
36
|
-
readonly type: 'buffer';
|
|
37
|
-
} | {
|
|
38
|
-
readonly stream: Readable;
|
|
39
|
-
readonly type: 'readable';
|
|
40
|
-
} | {
|
|
41
|
-
readonly stream: Readable;
|
|
42
|
-
readonly type: 'remote';
|
|
43
|
-
} | {
|
|
44
|
-
readonly stream: import("node:fs").ReadStream;
|
|
45
|
-
readonly type: 'file';
|
|
46
|
-
}>;
|
|
47
|
-
/** generates a thumbnail for a given media, if required */
|
|
48
|
-
export declare function generateThumbnail(bufferOrPath: Buffer | string, mediaType: 'video' | 'image', options: {
|
|
49
|
-
logger?: ILogger;
|
|
50
|
-
}): Promise<{
|
|
51
|
-
thumbnail: string | undefined;
|
|
52
|
-
originalImageDimensions: {
|
|
53
|
-
width: number;
|
|
54
|
-
height: number;
|
|
55
|
-
} | undefined;
|
|
56
|
-
}>;
|
|
57
|
-
export declare const getHttpStream: (url: string | URL, options?: RequestInit & {
|
|
58
|
-
isStream?: true;
|
|
59
|
-
}) => Promise<Readable>;
|
|
60
|
-
export type MediaDownloadOptions = {
|
|
61
|
-
options?: RequestInit;
|
|
62
|
-
};
|
|
63
|
-
export declare const DEF_MEDIA_HOST = "mmg.whatsapp.net";
|
|
64
|
-
export declare const getUrlFromDirectPath: (directPath: string, host?: string) => string;
|
|
65
|
-
export declare function extensionForMediaMessage(message: WAMessageContent): string;
|
|
66
|
-
export declare const encryptMediaRetryRequest: (key: WAMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => BinaryNode;
|
|
67
|
-
export declare const getStatusCodeForMediaRetry: (code: number) => 200 | 412 | 404 | 418;
|
|
68
|
-
export declare const decodeMediaRetryNode: (node: BinaryNode) => BaileysEventMap['messages.media-update'][number];
|
|
69
|
-
export declare const decryptMediaRetryData: ({ ciphertext, iv }: {
|
|
70
|
-
ciphertext: Uint8Array;
|
|
71
|
-
iv: Uint8Array;
|
|
72
|
-
}, mediaKey: Uint8Array, msgId: string) => proto.MediaRetryNotification;
|
|
1
|
+
export * from './messages-media-core.js';
|
|
73
2
|
//# sourceMappingURL=messages-media.d.ts.map
|