kryptheon 0.1.61 → 0.1.62
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/kryptheon-api.js +67 -11
- package/kryptheon-map.js +32 -0
- package/kryptheon-webhook.js +27 -10
- package/package.json +1 -1
package/kryptheon-api.js
CHANGED
|
@@ -1038,10 +1038,26 @@ async function runJobs(root, options) {
|
|
|
1038
1038
|
const send = (s) => s.send(a.method, a.template, a.encoding === 'form' ? { form: a.fullData || {} } : { json: a.fullData || {} });
|
|
1039
1039
|
let first = null;
|
|
1040
1040
|
let by = null;
|
|
1041
|
+
let crash = null;
|
|
1041
1042
|
for (const s of sessions) {
|
|
1042
1043
|
const res = await send(s);
|
|
1043
1044
|
const reply = authz.replyOf(res);
|
|
1044
1045
|
if (reply.kind === 'accepted' || reply.kind === 'redirect') { first = res; by = s; break; }
|
|
1046
|
+
if (res.status >= 500 && !crash) crash = { res, s };
|
|
1047
|
+
}
|
|
1048
|
+
// a job that fails when it runs is not a refusal (round 28: the settlement job answered 500 whenever there was a
|
|
1049
|
+
// paid order to settle - its own SQL named a column the table does not have)
|
|
1050
|
+
if (!first && crash) {
|
|
1051
|
+
out.tried.push({ what, by: crash.s.email, verdict: 'crashed (' + crash.res.status + ')' });
|
|
1052
|
+
const finding = {
|
|
1053
|
+
kind: 'job-crash', where: what, from: a.from.file + ':' + a.from.line, status: 'confirmed', severity: 'LOW',
|
|
1054
|
+
headline: what + ' answered ' + crash.res.status + ' when ' + crash.s.email + ' ran it, with nothing wrong in the request: the job fails when it has work to do.',
|
|
1055
|
+
proof: ['As ' + crash.s.email + ', sent ' + what + '.', 'The answer: ' + crash.res.status + ' ' + authz.clip(crash.res.text, 120)],
|
|
1056
|
+
};
|
|
1057
|
+
finding.fixPrompt = ['Kryptheon ran a job of my app and it failed:', '', finding.headline, '', 'Where: ' + finding.from, '',
|
|
1058
|
+
'Look at the server log for this request: the job\'s query or code fails on the data it reads. Fix that one cause, then run the job twice in a row to see it does its work once. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
|
|
1059
|
+
out.findings.push(finding);
|
|
1060
|
+
continue;
|
|
1045
1061
|
}
|
|
1046
1062
|
if (!first) { out.tried.push({ what, verdict: 'no account was let in' }); continue; }
|
|
1047
1063
|
// The rows a job's answer gives, each marked with its organization (round 27 R27-22: /maintenance/run made every
|
|
@@ -1069,7 +1085,7 @@ async function runJobs(root, options) {
|
|
|
1069
1085
|
const c1 = countsIn(first.json);
|
|
1070
1086
|
const c2 = countsIn(second.json);
|
|
1071
1087
|
const again = Object.keys(c1).filter((k) => c1[k] > 0 && c2[k] === c1[k]);
|
|
1072
|
-
out.tried.push({ what, by: by.email, first: c1, second: c2, verdict: again.length ? 'made the same again' : Object.keys(c1).length ? '
|
|
1088
|
+
out.tried.push({ what, by: by.email, first: c1, second: c2, verdict: again.length ? 'made the same again' : !Object.keys(c1).length ? 'no count to compare' : Object.values(c1).every((v) => !v) && Object.values(c2).every((v) => !v) ? 'nothing to do either time' : 'held' });
|
|
1073
1089
|
out.left.push(what + ' run twice as ' + by.email + (Object.keys(c1).length ? ' (' + JSON.stringify(c1) + ', then ' + JSON.stringify(c2) + ')' : ''));
|
|
1074
1090
|
if (!again.length) continue;
|
|
1075
1091
|
const k = again[0];
|
|
@@ -1102,7 +1118,19 @@ async function runCreates(root, options) {
|
|
|
1102
1118
|
if (!plan.ran) return { ran: false, why: plan.why };
|
|
1103
1119
|
const j = plan.journey;
|
|
1104
1120
|
const lists = new Set(j.actions.filter((x) => x.method === 'GET' && !x.params.length).map((x) => x.template));
|
|
1105
|
-
|
|
1121
|
+
// a create with its parent's id in the address (POST /api/invoices/{orderId}/payment-order, round 28): aimed at a
|
|
1122
|
+
// record of the list its id is named for (orderId: /api/orders), or of the list its address starts with
|
|
1123
|
+
const singular = (w) => w.replace(/ies$/, 'y').replace(/s$/, '');
|
|
1124
|
+
const parentListOf = (a) => {
|
|
1125
|
+
const r = (plan.map.routes || []).find((x) => x.method === a.method && normRoute(x.path || '') === a.template);
|
|
1126
|
+
const m = r && /:(\w+)/.exec(r.path);
|
|
1127
|
+
const stem = m ? singular(m[1].replace(/_?[iI]d$/, '').toLowerCase()) : '';
|
|
1128
|
+
const named = stem && Array.from(lists).find((l) => singular(l.split('/').pop().toLowerCase().replace(/[^a-z]/g, '')) === stem);
|
|
1129
|
+
const head = '/' + a.segs.slice(0, a.params[0].index).join('/');
|
|
1130
|
+
return named || (lists.has(head) ? head : null);
|
|
1131
|
+
};
|
|
1132
|
+
const routes = j.actions.filter((a) => a.method === 'POST' && COSTLY_RECORD.test(a.template) && !JOB_PATH.test(a.template) &&
|
|
1133
|
+
(a.params.length ? a.params.length === 1 && parentListOf(a) : lists.has(a.template)));
|
|
1106
1134
|
if (!routes.length) return { ran: false, why: 'no create route of an order, payment or delivery is also listed at its address' };
|
|
1107
1135
|
let origin;
|
|
1108
1136
|
try {
|
|
@@ -1131,13 +1159,28 @@ async function runCreates(root, options) {
|
|
|
1131
1159
|
for (const f of a.fields || []) if (f.kinds.length) minimal[f.name] = f.sample;
|
|
1132
1160
|
const bodies = [a.fullData || {}].concat(Object.keys(minimal).length && JSON.stringify(minimal) !== JSON.stringify(a.fullData || {}) ? [minimal] : []);
|
|
1133
1161
|
for (const s of sessions) {
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1162
|
+
let at = a.template;
|
|
1163
|
+
let parentId = null;
|
|
1164
|
+
if (a.params.length) {
|
|
1165
|
+
const parents = recordsIn(await s.send('GET', parentListOf(a), {}));
|
|
1166
|
+
const parent = parents && parents.find((x) => x && x.id !== undefined);
|
|
1167
|
+
if (!parent) { t.verdict = 'no record at GET ' + parentListOf(a) + ' to aim it at'; continue; }
|
|
1168
|
+
parentId = parent.id;
|
|
1169
|
+
at = a.template.replace('{id}', encodeURIComponent(String(parent.id)));
|
|
1170
|
+
}
|
|
1171
|
+
// its own list, or - when the record it makes lives elsewhere - every list the account reads
|
|
1172
|
+
const watch = a.params.length ? Array.from(lists) : [a.template];
|
|
1173
|
+
const readAll = async () => {
|
|
1174
|
+
const o = {};
|
|
1175
|
+
for (const l of watch) { const r = recordsIn(await s.send('GET', l, {})); if (r) o[l] = r; }
|
|
1176
|
+
return o;
|
|
1177
|
+
};
|
|
1178
|
+
const beforeAll = await readAll();
|
|
1179
|
+
if (!Object.keys(beforeAll).length) continue;
|
|
1137
1180
|
let done = null;
|
|
1138
1181
|
let answered = [];
|
|
1139
1182
|
for (const body of bodies) {
|
|
1140
|
-
const send = () => s.send(a.method,
|
|
1183
|
+
const send = () => s.send(a.method, at, a.encoding === 'form' ? { form: body } : { json: body });
|
|
1141
1184
|
const [r1, r2] = await Promise.all([send(), send()]);
|
|
1142
1185
|
answered = [r1.status, r2.status];
|
|
1143
1186
|
if (!taken(r1) && !taken(r2)) continue;
|
|
@@ -1146,21 +1189,32 @@ async function runCreates(root, options) {
|
|
|
1146
1189
|
}
|
|
1147
1190
|
// refused both times: the next account may be let in
|
|
1148
1191
|
if (!done) { t.verdict = 'refused (' + answered.join(' and ') + ')'; continue; }
|
|
1149
|
-
|
|
1150
|
-
const
|
|
1192
|
+
// the list that gained the most records is where the creates went
|
|
1193
|
+
const afterAll = await readAll();
|
|
1194
|
+
let made = [];
|
|
1195
|
+
let madeIn = watch[0];
|
|
1196
|
+
for (const l of Object.keys(afterAll)) {
|
|
1197
|
+
const had = new Set((beforeAll[l] || []).map((x) => x && JSON.stringify(x.id)));
|
|
1198
|
+
const fresh = afterAll[l].filter((x) => x && x.id !== undefined && !had.has(JSON.stringify(x.id)));
|
|
1199
|
+
if (fresh.length > made.length) { made = fresh; madeIn = l; }
|
|
1200
|
+
}
|
|
1201
|
+
const before = beforeAll[madeIn] || [];
|
|
1202
|
+
const after = afterAll[madeIn] || [];
|
|
1151
1203
|
t.by = s.email;
|
|
1152
1204
|
t.answers = [done.r1.status, done.r2.status];
|
|
1153
1205
|
t.made = made.map((x) => x.id);
|
|
1154
1206
|
if (made.length) out.left.push(what + ' sent twice at once as ' + s.email + ': record(s) ' + made.map((x) => x.id).join(', ') + ' (Kryptheon never deletes)');
|
|
1155
1207
|
// the same fields the body set, on both records
|
|
1156
|
-
const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined))
|
|
1208
|
+
const keys = Object.keys(done.body).filter((k) => made.length && made.every((x) => valueIn(x, k) !== undefined))
|
|
1209
|
+
// and the field that points at the parent the address named (order_id = 1 on both payment orders)
|
|
1210
|
+
.concat(parentId === null || !made.length ? [] : Object.keys(made[0]).filter((k) => k !== 'id' && String(made[0][k]) === String(parentId)));
|
|
1157
1211
|
const alike = made.length >= 2 && keys.every((k) => JSON.stringify(valueIn(made[0], k)) === JSON.stringify(valueIn(made[1], k)));
|
|
1158
1212
|
t.verdict = alike ? 'made twice' : made.length === 1 ? 'held: one record' : made.length ? 'made ' + made.length + ' different records' : 'nothing new in the list';
|
|
1159
1213
|
if (t.verdict === 'made twice') {
|
|
1160
1214
|
const finding = {
|
|
1161
1215
|
kind: 'create-twice', where: what, from: a.from.file + ':' + a.from.line, status: 'verification required', severity: 'MEDIUM',
|
|
1162
1216
|
headline: 'Sent twice at once, as a double click sends it, ' + what + ' made ' + made.length + ' records with the same ' + (keys.slice(0, 3).join(', ') || 'fields') + ' (' + made.map((x) => x.id).join(', ') + '): nothing stops a repeated submit.',
|
|
1163
|
-
proof: ['As ' + s.email + ', sent ' +
|
|
1217
|
+
proof: ['As ' + s.email + ', sent ' + a.method + ' ' + at + ' twice at the same moment with the same body: ' + authz.clip(JSON.stringify(done.body), 120), 'Answered ' + done.r1.status + ' and ' + done.r2.status + '.', 'GET ' + madeIn + ' before: ' + before.length + ' records; after: ' + after.length + ', new ' + made.map((x) => x.id).join(', ') + '.'],
|
|
1164
1218
|
};
|
|
1165
1219
|
finding.fixPrompt = ['Kryptheon sent the same create to my running app twice at once, as a double click does:', '', finding.headline, '', 'Where: ' + finding.from, '',
|
|
1166
1220
|
'First check this is real: is a second, identical record ever wanted here? If not, accept an idempotency key from the client (or a fingerprint of the request) and return the first record for a repeat, with a unique index behind it; and disable the submit button while the request is pending. Change nothing else.', '', 'Afterwards I will run: npx kryptheon api'].join(String.fromCharCode(10));
|
|
@@ -1582,11 +1636,13 @@ async function runApi(root, options) {
|
|
|
1582
1636
|
if (!m.ran) return { ran: false, why: m.why };
|
|
1583
1637
|
const toCheck = opts.toCheck || map.toCheck(m);
|
|
1584
1638
|
const shared = Object.assign({}, opts, { map: m, say: say });
|
|
1639
|
+
// jobs first, on the app as it was seeded: the tries after them cancel and refund the records a job works on
|
|
1640
|
+
// (round 28: the settlement job found no paid order left and made nothing either time)
|
|
1641
|
+
const jobsRun = await runJobs(root, shared);
|
|
1585
1642
|
const wall = await runWall(root, shared);
|
|
1586
1643
|
const forged = await runForged(root, shared);
|
|
1587
1644
|
const webhooks = await webhook.run(root, shared);
|
|
1588
1645
|
const exportsRun = await csv.run(root, shared);
|
|
1589
|
-
const jobsRun = await runJobs(root, shared);
|
|
1590
1646
|
const keptRun = await runKept(root, shared);
|
|
1591
1647
|
const createsRun = await runCreates(root, shared);
|
|
1592
1648
|
const stepsRun = await runSteps(root, Object.assign({}, shared, { toCheck: toCheck }));
|
package/kryptheon-map.js
CHANGED
|
@@ -209,6 +209,16 @@ function makeContext(root, parser) {
|
|
|
209
209
|
}
|
|
210
210
|
|
|
211
211
|
/** ['req', 'session', 'user'] for req.session.user; null when any part is computed and not a fixed text. */
|
|
212
|
+
/** The function a name of this scope is bound to (function name() {} or const name = () => {}), or null. */
|
|
213
|
+
function localFunction(scope, name) {
|
|
214
|
+
const b = scope.getBinding(name);
|
|
215
|
+
const n = b && b.path && b.path.node;
|
|
216
|
+
if (!n) return null;
|
|
217
|
+
if (n.type === 'FunctionDeclaration') return n;
|
|
218
|
+
if (n.type === 'VariableDeclarator' && n.init && /Function/.test(n.init.type)) return n.init;
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
|
|
212
222
|
function memberChain(node) {
|
|
213
223
|
const out = [];
|
|
214
224
|
let n = node;
|
|
@@ -216,6 +226,8 @@ function memberChain(node) {
|
|
|
216
226
|
if (n.type === 'MemberExpression' || n.type === 'OptionalMemberExpression') {
|
|
217
227
|
if (!n.computed && n.property.type === 'Identifier') out.unshift(n.property.name);
|
|
218
228
|
else if (n.computed && n.property.type === 'StringLiteral') out.unshift(n.property.value);
|
|
229
|
+
// rows[0]: one item of the list, read by the same names (round 28: p.rows[0].id is the id of p's row)
|
|
230
|
+
else if (n.computed && n.property.type === 'NumericLiteral') { /* skipped */ }
|
|
219
231
|
else return null;
|
|
220
232
|
n = n.object;
|
|
221
233
|
} else if (n.type === 'Identifier') {
|
|
@@ -1551,6 +1563,17 @@ function sourceReader(ctx, fnRef, env) {
|
|
|
1551
1563
|
}
|
|
1552
1564
|
case 'CallExpression': case 'OptionalCallExpression': {
|
|
1553
1565
|
const callee = memberChain(n.callee) || [];
|
|
1566
|
+
// hashToken(token): a one-argument helper of the file that runs no query hands its argument on - the hash of the
|
|
1567
|
+
// caller's own session cookie is still the caller's (round 28 logout)
|
|
1568
|
+
if (n.callee.type === 'Identifier' && n.arguments.length === 1) {
|
|
1569
|
+
const helper = localFunction(fn.scope, n.callee.name);
|
|
1570
|
+
const text = helper ? mod.source.slice(helper.start, helper.end) : '';
|
|
1571
|
+
// only when the argument tells something: scopedOrg(req) is decided by what it returns, below
|
|
1572
|
+
// a database call, not a hash's .update(token)
|
|
1573
|
+
const readsDb = /\bquery\s*\(|\.from\s*\(|\bfind\w*\s*\(|\bfetch\s*\(|\bsql`|\$queryRaw/.test(text);
|
|
1574
|
+
const passed = helper && helper.params.length === 1 && !readsDb ? of(n.arguments[0], depth + 1) : 'unknown';
|
|
1575
|
+
if (passed !== 'unknown') return passed;
|
|
1576
|
+
}
|
|
1554
1577
|
// a Fetch-style request: await req.json(), req.formData(), req.headers.get(...)
|
|
1555
1578
|
if (reqName && callee[0] === reqName && /^(json|formData|text|arrayBuffer|blob)$/.test(callee[callee.length - 1] || '')) return 'client';
|
|
1556
1579
|
// body.get('x'), url.searchParams.get('x'): a method of something the request sent
|
|
@@ -1601,6 +1624,11 @@ function sourceReader(ctx, fnRef, env) {
|
|
|
1601
1624
|
const awaited = init.type === 'AwaitExpression' ? init.argument : init;
|
|
1602
1625
|
if (s === 'unknown' && isSessionLookup(awaited)) s = 'identity';
|
|
1603
1626
|
if (s === 'unknown' && /query|rows|select|find|maybeSingle|single\(/i.test(mod.source.slice(init.start, init.end))) s = 'record';
|
|
1627
|
+
// const ev = await getOrgEvent(id, org): a helper of the file that runs a query returns a row (round 28)
|
|
1628
|
+
if (s === 'unknown' && awaited && awaited.type === 'CallExpression' && awaited.callee.type === 'Identifier') {
|
|
1629
|
+
const fnNode = localFunction(q.scope, awaited.callee.name);
|
|
1630
|
+
if (fnNode && /\b(query|select|find\w*)\s*\(/i.test(mod.source.slice(fnNode.start, fnNode.end))) s = 'record';
|
|
1631
|
+
}
|
|
1604
1632
|
for (const name of patternNames(id)) if (!locals.has(name)) locals.set(name, s);
|
|
1605
1633
|
},
|
|
1606
1634
|
// for (const s of pending): each item is a row of the list it walks
|
|
@@ -1732,6 +1760,10 @@ function readDb(ctx, fnRef, schema, opts = {}) {
|
|
|
1732
1760
|
});
|
|
1733
1761
|
const inner = readDb(ctx, r, schema, { depth: depth + 1, paramSources, reqName: null });
|
|
1734
1762
|
for (const op of inner) out.push(Object.assign({}, op, { via: callee.node.name + '() at ' + mod.rel + ':' + line }));
|
|
1763
|
+
// const ev = await getOrgEvent(id, org): the row the helper returns was found the way its query found it
|
|
1764
|
+
const holder = q.findParent((x) => x.isVariableDeclarator() || x.isFunction());
|
|
1765
|
+
const found = inner.find((x) => x.op === 'SELECT' && !x.joinedTo);
|
|
1766
|
+
if (found && holder && holder.isVariableDeclarator() && t.isIdentifier(holder.node.id)) recordScopes.set(holder.node.id.name, { table: found.table, status: found.scoping.status, line, file: mod.rel });
|
|
1735
1767
|
}
|
|
1736
1768
|
};
|
|
1737
1769
|
fnRef.path.traverse({
|
package/kryptheon-webhook.js
CHANGED
|
@@ -164,6 +164,9 @@ function readSigning(parser, root, route, fields) {
|
|
|
164
164
|
const kindsOf = () => Array.from(literals).concat(Array.from(listed).filter((t) => !literals.has(t)));
|
|
165
165
|
const destructured = [];
|
|
166
166
|
const helpers = [];
|
|
167
|
+
// the helper call being read now: a call found inside it is that call's child, so a secret can be followed back
|
|
168
|
+
// through each of them to the handler (round 28: hmac(secret, raw) -> signBody(secret, body) -> createHmac)
|
|
169
|
+
let reading = null;
|
|
167
170
|
const readFrom = (fnPath) => fnPath.traverse({
|
|
168
171
|
CallExpression(p) {
|
|
169
172
|
const n = p.node;
|
|
@@ -183,7 +186,7 @@ function readSigning(parser, root, route, fields) {
|
|
|
183
186
|
if (n.callee && n.callee.type === 'Identifier') {
|
|
184
187
|
// handed the body, or the body's bytes: sign(raw, key) where raw = JSON.stringify(req.body || {})
|
|
185
188
|
const at = n.arguments.findIndex((a) => isBody(a) || isPayload(resolve(a, 3, p)));
|
|
186
|
-
if (at >= 0) helpers.push({ name: n.callee.name, at, scope: p.scope, callPath: p });
|
|
189
|
+
if (at >= 0) helpers.push({ name: n.callee.name, at, scope: p.scope, callPath: p, parent: reading });
|
|
187
190
|
}
|
|
188
191
|
// ['payment.captured', 'refund.processed'].includes(payload.type), or TYPES.includes(event.type): every kind in
|
|
189
192
|
// the list (round 27)
|
|
@@ -239,12 +242,15 @@ function readSigning(parser, root, route, fields) {
|
|
|
239
242
|
if (!param || param.type !== 'Identifier') continue;
|
|
240
243
|
bodyNames.add(param.name);
|
|
241
244
|
scopePath = fnPath;
|
|
245
|
+
call.fnPath = fnPath;
|
|
246
|
+
reading = call;
|
|
242
247
|
readFrom(fnPath);
|
|
248
|
+
reading = null;
|
|
243
249
|
if (hmac) {
|
|
244
250
|
via = call.name;
|
|
245
251
|
// the helper's own parameters are this call's arguments: hmac(secret, body) called as
|
|
246
252
|
// hmac(process.env.PAYMENT_WEBHOOK_SECRET || '', raw) signs with that secret (round 27)
|
|
247
|
-
hmac.via =
|
|
253
|
+
hmac.via = call;
|
|
248
254
|
call.read = true;
|
|
249
255
|
break;
|
|
250
256
|
}
|
|
@@ -282,10 +288,12 @@ function readSigning(parser, root, route, fields) {
|
|
|
282
288
|
if (!isPayload(pay)) return { ok: false, why: 'what is signed is more than the body (a timestamp or a prefix in front, say), which this check does not make' };
|
|
283
289
|
// the secret
|
|
284
290
|
let key = resolve(hmac.key, 3, hmac.keyScope);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const
|
|
288
|
-
|
|
291
|
+
// a parameter of the helper is the argument of its call, and that may be a parameter of the helper around it
|
|
292
|
+
for (let step = hmac.via; step && key && key.type === 'Identifier'; step = step.parent) {
|
|
293
|
+
const at = step.fnPath.node.params.findIndex((q) => q.type === 'Identifier' && q.name === key.name);
|
|
294
|
+
const arg = at >= 0 ? step.callPath.node.arguments[at] : null;
|
|
295
|
+
if (!arg) break;
|
|
296
|
+
key = resolve(arg, 3, step.callPath);
|
|
289
297
|
}
|
|
290
298
|
let secret = null;
|
|
291
299
|
const envName = (n) => { const c = n && chainOf(n); return c && c[0] === 'process' && c[1] === 'env' && c[2] ? c[2] : null; };
|
|
@@ -526,7 +534,13 @@ async function run(root, options) {
|
|
|
526
534
|
// 1. a correctly signed event first: if the app refuses its own, nothing after it means anything.
|
|
527
535
|
// Open (no signature checked at all): the first event goes with none, and is itself the finding if it is taken.
|
|
528
536
|
const S = open ? 'none' : 'signed';
|
|
529
|
-
|
|
537
|
+
// With an amount to try and at most one record left that a capture could still land on, the control is a kind that
|
|
538
|
+
// neither captures nor refunds, so the wrong amount has somewhere to go (round 28: one payment per organization,
|
|
539
|
+
// and a captured control left none). With more records the order stays (round 26 refuses payment.created).
|
|
540
|
+
const CAPTURE = /captur|charge|paid|success/i;
|
|
541
|
+
const capturable = records.filter((r) => r.id !== null && (!statusKey || !(CAPTURE.test(String(r[statusKey])) || REFUNDED.test(String(r[statusKey])))));
|
|
542
|
+
const plain = shape.amount && amountKey && capturable.length <= 1 ? types.find((t) => !REFUNDED.test(t) && !CAPTURE.test(t)) : null;
|
|
543
|
+
const ctypes = types.length ? (plain ? [plain].concat(types.filter((t) => t !== plain)) : types) : [NEUTRAL];
|
|
530
544
|
say('Sending ' + (open ? 'an unsigned' : 'a correctly signed') + ' ' + ctypes[0] + ' to ' + name + ' first...');
|
|
531
545
|
const first = nextRecord(unpaidFor(ctypes[0])) || nextRecord();
|
|
532
546
|
if (!first) { hook.notTried = 'no record left to aim an event at'; continue; }
|
|
@@ -534,9 +548,13 @@ async function run(root, options) {
|
|
|
534
548
|
// a refusal can be the record's state, not the signature (round 26: "captured" sent to a failed payment is 409):
|
|
535
549
|
// the same type at the other records, then the next type, three sends at most - a refusal changes nothing
|
|
536
550
|
const refusals = [control];
|
|
537
|
-
|
|
551
|
+
// a plain control refused (round 26 refuses payment.created for the record's state): the usual order then, with the
|
|
552
|
+
// usual three sends besides it
|
|
553
|
+
const order = plain && !control.took ? types : ctypes;
|
|
554
|
+
const budget = plain && !control.took ? 4 : 3;
|
|
555
|
+
for (const type of order) {
|
|
538
556
|
for (const rec of records) {
|
|
539
|
-
if (control.took || refusals.length >=
|
|
557
|
+
if (control.took || refusals.length >= budget) break;
|
|
540
558
|
if (refusals.some((t) => t.type === type && t.record === rec.id) || (statusKey && REFUNDED.test(String(rec[statusKey])))) continue;
|
|
541
559
|
control = await attempt(S, type, rec, undefined, open ? 'unsigned' : 'signed');
|
|
542
560
|
refusals.push(control);
|
|
@@ -569,7 +587,6 @@ async function run(root, options) {
|
|
|
569
587
|
// (round 26: the next record was a failed payment, which refuses any capture for its state, not its amount)
|
|
570
588
|
// a capture-kind event, even when the control was another kind (round 27: payment.created took the control); at the
|
|
571
589
|
// control's record first, then the others, until one takes a capture at all - a refusal for its state says nothing
|
|
572
|
-
const CAPTURE = /captur|charge|paid|success/i;
|
|
573
590
|
const captureType = shape.amount && amountKey ? (!REFUNDED.test(control.type) && CAPTURE.test(control.type) ? control.type : signing.types.find((t) => !REFUNDED.test(t) && CAPTURE.test(t))) : null;
|
|
574
591
|
const payControl = !!captureType;
|
|
575
592
|
if (captureType) {
|