mcp-scraper 0.95.1 → 0.96.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/CHANGELOG.md +98 -39
- package/README.md +25 -7
- package/THIRD_PARTY_NOTICES.html +203 -0
- package/dist/actions-YCMJY3PF.js +1 -0
- package/dist/artifacts-4ZDNP2JW.js +1 -0
- package/dist/artifacts-N62OPIT5.js +1 -0
- package/dist/artifacts-PSADD5OC.js +1 -0
- package/dist/artifacts-RUR3OHAK.js +1 -0
- package/dist/bin/api-server.js +1 -1
- package/dist/bin/mcp-scraper-cli.js +22 -22
- package/dist/bin/mcp-scraper-core.js +1 -1
- package/dist/bin/mcp-scraper-install.js +1 -1
- package/dist/bin/mcp-stdio-server.js +1 -1
- package/dist/bin/paa-harvest.js +1 -1
- package/dist/blob-store-GIH26EDO.js +1 -0
- package/dist/bundle-TTJP6DHU.js +1 -0
- package/dist/chunk-24YDQ3EO.js +5 -0
- package/dist/chunk-2PRH4Z3N.js +181 -0
- package/dist/chunk-3F7AFM53.js +1 -0
- package/dist/chunk-4HZXATHB.js +27 -0
- package/dist/chunk-55ZANTHL.js +3 -0
- package/dist/chunk-5BKSKLHX.js +1 -0
- package/dist/chunk-63VWBMYR.js +3 -0
- package/dist/chunk-6N5EC7ZO.js +1 -0
- package/dist/chunk-6SNOUSSZ.js +182 -0
- package/dist/chunk-7POHNTVW.js +1 -0
- package/dist/chunk-7SR7MEU5.js +62 -0
- package/dist/chunk-7UHDOIUE.js +1 -0
- package/dist/chunk-ARSQZCTJ.js +945 -0
- package/dist/chunk-B3JRL45T.js +1 -0
- package/dist/chunk-B4ZKPLSP.js +1 -0
- package/dist/chunk-C7MGJUTU.js +1 -0
- package/dist/{chunk-W2BVJ7S2.js → chunk-CNSVZTTT.js} +4 -4
- package/dist/chunk-DGAW7FD7.js +12 -0
- package/dist/chunk-FRXKMOXI.js +41 -0
- package/dist/chunk-GVYGAZV7.js +1 -0
- package/dist/chunk-HCK6PLM3.js +3 -0
- package/dist/{chunk-I3HNHCIJ.js → chunk-HJIOH7HF.js} +199 -84
- package/dist/chunk-HNNYXMSI.js +1 -0
- package/dist/chunk-HUNSEGUN.js +57 -0
- package/dist/chunk-IFPION5D.js +121 -0
- package/dist/{chunk-DYDAQQMC.js → chunk-JAV5V3JT.js} +41 -8
- package/dist/chunk-JMZIDHYQ.js +2 -0
- package/dist/{chunk-FN7U6KRI.js → chunk-JW4GDY7S.js} +1 -1
- package/dist/chunk-KXJCGV3H.js +1 -0
- package/dist/chunk-LFRAA3MY.js +4 -0
- package/dist/{chunk-Y6STAKF3.js → chunk-M5QHXNFZ.js} +3 -3
- package/dist/chunk-MVMWPKRC.js +2 -0
- package/dist/chunk-NALXKTUU.js +1 -0
- package/dist/chunk-NNOX3FYG.js +198 -0
- package/dist/chunk-OB6E6UTD.js +1 -0
- package/dist/chunk-OILOHQNL.js +2 -0
- package/dist/chunk-OLYPLX5C.js +1 -0
- package/dist/chunk-QER2ELZP.js +1 -0
- package/dist/chunk-QWE7VKIX.js +1 -0
- package/dist/chunk-R5LJL6IR.js +225 -0
- package/dist/chunk-RDHGQ2WZ.js +1 -0
- package/dist/chunk-SDQW7IJN.js +1 -0
- package/dist/chunk-SEZETSB3.js +1 -0
- package/dist/chunk-SNJR5N36.js +1 -0
- package/dist/chunk-TPN25AFZ.js +159 -0
- package/dist/{chunk-AK6HVF5F.js → chunk-U4WIJZCX.js} +1 -1
- package/dist/chunk-UIOSADPB.js +1718 -0
- package/dist/{chunk-DYKSPHHR.js → chunk-UNEJYZ4T.js} +1 -1
- package/dist/chunk-VAC3SQ5T.js +16 -0
- package/dist/chunk-VVSHZV5S.js +73 -0
- package/dist/chunk-WNUXMPVX.js +1 -0
- package/dist/chunk-WP3QS4CJ.js +1 -0
- package/dist/chunk-ZPQNY5TI.js +1 -0
- package/dist/contracts-GUXGFCVV.js +1 -0
- package/dist/core-schema-423BSTE6.js +936 -0
- package/dist/dispatch-3HY4735M.js +1 -0
- package/dist/index.cjs +104 -214
- package/dist/index.d.cts +192 -180
- package/dist/index.d.ts +192 -180
- package/dist/index.js +1 -1
- package/dist/media-artifacts-67CJWI7O.js +1 -0
- package/dist/metering-ZH2P62IC.js +1 -0
- package/dist/migrations-3AKEC5QE.js +1 -0
- package/dist/queue-HUP2GNXF.js +1 -0
- package/dist/reconciliation-ANXNJWME.js +1 -0
- package/dist/report-artifact-offload-6OOY2PTM.js +1 -0
- package/dist/repository-4EC5EXVN.js +1 -0
- package/dist/{location-data-repository-NWWKDYG6.js → repository-CYXVMYAI.js} +1 -1
- package/dist/server-VQU6S24A.js +5825 -0
- package/dist/settlement-6CCE2PDH.js +1 -0
- package/dist/worker-5G5BK23B.js +1 -0
- package/package.json +18 -138
- package/dist/analytics-repository-A7VMA24D.js +0 -1
- package/dist/chunk-56C243PX.js +0 -2595
- package/dist/chunk-6ND5AFV5.js +0 -27
- package/dist/chunk-6NE4OIXR.js +0 -6
- package/dist/chunk-DOJMTFLB.js +0 -1
- package/dist/chunk-EDQ57S4P.js +0 -1
- package/dist/chunk-FCJXL4PU.js +0 -182
- package/dist/chunk-GPSPSJBJ.js +0 -1
- package/dist/chunk-HE45FFBU.js +0 -1
- package/dist/chunk-HUV2WTRW.js +0 -1
- package/dist/chunk-L552AQQY.js +0 -84
- package/dist/chunk-MVDMZ3N2.js +0 -1282
- package/dist/chunk-NA76FFGD.js +0 -73
- package/dist/chunk-NZSDHDX3.js +0 -1
- package/dist/chunk-OS4XFDIG.js +0 -21
- package/dist/chunk-QQ7OD7BJ.js +0 -100
- package/dist/chunk-RPA5ADXW.js +0 -16
- package/dist/chunk-TMB56NCA.js +0 -1
- package/dist/chunk-WO3N5FH2.js +0 -5
- package/dist/chunk-XPEJB4BZ.js +0 -1
- package/dist/chunk-YK4OAZRQ.js +0 -4
- package/dist/chunk-YWUNN24S.js +0 -1
- package/dist/chunk-ZJTUAWZA.js +0 -19
- package/dist/chunk-ZPK5LYEN.js +0 -1565
- package/dist/chunk-ZUGTYD2I.js +0 -63
- package/dist/db-B6RNXKEI.js +0 -1
- package/dist/extract-bundle-X7SRN63Y.js +0 -26
- package/dist/gmail-service-FZVJBSUT.js +0 -1
- package/dist/lead-list-enrichment-repository-SXYSS7ZX.js +0 -1
- package/dist/operation-metering-OBKSWJVJ.js +0 -1
- package/dist/server-JV6L7F2X.js +0 -5014
- package/dist/site-extract-repository-UVE7TWJO.js +0 -1
- package/dist/stripe-event-worker-2LSUVQOM.js +0 -1
- package/dist/worker-XPIO5TAV.js +0 -1
- /package/dist/{chunk-YGBTTW5D.js → chunk-2IATUJO6.js} +0 -0
- /package/dist/{chunk-6DTXIZY2.js → chunk-4ZVO6GX4.js} +0 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import{a as g}from"./chunk-NALXKTUU.js";import{Pool as B}from"pg";var u=class extends Error{constructor(t,s,n=400){super(s);this.code=t;this.status=n;this.name="AnalyticsRepositoryError"}code;status};var D=null;function K(){let i=process.env.ANALYTICS_DATABASE_URL?.trim();if(!i)throw new u("analytics_database_unavailable","Site Analytics is not configured.",503);return i}function E(){return D||(D=new B({connectionString:K(),max:8})),D}import{randomBytes as _e,randomUUID as p}from"crypto";import{createHash as Z,randomUUID as v}from"crypto";import{createCipheriv as X,createDecipheriv as J,createHmac as x,randomBytes as Y,scryptSync as z,timingSafeEqual as V}from"crypto";var N="xray1";function G(){return process.env.ANALYTICS_RESTRICTED_DATA_SECRET?.trim()||g()}function T(i){return z(G(),`xray-restricted:${i}:v1`,32)}function k(i,e){let t=e.trim().slice(0,80);if(!t)throw new Error("analytics encryption purpose required");let s=Y(12),n=X("aes-256-gcm",T(t),s);n.setAAD(Buffer.from(`${N}:${t}`,"utf8"));let r=Buffer.concat([n.update(i,"utf8"),n.final()]),o=n.getAuthTag();return[N,Buffer.from(t,"utf8").toString("base64url"),s.toString("base64url"),o.toString("base64url"),r.toString("base64url")].join(".")}function U(i,e){try{let[t,s,n,r,o]=i.split(".");if(t!==N||!s||!n||!r||!o)return null;let a=Buffer.from(s,"base64url").toString("utf8");if(a!==e)return null;let d=J("aes-256-gcm",T(a),Buffer.from(n,"base64url"));return d.setAAD(Buffer.from(`${N}:${a}`,"utf8")),d.setAuthTag(Buffer.from(r,"base64url")),Buffer.concat([d.update(Buffer.from(o,"base64url")),d.final()]).toString("utf8")}catch{return null}}function Ne(i,e){return x("sha256",T(`identity:${i}`)).update(e.trim().toLowerCase()).digest("hex")}function ve(i,e,t,s){return x("sha256",s).update(`${i}.${e}.${t}`).digest("hex")}function Ae(i,e){if(!i||i.length!==e.length)return!1;try{return V(Buffer.from(i),Buffer.from(e))}catch{return!1}}var M="enhanced_matching",Q=90,ee=10,f=class extends Error{constructor(t,s,n){super(s);this.code=t;this.status=n;this.name="AnalyticsIdentityProfileError"}code;status};function A(i,e){return i?.trim().replace(/\s+/g," ").slice(0,e)||null}function ie(i){if(!i)return null;let[e,t]=i.split("@");return!e||!t?null:`${e.slice(0,1)}${"*".repeat(Math.min(6,Math.max(2,e.length-1)))}@${t}`}function te(i){if(!i)return null;let e=i.replace(/\D/g,"");return e.length<4?null:`***-***-${e.slice(-4)}`}function O(i){return Z("sha256").update(i).digest("hex")}async function h(i,e){if(!e.signals.length)return;let t=e.signals.map(r=>r.kind),s=e.signals.map(r=>r.valueHmac);if((await i.query(`SELECT 1 FROM analytics_identity_tombstones
|
|
2
|
+
WHERE site_id=$1 AND (kind,value_hmac) IN (
|
|
3
|
+
SELECT * FROM unnest($2::text[],$3::text[])
|
|
4
|
+
) LIMIT 1`,[e.siteId,t,s])).rowCount)throw new f("analytics_identity_deleted","This identity cannot be automatically recreated.",410)}async function $(i,e){let t=A(e.displayName,160),s=A(e.email,320)?.toLowerCase()??null,n=A(e.phone,40);if(!t&&!s&&!n)return!1;let o=(await i.query(`SELECT p.public_ref FROM analytics_people p
|
|
5
|
+
JOIN analytics_identity_namespaces n ON n.id=p.namespace_id AND n.site_id=p.site_id AND n.status='approved'
|
|
6
|
+
WHERE p.id=$1 AND p.site_id=$2`,[e.personId,e.siteId])).rows[0]?.public_ref;if(!o)return!1;let a=[...new Set([o,...e.consentSubjectRefs].filter(Boolean))],l=(await i.query(`SELECT s.latest_receipt_id::text FROM analytics_consent_current_state s
|
|
7
|
+
JOIN analytics_consent_receipts r ON r.id=s.latest_receipt_id AND r.site_id=s.site_id
|
|
8
|
+
WHERE s.site_id=$1 AND s.subject_ref=ANY($2::text[])
|
|
9
|
+
AND s.site_analytics=true AND s.enhanced_matching=true AND s.gpc=false
|
|
10
|
+
AND r.action NOT IN ('withdraw','gpc')
|
|
11
|
+
AND (s.expires_at IS NULL OR s.expires_at > now())
|
|
12
|
+
ORDER BY s.effective_at DESC LIMIT 1`,[e.siteId,a])).rows[0]?.latest_receipt_id;if(!l)return!1;let m=e.verifiedAt&&Number.isFinite(Date.parse(e.verifiedAt))?new Date(e.verifiedAt).toISOString():new Date().toISOString(),_=k(JSON.stringify({schemaVersion:1,displayName:t,email:s,phone:n,evidenceType:e.evidenceType,verifiedAt:m}),"identity-profile");return await i.query(`INSERT INTO analytics_restricted_data(id,site_id,person_id,purpose,subject_ref,payload_ciphertext,expires_at,consent_receipt_id)
|
|
13
|
+
VALUES($1,$2,$3,$4,$5,$6,now()+($7::int * interval '1 day'),$8)
|
|
14
|
+
ON CONFLICT(site_id,purpose,subject_ref) DO UPDATE SET
|
|
15
|
+
person_id=EXCLUDED.person_id,payload_ciphertext=EXCLUDED.payload_ciphertext,
|
|
16
|
+
expires_at=EXCLUDED.expires_at,consent_receipt_id=EXCLUDED.consent_receipt_id,created_at=now()`,[v(),e.siteId,e.personId,M,o,_,Q,l]),!0}async function Re(i,e){let t=e.now??new Date,s=await i.query("SELECT role FROM analytics_site_grants WHERE site_id=$1 AND user_id=$2",[e.siteId,e.actorUserId]);if(!s.rowCount)throw new f("analytics_site_not_found","Site not found.",404);if(!["owner","editor"].includes(s.rows[0].role))throw new f("analytics_forbidden","Editor access is required.",403);let n=await i.query(`SELECT count(*)::text AS count FROM analytics_identity_profile_access_audits
|
|
17
|
+
WHERE site_id=$1 AND actor_user_id=$2 AND created_at > $3::timestamptz - interval '1 minute'`,[e.siteId,e.actorUserId,t.toISOString()]);if(Number(n.rows[0]?.count??0)>=ee)throw await i.query(`INSERT INTO analytics_identity_profile_access_audits(id,site_id,actor_user_id,subject_ref_hmac,purpose,result,created_at)
|
|
18
|
+
VALUES($1,$2,$3,$4,'journey_identity_display','rate_limited',$5)`,[v(),e.siteId,e.actorUserId,O(e.requestedPersonRef),t.toISOString()]),new f("analytics_identity_profile_rate_limited","Identity profile rate limit exceeded.",429);let r=async _=>{throw await i.query(`INSERT INTO analytics_identity_profile_access_audits(id,site_id,actor_user_id,subject_ref_hmac,purpose,result,created_at)
|
|
19
|
+
VALUES($1,$2,$3,$4,'journey_identity_display',$5,$6)`,[v(),e.siteId,e.actorUserId,O(e.requestedPersonRef),_,t.toISOString()]),new f("analytics_identity_profile_not_found","Identity profile not found.",404)};if(!e.personId)return r("not_found");let o=await i.query(`SELECT p.public_ref,r.payload_ciphertext,r.expires_at
|
|
20
|
+
FROM analytics_people p
|
|
21
|
+
JOIN analytics_restricted_data r ON r.person_id=p.id AND r.site_id=p.site_id
|
|
22
|
+
AND r.purpose=$3 AND r.subject_ref=p.public_ref AND r.expires_at>$4
|
|
23
|
+
JOIN analytics_consent_current_state s ON s.site_id=p.site_id AND s.latest_receipt_id=r.consent_receipt_id
|
|
24
|
+
AND s.site_analytics=true AND s.enhanced_matching=true AND s.gpc=false
|
|
25
|
+
AND (s.expires_at IS NULL OR s.expires_at>$4)
|
|
26
|
+
JOIN analytics_consent_receipts c ON c.id=s.latest_receipt_id AND c.site_id=s.site_id
|
|
27
|
+
AND c.action NOT IN ('withdraw','gpc')
|
|
28
|
+
WHERE p.id=$1 AND p.site_id=$2
|
|
29
|
+
AND NOT EXISTS (SELECT 1 FROM analytics_identity_tombstones t WHERE t.site_id=p.site_id AND t.person_id=p.id)
|
|
30
|
+
LIMIT 1`,[e.personId,e.siteId,M,t.toISOString()]);if(!o.rowCount)return r("not_found");let a=U(o.rows[0].payload_ciphertext,"identity-profile");if(!a)return r("unavailable");let d;try{d=JSON.parse(a)}catch{return r("unavailable")}let l=d.evidenceType;if(!["first_party_form","authenticated_user","crm_contact"].includes(String(l)))return r("unavailable");let m={personRef:e.requestedPersonRef,displayName:A(typeof d.displayName=="string"?d.displayName:void 0,160),maskedEmail:ie(typeof d.email=="string"?d.email:null),maskedPhone:te(typeof d.phone=="string"?d.phone:null),evidenceType:l,verifiedAt:typeof d.verifiedAt=="string"&&Number.isFinite(Date.parse(d.verifiedAt))?new Date(d.verifiedAt).toISOString():t.toISOString()};return await i.query(`INSERT INTO analytics_identity_profile_access_audits(id,site_id,person_id,actor_user_id,subject_ref_hmac,purpose,result,created_at)
|
|
31
|
+
VALUES($1,$2,$3,$4,$5,'journey_identity_display','returned_masked',$6)`,[v(),e.siteId,e.personId,e.actorUserId,O(e.requestedPersonRef),t.toISOString()]),m}import{randomUUID as re}from"crypto";import{createHmac as se}from"crypto";var ne=new Set(["fbclid","gclid","gbraid","wbraid","ttclid","rdt_cid","msclkid","fbp","fbc","li_fat_id","snapclid"]);function w(i){let e={};for(let[t,s]of Object.entries(i??{})){if(!ne.has(t)||typeof s!="string")continue;let n=s.trim();n&&(e[t]=n.slice(0,500))}return e}function c(i,e){return se("sha256",g()).update(`${i}:${e.trim().toLowerCase()}`).digest("hex")}function oe(i){return i.consent?.analytics==="granted"&&(i.consent.purposes?.enhanced_matching==="granted"||i.consent.adUserData==="granted")&&i.consent.globalPrivacyControl!==!0}function be(i){return!!(i.identity&&oe(i))}async function Pe(i,e){let t=e.event.visitorId?.trim().slice(0,240),s=e.event.consent;if(!t||!s)return null;let n=Date.parse(e.event.occurredAt),r=Date.now();if(!Number.isFinite(n)||n>r+5*6e4||n<r-1440*6e4)return null;let o=s.purposes,a={siteAnalytics:o?o.site_analytics==="granted":s.analytics==="granted",enhancedMatching:o?o.enhanced_matching==="granted":s.adUserData==="granted",advertisingMeasurement:o?o.advertising_measurement==="granted":s.adPersonalization==="granted"},d=s.globalPrivacyControl===!0;d&&(a.siteAnalytics=!1,a.enhancedMatching=!1,a.advertisingMeasurement=!1);let l=d?"gpc":Object.values(a).every(Boolean)?"accept_all":Object.values(a).every(R=>!R)?"reject_all":"save_choices",m=new Date(n).toISOString(),y=(await i.query(`SELECT s.latest_receipt_id::text,s.site_analytics,s.enhanced_matching,s.advertising_measurement,s.gpc,r.action
|
|
32
|
+
FROM analytics_consent_current_state s JOIN analytics_consent_receipts r ON r.id=s.latest_receipt_id
|
|
33
|
+
WHERE s.site_id=$1 AND s.subject_ref=$2 FOR UPDATE`,[e.siteId,t])).rows[0];if(y&&y.site_analytics===a.siteAnalytics&&y.enhanced_matching===a.enhancedMatching&&y.advertising_measurement===a.advertisingMeasurement&&y.gpc===d&&y.action===l)return y.latest_receipt_id;let I=re();return await i.query(`INSERT INTO analytics_consent_receipts(id,site_id,pixel_id,subject_ref,policy_version,jurisdiction_profile,
|
|
34
|
+
necessary,site_analytics,enhanced_matching,advertising_measurement,gpc,action,source,presented_at,occurred_at,supersedes_id)
|
|
35
|
+
VALUES($1,$2,$3,$4,1,'pixel',true,$5,$6,$7,$8,$9,'gateway',$10,$10,$11)`,[I,e.siteId,e.pixelId,t,a.siteAnalytics,a.enhancedMatching,a.advertisingMeasurement,d,l,m,y?.latest_receipt_id??null]),await i.query(`INSERT INTO analytics_consent_current_state(site_id,subject_ref,latest_receipt_id,policy_version,jurisdiction_profile,
|
|
36
|
+
necessary,site_analytics,enhanced_matching,advertising_measurement,gpc,effective_at)
|
|
37
|
+
VALUES($1,$2,$3,1,'pixel',true,$4,$5,$6,$7,$8)
|
|
38
|
+
ON CONFLICT(site_id,subject_ref) DO UPDATE SET latest_receipt_id=EXCLUDED.latest_receipt_id,
|
|
39
|
+
policy_version=EXCLUDED.policy_version,jurisdiction_profile=EXCLUDED.jurisdiction_profile,
|
|
40
|
+
site_analytics=EXCLUDED.site_analytics,enhanced_matching=EXCLUDED.enhanced_matching,
|
|
41
|
+
advertising_measurement=EXCLUDED.advertising_measurement,gpc=EXCLUDED.gpc,
|
|
42
|
+
effective_at=EXCLUDED.effective_at,updated_at=now()
|
|
43
|
+
WHERE EXCLUDED.effective_at>=analytics_consent_current_state.effective_at`,[e.siteId,t,I,a.siteAnalytics,a.enhancedMatching,a.advertisingMeasurement,d,m]),I}async function xe(i,e){if(!e.subjectRef)return{personId:null,activationEligible:!1};let s=(await i.query(`SELECT p.id::text AS person_id,s.advertising_measurement
|
|
44
|
+
FROM analytics_people p
|
|
45
|
+
JOIN analytics_consent_current_state s ON s.site_id=p.site_id AND s.subject_ref=$2
|
|
46
|
+
JOIN analytics_consent_receipts r ON r.id=s.latest_receipt_id AND r.site_id=s.site_id
|
|
47
|
+
WHERE p.site_id=$1
|
|
48
|
+
AND (($3::uuid IS NOT NULL AND p.id=$3) OR ($4::text IS NOT NULL AND EXISTS (
|
|
49
|
+
SELECT 1 FROM analytics_events e WHERE e.site_id=p.site_id AND e.event_id=$4
|
|
50
|
+
AND e.person_id=p.id AND e.visitor_id=$2
|
|
51
|
+
)))
|
|
52
|
+
AND s.site_analytics=true AND s.gpc=false AND r.action NOT IN ('withdraw','gpc')
|
|
53
|
+
AND (s.expires_at IS NULL OR s.expires_at>now()) LIMIT 1`,[e.siteId,e.subjectRef,e.personId??null,e.eventId??null])).rows[0];return s?{personId:s.person_id,activationEligible:s.advertising_measurement===!0}:{personId:null,activationEligible:!1}}async function C(i,e,t,s){return!t?.trim()||!s?.trim()?!1:!!(await i.query(`SELECT 1 FROM analytics_consent_current_state s
|
|
54
|
+
JOIN analytics_consent_receipts r ON r.id=s.latest_receipt_id AND r.site_id=s.site_id
|
|
55
|
+
WHERE s.site_id=$1 AND s.subject_ref=$2
|
|
56
|
+
AND s.site_analytics=true AND s.enhanced_matching=true AND s.gpc=false
|
|
57
|
+
AND r.action NOT IN ('withdraw','gpc')
|
|
58
|
+
AND (s.expires_at IS NULL OR s.expires_at>now())
|
|
59
|
+
AND EXISTS (SELECT 1 FROM analytics_sessions x
|
|
60
|
+
WHERE x.site_id=s.site_id AND x.visitor_hmac=$3 AND x.session_hmac=$4)
|
|
61
|
+
ORDER BY s.effective_at DESC LIMIT 1`,[e,t,c("visitor_id",t),c("session_id",s)])).rowCount}async function H(i,e,t){return!!(await i.query(`SELECT 1 FROM analytics_consent_current_state s
|
|
62
|
+
JOIN analytics_consent_receipts r ON r.id=s.latest_receipt_id AND r.site_id=s.site_id
|
|
63
|
+
WHERE s.site_id=$1 AND s.subject_ref=$2
|
|
64
|
+
AND s.site_analytics=true AND s.enhanced_matching=true AND s.gpc=false
|
|
65
|
+
AND r.action NOT IN ('withdraw','gpc')
|
|
66
|
+
AND (s.expires_at IS NULL OR s.expires_at>now()) LIMIT 1`,[e,t])).rowCount}import{createHash as ae}from"crypto";function He(i,e){let t=i?.trim().toUpperCase();return t&&/^[A-Z0-9-]+$/.test(t)?t.slice(0,e):null}function q(i,e,t){return ae("sha256").update(JSON.stringify({scope:i,siteId:e,filters:t})).digest("base64url").slice(0,18)}function de(i,e){if(!i)return 0;try{let t=JSON.parse(Buffer.from(i,"base64url").toString("utf8"));if(t.v!==1||t.f!==e||!Number.isInteger(t.o)||Number(t.o)<0)throw new Error("invalid");return Number(t.o)}catch{throw new u("invalid_cursor","The pagination cursor is invalid for these filters.")}}function F(i,e,t){if(!e)return{items:i,pageInfo:{hasNextPage:!1,nextCursor:null}};let s=Math.min(100,Math.max(1,Math.trunc(e?.limit??25))),n=de(e?.cursor,t),r=i.slice(n,n+s),o=n+r.length<i.length;return{items:r,pageInfo:{hasNextPage:o,nextCursor:o?Buffer.from(JSON.stringify({v:1,f:t,o:n+r.length})).toString("base64url"):null}}}async function S(i,e,t){let n=(await i.query("SELECT role FROM analytics_site_grants WHERE site_id = $1 AND user_id = $2",[e,t])).rows[0]?.role;if(!n)throw new u("analytics_site_not_found","Site not found.",404);return n}async function ce(i,e,t){let s=await S(i,e,t);if(s==="viewer")throw new u("analytics_forbidden","Editor access is required.",403);return s}async function We(i,e,t){return S(i,e,t)}async function je(i,e,t){return ce(i,e,t)}async function Qe(i){for(;;){let t=await i.query(`SELECT id, visitor_id, session_id
|
|
67
|
+
FROM analytics_events
|
|
68
|
+
WHERE (visitor_id IS NOT NULL AND visitor_hmac IS NULL)
|
|
69
|
+
OR (session_id IS NOT NULL AND session_hmac IS NULL)
|
|
70
|
+
ORDER BY id
|
|
71
|
+
LIMIT $1`,[500]);if(!t.rowCount)break;await i.query(`UPDATE analytics_events e
|
|
72
|
+
SET visitor_hmac = COALESCE(e.visitor_hmac, patch.visitor_hmac),
|
|
73
|
+
session_hmac = COALESCE(e.session_hmac, patch.session_hmac)
|
|
74
|
+
FROM unnest($1::uuid[], $2::text[], $3::text[]) AS patch(id, visitor_hmac, session_hmac)
|
|
75
|
+
WHERE e.id = patch.id`,[t.rows.map(s=>s.id),t.rows.map(s=>s.visitor_id?c("visitor_id",s.visitor_id):null),t.rows.map(s=>s.session_id?c("session_id",s.session_id):null)])}for(;;){let t=await i.query(`SELECT id, session_id
|
|
76
|
+
FROM analytics_conversions
|
|
77
|
+
WHERE session_id IS NOT NULL AND session_hmac IS NULL
|
|
78
|
+
ORDER BY id
|
|
79
|
+
LIMIT $1`,[500]);if(!t.rowCount)break;await i.query(`UPDATE analytics_conversions c
|
|
80
|
+
SET session_hmac = patch.session_hmac
|
|
81
|
+
FROM unnest($1::uuid[], $2::text[]) AS patch(id, session_hmac)
|
|
82
|
+
WHERE c.id = patch.id AND c.session_hmac IS NULL`,[t.rows.map(s=>s.id),t.rows.map(s=>c("session_id",s.session_id))])}}function W(){return`xrp_${_e(18).toString("base64url")}`}function ei(i,e){let t=c("crm_entity_id",`${i}:${e}`).slice(0,32).split("");return t[12]="5",t[16]=["8","9","a","b"][Number.parseInt(t[16],16)%4],`${t.slice(0,8).join("")}-${t.slice(8,12).join("")}-${t.slice(12,16).join("")}-${t.slice(16,20).join("")}-${t.slice(20).join("")}`}async function j(i,e,t){let s=await i.query("SELECT id FROM analytics_identity_namespaces WHERE site_id=$1 AND status='approved' ORDER BY version DESC LIMIT 1",[e]);s.rows[0]||(await i.query(`INSERT INTO analytics_identity_namespaces(
|
|
83
|
+
id,site_id,name,status,approved_hosts,approved_sources,version,approved_by_user_id,approved_at
|
|
84
|
+
)
|
|
85
|
+
SELECT $1,s.id,'X-Ray primary','approved',
|
|
86
|
+
COALESCE((SELECT jsonb_agg(d.hostname ORDER BY d.hostname)
|
|
87
|
+
FROM analytics_pixels p
|
|
88
|
+
JOIN analytics_pixel_domains d ON d.pixel_id=p.id AND d.state='approved'
|
|
89
|
+
WHERE p.site_id=s.id),'[]'::jsonb),
|
|
90
|
+
$3::jsonb,1,s.owner_user_id,now()
|
|
91
|
+
FROM analytics_sites s WHERE s.id=$2
|
|
92
|
+
ON CONFLICT(site_id,name,version) DO NOTHING`,[p(),e,JSON.stringify([t])]),s=await i.query("SELECT id FROM analytics_identity_namespaces WHERE site_id=$1 AND status='approved' ORDER BY version DESC LIMIT 1",[e]));let n=s.rows[0]?.id;if(!n)throw new u("identity_namespace_unavailable","The Site identity graph could not be initialized.",409);return await i.query(`UPDATE analytics_identity_namespaces
|
|
93
|
+
SET approved_sources=(SELECT jsonb_agg(value ORDER BY value)
|
|
94
|
+
FROM (SELECT DISTINCT value
|
|
95
|
+
FROM jsonb_array_elements_text(approved_sources || $3::jsonb)) merged_sources),
|
|
96
|
+
approved_hosts=(SELECT COALESCE(jsonb_agg(hostname ORDER BY hostname),'[]'::jsonb)
|
|
97
|
+
FROM (SELECT DISTINCT hostname
|
|
98
|
+
FROM (SELECT jsonb_array_elements_text(approved_hosts) AS hostname
|
|
99
|
+
UNION ALL
|
|
100
|
+
SELECT d.hostname
|
|
101
|
+
FROM analytics_pixels p
|
|
102
|
+
JOIN analytics_pixel_domains d ON d.pixel_id=p.id AND d.state='approved'
|
|
103
|
+
WHERE p.site_id=$2) hosts) distinct_hosts),
|
|
104
|
+
updated_at=now()
|
|
105
|
+
WHERE id=$1 AND site_id=$2`,[n,e,JSON.stringify([t])]),n}async function le(i,e){if(!e.visitorId&&!e.sessionId)return{events:0,sessions:0,touches:0,conversions:0};let t=e.visitorId?c("visitor_id",e.visitorId):null,s=e.sessionId?c("session_id",e.sessionId):null,n=await i.query(`UPDATE analytics_events SET person_id = $1 WHERE site_id = $2 AND person_id IS NULL
|
|
106
|
+
AND (($3::text IS NOT NULL AND visitor_id = $3) OR ($4::text IS NOT NULL AND session_id = $4))`,[e.personId,e.siteId,e.visitorId??null,e.sessionId??null]),r=await i.query(`UPDATE analytics_conversions SET person_id = $1 WHERE site_id = $2 AND person_id IS NULL
|
|
107
|
+
AND $3::text IS NOT NULL AND session_id = $3`,[e.personId,e.siteId,e.sessionId??null]),o=await i.query(`UPDATE analytics_sessions SET person_id = $1
|
|
108
|
+
WHERE site_id = $2 AND person_id IS NULL
|
|
109
|
+
AND (($3::text IS NOT NULL AND visitor_hmac = $3)
|
|
110
|
+
OR ($4::text IS NOT NULL AND session_hmac = $4))`,[e.personId,e.siteId,t,s]),a=await i.query(`UPDATE analytics_attribution_touches SET person_id = $1
|
|
111
|
+
WHERE site_id = $2 AND person_id IS NULL
|
|
112
|
+
AND (($3::text IS NOT NULL AND visitor_hmac = $3)
|
|
113
|
+
OR ($4::text IS NOT NULL AND session_id IN (
|
|
114
|
+
SELECT id FROM analytics_sessions
|
|
115
|
+
WHERE site_id = $2 AND session_hmac = $4
|
|
116
|
+
)))`,[e.personId,e.siteId,t,s]);return{events:n.rowCount??0,sessions:o.rowCount??0,touches:a.rowCount??0,conversions:r.rowCount??0}}async function L(i,e){let s=(await i.query(`SELECT id FROM analytics_people
|
|
117
|
+
WHERE site_id=$1 AND (namespace_id=$2 OR namespace_id IS NULL)
|
|
118
|
+
AND (($3::uuid IS NOT NULL AND crm_entity_id=$3::uuid)
|
|
119
|
+
OR ($3::uuid IS NULL AND $4::text IS NOT NULL AND crm_person_ref=$4))
|
|
120
|
+
ORDER BY first_seen_at,id LIMIT 1 FOR UPDATE`,[e.siteId,e.namespaceId,e.crmEntityId??null,e.crmPersonRef??null])).rows[0]?.id;if(!s&&!e.crmEntityId&&!e.crmPersonRef&&e.signals.length&&(s=(await i.query(`SELECT assignment.person_id
|
|
121
|
+
FROM analytics_identity_nodes node
|
|
122
|
+
JOIN analytics_identity_assignments assignment
|
|
123
|
+
ON assignment.identity_node_id=node.id AND assignment.site_id=node.site_id
|
|
124
|
+
WHERE node.site_id=$1 AND EXISTS (
|
|
125
|
+
SELECT 1 FROM jsonb_to_recordset($2::jsonb) signal(kind text,value_hmac text)
|
|
126
|
+
WHERE signal.kind=node.kind AND signal.value_hmac=node.value_hmac
|
|
127
|
+
)
|
|
128
|
+
ORDER BY assignment.assigned_at,assignment.person_id LIMIT 1`,[e.siteId,JSON.stringify(e.signals.map(r=>({kind:r.kind,value_hmac:r.valueHmac})))])).rows[0]?.person_id),s)await i.query(`UPDATE analytics_people SET last_seen_at=now(),
|
|
129
|
+
namespace_id=COALESCE(namespace_id,$2),
|
|
130
|
+
crm_entity_id=COALESCE(crm_entity_id,$3::uuid),
|
|
131
|
+
public_ref=COALESCE(public_ref,$4)
|
|
132
|
+
WHERE id=$1 AND site_id=$5`,[s,e.namespaceId,e.crmEntityId??null,W(),e.siteId]);else{let n=e.crmEntityId??null,r=e.crmPersonRef??(n?`XRay::entity::${n}`:`XRay::anonymous::${p()}`);s=(await i.query(`INSERT INTO analytics_people(id,site_id,namespace_id,crm_person_ref,crm_entity_id,public_ref)
|
|
133
|
+
VALUES($1,$2,$3,$4,$5::uuid,$6)
|
|
134
|
+
ON CONFLICT(site_id,crm_person_ref) DO UPDATE SET last_seen_at=now(),
|
|
135
|
+
namespace_id=COALESCE(analytics_people.namespace_id,EXCLUDED.namespace_id),
|
|
136
|
+
crm_entity_id=COALESCE(analytics_people.crm_entity_id,EXCLUDED.crm_entity_id),
|
|
137
|
+
public_ref=COALESCE(analytics_people.public_ref,EXCLUDED.public_ref)
|
|
138
|
+
RETURNING id`,[p(),e.siteId,e.namespaceId,r,n,W()])).rows[0].id}return{personId:s,mergedPersonIds:[]}}async function b(i,e){let s=(await i.query(`SELECT person_id,crm_entity_id::text FROM analytics_identity_assignments
|
|
139
|
+
WHERE identity_node_id=$1 AND site_id=$2 FOR UPDATE`,[e.identityNodeId,e.siteId])).rows[0];if(!s){await i.query(`INSERT INTO analytics_identity_assignments(
|
|
140
|
+
identity_node_id,site_id,person_id,crm_entity_id,evidence_kind
|
|
141
|
+
) VALUES($1,$2,$3,$4::uuid,$5)
|
|
142
|
+
ON CONFLICT(identity_node_id) DO NOTHING`,[e.identityNodeId,e.siteId,e.personId,e.crmEntityId??null,e.evidenceKind]);let n=await i.query("SELECT person_id FROM analytics_identity_assignments WHERE identity_node_id=$1 AND site_id=$2",[e.identityNodeId,e.siteId]);if(n.rows[0]?.person_id===e.personId)return"assigned";if(!n.rows[0])throw new u("identity_assignment_unavailable","Identity assignment could not be recorded.",409);s={person_id:n.rows[0].person_id,crm_entity_id:null}}return s.person_id===e.personId?(await i.query(`UPDATE analytics_identity_assignments SET updated_at=now(),
|
|
143
|
+
crm_entity_id=COALESCE(crm_entity_id,$3::uuid),evidence_kind=$4
|
|
144
|
+
WHERE identity_node_id=$1 AND site_id=$2`,[e.identityNodeId,e.siteId,e.crmEntityId??null,e.evidenceKind]),"existing"):(await i.query(`INSERT INTO analytics_identity_assignment_conflicts(
|
|
145
|
+
id,site_id,identity_node_id,active_person_id,attempted_person_id,
|
|
146
|
+
active_crm_entity_id,attempted_crm_entity_id,evidence_kind
|
|
147
|
+
) VALUES($1,$2,$3,$4,$5,$6::uuid,$7::uuid,$8)
|
|
148
|
+
ON CONFLICT(identity_node_id,active_person_id,attempted_person_id)
|
|
149
|
+
WHERE status='pending' DO UPDATE SET observed_at=now(),evidence_kind=EXCLUDED.evidence_kind`,[p(),e.siteId,e.identityNodeId,s.person_id,e.personId,s.crm_entity_id,e.crmEntityId??null,e.evidenceKind]),"conflict")}async function me(i,e){let s=(await i.query(`WITH person_nodes AS (
|
|
150
|
+
SELECT DISTINCT identity_node_id
|
|
151
|
+
FROM analytics_identity_edges
|
|
152
|
+
WHERE site_id=$1 AND namespace_id=$2 AND person_id=$3
|
|
153
|
+
)
|
|
154
|
+
UPDATE analytics_candidate_associations a
|
|
155
|
+
SET status='promoted',promoted_person_id=$3,promoted_at=$4::timestamptz,updated_at=now()
|
|
156
|
+
WHERE a.site_id=$1 AND a.namespace_id=$2
|
|
157
|
+
AND (a.left_identity_node_id IN (SELECT identity_node_id FROM person_nodes)
|
|
158
|
+
OR a.right_identity_node_id IN (SELECT identity_node_id FROM person_nodes))
|
|
159
|
+
AND (a.status IN ('active','expired') OR (a.status='promoted' AND a.promoted_person_id=$3))
|
|
160
|
+
RETURNING a.id`,[e.siteId,e.namespaceId,e.personId,e.occurredAt])).rows.map(r=>r.id);if(!s.length)return{associationIds:[],touches:0};for(let r of s)await i.query(`INSERT INTO analytics_candidate_promotions(
|
|
161
|
+
id,site_id,namespace_id,association_id,person_id,evidence_kind,
|
|
162
|
+
evidence_ref_hmac,promoted_by,promoted_at
|
|
163
|
+
) VALUES($1,$2,$3,$4,$5,$6,$7,'system',$8::timestamptz)
|
|
164
|
+
ON CONFLICT(association_id,person_id) DO NOTHING`,[p(),e.siteId,e.namespaceId,r,e.personId,e.candidateEvidenceKind,e.evidenceRefHmac,e.occurredAt]);let n=await i.query(`UPDATE analytics_attribution_touches touch SET person_id=$3
|
|
165
|
+
FROM analytics_candidate_attribution_touches candidate_touch
|
|
166
|
+
WHERE candidate_touch.site_id=$1 AND candidate_touch.namespace_id=$2
|
|
167
|
+
AND candidate_touch.association_id=ANY($4::uuid[])
|
|
168
|
+
AND candidate_touch.touch_id=touch.id AND touch.site_id=$1 AND touch.person_id IS NULL`,[e.siteId,e.namespaceId,e.personId,s]);return{associationIds:s,touches:n.rowCount??0}}async function ye(i,e){let t=c("promotion_evidence",`${e.evidenceKind}:${e.evidenceRef}`),s=c("promotion_person",e.personId),n=c("promotion_receipt",`${e.siteId}:${e.personId}:${e.evidenceKind}:${e.evidenceRef}`);await i.query(`INSERT INTO analytics_identity_promotion_receipts(
|
|
169
|
+
id,site_id,namespace_id,canonical_person_id,canonical_person_ref_hmac,
|
|
170
|
+
evidence_kind,evidence_ref_hmac,prior_person_ids,prior_identity_node_ids,
|
|
171
|
+
candidate_association_ids,affected_history_counts,schema_version,idempotency_key,promoted_at
|
|
172
|
+
) VALUES($1,$2,$3,$4,$5,$6,$7,$8::uuid[],$9::uuid[],$10::uuid[],$11::jsonb,
|
|
173
|
+
'xray.identity-promotion.v1',$12,$13::timestamptz)
|
|
174
|
+
ON CONFLICT(site_id,idempotency_key) DO NOTHING`,[p(),e.siteId,e.namespaceId,e.personId,s,e.evidenceKind,t,e.priorPersonIds,e.priorIdentityNodeIds,e.candidateAssociationIds,JSON.stringify({...e.history,candidateTouches:e.candidateTouches}),n,e.occurredAt])}async function P(i,e){let t=e.occurredAt??new Date().toISOString(),s=await le(i,e),n=c("promotion_evidence",`${e.evidenceKind}:${e.evidenceRef}`),r=await me(i,{siteId:e.siteId,namespaceId:e.namespaceId,personId:e.personId,candidateEvidenceKind:e.candidateEvidenceKind,evidenceRefHmac:n,occurredAt:t});await ye(i,{...e,history:s,candidateAssociationIds:r.associationIds,candidateTouches:r.touches,occurredAt:t})}async function ii(i,e){let t=e.customerId||e.orderId||e.email||e.phone;if(!t||!await C(i,e.siteId,e.visitorId,e.sessionId))return null;let s=await j(i,e.siteId,"pixel"),n=[...e.email?[{kind:"email",valueHmac:c("email",e.email)}]:[],...e.phone?[{kind:"phone",valueHmac:c("phone",e.phone)}]:[],...e.customerId?[{kind:"customer_id",valueHmac:c("customer_id",e.customerId)}]:[],...e.orderId?[{kind:"customer_id",valueHmac:c("customer_id",`order:${e.orderId}`)}]:[]];await h(i,{siteId:e.siteId,signals:n});let r=`XRay::${c("person_ref",t).slice(0,24)}`,o=[...n,...e.visitorId?[{kind:"visitor_id",valueHmac:c("visitor_id",e.visitorId)}]:[],...e.sessionId?[{kind:"session_id",valueHmac:c("session_id",e.sessionId)}]:[]],a=await L(i,{siteId:e.siteId,namespaceId:s,crmPersonRef:r,signals:o}),d=a.personId,l=[];e.visitorId&&l.push({kind:"visitor_id",value:e.visitorId,evidence:"pixel_identify",confidence:.98}),e.sessionId&&l.push({kind:"session_id",value:e.sessionId,evidence:"pixel_identify",confidence:.98}),e.email&&l.push({kind:"email",value:e.email,evidence:"pixel_identify",confidence:1}),e.phone&&l.push({kind:"phone",value:e.phone,evidence:"pixel_identify",confidence:.99}),e.customerId&&l.push({kind:"customer_id",value:e.customerId,evidence:"customer_id",confidence:1}),e.orderId&&l.push({kind:"customer_id",value:`order:${e.orderId}`,evidence:"order_id",confidence:1});for(let[y,I]of Object.entries(w(e.clickIds)))l.push({kind:"click_id",value:`${y}:${I}`,evidence:y,confidence:.9});let m=[],_=!1;for(let y of l){let I=await i.query(`INSERT INTO analytics_identity_nodes(id, site_id, namespace_id, kind, value_hmac) VALUES ($1,$2,$3,$4,$5)
|
|
175
|
+
ON CONFLICT(site_id, kind, value_hmac) DO UPDATE SET last_seen_at = now(),
|
|
176
|
+
namespace_id=COALESCE(analytics_identity_nodes.namespace_id,EXCLUDED.namespace_id) RETURNING id`,[p(),e.siteId,s,y.kind,c(y.kind,y.value)]);m.push(I.rows[0].id),await i.query(`INSERT INTO analytics_identity_edges(id, site_id, namespace_id, person_id, identity_node_id, evidence_kind, confidence)
|
|
177
|
+
VALUES ($1,$2,$3,$4,$5,$6,$7)
|
|
178
|
+
ON CONFLICT(person_id, identity_node_id, evidence_kind) DO UPDATE
|
|
179
|
+
SET last_seen_at = now(), confidence = greatest(analytics_identity_edges.confidence, EXCLUDED.confidence),
|
|
180
|
+
namespace_id=COALESCE(analytics_identity_edges.namespace_id,EXCLUDED.namespace_id)`,[p(),e.siteId,s,d,I.rows[0].id,y.evidence,y.confidence]);let R=await b(i,{siteId:e.siteId,personId:d,identityNodeId:I.rows[0].id,evidenceKind:y.evidence});_||=R==="conflict"}return _||await P(i,{siteId:e.siteId,namespaceId:s,personId:d,evidenceKind:e.customerId?"authenticated_login":"browser_identify",candidateEvidenceKind:e.email?"verified_email":e.phone?"verified_phone":"authenticated_user",evidenceRef:t,priorPersonIds:a.mergedPersonIds,priorIdentityNodeIds:m,visitorId:e.visitorId,sessionId:e.sessionId,occurredAt:e.occurredAt}),e.customerId&&!_&&await $(i,{siteId:e.siteId,personId:d,consentSubjectRefs:[e.visitorId??"",e.sessionId??""],evidenceType:"authenticated_user",email:e.email,phone:e.phone}),d}async function ti(i){let e=await E().connect();try{if(await e.query("BEGIN"),!await C(e,i.siteId,i.visitorId,i.sessionId))return await e.query("COMMIT"),{personId:null,linkedSignals:0,conflicts:0};let t=await j(e,i.siteId,"form"),s=[...i.email?[{kind:"email",valueHmac:c("email",i.email)}]:[],...i.phone?[{kind:"phone",valueHmac:c("phone",i.phone)}]:[]];await h(e,{siteId:i.siteId,signals:s});let n=[...s,...i.visitorId?[{kind:"visitor_id",valueHmac:c("visitor_id",i.visitorId)}]:[],...i.sessionId?[{kind:"session_id",valueHmac:c("session_id",i.sessionId)}]:[]],r=await L(e,{siteId:i.siteId,namespaceId:t,crmPersonRef:i.crmPersonRef,crmEntityId:i.crmEntityId,signals:n}),o=r.personId,a=[];i.visitorId&&a.push({kind:"visitor_id",value:i.visitorId,evidence:"first_party_form",confidence:.98}),i.sessionId&&a.push({kind:"session_id",value:i.sessionId,evidence:"first_party_form",confidence:.95}),i.email&&a.push({kind:"email",value:i.email,evidence:"submitted_email",confidence:1}),i.phone&&a.push({kind:"phone",value:i.phone,evidence:"submitted_phone",confidence:.99});for(let[m,_]of Object.entries(w(i.clickIds)))a.push({kind:"click_id",value:`${m}:${_}`,evidence:m,confidence:.9});let d=[],l=0;for(let m of a){let _=await e.query(`INSERT INTO analytics_identity_nodes(id, site_id, namespace_id, kind, value_hmac) VALUES ($1,$2,$3,$4,$5)
|
|
181
|
+
ON CONFLICT(site_id, kind, value_hmac) DO UPDATE SET last_seen_at = now(),
|
|
182
|
+
namespace_id=COALESCE(analytics_identity_nodes.namespace_id,EXCLUDED.namespace_id) RETURNING id`,[p(),i.siteId,t,m.kind,c(m.kind,m.value)]);d.push(_.rows[0].id),await e.query(`INSERT INTO analytics_identity_edges(id, site_id, namespace_id, person_id, identity_node_id, evidence_kind, confidence)
|
|
183
|
+
VALUES ($1,$2,$3,$4,$5,$6,$7)
|
|
184
|
+
ON CONFLICT(person_id, identity_node_id, evidence_kind) DO UPDATE SET last_seen_at = now(),
|
|
185
|
+
confidence = greatest(analytics_identity_edges.confidence, EXCLUDED.confidence),
|
|
186
|
+
namespace_id=COALESCE(analytics_identity_edges.namespace_id,EXCLUDED.namespace_id)`,[p(),i.siteId,t,o,_.rows[0].id,m.evidence,m.confidence]),await b(e,{siteId:i.siteId,personId:o,crmEntityId:i.crmEntityId,identityNodeId:_.rows[0].id,evidenceKind:m.evidence})==="conflict"&&(l+=1)}return i.submissionId&&await e.query("UPDATE analytics_form_submissions SET person_id = $1 WHERE id = $2 AND site_id = $3",[o,i.submissionId,i.siteId]),l===0&&await P(e,{siteId:i.siteId,namespaceId:t,personId:o,evidenceKind:"form_fill",candidateEvidenceKind:i.email?"verified_email":i.phone?"verified_phone":"authenticated_user",evidenceRef:i.submissionId??i.crmEntityId??i.crmPersonRef??o,priorPersonIds:r.mergedPersonIds,priorIdentityNodeIds:d,visitorId:i.visitorId,sessionId:i.sessionId}),l===0&&await $(e,{siteId:i.siteId,personId:o,consentSubjectRefs:[i.visitorId??""],evidenceType:i.evidenceType??"first_party_form",displayName:i.displayName,email:i.email,phone:i.phone}),await e.query("COMMIT"),{personId:o,linkedSignals:a.length,conflicts:l}}catch(t){throw await e.query("ROLLBACK"),t}finally{e.release()}}async function si(i){let e=await E().connect();try{await e.query("BEGIN");let s=(await e.query(`SELECT p.id,p.public_ref,p.namespace_id FROM analytics_people p
|
|
187
|
+
JOIN analytics_identity_namespaces n ON n.id=p.namespace_id AND n.site_id=p.site_id AND n.status='approved'
|
|
188
|
+
WHERE p.site_id=$1 AND p.crm_person_ref=$2 LIMIT 1 FOR UPDATE OF p`,[i.siteId,i.crmPersonRef])).rows[0];if(!s||!await H(e,i.siteId,s.public_ref))return await e.query("COMMIT"),{personId:null,linkedSignals:0,conflicts:0};let n=[...i.email?[{kind:"email",value:i.email,confidence:1}]:[],...i.phone?[{kind:"phone",value:i.phone,confidence:.99}]:[]];await h(e,{siteId:i.siteId,signals:n.map(_=>({kind:_.kind,valueHmac:c(_.kind,_.value)}))});let r=await L(e,{siteId:i.siteId,namespaceId:s.namespace_id,crmPersonRef:i.crmPersonRef,crmEntityId:i.crmEntityId,signals:n.map(_=>({kind:_.kind,valueHmac:c(_.kind,_.value)}))}),o=r.personId,d=(await e.query("SELECT public_ref FROM analytics_people WHERE id=$1 AND site_id=$2",[o,i.siteId])).rows[0]?.public_ref??s.public_ref,l=[],m=0;for(let _ of n){let y=await e.query(`INSERT INTO analytics_identity_nodes(id,site_id,namespace_id,kind,value_hmac) VALUES($1,$2,$3,$4,$5)
|
|
189
|
+
ON CONFLICT(site_id,kind,value_hmac) DO UPDATE SET last_seen_at=now(),
|
|
190
|
+
namespace_id=COALESCE(analytics_identity_nodes.namespace_id,EXCLUDED.namespace_id) RETURNING id`,[p(),i.siteId,s.namespace_id,_.kind,c(_.kind,_.value)]);l.push(y.rows[0].id),await e.query(`INSERT INTO analytics_identity_edges(id,site_id,namespace_id,person_id,identity_node_id,evidence_kind,confidence)
|
|
191
|
+
VALUES($1,$2,$3,$4,$5,'crm_contact',$6)
|
|
192
|
+
ON CONFLICT(person_id,identity_node_id,evidence_kind) DO UPDATE SET last_seen_at=now(),
|
|
193
|
+
confidence=greatest(analytics_identity_edges.confidence,EXCLUDED.confidence)`,[p(),i.siteId,s.namespace_id,o,y.rows[0].id,_.confidence]),await b(e,{siteId:i.siteId,personId:o,crmEntityId:i.crmEntityId,identityNodeId:y.rows[0].id,evidenceKind:"crm_contact"})==="conflict"&&(m+=1)}return m===0&&await P(e,{siteId:i.siteId,namespaceId:s.namespace_id,personId:o,evidenceKind:"crm_identity",candidateEvidenceKind:"crm_contact",evidenceRef:i.crmPersonRef,priorPersonIds:r.mergedPersonIds,priorIdentityNodeIds:l}),m===0&&await $(e,{siteId:i.siteId,personId:o,consentSubjectRefs:[d],evidenceType:"crm_contact",displayName:i.displayName,email:i.email,phone:i.phone}),await e.query("COMMIT"),{personId:o,linkedSignals:n.length,conflicts:m}}catch(t){throw await e.query("ROLLBACK"),t}finally{e.release()}}async function ni(i,e,t){let s=E();await S(s,i,e);let n=await s.query(`SELECT p.id, p.first_seen_at, p.last_seen_at, true AS crm_linked,
|
|
194
|
+
(SELECT count(*)::int FROM analytics_identity_edges e WHERE e.person_id = p.id) AS signal_count,
|
|
195
|
+
(SELECT count(*)::int FROM analytics_form_submissions f WHERE f.person_id = p.id) AS form_submission_count,
|
|
196
|
+
(SELECT count(*)::int FROM analytics_conversions c WHERE c.person_id = p.id) AS conversion_count,
|
|
197
|
+
(SELECT COALESCE(sum(c.value_minor), 0)::bigint FROM analytics_conversions c WHERE c.person_id = p.id) AS revenue_minor
|
|
198
|
+
FROM analytics_people p WHERE p.site_id = $1 ORDER BY p.last_seen_at DESC, p.id DESC LIMIT 1000`,[i]),r=F(n.rows,t,q("people",i,{start:"all",end:"all"}));return{people:r.items,pageInfo:r.pageInfo}}export{u as a,E as b,k as c,U as d,Ne as e,ve as f,Ae as g,w as h,c as i,f as j,Re as k,oe as l,be as m,Pe as n,xe as o,He as p,q,F as r,S as s,ce as t,We as u,je as v,Qe as w,W as x,ei as y,le as z,b as A,ii as B,ti as C,si as D,ni as E};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{g as v,i as S}from"./chunk-C7MGJUTU.js";var C={\u006b\u0065\u0072\u006e\u0065\u006c:"browserRuntime"},b=/error|message/i,P=(new RegExp("(?:spending|billing|organization|account).{0,80}(?:cap|limit|blocked|disabled)|(?:quota|capacity).{0,40}(?:reached|exceeded)|https?:\\/\\/\\S*(?:dashboard|on\u006b\u0065\u0072\u006e\u0065\u006c)", "i")),A=/\b(?:ENOENT|ENOTDIR|EACCES|EPERM|EISDIR|EMFILE|ENFILE)\b|\b(?:mkdir|open|stat|lstat|readFile|writeFile)\s+['"]?(?:\/|[A-Za-z]:\\)|(?:^|[\s'"])(?:\/Users|\/home|\/tmp|\/var|\/private|\/opt|\/srv|\/root)\//i,I=new Set(["\u006b\u0065\u0072\u006e\u0065\u006c_session_id","browser_session_id","\u006b\u0065\u0072\u006e\u0065\u006cSessionId","browserSessionId","sessionId","\u006b\u0065\u0072\u006e\u0065\u006c_delete_started","session_cleanup_started","\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted","sessionCleanupStarted","\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded","session_cleanup_succeeded","\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded","sessionCleanupSucceeded","\u006b\u0065\u0072\u006e\u0065\u006c_delete_error","session_cleanup_error","\u006b\u0065\u0072\u006e\u0065\u006cDeleteError","sessionCleanupError","\u006b\u0065\u0072\u006e\u0065\u006cProxyId","proxyId","requestedProxyIdSuffix","retrievedProxyIdSuffix","provider","providerName","service","serviceName","vendor","vendorName","organization","organizationId","accountId","browser_provider","provider_session_id","provider_session_observed_at","provider_lookup_status","provider_status","provider_error_code","provider_error_message","provider_reason_source","provider_duration_ms","provider_bandwidth_bytes","provider_captcha","provider_checked_at","provider_detail","provider_detail_json","provider_disconnect_observed","provider_disconnect_message","browserProvider","providerSessionId","providerLookupStatus","providerStatus","providerErrorCode","providerErrorMessage","providerReasonSource","providerDurationMs","providerBandwidthBytes","providerCaptcha","providerCheckedAt","providerDetail","providerDisconnectObserved","providerDisconnectMessage"]),T=new Set(["hosted_url","hostedUrl","live_view_url","liveViewUrl","cdp_ws_url","cdpWsUrl","browser_live_view_url"]),O=(new RegExp("\\b(?:wss?|https?):\\/\\/[^\\s\"'<>]*\\bon\u006b\u0065\u0072\u006e\u0065\u006c\\.com[^\\s\"'<>]*", "gi"));function E(e){return e.replace(O,"[browser-service]")}var x=new Set(["\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api","\u006b\u0065\u0072\u006e\u0065\u006c","local"]),q=new Set(["pending","complete","not_found","unavailable_credentials","temporary_error","invalid_payload","not_applicable"]),F=new Set(["active","running","complete","completed","failed","closed","stopped","terminated","timed_out"]),L=new Set(["provider_session_api","client_runtime","extractor","unknown"]),N=new Set(["browser_disconnected","worker_disconnect","job_killed","cdp_cmd_timeout","network_inactivity_timeout","session_timeout","client_timeout","navigate_domains_limit","cdp_error","browser_session_interrupted","provider_detail_pending","provider_lookup_unavailable","provider_reported_no_error","provider_session_not_found","provider_invalid_json","provider_invalid_payload","provider_lookup_timeout","provider_lookup_network_error","unknown"]),D=new Set(["browser_provider","provider_session_id","provider_lookup_status","provider_status","provider_error_code","provider_error_message","provider_reason_source","provider_duration_ms","provider_bandwidth_bytes","provider_captcha","provider_checked_at","provider_detail","provider_detail_json","provider_disconnect_observed","provider_disconnect_message"]),U=512,M=320;function p(e,r){return typeof e=="string"&&r.has(e)?e:null}function z(e){return typeof e!="string"?null:N.has(e)||/^provider_http_(?:429|5\d\d)$/.test(e)?e:null}function V(e){return typeof e!="string"||e.length<1||e.length>U||/^(?:authorization|bearer|password|secret|token|api[_-]?key|account)/i.test(e)?null:/^[A-Za-z0-9._:-]+$/.test(e)&&!e.includes("://")?e:null}function f(e){return typeof e=="number"&&Number.isSafeInteger(e)&&e>=0?e:null}function y(e){if(typeof e!="string"||e.length>40)return null;let r=Date.parse(e);return Number.isFinite(r)&&new Date(r).toISOString()===e?e:null}function h(e){if(typeof e!="string")return null;let r=e.replace(/[\u0000-\u001f\u007f]+/g," ").replace(/\s+/g," ").trim();if(!r||r.startsWith("{")||r.startsWith("["))return null;let n=r.replace(/\b(?:wss?|https?):\/\/\S+/gi,"[redacted endpoint]").replace(/\b(?:authorization|proxy-authorization)\s*:\s*(?:Bearer\s+)?\S+/gi,"[redacted credential]").replace(/\bBearer\s+\S+/gi,"[redacted credential]").replace(/\b(?:api[_-]?key|password|secret|token|account[_-]?id)\s*[=:]\s*[^\s,;]+/gi,"[redacted credential]").replace(/\b(?:account|organization)\s+(?:id\s+)?[A-Za-z0-9_-]{4,}/gi,"[redacted account]");return E(n).slice(0,M)}function w(e){return typeof e=="boolean"?e:null}function H(e){return{browser_provider:p(e.browser_provider,x),provider_session_id:V(e.provider_session_id),provider_session_observed_at:y(e.provider_session_observed_at),provider_lookup_status:p(e.provider_lookup_status,q),provider_status:p(e.provider_status,F),provider_error_code:z(e.provider_error_code),provider_error_message:h(e.provider_error_message),provider_reason_source:p(e.provider_reason_source,L),provider_duration_ms:f(e.provider_duration_ms),provider_bandwidth_bytes:f(e.provider_bandwidth_bytes),provider_captcha:w(e.provider_captcha),provider_checked_at:y(e.provider_checked_at),provider_disconnect_observed:w(e.provider_disconnect_observed),provider_disconnect_message:h(e.provider_disconnect_message)}}function l(e,r=""){if(typeof e=="string"){let n=E(e);return b.test(r)&&(P.test(e)||A.test(e))?n=v("service_unavailable"):b.test(r)&&(n=S(n)),n}if(Array.isArray(e))return e.map(n=>l(n,r));if(e!==null&&typeof e=="object"){let n={};for(let[t,s]of Object.entries(e)){if(I.has(t))continue;let a=C[t]??t;if(T.has(t)){n[a]=null;continue}n[a]=l(s,t)}return n}return e}function K(e){return e.map(r=>{if(r===null||typeof r!="object"||Array.isArray(r))return l(r);let n=r,t=l(n);return Object.keys(n).some(a=>D.has(a))?{...t,...H(n)}:t})}function W(e){let r=e?.diagnostics;if(!r)return e;let n=r.serpCompleteness,t=n?.queryIntegrity;return!r.debug&&!t?e:{...e,diagnostics:{...r,...r.debug?{debug:l(r.debug)}:{},...t?{serpCompleteness:{...n,queryIntegrity:{...t,outboundProviderMethod:"search"}}}:{}}}}import{createHash as Q}from"crypto";var j=["observed_present","observed_absent","incomplete","not_requested","unknown","unsupported"],B=["provider_transport","invalid_payload","serp_readiness","query_integrity","pagination","optional_feature_parsing","unknown"],R=class extends Error{stage;failureCode;constructor(r,n,t){super(t),this.name="SerpExtractionError",this.stage=r,this.failureCode=n}};function _(e){return Q("sha256").update(e).digest("hex")}function G(e){return e.normalize("NFKC").toLocaleLowerCase("en-US").split(/[^\p{L}\p{N}]+/u).filter(Boolean).slice(0,64)}function X(e){try{let r=new URL(e);return r.hash="",r.pathname.length>1&&(r.pathname=r.pathname.replace(/\/+$/,"")),r.toString()}catch{return e.trim()}}function J(e,r){let n=e===2?[1,2]:[1],t=r.filter(o=>o.attempted).map(o=>o.page),s=r.filter(o=>o.captured&&o.resultCount>0).map(o=>o.page),a=r.find(o=>o.failureCode);return{status:n.every(o=>s.includes(o))?"complete":"incomplete",requestedPages:n,attemptedPages:t,capturedPages:s,pageResultCounts:n.map(o=>({page:o,resultCount:r.find(g=>g.page===o)?.resultCount??0})),providerRequestCount:t.length,evidence:r,failureCode:a?.failureCode??null}}function ee(e){return e.requested?e.moduleObserved?!e.rawShapeValid||e.rawCount>0&&e.mappedCount===0?"incomplete":e.mappedCount>0?"observed_present":"observed_absent":"unknown":"not_requested"}function re(e){let r=_(e.query),n=e.finalQuery==null?null:_(e.finalQuery),t=e.locationResolutionSource==="unresolved";return{status:n!==null&&n!==r?"query_mismatch":t?"unresolved_location":"matched",queryHash:r,normalizedQueryTokens:G(e.query),canonicalLocation:e.canonicalLocation??null,locationResolutionSource:e.locationResolutionSource,locationAmbiguousCandidates:Math.max(0,Math.trunc(e.locationAmbiguousCandidates??0)),uulePresent:!!e.uule,uuleHash:e.uule?_(e.uule):null,outboundProviderMethod:e.outboundProviderMethod,finalQueryHash:n}}function ne(e){return _(JSON.stringify(Object.fromEntries(Object.entries(e).filter(([r,n])=>r!=="requestFingerprint"&&n!==void 0).sort(([r],[n])=>r.localeCompare(n)))))}function c(e){return e&&typeof e=="object"&&!Array.isArray(e)?e:{}}function m(e){return typeof e=="number"&&Number.isFinite(e)&&e>=0?Math.trunc(e):0}function u(e,r){return typeof e=="string"&&e.length<=r?e:null}function te(e){let r=c(e),n=c(r.pagination),t=c(r.features),s=c(r.queryIntegrity),a=i=>Array.isArray(i)?i.filter(d=>d===1||d===2).slice(0,2):[],o=a(n.requestedPages);if(o.length===0)return null;let g=new Set(j),k=new Set(B);return{pagination:{status:n.status==="complete"?"complete":"incomplete",requestedPages:o,attemptedPages:a(n.attemptedPages),capturedPages:a(n.capturedPages),pageResultCounts:Array.isArray(n.pageResultCounts)?n.pageResultCounts.slice(0,2).map(i=>{let d=c(i);return{page:d.page===2?2:1,resultCount:m(d.resultCount)}}):[],providerRequestCount:m(n.providerRequestCount),failureCode:u(n.failureCode,100)},features:Object.fromEntries(["localPack","forums","videos","aiOverview","whatPeopleSaying","aiMode","paaPreview","entityIds"].map(i=>[i,g.has(String(t[i]))?t[i]:"unknown"])),queryIntegrity:{status:["matched","unresolved_location","query_mismatch"].includes(String(s.status))?s.status:"query_mismatch",queryHash:u(s.queryHash,64)??"",normalizedQueryTokens:Array.isArray(s.normalizedQueryTokens)?s.normalizedQueryTokens.slice(0,64).flatMap(i=>typeof i=="string"?[i.slice(0,100)]:[]):[],canonicalLocation:u(s.canonicalLocation,500),locationResolutionSource:u(s.locationResolutionSource,100)??"unknown",locationAmbiguousCandidates:m(s.locationAmbiguousCandidates),uulePresent:s.uulePresent===!0,uuleHash:u(s.uuleHash,64),outboundProviderMethod:"search",finalQueryHash:u(s.finalQueryHash,64)},failureStage:k.has(String(r.failureStage))?r.failureStage:null}}export{l as a,K as b,W as c,R as d,X as e,J as f,ee as g,re as h,ne as i,te as j};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{mkdirSync as p,openSync as f,writeFileSync as m,closeSync as c,unlinkSync as g}from"fs";import{homedir as S}from"os";import{join as s}from"path";var u=!0;function w(r){u=r}function d(r){return r.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,80)||"mcp-scraper-report"}function R(r){return r.split(`
|
|
2
|
+
`).find(t=>t.startsWith("# "))?.replace(/^#\s+/,"").trim()||"MCP Scraper Report"}function v(r){return r?.trim()||process.env.MCP_SCRAPER_OUTPUT_DIR?.trim()||s(S(),"Downloads","mcp-scraper")}function C(r){if(!y())return null;let o=v(),t=null,e=null;try{p(o,{recursive:!0});let a=`${new Date().toISOString().replace(/[:.]/g,"-")}-${d(R(r))}`;for(let n=0;n<1e3;n++){let i=s(o,`${a}${n?"-"+n:""}.md`);try{t=f(i,"wx"),e=i;break}catch(l){if(l.code!=="EEXIST")throw l}}return t===null?null:(m(t,r,"utf8"),c(t),t=null,e)}catch{if(t!==null)try{c(t)}catch{}if(e!==null)try{g(e)}catch{}return null}}function y(){return u&&process.env.MCP_SCRAPER_SAVE_REPORTS!=="false"}export{w as a,d as b,v as c,C as d,y as e};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{load as w}from"cheerio";var T="script,style,noscript,svg,iframe,canvas,nav,header,footer,aside",_=4096;function l(r){return(r??"").replace(/\s+/g," ").trim()}function C(r){let e=r.replace(/[\x00-\x09\x0b\x0c\x0e-\x1f]/g," ").trim();if(!e)return null;try{return JSON.parse(e)}catch(a){let o=/at position (\d+)/.exec(a instanceof Error?a.message:"")?.[1];if(!o)return null;try{return JSON.parse(e.slice(0,Number(o)))}catch{return null}}}function f(r,e,a={value:2e3}){if(a.value--<=0||r==null)return;if(Array.isArray(r)){for(let d of r)f(d,e,a);return}if(typeof r!="object")return;let o=r,c=o["@type"];if(typeof c=="string"&&c&&e.add(c),Array.isArray(c))for(let d of c)typeof d=="string"&&d&&e.add(d);Array.isArray(o["@graph"])&&f(o["@graph"],e,a)}function N(r){let e=r("main").first();if(e.length)return e;let a=r("article").first();if(a.length)return a;let o=r("body").first();return o.length?o:r.root()}function x(r,e){let a=Number(r.attr(e)??0);return Number.isFinite(a)&&a>0?a:null}function P(r){let e=w(r),a={};e("meta").each((m,s)=>{let t=e(s),n=l(t.attr("name")??t.attr("property")).toLowerCase(),i=l(t.attr("content"));n&&i&&(a[n]=i)});let o=[];e("h1,h2,h3,h4,h5,h6").each((m,s)=>{let t=e(s),n=l(t.text());n&&o.push({level:Number(s.tagName.slice(1)),text:n})});let c=[],d=new Set;e('script[type="application/ld+json"]').each((m,s)=>{let t=C(e(s).text());t!=null&&(Array.isArray(t)?c.push(...t):c.push(t),f(t,d))});let g=new Set;e("[itemtype]").each((m,s)=>{let t=e(s).attr("itemtype")??"";for(let n of t.split(/\s+/))try{let i=new URL(n);if(i.hostname==="schema.org"||i.hostname==="www.schema.org"){let h=i.pathname.replace(/^\/+/,"");h&&g.add(h)}}catch{}});let u=N(e).clone();u.find(T).remove(),u.find("[href],[src],[srcset],[data-src],[data-srcset]").each((m,s)=>{let t=e(s);for(let n of["href","src","srcset","data-src","data-srcset"]){let i=t.attr(n);i&&i.length>_&&t.removeAttr(n)}}),u.find("img").each((m,s)=>{let t=e(s),n=t.attr("src");if(!n||n.startsWith("data:")){let i=t.attr("data-src")??t.attr("data-lazy-src")??t.attr("data-original");i&&t.attr("src",i)}});let b=u.html()??"",A=l(u.text()),p=[];e("a[href]").each((m,s)=>{let t=e(s),n=l(t.attr("href"));n&&p.push({href:n,text:l(t.text()),rel:l(t.attr("rel")).toLowerCase()||null})});let y=[];return e("img").each((m,s)=>{let t=e(s),n=Object.fromEntries(Object.entries(s.attribs??{}).map(([i,h])=>[i.toLowerCase(),h]));y.push({src:t.attr("src")??t.attr("data-src")??t.attr("data-lazy-src")??t.attr("data-original")??null,srcset:t.attr("srcset")??t.attr("data-srcset")??null,alt:t.attr("alt")??null,width:x(t,"width"),height:x(t,"height"),attributes:n})}),{$:e,sourceHtml:r,title:l(e("title").first().text())||null,meta:a,headings:o,schema:c,schemaTypes:[...d],microdataTypes:[...g],canonicalUrl:l(e('link[rel~="canonical"]').first().attr("href"))||null,mainHtml:b,mainText:A,bodyClass:e("body").first().attr("class")??"",links:p,images:y}}export{P as a};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function u(e){if(Date.now()>=e)throw new Error("Reddit request deadline exhausted")}var t=class extends Error{constructor(n){super(`${n} deadline exceeded`),this.name="DeadlineExceededError"}};async function w(e,n,r,s=Number.POSITIVE_INFINITY){let o=Math.min(e-Date.now(),s);if(o<=0)throw new t(r);let T=Date.now()+o,i,a=Promise.resolve().then(()=>{if(Date.now()>=e)throw new t(r);return n()});try{let E=await Promise.race([a,new Promise((_,D)=>{i=setTimeout(()=>D(new t(r)),o)})]);if(Date.now()>=Math.min(e,T))throw new t(r);return E}finally{i&&clearTimeout(i)}}export{u as a,t as b,w as c};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as t,d as o,e as _,f as l}from"./chunk-ZPQNY5TI.js";import{a as g}from"./chunk-WP3QS4CJ.js";var F=t.browser_minute/6e4;function de(e){return Math.round(e*F)}var pe=1e3,ue=_,$=t.serp_headful/o,me=t.paa_base/o,ge=t.paa/o,be=t.page_scrape/o,V={billingMode:"credits",chargeEvent:"accepted_write",validationCredits:0,create:{credits:t.listing_create/o,mc:t.listing_create},edit:{credits:t.listing_edit/o,mc:t.listing_edit},surfaces:["local_sourcebook","transparent_commons"],sourcebookAcquisitionIncluded:!0},H=2e6,K=3,v=1e4,y=1e9,j=75,fe=j*o,Q=15e3,Ce=Q/v,Ee=5e3,W="2026-08-03.connected-usage-v3",S=15e3,Re=S*o,I=2,A=2,x=5,N=I*o,k=A*o,M=x*o,he={schemaVersion:2,ratePolicyVersion:W,effectiveAt:"2026-08-03T00:00:00.000Z",provider:"nango",entitlement:"paid_plan_add_on",connection:{billingMode:"flat_recurring_credits",amountCredits:S,interval:"month",unit:"active_connection",proration:"none"},usage:{billingMode:"credits",settlementMode:"authoritative_receipt",rounding:"aggregate_then_ceil_to_mc",functionRun:{credits:I,mc:N,unit:"per_function_run"},proxyRequest:{credits:A,mc:k,unit:"per_proxy_request"},compute:{creditsPerSecond:x,mcPerSecond:M,sourceUnit:"millisecond",unit:"per_compute_second",formula:"ceil(computeMilliseconds * mcPerSecond / 1000)"}}};function b(e,r){if(!Number.isSafeInteger(e)||e<0)throw new Error(`${r} must be a non-negative safe integer`);return BigInt(e)}function p(e){if(e>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("connected usage charge exceeds the safe integer range");return Number(e)}function ye(e){let r=b(e.functionRuns,"functionRuns"),a=b(e.proxyRequests,"proxyRequests"),s=b(e.computeMilliseconds,"computeMilliseconds"),n=r*BigInt(N),c=a*BigInt(k),d=s*BigInt(M),m=d===0n?0n:(d+999n)/1000n,h=n+c+m;return{functionRunMc:p(n),proxyRequestMc:p(c),computeMc:p(m),amountMc:p(h),credits:p(h)/o}}function Te(e,r){if(!Number.isFinite(e)||e<0)throw new Error("costUsd must be a finite non-negative number");if(!Number.isInteger(r)||r<0)throw new Error("markupBps must be a non-negative integer");let s=BigInt(Math.round(e*y))*BigInt(r)*BigInt(H),n=BigInt(y)*BigInt(v)*BigInt(K),c=s===0n?0n:(s+n-1n)/n;if(c>BigInt(Number.MAX_SAFE_INTEGER))throw new Error("calculated mc exceeds the safe integer range");return Number(c)}var Z=[{key:"serp",label:"SERP search",aliases:["search_serp","serp","google search","organic results"],credits:_,unit:"per page (ordinary search)",notes:`search_serp light mode returns one page of organic positions, URLs, titles, and descriptions by default. Unfiltered one-page full mode returns available same-page SERP features. Either mode requests two pages only when pages:2 is explicit. Unfiltered light searches cost ${_} credits per delivered page, or ${l} credits per delivered page if the backup supplies the result. One-page full searches cost ${l} credits. Setting recency to week or month applies Google's date filter through a managed browser and returns organic results only. It costs ${l} credits for one page; filtered pages:2 follows Google's page-two control and costs ${l*2} credits when both pages are delivered. Unfiltered searches use the managed browser only as the last backup. Unfiltered two-page full searches return organic results only for ${_*2} credits, with rich features marked unsupported. If only page 1 is delivered, the search is marked partial and billed for one delivered page. A temporary ${l}-credit-per-requested-page hold is settled to the delivered rate. Location, language, device, and optional module fields do not change ordinary searches. Saved browser identity and capture_serp_snapshot searches retain their ${$}-credit base price; optional page snapshots add ${i(t.page_scrape)} credit per attempted URL.`},{key:"paa",label:"PAA harvest",aliases:["harvest_paa","paa","people also ask","questions"],credits:i(t.paa),unit:`per question (+${i(t.paa_base)} credit base)`,notes:`Returns original-query PAA questions with answers and sources plus organic results and Google entity IDs. Optional same-page SERP modules are flag-controlled. Billed ${i(t.paa_base)} credit base + ${i(t.paa)} per question actually returned (unused estimate refunded).`},{key:"page_scrape",label:"Page crawl / extract",aliases:["extract_url","extract_site","page scrape","url scrape","single page","site crawl"],credits:i(t.page_scrape),unit:"per page",notes:"Applies to both single-URL extraction and per-page site crawls."},{key:"listing_create",label:"Directory or wiki listing creation",aliases:["local-sourcebook-capture","commons_submit_entity","directory listing","wiki listing"],credits:i(t.listing_create),unit:"per new listing",notes:"Fixed total price for a new Local Sourcebook directory listing or Transparent Commons wiki entity. Sourcebook acquisition is included."},{key:"listing_edit",label:"Directory or wiki listing edit",aliases:["local_sourcebook_refresh","commons_submit_entity","listing refresh","wiki edit"],credits:i(t.listing_edit),unit:"per existing listing revision",notes:"Fixed total price for refreshing an existing Local Sourcebook listing or revising an existing Transparent Commons entity."},{key:"url_map",label:"Site URL mapping",aliases:["map_site_urls","url map","site map","crawl urls"],credits:i(t.url_map),unit:"per mapping operation",notes:"Flat rate for the full /map-urls call regardless of URL count discovered."},{key:"yt_channel",label:"YouTube search / channel harvest",aliases:["youtube_harvest","youtube search","youtube channel","yt_channel"],credits:i(t.yt_channel),unit:"per call"},{key:"yt_transcription",label:"YouTube transcription",aliases:["youtube_transcribe","youtube transcript","transcription","yt_transcription"],credits:i(2e4),unit:"base + 2/min of video",notes:"Flat 200-credit base plus 2 credits per minute of video, reconciled to the real video length after transcription (e.g. a 10-minute video is about 220 credits)."},{key:"fb_ad",label:"Facebook search / ad lookup",aliases:["facebook_page_intel","facebook_ad_search","facebook_ad","facebook ads","fb ads"],credits:i(t.fb_ad),unit:"per call"},{key:"maps_search",label:"Maps business search",aliases:["maps_search","google maps search","gmb search","gbp search","business profiles"],credits:i(t.maps_search),unit:"per search",notes:"Returns up to 50 Google Maps business/profile candidates. Use maps_place_intel to hydrate selected businesses."},{key:"maps_place",label:"Maps business lookup",aliases:["maps_place_intel","google maps","maps place","place intel"],credits:i(t.maps_place),unit:"per business",notes:"Maps place runs reserve the 60-credit base plus the declared review maximum before browsing. Completed runs settle to the base plus unique saved reviews; stopped attempts are refunded."},{key:"maps_review",label:"Maps review",aliases:["maps_reviews","google reviews","review cards","reviews"],credits:i(t.maps_review),unit:"per review card",notes:"One credit per unique saved review on a completed Maps place run. The declared maximum is held before browsing; unused credits are refunded."},{key:"fb_search",label:"Facebook ad library search",aliases:["facebook_search","fb_search","fb ad search"],credits:i(t.fb_search),unit:"per search",notes:"Browser automation to search Facebook Ads Library by keyword."},{key:"google_ads_search",label:"Google Ads Transparency search",aliases:["google_ads_search","google ads search","ads transparency search"],credits:i(t.google_ads_search),unit:"per search",notes:"Browser automation to find advertisers in Google Ads Transparency Center by domain or name."},{key:"google_ads_intel",label:"Google Ads Transparency advertiser intel",aliases:["google_ads_page_intel","google ads intel","ads transparency intel"],credits:i(t.google_ads_intel),unit:"per call",notes:"Lists and hydrates an advertiser's creatives with image URLs and video references."},{key:"google_ads_transcribe",label:"Google ad video transcription",aliases:["google_ads_transcribe","google ad transcript"],credits:i(2e4),unit:"base + 2/min of video",notes:"Flat 200-credit base plus 2 credits per minute of video, reconciled to the real video length after transcription (e.g. a 10-minute video is about 220 credits)."},{key:"fb_transcribe",label:"Facebook video / ad transcription",aliases:["facebook_transcribe","facebook_video_transcribe","fb_transcribe","fb ad transcript"],credits:i(2e4),unit:"base + 2/min of video",notes:"Flat 200-credit base plus 2 credits per minute of video, reconciled to the real video length after transcription (e.g. a 10-minute video is about 220 credits)."},{key:"fb_reels_inventory",label:"Facebook Reel URL inventory",aliases:["facebook_reels_inventory","facebook reels","facebook reel urls","facebook profile reels"],credits:i(t.fb_reels_inventory),unit:"per profile scan",notes:"Collects canonical Reel URLs from a Facebook profile. Logged-out scans are capped at 60 URLs; saved browser profiles can request larger inventories."},{key:"tiktok_transcribe",label:"TikTok video transcription",aliases:["tiktok_video_transcribe","tiktok transcript"],credits:i(2e4),unit:"base + 2/min of video",notes:"Transcribes a public TikTok video from its playback media. Flat 200-credit base plus 2 credits per minute, reconciled after transcription."},{key:"instagram_profile",label:"Instagram profile content discovery",aliases:["instagram_profile_content","instagram profile","instagram content list","ig profile"],credits:i(t.instagram_profile),unit:"per profile scan",notes:"Browser extraction of public Instagram profile grid links. Complete history may require a logged-in profile."},{key:"instagram_media",label:"Instagram media download",aliases:["instagram_media_download","instagram reel download","instagram post download","ig media"],credits:i(t.instagram_media),unit:"per post or reel",notes:"Extracts post text, image URLs, and reel audio/video tracks, with local downloads when the server can write files."},{key:"instagram_transcribe",label:"Instagram media transcription",aliases:["instagram transcript","instagram reel transcript","ig transcribe"],credits:i(2e4),unit:"base + 2/min of video",notes:"Whisper transcription of selected Instagram audio/video media. Flat 200-credit base plus 2 credits per minute of video, reconciled to the real video length after transcription (e.g. a 10-minute video is about 220 credits)."},{key:"browser_minute",label:"Interactive browser session",aliases:["browser_open","browser agent","browser_agent","live browser","browse","browser control","interactive browser"],credits:i(t.browser_minute),unit:"per minute of use",notes:"Metered per minute of use for the whole time the browser session is open. Close the session to stop the meter; abandoned sessions are auto-closed after a short idle window."},{key:"reddit_thread",label:"Reddit thread base lookup",aliases:["reddit","reddit_thread","reddit comments","subreddit","reddit post"],credits:i(t.reddit_thread),unit:"per thread",notes:"Base lookup for the post itself. Comments billed separately per comment at reddit_comment rate. Refunded if the thread cannot be retrieved."},{key:"reddit_comment",label:"Reddit comment",aliases:["reddit_comment","reddit comments"],credits:i(t.reddit_comment),unit:"per comment",notes:"Charged per comment actually extracted, billed down automatically if the balance runs out mid-thread."},{key:"video_analysis",label:"Video breakdown (frame-by-frame + transcript)",aliases:["video_frame_analysis","video breakdown","video analysis","analyze video"],credits:i(t.video_analysis),unit:"per 120 frames (max 480)",notes:`Full video breakdown: samples frames across a video up to 60 minutes, analyzes visuals and audio, then produces a summary, pacing, topic outline, key points, hook, style, and replication guidance. Reserves ${i(t.video_analysis)} Credits per requested 120-frame tier, up to 480 frames. Unused frame tiers and failed-run frame charges are refunded. Incurred analysis-model usage is billed separately, including usage before a failure; terminal status checks settle the original run.`},{key:"trustpilot_reviews",label:"Trustpilot review harvest",aliases:["trustpilot_reviews","trustpilot","trustpilot reviews"],credits:i(t.trustpilot_reviews),unit:"per call",notes:"Base lookup. Reviews billed separately per card at trustpilot_review rate. Refunded if no reviews are found."},{key:"trustpilot_review",label:"Trustpilot review card",aliases:["trustpilot_review","trustpilot card"],credits:i(t.trustpilot_review),unit:"per review card",notes:"Charged per review actually extracted, billed down automatically if the balance runs out mid-page."},{key:"g2_reviews",label:"G2 review harvest",aliases:["g2_reviews","g2","g2 reviews"],credits:i(t.g2_reviews),unit:"per call",notes:"Base lookup. Reviews billed separately per card at g2_review rate. Refunded if no reviews are found."},{key:"g2_review",label:"G2 review card",aliases:["g2_review","g2 card"],credits:i(t.g2_review),unit:"per review card",notes:"Charged per review actually extracted (each card carries up to 3 Q&A sections), billed down automatically if the balance runs out mid-page."},{key:"diff_page",label:"Page change check",aliases:["diff_page","page diff","change detection"],credits:i(t.diff_page),unit:"per check",notes:"Same cost as a single page extract \u2014 one scrape is performed per check, then compared against your last stored snapshot for that URL."}],ve="price_1Ta1NRS8aAcsk3TGwsRnYbix";function w(e){return e.billing_interval??"month"}function P(e){return e.billed_usd??e.monthly_usd}function O(e){return e.credits_mc/o}var u={price_1TmiHRS8aAcsk3TGwmSNfNIa:{tier:"starter",label:"Starter",price_id:"price_1TmiHRS8aAcsk3TGwmSNfNIa",monthly_usd:12,credits_mc:8e6,concurrency:3,intro_coupon:"mcp-starter-1dollar-intro-12"},price_1TrgihS8aAcsk3TG5cglrq4D:{tier:"growth",label:"Growth",price_id:"price_1TrgihS8aAcsk3TG5cglrq4D",monthly_usd:40,credits_mc:26666700,concurrency:10,intro_coupon:null},price_1TrgihS8aAcsk3TG4HnG4gbY:{tier:"scale",label:"Scale",price_id:"price_1TrgihS8aAcsk3TG4HnG4gbY",monthly_usd:100,credits_mc:8e7,concurrency:20,intro_coupon:null},price_1TwqIVS8aAcsk3TG0FE3ddDM:{tier:"sonic",label:"Sonic Annual",price_id:"price_1TwqIVS8aAcsk3TG0FE3ddDM",monthly_usd:25,billed_usd:300,credits_mc:25e7,concurrency:3,intro_coupon:null,billing_interval:"year",credits_never_expire:!0,private_offer:!0},price_1TwqIWS8aAcsk3TGbGbtlLak:{tier:"sonic-memory",label:"Sonic Annual + Memory Pro",price_id:"price_1TwqIWS8aAcsk3TGbGbtlLak",monthly_usd:34.5,billed_usd:414,credits_mc:25e7,concurrency:3,intro_coupon:null,billing_interval:"year",credits_never_expire:!0,includes_memory:!0,private_offer:!0}},C=Object.fromEntries(Object.values(u).map(e=>[e.tier,e])),E={sonic:{slug:"sonic",label:"Sonic annual partner offer",headline:"A full year of MCP Scraper, with Credits that never expire.",tier_keys:["sonic","sonic-memory"]}};function X(e){let r=Object.values(E).find(a=>a.tier_keys.includes(e));return r?`/${r.slug}/claim`:null}function z(e){return e?Object.values(E).find(r=>r.tier_keys.includes(e))??null:null}function Se(e,r){let a=z(e);return a?!a.tier_keys.includes(r):!1}function Ie(e){if(!e)return null;let r=C[e];return r?{tier:r.tier,label:r.label,amount_usd:P(r),interval:w(r),monthly_equivalent_usd:r.monthly_usd,credits_per_interval:O(r),concurrency:r.concurrency,credits_never_expire:!!r.credits_never_expire,includes_memory:!!r.includes_memory,private_offer:!!r.private_offer,offer_claim_path:X(r.tier)}:null}function Ae(e){let r=E[e];if(!r)return null;let a=r.tier_keys.map(s=>C[s]).filter(s=>!!s).map(s=>({tier:s.tier,label:s.label,amount_usd:P(s),interval:w(s),monthly_equivalent_usd:s.monthly_usd,credits:O(s),credits_never_expire:!!s.credits_never_expire,includes_memory:!!s.includes_memory,concurrency:s.concurrency}));return a.length?{slug:r.slug,label:r.label,headline:r.headline,options:a}:null}function xe(){let e=C.starter,r=Z.map(({aliases:n,...c})=>({...c,...c.notes?{notes:c.notes.replace(/ via fal\.ai/g,"")}:{}})),a=Object.fromEntries(r.map(n=>[n.key,{credits:n.credits,unit:n.unit,label:n.label,...n.notes?{notes:n.notes}:{}}])),s=Object.values(u).filter(n=>!n.private_offer).map(n=>({tier:n.tier,label:n.label,monthlyUsd:n.monthly_usd,credits:n.credits_mc/o,concurrency:n.concurrency}));return{...g,operationCosts:a,publicTiers:s,concurrencyPack:{ratePolicyVersion:B,monthlyUsd:U,currency:L,interval:D,billingUnit:"pack",slotsPerPack:f},scaleCreditPack:{ratePolicyVersion:ee,eligibleTier:"scale",incrementUsd:G,creditsPerIncrement:R,maxQuantity:re,currency:"usd",expiryMonths:3},creditsPerDollarStarter:Math.round(i(e.credits_mc)/e.monthly_usd),costs:r,scheduling:g.addons.scheduling,connectedAccounts:g.addons.connectedAccounts,listingWrites:V}}var U=5,L="usd",D="month",f=2,B="2026-08-14.concurrency-pack-v1",J="$5/month for 2 extra browsers",Ne="MCP Scraper browser concurrency pack \u2014 each $5 monthly pack adds 2 browsers.",ke="Billed monthly until canceled. A receipt is emailed after each successful payment.",T="npx -y -p mcp-scraper@latest mcp-scraper-cli billing concurrency checkout",ee="2026-09-21.scale-credit-pack-v1",G=10,R=8e4,Me=R*o,re=100;function we(){let e=process.env.CONCURRENCY_BILLING_URL??"https://mcpscraper.dev/billing";return{product:"Extra browser concurrency pack",rate_policy_version:B,price_label:J,unit_amount_usd:U,currency:L,interval:D,billing_unit:"pack",pack_quantity:1,slots_per_pack:f,extra_slots:f,billing_url:e,terminal_command:T,terminal_command_with_api_key_env:`MCP_SCRAPER_API_KEY=sk_live_your_key ${T}`}}function i(e){return e/o}var te="npx -y -p mcp-scraper@latest mcp-scraper-cli billing subscribe";function ie(){return Object.values(u).filter(e=>!e.private_offer).sort((e,r)=>e.credits_mc-r.credits_mc)}function Pe(e,r){let a=process.env.TOPUP_URL??"https://mcpscraper.dev/billing",s=i(e),n=i(r),c=ie().map(m=>m.tier).join("|"),d=`${te} <${c}>`;return{error:"insufficient_balance",error_code:"insufficient_balance",message:`Insufficient credits. Balance: ${s} credits. This call requires ${n} credits. Credits refresh when your plan renews. Scale customers can buy ${R} Credits per $${G} increment; other customers can move to a larger plan at ${a} (terminal: ${d})`,balance_credits:s,required_credits:n,topup_url:a,upgrade_command:d}}var ne=1e7*1,Oe={price_1TnMSTS8aAcsk3TGBgiwuvqL:{plan:"pro",label:"Pro",price_id:"price_1TnMSTS8aAcsk3TGBgiwuvqL",interval:"month",monthly_usd:19,quota_bytes:5e9},price_1TnMSTS8aAcsk3TGWBVU2agY:{plan:"pro",label:"Pro",price_id:"price_1TnMSTS8aAcsk3TGWBVU2agY",interval:"year",monthly_usd:19,quota_bytes:5e9}},Ue={free:ne,pro:5e9,team:5e10},se=3;var Le=19/se,De=3,Be=1.5,Ge=1.5,Y=2e4,q=200;function Ye(e){let r=Math.max(1,Math.ceil((e||60)/60));return Y+q*r}var qe=Y+q*30,Fe=u.price_1TmiHRS8aAcsk3TGwmSNfNIa.credits_mc/u.price_1TmiHRS8aAcsk3TGwmSNfNIa.monthly_usd;export{de as a,pe as b,ue as c,$ as d,me as e,ge as f,be as g,V as h,y as i,j,fe as k,Q as l,Ce as m,Ee as n,W as o,S as p,Re as q,I as r,A as s,x as t,N as u,he as v,ye as w,Te as x,Z as y,ve as z,w as A,P as B,O as C,u as D,C as E,X as F,z as G,Se as H,Ie as I,Ae as J,xe as K,U as L,f as M,Ne as N,ke as O,G as P,R as Q,Me as R,re as S,we as T,i as U,Pe as V,Oe as W,Ue as X,De as Y,Be as Z,Ge as _,Y as $,q as aa,Ye as ba,qe as ca,Fe as da};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import{a as u}from"./chunk-WNUXMPVX.js";var L="2026-06-26 21:58:03";var W="+3 months";function R(e){return{sql:`INSERT INTO credit_lots (user_id, amount_mc, remaining_mc, source, expires_at)
|
|
2
|
+
SELECT ?, legacy.balance_mc, legacy.balance_mc, 'migration', datetime('now', ?)
|
|
3
|
+
FROM (
|
|
4
|
+
SELECT COALESCE(SUM(amount_mc), 0) AS balance_mc
|
|
5
|
+
FROM ledger
|
|
6
|
+
WHERE user_id = ?
|
|
7
|
+
) AS legacy
|
|
8
|
+
WHERE legacy.balance_mc > 0
|
|
9
|
+
AND EXISTS (
|
|
10
|
+
SELECT 1 FROM ledger
|
|
11
|
+
WHERE user_id = ? AND created_at < ?
|
|
12
|
+
)
|
|
13
|
+
AND NOT EXISTS (
|
|
14
|
+
SELECT 1 FROM credit_lots WHERE user_id = ?
|
|
15
|
+
)`,args:[e,W,e,e,L,e]}}async function T(e){return(await u().execute(R(e))).rowsAffected}import{createHash as O,randomUUID as A}from"crypto";var _=class extends Error{constructor(){super("Current operation wallet claim and matching durable plan are required"),this.name="WalletClaimLostError"}},b=class extends Error{constructor(){super("Named operation debit plan conflicts with durable billing history"),this.name="DebitPlanConflictError"}};function y(e){if(!Number.isSafeInteger(e.userId)||e.userId<=0||!Number.isSafeInteger(e.amountMc)||e.amountMc<=0||!e.key||e.key.length>500||!e.operation||!e.description||e.description.length>500)throw new b;return{sql:`EXISTS (SELECT 1 FROM billing_debits d WHERE d.idempotency_key=? AND d.user_id=?
|
|
16
|
+
AND d.amount_mc=? AND d.operation=? AND d.description=? AND d.status='applied'
|
|
17
|
+
AND NOT EXISTS (SELECT 1 FROM billing_events b WHERE b.user_id=d.user_id
|
|
18
|
+
AND b.billing_class='wallet_hold_settlement'
|
|
19
|
+
AND json_extract(b.metadata,'$.debitIdempotencyKey')=d.idempotency_key))`,args:[e.key,e.userId,e.amountMc,e.operation,e.description]}}function v(e,t){if(!/^[a-z_]+\.[a-z_]+$/.test(e)||!/^json_extract\([a-z_]+\.[a-z_]+,'\$\.hold\.key'\)$/.test(t))throw new Error("Invalid stored hold selector");return`EXISTS (SELECT 1 FROM billing_debits d WHERE d.user_id=${e}
|
|
20
|
+
AND d.idempotency_key=${t} AND d.status='applied'
|
|
21
|
+
AND NOT EXISTS (SELECT 1 FROM billing_events b WHERE b.user_id=d.user_id
|
|
22
|
+
AND b.billing_class='wallet_hold_settlement' AND json_extract(b.metadata,'$.debitIdempotencyKey')=d.idempotency_key))`}var D="(CAST(strftime('%s', 'now') AS INTEGER) * 1000)",f="operation-debit:",w=e=>e==="execute"?"e.phase = 'effects_started' AND r.status = 'running'":"e.phase = 'unknown' AND r.status = 'unknown'";function C(e,t,r){if(!r){if(t.startsWith(f))throw new _;return}if(r.userId!==e||!r.runId||!r.token||!["execute","compensate"].includes(r.mode))throw new _}var h=e=>String(e).includes("operation_wallet_claim_current");function x(e,t,r,n){if(C(e,t,r),!r){let a=A(),c="NOT EXISTS (SELECT 1 FROM operation_debit_plans WHERE debit_key = ?)";return{before:[{sql:`INSERT INTO operation_wallet_fences (id, allowed)
|
|
23
|
+
SELECT ?, CASE WHEN ${c} THEN 1 ELSE 0 END`,args:[a,t]}],after:[{sql:"DELETE FROM operation_wallet_fences WHERE id = ?",args:[a]}],current:async p=>!!(await p.execute({sql:`SELECT ${c} AS allowed`,args:[t]})).rows[0]?.allowed}}if(n.kind==="debit"&&r.mode!=="execute"||r.mode==="compensate"&&n.amountMc!==0&&!r.settlementPlanId)throw new _;let i=r.settlementPlanId?" AND EXISTS (SELECT 1 FROM operation_final_settlements s WHERE s.id=? AND s.run_id=p.run_id AND s.user_id=r.user_id AND s.debit_key=p.debit_key AND s.final_amount_mc=? AND s.refund_operation=? AND s.source_surface=?)":"",l=`SELECT 1 FROM operation_debit_plans p
|
|
24
|
+
JOIN operation_runs r ON r.id = p.run_id JOIN operation_executions e ON e.run_id = r.id
|
|
25
|
+
WHERE p.debit_key = ? AND r.id = ? AND r.user_id = ? AND e.claim_token = ?
|
|
26
|
+
AND e.expires_at_ms > ${D} AND ${w(r.mode)}
|
|
27
|
+
AND ${n.kind==="debit"?"p.amount_mc = ? AND p.operation = ? AND p.description = ?":"p.refund_operation = ? AND p.source_surface = ?"}${i}`,d=[t,r.runId,e,r.token,...n.kind==="debit"?[n.amountMc,n.operation,n.description]:[n.operation,n.sourceSurface],...r.settlementPlanId?[r.settlementPlanId,n.amountMc,n.operation,n.sourceSurface]:[]],o=A(),s=n.kind==="debit"?{sql:`INSERT OR IGNORE INTO operation_billing_links (run_id, billing_table, billing_key, relation, amount_mc)
|
|
28
|
+
SELECT p.run_id, 'billing_debits', d.idempotency_key, 'debit', d.amount_mc FROM operation_debit_plans p
|
|
29
|
+
JOIN billing_debits d ON d.idempotency_key = p.debit_key
|
|
30
|
+
WHERE p.debit_key = ? AND d.user_id = ? AND d.status = 'applied'`,args:[t,e]}:{sql:`INSERT OR IGNORE INTO operation_billing_links (run_id, billing_table, billing_key, relation, amount_mc)
|
|
31
|
+
SELECT p.run_id, 'billing_events', b.id, 'settlement', b.amount_mc FROM operation_debit_plans p
|
|
32
|
+
JOIN billing_events b ON b.idempotency_key = ?
|
|
33
|
+
WHERE p.debit_key = ? AND b.user_id = ? AND b.status = 'settled'`,args:[`debit-settlement:${O("sha256").update(t).digest("hex")}`,t,e]};return{before:[{sql:`INSERT INTO operation_wallet_fences (id, allowed)
|
|
34
|
+
SELECT ?, CASE WHEN EXISTS (${l}) THEN 1 ELSE 0 END`,args:[o,...d]},R(e)],after:[s,{sql:"DELETE FROM operation_wallet_fences WHERE id = ?",args:[o]}],current:async a=>(await a.execute({sql:l,args:d})).rows.length===1}}function M(e){return{label:String(e.label),key:String(e.debit_key),amountMc:Number(e.amount_mc),operation:String(e.operation),description:String(e.description),refundOperation:String(e.refund_operation),sourceSurface:String(e.source_surface)}}async function B(e,t){if(e.mode!=="execute"||!Number.isSafeInteger(t.amountMc)||t.amountMc<=0||!t.label||t.label.length>200||!t.operation||!t.refundOperation||!t.sourceSurface)throw new b;let r=u(),n=f+A();C(e.userId,n,e);let i=`EXISTS (SELECT 1 FROM operation_runs r JOIN operation_executions e ON e.run_id = r.id
|
|
35
|
+
WHERE r.id = ? AND r.user_id = ? AND e.claim_token = ? AND e.expires_at_ms > ${D}
|
|
36
|
+
AND ${w("execute")})`,l=[e.runId,e.userId,e.token],d;try{await r.batch([{sql:`INSERT OR IGNORE INTO operation_debit_plans
|
|
37
|
+
(run_id, label, debit_key, amount_mc, operation, description, refund_operation, source_surface)
|
|
38
|
+
SELECT ?, ?, ?, ?, ?, ?, ?, ? WHERE ${i}`,args:[e.runId,t.label,n,t.amountMc,t.operation,t.description,t.refundOperation,t.sourceSurface,...l]},{sql:`UPDATE operation_debit_plans SET amount_mc = ? WHERE run_id = ? AND label = ?
|
|
39
|
+
AND operation = ? AND description = ? AND refund_operation = ? AND source_surface = ? AND ${i}
|
|
40
|
+
AND EXISTS (SELECT 1 FROM billing_debits d WHERE d.idempotency_key = operation_debit_plans.debit_key
|
|
41
|
+
AND d.user_id = ? AND d.status = 'claimed' AND d.amount_mc = operation_debit_plans.amount_mc
|
|
42
|
+
AND d.operation = operation_debit_plans.operation AND d.description = operation_debit_plans.description)`,args:[t.amountMc,e.runId,t.label,t.operation,t.description,t.refundOperation,t.sourceSurface,...l,e.userId]}],"write")}catch(a){d=a}let o;try{o=(await r.execute({sql:`SELECT p.* FROM operation_debit_plans p WHERE p.run_id = ? AND p.label = ? AND ${i}`,args:[e.runId,t.label,...l]})).rows[0]}catch(a){throw d??a}if(!o)throw d||new _;let s=M(o);if(s.amountMc!==t.amountMc||s.operation!==t.operation||s.description!==t.description||s.refundOperation!==t.refundOperation||s.sourceSurface!==t.sourceSurface)throw new b;return s}async function J(e){C(e.userId,f,e);let t=u();if(!(await t.execute({sql:`SELECT 1 FROM operation_executions e JOIN operation_runs r ON r.id = e.run_id
|
|
43
|
+
WHERE r.id = ? AND r.user_id = ? AND e.claim_token = ? AND e.expires_at_ms > ${D}
|
|
44
|
+
AND ${w(e.mode)}`,args:[e.runId,e.userId,e.token]})).rows.length)throw new _;return(await t.execute({sql:"SELECT * FROM operation_debit_plans WHERE run_id = ? ORDER BY label",args:[e.runId]})).rows.map(M)}async function V(e,t){let n=(await u().execute({sql:"SELECT user_id, amount_mc, operation, description, status FROM billing_debits WHERE idempotency_key = ? AND user_id = ? LIMIT 1",args:[t,e]})).rows[0];return n?{amountMc:Number(n.amount_mc),operation:String(n.operation),description:String(n.description),status:String(n.status)}:null}async function Y(e,t,r,n,i,l){if(!Number.isSafeInteger(t)||t<=0)throw new Error("idempotent debit amount must be a positive integer");if(!i||i.length>500)throw new Error("idempotent debit key is required and must be bounded");let d=u(),o=x(e,i,l,{kind:"debit",amountMc:t,operation:r,description:n});l||await T(e);let s;try{s=await d.batch([...o.before,{sql:"UPDATE billing_debits SET amount_mc = ? WHERE idempotency_key = ? AND user_id = ? AND operation = ? AND description = ? AND status = 'claimed'",args:[t,i,e,r,n]},{sql:"INSERT OR IGNORE INTO billing_debits (idempotency_key, user_id, amount_mc, operation, description, status) VALUES (?, ?, ?, ?, ?, 'claimed')",args:[i,e,t,r,n]},{sql:`UPDATE users
|
|
45
|
+
SET balance_mc = balance_mc - ?
|
|
46
|
+
WHERE id = ?
|
|
47
|
+
AND balance_mc >= ?
|
|
48
|
+
AND (SELECT COALESCE(SUM(remaining_mc), 0)
|
|
49
|
+
FROM credit_lots
|
|
50
|
+
WHERE user_id = ?
|
|
51
|
+
AND remaining_mc > 0
|
|
52
|
+
AND (never_expires = 1 OR expires_at > datetime('now'))) >= ?
|
|
53
|
+
AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND user_id = ? AND amount_mc = ? AND operation = ? AND description = ? AND status = 'claimed')`,args:[t,e,t,e,t,i,e,t,r,n]},{sql:"UPDATE billing_debits SET status = 'applying' WHERE idempotency_key = ? AND user_id = ? AND amount_mc = ? AND operation = ? AND description = ? AND status = 'claimed' AND changes() = 1",args:[i,e,t,r,n]},{sql:`INSERT OR IGNORE INTO billing_debit_allocations
|
|
54
|
+
(debit_idempotency_key, lot_id, user_id, amount_mc, expires_at)
|
|
55
|
+
WITH ordered AS MATERIALIZED (
|
|
56
|
+
SELECT id,
|
|
57
|
+
remaining_mc,
|
|
58
|
+
expires_at,
|
|
59
|
+
COALESCE(SUM(remaining_mc) OVER (
|
|
60
|
+
ORDER BY never_expires ASC, expires_at ASC, granted_at ASC, id ASC
|
|
61
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
|
|
62
|
+
), 0) AS prior_mc
|
|
63
|
+
FROM credit_lots
|
|
64
|
+
WHERE user_id = ? AND remaining_mc > 0 AND (never_expires = 1 OR expires_at > datetime('now'))
|
|
65
|
+
), allocation AS (
|
|
66
|
+
SELECT id, expires_at, MIN(remaining_mc, MAX(0, ? - prior_mc)) AS take_mc
|
|
67
|
+
FROM ordered
|
|
68
|
+
)
|
|
69
|
+
SELECT ?, id, ?, take_mc, expires_at
|
|
70
|
+
FROM allocation
|
|
71
|
+
WHERE take_mc > 0
|
|
72
|
+
AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND status = 'applying')`,args:[e,t,i,e,i]},{sql:`UPDATE credit_lots
|
|
73
|
+
SET remaining_mc = remaining_mc - COALESCE((
|
|
74
|
+
SELECT amount_mc FROM billing_debit_allocations
|
|
75
|
+
WHERE debit_idempotency_key = ? AND lot_id = credit_lots.id
|
|
76
|
+
), 0)
|
|
77
|
+
WHERE id IN (
|
|
78
|
+
SELECT lot_id FROM billing_debit_allocations WHERE debit_idempotency_key = ?
|
|
79
|
+
)
|
|
80
|
+
AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND status = 'applying')`,args:[i,i,i]},{sql:"INSERT INTO ledger (user_id, amount_mc, operation, description) SELECT user_id, -amount_mc, operation, description FROM billing_debits WHERE idempotency_key = ? AND status = 'applying'",args:[i]},{sql:"UPDATE billing_debits SET status = 'applied', applied_at = datetime('now') WHERE idempotency_key = ? AND status = 'applying'",args:[i]},...o.after],"write")}catch(g){if(h(g))throw new _;try{if(!await o.current(d))throw new _;let E=(await d.execute({sql:"SELECT user_id, amount_mc, operation, description, status FROM billing_debits WHERE idempotency_key = ? AND status = 'applied' LIMIT 1",args:[i]})).rows[0];if(E&&Number(E.user_id)===e&&Number(E.amount_mc)===t&&String(E.operation)===r&&String(E.description)===n){let S=await d.execute({sql:"SELECT balance_mc FROM users WHERE id = ?",args:[e]});return{ok:!0,balance_mc:Number(S.rows[0]?.balance_mc??0),duplicate:!0}}}catch(m){if(m instanceof _)throw m}throw g}let c=(await d.execute({sql:"SELECT user_id, amount_mc, operation, description, status FROM billing_debits WHERE idempotency_key = ? LIMIT 1",args:[i]})).rows[0];if(!c||Number(c.user_id)!==e||Number(c.amount_mc)!==t||String(c.operation)!==r||String(c.description)!==n)throw new Error("idempotent debit key conflicts with another debit");let p=await d.execute({sql:"SELECT balance_mc FROM users WHERE id = ?",args:[e]});return{ok:String(c.status)==="applied",balance_mc:Number(p.rows[0]?.balance_mc??0),duplicate:s[2+o.before.length]?.rowsAffected===0&&String(c.status)==="applied"}}async function z(e,t,r,n,i,l,d){if(!Number.isSafeInteger(r)||r<0)throw new Error("idempotent debit settlement amount must be a non-negative integer");if(!t||t.length>500)throw new Error("idempotent debit settlement key is required and must be bounded");if(!n||!l)throw new Error("idempotent debit settlement operation and source are required");let o=u(),s=x(e,t,d,{kind:"settlement",amountMc:r,operation:n,description:i,sourceSurface:l});d||await T(e);let a=`debit-settlement:${O("sha256").update(t).digest("hex")}`,c="wallet_hold_settlement",p=A(),g=JSON.stringify({debitIdempotencyKey:t,refundOperation:n,description:i}),m=()=>k(e,t,l),E;try{E=await o.batch([...s.before,{sql:`INSERT OR IGNORE INTO billing_events
|
|
81
|
+
(id, user_id, idempotency_key, billing_class, source_surface, status, amount_mc, metadata)
|
|
82
|
+
VALUES (?, ?, ?, ?, ?, 'settling', ?, ?)`,args:[p,e,a,c,l,r,g]},{sql:`WITH ordered AS (
|
|
83
|
+
SELECT a.lot_id,
|
|
84
|
+
a.amount_mc,
|
|
85
|
+
a.expires_at,
|
|
86
|
+
COALESCE(SUM(a.amount_mc) OVER (
|
|
87
|
+
ORDER BY a.expires_at ASC, a.lot_id ASC
|
|
88
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
|
|
89
|
+
), 0) AS prior_mc
|
|
90
|
+
FROM billing_debit_allocations a
|
|
91
|
+
WHERE a.debit_idempotency_key = ?
|
|
92
|
+
), refunds AS (
|
|
93
|
+
SELECT lot_id,
|
|
94
|
+
expires_at,
|
|
95
|
+
amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
|
|
96
|
+
FROM ordered
|
|
97
|
+
)
|
|
98
|
+
UPDATE users
|
|
99
|
+
SET balance_mc = balance_mc + CASE
|
|
100
|
+
WHEN EXISTS (SELECT 1 FROM ordered) THEN COALESCE(
|
|
101
|
+
(SELECT SUM(refund_mc) FROM refunds WHERE expires_at > datetime('now')),
|
|
102
|
+
0
|
|
103
|
+
)
|
|
104
|
+
-- Holds created before allocation provenance cannot be safely
|
|
105
|
+
-- tied back to a source lot. Treat their unused portion as
|
|
106
|
+
-- expired instead of minting spendable credits with a guessed
|
|
107
|
+
-- expiry.
|
|
108
|
+
ELSE 0
|
|
109
|
+
END
|
|
110
|
+
WHERE id = ?
|
|
111
|
+
AND EXISTS (
|
|
112
|
+
SELECT 1
|
|
113
|
+
FROM billing_events e
|
|
114
|
+
JOIN billing_debits d ON d.idempotency_key = ?
|
|
115
|
+
WHERE e.idempotency_key = ?
|
|
116
|
+
AND e.user_id = ?
|
|
117
|
+
AND e.billing_class = ?
|
|
118
|
+
AND e.source_surface = ?
|
|
119
|
+
AND e.status = 'settling'
|
|
120
|
+
AND d.user_id = e.user_id
|
|
121
|
+
AND d.status = 'applied'
|
|
122
|
+
AND e.amount_mc BETWEEN 0 AND d.amount_mc
|
|
123
|
+
)`,args:[t,r,e,t,a,e,c,l]},{sql:`UPDATE billing_events
|
|
124
|
+
SET status = 'applying'
|
|
125
|
+
WHERE idempotency_key = ?
|
|
126
|
+
AND user_id = ?
|
|
127
|
+
AND billing_class = ?
|
|
128
|
+
AND source_surface = ?
|
|
129
|
+
AND status = 'settling'
|
|
130
|
+
AND changes() = 1`,args:[a,e,c,l]},{sql:`WITH ordered AS (
|
|
131
|
+
SELECT a.lot_id,
|
|
132
|
+
a.amount_mc,
|
|
133
|
+
a.expires_at,
|
|
134
|
+
COALESCE(SUM(a.amount_mc) OVER (
|
|
135
|
+
ORDER BY a.expires_at ASC, a.lot_id ASC
|
|
136
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
|
|
137
|
+
), 0) AS prior_mc
|
|
138
|
+
FROM billing_debit_allocations a
|
|
139
|
+
WHERE a.debit_idempotency_key = ?
|
|
140
|
+
), refunds AS (
|
|
141
|
+
SELECT lot_id,
|
|
142
|
+
expires_at,
|
|
143
|
+
amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
|
|
144
|
+
FROM ordered
|
|
145
|
+
)
|
|
146
|
+
UPDATE credit_lots
|
|
147
|
+
SET remaining_mc = remaining_mc + COALESCE((SELECT refund_mc FROM refunds WHERE refunds.lot_id = credit_lots.id), 0)
|
|
148
|
+
WHERE id IN (
|
|
149
|
+
SELECT lot_id FROM refunds
|
|
150
|
+
WHERE refund_mc > 0 AND expires_at > datetime('now')
|
|
151
|
+
)
|
|
152
|
+
AND EXISTS (SELECT 1 FROM billing_events WHERE idempotency_key = ? AND status = 'applying')`,args:[t,r,a]},{sql:`WITH ordered AS (
|
|
153
|
+
SELECT a.amount_mc,
|
|
154
|
+
a.expires_at,
|
|
155
|
+
COALESCE(SUM(a.amount_mc) OVER (
|
|
156
|
+
ORDER BY a.expires_at ASC, a.lot_id ASC
|
|
157
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
|
|
158
|
+
), 0) AS prior_mc
|
|
159
|
+
FROM billing_debit_allocations a
|
|
160
|
+
WHERE a.debit_idempotency_key = ?
|
|
161
|
+
), refunds AS (
|
|
162
|
+
SELECT expires_at,
|
|
163
|
+
amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
|
|
164
|
+
FROM ordered
|
|
165
|
+
), credited AS (
|
|
166
|
+
SELECT e.user_id,
|
|
167
|
+
CASE
|
|
168
|
+
WHEN EXISTS (SELECT 1 FROM ordered) THEN COALESCE(
|
|
169
|
+
(SELECT SUM(refund_mc) FROM refunds WHERE expires_at > datetime('now')),
|
|
170
|
+
0
|
|
171
|
+
)
|
|
172
|
+
ELSE 0
|
|
173
|
+
END AS refund_mc
|
|
174
|
+
FROM billing_events e
|
|
175
|
+
JOIN billing_debits d ON d.idempotency_key = ?
|
|
176
|
+
WHERE e.idempotency_key = ?
|
|
177
|
+
AND e.status = 'applying'
|
|
178
|
+
)
|
|
179
|
+
INSERT INTO ledger (user_id, amount_mc, operation, description)
|
|
180
|
+
SELECT user_id,
|
|
181
|
+
refund_mc,
|
|
182
|
+
?,
|
|
183
|
+
?
|
|
184
|
+
FROM credited
|
|
185
|
+
WHERE refund_mc > 0`,args:[t,r,t,a,n,i]},{sql:`UPDATE billing_events
|
|
186
|
+
SET status = 'settled', settled_at = datetime('now')
|
|
187
|
+
WHERE idempotency_key = ?
|
|
188
|
+
AND status = 'applying'`,args:[a]},...s.after],"write")}catch(I){if(h(I))throw new _;try{if(!await s.current(o))throw new _;let N=await m();if(N)return N}catch(N){if(N instanceof _)throw N}throw I}let S=await m();if(!S)throw new Error("idempotent debit settlement did not reach a durable settled state");return{...S,duplicate:E[s.before.length]?.rowsAffected===0}}async function Z(e,t){let{hold:r}=t,n=y(r);if(r.userId!==e.userId||!e.runId||!e.token||!t.label||t.label.length>200||!t.refundOperation||!t.sourceSurface||!t.tool||!t.jobId)throw new b;let i=`EXISTS (SELECT 1 FROM operation_runs r JOIN operation_executions e ON e.run_id=r.id
|
|
189
|
+
WHERE r.id=? AND r.user_id=? AND r.tool=? AND r.job_id=? AND r.status='running'
|
|
190
|
+
AND json_extract(r.normalized_flags_json,'$._storeGateRequired') IS 1
|
|
191
|
+
AND e.claim_token=? AND e.phase='claimed' AND e.expires_at_ms > ${D})`,l=[e.runId,e.userId,t.tool,t.jobId,e.token],d=u(),o;try{await d.batch([{sql:`INSERT OR IGNORE INTO operation_debit_plans
|
|
192
|
+
(run_id,label,debit_key,amount_mc,operation,description,refund_operation,source_surface)
|
|
193
|
+
SELECT ?,?,?,?,?,?,?,? WHERE ${i} AND (${n.sql})`,args:[e.runId,t.label,r.key,r.amountMc,r.operation,r.description,t.refundOperation,t.sourceSurface,...l,...n.args]},{sql:`INSERT OR IGNORE INTO operation_billing_links (run_id,billing_table,billing_key,relation,amount_mc)
|
|
194
|
+
SELECT p.run_id,'billing_debits',p.debit_key,'hold',p.amount_mc FROM operation_debit_plans p
|
|
195
|
+
WHERE p.run_id=? AND p.label=? AND p.debit_key=? AND p.amount_mc=? AND p.operation=? AND p.description=?
|
|
196
|
+
AND p.refund_operation=? AND p.source_surface=? AND ${i} AND (${n.sql})`,args:[e.runId,t.label,r.key,r.amountMc,r.operation,r.description,t.refundOperation,t.sourceSurface,...l,...n.args]}],"write")}catch(c){o=c}let s;try{s=(await d.execute({sql:`SELECT p.* FROM operation_debit_plans p
|
|
197
|
+
WHERE p.run_id=? AND p.label=? AND ${i} AND (${n.sql})`,args:[e.runId,t.label,...l,...n.args]})).rows[0]}catch(c){throw o??c}if(!s)throw o||new _;let a=M(s);if(a.key!==r.key||a.amountMc!==r.amountMc||a.operation!==r.operation||a.description!==r.description||a.refundOperation!==t.refundOperation||a.sourceSurface!==t.sourceSurface)throw new b;return a}async function k(e,t,r){if(!Number.isSafeInteger(e)||e<=0||!t||!r)throw new _;let n=u(),i=`debit-settlement:${O("sha256").update(t).digest("hex")}`,l="wallet_hold_settlement",o=(await n.execute({sql:`SELECT e.user_id,
|
|
198
|
+
e.status,
|
|
199
|
+
e.amount_mc AS final_amount_mc,
|
|
200
|
+
e.billing_class,
|
|
201
|
+
e.source_surface,
|
|
202
|
+
e.settled_at,
|
|
203
|
+
d.amount_mc AS held_amount_mc
|
|
204
|
+
FROM billing_events e
|
|
205
|
+
JOIN billing_debits d ON d.idempotency_key = ?
|
|
206
|
+
WHERE e.idempotency_key = ?
|
|
207
|
+
AND d.user_id = e.user_id
|
|
208
|
+
AND d.status = 'applied'
|
|
209
|
+
LIMIT 1`,args:[t,i]})).rows[0];if(!o)return null;if(Number(o.user_id)!==e||String(o.billing_class)!==l||String(o.source_surface)!==r)throw new Error("idempotent debit settlement key conflicts with another settlement");if(String(o.status)!=="settled")return null;let s=Number(o.held_amount_mc),a=Number(o.final_amount_mc);if(!Number.isSafeInteger(s)||!Number.isSafeInteger(a)||a<0||a>s)throw new Error("idempotent debit settlement receipt is invalid");let c=s-a,p=await n.execute({sql:`WITH ordered AS (
|
|
210
|
+
SELECT a.amount_mc,
|
|
211
|
+
a.expires_at,
|
|
212
|
+
COALESCE(SUM(a.amount_mc) OVER (
|
|
213
|
+
ORDER BY a.expires_at ASC, a.lot_id ASC
|
|
214
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
|
|
215
|
+
), 0) AS prior_mc
|
|
216
|
+
FROM billing_debit_allocations a
|
|
217
|
+
WHERE a.debit_idempotency_key = ?
|
|
218
|
+
), refunds AS (
|
|
219
|
+
SELECT expires_at,
|
|
220
|
+
amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
|
|
221
|
+
FROM ordered
|
|
222
|
+
)
|
|
223
|
+
SELECT (SELECT COUNT(*) FROM ordered) AS allocation_count,
|
|
224
|
+
COALESCE(SUM(CASE WHEN expires_at > ? THEN refund_mc ELSE 0 END), 0) AS credited_refund_mc
|
|
225
|
+
FROM refunds`,args:[t,a,String(o.settled_at)]}),m=Number(p.rows[0]?.allocation_count??0)>0?Number(p.rows[0]?.credited_refund_mc??0):0,E=await n.execute({sql:"SELECT balance_mc FROM users WHERE id = ?",args:[e]});return{balance_mc:Number(E.rows[0]?.balance_mc??0),final_amount_mc:a,refund_mc:m,expired_refund_mc:c-m,duplicate:!0}}export{W as a,T as b,y as c,v as d,B as e,J as f,V as g,Y as h,z as i,Z as j,k};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as f,d,e as p,h as u,i as A,j as m}from"./chunk-SEZETSB3.js";import{c as s,i as l}from"./chunk-SDQW7IJN.js";import{randomUUID as D}from"crypto";var x="connected-data-exports/",_=10080*60*1e3,b=900*1e3;function C(){return process.env.CONNECTED_DATA_READ_WRITE_TOKEN?.trim()||process.env.CONNECTED_DATA_BLOB_READ_WRITE_TOKEN?.trim()||null}function r(){return{prefix:x,artifactTtlMs:_,downloadTtlMs:b,token:C()}}function i(t){return(t.toLowerCase().replace(/[^a-z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||"connected-data-export").slice(0,120)}function N(t){return l(t,x)}function w(t){return f(t,r())}function O(t,e=Date.now()){let n=w(t);return!n||n.getTime()<=e}async function P(t){return g({...t,contentType:"application/x-ndjson"})}async function g(t){let e=t.stableCreatedAt?Date.parse(t.stableCreatedAt):NaN,n=Number.isFinite(e)?e:Date.now(),c=i(t.filename),y=t.stableKey?i(t.stableKey):D(),a;try{a=await p({policy:r(),ownerId:t.ownerId,artifactKey:`${i(t.exportId)}-${y}--${c}`,createdAt:n,filename:c,contentType:t.contentType,content:typeof t.content=="string"?t.content:Buffer.from(t.content)})}catch(o){throw o instanceof Error&&o.message==="private_artifact_blob_not_configured"?new Error("connected_data_private_blob_not_configured"):o}return{...a,...a.capabilities.includes("text")?{readback:{tool:"report_artifact_read",arguments:{artifactId:a.artifactId,offset:0,maxBytes:2e4},continuation:"Repeat with offset set to the previous result nextOffset until nextOffset is null."}}:{}}}async function B(t){return u({...t,policy:r()})}async function R(t,e,n){return A({policy:r(),artifactId:t,offset:e,maxBytes:n})}async function U(t){try{return await m({...t,policy:r(),maxBytes:t.maxBytes??25*1024*1024})}catch(e){if(e instanceof s&&e.code==="read_limit")return null;throw e}}async function h(t={}){let e=t.now??new Date,n=r();return n.token?!t.force&&!(e.getUTCHours()===3&&e.getUTCMinutes()===17)?{deleted:0,store:"private-vercel-blob",skipped:!0}:d({policy:n,now:e,includeLegacy:!0,maxObjects:2e4}):{deleted:0,store:process.env.VERCEL==="1"||process.env.NODE_ENV==="production"?"none":"local"}}export{x as a,_ as b,b as c,r as d,N as e,w as f,O as g,P as h,g as i,B as j,R as k,U as l,h as m};
|