@mailto.foo/sdk 1.4.2 → 1.4.3

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/cdn.js CHANGED
@@ -1,5 +1,25 @@
1
1
  import { Mailto } from "./index.js";
2
- const FORM_ACTION_PATTERN = /\/(pk_[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/;
2
+ const FORM_ACTION_PATTERN = /\/((?:pk_|ph_public_)[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/;
3
+ // Broader than FORM_ACTION_PATTERN: catches forms pointed at a mailto.foo subscribe
4
+ // endpoint whose key prefix we don't (yet) recognize, so we can stop the native
5
+ // submission instead of letting the browser navigate the whole page to a 404.
6
+ const SUBSCRIBE_PATH_PATTERN = /\/[A-Za-z0-9_-]+\/subscribe\/?(?:[?#].*)?$/;
7
+ const MAILTO_FOO_HOST_PATTERN = /(?:^|\.)mailto-foo\.|(?:^|\.)mailto\.foo$/;
8
+ const looksLikeMailtoFooSubscribeForm = (action) => {
9
+ try {
10
+ const url = new URL(action, window.location.href);
11
+ return MAILTO_FOO_HOST_PATTERN.test(url.hostname) && SUBSCRIBE_PATH_PATTERN.test(url.pathname);
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ };
17
+ const guardForm = (form) => {
18
+ form.addEventListener("submit", (event) => {
19
+ event.preventDefault();
20
+ console.warn(`mailto.foo: unrecognized publish key in form action "${form.action}" — submission blocked to avoid a full-page redirect. Update the SDK or check the form action URL.`);
21
+ });
22
+ };
3
23
  const collectFormData = (form) => {
4
24
  const data = {};
5
25
  for (const [key, value] of new FormData(form).entries()) {
@@ -71,6 +91,8 @@ const enhanceForms = () => {
71
91
  const match = form.action.match(FORM_ACTION_PATTERN);
72
92
  if (match?.[1])
73
93
  enhanceForm(form, match[1]);
94
+ else if (looksLikeMailtoFooSubscribeForm(form.action))
95
+ guardForm(form);
74
96
  }
75
97
  };
76
98
  if (document.readyState === "lo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let n=new AbortController,t=setTimeout(()=>n.abort(),5e3);try{let o=await fetch(`${u}/config/${e}/config`,{signal:n.signal});if(clearTimeout(t),!o.ok){if(o.status===404)throw new Error(`Invalid publish key: ${e}`);let i=(o.headers.get("content-type")??"").includes("application/json")?(await o.json()).error??`Config fetch failed: ${o.status}`:await o.text();throw new Error(i)}return o.json()}catch(o){throw clearTimeout(t),o}},a=null,g=()=>window.turnstile?Promise.resolve():a||(a=new Promise((e,n)=>{let t=document.createElement("script");t.src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",t.async=!0,t.defer=!0,t.onload=()=>e(),t.onerror=o=>{a=null,n(o)},document.head.appendChild(t)}),a),y=e=>new Promise((n,t)=>{let o=document.createElement("div");o.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(0,0,0,0.5)"].join(";");let s=document.createElement("div");o.appendChild(s),document.body.appendChild(o);let i,c=()=>{try{window.turnstile.remove(i)}catch{}try{document.body.removeChild(o)}catch{}};i=window.turnstile.render(s,{sitekey:e,callback:r=>{c(),n(r)},"error-callback":()=>{c(),t(new Error("Turnstile verification failed"))},"expired-callback":()=>{c(),t(new Error("Turnstile token expired"))}})}),f=async e=>{await g();let n=document.createElement("div");n.style.cssText="position:fixed;bottom:0;right:0;z-index:9999;visibility:hidden;",document.body.appendChild(n);let t,o=()=>{try{window.turnstile.remove(t)}catch{}try{document.body.removeChild(n)}catch{}};return await new Promise((i,c)=>{t=window.turnstile.render(n,{sitekey:e,appearance:"interaction-only",callback:r=>{o(),i(r)},"error-callback":()=>{o(),i(null)},"expired-callback":()=>{o(),c(new Error("Turnstile token expired"))}})})??y(e)},l=class{publishKey;constructor(n){this.publishKey=n.publishKey}async subscribe(n){let t=await p(this.publishKey),o=t.turnstileSiteKey?await f(t.turnstileSiteKey):void 0,s={data:n,config:{publishKey:this.publishKey,token:o}},i=await fetch(`${u}/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!i.ok){let r=(i.headers.get("content-type")??"").includes("application/json")?(await i.json()).error??"Subscription failed":await i.text();throw new Error(r)}}};var h=/\/(pk_[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/,w=e=>{let n={};for(let[t,o]of new FormData(e).entries())typeof o=="string"&&(n[t]=o);return n},E="mailto-foo-error",T="mailto-foo-success",S="mailto-foo-button",C="mailto:submitting",v="mailto:success",k="mailto:error",d=(e,n)=>{let t=e.querySelector(`.${n}`);if(t)return t;let o=document.createElement("div");return o.className=n,e.appendChild(o),o},x=e=>{let n=new Set;for(let t of e.querySelectorAll(`.${S}, button[type="submit"], input[type="submit"]`))(t instanceof HTMLButtonElement||t instanceof HTMLInputElement)&&n.add(t);return[...n]},m=(e,n)=>{for(let t of e)t.disabled=n,n?t.setAttribute("data-loading","true"):t.removeAttribute("data-loading")},L=(e,n)=>{let t=new l({publishKey:n}),o=d(e,E),s=d(e,T),i=x(e);e.addEventListener("submit",async c=>{c.preventDefault(),m(i,!0),e.dispatchEvent(new CustomEvent(C,{bubbles:!0}));try{await t.subscribe(w(e)),o.textContent="",s.textContent="Thanks for subscribing!",e.reset(),e.dispatchEvent(new CustomEvent(v,{bubbles:!0}))}catch(r){s.textContent="",o.textContent=r instanceof Error?r.message:String(r),e.dispatchEvent(new CustomEvent(k,{bubbles:!0,detail:{error:r}}))}finally{m(i,!1)}})},b=()=>{for(let e of document.querySelectorAll("form[action]")){let n=e.action.match(h);n?.[1]&&L(e,n[1])}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",b):b();window.Mailto=l;})();
1
+ "use strict";(()=>{var u="https://api-mailto-foo.dan-7bc.workers.dev",p=async t=>{let e=new AbortController,n=setTimeout(()=>e.abort(),5e3);try{let o=await fetch(`${u}/config/${t}/config`,{signal:e.signal});if(clearTimeout(n),!o.ok){if(o.status===404)throw new Error(`Invalid publish key: ${t}`);let i=(o.headers.get("content-type")??"").includes("application/json")?(await o.json()).error??`Config fetch failed: ${o.status}`:await o.text();throw new Error(i)}return o.json()}catch(o){throw clearTimeout(n),o}},a=null,g=()=>window.turnstile?Promise.resolve():a||(a=new Promise((t,e)=>{let n=document.createElement("script");n.src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",n.async=!0,n.defer=!0,n.onload=()=>t(),n.onerror=o=>{a=null,e(o)},document.head.appendChild(n)}),a),f=t=>new Promise((e,n)=>{let o=document.createElement("div");o.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(0,0,0,0.5)"].join(";");let s=document.createElement("div");o.appendChild(s),document.body.appendChild(o);let i,c=()=>{try{window.turnstile.remove(i)}catch{}try{document.body.removeChild(o)}catch{}};i=window.turnstile.render(s,{sitekey:t,callback:r=>{c(),e(r)},"error-callback":()=>{c(),n(new Error("Turnstile verification failed"))},"expired-callback":()=>{c(),n(new Error("Turnstile token expired"))}})}),y=async t=>{await g();let e=document.createElement("div");e.style.cssText="position:fixed;bottom:0;right:0;z-index:9999;visibility:hidden;",document.body.appendChild(e);let n,o=()=>{try{window.turnstile.remove(n)}catch{}try{document.body.removeChild(e)}catch{}};return await new Promise((i,c)=>{n=window.turnstile.render(e,{sitekey:t,appearance:"interaction-only",callback:r=>{o(),i(r)},"error-callback":()=>{o(),i(null)},"expired-callback":()=>{o(),c(new Error("Turnstile token expired"))}})})??f(t)},l=class{publishKey;constructor(e){this.publishKey=e.publishKey}async subscribe(e){let n=await p(this.publishKey),o=n.turnstileSiteKey?await y(n.turnstileSiteKey):void 0,s={data:e,config:{publishKey:this.publishKey,token:o}},i=await fetch(`${u}/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!i.ok){let r=(i.headers.get("content-type")??"").includes("application/json")?(await i.json()).error??"Subscription failed":await i.text();throw new Error(r)}}};var h=/\/((?:pk_|ph_public_)[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/,w=/\/[A-Za-z0-9_-]+\/subscribe\/?(?:[?#].*)?$/,E=/(?:^|\.)mailto-foo\.|(?:^|\.)mailto\.foo$/,T=t=>{try{let e=new URL(t,window.location.href);return E.test(e.hostname)&&w.test(e.pathname)}catch{return!1}},S=t=>{t.addEventListener("submit",e=>{e.preventDefault(),console.warn(`mailto.foo: unrecognized publish key in form action "${t.action}" \u2014 submission blocked to avoid a full-page redirect. Update the SDK or check the form action URL.`)})},C=t=>{let e={};for(let[n,o]of new FormData(t).entries())typeof o=="string"&&(e[n]=o);return e},v="mailto-foo-error",k="mailto-foo-success",L="mailto-foo-button",A="mailto:submitting",P="mailto:success",x="mailto:error",d=(t,e)=>{let n=t.querySelector(`.${e}`);if(n)return n;let o=document.createElement("div");return o.className=e,t.appendChild(o),o},M=t=>{let e=new Set;for(let n of t.querySelectorAll(`.${L}, button[type="submit"], input[type="submit"]`))(n instanceof HTMLButtonElement||n instanceof HTMLInputElement)&&e.add(n);return[...e]},m=(t,e)=>{for(let n of t)n.disabled=e,e?n.setAttribute("data-loading","true"):n.removeAttribute("data-loading")},_=(t,e)=>{let n=new l({publishKey:e}),o=d(t,v),s=d(t,k),i=M(t);t.addEventListener("submit",async c=>{c.preventDefault(),m(i,!0),t.dispatchEvent(new CustomEvent(A,{bubbles:!0}));try{await n.subscribe(C(t)),o.textContent="",s.textContent="Thanks for subscribing!",t.reset(),t.dispatchEvent(new CustomEvent(P,{bubbles:!0}))}catch(r){s.textContent="",o.textContent=r instanceof Error?r.message:String(r),t.dispatchEvent(new CustomEvent(x,{bubbles:!0,detail:{error:r}}))}finally{m(i,!1)}})},b=()=>{for(let t of document.querySelectorAll("form[action]")){let e=t.action.match(h);e?.[1]?_(t,e[1]):T(t.action)&&S(t)}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",b):b();window.Mailto=l;})();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@mailto.foo/sdk",
3
3
  "type": "module",
4
4
  "description": "Headless email subscription and contact form SDK for mailto.foo",
5
- "version": "1.4.2",
5
+ "version": "1.4.3",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "repository": {