@celljs/ta-relay 4.8.0 → 4.9.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.
Files changed (96) hide show
  1. package/README.en.md +8 -3
  2. package/README.md +8 -3
  3. package/esm/common/index.d.ts +2 -0
  4. package/esm/common/index.js +2 -0
  5. package/esm/common/qr-code.d.ts +42 -0
  6. package/esm/common/qr-code.js +366 -0
  7. package/esm/common/relay-protocol.d.ts +42 -1
  8. package/esm/common/relay-protocol.js +26 -0
  9. package/esm/common/relay-room.d.ts +1 -0
  10. package/esm/common/relay-room.js +13 -2
  11. package/esm/common/relay-share.d.ts +31 -0
  12. package/esm/common/relay-share.js +117 -0
  13. package/esm/common/relay-socket.d.ts +18 -1
  14. package/esm/common/relay-socket.js +43 -5
  15. package/esm/node/relay-host.d.ts +18 -3
  16. package/esm/node/relay-host.js +82 -8
  17. package/esm/node/relay-server.js +10 -6
  18. package/esm/node/relay-store.d.ts +29 -1
  19. package/esm/node/relay-store.js +54 -2
  20. package/lib/common/index.d.ts +2 -0
  21. package/lib/common/index.js +2 -0
  22. package/lib/common/qr-code.d.ts +43 -0
  23. package/lib/common/qr-code.js +370 -0
  24. package/lib/common/relay-protocol.d.ts +42 -1
  25. package/lib/common/relay-protocol.js +27 -1
  26. package/lib/common/relay-room.d.ts +1 -0
  27. package/lib/common/relay-room.js +12 -1
  28. package/lib/common/relay-share.d.ts +32 -0
  29. package/lib/common/relay-share.js +120 -0
  30. package/lib/common/relay-socket.d.ts +18 -1
  31. package/lib/common/relay-socket.js +42 -4
  32. package/lib/node/relay-host.d.ts +18 -3
  33. package/lib/node/relay-host.js +81 -7
  34. package/lib/node/relay-server.js +9 -5
  35. package/lib/node/relay-store.d.ts +29 -1
  36. package/lib/node/relay-store.js +54 -2
  37. package/package.json +4 -3
  38. package/esm/common/index.js.map +0 -1
  39. package/esm/common/relay-bytes.js.map +0 -1
  40. package/esm/common/relay-cipher.js.map +0 -1
  41. package/esm/common/relay-pairing.js.map +0 -1
  42. package/esm/common/relay-protocol.js.map +0 -1
  43. package/esm/common/relay-room.js.map +0 -1
  44. package/esm/common/relay-socket.js.map +0 -1
  45. package/esm/node/index.js.map +0 -1
  46. package/esm/node/relay-host.js.map +0 -1
  47. package/esm/node/relay-server.js.map +0 -1
  48. package/esm/node/relay-store.js.map +0 -1
  49. package/lib/common/index.d.ts.map +0 -1
  50. package/lib/common/index.js.map +0 -1
  51. package/lib/common/relay-bytes.d.ts.map +0 -1
  52. package/lib/common/relay-bytes.js.map +0 -1
  53. package/lib/common/relay-cipher.d.ts.map +0 -1
  54. package/lib/common/relay-cipher.js.map +0 -1
  55. package/lib/common/relay-cipher.spec.d.ts +0 -2
  56. package/lib/common/relay-cipher.spec.d.ts.map +0 -1
  57. package/lib/common/relay-cipher.spec.js +0 -43
  58. package/lib/common/relay-cipher.spec.js.map +0 -1
  59. package/lib/common/relay-pairing.d.ts.map +0 -1
  60. package/lib/common/relay-pairing.js.map +0 -1
  61. package/lib/common/relay-pairing.spec.d.ts +0 -2
  62. package/lib/common/relay-pairing.spec.d.ts.map +0 -1
  63. package/lib/common/relay-pairing.spec.js +0 -25
  64. package/lib/common/relay-pairing.spec.js.map +0 -1
  65. package/lib/common/relay-protocol.d.ts.map +0 -1
  66. package/lib/common/relay-protocol.js.map +0 -1
  67. package/lib/common/relay-room.d.ts.map +0 -1
  68. package/lib/common/relay-room.js.map +0 -1
  69. package/lib/common/relay-socket.d.ts.map +0 -1
  70. package/lib/common/relay-socket.js.map +0 -1
  71. package/lib/node/index.d.ts.map +0 -1
  72. package/lib/node/index.js.map +0 -1
  73. package/lib/node/relay-host.d.ts.map +0 -1
  74. package/lib/node/relay-host.js.map +0 -1
  75. package/lib/node/relay-server.d.ts.map +0 -1
  76. package/lib/node/relay-server.js.map +0 -1
  77. package/lib/node/relay-store.d.ts.map +0 -1
  78. package/lib/node/relay-store.js.map +0 -1
  79. package/lib/node/relay.spec.d.ts +0 -2
  80. package/lib/node/relay.spec.d.ts.map +0 -1
  81. package/lib/node/relay.spec.js +0 -141
  82. package/lib/node/relay.spec.js.map +0 -1
  83. package/src/common/index.ts +0 -6
  84. package/src/common/relay-bytes.ts +0 -37
  85. package/src/common/relay-cipher.spec.ts +0 -45
  86. package/src/common/relay-cipher.ts +0 -74
  87. package/src/common/relay-pairing.spec.ts +0 -26
  88. package/src/common/relay-pairing.ts +0 -46
  89. package/src/common/relay-protocol.ts +0 -50
  90. package/src/common/relay-room.ts +0 -79
  91. package/src/common/relay-socket.ts +0 -106
  92. package/src/node/index.ts +0 -4
  93. package/src/node/relay-host.ts +0 -230
  94. package/src/node/relay-server.ts +0 -143
  95. package/src/node/relay-store.ts +0 -84
  96. package/src/node/relay.spec.ts +0 -151
