@namiml/sdk-core 3.4.12-dev.202609252349 → 3.4.12-dev.202609281806

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
@@ -106,7 +106,7 @@ const {
106
106
  // version — stamped by scripts/version.sh
107
107
  NAMI_SDK_VERSION: exports.NAMI_SDK_VERSION = "3.4.12",
108
108
  // full package version including dev suffix — stamped by scripts/version.sh
109
- NAMI_SDK_PACKAGE_VERSION: exports.NAMI_SDK_PACKAGE_VERSION = "3.4.12-dev.202609252349",
109
+ NAMI_SDK_PACKAGE_VERSION: exports.NAMI_SDK_PACKAGE_VERSION = "3.4.12-dev.202609281806",
110
110
  // environments
111
111
  PRODUCTION: exports.PRODUCTION = "production", DEVELOPMENT: exports.DEVELOPMENT = "development",
112
112
  // error messages
@@ -119,7 +119,13 @@ API_VERSION: exports.API_VERSION = "v3", BASE_URL_PATH: exports.BASE_URL_PATH =
119
119
  EXTENDED_CLIENT_INFO_PREFIX: exports.EXTENDED_CLIENT_INFO_PREFIX = "extendedClientInfo", EXTENDED_CLIENT_INFO_DELIMITER: exports.EXTENDED_CLIENT_INFO_DELIMITER = ":", VALIDATE_PRODUCT_GROUPS: exports.VALIDATE_PRODUCT_GROUPS = "validateProductGroups", LOG_HTTP_REQUESTS: exports.LOG_HTTP_REQUESTS = "logHTTPRequests", LOG_HTTP_TRAFFIC: exports.LOG_HTTP_TRAFFIC = "logHTTPTraffic", API_TRAFFIC_OBSERVER: exports.API_TRAFFIC_OBSERVER = "apiTrafficObserver", STARTUP_TELEMETRY: exports.STARTUP_TELEMETRY = "startupTelemetry",
120
120
  // Test hook: every platform-config fetch attempt fails before the request leaves.
121
121
  // Same namiCommand as Apple's and Android's, so the shared first-run flows drive it.
122
- SIMULATE_CONFIG_FETCH_FAILURE: exports.SIMULATE_CONFIG_FETCH_FAILURE = "simulateConfigFetchFailure", EXTENDED_PLATFORM: exports.EXTENDED_PLATFORM = "extended-platform", EXTENDED_PLATFORM_VERSION: exports.EXTENDED_PLATFORM_VERSION = "extended-platform-version", API_MAX_CALLS_LIMIT: exports.API_MAX_CALLS_LIMIT = 2, API_RETRY_DELAY_SEC: exports.API_RETRY_DELAY_SEC = 2, API_TIMEOUT_LIMIT: exports.API_TIMEOUT_LIMIT = 20000, DEVICE_API_TIMEOUT_LIMIT: exports.DEVICE_API_TIMEOUT_LIMIT = 2000,
122
+ SIMULATE_CONFIG_FETCH_FAILURE: exports.SIMULATE_CONFIG_FETCH_FAILURE = "simulateConfigFetchFailure",
123
+ // Test hook: every per-paywall body fetch fails before the request leaves, so the body cache's
124
+ // failure rows can be exercised without a proxy. Same namiCommand as Apple's and Android's (NAM-4357).
125
+ SIMULATE_PAYWALL_FETCH_FAILURE: exports.SIMULATE_PAYWALL_FETCH_FAILURE = "simulatePaywallFetchFailure",
126
+ // `campaignCacheTTL=<seconds>` overrides the config's campaign_cache_ttl — Apple's command name.
127
+ // It lets the paywall-cache e2e suite pin TTL 3600 / 1 / 0 per load (NAM-4357).
128
+ CAMPAIGN_CACHE_TTL_PREFIX: exports.CAMPAIGN_CACHE_TTL_PREFIX = "campaignCacheTTL=", EXTENDED_PLATFORM: exports.EXTENDED_PLATFORM = "extended-platform", EXTENDED_PLATFORM_VERSION: exports.EXTENDED_PLATFORM_VERSION = "extended-platform-version", API_MAX_CALLS_LIMIT: exports.API_MAX_CALLS_LIMIT = 2, API_RETRY_DELAY_SEC: exports.API_RETRY_DELAY_SEC = 2, API_TIMEOUT_LIMIT: exports.API_TIMEOUT_LIMIT = 20000, DEVICE_API_TIMEOUT_LIMIT: exports.DEVICE_API_TIMEOUT_LIMIT = 2000,
123
129
  // Platform-config attempts when no initial config was supplied (Android's
124
130
  // CONFIG_MAX_ATTEMPTS; Apple's retry(maxAttempts: 3)).
125
131
  CONFIG_MAX_ATTEMPTS: exports.CONFIG_MAX_ATTEMPTS = 3,
@@ -3059,6 +3065,206 @@ function uniqBy(array, iteratee) {
3059
3065
  return (array && array.length) ? baseUniq(array, baseIteratee(iteratee)) : [];
3060
3066
  }
3061
3067
 
3068
+ /** Storage key of an entry's raw body; the exact URL follows the prefix. */
3069
+ const PAYWALL_BODY_KEY_PREFIX = "api_paywall_body:";
3070
+ /**
3071
+ * Storage key of an entry's metadata. Written AFTER the body, so it is the commit point: an entry
3072
+ * is visible only once both are durable, and a body with no metadata is an interrupted write.
3073
+ */
3074
+ const PAYWALL_BODY_META_KEY_PREFIX = "api_paywall_body_meta:";
3075
+ const DEFAULT_MAX_BODY_ENTRIES = 500;
3076
+ class PaywallBodyCache {
3077
+ constructor(storage = () => getPlatformAdapters().storage, maxEntries = DEFAULT_MAX_BODY_ENTRIES) {
3078
+ this.storage = storage;
3079
+ this.maxEntries = maxEntries;
3080
+ /** Set under `campaign_cache_ttl == 0`: entries live here, in this process only. */
3081
+ this.memory = null;
3082
+ /** Metadata of every persisted entry, loaded once from the storage adapter. */
3083
+ this.index = null;
3084
+ }
3085
+ get count() {
3086
+ return this.memory ? this.memory.size : this.loadIndex().size;
3087
+ }
3088
+ /**
3089
+ * Applies `campaign_cache_ttl` at the start of a resolution. `null` keeps everything, `n > 0`
3090
+ * purges entries whose own age is `>= n` seconds, and `0` purges the persistent store and keeps
3091
+ * this process's entries in memory only.
3092
+ */
3093
+ evaluateTtl(ttl, now = Date.now()) {
3094
+ if (ttl === 0) {
3095
+ if (this.memory)
3096
+ return;
3097
+ this.dropPersisted([...this.loadIndex().keys()]);
3098
+ this.memory = new Map();
3099
+ return;
3100
+ }
3101
+ // Leaving TTL 0 goes back to the persistent store; the process-local entries are dropped.
3102
+ this.memory = null;
3103
+ if (ttl === null)
3104
+ return;
3105
+ const expired = [...this.loadIndex().values()].filter((meta) => isExpired(meta, ttl, now));
3106
+ this.dropPersisted(expired.map((meta) => meta.url));
3107
+ }
3108
+ /**
3109
+ * The entry stored under this exact URL, or null when there is none, it is past `ttl`, or its
3110
+ * body is missing (a half-written entry, which is deleted here).
3111
+ */
3112
+ validEntry(url, ttl, now = Date.now()) {
3113
+ if (this.memory)
3114
+ return this.memory.get(url) ?? null;
3115
+ const meta = this.loadIndex().get(url);
3116
+ if (!meta)
3117
+ return null;
3118
+ if (ttl !== null && ttl > 0 && isExpired(meta, ttl, now)) {
3119
+ this.dropPersisted([url]);
3120
+ return null;
3121
+ }
3122
+ const body = this.read(PAYWALL_BODY_KEY_PREFIX + url);
3123
+ if (body === null) {
3124
+ this.dropPersisted([url]);
3125
+ return null;
3126
+ }
3127
+ return { meta, body };
3128
+ }
3129
+ /**
3130
+ * The stored body for this exact URL, read straight from storage rather than through the index,
3131
+ * with no TTL check. This is what rebuilds the aggregate `api_paywalls`, so it also sees an entry
3132
+ * a host wrote after the index was loaded (the web test apps inject local test paywalls that way).
3133
+ */
3134
+ bodyFor(url) {
3135
+ if (this.memory)
3136
+ return this.memory.get(url)?.body ?? null;
3137
+ return this.read(PAYWALL_BODY_KEY_PREFIX + url);
3138
+ }
3139
+ /** Stores a body under its exact URL. Returns false when the write did not land (quota, I/O). */
3140
+ store(url, contentHash, body, now = Date.now()) {
3141
+ const meta = { url, savedAt: now, ...(contentHash ? { contentHash } : {}) };
3142
+ if (this.memory) {
3143
+ this.memory.set(url, { meta, body });
3144
+ return true;
3145
+ }
3146
+ const index = this.loadIndex();
3147
+ const metaJson = JSON.stringify(meta);
3148
+ // Body first, metadata last. Each write is verified by reading it back: the web adapter
3149
+ // swallows a quota error rather than throwing, so a silent no-op is the common failure.
3150
+ if (this.write(PAYWALL_BODY_KEY_PREFIX + url, body) &&
3151
+ this.write(PAYWALL_BODY_META_KEY_PREFIX + url, metaJson)) {
3152
+ index.set(url, meta);
3153
+ return true;
3154
+ }
3155
+ logger.warn(`Paywall body cache: could not store ${url}; it will be fetched next time`);
3156
+ this.dropPersisted([url]);
3157
+ return false;
3158
+ }
3159
+ /** Stores a bundled body unless an entry already exists — a body the SDK fetched always wins. */
3160
+ seedIfAbsent(url, contentHash, body, now = Date.now()) {
3161
+ const exists = this.memory ? this.memory.has(url) : this.loadIndex().has(url);
3162
+ return exists ? false : this.store(url, contentHash, body, now);
3163
+ }
3164
+ /** Deletes one entry — used for an undecodable body. Not eviction. */
3165
+ remove(url) {
3166
+ if (this.memory)
3167
+ this.memory.delete(url);
3168
+ else
3169
+ this.dropPersisted([url]);
3170
+ }
3171
+ /**
3172
+ * Eviction, run once per resolution after a complete `campaign_rules` fetch: delete every entry
3173
+ * whose URL this resolution did not name, then cap the store at `maxEntries`, oldest first.
3174
+ */
3175
+ reconcile(currentUrls) {
3176
+ const metas = this.memory
3177
+ ? [...this.memory.values()].map((entry) => entry.meta)
3178
+ : [...this.loadIndex().values()];
3179
+ const unnamed = metas.filter((meta) => !currentUrls.has(meta.url)).map((meta) => meta.url);
3180
+ const kept = metas.filter((meta) => currentUrls.has(meta.url)).sort((a, b) => a.savedAt - b.savedAt);
3181
+ const overCap = kept.slice(0, Math.max(0, kept.length - this.maxEntries)).map((meta) => meta.url);
3182
+ [...unnamed, ...overCap].forEach((url) => this.remove(url));
3183
+ }
3184
+ /** Empties the store — persisted and in-memory entries alike. Called by `Nami.reset()`. */
3185
+ purgeAll() {
3186
+ this.dropPersisted([...this.loadIndex().keys()]);
3187
+ this.memory?.clear();
3188
+ }
3189
+ /** Builds the index from the metadata keys, clearing away half-written and unreadable entries. */
3190
+ loadIndex() {
3191
+ if (this.index)
3192
+ return this.index;
3193
+ this.index = new Map();
3194
+ let keys;
3195
+ try {
3196
+ keys = this.storage().getAllKeys();
3197
+ }
3198
+ catch (error) {
3199
+ logger.warn(`Paywall body cache: storage unavailable, caching nothing (${error})`);
3200
+ return this.index;
3201
+ }
3202
+ const bodies = new Set(keys.filter((key) => key.startsWith(PAYWALL_BODY_KEY_PREFIX)).map((key) => key.slice(PAYWALL_BODY_KEY_PREFIX.length)));
3203
+ const stale = [];
3204
+ for (const key of keys) {
3205
+ if (!key.startsWith(PAYWALL_BODY_META_KEY_PREFIX))
3206
+ continue;
3207
+ const url = key.slice(PAYWALL_BODY_META_KEY_PREFIX.length);
3208
+ const meta = parseMeta(this.read(key));
3209
+ if (meta?.url === url && bodies.has(url))
3210
+ this.index.set(url, meta);
3211
+ else
3212
+ stale.push(url);
3213
+ bodies.delete(url);
3214
+ }
3215
+ // Bodies whose metadata never landed: interrupted writes.
3216
+ this.dropPersisted([...stale, ...bodies]);
3217
+ return this.index;
3218
+ }
3219
+ dropPersisted(urls) {
3220
+ for (const url of urls) {
3221
+ this.index?.delete(url);
3222
+ this.removeKey(PAYWALL_BODY_META_KEY_PREFIX + url);
3223
+ this.removeKey(PAYWALL_BODY_KEY_PREFIX + url);
3224
+ }
3225
+ }
3226
+ read(key) {
3227
+ try {
3228
+ return this.storage().getItem(key);
3229
+ }
3230
+ catch {
3231
+ return null;
3232
+ }
3233
+ }
3234
+ write(key, value) {
3235
+ try {
3236
+ this.storage().setItem(key, value);
3237
+ }
3238
+ catch {
3239
+ return false;
3240
+ }
3241
+ return this.read(key) === value;
3242
+ }
3243
+ removeKey(key) {
3244
+ try {
3245
+ this.storage().removeItem(key);
3246
+ }
3247
+ catch {
3248
+ // Nothing more to do: an entry that cannot be deleted is re-validated on the next read.
3249
+ }
3250
+ }
3251
+ }
3252
+ function isExpired(meta, ttlSeconds, now) {
3253
+ return (now - meta.savedAt) / 1000 >= ttlSeconds;
3254
+ }
3255
+ function parseMeta(raw) {
3256
+ if (raw === null)
3257
+ return null;
3258
+ try {
3259
+ const meta = JSON.parse(raw);
3260
+ return typeof meta?.url === "string" && typeof meta?.savedAt === "number" ? meta : null;
3261
+ }
3262
+ catch {
3263
+ return null;
3264
+ }
3265
+ }
3266
+ const paywallBodyCache = new PaywallBodyCache();
3267
+
3062
3268
  class StorageService {
3063
3269
  constructor() {
3064
3270
  this.memoryStore = {};
@@ -3141,6 +3347,20 @@ class StorageService {
3141
3347
  }
3142
3348
  this.setItem(key, paywalls);
3143
3349
  }
3350
+ /**
3351
+ * Writes the aggregate `api_paywalls` as the list of URLs a per-URL resolution produced, rather
3352
+ * than a second copy of their bodies (NAM-4357): the bodies already live in the per-URL body
3353
+ * cache, and `getPaywalls(API_PAYWALLS)` rebuilds the paywall objects from it. The legacy bulk
3354
+ * path still writes whole objects through `setPaywalls`.
3355
+ */
3356
+ setApiPaywallsFromBodyCache(urls) {
3357
+ const index = { bodyCacheUrls: urls };
3358
+ if (this.getCampaignCacheTtl() === 0) {
3359
+ this.memoryStore[exports.API_PAYWALLS] = JSON.stringify(index);
3360
+ return;
3361
+ }
3362
+ this.setItem(exports.API_PAYWALLS, index);
3363
+ }
3144
3364
  getPaywalls(key) {
3145
3365
  if (key === exports.INITIAL_PAYWALLS) {
3146
3366
  return this.memoryStore[exports.INITIAL_PAYWALLS]
@@ -3149,8 +3369,10 @@ class StorageService {
3149
3369
  }
3150
3370
  if (key === exports.API_PAYWALLS) {
3151
3371
  const memoryValue = this.memoryStore[exports.API_PAYWALLS];
3152
- if (memoryValue != null)
3153
- return JSON.parse(memoryValue);
3372
+ if (memoryValue != null) {
3373
+ const parsed = JSON.parse(memoryValue);
3374
+ return isBodyCacheAggregate(parsed) ? paywallsFromBodyCache(parsed.bodyCacheUrls) : parsed;
3375
+ }
3154
3376
  let stored = this.getItem(exports.API_PAYWALLS);
3155
3377
  if (stored === null)
3156
3378
  return null;
@@ -3162,6 +3384,8 @@ class StorageService {
3162
3384
  return stored;
3163
3385
  }
3164
3386
  }
3387
+ if (isBodyCacheAggregate(stored))
3388
+ return paywallsFromBodyCache(stored.bodyCacheUrls);
3165
3389
  return this.isLegacyTimestampedCache(stored) ? stored.data : stored;
3166
3390
  }
3167
3391
  return this.getItem(key);
@@ -3330,7 +3554,18 @@ class StorageService {
3330
3554
  this.resetItem(exports.API_CAMPAIGN_RULES);
3331
3555
  this.resetItem(exports.API_CAMPAIGN_SESSION_TIMESTAMP);
3332
3556
  }
3557
+ /**
3558
+ * The effective `campaign_cache_ttl`: a `campaignCacheTTL=<n>` namiCommand first, then the API
3559
+ * config, then the initial config. Lives here rather than in `utils/config` because this service
3560
+ * needs it to decide where the aggregate caches go, and `utils/config` already imports this one.
3561
+ */
3333
3562
  getCampaignCacheTtl() {
3563
+ const override = this.getNamiConfig()?.namiCommands
3564
+ ?.filter((cmd) => cmd.startsWith(exports.CAMPAIGN_CACHE_TTL_PREFIX))
3565
+ .map((cmd) => cmd.slice(exports.CAMPAIGN_CACHE_TTL_PREFIX.length))
3566
+ .find((value) => /^\d+$/.test(value));
3567
+ if (override !== undefined)
3568
+ return parseInt(override, 10);
3334
3569
  const config = this.getAppConfig(exports.API_CONFIG)
3335
3570
  || this.getAppConfig(exports.INITIAL_APP_CONFIG);
3336
3571
  return config?.campaign_cache_ttl ?? null;
@@ -3422,6 +3657,28 @@ class StorageService {
3422
3657
  this.memoryStore = {};
3423
3658
  }
3424
3659
  }
3660
+ function isBodyCacheAggregate(value) {
3661
+ return typeof value === "object" && value !== null && Array.isArray(value.bodyCacheUrls);
3662
+ }
3663
+ /**
3664
+ * The paywalls a per-URL resolution produced, decoded from the body cache. A URL whose entry has
3665
+ * since gone (evicted, purged, or a write that never landed) is simply absent.
3666
+ */
3667
+ function paywallsFromBodyCache(urls) {
3668
+ const paywalls = [];
3669
+ for (const url of urls) {
3670
+ const body = paywallBodyCache.bodyFor(url);
3671
+ if (body === null)
3672
+ continue;
3673
+ try {
3674
+ paywalls.push(JSON.parse(body));
3675
+ }
3676
+ catch {
3677
+ paywallBodyCache.remove(url);
3678
+ }
3679
+ }
3680
+ return paywalls;
3681
+ }
3425
3682
  const storageService = new StorageService();
3426
3683
 
3427
3684
  class RetryLimitExceededError extends Error {
@@ -6799,11 +7056,8 @@ const hasCapability = (capability) => {
6799
7056
  || storageService.getAppConfig(exports.INITIAL_APP_CONFIG);
6800
7057
  return appConfig?.capabilities?.includes(capability) ?? false;
6801
7058
  };
6802
- const getCampaignCacheTtl = () => {
6803
- const config = storageService.getAppConfig(exports.API_CONFIG)
6804
- || storageService.getAppConfig(exports.INITIAL_APP_CONFIG);
6805
- return config?.campaign_cache_ttl ?? null;
6806
- };
7059
+ /** The effective `campaign_cache_ttl`, `campaignCacheTTL=<n>` namiCommand first. */
7060
+ const getCampaignCacheTtl = () => storageService.getCampaignCacheTtl();
6807
7061
  /**
6808
7062
  * The effective global journey settings from the current config, coalescing a missing
6809
7063
  * `journey_settings` object — or any missing field — to {@link DEFAULT_JOURNEY_SETTINGS}.
@@ -57510,22 +57764,23 @@ class CampaignRuleRepository {
57510
57764
  static hasPaywallUrls(campaigns) {
57511
57765
  return campaigns.some((c) => c.paywall_url || c.page_urls || c.flow?.page_urls);
57512
57766
  }
57767
+ /**
57768
+ * The campaign rules for this form factor, unvalidated, plus whether they came from the network.
57769
+ * `fromNetwork` is false when the stored rules stood in for a failed fetch (or updates are
57770
+ * disabled): the per-URL body cache must then neither resolve nor evict (NAM-4357).
57771
+ */
57513
57772
  async fetchCampaignRulesRaw() {
57514
- const authDevice = storageService.getDevice();
57515
57773
  if (this.disableCampaignUpdates) {
57516
57774
  logger.info("Campaign updates disabled by configuration. Skipping fetch.");
57517
- return [];
57518
- }
57519
- let campaignRules;
57520
- if (!authDevice?.id) {
57521
- campaignRules = await this.getAnonymousCampaigns();
57522
- }
57523
- else {
57524
- campaignRules = await this.getCampaigns(authDevice.id);
57775
+ return { campaigns: [], fromNetwork: false };
57525
57776
  }
57526
- campaignRules = campaignRules?.filter((cRule) => (cRule.paywall && cRule.form_factors.some((f) => f.form_factor === this.currentFormFactor)) ||
57777
+ const authDevice = storageService.getDevice();
57778
+ const fetched = authDevice?.id
57779
+ ? await this.fetchCampaigns(authDevice.id)
57780
+ : await this.fetchAnonymousCampaigns();
57781
+ const campaigns = fetched.campaigns?.filter((cRule) => (cRule.paywall && cRule.form_factors.some((f) => f.form_factor === this.currentFormFactor)) ||
57527
57782
  (cRule.flow && cRule.form_factors?.some((f) => f.form_factor === this.currentFormFactor)));
57528
- return campaignRules;
57783
+ return { campaigns, fromNetwork: fetched.fromNetwork };
57529
57784
  }
57530
57785
  finalizeCampaignRules(campaignRules, paywalls) {
57531
57786
  // NAM-2457: a server flow rule is kept only when BOTH assets are satisfied —
@@ -57561,10 +57816,19 @@ class CampaignRuleRepository {
57561
57816
  listeners.forEach((handler) => handler(campaigns));
57562
57817
  }
57563
57818
  async getAnonymousCampaigns(sdkVersion = exports.NAMI_SDK_VERSION) {
57819
+ return (await this.fetchAnonymousCampaigns(sdkVersion)).campaigns;
57820
+ }
57821
+ async getCampaigns(deviceId) {
57822
+ return (await this.fetchCampaigns(deviceId)).campaigns;
57823
+ }
57824
+ async fetchAnonymousCampaigns(sdkVersion = exports.NAMI_SDK_VERSION) {
57564
57825
  const path = `campaign_rules/?sdk_version=${sdkVersion}`;
57565
57826
  try {
57566
57827
  const resp = await NamiAPI.instance.fetchAPI(path);
57567
- return mapAnonymousCampaigns(resp.results ?? [], this.splitPosition, this.currentFormFactor);
57828
+ return {
57829
+ campaigns: mapAnonymousCampaigns(resp.results ?? [], this.splitPosition, this.currentFormFactor),
57830
+ fromNetwork: true,
57831
+ };
57568
57832
  }
57569
57833
  catch (error) {
57570
57834
  const data = this.fallbackData();
@@ -57572,23 +57836,23 @@ class CampaignRuleRepository {
57572
57836
  logger.error(handleErrors(error.status, path));
57573
57837
  throw error;
57574
57838
  }
57575
- return data.results;
57839
+ return { campaigns: data.results, fromNetwork: false };
57576
57840
  }
57577
57841
  }
57578
- async getCampaigns(deviceId) {
57842
+ async fetchCampaigns(deviceId) {
57579
57843
  const path = `device/${deviceId}/campaign_rules/`;
57580
- let data;
57581
57844
  try {
57582
- data = await NamiAPI.instance.fetchAPI(path);
57845
+ const data = await NamiAPI.instance.fetchAPI(path);
57846
+ return { campaigns: data?.results || [], fromNetwork: true };
57583
57847
  }
57584
57848
  catch (error) {
57585
- data = this.fallbackData();
57849
+ const data = this.fallbackData();
57586
57850
  if (!data) {
57587
57851
  logger.error(handleErrors(error.status, path));
57588
57852
  throw error;
57589
57853
  }
57854
+ return { campaigns: data.results || [], fromNetwork: false };
57590
57855
  }
57591
- return data?.results || [];
57592
57856
  }
57593
57857
  fallbackData() {
57594
57858
  const storedData = storageService.getCampaignRules(exports.API_CAMPAIGN_RULES)
@@ -57871,6 +58135,15 @@ const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
57871
58135
  * RetryExhaustedError when all attempts and/or the budget are spent.
57872
58136
  */
57873
58137
  async function fetchWithCdnRetry(url, opts = {}) {
58138
+ const { data, retryCount } = await fetchTextWithCdnRetry(url, opts);
58139
+ return { data: JSON.parse(data), retryCount };
58140
+ }
58141
+ /**
58142
+ * {@link fetchWithCdnRetry}'s retry loop, returning the raw 2xx body text instead of parsed JSON.
58143
+ * The per-URL paywall body cache stores those exact bytes, so a cached body is decoded by the
58144
+ * same `JSON.parse` a fetched one is (NAM-4357).
58145
+ */
58146
+ async function fetchTextWithCdnRetry(url, opts = {}) {
57874
58147
  const maxAttempts = opts.maxAttempts ?? DEFAULTS.maxAttempts;
57875
58148
  const baseDelayMs = opts.baseDelayMs ?? DEFAULTS.baseDelayMs;
57876
58149
  const maxDelayMs = opts.maxDelayMs ?? DEFAULTS.maxDelayMs;
@@ -57920,8 +58193,7 @@ async function fetchWithCdnRetry(url, opts = {}) {
57920
58193
  const responseBody = await response.clone().text();
57921
58194
  logger.debug(`[HTTP] Response ${response.status}: ${responseBody}`);
57922
58195
  }
57923
- const data = (await response.json());
57924
- return { data, retryCount: attempt };
58196
+ return { data: await response.text(), retryCount: attempt };
57925
58197
  }
57926
58198
  catch (err) {
57927
58199
  if (err instanceof NonRetryableHttpError)
@@ -57942,7 +58214,43 @@ async function fetchWithCdnRetry(url, opts = {}) {
57942
58214
  throw new RetryExhaustedError(url, attempt, lastStatus);
57943
58215
  }
57944
58216
 
58217
+ /**
58218
+ * Reads the parts of a `campaign_rules` paywall URL the per-URL body cache needs (NAM-4357).
58219
+ *
58220
+ * Both helpers scan the string rather than build a `URL`: the cache is keyed on the exact bytes
58221
+ * the server sent, so nothing here may normalise, re-encode or reject a URL — a malformed one is
58222
+ * simply un-hashed.
58223
+ */
58224
+ /**
58225
+ * The `content_hash` query value, or undefined when it is missing or empty. When the item appears
58226
+ * more than once the first value wins. The value is never parsed or validated: it is a
58227
+ * cache-validity token, not a content selector.
58228
+ */
58229
+ function contentHashOf(url) {
58230
+ const query = url.split("#")[0].split("?")[1];
58231
+ if (!query)
58232
+ return undefined;
58233
+ for (const item of query.split("&")) {
58234
+ const eq = item.indexOf("=");
58235
+ const name = eq === -1 ? item : item.slice(0, eq);
58236
+ if (name !== "content_hash")
58237
+ continue;
58238
+ const value = eq === -1 ? "" : item.slice(eq + 1);
58239
+ return value || undefined;
58240
+ }
58241
+ return undefined;
58242
+ }
58243
+ /** The path segment after `/paywall/` — the paywall id the URL names — or undefined. */
58244
+ function paywallIdOf(url) {
58245
+ const path = url.split(/[?#]/)[0];
58246
+ const match = /\/paywall\/([^/]+)/.exec(path);
58247
+ return match ? match[1] : undefined;
58248
+ }
58249
+
57945
58250
  class PaywallRepository {
58251
+ constructor(bodyCache = paywallBodyCache) {
58252
+ this.bodyCache = bodyCache;
58253
+ }
57946
58254
  async fetchPaywalls() {
57947
58255
  const authDevice = storageService.getDevice();
57948
58256
  let paywalls;
@@ -57991,34 +58299,131 @@ class PaywallRepository {
57991
58299
  }
57992
58300
  return data?.results || [];
57993
58301
  }
57994
- async fetchPaywallByUrl(url) {
57995
- return fetchWithCdnRetry(url);
57996
- }
57997
- async fetchPaywallsByUrls(urls) {
58302
+ /**
58303
+ * Resolves the URL set named by `campaign_rules` into paywall objects — the decision table of
58304
+ * `api-resilience-and-caching.md` § 2.2, run identically at startup and on `refresh()`:
58305
+ *
58306
+ * | URL | entry for the exact URL within TTL | action | provenance |
58307
+ * |-----------|------------------------------------|-------------------------------|--------------------------|
58308
+ * | hashed | yes | serve it, no request | `cacheHit` |
58309
+ * | hashed | no | fetch; store the 2xx body | `fetched` |
58310
+ * | un-hashed | not consulted | fetch; store the 2xx body | `fetched` |
58311
+ * | either | fetch failed, entry within TTL | serve it, keep it | `cacheAfterFetchFailure` |
58312
+ * | either | fetch failed, no entry | no body | counts `failed` |
58313
+ *
58314
+ * Writes the aggregate `api_paywalls` — as the URLs of every body this resolution produced, since
58315
+ * the bodies themselves are already in the body cache — unless it produced none, so an outage
58316
+ * cannot overwrite a good aggregate with an empty one. Eviction is the caller's: it runs only
58317
+ * after a complete `campaign_rules` fetch.
58318
+ */
58319
+ async resolvePaywallsByUrls(urls) {
57998
58320
  const uniqueUrls = [...new Set(urls)];
57999
- const results = await Promise.allSettled(uniqueUrls.map((url) => this.fetchPaywallByUrl(url)));
58000
- const paywalls = [];
58001
- let retryCount = 0;
58002
- for (const result of results) {
58003
- if (result.status === "fulfilled") {
58004
- paywalls.push(result.value.data);
58005
- retryCount += result.value.retryCount;
58006
- }
58007
- else {
58008
- // Note: an exhausted-failure path contributed retries we can't see
58009
- // from the rejected promise, so retryCount under-reports failures.
58010
- // Acceptable for the telemetry signal; revisit if full accounting matters.
58011
- logger.error(`Failed to fetch individual paywall: ${result.reason}`);
58321
+ const ttl = getCampaignCacheTtl();
58322
+ this.bodyCache.evaluateTtl(ttl);
58323
+ const resolutions = await Promise.all(uniqueUrls.map((url) => this.resolvePaywall(url, ttl)));
58324
+ const outcome = {
58325
+ paywalls: [],
58326
+ urls: uniqueUrls.length,
58327
+ hashed: uniqueUrls.filter((url) => contentHashOf(url) !== undefined).length,
58328
+ cacheHits: 0,
58329
+ fetched: 0,
58330
+ cacheAfterFetchFailure: 0,
58331
+ failed: 0,
58332
+ retryCount: 0,
58333
+ };
58334
+ const resolvedUrls = new Map();
58335
+ resolutions.forEach((resolution, i) => {
58336
+ outcome.retryCount += resolution.retryCount;
58337
+ if (!resolution.paywall) {
58338
+ outcome.failed += 1;
58339
+ return;
58012
58340
  }
58341
+ resolvedUrls.set(resolution.paywall, uniqueUrls[i]);
58342
+ if (resolution.paywall.fetchSource === "cacheHit")
58343
+ outcome.cacheHits += 1;
58344
+ else if (resolution.paywall.fetchSource === "cacheAfterFetchFailure")
58345
+ outcome.cacheAfterFetchFailure += 1;
58346
+ else
58347
+ outcome.fetched += 1;
58348
+ });
58349
+ outcome.paywalls = this.validatePaywalls([...resolvedUrls.keys()]);
58350
+ if (outcome.paywalls.length > 0) {
58351
+ storageService.setApiPaywallsFromBodyCache(outcome.paywalls.map((paywall) => resolvedUrls.get(paywall)));
58013
58352
  }
58014
- if (paywalls.length > 0) {
58015
- const valid = this.validatePaywalls(paywalls);
58016
- if (valid.length > 0) {
58017
- storageService.setPaywalls(exports.API_PAYWALLS, valid);
58353
+ return outcome;
58354
+ }
58355
+ /**
58356
+ * Seeds the body cache from the initial-config bundle (§ 2.2 "Seeding"): every **hashed** bundled
58357
+ * URL whose paywall id matches a bundled body is stored under that exact URL unless an entry
58358
+ * already exists. A hash in a bundled URL was minted against the bundled body, so the pair is
58359
+ * exact. Runs at configure, before the first resolution.
58360
+ */
58361
+ seedFromInitialConfig(campaignRules, paywalls) {
58362
+ const bundled = new Map(paywalls.map((paywall) => [paywall.id, paywall]));
58363
+ this.bodyCache.evaluateTtl(getCampaignCacheTtl());
58364
+ let candidates = 0;
58365
+ let seeded = 0;
58366
+ for (const url of new Set(bundledPaywallUrls(campaignRules))) {
58367
+ const contentHash = contentHashOf(url);
58368
+ const id = paywallIdOf(url);
58369
+ const paywall = id ? bundled.get(id) : undefined;
58370
+ if (!contentHash || !paywall)
58371
+ continue;
58372
+ candidates += 1;
58373
+ if (this.bodyCache.seedIfAbsent(url, contentHash, JSON.stringify(paywall)))
58374
+ seeded += 1;
58375
+ }
58376
+ logger.info(`Paywall body cache seeded from initial config: seeded=${seeded}, candidates=${candidates}`);
58377
+ }
58378
+ /** One row of the decision table. Never throws. */
58379
+ async resolvePaywall(url, ttl) {
58380
+ const contentHash = contentHashOf(url);
58381
+ if (contentHash) {
58382
+ const hit = this.decodeCached(url, ttl);
58383
+ if (hit)
58384
+ return { paywall: withProvenance(hit, "cacheHit", contentHash), retryCount: 0 };
58385
+ }
58386
+ let retryCount = 0;
58387
+ try {
58388
+ const response = await this.fetchPaywallText(url);
58389
+ retryCount = response.retryCount;
58390
+ const paywall = decodePaywall(response.data);
58391
+ if (paywall) {
58392
+ this.bodyCache.store(url, contentHash, response.data);
58393
+ return { paywall: withProvenance(paywall, "fetched", contentHash), retryCount };
58018
58394
  }
58019
- return { paywalls: valid, retryCount };
58395
+ logger.error(`Paywall body from ${url} is not a decodable paywall`);
58020
58396
  }
58021
- return { paywalls, retryCount };
58397
+ catch (error) {
58398
+ // Note: an exhausted-failure path contributed retries we can't see from the thrown error,
58399
+ // so retry_count under-reports failures. Acceptable for the telemetry signal.
58400
+ logger.error(`Failed to fetch individual paywall: ${error}`);
58401
+ }
58402
+ const fallback = this.decodeCached(url, ttl);
58403
+ if (fallback) {
58404
+ logger.warn(`Serving the cached body for ${url} after fetch failure (within campaign_cache_ttl)`);
58405
+ return { paywall: withProvenance(fallback, "cacheAfterFetchFailure", contentHash), retryCount };
58406
+ }
58407
+ return { retryCount };
58408
+ }
58409
+ /** The stored body for this exact URL, decoded; an undecodable one is deleted (not eviction). */
58410
+ decodeCached(url, ttl) {
58411
+ const entry = this.bodyCache.validEntry(url, ttl);
58412
+ if (!entry)
58413
+ return undefined;
58414
+ const paywall = decodePaywall(entry.body);
58415
+ if (!paywall) {
58416
+ logger.warn(`Deleting an undecodable cached paywall body for ${url}`);
58417
+ this.bodyCache.remove(url);
58418
+ }
58419
+ return paywall;
58420
+ }
58421
+ fetchPaywallText(url) {
58422
+ if (storageService.getNamiConfig()?.namiCommands?.includes(exports.SIMULATE_PAYWALL_FETCH_FAILURE)) {
58423
+ logger.warn(`${exports.SIMULATE_PAYWALL_FETCH_FAILURE}: failing the fetch for ${url} before the network`);
58424
+ return Promise.reject(new RetryExhaustedError(url, 0));
58425
+ }
58426
+ return fetchTextWithCdnRetry(url);
58022
58427
  }
58023
58428
  validatePaywalls(paywalls) {
58024
58429
  return paywalls.filter((paywall) => {
@@ -58043,6 +58448,33 @@ class PaywallRepository {
58043
58448
  }
58044
58449
  }
58045
58450
  PaywallRepository.instance = new PaywallRepository();
58451
+ function decodePaywall(text) {
58452
+ try {
58453
+ const value = JSON.parse(text);
58454
+ return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
58455
+ }
58456
+ catch {
58457
+ return undefined;
58458
+ }
58459
+ }
58460
+ function withProvenance(paywall, fetchSource, contentHash) {
58461
+ return { ...paywall, fetchSource, ...(contentHash ? { contentHash } : {}) };
58462
+ }
58463
+ /** Every paywall URL a bundled (anonymous-format) rule names, across all of its segments. */
58464
+ function bundledPaywallUrls(campaignRules) {
58465
+ const urls = [];
58466
+ for (const rule of campaignRules) {
58467
+ for (const segment of rule.segments ?? []) {
58468
+ if (segment.paywall_url)
58469
+ urls.push(segment.paywall_url);
58470
+ if (segment.page_urls)
58471
+ urls.push(...Object.values(segment.page_urls));
58472
+ if (segment.flow?.page_urls)
58473
+ urls.push(...Object.values(segment.flow.page_urls));
58474
+ }
58475
+ }
58476
+ return urls;
58477
+ }
58046
58478
 
58047
58479
  class ConfigRepository {
58048
58480
  /**
@@ -61843,7 +62275,10 @@ class NamiRefs {
61843
62275
  }
61844
62276
  }
61845
62277
  if (initconfig?.paywalls) {
61846
- storageService.setPaywalls(exports.INITIAL_PAYWALLS, initconfig.paywalls);
62278
+ storageService.setPaywalls(exports.INITIAL_PAYWALLS, initconfig.paywalls.map((paywall) => ({ ...paywall, fetchSource: "initialConfig" })));
62279
+ if (initconfig.campaign_rules?.length) {
62280
+ PaywallRepository.instance.seedFromInitialConfig(initconfig.campaign_rules, initconfig.paywalls);
62281
+ }
61847
62282
  }
61848
62283
  if (initconfig?.products) {
61849
62284
  storageService.setProducts(exports.INITIAL_PRODUCTS, initconfig.products);
@@ -61910,7 +62345,7 @@ class NamiRefs {
61910
62345
  const paywallRepo = PaywallRepository.instance;
61911
62346
  const startTime = Date.now();
61912
62347
  const campaignRulesStartTime = Date.now();
61913
- const rawCampaigns = await campaignRepo.fetchCampaignRulesRaw();
62348
+ const { campaigns: rawCampaigns, fromNetwork } = await campaignRepo.fetchCampaignRulesRaw();
61914
62349
  const campaignRulesDuration = Date.now() - campaignRulesStartTime;
61915
62350
  // Hydrate flow objects: fetch flow JSON from flow.url for campaigns that
61916
62351
  // use the URL-based format (4.0.0+ since NAM-3493) instead of inline flow.object
@@ -61931,24 +62366,35 @@ class NamiRefs {
61931
62366
  const useIndividual = !campaignRepo.useLegacyPaywallFetch &&
61932
62367
  CampaignRuleRepository.hasPaywallUrls(rawCampaigns);
61933
62368
  let paywalls;
61934
- let retryCount = 0;
62369
+ let outcome = null;
61935
62370
  const paywallsStartTime = Date.now();
61936
- if (useIndividual) {
62371
+ if (useIndividual && !fromNetwork) {
62372
+ // campaign_rules came from the stored copy, so this is not a complete page set: no
62373
+ // resolution and no eviction (api-resilience-and-caching.md § 2.2). The aggregate serves.
62374
+ logger.info("Paywall resolution skipped: campaign_rules were served from the stored copy");
62375
+ paywalls = getApiPaywalls();
62376
+ }
62377
+ else if (useIndividual) {
61937
62378
  const urls = CampaignRuleRepository.extractPaywallUrls(rawCampaigns);
61938
- const result = await paywallRepo.fetchPaywallsByUrls(urls);
61939
- paywalls = result.paywalls;
61940
- retryCount = result.retryCount;
62379
+ outcome = await paywallRepo.resolvePaywallsByUrls(urls);
62380
+ paywalls = outcome.paywalls;
62381
+ paywallBodyCache.reconcile(new Set(urls));
61941
62382
  }
61942
62383
  else {
61943
62384
  paywalls = await paywallRepo.fetchPaywalls();
61944
62385
  }
61945
62386
  const paywallsDuration = Date.now() - paywallsStartTime;
61946
62387
  const totalDuration = Date.now() - startTime;
61947
- if (Nami.instance.maxLogging) {
61948
- logger.info(`Paywall fetch telemetry: strategy=${useIndividual ? "individual" : "bulk"}, ` +
61949
- `count=${paywalls.length}, total=${totalDuration}ms, ` +
62388
+ if (!useIndividual || outcome) {
62389
+ // The line every SDK emits once per resolution; the paywall-cache e2e suites parse it.
62390
+ const cacheFields = outcome
62391
+ ? `, urls=${outcome.urls}, hashed=${outcome.hashed}, cache_hits=${outcome.cacheHits}, ` +
62392
+ `fetched=${outcome.fetched}, cache_after_fetch_failure=${outcome.cacheAfterFetchFailure}, failed=${outcome.failed}`
62393
+ : "";
62394
+ logger.debug(`Paywall fetch telemetry: strategy=${useIndividual ? "individual" : "bulk"}, ` +
62395
+ `count=${paywalls.length}${cacheFields}, total=${totalDuration}ms, ` +
61950
62396
  `campaigns=${campaignRulesDuration}ms, paywalls=${paywallsDuration}ms` +
61951
- `${useIndividual ? `, retry_count=${retryCount}` : ""}`);
62397
+ `${outcome ? `, retry_count=${outcome.retryCount}` : ""}`);
61952
62398
  }
61953
62399
  return campaignRepo.finalizeCampaignRules(rawCampaigns, paywalls);
61954
62400
  }
@@ -62122,6 +62568,8 @@ class Nami {
62122
62568
  static async reset() {
62123
62569
  var _a;
62124
62570
  storageService.clearAll();
62571
+ // The per-URL paywall body cache keeps its own prefixed keys and an in-memory index (NAM-4357).
62572
+ paywallBodyCache.purgeAll();
62125
62573
  clearInMemoryAnonymousMode();
62126
62574
  __classPrivateFieldSet(Nami.instance, _Nami_isInitialized, false, "f");
62127
62575
  __classPrivateFieldGet((_a = Nami.instance), _Nami_removeForegroundListener, "f")?.call(_a);