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