discord-message-transcript 1.2.0-dev.1.2.0.10.0 → 1.2.0-dev.1.2.0.12.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.
@@ -1,51 +1,56 @@
1
- import { CustomError } from "discord-message-transcript-base";
2
1
  import https from 'https';
3
- import { CustomWarn } from "../../../discord-message-transcript-base/src/core/customMessages.js";
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 request = https.get(url, async (response) => {
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
- CustomWarn(`Failed to receive a valid content-type from ${url}.\nUsing default url instead uploading to CDN`);
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
- if (!cdnOptions.includeAudio && isAudio) {
21
- return resolve(url);
22
- }
23
- const isImage = contentType.startsWith('image/');
24
- if (!cdnOptions.includeImage && isImage) {
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
- return reject(new CustomError(`Error fetching image from ${url}: ${err.message}`));
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_R2": {
50
+ return await cdnCloudflareR2();
51
+ }
52
+ }
53
+ }
54
+ async function cdnCloudflareR2() {
55
+ return ''; // TODO: Implement cloudflareR2 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 { CustomError } from 'discord-message-transcript-base';
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 request = https.get(url, (response) => {
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
- reject(new CustomError(`Failed to fetch image with status code: ${response.statusCode} from ${url}`));
8
- return;
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
- reject(new CustomError(`URL did not point to an image. Content-Type: ${contentType} from ${url}`));
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
- reject(new CustomError(`Error fetching image from ${url}: ${err.message}`));
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
  });
@@ -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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { CreateTranscriptOptions, ConvertTranscriptOptions, TranscriptOptions, ReturnType, CDNOptions, CDNType, MimeType } from "./types/types.js";
1
+ export { CreateTranscriptOptions, ConvertTranscriptOptions, TranscriptOptions, ReturnType, CDNOptions, MimeType } from "./types/types.js";
2
2
  export { ReturnFormat, LocalDate, TimeZone } from "discord-message-transcript-base";
3
3
  import { TextBasedChannel } from "discord.js";
4
4
  import { ConvertTranscriptOptions, CreateTranscriptOptions, OutputType, ReturnType } from "./types/types.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { ReturnType, CDNType } from "./types/types.js";
1
+ export { ReturnType } from "./types/types.js";
2
2
  export { ReturnFormat } from "discord-message-transcript-base";
3
3
  import { AttachmentBuilder } from "discord.js";
4
4
  import { Json } from "./renderers/json/json.js";
@@ -139,11 +139,6 @@ export interface MapMentions {
139
139
  roles: Map<string, JsonMessageMentionsRoles>;
140
140
  users: Map<string, JsonMessageMentionsUsers>;
141
141
  }
142
- export declare const CDNType: {
143
- readonly CUSTOM: "CUSTOM";
144
- readonly CLOUDFLARE: "CLOUDFLARE";
145
- };
146
- export type CDNType = typeof CDNType[keyof typeof CDNType];
147
142
  export type MimeType = `${string}/${string}`;
148
143
  export type CDNOptions<Other> = (Partial<{
149
144
  includeAudio: boolean;
@@ -151,9 +146,14 @@ export type CDNOptions<Other> = (Partial<{
151
146
  includeVideo: boolean;
152
147
  includeOthers: boolean;
153
148
  }>) & ({
154
- type: Exclude<CDNType, "CUSTOM">;
155
- } | {
156
149
  type: "CUSTOM";
157
150
  customCdnResolver: (url: string, contentType: MimeType | null, other: Other) => Promise<string> | string;
158
151
  other: Other;
152
+ } | {
153
+ type: "CLOUDFLARE_R2";
154
+ accountId: string;
155
+ accessKey: string;
156
+ secretKey: string;
157
+ bucket: string;
158
+ publicUrl: string;
159
159
  });
@@ -5,7 +5,3 @@ export const ReturnType = {
5
5
  String: "string",
6
6
  Uploadable: "uploadable"
7
7
  };
8
- export const CDNType = {
9
- CUSTOM: "CUSTOM",
10
- CLOUDFLARE: "CLOUDFLARE",
11
- };
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.10.0",
3
+ "version": "1.2.0-dev.1.2.0.12.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.10.0"
51
+ "discord-message-transcript-base": "1.2.0-dev.1.2.0.12.0"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "discord.js": ">=14.19.0 <15"