discord-message-transcript 1.2.0-dev.1.2.0.10.0 → 1.2.0-dev.1.2.0.11.0
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/dist/core/cdnResolver.js
CHANGED
|
@@ -1,51 +1,56 @@
|
|
|
1
|
-
import { CustomError } from "discord-message-transcript-base";
|
|
2
1
|
import https from 'https';
|
|
3
|
-
import
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import { CustomWarn } from "discord-message-transcript-base";
|
|
4
4
|
export async function cdnResolver(url, cdnOptions) {
|
|
5
5
|
return new Promise((resolve, reject) => {
|
|
6
|
-
const
|
|
6
|
+
const client = url.startsWith('https') ? https : http;
|
|
7
|
+
const request = client.get(url, { headers: { "User-Agent": "discord-message-transcript" } }, async (response) => {
|
|
7
8
|
if (response.statusCode !== 200) {
|
|
8
|
-
CustomWarn(`Failed to fetch attachment with status code: ${response.statusCode} from ${url}.\nUsing default url instead uploading to CDN`);
|
|
9
9
|
response.destroy();
|
|
10
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of uploading to CDN.\nFailed to fetch attachment with status code: ${response.statusCode} from ${url}.`);
|
|
10
11
|
return resolve(url);
|
|
11
12
|
}
|
|
12
13
|
const contentType = response.headers["content-type"];
|
|
13
|
-
response.destroy();
|
|
14
14
|
const splitContentType = contentType ? contentType?.split('/') : [];
|
|
15
15
|
if (!contentType || splitContentType.length != 2 || splitContentType[0].length == 0 || splitContentType[1].length == 0) {
|
|
16
|
-
|
|
16
|
+
response.destroy();
|
|
17
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of uploading to CDN.\nFailed to receive a valid content-type from ${url}.`);
|
|
17
18
|
return resolve(url);
|
|
18
19
|
}
|
|
20
|
+
response.destroy();
|
|
21
|
+
const isImage = contentType.startsWith('image/') && contentType !== 'image/gif';
|
|
19
22
|
const isAudio = contentType.startsWith('audio/');
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return resolve(url);
|
|
26
|
-
}
|
|
27
|
-
const isVideo = contentType.startsWith('video/');
|
|
28
|
-
if (!cdnOptions.includeVideo && isVideo) {
|
|
29
|
-
return resolve(url);
|
|
30
|
-
}
|
|
31
|
-
if (!cdnOptions.includeOthers && !isAudio && !isImage && !isVideo) {
|
|
32
|
-
return resolve(url);
|
|
33
|
-
}
|
|
34
|
-
switch (cdnOptions.type) {
|
|
35
|
-
case "CLOUDFLARE": {
|
|
36
|
-
// TODO: Implement cloudflare cdn code
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
case "CUSTOM": {
|
|
40
|
-
return resolve(await cdnOptions.customCdnResolver(url, contentType, cdnOptions.other));
|
|
41
|
-
}
|
|
42
|
-
default: {
|
|
43
|
-
return reject(new CustomError("CDN type invalid!"));
|
|
44
|
-
}
|
|
23
|
+
const isVideo = contentType.startsWith('video/') || contentType === 'image/gif';
|
|
24
|
+
if ((cdnOptions.includeImage && isImage) ||
|
|
25
|
+
(cdnOptions.includeAudio && isAudio) ||
|
|
26
|
+
(cdnOptions.includeVideo && isVideo) ||
|
|
27
|
+
(cdnOptions.includeOthers && !isAudio && !isImage && !isVideo)) {
|
|
28
|
+
return resolve(await cdnRedirectType(url, contentType, cdnOptions));
|
|
45
29
|
}
|
|
30
|
+
return resolve(url);
|
|
46
31
|
});
|
|
47
32
|
request.on('error', (err) => {
|
|
48
|
-
|
|
33
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of uploading to CDN.\nError message: ${err.message}`);
|
|
34
|
+
return resolve(url);
|
|
49
35
|
});
|
|
36
|
+
request.setTimeout(15000, () => {
|
|
37
|
+
request.destroy();
|
|
38
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of uploading to CDN.\nRequest timeout for ${url}. Using original URL.`);
|
|
39
|
+
resolve(url);
|
|
40
|
+
});
|
|
41
|
+
request.end();
|
|
50
42
|
});
|
|
51
43
|
}
|
|
44
|
+
async function cdnRedirectType(url, contentType, cdnOptions) {
|
|
45
|
+
switch (cdnOptions.type) {
|
|
46
|
+
case "CUSTOM": {
|
|
47
|
+
return await cdnOptions.customCdnResolver(url, contentType, cdnOptions.other);
|
|
48
|
+
}
|
|
49
|
+
case "CLOUDFLARE": {
|
|
50
|
+
return await cdnCloudflare();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function cdnCloudflare() {
|
|
55
|
+
return ''; // TODO: Implement cloudflare cdn
|
|
56
|
+
}
|
|
@@ -95,7 +95,7 @@ export async function componentsToJson(components, options, cdnOptions) {
|
|
|
95
95
|
disabled: component.accessory.disabled,
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
|
-
else {
|
|
98
|
+
else if (component.accessory.type === ComponentType.Thumbnail) {
|
|
99
99
|
accessoryJson = {
|
|
100
100
|
type: JsonComponentType.Thumbnail,
|
|
101
101
|
media: {
|
|
@@ -104,6 +104,8 @@ export async function componentsToJson(components, options, cdnOptions) {
|
|
|
104
104
|
spoiler: component.accessory.spoiler,
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
|
+
else
|
|
108
|
+
return null;
|
|
107
109
|
const sectionComponents = component.components.map(c => ({
|
|
108
110
|
type: JsonComponentType.TextDisplay,
|
|
109
111
|
content: c.content,
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import https from 'https';
|
|
2
|
-
import
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import { CustomWarn } from 'discord-message-transcript-base';
|
|
3
4
|
export async function imageToBase64(url) {
|
|
4
5
|
return new Promise((resolve, reject) => {
|
|
5
|
-
const
|
|
6
|
+
const client = url.startsWith('https') ? https : http;
|
|
7
|
+
const request = client.get(url, { headers: { "User-Agent": "discord-message-transcript" } }, (response) => {
|
|
6
8
|
if (response.statusCode !== 200) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
response.destroy();
|
|
10
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of converting to base64.\nFailed to fetch image with status code: ${response.statusCode} from ${url}.`);
|
|
11
|
+
return resolve(url);
|
|
9
12
|
}
|
|
10
13
|
const contentType = response.headers['content-type'];
|
|
11
|
-
if (!contentType || !contentType.startsWith('image/')) {
|
|
12
|
-
|
|
13
|
-
return;
|
|
14
|
+
if (!contentType || !contentType.startsWith('image/') || contentType === 'image/gif') {
|
|
15
|
+
response.destroy();
|
|
16
|
+
return resolve(url);
|
|
14
17
|
}
|
|
15
18
|
const chunks = [];
|
|
16
19
|
response.on('data', (chunk) => {
|
|
@@ -21,9 +24,19 @@ export async function imageToBase64(url) {
|
|
|
21
24
|
const base64 = buffer.toString('base64');
|
|
22
25
|
resolve(`data:${contentType};base64,${base64}`);
|
|
23
26
|
});
|
|
27
|
+
response.on('error', (err) => {
|
|
28
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of converting to base64.\nStream error while fetching from ${url}.\nError message: ${err.message}`);
|
|
29
|
+
resolve(url);
|
|
30
|
+
});
|
|
24
31
|
});
|
|
25
32
|
request.on('error', (err) => {
|
|
26
|
-
|
|
33
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of converting to base64.\nError fetching image from ${url}\nError message: ${err.message}`);
|
|
34
|
+
return resolve(url);
|
|
35
|
+
});
|
|
36
|
+
request.setTimeout(15000, () => {
|
|
37
|
+
request.destroy();
|
|
38
|
+
CustomWarn(`This is not an issue with the package. Using the original URL as fallback instead of converting to base64.\nRequest timeout for ${url}. Using original URL.`);
|
|
39
|
+
resolve(url);
|
|
27
40
|
});
|
|
28
41
|
request.end();
|
|
29
42
|
});
|
package/dist/core/urlResolver.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { cdnResolver } from "./cdnResolver.js";
|
|
2
2
|
import { imageToBase64 } from "./imageToBase64.js";
|
|
3
3
|
export async function urlResolver(url, options, cdnOptions) {
|
|
4
|
-
if (cdnOptions)
|
|
4
|
+
if (cdnOptions)
|
|
5
5
|
return await cdnResolver(url, cdnOptions);
|
|
6
|
-
|
|
7
|
-
if (options.saveImages) {
|
|
6
|
+
if (options.saveImages)
|
|
8
7
|
return await imageToBase64(url);
|
|
9
|
-
}
|
|
10
8
|
return url;
|
|
11
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "discord-message-transcript",
|
|
3
|
-
"version": "1.2.0-dev.1.2.0.
|
|
3
|
+
"version": "1.2.0-dev.1.2.0.11.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"typescript": "^5.9.3"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"discord-message-transcript-base": "1.2.0-dev.1.2.0.
|
|
51
|
+
"discord-message-transcript-base": "1.2.0-dev.1.2.0.11.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"discord.js": ">=14.19.0 <15"
|