@@ -2,6 +2,7 @@ import { createHash, randomBytes } from 'node:crypto';
2
2
  import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
3
3
  import { hostname } from 'node:os';
4
4
  import { dirname, join } from 'node:path';
5
+ import { RELAY_DEFAULT_URL, RELAY_SHARE_DAYS } from '../common/relay-protocol.js';
5
6
  const id = () => randomBytes(16).toString('base64url');
6
7
  /** The devices paired with this ta serve. Read again on every use: `ta pair` changes it while ta serve runs. */
7
8
  export class RelayStore {
@@ -14,7 +15,12 @@ export class RelayStore {
14
15
  read() {
15
16
  try {
16
17
  const config = JSON.parse(readFileSync(this.file, 'utf8'));
17
- return { url: config.url, host: config.host, devices: Array.isArray(config.devices) ? config.devices : [] };
18
+ return {
19
+ url: config.url,
20
+ host: config.host,
21
+ devices: Array.isArray(config.devices) ? config.devices : [],
22
+ shares: Array.isArray(config.shares) ? config.shares : [],
23
+ };
18
24
  }
19
25
  catch (_a) {
20
26
  return { devices: [] };
@@ -23,11 +29,57 @@ export class RelayStore {
23
29
  device(deviceId) {
24
30
  return this.read().devices.find(device => device.id === deviceId);
25
31
  }
32
+ /** A share still on, by its id. */
33
+ share(shareId, now = Date.now()) {
34
+ var _a;
35
+ return (_a = this.read().shares) === null || _a === void 0 ? void 0 : _a.find(share => share.id === shareId && share.expiresAt > now);
36
+ }
37
+ /** The shares still on. */
38
+ shares(now = Date.now()) {
39
+ var _a;
40
+ return ((_a = this.read().shares) !== null && _a !== void 0 ? _a : []).filter(share => share.expiresAt > now);
41
+ }
42
+ /**
43
+ * A link of the package in `root`: the one of before while it lasts, else a new one for RELAY_SHARE_DAYS,
44
+ * on the relay of the devices (the default one when there is none), which this ta serve then connects to.
45
+ */
46
+ shareRoot(root, name, now = Date.now()) {
47
+ var _a, _b, _c;
48
+ const before = this.read();
49
+ const url = (_a = before.url) !== null && _a !== void 0 ? _a : RELAY_DEFAULT_URL;
50
+ const host = (_b = before.host) !== null && _b !== void 0 ? _b : { id: id(), secret: randomBytes(32).toString('base64url') };
51
+ const live = ((_c = before.shares) !== null && _c !== void 0 ? _c : []).filter(share => share.expiresAt > now);
52
+ let share = live.find(candidate => candidate.root === root);
53
+ if (!share) {
54
+ share = {
55
+ id: id(),
56
+ key: randomBytes(32).toString('base64url'),
57
+ root,
58
+ name,
59
+ createdAt: now,
60
+ expiresAt: now + RELAY_SHARE_DAYS * 24 * 60 * 60 * 1000,
61
+ };
62
+ live.push(share);
63
+ }
64
+ this.write({ ...before, url, host, shares: live });
65
+ return { share, link: { relay: url, host: host.id, share: share.id, key: share.key, name } };
66
+ }
67
+ /** Stops the shares of a package: their links no longer work. False when there were none. */
68
+ unshareRoot(root) {
69
+ var _a, _b;
70
+ const config = this.read();
71
+ const shares = ((_a = config.shares) !== null && _a !== void 0 ? _a : []).filter(share => share.root !== root);
72
+ if (shares.length === ((_b = config.shares) !== null && _b !== void 0 ? _b : []).length) {
73
+ return false;
74
+ }
75
+ this.write({ ...config, shares });
76
+ return true;
77
+ }
26
78
  /** A new device for this relay; another relay than before unpairs the devices of the old one. */
27
79
  pair(relay, deviceName) {
28
80
  const url = relay.replace(/\/+$/, '');
29
81
  const before = this.read();
30
- const config = before.url === url && before.host ? before : { url, host: { id: id(), secret: randomBytes(32).toString('base64url') }, devices: [] };
82
+ const config = before.url === url && before.host ? before : { url, host: { id: id(), secret: randomBytes(32).toString('base64url') }, devices: [], shares: [] };
31
83
  const device = { id: id(), name: deviceName, key: randomBytes(32).toString('base64url'), createdAt: Date.now() };
32
84
  config.devices.push(device);
33
85
  this.write(config);
@@ -1,7 +1,9 @@
1
+ export * from './qr-code';
1
2
  export * from './relay-bytes';
2
3
  export * from './relay-cipher';
3
4
  export * from './relay-pairing';
4
5
  export * from './relay-protocol';
5
6
  export * from './relay-room';
7
+ export * from './relay-share';
6
8
  export * from './relay-socket';
7
9
  //# sourceMappingURL=index.d.ts.map
@@ -14,10 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./qr-code"), exports);
17
18
  __exportStar(require("./relay-bytes"), exports);
18
19
  __exportStar(require("./relay-cipher"), exports);
19
20
  __exportStar(require("./relay-pairing"), exports);
20
21
  __exportStar(require("./relay-protocol"), exports);
21
22
  __exportStar(require("./relay-room"), exports);
23
+ __exportStar(require("./relay-share"), exports);
22
24
  __exportStar(require("./relay-socket"), exports);
23
25
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,43 @@
1
+ /**
2
+ * A QR code (ISO/IEC 18004) of a text, in byte mode with the error correction level M, in the smallest
3
+ * version that holds it. Enough for a link a phone scans; written here rather than taken from a package.
4
+ * The steps and tables follow the standard, as does the well-known reference implementation by Nayuki.
5
+ */
6
+ export declare class QrCode {
7
+ readonly version: number;
8
+ /** Codewords for error correction per block, and blocks, at level M, by version (index 0 unused). */
9
+ protected static readonly ECC_PER_BLOCK: number[];
10
+ protected static readonly BLOCKS: number[];
11
+ /** Level M in the format bits. */
12
+ protected static readonly ECC_FORMAT = 0;
13
+ readonly size: number;
14
+ protected readonly modules: boolean[][];
15
+ protected readonly reserved: boolean[][];
16
+ protected constructor(version: number, data: number[]);
17
+ /** Throws when the text is too long for any version. */
18
+ static encode(text: string): QrCode;
19
+ isDark(x: number, y: number): boolean;
20
+ /** The code as an SVG path of its dark modules, one unit each, without the quiet zone. */
21
+ /**
22
+ * The code as lines of text for a terminal, two rows of modules a line in half blocks (▀ ▄ █), with
23
+ * `quiet` light modules around: print them dark on light.
24
+ */
25
+ lines(quiet?: number): string[];
26
+ path(): string;
27
+ protected static rawModules(version: number): number;
28
+ protected static dataCodewords(version: number): number;
29
+ protected alignmentPositions(): number[];
30
+ protected set(x: number, y: number, dark: boolean): void;
31
+ protected drawPatterns(): void;
32
+ protected drawFormat(mask: number): void;
33
+ /** The data split into blocks, each with its Reed-Solomon codewords, interleaved. */
34
+ protected withEcc(data: number[]): number[];
35
+ protected drawCodewords(codewords: number[]): void;
36
+ protected applyMask(mask: number): void;
37
+ /** How hard the code is to read with a mask: long runs, blocks, finder-like patterns, imbalance. */
38
+ protected penalty(): number;
39
+ protected static multiply(x: number, y: number): number;
40
+ protected static reedSolomonDivisor(degree: number): number[];
41
+ protected static reedSolomonRemainder(data: number[], divisor: number[]): number[];
42
+ }
43
+ //# sourceMappingURL=qr-code.d.ts.map
@@ -0,0 +1,370 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QrCode = void 0;
4
+ /**
5
+ * A QR code (ISO/IEC 18004) of a text, in byte mode with the error correction level M, in the smallest
6
+ * version that holds it. Enough for a link a phone scans; written here rather than taken from a package.
7
+ * The steps and tables follow the standard, as does the well-known reference implementation by Nayuki.
8
+ */
9
+ class QrCode {
10
+ constructor(version, data) {
11
+ this.version = version;
12
+ this.size = version * 4 + 17;
13
+ this.modules = Array.from({ length: this.size }, () => new Array(this.size).fill(false));
14
+ this.reserved = Array.from({ length: this.size }, () => new Array(this.size).fill(false));
15
+ this.drawPatterns();
16
+ this.drawCodewords(this.withEcc(data));
17
+ let best = 0;
18
+ let lowest = Number.POSITIVE_INFINITY;
19
+ for (let mask = 0; mask < 8; mask++) {
20
+ this.applyMask(mask);
21
+ this.drawFormat(mask);
22
+ const penalty = this.penalty();
23
+ if (penalty < lowest) {
24
+ best = mask;
25
+ lowest = penalty;
26
+ }
27
+ this.applyMask(mask);
28
+ }
29
+ this.applyMask(best);
30
+ this.drawFormat(best);
31
+ }
32
+ /** Throws when the text is too long for any version. */
33
+ static encode(text) {
34
+ const bytes = [...new TextEncoder().encode(text)];
35
+ for (let version = 1; version <= 40; version++) {
36
+ const capacity = QrCode.dataCodewords(version) * 8;
37
+ const countBits = version <= 9 ? 8 : 16;
38
+ const used = 4 + countBits + bytes.length * 8;
39
+ if (used <= capacity) {
40
+ const bits = [];
41
+ const push = (value, length) => {
42
+ for (let i = length - 1; i >= 0; i--) {
43
+ bits.push((value >>> i) & 1);
44
+ }
45
+ };
46
+ push(0b0100, 4);
47
+ push(bytes.length, countBits);
48
+ for (const byte of bytes) {
49
+ push(byte, 8);
50
+ }
51
+ push(0, Math.min(4, capacity - bits.length));
52
+ push(0, (8 - (bits.length % 8)) % 8);
53
+ for (let pad = 0xec; bits.length < capacity; pad ^= 0xec ^ 0x11) {
54
+ push(pad, 8);
55
+ }
56
+ const data = [];
57
+ for (let i = 0; i < bits.length; i += 8) {
58
+ data.push(bits.slice(i, i + 8).reduce((byte, bit) => (byte << 1) | bit, 0));
59
+ }
60
+ return new QrCode(version, data);
61
+ }
62
+ }
63
+ throw new Error('The text is too long for a QR code.');
64
+ }
65
+ isDark(x, y) {
66
+ return this.modules[y][x];
67
+ }
68
+ /** The code as an SVG path of its dark modules, one unit each, without the quiet zone. */
69
+ /**
70
+ * The code as lines of text for a terminal, two rows of modules a line in half blocks (▀ ▄ █), with
71
+ * `quiet` light modules around: print them dark on light.
72
+ */
73
+ lines(quiet = 2) {
74
+ const dark = (x, y) => x >= 0 && y >= 0 && x < this.size && y < this.size && this.isDark(x, y);
75
+ const lines = [];
76
+ for (let y = -quiet; y < this.size + quiet; y += 2) {
77
+ let line = '';
78
+ for (let x = -quiet; x < this.size + quiet; x++) {
79
+ const [top, bottom] = [dark(x, y), dark(x, y + 1)];
80
+ line += top && bottom ? '█' : top ? '▀' : bottom ? '▄' : ' ';
81
+ }
82
+ lines.push(line);
83
+ }
84
+ return lines;
85
+ }
86
+ path() {
87
+ let d = '';
88
+ for (let y = 0; y < this.size; y++) {
89
+ for (let x = 0; x < this.size; x++) {
90
+ if (this.modules[y][x]) {
91
+ d += `M${x} ${y}h1v1h-1z`;
92
+ }
93
+ }
94
+ }
95
+ return d;
96
+ }
97
+ static rawModules(version) {
98
+ let result = (16 * version + 128) * version + 64;
99
+ if (version >= 2) {
100
+ const align = Math.floor(version / 7) + 2;
101
+ result -= (25 * align - 10) * align - 55;
102
+ if (version >= 7) {
103
+ result -= 36;
104
+ }
105
+ }
106
+ return result;
107
+ }
108
+ static dataCodewords(version) {
109
+ return Math.floor(QrCode.rawModules(version) / 8) - QrCode.ECC_PER_BLOCK[version] * QrCode.BLOCKS[version];
110
+ }
111
+ alignmentPositions() {
112
+ if (this.version === 1) {
113
+ return [];
114
+ }
115
+ const count = Math.floor(this.version / 7) + 2;
116
+ const step = this.version === 32 ? 26 : Math.ceil((this.version * 4 + 4) / (count * 2 - 2)) * 2;
117
+ const result = [6];
118
+ for (let position = this.size - 7; result.length < count; position -= step) {
119
+ result.splice(1, 0, position);
120
+ }
121
+ return result;
122
+ }
123
+ set(x, y, dark) {
124
+ this.modules[y][x] = dark;
125
+ this.reserved[y][x] = true;
126
+ }
127
+ drawPatterns() {
128
+ for (let i = 0; i < this.size; i++) {
129
+ this.set(6, i, i % 2 === 0);
130
+ this.set(i, 6, i % 2 === 0);
131
+ }
132
+ const finder = (cx, cy) => {
133
+ for (let dy = -4; dy <= 4; dy++) {
134
+ for (let dx = -4; dx <= 4; dx++) {
135
+ const x = cx + dx;
136
+ const y = cy + dy;
137
+ if (x >= 0 && x < this.size && y >= 0 && y < this.size) {
138
+ const distance = Math.max(Math.abs(dx), Math.abs(dy));
139
+ this.set(x, y, distance !== 2 && distance !== 4);
140
+ }
141
+ }
142
+ }
143
+ };
144
+ finder(3, 3);
145
+ finder(this.size - 4, 3);
146
+ finder(3, this.size - 4);
147
+ const positions = this.alignmentPositions();
148
+ for (let i = 0; i < positions.length; i++) {
149
+ for (let j = 0; j < positions.length; j++) {
150
+ const corner = (i === 0 && j === 0) || (i === 0 && j === positions.length - 1) || (i === positions.length - 1 && j === 0);
151
+ if (!corner) {
152
+ for (let dy = -2; dy <= 2; dy++) {
153
+ for (let dx = -2; dx <= 2; dx++) {
154
+ this.set(positions[i] + dx, positions[j] + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ // Reserved now, drawn with each mask.
161
+ this.drawFormat(0);
162
+ if (this.version >= 7) {
163
+ let rem = this.version;
164
+ for (let i = 0; i < 12; i++) {
165
+ rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25);
166
+ }
167
+ const bits = (this.version << 12) | rem;
168
+ for (let i = 0; i < 18; i++) {
169
+ const dark = ((bits >>> i) & 1) !== 0;
170
+ const a = this.size - 11 + (i % 3);
171
+ const b = Math.floor(i / 3);
172
+ this.set(a, b, dark);
173
+ this.set(b, a, dark);
174
+ }
175
+ }
176
+ }
177
+ drawFormat(mask) {
178
+ const data = (QrCode.ECC_FORMAT << 3) | mask;
179
+ let rem = data;
180
+ for (let i = 0; i < 10; i++) {
181
+ rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
182
+ }
183
+ const bits = ((data << 10) | rem) ^ 0x5412;
184
+ const bit = (i) => ((bits >>> i) & 1) !== 0;
185
+ for (let i = 0; i <= 5; i++) {
186
+ this.set(8, i, bit(i));
187
+ }
188
+ this.set(8, 7, bit(6));
189
+ this.set(8, 8, bit(7));
190
+ this.set(7, 8, bit(8));
191
+ for (let i = 9; i < 15; i++) {
192
+ this.set(14 - i, 8, bit(i));
193
+ }
194
+ for (let i = 0; i < 8; i++) {
195
+ this.set(this.size - 1 - i, 8, bit(i));
196
+ }
197
+ for (let i = 8; i < 15; i++) {
198
+ this.set(8, this.size - 15 + i, bit(i));
199
+ }
200
+ this.set(8, this.size - 8, true);
201
+ }
202
+ /** The data split into blocks, each with its Reed-Solomon codewords, interleaved. */
203
+ withEcc(data) {
204
+ const blocks = QrCode.BLOCKS[this.version];
205
+ const eccLength = QrCode.ECC_PER_BLOCK[this.version];
206
+ const raw = Math.floor(QrCode.rawModules(this.version) / 8);
207
+ const shortBlocks = blocks - (raw % blocks);
208
+ const shortLength = Math.floor(raw / blocks);
209
+ const divisor = QrCode.reedSolomonDivisor(eccLength);
210
+ const all = [];
211
+ for (let i = 0, k = 0; i < blocks; i++) {
212
+ const dataLength = shortLength - eccLength + (i < shortBlocks ? 0 : 1);
213
+ const block = data.slice(k, k + dataLength);
214
+ k += dataLength;
215
+ const ecc = QrCode.reedSolomonRemainder(block, divisor);
216
+ if (i < shortBlocks) {
217
+ block.push(0);
218
+ }
219
+ all.push([...block, ...ecc]);
220
+ }
221
+ const result = [];
222
+ for (let i = 0; i < all[0].length; i++) {
223
+ for (let j = 0; j < all.length; j++) {
224
+ // The padding byte of a short block is not sent.
225
+ if (i !== shortLength - eccLength || j >= shortBlocks) {
226
+ result.push(all[j][i]);
227
+ }
228
+ }
229
+ }
230
+ return result;
231
+ }
232
+ drawCodewords(codewords) {
233
+ let i = 0;
234
+ for (let right = this.size - 1; right >= 1; right -= 2) {
235
+ if (right === 6) {
236
+ right = 5;
237
+ }
238
+ for (let vertical = 0; vertical < this.size; vertical++) {
239
+ for (let j = 0; j < 2; j++) {
240
+ const x = right - j;
241
+ const upward = ((right + 1) & 2) === 0;
242
+ const y = upward ? this.size - 1 - vertical : vertical;
243
+ if (!this.reserved[y][x] && i < codewords.length * 8) {
244
+ this.modules[y][x] = ((codewords[i >>> 3] >>> (7 - (i & 7))) & 1) !== 0;
245
+ i++;
246
+ }
247
+ }
248
+ }
249
+ }
250
+ }
251
+ applyMask(mask) {
252
+ for (let y = 0; y < this.size; y++) {
253
+ for (let x = 0; x < this.size; x++) {
254
+ if (this.reserved[y][x]) {
255
+ continue;
256
+ }
257
+ const flip = [
258
+ (x + y) % 2,
259
+ y % 2,
260
+ x % 3,
261
+ (x + y) % 3,
262
+ (Math.floor(x / 3) + Math.floor(y / 2)) % 2,
263
+ ((x * y) % 2) + ((x * y) % 3),
264
+ (((x * y) % 2) + ((x * y) % 3)) % 2,
265
+ (((x + y) % 2) + ((x * y) % 3)) % 2,
266
+ ][mask];
267
+ if (flip === 0) {
268
+ this.modules[y][x] = !this.modules[y][x];
269
+ }
270
+ }
271
+ }
272
+ }
273
+ /** How hard the code is to read with a mask: long runs, blocks, finder-like patterns, imbalance. */
274
+ penalty() {
275
+ let result = 0;
276
+ const line = (get) => {
277
+ for (let i = 0; i < this.size; i++) {
278
+ let run = 1;
279
+ const seen = [];
280
+ for (let j = 0; j < this.size; j++) {
281
+ const dark = get(i, j);
282
+ if (j > 0 && dark === get(i, j - 1)) {
283
+ run++;
284
+ if (run === 5) {
285
+ result += 3;
286
+ }
287
+ else if (run > 5) {
288
+ result++;
289
+ }
290
+ }
291
+ else {
292
+ run = 1;
293
+ }
294
+ seen.push(dark);
295
+ }
296
+ const text = seen.map(dark => (dark ? '1' : '0')).join('');
297
+ for (const pattern of ['10111010000', '00001011101']) {
298
+ for (let at = text.indexOf(pattern); at >= 0; at = text.indexOf(pattern, at + 1)) {
299
+ result += 40;
300
+ }
301
+ }
302
+ }
303
+ };
304
+ line((i, j) => this.modules[i][j]);
305
+ line((i, j) => this.modules[j][i]);
306
+ let dark = 0;
307
+ for (let y = 0; y < this.size; y++) {
308
+ for (let x = 0; x < this.size; x++) {
309
+ const value = this.modules[y][x];
310
+ dark += value ? 1 : 0;
311
+ if (x < this.size - 1 &&
312
+ y < this.size - 1 &&
313
+ value === this.modules[y][x + 1] &&
314
+ value === this.modules[y + 1][x] &&
315
+ value === this.modules[y + 1][x + 1]) {
316
+ result += 3;
317
+ }
318
+ }
319
+ }
320
+ const total = this.size * this.size;
321
+ result += Math.floor(Math.abs(dark * 20 - total * 10) / total) * 10;
322
+ return result;
323
+ }
324
+ static multiply(x, y) {
325
+ let z = 0;
326
+ for (let i = 7; i >= 0; i--) {
327
+ z = (z << 1) ^ ((z >>> 7) * 0x11d);
328
+ z ^= ((y >>> i) & 1) * x;
329
+ }
330
+ return z & 0xff;
331
+ }
332
+ static reedSolomonDivisor(degree) {
333
+ const result = new Array(degree).fill(0);
334
+ result[degree - 1] = 1;
335
+ let root = 1;
336
+ for (let i = 0; i < degree; i++) {
337
+ for (let j = 0; j < result.length; j++) {
338
+ result[j] = QrCode.multiply(result[j], root);
339
+ if (j + 1 < result.length) {
340
+ result[j] ^= result[j + 1];
341
+ }
342
+ }
343
+ root = QrCode.multiply(root, 0x02);
344
+ }
345
+ return result;
346
+ }
347
+ static reedSolomonRemainder(data, divisor) {
348
+ const result = new Array(divisor.length).fill(0);
349
+ for (const byte of data) {
350
+ const factor = byte ^ result.shift();
351
+ result.push(0);
352
+ divisor.forEach((coefficient, i) => {
353
+ result[i] ^= QrCode.multiply(coefficient, factor);
354
+ });
355
+ }
356
+ return result;
357
+ }
358
+ }
359
+ exports.QrCode = QrCode;
360
+ /** Codewords for error correction per block, and blocks, at level M, by version (index 0 unused). */
361
+ QrCode.ECC_PER_BLOCK = [
362
+ -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,
363
+ 28, 28, 28,
364
+ ];
365
+ QrCode.BLOCKS = [
366
+ -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,
367
+ ];
368
+ /** Level M in the format bits. */
369
+ QrCode.ECC_FORMAT = 0;
370
+ //# sourceMappingURL=qr-code.js.map
@@ -4,8 +4,36 @@ export declare const RELAY_DEFAULT_URL = "https://relay.cellbang.com";
4
4
  export declare const RELAY_HOST_PATH = "/v1/host/";
5
5
  export declare const RELAY_CONNECT_PATH = "/v1/connect/";
6
6
  export declare const RELAY_PAIR_PATH = "/pair";
7
+ /** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
8
+ export declare const RELAY_SHARE_PATH = "/share";
7
9
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
8
10
  export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p></body>";
11
+ /** The page a link of an extension opens in a browser. */
12
+ export declare const RELAY_SHARE_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>\n<p>\u6709\u4EBA\u5206\u4EAB\u4E86\u4E00\u4E2A Ta \u6269\u5C55\u3002\u5728 Ta \u5E94\u7528\u91CC\u626B\u7801\uFF0C\u6216\u628A\u8FD9\u4E2A\u94FE\u63A5\u7C98\u8D34\u5230\u201C\u5FEB\u6377\u6253\u5F00\u201D\uFF0C\u5C31\u80FD\u6253\u5F00\u5B83\u3002</p></body>";
13
+ /**
14
+ * A package handed on through the relay (RelayShare): the host, asked on a channel by the key of the share,
15
+ * sends what the package is, then its bytes in chunks small enough for any relay, then the end; sealed as the
16
+ * rest. Never stored on the relay.
17
+ */
18
+ export type RelayShareMessage = {
19
+ t: 'package';
20
+ name: string;
21
+ size: number;
22
+ } | {
23
+ t: 'chunk';
24
+ d: string;
25
+ } | {
26
+ t: 'end';
27
+ } | {
28
+ t: 'error';
29
+ message: string;
30
+ };
31
+ /** The largest package that is handed on: a gzipped tar of a package of the user, its code and a few assets. */
32
+ export declare const RELAY_SHARE_MAX_BYTES: number;
33
+ /** The bytes of a chunk, base64 in a sealed message: well under the 1 MiB of a message of Cloudflare. */
34
+ export declare const RELAY_SHARE_CHUNK_BYTES: number;
35
+ /** How long a link of a package works. */
36
+ export declare const RELAY_SHARE_DAYS = 7;
9
37
  /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
10
38
  export declare const RELAY_ID: RegExp;
11
39
  /** Close codes of the relay. */
@@ -19,8 +47,21 @@ export declare enum RelayCloseCode {
19
47
  /** The host closed this channel, or refused the device. */
20
48
  CLOSED_BY_HOST = 4410,
21
49
  /** Too many devices are connected to this host. */
22
- FULL = 4429
50
+ FULL = 4429,
51
+ /** No answer in time: to the hello, or to the pings of the heartbeat; the connection is gone without a word. */
52
+ TIMEOUT = 4408
23
53
  }
54
+ /**
55
+ * The heartbeat: hosts and devices send `ping` to the relay every so often, outside the channels, and it
56
+ * answers `pong` itself (a Durable Object without waking). A connection that stops being answered is gone
57
+ * without a word, as one through a proxy or a network that changed often is, and is made again; the relay
58
+ * finds such connections by the pings that stopped.
59
+ */
60
+ export declare const RELAY_PING = "ping";
61
+ export declare const RELAY_PONG = "pong";
62
+ /** Between pings. A peer is stale once it missed about three of them (RELAY_STALE_MS). */
63
+ export declare const RELAY_PING_MS = 25000;
64
+ export declare const RELAY_STALE_MS = 80000;
24
65
  /**
25
66
  * Between the relay and a host, which takes the connections of many devices over one WebSocket: each
26
67
  * device connection is a channel. What a device sends goes to the host as `data`, and back; the relay
@@ -1,17 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RELAY_PROOF = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_PAIR_PAGE = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
3
+ exports.RELAY_PROOF = exports.RELAY_STALE_MS = exports.RELAY_PING_MS = exports.RELAY_PONG = exports.RELAY_PING = exports.RelayCloseCode = exports.RELAY_ID = exports.RELAY_SHARE_DAYS = exports.RELAY_SHARE_CHUNK_BYTES = exports.RELAY_SHARE_MAX_BYTES = exports.RELAY_SHARE_PAGE = exports.RELAY_PAIR_PAGE = exports.RELAY_SHARE_PATH = exports.RELAY_PAIR_PATH = exports.RELAY_CONNECT_PATH = exports.RELAY_HOST_PATH = exports.RELAY_DEFAULT_URL = void 0;
4
4
  /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
5
5
  exports.RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
6
6
  /** What the relay serves: hosts connect to the first, devices to the second. */
7
7
  exports.RELAY_HOST_PATH = '/v1/host/';
8
8
  exports.RELAY_CONNECT_PATH = '/v1/connect/';
9
9
  exports.RELAY_PAIR_PATH = '/pair';
10
+ /** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
11
+ exports.RELAY_SHARE_PATH = '/share';
10
12
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
11
13
  exports.RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
12
14
  <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
13
15
  <h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
14
16
  <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p></body>`;
17
+ /** The page a link of an extension opens in a browser. */
18
+ exports.RELAY_SHARE_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
19
+ <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
20
+ <h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>
21
+ <p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p></body>`;
22
+ /** The largest package that is handed on: a gzipped tar of a package of the user, its code and a few assets. */
23
+ exports.RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
24
+ /** The bytes of a chunk, base64 in a sealed message: well under the 1 MiB of a message of Cloudflare. */
25
+ exports.RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
26
+ /** How long a link of a package works. */
27
+ exports.RELAY_SHARE_DAYS = 7;
15
28
  /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
16
29
  exports.RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
17
30
  /** Close codes of the relay. */
@@ -27,6 +40,19 @@ var RelayCloseCode;
27
40
  RelayCloseCode[RelayCloseCode["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
28
41
  /** Too many devices are connected to this host. */
29
42
  RelayCloseCode[RelayCloseCode["FULL"] = 4429] = "FULL";
43
+ /** No answer in time: to the hello, or to the pings of the heartbeat; the connection is gone without a word. */
44
+ RelayCloseCode[RelayCloseCode["TIMEOUT"] = 4408] = "TIMEOUT";
30
45
  })(RelayCloseCode || (exports.RelayCloseCode = RelayCloseCode = {}));
46
+ /**
47
+ * The heartbeat: hosts and devices send `ping` to the relay every so often, outside the channels, and it
48
+ * answers `pong` itself (a Durable Object without waking). A connection that stops being answered is gone
49
+ * without a word, as one through a proxy or a network that changed often is, and is made again; the relay
50
+ * finds such connections by the pings that stopped.
51
+ */
52
+ exports.RELAY_PING = 'ping';
53
+ exports.RELAY_PONG = 'pong';
54
+ /** Between pings. A peer is stale once it missed about three of them (RELAY_STALE_MS). */
55
+ exports.RELAY_PING_MS = 25000;
56
+ exports.RELAY_STALE_MS = 80000;
31
57
  exports.RELAY_PROOF = 'ta-relay ready';
32
58
  //# sourceMappingURL=relay-protocol.js.map
@@ -26,6 +26,7 @@ export declare class RelayRoom {
26
26
  clientClosed(channel: string): void;
27
27
  /** What the host sends that is not a message of the protocol is dropped. */
28
28
  fromHost(text: string): void;
29
+ /** The host went, or connected again (its channels went with the connection before): its devices are told to come again. */
29
30
  hostClosed(): void;
30
31
  protected toHost(host: RelayPeer, message: RelayHostMessage): void;
31
32
  }