@nexustechpro/baileys 1.1.9 → 2.0.2

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.
@@ -18,72 +18,82 @@ export const getUrlInfo = async (text, opts = {
18
18
  fetchOpts: { timeout: 3000 }
19
19
  }) => {
20
20
  try {
21
- // retries
22
- const retries = 0;
23
- const maxRetry = 5;
24
- const { getLinkPreview } = await import('link-preview-js');
25
- let previewLink = text;
21
+ // Fix 1: use let so retries can actually increment
22
+ let retries = 0
23
+ const maxRetry = 5
24
+ const { getLinkPreview } = await import('link-preview-js')
25
+ let previewLink = text
26
26
  if (!text.startsWith('https://') && !text.startsWith('http://')) {
27
- previewLink = 'https://' + previewLink;
27
+ previewLink = 'https://' + previewLink
28
28
  }
29
29
  const info = await getLinkPreview(previewLink, {
30
30
  ...opts.fetchOpts,
31
31
  followRedirects: 'follow',
32
32
  handleRedirects: (baseURL, forwardedURL) => {
33
- const urlObj = new URL(baseURL);
34
- const forwardedURLObj = new URL(forwardedURL);
35
- if (retries >= maxRetry) {
36
- return false;
37
- }
38
- if (forwardedURLObj.hostname === urlObj.hostname ||
33
+ const urlObj = new URL(baseURL)
34
+ const forwardedURLObj = new URL(forwardedURL)
35
+ if (retries >= maxRetry) return false
36
+ if (
37
+ forwardedURLObj.hostname === urlObj.hostname ||
39
38
  forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
40
- 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
41
- retries + 1;
42
- return true;
43
- }
44
- else {
45
- return false;
39
+ 'www.' + forwardedURLObj.hostname === urlObj.hostname
40
+ ) {
41
+ retries += 1 // Fix 1: actually increment
42
+ return true
46
43
  }
44
+ return false
47
45
  },
48
46
  headers: opts.fetchOpts?.headers
49
- });
47
+ })
48
+
50
49
  if (info && 'title' in info && info.title) {
51
- const [image] = info.images;
50
+ const [image] = info.images
52
51
  const urlInfo = {
53
52
  'canonical-url': info.url,
54
53
  'matched-text': text,
55
54
  title: info.title,
56
55
  description: info.description,
57
56
  originalThumbnailUrl: image
58
- };
57
+ }
58
+
59
59
  if (opts.uploadImage) {
60
60
  try {
61
61
  const { imageMessage } = await prepareWAMessageMedia({ image: { url: image } }, {
62
62
  upload: opts.uploadImage,
63
63
  mediaTypeOverride: 'thumbnail-link',
64
64
  options: opts.fetchOpts
65
- });
66
- urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
67
- urlInfo.highQualityThumbnail = imageMessage || undefined;
65
+ })
66
+ urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
67
+ ? Buffer.from(imageMessage.jpegThumbnail)
68
+ : undefined
69
+ urlInfo.highQualityThumbnail = imageMessage || undefined
68
70
  } catch (error) {
69
- opts.logger?.warn({ err: error.message, url: image }, 'failed to upload link preview thumbnail, continuing without it');
71
+ opts.logger?.warn({ err: error.message, url: image }, 'failed to upload link preview thumbnail, falling back to compressed')
72
+ // Fix 2: fallback to compressed thumbnail instead of giving up entirely
73
+ try {
74
+ urlInfo.jpegThumbnail = image
75
+ ? (await getCompressedJpegThumbnail(image, opts)).buffer
76
+ : undefined
77
+ } catch (fallbackErr) {
78
+ opts.logger?.debug({ err: fallbackErr.stack }, 'fallback thumbnail also failed')
79
+ }
70
80
  }
71
- }
72
- else {
81
+ } else {
73
82
  try {
74
- urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
75
- }
76
- catch (error) {
77
- opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
83
+ urlInfo.jpegThumbnail = image
84
+ ? (await getCompressedJpegThumbnail(image, opts)).buffer
85
+ : undefined
86
+ } catch (error) {
87
+ opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail')
78
88
  }
79
89
  }
80
- return urlInfo;
90
+
91
+ return urlInfo
81
92
  }
82
- }
83
- catch (error) {
93
+ } catch (error) {
84
94
  if (!error.message.includes('receive a valid')) {
85
- throw error;
95
+ throw error
86
96
  }
87
97
  }
88
- };
98
+ }
89
99
  //# sourceMappingURL=link-preview.js.map