hermes-pilot 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ja.md +419 -0
- package/README.md +428 -2
- package/__init__.py +15 -0
- package/apps/bridge/src/api-errors.js +165 -0
- package/apps/bridge/src/architect.js +1298 -0
- package/apps/bridge/src/auth.js +202 -0
- package/apps/bridge/src/capabilities.js +256 -0
- package/apps/bridge/src/capture.js +145 -0
- package/apps/bridge/src/chat-profile.js +95 -0
- package/apps/bridge/src/config.js +73 -0
- package/apps/bridge/src/discord.js +89 -0
- package/apps/bridge/src/gitinfo.js +211 -0
- package/apps/bridge/src/hermes.js +97 -0
- package/apps/bridge/src/inbox.js +347 -0
- package/apps/bridge/src/kanban.js +361 -0
- package/apps/bridge/src/lang.js +60 -0
- package/apps/bridge/src/live.js +285 -0
- package/apps/bridge/src/memos.js +150 -0
- package/apps/bridge/src/ops.js +201 -0
- package/apps/bridge/src/pair-codes.js +125 -0
- package/apps/bridge/src/pipeline-optimizer.js +513 -0
- package/apps/bridge/src/pipeline-watcher.js +1022 -0
- package/apps/bridge/src/qr.js +808 -0
- package/apps/bridge/src/repos.js +544 -0
- package/apps/bridge/src/review-watcher.js +271 -0
- package/apps/bridge/src/review.js +1749 -0
- package/apps/bridge/src/search.js +162 -0
- package/apps/bridge/src/server.js +1745 -0
- package/apps/bridge/src/snippets.js +373 -0
- package/apps/bridge/src/specs.js +806 -0
- package/apps/bridge/src/tail.js +28 -0
- package/apps/bridge/src/telemetry.js +667 -0
- package/apps/bridge/src/term-snippets.js +228 -0
- package/apps/bridge/src/transcript.js +231 -0
- package/apps/bridge/src/usage.js +294 -0
- package/apps/bridge/src/workerFeed.js +72 -0
- package/apps/dashboard/index.js +2014 -0
- package/apps/dashboard/manifest.json +14 -0
- package/apps/dashboard/pilot_dashboard.py +1466 -0
- package/apps/dashboard/plugin_api.py +237 -0
- package/apps/dashboard/plugin_init.py +15 -0
- package/apps/dashboard/style.css +928 -0
- package/apps/web/dist/assets/TermPane-DYP7pi_n.css +32 -0
- package/apps/web/dist/assets/TermPane-q4f5Zu0I.js +12 -0
- package/apps/web/dist/assets/index-BzUof8lw.js +94 -0
- package/apps/web/dist/assets/index-DwZdbPU7.css +1 -0
- package/apps/web/dist/favicon.ico +0 -0
- package/apps/web/dist/icon-180-v6.png +0 -0
- package/apps/web/dist/icon-192-v6.png +0 -0
- package/apps/web/dist/icon-512-v6.png +0 -0
- package/apps/web/dist/icon-maskable-192-v6.png +0 -0
- package/apps/web/dist/icon-maskable-512-v6.png +0 -0
- package/apps/web/dist/icon-v6.svg +15 -0
- package/apps/web/dist/index.html +21 -0
- package/apps/web/dist/manifest.webmanifest +49 -0
- package/apps/web/dist/sw.js +69 -0
- package/bin/hermes-pilot.mjs +964 -0
- package/dashboard/dist/index.js +2014 -0
- package/dashboard/dist/style.css +928 -0
- package/dashboard/manifest.json +14 -0
- package/dashboard/pilot_dashboard.py +1466 -0
- package/dashboard/plugin_api.py +237 -0
- package/package.json +75 -4
- package/plugin.json +70 -0
- package/plugin.yaml +4 -0
- package/scripts/deploy.mjs +58 -0
- package/scripts/migrate-to-hermes-pilot.mjs +472 -0
- package/scripts/postinstall.mjs +32 -0
- package/scripts/sync-dashboard.mjs +98 -0
- package/skills/annotation/SKILL.md +138 -0
- package/skills/quick-review/SKILL.md +200 -0
- package/skills/quick-review/assets/sheet-template.html +175 -0
- package/skills/spec-architect/SKILL.md +226 -0
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ペアリング用 QR コードと接続先候補の検出(SPEC §8.1 / §8.2、Batch T6)。
|
|
3
|
+
*
|
|
4
|
+
* 依存ゼロの QR エンコーダ(byte モード・UTF-8・誤り訂正レベル M・型番 1〜10)。
|
|
5
|
+
* - 型番 10-M で 213 バイトまで入る。ペアリング URL(`https://<host>/?token=<64hex>`)は
|
|
6
|
+
* 80 バイト前後なので余裕がある。それ以上は明示的に失敗させる(黙って切らない)。
|
|
7
|
+
* - SVG は `shape-rendering="crispEdges"` の 1 パス。ターミナルは ANSI 背景色つきの
|
|
8
|
+
* 半角ブロック(`▀▄█`)2 モジュール分を 1 行に圧縮して出す。
|
|
9
|
+
*
|
|
10
|
+
* 検証方針(重要): 生成したモジュール行列は **qrencode(1) の PBM 出力と完全一致**することを
|
|
11
|
+
* 単体テスト(`apps/bridge/test/qr.test.js`)で確かめている。目視に頼らない。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import os from 'node:os'
|
|
15
|
+
import zlib from 'node:zlib'
|
|
16
|
+
|
|
17
|
+
/** 誤り訂正レベル M のブロック構造(型番: [EC コードワード/ブロック, 群1ブロック数, 群1データ長, 群2ブロック数, 群2データ長])。 */
|
|
18
|
+
const ECC_M = {
|
|
19
|
+
1: [10, 1, 16, 0, 0],
|
|
20
|
+
2: [16, 1, 28, 0, 0],
|
|
21
|
+
3: [26, 1, 44, 0, 0],
|
|
22
|
+
4: [18, 2, 32, 0, 0],
|
|
23
|
+
5: [24, 2, 43, 0, 0],
|
|
24
|
+
6: [16, 4, 27, 0, 0],
|
|
25
|
+
7: [18, 4, 31, 0, 0],
|
|
26
|
+
8: [22, 2, 38, 2, 39],
|
|
27
|
+
9: [22, 3, 36, 2, 37],
|
|
28
|
+
10: [26, 4, 43, 1, 44],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 誤り訂正レベル M の識別ビット(仕様では 00)。 */
|
|
32
|
+
const ECL_M_BITS = 0b00
|
|
33
|
+
|
|
34
|
+
/** 対応する最大型番。 */
|
|
35
|
+
export const MAX_VERSION = 10
|
|
36
|
+
|
|
37
|
+
/** 隅の余白(モジュール数)。仕様の 4 モジュールを既定にする。 */
|
|
38
|
+
export const DEFAULT_MARGIN = 4
|
|
39
|
+
|
|
40
|
+
// --- Reed-Solomon (GF(256)) -------------------------------------------------
|
|
41
|
+
|
|
42
|
+
const EXP = new Uint8Array(512)
|
|
43
|
+
const LOG = new Uint8Array(256)
|
|
44
|
+
{
|
|
45
|
+
let x = 1
|
|
46
|
+
for (let i = 0; i < 255; i++) {
|
|
47
|
+
EXP[i] = x
|
|
48
|
+
LOG[x] = i
|
|
49
|
+
x <<= 1
|
|
50
|
+
if (x & 0x100) x ^= 0x11d
|
|
51
|
+
}
|
|
52
|
+
for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* GF(256) の掛け算。
|
|
57
|
+
* @param {number} a 左辺
|
|
58
|
+
* @param {number} b 右辺
|
|
59
|
+
* @returns {number} 積
|
|
60
|
+
*/
|
|
61
|
+
function gfMul(a, b) {
|
|
62
|
+
if (a === 0 || b === 0) return 0
|
|
63
|
+
return EXP[LOG[a] + LOG[b]]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 生成多項式(最高次が 1)の係数列。
|
|
68
|
+
* @param {number} degree 次数
|
|
69
|
+
* @returns {Uint8Array} 係数(次数の降順、先頭は常に 1)
|
|
70
|
+
*/
|
|
71
|
+
function rsDivisor(degree) {
|
|
72
|
+
const result = new Uint8Array(degree)
|
|
73
|
+
result[degree - 1] = 1
|
|
74
|
+
let root = 1
|
|
75
|
+
for (let i = 0; i < degree; i++) {
|
|
76
|
+
for (let j = 0; j < degree; j++) {
|
|
77
|
+
result[j] = gfMul(result[j], root)
|
|
78
|
+
if (j + 1 < degree) result[j] ^= result[j + 1]
|
|
79
|
+
}
|
|
80
|
+
root = gfMul(root, 0x02)
|
|
81
|
+
}
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* データコードワードから誤り訂正コードワードを作る。
|
|
87
|
+
* @param {Uint8Array|number[]} data データコードワード
|
|
88
|
+
* @param {number} ecLen 生成する EC コードワード数
|
|
89
|
+
* @returns {Uint8Array} EC コードワード
|
|
90
|
+
*/
|
|
91
|
+
export function reedSolomon(data, ecLen) {
|
|
92
|
+
const divisor = rsDivisor(ecLen)
|
|
93
|
+
const result = new Uint8Array(ecLen)
|
|
94
|
+
for (const b of data) {
|
|
95
|
+
const factor = b ^ result[0]
|
|
96
|
+
result.copyWithin(0, 1)
|
|
97
|
+
result[ecLen - 1] = 0
|
|
98
|
+
for (let i = 0; i < ecLen; i++) result[i] ^= gfMul(divisor[i], factor)
|
|
99
|
+
}
|
|
100
|
+
return result
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --- ビット列・コードワード -------------------------------------------------
|
|
104
|
+
|
|
105
|
+
/** ビット列ビルダー。 */
|
|
106
|
+
class BitStream {
|
|
107
|
+
constructor() {
|
|
108
|
+
this.bits = []
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {number} value 値
|
|
113
|
+
* @param {number} len ビット長
|
|
114
|
+
*/
|
|
115
|
+
push(value, len) {
|
|
116
|
+
for (let i = len - 1; i >= 0; i--) this.bits.push((value >>> i) & 1)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @returns {number[]} バイト配列 */
|
|
120
|
+
toBytes() {
|
|
121
|
+
const out = []
|
|
122
|
+
for (let i = 0; i < this.bits.length; i += 8) {
|
|
123
|
+
let b = 0
|
|
124
|
+
for (let j = 0; j < 8; j++) b = (b << 1) | (this.bits[i + j] ?? 0)
|
|
125
|
+
out.push(b)
|
|
126
|
+
}
|
|
127
|
+
return out
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 型番ごとのデータ容量(コードワード数)。
|
|
133
|
+
* @param {number} version 型番
|
|
134
|
+
* @returns {number} データコードワード数
|
|
135
|
+
*/
|
|
136
|
+
function dataCodewords(version) {
|
|
137
|
+
const [, b1, d1, b2, d2] = ECC_M[version]
|
|
138
|
+
return b1 * d1 + b2 * d2
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 文字数指示子のビット長(byte モード)。
|
|
143
|
+
* @param {number} version 型番
|
|
144
|
+
* @returns {number} ビット長
|
|
145
|
+
*/
|
|
146
|
+
function countBits(version) {
|
|
147
|
+
return version <= 9 ? 8 : 16
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 生のデータモジュール数(余りビット込み)。
|
|
152
|
+
* @param {number} version 型番
|
|
153
|
+
* @returns {number} モジュール数
|
|
154
|
+
*/
|
|
155
|
+
function rawDataModules(version) {
|
|
156
|
+
let result = (16 * version + 128) * version + 64
|
|
157
|
+
if (version >= 2) {
|
|
158
|
+
const numAlign = Math.floor(version / 7) + 2
|
|
159
|
+
result -= (25 * numAlign - 10) * numAlign - 55
|
|
160
|
+
if (version >= 7) result -= 36
|
|
161
|
+
}
|
|
162
|
+
return result
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 余りビット数(型番により 0/3/4/7)。
|
|
167
|
+
* @param {number} version 型番
|
|
168
|
+
* @returns {number} ビット数
|
|
169
|
+
*/
|
|
170
|
+
function remainderBits(version) {
|
|
171
|
+
return rawDataModules(version) - Math.floor(rawDataModules(version) / 8) * 8
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 入力バイト列を型番に合わせてコードワード化し、ブロック間でインターリーブする。
|
|
176
|
+
* @param {Uint8Array} bytes 入力(UTF-8)
|
|
177
|
+
* @param {number} version 型番
|
|
178
|
+
* @returns {number[]} 最終コードワード列(データ→EC の順にインターリーブ済み)
|
|
179
|
+
*/
|
|
180
|
+
export function buildCodewords(bytes, version) {
|
|
181
|
+
const [ecLen, b1, d1, b2, d2] = ECC_M[version]
|
|
182
|
+
const capacityBits = dataCodewords(version) * 8
|
|
183
|
+
const stream = new BitStream()
|
|
184
|
+
stream.push(0b0100, 4)
|
|
185
|
+
stream.push(bytes.length, countBits(version))
|
|
186
|
+
for (const b of bytes) stream.push(b, 8)
|
|
187
|
+
// 終端(最大 4 ビット)→ バイト境界まで 0
|
|
188
|
+
const terminator = Math.min(4, capacityBits - stream.bits.length)
|
|
189
|
+
stream.push(0, terminator)
|
|
190
|
+
while (stream.bits.length % 8 !== 0) stream.push(0, 1)
|
|
191
|
+
const data = stream.toBytes()
|
|
192
|
+
// 埋め草(0xEC / 0x11 の交互)
|
|
193
|
+
const total = dataCodewords(version)
|
|
194
|
+
for (let i = 0; data.length < total; i++) data.push(i % 2 === 0 ? 0xec : 0x11)
|
|
195
|
+
|
|
196
|
+
const blocks = []
|
|
197
|
+
let offset = 0
|
|
198
|
+
for (let i = 0; i < b1 + b2; i++) {
|
|
199
|
+
const len = i < b1 ? d1 : d2
|
|
200
|
+
const chunk = data.slice(offset, offset + len)
|
|
201
|
+
offset += len
|
|
202
|
+
blocks.push({ data: chunk, ecc: Array.from(reedSolomon(chunk, ecLen)) })
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const out = []
|
|
206
|
+
const maxData = Math.max(...blocks.map((b) => b.data.length))
|
|
207
|
+
for (let i = 0; i < maxData; i++) {
|
|
208
|
+
for (const b of blocks) if (i < b.data.length) out.push(b.data[i])
|
|
209
|
+
}
|
|
210
|
+
for (let i = 0; i < ecLen; i++) {
|
|
211
|
+
for (const b of blocks) out.push(b.ecc[i])
|
|
212
|
+
}
|
|
213
|
+
return out
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// --- 行列 -------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
/** BCH(15,5) で形式情報を作る。 */
|
|
219
|
+
function formatBits(mask) {
|
|
220
|
+
const data = (ECL_M_BITS << 3) | mask
|
|
221
|
+
let rem = data
|
|
222
|
+
for (let i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >>> 9) * 0x537)
|
|
223
|
+
return ((data << 10) | rem) ^ 0x5412
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** BCH(18,6) で型番情報を作る。 */
|
|
227
|
+
function versionBits(version) {
|
|
228
|
+
let rem = version
|
|
229
|
+
for (let i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25)
|
|
230
|
+
return (version << 12) | rem
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** 位置検出パターンの中心座標。 */
|
|
234
|
+
function alignmentPositions(version) {
|
|
235
|
+
if (version === 1) return []
|
|
236
|
+
const numAlign = Math.floor(version / 7) + 2
|
|
237
|
+
const step = version === 32 ? 26 : Math.ceil((version * 4 + 4) / (numAlign * 2 - 2)) * 2
|
|
238
|
+
const result = [6]
|
|
239
|
+
for (let pos = version * 4 + 10; result.length < numAlign; pos -= step) result.splice(1, 0, pos)
|
|
240
|
+
return result
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* キャンバス(モジュール行列+機能モジュール印)を作る。
|
|
245
|
+
* @param {number} version 型番
|
|
246
|
+
* @returns {{size: number, modules: Uint8Array, func: Uint8Array}} キャンバス
|
|
247
|
+
*/
|
|
248
|
+
function createCanvas(version) {
|
|
249
|
+
const size = version * 4 + 17
|
|
250
|
+
const modules = new Uint8Array(size * size)
|
|
251
|
+
const func = new Uint8Array(size * size)
|
|
252
|
+
const set = (x, y, dark) => {
|
|
253
|
+
modules[y * size + x] = dark ? 1 : 0
|
|
254
|
+
func[y * size + x] = 1
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 位置検出パターン+分離パターン
|
|
258
|
+
for (const [cx, cy] of [
|
|
259
|
+
[3, 3],
|
|
260
|
+
[size - 4, 3],
|
|
261
|
+
[3, size - 4],
|
|
262
|
+
]) {
|
|
263
|
+
for (let dy = -4; dy <= 4; dy++) {
|
|
264
|
+
for (let dx = -4; dx <= 4; dx++) {
|
|
265
|
+
const x = cx + dx
|
|
266
|
+
const y = cy + dy
|
|
267
|
+
if (x < 0 || x >= size || y < 0 || y >= size) continue
|
|
268
|
+
const dist = Math.max(Math.abs(dx), Math.abs(dy))
|
|
269
|
+
set(x, y, dist !== 2 && dist !== 4)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// タイミングパターン
|
|
275
|
+
for (let i = 0; i < size; i++) {
|
|
276
|
+
if (!func[6 * size + i]) set(i, 6, i % 2 === 0)
|
|
277
|
+
if (!func[i * size + 6]) set(6, i, i % 2 === 0)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 位置合わせパターン
|
|
281
|
+
const pos = alignmentPositions(version)
|
|
282
|
+
for (let i = 0; i < pos.length; i++) {
|
|
283
|
+
for (let j = 0; j < pos.length; j++) {
|
|
284
|
+
// 位置検出パターンと重なる 3 箇所は置かない
|
|
285
|
+
if ((i === 0 && j === 0) || (i === 0 && j === pos.length - 1) || (i === pos.length - 1 && j === 0)) continue
|
|
286
|
+
for (let dy = -2; dy <= 2; dy++) {
|
|
287
|
+
for (let dx = -2; dx <= 2; dx++) {
|
|
288
|
+
set(pos[i] + dx, pos[j] + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1)
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// 形式情報・型番情報の領域を予約する(値は後で書く)
|
|
295
|
+
const reserve = (x, y) => {
|
|
296
|
+
func[y * size + x] = 1
|
|
297
|
+
}
|
|
298
|
+
for (let i = 0; i <= 5; i++) reserve(8, i)
|
|
299
|
+
reserve(8, 7)
|
|
300
|
+
reserve(8, 8)
|
|
301
|
+
reserve(7, 8)
|
|
302
|
+
for (let i = 9; i < 15; i++) reserve(14 - i, 8)
|
|
303
|
+
for (let i = 0; i < 8; i++) reserve(size - 1 - i, 8)
|
|
304
|
+
for (let i = 8; i < 15; i++) reserve(8, size - 15 + i)
|
|
305
|
+
reserve(8, size - 8)
|
|
306
|
+
|
|
307
|
+
if (version >= 7) {
|
|
308
|
+
for (let i = 0; i < 18; i++) {
|
|
309
|
+
const a = size - 11 + (i % 3)
|
|
310
|
+
const b = Math.floor(i / 3)
|
|
311
|
+
reserve(a, b)
|
|
312
|
+
reserve(b, a)
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return { size, modules, func, set }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** マスク条件。 */
|
|
320
|
+
function maskBit(mask, x, y) {
|
|
321
|
+
switch (mask) {
|
|
322
|
+
case 0:
|
|
323
|
+
return (x + y) % 2 === 0
|
|
324
|
+
case 1:
|
|
325
|
+
return y % 2 === 0
|
|
326
|
+
case 2:
|
|
327
|
+
return x % 3 === 0
|
|
328
|
+
case 3:
|
|
329
|
+
return (x + y) % 3 === 0
|
|
330
|
+
case 4:
|
|
331
|
+
return (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0
|
|
332
|
+
case 5:
|
|
333
|
+
return ((x * y) % 2) + ((x * y) % 3) === 0
|
|
334
|
+
case 6:
|
|
335
|
+
return (((x * y) % 2) + ((x * y) % 3)) % 2 === 0
|
|
336
|
+
case 7:
|
|
337
|
+
return (((x + y) % 2) + ((x * y) % 3)) % 2 === 0
|
|
338
|
+
default:
|
|
339
|
+
throw new Error(`mask out of range: ${mask}`)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** 形式情報・型番情報を書き込む(対象は渡された modules 配列。func は既に予約済み)。 */
|
|
344
|
+
function drawFormatAndVersion(modules, size, version, mask) {
|
|
345
|
+
const set = (x, y, dark) => {
|
|
346
|
+
modules[y * size + x] = dark ? 1 : 0
|
|
347
|
+
}
|
|
348
|
+
const bits = formatBits(mask)
|
|
349
|
+
const bit = (i) => ((bits >>> i) & 1) !== 0
|
|
350
|
+
for (let i = 0; i <= 5; i++) set(8, i, bit(i))
|
|
351
|
+
set(8, 7, bit(6))
|
|
352
|
+
set(8, 8, bit(7))
|
|
353
|
+
set(7, 8, bit(8))
|
|
354
|
+
for (let i = 9; i < 15; i++) set(14 - i, 8, bit(i))
|
|
355
|
+
for (let i = 0; i < 8; i++) set(size - 1 - i, 8, bit(i))
|
|
356
|
+
for (let i = 8; i < 15; i++) set(8, size - 15 + i, bit(i))
|
|
357
|
+
set(8, size - 8, true) // 常時ダーク
|
|
358
|
+
|
|
359
|
+
if (version >= 7) {
|
|
360
|
+
const vbits = versionBits(version)
|
|
361
|
+
for (let i = 0; i < 18; i++) {
|
|
362
|
+
const dark = ((vbits >>> i) & 1) !== 0
|
|
363
|
+
const a = size - 11 + (i % 3)
|
|
364
|
+
const b = Math.floor(i / 3)
|
|
365
|
+
set(a, b, dark)
|
|
366
|
+
set(b, a, dark)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** データコードワードをジグザグに配置する。 */
|
|
372
|
+
function drawCodewords(canvas, codewords) {
|
|
373
|
+
const { size, modules, func } = canvas
|
|
374
|
+
let i = 0
|
|
375
|
+
for (let right = size - 1; right >= 1; right -= 2) {
|
|
376
|
+
if (right === 6) right = 5
|
|
377
|
+
for (let vert = 0; vert < size; vert++) {
|
|
378
|
+
for (let j = 0; j < 2; j++) {
|
|
379
|
+
const x = right - j
|
|
380
|
+
const upward = ((right + 1) & 2) === 0
|
|
381
|
+
const y = upward ? size - 1 - vert : vert
|
|
382
|
+
if (func[y * size + x]) continue
|
|
383
|
+
if (i < codewords.length * 8) {
|
|
384
|
+
modules[y * size + x] = (codewords[i >>> 3] >>> (7 - (i & 7))) & 1
|
|
385
|
+
i++
|
|
386
|
+
}
|
|
387
|
+
// 余りビットは 0 のまま(既に 0)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** マスクを適用する(機能モジュールは変えない)。 */
|
|
394
|
+
function applyMask(canvas, mask) {
|
|
395
|
+
const { size, modules, func } = canvas
|
|
396
|
+
for (let y = 0; y < size; y++) {
|
|
397
|
+
for (let x = 0; x < size; x++) {
|
|
398
|
+
if (func[y * size + x]) continue
|
|
399
|
+
if (maskBit(mask, x, y)) modules[y * size + x] ^= 1
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** ペナルティ(仕様の 4 規則)。 */
|
|
405
|
+
function penalty(modules, size) {
|
|
406
|
+
const N1 = 3
|
|
407
|
+
const N2 = 3
|
|
408
|
+
const N3 = 40
|
|
409
|
+
const N4 = 10
|
|
410
|
+
let result = 0
|
|
411
|
+
|
|
412
|
+
const finderPenaltyAddHistory = (runLength, history) => {
|
|
413
|
+
let len = runLength
|
|
414
|
+
if (history[0] === 0) len += size
|
|
415
|
+
history.copyWithin(1, 0, history.length - 1)
|
|
416
|
+
history[0] = len
|
|
417
|
+
}
|
|
418
|
+
const finderPenaltyCount = (history) => {
|
|
419
|
+
const n = history[1]
|
|
420
|
+
const core = n > 0 && history[2] === n && history[3] === n * 3 && history[4] === n && history[5] === n
|
|
421
|
+
return (core && history[0] >= n * 4 && history[6] >= n ? 1 : 0) + (core && history[6] >= n * 4 && history[0] >= n ? 1 : 0)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const scan = (get) => {
|
|
425
|
+
for (let a = 0; a < size; a++) {
|
|
426
|
+
let runColor = false
|
|
427
|
+
let runLen = 0
|
|
428
|
+
const history = new Int32Array(7)
|
|
429
|
+
for (let b = 0; b < size; b++) {
|
|
430
|
+
if (get(a, b) === runColor) {
|
|
431
|
+
runLen++
|
|
432
|
+
if (runLen === 5) result += N1
|
|
433
|
+
else if (runLen > 5) result++
|
|
434
|
+
} else {
|
|
435
|
+
finderPenaltyAddHistory(runLen, history)
|
|
436
|
+
if (!runColor) result += finderPenaltyCount(history) * N3
|
|
437
|
+
runColor = get(a, b)
|
|
438
|
+
runLen = 1
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (runColor) {
|
|
442
|
+
finderPenaltyAddHistory(runLen, history)
|
|
443
|
+
runLen = 0
|
|
444
|
+
}
|
|
445
|
+
runLen += size
|
|
446
|
+
finderPenaltyAddHistory(runLen, history)
|
|
447
|
+
result += finderPenaltyCount(history) * N3
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
scan((y, x) => modules[y * size + x] !== 0) // 行
|
|
452
|
+
scan((x, y) => modules[y * size + x] !== 0) // 列
|
|
453
|
+
|
|
454
|
+
for (let y = 0; y < size - 1; y++) {
|
|
455
|
+
for (let x = 0; x < size - 1; x++) {
|
|
456
|
+
const c = modules[y * size + x]
|
|
457
|
+
if (c === modules[y * size + x + 1] && c === modules[(y + 1) * size + x] && c === modules[(y + 1) * size + x + 1]) {
|
|
458
|
+
result += N2
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
let dark = 0
|
|
464
|
+
for (const v of modules) dark += v ? 1 : 0
|
|
465
|
+
const total = size * size
|
|
466
|
+
const k = Math.floor((Math.abs(dark * 20 - total * 10) + total - 1) / total) - 1
|
|
467
|
+
result += k * N4
|
|
468
|
+
return result
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* 型番を選ぶ(誤り訂正レベル M・byte モード)。
|
|
473
|
+
* @param {number} byteLength 入力バイト長
|
|
474
|
+
* @returns {number} 型番
|
|
475
|
+
*/
|
|
476
|
+
export function chooseVersion(byteLength) {
|
|
477
|
+
for (let v = 1; v <= MAX_VERSION; v++) {
|
|
478
|
+
const capacityBits = dataCodewords(v) * 8
|
|
479
|
+
const needed = 4 + countBits(v) + byteLength * 8
|
|
480
|
+
if (needed <= capacityBits) return v
|
|
481
|
+
}
|
|
482
|
+
throw new Error(`PAIRING_TOO_LONG: ${byteLength} bytes exceeds version ${MAX_VERSION}-M capacity`)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* QR コードのモジュール行列を作る(余白なし)。
|
|
487
|
+
* @param {string} text 内容
|
|
488
|
+
* @param {object} [opts] オプション
|
|
489
|
+
* @param {number} [opts.mask] マスク番号を固定する(未指定ならペナルティ最小を選ぶ)
|
|
490
|
+
* @returns {{size: number, version: number, modules: boolean[][], mask: number}} 行列
|
|
491
|
+
*/
|
|
492
|
+
export function qrMatrix(text, { mask } = {}) {
|
|
493
|
+
const bytes = new Uint8Array(Buffer.from(String(text), 'utf8'))
|
|
494
|
+
const version = chooseVersion(bytes.length)
|
|
495
|
+
const codewords = buildCodewords(bytes, version)
|
|
496
|
+
const canvas = createCanvas(version)
|
|
497
|
+
drawCodewords(canvas, codewords)
|
|
498
|
+
|
|
499
|
+
let best = null
|
|
500
|
+
const masks = typeof mask === 'number' ? [mask] : [0, 1, 2, 3, 4, 5, 6, 7]
|
|
501
|
+
for (const m of masks) {
|
|
502
|
+
const trial = {
|
|
503
|
+
size: canvas.size,
|
|
504
|
+
modules: canvas.modules.slice(),
|
|
505
|
+
func: canvas.func,
|
|
506
|
+
set: canvas.set,
|
|
507
|
+
}
|
|
508
|
+
applyMask(trial, m)
|
|
509
|
+
drawFormatAndVersion(trial.modules, trial.size, version, m)
|
|
510
|
+
const score = penalty(trial.modules, trial.size)
|
|
511
|
+
if (!best || score < best.score) best = { score, modules: trial.modules, mask: m }
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const { size } = canvas
|
|
515
|
+
const grid = []
|
|
516
|
+
for (let y = 0; y < size; y++) {
|
|
517
|
+
const row = []
|
|
518
|
+
for (let x = 0; x < size; x++) row.push(best.modules[y * size + x] !== 0)
|
|
519
|
+
grid.push(row)
|
|
520
|
+
}
|
|
521
|
+
return { size, version, modules: grid, mask: best.mask }
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* QR コードを SVG にする(外部依存なし・1 パス)。
|
|
526
|
+
* @param {string} text 内容
|
|
527
|
+
* @param {object} [opts] オプション
|
|
528
|
+
* @param {number} [opts.margin] 余白モジュール数(既定 4)
|
|
529
|
+
* @param {number} [opts.scale] 1 モジュールのピクセル数(既定: viewBox は 1 単位)
|
|
530
|
+
* @param {string} [opts.dark] 暗色
|
|
531
|
+
* @param {string} [opts.light] 明色
|
|
532
|
+
* @param {string} [opts.title] アクセシブル名
|
|
533
|
+
* @returns {string} SVG 文字列
|
|
534
|
+
*/
|
|
535
|
+
export function qrSvg(text, { margin = DEFAULT_MARGIN, dark = '#0a0f14', light = '#ffffff', title = 'Pairing QR' } = {}) {
|
|
536
|
+
const { size, modules } = qrMatrix(text)
|
|
537
|
+
const total = size + margin * 2
|
|
538
|
+
let path = ''
|
|
539
|
+
for (let y = 0; y < size; y++) {
|
|
540
|
+
for (let x = 0; x < size; x++) {
|
|
541
|
+
if (!modules[y][x]) continue
|
|
542
|
+
path += `M${x + margin} ${y + margin}h1v1h-1z`
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return (
|
|
546
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${total} ${total}" ` +
|
|
547
|
+
`width="${total * 8}" height="${total * 8}" role="img" aria-label="${escapeXml(title)}" ` +
|
|
548
|
+
`shape-rendering="crispEdges">` +
|
|
549
|
+
`<rect width="${total}" height="${total}" fill="${light}"/>` +
|
|
550
|
+
`<path d="${path}" fill="${dark}"/>` +
|
|
551
|
+
`</svg>`
|
|
552
|
+
)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* 端末表示用の文字列をやめ、**画像**として書き出せる PNG を組み立てる。
|
|
557
|
+
*
|
|
558
|
+
* 端末の半角ブロックは行間(セルの隙間)が背景色で塗られず、カメラから見ると
|
|
559
|
+
* 暗い横縞が QR を横切るため読み取れない(実測: 80x24 の端末で別端末のスキャナも無反応)。
|
|
560
|
+
* モジュールを整数ピクセルで描いた PNG なら、画面でも印刷でも読める。
|
|
561
|
+
*
|
|
562
|
+
* 形式は 8bit グレースケール(colorType 0)。依存ゼロ(`node:zlib` のみ)。
|
|
563
|
+
* @param {string} text 内容
|
|
564
|
+
* @param {object} [opts] オプション
|
|
565
|
+
* @param {number} [opts.margin] 余白モジュール数(既定 4)
|
|
566
|
+
* @param {number} [opts.scale] 1 モジュールのピクセル数(既定 10)
|
|
567
|
+
* @returns {Buffer} PNG バイト列
|
|
568
|
+
*/
|
|
569
|
+
export function qrPng(text, { margin = 4, scale = 10 } = {}) {
|
|
570
|
+
const { size, modules } = qrMatrix(text)
|
|
571
|
+
const total = size + margin * 2
|
|
572
|
+
const w = total * scale
|
|
573
|
+
// 1 行 = フィルタ種別 1 バイト + グレースケール w バイト
|
|
574
|
+
const raw = Buffer.alloc((w + 1) * w, 0xff)
|
|
575
|
+
// フィルタ種別は 0(None)でなければならない(0xff のままだと不正な PNG になる)
|
|
576
|
+
for (let y = 0; y < w; y += 1) raw[y * (w + 1)] = 0x00
|
|
577
|
+
for (let my = 0; my < size; my += 1) {
|
|
578
|
+
for (let mx = 0; mx < size; mx += 1) {
|
|
579
|
+
if (!modules[my][mx]) continue
|
|
580
|
+
const x0 = (mx + margin) * scale
|
|
581
|
+
const y0 = (my + margin) * scale
|
|
582
|
+
for (let y = y0; y < y0 + scale; y += 1) {
|
|
583
|
+
const rowStart = y * (w + 1) + 1
|
|
584
|
+
raw.fill(0x00, rowStart + x0, rowStart + x0 + scale)
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
const ihdr = Buffer.alloc(13)
|
|
589
|
+
ihdr.writeUInt32BE(w, 0)
|
|
590
|
+
ihdr.writeUInt32BE(w, 4)
|
|
591
|
+
ihdr[8] = 8 // bit depth
|
|
592
|
+
ihdr[9] = 0 // grayscale
|
|
593
|
+
ihdr[10] = 0 // deflate
|
|
594
|
+
ihdr[11] = 0 // adaptive filter
|
|
595
|
+
ihdr[12] = 0 // no interlace
|
|
596
|
+
return Buffer.concat([
|
|
597
|
+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
598
|
+
pngChunk('IHDR', ihdr),
|
|
599
|
+
pngChunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
|
|
600
|
+
pngChunk('IEND', Buffer.alloc(0)),
|
|
601
|
+
])
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** CRC32(PNG のチャンク用)。 */
|
|
605
|
+
const CRC_TABLE = (() => {
|
|
606
|
+
const table = new Uint32Array(256)
|
|
607
|
+
for (let n = 0; n < 256; n += 1) {
|
|
608
|
+
let c = n
|
|
609
|
+
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
|
610
|
+
table[n] = c >>> 0
|
|
611
|
+
}
|
|
612
|
+
return table
|
|
613
|
+
})()
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* PNG のチャンク(長さ・種別・データ・CRC)。
|
|
617
|
+
* @param {string} type 種別(4 文字)
|
|
618
|
+
* @param {Buffer} data データ
|
|
619
|
+
* @returns {Buffer} チャンク
|
|
620
|
+
*/
|
|
621
|
+
function pngChunk(type, data) {
|
|
622
|
+
const len = Buffer.alloc(4)
|
|
623
|
+
len.writeUInt32BE(data.length, 0)
|
|
624
|
+
const body = Buffer.concat([Buffer.from(type, 'ascii'), data])
|
|
625
|
+
let crc = 0xffffffff
|
|
626
|
+
for (const byte of body) crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8)
|
|
627
|
+
const crcBuf = Buffer.alloc(4)
|
|
628
|
+
crcBuf.writeUInt32BE((crc ^ 0xffffffff) >>> 0, 0)
|
|
629
|
+
return Buffer.concat([len, body, crcBuf])
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* QR のモジュール行列を 0/1 の文字列配列にする(フロントエンドが canvas に
|
|
634
|
+
* 整数ピクセルで描くための形。SVG を CSS で縮小すると、モジュールが半端な
|
|
635
|
+
* ピクセルになりスキャナが反応しない)。
|
|
636
|
+
* @param {string} text 内容
|
|
637
|
+
* @returns {string[]} 1 行 1 文字列('1' = 暗)
|
|
638
|
+
*/
|
|
639
|
+
export function qrRows(text) {
|
|
640
|
+
const { modules } = qrMatrix(text)
|
|
641
|
+
return modules.map((row) => row.map((v) => (v ? '1' : '0')).join(''))
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* ターミナル表示用の文字列(ANSI 背景色つき半角ブロック)。
|
|
646
|
+
*
|
|
647
|
+
* 端末の前景/背景色に左右されないよう、各行を「白背景・黒前景」に固定して描く。
|
|
648
|
+
* これでダークテーマでもライトテーマでも、暗モジュールが暗く出る(QR の極性が崩れない)。
|
|
649
|
+
* @param {string} text 内容
|
|
650
|
+
* @param {object} [opts] オプション
|
|
651
|
+
* @param {number} [opts.margin] 余白モジュール数(既定 2。端末では 4 は広すぎる)
|
|
652
|
+
* @param {boolean} [opts.color] ANSI 色を使う(既定 true)
|
|
653
|
+
* @returns {string} 表示用文字列
|
|
654
|
+
*/
|
|
655
|
+
export function qrTerminal(text, { margin = 2, color = true } = {}) {
|
|
656
|
+
const { size, modules } = qrMatrix(text)
|
|
657
|
+
const total = size + margin * 2
|
|
658
|
+
const at = (x, y) => {
|
|
659
|
+
const mx = x - margin
|
|
660
|
+
const my = y - margin
|
|
661
|
+
if (mx < 0 || my < 0 || mx >= size || my >= size) return false
|
|
662
|
+
return modules[my][mx]
|
|
663
|
+
}
|
|
664
|
+
const reset = color ? '\x1b[0m' : ''
|
|
665
|
+
const paint = color ? '\x1b[30;47m' : ''
|
|
666
|
+
const lines = []
|
|
667
|
+
for (let y = 0; y < total; y += 2) {
|
|
668
|
+
let line = ''
|
|
669
|
+
for (let x = 0; x < total; x++) {
|
|
670
|
+
const top = at(x, y)
|
|
671
|
+
const bottom = y + 1 < total ? at(x, y + 1) : false
|
|
672
|
+
// 前景=黒・背景=白なので、ブロック(█)が暗モジュールになる
|
|
673
|
+
const ch = top && bottom ? '█' : top ? '▀' : bottom ? '▄' : ' '
|
|
674
|
+
line += color ? ch : ch === ' ' ? ' ' : ch === '█' ? '██' : ch === '▀' ? '▀▀' : '▄▄'
|
|
675
|
+
}
|
|
676
|
+
lines.push(`${paint}${line}${reset}`)
|
|
677
|
+
}
|
|
678
|
+
return lines.join('\n')
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* XML 用のエスケープ。
|
|
683
|
+
* @param {string} s 文字列
|
|
684
|
+
* @returns {string} エスケープ済み
|
|
685
|
+
*/
|
|
686
|
+
function escapeXml(s) {
|
|
687
|
+
return String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c])
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// --- 接続先候補の検出 -------------------------------------------------------
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* LAN の IPv4 を 1 つ選ぶ。
|
|
694
|
+
*
|
|
695
|
+
* 優先順は ①家用の `192.168.*` ②その他のプライベート/グローバル IPv4 ③Tailscale の CGNAT
|
|
696
|
+
* (`100.64.0.0/10`)。loopback・link-local (`169.254.*`) は常に除外する。
|
|
697
|
+
* @param {Record<string, Array<{address: string, family: string|number, internal: boolean}>>} [ifaces] `os.networkInterfaces()` の戻り
|
|
698
|
+
* @returns {string|null} IP または null
|
|
699
|
+
*/
|
|
700
|
+
export function detectLanIp(ifaces = os.networkInterfaces()) {
|
|
701
|
+
const ranked = []
|
|
702
|
+
for (const list of Object.values(ifaces ?? {})) {
|
|
703
|
+
for (const addr of list ?? []) {
|
|
704
|
+
const family = typeof addr.family === 'string' ? addr.family : addr.family === 4 ? 'IPv4' : 'IPv6'
|
|
705
|
+
if (family !== 'IPv4' || addr.internal) continue
|
|
706
|
+
if (addr.address.startsWith('169.254.')) continue
|
|
707
|
+
const [a, b] = addr.address.split('.').map(Number)
|
|
708
|
+
const cgnat = a === 100 && b >= 64 && b <= 127
|
|
709
|
+
const rank = cgnat ? 2 : addr.address.startsWith('192.168.') ? 0 : 1
|
|
710
|
+
ranked.push({ rank, ip: addr.address })
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
ranked.sort((x, y) => x.rank - y.rank)
|
|
714
|
+
return ranked[0]?.ip ?? null
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Tailscale CLI の候補(PATH に無い macOS アプリ版のバイナリも見る)。
|
|
719
|
+
*
|
|
720
|
+
* 常駐プロセス(launchd / systemd)からアプリ版バイナリを叩くと
|
|
721
|
+
* 「The Tailscale GUI failed to start」を返す環境があるため、候補を広げつつ
|
|
722
|
+
* `HP_TAILSCALE_BIN` で明示指定できるようにする(読み取れなければ LAN 候補に落ちる)。
|
|
723
|
+
* @param {string} [platform] `process.platform`
|
|
724
|
+
* @param {object} [opts] 差し替え
|
|
725
|
+
* @param {Record<string, string|undefined>} [opts.env] 環境変数
|
|
726
|
+
* @returns {string[]} 候補コマンド
|
|
727
|
+
*/
|
|
728
|
+
export function tailscaleCommandCandidates(platform = process.platform, { env = process.env } = {}) {
|
|
729
|
+
const out = []
|
|
730
|
+
const override = String(env?.HP_TAILSCALE_BIN ?? '').trim()
|
|
731
|
+
if (override) out.push(override)
|
|
732
|
+
out.push('tailscale', '/opt/homebrew/bin/tailscale', '/usr/local/bin/tailscale')
|
|
733
|
+
if (platform === 'darwin') out.push('/Applications/Tailscale.app/Contents/MacOS/Tailscale')
|
|
734
|
+
return [...new Set(out)]
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* `tailscale status --json` の出力から MagicDNS 名を取り出す。
|
|
739
|
+
* @param {string|object} status JSON 文字列またはオブジェクト
|
|
740
|
+
* @returns {string|null} `mac-mini.tail1234.ts.net` 形式(末尾ドット無し)または null
|
|
741
|
+
*/
|
|
742
|
+
export function parseTailscaleDnsName(status) {
|
|
743
|
+
let json = status
|
|
744
|
+
if (typeof status === 'string') {
|
|
745
|
+
try {
|
|
746
|
+
json = JSON.parse(status)
|
|
747
|
+
} catch {
|
|
748
|
+
return null
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
const name = json?.Self?.DNSName
|
|
752
|
+
if (typeof name !== 'string' || !name.trim()) return null
|
|
753
|
+
return name.trim().replace(/\.$/, '')
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* ペアリング URL の候補を組み立てる(Tailscale → LAN → localhost の順)。
|
|
758
|
+
* @param {object} opts 入力
|
|
759
|
+
* @param {string} opts.token トークン
|
|
760
|
+
* @param {number} [opts.port] ブリッジのポート
|
|
761
|
+
* @param {string|null} [opts.dnsName] Tailscale MagicDNS 名
|
|
762
|
+
* @param {string|null} [opts.lanIp] LAN IP
|
|
763
|
+
* @param {number} [opts.tlsPort] Tailscale serve の HTTPS ポート(未指定なら 443 相当)
|
|
764
|
+
* @returns {Array<{kind: string, url: string, label: string}>} 候補
|
|
765
|
+
*/
|
|
766
|
+
export function pairingTargets({ token, port = 4097, dnsName = null, lanIp = null, tlsPort = null } = {}) {
|
|
767
|
+
const q = `?token=${encodeURIComponent(String(token ?? ''))}`
|
|
768
|
+
const out = []
|
|
769
|
+
if (dnsName) {
|
|
770
|
+
const suffix = tlsPort && Number(tlsPort) !== 443 ? `:${tlsPort}` : ''
|
|
771
|
+
out.push({ kind: 'tailscale', url: `https://${dnsName}${suffix}/${q}`, label: 'Tailscale (HTTPS)' })
|
|
772
|
+
}
|
|
773
|
+
if (lanIp) out.push({ kind: 'lan', url: `http://${lanIp}:${port}/${q}`, label: 'LAN' })
|
|
774
|
+
out.push({ kind: 'local', url: `http://127.0.0.1:${port}/${q}`, label: 'This machine' })
|
|
775
|
+
return out
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* 既定で QR に焼く 1 本を選ぶ(Tailscale があればそれ、無ければ LAN)。
|
|
780
|
+
* @param {Array<{kind: string, url: string}>} targets 候補
|
|
781
|
+
* @returns {{kind: string, url: string}|null} 先頭候補
|
|
782
|
+
*/
|
|
783
|
+
export function bestTarget(targets) {
|
|
784
|
+
return Array.isArray(targets) && targets.length ? targets[0] : null
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* 候補検出の材料をまとめて 1 つの応答にする(サーバと CLI の共通経路)。
|
|
789
|
+
* @param {object} opts 入力
|
|
790
|
+
* @param {string} opts.token トークン
|
|
791
|
+
* @param {number} [opts.port] ブリッジのポート
|
|
792
|
+
* @param {string|null} [opts.tailscaleJson] `tailscale status --json` の生出力
|
|
793
|
+
* @param {string|null} [opts.dnsName] MagicDNS 名の明示指定(CLI を叩けない環境の控え)
|
|
794
|
+
* @param {string|null} [opts.lanIp] LAN IP(未指定なら自動検出)
|
|
795
|
+
* @param {number|null} [opts.tlsPort] Tailscale serve の HTTPS ポート
|
|
796
|
+
* @param {Record<string, any>} [opts.ifaces] `os.networkInterfaces()` の差し替え
|
|
797
|
+
* @returns {{targets: Array<{kind: string, url: string, label: string}>, best: {kind: string, url: string, label: string}|null, dnsName: string|null, lanIp: string|null}} 結果
|
|
798
|
+
*/
|
|
799
|
+
export function collectPairing({ token, port = 4097, tailscaleJson = null, dnsName: explicitDns = null, lanIp, tlsPort = null, ifaces } = {}) {
|
|
800
|
+
// CLI の応答が取れない常駐環境(launchd からアプリ版 Tailscale を叩けない等)では
|
|
801
|
+
// 設定ファイルの `tailscaleDns` を控えとして使う。どちらも無ければ LAN 候補だけ。
|
|
802
|
+
const fromJson = tailscaleJson ? parseTailscaleDnsName(tailscaleJson) : null
|
|
803
|
+
const fallback = typeof explicitDns === 'string' ? explicitDns.trim().replace(/\.$/, '') : ''
|
|
804
|
+
const dnsName = fromJson || fallback || null
|
|
805
|
+
const resolvedLan = lanIp ?? detectLanIp(ifaces ?? os.networkInterfaces())
|
|
806
|
+
const targets = pairingTargets({ token, port, dnsName, lanIp: resolvedLan, tlsPort })
|
|
807
|
+
return { targets, best: bestTarget(targets), dnsName, lanIp: resolvedLan }
|
|
808
|
+
}
|