@lazyneoaz/metachat 6.0.3 → 6.0.5

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.cjs CHANGED
@@ -85890,7 +85890,7 @@ async function ensureCometTokens(ctx, defaultFuncs, force = false) {
85890
85890
  }
85891
85891
  }
85892
85892
  if (!state.inflight) {
85893
- state.inflight = scrapeTokens(ctx, defaultFuncs, state).finally(() => {
85893
+ state.inflight = scrapeTokens(ctx, defaultFuncs, state, force).finally(() => {
85894
85894
  state.inflight = null;
85895
85895
  });
85896
85896
  }
@@ -85900,10 +85900,11 @@ async function ensureCometTokens(ctx, defaultFuncs, force = false) {
85900
85900
  }
85901
85901
  applyCached(ctx, state);
85902
85902
  }
85903
- async function scrapeTokens(ctx, defaultFuncs, state) {
85903
+ async function scrapeTokens(ctx, defaultFuncs, state, force = false) {
85904
85904
  state.attemptedAt = Date.now();
85905
85905
  try {
85906
- const res = await defaultFuncs.get("https://www.facebook.com/", ctx.jar);
85906
+ const url2 = force ? `https://www.facebook.com/?_nc_hashed_token=${Date.now().toString(36)}${Math.floor(Math.random() * 1e6).toString(36)}` : "https://www.facebook.com/";
85907
+ const res = await defaultFuncs.get(url2, ctx.jar);
85907
85908
  const html3 = htmlOf(res);
85908
85909
  if (!html3) {
85909
85910
  state.failures++;
@@ -90205,10 +90206,24 @@ function collectTracks(payload) {
90205
90206
  }
90206
90207
 
90207
90208
  // src/deltas/apis/music/searchMusic.ts
90209
+ var MUSIC_CACHE_TTL_MS = 5 * 60 * 1e3;
90208
90210
  function nextRequestId(ctx) {
90209
90211
  ctx.__reqCounter = (typeof ctx.__reqCounter === "number" ? ctx.__reqCounter : 0) + 1;
90210
90212
  return ctx.__reqCounter.toString(36);
90211
90213
  }
90214
+ var MUSIC_MIN_INTERVAL_MS = 1100;
90215
+ async function paceMusic(ctx) {
90216
+ const configured = Number(ctx?.globalOptions?.musicMinIntervalMs);
90217
+ const min = Number.isFinite(configured) && configured >= 0 ? configured : MUSIC_MIN_INTERVAL_MS;
90218
+ const last2 = typeof ctx.__musicLastCallAt === "number" ? ctx.__musicLastCallAt : 0;
90219
+ const wait = last2 + min - Date.now();
90220
+ if (wait > 0) await sleep(wait);
90221
+ ctx.__musicLastCallAt = Date.now();
90222
+ }
90223
+ function cacheResult(ctx, key, result) {
90224
+ const cache = ctx.__musicCache || (ctx.__musicCache = {});
90225
+ cache[key] = { at: Date.now(), result };
90226
+ }
90212
90227
  function normalizeError(error2) {
90213
90228
  if (!error2 || typeof error2 !== "object") {
90214
90229
  const message2 = String(error2);
@@ -90230,6 +90245,13 @@ function normalizeError(error2) {
90230
90245
  }
90231
90246
  function searchMusic_default(defaultFuncs, _api, ctx) {
90232
90247
  async function runSearch(text3, count, selectedOptions, callback) {
90248
+ const cacheKey = `${selectedOptions.product || PRODUCT}|${count}|${text3}|${selectedOptions.cursor || ""}`;
90249
+ if (!selectedOptions.noCache) {
90250
+ const cache = ctx.__musicCache || (ctx.__musicCache = {});
90251
+ const hit = cache[cacheKey];
90252
+ if (hit && Date.now() - hit.at < MUSIC_CACHE_TTL_MS) return hit.result;
90253
+ }
90254
+ await paceMusic(ctx);
90233
90255
  const params = { first: count };
90234
90256
  if (text3) params.search_text = text3;
90235
90257
  if (selectedOptions.cursor) params.after = String(selectedOptions.cursor);
@@ -90302,11 +90324,12 @@ function searchMusic_default(defaultFuncs, _api, ctx) {
90302
90324
  result.endCursor = page.endCursor;
90303
90325
  result.hasNextPage = page.hasNextPage;
90304
90326
  result.query = text3;
90327
+ cacheResult(ctx, cacheKey, result);
90305
90328
  return result;
90306
90329
  }
90307
- for (let retry = 0; retry < 3; retry++) {
90308
- await sleep(600 + retry * 900);
90309
- const retried = await attempt(true);
90330
+ for (let retry = 0; retry < 6; retry++) {
90331
+ await sleep(1e3 + retry * 600 + Math.floor(Math.random() * 350));
90332
+ const retried = await attempt(retry >= 3 || !hasCometTokens(ctx));
90310
90333
  if (isStaleTokenError(retried) || retried?.error || (retried?.errors || []).length) continue;
90311
90334
  const retryPage = collectTracks(retried);
90312
90335
  const retryTracks = retryPage.tracks.slice(0, count);
@@ -90316,6 +90339,7 @@ function searchMusic_default(defaultFuncs, _api, ctx) {
90316
90339
  result.endCursor = retryPage.endCursor;
90317
90340
  result.hasNextPage = retryPage.hasNextPage;
90318
90341
  result.query = text3;
90342
+ cacheResult(ctx, cacheKey, result);
90319
90343
  return result;
90320
90344
  }
90321
90345
  }
package/dist/index.mjs CHANGED
@@ -85858,7 +85858,7 @@ async function ensureCometTokens(ctx, defaultFuncs, force = false) {
85858
85858
  }
85859
85859
  }
85860
85860
  if (!state.inflight) {
85861
- state.inflight = scrapeTokens(ctx, defaultFuncs, state).finally(() => {
85861
+ state.inflight = scrapeTokens(ctx, defaultFuncs, state, force).finally(() => {
85862
85862
  state.inflight = null;
85863
85863
  });
85864
85864
  }
@@ -85868,10 +85868,11 @@ async function ensureCometTokens(ctx, defaultFuncs, force = false) {
85868
85868
  }
85869
85869
  applyCached(ctx, state);
85870
85870
  }
85871
- async function scrapeTokens(ctx, defaultFuncs, state) {
85871
+ async function scrapeTokens(ctx, defaultFuncs, state, force = false) {
85872
85872
  state.attemptedAt = Date.now();
85873
85873
  try {
85874
- const res = await defaultFuncs.get("https://www.facebook.com/", ctx.jar);
85874
+ const url2 = force ? `https://www.facebook.com/?_nc_hashed_token=${Date.now().toString(36)}${Math.floor(Math.random() * 1e6).toString(36)}` : "https://www.facebook.com/";
85875
+ const res = await defaultFuncs.get(url2, ctx.jar);
85875
85876
  const html3 = htmlOf(res);
85876
85877
  if (!html3) {
85877
85878
  state.failures++;
@@ -90173,10 +90174,24 @@ function collectTracks(payload) {
90173
90174
  }
90174
90175
 
90175
90176
  // src/deltas/apis/music/searchMusic.ts
90177
+ var MUSIC_CACHE_TTL_MS = 5 * 60 * 1e3;
90176
90178
  function nextRequestId(ctx) {
90177
90179
  ctx.__reqCounter = (typeof ctx.__reqCounter === "number" ? ctx.__reqCounter : 0) + 1;
90178
90180
  return ctx.__reqCounter.toString(36);
90179
90181
  }
90182
+ var MUSIC_MIN_INTERVAL_MS = 1100;
90183
+ async function paceMusic(ctx) {
90184
+ const configured = Number(ctx?.globalOptions?.musicMinIntervalMs);
90185
+ const min = Number.isFinite(configured) && configured >= 0 ? configured : MUSIC_MIN_INTERVAL_MS;
90186
+ const last2 = typeof ctx.__musicLastCallAt === "number" ? ctx.__musicLastCallAt : 0;
90187
+ const wait = last2 + min - Date.now();
90188
+ if (wait > 0) await sleep(wait);
90189
+ ctx.__musicLastCallAt = Date.now();
90190
+ }
90191
+ function cacheResult(ctx, key, result) {
90192
+ const cache = ctx.__musicCache || (ctx.__musicCache = {});
90193
+ cache[key] = { at: Date.now(), result };
90194
+ }
90180
90195
  function normalizeError(error2) {
90181
90196
  if (!error2 || typeof error2 !== "object") {
90182
90197
  const message2 = String(error2);
@@ -90198,6 +90213,13 @@ function normalizeError(error2) {
90198
90213
  }
90199
90214
  function searchMusic_default(defaultFuncs, _api, ctx) {
90200
90215
  async function runSearch(text3, count, selectedOptions, callback) {
90216
+ const cacheKey = `${selectedOptions.product || PRODUCT}|${count}|${text3}|${selectedOptions.cursor || ""}`;
90217
+ if (!selectedOptions.noCache) {
90218
+ const cache = ctx.__musicCache || (ctx.__musicCache = {});
90219
+ const hit = cache[cacheKey];
90220
+ if (hit && Date.now() - hit.at < MUSIC_CACHE_TTL_MS) return hit.result;
90221
+ }
90222
+ await paceMusic(ctx);
90201
90223
  const params = { first: count };
90202
90224
  if (text3) params.search_text = text3;
90203
90225
  if (selectedOptions.cursor) params.after = String(selectedOptions.cursor);
@@ -90270,11 +90292,12 @@ function searchMusic_default(defaultFuncs, _api, ctx) {
90270
90292
  result.endCursor = page.endCursor;
90271
90293
  result.hasNextPage = page.hasNextPage;
90272
90294
  result.query = text3;
90295
+ cacheResult(ctx, cacheKey, result);
90273
90296
  return result;
90274
90297
  }
90275
- for (let retry = 0; retry < 3; retry++) {
90276
- await sleep(600 + retry * 900);
90277
- const retried = await attempt(true);
90298
+ for (let retry = 0; retry < 6; retry++) {
90299
+ await sleep(1e3 + retry * 600 + Math.floor(Math.random() * 350));
90300
+ const retried = await attempt(retry >= 3 || !hasCometTokens(ctx));
90278
90301
  if (isStaleTokenError(retried) || retried?.error || (retried?.errors || []).length) continue;
90279
90302
  const retryPage = collectTracks(retried);
90280
90303
  const retryTracks = retryPage.tracks.slice(0, count);
@@ -90284,6 +90307,7 @@ function searchMusic_default(defaultFuncs, _api, ctx) {
90284
90307
  result.endCursor = retryPage.endCursor;
90285
90308
  result.hasNextPage = retryPage.hasNextPage;
90286
90309
  result.query = text3;
90310
+ cacheResult(ctx, cacheKey, result);
90287
90311
  return result;
90288
90312
  }
90289
90313
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lazyneoaz/metachat",
3
- "version": "6.0.3",
3
+ "version": "6.0.5",
4
4
  "description": "A modern TypeScript Facebook Chat API client for building reliable Messenger bots, with built-in anti-detection protection, automatic cookie refresh, music search, and AI theme support.",
5
5
  "license": "MIT",
6
6
  "author": {