lunibee 0.3.0 → 0.3.1

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.
Files changed (38) hide show
  1. package/dist/builders/embed.d.ts +1 -0
  2. package/dist/builders/index.js +6 -2
  3. package/dist/builders/index.js.map +4 -4
  4. package/dist/core/index.js +66 -15
  5. package/dist/core/index.js.map +8 -8
  6. package/dist/index.js +2345 -199
  7. package/dist/index.js.map +22 -14
  8. package/dist/managers/index.js +2 -2
  9. package/dist/managers/index.js.map +3 -3
  10. package/dist/rest/errors.d.ts +8 -0
  11. package/dist/rest/index.d.ts +2 -0
  12. package/dist/rest/index.js +65 -14
  13. package/dist/rest/index.js.map +5 -5
  14. package/dist/rest/transport.d.ts +9 -0
  15. package/dist/sharding/bus.d.ts +8 -0
  16. package/dist/sharding/cluster.d.ts +224 -14
  17. package/dist/sharding/context.d.ts +41 -0
  18. package/dist/sharding/gateway-bot.d.ts +20 -0
  19. package/dist/sharding/health.d.ts +94 -0
  20. package/dist/sharding/host.d.ts +70 -0
  21. package/dist/sharding/identify.d.ts +62 -0
  22. package/dist/sharding/index.d.ts +56 -12
  23. package/dist/sharding/index.js +2255 -164
  24. package/dist/sharding/index.js.map +18 -10
  25. package/dist/sharding/protocol.d.ts +47 -0
  26. package/dist/sharding/remote-protocol.d.ts +100 -0
  27. package/dist/sharding/remote.d.ts +118 -0
  28. package/dist/sharding/requests.d.ts +54 -0
  29. package/dist/sharding/worker.d.ts +45 -0
  30. package/dist/structures/index.js +2 -2
  31. package/dist/structures/index.js.map +3 -3
  32. package/dist/types/gateway.d.ts +3 -0
  33. package/dist/types/index.js.map +1 -1
  34. package/dist/ws/index.d.ts +1 -1
  35. package/dist/ws/index.js +2 -2
  36. package/dist/ws/index.js.map +5 -5
  37. package/dist/ws/send-budget.d.ts +1 -1
  38. package/package.json +18 -16
