@auth-link/web 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2572 @@
1
+ // node_modules/qrcode-generator/dist/qrcode.mjs
2
+ var qrcode = function(typeNumber, errorCorrectionLevel) {
3
+ const PAD0 = 236;
4
+ const PAD1 = 17;
5
+ let _typeNumber = typeNumber;
6
+ const _errorCorrectionLevel = QRErrorCorrectionLevel[errorCorrectionLevel];
7
+ let _modules = null;
8
+ let _moduleCount = 0;
9
+ let _dataCache = null;
10
+ const _dataList = [];
11
+ const _this = {};
12
+ const makeImpl = function(test, maskPattern) {
13
+ _moduleCount = _typeNumber * 4 + 17;
14
+ _modules = (function(moduleCount) {
15
+ const modules = new Array(moduleCount);
16
+ for (let row = 0; row < moduleCount; row += 1) {
17
+ modules[row] = new Array(moduleCount);
18
+ for (let col = 0; col < moduleCount; col += 1) {
19
+ modules[row][col] = null;
20
+ }
21
+ }
22
+ return modules;
23
+ })(_moduleCount);
24
+ setupPositionProbePattern(0, 0);
25
+ setupPositionProbePattern(_moduleCount - 7, 0);
26
+ setupPositionProbePattern(0, _moduleCount - 7);
27
+ setupPositionAdjustPattern();
28
+ setupTimingPattern();
29
+ setupTypeInfo(test, maskPattern);
30
+ if (_typeNumber >= 7) {
31
+ setupTypeNumber(test);
32
+ }
33
+ if (_dataCache == null) {
34
+ _dataCache = createData(_typeNumber, _errorCorrectionLevel, _dataList);
35
+ }
36
+ mapData(_dataCache, maskPattern);
37
+ };
38
+ const setupPositionProbePattern = function(row, col) {
39
+ for (let r = -1; r <= 7; r += 1) {
40
+ if (row + r <= -1 || _moduleCount <= row + r) continue;
41
+ for (let c = -1; c <= 7; c += 1) {
42
+ if (col + c <= -1 || _moduleCount <= col + c) continue;
43
+ if (0 <= r && r <= 6 && (c == 0 || c == 6) || 0 <= c && c <= 6 && (r == 0 || r == 6) || 2 <= r && r <= 4 && 2 <= c && c <= 4) {
44
+ _modules[row + r][col + c] = true;
45
+ } else {
46
+ _modules[row + r][col + c] = false;
47
+ }
48
+ }
49
+ }
50
+ };
51
+ const getBestMaskPattern = function() {
52
+ let minLostPoint = 0;
53
+ let pattern = 0;
54
+ for (let i = 0; i < 8; i += 1) {
55
+ makeImpl(true, i);
56
+ const lostPoint = QRUtil.getLostPoint(_this);
57
+ if (i == 0 || minLostPoint > lostPoint) {
58
+ minLostPoint = lostPoint;
59
+ pattern = i;
60
+ }
61
+ }
62
+ return pattern;
63
+ };
64
+ const setupTimingPattern = function() {
65
+ for (let r = 8; r < _moduleCount - 8; r += 1) {
66
+ if (_modules[r][6] != null) {
67
+ continue;
68
+ }
69
+ _modules[r][6] = r % 2 == 0;
70
+ }
71
+ for (let c = 8; c < _moduleCount - 8; c += 1) {
72
+ if (_modules[6][c] != null) {
73
+ continue;
74
+ }
75
+ _modules[6][c] = c % 2 == 0;
76
+ }
77
+ };
78
+ const setupPositionAdjustPattern = function() {
79
+ const pos = QRUtil.getPatternPosition(_typeNumber);
80
+ for (let i = 0; i < pos.length; i += 1) {
81
+ for (let j = 0; j < pos.length; j += 1) {
82
+ const row = pos[i];
83
+ const col = pos[j];
84
+ if (_modules[row][col] != null) {
85
+ continue;
86
+ }
87
+ for (let r = -2; r <= 2; r += 1) {
88
+ for (let c = -2; c <= 2; c += 1) {
89
+ if (r == -2 || r == 2 || c == -2 || c == 2 || r == 0 && c == 0) {
90
+ _modules[row + r][col + c] = true;
91
+ } else {
92
+ _modules[row + r][col + c] = false;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ };
99
+ const setupTypeNumber = function(test) {
100
+ const bits = QRUtil.getBCHTypeNumber(_typeNumber);
101
+ for (let i = 0; i < 18; i += 1) {
102
+ const mod = !test && (bits >> i & 1) == 1;
103
+ _modules[Math.floor(i / 3)][i % 3 + _moduleCount - 8 - 3] = mod;
104
+ }
105
+ for (let i = 0; i < 18; i += 1) {
106
+ const mod = !test && (bits >> i & 1) == 1;
107
+ _modules[i % 3 + _moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
108
+ }
109
+ };
110
+ const setupTypeInfo = function(test, maskPattern) {
111
+ const data = _errorCorrectionLevel << 3 | maskPattern;
112
+ const bits = QRUtil.getBCHTypeInfo(data);
113
+ for (let i = 0; i < 15; i += 1) {
114
+ const mod = !test && (bits >> i & 1) == 1;
115
+ if (i < 6) {
116
+ _modules[i][8] = mod;
117
+ } else if (i < 8) {
118
+ _modules[i + 1][8] = mod;
119
+ } else {
120
+ _modules[_moduleCount - 15 + i][8] = mod;
121
+ }
122
+ }
123
+ for (let i = 0; i < 15; i += 1) {
124
+ const mod = !test && (bits >> i & 1) == 1;
125
+ if (i < 8) {
126
+ _modules[8][_moduleCount - i - 1] = mod;
127
+ } else if (i < 9) {
128
+ _modules[8][15 - i - 1 + 1] = mod;
129
+ } else {
130
+ _modules[8][15 - i - 1] = mod;
131
+ }
132
+ }
133
+ _modules[_moduleCount - 8][8] = !test;
134
+ };
135
+ const mapData = function(data, maskPattern) {
136
+ let inc = -1;
137
+ let row = _moduleCount - 1;
138
+ let bitIndex = 7;
139
+ let byteIndex = 0;
140
+ const maskFunc = QRUtil.getMaskFunction(maskPattern);
141
+ for (let col = _moduleCount - 1; col > 0; col -= 2) {
142
+ if (col == 6) col -= 1;
143
+ while (true) {
144
+ for (let c = 0; c < 2; c += 1) {
145
+ if (_modules[row][col - c] == null) {
146
+ let dark = false;
147
+ if (byteIndex < data.length) {
148
+ dark = (data[byteIndex] >>> bitIndex & 1) == 1;
149
+ }
150
+ const mask = maskFunc(row, col - c);
151
+ if (mask) {
152
+ dark = !dark;
153
+ }
154
+ _modules[row][col - c] = dark;
155
+ bitIndex -= 1;
156
+ if (bitIndex == -1) {
157
+ byteIndex += 1;
158
+ bitIndex = 7;
159
+ }
160
+ }
161
+ }
162
+ row += inc;
163
+ if (row < 0 || _moduleCount <= row) {
164
+ row -= inc;
165
+ inc = -inc;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ };
171
+ const createBytes = function(buffer, rsBlocks) {
172
+ let offset = 0;
173
+ let maxDcCount = 0;
174
+ let maxEcCount = 0;
175
+ const dcdata = new Array(rsBlocks.length);
176
+ const ecdata = new Array(rsBlocks.length);
177
+ for (let r = 0; r < rsBlocks.length; r += 1) {
178
+ const dcCount = rsBlocks[r].dataCount;
179
+ const ecCount = rsBlocks[r].totalCount - dcCount;
180
+ maxDcCount = Math.max(maxDcCount, dcCount);
181
+ maxEcCount = Math.max(maxEcCount, ecCount);
182
+ dcdata[r] = new Array(dcCount);
183
+ for (let i = 0; i < dcdata[r].length; i += 1) {
184
+ dcdata[r][i] = 255 & buffer.getBuffer()[i + offset];
185
+ }
186
+ offset += dcCount;
187
+ const rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
188
+ const rawPoly = qrPolynomial(dcdata[r], rsPoly.getLength() - 1);
189
+ const modPoly = rawPoly.mod(rsPoly);
190
+ ecdata[r] = new Array(rsPoly.getLength() - 1);
191
+ for (let i = 0; i < ecdata[r].length; i += 1) {
192
+ const modIndex = i + modPoly.getLength() - ecdata[r].length;
193
+ ecdata[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
194
+ }
195
+ }
196
+ let totalCodeCount = 0;
197
+ for (let i = 0; i < rsBlocks.length; i += 1) {
198
+ totalCodeCount += rsBlocks[i].totalCount;
199
+ }
200
+ const data = new Array(totalCodeCount);
201
+ let index = 0;
202
+ for (let i = 0; i < maxDcCount; i += 1) {
203
+ for (let r = 0; r < rsBlocks.length; r += 1) {
204
+ if (i < dcdata[r].length) {
205
+ data[index] = dcdata[r][i];
206
+ index += 1;
207
+ }
208
+ }
209
+ }
210
+ for (let i = 0; i < maxEcCount; i += 1) {
211
+ for (let r = 0; r < rsBlocks.length; r += 1) {
212
+ if (i < ecdata[r].length) {
213
+ data[index] = ecdata[r][i];
214
+ index += 1;
215
+ }
216
+ }
217
+ }
218
+ return data;
219
+ };
220
+ const createData = function(typeNumber2, errorCorrectionLevel2, dataList) {
221
+ const rsBlocks = QRRSBlock.getRSBlocks(typeNumber2, errorCorrectionLevel2);
222
+ const buffer = qrBitBuffer();
223
+ for (let i = 0; i < dataList.length; i += 1) {
224
+ const data = dataList[i];
225
+ buffer.put(data.getMode(), 4);
226
+ buffer.put(data.getLength(), QRUtil.getLengthInBits(data.getMode(), typeNumber2));
227
+ data.write(buffer);
228
+ }
229
+ let totalDataCount = 0;
230
+ for (let i = 0; i < rsBlocks.length; i += 1) {
231
+ totalDataCount += rsBlocks[i].dataCount;
232
+ }
233
+ if (buffer.getLengthInBits() > totalDataCount * 8) {
234
+ throw "code length overflow. (" + buffer.getLengthInBits() + ">" + totalDataCount * 8 + ")";
235
+ }
236
+ if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
237
+ buffer.put(0, 4);
238
+ }
239
+ while (buffer.getLengthInBits() % 8 != 0) {
240
+ buffer.putBit(false);
241
+ }
242
+ while (true) {
243
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
244
+ break;
245
+ }
246
+ buffer.put(PAD0, 8);
247
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
248
+ break;
249
+ }
250
+ buffer.put(PAD1, 8);
251
+ }
252
+ return createBytes(buffer, rsBlocks);
253
+ };
254
+ _this.addData = function(data, mode) {
255
+ mode = mode || "Byte";
256
+ let newData = null;
257
+ switch (mode) {
258
+ case "Numeric":
259
+ newData = qrNumber(data);
260
+ break;
261
+ case "Alphanumeric":
262
+ newData = qrAlphaNum(data);
263
+ break;
264
+ case "Byte":
265
+ newData = qr8BitByte(data);
266
+ break;
267
+ case "Kanji":
268
+ newData = qrKanji(data);
269
+ break;
270
+ default:
271
+ throw "mode:" + mode;
272
+ }
273
+ _dataList.push(newData);
274
+ _dataCache = null;
275
+ };
276
+ _this.isDark = function(row, col) {
277
+ if (row < 0 || _moduleCount <= row || col < 0 || _moduleCount <= col) {
278
+ throw row + "," + col;
279
+ }
280
+ return _modules[row][col];
281
+ };
282
+ _this.getModuleCount = function() {
283
+ return _moduleCount;
284
+ };
285
+ _this.make = function() {
286
+ if (_typeNumber < 1) {
287
+ let typeNumber2 = 1;
288
+ for (; typeNumber2 < 40; typeNumber2++) {
289
+ const rsBlocks = QRRSBlock.getRSBlocks(typeNumber2, _errorCorrectionLevel);
290
+ const buffer = qrBitBuffer();
291
+ for (let i = 0; i < _dataList.length; i++) {
292
+ const data = _dataList[i];
293
+ buffer.put(data.getMode(), 4);
294
+ buffer.put(data.getLength(), QRUtil.getLengthInBits(data.getMode(), typeNumber2));
295
+ data.write(buffer);
296
+ }
297
+ let totalDataCount = 0;
298
+ for (let i = 0; i < rsBlocks.length; i++) {
299
+ totalDataCount += rsBlocks[i].dataCount;
300
+ }
301
+ if (buffer.getLengthInBits() <= totalDataCount * 8) {
302
+ break;
303
+ }
304
+ }
305
+ _typeNumber = typeNumber2;
306
+ }
307
+ makeImpl(false, getBestMaskPattern());
308
+ };
309
+ _this.createTableTag = function(cellSize, margin) {
310
+ cellSize = cellSize || 2;
311
+ margin = typeof margin == "undefined" ? cellSize * 4 : margin;
312
+ let qrHtml = "";
313
+ qrHtml += '<table style="';
314
+ qrHtml += " border-width: 0px; border-style: none;";
315
+ qrHtml += " border-collapse: collapse;";
316
+ qrHtml += " padding: 0px; margin: " + margin + "px;";
317
+ qrHtml += '">';
318
+ qrHtml += "<tbody>";
319
+ for (let r = 0; r < _this.getModuleCount(); r += 1) {
320
+ qrHtml += "<tr>";
321
+ for (let c = 0; c < _this.getModuleCount(); c += 1) {
322
+ qrHtml += '<td style="';
323
+ qrHtml += " border-width: 0px; border-style: none;";
324
+ qrHtml += " border-collapse: collapse;";
325
+ qrHtml += " padding: 0px; margin: 0px;";
326
+ qrHtml += " width: " + cellSize + "px;";
327
+ qrHtml += " height: " + cellSize + "px;";
328
+ qrHtml += " background-color: ";
329
+ qrHtml += _this.isDark(r, c) ? "#000000" : "#ffffff";
330
+ qrHtml += ";";
331
+ qrHtml += '"/>';
332
+ }
333
+ qrHtml += "</tr>";
334
+ }
335
+ qrHtml += "</tbody>";
336
+ qrHtml += "</table>";
337
+ return qrHtml;
338
+ };
339
+ _this.createSvgTag = function(cellSize, margin, alt, title) {
340
+ let opts = {};
341
+ if (typeof arguments[0] == "object") {
342
+ opts = arguments[0];
343
+ cellSize = opts.cellSize;
344
+ margin = opts.margin;
345
+ alt = opts.alt;
346
+ title = opts.title;
347
+ }
348
+ cellSize = cellSize || 2;
349
+ margin = typeof margin == "undefined" ? cellSize * 4 : margin;
350
+ alt = typeof alt === "string" ? { text: alt } : alt || {};
351
+ alt.text = alt.text || null;
352
+ alt.id = alt.text ? alt.id || "qrcode-description" : null;
353
+ title = typeof title === "string" ? { text: title } : title || {};
354
+ title.text = title.text || null;
355
+ title.id = title.text ? title.id || "qrcode-title" : null;
356
+ const size = _this.getModuleCount() * cellSize + margin * 2;
357
+ let c, mc, r, mr, qrSvg = "", rect;
358
+ rect = "l" + cellSize + ",0 0," + cellSize + " -" + cellSize + ",0 0,-" + cellSize + "z ";
359
+ qrSvg += '<svg version="1.1" xmlns="http://www.w3.org/2000/svg"';
360
+ qrSvg += !opts.scalable ? ' width="' + size + 'px" height="' + size + 'px"' : "";
361
+ qrSvg += ' viewBox="0 0 ' + size + " " + size + '" ';
362
+ qrSvg += ' preserveAspectRatio="xMinYMin meet"';
363
+ qrSvg += title.text || alt.text ? ' role="img" aria-labelledby="' + escapeXml([title.id, alt.id].join(" ").trim()) + '"' : "";
364
+ qrSvg += ">";
365
+ qrSvg += title.text ? '<title id="' + escapeXml(title.id) + '">' + escapeXml(title.text) + "</title>" : "";
366
+ qrSvg += alt.text ? '<description id="' + escapeXml(alt.id) + '">' + escapeXml(alt.text) + "</description>" : "";
367
+ qrSvg += '<rect width="100%" height="100%" fill="white" cx="0" cy="0"/>';
368
+ qrSvg += '<path d="';
369
+ for (r = 0; r < _this.getModuleCount(); r += 1) {
370
+ mr = r * cellSize + margin;
371
+ for (c = 0; c < _this.getModuleCount(); c += 1) {
372
+ if (_this.isDark(r, c)) {
373
+ mc = c * cellSize + margin;
374
+ qrSvg += "M" + mc + "," + mr + rect;
375
+ }
376
+ }
377
+ }
378
+ qrSvg += '" stroke="transparent" fill="black"/>';
379
+ qrSvg += "</svg>";
380
+ return qrSvg;
381
+ };
382
+ _this.createDataURL = function(cellSize, margin) {
383
+ cellSize = cellSize || 2;
384
+ margin = typeof margin == "undefined" ? cellSize * 4 : margin;
385
+ const size = _this.getModuleCount() * cellSize + margin * 2;
386
+ const min = margin;
387
+ const max = size - margin;
388
+ return createDataURL(size, size, function(x, y) {
389
+ if (min <= x && x < max && min <= y && y < max) {
390
+ const c = Math.floor((x - min) / cellSize);
391
+ const r = Math.floor((y - min) / cellSize);
392
+ return _this.isDark(r, c) ? 0 : 1;
393
+ } else {
394
+ return 1;
395
+ }
396
+ });
397
+ };
398
+ _this.createImgTag = function(cellSize, margin, alt) {
399
+ cellSize = cellSize || 2;
400
+ margin = typeof margin == "undefined" ? cellSize * 4 : margin;
401
+ const size = _this.getModuleCount() * cellSize + margin * 2;
402
+ let img = "";
403
+ img += "<img";
404
+ img += ' src="';
405
+ img += _this.createDataURL(cellSize, margin);
406
+ img += '"';
407
+ img += ' width="';
408
+ img += size;
409
+ img += '"';
410
+ img += ' height="';
411
+ img += size;
412
+ img += '"';
413
+ if (alt) {
414
+ img += ' alt="';
415
+ img += escapeXml(alt);
416
+ img += '"';
417
+ }
418
+ img += "/>";
419
+ return img;
420
+ };
421
+ const escapeXml = function(s) {
422
+ let escaped = "";
423
+ for (let i = 0; i < s.length; i += 1) {
424
+ const c = s.charAt(i);
425
+ switch (c) {
426
+ case "<":
427
+ escaped += "&lt;";
428
+ break;
429
+ case ">":
430
+ escaped += "&gt;";
431
+ break;
432
+ case "&":
433
+ escaped += "&amp;";
434
+ break;
435
+ case '"':
436
+ escaped += "&quot;";
437
+ break;
438
+ default:
439
+ escaped += c;
440
+ break;
441
+ }
442
+ }
443
+ return escaped;
444
+ };
445
+ const _createHalfASCII = function(margin) {
446
+ const cellSize = 1;
447
+ margin = typeof margin == "undefined" ? cellSize * 2 : margin;
448
+ const size = _this.getModuleCount() * cellSize + margin * 2;
449
+ const min = margin;
450
+ const max = size - margin;
451
+ let y, x, r1, r2, p;
452
+ const blocks = {
453
+ "\u2588\u2588": "\u2588",
454
+ "\u2588 ": "\u2580",
455
+ " \u2588": "\u2584",
456
+ " ": " "
457
+ };
458
+ const blocksLastLineNoMargin = {
459
+ "\u2588\u2588": "\u2580",
460
+ "\u2588 ": "\u2580",
461
+ " \u2588": " ",
462
+ " ": " "
463
+ };
464
+ let ascii = "";
465
+ for (y = 0; y < size; y += 2) {
466
+ r1 = Math.floor((y - min) / cellSize);
467
+ r2 = Math.floor((y + 1 - min) / cellSize);
468
+ for (x = 0; x < size; x += 1) {
469
+ p = "\u2588";
470
+ if (min <= x && x < max && min <= y && y < max && _this.isDark(r1, Math.floor((x - min) / cellSize))) {
471
+ p = " ";
472
+ }
473
+ if (min <= x && x < max && min <= y + 1 && y + 1 < max && _this.isDark(r2, Math.floor((x - min) / cellSize))) {
474
+ p += " ";
475
+ } else {
476
+ p += "\u2588";
477
+ }
478
+ ascii += margin < 1 && y + 1 >= max ? blocksLastLineNoMargin[p] : blocks[p];
479
+ }
480
+ ascii += "\n";
481
+ }
482
+ if (size % 2 && margin > 0) {
483
+ return ascii.substring(0, ascii.length - size - 1) + Array(size + 1).join("\u2580");
484
+ }
485
+ return ascii.substring(0, ascii.length - 1);
486
+ };
487
+ _this.createASCII = function(cellSize, margin) {
488
+ cellSize = cellSize || 1;
489
+ if (cellSize < 2) {
490
+ return _createHalfASCII(margin);
491
+ }
492
+ cellSize -= 1;
493
+ margin = typeof margin == "undefined" ? cellSize * 2 : margin;
494
+ const size = _this.getModuleCount() * cellSize + margin * 2;
495
+ const min = margin;
496
+ const max = size - margin;
497
+ let y, x, r, p;
498
+ const white = Array(cellSize + 1).join("\u2588\u2588");
499
+ const black = Array(cellSize + 1).join(" ");
500
+ let ascii = "";
501
+ let line = "";
502
+ for (y = 0; y < size; y += 1) {
503
+ r = Math.floor((y - min) / cellSize);
504
+ line = "";
505
+ for (x = 0; x < size; x += 1) {
506
+ p = 1;
507
+ if (min <= x && x < max && min <= y && y < max && _this.isDark(r, Math.floor((x - min) / cellSize))) {
508
+ p = 0;
509
+ }
510
+ line += p ? white : black;
511
+ }
512
+ for (r = 0; r < cellSize; r += 1) {
513
+ ascii += line + "\n";
514
+ }
515
+ }
516
+ return ascii.substring(0, ascii.length - 1);
517
+ };
518
+ _this.renderTo2dContext = function(context, cellSize) {
519
+ cellSize = cellSize || 2;
520
+ const length = _this.getModuleCount();
521
+ for (let row = 0; row < length; row++) {
522
+ for (let col = 0; col < length; col++) {
523
+ context.fillStyle = _this.isDark(row, col) ? "black" : "white";
524
+ context.fillRect(col * cellSize, row * cellSize, cellSize, cellSize);
525
+ }
526
+ }
527
+ };
528
+ return _this;
529
+ };
530
+ qrcode.stringToBytes = function(s) {
531
+ const bytes = [];
532
+ for (let i = 0; i < s.length; i += 1) {
533
+ const c = s.charCodeAt(i);
534
+ bytes.push(c & 255);
535
+ }
536
+ return bytes;
537
+ };
538
+ qrcode.createStringToBytes = function(unicodeData, numChars) {
539
+ const unicodeMap = (function() {
540
+ const bin = base64DecodeInputStream(unicodeData);
541
+ const read = function() {
542
+ const b = bin.read();
543
+ if (b == -1) throw "eof";
544
+ return b;
545
+ };
546
+ let count = 0;
547
+ const unicodeMap2 = {};
548
+ while (true) {
549
+ const b0 = bin.read();
550
+ if (b0 == -1) break;
551
+ const b1 = read();
552
+ const b2 = read();
553
+ const b3 = read();
554
+ const k = String.fromCharCode(b0 << 8 | b1);
555
+ const v = b2 << 8 | b3;
556
+ unicodeMap2[k] = v;
557
+ count += 1;
558
+ }
559
+ if (count != numChars) {
560
+ throw count + " != " + numChars;
561
+ }
562
+ return unicodeMap2;
563
+ })();
564
+ const unknownChar = "?".charCodeAt(0);
565
+ return function(s) {
566
+ const bytes = [];
567
+ for (let i = 0; i < s.length; i += 1) {
568
+ const c = s.charCodeAt(i);
569
+ if (c < 128) {
570
+ bytes.push(c);
571
+ } else {
572
+ const b = unicodeMap[s.charAt(i)];
573
+ if (typeof b == "number") {
574
+ if ((b & 255) == b) {
575
+ bytes.push(b);
576
+ } else {
577
+ bytes.push(b >>> 8);
578
+ bytes.push(b & 255);
579
+ }
580
+ } else {
581
+ bytes.push(unknownChar);
582
+ }
583
+ }
584
+ }
585
+ return bytes;
586
+ };
587
+ };
588
+ var QRMode = {
589
+ MODE_NUMBER: 1 << 0,
590
+ MODE_ALPHA_NUM: 1 << 1,
591
+ MODE_8BIT_BYTE: 1 << 2,
592
+ MODE_KANJI: 1 << 3
593
+ };
594
+ var QRErrorCorrectionLevel = {
595
+ L: 1,
596
+ M: 0,
597
+ Q: 3,
598
+ H: 2
599
+ };
600
+ var QRMaskPattern = {
601
+ PATTERN000: 0,
602
+ PATTERN001: 1,
603
+ PATTERN010: 2,
604
+ PATTERN011: 3,
605
+ PATTERN100: 4,
606
+ PATTERN101: 5,
607
+ PATTERN110: 6,
608
+ PATTERN111: 7
609
+ };
610
+ var QRUtil = (function() {
611
+ const PATTERN_POSITION_TABLE = [
612
+ [],
613
+ [6, 18],
614
+ [6, 22],
615
+ [6, 26],
616
+ [6, 30],
617
+ [6, 34],
618
+ [6, 22, 38],
619
+ [6, 24, 42],
620
+ [6, 26, 46],
621
+ [6, 28, 50],
622
+ [6, 30, 54],
623
+ [6, 32, 58],
624
+ [6, 34, 62],
625
+ [6, 26, 46, 66],
626
+ [6, 26, 48, 70],
627
+ [6, 26, 50, 74],
628
+ [6, 30, 54, 78],
629
+ [6, 30, 56, 82],
630
+ [6, 30, 58, 86],
631
+ [6, 34, 62, 90],
632
+ [6, 28, 50, 72, 94],
633
+ [6, 26, 50, 74, 98],
634
+ [6, 30, 54, 78, 102],
635
+ [6, 28, 54, 80, 106],
636
+ [6, 32, 58, 84, 110],
637
+ [6, 30, 58, 86, 114],
638
+ [6, 34, 62, 90, 118],
639
+ [6, 26, 50, 74, 98, 122],
640
+ [6, 30, 54, 78, 102, 126],
641
+ [6, 26, 52, 78, 104, 130],
642
+ [6, 30, 56, 82, 108, 134],
643
+ [6, 34, 60, 86, 112, 138],
644
+ [6, 30, 58, 86, 114, 142],
645
+ [6, 34, 62, 90, 118, 146],
646
+ [6, 30, 54, 78, 102, 126, 150],
647
+ [6, 24, 50, 76, 102, 128, 154],
648
+ [6, 28, 54, 80, 106, 132, 158],
649
+ [6, 32, 58, 84, 110, 136, 162],
650
+ [6, 26, 54, 82, 110, 138, 166],
651
+ [6, 30, 58, 86, 114, 142, 170]
652
+ ];
653
+ const G15 = 1 << 10 | 1 << 8 | 1 << 5 | 1 << 4 | 1 << 2 | 1 << 1 | 1 << 0;
654
+ const G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0;
655
+ const G15_MASK = 1 << 14 | 1 << 12 | 1 << 10 | 1 << 4 | 1 << 1;
656
+ const _this = {};
657
+ const getBCHDigit = function(data) {
658
+ let digit = 0;
659
+ while (data != 0) {
660
+ digit += 1;
661
+ data >>>= 1;
662
+ }
663
+ return digit;
664
+ };
665
+ _this.getBCHTypeInfo = function(data) {
666
+ let d = data << 10;
667
+ while (getBCHDigit(d) - getBCHDigit(G15) >= 0) {
668
+ d ^= G15 << getBCHDigit(d) - getBCHDigit(G15);
669
+ }
670
+ return (data << 10 | d) ^ G15_MASK;
671
+ };
672
+ _this.getBCHTypeNumber = function(data) {
673
+ let d = data << 12;
674
+ while (getBCHDigit(d) - getBCHDigit(G18) >= 0) {
675
+ d ^= G18 << getBCHDigit(d) - getBCHDigit(G18);
676
+ }
677
+ return data << 12 | d;
678
+ };
679
+ _this.getPatternPosition = function(typeNumber) {
680
+ return PATTERN_POSITION_TABLE[typeNumber - 1];
681
+ };
682
+ _this.getMaskFunction = function(maskPattern) {
683
+ switch (maskPattern) {
684
+ case QRMaskPattern.PATTERN000:
685
+ return function(i, j) {
686
+ return (i + j) % 2 == 0;
687
+ };
688
+ case QRMaskPattern.PATTERN001:
689
+ return function(i, j) {
690
+ return i % 2 == 0;
691
+ };
692
+ case QRMaskPattern.PATTERN010:
693
+ return function(i, j) {
694
+ return j % 3 == 0;
695
+ };
696
+ case QRMaskPattern.PATTERN011:
697
+ return function(i, j) {
698
+ return (i + j) % 3 == 0;
699
+ };
700
+ case QRMaskPattern.PATTERN100:
701
+ return function(i, j) {
702
+ return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
703
+ };
704
+ case QRMaskPattern.PATTERN101:
705
+ return function(i, j) {
706
+ return i * j % 2 + i * j % 3 == 0;
707
+ };
708
+ case QRMaskPattern.PATTERN110:
709
+ return function(i, j) {
710
+ return (i * j % 2 + i * j % 3) % 2 == 0;
711
+ };
712
+ case QRMaskPattern.PATTERN111:
713
+ return function(i, j) {
714
+ return (i * j % 3 + (i + j) % 2) % 2 == 0;
715
+ };
716
+ default:
717
+ throw "bad maskPattern:" + maskPattern;
718
+ }
719
+ };
720
+ _this.getErrorCorrectPolynomial = function(errorCorrectLength) {
721
+ let a = qrPolynomial([1], 0);
722
+ for (let i = 0; i < errorCorrectLength; i += 1) {
723
+ a = a.multiply(qrPolynomial([1, QRMath.gexp(i)], 0));
724
+ }
725
+ return a;
726
+ };
727
+ _this.getLengthInBits = function(mode, type) {
728
+ if (1 <= type && type < 10) {
729
+ switch (mode) {
730
+ case QRMode.MODE_NUMBER:
731
+ return 10;
732
+ case QRMode.MODE_ALPHA_NUM:
733
+ return 9;
734
+ case QRMode.MODE_8BIT_BYTE:
735
+ return 8;
736
+ case QRMode.MODE_KANJI:
737
+ return 8;
738
+ default:
739
+ throw "mode:" + mode;
740
+ }
741
+ } else if (type < 27) {
742
+ switch (mode) {
743
+ case QRMode.MODE_NUMBER:
744
+ return 12;
745
+ case QRMode.MODE_ALPHA_NUM:
746
+ return 11;
747
+ case QRMode.MODE_8BIT_BYTE:
748
+ return 16;
749
+ case QRMode.MODE_KANJI:
750
+ return 10;
751
+ default:
752
+ throw "mode:" + mode;
753
+ }
754
+ } else if (type < 41) {
755
+ switch (mode) {
756
+ case QRMode.MODE_NUMBER:
757
+ return 14;
758
+ case QRMode.MODE_ALPHA_NUM:
759
+ return 13;
760
+ case QRMode.MODE_8BIT_BYTE:
761
+ return 16;
762
+ case QRMode.MODE_KANJI:
763
+ return 12;
764
+ default:
765
+ throw "mode:" + mode;
766
+ }
767
+ } else {
768
+ throw "type:" + type;
769
+ }
770
+ };
771
+ _this.getLostPoint = function(qrcode2) {
772
+ const moduleCount = qrcode2.getModuleCount();
773
+ let lostPoint = 0;
774
+ for (let row = 0; row < moduleCount; row += 1) {
775
+ for (let col = 0; col < moduleCount; col += 1) {
776
+ let sameCount = 0;
777
+ const dark = qrcode2.isDark(row, col);
778
+ for (let r = -1; r <= 1; r += 1) {
779
+ if (row + r < 0 || moduleCount <= row + r) {
780
+ continue;
781
+ }
782
+ for (let c = -1; c <= 1; c += 1) {
783
+ if (col + c < 0 || moduleCount <= col + c) {
784
+ continue;
785
+ }
786
+ if (r == 0 && c == 0) {
787
+ continue;
788
+ }
789
+ if (dark == qrcode2.isDark(row + r, col + c)) {
790
+ sameCount += 1;
791
+ }
792
+ }
793
+ }
794
+ if (sameCount > 5) {
795
+ lostPoint += 3 + sameCount - 5;
796
+ }
797
+ }
798
+ }
799
+ ;
800
+ for (let row = 0; row < moduleCount - 1; row += 1) {
801
+ for (let col = 0; col < moduleCount - 1; col += 1) {
802
+ let count = 0;
803
+ if (qrcode2.isDark(row, col)) count += 1;
804
+ if (qrcode2.isDark(row + 1, col)) count += 1;
805
+ if (qrcode2.isDark(row, col + 1)) count += 1;
806
+ if (qrcode2.isDark(row + 1, col + 1)) count += 1;
807
+ if (count == 0 || count == 4) {
808
+ lostPoint += 3;
809
+ }
810
+ }
811
+ }
812
+ for (let row = 0; row < moduleCount; row += 1) {
813
+ for (let col = 0; col < moduleCount - 6; col += 1) {
814
+ if (qrcode2.isDark(row, col) && !qrcode2.isDark(row, col + 1) && qrcode2.isDark(row, col + 2) && qrcode2.isDark(row, col + 3) && qrcode2.isDark(row, col + 4) && !qrcode2.isDark(row, col + 5) && qrcode2.isDark(row, col + 6)) {
815
+ lostPoint += 40;
816
+ }
817
+ }
818
+ }
819
+ for (let col = 0; col < moduleCount; col += 1) {
820
+ for (let row = 0; row < moduleCount - 6; row += 1) {
821
+ if (qrcode2.isDark(row, col) && !qrcode2.isDark(row + 1, col) && qrcode2.isDark(row + 2, col) && qrcode2.isDark(row + 3, col) && qrcode2.isDark(row + 4, col) && !qrcode2.isDark(row + 5, col) && qrcode2.isDark(row + 6, col)) {
822
+ lostPoint += 40;
823
+ }
824
+ }
825
+ }
826
+ let darkCount = 0;
827
+ for (let col = 0; col < moduleCount; col += 1) {
828
+ for (let row = 0; row < moduleCount; row += 1) {
829
+ if (qrcode2.isDark(row, col)) {
830
+ darkCount += 1;
831
+ }
832
+ }
833
+ }
834
+ const ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
835
+ lostPoint += ratio * 10;
836
+ return lostPoint;
837
+ };
838
+ return _this;
839
+ })();
840
+ var QRMath = (function() {
841
+ const EXP_TABLE = new Array(256);
842
+ const LOG_TABLE = new Array(256);
843
+ for (let i = 0; i < 8; i += 1) {
844
+ EXP_TABLE[i] = 1 << i;
845
+ }
846
+ for (let i = 8; i < 256; i += 1) {
847
+ EXP_TABLE[i] = EXP_TABLE[i - 4] ^ EXP_TABLE[i - 5] ^ EXP_TABLE[i - 6] ^ EXP_TABLE[i - 8];
848
+ }
849
+ for (let i = 0; i < 255; i += 1) {
850
+ LOG_TABLE[EXP_TABLE[i]] = i;
851
+ }
852
+ const _this = {};
853
+ _this.glog = function(n) {
854
+ if (n < 1) {
855
+ throw "glog(" + n + ")";
856
+ }
857
+ return LOG_TABLE[n];
858
+ };
859
+ _this.gexp = function(n) {
860
+ while (n < 0) {
861
+ n += 255;
862
+ }
863
+ while (n >= 256) {
864
+ n -= 255;
865
+ }
866
+ return EXP_TABLE[n];
867
+ };
868
+ return _this;
869
+ })();
870
+ var qrPolynomial = function(num, shift) {
871
+ if (typeof num.length == "undefined") {
872
+ throw num.length + "/" + shift;
873
+ }
874
+ const _num = (function() {
875
+ let offset = 0;
876
+ while (offset < num.length && num[offset] == 0) {
877
+ offset += 1;
878
+ }
879
+ const _num2 = new Array(num.length - offset + shift);
880
+ for (let i = 0; i < num.length - offset; i += 1) {
881
+ _num2[i] = num[i + offset];
882
+ }
883
+ return _num2;
884
+ })();
885
+ const _this = {};
886
+ _this.getAt = function(index) {
887
+ return _num[index];
888
+ };
889
+ _this.getLength = function() {
890
+ return _num.length;
891
+ };
892
+ _this.multiply = function(e) {
893
+ const num2 = new Array(_this.getLength() + e.getLength() - 1);
894
+ for (let i = 0; i < _this.getLength(); i += 1) {
895
+ for (let j = 0; j < e.getLength(); j += 1) {
896
+ num2[i + j] ^= QRMath.gexp(QRMath.glog(_this.getAt(i)) + QRMath.glog(e.getAt(j)));
897
+ }
898
+ }
899
+ return qrPolynomial(num2, 0);
900
+ };
901
+ _this.mod = function(e) {
902
+ if (_this.getLength() - e.getLength() < 0) {
903
+ return _this;
904
+ }
905
+ const ratio = QRMath.glog(_this.getAt(0)) - QRMath.glog(e.getAt(0));
906
+ const num2 = new Array(_this.getLength());
907
+ for (let i = 0; i < _this.getLength(); i += 1) {
908
+ num2[i] = _this.getAt(i);
909
+ }
910
+ for (let i = 0; i < e.getLength(); i += 1) {
911
+ num2[i] ^= QRMath.gexp(QRMath.glog(e.getAt(i)) + ratio);
912
+ }
913
+ return qrPolynomial(num2, 0).mod(e);
914
+ };
915
+ return _this;
916
+ };
917
+ var QRRSBlock = (function() {
918
+ const RS_BLOCK_TABLE = [
919
+ // L
920
+ // M
921
+ // Q
922
+ // H
923
+ // 1
924
+ [1, 26, 19],
925
+ [1, 26, 16],
926
+ [1, 26, 13],
927
+ [1, 26, 9],
928
+ // 2
929
+ [1, 44, 34],
930
+ [1, 44, 28],
931
+ [1, 44, 22],
932
+ [1, 44, 16],
933
+ // 3
934
+ [1, 70, 55],
935
+ [1, 70, 44],
936
+ [2, 35, 17],
937
+ [2, 35, 13],
938
+ // 4
939
+ [1, 100, 80],
940
+ [2, 50, 32],
941
+ [2, 50, 24],
942
+ [4, 25, 9],
943
+ // 5
944
+ [1, 134, 108],
945
+ [2, 67, 43],
946
+ [2, 33, 15, 2, 34, 16],
947
+ [2, 33, 11, 2, 34, 12],
948
+ // 6
949
+ [2, 86, 68],
950
+ [4, 43, 27],
951
+ [4, 43, 19],
952
+ [4, 43, 15],
953
+ // 7
954
+ [2, 98, 78],
955
+ [4, 49, 31],
956
+ [2, 32, 14, 4, 33, 15],
957
+ [4, 39, 13, 1, 40, 14],
958
+ // 8
959
+ [2, 121, 97],
960
+ [2, 60, 38, 2, 61, 39],
961
+ [4, 40, 18, 2, 41, 19],
962
+ [4, 40, 14, 2, 41, 15],
963
+ // 9
964
+ [2, 146, 116],
965
+ [3, 58, 36, 2, 59, 37],
966
+ [4, 36, 16, 4, 37, 17],
967
+ [4, 36, 12, 4, 37, 13],
968
+ // 10
969
+ [2, 86, 68, 2, 87, 69],
970
+ [4, 69, 43, 1, 70, 44],
971
+ [6, 43, 19, 2, 44, 20],
972
+ [6, 43, 15, 2, 44, 16],
973
+ // 11
974
+ [4, 101, 81],
975
+ [1, 80, 50, 4, 81, 51],
976
+ [4, 50, 22, 4, 51, 23],
977
+ [3, 36, 12, 8, 37, 13],
978
+ // 12
979
+ [2, 116, 92, 2, 117, 93],
980
+ [6, 58, 36, 2, 59, 37],
981
+ [4, 46, 20, 6, 47, 21],
982
+ [7, 42, 14, 4, 43, 15],
983
+ // 13
984
+ [4, 133, 107],
985
+ [8, 59, 37, 1, 60, 38],
986
+ [8, 44, 20, 4, 45, 21],
987
+ [12, 33, 11, 4, 34, 12],
988
+ // 14
989
+ [3, 145, 115, 1, 146, 116],
990
+ [4, 64, 40, 5, 65, 41],
991
+ [11, 36, 16, 5, 37, 17],
992
+ [11, 36, 12, 5, 37, 13],
993
+ // 15
994
+ [5, 109, 87, 1, 110, 88],
995
+ [5, 65, 41, 5, 66, 42],
996
+ [5, 54, 24, 7, 55, 25],
997
+ [11, 36, 12, 7, 37, 13],
998
+ // 16
999
+ [5, 122, 98, 1, 123, 99],
1000
+ [7, 73, 45, 3, 74, 46],
1001
+ [15, 43, 19, 2, 44, 20],
1002
+ [3, 45, 15, 13, 46, 16],
1003
+ // 17
1004
+ [1, 135, 107, 5, 136, 108],
1005
+ [10, 74, 46, 1, 75, 47],
1006
+ [1, 50, 22, 15, 51, 23],
1007
+ [2, 42, 14, 17, 43, 15],
1008
+ // 18
1009
+ [5, 150, 120, 1, 151, 121],
1010
+ [9, 69, 43, 4, 70, 44],
1011
+ [17, 50, 22, 1, 51, 23],
1012
+ [2, 42, 14, 19, 43, 15],
1013
+ // 19
1014
+ [3, 141, 113, 4, 142, 114],
1015
+ [3, 70, 44, 11, 71, 45],
1016
+ [17, 47, 21, 4, 48, 22],
1017
+ [9, 39, 13, 16, 40, 14],
1018
+ // 20
1019
+ [3, 135, 107, 5, 136, 108],
1020
+ [3, 67, 41, 13, 68, 42],
1021
+ [15, 54, 24, 5, 55, 25],
1022
+ [15, 43, 15, 10, 44, 16],
1023
+ // 21
1024
+ [4, 144, 116, 4, 145, 117],
1025
+ [17, 68, 42],
1026
+ [17, 50, 22, 6, 51, 23],
1027
+ [19, 46, 16, 6, 47, 17],
1028
+ // 22
1029
+ [2, 139, 111, 7, 140, 112],
1030
+ [17, 74, 46],
1031
+ [7, 54, 24, 16, 55, 25],
1032
+ [34, 37, 13],
1033
+ // 23
1034
+ [4, 151, 121, 5, 152, 122],
1035
+ [4, 75, 47, 14, 76, 48],
1036
+ [11, 54, 24, 14, 55, 25],
1037
+ [16, 45, 15, 14, 46, 16],
1038
+ // 24
1039
+ [6, 147, 117, 4, 148, 118],
1040
+ [6, 73, 45, 14, 74, 46],
1041
+ [11, 54, 24, 16, 55, 25],
1042
+ [30, 46, 16, 2, 47, 17],
1043
+ // 25
1044
+ [8, 132, 106, 4, 133, 107],
1045
+ [8, 75, 47, 13, 76, 48],
1046
+ [7, 54, 24, 22, 55, 25],
1047
+ [22, 45, 15, 13, 46, 16],
1048
+ // 26
1049
+ [10, 142, 114, 2, 143, 115],
1050
+ [19, 74, 46, 4, 75, 47],
1051
+ [28, 50, 22, 6, 51, 23],
1052
+ [33, 46, 16, 4, 47, 17],
1053
+ // 27
1054
+ [8, 152, 122, 4, 153, 123],
1055
+ [22, 73, 45, 3, 74, 46],
1056
+ [8, 53, 23, 26, 54, 24],
1057
+ [12, 45, 15, 28, 46, 16],
1058
+ // 28
1059
+ [3, 147, 117, 10, 148, 118],
1060
+ [3, 73, 45, 23, 74, 46],
1061
+ [4, 54, 24, 31, 55, 25],
1062
+ [11, 45, 15, 31, 46, 16],
1063
+ // 29
1064
+ [7, 146, 116, 7, 147, 117],
1065
+ [21, 73, 45, 7, 74, 46],
1066
+ [1, 53, 23, 37, 54, 24],
1067
+ [19, 45, 15, 26, 46, 16],
1068
+ // 30
1069
+ [5, 145, 115, 10, 146, 116],
1070
+ [19, 75, 47, 10, 76, 48],
1071
+ [15, 54, 24, 25, 55, 25],
1072
+ [23, 45, 15, 25, 46, 16],
1073
+ // 31
1074
+ [13, 145, 115, 3, 146, 116],
1075
+ [2, 74, 46, 29, 75, 47],
1076
+ [42, 54, 24, 1, 55, 25],
1077
+ [23, 45, 15, 28, 46, 16],
1078
+ // 32
1079
+ [17, 145, 115],
1080
+ [10, 74, 46, 23, 75, 47],
1081
+ [10, 54, 24, 35, 55, 25],
1082
+ [19, 45, 15, 35, 46, 16],
1083
+ // 33
1084
+ [17, 145, 115, 1, 146, 116],
1085
+ [14, 74, 46, 21, 75, 47],
1086
+ [29, 54, 24, 19, 55, 25],
1087
+ [11, 45, 15, 46, 46, 16],
1088
+ // 34
1089
+ [13, 145, 115, 6, 146, 116],
1090
+ [14, 74, 46, 23, 75, 47],
1091
+ [44, 54, 24, 7, 55, 25],
1092
+ [59, 46, 16, 1, 47, 17],
1093
+ // 35
1094
+ [12, 151, 121, 7, 152, 122],
1095
+ [12, 75, 47, 26, 76, 48],
1096
+ [39, 54, 24, 14, 55, 25],
1097
+ [22, 45, 15, 41, 46, 16],
1098
+ // 36
1099
+ [6, 151, 121, 14, 152, 122],
1100
+ [6, 75, 47, 34, 76, 48],
1101
+ [46, 54, 24, 10, 55, 25],
1102
+ [2, 45, 15, 64, 46, 16],
1103
+ // 37
1104
+ [17, 152, 122, 4, 153, 123],
1105
+ [29, 74, 46, 14, 75, 47],
1106
+ [49, 54, 24, 10, 55, 25],
1107
+ [24, 45, 15, 46, 46, 16],
1108
+ // 38
1109
+ [4, 152, 122, 18, 153, 123],
1110
+ [13, 74, 46, 32, 75, 47],
1111
+ [48, 54, 24, 14, 55, 25],
1112
+ [42, 45, 15, 32, 46, 16],
1113
+ // 39
1114
+ [20, 147, 117, 4, 148, 118],
1115
+ [40, 75, 47, 7, 76, 48],
1116
+ [43, 54, 24, 22, 55, 25],
1117
+ [10, 45, 15, 67, 46, 16],
1118
+ // 40
1119
+ [19, 148, 118, 6, 149, 119],
1120
+ [18, 75, 47, 31, 76, 48],
1121
+ [34, 54, 24, 34, 55, 25],
1122
+ [20, 45, 15, 61, 46, 16]
1123
+ ];
1124
+ const qrRSBlock = function(totalCount, dataCount) {
1125
+ const _this2 = {};
1126
+ _this2.totalCount = totalCount;
1127
+ _this2.dataCount = dataCount;
1128
+ return _this2;
1129
+ };
1130
+ const _this = {};
1131
+ const getRsBlockTable = function(typeNumber, errorCorrectionLevel) {
1132
+ switch (errorCorrectionLevel) {
1133
+ case QRErrorCorrectionLevel.L:
1134
+ return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
1135
+ case QRErrorCorrectionLevel.M:
1136
+ return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
1137
+ case QRErrorCorrectionLevel.Q:
1138
+ return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
1139
+ case QRErrorCorrectionLevel.H:
1140
+ return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
1141
+ default:
1142
+ return void 0;
1143
+ }
1144
+ };
1145
+ _this.getRSBlocks = function(typeNumber, errorCorrectionLevel) {
1146
+ const rsBlock = getRsBlockTable(typeNumber, errorCorrectionLevel);
1147
+ if (typeof rsBlock == "undefined") {
1148
+ throw "bad rs block @ typeNumber:" + typeNumber + "/errorCorrectionLevel:" + errorCorrectionLevel;
1149
+ }
1150
+ const length = rsBlock.length / 3;
1151
+ const list = [];
1152
+ for (let i = 0; i < length; i += 1) {
1153
+ const count = rsBlock[i * 3 + 0];
1154
+ const totalCount = rsBlock[i * 3 + 1];
1155
+ const dataCount = rsBlock[i * 3 + 2];
1156
+ for (let j = 0; j < count; j += 1) {
1157
+ list.push(qrRSBlock(totalCount, dataCount));
1158
+ }
1159
+ }
1160
+ return list;
1161
+ };
1162
+ return _this;
1163
+ })();
1164
+ var qrBitBuffer = function() {
1165
+ const _buffer = [];
1166
+ let _length = 0;
1167
+ const _this = {};
1168
+ _this.getBuffer = function() {
1169
+ return _buffer;
1170
+ };
1171
+ _this.getAt = function(index) {
1172
+ const bufIndex = Math.floor(index / 8);
1173
+ return (_buffer[bufIndex] >>> 7 - index % 8 & 1) == 1;
1174
+ };
1175
+ _this.put = function(num, length) {
1176
+ for (let i = 0; i < length; i += 1) {
1177
+ _this.putBit((num >>> length - i - 1 & 1) == 1);
1178
+ }
1179
+ };
1180
+ _this.getLengthInBits = function() {
1181
+ return _length;
1182
+ };
1183
+ _this.putBit = function(bit) {
1184
+ const bufIndex = Math.floor(_length / 8);
1185
+ if (_buffer.length <= bufIndex) {
1186
+ _buffer.push(0);
1187
+ }
1188
+ if (bit) {
1189
+ _buffer[bufIndex] |= 128 >>> _length % 8;
1190
+ }
1191
+ _length += 1;
1192
+ };
1193
+ return _this;
1194
+ };
1195
+ var qrNumber = function(data) {
1196
+ const _mode = QRMode.MODE_NUMBER;
1197
+ const _data = data;
1198
+ const _this = {};
1199
+ _this.getMode = function() {
1200
+ return _mode;
1201
+ };
1202
+ _this.getLength = function(buffer) {
1203
+ return _data.length;
1204
+ };
1205
+ _this.write = function(buffer) {
1206
+ const data2 = _data;
1207
+ let i = 0;
1208
+ while (i + 2 < data2.length) {
1209
+ buffer.put(strToNum(data2.substring(i, i + 3)), 10);
1210
+ i += 3;
1211
+ }
1212
+ if (i < data2.length) {
1213
+ if (data2.length - i == 1) {
1214
+ buffer.put(strToNum(data2.substring(i, i + 1)), 4);
1215
+ } else if (data2.length - i == 2) {
1216
+ buffer.put(strToNum(data2.substring(i, i + 2)), 7);
1217
+ }
1218
+ }
1219
+ };
1220
+ const strToNum = function(s) {
1221
+ let num = 0;
1222
+ for (let i = 0; i < s.length; i += 1) {
1223
+ num = num * 10 + chatToNum(s.charAt(i));
1224
+ }
1225
+ return num;
1226
+ };
1227
+ const chatToNum = function(c) {
1228
+ if ("0" <= c && c <= "9") {
1229
+ return c.charCodeAt(0) - "0".charCodeAt(0);
1230
+ }
1231
+ throw "illegal char :" + c;
1232
+ };
1233
+ return _this;
1234
+ };
1235
+ var qrAlphaNum = function(data) {
1236
+ const _mode = QRMode.MODE_ALPHA_NUM;
1237
+ const _data = data;
1238
+ const _this = {};
1239
+ _this.getMode = function() {
1240
+ return _mode;
1241
+ };
1242
+ _this.getLength = function(buffer) {
1243
+ return _data.length;
1244
+ };
1245
+ _this.write = function(buffer) {
1246
+ const s = _data;
1247
+ let i = 0;
1248
+ while (i + 1 < s.length) {
1249
+ buffer.put(
1250
+ getCode(s.charAt(i)) * 45 + getCode(s.charAt(i + 1)),
1251
+ 11
1252
+ );
1253
+ i += 2;
1254
+ }
1255
+ if (i < s.length) {
1256
+ buffer.put(getCode(s.charAt(i)), 6);
1257
+ }
1258
+ };
1259
+ const getCode = function(c) {
1260
+ if ("0" <= c && c <= "9") {
1261
+ return c.charCodeAt(0) - "0".charCodeAt(0);
1262
+ } else if ("A" <= c && c <= "Z") {
1263
+ return c.charCodeAt(0) - "A".charCodeAt(0) + 10;
1264
+ } else {
1265
+ switch (c) {
1266
+ case " ":
1267
+ return 36;
1268
+ case "$":
1269
+ return 37;
1270
+ case "%":
1271
+ return 38;
1272
+ case "*":
1273
+ return 39;
1274
+ case "+":
1275
+ return 40;
1276
+ case "-":
1277
+ return 41;
1278
+ case ".":
1279
+ return 42;
1280
+ case "/":
1281
+ return 43;
1282
+ case ":":
1283
+ return 44;
1284
+ default:
1285
+ throw "illegal char :" + c;
1286
+ }
1287
+ }
1288
+ };
1289
+ return _this;
1290
+ };
1291
+ var qr8BitByte = function(data) {
1292
+ const _mode = QRMode.MODE_8BIT_BYTE;
1293
+ const _data = data;
1294
+ const _bytes = qrcode.stringToBytes(data);
1295
+ const _this = {};
1296
+ _this.getMode = function() {
1297
+ return _mode;
1298
+ };
1299
+ _this.getLength = function(buffer) {
1300
+ return _bytes.length;
1301
+ };
1302
+ _this.write = function(buffer) {
1303
+ for (let i = 0; i < _bytes.length; i += 1) {
1304
+ buffer.put(_bytes[i], 8);
1305
+ }
1306
+ };
1307
+ return _this;
1308
+ };
1309
+ var qrKanji = function(data) {
1310
+ const _mode = QRMode.MODE_KANJI;
1311
+ const _data = data;
1312
+ const stringToBytes2 = qrcode.stringToBytes;
1313
+ !(function(c, code) {
1314
+ const test = stringToBytes2(c);
1315
+ if (test.length != 2 || (test[0] << 8 | test[1]) != code) {
1316
+ throw "sjis not supported.";
1317
+ }
1318
+ })("\u53CB", 38726);
1319
+ const _bytes = stringToBytes2(data);
1320
+ const _this = {};
1321
+ _this.getMode = function() {
1322
+ return _mode;
1323
+ };
1324
+ _this.getLength = function(buffer) {
1325
+ return ~~(_bytes.length / 2);
1326
+ };
1327
+ _this.write = function(buffer) {
1328
+ const data2 = _bytes;
1329
+ let i = 0;
1330
+ while (i + 1 < data2.length) {
1331
+ let c = (255 & data2[i]) << 8 | 255 & data2[i + 1];
1332
+ if (33088 <= c && c <= 40956) {
1333
+ c -= 33088;
1334
+ } else if (57408 <= c && c <= 60351) {
1335
+ c -= 49472;
1336
+ } else {
1337
+ throw "illegal char at " + (i + 1) + "/" + c;
1338
+ }
1339
+ c = (c >>> 8 & 255) * 192 + (c & 255);
1340
+ buffer.put(c, 13);
1341
+ i += 2;
1342
+ }
1343
+ if (i < data2.length) {
1344
+ throw "illegal char at " + (i + 1);
1345
+ }
1346
+ };
1347
+ return _this;
1348
+ };
1349
+ var byteArrayOutputStream = function() {
1350
+ const _bytes = [];
1351
+ const _this = {};
1352
+ _this.writeByte = function(b) {
1353
+ _bytes.push(b & 255);
1354
+ };
1355
+ _this.writeShort = function(i) {
1356
+ _this.writeByte(i);
1357
+ _this.writeByte(i >>> 8);
1358
+ };
1359
+ _this.writeBytes = function(b, off, len) {
1360
+ off = off || 0;
1361
+ len = len || b.length;
1362
+ for (let i = 0; i < len; i += 1) {
1363
+ _this.writeByte(b[i + off]);
1364
+ }
1365
+ };
1366
+ _this.writeString = function(s) {
1367
+ for (let i = 0; i < s.length; i += 1) {
1368
+ _this.writeByte(s.charCodeAt(i));
1369
+ }
1370
+ };
1371
+ _this.toByteArray = function() {
1372
+ return _bytes;
1373
+ };
1374
+ _this.toString = function() {
1375
+ let s = "";
1376
+ s += "[";
1377
+ for (let i = 0; i < _bytes.length; i += 1) {
1378
+ if (i > 0) {
1379
+ s += ",";
1380
+ }
1381
+ s += _bytes[i];
1382
+ }
1383
+ s += "]";
1384
+ return s;
1385
+ };
1386
+ return _this;
1387
+ };
1388
+ var base64EncodeOutputStream = function() {
1389
+ let _buffer = 0;
1390
+ let _buflen = 0;
1391
+ let _length = 0;
1392
+ let _base64 = "";
1393
+ const _this = {};
1394
+ const writeEncoded = function(b) {
1395
+ _base64 += String.fromCharCode(encode(b & 63));
1396
+ };
1397
+ const encode = function(n) {
1398
+ if (n < 0) {
1399
+ throw "n:" + n;
1400
+ } else if (n < 26) {
1401
+ return 65 + n;
1402
+ } else if (n < 52) {
1403
+ return 97 + (n - 26);
1404
+ } else if (n < 62) {
1405
+ return 48 + (n - 52);
1406
+ } else if (n == 62) {
1407
+ return 43;
1408
+ } else if (n == 63) {
1409
+ return 47;
1410
+ } else {
1411
+ throw "n:" + n;
1412
+ }
1413
+ };
1414
+ _this.writeByte = function(n) {
1415
+ _buffer = _buffer << 8 | n & 255;
1416
+ _buflen += 8;
1417
+ _length += 1;
1418
+ while (_buflen >= 6) {
1419
+ writeEncoded(_buffer >>> _buflen - 6);
1420
+ _buflen -= 6;
1421
+ }
1422
+ };
1423
+ _this.flush = function() {
1424
+ if (_buflen > 0) {
1425
+ writeEncoded(_buffer << 6 - _buflen);
1426
+ _buffer = 0;
1427
+ _buflen = 0;
1428
+ }
1429
+ if (_length % 3 != 0) {
1430
+ const padlen = 3 - _length % 3;
1431
+ for (let i = 0; i < padlen; i += 1) {
1432
+ _base64 += "=";
1433
+ }
1434
+ }
1435
+ };
1436
+ _this.toString = function() {
1437
+ return _base64;
1438
+ };
1439
+ return _this;
1440
+ };
1441
+ var base64DecodeInputStream = function(str) {
1442
+ const _str = str;
1443
+ let _pos = 0;
1444
+ let _buffer = 0;
1445
+ let _buflen = 0;
1446
+ const _this = {};
1447
+ _this.read = function() {
1448
+ while (_buflen < 8) {
1449
+ if (_pos >= _str.length) {
1450
+ if (_buflen == 0) {
1451
+ return -1;
1452
+ }
1453
+ throw "unexpected end of file./" + _buflen;
1454
+ }
1455
+ const c = _str.charAt(_pos);
1456
+ _pos += 1;
1457
+ if (c == "=") {
1458
+ _buflen = 0;
1459
+ return -1;
1460
+ } else if (c.match(/^\s$/)) {
1461
+ continue;
1462
+ }
1463
+ _buffer = _buffer << 6 | decode(c.charCodeAt(0));
1464
+ _buflen += 6;
1465
+ }
1466
+ const n = _buffer >>> _buflen - 8 & 255;
1467
+ _buflen -= 8;
1468
+ return n;
1469
+ };
1470
+ const decode = function(c) {
1471
+ if (65 <= c && c <= 90) {
1472
+ return c - 65;
1473
+ } else if (97 <= c && c <= 122) {
1474
+ return c - 97 + 26;
1475
+ } else if (48 <= c && c <= 57) {
1476
+ return c - 48 + 52;
1477
+ } else if (c == 43) {
1478
+ return 62;
1479
+ } else if (c == 47) {
1480
+ return 63;
1481
+ } else {
1482
+ throw "c:" + c;
1483
+ }
1484
+ };
1485
+ return _this;
1486
+ };
1487
+ var gifImage = function(width, height) {
1488
+ const _width = width;
1489
+ const _height = height;
1490
+ const _data = new Array(width * height);
1491
+ const _this = {};
1492
+ _this.setPixel = function(x, y, pixel) {
1493
+ _data[y * _width + x] = pixel;
1494
+ };
1495
+ _this.write = function(out) {
1496
+ out.writeString("GIF87a");
1497
+ out.writeShort(_width);
1498
+ out.writeShort(_height);
1499
+ out.writeByte(128);
1500
+ out.writeByte(0);
1501
+ out.writeByte(0);
1502
+ out.writeByte(0);
1503
+ out.writeByte(0);
1504
+ out.writeByte(0);
1505
+ out.writeByte(255);
1506
+ out.writeByte(255);
1507
+ out.writeByte(255);
1508
+ out.writeString(",");
1509
+ out.writeShort(0);
1510
+ out.writeShort(0);
1511
+ out.writeShort(_width);
1512
+ out.writeShort(_height);
1513
+ out.writeByte(0);
1514
+ const lzwMinCodeSize = 2;
1515
+ const raster = getLZWRaster(lzwMinCodeSize);
1516
+ out.writeByte(lzwMinCodeSize);
1517
+ let offset = 0;
1518
+ while (raster.length - offset > 255) {
1519
+ out.writeByte(255);
1520
+ out.writeBytes(raster, offset, 255);
1521
+ offset += 255;
1522
+ }
1523
+ out.writeByte(raster.length - offset);
1524
+ out.writeBytes(raster, offset, raster.length - offset);
1525
+ out.writeByte(0);
1526
+ out.writeString(";");
1527
+ };
1528
+ const bitOutputStream = function(out) {
1529
+ const _out = out;
1530
+ let _bitLength = 0;
1531
+ let _bitBuffer = 0;
1532
+ const _this2 = {};
1533
+ _this2.write = function(data, length) {
1534
+ if (data >>> length != 0) {
1535
+ throw "length over";
1536
+ }
1537
+ while (_bitLength + length >= 8) {
1538
+ _out.writeByte(255 & (data << _bitLength | _bitBuffer));
1539
+ length -= 8 - _bitLength;
1540
+ data >>>= 8 - _bitLength;
1541
+ _bitBuffer = 0;
1542
+ _bitLength = 0;
1543
+ }
1544
+ _bitBuffer = data << _bitLength | _bitBuffer;
1545
+ _bitLength = _bitLength + length;
1546
+ };
1547
+ _this2.flush = function() {
1548
+ if (_bitLength > 0) {
1549
+ _out.writeByte(_bitBuffer);
1550
+ }
1551
+ };
1552
+ return _this2;
1553
+ };
1554
+ const getLZWRaster = function(lzwMinCodeSize) {
1555
+ const clearCode = 1 << lzwMinCodeSize;
1556
+ const endCode = (1 << lzwMinCodeSize) + 1;
1557
+ let bitLength = lzwMinCodeSize + 1;
1558
+ const table = lzwTable();
1559
+ for (let i = 0; i < clearCode; i += 1) {
1560
+ table.add(String.fromCharCode(i));
1561
+ }
1562
+ table.add(String.fromCharCode(clearCode));
1563
+ table.add(String.fromCharCode(endCode));
1564
+ const byteOut = byteArrayOutputStream();
1565
+ const bitOut = bitOutputStream(byteOut);
1566
+ bitOut.write(clearCode, bitLength);
1567
+ let dataIndex = 0;
1568
+ let s = String.fromCharCode(_data[dataIndex]);
1569
+ dataIndex += 1;
1570
+ while (dataIndex < _data.length) {
1571
+ const c = String.fromCharCode(_data[dataIndex]);
1572
+ dataIndex += 1;
1573
+ if (table.contains(s + c)) {
1574
+ s = s + c;
1575
+ } else {
1576
+ bitOut.write(table.indexOf(s), bitLength);
1577
+ if (table.size() < 4095) {
1578
+ if (table.size() == 1 << bitLength) {
1579
+ bitLength += 1;
1580
+ }
1581
+ table.add(s + c);
1582
+ }
1583
+ s = c;
1584
+ }
1585
+ }
1586
+ bitOut.write(table.indexOf(s), bitLength);
1587
+ bitOut.write(endCode, bitLength);
1588
+ bitOut.flush();
1589
+ return byteOut.toByteArray();
1590
+ };
1591
+ const lzwTable = function() {
1592
+ const _map = {};
1593
+ let _size = 0;
1594
+ const _this2 = {};
1595
+ _this2.add = function(key) {
1596
+ if (_this2.contains(key)) {
1597
+ throw "dup key:" + key;
1598
+ }
1599
+ _map[key] = _size;
1600
+ _size += 1;
1601
+ };
1602
+ _this2.size = function() {
1603
+ return _size;
1604
+ };
1605
+ _this2.indexOf = function(key) {
1606
+ return _map[key];
1607
+ };
1608
+ _this2.contains = function(key) {
1609
+ return typeof _map[key] != "undefined";
1610
+ };
1611
+ return _this2;
1612
+ };
1613
+ return _this;
1614
+ };
1615
+ var createDataURL = function(width, height, getPixel) {
1616
+ const gif = gifImage(width, height);
1617
+ for (let y = 0; y < height; y += 1) {
1618
+ for (let x = 0; x < width; x += 1) {
1619
+ gif.setPixel(x, y, getPixel(x, y));
1620
+ }
1621
+ }
1622
+ const b = byteArrayOutputStream();
1623
+ gif.write(b);
1624
+ const base64 = base64EncodeOutputStream();
1625
+ const bytes = b.toByteArray();
1626
+ for (let i = 0; i < bytes.length; i += 1) {
1627
+ base64.writeByte(bytes[i]);
1628
+ }
1629
+ base64.flush();
1630
+ return "data:image/gif;base64," + base64;
1631
+ };
1632
+ var qrcode_default = qrcode;
1633
+ var stringToBytes = qrcode.stringToBytes;
1634
+
1635
+ // src/core.ts
1636
+ var token = /^[A-Za-z0-9_-]{43}$/;
1637
+ var AuthLinkError = class extends Error {
1638
+ constructor(code) {
1639
+ super(code);
1640
+ this.code = code;
1641
+ this.name = "AuthLinkError";
1642
+ }
1643
+ code;
1644
+ };
1645
+ function fail(code) {
1646
+ throw new AuthLinkError(code);
1647
+ }
1648
+ function object(value) {
1649
+ if (!value || typeof value !== "object" || Array.isArray(value)) fail("INVALID_RESPONSE");
1650
+ return value;
1651
+ }
1652
+ function validateDisplayCode(value) {
1653
+ const m = object(value);
1654
+ const span = Date.parse(String(m.validUntil)) - Date.parse(String(m.serverTime));
1655
+ if (typeof m.value !== "string" || !/^\d{6}$/.test(m.value) || m.periodSeconds !== 30 || !Number.isFinite(span) || span <= 0 || span > 3e4 || "seed" in m || "secret" in m || "key" in m) fail("INVALID_DISPLAY_CODE");
1656
+ return { value: m.value, serverTime: String(m.serverTime), validUntil: String(m.validUntil), periodSeconds: 30 };
1657
+ }
1658
+ function validateTicket(value, expectedMode, trustedOrigin) {
1659
+ const m = object(value);
1660
+ if ("pairing" in m || "seed" in m || "apiKey" in m) fail("SERVER_SECRET_FORBIDDEN");
1661
+ if (typeof m.requestId !== "string" || !token.test(m.requestId) || !["bind", "confirm"].includes(String(m.purpose)) || expectedMode && m.purpose !== expectedMode || typeof m.qr !== "string" || m.qr.length > 4096 || typeof m.pairingCode !== "string" || !/^\d{6}$/.test(m.pairingCode)) fail("INVALID_TICKET");
1662
+ let qr;
1663
+ try {
1664
+ qr = object(JSON.parse(m.qr));
1665
+ } catch {
1666
+ return fail("INVALID_QR");
1667
+ }
1668
+ let origin;
1669
+ try {
1670
+ origin = new URL(String(qr.origin));
1671
+ } catch {
1672
+ return fail("INVALID_QR");
1673
+ }
1674
+ if (qr.v !== 1 || qr.requestId !== m.requestId || typeof qr.token !== "string" || !token.test(qr.token) || origin.protocol !== "https:" || origin.origin !== qr.origin || origin.origin.length > 512 || origin.username || origin.password || trustedOrigin && origin.origin !== trustedOrigin) fail("UNTRUSTED_QR");
1675
+ const displayCode = m.displayCode === void 0 ? void 0 : validateDisplayCode(m.displayCode);
1676
+ if (m.purpose === "confirm" && !displayCode) fail("DYNAMIC_CODE_REQUIRED");
1677
+ const expiry = Date.parse(String(m.expiresAt));
1678
+ const now = displayCode ? Date.parse(displayCode.serverTime) : Date.now();
1679
+ if (!Number.isFinite(expiry) || expiry > now + 18e4) fail("INVALID_EXPIRY");
1680
+ return {
1681
+ requestId: m.requestId,
1682
+ purpose: m.purpose,
1683
+ qr: m.qr,
1684
+ pairingCode: m.pairingCode,
1685
+ expiresAt: String(m.expiresAt),
1686
+ ...displayCode ? { displayCode } : {}
1687
+ };
1688
+ }
1689
+ function qrImage(ticket) {
1690
+ const value = validateTicket(ticket);
1691
+ const payload = JSON.parse(value.qr);
1692
+ const qr = qrcode_default(0, "M");
1693
+ qr.addData(JSON.stringify({ v: 1, origin: payload.origin, requestId: payload.requestId, token: payload.token }), "Byte");
1694
+ qr.make();
1695
+ return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(qr.createSvgTag({ cellSize: 4, margin: 16, scalable: true }))}`;
1696
+ }
1697
+ function backendProtocol(url) {
1698
+ return url.protocol === "https:" || url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
1699
+ }
1700
+ async function limitedJson(response) {
1701
+ const reader = response.body?.getReader();
1702
+ if (!reader) fail("INVALID_RESPONSE");
1703
+ const chunks = [];
1704
+ let size = 0;
1705
+ try {
1706
+ for (; ; ) {
1707
+ const { done, value } = await reader.read();
1708
+ if (done) break;
1709
+ size += value.length;
1710
+ if (size > 65536) {
1711
+ await reader.cancel();
1712
+ fail("RESPONSE_TOO_LARGE");
1713
+ }
1714
+ chunks.push(value);
1715
+ }
1716
+ } finally {
1717
+ reader.releaseLock();
1718
+ }
1719
+ const bytes = new Uint8Array(size);
1720
+ let at = 0;
1721
+ for (const chunk of chunks) {
1722
+ bytes.set(chunk, at);
1723
+ at += chunk.length;
1724
+ }
1725
+ try {
1726
+ return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
1727
+ } catch {
1728
+ return fail("INVALID_RESPONSE");
1729
+ }
1730
+ }
1731
+ function createAuthLinkClient(options) {
1732
+ if ("apiKey" in options || "secret" in options) fail("SERVER_SECRET_FORBIDDEN");
1733
+ const base = options.baseUrl ?? "/api/authlink";
1734
+ const allowedOrigins = new Set(options.allowedBackendOrigins ?? []);
1735
+ for (const origin of allowedOrigins) {
1736
+ let url;
1737
+ try {
1738
+ url = new URL(origin);
1739
+ } catch {
1740
+ return fail("INVALID_BACKEND_ORIGIN");
1741
+ }
1742
+ if (url.origin !== origin || url.hostname.includes("*") || !backendProtocol(url)) fail("INVALID_BACKEND_ORIGIN");
1743
+ }
1744
+ const credentials = options.credentials ?? (options.accessToken ? "omit" : "same-origin");
1745
+ if (!["omit", "same-origin", "include"].includes(credentials)) fail("INVALID_CREDENTIALS");
1746
+ const csrfHeaderName = options.csrfHeaderName ?? "X-CSRF-Token";
1747
+ const reserved = /* @__PURE__ */ new Set(["accept", "content-type", "authorization", "idempotency-key"]);
1748
+ try {
1749
+ new Headers([[csrfHeaderName, "test"]]);
1750
+ } catch {
1751
+ return fail("INVALID_CSRF_HEADER");
1752
+ }
1753
+ if (reserved.has(csrfHeaderName.toLowerCase())) fail("INVALID_CSRF_HEADER");
1754
+ reserved.add(csrfHeaderName.toLowerCase());
1755
+ const call = async (path, body, signal, key) => {
1756
+ if (typeof window === "undefined") fail("BROWSER_REQUIRED");
1757
+ let url;
1758
+ try {
1759
+ url = new URL(base.replace(/\/+$/, "") + path, window.location.href);
1760
+ } catch {
1761
+ return fail("INVALID_BASE_URL");
1762
+ }
1763
+ if (url.username || url.password || url.search || url.hash || !backendProtocol(url)) fail("INVALID_BASE_URL");
1764
+ if (url.origin !== window.location.origin && !allowedOrigins.has(url.origin)) fail("BACKEND_ORIGIN_NOT_ALLOWED");
1765
+ signal?.throwIfAborted();
1766
+ const controller = new AbortController();
1767
+ const cancel = () => controller.abort(signal?.reason);
1768
+ signal?.addEventListener("abort", cancel, { once: true });
1769
+ const timeout = setTimeout(() => controller.abort(), 1e4);
1770
+ try {
1771
+ const headers = new Headers(typeof options.headers === "function" ? await abortable(Promise.resolve().then(options.headers), controller.signal) : options.headers);
1772
+ for (const name of reserved) if (headers.has(name)) fail("RESERVED_HEADER");
1773
+ headers.set("Accept", "application/json");
1774
+ headers.set("Content-Type", "application/json");
1775
+ if (key) headers.set("Idempotency-Key", key);
1776
+ if (options.csrfToken) {
1777
+ const csrf = await abortable(Promise.resolve().then(options.csrfToken), controller.signal);
1778
+ if (typeof csrf !== "string" || !csrf.trim() || csrf.length > 1024 || /[\r\n]/.test(csrf)) fail("CSRF_REQUIRED");
1779
+ headers.set(csrfHeaderName, csrf);
1780
+ } else if (!options.accessToken || credentials !== "omit") fail("CSRF_REQUIRED");
1781
+ if (options.accessToken) {
1782
+ const accessToken = await abortable(Promise.resolve().then(options.accessToken), controller.signal);
1783
+ if (typeof accessToken !== "string" || !accessToken || /\s/.test(accessToken)) fail("ACCESS_TOKEN_REQUIRED");
1784
+ headers.set("Authorization", `Bearer ${accessToken}`);
1785
+ }
1786
+ controller.signal.throwIfAborted();
1787
+ const response = await (options.fetch ?? globalThis.fetch)(url, {
1788
+ method: "POST",
1789
+ credentials,
1790
+ redirect: "error",
1791
+ cache: "no-store",
1792
+ signal: controller.signal,
1793
+ headers,
1794
+ body: JSON.stringify(body)
1795
+ });
1796
+ if (!response.ok) fail(response.status === 401 || response.status === 403 ? "CMS_UNAUTHORIZED" : "CMS_REQUEST_FAILED");
1797
+ return await limitedJson(response);
1798
+ } catch (error) {
1799
+ if (error instanceof AuthLinkError) throw error;
1800
+ if (signal?.aborted) throw signal.reason;
1801
+ return fail("NETWORK_ERROR");
1802
+ } finally {
1803
+ clearTimeout(timeout);
1804
+ signal?.removeEventListener("abort", cancel);
1805
+ }
1806
+ };
1807
+ return {
1808
+ async create(mode, { idempotencyKey, signal }) {
1809
+ if (!["bind", "confirm"].includes(mode) || !token.test(idempotencyKey)) fail("INVALID_CREATE_OPTIONS");
1810
+ return validateTicket(await call("/requests", { purpose: mode }, signal, idempotencyKey), mode, options.authLinkOrigin);
1811
+ },
1812
+ async result(ticket, signal) {
1813
+ if (!token.test(ticket.requestId)) fail("INVALID_TICKET");
1814
+ return await call(`/requests/${ticket.requestId}/result`, {}, signal);
1815
+ }
1816
+ };
1817
+ }
1818
+ function requestKey() {
1819
+ const bytes = globalThis.crypto.getRandomValues(new Uint8Array(32));
1820
+ return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1821
+ }
1822
+ function initialSnapshot() {
1823
+ return { status: "idle", code: null, secondsRemaining: 0, codeSecondsRemaining: 0, error: null };
1824
+ }
1825
+ function abortable(promise, signal) {
1826
+ signal.throwIfAborted();
1827
+ let cancel;
1828
+ const abort = new Promise((_, reject) => {
1829
+ cancel = () => reject(signal.reason);
1830
+ signal.addEventListener("abort", cancel, { once: true });
1831
+ });
1832
+ return Promise.race([promise, abort]).finally(() => signal.removeEventListener("abort", cancel));
1833
+ }
1834
+ async function observeRequest(input, options) {
1835
+ const { client, signal, onUpdate } = options;
1836
+ const interval = options.intervalMs ?? 1500;
1837
+ let ticket;
1838
+ try {
1839
+ if (interval < 250 || interval > 1e4 || !Number.isFinite(interval)) fail("INVALID_INTERVAL");
1840
+ ticket = validateTicket(input, options.expectedMode);
1841
+ } catch (error) {
1842
+ if (!signal.aborted) {
1843
+ const e = error instanceof Error ? error : new AuthLinkError("INVALID_TICKET");
1844
+ onUpdate({ ...initialSnapshot(), status: "error", error: e });
1845
+ options.onError?.(e);
1846
+ }
1847
+ return;
1848
+ }
1849
+ let frame = ticket.displayCode;
1850
+ let serverAnchor = frame ? Date.parse(frame.serverTime) : Date.now();
1851
+ let monotonicAnchor = performance.now();
1852
+ const serverNow = () => serverAnchor + performance.now() - monotonicAnchor;
1853
+ const expiry = Date.parse(ticket.expiresAt);
1854
+ let ended = false;
1855
+ const controller = new AbortController();
1856
+ const cancel = () => controller.abort(signal.reason);
1857
+ signal.addEventListener("abort", cancel, { once: true });
1858
+ if (signal.aborted) cancel();
1859
+ let last = initialSnapshot();
1860
+ const update = (value) => {
1861
+ if (signal.aborted) return;
1862
+ if (JSON.stringify(value) !== JSON.stringify(last)) {
1863
+ last = value;
1864
+ onUpdate(value);
1865
+ }
1866
+ };
1867
+ const tick = () => {
1868
+ if (ended || signal.aborted) return;
1869
+ const now = serverNow();
1870
+ if (now >= expiry) {
1871
+ controller.abort(new AuthLinkError("REQUEST_EXPIRED"));
1872
+ return;
1873
+ }
1874
+ const codeRemaining = Math.max(0, ((frame ? Date.parse(frame.validUntil) : expiry) - now) / 1e3);
1875
+ options.onCodeTiming?.(codeRemaining * 1e3);
1876
+ update({
1877
+ status: "pending",
1878
+ code: codeRemaining > 0 ? frame?.value ?? ticket.pairingCode : null,
1879
+ secondsRemaining: Math.max(0, Math.ceil((expiry - now) / 1e3)),
1880
+ codeSecondsRemaining: Math.ceil(codeRemaining),
1881
+ error: null
1882
+ });
1883
+ };
1884
+ const timer = setInterval(tick, 200);
1885
+ const finish = (status, result, error) => {
1886
+ if (ended || signal.aborted) return;
1887
+ ended = true;
1888
+ update({ ...last, status, code: null, error: error ?? null });
1889
+ try {
1890
+ let effect = void 0;
1891
+ if (status === "confirmed") effect = options.onConfirmed?.(result);
1892
+ else if (status === "rejected") effect = options.onRejected?.({ status: "rejected", code: "REJECTED", requestId: ticket.requestId, purpose: ticket.purpose });
1893
+ else if (status === "expired") effect = options.onExpired?.();
1894
+ else options.onError?.(error);
1895
+ void Promise.resolve(effect).catch(() => {
1896
+ if (!signal.aborted) options.onError?.(new AuthLinkError("CALLBACK_FAILED"));
1897
+ });
1898
+ } catch {
1899
+ if (status !== "error") options.onError?.(new AuthLinkError("CALLBACK_FAILED"));
1900
+ }
1901
+ };
1902
+ try {
1903
+ tick();
1904
+ for (; ; ) {
1905
+ controller.signal.throwIfAborted();
1906
+ const value = object(await abortable(client.result(ticket, controller.signal), controller.signal));
1907
+ controller.signal.throwIfAborted();
1908
+ if (value.requestId !== ticket.requestId) fail("REQUEST_MISMATCH");
1909
+ if (value.status === "confirmed") {
1910
+ if (!token.test(String(value.verificationId)) || !["device-presence.v1"].includes(String(value.authenticationPolicy)) || value.expiresAt !== ticket.expiresAt || value.purpose !== ticket.purpose || serverNow() >= expiry || "action" in value || "transactionHash" in value) fail("INVALID_CONFIRMATION");
1911
+ finish("confirmed", value);
1912
+ return;
1913
+ }
1914
+ if (value.status === "rejected" || value.status === "expired") {
1915
+ finish(value.status);
1916
+ return;
1917
+ }
1918
+ if (!["new", "pending", "approved", "consumed"].includes(String(value.status))) fail("INVALID_RESPONSE");
1919
+ if (value.displayCode !== void 0) {
1920
+ const nextFrame = validateDisplayCode(value.displayCode);
1921
+ const nextExpiry = Date.parse(nextFrame.validUntil);
1922
+ const currentExpiry = frame ? Date.parse(frame.validUntil) : 0;
1923
+ if (!frame || nextExpiry > currentExpiry) {
1924
+ serverAnchor = Math.max(serverNow(), Date.parse(nextFrame.serverTime));
1925
+ monotonicAnchor = performance.now();
1926
+ }
1927
+ if (!frame || nextExpiry >= currentExpiry) frame = nextFrame;
1928
+ }
1929
+ tick();
1930
+ let pause;
1931
+ try {
1932
+ await abortable(new Promise((resolve) => {
1933
+ pause = setTimeout(resolve, interval);
1934
+ }), controller.signal);
1935
+ } finally {
1936
+ clearTimeout(pause);
1937
+ }
1938
+ }
1939
+ } catch (error) {
1940
+ if (!signal.aborted && !ended) {
1941
+ if (error instanceof AuthLinkError && error.code === "REQUEST_EXPIRED") finish("expired");
1942
+ else finish("error", void 0, error instanceof AuthLinkError ? error : new AuthLinkError("NETWORK_ERROR"));
1943
+ }
1944
+ } finally {
1945
+ ended = true;
1946
+ clearInterval(timer);
1947
+ controller.abort();
1948
+ signal.removeEventListener("abort", cancel);
1949
+ }
1950
+ }
1951
+
1952
+ // src/locales/en.json
1953
+ var en_default = {
1954
+ bind: "Bind your phone",
1955
+ confirm: "Confirm verification",
1956
+ scan: "Scan with AuthLink",
1957
+ code: "Matching code",
1958
+ help: "Check that the numbers match in the app, then confirm on your phone.",
1959
+ waiting: "Waiting for your phone",
1960
+ loading: "Connecting",
1961
+ confirmed: "Confirmed",
1962
+ rejected: "Rejected",
1963
+ expired: "Request expired",
1964
+ error: "Verification is unavailable",
1965
+ retry: "Retry",
1966
+ refresh: "New request",
1967
+ updating: "Updating code",
1968
+ close: "Close",
1969
+ cancel: "Cancel",
1970
+ autoUpdate: "Code updates automatically every 30 seconds",
1971
+ expiring: "Request expiring soon"
1972
+ };
1973
+
1974
+ // src/locales/zh-CN.json
1975
+ var zh_CN_default = {
1976
+ bind: "\u7ED1\u5B9A\u624B\u673A",
1977
+ confirm: "\u786E\u8BA4\u8EAB\u4EFD\u9A8C\u8BC1",
1978
+ scan: "\u4F7F\u7528 AuthLink \u626B\u63CF\u4E8C\u7EF4\u7801",
1979
+ code: "\u52A8\u6001\u6838\u5BF9\u7801",
1980
+ help: "\u6838\u5BF9 App \u4E2D\u7684\u6570\u5B57\u4E00\u81F4\u540E\uFF0C\u5728\u624B\u673A\u4E0A\u786E\u8BA4\u3002",
1981
+ waiting: "\u7B49\u5F85\u624B\u673A\u786E\u8BA4",
1982
+ loading: "\u6B63\u5728\u8FDE\u63A5",
1983
+ confirmed: "\u5DF2\u786E\u8BA4",
1984
+ rejected: "\u5DF2\u62D2\u7EDD",
1985
+ expired: "\u8BF7\u6C42\u5DF2\u8FC7\u671F",
1986
+ error: "\u6682\u65F6\u65E0\u6CD5\u5B8C\u6210\u9A8C\u8BC1",
1987
+ retry: "\u91CD\u8BD5",
1988
+ refresh: "\u91CD\u65B0\u53D1\u8D77",
1989
+ updating: "\u6B63\u5728\u66F4\u65B0\u6570\u5B57",
1990
+ close: "\u5173\u95ED",
1991
+ cancel: "\u53D6\u6D88",
1992
+ autoUpdate: "\u6838\u5BF9\u7801\u6BCF 30 \u79D2\u81EA\u52A8\u66F4\u65B0",
1993
+ expiring: "\u8BF7\u6C42\u5373\u5C06\u8FC7\u671F"
1994
+ };
1995
+
1996
+ // src/locales/zh-TW.json
1997
+ var zh_TW_default = {
1998
+ bind: "\u7D81\u5B9A\u624B\u6A5F",
1999
+ confirm: "\u78BA\u8A8D\u8EAB\u5206\u9A57\u8B49",
2000
+ scan: "\u4F7F\u7528 AuthLink \u6383\u63CF QR \u78BC",
2001
+ code: "\u52D5\u614B\u6838\u5C0D\u78BC",
2002
+ help: "\u6838\u5C0D App \u4E2D\u7684\u6578\u5B57\u4E00\u81F4\u5F8C\uFF0C\u5728\u624B\u6A5F\u4E0A\u78BA\u8A8D\u3002",
2003
+ waiting: "\u7B49\u5F85\u624B\u6A5F\u78BA\u8A8D",
2004
+ loading: "\u6B63\u5728\u9023\u7DDA",
2005
+ confirmed: "\u5DF2\u78BA\u8A8D",
2006
+ rejected: "\u5DF2\u62D2\u7D55",
2007
+ expired: "\u8ACB\u6C42\u5DF2\u904E\u671F",
2008
+ error: "\u66AB\u6642\u7121\u6CD5\u5B8C\u6210\u9A57\u8B49",
2009
+ retry: "\u91CD\u8A66",
2010
+ refresh: "\u91CD\u65B0\u767C\u8D77",
2011
+ updating: "\u6B63\u5728\u66F4\u65B0\u6578\u5B57",
2012
+ close: "\u95DC\u9589",
2013
+ cancel: "\u53D6\u6D88",
2014
+ autoUpdate: "\u6838\u5C0D\u78BC\u6BCF 30 \u79D2\u81EA\u52D5\u66F4\u65B0",
2015
+ expiring: "\u8ACB\u6C42\u5373\u5C07\u5230\u671F"
2016
+ };
2017
+
2018
+ // src/locales/ja.json
2019
+ var ja_default = {
2020
+ bind: "\u30B9\u30DE\u30FC\u30C8\u30D5\u30A9\u30F3\u3092\u767B\u9332",
2021
+ confirm: "\u672C\u4EBA\u78BA\u8A8D",
2022
+ scan: "AuthLink \u3067 QR \u30B3\u30FC\u30C9\u3092\u30B9\u30AD\u30E3\u30F3",
2023
+ code: "\u7167\u5408\u30B3\u30FC\u30C9",
2024
+ help: "\u30A2\u30D7\u30EA\u306E\u6570\u5B57\u304C\u4E00\u81F4\u3059\u308B\u3053\u3068\u3092\u78BA\u8A8D\u3057\u3001\u30B9\u30DE\u30FC\u30C8\u30D5\u30A9\u30F3\u3067\u627F\u8A8D\u3057\u3066\u304F\u3060\u3055\u3044\u3002",
2025
+ waiting: "\u30B9\u30DE\u30FC\u30C8\u30D5\u30A9\u30F3\u306E\u78BA\u8A8D\u3092\u5F85\u3063\u3066\u3044\u307E\u3059",
2026
+ loading: "\u63A5\u7D9A\u4E2D",
2027
+ confirmed: "\u78BA\u8A8D\u6E08\u307F",
2028
+ rejected: "\u62D2\u5426\u3055\u308C\u307E\u3057\u305F",
2029
+ expired: "\u6709\u52B9\u671F\u9650\u304C\u5207\u308C\u307E\u3057\u305F",
2030
+ error: "\u672C\u4EBA\u78BA\u8A8D\u3092\u5B8C\u4E86\u3067\u304D\u307E\u305B\u3093",
2031
+ retry: "\u518D\u8A66\u884C",
2032
+ refresh: "\u65B0\u3057\u3044\u30EA\u30AF\u30A8\u30B9\u30C8",
2033
+ updating: "\u30B3\u30FC\u30C9\u3092\u66F4\u65B0\u4E2D",
2034
+ close: "\u9589\u3058\u308B",
2035
+ cancel: "\u30AD\u30E3\u30F3\u30BB\u30EB",
2036
+ autoUpdate: "\u7167\u5408\u30B3\u30FC\u30C9\u306F30\u79D2\u3054\u3068\u306B\u81EA\u52D5\u66F4\u65B0",
2037
+ expiring: "\u30EA\u30AF\u30A8\u30B9\u30C8\u306E\u6709\u52B9\u671F\u9650\u304C\u307E\u3082\u306A\u304F\u5207\u308C\u307E\u3059"
2038
+ };
2039
+
2040
+ // src/locales/bn.json
2041
+ var bn_default = {
2042
+ bind: "\u0986\u09AA\u09A8\u09BE\u09B0 \u09AB\u09CB\u09A8 \u09AF\u09C1\u0995\u09CD\u09A4 \u0995\u09B0\u09C1\u09A8",
2043
+ confirm: "\u09AF\u09BE\u099A\u09BE\u0987\u0995\u09B0\u09A3 \u09A8\u09BF\u09B6\u09CD\u099A\u09BF\u09A4 \u0995\u09B0\u09C1\u09A8",
2044
+ scan: "AuthLink \u09A6\u09BF\u09AF\u09BC\u09C7 \u09B8\u09CD\u0995\u09CD\u09AF\u09BE\u09A8 \u0995\u09B0\u09C1\u09A8",
2045
+ code: "\u09AE\u09BF\u09B2\u09BF\u09AF\u09BC\u09C7 \u09A6\u09C7\u0996\u09BE\u09B0 \u0995\u09CB\u09A1",
2046
+ help: "\u0985\u09CD\u09AF\u09BE\u09AA\u09C7\u09B0 \u09B8\u0982\u0996\u09CD\u09AF\u09BE\u0997\u09C1\u09B2\u09CB \u09AE\u09BF\u09B2\u09C7 \u0997\u09C7\u09B2\u09C7 \u09AB\u09CB\u09A8\u09C7 \u09A8\u09BF\u09B6\u09CD\u099A\u09BF\u09A4 \u0995\u09B0\u09C1\u09A8\u0964",
2047
+ waiting: "\u09AB\u09CB\u09A8\u09C7 \u09A8\u09BF\u09B6\u09CD\u099A\u09BF\u09A4\u0995\u09B0\u09A3\u09C7\u09B0 \u0985\u09AA\u09C7\u0995\u09CD\u09B7\u09BE\u09AF\u09BC",
2048
+ loading: "\u09B8\u0982\u09AF\u09CB\u0997 \u0995\u09B0\u09BE \u09B9\u099A\u09CD\u099B\u09C7",
2049
+ confirmed: "\u09A8\u09BF\u09B6\u09CD\u099A\u09BF\u09A4 \u09B9\u09AF\u09BC\u09C7\u099B\u09C7",
2050
+ rejected: "\u09AA\u09CD\u09B0\u09A4\u09CD\u09AF\u09BE\u0996\u09CD\u09AF\u09BE\u09A4",
2051
+ expired: "\u0985\u09A8\u09C1\u09B0\u09CB\u09A7\u09C7\u09B0 \u09AE\u09C7\u09AF\u09BC\u09BE\u09A6 \u09B6\u09C7\u09B7",
2052
+ error: "\u09AF\u09BE\u099A\u09BE\u0987\u0995\u09B0\u09A3 \u098F\u0996\u09A8 \u09B8\u09AE\u09CD\u09AD\u09AC \u09A8\u09AF\u09BC",
2053
+ retry: "\u0986\u09AC\u09BE\u09B0 \u099A\u09C7\u09B7\u09CD\u099F\u09BE \u0995\u09B0\u09C1\u09A8",
2054
+ refresh: "\u09A8\u09A4\u09C1\u09A8 \u0985\u09A8\u09C1\u09B0\u09CB\u09A7",
2055
+ updating: "\u0995\u09CB\u09A1 \u0986\u09AA\u09A1\u09C7\u099F \u09B9\u099A\u09CD\u099B\u09C7",
2056
+ close: "\u09AC\u09A8\u09CD\u09A7 \u0995\u09B0\u09C1\u09A8",
2057
+ cancel: "\u09AC\u09BE\u09A4\u09BF\u09B2",
2058
+ autoUpdate: "\u0995\u09CB\u09A1 \u09AA\u09CD\u09B0\u09A4\u09BF \u09E9\u09E6 \u09B8\u09C7\u0995\u09C7\u09A8\u09CD\u09A1\u09C7 \u09B8\u09CD\u09AC\u09AF\u09BC\u0982\u0995\u09CD\u09B0\u09BF\u09AF\u09BC\u09AD\u09BE\u09AC\u09C7 \u09AC\u09A6\u09B2\u09BE\u09AF\u09BC",
2059
+ expiring: "\u0985\u09A8\u09C1\u09B0\u09CB\u09A7\u09C7\u09B0 \u09AE\u09C7\u09AF\u09BC\u09BE\u09A6 \u09B6\u09C0\u0998\u09CD\u09B0\u0987 \u09B6\u09C7\u09B7 \u09B9\u09AC\u09C7"
2060
+ };
2061
+
2062
+ // src/locales/es.json
2063
+ var es_default = {
2064
+ bind: "Vincula tu tel\xE9fono",
2065
+ confirm: "Confirma la verificaci\xF3n",
2066
+ scan: "Escanea con AuthLink",
2067
+ code: "C\xF3digo de comparaci\xF3n",
2068
+ help: "Comprueba que los n\xFAmeros coincidan en la aplicaci\xF3n y confirma en tu tel\xE9fono.",
2069
+ waiting: "Esperando la confirmaci\xF3n del tel\xE9fono",
2070
+ loading: "Conectando",
2071
+ confirmed: "Confirmado",
2072
+ rejected: "Rechazado",
2073
+ expired: "La solicitud ha caducado",
2074
+ error: "La verificaci\xF3n no est\xE1 disponible",
2075
+ retry: "Reintentar",
2076
+ refresh: "Nueva solicitud",
2077
+ updating: "Actualizando el c\xF3digo",
2078
+ close: "Cerrar",
2079
+ cancel: "Cancelar",
2080
+ autoUpdate: "El c\xF3digo se actualiza autom\xE1ticamente cada 30 segundos",
2081
+ expiring: "La solicitud est\xE1 por caducar"
2082
+ };
2083
+
2084
+ // src/locales/fil.json
2085
+ var fil_default = {
2086
+ bind: "Iugnay ang iyong telepono",
2087
+ confirm: "Kumpirmahin ang pagpapatunay",
2088
+ scan: "Mag-scan gamit ang AuthLink",
2089
+ code: "Code na paghahambingin",
2090
+ help: "Tiyaking magkapareho ang mga numero sa app, pagkatapos ay kumpirmahin sa iyong telepono.",
2091
+ waiting: "Hinihintay ang kumpirmasyon sa telepono",
2092
+ loading: "Kumokonekta",
2093
+ confirmed: "Nakumpirma",
2094
+ rejected: "Tinanggihan",
2095
+ expired: "Nag-expire ang kahilingan",
2096
+ error: "Hindi available ang pagpapatunay",
2097
+ retry: "Subukan muli",
2098
+ refresh: "Bagong kahilingan",
2099
+ updating: "Ina-update ang code",
2100
+ close: "Isara",
2101
+ cancel: "Kanselahin",
2102
+ autoUpdate: "Awtomatikong nagbabago ang code kada 30 segundo",
2103
+ expiring: "Malapit nang mag-expire ang kahilingan"
2104
+ };
2105
+
2106
+ // src/locales/hi.json
2107
+ var hi_default = {
2108
+ bind: "\u0905\u092A\u0928\u093E \u092B\u093C\u094B\u0928 \u091C\u094B\u0921\u093C\u0947\u0902",
2109
+ confirm: "\u0938\u0924\u094D\u092F\u093E\u092A\u0928 \u0915\u0940 \u092A\u0941\u0937\u094D\u091F\u093F \u0915\u0930\u0947\u0902",
2110
+ scan: "AuthLink \u0938\u0947 \u0938\u094D\u0915\u0948\u0928 \u0915\u0930\u0947\u0902",
2111
+ code: "\u092E\u093F\u0932\u093E\u0928 \u0915\u094B\u0921",
2112
+ help: "\u0910\u092A \u092E\u0947\u0902 \u0938\u0902\u0916\u094D\u092F\u093E\u0913\u0902 \u0915\u093E \u092E\u093F\u0932\u093E\u0928 \u0915\u0930\u0947\u0902, \u092B\u093F\u0930 \u0905\u092A\u0928\u0947 \u092B\u093C\u094B\u0928 \u092A\u0930 \u092A\u0941\u0937\u094D\u091F\u093F \u0915\u0930\u0947\u0902\u0964",
2113
+ waiting: "\u092B\u093C\u094B\u0928 \u092A\u0930 \u092A\u0941\u0937\u094D\u091F\u093F \u0915\u0940 \u092A\u094D\u0930\u0924\u0940\u0915\u094D\u0937\u093E \u0939\u0948",
2114
+ loading: "\u0915\u0928\u0947\u0915\u094D\u091F \u0939\u094B \u0930\u0939\u093E \u0939\u0948",
2115
+ confirmed: "\u092A\u0941\u0937\u094D\u091F\u093F \u0939\u094B \u0917\u0908",
2116
+ rejected: "\u0905\u0938\u094D\u0935\u0940\u0915\u0943\u0924",
2117
+ expired: "\u0905\u0928\u0941\u0930\u094B\u0927 \u0915\u0940 \u0938\u092E\u092F \u0938\u0940\u092E\u093E \u0938\u092E\u093E\u092A\u094D\u0924",
2118
+ error: "\u0938\u0924\u094D\u092F\u093E\u092A\u0928 \u0909\u092A\u0932\u092C\u094D\u0927 \u0928\u0939\u0940\u0902 \u0939\u0948",
2119
+ retry: "\u092B\u093F\u0930 \u0915\u094B\u0936\u093F\u0936 \u0915\u0930\u0947\u0902",
2120
+ refresh: "\u0928\u092F\u093E \u0905\u0928\u0941\u0930\u094B\u0927",
2121
+ updating: "\u0915\u094B\u0921 \u0905\u092A\u0921\u0947\u091F \u0939\u094B \u0930\u0939\u093E \u0939\u0948",
2122
+ close: "\u092C\u0902\u0926 \u0915\u0930\u0947\u0902",
2123
+ cancel: "\u0930\u0926\u094D\u0926 \u0915\u0930\u0947\u0902",
2124
+ autoUpdate: "\u0915\u094B\u0921 \u0939\u0930 30 \u0938\u0947\u0915\u0902\u0921 \u092E\u0947\u0902 \u0905\u092A\u0928\u0947 \u0906\u092A \u092C\u0926\u0932\u0924\u093E \u0939\u0948",
2125
+ expiring: "\u0905\u0928\u0941\u0930\u094B\u0927 \u0915\u0940 \u0938\u092E\u092F-\u0938\u0940\u092E\u093E \u0938\u092E\u093E\u092A\u094D\u0924 \u0939\u094B\u0928\u0947 \u0935\u093E\u0932\u0940 \u0939\u0948"
2126
+ };
2127
+
2128
+ // src/locales/id.json
2129
+ var id_default = {
2130
+ bind: "Tautkan ponsel Anda",
2131
+ confirm: "Konfirmasi verifikasi",
2132
+ scan: "Pindai dengan AuthLink",
2133
+ code: "Kode pencocokan",
2134
+ help: "Pastikan angka di aplikasi cocok, lalu konfirmasi di ponsel Anda.",
2135
+ waiting: "Menunggu konfirmasi dari ponsel",
2136
+ loading: "Menghubungkan",
2137
+ confirmed: "Dikonfirmasi",
2138
+ rejected: "Ditolak",
2139
+ expired: "Permintaan kedaluwarsa",
2140
+ error: "Verifikasi tidak tersedia",
2141
+ retry: "Coba lagi",
2142
+ refresh: "Permintaan baru",
2143
+ updating: "Memperbarui kode",
2144
+ close: "Tutup",
2145
+ cancel: "Batal",
2146
+ autoUpdate: "Kode diperbarui otomatis setiap 30 detik",
2147
+ expiring: "Permintaan akan segera kedaluwarsa"
2148
+ };
2149
+
2150
+ // src/locales/ms.json
2151
+ var ms_default = {
2152
+ bind: "Pautkan telefon anda",
2153
+ confirm: "Sahkan pengesahan",
2154
+ scan: "Imbas dengan AuthLink",
2155
+ code: "Kod padanan",
2156
+ help: "Pastikan nombor dalam aplikasi sepadan, kemudian sahkan pada telefon anda.",
2157
+ waiting: "Menunggu pengesahan daripada telefon",
2158
+ loading: "Sedang menyambung",
2159
+ confirmed: "Disahkan",
2160
+ rejected: "Ditolak",
2161
+ expired: "Permintaan telah tamat tempoh",
2162
+ error: "Pengesahan tidak tersedia",
2163
+ retry: "Cuba lagi",
2164
+ refresh: "Permintaan baharu",
2165
+ updating: "Sedang mengemas kini kod",
2166
+ close: "Tutup",
2167
+ cancel: "Batal",
2168
+ autoUpdate: "Kod dikemas kini secara automatik setiap 30 saat",
2169
+ expiring: "Permintaan akan tamat tempoh tidak lama lagi"
2170
+ };
2171
+
2172
+ // src/locales/pt.json
2173
+ var pt_default = {
2174
+ bind: "Vincule seu celular",
2175
+ confirm: "Confirme a verifica\xE7\xE3o",
2176
+ scan: "Escaneie com o AuthLink",
2177
+ code: "C\xF3digo de compara\xE7\xE3o",
2178
+ help: "Confira se os n\xFAmeros no aplicativo coincidem e confirme no celular.",
2179
+ waiting: "Aguardando confirma\xE7\xE3o no celular",
2180
+ loading: "Conectando",
2181
+ confirmed: "Confirmado",
2182
+ rejected: "Recusado",
2183
+ expired: "A solicita\xE7\xE3o expirou",
2184
+ error: "A verifica\xE7\xE3o est\xE1 indispon\xEDvel",
2185
+ retry: "Tentar novamente",
2186
+ refresh: "Nova solicita\xE7\xE3o",
2187
+ updating: "Atualizando o c\xF3digo",
2188
+ close: "Fechar",
2189
+ cancel: "Cancelar",
2190
+ autoUpdate: "O c\xF3digo \xE9 atualizado automaticamente a cada 30 segundos",
2191
+ expiring: "A solicita\xE7\xE3o est\xE1 prestes a expirar"
2192
+ };
2193
+
2194
+ // src/locales/th.json
2195
+ var th_default = {
2196
+ bind: "\u0E1C\u0E39\u0E01\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C\u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13",
2197
+ confirm: "\u0E22\u0E37\u0E19\u0E22\u0E31\u0E19\u0E01\u0E32\u0E23\u0E15\u0E23\u0E27\u0E08\u0E2A\u0E2D\u0E1A",
2198
+ scan: "\u0E2A\u0E41\u0E01\u0E19\u0E14\u0E49\u0E27\u0E22 AuthLink",
2199
+ code: "\u0E23\u0E2B\u0E31\u0E2A\u0E2A\u0E33\u0E2B\u0E23\u0E31\u0E1A\u0E40\u0E1B\u0E23\u0E35\u0E22\u0E1A\u0E40\u0E17\u0E35\u0E22\u0E1A",
2200
+ help: "\u0E15\u0E23\u0E27\u0E08\u0E2A\u0E2D\u0E1A\u0E27\u0E48\u0E32\u0E15\u0E31\u0E27\u0E40\u0E25\u0E02\u0E43\u0E19\u0E41\u0E2D\u0E1B\u0E15\u0E23\u0E07\u0E01\u0E31\u0E19 \u0E41\u0E25\u0E49\u0E27\u0E08\u0E36\u0E07\u0E22\u0E37\u0E19\u0E22\u0E31\u0E19\u0E1A\u0E19\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C\u0E02\u0E2D\u0E07\u0E04\u0E38\u0E13",
2201
+ waiting: "\u0E01\u0E33\u0E25\u0E31\u0E07\u0E23\u0E2D\u0E01\u0E32\u0E23\u0E22\u0E37\u0E19\u0E22\u0E31\u0E19\u0E08\u0E32\u0E01\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C",
2202
+ loading: "\u0E01\u0E33\u0E25\u0E31\u0E07\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D",
2203
+ confirmed: "\u0E22\u0E37\u0E19\u0E22\u0E31\u0E19\u0E41\u0E25\u0E49\u0E27",
2204
+ rejected: "\u0E1B\u0E0F\u0E34\u0E40\u0E2A\u0E18\u0E41\u0E25\u0E49\u0E27",
2205
+ expired: "\u0E04\u0E33\u0E02\u0E2D\u0E2B\u0E21\u0E14\u0E2D\u0E32\u0E22\u0E38",
2206
+ error: "\u0E44\u0E21\u0E48\u0E2A\u0E32\u0E21\u0E32\u0E23\u0E16\u0E15\u0E23\u0E27\u0E08\u0E2A\u0E2D\u0E1A\u0E44\u0E14\u0E49\u0E43\u0E19\u0E02\u0E13\u0E30\u0E19\u0E35\u0E49",
2207
+ retry: "\u0E25\u0E2D\u0E07\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07",
2208
+ refresh: "\u0E04\u0E33\u0E02\u0E2D\u0E43\u0E2B\u0E21\u0E48",
2209
+ updating: "\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2D\u0E31\u0E1B\u0E40\u0E14\u0E15\u0E23\u0E2B\u0E31\u0E2A",
2210
+ close: "\u0E1B\u0E34\u0E14",
2211
+ cancel: "\u0E22\u0E01\u0E40\u0E25\u0E34\u0E01",
2212
+ autoUpdate: "\u0E23\u0E2B\u0E31\u0E2A\u0E08\u0E30\u0E2D\u0E31\u0E1B\u0E40\u0E14\u0E15\u0E2D\u0E31\u0E15\u0E42\u0E19\u0E21\u0E31\u0E15\u0E34\u0E17\u0E38\u0E01 30 \u0E27\u0E34\u0E19\u0E32\u0E17\u0E35",
2213
+ expiring: "\u0E04\u0E33\u0E02\u0E2D\u0E43\u0E01\u0E25\u0E49\u0E2B\u0E21\u0E14\u0E2D\u0E32\u0E22\u0E38"
2214
+ };
2215
+
2216
+ // src/locales/vi.json
2217
+ var vi_default = {
2218
+ bind: "Li\xEAn k\u1EBFt \u0111i\u1EC7n tho\u1EA1i",
2219
+ confirm: "X\xE1c nh\u1EADn x\xE1c th\u1EF1c",
2220
+ scan: "Qu\xE9t b\u1EB1ng AuthLink",
2221
+ code: "M\xE3 \u0111\u1ED1i chi\u1EBFu",
2222
+ help: "Ki\u1EC3m tra c\xE1c ch\u1EEF s\u1ED1 trong \u1EE9ng d\u1EE5ng c\xF3 kh\u1EDBp kh\xF4ng, r\u1ED3i x\xE1c nh\u1EADn tr\xEAn \u0111i\u1EC7n tho\u1EA1i.",
2223
+ waiting: "\u0110ang ch\u1EDD x\xE1c nh\u1EADn t\u1EEB \u0111i\u1EC7n tho\u1EA1i",
2224
+ loading: "\u0110ang k\u1EBFt n\u1ED1i",
2225
+ confirmed: "\u0110\xE3 x\xE1c nh\u1EADn",
2226
+ rejected: "\u0110\xE3 t\u1EEB ch\u1ED1i",
2227
+ expired: "Y\xEAu c\u1EA7u \u0111\xE3 h\u1EBFt h\u1EA1n",
2228
+ error: "Hi\u1EC7n kh\xF4ng th\u1EC3 x\xE1c th\u1EF1c",
2229
+ retry: "Th\u1EED l\u1EA1i",
2230
+ refresh: "Y\xEAu c\u1EA7u m\u1EDBi",
2231
+ updating: "\u0110ang c\u1EADp nh\u1EADt m\xE3",
2232
+ close: "\u0110\xF3ng",
2233
+ cancel: "H\u1EE7y",
2234
+ autoUpdate: "M\xE3 t\u1EF1 \u0111\u1ED9ng c\u1EADp nh\u1EADt m\u1ED7i 30 gi\xE2y",
2235
+ expiring: "Y\xEAu c\u1EA7u s\u1EAFp h\u1EBFt h\u1EA1n"
2236
+ };
2237
+
2238
+ // src/i18n.ts
2239
+ var supportedLocales = ["en", "zh-CN", "zh-TW", "ja", "bn", "es", "fil", "hi", "id", "ms", "pt", "th", "vi"];
2240
+ var messages = {
2241
+ "en": en_default,
2242
+ "zh-CN": zh_CN_default,
2243
+ "zh-TW": zh_TW_default,
2244
+ "ja": ja_default,
2245
+ "bn": bn_default,
2246
+ "es": es_default,
2247
+ "fil": fil_default,
2248
+ "hi": hi_default,
2249
+ "id": id_default,
2250
+ "ms": ms_default,
2251
+ "pt": pt_default,
2252
+ "th": th_default,
2253
+ "vi": vi_default
2254
+ };
2255
+ var defaultLocale = "zh-CN";
2256
+ function resolveLocale(locale) {
2257
+ return locale && Object.hasOwn(messages, locale) ? locale : defaultLocale;
2258
+ }
2259
+ function getMessages(locale) {
2260
+ return messages[resolveLocale(locale)];
2261
+ }
2262
+
2263
+ // src/view.ts
2264
+ var node = (tag, props, ...children) => ({ tag, props, children });
2265
+ function resolveVariant(variant) {
2266
+ return variant === "B" || variant === "C" ? variant : "A";
2267
+ }
2268
+ function widgetView(options) {
2269
+ const { snapshot: s, mode, retry, refresh } = options;
2270
+ const locale = resolveLocale(options.locale), variant = resolveVariant(options.variant);
2271
+ const l = getMessages(locale);
2272
+ const pending = s.status === "pending", hasQr = pending && (mode === "bind" || Boolean(options.showQr)) && Boolean(options.qr);
2273
+ const status = { idle: l.loading, pending: l.waiting, confirmed: l.confirmed, rejected: l.rejected, expired: l.expired, error: l.error }[s.status];
2274
+ const expiring = pending && s.secondsRemaining <= 30;
2275
+ const expiryTime = `00:${String(Math.max(0, Math.ceil(s.secondsRemaining))).padStart(2, "0")}`;
2276
+ const statusNode = () => node(
2277
+ "p",
2278
+ {
2279
+ class: "authlink-status",
2280
+ role: s.status === "error" ? "alert" : "status",
2281
+ "data-expiring": String(expiring)
2282
+ },
2283
+ node("span", { class: "authlink-status-dot", "aria-hidden": "true" }),
2284
+ expiring ? l.expiring : status,
2285
+ ...expiring ? [node("span", { class: "authlink-expiry", "aria-hidden": "true" }, ` \xB7 ${expiryTime}`)] : []
2286
+ );
2287
+ const cancel = () => options.close ? [node("button", {
2288
+ type: "button",
2289
+ class: "authlink-cancel",
2290
+ "data-authlink-focus": "cancel",
2291
+ onClick: options.close
2292
+ }, l.cancel)] : [];
2293
+ const children = [node("h3", { class: "authlink-title" }, l[mode])];
2294
+ if (hasQr) children.push(node("p", { class: "authlink-subtitle" }, l.scan));
2295
+ if (pending) {
2296
+ const qr = hasQr ? [node(
2297
+ "figure",
2298
+ { class: "authlink-qr-wrap" },
2299
+ node("img", { class: "authlink-qr", src: options.qr, alt: l.scan, width: 240, height: 240 }),
2300
+ node("figcaption", { class: "authlink-qr-caption" }, l.scan)
2301
+ )] : [];
2302
+ const seconds = Math.max(0, Math.min(30, s.codeSecondsRemaining));
2303
+ const code = node(
2304
+ "output",
2305
+ {
2306
+ class: "authlink-code",
2307
+ "aria-label": s.code ? `${l.code}: ${s.code.split("").join(" ")}` : l.updating,
2308
+ "aria-live": "polite",
2309
+ "data-testid": "authlink-code"
2310
+ },
2311
+ node("span", { class: "authlink-code-group" }, s.code?.slice(0, 3) ?? "---"),
2312
+ node("span", { class: "authlink-code-group" }, s.code?.slice(3) ?? "---")
2313
+ );
2314
+ const panel = node(
2315
+ "div",
2316
+ { class: "authlink-code-panel" },
2317
+ node("div", { class: "authlink-code-values" }, node("div", { class: "authlink-code-label" }, l.code), code),
2318
+ node(
2319
+ "div",
2320
+ {
2321
+ class: "authlink-progress",
2322
+ role: "progressbar",
2323
+ "aria-valuemin": 0,
2324
+ "aria-valuemax": 30,
2325
+ "aria-valuenow": seconds,
2326
+ "aria-label": l.autoUpdate
2327
+ },
2328
+ node("span", { class: "authlink-progress-fill", "aria-hidden": "true", style: `transform:scaleX(${seconds / 30})` })
2329
+ ),
2330
+ node("p", { class: "authlink-code-caption" }, s.code ? l.autoUpdate : l.updating)
2331
+ );
2332
+ const detail = [panel, node("p", { class: "authlink-help" }, l.help)];
2333
+ children.push(node("div", { class: "authlink-body" }, ...qr, node("div", { class: "authlink-details" }, ...detail)));
2334
+ if (variant === "A") children.push(node("div", { class: "authlink-footer" }, statusNode()));
2335
+ if (variant === "B") children.push(node("div", { class: "authlink-footer" }, statusNode(), ...cancel()));
2336
+ if (variant === "C") children.push(node("div", { class: "authlink-footer" }, statusNode(), ...cancel()));
2337
+ } else {
2338
+ children.push(statusNode());
2339
+ if (s.status === "error") children.push(node("button", {
2340
+ type: "button",
2341
+ class: "authlink-button",
2342
+ "data-authlink-focus": "retry",
2343
+ onClick: retry
2344
+ }, l.retry));
2345
+ if (refresh && ["expired", "rejected"].includes(s.status)) children.push(node("button", {
2346
+ type: "button",
2347
+ class: "authlink-button",
2348
+ "data-authlink-focus": "refresh",
2349
+ onClick: refresh
2350
+ }, l.refresh));
2351
+ }
2352
+ return node("section", {
2353
+ lang: locale,
2354
+ class: `authlink-widget ${options.className ?? ""}`.trim(),
2355
+ "data-status": s.status,
2356
+ "data-variant": variant,
2357
+ "data-has-qr": String(hasQr),
2358
+ "aria-label": l[mode]
2359
+ }, ...children);
2360
+ }
2361
+
2362
+ // src/modal.ts
2363
+ function element(value, svg = false) {
2364
+ if (typeof value === "string") return document.createTextNode(value);
2365
+ svg ||= value.tag === "svg";
2366
+ const result = svg ? document.createElementNS("http://www.w3.org/2000/svg", value.tag) : document.createElement(value.tag);
2367
+ for (const [key, v] of Object.entries(value.props)) {
2368
+ if (key === "onClick" && typeof v === "function") result.addEventListener("click", v);
2369
+ else if (v !== void 0 && v !== null) result.setAttribute(key, String(v));
2370
+ }
2371
+ result.append(...value.children.map((child) => element(child, svg)));
2372
+ return result;
2373
+ }
2374
+ function openAuthLink(options) {
2375
+ if (typeof document === "undefined" || !document.body) throw new AuthLinkError("BROWSER_REQUIRED");
2376
+ const mode = options.mode ?? options.ticket.purpose;
2377
+ const ticket = validateTicket(options.ticket, mode);
2378
+ const dialog = document.createElement("dialog");
2379
+ if (typeof dialog.showModal !== "function") throw new AuthLinkError("DIALOG_UNSUPPORTED");
2380
+ const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
2381
+ dialog.className = `authlink-dialog ${options.className ?? ""}`.trim();
2382
+ dialog.setAttribute("aria-modal", "true");
2383
+ const header = document.createElement("div");
2384
+ header.className = "authlink-header";
2385
+ const brand = document.createElement("span");
2386
+ brand.className = "authlink-brand";
2387
+ brand.textContent = "AuthLink";
2388
+ brand.tabIndex = -1;
2389
+ const closeButton = document.createElement("button");
2390
+ closeButton.type = "button";
2391
+ closeButton.className = "authlink-close";
2392
+ const closeIcon = document.createElement("span");
2393
+ closeIcon.className = "authlink-close-icon";
2394
+ closeIcon.setAttribute("aria-hidden", "true");
2395
+ closeButton.append(closeIcon);
2396
+ const content = document.createElement("div");
2397
+ content.className = "authlink-modal-content";
2398
+ header.append(brand, closeButton);
2399
+ dialog.append(header, content);
2400
+ let locale = options.locale, variant = resolveVariant(options.variant), snapshot = initialSnapshot(), closed = false;
2401
+ let abort;
2402
+ const image = qrImage(ticket);
2403
+ let progressFill, progressFrame, progressDeadline = 0;
2404
+ const motion = window.matchMedia?.("(prefers-reduced-motion: reduce)");
2405
+ const stopProgress = () => {
2406
+ if (progressFrame !== void 0) window.cancelAnimationFrame(progressFrame);
2407
+ progressFrame = void 0;
2408
+ };
2409
+ const drawProgress = () => {
2410
+ stopProgress();
2411
+ if (closed || !progressFill?.isConnected) return;
2412
+ const remaining = Math.max(0, progressDeadline - performance.now());
2413
+ progressFill.style.transform = `scaleX(${Math.min(1, remaining / 3e4)})`;
2414
+ if (remaining > 0 && !motion?.matches && typeof window.requestAnimationFrame === "function") {
2415
+ progressFrame = window.requestAnimationFrame(drawProgress);
2416
+ }
2417
+ };
2418
+ const syncProgress = (remainingMs) => {
2419
+ progressDeadline = performance.now() + remainingMs;
2420
+ drawProgress();
2421
+ };
2422
+ motion?.addEventListener?.("change", drawProgress);
2423
+ const render = () => {
2424
+ if (closed) return;
2425
+ const language = resolveLocale(locale), messages2 = getMessages(language);
2426
+ dialog.lang = language;
2427
+ dialog.setAttribute("aria-label", messages2[mode]);
2428
+ dialog.dataset.variant = variant;
2429
+ dialog.dataset.mode = mode;
2430
+ dialog.dataset.hasQr = String(snapshot.status === "pending" && (mode === "bind" || Boolean(options.showQr)));
2431
+ closeButton.setAttribute("aria-label", messages2.close);
2432
+ const focused = document.activeElement;
2433
+ const focusKey = focused instanceof HTMLElement && content.contains(focused) ? focused.dataset.authlinkFocus : void 0;
2434
+ const next = element(widgetView({
2435
+ mode,
2436
+ locale,
2437
+ variant,
2438
+ snapshot,
2439
+ qr: image,
2440
+ showQr: options.showQr,
2441
+ retry,
2442
+ refresh: options.onRefresh,
2443
+ close: () => dispose(true)
2444
+ }));
2445
+ const oldCode = content.querySelector(".authlink-code");
2446
+ const nextCode = next.querySelector(".authlink-code");
2447
+ const codeChanged = Boolean(oldCode && nextCode && snapshot.code && oldCode.textContent !== nextCode.textContent);
2448
+ if (oldCode && nextCode && oldCode.textContent === nextCode.textContent) {
2449
+ oldCode.setAttribute("aria-label", nextCode.getAttribute("aria-label"));
2450
+ nextCode.replaceWith(oldCode);
2451
+ }
2452
+ const oldProgress = content.querySelector(".authlink-progress");
2453
+ const nextProgress = next.querySelector(".authlink-progress");
2454
+ if (oldProgress && nextProgress) {
2455
+ oldProgress.setAttribute("aria-valuenow", nextProgress.getAttribute("aria-valuenow"));
2456
+ oldProgress.setAttribute("aria-label", nextProgress.getAttribute("aria-label"));
2457
+ nextProgress.replaceWith(oldProgress);
2458
+ }
2459
+ content.replaceChildren(next);
2460
+ progressFill = content.querySelector(".authlink-progress-fill") ?? void 0;
2461
+ drawProgress();
2462
+ if (codeChanged && !window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) {
2463
+ nextCode?.animate?.([{ opacity: 0.5 }, { opacity: 1 }], { duration: 180, easing: "ease-out" });
2464
+ }
2465
+ if (focusKey) (Array.from(content.querySelectorAll("[data-authlink-focus]")).find((item) => item.dataset.authlinkFocus === focusKey) ?? brand).focus({ preventScroll: true });
2466
+ };
2467
+ const dispose = (notify) => {
2468
+ if (closed) return;
2469
+ closed = true;
2470
+ abort?.abort();
2471
+ stopProgress();
2472
+ progressFill = void 0;
2473
+ motion?.removeEventListener?.("change", drawProgress);
2474
+ if (dialog.open) dialog.close();
2475
+ dialog.remove();
2476
+ if (!document.querySelector("dialog.authlink-dialog[open]")) {
2477
+ if (document.body.style.overflow === "hidden") document.body.style.overflow = document.body.dataset.authlinkOverflow ?? "";
2478
+ delete document.body.dataset.authlinkOverflow;
2479
+ }
2480
+ if (previousFocus?.isConnected) previousFocus.focus();
2481
+ if (notify) options.onClose?.();
2482
+ };
2483
+ const delivered = /* @__PURE__ */ new Set();
2484
+ const once = (status, callback) => {
2485
+ if (delivered.has(status)) return;
2486
+ delivered.add(status);
2487
+ return callback();
2488
+ };
2489
+ const retry = () => {
2490
+ if (closed) return;
2491
+ abort?.abort();
2492
+ abort = new AbortController();
2493
+ snapshot = initialSnapshot();
2494
+ render();
2495
+ void observeRequest(ticket, {
2496
+ client: options.client,
2497
+ intervalMs: options.intervalMs,
2498
+ expectedMode: mode,
2499
+ signal: abort.signal,
2500
+ onCodeTiming: syncProgress,
2501
+ onUpdate: (value) => {
2502
+ snapshot = value;
2503
+ if (value.status !== "rejected") render();
2504
+ },
2505
+ onConfirmed: (value) => once("confirmed", () => {
2506
+ dispose(false);
2507
+ try {
2508
+ return Promise.resolve(options.onConfirmed?.(value)).catch(() => options.onError?.(new AuthLinkError("CALLBACK_FAILED")));
2509
+ } catch {
2510
+ options.onError?.(new AuthLinkError("CALLBACK_FAILED"));
2511
+ }
2512
+ }),
2513
+ onRejected: (value) => once("rejected", () => {
2514
+ dispose(false);
2515
+ try {
2516
+ return Promise.resolve(options.onRejected?.(value)).catch(() => options.onError?.(new AuthLinkError("CALLBACK_FAILED")));
2517
+ } catch {
2518
+ options.onError?.(new AuthLinkError("CALLBACK_FAILED"));
2519
+ }
2520
+ }),
2521
+ onExpired: () => once("expired", () => options.onExpired?.()),
2522
+ onError: options.onError
2523
+ });
2524
+ };
2525
+ closeButton.addEventListener("click", () => dispose(true));
2526
+ dialog.addEventListener("cancel", (event) => {
2527
+ event.preventDefault();
2528
+ dispose(true);
2529
+ });
2530
+ dialog.addEventListener("close", () => dispose(true));
2531
+ render();
2532
+ document.body.append(dialog);
2533
+ try {
2534
+ dialog.showModal();
2535
+ } catch (error) {
2536
+ dialog.remove();
2537
+ throw error;
2538
+ }
2539
+ brand.focus({ preventScroll: true });
2540
+ if (document.body.dataset.authlinkOverflow === void 0) document.body.dataset.authlinkOverflow = document.body.style.overflow;
2541
+ document.body.style.overflow = "hidden";
2542
+ retry();
2543
+ return {
2544
+ close: () => dispose(true),
2545
+ destroy: () => dispose(false),
2546
+ setLocale: (value) => {
2547
+ locale = value;
2548
+ render();
2549
+ },
2550
+ setVariant: (value) => {
2551
+ variant = resolveVariant(value);
2552
+ render();
2553
+ },
2554
+ retry
2555
+ };
2556
+ }
2557
+ var openAuthLinkBind = (options) => openAuthLink({ ...options, mode: "bind" });
2558
+ var openAuthLinkConfirm = (options) => openAuthLink({ ...options, mode: "confirm" });
2559
+ export {
2560
+ AuthLinkError,
2561
+ createAuthLinkClient,
2562
+ initialSnapshot,
2563
+ observeRequest,
2564
+ openAuthLink,
2565
+ openAuthLinkBind,
2566
+ openAuthLinkConfirm,
2567
+ qrImage,
2568
+ requestKey,
2569
+ supportedLocales,
2570
+ validateTicket
2571
+ };
2572
+ //# sourceMappingURL=index.js.map