nanoodle 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -9
- package/bin/nanoodle.mjs +33 -5
- package/package.json +5 -1
- package/src/browser.mjs +38 -0
- package/src/catalog.mjs +22 -0
- package/src/client.mjs +116 -17
- package/src/graph.mjs +25 -9
- package/src/index.mjs +7 -2
- package/src/io.mjs +42 -2
- package/src/local-media.mjs +998 -0
- package/src/media.mjs +68 -16
- package/src/mp4cat.mjs +284 -0
- package/src/nodes.mjs +253 -37
- package/src/qr.mjs +385 -0
- package/src/share.mjs +20 -14
- package/src/workflow.mjs +102 -18
- package/src/x402.mjs +77 -0
- package/src/zlib.mjs +44 -0
package/src/qr.mjs
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* qrModules — self-contained QR code generator (module matrix output).
|
|
3
|
+
*
|
|
4
|
+
* Derived from the "QR Code generator library" by Project Nayuki
|
|
5
|
+
* (https://www.nayuki.io/page/qr-code-generator-library), trimmed to
|
|
6
|
+
* byte mode + ECC level M + automatic mask selection. Algorithm unmodified.
|
|
7
|
+
*
|
|
8
|
+
* Copyright (c) Project Nayuki. (MIT License)
|
|
9
|
+
*
|
|
10
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
11
|
+
* this software and associated documentation files (the "Software"), to deal in
|
|
12
|
+
* the Software without restriction, including without limitation the rights to
|
|
13
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
14
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
15
|
+
* subject to the following conditions:
|
|
16
|
+
* - The above copyright notice and this permission notice shall be included in
|
|
17
|
+
* all copies or substantial portions of the Software.
|
|
18
|
+
* - The Software is provided "as is", without warranty of any kind, express or
|
|
19
|
+
* implied, including but not limited to the warranties of merchantability,
|
|
20
|
+
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
21
|
+
* authors or copyright holders be liable for any claim, damages or other
|
|
22
|
+
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
23
|
+
* out of or in connection with the Software or the use or other dealings in the
|
|
24
|
+
* Software.
|
|
25
|
+
*/
|
|
26
|
+
export function qrModules(text) {
|
|
27
|
+
// ---- UTF-8 encode the text into bytes ----
|
|
28
|
+
var data = [];
|
|
29
|
+
var enc = encodeURIComponent(text);
|
|
30
|
+
for (var ei = 0; ei < enc.length; ei++) {
|
|
31
|
+
var ch = enc.charAt(ei);
|
|
32
|
+
if (ch === "%") { data.push(parseInt(enc.substr(ei + 1, 2), 16)); ei += 2; }
|
|
33
|
+
else data.push(ch.charCodeAt(0));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---- Tables for ECC level M (index = version; index 0 unused) ----
|
|
37
|
+
var ECC_PER_BLOCK = [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28];
|
|
38
|
+
var NUM_BLOCKS = [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49];
|
|
39
|
+
|
|
40
|
+
function numRawDataModules(ver) {
|
|
41
|
+
var result = (16 * ver + 128) * ver + 64;
|
|
42
|
+
if (ver >= 2) {
|
|
43
|
+
var numAlign = Math.floor(ver / 7) + 2;
|
|
44
|
+
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
45
|
+
if (ver >= 7) result -= 36;
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
function numDataCodewords(ver) {
|
|
50
|
+
return Math.floor(numRawDataModules(ver) / 8) - ECC_PER_BLOCK[ver] * NUM_BLOCKS[ver];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---- Choose the smallest version that fits (byte mode) ----
|
|
54
|
+
var version = -1;
|
|
55
|
+
for (var v = 1; v <= 40; v++) {
|
|
56
|
+
var used = 4 + (v < 10 ? 8 : 16) + data.length * 8;
|
|
57
|
+
if (used <= numDataCodewords(v) * 8) { version = v; break; }
|
|
58
|
+
}
|
|
59
|
+
if (version < 0) throw new RangeError("Data too long");
|
|
60
|
+
|
|
61
|
+
// ---- Bit buffer: mode + char count + data, terminator, padding ----
|
|
62
|
+
var bb = [];
|
|
63
|
+
function appendBits(val, len) {
|
|
64
|
+
for (var i = len - 1; i >= 0; i--) bb.push((val >>> i) & 1);
|
|
65
|
+
}
|
|
66
|
+
appendBits(4, 4); // byte-mode indicator
|
|
67
|
+
appendBits(data.length, version < 10 ? 8 : 16);
|
|
68
|
+
for (var di = 0; di < data.length; di++) appendBits(data[di], 8);
|
|
69
|
+
var capacityBits = numDataCodewords(version) * 8;
|
|
70
|
+
appendBits(0, Math.min(4, capacityBits - bb.length));
|
|
71
|
+
appendBits(0, (8 - bb.length % 8) % 8);
|
|
72
|
+
for (var padByte = 0xEC; bb.length < capacityBits; padByte ^= 0xEC ^ 0x11)
|
|
73
|
+
appendBits(padByte, 8);
|
|
74
|
+
var dataCodewords = [];
|
|
75
|
+
for (var ci = 0; ci * 8 < bb.length; ci++) dataCodewords.push(0);
|
|
76
|
+
for (var bi = 0; bi < bb.length; bi++)
|
|
77
|
+
dataCodewords[bi >>> 3] |= bb[bi] << (7 - (bi & 7));
|
|
78
|
+
|
|
79
|
+
// ---- Reed-Solomon over GF(2^8/0x11D) ----
|
|
80
|
+
function rsMultiply(x, y) {
|
|
81
|
+
var z = 0;
|
|
82
|
+
for (var i = 7; i >= 0; i--) {
|
|
83
|
+
z = (z << 1) ^ ((z >>> 7) * 0x11D);
|
|
84
|
+
z ^= ((y >>> i) & 1) * x;
|
|
85
|
+
}
|
|
86
|
+
return z;
|
|
87
|
+
}
|
|
88
|
+
function rsDivisor(degree) {
|
|
89
|
+
var result = [];
|
|
90
|
+
for (var i = 0; i < degree - 1; i++) result.push(0);
|
|
91
|
+
result.push(1);
|
|
92
|
+
var root = 1;
|
|
93
|
+
for (var d = 0; d < degree; d++) {
|
|
94
|
+
for (var j = 0; j < result.length; j++) {
|
|
95
|
+
result[j] = rsMultiply(result[j], root);
|
|
96
|
+
if (j + 1 < result.length) result[j] ^= result[j + 1];
|
|
97
|
+
}
|
|
98
|
+
root = rsMultiply(root, 0x02);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
function rsRemainder(dat, divisor) {
|
|
103
|
+
var result = divisor.map(function () { return 0; });
|
|
104
|
+
for (var i = 0; i < dat.length; i++) {
|
|
105
|
+
var factor = dat[i] ^ result.shift();
|
|
106
|
+
result.push(0);
|
|
107
|
+
for (var j = 0; j < divisor.length; j++)
|
|
108
|
+
result[j] ^= rsMultiply(divisor[j], factor);
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---- Split into blocks, append ECC, interleave ----
|
|
114
|
+
var numBlocks = NUM_BLOCKS[version];
|
|
115
|
+
var blockEccLen = ECC_PER_BLOCK[version];
|
|
116
|
+
var rawCodewords = Math.floor(numRawDataModules(version) / 8);
|
|
117
|
+
var numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
|
118
|
+
var shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
|
119
|
+
var blocks = [];
|
|
120
|
+
var rsDiv = rsDivisor(blockEccLen);
|
|
121
|
+
for (var b = 0, k = 0; b < numBlocks; b++) {
|
|
122
|
+
var dat = dataCodewords.slice(k, k + shortBlockLen - blockEccLen + (b < numShortBlocks ? 0 : 1));
|
|
123
|
+
k += dat.length;
|
|
124
|
+
var ecc = rsRemainder(dat, rsDiv);
|
|
125
|
+
if (b < numShortBlocks) dat.push(0);
|
|
126
|
+
blocks.push(dat.concat(ecc));
|
|
127
|
+
}
|
|
128
|
+
var allCodewords = [];
|
|
129
|
+
for (var p = 0; p < blocks[0].length; p++) {
|
|
130
|
+
for (var q = 0; q < blocks.length; q++) {
|
|
131
|
+
if (p != shortBlockLen - blockEccLen || q >= numShortBlocks)
|
|
132
|
+
allCodewords.push(blocks[q][p]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ---- Module grid ----
|
|
137
|
+
var size = version * 4 + 17;
|
|
138
|
+
var modules = [];
|
|
139
|
+
var isFunction = [];
|
|
140
|
+
for (var r = 0; r < size; r++) {
|
|
141
|
+
var row = [], frow = [];
|
|
142
|
+
for (var c = 0; c < size; c++) { row.push(false); frow.push(false); }
|
|
143
|
+
modules.push(row);
|
|
144
|
+
isFunction.push(frow);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function setFunctionModule(x, y, isDark) {
|
|
148
|
+
modules[y][x] = isDark;
|
|
149
|
+
isFunction[y][x] = true;
|
|
150
|
+
}
|
|
151
|
+
function getBitOf(x, i) {
|
|
152
|
+
return ((x >>> i) & 1) != 0;
|
|
153
|
+
}
|
|
154
|
+
function drawFinderPattern(x, y) {
|
|
155
|
+
for (var dy = -4; dy <= 4; dy++) {
|
|
156
|
+
for (var dx = -4; dx <= 4; dx++) {
|
|
157
|
+
var dist = Math.max(Math.abs(dx), Math.abs(dy));
|
|
158
|
+
var xx = x + dx, yy = y + dy;
|
|
159
|
+
if (0 <= xx && xx < size && 0 <= yy && yy < size)
|
|
160
|
+
setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function drawAlignmentPattern(x, y) {
|
|
165
|
+
for (var dy = -2; dy <= 2; dy++) {
|
|
166
|
+
for (var dx = -2; dx <= 2; dx++)
|
|
167
|
+
setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function alignmentPatternPositions() {
|
|
171
|
+
if (version == 1) return [];
|
|
172
|
+
var numAlign = Math.floor(version / 7) + 2;
|
|
173
|
+
var step = (version == 32) ? 26 :
|
|
174
|
+
Math.ceil((version * 4 + 4) / (numAlign * 2 - 2)) * 2;
|
|
175
|
+
var result = [6];
|
|
176
|
+
for (var pos = size - 7; result.length < numAlign; pos -= step)
|
|
177
|
+
result.splice(1, 0, pos);
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
// ECC level M has formatBits = 0, so format data = mask alone.
|
|
181
|
+
function drawFormatBits(mask) {
|
|
182
|
+
var fdata = 0 << 3 | mask;
|
|
183
|
+
var rem = fdata;
|
|
184
|
+
for (var i = 0; i < 10; i++)
|
|
185
|
+
rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
186
|
+
var bits = (fdata << 10 | rem) ^ 0x5412;
|
|
187
|
+
for (var a = 0; a <= 5; a++)
|
|
188
|
+
setFunctionModule(8, a, getBitOf(bits, a));
|
|
189
|
+
setFunctionModule(8, 7, getBitOf(bits, 6));
|
|
190
|
+
setFunctionModule(8, 8, getBitOf(bits, 7));
|
|
191
|
+
setFunctionModule(7, 8, getBitOf(bits, 8));
|
|
192
|
+
for (var i2 = 9; i2 < 15; i2++)
|
|
193
|
+
setFunctionModule(14 - i2, 8, getBitOf(bits, i2));
|
|
194
|
+
for (var i3 = 0; i3 < 8; i3++)
|
|
195
|
+
setFunctionModule(size - 1 - i3, 8, getBitOf(bits, i3));
|
|
196
|
+
for (var i4 = 8; i4 < 15; i4++)
|
|
197
|
+
setFunctionModule(8, size - 15 + i4, getBitOf(bits, i4));
|
|
198
|
+
setFunctionModule(8, size - 8, true); // always dark
|
|
199
|
+
}
|
|
200
|
+
function drawVersion() {
|
|
201
|
+
if (version < 7) return;
|
|
202
|
+
var rem = version;
|
|
203
|
+
for (var i = 0; i < 12; i++)
|
|
204
|
+
rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
|
|
205
|
+
var bits = version << 12 | rem;
|
|
206
|
+
for (var j = 0; j < 18; j++) {
|
|
207
|
+
var color = getBitOf(bits, j);
|
|
208
|
+
var a = size - 11 + j % 3;
|
|
209
|
+
var bpos = Math.floor(j / 3);
|
|
210
|
+
setFunctionModule(a, bpos, color);
|
|
211
|
+
setFunctionModule(bpos, a, color);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function drawFunctionPatterns() {
|
|
215
|
+
for (var i = 0; i < size; i++) {
|
|
216
|
+
setFunctionModule(6, i, i % 2 == 0);
|
|
217
|
+
setFunctionModule(i, 6, i % 2 == 0);
|
|
218
|
+
}
|
|
219
|
+
drawFinderPattern(3, 3);
|
|
220
|
+
drawFinderPattern(size - 4, 3);
|
|
221
|
+
drawFinderPattern(3, size - 4);
|
|
222
|
+
var alignPatPos = alignmentPatternPositions();
|
|
223
|
+
var numAlign = alignPatPos.length;
|
|
224
|
+
for (var i5 = 0; i5 < numAlign; i5++) {
|
|
225
|
+
for (var j = 0; j < numAlign; j++) {
|
|
226
|
+
if (!(i5 == 0 && j == 0 || i5 == 0 && j == numAlign - 1 || i5 == numAlign - 1 && j == 0))
|
|
227
|
+
drawAlignmentPattern(alignPatPos[i5], alignPatPos[j]);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
drawFormatBits(0); // dummy; overwritten after mask choice
|
|
231
|
+
drawVersion();
|
|
232
|
+
}
|
|
233
|
+
function drawCodewords(cw) {
|
|
234
|
+
var i = 0;
|
|
235
|
+
for (var right = size - 1; right >= 1; right -= 2) {
|
|
236
|
+
if (right == 6) right = 5;
|
|
237
|
+
for (var vert = 0; vert < size; vert++) {
|
|
238
|
+
for (var j = 0; j < 2; j++) {
|
|
239
|
+
var x = right - j;
|
|
240
|
+
var upward = ((right + 1) & 2) == 0;
|
|
241
|
+
var y = upward ? size - 1 - vert : vert;
|
|
242
|
+
if (!isFunction[y][x] && i < cw.length * 8) {
|
|
243
|
+
modules[y][x] = getBitOf(cw[i >>> 3], 7 - (i & 7));
|
|
244
|
+
i++;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function applyMask(mask) {
|
|
251
|
+
for (var y = 0; y < size; y++) {
|
|
252
|
+
for (var x = 0; x < size; x++) {
|
|
253
|
+
var invert;
|
|
254
|
+
switch (mask) {
|
|
255
|
+
case 0: invert = (x + y) % 2 == 0; break;
|
|
256
|
+
case 1: invert = y % 2 == 0; break;
|
|
257
|
+
case 2: invert = x % 3 == 0; break;
|
|
258
|
+
case 3: invert = (x + y) % 3 == 0; break;
|
|
259
|
+
case 4: invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0; break;
|
|
260
|
+
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
|
261
|
+
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
|
262
|
+
default: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
|
263
|
+
}
|
|
264
|
+
if (!isFunction[y][x] && invert)
|
|
265
|
+
modules[y][x] = !modules[y][x];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
var PENALTY_N1 = 3, PENALTY_N2 = 3, PENALTY_N3 = 40, PENALTY_N4 = 10;
|
|
270
|
+
function finderPenaltyCountPatterns(runHistory) {
|
|
271
|
+
var n = runHistory[1];
|
|
272
|
+
var core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
|
273
|
+
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
|
|
274
|
+
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
|
275
|
+
}
|
|
276
|
+
function finderPenaltyAddHistory(currentRunLength, runHistory) {
|
|
277
|
+
if (runHistory[0] == 0) currentRunLength += size; // light border on initial run
|
|
278
|
+
runHistory.pop();
|
|
279
|
+
runHistory.unshift(currentRunLength);
|
|
280
|
+
}
|
|
281
|
+
function finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
|
|
282
|
+
if (currentRunColor) {
|
|
283
|
+
finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
284
|
+
currentRunLength = 0;
|
|
285
|
+
}
|
|
286
|
+
currentRunLength += size; // light border on final run
|
|
287
|
+
finderPenaltyAddHistory(currentRunLength, runHistory);
|
|
288
|
+
return finderPenaltyCountPatterns(runHistory);
|
|
289
|
+
}
|
|
290
|
+
function getPenaltyScore() {
|
|
291
|
+
var result = 0;
|
|
292
|
+
var x, y, runColor, run, runHistory;
|
|
293
|
+
for (y = 0; y < size; y++) {
|
|
294
|
+
runColor = false; run = 0; runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
295
|
+
for (x = 0; x < size; x++) {
|
|
296
|
+
if (modules[y][x] == runColor) {
|
|
297
|
+
run++;
|
|
298
|
+
if (run == 5) result += PENALTY_N1;
|
|
299
|
+
else if (run > 5) result++;
|
|
300
|
+
} else {
|
|
301
|
+
finderPenaltyAddHistory(run, runHistory);
|
|
302
|
+
if (!runColor) result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
303
|
+
runColor = modules[y][x];
|
|
304
|
+
run = 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
result += finderPenaltyTerminateAndCount(runColor, run, runHistory) * PENALTY_N3;
|
|
308
|
+
}
|
|
309
|
+
for (x = 0; x < size; x++) {
|
|
310
|
+
runColor = false; run = 0; runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
311
|
+
for (y = 0; y < size; y++) {
|
|
312
|
+
if (modules[y][x] == runColor) {
|
|
313
|
+
run++;
|
|
314
|
+
if (run == 5) result += PENALTY_N1;
|
|
315
|
+
else if (run > 5) result++;
|
|
316
|
+
} else {
|
|
317
|
+
finderPenaltyAddHistory(run, runHistory);
|
|
318
|
+
if (!runColor) result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
|
319
|
+
runColor = modules[y][x];
|
|
320
|
+
run = 1;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
result += finderPenaltyTerminateAndCount(runColor, run, runHistory) * PENALTY_N3;
|
|
324
|
+
}
|
|
325
|
+
for (y = 0; y < size - 1; y++) {
|
|
326
|
+
for (x = 0; x < size - 1; x++) {
|
|
327
|
+
var color = modules[y][x];
|
|
328
|
+
if (color == modules[y][x + 1] && color == modules[y + 1][x] && color == modules[y + 1][x + 1])
|
|
329
|
+
result += PENALTY_N2;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
var dark = 0;
|
|
333
|
+
for (y = 0; y < size; y++) {
|
|
334
|
+
for (x = 0; x < size; x++) {
|
|
335
|
+
if (modules[y][x]) dark++;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
var total = size * size;
|
|
339
|
+
var kk = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
|
|
340
|
+
result += kk * PENALTY_N4;
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ---- Assemble: function patterns, codewords, best-of-8 mask ----
|
|
345
|
+
drawFunctionPatterns();
|
|
346
|
+
drawCodewords(allCodewords);
|
|
347
|
+
var bestMask = 0;
|
|
348
|
+
var minPenalty = Infinity;
|
|
349
|
+
for (var m = 0; m < 8; m++) {
|
|
350
|
+
applyMask(m);
|
|
351
|
+
drawFormatBits(m);
|
|
352
|
+
var penalty = getPenaltyScore();
|
|
353
|
+
if (penalty < minPenalty) { bestMask = m; minPenalty = penalty; }
|
|
354
|
+
applyMask(m); // undo (XOR)
|
|
355
|
+
}
|
|
356
|
+
applyMask(bestMask);
|
|
357
|
+
drawFormatBits(bestMask);
|
|
358
|
+
|
|
359
|
+
return modules;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Render a QR as terminal text using half-block characters (two module rows per
|
|
364
|
+
* text line), with a 2-module quiet zone. Dark modules are "on" (background),
|
|
365
|
+
* matching how phone cameras expect QR contrast on a dark terminal is inverted —
|
|
366
|
+
* so this emits LIGHT blocks for light modules over the terminal default, which
|
|
367
|
+
* scans reliably on both dark and light terminals.
|
|
368
|
+
*/
|
|
369
|
+
export function qrTerminal(text) {
|
|
370
|
+
const m = qrModules(text);
|
|
371
|
+
const size = m.length, border = 2, dim = size + border * 2;
|
|
372
|
+
const at = (x, y) => x >= border && x < size + border && y >= border && y < size + border
|
|
373
|
+
? m[y - border][x - border] : false;
|
|
374
|
+
const lines = [];
|
|
375
|
+
for (let y = 0; y < dim; y += 2) {
|
|
376
|
+
let line = "";
|
|
377
|
+
for (let x = 0; x < dim; x++) {
|
|
378
|
+
const top = at(x, y), bot = at(x, y + 1);
|
|
379
|
+
// light modules are drawn (block chars), dark modules are terminal background
|
|
380
|
+
line += top && bot ? " " : top ? "\u2584" : bot ? "\u2580" : "\u2588";
|
|
381
|
+
}
|
|
382
|
+
lines.push(line);
|
|
383
|
+
}
|
|
384
|
+
return lines.join("\n");
|
|
385
|
+
}
|
package/src/share.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { gunzip } from "./zlib.mjs";
|
|
2
|
+
import { base64ToBytes } from "./media.mjs";
|
|
2
3
|
import { NanoodleError } from "./errors.mjs";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -23,28 +24,33 @@ export function isShareRef(s) {
|
|
|
23
24
|
return typeof s === "string" && (URL_RE.test(s) || FRAG_RE.test(s));
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
function
|
|
27
|
+
function b64urlToBytes(s, what) {
|
|
27
28
|
if (!/^[A-Za-z0-9_-]+$/.test(s)) {
|
|
28
29
|
throw new NanoodleError(`share link: ${what} payload is not base64url data — is the URL complete?`);
|
|
29
30
|
}
|
|
30
|
-
return
|
|
31
|
+
try { return base64ToBytes(s.replace(/-/g, "+").replace(/_/g, "/")); }
|
|
32
|
+
catch { throw new NanoodleError(`share link: ${what} payload is not base64url data — is the URL complete?`); }
|
|
31
33
|
}
|
|
32
34
|
|
|
35
|
+
const utf8 = new TextDecoder();
|
|
36
|
+
|
|
33
37
|
function parseJson(text, what) {
|
|
34
38
|
try { return JSON.parse(text); }
|
|
35
39
|
catch { throw new NanoodleError(`share link: ${what} payload decoded but is not valid JSON — the link may be truncated`); }
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
function gunzipText(buf, what) {
|
|
39
|
-
try { return
|
|
42
|
+
async function gunzipText(buf, what) {
|
|
43
|
+
try { return utf8.decode(await gunzip(buf)); }
|
|
40
44
|
catch { throw new NanoodleError(`share link: ${what} payload is not valid gzip data — the link may be truncated`); }
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
48
|
* Decode a share fragment ("#g=…", "g=…", "#a=…", …) to its graph.
|
|
45
|
-
*
|
|
49
|
+
* Async since v0.4: gzip decoding goes through DecompressionStream in the
|
|
50
|
+
* browser, which has no synchronous form.
|
|
51
|
+
* @returns {Promise<{ graph: object, kind: "g"|"j"|"a", app: { name?, lang?, hasFiles: boolean }|null }>}
|
|
46
52
|
*/
|
|
47
|
-
export function decodeShareFragment(fragment) {
|
|
53
|
+
export async function decodeShareFragment(fragment) {
|
|
48
54
|
let f = String(fragment);
|
|
49
55
|
if (f.startsWith("#")) f = f.slice(1);
|
|
50
56
|
if (f.startsWith("ga=")) {
|
|
@@ -53,16 +59,16 @@ export function decodeShareFragment(fragment) {
|
|
|
53
59
|
"Open the link in a browser and use 🔗 Share to mint a #g= workflow link instead.");
|
|
54
60
|
}
|
|
55
61
|
if (f.startsWith("g=")) {
|
|
56
|
-
return { graph: parseJson(gunzipText(
|
|
62
|
+
return { graph: parseJson(await gunzipText(b64urlToBytes(f.slice(2), "#g="), "#g="), "#g="), kind: "g", app: null };
|
|
57
63
|
}
|
|
58
64
|
if (f.startsWith("j=")) {
|
|
59
|
-
return { graph: parseJson(
|
|
65
|
+
return { graph: parseJson(utf8.decode(b64urlToBytes(f.slice(2), "#j=")), "#j="), kind: "j", app: null };
|
|
60
66
|
}
|
|
61
67
|
if (f.startsWith("a=")) {
|
|
62
68
|
const tag = f.slice(2);
|
|
63
69
|
const json = tag[0] === "u"
|
|
64
|
-
?
|
|
65
|
-
: gunzipText(
|
|
70
|
+
? utf8.decode(b64urlToBytes(tag.slice(1), "#a=u"))
|
|
71
|
+
: await gunzipText(b64urlToBytes(tag, "#a="), "#a=");
|
|
66
72
|
const payload = parseJson(json, "#a=");
|
|
67
73
|
if (!payload || typeof payload !== "object" || !payload.graph) {
|
|
68
74
|
throw new NanoodleError("share link: #a= app payload has no graph in it");
|
|
@@ -100,11 +106,11 @@ function fragmentOf(url) {
|
|
|
100
106
|
*/
|
|
101
107
|
export async function decodeShareUrl(input, opts = {}) {
|
|
102
108
|
const s = String(input).trim();
|
|
103
|
-
if (!URL_RE.test(s)) return { ...decodeShareFragment(s), url: s };
|
|
109
|
+
if (!URL_RE.test(s)) return { ...(await decodeShareFragment(s)), url: s };
|
|
104
110
|
|
|
105
111
|
let url = s;
|
|
106
112
|
const frag = fragmentOf(url);
|
|
107
|
-
if (frag && FRAG_RE.test(frag)) return { ...decodeShareFragment(frag), url };
|
|
113
|
+
if (frag && FRAG_RE.test(frag)) return { ...(await decodeShareFragment(frag)), url };
|
|
108
114
|
|
|
109
115
|
// No fragment on the URL itself → treat it as a short link and follow
|
|
110
116
|
// redirects by hand: fragments ride in the Location header, which automatic
|
|
@@ -123,7 +129,7 @@ export async function decodeShareUrl(input, opts = {}) {
|
|
|
123
129
|
}
|
|
124
130
|
url = new URL(loc, url).href;
|
|
125
131
|
const hopFrag = fragmentOf(url);
|
|
126
|
-
if (hopFrag && FRAG_RE.test(hopFrag)) return { ...decodeShareFragment(hopFrag), url };
|
|
132
|
+
if (hopFrag && FRAG_RE.test(hopFrag)) return { ...(await decodeShareFragment(hopFrag)), url };
|
|
127
133
|
}
|
|
128
134
|
throw new NanoodleError(`share link: gave up after ${maxHops} redirects without finding a share fragment`);
|
|
129
135
|
}
|