kiteline 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/auth-YhkfTv6K.mjs +60 -0
- package/dist/banner-DJdyE8-G.mjs +1067 -0
- package/dist/cli.mjs +107 -0
- package/dist/package-D46-e3oS.mjs +3 -0
- package/dist/service-1H4ozSPX.mjs +192 -0
- package/dist/show-pairing-jhFEXWi7.mjs +30 -0
- package/dist/show-pairing-u0LfkF9W.mjs +1 -0
- package/dist/src-BnClcgTs.mjs +13382 -0
- package/dist/web/apple-touch-icon-180x180.png +0 -0
- package/dist/web/assets/IconBase.es-CfbvcPwJ.js +9 -0
- package/dist/web/assets/MagnifyingGlass.es-DLNPT1gT.js +1 -0
- package/dist/web/assets/composer-B6OuQTWV.js +1 -0
- package/dist/web/assets/copy-button-DrJvfi1A.js +1 -0
- package/dist/web/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/dist/web/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/dist/web/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/dist/web/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/dist/web/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/dist/web/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/dist/web/assets/index-CA73h-Vn.css +1 -0
- package/dist/web/assets/index-Da9GDrpu.js +11 -0
- package/dist/web/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/dist/web/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/dist/web/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/dist/web/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/dist/web/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/dist/web/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/dist/web/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/dist/web/assets/lazy/core-DbyPnbRm.js +12 -0
- package/dist/web/assets/lazy/css-BGIxQoYs.js +1 -0
- package/dist/web/assets/lazy/css-DgaLhXQH.js +1 -0
- package/dist/web/assets/lazy/diff-woXpYk--.js +1 -0
- package/dist/web/assets/lazy/dockerfile-IyjqRm3v.js +1 -0
- package/dist/web/assets/lazy/engine-javascript-C30F4iNc.js +141 -0
- package/dist/web/assets/lazy/github-dark-default-DXG-b-1a.js +1 -0
- package/dist/web/assets/lazy/github-light-default-BXViO-2h.js +1 -0
- package/dist/web/assets/lazy/go-rLFTqkRN.js +1 -0
- package/dist/web/assets/lazy/html-CvFb5cGI.js +1 -0
- package/dist/web/assets/lazy/javascript-BzbQS41l.js +1 -0
- package/dist/web/assets/lazy/javascript-CpIGwZ_Y.js +1 -0
- package/dist/web/assets/lazy/json-qhed-kSA.js +1 -0
- package/dist/web/assets/lazy/jsx-Bz0zcwM4.js +1 -0
- package/dist/web/assets/lazy/markdown-BYOwaDjH.js +1 -0
- package/dist/web/assets/lazy/python-gzcpVVnB.js +1 -0
- package/dist/web/assets/lazy/rust-DpiIf5JO.js +1 -0
- package/dist/web/assets/lazy/shellscript-CLZ0U2zV.js +1 -0
- package/dist/web/assets/lazy/sql-CEbI0hMM.js +1 -0
- package/dist/web/assets/lazy/toml-CcmNWLt0.js +1 -0
- package/dist/web/assets/lazy/tsx-udAQXfEw.js +1 -0
- package/dist/web/assets/lazy/typescript-bsJCZSQ-.js +1 -0
- package/dist/web/assets/lazy/yaml-rwi0_p6S.js +1 -0
- package/dist/web/assets/new-x01_nUpy.js +1 -0
- package/dist/web/assets/prefs-CliZrwNF.js +1 -0
- package/dist/web/assets/routes-NIOlnkZY.js +1 -0
- package/dist/web/assets/s._sessionId-BtgSN22Q.js +45 -0
- package/dist/web/assets/settings-B7yar__r.js +1 -0
- package/dist/web/assets/status-BFb0ZUqi.js +1 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/index.html +40 -0
- package/dist/web/logo.svg +12 -0
- package/dist/web/manifest.webmanifest +1 -0
- package/dist/web/maskable-icon-512x512.png +0 -0
- package/dist/web/pwa-192x192.png +0 -0
- package/dist/web/pwa-512x512.png +0 -0
- package/dist/web/pwa-64x64.png +0 -0
- package/dist/web/registerSW.js +1 -0
- package/dist/web/sw.js +1 -0
- package/dist/web/workbox-dcde9eb3.js +1 -0
- package/package.json +34 -4
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
import { parseEnv } from "node:util";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { networkInterfaces } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
// ../../node_modules/.pnpm/uqr@0.1.3/node_modules/uqr/dist/index.mjs
|
|
7
|
+
var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
|
|
8
|
+
QrCodeDataType2[QrCodeDataType2["Border"] = -1] = "Border";
|
|
9
|
+
QrCodeDataType2[QrCodeDataType2["Data"] = 0] = "Data";
|
|
10
|
+
QrCodeDataType2[QrCodeDataType2["Function"] = 1] = "Function";
|
|
11
|
+
QrCodeDataType2[QrCodeDataType2["Position"] = 2] = "Position";
|
|
12
|
+
QrCodeDataType2[QrCodeDataType2["Timing"] = 3] = "Timing";
|
|
13
|
+
QrCodeDataType2[QrCodeDataType2["Alignment"] = 4] = "Alignment";
|
|
14
|
+
return QrCodeDataType2;
|
|
15
|
+
})(QrCodeDataType || {});
|
|
16
|
+
const LOW = [0, 1];
|
|
17
|
+
const MEDIUM = [1, 0];
|
|
18
|
+
const QUARTILE = [2, 3];
|
|
19
|
+
const HIGH = [3, 2];
|
|
20
|
+
const EccMap = {
|
|
21
|
+
L: LOW,
|
|
22
|
+
M: MEDIUM,
|
|
23
|
+
Q: QUARTILE,
|
|
24
|
+
H: HIGH
|
|
25
|
+
};
|
|
26
|
+
const NUMERIC_REGEX = /^\d*$/;
|
|
27
|
+
const ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+./:-]*$/;
|
|
28
|
+
const ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
|
29
|
+
const MIN_VERSION = 1;
|
|
30
|
+
const MAX_VERSION = 40;
|
|
31
|
+
const PENALTY_N1 = 3;
|
|
32
|
+
const PENALTY_N2 = 3;
|
|
33
|
+
const PENALTY_N3 = 40;
|
|
34
|
+
const PENALTY_N4 = 10;
|
|
35
|
+
const ECC_CODEWORDS_PER_BLOCK = [
|
|
36
|
+
[
|
|
37
|
+
-1,
|
|
38
|
+
7,
|
|
39
|
+
10,
|
|
40
|
+
15,
|
|
41
|
+
20,
|
|
42
|
+
26,
|
|
43
|
+
18,
|
|
44
|
+
20,
|
|
45
|
+
24,
|
|
46
|
+
30,
|
|
47
|
+
18,
|
|
48
|
+
20,
|
|
49
|
+
24,
|
|
50
|
+
26,
|
|
51
|
+
30,
|
|
52
|
+
22,
|
|
53
|
+
24,
|
|
54
|
+
28,
|
|
55
|
+
30,
|
|
56
|
+
28,
|
|
57
|
+
28,
|
|
58
|
+
28,
|
|
59
|
+
28,
|
|
60
|
+
30,
|
|
61
|
+
30,
|
|
62
|
+
26,
|
|
63
|
+
28,
|
|
64
|
+
30,
|
|
65
|
+
30,
|
|
66
|
+
30,
|
|
67
|
+
30,
|
|
68
|
+
30,
|
|
69
|
+
30,
|
|
70
|
+
30,
|
|
71
|
+
30,
|
|
72
|
+
30,
|
|
73
|
+
30,
|
|
74
|
+
30,
|
|
75
|
+
30,
|
|
76
|
+
30,
|
|
77
|
+
30
|
|
78
|
+
],
|
|
79
|
+
[
|
|
80
|
+
-1,
|
|
81
|
+
10,
|
|
82
|
+
16,
|
|
83
|
+
26,
|
|
84
|
+
18,
|
|
85
|
+
24,
|
|
86
|
+
16,
|
|
87
|
+
18,
|
|
88
|
+
22,
|
|
89
|
+
22,
|
|
90
|
+
26,
|
|
91
|
+
30,
|
|
92
|
+
22,
|
|
93
|
+
22,
|
|
94
|
+
24,
|
|
95
|
+
24,
|
|
96
|
+
28,
|
|
97
|
+
28,
|
|
98
|
+
26,
|
|
99
|
+
26,
|
|
100
|
+
26,
|
|
101
|
+
26,
|
|
102
|
+
28,
|
|
103
|
+
28,
|
|
104
|
+
28,
|
|
105
|
+
28,
|
|
106
|
+
28,
|
|
107
|
+
28,
|
|
108
|
+
28,
|
|
109
|
+
28,
|
|
110
|
+
28,
|
|
111
|
+
28,
|
|
112
|
+
28,
|
|
113
|
+
28,
|
|
114
|
+
28,
|
|
115
|
+
28,
|
|
116
|
+
28,
|
|
117
|
+
28,
|
|
118
|
+
28,
|
|
119
|
+
28,
|
|
120
|
+
28
|
|
121
|
+
],
|
|
122
|
+
[
|
|
123
|
+
-1,
|
|
124
|
+
13,
|
|
125
|
+
22,
|
|
126
|
+
18,
|
|
127
|
+
26,
|
|
128
|
+
18,
|
|
129
|
+
24,
|
|
130
|
+
18,
|
|
131
|
+
22,
|
|
132
|
+
20,
|
|
133
|
+
24,
|
|
134
|
+
28,
|
|
135
|
+
26,
|
|
136
|
+
24,
|
|
137
|
+
20,
|
|
138
|
+
30,
|
|
139
|
+
24,
|
|
140
|
+
28,
|
|
141
|
+
28,
|
|
142
|
+
26,
|
|
143
|
+
30,
|
|
144
|
+
28,
|
|
145
|
+
30,
|
|
146
|
+
30,
|
|
147
|
+
30,
|
|
148
|
+
30,
|
|
149
|
+
28,
|
|
150
|
+
30,
|
|
151
|
+
30,
|
|
152
|
+
30,
|
|
153
|
+
30,
|
|
154
|
+
30,
|
|
155
|
+
30,
|
|
156
|
+
30,
|
|
157
|
+
30,
|
|
158
|
+
30,
|
|
159
|
+
30,
|
|
160
|
+
30,
|
|
161
|
+
30,
|
|
162
|
+
30,
|
|
163
|
+
30
|
|
164
|
+
],
|
|
165
|
+
[
|
|
166
|
+
-1,
|
|
167
|
+
17,
|
|
168
|
+
28,
|
|
169
|
+
22,
|
|
170
|
+
16,
|
|
171
|
+
22,
|
|
172
|
+
28,
|
|
173
|
+
26,
|
|
174
|
+
26,
|
|
175
|
+
24,
|
|
176
|
+
28,
|
|
177
|
+
24,
|
|
178
|
+
28,
|
|
179
|
+
22,
|
|
180
|
+
24,
|
|
181
|
+
24,
|
|
182
|
+
30,
|
|
183
|
+
28,
|
|
184
|
+
28,
|
|
185
|
+
26,
|
|
186
|
+
28,
|
|
187
|
+
30,
|
|
188
|
+
24,
|
|
189
|
+
30,
|
|
190
|
+
30,
|
|
191
|
+
30,
|
|
192
|
+
30,
|
|
193
|
+
30,
|
|
194
|
+
30,
|
|
195
|
+
30,
|
|
196
|
+
30,
|
|
197
|
+
30,
|
|
198
|
+
30,
|
|
199
|
+
30,
|
|
200
|
+
30,
|
|
201
|
+
30,
|
|
202
|
+
30,
|
|
203
|
+
30,
|
|
204
|
+
30,
|
|
205
|
+
30,
|
|
206
|
+
30
|
|
207
|
+
]
|
|
208
|
+
];
|
|
209
|
+
const NUM_ERROR_CORRECTION_BLOCKS = [
|
|
210
|
+
[
|
|
211
|
+
-1,
|
|
212
|
+
1,
|
|
213
|
+
1,
|
|
214
|
+
1,
|
|
215
|
+
1,
|
|
216
|
+
1,
|
|
217
|
+
2,
|
|
218
|
+
2,
|
|
219
|
+
2,
|
|
220
|
+
2,
|
|
221
|
+
4,
|
|
222
|
+
4,
|
|
223
|
+
4,
|
|
224
|
+
4,
|
|
225
|
+
4,
|
|
226
|
+
6,
|
|
227
|
+
6,
|
|
228
|
+
6,
|
|
229
|
+
6,
|
|
230
|
+
7,
|
|
231
|
+
8,
|
|
232
|
+
8,
|
|
233
|
+
9,
|
|
234
|
+
9,
|
|
235
|
+
10,
|
|
236
|
+
12,
|
|
237
|
+
12,
|
|
238
|
+
12,
|
|
239
|
+
13,
|
|
240
|
+
14,
|
|
241
|
+
15,
|
|
242
|
+
16,
|
|
243
|
+
17,
|
|
244
|
+
18,
|
|
245
|
+
19,
|
|
246
|
+
19,
|
|
247
|
+
20,
|
|
248
|
+
21,
|
|
249
|
+
22,
|
|
250
|
+
24,
|
|
251
|
+
25
|
|
252
|
+
],
|
|
253
|
+
[
|
|
254
|
+
-1,
|
|
255
|
+
1,
|
|
256
|
+
1,
|
|
257
|
+
1,
|
|
258
|
+
2,
|
|
259
|
+
2,
|
|
260
|
+
4,
|
|
261
|
+
4,
|
|
262
|
+
4,
|
|
263
|
+
5,
|
|
264
|
+
5,
|
|
265
|
+
5,
|
|
266
|
+
8,
|
|
267
|
+
9,
|
|
268
|
+
9,
|
|
269
|
+
10,
|
|
270
|
+
10,
|
|
271
|
+
11,
|
|
272
|
+
13,
|
|
273
|
+
14,
|
|
274
|
+
16,
|
|
275
|
+
17,
|
|
276
|
+
17,
|
|
277
|
+
18,
|
|
278
|
+
20,
|
|
279
|
+
21,
|
|
280
|
+
23,
|
|
281
|
+
25,
|
|
282
|
+
26,
|
|
283
|
+
28,
|
|
284
|
+
29,
|
|
285
|
+
31,
|
|
286
|
+
33,
|
|
287
|
+
35,
|
|
288
|
+
37,
|
|
289
|
+
38,
|
|
290
|
+
40,
|
|
291
|
+
43,
|
|
292
|
+
45,
|
|
293
|
+
47,
|
|
294
|
+
49
|
|
295
|
+
],
|
|
296
|
+
[
|
|
297
|
+
-1,
|
|
298
|
+
1,
|
|
299
|
+
1,
|
|
300
|
+
2,
|
|
301
|
+
2,
|
|
302
|
+
4,
|
|
303
|
+
4,
|
|
304
|
+
6,
|
|
305
|
+
6,
|
|
306
|
+
8,
|
|
307
|
+
8,
|
|
308
|
+
8,
|
|
309
|
+
10,
|
|
310
|
+
12,
|
|
311
|
+
16,
|
|
312
|
+
12,
|
|
313
|
+
17,
|
|
314
|
+
16,
|
|
315
|
+
18,
|
|
316
|
+
21,
|
|
317
|
+
20,
|
|
318
|
+
23,
|
|
319
|
+
23,
|
|
320
|
+
25,
|
|
321
|
+
27,
|
|
322
|
+
29,
|
|
323
|
+
34,
|
|
324
|
+
34,
|
|
325
|
+
35,
|
|
326
|
+
38,
|
|
327
|
+
40,
|
|
328
|
+
43,
|
|
329
|
+
45,
|
|
330
|
+
48,
|
|
331
|
+
51,
|
|
332
|
+
53,
|
|
333
|
+
56,
|
|
334
|
+
59,
|
|
335
|
+
62,
|
|
336
|
+
65,
|
|
337
|
+
68
|
|
338
|
+
],
|
|
339
|
+
[
|
|
340
|
+
-1,
|
|
341
|
+
1,
|
|
342
|
+
1,
|
|
343
|
+
2,
|
|
344
|
+
4,
|
|
345
|
+
4,
|
|
346
|
+
4,
|
|
347
|
+
5,
|
|
348
|
+
6,
|
|
349
|
+
8,
|
|
350
|
+
8,
|
|
351
|
+
11,
|
|
352
|
+
11,
|
|
353
|
+
16,
|
|
354
|
+
16,
|
|
355
|
+
18,
|
|
356
|
+
16,
|
|
357
|
+
19,
|
|
358
|
+
21,
|
|
359
|
+
25,
|
|
360
|
+
25,
|
|
361
|
+
25,
|
|
362
|
+
34,
|
|
363
|
+
30,
|
|
364
|
+
32,
|
|
365
|
+
35,
|
|
366
|
+
37,
|
|
367
|
+
40,
|
|
368
|
+
42,
|
|
369
|
+
45,
|
|
370
|
+
48,
|
|
371
|
+
51,
|
|
372
|
+
54,
|
|
373
|
+
57,
|
|
374
|
+
60,
|
|
375
|
+
63,
|
|
376
|
+
66,
|
|
377
|
+
70,
|
|
378
|
+
74,
|
|
379
|
+
77,
|
|
380
|
+
81
|
|
381
|
+
]
|
|
382
|
+
];
|
|
383
|
+
var QrCode = class {
|
|
384
|
+
constructor(version, ecc, dataCodewords, msk) {
|
|
385
|
+
this.version = version;
|
|
386
|
+
this.ecc = ecc;
|
|
387
|
+
if (version < MIN_VERSION || version > MAX_VERSION) throw new RangeError("Version value out of range");
|
|
388
|
+
if (msk < -1 || msk > 7) throw new RangeError("Mask value out of range");
|
|
389
|
+
this.size = version * 4 + 17;
|
|
390
|
+
const row = Array.from({ length: this.size }).fill(false);
|
|
391
|
+
for (let i = 0; i < this.size; i++) {
|
|
392
|
+
this.modules.push(row.slice());
|
|
393
|
+
this.types.push(row.map(() => 0));
|
|
394
|
+
}
|
|
395
|
+
this.drawFunctionPatterns();
|
|
396
|
+
const allCodewords = this.addEccAndInterleave(dataCodewords);
|
|
397
|
+
this.drawCodewords(allCodewords);
|
|
398
|
+
if (msk === -1) {
|
|
399
|
+
let minPenalty = 1e9;
|
|
400
|
+
for (let i = 0; i < 8; i++) {
|
|
401
|
+
this.applyMask(i);
|
|
402
|
+
this.drawFormatBits(i);
|
|
403
|
+
const penalty = this.getPenaltyScore();
|
|
404
|
+
if (penalty < minPenalty) {
|
|
405
|
+
msk = i;
|
|
406
|
+
minPenalty = penalty;
|
|
407
|
+
}
|
|
408
|
+
this.applyMask(i);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
this.mask = msk;
|
|
412
|
+
this.applyMask(msk);
|
|
413
|
+
this.drawFormatBits(msk);
|
|
414
|
+
}
|
|
415
|
+
size;
|
|
416
|
+
mask;
|
|
417
|
+
modules = [];
|
|
418
|
+
types = [];
|
|
419
|
+
getModule(x, y) {
|
|
420
|
+
return x >= 0 && x < this.size && y >= 0 && y < this.size && this.modules[y][x];
|
|
421
|
+
}
|
|
422
|
+
drawFunctionPatterns() {
|
|
423
|
+
for (let i = 0; i < this.size; i++) {
|
|
424
|
+
this.setFunctionModule(6, i, i % 2 === 0, QrCodeDataType.Timing);
|
|
425
|
+
this.setFunctionModule(i, 6, i % 2 === 0, QrCodeDataType.Timing);
|
|
426
|
+
}
|
|
427
|
+
this.drawFinderPattern(3, 3);
|
|
428
|
+
this.drawFinderPattern(this.size - 4, 3);
|
|
429
|
+
this.drawFinderPattern(3, this.size - 4);
|
|
430
|
+
const alignPatPos = this.getAlignmentPatternPositions();
|
|
431
|
+
const numAlign = alignPatPos.length;
|
|
432
|
+
for (let i = 0; i < numAlign; i++) for (let j = 0; j < numAlign; j++) if (!(i === 0 && j === 0 || i === 0 && j === numAlign - 1 || i === numAlign - 1 && j === 0)) this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
|
433
|
+
this.drawFormatBits(0);
|
|
434
|
+
this.drawVersion();
|
|
435
|
+
}
|
|
436
|
+
drawFormatBits(mask) {
|
|
437
|
+
const data = this.ecc[1] << 3 | mask;
|
|
438
|
+
let rem = data;
|
|
439
|
+
for (let i = 0; i < 10; i++) rem = rem << 1 ^ (rem >>> 9) * 1335;
|
|
440
|
+
const bits = (data << 10 | rem) ^ 21522;
|
|
441
|
+
for (let i = 0; i <= 5; i++) this.setFunctionModule(8, i, getBit(bits, i));
|
|
442
|
+
this.setFunctionModule(8, 7, getBit(bits, 6));
|
|
443
|
+
this.setFunctionModule(8, 8, getBit(bits, 7));
|
|
444
|
+
this.setFunctionModule(7, 8, getBit(bits, 8));
|
|
445
|
+
for (let i = 9; i < 15; i++) this.setFunctionModule(14 - i, 8, getBit(bits, i));
|
|
446
|
+
for (let i = 0; i < 8; i++) this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
|
|
447
|
+
for (let i = 8; i < 15; i++) this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
|
|
448
|
+
this.setFunctionModule(8, this.size - 8, true);
|
|
449
|
+
}
|
|
450
|
+
drawVersion() {
|
|
451
|
+
if (this.version < 7) return;
|
|
452
|
+
let rem = this.version;
|
|
453
|
+
for (let i = 0; i < 12; i++) rem = rem << 1 ^ (rem >>> 11) * 7973;
|
|
454
|
+
const bits = this.version << 12 | rem;
|
|
455
|
+
for (let i = 0; i < 18; i++) {
|
|
456
|
+
const color = getBit(bits, i);
|
|
457
|
+
const a = this.size - 11 + i % 3;
|
|
458
|
+
const b = Math.floor(i / 3);
|
|
459
|
+
this.setFunctionModule(a, b, color);
|
|
460
|
+
this.setFunctionModule(b, a, color);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
drawFinderPattern(x, y) {
|
|
464
|
+
for (let dy = -4; dy <= 4; dy++) for (let dx = -4; dx <= 4; dx++) {
|
|
465
|
+
const dist = Math.max(Math.abs(dx), Math.abs(dy));
|
|
466
|
+
const xx = x + dx;
|
|
467
|
+
const yy = y + dy;
|
|
468
|
+
if (xx >= 0 && xx < this.size && yy >= 0 && yy < this.size) this.setFunctionModule(xx, yy, dist !== 2 && dist !== 4, QrCodeDataType.Position);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
drawAlignmentPattern(x, y) {
|
|
472
|
+
for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1, QrCodeDataType.Alignment);
|
|
473
|
+
}
|
|
474
|
+
setFunctionModule(x, y, isDark, type = QrCodeDataType.Function) {
|
|
475
|
+
this.modules[y][x] = isDark;
|
|
476
|
+
this.types[y][x] = type;
|
|
477
|
+
}
|
|
478
|
+
addEccAndInterleave(data) {
|
|
479
|
+
const ver = this.version;
|
|
480
|
+
const ecl = this.ecc;
|
|
481
|
+
if (data.length !== getNumDataCodewords(ver, ecl)) throw new RangeError("Invalid argument");
|
|
482
|
+
const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
|
|
483
|
+
const blockEccLen = ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver];
|
|
484
|
+
const rawCodewords = Math.floor(getNumRawDataModules(ver) / 8);
|
|
485
|
+
const numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
486
|
+
const shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
|
487
|
+
const blocks = [];
|
|
488
|
+
const rsDiv = reedSolomonComputeDivisor(blockEccLen);
|
|
489
|
+
for (let i = 0, k = 0; i < numBlocks; i++) {
|
|
490
|
+
const dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
|
|
491
|
+
k += dat.length;
|
|
492
|
+
const ecc = reedSolomonComputeRemainder(dat, rsDiv);
|
|
493
|
+
if (i < numShortBlocks) dat.push(0);
|
|
494
|
+
blocks.push(dat.concat(ecc));
|
|
495
|
+
}
|
|
496
|
+
const result = [];
|
|
497
|
+
for (let i = 0; i < blocks[0].length; i++) blocks.forEach((block, j) => {
|
|
498
|
+
if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks) result.push(block[i]);
|
|
499
|
+
});
|
|
500
|
+
return result;
|
|
501
|
+
}
|
|
502
|
+
drawCodewords(data) {
|
|
503
|
+
if (data.length !== Math.floor(getNumRawDataModules(this.version) / 8)) throw new RangeError("Invalid argument");
|
|
504
|
+
let i = 0;
|
|
505
|
+
for (let right = this.size - 1; right >= 1; right -= 2) {
|
|
506
|
+
if (right === 6) right = 5;
|
|
507
|
+
for (let vert = 0; vert < this.size; vert++) for (let j = 0; j < 2; j++) {
|
|
508
|
+
const x = right - j;
|
|
509
|
+
const y = (right + 1 & 2) === 0 ? this.size - 1 - vert : vert;
|
|
510
|
+
if (!this.types[y][x] && i < data.length * 8) {
|
|
511
|
+
this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
|
|
512
|
+
i++;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
applyMask(mask) {
|
|
518
|
+
if (mask < 0 || mask > 7) throw new RangeError("Mask value out of range");
|
|
519
|
+
for (let y = 0; y < this.size; y++) for (let x = 0; x < this.size; x++) {
|
|
520
|
+
let invert;
|
|
521
|
+
switch (mask) {
|
|
522
|
+
case 0:
|
|
523
|
+
invert = (x + y) % 2 === 0;
|
|
524
|
+
break;
|
|
525
|
+
case 1:
|
|
526
|
+
invert = y % 2 === 0;
|
|
527
|
+
break;
|
|
528
|
+
case 2:
|
|
529
|
+
invert = x % 3 === 0;
|
|
530
|
+
break;
|
|
531
|
+
case 3:
|
|
532
|
+
invert = (x + y) % 3 === 0;
|
|
533
|
+
break;
|
|
534
|
+
case 4:
|
|
535
|
+
invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0;
|
|
536
|
+
break;
|
|
537
|
+
case 5:
|
|
538
|
+
invert = x * y % 2 + x * y % 3 === 0;
|
|
539
|
+
break;
|
|
540
|
+
case 6:
|
|
541
|
+
invert = (x * y % 2 + x * y % 3) % 2 === 0;
|
|
542
|
+
break;
|
|
543
|
+
case 7:
|
|
544
|
+
invert = ((x + y) % 2 + x * y % 3) % 2 === 0;
|
|
545
|
+
break;
|
|
546
|
+
default: throw new Error("Unreachable");
|
|
547
|
+
}
|
|
548
|
+
if (!this.types[y][x] && invert) this.modules[y][x] = !this.modules[y][x];
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
getPenaltyScore() {
|
|
552
|
+
let result = 0;
|
|
553
|
+
for (let y = 0; y < this.size; y++) {
|
|
554
|
+
let runColor = false;
|
|
555
|
+
let runX = 0;
|
|
556
|
+
const runHistory = [
|
|
557
|
+
0,
|
|
558
|
+
0,
|
|
559
|
+
0,
|
|
560
|
+
0,
|
|
561
|
+
0,
|
|
562
|
+
0,
|
|
563
|
+
0
|
|
564
|
+
];
|
|
565
|
+
for (let x = 0; x < this.size; x++) if (this.modules[y][x] === runColor) {
|
|
566
|
+
runX++;
|
|
567
|
+
if (runX === 5) result += PENALTY_N1;
|
|
568
|
+
else if (runX > 5) result++;
|
|
569
|
+
} else {
|
|
570
|
+
this.finderPenaltyAddHistory(runX, runHistory);
|
|
571
|
+
if (!runColor) result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
572
|
+
runColor = this.modules[y][x];
|
|
573
|
+
runX = 1;
|
|
574
|
+
}
|
|
575
|
+
result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
|
|
576
|
+
}
|
|
577
|
+
for (let x = 0; x < this.size; x++) {
|
|
578
|
+
let runColor = false;
|
|
579
|
+
let runY = 0;
|
|
580
|
+
const runHistory = [
|
|
581
|
+
0,
|
|
582
|
+
0,
|
|
583
|
+
0,
|
|
584
|
+
0,
|
|
585
|
+
0,
|
|
586
|
+
0,
|
|
587
|
+
0
|
|
588
|
+
];
|
|
589
|
+
for (let y = 0; y < this.size; y++) if (this.modules[y][x] === runColor) {
|
|
590
|
+
runY++;
|
|
591
|
+
if (runY === 5) result += PENALTY_N1;
|
|
592
|
+
else if (runY > 5) result++;
|
|
593
|
+
} else {
|
|
594
|
+
this.finderPenaltyAddHistory(runY, runHistory);
|
|
595
|
+
if (!runColor) result += this.finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
596
|
+
runColor = this.modules[y][x];
|
|
597
|
+
runY = 1;
|
|
598
|
+
}
|
|
599
|
+
result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
|
|
600
|
+
}
|
|
601
|
+
for (let y = 0; y < this.size - 1; y++) for (let x = 0; x < this.size - 1; x++) {
|
|
602
|
+
const color = this.modules[y][x];
|
|
603
|
+
if (color === this.modules[y][x + 1] && color === this.modules[y + 1][x] && color === this.modules[y + 1][x + 1]) result += PENALTY_N2;
|
|
604
|
+
}
|
|
605
|
+
let dark = 0;
|
|
606
|
+
for (const row of this.modules) dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
|
|
607
|
+
const total = this.size * this.size;
|
|
608
|
+
const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
|
|
609
|
+
result += k * PENALTY_N4;
|
|
610
|
+
return result;
|
|
611
|
+
}
|
|
612
|
+
getAlignmentPatternPositions() {
|
|
613
|
+
if (this.version === 1) return [];
|
|
614
|
+
else {
|
|
615
|
+
const numAlign = Math.floor(this.version / 7) + 2;
|
|
616
|
+
const step = this.version === 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
|
|
617
|
+
const result = [6];
|
|
618
|
+
for (let pos = this.size - 7; result.length < numAlign; pos -= step) result.splice(1, 0, pos);
|
|
619
|
+
return result;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
finderPenaltyCountPatterns(runHistory) {
|
|
623
|
+
const n = runHistory[1];
|
|
624
|
+
const core = n > 0 && runHistory[2] === n && runHistory[3] === n * 3 && runHistory[4] === n && runHistory[5] === n;
|
|
625
|
+
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
|
626
|
+
}
|
|
627
|
+
finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
|
|
628
|
+
if (currentRunColor) {
|
|
629
|
+
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
630
|
+
currentRunLength = 0;
|
|
631
|
+
}
|
|
632
|
+
currentRunLength += this.size;
|
|
633
|
+
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
634
|
+
return this.finderPenaltyCountPatterns(runHistory);
|
|
635
|
+
}
|
|
636
|
+
finderPenaltyAddHistory(currentRunLength, runHistory) {
|
|
637
|
+
if (runHistory[0] === 0) currentRunLength += this.size;
|
|
638
|
+
runHistory.pop();
|
|
639
|
+
runHistory.unshift(currentRunLength);
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
function appendBits(val, len, bb) {
|
|
643
|
+
if (len < 0 || len > 31 || val >>> len !== 0) throw new RangeError("Value out of range");
|
|
644
|
+
for (let i = len - 1; i >= 0; i--) bb.push(val >>> i & 1);
|
|
645
|
+
}
|
|
646
|
+
function getBit(x, i) {
|
|
647
|
+
return (x >>> i & 1) !== 0;
|
|
648
|
+
}
|
|
649
|
+
var QrSegment = class {
|
|
650
|
+
constructor(mode, numChars, bitData) {
|
|
651
|
+
this.mode = mode;
|
|
652
|
+
this.numChars = numChars;
|
|
653
|
+
this.bitData = bitData;
|
|
654
|
+
if (numChars < 0) throw new RangeError("Invalid argument");
|
|
655
|
+
this.bitData = bitData.slice();
|
|
656
|
+
}
|
|
657
|
+
getData() {
|
|
658
|
+
return this.bitData.slice();
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
const MODE_NUMERIC = [
|
|
662
|
+
1,
|
|
663
|
+
10,
|
|
664
|
+
12,
|
|
665
|
+
14
|
|
666
|
+
];
|
|
667
|
+
const MODE_ALPHANUMERIC = [
|
|
668
|
+
2,
|
|
669
|
+
9,
|
|
670
|
+
11,
|
|
671
|
+
13
|
|
672
|
+
];
|
|
673
|
+
const MODE_BYTE = [
|
|
674
|
+
4,
|
|
675
|
+
8,
|
|
676
|
+
16,
|
|
677
|
+
16
|
|
678
|
+
];
|
|
679
|
+
function numCharCountBits(mode, ver) {
|
|
680
|
+
return mode[Math.floor((ver + 7) / 17) + 1];
|
|
681
|
+
}
|
|
682
|
+
function makeBytes(data) {
|
|
683
|
+
const bb = [];
|
|
684
|
+
for (const b of data) appendBits(b, 8, bb);
|
|
685
|
+
return new QrSegment(MODE_BYTE, data.length, bb);
|
|
686
|
+
}
|
|
687
|
+
function makeNumeric(digits) {
|
|
688
|
+
if (!isNumeric(digits)) throw new RangeError("String contains non-numeric characters");
|
|
689
|
+
const bb = [];
|
|
690
|
+
for (let i = 0; i < digits.length;) {
|
|
691
|
+
const n = Math.min(digits.length - i, 3);
|
|
692
|
+
appendBits(Number.parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
|
|
693
|
+
i += n;
|
|
694
|
+
}
|
|
695
|
+
return new QrSegment(MODE_NUMERIC, digits.length, bb);
|
|
696
|
+
}
|
|
697
|
+
function makeAlphanumeric(text) {
|
|
698
|
+
if (!isAlphanumeric(text)) throw new RangeError("String contains unencodable characters in alphanumeric mode");
|
|
699
|
+
const bb = [];
|
|
700
|
+
let i = 0;
|
|
701
|
+
for (; i + 2 <= text.length; i += 2) {
|
|
702
|
+
let temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
|
|
703
|
+
temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
|
|
704
|
+
appendBits(temp, 11, bb);
|
|
705
|
+
}
|
|
706
|
+
if (i < text.length) appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
|
|
707
|
+
return new QrSegment(MODE_ALPHANUMERIC, text.length, bb);
|
|
708
|
+
}
|
|
709
|
+
function makeSegments(text) {
|
|
710
|
+
if (text === "") return [];
|
|
711
|
+
else if (isNumeric(text)) return [makeNumeric(text)];
|
|
712
|
+
else if (isAlphanumeric(text)) return [makeAlphanumeric(text)];
|
|
713
|
+
else return [makeBytes(toUtf8ByteArray(text))];
|
|
714
|
+
}
|
|
715
|
+
function isNumeric(text) {
|
|
716
|
+
return NUMERIC_REGEX.test(text);
|
|
717
|
+
}
|
|
718
|
+
function isAlphanumeric(text) {
|
|
719
|
+
return ALPHANUMERIC_REGEX.test(text);
|
|
720
|
+
}
|
|
721
|
+
function getTotalBits(segs, version) {
|
|
722
|
+
let result = 0;
|
|
723
|
+
for (const seg of segs) {
|
|
724
|
+
const ccbits = numCharCountBits(seg.mode, version);
|
|
725
|
+
if (seg.numChars >= 1 << ccbits) return Number.POSITIVE_INFINITY;
|
|
726
|
+
result += 4 + ccbits + seg.bitData.length;
|
|
727
|
+
}
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
function toUtf8ByteArray(str) {
|
|
731
|
+
str = encodeURI(str);
|
|
732
|
+
const result = [];
|
|
733
|
+
for (let i = 0; i < str.length; i++) if (str.charAt(i) !== "%") result.push(str.charCodeAt(i));
|
|
734
|
+
else {
|
|
735
|
+
result.push(Number.parseInt(str.substring(i + 1, i + 3), 16));
|
|
736
|
+
i += 2;
|
|
737
|
+
}
|
|
738
|
+
return result;
|
|
739
|
+
}
|
|
740
|
+
function getNumRawDataModules(ver) {
|
|
741
|
+
if (ver < MIN_VERSION || ver > MAX_VERSION) throw new RangeError("Version number out of range");
|
|
742
|
+
let result = (16 * ver + 128) * ver + 64;
|
|
743
|
+
if (ver >= 2) {
|
|
744
|
+
const numAlign = Math.floor(ver / 7) + 2;
|
|
745
|
+
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
746
|
+
if (ver >= 7) result -= 36;
|
|
747
|
+
}
|
|
748
|
+
return result;
|
|
749
|
+
}
|
|
750
|
+
function getNumDataCodewords(ver, ecl) {
|
|
751
|
+
return Math.floor(getNumRawDataModules(ver) / 8) - ECC_CODEWORDS_PER_BLOCK[ecl[0]][ver] * NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
|
|
752
|
+
}
|
|
753
|
+
function reedSolomonComputeDivisor(degree) {
|
|
754
|
+
if (degree < 1 || degree > 255) throw new RangeError("Degree out of range");
|
|
755
|
+
const result = [];
|
|
756
|
+
for (let i = 0; i < degree - 1; i++) result.push(0);
|
|
757
|
+
result.push(1);
|
|
758
|
+
let root = 1;
|
|
759
|
+
for (let i = 0; i < degree; i++) {
|
|
760
|
+
for (let j = 0; j < result.length; j++) {
|
|
761
|
+
result[j] = reedSolomonMultiply(result[j], root);
|
|
762
|
+
if (j + 1 < result.length) result[j] ^= result[j + 1];
|
|
763
|
+
}
|
|
764
|
+
root = reedSolomonMultiply(root, 2);
|
|
765
|
+
}
|
|
766
|
+
return result;
|
|
767
|
+
}
|
|
768
|
+
function reedSolomonComputeRemainder(data, divisor) {
|
|
769
|
+
const result = divisor.map((_) => 0);
|
|
770
|
+
for (const b of data) {
|
|
771
|
+
const factor = b ^ result.shift();
|
|
772
|
+
result.push(0);
|
|
773
|
+
divisor.forEach((coef, i) => result[i] ^= reedSolomonMultiply(coef, factor));
|
|
774
|
+
}
|
|
775
|
+
return result;
|
|
776
|
+
}
|
|
777
|
+
function reedSolomonMultiply(x, y) {
|
|
778
|
+
if (x >>> 8 !== 0 || y >>> 8 !== 0) throw new RangeError("Byte out of range");
|
|
779
|
+
let z = 0;
|
|
780
|
+
for (let i = 7; i >= 0; i--) {
|
|
781
|
+
z = z << 1 ^ (z >>> 7) * 285;
|
|
782
|
+
z ^= (y >>> i & 1) * x;
|
|
783
|
+
}
|
|
784
|
+
return z;
|
|
785
|
+
}
|
|
786
|
+
function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
|
|
787
|
+
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7) throw new RangeError("Invalid value");
|
|
788
|
+
let version;
|
|
789
|
+
let dataUsedBits;
|
|
790
|
+
for (version = minVersion;; version++) {
|
|
791
|
+
const dataCapacityBits2 = getNumDataCodewords(version, ecl) * 8;
|
|
792
|
+
const usedBits = getTotalBits(segs, version);
|
|
793
|
+
if (usedBits <= dataCapacityBits2) {
|
|
794
|
+
dataUsedBits = usedBits;
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
if (version >= maxVersion) throw new RangeError("Data too long");
|
|
798
|
+
}
|
|
799
|
+
for (const newEcl of [
|
|
800
|
+
MEDIUM,
|
|
801
|
+
QUARTILE,
|
|
802
|
+
HIGH
|
|
803
|
+
]) if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8) ecl = newEcl;
|
|
804
|
+
const bb = [];
|
|
805
|
+
for (const seg of segs) {
|
|
806
|
+
appendBits(seg.mode[0], 4, bb);
|
|
807
|
+
appendBits(seg.numChars, numCharCountBits(seg.mode, version), bb);
|
|
808
|
+
for (const b of seg.getData()) bb.push(b);
|
|
809
|
+
}
|
|
810
|
+
const dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
|
811
|
+
appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
|
|
812
|
+
appendBits(0, (8 - bb.length % 8) % 8, bb);
|
|
813
|
+
for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 253) appendBits(padByte, 8, bb);
|
|
814
|
+
const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0);
|
|
815
|
+
bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
|
|
816
|
+
return new QrCode(version, ecl, dataCodewords, mask);
|
|
817
|
+
}
|
|
818
|
+
function encode(data, options) {
|
|
819
|
+
const { ecc = "L", boostEcc = false, minVersion = 1, maxVersion = 40, maskPattern = -1, border = 1 } = options || {};
|
|
820
|
+
const segment = typeof data === "string" ? makeSegments(data) : Array.isArray(data) ? [makeBytes(data)] : void 0;
|
|
821
|
+
if (!segment) throw new Error(`uqr only supports encoding string and binary data, but got: ${typeof data}`);
|
|
822
|
+
const qr = encodeSegments(segment, EccMap[ecc], minVersion, maxVersion, maskPattern, boostEcc);
|
|
823
|
+
const result = addBorder({
|
|
824
|
+
version: qr.version,
|
|
825
|
+
maskPattern: qr.mask,
|
|
826
|
+
size: qr.size,
|
|
827
|
+
data: qr.modules,
|
|
828
|
+
types: qr.types
|
|
829
|
+
}, border);
|
|
830
|
+
if (options?.invert) result.data = result.data.map((row) => row.map((mod) => !mod));
|
|
831
|
+
options?.onEncoded?.(result);
|
|
832
|
+
return result;
|
|
833
|
+
}
|
|
834
|
+
function addBorder(input, border = 1) {
|
|
835
|
+
if (!border) return input;
|
|
836
|
+
const { size } = input;
|
|
837
|
+
const newSize = size + border * 2;
|
|
838
|
+
input.size = newSize;
|
|
839
|
+
input.data.forEach((row) => {
|
|
840
|
+
for (let i = 0; i < border; i++) {
|
|
841
|
+
row.unshift(false);
|
|
842
|
+
row.push(false);
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
for (let i = 0; i < border; i++) {
|
|
846
|
+
input.data.unshift(Array.from({ length: newSize }, (_) => false));
|
|
847
|
+
input.data.push(Array.from({ length: newSize }, (_) => false));
|
|
848
|
+
}
|
|
849
|
+
const b = QrCodeDataType.Border;
|
|
850
|
+
input.types.forEach((row) => {
|
|
851
|
+
for (let i = 0; i < border; i++) {
|
|
852
|
+
row.unshift(b);
|
|
853
|
+
row.push(b);
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
for (let i = 0; i < border; i++) {
|
|
857
|
+
input.types.unshift(Array.from({ length: newSize }, (_) => b));
|
|
858
|
+
input.types.push(Array.from({ length: newSize }, (_) => b));
|
|
859
|
+
}
|
|
860
|
+
return input;
|
|
861
|
+
}
|
|
862
|
+
function getDataAt(data, x, y, defaults = false) {
|
|
863
|
+
if (x < 0 || y < 0 || x >= data.length || y >= data.length) return defaults;
|
|
864
|
+
return data[y][x];
|
|
865
|
+
}
|
|
866
|
+
function renderUnicodeCompact(data, options = {}) {
|
|
867
|
+
const palette = {
|
|
868
|
+
WHITE_ALL: "█",
|
|
869
|
+
WHITE_BLACK: "▀",
|
|
870
|
+
BLACK_WHITE: "▄",
|
|
871
|
+
BLACK_ALL: " "
|
|
872
|
+
};
|
|
873
|
+
const result = encode(data, options);
|
|
874
|
+
const WHITE = false;
|
|
875
|
+
const BLACK = true;
|
|
876
|
+
const at = (x, y) => getDataAt(result.data, x, y, true);
|
|
877
|
+
const lines = [];
|
|
878
|
+
let line = "";
|
|
879
|
+
for (let row = 0; row < result.size; row += 2) {
|
|
880
|
+
for (let col = 0; col < result.size; col++) if (at(col, row) === WHITE && at(col, row + 1) === WHITE) line += palette.WHITE_ALL;
|
|
881
|
+
else if (at(col, row) === WHITE && at(col, row + 1) === BLACK) line += palette.WHITE_BLACK;
|
|
882
|
+
else if (at(col, row) === BLACK && at(col, row + 1) === WHITE) line += palette.BLACK_WHITE;
|
|
883
|
+
else line += palette.BLACK_ALL;
|
|
884
|
+
lines.push(line);
|
|
885
|
+
line = "";
|
|
886
|
+
}
|
|
887
|
+
return lines.join("\n");
|
|
888
|
+
}
|
|
889
|
+
function escapeAttr(value) {
|
|
890
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
891
|
+
}
|
|
892
|
+
function renderSVG(data, options = {}) {
|
|
893
|
+
const result = encode(data, options);
|
|
894
|
+
const { pixelSize = 10, whiteColor = "white", blackColor = "black" } = options;
|
|
895
|
+
const height = result.size * pixelSize;
|
|
896
|
+
const width = result.size * pixelSize;
|
|
897
|
+
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}">`;
|
|
898
|
+
const paths = [];
|
|
899
|
+
for (let row = 0; row < result.size; row++) for (let col = 0; col < result.size; col++) {
|
|
900
|
+
const x = col * pixelSize;
|
|
901
|
+
const y = row * pixelSize;
|
|
902
|
+
if (result.data[row][col]) paths.push(`M${x},${y}h${pixelSize}v${pixelSize}h-${pixelSize}z`);
|
|
903
|
+
}
|
|
904
|
+
svg += `<rect fill="${escapeAttr(whiteColor)}" width="${width}" height="${height}"/>`;
|
|
905
|
+
svg += `<path fill="${escapeAttr(blackColor)}" d="${paths.join("")}"/>`;
|
|
906
|
+
svg += "</svg>";
|
|
907
|
+
return svg;
|
|
908
|
+
}
|
|
909
|
+
// src/config.ts
|
|
910
|
+
var SettingsError = class extends Error {};
|
|
911
|
+
/** `apps/server/.env` when running from a clone; a published package has none. */
|
|
912
|
+
function loadOwnEnvFile() {
|
|
913
|
+
const file = join(dirname(fileURLToPath(import.meta.url)), "..", ".env");
|
|
914
|
+
if (!existsSync(file)) return;
|
|
915
|
+
for (const [key, value] of Object.entries(parseEnv(readFileSync(file, "utf8")))) if (process.env[key] === void 0) process.env[key] = value;
|
|
916
|
+
}
|
|
917
|
+
function text(name) {
|
|
918
|
+
const value = process.env[name]?.trim();
|
|
919
|
+
return value ? value : void 0;
|
|
920
|
+
}
|
|
921
|
+
function whole(name, what, fallback, min, max) {
|
|
922
|
+
const raw = text(name);
|
|
923
|
+
if (raw === void 0) return fallback;
|
|
924
|
+
const value = Number(raw);
|
|
925
|
+
if (!Number.isInteger(value) || value < min || value > max) throw new SettingsError(`${what} (${name}) should be a whole number from ${min} to ${max}, not "${raw}".`);
|
|
926
|
+
return value;
|
|
927
|
+
}
|
|
928
|
+
function yesNo(name, what, fallback) {
|
|
929
|
+
const raw = text(name)?.toLowerCase();
|
|
930
|
+
if (raw === void 0) return fallback;
|
|
931
|
+
if ([
|
|
932
|
+
"true",
|
|
933
|
+
"1",
|
|
934
|
+
"yes",
|
|
935
|
+
"on"
|
|
936
|
+
].includes(raw)) return true;
|
|
937
|
+
if ([
|
|
938
|
+
"false",
|
|
939
|
+
"0",
|
|
940
|
+
"no",
|
|
941
|
+
"off"
|
|
942
|
+
].includes(raw)) return false;
|
|
943
|
+
throw new SettingsError(`${what} (${name}) should be true or false, not "${raw}".`);
|
|
944
|
+
}
|
|
945
|
+
function address(name, what) {
|
|
946
|
+
const raw = text(name);
|
|
947
|
+
if (raw === void 0) return void 0;
|
|
948
|
+
try {
|
|
949
|
+
const url = new URL(raw);
|
|
950
|
+
if (url.protocol === "http:" || url.protocol === "https:") return raw.replace(/\/+$/, "");
|
|
951
|
+
} catch {}
|
|
952
|
+
throw new SettingsError(`${what} (${name}) should be a web address such as https://my-computer.my-tailnet.ts.net, not "${raw}".`);
|
|
953
|
+
}
|
|
954
|
+
function readSettings() {
|
|
955
|
+
loadOwnEnvFile();
|
|
956
|
+
return {
|
|
957
|
+
port: whole("PORT", "The port", 4545, 1, 65535),
|
|
958
|
+
host: text("HOST") ?? "0.0.0.0",
|
|
959
|
+
publicUrl: address("KITELINE_PUBLIC_URL", "The public address"),
|
|
960
|
+
token: text("KITELINE_TOKEN"),
|
|
961
|
+
home: text("KITELINE_HOME"),
|
|
962
|
+
claudePath: text("CLAUDE_PATH"),
|
|
963
|
+
allowBypass: yesNo("KITELINE_ALLOW_BYPASS", "Allowing Bypass mode", true),
|
|
964
|
+
maxSessions: whole("KITELINE_MAX_SESSIONS", "The session limit", 6, 1, 100),
|
|
965
|
+
idleMinutes: whole("KITELINE_IDLE_MINUTES", "The idle time", 20, 1, 10080)
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
/** How the person started Kiteline, so the hints it prints name a command they can run. */
|
|
969
|
+
let viaCli = false;
|
|
970
|
+
function startedFromCli() {
|
|
971
|
+
viaCli = true;
|
|
972
|
+
}
|
|
973
|
+
function command(name) {
|
|
974
|
+
if (viaCli) return name === "start" ? "kiteline" : "kiteline pair";
|
|
975
|
+
return `pnpm ${name}`;
|
|
976
|
+
}
|
|
977
|
+
/** How to pick another port, in the same terms. */
|
|
978
|
+
function otherPortHint() {
|
|
979
|
+
return viaCli ? "use another with kiteline --port 4546" : "set PORT to use a different one";
|
|
980
|
+
}
|
|
981
|
+
// src/banner.ts
|
|
982
|
+
/** LAN and tailnet addresses first, so the QR code points somewhere a phone can reach. */
|
|
983
|
+
function reachableHosts() {
|
|
984
|
+
const hosts = [];
|
|
985
|
+
for (const addresses of Object.values(networkInterfaces())) for (const address of addresses ?? []) if (address.family === "IPv4" && !address.internal) hosts.push(address.address);
|
|
986
|
+
const rank = (ip) => ip.startsWith("192.168.") ? 0 : ip.startsWith("10.") ? 1 : ip.startsWith("100.") ? 2 : 3;
|
|
987
|
+
return hosts.sort((a, b) => rank(a) - rank(b));
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* The pairing link points at the public URL when one is set (say, the HTTPS
|
|
991
|
+
* address from `tailscale serve`), otherwise at the first LAN address.
|
|
992
|
+
*/
|
|
993
|
+
function buildPairing(port, token, options) {
|
|
994
|
+
const publicUrl = options.publicUrl?.replace(/\/+$/, "");
|
|
995
|
+
const lan = options.loopbackOnly ? [] : reachableHosts().map((host) => `http://${host}:${port}`);
|
|
996
|
+
const urls = [
|
|
997
|
+
...publicUrl ? [publicUrl] : [],
|
|
998
|
+
...lan,
|
|
999
|
+
`http://localhost:${port}`
|
|
1000
|
+
];
|
|
1001
|
+
const url = `${urls[0]}/#pair=${token}`;
|
|
1002
|
+
return {
|
|
1003
|
+
url,
|
|
1004
|
+
urls,
|
|
1005
|
+
svg: renderSVG(url, { border: 1 })
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
const color = !!process.stdout.isTTY && !process.env.NO_COLOR;
|
|
1009
|
+
const paint = (open, close) => (s) => color ? `\x1b[${open}m${s}\x1b[${close}m` : s;
|
|
1010
|
+
const dim = paint("2", "22");
|
|
1011
|
+
const bold = paint("1", "22");
|
|
1012
|
+
const accent = paint("38;5;117", "39");
|
|
1013
|
+
function printBanner(pairing, options) {
|
|
1014
|
+
const lines = [
|
|
1015
|
+
"",
|
|
1016
|
+
` ${accent("◆")} ${bold("Kiteline")} ${dim(`v${options.version}`)}${options.dev ? dim(" (dev, web on :4546)") : ""}`,
|
|
1017
|
+
"",
|
|
1018
|
+
...pairing.urls.map((url, i) => ` ${dim(i === pairing.urls.length - 1 ? "Local " : "Network ")} ${url}`),
|
|
1019
|
+
"",
|
|
1020
|
+
` ${bold("Pair your phone:")} scan this code, or open the link on it.`,
|
|
1021
|
+
"",
|
|
1022
|
+
...renderUnicodeCompact(pairing.url, { border: 1 }).split("\n").map((row) => ` ${row}`),
|
|
1023
|
+
"",
|
|
1024
|
+
` ${accent(pairing.url)}`,
|
|
1025
|
+
` ${dim("Keep this link private. Start with --new-token to unpair every device.")}`,
|
|
1026
|
+
""
|
|
1027
|
+
];
|
|
1028
|
+
console.log(lines.join("\n"));
|
|
1029
|
+
}
|
|
1030
|
+
/** Lets `pnpm pair` show the code later without starting a second server. */
|
|
1031
|
+
function savePairing(home, pairing, version) {
|
|
1032
|
+
const saved = {
|
|
1033
|
+
version,
|
|
1034
|
+
url: pairing.url,
|
|
1035
|
+
urls: pairing.urls,
|
|
1036
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1037
|
+
};
|
|
1038
|
+
try {
|
|
1039
|
+
mkdirSync(home, {
|
|
1040
|
+
recursive: true,
|
|
1041
|
+
mode: 448
|
|
1042
|
+
});
|
|
1043
|
+
writeFileSync(join(home, "pairing.json"), `${JSON.stringify(saved, null, 2)}\n`, { mode: 384 });
|
|
1044
|
+
} catch {}
|
|
1045
|
+
}
|
|
1046
|
+
function printAlreadyRunning(port) {
|
|
1047
|
+
console.error([
|
|
1048
|
+
"",
|
|
1049
|
+
` ${accent("◆")} ${bold("Kiteline didn't start:")} port ${port} is already in use.`,
|
|
1050
|
+
"",
|
|
1051
|
+
" If Kiteline is already running, for example as the background service,",
|
|
1052
|
+
" there's nothing to start. Show the pairing code with:",
|
|
1053
|
+
"",
|
|
1054
|
+
` ${accent(command("pair"))}`,
|
|
1055
|
+
"",
|
|
1056
|
+
dim(` Otherwise another program has the port; ${otherPortHint()}.`),
|
|
1057
|
+
""
|
|
1058
|
+
].join("\n"));
|
|
1059
|
+
}
|
|
1060
|
+
function printSettingsError(message) {
|
|
1061
|
+
console.error([
|
|
1062
|
+
"",
|
|
1063
|
+
` ${accent("◆")} ${bold("Kiteline didn't start:")} ${message}`,
|
|
1064
|
+
""
|
|
1065
|
+
].join("\n"));
|
|
1066
|
+
}
|
|
1067
|
+
export { printAlreadyRunning as a, savePairing as c, loadOwnEnvFile as d, readSettings as f, dim as i, SettingsError as l, renderSVG as m, bold as n, printBanner as o, startedFromCli as p, buildPairing as r, printSettingsError as s, accent as t, command as u };
|