@@ -3016,7 +3016,7 @@ class ResourceManager extends Manager {
3016
3016
  // packages/rest/package.json
3017
3017
  var package_default = {
3018
3018
  name: "@lunibee/rest",
3019
- version: "0.3.0",
3019
+ version: "0.3.1",
3020
3020
  type: "module",
3021
3021
  module: "./src/index.ts",
3022
3022
  types: "./src/index.ts",
@@ -3099,6 +3099,24 @@ function kindFromStatus(status) {
3099
3099
  function redactPath(path) {
3100
3100
  return path.replace(/^(\/webhooks\/[^/?]+\/)[^/?]+/i, "$1:token").replace(/^(\/interactions\/[^/?]+\/)[^/?]+/i, "$1:token");
3101
3101
  }
3102
+ var TOKEN_IN_TEXT = /(\/(?:webhooks|interactions)\/[^/\s?"'`]+\/)[^/\s?"'`]+/gi;
3103
+ function redactText(text) {
3104
+ return text.replace(TOKEN_IN_TEXT, "$1:token");
3105
+ }
3106
+ function sanitizeCause(error) {
3107
+ if (!(error instanceof Error))
3108
+ return;
3109
+ const copy = new Error(redactText(error.message));
3110
+ copy.name = error.name;
3111
+ if (error.stack)
3112
+ copy.stack = redactText(error.stack);
3113
+ for (const key of ["code", "errno", "syscall"]) {
3114
+ const value = error[key];
3115
+ if (typeof value === "string" || typeof value === "number")
3116
+ copy[key] = value;
3117
+ }
3118
+ return copy;
3119
+ }
3102
3120
  var HINTS = {
3103
3121
  50001: "The bot cannot see this resource: check that it is in the guild and has View Channel there.",
3104
3122
  50013: "The bot lacks a permission for this action, or the target role or member is at or above the bot's highest role."
@@ -3134,14 +3152,19 @@ async function sleep(delay, signal, path) {
3134
3152
  if (delay <= 0)
3135
3153
  return;
3136
3154
  await new Promise((resolve, reject) => {
3137
- const timer = setTimeout(resolve, delay);
3138
- const abort = () => {
3155
+ const finish = (error) => {
3139
3156
  clearTimeout(timer);
3140
- reject(abortError(path, signal?.reason));
3157
+ signal?.removeEventListener("abort", onAbort);
3158
+ if (error)
3159
+ reject(error);
3160
+ else
3161
+ resolve();
3141
3162
  };
3163
+ const onAbort = () => finish(abortError(path, signal?.reason));
3164
+ const timer = setTimeout(finish, delay);
3142
3165
  if (signal?.aborted)
3143
- return abort();
3144
- signal?.addEventListener("abort", abort, { once: true });
3166
+ return onAbort();
3167
+ signal?.addEventListener("abort", onAbort, { once: true });
3145
3168
  });
3146
3169
  }
3147
3170
  async function abortable(promise, signal, path) {
@@ -3149,10 +3172,17 @@ async function abortable(promise, signal, path) {
3149
3172
  return promise;
3150
3173
  if (signal.aborted)
3151
3174
  throw abortError(path, signal.reason);
3152
- return Promise.race([
3153
- promise,
3154
- new Promise((_, reject) => signal.addEventListener("abort", () => reject(abortError(path, signal.reason)), { once: true }))
3155
- ]);
3175
+ return new Promise((resolve, reject) => {
3176
+ const onAbort = () => reject(abortError(path, signal.reason));
3177
+ signal.addEventListener("abort", onAbort, { once: true });
3178
+ promise.then(() => {
3179
+ signal.removeEventListener("abort", onAbort);
3180
+ resolve();
3181
+ }, (error) => {
3182
+ signal.removeEventListener("abort", onAbort);
3183
+ reject(error);
3184
+ });
3185
+ });
3156
3186
  }
3157
3187
 
3158
3188
  // packages/rest/src/route.ts
@@ -3326,6 +3356,25 @@ class RateLimiter {
3326
3356
  }
3327
3357
 
3328
3358
  // packages/rest/src/transport.ts
3359
+ var LOOPBACK = /^(?:localhost|127(?:\.\d{1,3}){3}|\[::1\])$/i;
3360
+ function parseBaseURL(raw, allowInsecure = false) {
3361
+ let url;
3362
+ try {
3363
+ url = new URL(raw);
3364
+ } catch {
3365
+ throw new TypeError("baseURL must be an absolute http(s) URL.");
3366
+ }
3367
+ if (url.protocol !== "https:" && url.protocol !== "http:")
3368
+ throw new TypeError("baseURL must use https.");
3369
+ if (url.username || url.password)
3370
+ throw new TypeError("baseURL must not contain credentials.");
3371
+ if (url.search || url.hash)
3372
+ throw new TypeError("baseURL must not contain a query or fragment.");
3373
+ if (url.protocol === "http:" && !allowInsecure && !LOOPBACK.test(url.hostname))
3374
+ throw new TypeError("baseURL must use https: the bot token would travel in cleartext (allowInsecureBaseURL: true overrides this for a trusted network).");
3375
+ return raw.replace(/\/$/, "");
3376
+ }
3377
+
3329
3378
  class TransportError extends Error {
3330
3379
  timedOut;
3331
3380
  constructor(message, timedOut, cause) {
@@ -3340,7 +3389,7 @@ class HttpTransport {
3340
3389
  #timeout;
3341
3390
  #fetch;
3342
3391
  constructor(options = {}) {
3343
- this.#baseURL = (options.baseURL ?? "https://discord.com/api/v10").replace(/\/$/, "");
3392
+ this.#baseURL = parseBaseURL(options.baseURL ?? "https://discord.com/api/v10", options.allowInsecureBaseURL);
3344
3393
  this.#timeout = Math.max(1, options.timeout ?? 15000);
3345
3394
  this.#fetch = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
3346
3395
  }
@@ -3362,13 +3411,14 @@ class HttpTransport {
3362
3411
  method: request.method,
3363
3412
  headers: request.headers,
3364
3413
  body: request.body,
3365
- signal: controller.signal
3414
+ signal: controller.signal,
3415
+ redirect: "error"
3366
3416
  });
3367
3417
  } catch (error) {
3368
3418
  if (error instanceof RESTError)
3369
3419
  throw error;
3370
3420
  const timedOut = error instanceof DOMException && (error.name === "TimeoutError" || error.name === "AbortError");
3371
- throw new TransportError(timedOut ? `Discord REST request timed out after ${this.#timeout}ms` : "Discord REST request failed", timedOut, error);
3421
+ throw new TransportError(timedOut ? `Discord REST request timed out after ${this.#timeout}ms` : "Discord REST request failed", timedOut, sanitizeCause(error));
3372
3422
  } finally {
3373
3423
  clearTimeout(timer);
3374
3424
  request.signal?.removeEventListener("abort", onAbort);
@@ -3585,6 +3635,7 @@ class REST {
3585
3635
  this.#retryPolicy = options.retryPolicy ?? createRetryPolicy(options.retries ?? 2);
3586
3636
  this.#transport = options.transport ?? new HttpTransport({
3587
3637
  baseURL: options.baseURL,
3638
+ allowInsecureBaseURL: options.allowInsecureBaseURL,
3588
3639
  timeout: options.timeout
3589
3640
  });
3590
3641
  const store = options.store ?? new MemoryRateLimitStore;
@@ -7446,7 +7497,7 @@ class Gateway {
7446
7497
  }
7447
7498
  connect(url = DEFAULT_GATEWAY_URL) {
7448
7499
  if (this.state === "CLOSED" /* Closed */)
7449
- throw new GatewayError("Gateway has been permanently closed.");
7500
+ return Promise.reject(new GatewayError("Gateway has been permanently closed."));
7450
7501
  const inFlight = this.#reconnect.attemptPromise;
7451
7502
  if (inFlight)
7452
7503
  return inFlight;
@@ -8570,4 +8621,4 @@ export {
8570
8621
  resolveGatewayIntents
8571
8622
  };
8572
8623
 
8573
- //# debugId=EEC9EA7D2E19B5EE64756E2164756E21
8624
+ //# debugId=3D7CC48CA04F88E364756E2164756E21