@fluxerjs/util 1.3.1 → 2.0.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/index.d.mts +139 -127
- package/dist/index.d.ts +139 -127
- package/dist/index.js +215 -171
- package/dist/index.mjs +213 -168
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,126 +1,157 @@
|
|
|
1
1
|
// src/BitField.ts
|
|
2
|
+
function invalidBit(bits) {
|
|
3
|
+
throw new RangeError(`Invalid bitfield flag or number: ${String(bits)}`);
|
|
4
|
+
}
|
|
2
5
|
var BitField = class _BitField {
|
|
3
6
|
static Flags = {};
|
|
4
|
-
|
|
7
|
+
static DefaultBit = 0n;
|
|
5
8
|
bitfield;
|
|
6
|
-
constructor(bits =
|
|
9
|
+
constructor(bits = this.constructor.DefaultBit) {
|
|
7
10
|
this.bitfield = this.constructor.resolve(bits);
|
|
8
11
|
}
|
|
9
12
|
get [Symbol.toStringTag]() {
|
|
10
|
-
return `BitField${this.bitfield}`;
|
|
13
|
+
return `BitField(${this.bitfield})`;
|
|
11
14
|
}
|
|
15
|
+
/** Resolve a resolvable into a single bigint bitfield. */
|
|
12
16
|
static resolve(bits) {
|
|
13
|
-
if (typeof bits === "
|
|
14
|
-
|
|
17
|
+
if (typeof bits === "bigint") {
|
|
18
|
+
if (bits < 0n) invalidBit(bits);
|
|
19
|
+
return bits;
|
|
20
|
+
}
|
|
21
|
+
if (typeof bits === "number") {
|
|
22
|
+
if (!Number.isInteger(bits) || bits < 0 || !Number.isSafeInteger(bits)) invalidBit(bits);
|
|
23
|
+
return BigInt(bits);
|
|
24
|
+
}
|
|
15
25
|
if (bits instanceof _BitField) return bits.bitfield;
|
|
16
|
-
if (Array.isArray(bits))
|
|
17
|
-
return bits.map((b) => this.resolve(b)).reduce((a, b) => BigInt(a) | BigInt(b), 0n);
|
|
18
26
|
if (typeof bits === "string") {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
const Flags = this.Flags;
|
|
28
|
+
if (Object.hasOwn(Flags, bits)) return Flags[bits];
|
|
29
|
+
if (/^(0|[1-9]\d*)$/.test(bits)) return BigInt(bits);
|
|
30
|
+
invalidBit(bits);
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(bits)) {
|
|
33
|
+
let acc = 0n;
|
|
34
|
+
for (const bit of bits) acc |= this.resolve(bit);
|
|
35
|
+
return acc;
|
|
27
36
|
}
|
|
28
|
-
|
|
37
|
+
invalidBit(bits);
|
|
38
|
+
}
|
|
39
|
+
get ctor() {
|
|
40
|
+
return this.constructor;
|
|
41
|
+
}
|
|
42
|
+
orAll(...bits) {
|
|
43
|
+
const resolve = this.ctor.resolve.bind(this.ctor);
|
|
44
|
+
let total = 0n;
|
|
45
|
+
for (const bit of bits) total |= resolve(bit);
|
|
46
|
+
return total;
|
|
29
47
|
}
|
|
48
|
+
/** Whether this bitfield contains all of the given bits. */
|
|
30
49
|
has(bit) {
|
|
31
|
-
|
|
32
|
-
return (this.bitfield &
|
|
50
|
+
const resolved = this.ctor.resolve(bit);
|
|
51
|
+
return (this.bitfield & resolved) === resolved;
|
|
52
|
+
}
|
|
53
|
+
/** Whether this bitfield contains any of the given bits. */
|
|
54
|
+
any(bit) {
|
|
55
|
+
return (this.bitfield & this.ctor.resolve(bit)) !== 0n;
|
|
56
|
+
}
|
|
57
|
+
/** Bits present in `bit` but missing from this bitfield. */
|
|
58
|
+
missing(bit) {
|
|
59
|
+
return new this.ctor(this.ctor.resolve(bit) & ~this.bitfield).toArray();
|
|
33
60
|
}
|
|
34
61
|
add(...bits) {
|
|
35
|
-
|
|
36
|
-
for (const bit of bits) {
|
|
37
|
-
total |= this.constructor.resolve(bit);
|
|
38
|
-
}
|
|
39
|
-
this.bitfield |= total;
|
|
62
|
+
this.bitfield |= this.orAll(...bits);
|
|
40
63
|
return this;
|
|
41
64
|
}
|
|
42
65
|
remove(...bits) {
|
|
43
|
-
|
|
44
|
-
for (const bit of bits) {
|
|
45
|
-
total |= this.constructor.resolve(bit);
|
|
46
|
-
}
|
|
47
|
-
this.bitfield &= ~total;
|
|
66
|
+
this.bitfield &= ~this.orAll(...bits);
|
|
48
67
|
return this;
|
|
49
68
|
}
|
|
50
69
|
serialize() {
|
|
51
|
-
const Flags = this.
|
|
52
|
-
const
|
|
53
|
-
for (const
|
|
54
|
-
|
|
70
|
+
const Flags = this.ctor.Flags;
|
|
71
|
+
const out = {};
|
|
72
|
+
for (const key of Object.keys(Flags)) {
|
|
73
|
+
out[key] = (this.bitfield & Flags[key]) === Flags[key];
|
|
55
74
|
}
|
|
56
|
-
return
|
|
75
|
+
return out;
|
|
57
76
|
}
|
|
58
77
|
toArray() {
|
|
59
|
-
const Flags = this.
|
|
60
|
-
|
|
78
|
+
const Flags = this.ctor.Flags;
|
|
79
|
+
const result = [];
|
|
80
|
+
for (const key of Object.keys(Flags)) {
|
|
81
|
+
if ((this.bitfield & Flags[key]) === Flags[key]) result.push(key);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
61
84
|
}
|
|
85
|
+
/** Decimal string of the bigint bitfield (API / JSON). */
|
|
62
86
|
toJSON() {
|
|
63
87
|
return this.bitfield.toString();
|
|
64
88
|
}
|
|
65
|
-
|
|
89
|
+
toString() {
|
|
66
90
|
return this.bitfield.toString();
|
|
67
91
|
}
|
|
68
|
-
|
|
69
|
-
return this.bitfield
|
|
92
|
+
valueOf() {
|
|
93
|
+
return this.bitfield;
|
|
94
|
+
}
|
|
95
|
+
equals(other) {
|
|
96
|
+
return this.bitfield === this.ctor.resolve(other);
|
|
70
97
|
}
|
|
71
98
|
freeze() {
|
|
72
99
|
return Object.freeze(this);
|
|
73
100
|
}
|
|
101
|
+
*[Symbol.iterator]() {
|
|
102
|
+
yield* this.toArray();
|
|
103
|
+
}
|
|
74
104
|
};
|
|
75
105
|
|
|
76
106
|
// src/SnowflakeUtil.ts
|
|
77
107
|
var FLUXER_EPOCH = 1420070400000n;
|
|
78
|
-
var
|
|
108
|
+
var SNOWFLAKE_RE = /^(0|[1-9]\d{0,19})$/;
|
|
109
|
+
function invalidSnowflake(value) {
|
|
110
|
+
throw new TypeError(
|
|
111
|
+
`Invalid snowflake "${value}": expected a non-negative decimal string (\u226420 digits, no leading zeros)`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
var SnowflakeUtil = class _SnowflakeUtil {
|
|
79
115
|
static EPOCH = FLUXER_EPOCH;
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
|
|
116
|
+
/** Whether `value` is a non-negative decimal snowflake string (≤ 20 digits). */
|
|
117
|
+
static isValid(snowflake) {
|
|
118
|
+
return typeof snowflake === "string" && SNOWFLAKE_RE.test(snowflake);
|
|
119
|
+
}
|
|
120
|
+
/** Parse a snowflake string to bigint. */
|
|
121
|
+
static parse(snowflake) {
|
|
122
|
+
if (!_SnowflakeUtil.isValid(snowflake)) invalidSnowflake(snowflake);
|
|
123
|
+
return BigInt(snowflake);
|
|
124
|
+
}
|
|
125
|
+
/** Unix timestamp (ms) from a snowflake. */
|
|
83
126
|
static timestampFromSnowflake(snowflake) {
|
|
84
|
-
return Number((
|
|
127
|
+
return Number((_SnowflakeUtil.parse(snowflake) >> 22n) + FLUXER_EPOCH);
|
|
85
128
|
}
|
|
86
|
-
/**
|
|
87
|
-
* Converts a snowflake to a Date.
|
|
88
|
-
*/
|
|
129
|
+
/** Date from a snowflake. */
|
|
89
130
|
static dateFromSnowflake(snowflake) {
|
|
90
|
-
return new Date(
|
|
131
|
+
return new Date(_SnowflakeUtil.timestampFromSnowflake(snowflake));
|
|
91
132
|
}
|
|
92
133
|
/**
|
|
93
|
-
*
|
|
94
|
-
* Useful for pagination (before/after).
|
|
134
|
+
* Snowflake string from a Unix timestamp (ms).
|
|
135
|
+
* Useful for pagination (`before` / `after`).
|
|
95
136
|
*/
|
|
96
137
|
static snowflakeFromTimestamp(timestamp) {
|
|
97
|
-
|
|
138
|
+
if (!Number.isFinite(timestamp)) {
|
|
139
|
+
throw new TypeError(`Invalid timestamp ${String(timestamp)}: must be a finite number`);
|
|
140
|
+
}
|
|
141
|
+
return (BigInt(Math.trunc(timestamp)) - FLUXER_EPOCH << 22n).toString();
|
|
98
142
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Deconstructs a snowflake into its components.
|
|
101
|
-
* @throws TypeError If snowflake is not a valid numeric string
|
|
102
|
-
*/
|
|
143
|
+
/** Deconstruct a snowflake into timestamp / worker / process / increment. */
|
|
103
144
|
static deconstruct(snowflake) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
big = BigInt(snowflake);
|
|
107
|
-
} catch {
|
|
108
|
-
throw new TypeError("Invalid snowflake: must be a numeric string");
|
|
109
|
-
}
|
|
145
|
+
const big = _SnowflakeUtil.parse(snowflake);
|
|
146
|
+
const timestamp = Number((big >> 22n) + FLUXER_EPOCH);
|
|
110
147
|
return {
|
|
111
|
-
timestamp
|
|
112
|
-
date: new Date(
|
|
148
|
+
timestamp,
|
|
149
|
+
date: new Date(timestamp),
|
|
113
150
|
workerId: Number(big >> 17n & 0x1fn),
|
|
114
151
|
processId: Number(big >> 12n & 0x1fn),
|
|
115
152
|
increment: Number(big & 0xfffn)
|
|
116
153
|
};
|
|
117
154
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Checks if a string is a valid snowflake format (numeric string, 0 or positive).
|
|
120
|
-
*/
|
|
121
|
-
static isValid(snowflake) {
|
|
122
|
-
return /^(0|[1-9]\d*)$/.test(snowflake) && snowflake.length <= 20;
|
|
123
|
-
}
|
|
124
155
|
};
|
|
125
156
|
|
|
126
157
|
// src/PermissionsBitField.ts
|
|
@@ -154,56 +185,47 @@ var PermissionFlags = {
|
|
|
154
185
|
ManageNicknames: 1n << 27n,
|
|
155
186
|
ManageRoles: 1n << 28n,
|
|
156
187
|
ManageWebhooks: 1n << 29n,
|
|
157
|
-
|
|
188
|
+
/** Manage emojis & stickers (expressions). */
|
|
158
189
|
ManageExpressions: 1n << 30n,
|
|
159
190
|
UseExternalStickers: 1n << 37n,
|
|
160
191
|
ModerateMembers: 1n << 40n,
|
|
161
192
|
CreateExpressions: 1n << 43n,
|
|
162
193
|
PinMessages: 1n << 51n,
|
|
163
194
|
BypassSlowmode: 1n << 52n,
|
|
164
|
-
UpdateRtcRegion: 1n << 53n
|
|
195
|
+
UpdateRtcRegion: 1n << 53n,
|
|
196
|
+
ViewChannelMembers: 1n << 54n
|
|
165
197
|
};
|
|
166
|
-
var ALL_PERMISSIONS_BIGINT = Object.values(PermissionFlags).reduce(
|
|
198
|
+
var ALL_PERMISSIONS_BIGINT = Object.values(PermissionFlags).reduce(
|
|
199
|
+
(a, b) => a | b,
|
|
200
|
+
0n
|
|
201
|
+
);
|
|
167
202
|
var PermissionsBitField = class extends BitField {
|
|
168
203
|
static Flags = PermissionFlags;
|
|
204
|
+
get isAdmin() {
|
|
205
|
+
return (this.bitfield & PermissionFlags.Administrator) !== 0n;
|
|
206
|
+
}
|
|
207
|
+
/** Administrator implies every permission. */
|
|
208
|
+
has(bit) {
|
|
209
|
+
return this.isAdmin || super.has(bit);
|
|
210
|
+
}
|
|
211
|
+
any(bit) {
|
|
212
|
+
return this.isAdmin || super.any(bit);
|
|
213
|
+
}
|
|
214
|
+
missing(bit) {
|
|
215
|
+
return this.isAdmin ? [] : super.missing(bit);
|
|
216
|
+
}
|
|
169
217
|
};
|
|
170
218
|
function resolvePermissionsToBitfield(perms) {
|
|
171
|
-
|
|
172
|
-
const num = Number(perms);
|
|
173
|
-
if (!Number.isNaN(num) && perms.trim() !== "") return perms;
|
|
174
|
-
const mapped = PermissionFlags[perms];
|
|
175
|
-
if (mapped !== void 0) return String(mapped);
|
|
176
|
-
throw new RangeError(`Invalid permission string: ${perms}`);
|
|
177
|
-
}
|
|
178
|
-
if (typeof perms === "number") return String(perms);
|
|
179
|
-
if (typeof perms === "bigint") return String(perms);
|
|
180
|
-
if (perms instanceof PermissionsBitField) return String(BigInt(perms.bitfield));
|
|
181
|
-
if (Array.isArray(perms)) {
|
|
182
|
-
let acc = 0n;
|
|
183
|
-
for (const p of perms) {
|
|
184
|
-
let v;
|
|
185
|
-
if (typeof p === "number") v = BigInt(p);
|
|
186
|
-
else if (typeof p === "string") {
|
|
187
|
-
const mapped = PermissionFlags[p];
|
|
188
|
-
v = mapped !== void 0 ? BigInt(mapped) : BigInt(Number(p) || 0);
|
|
189
|
-
} else v = BigInt(p.bitfield);
|
|
190
|
-
acc |= v;
|
|
191
|
-
}
|
|
192
|
-
return String(acc);
|
|
193
|
-
}
|
|
194
|
-
throw new RangeError(`Invalid permission resolvable: ${perms}`);
|
|
219
|
+
return PermissionsBitField.resolve(perms).toString();
|
|
195
220
|
}
|
|
196
221
|
|
|
197
222
|
// src/MessageFlagsBitField.ts
|
|
223
|
+
import { MessageFlags } from "@fluxerjs/types";
|
|
198
224
|
var MessageFlagsBits = {
|
|
199
|
-
SuppressEmbeds:
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
VoiceMessage: 1n << 13n,
|
|
204
|
-
// 8192
|
|
205
|
-
CompactAttachments: 1n << 17n
|
|
206
|
-
// 131072
|
|
225
|
+
SuppressEmbeds: BigInt(MessageFlags.SuppressEmbeds),
|
|
226
|
+
SuppressNotifications: BigInt(MessageFlags.SuppressNotifications),
|
|
227
|
+
VoiceMessage: BigInt(MessageFlags.VoiceMessage),
|
|
228
|
+
CompactAttachments: BigInt(MessageFlags.CompactAttachments)
|
|
207
229
|
};
|
|
208
230
|
var MessageFlagsBitField = class extends BitField {
|
|
209
231
|
static Flags = MessageFlagsBits;
|
|
@@ -249,44 +271,52 @@ var UserFlagsBitField = class extends BitField {
|
|
|
249
271
|
|
|
250
272
|
// src/formatters.ts
|
|
251
273
|
function formatColor(color) {
|
|
252
|
-
|
|
253
|
-
|
|
274
|
+
if (!Number.isInteger(color) || color < 0 || color > 16777215) {
|
|
275
|
+
throw new RangeError("Color must be an integer between 0 and 16777215");
|
|
276
|
+
}
|
|
277
|
+
return `#${color.toString(16).padStart(6, "0")}`;
|
|
254
278
|
}
|
|
255
279
|
function escapeMarkdown(text) {
|
|
256
280
|
return text.replace(/([\\*_`~|])/g, "\\$1");
|
|
257
281
|
}
|
|
258
|
-
function formatTimestamp(
|
|
259
|
-
const
|
|
260
|
-
return `<t:${
|
|
282
|
+
function formatTimestamp(timestampMs, style) {
|
|
283
|
+
const seconds = Math.floor(timestampMs / 1e3);
|
|
284
|
+
return style ? `<t:${seconds}:${style}>` : `<t:${seconds}>`;
|
|
261
285
|
}
|
|
262
286
|
function truncate(str, maxLength, suffix = "...") {
|
|
287
|
+
if (maxLength < 0) throw new RangeError("maxLength must be non-negative");
|
|
263
288
|
if (str.length <= maxLength) return str;
|
|
289
|
+
if (maxLength <= suffix.length) return suffix;
|
|
264
290
|
return str.slice(0, maxLength - suffix.length) + suffix;
|
|
265
291
|
}
|
|
266
292
|
|
|
267
293
|
// src/resolvers.ts
|
|
268
294
|
function resolveColor(color) {
|
|
269
295
|
if (typeof color === "number") {
|
|
270
|
-
if (color < 0 || color > 16777215)
|
|
296
|
+
if (!Number.isInteger(color) || color < 0 || color > 16777215) {
|
|
297
|
+
throw new RangeError("Color must be between 0 and 16777215");
|
|
298
|
+
}
|
|
271
299
|
return color;
|
|
272
300
|
}
|
|
273
301
|
if (Array.isArray(color)) {
|
|
274
302
|
const [r, g, b] = color;
|
|
303
|
+
if (!Number.isInteger(r) || !Number.isInteger(g) || !Number.isInteger(b) || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
|
|
304
|
+
throw new RangeError("RGB components must be integers 0\u2013255");
|
|
305
|
+
}
|
|
275
306
|
return r << 16 | g << 8 | b;
|
|
276
307
|
}
|
|
277
308
|
if (typeof color === "string") {
|
|
278
|
-
const hex = color.
|
|
309
|
+
const hex = color.startsWith("#") ? color.slice(1) : color;
|
|
279
310
|
if (!/^[0-9a-fA-F]{6}$/.test(hex)) throw new RangeError("Invalid hex color");
|
|
280
|
-
return parseInt(hex, 16);
|
|
311
|
+
return Number.parseInt(hex, 16);
|
|
281
312
|
}
|
|
282
313
|
throw new TypeError("Color must be a number, hex string, or RGB array");
|
|
283
314
|
}
|
|
284
315
|
function parseEmoji(emoji) {
|
|
285
|
-
if (
|
|
316
|
+
if (typeof emoji !== "string") return null;
|
|
286
317
|
const trimmed = emoji.trim();
|
|
287
318
|
if (trimmed.length === 0) return null;
|
|
288
|
-
const
|
|
289
|
-
const mentionMatch = trimmed.match(mention);
|
|
319
|
+
const mentionMatch = /^<(a?):(\w+):(\d+)>$/.exec(trimmed);
|
|
290
320
|
if (mentionMatch) {
|
|
291
321
|
return {
|
|
292
322
|
id: mentionMatch[3],
|
|
@@ -294,47 +324,45 @@ function parseEmoji(emoji) {
|
|
|
294
324
|
animated: mentionMatch[1] === "a"
|
|
295
325
|
};
|
|
296
326
|
}
|
|
297
|
-
const
|
|
298
|
-
const nameIdMatch = trimmed.match(nameId);
|
|
327
|
+
const nameIdMatch = /^(\w+):(\d{17,19})$/.exec(trimmed);
|
|
299
328
|
if (nameIdMatch) {
|
|
300
329
|
return { id: nameIdMatch[2], name: nameIdMatch[1] };
|
|
301
330
|
}
|
|
302
|
-
const
|
|
303
|
-
const colonsMatch = trimmed.match(colons);
|
|
331
|
+
const colonsMatch = /^:(\w+):$/.exec(trimmed);
|
|
304
332
|
if (colonsMatch) {
|
|
305
333
|
return { id: null, name: colonsMatch[1] };
|
|
306
334
|
}
|
|
307
335
|
return { id: null, name: trimmed };
|
|
308
336
|
}
|
|
309
337
|
function parseUserMention(arg) {
|
|
310
|
-
if (
|
|
338
|
+
if (typeof arg !== "string") return null;
|
|
311
339
|
const trimmed = arg.trim();
|
|
312
340
|
if (trimmed.length === 0) return null;
|
|
313
|
-
const mentionMatch =
|
|
341
|
+
const mentionMatch = /^<@!?(\d{17,19})>$/.exec(trimmed);
|
|
314
342
|
if (mentionMatch) return mentionMatch[1];
|
|
315
|
-
|
|
316
|
-
|
|
343
|
+
return SnowflakeUtil.isValid(trimmed) && trimmed.length >= 17 && trimmed.length <= 19 ? trimmed : null;
|
|
344
|
+
}
|
|
345
|
+
function parseRoleMention(arg) {
|
|
346
|
+
if (typeof arg !== "string") return null;
|
|
347
|
+
const match = /^<@&(\d{17,19})>$/.exec(arg.trim());
|
|
348
|
+
return match?.[1] ?? null;
|
|
317
349
|
}
|
|
318
350
|
function parsePrefixCommand(content, prefix) {
|
|
319
|
-
if (content
|
|
351
|
+
if (typeof content !== "string" || typeof prefix !== "string" || prefix.length === 0) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
320
354
|
const trimmed = content.trim();
|
|
321
355
|
if (!trimmed.startsWith(prefix)) return null;
|
|
322
356
|
const rest = trimmed.slice(prefix.length).trim();
|
|
323
357
|
if (rest.length === 0) return null;
|
|
324
358
|
const parts = rest.split(/\s+/);
|
|
325
359
|
const command = parts[0]?.toLowerCase() ?? "";
|
|
326
|
-
|
|
327
|
-
return { command, args };
|
|
328
|
-
}
|
|
329
|
-
function parseRoleMention(arg) {
|
|
330
|
-
if (arg == null || typeof arg !== "string") return null;
|
|
331
|
-
const match = arg.trim().match(/^<@&(\d{17,19})>$/);
|
|
332
|
-
return match?.[1] ?? null;
|
|
360
|
+
if (command.length === 0) return null;
|
|
361
|
+
return { command, args: parts.slice(1) };
|
|
333
362
|
}
|
|
334
363
|
function formatEmoji(emoji) {
|
|
335
364
|
if (emoji.id) {
|
|
336
|
-
|
|
337
|
-
return `${prefix}:${emoji.name}:${emoji.id}`;
|
|
365
|
+
return emoji.animated ? `a:${emoji.name}:${emoji.id}` : `:${emoji.name}:${emoji.id}`;
|
|
338
366
|
}
|
|
339
367
|
return encodeURIComponent(emoji.name);
|
|
340
368
|
}
|
|
@@ -353,18 +381,18 @@ function getUnicodeFromShortcode(name) {
|
|
|
353
381
|
// src/tenorUtils.ts
|
|
354
382
|
import { EmbedMediaFlags } from "@fluxerjs/types";
|
|
355
383
|
async function resolveTenorToImageUrl(tenorViewUrl) {
|
|
356
|
-
if (
|
|
384
|
+
if (typeof tenorViewUrl !== "string" || !tenorViewUrl.includes("tenor.com")) return null;
|
|
357
385
|
try {
|
|
358
386
|
const pageRes = await fetch(tenorViewUrl, {
|
|
359
387
|
signal: AbortSignal.timeout(5e3),
|
|
360
388
|
headers: { "User-Agent": "Mozilla/5.0 (compatible; FluxerSDK/1.0)" }
|
|
361
389
|
});
|
|
362
|
-
if (
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
390
|
+
if (pageRes.ok) {
|
|
391
|
+
const html = await pageRes.text();
|
|
392
|
+
const gifUrl = extractGifUrlFromJsonLd(extractTenorJsonLd(html));
|
|
393
|
+
if (gifUrl) {
|
|
394
|
+
return { url: gifUrl, flags: EmbedMediaFlags.IS_ANIMATED };
|
|
395
|
+
}
|
|
368
396
|
}
|
|
369
397
|
} catch {
|
|
370
398
|
}
|
|
@@ -373,7 +401,8 @@ async function resolveTenorToImageUrl(tenorViewUrl) {
|
|
|
373
401
|
const res = await fetch(oembedUrl, { signal: AbortSignal.timeout(5e3) });
|
|
374
402
|
if (!res.ok) return null;
|
|
375
403
|
const data = await res.json();
|
|
376
|
-
const
|
|
404
|
+
const thumb = typeof data === "object" && data !== null && "thumbnail_url" in data && typeof data.thumbnail_url === "string" ? data.thumbnail_url : void 0;
|
|
405
|
+
const gifUrl = deriveGifFromThumbnail(thumb);
|
|
377
406
|
return gifUrl ? { url: gifUrl, flags: EmbedMediaFlags.IS_ANIMATED } : null;
|
|
378
407
|
} catch {
|
|
379
408
|
return null;
|
|
@@ -381,10 +410,10 @@ async function resolveTenorToImageUrl(tenorViewUrl) {
|
|
|
381
410
|
}
|
|
382
411
|
function extractGifUrlFromJsonLd(jsonLd) {
|
|
383
412
|
if (!jsonLd) return null;
|
|
384
|
-
const image = jsonLd.image;
|
|
385
|
-
const video = jsonLd.video;
|
|
386
|
-
const thumbnailUrl = image?.thumbnailUrl ?? image?.url;
|
|
387
|
-
const contentUrl = image?.contentUrl ?? video?.contentUrl;
|
|
413
|
+
const image = asRecord(jsonLd.image);
|
|
414
|
+
const video = asRecord(jsonLd.video);
|
|
415
|
+
const thumbnailUrl = asString(image?.thumbnailUrl) ?? asString(image?.url);
|
|
416
|
+
const contentUrl = asString(image?.contentUrl) ?? asString(video?.contentUrl);
|
|
388
417
|
if (contentUrl && /\.gif($|\?)/i.test(contentUrl)) return contentUrl;
|
|
389
418
|
if (thumbnailUrl) return deriveGifFromThumbnail(thumbnailUrl);
|
|
390
419
|
return null;
|
|
@@ -397,41 +426,36 @@ function deriveGifFromThumbnail(thumbUrl) {
|
|
|
397
426
|
function extractTenorJsonLd(html) {
|
|
398
427
|
const re = /<script[^>]*class="dynamic"[^>]*type="application\/ld\+json"[^>]*>([\s\S]*?)<\/script>/i;
|
|
399
428
|
const alt = /<script[^>]*type="application\/ld\+json"[^>]*class="dynamic"[^>]*>([\s\S]*?)<\/script>/i;
|
|
400
|
-
const match =
|
|
429
|
+
const match = re.exec(html) ?? alt.exec(html);
|
|
401
430
|
if (!match?.[1]) return null;
|
|
402
431
|
try {
|
|
403
|
-
|
|
432
|
+
const parsed = JSON.parse(match[1].trim());
|
|
433
|
+
return asRecord(parsed);
|
|
404
434
|
} catch {
|
|
405
435
|
return null;
|
|
406
436
|
}
|
|
407
437
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
var warned = /* @__PURE__ */ new Set();
|
|
411
|
-
function isSuppressed() {
|
|
412
|
-
try {
|
|
413
|
-
const g = typeof globalThis !== "undefined" ? globalThis : void 0;
|
|
414
|
-
const proc = g && typeof g.process === "object" ? g.process : void 0;
|
|
415
|
-
return proc?.env?.FLUXER_SUPPRESS_DEPRECATION === "1";
|
|
416
|
-
} catch {
|
|
417
|
-
return false;
|
|
418
|
-
}
|
|
438
|
+
function asRecord(value) {
|
|
439
|
+
return typeof value === "object" && value !== null ? value : null;
|
|
419
440
|
}
|
|
420
|
-
function
|
|
421
|
-
|
|
422
|
-
if (warned.has(symbol)) return;
|
|
423
|
-
warned.add(symbol);
|
|
424
|
-
console.warn(`[Fluxer] DeprecationWarning: ${symbol} is deprecated. ${message}`);
|
|
441
|
+
function asString(value) {
|
|
442
|
+
return typeof value === "string" ? value : void 0;
|
|
425
443
|
}
|
|
426
444
|
|
|
427
445
|
// src/errors/FluxerError.ts
|
|
428
446
|
var FluxerError = class _FluxerError extends Error {
|
|
429
447
|
code;
|
|
430
448
|
constructor(message, options) {
|
|
431
|
-
super(message, options?.cause ? { cause: options.cause } : void 0);
|
|
449
|
+
super(message, options?.cause !== void 0 ? { cause: options.cause } : void 0);
|
|
432
450
|
this.name = "FluxerError";
|
|
433
451
|
this.code = options?.code;
|
|
434
|
-
Object.setPrototypeOf(this,
|
|
452
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
453
|
+
}
|
|
454
|
+
static isFluxerError(error) {
|
|
455
|
+
return error instanceof _FluxerError;
|
|
456
|
+
}
|
|
457
|
+
toString() {
|
|
458
|
+
return this.code ? `${this.name} [${this.code}]: ${this.message}` : `${this.name}: ${this.message}`;
|
|
435
459
|
}
|
|
436
460
|
};
|
|
437
461
|
|
|
@@ -464,9 +488,31 @@ var ErrorCodes = {
|
|
|
464
488
|
VoiceWebSocketRequired: "VOICE_WEBSOCKET_REQUIRED",
|
|
465
489
|
VoiceHttpError: "VOICE_HTTP_ERROR",
|
|
466
490
|
VoiceNoResponseBody: "VOICE_NO_RESPONSE_BODY",
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
491
|
+
InvalidInstanceDiscovery: "INVALID_INSTANCE_DISCOVERY",
|
|
492
|
+
ConflictingInstanceConfig: "CONFLICTING_INSTANCE_CONFIG",
|
|
493
|
+
/** @beta ClientCluster: duplicate runtime id */
|
|
494
|
+
DuplicateRuntimeId: "DUPLICATE_RUNTIME_ID",
|
|
495
|
+
/** @beta ClientCluster: unknown runtime id */
|
|
496
|
+
RuntimeNotFound: "RUNTIME_NOT_FOUND",
|
|
497
|
+
/** @beta ClientCluster: concurrent conflicting op on same id */
|
|
498
|
+
RuntimeConflict: "RUNTIME_CONFLICT",
|
|
499
|
+
/** @beta ClientCluster: cluster already destroyed */
|
|
500
|
+
ClusterDestroyed: "CLUSTER_DESTROYED",
|
|
501
|
+
/** @beta ClientCluster: prebuilt client already logged in */
|
|
502
|
+
RuntimeAlreadyLoggedIn: "RUNTIME_ALREADY_LOGGED_IN",
|
|
503
|
+
/** @beta ClientCluster: missing/empty runtime id or token */
|
|
504
|
+
InvalidRuntimeConfig: "INVALID_RUNTIME_CONFIG",
|
|
505
|
+
EmptyMessage: "EMPTY_MESSAGE",
|
|
506
|
+
InvalidMessageOptions: "INVALID_MESSAGE_OPTIONS",
|
|
507
|
+
InvalidBulkDelete: "INVALID_BULK_DELETE",
|
|
508
|
+
/** Fetch / bulk-fetch limit or batch size out of range. */
|
|
509
|
+
InvalidFetchLimit: "INVALID_FETCH_LIMIT",
|
|
510
|
+
/** Attachment plan/file mismatch or invalid attachment input. */
|
|
511
|
+
InvalidAttachmentInput: "INVALID_ATTACHMENT_INPUT",
|
|
512
|
+
/** Presigned CDN PUT failed. */
|
|
513
|
+
AttachmentUploadFailed: "ATTACHMENT_UPLOAD_FAILED",
|
|
514
|
+
/** Operation requires a personal-notes channel (or wrong channel type). */
|
|
515
|
+
InvalidChannelType: "INVALID_CHANNEL_TYPE"
|
|
470
516
|
};
|
|
471
517
|
export {
|
|
472
518
|
ALL_PERMISSIONS_BIGINT,
|
|
@@ -481,7 +527,6 @@ export {
|
|
|
481
527
|
SnowflakeUtil,
|
|
482
528
|
UserFlagsBitField,
|
|
483
529
|
UserFlagsBits,
|
|
484
|
-
emitDeprecationWarning,
|
|
485
530
|
escapeMarkdown,
|
|
486
531
|
formatColor,
|
|
487
532
|
formatEmoji,
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "2.0.0",
|
|
7
7
|
"description": "Shared utilities for @fluxerjs/core",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"license": "Apache-2.0",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@fluxerjs/types": "
|
|
23
|
+
"@fluxerjs/types": "2.0.0"
|
|
24
24
|
},
|
|
25
25
|
"main": "./dist/index.js",
|
|
26
26
|
"module": "./dist/index.mjs",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "pnpm exec tsx ../../scripts/generate-emoji-shortcodes.ts && tsup",
|
|
45
45
|
"clean": "rm -rf dist",
|
|
46
|
-
"lint": "
|
|
47
|
-
"lint:fix": "
|
|
46
|
+
"lint": "biome lint .",
|
|
47
|
+
"lint:fix": "biome lint --write .",
|
|
48
48
|
"test": "vitest run",
|
|
49
49
|
"test:coverage": "vitest run --coverage"
|
|
50
50
|
}
|