@chainfuse/helpers 0.5.0 → 0.7.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/discord.d.mts +3 -1
- package/dist/discord.mjs +24 -1
- package/dist/dns.d.mts +74 -0
- package/dist/dns.mjs +46 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +4 -4
package/dist/discord.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CustomLoging } from '@chainfuse/types';
|
|
2
2
|
import type { ExecutionContext } from '@cloudflare/workers-types/experimental';
|
|
3
|
-
import { REST, type RESTOptions } from '@discordjs/rest';
|
|
3
|
+
import { CDN, REST, type RESTOptions } from '@discordjs/rest';
|
|
4
4
|
export declare class DiscordHelpers {
|
|
5
5
|
/**
|
|
6
6
|
* Discord Epoch, the first second of 2015 or 1420070400000
|
|
@@ -18,4 +18,6 @@ export declare class DiscordHelpers {
|
|
|
18
18
|
*/
|
|
19
19
|
static discordSnowflakeToDate(snowflakeRaw?: bigint | string): Date;
|
|
20
20
|
static discordRest(apiKey: string, cacheTtl?: number, forceCache?: boolean, executionContext?: ExecutionContext, logger?: CustomLoging, restOptions?: Partial<Omit<RESTOptions, 'agent' | 'authPrefix' | 'makeRequest'>>): REST;
|
|
21
|
+
static userIcon(userId: bigint | string, userIconHash?: Parameters<CDN['avatar']>[1] | null, guildId?: bigint | string, memberIconHash?: Parameters<CDN['avatar']>[1] | null, options?: Parameters<CDN['avatar']>[2]): string;
|
|
22
|
+
static guildIcon(guildId: bigint | string, iconHash: Parameters<CDN['icon']>[1], options?: Parameters<CDN['icon']>[2]): string;
|
|
21
23
|
}
|
package/dist/discord.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { REST } from '@discordjs/rest';
|
|
1
|
+
import { CDN, REST } from '@discordjs/rest';
|
|
2
2
|
import { CryptoHelpers } from './crypto.mjs';
|
|
3
3
|
import { NetHelpers } from './net.mjs';
|
|
4
4
|
export class DiscordHelpers {
|
|
@@ -182,4 +182,27 @@ export class DiscordHelpers {
|
|
|
182
182
|
},
|
|
183
183
|
}).setToken(apiKey);
|
|
184
184
|
}
|
|
185
|
+
static userIcon(userId, userIconHash, guildId, memberIconHash, options) {
|
|
186
|
+
options = {
|
|
187
|
+
extension: (memberIconHash ?? userIconHash)?.startsWith('a_') ? 'gif' : 'png',
|
|
188
|
+
...options,
|
|
189
|
+
};
|
|
190
|
+
if (userIconHash) {
|
|
191
|
+
if (guildId && memberIconHash) {
|
|
192
|
+
return new CDN().guildMemberAvatar(BigInt(guildId).toString(), BigInt(userId).toString(), memberIconHash, options);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return new CDN().avatar(BigInt(userId).toString(), userIconHash, options);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
return new CDN().defaultAvatar(Number((BigInt(userId) >> BigInt(22)) % BigInt(6)));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
static guildIcon(guildId, iconHash, options) {
|
|
203
|
+
return new CDN().icon(BigInt(guildId).toString(), iconHash, {
|
|
204
|
+
extension: iconHash.startsWith('a_') ? 'gif' : 'png',
|
|
205
|
+
...options,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
185
208
|
}
|
package/dist/dns.d.mts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported parameters for Cloudflare's 1.1.1.1 DNS over HTTPS API
|
|
3
|
+
*/
|
|
4
|
+
export interface DohBodyRequest {
|
|
5
|
+
/** Query name (required) */
|
|
6
|
+
name: string;
|
|
7
|
+
/** Query type - either a numeric value or text. Default is 'A' */
|
|
8
|
+
type?: string | number;
|
|
9
|
+
/**
|
|
10
|
+
* DO bit - whether the client wants DNSSEC data.
|
|
11
|
+
* Either empty or one of `0`, `false`, `1`, or `true`. Default is `false`
|
|
12
|
+
*/
|
|
13
|
+
do?: string | number | boolean;
|
|
14
|
+
/**
|
|
15
|
+
* CD bit - disable validation.
|
|
16
|
+
* Either empty or one of `0`, `false`, `1`, or `true`. Default is `false`
|
|
17
|
+
*/
|
|
18
|
+
cd?: string | number | boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A record structure with name, type, TTL, and data.
|
|
22
|
+
*/
|
|
23
|
+
interface Record {
|
|
24
|
+
/** The record owner */
|
|
25
|
+
name: string;
|
|
26
|
+
/** The type of DNS record. Defined here: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 */
|
|
27
|
+
type: number;
|
|
28
|
+
/** The number of seconds the answer can be stored in cache before it is considered stale */
|
|
29
|
+
TTL: number;
|
|
30
|
+
/** The value of the DNS record for the given name and type. The data will be in text for standardized record types and in hex for unknown types */
|
|
31
|
+
data: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A successful DNS response from Cloudflare's 1.1.1.1 DNS over HTTPS API
|
|
35
|
+
*/
|
|
36
|
+
export interface DohSuccessfulResponse {
|
|
37
|
+
/** The Response Code of the DNS Query. Defined here: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */
|
|
38
|
+
Status: number;
|
|
39
|
+
/** True if the truncated bit was set. This happens when the DNS answer is larger than a single UDP or TCP packet */
|
|
40
|
+
TC: boolean;
|
|
41
|
+
/** True if the Recursive Desired bit was set. This is always set to true for Cloudflare DNS over HTTPS */
|
|
42
|
+
RD: boolean;
|
|
43
|
+
/** True if the Recursion Available bit was set. This is always set to true for Cloudflare DNS over HTTPS */
|
|
44
|
+
RA: boolean;
|
|
45
|
+
/** True if every record in the answer was verified with DNSSEC */
|
|
46
|
+
AD: boolean;
|
|
47
|
+
/** True if the client asked to disable DNSSEC validation. In this case, Cloudflare will still fetch the DNSSEC-related records, but it will not attempt to validate the records */
|
|
48
|
+
CD: boolean;
|
|
49
|
+
/** The record name requested */
|
|
50
|
+
Question: Record[];
|
|
51
|
+
/** The answer record */
|
|
52
|
+
Answer?: Record[];
|
|
53
|
+
/** The authority record */
|
|
54
|
+
Authority: Record[];
|
|
55
|
+
/** The additional record */
|
|
56
|
+
Additional: Record[];
|
|
57
|
+
/** List of EDE messages. Refer to Extended DNS error codes for more information */
|
|
58
|
+
Comment?: string[];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* An error response from Cloudflare's 1.1.1.1 DNS over HTTPS API
|
|
62
|
+
*/
|
|
63
|
+
export interface DohErrorResponse {
|
|
64
|
+
/** An explanation of the error that occurred */
|
|
65
|
+
error: string;
|
|
66
|
+
}
|
|
67
|
+
export declare class DnsHelpers {
|
|
68
|
+
private nameserver_url;
|
|
69
|
+
constructor(nameserver_url: string | URL);
|
|
70
|
+
query(qName: string, qType?: string | number, qDo?: string | number | boolean, qCd?: string | number | boolean, timeout?: number): Promise<DohSuccessfulResponse | DohErrorResponse>;
|
|
71
|
+
private makeGetQuery;
|
|
72
|
+
private sendDohMsg;
|
|
73
|
+
}
|
|
74
|
+
export {};
|
package/dist/dns.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class DnsHelpers {
|
|
2
|
+
nameserver_url;
|
|
3
|
+
constructor(nameserver_url) {
|
|
4
|
+
this.nameserver_url = new URL(nameserver_url);
|
|
5
|
+
}
|
|
6
|
+
query(qName, qType = 'A', qDo = false, qCd = false, timeout = 10 * 1000) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
this.sendDohMsg(timeout, this.makeGetQuery(this.nameserver_url, qName, qType, qDo, qCd))
|
|
9
|
+
.then((response) => {
|
|
10
|
+
if (response.ok) {
|
|
11
|
+
response.json().then(resolve).catch(reject);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
15
|
+
reject(response.status);
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
.catch(reject);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
makeGetQuery(url, qName, qType = 'A', qDo = false, qCd = false) {
|
|
22
|
+
url.searchParams.set('name', qName);
|
|
23
|
+
url.searchParams.set('type', qType.toString());
|
|
24
|
+
url.searchParams.set('do', qDo.toString());
|
|
25
|
+
url.searchParams.set('cd', qCd.toString());
|
|
26
|
+
return url;
|
|
27
|
+
}
|
|
28
|
+
async sendDohMsg(timeout = 10 * 1000, url = this.nameserver_url) {
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
31
|
+
const response = await fetch(url, {
|
|
32
|
+
method: 'GET',
|
|
33
|
+
headers: {
|
|
34
|
+
Accept: 'application/dns-json',
|
|
35
|
+
},
|
|
36
|
+
signal: controller.signal,
|
|
37
|
+
});
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
if (response.ok || response.status === 304) {
|
|
40
|
+
return response;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
|
|
@@ -51,11 +51,11 @@
|
|
|
51
51
|
"@discordjs/rest": "^2.4.2",
|
|
52
52
|
"chalk": "^5.4.1",
|
|
53
53
|
"cloudflare": "^3.5.0",
|
|
54
|
-
"uuid": "^11.0.
|
|
54
|
+
"uuid": "^11.0.5"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@chainfuse/types": "^1.4.
|
|
57
|
+
"@chainfuse/types": "^1.4.2",
|
|
58
58
|
"@types/node": "^22.10.5"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "abed5b9d3a63bfe419045c38f7e1402de09f11a6"
|
|
61
61
|
}
|