@paypercut/checkout-js 1.4.0 → 1.4.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.
@@ -1,4 +1,4 @@
1
- const SDK_VERSION = "1.4.0";
1
+ const SDK_VERSION = "1.4.1";
2
2
 
3
3
  const ELEMENTS_APPEARANCE_VARIABLE_NAMES = [
4
4
  'colorPrimary',
@@ -228,6 +228,7 @@ class DeveloperAssistantCoordinator {
228
228
  configurationIdentity: active.configurationIdentity,
229
229
  appearanceScopeId: active.appearanceScopeId,
230
230
  ...(presetTarget?.presets ? { presets: presetTarget.presets } : {}),
231
+ isVisible: () => identityTargets.some((target) => target.isVisible()),
231
232
  isAvailable: () => presetTarget?.isAvailable() ?? true,
232
233
  isAppearanceAvailable: () => appearanceTargets.every((target) => target.isAppearanceAvailable()),
233
234
  didLastAppearancePreviewClearTestData: () => this.lastAppearancePreviewClearedTestData,
@@ -2490,15 +2491,89 @@ function createStyle() {
2490
2491
  return style;
2491
2492
  }
2492
2493
 
2494
+ const DEVELOPER_ASSISTANCE_BROWSER_ARTIFACT = 'paypercut-developer-assistance.iife.min.js';
2495
+ const DEVELOPER_ASSISTANCE_MODULE_ARTIFACT = 'developer-assistance.mjs';
2496
+ const DEVELOPER_ASSISTANCE_LOAD_DIAGNOSTIC = '[Paypercut Elements] developer_assistance_load_failed: Developer Assistant is unavailable. The SDK and Elements continue to work.';
2497
+ const IIFE_ARTIFACT_PATH = /\/paypercut-checkout(?:-dev)?\.iife\.min\.js$/;
2493
2498
  // Rollup rewrites import.meta.url for CommonJS and classic-script bundles, so
2494
2499
  // this remains the URL of the exact core artifact that owns the registration
2495
2500
  // boundary in every supported format.
2496
- typeof __filename === 'string' ? fileUrlFor(__filename) : import.meta.url;
2497
- typeof document === 'undefined' ? null : (document.currentScript?.getAttribute('src') ?? null);
2501
+ const coreArtifactUrl = typeof __filename === 'string' ? fileUrlFor(__filename) : import.meta.url;
2502
+ const classicScriptUrl = typeof document === 'undefined' ? null : (document.currentScript?.getAttribute('src') ?? null);
2503
+ let loadRequested = false;
2504
+ function requestDeveloperAssistance(isInstalled) {
2505
+ if (loadRequested || isInstalled())
2506
+ return;
2507
+ loadRequested = true;
2508
+ if (!canLoadDeveloperAssistance()) {
2509
+ warnDeveloperAssistanceUnavailable();
2510
+ return;
2511
+ }
2512
+ void Promise.resolve()
2513
+ .then(loadDeveloperAssistance)
2514
+ .then(() => {
2515
+ if (!isInstalled())
2516
+ throw new Error();
2517
+ })
2518
+ .catch(warnDeveloperAssistanceUnavailable);
2519
+ }
2520
+ function warnDeveloperAssistanceUnavailable() {
2521
+ console.warn(DEVELOPER_ASSISTANCE_LOAD_DIAGNOSTIC);
2522
+ }
2523
+ function canLoadDeveloperAssistance() {
2524
+ return (typeof document !== 'undefined' &&
2525
+ typeof document.createElement === 'function' &&
2526
+ Boolean(document.head || document.documentElement));
2527
+ }
2528
+ function loadDeveloperAssistance() {
2529
+ if (classicScriptUrl || isClassicScriptArtifact(coreArtifactUrl)) {
2530
+ return loadClassicScript(resolveBrowserArtifactUrl(coreArtifactUrl));
2531
+ }
2532
+ const moduleUrl = new URL(DEVELOPER_ASSISTANCE_MODULE_ARTIFACT, coreArtifactUrl).href;
2533
+ return import(/* @vite-ignore */ moduleUrl);
2534
+ }
2498
2535
  function fileUrlFor(filename) {
2499
2536
  const normalized = filename.replace(/\\/g, '/');
2500
2537
  return new URL(`file://${normalized.startsWith('/') ? '' : '/'}${normalized}`).href;
2501
2538
  }
2539
+ function resolveBrowserArtifactUrl(coreUrl) {
2540
+ const url = new URL(coreUrl);
2541
+ if (IIFE_ARTIFACT_PATH.test(url.pathname)) {
2542
+ const artifactUrl = new URL(DEVELOPER_ASSISTANCE_BROWSER_ARTIFACT, url);
2543
+ artifactUrl.search = url.search;
2544
+ artifactUrl.hash = '';
2545
+ return artifactUrl.href;
2546
+ }
2547
+ if (/\.js$/.test(url.pathname)) {
2548
+ url.pathname = url.pathname.replace(/[^/]+$/, DEVELOPER_ASSISTANCE_BROWSER_ARTIFACT);
2549
+ }
2550
+ else if (/\/cdn\/?$/.test(url.pathname)) {
2551
+ url.pathname = url.pathname.replace(/\/cdn\/?$/, `/dist/${DEVELOPER_ASSISTANCE_BROWSER_ARTIFACT}`);
2552
+ }
2553
+ else {
2554
+ url.pathname = `${url.pathname.replace(/\/$/, '')}/dist/${DEVELOPER_ASSISTANCE_BROWSER_ARTIFACT}`;
2555
+ }
2556
+ url.hash = '';
2557
+ return url.href;
2558
+ }
2559
+ function isClassicScriptArtifact(url) {
2560
+ try {
2561
+ return IIFE_ARTIFACT_PATH.test(new URL(url).pathname);
2562
+ }
2563
+ catch {
2564
+ return false;
2565
+ }
2566
+ }
2567
+ function loadClassicScript(src) {
2568
+ return new Promise((resolve, reject) => {
2569
+ const script = document.createElement('script');
2570
+ script.async = true;
2571
+ script.src = src;
2572
+ script.addEventListener('load', () => resolve(), { once: true });
2573
+ script.addEventListener('error', () => reject(new Error()), { once: true });
2574
+ (document.head || document.documentElement).appendChild(script);
2575
+ });
2576
+ }
2502
2577
 
2503
2578
  const registryKey = Symbol.for('paypercut.checkout-js.developer-assistance');
2504
2579
  function getRegistry() {
@@ -2519,6 +2594,45 @@ function safelyUnregister(entry) {
2519
2594
  }
2520
2595
  entry.delegate = null;
2521
2596
  }
2597
+ function safelyAttach(entry, provider) {
2598
+ if (!entry.registered || entry.delegate)
2599
+ return;
2600
+ try {
2601
+ entry.delegate = provider.register(entry.target);
2602
+ }
2603
+ catch {
2604
+ // A target can register before its hosted component is ready. Keep the
2605
+ // provider available for the other targets and retry this one on refresh.
2606
+ }
2607
+ }
2608
+ function isTargetVisible(entry) {
2609
+ try {
2610
+ return entry.target.isVisible?.() ?? true;
2611
+ }
2612
+ catch {
2613
+ // Developer Assistance is optional. A stale owner node must never make the
2614
+ // merchant page fail while its visibility is being reconciled.
2615
+ return false;
2616
+ }
2617
+ }
2618
+ function syncAttachment(entry, registry) {
2619
+ if (!entry.registered)
2620
+ return;
2621
+ if (!isTargetVisible(entry)) {
2622
+ safelyUnregister(entry);
2623
+ return;
2624
+ }
2625
+ if (registry.provider?.version === SDK_VERSION) {
2626
+ safelyAttach(entry, registry.provider);
2627
+ return;
2628
+ }
2629
+ requestDeveloperAssistance(() => hasAttachedDeveloperAssistant(registry));
2630
+ }
2631
+ function hasAttachedDeveloperAssistant(registry) {
2632
+ if (registry.provider?.version !== SDK_VERSION)
2633
+ return false;
2634
+ return Array.from(registry.registrations).some((entry) => entry.registered && entry.delegate !== null);
2635
+ }
2522
2636
  function installDeveloperAssistantProvider(provider) {
2523
2637
  const registry = getRegistry();
2524
2638
  const incompatibleCoreVersion = Array.from(registry.coreVersions).find((version) => version !== provider.version);
@@ -2531,21 +2645,8 @@ function installDeveloperAssistantProvider(provider) {
2531
2645
  throw new Error(`[Paypercut Elements] Developer Assistance ${provider.version} cannot replace ${registry.provider.version}.`);
2532
2646
  }
2533
2647
  registry.provider = provider;
2534
- const attached = [];
2535
- try {
2536
- for (const entry of registry.registrations) {
2537
- if (!entry.registered)
2538
- continue;
2539
- entry.delegate = provider.register(entry.target);
2540
- attached.push(entry);
2541
- }
2542
- }
2543
- catch (error) {
2544
- for (const entry of attached.reverse()) {
2545
- safelyUnregister(entry);
2546
- }
2547
- registry.provider = null;
2548
- throw error;
2648
+ for (const entry of registry.registrations) {
2649
+ syncAttachment(entry, registry);
2549
2650
  }
2550
2651
  }
2551
2652