@fetchbrain.com/sdk 0.5.1 → 0.6.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.mjs CHANGED
@@ -321,16 +321,38 @@ var DENYLIST = /TOKEN|SECRET|PASSWORD|CREDENTIAL|KEY|AUTH|COOKIE/i;
321
321
  var CREDENTIAL_VALUE = /^[\w-]{8,}\.[\w-]{8,}\.[\w-]{8,}$|\/\/[^/\s@]+:[^/\s@]+@/;
322
322
  var MAX_VALUE_LENGTH = 256;
323
323
  var MAX_HEADER_LENGTH = 2048;
324
- function buildNativeContext(env = process.env) {
324
+ var SCRAPER_NAME_VAR = "FETCHBRAIN_SCRAPER_ID";
325
+ var SCRAPER_NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/;
326
+ function normalizeScraperName(raw) {
327
+ const name = raw?.trim().toLowerCase();
328
+ return name && SCRAPER_NAME.test(name) ? name : void 0;
329
+ }
330
+ function resolveScraperName(option, env = process.env) {
331
+ const rejected = [];
332
+ const candidates = [
333
+ ["option", option],
334
+ [SCRAPER_NAME_VAR, env[SCRAPER_NAME_VAR]]
335
+ ];
336
+ for (const [source, raw] of candidates) {
337
+ if (raw === void 0 || raw.trim() === "") continue;
338
+ const name = normalizeScraperName(raw);
339
+ if (name) return { name, rejected };
340
+ rejected.push(source);
341
+ }
342
+ return { rejected };
343
+ }
344
+ function buildNativeContext(env = process.env, scraper) {
325
345
  const context = {};
326
- const names = Object.keys(env).filter((name) => ALLOWED_NAME.test(name) && !DENYLIST.test(name)).sort();
327
- for (const name of names) {
328
- const value = env[name];
346
+ const name = normalizeScraperName(scraper);
347
+ if (name) context[SCRAPER_NAME_VAR] = name;
348
+ const names = Object.keys(env).filter((n) => n !== SCRAPER_NAME_VAR && ALLOWED_NAME.test(n) && !DENYLIST.test(n)).sort();
349
+ for (const varName of names) {
350
+ const value = env[varName];
329
351
  if (!value || value.length > MAX_VALUE_LENGTH) continue;
330
352
  if (CREDENTIAL_VALUE.test(value)) continue;
331
- const candidate = { ...context, [name]: value };
353
+ const candidate = { ...context, [varName]: value };
332
354
  if (JSON.stringify(candidate).length > MAX_HEADER_LENGTH) break;
333
- context[name] = value;
355
+ context[varName] = value;
334
356
  }
335
357
  if (Object.keys(context).length === 0) return void 0;
336
358
  return JSON.stringify(context);
@@ -406,10 +428,18 @@ var FetchBrainClient = class {
406
428
  // enhance-only; stored so getConfig() round-trips
407
429
  preRecall: config.preRecall,
408
430
  // enhance-only; stored so getConfig() round-trips
409
- onRecalled: config.onRecalled
431
+ onRecalled: config.onRecalled,
410
432
  // enhance-only; stored so getConfig() round-trips
433
+ scraper: config.scraper
411
434
  };
412
435
  this.logger = createLogger(this.config.debug ? "debug" : "info", true);
436
+ const scraper = resolveScraperName(config.scraper);
437
+ this.scraperName = scraper.name;
438
+ for (const source of scraper.rejected) {
439
+ this.logger.warn(
440
+ `ignoring invalid scraper name from ${source === "option" ? "the scraper option" : source}; use 1-64 lowercase letters, digits, '.', '_' or '-', starting with a letter or digit. ` + (scraper.name ? `Using "${scraper.name}" instead.` : "This scraper uses the shared default memory.")
441
+ );
442
+ }
413
443
  this.circuitBreaker = new CircuitBreaker(
414
444
  this.config.circuitBreaker,
415
445
  this.logger
@@ -659,7 +689,7 @@ var FetchBrainClient = class {
659
689
  if (process.env.NODE_ENV) {
660
690
  contextHeaders["X-FB-Env"] = process.env.NODE_ENV;
661
691
  }
662
- const nativeContext = buildNativeContext();
692
+ const nativeContext = buildNativeContext(process.env, this.scraperName);
663
693
  if (nativeContext) {
664
694
  contextHeaders["X-FB-Context"] = nativeContext;
665
695
  }