z-zero-mcp-server 1.10.4 → 1.12.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/README.md +22 -11
- package/dist/base_backend.d.ts +46 -1
- package/dist/base_backend.js +143 -12
- package/dist/failure_taxonomy.d.ts +13 -6
- package/dist/failure_taxonomy.js +79 -6
- package/dist/index.js +650 -252
- package/dist/lib/api-base.d.ts +4 -1
- package/dist/lib/api-base.js +48 -26
- package/dist/lib/deal-search.d.ts +75 -0
- package/dist/lib/deal-search.js +182 -0
- package/dist/lib/domain-lock.d.ts +8 -0
- package/dist/lib/domain-lock.js +79 -16
- package/dist/lib/extract-total-price.d.ts +49 -16
- package/dist/lib/extract-total-price.js +286 -138
- package/dist/lib/key-store.d.ts +25 -2
- package/dist/lib/key-store.js +175 -25
- package/dist/lib/url-guard.js +12 -1
- package/dist/playwright_bridge.d.ts +25 -2
- package/dist/playwright_bridge.js +842 -146
- package/dist/types.d.ts +37 -1
- package/dist/ucp_bridge.js +12 -0
- package/package.json +4 -2
- package/dist/lib/web3-detector.d.ts +0 -19
- package/dist/lib/web3-detector.js +0 -157
|
@@ -1,165 +1,313 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// extract-total-price.ts
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// Reads the checkout total off a page for auto_pay_checkout — as a CROSS-CHECK
|
|
4
|
+
// against the ceiling the agent passes (max_amount), never as the authority.
|
|
5
|
+
//
|
|
6
|
+
// Rules (audit T12 F4, T13 F3; M.3):
|
|
7
|
+
// - only text a person can see counts: not display:none, not transparent,
|
|
8
|
+
// not a 0px font, not parked off-screen. A page used to get a $5 item
|
|
9
|
+
// charged as $49 by hiding "$49.00" somewhere;
|
|
10
|
+
// - a number is a price only with a currency mark next to it ("48 items" is
|
|
11
|
+
// not $48);
|
|
12
|
+
// - "0,49 $" is forty-nine cents, "1.234,56 €" is one thousand two hundred…;
|
|
13
|
+
// - the currency is read with the number. Anything not clearly US dollars is
|
|
14
|
+
// reported as such; the caller does not convert it.
|
|
6
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.parseAmount = parseAmount;
|
|
17
|
+
exports.parsePriceText = parsePriceText;
|
|
7
18
|
exports.extractTotalPrice = extractTotalPrice;
|
|
8
|
-
exports.
|
|
19
|
+
exports.dropTaxNote = dropTaxNote;
|
|
20
|
+
exports.parsePricesText = parsePricesText;
|
|
21
|
+
exports.extractCheckoutTotalStrict = extractCheckoutTotalStrict;
|
|
22
|
+
exports.pageMentionsOtherCurrency = pageMentionsOtherCurrency;
|
|
23
|
+
const NON_USD_SYMBOL = /[€£¥₹₩₺₽฿₴₫₱₦₪]|\b(?:CA|C|A|AU|NZ|HK|R|S|SG|MX|NT)\$/;
|
|
24
|
+
const NON_USD_CODE = /\b(?:EUR|GBP|JPY|CNY|RMB|INR|AUD|CAD|CHF|SGD|HKD|NZD|SEK|NOK|DKK|KRW|MXN|BRL|ZAR|AED|THB|MYR|PHP|VND|IDR|PLN|TRY|TWD|ILS|CZK|HUF)\b/;
|
|
25
|
+
const USD_MARK = /US\$|\bUSD\b|\$/;
|
|
26
|
+
/** "1,234.56" · "0,49" · "1.234,56" · "1,234" · "49" → number, or null. */
|
|
27
|
+
function parseAmount(raw) {
|
|
28
|
+
const s = raw.replace(/[\s ']/g, "");
|
|
29
|
+
if (!/^\d[\d.,]*$/.test(s))
|
|
30
|
+
return null;
|
|
31
|
+
const lastDot = s.lastIndexOf(".");
|
|
32
|
+
const lastComma = s.lastIndexOf(",");
|
|
33
|
+
let normalized;
|
|
34
|
+
if (lastDot >= 0 && lastComma >= 0) {
|
|
35
|
+
// The later separator is the decimal one.
|
|
36
|
+
normalized = lastDot > lastComma ? s.replace(/,/g, "") : s.replace(/\./g, "").replace(",", ".");
|
|
37
|
+
}
|
|
38
|
+
else if (lastComma >= 0) {
|
|
39
|
+
const decimals = s.length - lastComma - 1;
|
|
40
|
+
// "1,234" / "12,345,678" = thousands; "0,49" / "12,5" = decimals
|
|
41
|
+
normalized = decimals === 3 && /^\d{1,3}(,\d{3})+$/.test(s) ? s.replace(/,/g, "") : s.replace(/,(?=\d+$)/, ".").replace(/,/g, "");
|
|
42
|
+
}
|
|
43
|
+
else if (lastDot >= 0) {
|
|
44
|
+
const decimals = s.length - lastDot - 1;
|
|
45
|
+
normalized = decimals === 3 && /^\d{1,3}(\.\d{3})+$/.test(s) ? s.replace(/\./g, "") : s;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
normalized = s;
|
|
49
|
+
}
|
|
50
|
+
const v = Number(normalized);
|
|
51
|
+
return Number.isFinite(v) && v > 0 ? Math.round(v * 100) / 100 : null;
|
|
52
|
+
}
|
|
9
53
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
54
|
+
* A price in a string: a number with a currency mark right before or after it.
|
|
55
|
+
* Returns null when there is no such pair.
|
|
56
|
+
*/
|
|
57
|
+
function parsePriceText(text) {
|
|
58
|
+
const t = text.normalize("NFKC");
|
|
59
|
+
const MONEY = /((?:[A-Z]{2,3})?\$|US\$|[€£¥₹₩₺₽฿₴₫₱₦₪]|\b[A-Z]{3}\b)?\s*(\d[\d.,\s ]*\d|\d)\s*((?:[A-Z]{2,3})?\$|[€£¥₹₩₺₽฿₴₫₱₦₪]|\b[A-Z]{3}\b)?/g;
|
|
60
|
+
for (const m of t.matchAll(MONEY)) {
|
|
61
|
+
const before = m[1] || "";
|
|
62
|
+
const after = m[3] || "";
|
|
63
|
+
const mark = (before + " " + after).trim();
|
|
64
|
+
if (!mark)
|
|
65
|
+
continue;
|
|
66
|
+
const isNonUsd = NON_USD_SYMBOL.test(mark) || NON_USD_CODE.test(mark);
|
|
67
|
+
const isUsd = !isNonUsd && USD_MARK.test(mark);
|
|
68
|
+
if (!isNonUsd && !isUsd)
|
|
69
|
+
continue; // e.g. "48 ITEMS"
|
|
70
|
+
const amount = parseAmount(m[2]);
|
|
71
|
+
if (amount === null)
|
|
72
|
+
continue;
|
|
73
|
+
return { amount, currency: isUsd ? "USD" : "NON_USD", source: m[0].trim().slice(0, 40) };
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Multi-strategy total price extractor, visible text only.
|
|
79
|
+
* 1. Known total selectors for major platforms (Shopify, Stripe, Woo, generic)
|
|
80
|
+
* 2. Short visible texts that mention "total" / "amount due" / "you pay"
|
|
81
|
+
* 3. Median of all visible prices on the page (low confidence)
|
|
15
82
|
*/
|
|
16
83
|
async function extractTotalPrice(page) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
84
|
+
const candidates = await page.evaluate(() => {
|
|
85
|
+
const visible = (el) => {
|
|
86
|
+
const r = el.getBoundingClientRect();
|
|
87
|
+
if (r.width < 1 || r.height < 1)
|
|
88
|
+
return false;
|
|
89
|
+
const docW = Math.max(document.documentElement.scrollWidth, window.innerWidth);
|
|
90
|
+
const docH = Math.max(document.documentElement.scrollHeight, window.innerHeight);
|
|
91
|
+
const left = r.left + window.scrollX;
|
|
92
|
+
const top = r.top + window.scrollY;
|
|
93
|
+
if (left + r.width <= 0 || top + r.height <= 0 || left >= docW || top >= docH)
|
|
94
|
+
return false;
|
|
95
|
+
let opacity = 1;
|
|
96
|
+
for (let n = el; n; n = n.parentElement) {
|
|
97
|
+
const s = getComputedStyle(n);
|
|
98
|
+
if (s.display === "none" || s.visibility === "hidden")
|
|
99
|
+
return false;
|
|
100
|
+
opacity *= parseFloat(s.opacity || "1");
|
|
101
|
+
}
|
|
102
|
+
if (opacity < 0.1)
|
|
103
|
+
return false;
|
|
104
|
+
return parseFloat(getComputedStyle(el).fontSize || "16") >= 6;
|
|
105
|
+
};
|
|
106
|
+
const textOf = (el) => (el.innerText || "").trim();
|
|
107
|
+
const out = [];
|
|
108
|
+
const known = [
|
|
109
|
+
'[data-checkout-payment-amount]', '.payment-due__price',
|
|
110
|
+
'[data-testid="total-amount"]', '.OrderAmountRow--total .OrderAmountRow-amount',
|
|
111
|
+
'.order-total .amount', '.woocommerce-Price-amount.amount',
|
|
112
|
+
'[data-testid*="total" i]', '[class*="order-total" i]', '[class*="total-price" i]',
|
|
113
|
+
'[class*="grand-total" i]', '[id*="order-total" i]', '[id*="grand-total" i]',
|
|
114
|
+
'span[class*="total"]', 'td[class*="total"]',
|
|
115
|
+
];
|
|
116
|
+
for (const sel of known) {
|
|
117
|
+
for (const el of Array.from(document.querySelectorAll(sel))) {
|
|
118
|
+
if (visible(el))
|
|
119
|
+
out.push({ strategy: 1, text: textOf(el) });
|
|
48
120
|
}
|
|
49
121
|
}
|
|
50
|
-
|
|
51
|
-
|
|
122
|
+
for (const el of Array.from(document.querySelectorAll("body *"))) {
|
|
123
|
+
if (el.children.length >= 5)
|
|
124
|
+
continue;
|
|
125
|
+
const t = textOf(el).toLowerCase();
|
|
126
|
+
if ((t.includes("total") || t.includes("amount due") || t.includes("you pay")) && t.length < 200 && visible(el)) {
|
|
127
|
+
out.push({ strategy: 2, text: textOf(el) });
|
|
128
|
+
}
|
|
52
129
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.filter((el) => {
|
|
59
|
-
if (!(el instanceof HTMLElement))
|
|
60
|
-
return false;
|
|
61
|
-
const style = window.getComputedStyle(el);
|
|
62
|
-
return style.display !== 'none' && style.visibility !== 'hidden';
|
|
63
|
-
})
|
|
64
|
-
.filter(el => {
|
|
65
|
-
const text = el.innerText?.toLowerCase() ?? '';
|
|
66
|
-
return ((text.includes('total') || text.includes('amount due') || text.includes('you pay')) &&
|
|
67
|
-
el.children.length < 5 // narrow to leaf-ish nodes
|
|
68
|
-
);
|
|
69
|
-
})
|
|
70
|
-
.map(el => el.innerText ?? '');
|
|
71
|
-
// Find first valid $ pattern in those elements
|
|
72
|
-
const priceRegex = /\$\s*([\d,]+(?:\.\d{1,2})?)/;
|
|
73
|
-
for (const text of allText) {
|
|
74
|
-
const match = text.match(priceRegex);
|
|
75
|
-
if (match) {
|
|
76
|
-
const value = parseFloat(match[1].replace(/,/g, ''));
|
|
77
|
-
if (value > 0 && value < 10000)
|
|
78
|
-
return value; // sanity bound
|
|
79
|
-
}
|
|
130
|
+
for (const el of Array.from(document.querySelectorAll("body *"))) {
|
|
131
|
+
if (el.children.length === 0 && visible(el)) {
|
|
132
|
+
const t = textOf(el);
|
|
133
|
+
if (t && t.length < 200)
|
|
134
|
+
out.push({ strategy: 3, text: t });
|
|
80
135
|
}
|
|
81
|
-
return null;
|
|
82
|
-
});
|
|
83
|
-
if (amount !== null && amount > 0) {
|
|
84
|
-
console.error(`[PRICE] ✅ Strategy 2 (text heuristic) found: $${amount}`);
|
|
85
|
-
return amount;
|
|
86
136
|
}
|
|
87
|
-
|
|
88
|
-
catch
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const bodyText = document.body.innerText ?? '';
|
|
96
|
-
const values = [];
|
|
97
|
-
let m;
|
|
98
|
-
while ((m = regex.exec(bodyText)) !== null) {
|
|
99
|
-
const v = parseFloat(m[1].replace(/,/g, ''));
|
|
100
|
-
if (v >= 0.5 && v < 10000)
|
|
101
|
-
values.push(v);
|
|
137
|
+
return out.slice(0, 3000);
|
|
138
|
+
}).catch(() => []);
|
|
139
|
+
for (const strategy of [1, 2]) {
|
|
140
|
+
for (const c of candidates.filter((x) => x.strategy === strategy)) {
|
|
141
|
+
const p = parsePriceText(c.text);
|
|
142
|
+
if (p) {
|
|
143
|
+
console.error(`[PRICE] Strategy ${strategy}: ${p.currency} ${p.amount}`);
|
|
144
|
+
return p;
|
|
102
145
|
}
|
|
103
|
-
if (values.length === 0)
|
|
104
|
-
return null;
|
|
105
|
-
// ✅ FIX 10: Use median instead of max — prevents merchant injecting hidden large price
|
|
106
|
-
values.sort((a, b) => a - b);
|
|
107
|
-
return values[Math.floor(values.length / 2)];
|
|
108
|
-
});
|
|
109
|
-
if (amount !== null && amount > 0) {
|
|
110
|
-
console.error(`[PRICE] ⚠️ Strategy 3 (largest $ on page): $${amount}. Low confidence.`);
|
|
111
|
-
return amount;
|
|
112
146
|
}
|
|
113
147
|
}
|
|
114
|
-
|
|
115
|
-
|
|
148
|
+
// Strategy 3: median of every visible price — low confidence, same currency only.
|
|
149
|
+
const all = candidates.filter((x) => x.strategy === 3)
|
|
150
|
+
.map((c) => parsePriceText(c.text))
|
|
151
|
+
.filter((p) => p !== null && p.amount < 10_000);
|
|
152
|
+
if (all.length > 0) {
|
|
153
|
+
const currency = all.some((p) => p.currency === "NON_USD") ? "NON_USD" : "USD";
|
|
154
|
+
const sorted = all.filter((p) => p.currency === currency).sort((a, b) => a.amount - b.amount);
|
|
155
|
+
const pick = sorted[Math.floor(sorted.length / 2)];
|
|
156
|
+
console.error(`[PRICE] ⚠️ Strategy 3 (median of visible prices): ${pick.currency} ${pick.amount}. Low confidence.`);
|
|
157
|
+
return pick;
|
|
116
158
|
}
|
|
117
159
|
console.error('[PRICE] ❌ Could not detect total price on this page.');
|
|
118
160
|
return null;
|
|
119
161
|
}
|
|
120
162
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
163
|
+
* A total row often carries a second line under its label: "Including $0.35 in taxes",
|
|
164
|
+
* "(incl. $1.20 VAT)". That figure is not a total. It is taken out before the prices of a
|
|
165
|
+
* row are counted — otherwise the row reads as two prices (so it is thrown away as unclear),
|
|
166
|
+
* and its label cell, which holds only the tax figure, reads as a "total" of $0.35.
|
|
167
|
+
* Only a note of that exact shape goes: "including shipping $7.00" is left alone.
|
|
168
|
+
*/
|
|
169
|
+
function dropTaxNote(text) {
|
|
170
|
+
return text.replace(/\(?\b(?:including|includes|incl\.?|inc\.?)\s+(?:[A-Z]{3}\s*)?[$€£¥]?\s?\d[\d.,]*\s*(?:[A-Z]{3}\s+)?(?:(?:in|of)\s+)?(?:taxes|tax|vat|gst|hst|duties)\b\)?/gi, " ")
|
|
171
|
+
.replace(/\s+/g, " ").trim();
|
|
172
|
+
}
|
|
173
|
+
/** Every price in a string, in order (same rules as parsePriceText, which returns only the first). */
|
|
174
|
+
function parsePricesText(text) {
|
|
175
|
+
const out = [];
|
|
176
|
+
let rest = text.normalize("NFKC");
|
|
177
|
+
for (let guard = 0; guard < 20; guard++) {
|
|
178
|
+
const p = parsePriceText(rest);
|
|
179
|
+
if (!p)
|
|
180
|
+
break;
|
|
181
|
+
out.push(p);
|
|
182
|
+
const at = rest.indexOf(p.source);
|
|
183
|
+
if (at < 0)
|
|
184
|
+
break;
|
|
185
|
+
rest = rest.slice(at + p.source.length);
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The checkout TOTAL, read strictly — for deal orders, where the number decides whether the
|
|
191
|
+
* card is typed (extractTotalPrice above is a cross-check for auto_pay_checkout and is left
|
|
192
|
+
* as it is). Returns null whenever the total is not unmistakable; the caller then stops.
|
|
126
193
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
194
|
+
* What "unmistakable" means here (review of package 3 — the loose reader took "Subtotal" for
|
|
195
|
+
* "Total", and the struck-through old price for the new one):
|
|
196
|
+
* - a row counts only if its own visible text STARTS with the word total ("Total", "Order
|
|
197
|
+
* total", "Total due" …). "Subtotal", "Total savings", "Total tax", "Total items" do not;
|
|
198
|
+
* - struck-through text (<del>, <s>, line-through) is not read: "$7.00 $6.40" with the
|
|
199
|
+
* first crossed out is 6.40;
|
|
200
|
+
* - the row must hold exactly ONE price, and all such rows must agree on the amount;
|
|
201
|
+
* - no guessing from the other prices on the page (no "median of visible prices").
|
|
202
|
+
* `selector` (hint total_selector, plain CSS) names the element that holds the total and wins
|
|
203
|
+
* over the search when given — for a checkout whose total row cannot be found by its words.
|
|
131
204
|
*/
|
|
132
|
-
async function
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
for (const code of NON_USD) {
|
|
145
|
-
if (new RegExp(`\\b${code}\\b`).test(body))
|
|
146
|
-
return 'NON_USD';
|
|
205
|
+
async function extractCheckoutTotalStrict(page, selector) {
|
|
206
|
+
const found = await page.evaluate((sel) => {
|
|
207
|
+
const visible = (el) => {
|
|
208
|
+
const r = el.getBoundingClientRect();
|
|
209
|
+
if (r.width < 1 || r.height < 1)
|
|
210
|
+
return false;
|
|
211
|
+
let opacity = 1;
|
|
212
|
+
for (let n = el; n; n = n.parentElement) {
|
|
213
|
+
const s = getComputedStyle(n);
|
|
214
|
+
if (s.display === "none" || s.visibility === "hidden")
|
|
215
|
+
return false;
|
|
216
|
+
opacity *= parseFloat(s.opacity || "1");
|
|
147
217
|
}
|
|
148
|
-
|
|
149
|
-
|
|
218
|
+
if (opacity < 0.1)
|
|
219
|
+
return false;
|
|
220
|
+
return parseFloat(getComputedStyle(el).fontSize || "16") >= 6;
|
|
221
|
+
};
|
|
222
|
+
const struck = (el, root) => {
|
|
223
|
+
for (let n = el; n; n = n.parentElement) {
|
|
224
|
+
const tag = n.tagName;
|
|
225
|
+
if (tag === "DEL" || tag === "S" || tag === "STRIKE")
|
|
226
|
+
return true;
|
|
227
|
+
if ((getComputedStyle(n).textDecorationLine || "").includes("line-through"))
|
|
228
|
+
return true;
|
|
229
|
+
if (n === root)
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
};
|
|
234
|
+
/** Visible, not struck-through text of an element, in document order. */
|
|
235
|
+
const liveText = (root) => {
|
|
236
|
+
const parts = [];
|
|
237
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
238
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
239
|
+
const parent = node.parentElement;
|
|
240
|
+
const value = (node.nodeValue || "").trim();
|
|
241
|
+
if (!parent || !value)
|
|
242
|
+
continue;
|
|
243
|
+
if (!visible(parent) || struck(parent, root))
|
|
244
|
+
continue;
|
|
245
|
+
parts.push(value);
|
|
246
|
+
}
|
|
247
|
+
return parts.join(" ").replace(/\s+/g, " ").trim();
|
|
248
|
+
};
|
|
249
|
+
const out = [];
|
|
250
|
+
if (sel) {
|
|
251
|
+
let els = [];
|
|
252
|
+
try {
|
|
253
|
+
els = Array.from(document.querySelectorAll(sel));
|
|
254
|
+
}
|
|
255
|
+
catch { /* not a selector this page can run */ }
|
|
256
|
+
for (const el of els)
|
|
257
|
+
if (visible(el))
|
|
258
|
+
out.push({ kind: "hint", text: liveText(el) });
|
|
259
|
+
}
|
|
260
|
+
// Selectors that name the TOTAL and nothing else. (The fuzzy "*total*" ones are not here:
|
|
261
|
+
// they also match subtotal classes.)
|
|
262
|
+
const known = [
|
|
263
|
+
"[data-checkout-payment-amount]", ".payment-due__price", '[data-testid="total-amount"]',
|
|
264
|
+
".OrderAmountRow--total .OrderAmountRow-amount", ".order-total .amount",
|
|
265
|
+
'[class*="grand-total" i]', '[id*="grand-total" i]',
|
|
266
|
+
];
|
|
267
|
+
for (const k of known)
|
|
268
|
+
for (const el of Array.from(document.querySelectorAll(k)))
|
|
269
|
+
if (visible(el))
|
|
270
|
+
out.push({ kind: "known", text: liveText(el) });
|
|
271
|
+
// Rows whose own text starts with "total": the row, not a box that also holds other rows.
|
|
272
|
+
const START = /^(?:(?:order|grand|estimated|payment|final)\s+)?total\b(?!\s*(?:savings?|saved|discounts?|tax(?:es)?|items?|quantity|qty|weight|shipping|points|refund))|^(?:amount due|total due|you pay|to pay)\b/i;
|
|
273
|
+
for (const el of Array.from(document.querySelectorAll("body *"))) {
|
|
274
|
+
if (el.children.length > 6 || !visible(el))
|
|
275
|
+
continue;
|
|
276
|
+
const t = liveText(el);
|
|
277
|
+
if (t.length > 0 && t.length < 120 && START.test(t))
|
|
278
|
+
out.push({ kind: "row", text: t });
|
|
279
|
+
}
|
|
280
|
+
return out.slice(0, 400);
|
|
281
|
+
}, selector ?? null).catch(() => []);
|
|
282
|
+
for (const kind of ["hint", "known", "row"]) {
|
|
283
|
+
const prices = found.filter((f) => f.kind === kind).map((f) => parsePricesText(dropTaxNote(f.text)));
|
|
284
|
+
// Only texts that hold exactly one price say something unmistakable.
|
|
285
|
+
const single = prices.filter((p) => p.length === 1).map((p) => p[0]);
|
|
286
|
+
if (single.length === 0)
|
|
287
|
+
continue;
|
|
288
|
+
if (single.some((p) => p.currency === "NON_USD"))
|
|
289
|
+
return single.find((p) => p.currency === "NON_USD");
|
|
290
|
+
const amounts = new Set(single.map((p) => p.amount));
|
|
291
|
+
if (amounts.size !== 1) {
|
|
292
|
+
console.error(`[PRICE] strict: ${amounts.size} different totals on the page (${kind}); refusing to pick one.`);
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
return single[0];
|
|
296
|
+
}
|
|
297
|
+
console.error("[PRICE] strict: no unmistakable total on this page.");
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Does the page as a whole show a currency other than US dollars? A "$" alone
|
|
302
|
+
* is also how CAD/AUD/… stores print prices, so a "$" total on a page that
|
|
303
|
+
* mentions another currency is not taken as USD. Visible text only.
|
|
304
|
+
*/
|
|
305
|
+
async function pageMentionsOtherCurrency(page) {
|
|
306
|
+
try {
|
|
307
|
+
const body = await page.evaluate(() => document.body.innerText || "");
|
|
308
|
+
return NON_USD_SYMBOL.test(body) || NON_USD_CODE.test(body);
|
|
150
309
|
}
|
|
151
310
|
catch {
|
|
152
|
-
return
|
|
311
|
+
return false;
|
|
153
312
|
}
|
|
154
313
|
}
|
|
155
|
-
// ─── Utility ─────────────────────────────────────────────────────────────────
|
|
156
|
-
function parseMoneyString(text) {
|
|
157
|
-
// Handles: "$49.99", "USD 49.99", "49,99 $", "49.99 USD"
|
|
158
|
-
const match = text.match(/[\d,]+(?:\.\d{1,2})?/);
|
|
159
|
-
if (!match)
|
|
160
|
-
return null;
|
|
161
|
-
const value = parseFloat(match[0].replace(/,/g, ''));
|
|
162
|
-
if (isNaN(value) || value <= 0)
|
|
163
|
-
return null;
|
|
164
|
-
return value;
|
|
165
|
-
}
|
package/dist/lib/key-store.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Persist the key
|
|
3
|
-
* Returns true on success. Failure is
|
|
2
|
+
* Persist the key with owner-only permissions, noting which config key (if any)
|
|
3
|
+
* was in place when it was written. Returns true on success. Failure is
|
|
4
|
+
* non-fatal (key still lives in memory).
|
|
4
5
|
*/
|
|
5
6
|
export declare function persistPassportKey(key: string): boolean;
|
|
7
|
+
type KeySource = "file" | "env" | "set_api_key" | "none";
|
|
6
8
|
/** Read the current active Passport Key. */
|
|
7
9
|
export declare function getPassportKey(): string;
|
|
10
|
+
/** Where the active key came from, and whether it still has to be rotated. */
|
|
11
|
+
export declare function keyStatus(): {
|
|
12
|
+
source: KeySource;
|
|
13
|
+
rotation_pending: boolean;
|
|
14
|
+
};
|
|
8
15
|
/**
|
|
9
16
|
* Replace the active Passport Key in memory (no restart needed).
|
|
10
17
|
* @param newKey - must start with "zk_live_" or "zk_test_"
|
|
@@ -16,3 +23,19 @@ export declare function setPassportKey(newKey: string): {
|
|
|
16
23
|
};
|
|
17
24
|
/** Check if a key is currently configured. */
|
|
18
25
|
export declare function hasPassportKey(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Make sure a key that came in through the config has been traded for a fresh
|
|
28
|
+
* one. Called at server start and before every authenticated request; does
|
|
29
|
+
* nothing once done. A network failure leaves it pending and the next request
|
|
30
|
+
* tries again (the config key keeps working meanwhile — it is only dead once
|
|
31
|
+
* the server has swapped it).
|
|
32
|
+
*/
|
|
33
|
+
export declare function ensureKeyReady(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* The stored key was just rejected (401). If the config holds a different key
|
|
36
|
+
* that has not been tried, switch to it (and rotate it). Returns true when the
|
|
37
|
+
* caller should retry once. Handles credentials files written before 1.11,
|
|
38
|
+
* which do not record which config key they replaced (audit T14 F3).
|
|
39
|
+
*/
|
|
40
|
+
export declare function recoverFromRejectedKey(): Promise<boolean>;
|
|
41
|
+
export {};
|