fortrip 5.3.0 → 6.0.0-rc.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 +34 -20
- package/THIRD-PARTY-NOTICES +0 -26
- package/dist/{chunk-TEXEQ57H.js → chunk-OUCQQEEU.js} +1591 -966
- package/dist/cli.js +125 -756
- package/dist/{mcp-bridge-M763WDKU.js → mcp-bridge-ANLTZ4MB.js} +24 -19
- package/package.json +2 -3
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
API,
|
|
4
4
|
COMMANDS,
|
|
5
5
|
COMMAND_GROUPS,
|
|
6
|
-
CURRENCIES,
|
|
7
6
|
CliError,
|
|
8
7
|
EXIT,
|
|
9
8
|
GLOBAL_FLAGS,
|
|
@@ -17,18 +16,22 @@ import {
|
|
|
17
16
|
commandInvocation,
|
|
18
17
|
fieldLines,
|
|
19
18
|
formatError,
|
|
19
|
+
localRequestId,
|
|
20
20
|
login,
|
|
21
21
|
logout,
|
|
22
|
+
misplacedKeyHint,
|
|
23
|
+
misplacedKeys,
|
|
22
24
|
notices,
|
|
23
|
-
|
|
25
|
+
orderCancel,
|
|
24
26
|
renderToolResult,
|
|
25
27
|
resolveApiUrl,
|
|
26
28
|
resolveMcpUrl,
|
|
27
29
|
routePath,
|
|
30
|
+
unknownFieldsMessage,
|
|
28
31
|
unwrap,
|
|
29
32
|
usageError,
|
|
30
33
|
whoami
|
|
31
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-OUCQQEEU.js";
|
|
32
35
|
|
|
33
36
|
// src/commands.ts
|
|
34
37
|
import { readFileSync } from "fs";
|
|
@@ -152,9 +155,9 @@ ${optionList(GLOBAL_FLAGS)}
|
|
|
152
155
|
Output is compact text on stdout; --json prints it as one line of JSON.
|
|
153
156
|
Errors go to stderr, as text or, with --json, as JSON.
|
|
154
157
|
Exit codes: 0 ok, 1 error (retry once when the error says it is retryable),
|
|
155
|
-
2 invalid arguments,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
+
2 invalid arguments (nothing is sent), 3 not signed in or sign-in not
|
|
159
|
+
accepted, 4 rate limited, 5 this CLI is too old for Fortrip (the error says
|
|
160
|
+
how to update it).
|
|
158
161
|
|
|
159
162
|
Environment:
|
|
160
163
|
${ENVIRONMENT_LINES}
|
|
@@ -167,6 +170,16 @@ var FROM_ENV = { currency: "FORTRIP_CURRENCY" };
|
|
|
167
170
|
var ONE_ITEM_LISTS = /* @__PURE__ */ new Set(["items"]);
|
|
168
171
|
var GUEST_FIELD = "items.hotel.rooms.guests";
|
|
169
172
|
var ROOMS_FIELD = "items.hotel.rooms";
|
|
173
|
+
var REMOVED_FLAGS = {
|
|
174
|
+
"fortrip orders create": {
|
|
175
|
+
"--first-name": "give the contact's full name as --name",
|
|
176
|
+
"--last-name": "give the contact's full name as --name"
|
|
177
|
+
},
|
|
178
|
+
"fortrip orders pay": {
|
|
179
|
+
"--method": "an order is paid here with --token and --total only; any other way, the user pays on the order's payment link"
|
|
180
|
+
},
|
|
181
|
+
"fortrip hotels get": { "--rooms": "use --room-types" }
|
|
182
|
+
};
|
|
170
183
|
function parseOptions(spec, argv) {
|
|
171
184
|
const options = {};
|
|
172
185
|
for (const option of [...spec.flags, ...spec.options ?? []]) {
|
|
@@ -176,6 +189,11 @@ function parseOptions(spec, argv) {
|
|
|
176
189
|
..."multiple" in option && option.multiple ? { multiple: true } : {}
|
|
177
190
|
};
|
|
178
191
|
}
|
|
192
|
+
for (const arg of argv) {
|
|
193
|
+
if (arg === "--") break;
|
|
194
|
+
const now = REMOVED_FLAGS[commandInvocation(spec)]?.[arg.split("=")[0]];
|
|
195
|
+
if (now) throw usageError(`${arg.split("=")[0]} is no flag of ${commandInvocation(spec)} since 6.0: ${now}`);
|
|
196
|
+
}
|
|
179
197
|
let parsed;
|
|
180
198
|
try {
|
|
181
199
|
parsed = parseArgs({
|
|
@@ -222,7 +240,8 @@ function parseCommand(spec, argv, env) {
|
|
|
222
240
|
fromEnv.set(`/${field}`, name);
|
|
223
241
|
}
|
|
224
242
|
}
|
|
225
|
-
|
|
243
|
+
const given = new Set(spec.flags.filter((flag) => values[flag.flag] !== void 0).map((flag) => flag.flag));
|
|
244
|
+
check(spec, input, route, { given, body: values.body !== void 0, fromEnv });
|
|
226
245
|
const path = routePath(route, input);
|
|
227
246
|
if (route.method === "GET") return { method: "GET", path };
|
|
228
247
|
const body = Object.fromEntries(Object.entries(input).filter(([field]) => !route.pathFields.includes(field)));
|
|
@@ -240,7 +259,7 @@ function requestCurrency(spec, values, input, env) {
|
|
|
240
259
|
}
|
|
241
260
|
return env[FROM_ENV.currency]?.trim().toUpperCase() || "USD";
|
|
242
261
|
}
|
|
243
|
-
function
|
|
262
|
+
function cancelRequest(spec, argv, env) {
|
|
244
263
|
const flag = spec.tool === "cancel_order" ? spec.flags.find((f) => f.field === "maximumPenalty") : void 0;
|
|
245
264
|
if (!flag) return null;
|
|
246
265
|
const { values, positionals: positionals2 } = parseOptions(spec, argv);
|
|
@@ -252,29 +271,68 @@ function cancelPreview(spec, argv, env) {
|
|
|
252
271
|
);
|
|
253
272
|
}
|
|
254
273
|
if (positionals2.length !== 1) throw usageError(`Usage: ${usageLine(spec)}`);
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
274
|
+
const orderId = positionals2[0];
|
|
275
|
+
if (!close && !penalty) {
|
|
276
|
+
check({ ...spec, tool: "get_order" }, { orderId }, HTTP_ROUTES.get_order, { given: /* @__PURE__ */ new Set() });
|
|
277
|
+
const read = `${commandInvocation({ group: "orders", name: "get" })} ${orderId}`;
|
|
278
|
+
throw usageError(
|
|
279
|
+
`${commandInvocation(spec)} needs exactly one of --${flag.flag} ${flag.placeholder}, to cancel a paid order for the penalty the user agreed to, and --close, to close an unpaid one. Nothing was sent. \`${read}\` shows which applies, on the order's Cancel line, with the command to run.`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const request = parseCommand(spec, argv, env);
|
|
283
|
+
return { method: "GET", path: API.order(orderId), orderId, close, request };
|
|
260
284
|
}
|
|
261
|
-
function
|
|
285
|
+
function listed(names2) {
|
|
286
|
+
return names2.length > 1 ? `${names2.slice(0, -1).join(", ")} and ${names2.at(-1)}` : names2.join("");
|
|
287
|
+
}
|
|
288
|
+
function valueAt(input, path) {
|
|
289
|
+
let at = input;
|
|
290
|
+
for (const key of path) {
|
|
291
|
+
if (typeof at !== "object" || at === null) return void 0;
|
|
292
|
+
at = at[key];
|
|
293
|
+
}
|
|
294
|
+
return at;
|
|
295
|
+
}
|
|
296
|
+
function check(spec, input, route, source = { given: new Set(spec.flags.map((flag) => flag.flag)) }) {
|
|
262
297
|
const schema = TOOLS.find((tool) => tool.name === spec.tool)?.input;
|
|
263
298
|
const result = schema?.safeParse(input);
|
|
264
|
-
if (!result || result.success) return;
|
|
299
|
+
if (!schema || !result || result.success) return;
|
|
265
300
|
const pointer = (path) => {
|
|
266
301
|
const keys = path.map((key) => String(key).replaceAll("~", "~0").replaceAll("/", "~1"));
|
|
267
302
|
const where = keys[0] === void 0 ? [] : route.pathFields.includes(keys[0]) ? ["path"] : route.method === "GET" ? ["query"] : [];
|
|
268
303
|
return [...where, ...keys].map((key) => `/${key}`).join("");
|
|
269
304
|
};
|
|
270
|
-
const
|
|
271
|
-
(
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
305
|
+
const hints = new Map(
|
|
306
|
+
misplacedKeys(schema, result.error.issues, input).map((key) => [pointer(key.path), misplacedKeyHint(key)])
|
|
307
|
+
);
|
|
308
|
+
const typed = (path) => {
|
|
309
|
+
const field = path.filter((key) => typeof key !== "number").join(".");
|
|
310
|
+
const positional = spec.positionals?.find((candidate) => candidate.field === field);
|
|
311
|
+
if (positional) return { flag: `<${positional.name}>`, missing: false };
|
|
312
|
+
const given = spec.flags.filter((flag) => source.given.has(flag.flag) && (field === flag.field || field.startsWith(`${flag.field}.`))).sort((a, b) => b.field.length - a.field.length)[0];
|
|
313
|
+
if (given) return { flag: `--${given.flag}`, missing: false };
|
|
314
|
+
if (source.body || valueAt(input, path) !== void 0) return void 0;
|
|
315
|
+
const gives = spec.flags.filter(
|
|
316
|
+
(flag) => flag.field === field || flag.required && flag.field.startsWith(`${field}.`)
|
|
317
|
+
);
|
|
318
|
+
return gives.length > 0 ? { flag: listed(gives.map((flag) => `--${flag.flag}`)), missing: true } : void 0;
|
|
319
|
+
};
|
|
320
|
+
const issues = result.error.issues.flatMap((issue) => {
|
|
321
|
+
if (issue.code === "unrecognized_keys") {
|
|
322
|
+
return issue.keys.map((key) => {
|
|
323
|
+
const at2 = pointer([...issue.path, key]);
|
|
324
|
+
const hint = hints.get(at2);
|
|
325
|
+
const message2 = unknownFieldsMessage([key]);
|
|
326
|
+
return { pointer: at2, message: hint ? `${message2} (${hint})` : message2 };
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
const at = pointer(issue.path);
|
|
330
|
+
const variable = source.fromEnv?.get(at);
|
|
331
|
+
if (variable) return [{ pointer: at, message: `${variable}: ${issue.message}` }];
|
|
332
|
+
const from = typed(issue.path);
|
|
333
|
+
if (!from) return [{ pointer: at, message: issue.message }];
|
|
334
|
+
const message = from.missing ? `${from.flag} ${from.flag.includes(" ") ? "are" : "is"} required` : `${from.flag}: ${issue.message}`;
|
|
335
|
+
return [{ pointer: at, message }];
|
|
278
336
|
});
|
|
279
337
|
throw new CliError(
|
|
280
338
|
`Not sent: ${commandInvocation(spec)} was given an invalid request.`,
|
|
@@ -448,684 +506,6 @@ function withGuests(fromBody, guests) {
|
|
|
448
506
|
return rooms;
|
|
449
507
|
}
|
|
450
508
|
|
|
451
|
-
// ../../node_modules/.pnpm/uqr@0.1.3/node_modules/uqr/dist/index.mjs
|
|
452
|
-
var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
|
|
453
|
-
QrCodeDataType2[QrCodeDataType2["Border"] = -1] = "Border";
|
|
454
|
-
QrCodeDataType2[QrCodeDataType2["Data"] = 0] = "Data";
|
|
455
|
-
QrCodeDataType2[QrCodeDataType2["Function"] = 1] = "Function";
|
|
456
|
-
QrCodeDataType2[QrCodeDataType2["Position"] = 2] = "Position";
|
|
457
|
-
QrCodeDataType2[QrCodeDataType2["Timing"] = 3] = "Timing";
|
|
458
|
-
QrCodeDataType2[QrCodeDataType2["Alignment"] = 4] = "Alignment";
|
|
459
|
-
return QrCodeDataType2;
|
|
460
|
-
})(QrCodeDataType || {});
|
|
461
|
-
var LOW = [0, 1];
|
|
462
|
-
var MEDIUM = [1, 0];
|
|
463
|
-
var QUARTILE = [2, 3];
|
|
464
|
-
var HIGH = [3, 2];
|
|
465
|
-
var EccMap = {
|
|
466
|
-
L: LOW,
|
|
467
|
-
M: MEDIUM,
|
|
468
|
-
Q: QUARTILE,
|
|
469
|
-
H: HIGH
|
|
470
|
-
};
|
|
471
|
-
var NUMERIC_REGEX = /^\d*$/;
|
|
472
|
-
var ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+./:-]*$/;
|
|
473
|
-
var ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
|
474
|
-
var MIN_VERSION = 1;
|
|
475
|
-
var MAX_VERSION = 40;
|
|
476
|
-
var PENALTY_N1 = 3;
|
|
477
|
-
var PENALTY_N2 = 3;
|
|
478
|
-
var PENALTY_N3 = 40;
|
|
479
|
-
var PENALTY_N4 = 10;
|
|
480
|
-
var ECC_CODEWORDS_PER_BLOCK = [
|
|
481
|
-
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
482
|
-
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
|
483
|
-
[-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
|
484
|
-
// Low
|
|
485
|
-
[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
|
|
486
|
-
// Medium
|
|
487
|
-
[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
|
488
|
-
// Quartile
|
|
489
|
-
[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
|
|
490
|
-
// High
|
|
491
|
-
];
|
|
492
|
-
var NUM_ERROR_CORRECTION_BLOCKS = [
|
|
493
|
-
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
494
|
-
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
|
495
|
-
[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
|
|
496
|
-
// Low
|
|
497
|
-
[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
|
|
498
|
-
// Medium
|
|
499
|
-
[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
|
|
500
|
-
// Quartile
|
|
501
|
-
[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81]
|
|
502
|
-
// High
|
|
503
|
-
];
|
|
504
|
-
var QrCode = class {
|
|
505
|
-
/* -- Constructor (low level) and fields -- */
|
|
506
|
-
// Creates a new QR Code with the given version number,
|
|
507
|
-
// error correction level, data codeword bytes, and mask number.
|
|
508
|
-
// This is a low-level API that most users should not use directly.
|
|
509
|
-
// A mid-level API is the encodeSegments() function.
|
|
510
|
-
constructor(version, ecc, dataCodewords, msk) {
|
|
511
|
-
this.version = version;
|
|
512
|
-
this.ecc = ecc;
|
|
513
|
-
if (version < MIN_VERSION || version > MAX_VERSION)
|
|
514
|
-
throw new RangeError("Version value out of range");
|
|
515
|
-
if (msk < -1 || msk > 7)
|
|
516
|
-
throw new RangeError("Mask value out of range");
|
|
517
|
-
this.size = version * 4 + 17;
|
|
518
|
-
const row = Array.from({ length: this.size }).fill(false);
|
|
519
|
-
for (let i = 0; i < this.size; i++) {
|
|
520
|
-
this.modules.push(row.slice());
|
|
521
|
-
this.types.push(row.map(() => 0));
|
|
522
|
-
}
|
|
523
|
-
this.drawFunctionPatterns();
|
|
524
|
-
const allCodewords = this.addEccAndInterleave(dataCodewords);
|
|
525
|
-
this.drawCodewords(allCodewords);
|
|
526
|
-
if (msk === -1) {
|
|
527
|
-
let minPenalty = 1e9;
|
|
528
|
-
for (let i = 0; i < 8; i++) {
|
|
529
|
-
this.applyMask(i);
|
|
530
|
-
this.drawFormatBits(i);
|
|
531
|
-
const penalty = this.getPenaltyScore();
|
|
532
|
-
if (penalty < minPenalty) {
|
|
533
|
-
msk = i;
|
|
534
|
-
minPenalty = penalty;
|
|
535
|
-
}
|
|
536
|
-
this.applyMask(i);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
this.mask = msk;
|
|
540
|
-
this.applyMask(msk);
|
|
541
|
-
this.drawFormatBits(msk);
|
|
542
|
-
}
|
|
543
|
-
/* -- Fields -- */
|
|
544
|
-
// The width and height of this QR Code, measured in modules, between
|
|
545
|
-
// 21 and 177 (inclusive). This is equal to version * 4 + 17.
|
|
546
|
-
size;
|
|
547
|
-
// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
548
|
-
// Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
549
|
-
// the resulting object still has a mask value between 0 and 7.
|
|
550
|
-
mask;
|
|
551
|
-
// The modules of this QR Code (false = light, true = dark).
|
|
552
|
-
// Immutable after constructor finishes. Accessed through getModule().
|
|
553
|
-
modules = [];
|
|
554
|
-
types = [];
|
|
555
|
-
/* -- Accessor methods -- */
|
|
556
|
-
// Returns the color of the module (pixel) at the given coordinates, which is false
|
|
557
|
-
// for light or true for dark. The top left corner has the coordinates (x=0, y=0).
|
|
558
|
-
// If the given coordinates are out of bounds, then false (light) is returned.
|
|
559
|
-
getModule(x, y) {
|
|
560
|
-
return x >= 0 && x < this.size && y >= 0 && y < this.size && this.modules[y][x];
|
|
561
|
-
}
|
|
562
|
-
/* -- Private helper methods for constructor: Drawing function modules -- */
|
|
563
|
-
// Reads this object's version field, and draws and marks all function modules.
|
|
564
|
-
drawFunctionPatterns() {
|
|
565
|
-
for (let i = 0; i < this.size; i++) {
|
|
566
|
-
this.setFunctionModule(6, i, i % 2 === 0, QrCodeDataType.Timing);
|
|
567
|
-
this.setFunctionModule(i, 6, i % 2 === 0, QrCodeDataType.Timing);
|
|
568
|
-
}
|
|
569
|
-
this.drawFinderPattern(3, 3);
|
|
570
|
-
this.drawFinderPattern(this.size - 4, 3);
|
|
571
|
-
this.drawFinderPattern(3, this.size - 4);
|
|
572
|
-
const alignPatPos = this.getAlignmentPatternPositions();
|
|
573
|
-
const numAlign = alignPatPos.length;
|
|
574
|
-
for (let i = 0; i < numAlign; i++) {
|
|
575
|
-
for (let j = 0; j < numAlign; j++) {
|
|
576
|
-
if (!(i === 0 && j === 0 || i === 0 && j === numAlign - 1 || i === numAlign - 1 && j === 0))
|
|
577
|
-
this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
this.drawFormatBits(0);
|
|
581
|
-
this.drawVersion();
|
|
582
|
-
}
|
|
583
|
-
// Draws two copies of the format bits (with its own error correction code)
|
|
584
|
-
// based on the given mask and this object's error correction level field.
|
|
585
|
-
drawFormatBits(mask) {
|
|
586
|
-
const data = this.ecc[1] << 3 | mask;
|
|
587
|
-
let rem = data;
|
|
588
|
-
for (let i = 0; i < 10; i++)
|
|
589
|
-
rem = rem << 1 ^ (rem >>> 9) * 1335;
|
|
590
|
-
const bits = (data << 10 | rem) ^ 21522;
|
|
591
|
-
for (let i = 0; i <= 5; i++)
|
|
592
|
-
this.setFunctionModule(8, i, getBit(bits, i));
|
|
593
|
-
this.setFunctionModule(8, 7, getBit(bits, 6));
|
|
594
|
-
this.setFunctionModule(8, 8, getBit(bits, 7));
|
|
595
|
-
this.setFunctionModule(7, 8, getBit(bits, 8));
|
|
596
|
-
for (let i = 9; i < 15; i++)
|
|
597
|
-
this.setFunctionModule(14 - i, 8, getBit(bits, i));
|
|
598
|
-
for (let i = 0; i < 8; i++)
|
|
599
|
-
this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
|
|
600
|
-
for (let i = 8; i < 15; i++)
|
|
601
|
-
this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
|
|
602
|
-
this.setFunctionModule(8, this.size - 8, true);
|
|
603
|
-
}
|
|
604
|
-
// Draws two copies of the version bits (with its own error correction code),
|
|
605
|
-
// based on this object's version field, iff 7 <= version <= 40.
|
|
606
|
-
drawVersion() {
|
|
607
|
-
if (this.version < 7)
|
|
608
|
-
return;
|
|
609
|
-
let rem = this.version;
|
|
610
|
-
for (let i = 0; i < 12; i++)
|
|
611
|
-
rem = rem << 1 ^ (rem >>> 11) * 7973;
|
|
612
|
-
const bits = this.version << 12 | rem;
|
|
613
|
-
for (let i = 0; i < 18; i++) {
|
|
614
|
-
const color = getBit(bits, i);
|
|
615
|
-
const a = this.size - 11 + i % 3;
|
|
616
|
-
const b = Math.floor(i / 3);
|
|
617
|
-
this.setFunctionModule(a, b, color);
|
|
618
|
-
this.setFunctionModule(b, a, color);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
// Draws a 9*9 finder pattern including the border separator,
|
|
622
|
-
// with the center module at (x, y). Modules can be out of bounds.
|
|
623
|
-
drawFinderPattern(x, y) {
|
|
624
|
-
for (let dy = -4; dy <= 4; dy++) {
|
|
625
|
-
for (let dx = -4; dx <= 4; dx++) {
|
|
626
|
-
const dist = Math.max(Math.abs(dx), Math.abs(dy));
|
|
627
|
-
const xx = x + dx;
|
|
628
|
-
const yy = y + dy;
|
|
629
|
-
if (xx >= 0 && xx < this.size && yy >= 0 && yy < this.size)
|
|
630
|
-
this.setFunctionModule(xx, yy, dist !== 2 && dist !== 4, QrCodeDataType.Position);
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
// Draws a 5*5 alignment pattern, with the center module
|
|
635
|
-
// at (x, y). All modules must be in bounds.
|
|
636
|
-
drawAlignmentPattern(x, y) {
|
|
637
|
-
for (let dy = -2; dy <= 2; dy++) {
|
|
638
|
-
for (let dx = -2; dx <= 2; dx++) {
|
|
639
|
-
this.setFunctionModule(
|
|
640
|
-
x + dx,
|
|
641
|
-
y + dy,
|
|
642
|
-
Math.max(Math.abs(dx), Math.abs(dy)) !== 1,
|
|
643
|
-
QrCodeDataType.Alignment
|
|
644
|
-
);
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
// Sets the color of a module and marks it as a function module.
|
|
649
|
-
// Only used by the constructor. Coordinates must be in bounds.
|
|
650
|
-
setFunctionModule(x, y, isDark, type = QrCodeDataType.Function) {
|
|
651
|
-
this.modules[y][x] = isDark;
|
|
652
|
-
this.types[y][x] = type;
|
|
653
|
-
}
|
|
654
|
-
/* -- Private helper methods for constructor: Codewords and masking -- */
|
|
655
|
-
// Returns a new byte string representing the given data with the appropriate error correction
|
|
656
|
-
// codewords appended to it, based on this object's version and error correction level.
|
|
657
|
-
addEccAndInterleave(data) {
|
|
658
|
-
const ver = this.version;
|
|
659
|
-
const ecl = this.ecc;
|
|
660
|
-
if (data.length !== getNumDataCodewords(ver, ecl))
|
|
661
|
-
throw new RangeError("Invalid argument");
|
|
662
|
-
const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
|
|
663
|
-
const blockEccLen = ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver];
|
|
664
|
-
const rawCodewords = Math.floor(getNumRawDataModules(ver) / 8);
|
|
665
|
-
const numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
666
|
-
const shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
|
667
|
-
const blocks = [];
|
|
668
|
-
const rsDiv = reedSolomonComputeDivisor(blockEccLen);
|
|
669
|
-
for (let i = 0, k = 0; i < numBlocks; i++) {
|
|
670
|
-
const dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
|
|
671
|
-
k += dat.length;
|
|
672
|
-
const ecc = reedSolomonComputeRemainder(dat, rsDiv);
|
|
673
|
-
if (i < numShortBlocks)
|
|
674
|
-
dat.push(0);
|
|
675
|
-
blocks.push(dat.concat(ecc));
|
|
676
|
-
}
|
|
677
|
-
const result = [];
|
|
678
|
-
for (let i = 0; i < blocks[0].length; i++) {
|
|
679
|
-
blocks.forEach((block, j) => {
|
|
680
|
-
if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks)
|
|
681
|
-
result.push(block[i]);
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
return result;
|
|
685
|
-
}
|
|
686
|
-
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
|
687
|
-
// data area of this QR Code. Function modules need to be marked off before this is called.
|
|
688
|
-
drawCodewords(data) {
|
|
689
|
-
if (data.length !== Math.floor(getNumRawDataModules(this.version) / 8))
|
|
690
|
-
throw new RangeError("Invalid argument");
|
|
691
|
-
let i = 0;
|
|
692
|
-
for (let right = this.size - 1; right >= 1; right -= 2) {
|
|
693
|
-
if (right === 6)
|
|
694
|
-
right = 5;
|
|
695
|
-
for (let vert = 0; vert < this.size; vert++) {
|
|
696
|
-
for (let j = 0; j < 2; j++) {
|
|
697
|
-
const x = right - j;
|
|
698
|
-
const upward = (right + 1 & 2) === 0;
|
|
699
|
-
const y = upward ? this.size - 1 - vert : vert;
|
|
700
|
-
if (!this.types[y][x] && i < data.length * 8) {
|
|
701
|
-
this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
|
|
702
|
-
i++;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
// XORs the codeword modules in this QR Code with the given mask pattern.
|
|
709
|
-
// The function modules must be marked and the codeword bits must be drawn
|
|
710
|
-
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
711
|
-
// the same mask value a second time will undo the mask. A final well-formed
|
|
712
|
-
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
|
713
|
-
applyMask(mask) {
|
|
714
|
-
if (mask < 0 || mask > 7)
|
|
715
|
-
throw new RangeError("Mask value out of range");
|
|
716
|
-
for (let y = 0; y < this.size; y++) {
|
|
717
|
-
for (let x = 0; x < this.size; x++) {
|
|
718
|
-
let invert;
|
|
719
|
-
switch (mask) {
|
|
720
|
-
case 0:
|
|
721
|
-
invert = (x + y) % 2 === 0;
|
|
722
|
-
break;
|
|
723
|
-
case 1:
|
|
724
|
-
invert = y % 2 === 0;
|
|
725
|
-
break;
|
|
726
|
-
case 2:
|
|
727
|
-
invert = x % 3 === 0;
|
|
728
|
-
break;
|
|
729
|
-
case 3:
|
|
730
|
-
invert = (x + y) % 3 === 0;
|
|
731
|
-
break;
|
|
732
|
-
case 4:
|
|
733
|
-
invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0;
|
|
734
|
-
break;
|
|
735
|
-
case 5:
|
|
736
|
-
invert = x * y % 2 + x * y % 3 === 0;
|
|
737
|
-
break;
|
|
738
|
-
case 6:
|
|
739
|
-
invert = (x * y % 2 + x * y % 3) % 2 === 0;
|
|
740
|
-
break;
|
|
741
|
-
case 7:
|
|
742
|
-
invert = ((x + y) % 2 + x * y % 3) % 2 === 0;
|
|
743
|
-
break;
|
|
744
|
-
default:
|
|
745
|
-
throw new Error("Unreachable");
|
|
746
|
-
}
|
|
747
|
-
if (!this.types[y][x] && invert)
|
|
748
|
-
this.modules[y][x] = !this.modules[y][x];
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
753
|
-
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
|
754
|
-
getPenaltyScore() {
|
|
755
|
-
let result = 0;
|
|
756
|
-
for (let y = 0; y < this.size; y++) {
|
|
757
|
-
let runColor = false;
|
|
758
|
-
let runX = 0;
|
|
759
|
-
const runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
760
|
-
for (let x = 0; x < this.size; x++) {
|
|
761
|
-
if (this.modules[y][x] === runColor) {
|
|
762
|
-
runX++;
|
|
763
|
-
if (runX === 5)
|
|
764
|
-
result += PENALTY_N1;
|
|
765
|
-
else if (runX > 5)
|
|
766
|
-
result++;
|
|
767
|
-
} else {
|
|
768
|
-
this.finderPenaltyAddHistory(runX, runHistory);
|
|
769
|
-
if (!runColor)
|
|
770
|
-
result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
771
|
-
runColor = this.modules[y][x];
|
|
772
|
-
runX = 1;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
|
|
776
|
-
}
|
|
777
|
-
for (let x = 0; x < this.size; x++) {
|
|
778
|
-
let runColor = false;
|
|
779
|
-
let runY = 0;
|
|
780
|
-
const runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
781
|
-
for (let y = 0; y < this.size; y++) {
|
|
782
|
-
if (this.modules[y][x] === runColor) {
|
|
783
|
-
runY++;
|
|
784
|
-
if (runY === 5)
|
|
785
|
-
result += PENALTY_N1;
|
|
786
|
-
else if (runY > 5)
|
|
787
|
-
result++;
|
|
788
|
-
} else {
|
|
789
|
-
this.finderPenaltyAddHistory(runY, runHistory);
|
|
790
|
-
if (!runColor)
|
|
791
|
-
result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
792
|
-
runColor = this.modules[y][x];
|
|
793
|
-
runY = 1;
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
|
|
797
|
-
}
|
|
798
|
-
for (let y = 0; y < this.size - 1; y++) {
|
|
799
|
-
for (let x = 0; x < this.size - 1; x++) {
|
|
800
|
-
const color = this.modules[y][x];
|
|
801
|
-
if (color === this.modules[y][x + 1] && color === this.modules[y + 1][x] && color === this.modules[y + 1][x + 1]) {
|
|
802
|
-
result += PENALTY_N2;
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
let dark = 0;
|
|
807
|
-
for (const row of this.modules)
|
|
808
|
-
dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
|
|
809
|
-
const total = this.size * this.size;
|
|
810
|
-
const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
|
|
811
|
-
result += k * PENALTY_N4;
|
|
812
|
-
return result;
|
|
813
|
-
}
|
|
814
|
-
/* -- Private helper functions -- */
|
|
815
|
-
// Returns an ascending list of positions of alignment patterns for this version number.
|
|
816
|
-
// Each position is in the range [0,177), and are used on both the x and y axes.
|
|
817
|
-
// This could be implemented as lookup table of 40 variable-length lists of integers.
|
|
818
|
-
getAlignmentPatternPositions() {
|
|
819
|
-
if (this.version === 1) {
|
|
820
|
-
return [];
|
|
821
|
-
} else {
|
|
822
|
-
const numAlign = Math.floor(this.version / 7) + 2;
|
|
823
|
-
const step2 = this.version === 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
|
|
824
|
-
const result = [6];
|
|
825
|
-
for (let pos = this.size - 7; result.length < numAlign; pos -= step2)
|
|
826
|
-
result.splice(1, 0, pos);
|
|
827
|
-
return result;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
// Can only be called immediately after a light run is added, and
|
|
831
|
-
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
|
832
|
-
finderPenaltyCountPatterns(runHistory) {
|
|
833
|
-
const n = runHistory[1];
|
|
834
|
-
const core = n > 0 && runHistory[2] === n && runHistory[3] === n * 3 && runHistory[4] === n && runHistory[5] === n;
|
|
835
|
-
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
|
836
|
-
}
|
|
837
|
-
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
|
838
|
-
finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
|
|
839
|
-
if (currentRunColor) {
|
|
840
|
-
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
841
|
-
currentRunLength = 0;
|
|
842
|
-
}
|
|
843
|
-
currentRunLength += this.size;
|
|
844
|
-
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
845
|
-
return this.finderPenaltyCountPatterns(runHistory);
|
|
846
|
-
}
|
|
847
|
-
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
|
848
|
-
finderPenaltyAddHistory(currentRunLength, runHistory) {
|
|
849
|
-
if (runHistory[0] === 0)
|
|
850
|
-
currentRunLength += this.size;
|
|
851
|
-
runHistory.pop();
|
|
852
|
-
runHistory.unshift(currentRunLength);
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
function appendBits(val, len, bb) {
|
|
856
|
-
if (len < 0 || len > 31 || val >>> len !== 0)
|
|
857
|
-
throw new RangeError("Value out of range");
|
|
858
|
-
for (let i = len - 1; i >= 0; i--)
|
|
859
|
-
bb.push(val >>> i & 1);
|
|
860
|
-
}
|
|
861
|
-
function getBit(x, i) {
|
|
862
|
-
return (x >>> i & 1) !== 0;
|
|
863
|
-
}
|
|
864
|
-
var QrSegment = class {
|
|
865
|
-
// Creates a new QR Code segment with the given attributes and data.
|
|
866
|
-
// The character count (numChars) must agree with the mode and the bit buffer length,
|
|
867
|
-
// but the constraint isn't checked. The given bit buffer is cloned and stored.
|
|
868
|
-
constructor(mode, numChars, bitData) {
|
|
869
|
-
this.mode = mode;
|
|
870
|
-
this.numChars = numChars;
|
|
871
|
-
this.bitData = bitData;
|
|
872
|
-
if (numChars < 0)
|
|
873
|
-
throw new RangeError("Invalid argument");
|
|
874
|
-
this.bitData = bitData.slice();
|
|
875
|
-
}
|
|
876
|
-
/* -- Methods -- */
|
|
877
|
-
// Returns a new copy of the data bits of this segment.
|
|
878
|
-
getData() {
|
|
879
|
-
return this.bitData.slice();
|
|
880
|
-
}
|
|
881
|
-
};
|
|
882
|
-
var MODE_NUMERIC = [1, 10, 12, 14];
|
|
883
|
-
var MODE_ALPHANUMERIC = [2, 9, 11, 13];
|
|
884
|
-
var MODE_BYTE = [4, 8, 16, 16];
|
|
885
|
-
function numCharCountBits(mode, ver) {
|
|
886
|
-
return mode[Math.floor((ver + 7) / 17) + 1];
|
|
887
|
-
}
|
|
888
|
-
function makeBytes(data) {
|
|
889
|
-
const bb = [];
|
|
890
|
-
for (const b of data)
|
|
891
|
-
appendBits(b, 8, bb);
|
|
892
|
-
return new QrSegment(MODE_BYTE, data.length, bb);
|
|
893
|
-
}
|
|
894
|
-
function makeNumeric(digits) {
|
|
895
|
-
if (!isNumeric(digits))
|
|
896
|
-
throw new RangeError("String contains non-numeric characters");
|
|
897
|
-
const bb = [];
|
|
898
|
-
for (let i = 0; i < digits.length; ) {
|
|
899
|
-
const n = Math.min(digits.length - i, 3);
|
|
900
|
-
appendBits(Number.parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
|
|
901
|
-
i += n;
|
|
902
|
-
}
|
|
903
|
-
return new QrSegment(MODE_NUMERIC, digits.length, bb);
|
|
904
|
-
}
|
|
905
|
-
function makeAlphanumeric(text) {
|
|
906
|
-
if (!isAlphanumeric(text))
|
|
907
|
-
throw new RangeError("String contains unencodable characters in alphanumeric mode");
|
|
908
|
-
const bb = [];
|
|
909
|
-
let i;
|
|
910
|
-
for (i = 0; i + 2 <= text.length; i += 2) {
|
|
911
|
-
let temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
|
|
912
|
-
temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
|
|
913
|
-
appendBits(temp, 11, bb);
|
|
914
|
-
}
|
|
915
|
-
if (i < text.length)
|
|
916
|
-
appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
|
|
917
|
-
return new QrSegment(MODE_ALPHANUMERIC, text.length, bb);
|
|
918
|
-
}
|
|
919
|
-
function makeSegments(text) {
|
|
920
|
-
if (text === "")
|
|
921
|
-
return [];
|
|
922
|
-
else if (isNumeric(text))
|
|
923
|
-
return [makeNumeric(text)];
|
|
924
|
-
else if (isAlphanumeric(text))
|
|
925
|
-
return [makeAlphanumeric(text)];
|
|
926
|
-
else
|
|
927
|
-
return [makeBytes(toUtf8ByteArray(text))];
|
|
928
|
-
}
|
|
929
|
-
function isNumeric(text) {
|
|
930
|
-
return NUMERIC_REGEX.test(text);
|
|
931
|
-
}
|
|
932
|
-
function isAlphanumeric(text) {
|
|
933
|
-
return ALPHANUMERIC_REGEX.test(text);
|
|
934
|
-
}
|
|
935
|
-
function getTotalBits(segs, version) {
|
|
936
|
-
let result = 0;
|
|
937
|
-
for (const seg of segs) {
|
|
938
|
-
const ccbits = numCharCountBits(seg.mode, version);
|
|
939
|
-
if (seg.numChars >= 1 << ccbits)
|
|
940
|
-
return Number.POSITIVE_INFINITY;
|
|
941
|
-
result += 4 + ccbits + seg.bitData.length;
|
|
942
|
-
}
|
|
943
|
-
return result;
|
|
944
|
-
}
|
|
945
|
-
function toUtf8ByteArray(str) {
|
|
946
|
-
str = encodeURI(str);
|
|
947
|
-
const result = [];
|
|
948
|
-
for (let i = 0; i < str.length; i++) {
|
|
949
|
-
if (str.charAt(i) !== "%") {
|
|
950
|
-
result.push(str.charCodeAt(i));
|
|
951
|
-
} else {
|
|
952
|
-
result.push(Number.parseInt(str.substring(i + 1, i + 3), 16));
|
|
953
|
-
i += 2;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
return result;
|
|
957
|
-
}
|
|
958
|
-
function getNumRawDataModules(ver) {
|
|
959
|
-
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
|
960
|
-
throw new RangeError("Version number out of range");
|
|
961
|
-
let result = (16 * ver + 128) * ver + 64;
|
|
962
|
-
if (ver >= 2) {
|
|
963
|
-
const numAlign = Math.floor(ver / 7) + 2;
|
|
964
|
-
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
965
|
-
if (ver >= 7)
|
|
966
|
-
result -= 36;
|
|
967
|
-
}
|
|
968
|
-
return result;
|
|
969
|
-
}
|
|
970
|
-
function getNumDataCodewords(ver, ecl) {
|
|
971
|
-
return Math.floor(getNumRawDataModules(ver) / 8) - ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver] * NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
|
|
972
|
-
}
|
|
973
|
-
function reedSolomonComputeDivisor(degree) {
|
|
974
|
-
if (degree < 1 || degree > 255)
|
|
975
|
-
throw new RangeError("Degree out of range");
|
|
976
|
-
const result = [];
|
|
977
|
-
for (let i = 0; i < degree - 1; i++)
|
|
978
|
-
result.push(0);
|
|
979
|
-
result.push(1);
|
|
980
|
-
let root = 1;
|
|
981
|
-
for (let i = 0; i < degree; i++) {
|
|
982
|
-
for (let j = 0; j < result.length; j++) {
|
|
983
|
-
result[j] = reedSolomonMultiply(result[j], root);
|
|
984
|
-
if (j + 1 < result.length)
|
|
985
|
-
result[j] ^= result[j + 1];
|
|
986
|
-
}
|
|
987
|
-
root = reedSolomonMultiply(root, 2);
|
|
988
|
-
}
|
|
989
|
-
return result;
|
|
990
|
-
}
|
|
991
|
-
function reedSolomonComputeRemainder(data, divisor) {
|
|
992
|
-
const result = divisor.map((_) => 0);
|
|
993
|
-
for (const b of data) {
|
|
994
|
-
const factor = b ^ result.shift();
|
|
995
|
-
result.push(0);
|
|
996
|
-
divisor.forEach((coef, i) => result[i] ^= reedSolomonMultiply(coef, factor));
|
|
997
|
-
}
|
|
998
|
-
return result;
|
|
999
|
-
}
|
|
1000
|
-
function reedSolomonMultiply(x, y) {
|
|
1001
|
-
if (x >>> 8 !== 0 || y >>> 8 !== 0)
|
|
1002
|
-
throw new RangeError("Byte out of range");
|
|
1003
|
-
let z = 0;
|
|
1004
|
-
for (let i = 7; i >= 0; i--) {
|
|
1005
|
-
z = z << 1 ^ (z >>> 7) * 285;
|
|
1006
|
-
z ^= (y >>> i & 1) * x;
|
|
1007
|
-
}
|
|
1008
|
-
return z;
|
|
1009
|
-
}
|
|
1010
|
-
function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
|
|
1011
|
-
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7) {
|
|
1012
|
-
throw new RangeError("Invalid value");
|
|
1013
|
-
}
|
|
1014
|
-
let version;
|
|
1015
|
-
let dataUsedBits;
|
|
1016
|
-
for (version = minVersion; ; version++) {
|
|
1017
|
-
const dataCapacityBits2 = getNumDataCodewords(version, ecl) * 8;
|
|
1018
|
-
const usedBits = getTotalBits(segs, version);
|
|
1019
|
-
if (usedBits <= dataCapacityBits2) {
|
|
1020
|
-
dataUsedBits = usedBits;
|
|
1021
|
-
break;
|
|
1022
|
-
}
|
|
1023
|
-
if (version >= maxVersion)
|
|
1024
|
-
throw new RangeError("Data too long");
|
|
1025
|
-
}
|
|
1026
|
-
for (const newEcl of [MEDIUM, QUARTILE, HIGH]) {
|
|
1027
|
-
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
|
|
1028
|
-
ecl = newEcl;
|
|
1029
|
-
}
|
|
1030
|
-
const bb = [];
|
|
1031
|
-
for (const seg of segs) {
|
|
1032
|
-
appendBits(seg.mode[0], 4, bb);
|
|
1033
|
-
appendBits(seg.numChars, numCharCountBits(seg.mode, version), bb);
|
|
1034
|
-
for (const b of seg.getData())
|
|
1035
|
-
bb.push(b);
|
|
1036
|
-
}
|
|
1037
|
-
const dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
|
1038
|
-
appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
|
|
1039
|
-
appendBits(0, (8 - bb.length % 8) % 8, bb);
|
|
1040
|
-
for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 236 ^ 17)
|
|
1041
|
-
appendBits(padByte, 8, bb);
|
|
1042
|
-
const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0);
|
|
1043
|
-
bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
|
|
1044
|
-
return new QrCode(version, ecl, dataCodewords, mask);
|
|
1045
|
-
}
|
|
1046
|
-
function encode(data, options) {
|
|
1047
|
-
const {
|
|
1048
|
-
ecc = "L",
|
|
1049
|
-
boostEcc = false,
|
|
1050
|
-
minVersion = 1,
|
|
1051
|
-
maxVersion = 40,
|
|
1052
|
-
maskPattern = -1,
|
|
1053
|
-
border = 1
|
|
1054
|
-
} = options || {};
|
|
1055
|
-
const segment = typeof data === "string" ? makeSegments(data) : Array.isArray(data) ? [makeBytes(data)] : void 0;
|
|
1056
|
-
if (!segment)
|
|
1057
|
-
throw new Error(`uqr only supports encoding string and binary data, but got: ${typeof data}`);
|
|
1058
|
-
const qr = encodeSegments(
|
|
1059
|
-
segment,
|
|
1060
|
-
EccMap[ecc],
|
|
1061
|
-
minVersion,
|
|
1062
|
-
maxVersion,
|
|
1063
|
-
maskPattern,
|
|
1064
|
-
boostEcc
|
|
1065
|
-
);
|
|
1066
|
-
const result = addBorder({
|
|
1067
|
-
version: qr.version,
|
|
1068
|
-
maskPattern: qr.mask,
|
|
1069
|
-
size: qr.size,
|
|
1070
|
-
data: qr.modules,
|
|
1071
|
-
types: qr.types
|
|
1072
|
-
}, border);
|
|
1073
|
-
if (options?.invert)
|
|
1074
|
-
result.data = result.data.map((row) => row.map((mod) => !mod));
|
|
1075
|
-
options?.onEncoded?.(result);
|
|
1076
|
-
return result;
|
|
1077
|
-
}
|
|
1078
|
-
function addBorder(input, border = 1) {
|
|
1079
|
-
if (!border)
|
|
1080
|
-
return input;
|
|
1081
|
-
const { size } = input;
|
|
1082
|
-
const newSize = size + border * 2;
|
|
1083
|
-
input.size = newSize;
|
|
1084
|
-
input.data.forEach((row) => {
|
|
1085
|
-
for (let i = 0; i < border; i++) {
|
|
1086
|
-
row.unshift(false);
|
|
1087
|
-
row.push(false);
|
|
1088
|
-
}
|
|
1089
|
-
});
|
|
1090
|
-
for (let i = 0; i < border; i++) {
|
|
1091
|
-
input.data.unshift(Array.from({ length: newSize }, (_) => false));
|
|
1092
|
-
input.data.push(Array.from({ length: newSize }, (_) => false));
|
|
1093
|
-
}
|
|
1094
|
-
const b = QrCodeDataType.Border;
|
|
1095
|
-
input.types.forEach((row) => {
|
|
1096
|
-
for (let i = 0; i < border; i++) {
|
|
1097
|
-
row.unshift(b);
|
|
1098
|
-
row.push(b);
|
|
1099
|
-
}
|
|
1100
|
-
});
|
|
1101
|
-
for (let i = 0; i < border; i++) {
|
|
1102
|
-
input.types.unshift(Array.from({ length: newSize }, (_) => b));
|
|
1103
|
-
input.types.push(Array.from({ length: newSize }, (_) => b));
|
|
1104
|
-
}
|
|
1105
|
-
return input;
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
// src/qr.ts
|
|
1109
|
-
function halfBlocks(modules) {
|
|
1110
|
-
const lines = [];
|
|
1111
|
-
for (let y = 0; y < modules.length; y += 2) {
|
|
1112
|
-
const top = modules[y] ?? [];
|
|
1113
|
-
const bottom = modules[y + 1] ?? [];
|
|
1114
|
-
let line = "";
|
|
1115
|
-
for (let x = 0; x < top.length; x++) {
|
|
1116
|
-
line += top[x] ? bottom[x] ? "\u2588" : "\u2580" : bottom[x] ? "\u2584" : " ";
|
|
1117
|
-
}
|
|
1118
|
-
lines.push(line);
|
|
1119
|
-
}
|
|
1120
|
-
return lines;
|
|
1121
|
-
}
|
|
1122
|
-
function terminalQr(data, columns) {
|
|
1123
|
-
const { data: modules } = encode(data, { ecc: "M", border: 2 });
|
|
1124
|
-
const width = modules[0]?.length ?? 0;
|
|
1125
|
-
if (columns === void 0 || columns < width) return null;
|
|
1126
|
-
return halfBlocks(modules).map((line) => `\x1B[30;47m${line}\x1B[0m`).join("\n");
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
509
|
// src/cli.ts
|
|
1130
510
|
function globalFlag(name) {
|
|
1131
511
|
const option = GLOBAL_FLAGS.find((candidate) => candidate.flag === name);
|
|
@@ -1207,78 +587,78 @@ async function run(argv) {
|
|
|
1207
587
|
case "mcp": {
|
|
1208
588
|
parseOptions(spec, rest);
|
|
1209
589
|
const mcpUrl = resolveMcpUrl(apiUrl);
|
|
1210
|
-
const { runMcpBridge } = await import("./mcp-bridge-
|
|
590
|
+
const { runMcpBridge } = await import("./mcp-bridge-ANLTZ4MB.js");
|
|
1211
591
|
await runMcpBridge(mcpUrl, apiUrl);
|
|
1212
592
|
return void 0;
|
|
1213
593
|
}
|
|
1214
594
|
}
|
|
1215
595
|
}
|
|
1216
|
-
const
|
|
1217
|
-
if (
|
|
596
|
+
const mode = cancelRequest(spec, rest, process.env);
|
|
597
|
+
if (mode) return cancelOrder(apiUrl, spec, mode);
|
|
1218
598
|
const { method, path, body } = parseCommand(spec, rest, process.env);
|
|
1219
|
-
if (spec.tool === "pay_order"
|
|
599
|
+
if (spec.tool === "pay_order") await payableInUsd(apiUrl, spec, rest);
|
|
1220
600
|
const data = unwrap(await authorizedRequest(apiUrl, path, { method, body }));
|
|
1221
601
|
return { data, text: renderToolResult(spec.tool, data, "cli") };
|
|
1222
602
|
}
|
|
1223
|
-
async function
|
|
603
|
+
async function payableInUsd(apiUrl, spec, rest) {
|
|
1224
604
|
const [orderId] = parseOptions(spec, rest).positionals;
|
|
1225
|
-
const
|
|
1226
|
-
const currency =
|
|
1227
|
-
|
|
605
|
+
const order = unwrap(await authorizedRequest(apiUrl, API.order(orderId), { method: "GET" }));
|
|
606
|
+
const currency = order?.total?.currency;
|
|
607
|
+
const read = `\`${commandInvocation({ group: "orders", name: "get" })} ${orderId}\``;
|
|
608
|
+
if (typeof currency !== "string") {
|
|
609
|
+
throw new CliError(`Not sent: order ${orderId} shows no total to pay. Read it with ${read}.`, EXIT.error);
|
|
610
|
+
}
|
|
611
|
+
if (currency !== "USD") {
|
|
1228
612
|
throw new CliError(
|
|
1229
|
-
`Not sent:
|
|
613
|
+
`Not sent: order ${orderId} is not in USD, and a token pays a USD order only. The user pays by the order's payment link: ${read} shows it.`,
|
|
1230
614
|
EXIT.error
|
|
1231
615
|
);
|
|
1232
616
|
}
|
|
1233
|
-
body.total = { ...body.total, currency };
|
|
1234
|
-
check(spec, { ...body, orderId }, HTTP_ROUTES.pay_order, /* @__PURE__ */ new Map());
|
|
1235
617
|
}
|
|
1236
|
-
async function cancelOrder(apiUrl, spec,
|
|
1237
|
-
const {
|
|
1238
|
-
const
|
|
1239
|
-
const read =
|
|
1240
|
-
const
|
|
1241
|
-
if (!request || !close && closable) throw usageError(notCancelled(spec, flag, orderId, order2));
|
|
618
|
+
async function cancelOrder(apiUrl, spec, mode) {
|
|
619
|
+
const { orderId, close, request } = mode;
|
|
620
|
+
const order = unwrap(await authorizedRequest(apiUrl, mode.path, { method: mode.method }));
|
|
621
|
+
const read = order;
|
|
622
|
+
const action = read?.cancel?.action;
|
|
1242
623
|
if (close) {
|
|
1243
|
-
if (read?.status === "
|
|
1244
|
-
|
|
1245
|
-
if (read?.cancel?.available === true && read.cancel.reason === "cancellable") {
|
|
1246
|
-
throw usageError(
|
|
1247
|
-
`Not closed: the order is paid. To cancel it under its terms, show the user cancel.penalty and, once they agree, run \`${commandInvocation(spec)} ${orderId} ${flag} <amount>\`.`
|
|
1248
|
-
);
|
|
1249
|
-
}
|
|
1250
|
-
throw new CliError(["Not closed: nothing was sent.", ...orderLines(order2)].join("\n"), EXIT.error);
|
|
624
|
+
if (read?.status === "closed" || read?.status === "expired") {
|
|
625
|
+
return { data: order, text: renderToolResult("cancel_order", order, "cli") };
|
|
1251
626
|
}
|
|
627
|
+
if (action === "cancel") throw modeRefused(orderId, order);
|
|
1252
628
|
const data2 = unwrap(await authorizedRequest(apiUrl, request.path, { method: "POST", body: request.body }));
|
|
1253
|
-
|
|
1254
|
-
if (data2?.status !== "expired") {
|
|
1255
|
-
throw new CliError(["Not closed: it was paid meanwhile.", text].join("\n"), EXIT.error);
|
|
1256
|
-
}
|
|
1257
|
-
return { data: data2, text };
|
|
629
|
+
return { data: data2, text: renderToolResult("cancel_order", data2, "cli") };
|
|
1258
630
|
}
|
|
631
|
+
if (action === "close") throw modeRefused(orderId, order);
|
|
1259
632
|
const penalty = read?.cancel?.penalty?.currency;
|
|
1260
|
-
const currency = typeof penalty === "string" ? penalty : read?.
|
|
633
|
+
const currency = typeof penalty === "string" ? penalty : read?.total?.currency;
|
|
1261
634
|
if (typeof currency !== "string") {
|
|
1262
635
|
throw new CliError(`Not sent: the order ${orderId} has no currency to send the penalty in.`, EXIT.error);
|
|
1263
636
|
}
|
|
1264
637
|
const body = request.body;
|
|
1265
638
|
body.maximumPenalty = { ...body.maximumPenalty, currency };
|
|
1266
|
-
check(spec, { ...body, orderId }, HTTP_ROUTES.cancel_order
|
|
639
|
+
check(spec, { ...body, orderId }, HTTP_ROUTES.cancel_order);
|
|
1267
640
|
const data = unwrap(await authorizedRequest(apiUrl, request.path, { method: "POST", body }));
|
|
1268
641
|
return { data, text: renderToolResult("cancel_order", data, "cli") };
|
|
1269
642
|
}
|
|
1270
|
-
function
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
643
|
+
function modeRefused(orderId, order) {
|
|
644
|
+
const cancel = orderCancel.safeParse(order?.cancel);
|
|
645
|
+
if (!cancel.success) {
|
|
646
|
+
const read = `\`${commandInvocation({ group: "orders", name: "get" })} ${orderId}\``;
|
|
647
|
+
return new CliError(
|
|
648
|
+
`Not sent: order ${orderId} doesn't show what can be done to it now. Read it with ${read}.`,
|
|
649
|
+
EXIT.error
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const message = "The order can't be cancelled or closed the way that was asked, and nothing was sent.";
|
|
653
|
+
return new CliError(message, EXIT.error, {}, {
|
|
654
|
+
error: {
|
|
655
|
+
code: "order_not_cancellable",
|
|
656
|
+
message,
|
|
657
|
+
retryable: false,
|
|
658
|
+
requestId: localRequestId(),
|
|
659
|
+
details: { orderId, cancel: cancel.data }
|
|
660
|
+
}
|
|
661
|
+
});
|
|
1282
662
|
}
|
|
1283
663
|
function account(data) {
|
|
1284
664
|
const { apiUrl, userId, email, name } = data;
|
|
@@ -1305,13 +685,6 @@ function takeJson(argv) {
|
|
|
1305
685
|
const kept = before.filter((arg) => arg !== JSON_FLAG);
|
|
1306
686
|
return { json: kept.length < before.length, argv: [...kept, ...after] };
|
|
1307
687
|
}
|
|
1308
|
-
function walletCode(argv, data) {
|
|
1309
|
-
if (argv[0] !== "orders" || argv[1] !== "pay" || !process.stdout.isTTY) return null;
|
|
1310
|
-
const methods = data?.payment?.methods;
|
|
1311
|
-
const wallet = Array.isArray(methods) ? methods.find((m) => m?.id === "stripe_alipay_wechat_pay") : void 0;
|
|
1312
|
-
const qrData = wallet?.wechatPay?.qrData;
|
|
1313
|
-
return typeof qrData === "string" ? terminalQr(qrData, process.stdout.columns) : null;
|
|
1314
|
-
}
|
|
1315
688
|
async function main() {
|
|
1316
689
|
const { json, argv } = takeJson(process.argv.slice(2));
|
|
1317
690
|
try {
|
|
@@ -1324,10 +697,6 @@ async function main() {
|
|
|
1324
697
|
`);
|
|
1325
698
|
} else {
|
|
1326
699
|
process.stdout.write(`${[text, ...fieldLines(notices)].join("\n")}
|
|
1327
|
-
`);
|
|
1328
|
-
const code = walletCode(argv, data);
|
|
1329
|
-
if (code) process.stdout.write(`Or scan this WeChat Pay code:
|
|
1330
|
-
${code}
|
|
1331
700
|
`);
|
|
1332
701
|
}
|
|
1333
702
|
}
|